blob: 64d60ea6cc22c0a8ba2912e84e2f59c41e679a8a [file] [log] [blame]
Orion Hodson9b16e342019-10-09 13:29:16 +01001/*
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#define LOG_TAG "nativeloader"
18
19#include "nativeloader/native_loader.h"
20
21#include <dlfcn.h>
22#include <sys/types.h>
23
Kiyoung Kim272b36d2020-02-19 16:08:47 +090024#include <algorithm>
Orion Hodson9b16e342019-10-09 13:29:16 +010025#include <memory>
26#include <mutex>
27#include <string>
28#include <vector>
29
30#include <android-base/file.h>
31#include <android-base/macros.h>
32#include <android-base/strings.h>
33#include <nativebridge/native_bridge.h>
Orion Hodson6dc0a432020-02-06 14:28:28 +000034#include <nativehelper/scoped_utf_chars.h>
Orion Hodson9b16e342019-10-09 13:29:16 +010035
Nicolas Geoffray7ca8b672020-04-24 15:43:48 +010036#ifdef ART_TARGET_ANDROID
Orion Hodson9b16e342019-10-09 13:29:16 +010037#include <log/log.h>
38#include "library_namespaces.h"
39#include "nativeloader/dlext_namespaces.h"
40#endif
41
42namespace android {
43
44namespace {
Nicolas Geoffray7ca8b672020-04-24 15:43:48 +010045#if defined(ART_TARGET_ANDROID)
Orion Hodson9b16e342019-10-09 13:29:16 +010046using android::nativeloader::LibraryNamespaces;
47
Orion Hodson9b16e342019-10-09 13:29:16 +010048std::mutex g_namespaces_mutex;
49LibraryNamespaces* g_namespaces = new LibraryNamespaces;
50
51android_namespace_t* FindExportedNamespace(const char* caller_location) {
Jooyung Han538f99a2020-03-03 00:46:50 +090052 auto name = nativeloader::FindApexNamespaceName(caller_location);
53 if (name.ok()) {
54 android_namespace_t* boot_namespace = android_get_exported_namespace(name->c_str());
Orion Hodson9b16e342019-10-09 13:29:16 +010055 LOG_ALWAYS_FATAL_IF((boot_namespace == nullptr),
Jooyung Han538f99a2020-03-03 00:46:50 +090056 "Error finding namespace of apex: no namespace called %s", name->c_str());
Orion Hodson9b16e342019-10-09 13:29:16 +010057 return boot_namespace;
58 }
59 return nullptr;
60}
Nicolas Geoffray7ca8b672020-04-24 15:43:48 +010061#endif // #if defined(ART_TARGET_ANDROID)
Orion Hodson9b16e342019-10-09 13:29:16 +010062} // namespace
63
64void InitializeNativeLoader() {
Nicolas Geoffray7ca8b672020-04-24 15:43:48 +010065#if defined(ART_TARGET_ANDROID)
Orion Hodson9b16e342019-10-09 13:29:16 +010066 std::lock_guard<std::mutex> guard(g_namespaces_mutex);
67 g_namespaces->Initialize();
68#endif
69}
70
71void ResetNativeLoader() {
Nicolas Geoffray7ca8b672020-04-24 15:43:48 +010072#if defined(ART_TARGET_ANDROID)
Orion Hodson9b16e342019-10-09 13:29:16 +010073 std::lock_guard<std::mutex> guard(g_namespaces_mutex);
74 g_namespaces->Reset();
75#endif
76}
77
78jstring CreateClassLoaderNamespace(JNIEnv* env, int32_t target_sdk_version, jobject class_loader,
79 bool is_shared, jstring dex_path, jstring library_path,
Jiyong Parke741dfd2020-07-02 23:17:58 +090080 jstring permitted_path, jstring uses_library_list) {
Nicolas Geoffray7ca8b672020-04-24 15:43:48 +010081#if defined(ART_TARGET_ANDROID)
Orion Hodson9b16e342019-10-09 13:29:16 +010082 std::lock_guard<std::mutex> guard(g_namespaces_mutex);
83 auto ns = g_namespaces->Create(env, target_sdk_version, class_loader, is_shared, dex_path,
Jiyong Parke741dfd2020-07-02 23:17:58 +090084 library_path, permitted_path, uses_library_list);
Bernie Innocenti4bd58952020-02-06 15:43:57 +090085 if (!ns.ok()) {
Orion Hodson9b16e342019-10-09 13:29:16 +010086 return env->NewStringUTF(ns.error().message().c_str());
87 }
88#else
Jiyong Parke741dfd2020-07-02 23:17:58 +090089 UNUSED(env, target_sdk_version, class_loader, is_shared, dex_path, library_path, permitted_path,
90 uses_library_list);
Orion Hodson9b16e342019-10-09 13:29:16 +010091#endif
92 return nullptr;
93}
94
95void* OpenNativeLibrary(JNIEnv* env, int32_t target_sdk_version, const char* path,
96 jobject class_loader, const char* caller_location, jstring library_path,
97 bool* needs_native_bridge, char** error_msg) {
Nicolas Geoffray7ca8b672020-04-24 15:43:48 +010098#if defined(ART_TARGET_ANDROID)
Orion Hodson9b16e342019-10-09 13:29:16 +010099 UNUSED(target_sdk_version);
100 if (class_loader == nullptr) {
101 *needs_native_bridge = false;
102 if (caller_location != nullptr) {
103 android_namespace_t* boot_namespace = FindExportedNamespace(caller_location);
104 if (boot_namespace != nullptr) {
105 const android_dlextinfo dlextinfo = {
106 .flags = ANDROID_DLEXT_USE_NAMESPACE,
107 .library_namespace = boot_namespace,
108 };
109 void* handle = android_dlopen_ext(path, RTLD_NOW, &dlextinfo);
110 if (handle == nullptr) {
111 *error_msg = strdup(dlerror());
112 }
113 return handle;
114 }
115 }
116 void* handle = dlopen(path, RTLD_NOW);
117 if (handle == nullptr) {
118 *error_msg = strdup(dlerror());
119 }
120 return handle;
121 }
122
123 std::lock_guard<std::mutex> guard(g_namespaces_mutex);
124 NativeLoaderNamespace* ns;
125
126 if ((ns = g_namespaces->FindNamespaceByClassLoader(env, class_loader)) == nullptr) {
127 // This is the case where the classloader was not created by ApplicationLoaders
128 // In this case we create an isolated not-shared namespace for it.
129 Result<NativeLoaderNamespace*> isolated_ns =
130 g_namespaces->Create(env, target_sdk_version, class_loader, false /* is_shared */, nullptr,
Jiyong Parke741dfd2020-07-02 23:17:58 +0900131 library_path, nullptr, nullptr);
Bernie Innocenti4bd58952020-02-06 15:43:57 +0900132 if (!isolated_ns.ok()) {
Orion Hodson9b16e342019-10-09 13:29:16 +0100133 *error_msg = strdup(isolated_ns.error().message().c_str());
134 return nullptr;
135 } else {
136 ns = *isolated_ns;
137 }
138 }
139
140 return OpenNativeLibraryInNamespace(ns, path, needs_native_bridge, error_msg);
141#else
142 UNUSED(env, target_sdk_version, class_loader, caller_location);
143
144 // Do some best effort to emulate library-path support. It will not
145 // work for dependencies.
146 //
147 // Note: null has a special meaning and must be preserved.
148 std::string c_library_path; // Empty string by default.
149 if (library_path != nullptr && path != nullptr && path[0] != '/') {
150 ScopedUtfChars library_path_utf_chars(env, library_path);
151 c_library_path = library_path_utf_chars.c_str();
152 }
153
154 std::vector<std::string> library_paths = base::Split(c_library_path, ":");
155
156 for (const std::string& lib_path : library_paths) {
157 *needs_native_bridge = false;
158 const char* path_arg;
159 std::string complete_path;
160 if (path == nullptr) {
161 // Preserve null.
162 path_arg = nullptr;
163 } else {
164 complete_path = lib_path;
165 if (!complete_path.empty()) {
166 complete_path.append("/");
167 }
168 complete_path.append(path);
169 path_arg = complete_path.c_str();
170 }
171 void* handle = dlopen(path_arg, RTLD_NOW);
172 if (handle != nullptr) {
173 return handle;
174 }
175 if (NativeBridgeIsSupported(path_arg)) {
176 *needs_native_bridge = true;
177 handle = NativeBridgeLoadLibrary(path_arg, RTLD_NOW);
178 if (handle != nullptr) {
179 return handle;
180 }
181 *error_msg = strdup(NativeBridgeGetError());
182 } else {
183 *error_msg = strdup(dlerror());
184 }
185 }
186 return nullptr;
187#endif
188}
189
190bool CloseNativeLibrary(void* handle, const bool needs_native_bridge, char** error_msg) {
191 bool success;
192 if (needs_native_bridge) {
193 success = (NativeBridgeUnloadLibrary(handle) == 0);
194 if (!success) {
195 *error_msg = strdup(NativeBridgeGetError());
196 }
197 } else {
198 success = (dlclose(handle) == 0);
199 if (!success) {
200 *error_msg = strdup(dlerror());
201 }
202 }
203
204 return success;
205}
206
207void NativeLoaderFreeErrorMessage(char* msg) {
208 // The error messages get allocated through strdup, so we must call free on them.
209 free(msg);
210}
211
Nicolas Geoffray7ca8b672020-04-24 15:43:48 +0100212#if defined(ART_TARGET_ANDROID)
Orion Hodson9b16e342019-10-09 13:29:16 +0100213void* OpenNativeLibraryInNamespace(NativeLoaderNamespace* ns, const char* path,
214 bool* needs_native_bridge, char** error_msg) {
215 auto handle = ns->Load(path);
Bernie Innocenti4bd58952020-02-06 15:43:57 +0900216 if (!handle.ok() && error_msg != nullptr) {
Orion Hodson9b16e342019-10-09 13:29:16 +0100217 *error_msg = strdup(handle.error().message().c_str());
218 }
219 if (needs_native_bridge != nullptr) {
220 *needs_native_bridge = ns->IsBridged();
221 }
Bernie Innocenti4bd58952020-02-06 15:43:57 +0900222 return handle.ok() ? *handle : nullptr;
Orion Hodson9b16e342019-10-09 13:29:16 +0100223}
224
225// native_bridge_namespaces are not supported for callers of this function.
226// This function will return nullptr in the case when application is running
227// on native bridge.
228android_namespace_t* FindNamespaceByClassLoader(JNIEnv* env, jobject class_loader) {
229 std::lock_guard<std::mutex> guard(g_namespaces_mutex);
230 NativeLoaderNamespace* ns = g_namespaces->FindNamespaceByClassLoader(env, class_loader);
231 if (ns != nullptr && !ns->IsBridged()) {
232 return ns->ToRawAndroidNamespace();
233 }
234 return nullptr;
235}
236
237NativeLoaderNamespace* FindNativeLoaderNamespaceByClassLoader(JNIEnv* env, jobject class_loader) {
238 std::lock_guard<std::mutex> guard(g_namespaces_mutex);
239 return g_namespaces->FindNamespaceByClassLoader(env, class_loader);
240}
241#endif
242
243}; // namespace android