blob: 18a768d9e5bb41db3e670346c5a1a7c7efe44c12 [file] [log] [blame]
Jiyong Park40699612021-05-24 16:55:06 +09001/*
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 Stokes52d3c722022-10-04 17:27:13 +010017#include <android/dlext.h>
Jiyong Park40699612021-05-24 16:55:06 +090018#include <dlfcn.h>
19
20#include <cstdlib>
21#include <iostream>
Jiyong Parkfe5b28e2021-06-24 00:19:02 +090022#include <string>
23
Alan Stokes52d3c722022-10-04 17:27:13 +010024#include "vm_main.h"
Jiyong Parkfe5b28e2021-06-24 00:19:02 +090025
26extern "C" {
27enum {
28 ANDROID_NAMESPACE_TYPE_REGULAR = 0,
29 ANDROID_NAMESPACE_TYPE_ISOLATED = 1,
30 ANDROID_NAMESPACE_TYPE_SHARED = 2,
31};
32
33extern 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
39static void* load(const std::string& libname);
Jiyong Park40699612021-05-24 16:55:06 +090040
Alan Stokes52d3c722022-10-04 17:27:13 +010041constexpr char entrypoint_name[] = "AVmPayload_main";
42
Jiyong Park40699612021-05-24 16:55:06 +090043int main(int argc, char* argv[]) {
Alan Stokes52d3c722022-10-04 17:27:13 +010044 if (argc != 2) {
Jiyong Park40699612021-05-24 16:55:06 +090045 std::cout << "Usage:\n";
Alan Stokes52d3c722022-10-04 17:27:13 +010046 std::cout << " " << argv[0] << " LIBNAME\n";
Jiyong Park40699612021-05-24 16:55:06 +090047 return EXIT_FAILURE;
48 }
49
50 const char* libname = argv[1];
Jiyong Parkfe5b28e2021-06-24 00:19:02 +090051 void* handle = load(libname);
Jiyong Park40699612021-05-24 16:55:06 +090052 if (handle == nullptr) {
53 std::cerr << "Failed to load " << libname << ": " << dlerror() << "\n";
54 return EXIT_FAILURE;
55 }
56
Alan Stokes52d3c722022-10-04 17:27:13 +010057 AVmPayload_main_t* entry = reinterpret_cast<decltype(entry)>(dlsym(handle, entrypoint_name));
Jiyong Park40699612021-05-24 16:55:06 +090058 if (entry == nullptr) {
Alan Stokes52d3c722022-10-04 17:27:13 +010059 std::cerr << "Failed to find entrypoint `" << entrypoint_name << "`: " << dlerror() << "\n";
Jiyong Park40699612021-05-24 16:55:06 +090060 return EXIT_FAILURE;
61 }
62
Alan Stokes52d3c722022-10-04 17:27:13 +010063 return entry();
Jiyong Park40699612021-05-24 16:55:06 +090064}
Jiyong Parkfe5b28e2021-06-24 00:19:02 +090065
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.
69void* 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}