blob: fca4c80fd52e2fc08fdcf4e8a9c6dd1cc6a37550 [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"
18#include "ScopedUtfChars.h"
19
20#include <dlfcn.h>
21#ifdef __ANDROID__
Dimitry Ivanov7f9a1aa2016-03-22 13:59:59 -070022#define LOG_TAG "libnativeloader"
Jesse Hallb75d82b2017-01-09 16:04:28 -080023#include "nativeloader/dlext_namespaces.h"
Mark Salyzynff2dcd92016-09-28 15:54:45 -070024#include "cutils/properties.h"
Mark Salyzyn30f991f2017-01-10 13:19:54 -080025#include "log/log.h"
Dimitry Ivanovac1b1912015-12-01 13:56:44 -080026#endif
Zhenhua WANGf2804e52016-05-30 11:16:08 +080027#include "nativebridge/native_bridge.h"
Dimitry Ivanovac1b1912015-12-01 13:56:44 -080028
29#include <algorithm>
30#include <vector>
31#include <string>
32#include <mutex>
33
Mark Salyzynff2dcd92016-09-28 15:54:45 -070034#include <android-base/file.h>
35#include <android-base/macros.h>
36#include <android-base/strings.h>
Dimitry Ivanovac1b1912015-12-01 13:56:44 -080037
Zhenhua WANGf2804e52016-05-30 11:16:08 +080038#define CHECK(predicate) LOG_ALWAYS_FATAL_IF(!(predicate),\
39 "%s:%d: %s CHECK '" #predicate "' failed.",\
40 __FILE__, __LINE__, __FUNCTION__)
41
Dimitry Ivanovac1b1912015-12-01 13:56:44 -080042namespace android {
43
Dimitry Ivanov4b0e9632016-03-15 13:51:26 -070044#if defined(__ANDROID__)
Zhenhua WANGf2804e52016-05-30 11:16:08 +080045class NativeLoaderNamespace {
46 public:
47 NativeLoaderNamespace()
48 : android_ns_(nullptr), native_bridge_ns_(nullptr) { }
49
50 explicit NativeLoaderNamespace(android_namespace_t* ns)
51 : android_ns_(ns), native_bridge_ns_(nullptr) { }
52
53 explicit NativeLoaderNamespace(native_bridge_namespace_t* ns)
54 : android_ns_(nullptr), native_bridge_ns_(ns) { }
55
56 NativeLoaderNamespace(NativeLoaderNamespace&& that) = default;
57 NativeLoaderNamespace(const NativeLoaderNamespace& that) = default;
58
59 NativeLoaderNamespace& operator=(const NativeLoaderNamespace& that) = default;
60
61 android_namespace_t* get_android_ns() const {
62 CHECK(native_bridge_ns_ == nullptr);
63 return android_ns_;
64 }
65
66 native_bridge_namespace_t* get_native_bridge_ns() const {
67 CHECK(android_ns_ == nullptr);
68 return native_bridge_ns_;
69 }
70
71 bool is_android_namespace() const {
72 return native_bridge_ns_ == nullptr;
73 }
74
75 private:
76 // Only one of them can be not null
77 android_namespace_t* android_ns_;
78 native_bridge_namespace_t* native_bridge_ns_;
79};
80
81static constexpr const char* kPublicNativeLibrariesSystemConfigPathFromRoot =
82 "/etc/public.libraries.txt";
83static constexpr const char* kPublicNativeLibrariesVendorConfig =
84 "/vendor/etc/public.libraries.txt";
Dimitry Ivanovac1b1912015-12-01 13:56:44 -080085
Dimitry Ivanovf334cbf2016-05-10 10:39:48 -070086// (http://b/27588281) This is a workaround for apps using custom classloaders and calling
87// System.load() with an absolute path which is outside of the classloader library search path.
88// This list includes all directories app is allowed to access this way.
89static constexpr const char* kWhitelistedDirectories = "/data:/mnt/expand";
90
Dimitry Ivanov7d028292016-05-05 17:30:24 -070091static bool is_debuggable() {
92 char debuggable[PROP_VALUE_MAX];
93 property_get("ro.debuggable", debuggable, "0");
94 return std::string(debuggable) == "1";
95}
96
Dimitry Ivanovac1b1912015-12-01 13:56:44 -080097class LibraryNamespaces {
98 public:
Dimitry Ivanov426799d2016-02-22 11:27:48 -080099 LibraryNamespaces() : initialized_(false) { }
Dimitry Ivanovac1b1912015-12-01 13:56:44 -0800100
Zhenhua WANGf2804e52016-05-30 11:16:08 +0800101 bool Create(JNIEnv* env,
102 jobject class_loader,
103 bool is_shared,
104 jstring java_library_path,
105 jstring java_permitted_path,
106 NativeLoaderNamespace* ns,
107 std::string* error_msg) {
Dimitry Ivanovcf9892b2016-05-09 10:55:50 -0700108 std::string library_path; // empty string by default.
109
110 if (java_library_path != nullptr) {
111 ScopedUtfChars library_path_utf_chars(env, java_library_path);
112 library_path = library_path_utf_chars.c_str();
113 }
Dimitry Ivanovac1b1912015-12-01 13:56:44 -0800114
Dimitry Ivanovf334cbf2016-05-10 10:39:48 -0700115 // (http://b/27588281) This is a workaround for apps using custom
116 // classloaders and calling System.load() with an absolute path which
117 // is outside of the classloader library search path.
118 //
119 // This part effectively allows such a classloader to access anything
120 // under /data and /mnt/expand
121 std::string permitted_path = kWhitelistedDirectories;
122
Dimitry Ivanov0d6e5942015-12-08 11:16:56 -0800123 if (java_permitted_path != nullptr) {
124 ScopedUtfChars path(env, java_permitted_path);
Dimitry Ivanovd0b15312016-05-10 16:21:25 -0700125 if (path.c_str() != nullptr && path.size() > 0) {
126 permitted_path = permitted_path + ":" + path.c_str();
127 }
Dimitry Ivanov0d6e5942015-12-08 11:16:56 -0800128 }
129
Zhenhua WANGf2804e52016-05-30 11:16:08 +0800130 if (!initialized_ && !InitPublicNamespace(library_path.c_str(), error_msg)) {
131 return false;
Dimitry Ivanovac1b1912015-12-01 13:56:44 -0800132 }
133
Zhenhua WANGf2804e52016-05-30 11:16:08 +0800134 bool found = FindNamespaceByClassLoader(env, class_loader, nullptr);
Dimitry Ivanovac1b1912015-12-01 13:56:44 -0800135
Zhenhua WANGf2804e52016-05-30 11:16:08 +0800136 LOG_ALWAYS_FATAL_IF(found,
Dimitry Ivanov4b0e9632016-03-15 13:51:26 -0700137 "There is already a namespace associated with this classloader");
Dimitry Ivanovac1b1912015-12-01 13:56:44 -0800138
Dimitry Ivanovd2a62202015-12-15 11:06:57 -0800139 uint64_t namespace_type = ANDROID_NAMESPACE_TYPE_ISOLATED;
140 if (is_shared) {
141 namespace_type |= ANDROID_NAMESPACE_TYPE_SHARED;
142 }
143
Zhenhua WANGf2804e52016-05-30 11:16:08 +0800144 NativeLoaderNamespace parent_ns;
145 bool found_parent_namespace = FindParentNamespaceByClassLoader(env, class_loader, &parent_ns);
Dimitry Ivanov24db75c2016-05-12 15:34:41 -0700146
Zhenhua WANGf2804e52016-05-30 11:16:08 +0800147 bool is_native_bridge = false;
Dimitry Ivanovac1b1912015-12-01 13:56:44 -0800148
Zhenhua WANGf2804e52016-05-30 11:16:08 +0800149 if (found_parent_namespace) {
150 is_native_bridge = !parent_ns.is_android_namespace();
151 } else if (!library_path.empty()) {
152 is_native_bridge = NativeBridgeIsPathSupported(library_path.c_str());
Dimitry Ivanovd047c922016-02-23 14:23:51 -0800153 }
Dimitry Ivanovac1b1912015-12-01 13:56:44 -0800154
Zhenhua WANGf2804e52016-05-30 11:16:08 +0800155 NativeLoaderNamespace native_loader_ns;
156 if (!is_native_bridge) {
157 android_namespace_t* ns = android_create_namespace("classloader-namespace",
158 nullptr,
159 library_path.c_str(),
160 namespace_type,
161 permitted_path.c_str(),
162 parent_ns.get_android_ns());
163 if (ns == nullptr) {
164 *error_msg = dlerror();
165 return false;
166 }
167
Dimitry Ivanov26e1a842017-02-03 14:11:27 -0800168 if (!android_link_namespaces(ns, nullptr, public_libraries_.c_str())) {
169 *error_msg = dlerror();
170 return false;
171 }
172
Zhenhua WANGf2804e52016-05-30 11:16:08 +0800173 native_loader_ns = NativeLoaderNamespace(ns);
174 } else {
175 native_bridge_namespace_t* ns = NativeBridgeCreateNamespace("classloader-namespace",
176 nullptr,
177 library_path.c_str(),
178 namespace_type,
179 permitted_path.c_str(),
180 parent_ns.get_native_bridge_ns());
181 if (ns == nullptr) {
182 *error_msg = NativeBridgeGetError();
183 return false;
184 }
185
186 native_loader_ns = NativeLoaderNamespace(ns);
187 }
188
189 namespaces_.push_back(std::make_pair(env->NewWeakGlobalRef(class_loader), native_loader_ns));
190
191 *ns = native_loader_ns;
192 return true;
Dimitry Ivanovac1b1912015-12-01 13:56:44 -0800193 }
194
Zhenhua WANGf2804e52016-05-30 11:16:08 +0800195 bool FindNamespaceByClassLoader(JNIEnv* env, jobject class_loader, NativeLoaderNamespace* ns) {
Dimitry Ivanovf44ecde2016-02-22 13:48:22 -0800196 auto it = std::find_if(namespaces_.begin(), namespaces_.end(),
Zhenhua WANGf2804e52016-05-30 11:16:08 +0800197 [&](const std::pair<jweak, NativeLoaderNamespace>& value) {
Dimitry Ivanovf44ecde2016-02-22 13:48:22 -0800198 return env->IsSameObject(value.first, class_loader);
199 });
Zhenhua WANGf2804e52016-05-30 11:16:08 +0800200 if (it != namespaces_.end()) {
201 if (ns != nullptr) {
202 *ns = it->second;
203 }
204
205 return true;
206 }
207
208 return false;
Dimitry Ivanovf44ecde2016-02-22 13:48:22 -0800209 }
210
Dimitry Ivanov4b0e9632016-03-15 13:51:26 -0700211 void Initialize() {
Dimitry Ivanov80ddb8f2016-05-09 18:12:00 -0700212 // Once public namespace is initialized there is no
213 // point in running this code - it will have no effect
214 // on the current list of public libraries.
215 if (initialized_) {
216 return;
217 }
218
Dimitry Ivanovb6140452016-04-06 18:24:08 -0700219 std::vector<std::string> sonames;
Dimitry Ivanov0b5651e2016-04-21 16:42:48 -0700220 const char* android_root_env = getenv("ANDROID_ROOT");
221 std::string root_dir = android_root_env != nullptr ? android_root_env : "/system";
222 std::string public_native_libraries_system_config =
223 root_dir + kPublicNativeLibrariesSystemConfigPathFromRoot;
Dimitry Ivanovb6140452016-04-06 18:24:08 -0700224
Christopher Ferris39da84b2016-06-21 16:11:23 -0700225 std::string error_msg;
226 LOG_ALWAYS_FATAL_IF(!ReadConfig(public_native_libraries_system_config, &sonames, &error_msg),
Dimitry Ivanovb6140452016-04-06 18:24:08 -0700227 "Error reading public native library list from \"%s\": %s",
Christopher Ferris39da84b2016-06-21 16:11:23 -0700228 public_native_libraries_system_config.c_str(), error_msg.c_str());
Dimitry Ivanov7d028292016-05-05 17:30:24 -0700229
230 // For debuggable platform builds use ANDROID_ADDITIONAL_PUBLIC_LIBRARIES environment
231 // variable to add libraries to the list. This is intended for platform tests only.
232 if (is_debuggable()) {
233 const char* additional_libs = getenv("ANDROID_ADDITIONAL_PUBLIC_LIBRARIES");
234 if (additional_libs != nullptr && additional_libs[0] != '\0') {
235 std::vector<std::string> additional_libs_vector = base::Split(additional_libs, ":");
236 std::copy(additional_libs_vector.begin(),
237 additional_libs_vector.end(),
238 std::back_inserter(sonames));
239 }
240 }
241
Dimitry Ivanovb6140452016-04-06 18:24:08 -0700242 // This file is optional, quietly ignore if the file does not exist.
243 ReadConfig(kPublicNativeLibrariesVendorConfig, &sonames);
244
245 // android_init_namespaces() expects all the public libraries
246 // to be loaded so that they can be found by soname alone.
247 //
248 // TODO(dimitry): this is a bit misleading since we do not know
249 // if the vendor public library is going to be opened from /vendor/lib
250 // we might as well end up loading them from /system/lib
251 // For now we rely on CTS test to catch things like this but
252 // it should probably be addressed in the future.
253 for (const auto& soname : sonames) {
254 dlopen(soname.c_str(), RTLD_NOW | RTLD_NODELETE);
255 }
256
257 public_libraries_ = base::Join(sonames, ':');
258 }
259
Dimitry Ivanovbe4ca3a2016-05-02 10:43:16 -0700260 void Reset() {
261 namespaces_.clear();
262 }
263
Dimitry Ivanovb6140452016-04-06 18:24:08 -0700264 private:
Christopher Ferris39da84b2016-06-21 16:11:23 -0700265 bool ReadConfig(const std::string& configFile, std::vector<std::string>* sonames,
266 std::string* error_msg = nullptr) {
Dimitry Ivanov4b0e9632016-03-15 13:51:26 -0700267 // Read list of public native libraries from the config file.
268 std::string file_content;
Dimitry Ivanovb6140452016-04-06 18:24:08 -0700269 if(!base::ReadFileToString(configFile, &file_content)) {
Christopher Ferris39da84b2016-06-21 16:11:23 -0700270 if (error_msg) *error_msg = strerror(errno);
Dimitry Ivanovb6140452016-04-06 18:24:08 -0700271 return false;
272 }
Dimitry Ivanov4b0e9632016-03-15 13:51:26 -0700273
274 std::vector<std::string> lines = base::Split(file_content, "\n");
275
Christopher Ferris39da84b2016-06-21 16:11:23 -0700276 for (auto& line : lines) {
Dimitry Ivanov4b0e9632016-03-15 13:51:26 -0700277 auto trimmed_line = base::Trim(line);
278 if (trimmed_line[0] == '#' || trimmed_line.empty()) {
279 continue;
280 }
Christopher Ferris39da84b2016-06-21 16:11:23 -0700281 size_t space_pos = trimmed_line.rfind(' ');
282 if (space_pos != std::string::npos) {
283 std::string type = trimmed_line.substr(space_pos + 1);
284 if (type != "32" && type != "64") {
285 if (error_msg) *error_msg = "Malformed line: " + line;
286 return false;
287 }
288#if defined(__LP64__)
289 // Skip 32 bit public library.
290 if (type == "32") {
291 continue;
292 }
293#else
294 // Skip 64 bit public library.
295 if (type == "64") {
296 continue;
297 }
298#endif
299 trimmed_line.resize(space_pos);
300 }
Dimitry Ivanov4b0e9632016-03-15 13:51:26 -0700301
Dimitry Ivanovb6140452016-04-06 18:24:08 -0700302 sonames->push_back(trimmed_line);
Dimitry Ivanov4b0e9632016-03-15 13:51:26 -0700303 }
304
Dimitry Ivanovb6140452016-04-06 18:24:08 -0700305 return true;
Dimitry Ivanovd68c8e92016-02-10 14:09:22 -0800306 }
Dimitry Ivanovac1b1912015-12-01 13:56:44 -0800307
Zhenhua WANGf2804e52016-05-30 11:16:08 +0800308 bool InitPublicNamespace(const char* library_path, std::string* error_msg) {
309 // Ask native bride if this apps library path should be handled by it
310 bool is_native_bridge = NativeBridgeIsPathSupported(library_path);
311
Dimitry Ivanov4b0e9632016-03-15 13:51:26 -0700312 // (http://b/25844435) - Some apps call dlopen from generated code (mono jited
313 // code is one example) unknown to linker in which case linker uses anonymous
314 // namespace. The second argument specifies the search path for the anonymous
315 // namespace which is the library_path of the classloader.
Dimitry Ivanov26e1a842017-02-03 14:11:27 -0800316 initialized_ = android_init_anonymous_namespace(public_libraries_.c_str(),
317 is_native_bridge ? nullptr : library_path);
Dimitry Ivanovd836ab02016-11-02 18:03:10 -0700318 if (!initialized_) {
319 *error_msg = dlerror();
320 return false;
321 }
322
323 // and now initialize native bridge namespaces if necessary.
324 if (NativeBridgeInitialized()) {
325 initialized_ = NativeBridgeInitNamespace(public_libraries_.c_str(),
326 is_native_bridge ? library_path : nullptr);
Zhenhua WANGf2804e52016-05-30 11:16:08 +0800327 if (!initialized_) {
328 *error_msg = NativeBridgeGetError();
329 }
330 }
Dimitry Ivanovac1b1912015-12-01 13:56:44 -0800331
332 return initialized_;
333 }
334
Dimitry Ivanov24db75c2016-05-12 15:34:41 -0700335 jobject GetParentClassLoader(JNIEnv* env, jobject class_loader) {
336 jclass class_loader_class = env->FindClass("java/lang/ClassLoader");
337 jmethodID get_parent = env->GetMethodID(class_loader_class,
338 "getParent",
339 "()Ljava/lang/ClassLoader;");
340
341 return env->CallObjectMethod(class_loader, get_parent);
342 }
343
Zhenhua WANGf2804e52016-05-30 11:16:08 +0800344 bool FindParentNamespaceByClassLoader(JNIEnv* env,
345 jobject class_loader,
346 NativeLoaderNamespace* ns) {
Dimitry Ivanov24db75c2016-05-12 15:34:41 -0700347 jobject parent_class_loader = GetParentClassLoader(env, class_loader);
348
349 while (parent_class_loader != nullptr) {
Zhenhua WANGf2804e52016-05-30 11:16:08 +0800350 if (FindNamespaceByClassLoader(env, parent_class_loader, ns)) {
351 return true;
Dimitry Ivanov24db75c2016-05-12 15:34:41 -0700352 }
353
354 parent_class_loader = GetParentClassLoader(env, parent_class_loader);
355 }
Zhenhua WANGf2804e52016-05-30 11:16:08 +0800356
357 return false;
Dimitry Ivanov24db75c2016-05-12 15:34:41 -0700358 }
359
Dimitry Ivanovac1b1912015-12-01 13:56:44 -0800360 bool initialized_;
Zhenhua WANGf2804e52016-05-30 11:16:08 +0800361 std::vector<std::pair<jweak, NativeLoaderNamespace>> namespaces_;
Dimitry Ivanov4b0e9632016-03-15 13:51:26 -0700362 std::string public_libraries_;
363
Dimitry Ivanovac1b1912015-12-01 13:56:44 -0800364
365 DISALLOW_COPY_AND_ASSIGN(LibraryNamespaces);
366};
367
Dimitry Ivanov34d5a202016-02-29 13:21:43 -0800368static std::mutex g_namespaces_mutex;
Dimitry Ivanovac1b1912015-12-01 13:56:44 -0800369static LibraryNamespaces* g_namespaces = new LibraryNamespaces;
370#endif
371
Dimitry Ivanov4b0e9632016-03-15 13:51:26 -0700372void InitializeNativeLoader() {
Dimitry Ivanov426799d2016-02-22 11:27:48 -0800373#if defined(__ANDROID__)
Dimitry Ivanov34d5a202016-02-29 13:21:43 -0800374 std::lock_guard<std::mutex> guard(g_namespaces_mutex);
Dimitry Ivanov4b0e9632016-03-15 13:51:26 -0700375 g_namespaces->Initialize();
Dimitry Ivanov426799d2016-02-22 11:27:48 -0800376#endif
377}
378
Dimitry Ivanovbe4ca3a2016-05-02 10:43:16 -0700379void ResetNativeLoader() {
380#if defined(__ANDROID__)
381 std::lock_guard<std::mutex> guard(g_namespaces_mutex);
382 g_namespaces->Reset();
383#endif
384}
Dimitry Ivanovac1b1912015-12-01 13:56:44 -0800385
Dimitry Ivanovd047c922016-02-23 14:23:51 -0800386jstring CreateClassLoaderNamespace(JNIEnv* env,
387 int32_t target_sdk_version,
388 jobject class_loader,
389 bool is_shared,
390 jstring library_path,
391 jstring permitted_path) {
Dimitry Ivanovac1b1912015-12-01 13:56:44 -0800392#if defined(__ANDROID__)
Dimitry Ivanov5539db02016-04-20 16:07:30 -0700393 UNUSED(target_sdk_version);
Dimitry Ivanov34d5a202016-02-29 13:21:43 -0800394 std::lock_guard<std::mutex> guard(g_namespaces_mutex);
Zhenhua WANGf2804e52016-05-30 11:16:08 +0800395
396 std::string error_msg;
397 NativeLoaderNamespace ns;
398 bool success = g_namespaces->Create(env,
399 class_loader,
400 is_shared,
401 library_path,
402 permitted_path,
403 &ns,
404 &error_msg);
405 if (!success) {
406 return env->NewStringUTF(error_msg.c_str());
Dimitry Ivanovd047c922016-02-23 14:23:51 -0800407 }
408#else
409 UNUSED(env, target_sdk_version, class_loader, is_shared,
410 library_path, permitted_path);
411#endif
412 return nullptr;
413}
414
415void* OpenNativeLibrary(JNIEnv* env,
416 int32_t target_sdk_version,
417 const char* path,
418 jobject class_loader,
Zhenhua WANGf2804e52016-05-30 11:16:08 +0800419 jstring library_path,
420 bool* needs_native_bridge,
421 std::string* error_msg) {
Dimitry Ivanovd047c922016-02-23 14:23:51 -0800422#if defined(__ANDROID__)
Dimitry Ivanov5539db02016-04-20 16:07:30 -0700423 UNUSED(target_sdk_version);
424 if (class_loader == nullptr) {
Zhenhua WANGf2804e52016-05-30 11:16:08 +0800425 *needs_native_bridge = false;
Dimitry Ivanovac1b1912015-12-01 13:56:44 -0800426 return dlopen(path, RTLD_NOW);
427 }
428
Dimitry Ivanov34d5a202016-02-29 13:21:43 -0800429 std::lock_guard<std::mutex> guard(g_namespaces_mutex);
Zhenhua WANGf2804e52016-05-30 11:16:08 +0800430 NativeLoaderNamespace ns;
Dimitry Ivanovac1b1912015-12-01 13:56:44 -0800431
Zhenhua WANGf2804e52016-05-30 11:16:08 +0800432 if (!g_namespaces->FindNamespaceByClassLoader(env, class_loader, &ns)) {
Dimitry Ivanovd047c922016-02-23 14:23:51 -0800433 // This is the case where the classloader was not created by ApplicationLoaders
434 // In this case we create an isolated not-shared namespace for it.
Zhenhua WANGf2804e52016-05-30 11:16:08 +0800435 if (!g_namespaces->Create(env, class_loader, false, library_path, nullptr, &ns, error_msg)) {
Dimitry Ivanovd047c922016-02-23 14:23:51 -0800436 return nullptr;
437 }
Dimitry Ivanovac1b1912015-12-01 13:56:44 -0800438 }
439
Zhenhua WANGf2804e52016-05-30 11:16:08 +0800440 if (ns.is_android_namespace()) {
441 android_dlextinfo extinfo;
442 extinfo.flags = ANDROID_DLEXT_USE_NAMESPACE;
443 extinfo.library_namespace = ns.get_android_ns();
Dimitry Ivanovac1b1912015-12-01 13:56:44 -0800444
Zhenhua WANGf2804e52016-05-30 11:16:08 +0800445 void* handle = android_dlopen_ext(path, RTLD_NOW, &extinfo);
446 if (handle == nullptr) {
447 *error_msg = dlerror();
448 }
449 *needs_native_bridge = false;
450 return handle;
451 } else {
452 void* handle = NativeBridgeLoadLibraryExt(path, RTLD_NOW, ns.get_native_bridge_ns());
453 if (handle == nullptr) {
454 *error_msg = NativeBridgeGetError();
455 }
456 *needs_native_bridge = true;
457 return handle;
458 }
Dimitry Ivanovac1b1912015-12-01 13:56:44 -0800459#else
Dimitry Ivanovd047c922016-02-23 14:23:51 -0800460 UNUSED(env, target_sdk_version, class_loader, library_path);
Zhenhua WANGf2804e52016-05-30 11:16:08 +0800461 *needs_native_bridge = false;
462 void* handle = dlopen(path, RTLD_NOW);
463 if (handle == nullptr) {
464 if (NativeBridgeIsSupported(path)) {
465 *needs_native_bridge = true;
466 handle = NativeBridgeLoadLibrary(path, RTLD_NOW);
467 if (handle == nullptr) {
468 *error_msg = NativeBridgeGetError();
469 }
470 } else {
471 *needs_native_bridge = false;
472 *error_msg = dlerror();
473 }
474 }
475 return handle;
Dimitry Ivanovac1b1912015-12-01 13:56:44 -0800476#endif
477}
478
Zhenhua WANGf2804e52016-05-30 11:16:08 +0800479bool CloseNativeLibrary(void* handle, const bool needs_native_bridge) {
480 return needs_native_bridge ? NativeBridgeUnloadLibrary(handle) :
481 dlclose(handle);
Dimitry Ivanov09a516b2016-05-03 14:55:25 -0700482}
483
Dimitry Ivanovf44ecde2016-02-22 13:48:22 -0800484#if defined(__ANDROID__)
Dimitry Ivanov800083d2016-11-01 14:17:00 -0700485// native_bridge_namespaces are not supported for callers of this function.
486// This function will return nullptr in the case when application is running
487// on native bridge.
Dimitry Ivanovf44ecde2016-02-22 13:48:22 -0800488android_namespace_t* FindNamespaceByClassLoader(JNIEnv* env, jobject class_loader) {
Dimitry Ivanov34d5a202016-02-29 13:21:43 -0800489 std::lock_guard<std::mutex> guard(g_namespaces_mutex);
Zhenhua WANGf2804e52016-05-30 11:16:08 +0800490 NativeLoaderNamespace ns;
491 if (g_namespaces->FindNamespaceByClassLoader(env, class_loader, &ns)) {
Dimitry Ivanov800083d2016-11-01 14:17:00 -0700492 return ns.is_android_namespace() ? ns.get_android_ns() : nullptr;
Zhenhua WANGf2804e52016-05-30 11:16:08 +0800493 }
494
495 return nullptr;
Dimitry Ivanovf44ecde2016-02-22 13:48:22 -0800496}
497#endif
498
Dimitry Ivanovac1b1912015-12-01 13:56:44 -0800499}; // android namespace