Jiyong Park | 4069961 | 2021-05-24 16:55:06 +0900 | [diff] [blame] | 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 | |
Alan Stokes | 52d3c72 | 2022-10-04 17:27:13 +0100 | [diff] [blame^] | 17 | #include <android/dlext.h> |
Jiyong Park | 4069961 | 2021-05-24 16:55:06 +0900 | [diff] [blame] | 18 | #include <dlfcn.h> |
| 19 | |
| 20 | #include <cstdlib> |
| 21 | #include <iostream> |
Jiyong Park | fe5b28e | 2021-06-24 00:19:02 +0900 | [diff] [blame] | 22 | #include <string> |
| 23 | |
Alan Stokes | 52d3c72 | 2022-10-04 17:27:13 +0100 | [diff] [blame^] | 24 | #include "vm_main.h" |
Jiyong Park | fe5b28e | 2021-06-24 00:19:02 +0900 | [diff] [blame] | 25 | |
| 26 | extern "C" { |
| 27 | enum { |
| 28 | ANDROID_NAMESPACE_TYPE_REGULAR = 0, |
| 29 | ANDROID_NAMESPACE_TYPE_ISOLATED = 1, |
| 30 | ANDROID_NAMESPACE_TYPE_SHARED = 2, |
| 31 | }; |
| 32 | |
| 33 | extern struct android_namespace_t* android_create_namespace( |
| 34 | const char* name, const char* ld_library_path, const char* default_library_path, |
| 35 | uint64_t type, const char* permitted_when_isolated_path, |
| 36 | struct android_namespace_t* parent); |
| 37 | } // extern "C" |
| 38 | |
| 39 | static void* load(const std::string& libname); |
Jiyong Park | 4069961 | 2021-05-24 16:55:06 +0900 | [diff] [blame] | 40 | |
Alan Stokes | 52d3c72 | 2022-10-04 17:27:13 +0100 | [diff] [blame^] | 41 | constexpr char entrypoint_name[] = "AVmPayload_main"; |
| 42 | |
Jiyong Park | 4069961 | 2021-05-24 16:55:06 +0900 | [diff] [blame] | 43 | int main(int argc, char* argv[]) { |
Alan Stokes | 52d3c72 | 2022-10-04 17:27:13 +0100 | [diff] [blame^] | 44 | if (argc != 2) { |
Jiyong Park | 4069961 | 2021-05-24 16:55:06 +0900 | [diff] [blame] | 45 | std::cout << "Usage:\n"; |
Alan Stokes | 52d3c72 | 2022-10-04 17:27:13 +0100 | [diff] [blame^] | 46 | std::cout << " " << argv[0] << " LIBNAME\n"; |
Jiyong Park | 4069961 | 2021-05-24 16:55:06 +0900 | [diff] [blame] | 47 | return EXIT_FAILURE; |
| 48 | } |
| 49 | |
| 50 | const char* libname = argv[1]; |
Jiyong Park | fe5b28e | 2021-06-24 00:19:02 +0900 | [diff] [blame] | 51 | void* handle = load(libname); |
Jiyong Park | 4069961 | 2021-05-24 16:55:06 +0900 | [diff] [blame] | 52 | if (handle == nullptr) { |
| 53 | std::cerr << "Failed to load " << libname << ": " << dlerror() << "\n"; |
| 54 | return EXIT_FAILURE; |
| 55 | } |
| 56 | |
Alan Stokes | 52d3c72 | 2022-10-04 17:27:13 +0100 | [diff] [blame^] | 57 | AVmPayload_main_t* entry = reinterpret_cast<decltype(entry)>(dlsym(handle, entrypoint_name)); |
Jiyong Park | 4069961 | 2021-05-24 16:55:06 +0900 | [diff] [blame] | 58 | if (entry == nullptr) { |
Alan Stokes | 52d3c72 | 2022-10-04 17:27:13 +0100 | [diff] [blame^] | 59 | std::cerr << "Failed to find entrypoint `" << entrypoint_name << "`: " << dlerror() << "\n"; |
Jiyong Park | 4069961 | 2021-05-24 16:55:06 +0900 | [diff] [blame] | 60 | return EXIT_FAILURE; |
| 61 | } |
| 62 | |
Alan Stokes | 52d3c72 | 2022-10-04 17:27:13 +0100 | [diff] [blame^] | 63 | return entry(); |
Jiyong Park | 4069961 | 2021-05-24 16:55:06 +0900 | [diff] [blame] | 64 | } |
Jiyong Park | fe5b28e | 2021-06-24 00:19:02 +0900 | [diff] [blame] | 65 | |
| 66 | // Create a new linker namespace whose search path is set to the directory of the library. Then |
| 67 | // load it from there. Returns the handle to the loaded library if successful. Returns nullptr |
| 68 | // if failed. |
| 69 | void* load(const std::string& libname) { |
| 70 | // Parent as nullptr means the default namespace |
| 71 | android_namespace_t* parent = nullptr; |
| 72 | // The search paths of the new namespace are inherited from the parent namespace. |
| 73 | const uint64_t type = ANDROID_NAMESPACE_TYPE_SHARED; |
| 74 | // The directory of the library is appended to the search paths |
| 75 | const std::string libdir = libname.substr(0, libname.find_last_of("/")); |
| 76 | const char* ld_library_path = libdir.c_str(); |
| 77 | const char* default_library_path = libdir.c_str(); |
| 78 | |
| 79 | android_namespace_t* new_ns = nullptr; |
| 80 | new_ns = android_create_namespace("microdroid_app", ld_library_path, default_library_path, type, |
| 81 | /* permitted_when_isolated_path */ nullptr, parent); |
| 82 | if (new_ns == nullptr) { |
| 83 | std::cerr << "Failed to create linker namespace: " << dlerror() << "\n"; |
| 84 | return nullptr; |
| 85 | } |
| 86 | |
| 87 | const android_dlextinfo info = { |
| 88 | .flags = ANDROID_DLEXT_USE_NAMESPACE, |
| 89 | .library_namespace = new_ns, |
| 90 | }; |
| 91 | return android_dlopen_ext(libname.c_str(), RTLD_NOW, &info); |
| 92 | } |