• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2020 The Android Open Source Project
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  *  * Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  *  * Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in
12  *    the documentation and/or other materials provided with the
13  *    distribution.
14  *
15  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
16  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
17  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
18  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
19  * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
20  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
21  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
22  * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
23  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
24  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
25  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26  * SUCH DAMAGE.
27  */
28 
29 #include <android/dlext.h>
30 #include <dlfcn.h>
31 #include <stdlib.h>
32 
33 #include <string>
34 
35 #include "../core_shared_libs.h"
36 #include "../dlext_private.h"
37 
38 extern "C" void global_function();
39 extern "C" void internal_function();
40 
main(int argc,char * argv[])41 int main(int argc, char* argv[]) {
42   if (argc != 2) {
43     fprintf(stderr, "usage: %s NS_PATH\n", argv[0]);
44     fprintf(stderr, "NS_PATH   path to the ns_hidden_child_app directory\n");
45     exit(1);
46   }
47 
48   // Ensure that -Wl,--needed doesn't break the test by removing DT_NEEDED entries.
49   global_function();
50   internal_function();
51 
52   const char* app_lib_dir = argv[1];
53   android_namespace_t* app_ns =
54       android_create_namespace("app", nullptr, app_lib_dir, ANDROID_NAMESPACE_TYPE_ISOLATED,
55                                nullptr, nullptr);
56   if (app_ns == nullptr) {
57     fprintf(stderr, "android_create_namespace failed: %s\n", dlerror());
58     exit(1);
59   }
60 
61   std::string public_libs = std::string(kCoreSharedLibs) + ":libns_hidden_child_public.so";
62   if (!android_link_namespaces(app_ns, nullptr, public_libs.c_str())) {
63     fprintf(stderr, "android_link_namespaces failed: %s\n", dlerror());
64     exit(1);
65   }
66 
67   android_dlextinfo ext = {
68     .flags = ANDROID_DLEXT_USE_NAMESPACE,
69     .library_namespace = app_ns,
70   };
71   void* app_lib = android_dlopen_ext("libns_hidden_child_app.so", RTLD_NOW | RTLD_LOCAL, &ext);
72   if (app_lib == nullptr) {
73     fprintf(stderr, "android_dlopen_ext failed: %s\n", dlerror());
74     exit(1);
75   }
76 
77   auto app_function = reinterpret_cast<void(*)()>(dlsym(app_lib, "app_function"));
78   if (app_function == nullptr) {
79     fprintf(stderr, "dlsym failed to find app_function: %s\n", dlerror());
80     exit(1);
81   }
82 
83   app_function();
84   return 0;
85 }
86