blob: d6304a916062f01216e8c8e87f780a7ab0975d74 [file] [log] [blame]
Dimitry Ivanovac1b1912015-12-01 13:56:44 -08001/*
2 * Copyright (C) 2015 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 "nativeloader/native_loader.h"
Jiyong Park6291da22019-04-26 18:55:48 +090018#define LOG_TAG "nativeloader"
Dimitry Ivanovac1b1912015-12-01 13:56:44 -080019
20#include <dlfcn.h>
Jiyong Parkd1006fe2017-11-08 18:44:09 +090021#include <sys/types.h>
Dimitry Ivanovac1b1912015-12-01 13:56:44 -080022
Jiyong Parkd1006fe2017-11-08 18:44:09 +090023#include <memory>
Dimitry Ivanovac1b1912015-12-01 13:56:44 -080024#include <mutex>
Jiyong Parkd1006fe2017-11-08 18:44:09 +090025#include <string>
26#include <vector>
Dimitry Ivanovac1b1912015-12-01 13:56:44 -080027
Jiyong Park6291da22019-04-26 18:55:48 +090028#include "android-base/file.h"
29#include "android-base/macros.h"
30#include "android-base/strings.h"
31#ifdef __ANDROID__
32#include "library_namespaces.h"
33#include "log/log.h"
34#include "nativeloader/dlext_namespaces.h"
Justin Yun4a1d1102017-11-27 17:04:14 +090035#endif
Jiyong Park6291da22019-04-26 18:55:48 +090036#include "nativebridge/native_bridge.h"
37#include "nativehelper/ScopedUtfChars.h"
Inseob Kim67cb0562018-05-04 11:39:12 +090038
Dimitry Ivanovac1b1912015-12-01 13:56:44 -080039namespace android {
40
Jiyong Park6291da22019-04-26 18:55:48 +090041namespace {
Dimitry Ivanov4b0e9632016-03-15 13:51:26 -070042#if defined(__ANDROID__)
Jiyong Park40a60772019-05-03 16:21:31 +090043using android::nativeloader::LibraryNamespaces;
44
Jiyong Park6291da22019-04-26 18:55:48 +090045constexpr const char* kApexPath = "/apex/";
Zhenhua WANGf2804e52016-05-30 11:16:08 +080046
Jiyong Park6291da22019-04-26 18:55:48 +090047std::mutex g_namespaces_mutex;
48LibraryNamespaces* g_namespaces = new LibraryNamespaces;
Zhenhua WANGf2804e52016-05-30 11:16:08 +080049
Jiyong Park6291da22019-04-26 18:55:48 +090050android_namespace_t* FindExportedNamespace(const char* caller_location) {
51 std::string location = caller_location;
52 // Lots of implicit assumptions here: we expect `caller_location` to be of the form:
53 // /apex/com.android...modulename/...
54 //
55 // And we extract from it 'modulename', which is the name of the linker namespace.
56 if (android::base::StartsWith(location, kApexPath)) {
57 size_t slash_index = location.find_first_of('/', strlen(kApexPath));
58 LOG_ALWAYS_FATAL_IF((slash_index == std::string::npos),
59 "Error finding namespace of apex: no slash in path %s", caller_location);
60 size_t dot_index = location.find_last_of('.', slash_index);
61 LOG_ALWAYS_FATAL_IF((dot_index == std::string::npos),
62 "Error finding namespace of apex: no dot in apex name %s", caller_location);
63 std::string name = location.substr(dot_index + 1, slash_index - dot_index - 1);
64 android_namespace_t* boot_namespace = android_get_exported_namespace(name.c_str());
65 LOG_ALWAYS_FATAL_IF((boot_namespace == nullptr),
66 "Error finding namespace of apex: no namespace called %s", name.c_str());
67 return boot_namespace;
Zhenhua WANGf2804e52016-05-30 11:16:08 +080068 }
Jiyong Park6291da22019-04-26 18:55:48 +090069 return nullptr;
Dimitry Ivanov7d028292016-05-05 17:30:24 -070070}
Jiyong Park6291da22019-04-26 18:55:48 +090071#endif // #if defined(__ANDROID__)
72} // namespace
Dimitry Ivanovac1b1912015-12-01 13:56:44 -080073
Dimitry Ivanov4b0e9632016-03-15 13:51:26 -070074void InitializeNativeLoader() {
Dimitry Ivanov426799d2016-02-22 11:27:48 -080075#if defined(__ANDROID__)
Dimitry Ivanov34d5a202016-02-29 13:21:43 -080076 std::lock_guard<std::mutex> guard(g_namespaces_mutex);
Dimitry Ivanov4b0e9632016-03-15 13:51:26 -070077 g_namespaces->Initialize();
Dimitry Ivanov426799d2016-02-22 11:27:48 -080078#endif
79}
80
Dimitry Ivanovbe4ca3a2016-05-02 10:43:16 -070081void ResetNativeLoader() {
82#if defined(__ANDROID__)
83 std::lock_guard<std::mutex> guard(g_namespaces_mutex);
84 g_namespaces->Reset();
85#endif
86}
Dimitry Ivanovac1b1912015-12-01 13:56:44 -080087
Kiyoung Kim4639f692019-02-20 18:04:39 +090088jstring CreateClassLoaderNamespace(JNIEnv* env, int32_t target_sdk_version, jobject class_loader,
89 bool is_shared, jstring dex_path, jstring library_path,
Dimitry Ivanovd047c922016-02-23 14:23:51 -080090 jstring permitted_path) {
Dimitry Ivanovac1b1912015-12-01 13:56:44 -080091#if defined(__ANDROID__)
Dimitry Ivanov34d5a202016-02-29 13:21:43 -080092 std::lock_guard<std::mutex> guard(g_namespaces_mutex);
Zhenhua WANGf2804e52016-05-30 11:16:08 +080093
94 std::string error_msg;
Kiyoung Kim4639f692019-02-20 18:04:39 +090095 bool success = g_namespaces->Create(env, target_sdk_version, class_loader, is_shared, dex_path,
96 library_path, permitted_path, &error_msg) != nullptr;
Zhenhua WANGf2804e52016-05-30 11:16:08 +080097 if (!success) {
98 return env->NewStringUTF(error_msg.c_str());
Dimitry Ivanovd047c922016-02-23 14:23:51 -080099 }
100#else
Kiyoung Kim4639f692019-02-20 18:04:39 +0900101 UNUSED(env, target_sdk_version, class_loader, is_shared, dex_path, library_path, permitted_path);
Dimitry Ivanovd047c922016-02-23 14:23:51 -0800102#endif
103 return nullptr;
104}
105
Nicolas Geoffrayc3a73dc2019-01-12 15:01:20 +0000106void* OpenNativeLibrary(JNIEnv* env, int32_t target_sdk_version, const char* path,
Nicolas Geoffray53535022019-01-18 10:05:13 +0000107 jobject class_loader, const char* caller_location, jstring library_path,
108 bool* needs_native_bridge, char** error_msg) {
Dimitry Ivanovd047c922016-02-23 14:23:51 -0800109#if defined(__ANDROID__)
Dimitry Ivanov5539db02016-04-20 16:07:30 -0700110 UNUSED(target_sdk_version);
111 if (class_loader == nullptr) {
Zhenhua WANGf2804e52016-05-30 11:16:08 +0800112 *needs_native_bridge = false;
Nicolas Geoffray890e3bf2019-01-22 09:11:57 +0000113 if (caller_location != nullptr) {
114 android_namespace_t* boot_namespace = FindExportedNamespace(caller_location);
115 if (boot_namespace != nullptr) {
116 const android_dlextinfo dlextinfo = {
117 .flags = ANDROID_DLEXT_USE_NAMESPACE,
118 .library_namespace = boot_namespace,
119 };
120 void* handle = android_dlopen_ext(path, RTLD_NOW, &dlextinfo);
121 if (handle == nullptr) {
122 *error_msg = strdup(dlerror());
123 }
124 return handle;
125 }
126 }
Pete Bentley632f1422018-12-19 13:33:33 +0000127 void* handle = dlopen(path, RTLD_NOW);
128 if (handle == nullptr) {
Nicolas Geoffrayd06cb942019-01-16 20:20:27 +0000129 *error_msg = strdup(dlerror());
Pete Bentley632f1422018-12-19 13:33:33 +0000130 }
131 return handle;
Dimitry Ivanovac1b1912015-12-01 13:56:44 -0800132 }
133
Dimitry Ivanov34d5a202016-02-29 13:21:43 -0800134 std::lock_guard<std::mutex> guard(g_namespaces_mutex);
Victor Khimenko1443ec42018-07-09 17:01:22 +0200135 NativeLoaderNamespace* ns;
Dimitry Ivanovac1b1912015-12-01 13:56:44 -0800136
Victor Khimenko1443ec42018-07-09 17:01:22 +0200137 if ((ns = g_namespaces->FindNamespaceByClassLoader(env, class_loader)) == nullptr) {
Dimitry Ivanovd047c922016-02-23 14:23:51 -0800138 // This is the case where the classloader was not created by ApplicationLoaders
139 // In this case we create an isolated not-shared namespace for it.
Nicolas Geoffrayc3a73dc2019-01-12 15:01:20 +0000140 std::string create_error_msg;
141 if ((ns = g_namespaces->Create(env, target_sdk_version, class_loader, false /* is_shared */,
Kiyoung Kim4639f692019-02-20 18:04:39 +0900142 nullptr, library_path, nullptr, &create_error_msg)) == nullptr) {
Nicolas Geoffrayc3a73dc2019-01-12 15:01:20 +0000143 *error_msg = strdup(create_error_msg.c_str());
Dimitry Ivanovd047c922016-02-23 14:23:51 -0800144 return nullptr;
145 }
Dimitry Ivanovac1b1912015-12-01 13:56:44 -0800146 }
147
Nicolas Geoffrayc3a73dc2019-01-12 15:01:20 +0000148 return OpenNativeLibraryInNamespace(ns, path, needs_native_bridge, error_msg);
Dimitry Ivanovac1b1912015-12-01 13:56:44 -0800149#else
Nicolas Geoffray53535022019-01-18 10:05:13 +0000150 UNUSED(env, target_sdk_version, class_loader, caller_location);
Andreas Gampe5c7d5822017-12-28 19:08:13 -0800151
152 // Do some best effort to emulate library-path support. It will not
153 // work for dependencies.
154 //
155 // Note: null has a special meaning and must be preserved.
156 std::string c_library_path; // Empty string by default.
157 if (library_path != nullptr && path != nullptr && path[0] != '/') {
158 ScopedUtfChars library_path_utf_chars(env, library_path);
159 c_library_path = library_path_utf_chars.c_str();
160 }
161
162 std::vector<std::string> library_paths = base::Split(c_library_path, ":");
163
164 for (const std::string& lib_path : library_paths) {
165 *needs_native_bridge = false;
166 const char* path_arg;
167 std::string complete_path;
168 if (path == nullptr) {
169 // Preserve null.
170 path_arg = nullptr;
Zhenhua WANGf2804e52016-05-30 11:16:08 +0800171 } else {
Andreas Gampe5c7d5822017-12-28 19:08:13 -0800172 complete_path = lib_path;
173 if (!complete_path.empty()) {
174 complete_path.append("/");
175 }
176 complete_path.append(path);
177 path_arg = complete_path.c_str();
178 }
179 void* handle = dlopen(path_arg, RTLD_NOW);
180 if (handle != nullptr) {
181 return handle;
182 }
183 if (NativeBridgeIsSupported(path_arg)) {
184 *needs_native_bridge = true;
185 handle = NativeBridgeLoadLibrary(path_arg, RTLD_NOW);
186 if (handle != nullptr) {
187 return handle;
188 }
Nicolas Geoffrayc3a73dc2019-01-12 15:01:20 +0000189 *error_msg = strdup(NativeBridgeGetError());
Andreas Gampe5c7d5822017-12-28 19:08:13 -0800190 } else {
Nicolas Geoffrayc3a73dc2019-01-12 15:01:20 +0000191 *error_msg = strdup(dlerror());
Zhenhua WANGf2804e52016-05-30 11:16:08 +0800192 }
193 }
Andreas Gampe5c7d5822017-12-28 19:08:13 -0800194 return nullptr;
Dimitry Ivanovac1b1912015-12-01 13:56:44 -0800195#endif
196}
197
Nicolas Geoffrayc3a73dc2019-01-12 15:01:20 +0000198bool CloseNativeLibrary(void* handle, const bool needs_native_bridge, char** error_msg) {
dimitry3150f7c2018-09-12 01:09:19 +0200199 bool success;
200 if (needs_native_bridge) {
201 success = (NativeBridgeUnloadLibrary(handle) == 0);
202 if (!success) {
Nicolas Geoffrayc3a73dc2019-01-12 15:01:20 +0000203 *error_msg = strdup(NativeBridgeGetError());
dimitry3150f7c2018-09-12 01:09:19 +0200204 }
205 } else {
206 success = (dlclose(handle) == 0);
207 if (!success) {
Nicolas Geoffrayc3a73dc2019-01-12 15:01:20 +0000208 *error_msg = strdup(dlerror());
dimitry3150f7c2018-09-12 01:09:19 +0200209 }
210 }
211
212 return success;
Dimitry Ivanov09a516b2016-05-03 14:55:25 -0700213}
214
Nicolas Geoffrayc3a73dc2019-01-12 15:01:20 +0000215void NativeLoaderFreeErrorMessage(char* msg) {
216 // The error messages get allocated through strdup, so we must call free on them.
217 free(msg);
218}
219
Dimitry Ivanovf44ecde2016-02-22 13:48:22 -0800220#if defined(__ANDROID__)
Nicolas Geoffrayc3a73dc2019-01-12 15:01:20 +0000221void* OpenNativeLibraryInNamespace(NativeLoaderNamespace* ns, const char* path,
222 bool* needs_native_bridge, char** error_msg) {
Jiyong Park85377812019-05-04 00:30:23 +0900223 void* handle = ns->Load(path);
224 if (handle == nullptr) {
225 *error_msg = ns->GetError();
Victor Khimenko1443ec42018-07-09 17:01:22 +0200226 }
Jiyong Park85377812019-05-04 00:30:23 +0900227 *needs_native_bridge = ns->IsBridged();
228 return handle;
Victor Khimenko1443ec42018-07-09 17:01:22 +0200229}
230
Dimitry Ivanov800083d2016-11-01 14:17:00 -0700231// native_bridge_namespaces are not supported for callers of this function.
232// This function will return nullptr in the case when application is running
233// on native bridge.
Dimitry Ivanovf44ecde2016-02-22 13:48:22 -0800234android_namespace_t* FindNamespaceByClassLoader(JNIEnv* env, jobject class_loader) {
Dimitry Ivanov34d5a202016-02-29 13:21:43 -0800235 std::lock_guard<std::mutex> guard(g_namespaces_mutex);
Victor Khimenko1443ec42018-07-09 17:01:22 +0200236 NativeLoaderNamespace* ns = g_namespaces->FindNamespaceByClassLoader(env, class_loader);
Jiyong Park85377812019-05-04 00:30:23 +0900237 if (ns != nullptr && !ns->IsBridged()) {
238 return ns->ToRawAndroidNamespace();
Zhenhua WANGf2804e52016-05-30 11:16:08 +0800239 }
Zhenhua WANGf2804e52016-05-30 11:16:08 +0800240 return nullptr;
Dimitry Ivanovf44ecde2016-02-22 13:48:22 -0800241}
Jiyong Park6291da22019-04-26 18:55:48 +0900242
Victor Khimenko1443ec42018-07-09 17:01:22 +0200243NativeLoaderNamespace* FindNativeLoaderNamespaceByClassLoader(JNIEnv* env, jobject class_loader) {
244 std::lock_guard<std::mutex> guard(g_namespaces_mutex);
245 return g_namespaces->FindNamespaceByClassLoader(env, class_loader);
246}
Dimitry Ivanovf44ecde2016-02-22 13:48:22 -0800247#endif
248
Jiyong Park6291da22019-04-26 18:55:48 +0900249}; // namespace android