• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2021 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #include <dlfcn.h>
18 
19 #include <cstdlib>
20 #include <iostream>
21 #include <string>
22 
23 #include <android/dlext.h>
24 
25 extern "C" {
26 enum {
27     ANDROID_NAMESPACE_TYPE_REGULAR = 0,
28     ANDROID_NAMESPACE_TYPE_ISOLATED = 1,
29     ANDROID_NAMESPACE_TYPE_SHARED = 2,
30 };
31 
32 extern struct android_namespace_t* android_create_namespace(
33         const char* name, const char* ld_library_path, const char* default_library_path,
34         uint64_t type, const char* permitted_when_isolated_path,
35         struct android_namespace_t* parent);
36 } // extern "C"
37 
38 static void* load(const std::string& libname);
39 
main(int argc,char * argv[])40 int main(int argc, char* argv[]) {
41     if (argc < 2) {
42         std::cout << "Usage:\n";
43         std::cout << "    " << argv[0] << " LIBNAME [ARGS...]\n";
44         return EXIT_FAILURE;
45     }
46 
47     const char* libname = argv[1];
48     void* handle = load(libname);
49     if (handle == nullptr) {
50         std::cerr << "Failed to load " << libname << ": " << dlerror() << "\n";
51         return EXIT_FAILURE;
52     }
53 
54     int (*entry)(int argc, char* argv[]) = nullptr;
55     entry = reinterpret_cast<decltype(entry)>(dlsym(handle, "android_native_main"));
56     if (entry == nullptr) {
57         std::cerr << "Failed to find entrypoint `android_native_main`: " << dlerror() << "\n";
58         return EXIT_FAILURE;
59     }
60 
61     return entry(argc - 1, argv + 1);
62 }
63 
64 // Create a new linker namespace whose search path is set to the directory of the library. Then
65 // load it from there. Returns the handle to the loaded library if successful. Returns nullptr
66 // if failed.
load(const std::string & libname)67 void* load(const std::string& libname) {
68     // Parent as nullptr means the default namespace
69     android_namespace_t* parent = nullptr;
70     // The search paths of the new namespace are inherited from the parent namespace.
71     const uint64_t type = ANDROID_NAMESPACE_TYPE_SHARED;
72     // The directory of the library is appended to the search paths
73     const std::string libdir = libname.substr(0, libname.find_last_of("/"));
74     const char* ld_library_path = libdir.c_str();
75     const char* default_library_path = libdir.c_str();
76 
77     android_namespace_t* new_ns = nullptr;
78     new_ns = android_create_namespace("microdroid_app", ld_library_path, default_library_path, type,
79                                       /* permitted_when_isolated_path */ nullptr, parent);
80     if (new_ns == nullptr) {
81         std::cerr << "Failed to create linker namespace: " << dlerror() << "\n";
82         return nullptr;
83     }
84 
85     const android_dlextinfo info = {
86             .flags = ANDROID_DLEXT_USE_NAMESPACE,
87             .library_namespace = new_ns,
88     };
89     return android_dlopen_ext(libname.c_str(), RTLD_NOW, &info);
90 }
91