blob: ab17b29d4259bc70b44659d28935f08c31acc713 [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"
Steven Moreland00fe3ad2017-07-18 16:53:54 -070018#include <nativehelper/ScopedUtfChars.h>
Dimitry Ivanovac1b1912015-12-01 13:56:44 -080019
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
Jiyong Parkd1006fe2017-11-08 18:44:09 +090027#include <dirent.h>
28#include <sys/types.h>
Zhenhua WANGf2804e52016-05-30 11:16:08 +080029#include "nativebridge/native_bridge.h"
Dimitry Ivanovac1b1912015-12-01 13:56:44 -080030
31#include <algorithm>
Victor Khimenko1443ec42018-07-09 17:01:22 +020032#include <list>
Jiyong Parkd1006fe2017-11-08 18:44:09 +090033#include <memory>
Dimitry Ivanovac1b1912015-12-01 13:56:44 -080034#include <mutex>
Jiyong Parkd1006fe2017-11-08 18:44:09 +090035#include <string>
36#include <vector>
Dimitry Ivanovac1b1912015-12-01 13:56:44 -080037
Mark Salyzynff2dcd92016-09-28 15:54:45 -070038#include <android-base/file.h>
39#include <android-base/macros.h>
40#include <android-base/strings.h>
Dimitry Ivanovac1b1912015-12-01 13:56:44 -080041
Justin Yun4a1d1102017-11-27 17:04:14 +090042#ifdef __BIONIC__
43#include <android-base/properties.h>
44#endif
45
Nicolas Geoffray890e3bf2019-01-22 09:11:57 +000046extern "C" {
47struct android_namespace_t* android_get_exported_namespace(const char*);
48}
49
Zhenhua WANGf2804e52016-05-30 11:16:08 +080050#define CHECK(predicate) LOG_ALWAYS_FATAL_IF(!(predicate),\
51 "%s:%d: %s CHECK '" #predicate "' failed.",\
52 __FILE__, __LINE__, __FUNCTION__)
53
Inseob Kim67cb0562018-05-04 11:39:12 +090054using namespace std::string_literals;
55
Dimitry Ivanovac1b1912015-12-01 13:56:44 -080056namespace android {
57
Dimitry Ivanov4b0e9632016-03-15 13:51:26 -070058#if defined(__ANDROID__)
Nicolas Geoffrayc3a73dc2019-01-12 15:01:20 +000059struct NativeLoaderNamespace {
Zhenhua WANGf2804e52016-05-30 11:16:08 +080060 public:
61 NativeLoaderNamespace()
62 : android_ns_(nullptr), native_bridge_ns_(nullptr) { }
63
64 explicit NativeLoaderNamespace(android_namespace_t* ns)
65 : android_ns_(ns), native_bridge_ns_(nullptr) { }
66
67 explicit NativeLoaderNamespace(native_bridge_namespace_t* ns)
68 : android_ns_(nullptr), native_bridge_ns_(ns) { }
69
70 NativeLoaderNamespace(NativeLoaderNamespace&& that) = default;
71 NativeLoaderNamespace(const NativeLoaderNamespace& that) = default;
72
73 NativeLoaderNamespace& operator=(const NativeLoaderNamespace& that) = default;
74
75 android_namespace_t* get_android_ns() const {
76 CHECK(native_bridge_ns_ == nullptr);
77 return android_ns_;
78 }
79
80 native_bridge_namespace_t* get_native_bridge_ns() const {
81 CHECK(android_ns_ == nullptr);
82 return native_bridge_ns_;
83 }
84
85 bool is_android_namespace() const {
86 return native_bridge_ns_ == nullptr;
87 }
88
89 private:
90 // Only one of them can be not null
91 android_namespace_t* android_ns_;
92 native_bridge_namespace_t* native_bridge_ns_;
93};
94
Jiyong Parkd1006fe2017-11-08 18:44:09 +090095static constexpr const char kPublicNativeLibrariesSystemConfigPathFromRoot[] =
96 "/etc/public.libraries.txt";
97static constexpr const char kPublicNativeLibrariesExtensionConfigPrefix[] = "public.libraries-";
98static constexpr const size_t kPublicNativeLibrariesExtensionConfigPrefixLen =
99 sizeof(kPublicNativeLibrariesExtensionConfigPrefix) - 1;
100static constexpr const char kPublicNativeLibrariesExtensionConfigSuffix[] = ".txt";
101static constexpr const size_t kPublicNativeLibrariesExtensionConfigSuffixLen =
102 sizeof(kPublicNativeLibrariesExtensionConfigSuffix) - 1;
103static constexpr const char kPublicNativeLibrariesVendorConfig[] =
104 "/vendor/etc/public.libraries.txt";
105static constexpr const char kLlndkNativeLibrariesSystemConfigPathFromRoot[] =
106 "/etc/llndk.libraries.txt";
107static constexpr const char kVndkspNativeLibrariesSystemConfigPathFromRoot[] =
108 "/etc/vndksp.libraries.txt";
Dimitry Ivanovac1b1912015-12-01 13:56:44 -0800109
Dimitry Ivanov882cad22017-05-01 15:12:49 -0700110// The device may be configured to have the vendor libraries loaded to a separate namespace.
111// For historical reasons this namespace was named sphal but effectively it is intended
112// to use to load vendor libraries to separate namespace with controlled interface between
113// vendor and system namespaces.
114static constexpr const char* kVendorNamespaceName = "sphal";
115
Jiyong Parka07f3052017-08-22 10:26:10 +0900116static constexpr const char* kVndkNamespaceName = "vndk";
117
118static constexpr const char* kClassloaderNamespaceName = "classloader-namespace";
119static constexpr const char* kVendorClassloaderNamespaceName = "vendor-classloader-namespace";
120
Dimitry Ivanovf334cbf2016-05-10 10:39:48 -0700121// (http://b/27588281) This is a workaround for apps using custom classloaders and calling
122// System.load() with an absolute path which is outside of the classloader library search path.
123// This list includes all directories app is allowed to access this way.
124static constexpr const char* kWhitelistedDirectories = "/data:/mnt/expand";
125
Nicolas Geoffray890e3bf2019-01-22 09:11:57 +0000126static constexpr const char* kApexPath = "/apex/";
127
Dimitry Ivanov7d028292016-05-05 17:30:24 -0700128static bool is_debuggable() {
129 char debuggable[PROP_VALUE_MAX];
130 property_get("ro.debuggable", debuggable, "0");
131 return std::string(debuggable) == "1";
132}
133
Justin Yun4a1d1102017-11-27 17:04:14 +0900134static std::string vndk_version_str() {
135#ifdef __BIONIC__
136 std::string version = android::base::GetProperty("ro.vndk.version", "");
137 if (version != "" && version != "current") {
138 return "." + version;
139 }
140#endif
141 return "";
142}
143
144static void insert_vndk_version_str(std::string* file_name) {
145 CHECK(file_name != nullptr);
146 size_t insert_pos = file_name->find_last_of(".");
147 if (insert_pos == std::string::npos) {
148 insert_pos = file_name->length();
149 }
150 file_name->insert(insert_pos, vndk_version_str());
151}
152
Jiyong Parkd1006fe2017-11-08 18:44:09 +0900153static const std::function<bool(const std::string&, std::string*)> always_true =
154 [](const std::string&, std::string*) { return true; };
155
Dimitry Ivanovac1b1912015-12-01 13:56:44 -0800156class LibraryNamespaces {
157 public:
Dimitry Ivanov426799d2016-02-22 11:27:48 -0800158 LibraryNamespaces() : initialized_(false) { }
Dimitry Ivanovac1b1912015-12-01 13:56:44 -0800159
Nicolas Geoffrayc3a73dc2019-01-12 15:01:20 +0000160 NativeLoaderNamespace* Create(JNIEnv* env, uint32_t target_sdk_version, jobject class_loader,
161 bool is_shared, bool is_for_vendor, jstring java_library_path,
162 jstring java_permitted_path, std::string* error_msg) {
Dimitry Ivanovcf9892b2016-05-09 10:55:50 -0700163 std::string library_path; // empty string by default.
164
165 if (java_library_path != nullptr) {
166 ScopedUtfChars library_path_utf_chars(env, java_library_path);
167 library_path = library_path_utf_chars.c_str();
168 }
Dimitry Ivanovac1b1912015-12-01 13:56:44 -0800169
Dimitry Ivanovf334cbf2016-05-10 10:39:48 -0700170 // (http://b/27588281) This is a workaround for apps using custom
171 // classloaders and calling System.load() with an absolute path which
172 // is outside of the classloader library search path.
173 //
174 // This part effectively allows such a classloader to access anything
175 // under /data and /mnt/expand
176 std::string permitted_path = kWhitelistedDirectories;
177
Dimitry Ivanov0d6e5942015-12-08 11:16:56 -0800178 if (java_permitted_path != nullptr) {
179 ScopedUtfChars path(env, java_permitted_path);
Dimitry Ivanovd0b15312016-05-10 16:21:25 -0700180 if (path.c_str() != nullptr && path.size() > 0) {
181 permitted_path = permitted_path + ":" + path.c_str();
182 }
Dimitry Ivanov0d6e5942015-12-08 11:16:56 -0800183 }
184
Zhenhua WANGf2804e52016-05-30 11:16:08 +0800185 if (!initialized_ && !InitPublicNamespace(library_path.c_str(), error_msg)) {
Victor Khimenko1443ec42018-07-09 17:01:22 +0200186 return nullptr;
Dimitry Ivanovac1b1912015-12-01 13:56:44 -0800187 }
188
Victor Khimenko1443ec42018-07-09 17:01:22 +0200189 bool found = FindNamespaceByClassLoader(env, class_loader);
Dimitry Ivanovac1b1912015-12-01 13:56:44 -0800190
Zhenhua WANGf2804e52016-05-30 11:16:08 +0800191 LOG_ALWAYS_FATAL_IF(found,
Dimitry Ivanov4b0e9632016-03-15 13:51:26 -0700192 "There is already a namespace associated with this classloader");
Dimitry Ivanovac1b1912015-12-01 13:56:44 -0800193
Dimitry Ivanovd2a62202015-12-15 11:06:57 -0800194 uint64_t namespace_type = ANDROID_NAMESPACE_TYPE_ISOLATED;
195 if (is_shared) {
196 namespace_type |= ANDROID_NAMESPACE_TYPE_SHARED;
197 }
198
Dimitry Ivanov9e253ce2017-05-08 22:24:24 -0700199 if (target_sdk_version < 24) {
200 namespace_type |= ANDROID_NAMESPACE_TYPE_GREYLIST_ENABLED;
201 }
202
Victor Khimenko1443ec42018-07-09 17:01:22 +0200203 NativeLoaderNamespace* parent_ns = FindParentNamespaceByClassLoader(env, class_loader);
Dimitry Ivanov24db75c2016-05-12 15:34:41 -0700204
Zhenhua WANGf2804e52016-05-30 11:16:08 +0800205 bool is_native_bridge = false;
Dimitry Ivanovac1b1912015-12-01 13:56:44 -0800206
Victor Khimenko1443ec42018-07-09 17:01:22 +0200207 if (parent_ns != nullptr) {
208 is_native_bridge = !parent_ns->is_android_namespace();
Zhenhua WANGf2804e52016-05-30 11:16:08 +0800209 } else if (!library_path.empty()) {
210 is_native_bridge = NativeBridgeIsPathSupported(library_path.c_str());
Dimitry Ivanovd047c922016-02-23 14:23:51 -0800211 }
Dimitry Ivanovac1b1912015-12-01 13:56:44 -0800212
Jiyong Parka07f3052017-08-22 10:26:10 +0900213 std::string system_exposed_libraries = system_public_libraries_;
214 const char* namespace_name = kClassloaderNamespaceName;
215 android_namespace_t* vndk_ns = nullptr;
216 if (is_for_vendor && !is_shared) {
217 LOG_FATAL_IF(is_native_bridge, "Unbundled vendor apk must not use translated architecture");
218
219 // For vendor apks, give access to the vendor lib even though
220 // they are treated as unbundled; the libs and apks are still bundled
221 // together in the vendor partition.
222#if defined(__LP64__)
223 std::string vendor_lib_path = "/vendor/lib64";
224#else
225 std::string vendor_lib_path = "/vendor/lib";
226#endif
227 library_path = library_path + ":" + vendor_lib_path.c_str();
228 permitted_path = permitted_path + ":" + vendor_lib_path.c_str();
229
230 // Also give access to LLNDK libraries since they are available to vendors
231 system_exposed_libraries = system_exposed_libraries + ":" + system_llndk_libraries_.c_str();
232
233 // Give access to VNDK-SP libraries from the 'vndk' namespace.
234 vndk_ns = android_get_exported_namespace(kVndkNamespaceName);
235 LOG_ALWAYS_FATAL_IF(vndk_ns == nullptr,
236 "Cannot find \"%s\" namespace for vendor apks", kVndkNamespaceName);
237
238 // Different name is useful for debugging
239 namespace_name = kVendorClassloaderNamespaceName;
240 ALOGD("classloader namespace configured for unbundled vendor apk. library_path=%s", library_path.c_str());
Inseob Kim67cb0562018-05-04 11:39:12 +0900241 } else {
242 // oem and product public libraries are NOT available to vendor apks, otherwise it
Jiyong Parke0319942018-01-15 21:14:53 +0900243 // would be system->vendor violation.
Inseob Kim67cb0562018-05-04 11:39:12 +0900244 if (!oem_public_libraries_.empty()) {
245 system_exposed_libraries = system_exposed_libraries + ':' + oem_public_libraries_;
246 }
247 if (!product_public_libraries_.empty()) {
248 system_exposed_libraries = system_exposed_libraries + ':' + product_public_libraries_;
249 }
Jiyong Parka07f3052017-08-22 10:26:10 +0900250 }
251
Zhenhua WANGf2804e52016-05-30 11:16:08 +0800252 NativeLoaderNamespace native_loader_ns;
253 if (!is_native_bridge) {
Victor Khimenko1443ec42018-07-09 17:01:22 +0200254 android_namespace_t* android_parent_ns =
255 parent_ns == nullptr ? nullptr : parent_ns->get_android_ns();
Jiyong Parka07f3052017-08-22 10:26:10 +0900256 android_namespace_t* ns = android_create_namespace(namespace_name,
Zhenhua WANGf2804e52016-05-30 11:16:08 +0800257 nullptr,
258 library_path.c_str(),
259 namespace_type,
260 permitted_path.c_str(),
Victor Khimenko1443ec42018-07-09 17:01:22 +0200261 android_parent_ns);
Zhenhua WANGf2804e52016-05-30 11:16:08 +0800262 if (ns == nullptr) {
263 *error_msg = dlerror();
Victor Khimenko1443ec42018-07-09 17:01:22 +0200264 return nullptr;
Zhenhua WANGf2804e52016-05-30 11:16:08 +0800265 }
266
Dimitry Ivanov882cad22017-05-01 15:12:49 -0700267 // Note that when vendor_ns is not configured this function will return nullptr
268 // and it will result in linking vendor_public_libraries_ to the default namespace
269 // which is expected behavior in this case.
270 android_namespace_t* vendor_ns = android_get_exported_namespace(kVendorNamespaceName);
271
Jiyong Parka07f3052017-08-22 10:26:10 +0900272 if (!android_link_namespaces(ns, nullptr, system_exposed_libraries.c_str())) {
Dimitry Ivanov26e1a842017-02-03 14:11:27 -0800273 *error_msg = dlerror();
Victor Khimenko1443ec42018-07-09 17:01:22 +0200274 return nullptr;
Dimitry Ivanov26e1a842017-02-03 14:11:27 -0800275 }
276
Jiyong Parka07f3052017-08-22 10:26:10 +0900277 if (vndk_ns != nullptr && !system_vndksp_libraries_.empty()) {
278 // vendor apks are allowed to use VNDK-SP libraries.
279 if (!android_link_namespaces(ns, vndk_ns, system_vndksp_libraries_.c_str())) {
280 *error_msg = dlerror();
Victor Khimenko1443ec42018-07-09 17:01:22 +0200281 return nullptr;
Jiyong Parka07f3052017-08-22 10:26:10 +0900282 }
283 }
284
Dimitry Ivanov882cad22017-05-01 15:12:49 -0700285 if (!vendor_public_libraries_.empty()) {
286 if (!android_link_namespaces(ns, vendor_ns, vendor_public_libraries_.c_str())) {
287 *error_msg = dlerror();
Victor Khimenko1443ec42018-07-09 17:01:22 +0200288 return nullptr;
Dimitry Ivanov882cad22017-05-01 15:12:49 -0700289 }
290 }
291
Zhenhua WANGf2804e52016-05-30 11:16:08 +0800292 native_loader_ns = NativeLoaderNamespace(ns);
293 } else {
Victor Khimenko1443ec42018-07-09 17:01:22 +0200294 native_bridge_namespace_t* native_bridge_parent_namespace =
295 parent_ns == nullptr ? nullptr : parent_ns->get_native_bridge_ns();
Jiyong Parka07f3052017-08-22 10:26:10 +0900296 native_bridge_namespace_t* ns = NativeBridgeCreateNamespace(namespace_name,
Zhenhua WANGf2804e52016-05-30 11:16:08 +0800297 nullptr,
298 library_path.c_str(),
299 namespace_type,
300 permitted_path.c_str(),
Victor Khimenko1443ec42018-07-09 17:01:22 +0200301 native_bridge_parent_namespace);
Zhenhua WANGf2804e52016-05-30 11:16:08 +0800302 if (ns == nullptr) {
303 *error_msg = NativeBridgeGetError();
Victor Khimenko1443ec42018-07-09 17:01:22 +0200304 return nullptr;
Zhenhua WANGf2804e52016-05-30 11:16:08 +0800305 }
306
Dimitry Ivanov882cad22017-05-01 15:12:49 -0700307 native_bridge_namespace_t* vendor_ns = NativeBridgeGetVendorNamespace();
308
Jiyong Parka07f3052017-08-22 10:26:10 +0900309 if (!NativeBridgeLinkNamespaces(ns, nullptr, system_exposed_libraries.c_str())) {
Zhenhua WANGe8fb11d2017-02-27 10:14:45 +0800310 *error_msg = NativeBridgeGetError();
Victor Khimenko1443ec42018-07-09 17:01:22 +0200311 return nullptr;
Zhenhua WANGe8fb11d2017-02-27 10:14:45 +0800312 }
313
Dimitry Ivanov882cad22017-05-01 15:12:49 -0700314 if (!vendor_public_libraries_.empty()) {
315 if (!NativeBridgeLinkNamespaces(ns, vendor_ns, vendor_public_libraries_.c_str())) {
316 *error_msg = NativeBridgeGetError();
Victor Khimenko1443ec42018-07-09 17:01:22 +0200317 return nullptr;
Dimitry Ivanov882cad22017-05-01 15:12:49 -0700318 }
319 }
320
Zhenhua WANGf2804e52016-05-30 11:16:08 +0800321 native_loader_ns = NativeLoaderNamespace(ns);
322 }
323
324 namespaces_.push_back(std::make_pair(env->NewWeakGlobalRef(class_loader), native_loader_ns));
325
Victor Khimenko1443ec42018-07-09 17:01:22 +0200326 return &(namespaces_.back().second);
Dimitry Ivanovac1b1912015-12-01 13:56:44 -0800327 }
328
Victor Khimenko1443ec42018-07-09 17:01:22 +0200329 NativeLoaderNamespace* FindNamespaceByClassLoader(JNIEnv* env, jobject class_loader) {
Dimitry Ivanovf44ecde2016-02-22 13:48:22 -0800330 auto it = std::find_if(namespaces_.begin(), namespaces_.end(),
Zhenhua WANGf2804e52016-05-30 11:16:08 +0800331 [&](const std::pair<jweak, NativeLoaderNamespace>& value) {
Dimitry Ivanovf44ecde2016-02-22 13:48:22 -0800332 return env->IsSameObject(value.first, class_loader);
333 });
Zhenhua WANGf2804e52016-05-30 11:16:08 +0800334 if (it != namespaces_.end()) {
Victor Khimenko1443ec42018-07-09 17:01:22 +0200335 return &it->second;
Zhenhua WANGf2804e52016-05-30 11:16:08 +0800336 }
337
Victor Khimenko1443ec42018-07-09 17:01:22 +0200338 return nullptr;
Dimitry Ivanovf44ecde2016-02-22 13:48:22 -0800339 }
340
Dimitry Ivanov4b0e9632016-03-15 13:51:26 -0700341 void Initialize() {
Dimitry Ivanov80ddb8f2016-05-09 18:12:00 -0700342 // Once public namespace is initialized there is no
343 // point in running this code - it will have no effect
344 // on the current list of public libraries.
345 if (initialized_) {
346 return;
347 }
348
Dimitry Ivanovb6140452016-04-06 18:24:08 -0700349 std::vector<std::string> sonames;
Dimitry Ivanov0b5651e2016-04-21 16:42:48 -0700350 const char* android_root_env = getenv("ANDROID_ROOT");
351 std::string root_dir = android_root_env != nullptr ? android_root_env : "/system";
352 std::string public_native_libraries_system_config =
353 root_dir + kPublicNativeLibrariesSystemConfigPathFromRoot;
Jiyong Parka07f3052017-08-22 10:26:10 +0900354 std::string llndk_native_libraries_system_config =
355 root_dir + kLlndkNativeLibrariesSystemConfigPathFromRoot;
356 std::string vndksp_native_libraries_system_config =
357 root_dir + kVndkspNativeLibrariesSystemConfigPathFromRoot;
Dimitry Ivanovb6140452016-04-06 18:24:08 -0700358
Inseob Kim67cb0562018-05-04 11:39:12 +0900359 std::string product_public_native_libraries_dir = "/product/etc";
360
Christopher Ferris39da84b2016-06-21 16:11:23 -0700361 std::string error_msg;
Jiyong Parkd1006fe2017-11-08 18:44:09 +0900362 LOG_ALWAYS_FATAL_IF(
363 !ReadConfig(public_native_libraries_system_config, &sonames, always_true, &error_msg),
364 "Error reading public native library list from \"%s\": %s",
365 public_native_libraries_system_config.c_str(), error_msg.c_str());
366
Jiyong Parke0319942018-01-15 21:14:53 +0900367 // For debuggable platform builds use ANDROID_ADDITIONAL_PUBLIC_LIBRARIES environment
368 // variable to add libraries to the list. This is intended for platform tests only.
369 if (is_debuggable()) {
370 const char* additional_libs = getenv("ANDROID_ADDITIONAL_PUBLIC_LIBRARIES");
371 if (additional_libs != nullptr && additional_libs[0] != '\0') {
372 std::vector<std::string> additional_libs_vector = base::Split(additional_libs, ":");
373 std::copy(additional_libs_vector.begin(), additional_libs_vector.end(),
374 std::back_inserter(sonames));
375 }
376 }
377
378 // android_init_namespaces() expects all the public libraries
379 // to be loaded so that they can be found by soname alone.
380 //
381 // TODO(dimitry): this is a bit misleading since we do not know
382 // if the vendor public library is going to be opened from /vendor/lib
Inseob Kim67cb0562018-05-04 11:39:12 +0900383 // we might as well end up loading them from /system/lib or /product/lib
Jiyong Parke0319942018-01-15 21:14:53 +0900384 // For now we rely on CTS test to catch things like this but
385 // it should probably be addressed in the future.
386 for (const auto& soname : sonames) {
387 LOG_ALWAYS_FATAL_IF(dlopen(soname.c_str(), RTLD_NOW | RTLD_NODELETE) == nullptr,
388 "Error preloading public library %s: %s", soname.c_str(), dlerror());
389 }
390
391 system_public_libraries_ = base::Join(sonames, ':');
392
Jiyong Parkd1006fe2017-11-08 18:44:09 +0900393 // read /system/etc/public.libraries-<companyname>.txt which contain partner defined
394 // system libs that are exposed to apps. The libs in the txt files must be
395 // named as lib<name>.<companyname>.so.
Jiyong Parke0319942018-01-15 21:14:53 +0900396 sonames.clear();
Inseob Kim67cb0562018-05-04 11:39:12 +0900397 ReadExtensionLibraries(base::Dirname(public_native_libraries_system_config).c_str(), &sonames);
Jiyong Parke0319942018-01-15 21:14:53 +0900398 oem_public_libraries_ = base::Join(sonames, ':');
Dimitry Ivanov7d028292016-05-05 17:30:24 -0700399
Inseob Kim67cb0562018-05-04 11:39:12 +0900400 // read /product/etc/public.libraries-<companyname>.txt which contain partner defined
401 // product libs that are exposed to apps.
402 sonames.clear();
403 ReadExtensionLibraries(product_public_native_libraries_dir.c_str(), &sonames);
404 product_public_libraries_ = base::Join(sonames, ':');
405
Justin Yun4a1d1102017-11-27 17:04:14 +0900406 // Insert VNDK version to llndk and vndksp config file names.
407 insert_vndk_version_str(&llndk_native_libraries_system_config);
408 insert_vndk_version_str(&vndksp_native_libraries_system_config);
409
Dimitry Ivanov882cad22017-05-01 15:12:49 -0700410 sonames.clear();
Jiyong Parkd1006fe2017-11-08 18:44:09 +0900411 ReadConfig(llndk_native_libraries_system_config, &sonames, always_true);
Jiyong Parka07f3052017-08-22 10:26:10 +0900412 system_llndk_libraries_ = base::Join(sonames, ':');
413
414 sonames.clear();
Jiyong Parkd1006fe2017-11-08 18:44:09 +0900415 ReadConfig(vndksp_native_libraries_system_config, &sonames, always_true);
Jiyong Parka07f3052017-08-22 10:26:10 +0900416 system_vndksp_libraries_ = base::Join(sonames, ':');
417
418 sonames.clear();
Dimitry Ivanov882cad22017-05-01 15:12:49 -0700419 // This file is optional, quietly ignore if the file does not exist.
Jiyong Parkd1006fe2017-11-08 18:44:09 +0900420 ReadConfig(kPublicNativeLibrariesVendorConfig, &sonames, always_true, nullptr);
Dimitry Ivanov882cad22017-05-01 15:12:49 -0700421
422 vendor_public_libraries_ = base::Join(sonames, ':');
Dimitry Ivanovb6140452016-04-06 18:24:08 -0700423 }
424
Inseob Kim67cb0562018-05-04 11:39:12 +0900425 void Reset() { namespaces_.clear(); }
Dimitry Ivanovbe4ca3a2016-05-02 10:43:16 -0700426
Dimitry Ivanovb6140452016-04-06 18:24:08 -0700427 private:
Inseob Kim67cb0562018-05-04 11:39:12 +0900428 void ReadExtensionLibraries(const char* dirname, std::vector<std::string>* sonames) {
429 std::unique_ptr<DIR, decltype(&closedir)> dir(opendir(dirname), closedir);
430 if (dir != nullptr) {
431 // Failing to opening the dir is not an error, which can happen in
432 // webview_zygote.
433 while (struct dirent* ent = readdir(dir.get())) {
434 if (ent->d_type != DT_REG && ent->d_type != DT_LNK) {
435 continue;
436 }
437 const std::string filename(ent->d_name);
438 if (android::base::StartsWith(filename, kPublicNativeLibrariesExtensionConfigPrefix) &&
439 android::base::EndsWith(filename, kPublicNativeLibrariesExtensionConfigSuffix)) {
440 const size_t start = kPublicNativeLibrariesExtensionConfigPrefixLen;
441 const size_t end = filename.size() - kPublicNativeLibrariesExtensionConfigSuffixLen;
442 const std::string company_name = filename.substr(start, end - start);
443 const std::string config_file_path = dirname + "/"s + filename;
444 LOG_ALWAYS_FATAL_IF(
445 company_name.empty(),
446 "Error extracting company name from public native library list file path \"%s\"",
447 config_file_path.c_str());
448
449 std::string error_msg;
450
451 LOG_ALWAYS_FATAL_IF(
452 !ReadConfig(
453 config_file_path, sonames,
454 [&company_name](const std::string& soname, std::string* error_msg) {
455 if (android::base::StartsWith(soname, "lib") &&
456 android::base::EndsWith(soname, "." + company_name + ".so")) {
457 return true;
458 } else {
459 *error_msg = "Library name \"" + soname +
460 "\" does not end with the company name: " + company_name + ".";
461 return false;
462 }
463 },
464 &error_msg),
465 "Error reading public native library list from \"%s\": %s", config_file_path.c_str(),
466 error_msg.c_str());
467 }
468 }
469 }
470 }
471
472
Christopher Ferris39da84b2016-06-21 16:11:23 -0700473 bool ReadConfig(const std::string& configFile, std::vector<std::string>* sonames,
Jiyong Parkd1006fe2017-11-08 18:44:09 +0900474 const std::function<bool(const std::string& /* soname */,
475 std::string* /* error_msg */)>& check_soname,
Christopher Ferris39da84b2016-06-21 16:11:23 -0700476 std::string* error_msg = nullptr) {
Dimitry Ivanov4b0e9632016-03-15 13:51:26 -0700477 // Read list of public native libraries from the config file.
478 std::string file_content;
Dimitry Ivanovb6140452016-04-06 18:24:08 -0700479 if(!base::ReadFileToString(configFile, &file_content)) {
Christopher Ferris39da84b2016-06-21 16:11:23 -0700480 if (error_msg) *error_msg = strerror(errno);
Dimitry Ivanovb6140452016-04-06 18:24:08 -0700481 return false;
482 }
Dimitry Ivanov4b0e9632016-03-15 13:51:26 -0700483
484 std::vector<std::string> lines = base::Split(file_content, "\n");
485
Christopher Ferris39da84b2016-06-21 16:11:23 -0700486 for (auto& line : lines) {
Dimitry Ivanov4b0e9632016-03-15 13:51:26 -0700487 auto trimmed_line = base::Trim(line);
488 if (trimmed_line[0] == '#' || trimmed_line.empty()) {
489 continue;
490 }
Christopher Ferris39da84b2016-06-21 16:11:23 -0700491 size_t space_pos = trimmed_line.rfind(' ');
492 if (space_pos != std::string::npos) {
493 std::string type = trimmed_line.substr(space_pos + 1);
494 if (type != "32" && type != "64") {
495 if (error_msg) *error_msg = "Malformed line: " + line;
496 return false;
497 }
498#if defined(__LP64__)
499 // Skip 32 bit public library.
500 if (type == "32") {
501 continue;
502 }
503#else
504 // Skip 64 bit public library.
505 if (type == "64") {
506 continue;
507 }
508#endif
509 trimmed_line.resize(space_pos);
510 }
Dimitry Ivanov4b0e9632016-03-15 13:51:26 -0700511
Jiyong Parkd1006fe2017-11-08 18:44:09 +0900512 if (check_soname(trimmed_line, error_msg)) {
513 sonames->push_back(trimmed_line);
514 } else {
515 return false;
516 }
Dimitry Ivanov4b0e9632016-03-15 13:51:26 -0700517 }
518
Dimitry Ivanovb6140452016-04-06 18:24:08 -0700519 return true;
Dimitry Ivanovd68c8e92016-02-10 14:09:22 -0800520 }
Dimitry Ivanovac1b1912015-12-01 13:56:44 -0800521
Zhenhua WANGf2804e52016-05-30 11:16:08 +0800522 bool InitPublicNamespace(const char* library_path, std::string* error_msg) {
523 // Ask native bride if this apps library path should be handled by it
524 bool is_native_bridge = NativeBridgeIsPathSupported(library_path);
525
Dimitry Ivanov4b0e9632016-03-15 13:51:26 -0700526 // (http://b/25844435) - Some apps call dlopen from generated code (mono jited
527 // code is one example) unknown to linker in which case linker uses anonymous
528 // namespace. The second argument specifies the search path for the anonymous
529 // namespace which is the library_path of the classloader.
Dimitry Ivanov882cad22017-05-01 15:12:49 -0700530 initialized_ = android_init_anonymous_namespace(system_public_libraries_.c_str(),
Dimitry Ivanov26e1a842017-02-03 14:11:27 -0800531 is_native_bridge ? nullptr : library_path);
Dimitry Ivanovd836ab02016-11-02 18:03:10 -0700532 if (!initialized_) {
533 *error_msg = dlerror();
534 return false;
535 }
536
537 // and now initialize native bridge namespaces if necessary.
538 if (NativeBridgeInitialized()) {
Dimitry Ivanov882cad22017-05-01 15:12:49 -0700539 initialized_ = NativeBridgeInitAnonymousNamespace(system_public_libraries_.c_str(),
Zhenhua WANGe8fb11d2017-02-27 10:14:45 +0800540 is_native_bridge ? library_path : nullptr);
Zhenhua WANGf2804e52016-05-30 11:16:08 +0800541 if (!initialized_) {
542 *error_msg = NativeBridgeGetError();
543 }
544 }
Dimitry Ivanovac1b1912015-12-01 13:56:44 -0800545
546 return initialized_;
547 }
548
Dimitry Ivanov24db75c2016-05-12 15:34:41 -0700549 jobject GetParentClassLoader(JNIEnv* env, jobject class_loader) {
550 jclass class_loader_class = env->FindClass("java/lang/ClassLoader");
551 jmethodID get_parent = env->GetMethodID(class_loader_class,
552 "getParent",
553 "()Ljava/lang/ClassLoader;");
554
555 return env->CallObjectMethod(class_loader, get_parent);
556 }
557
Victor Khimenko1443ec42018-07-09 17:01:22 +0200558 NativeLoaderNamespace* FindParentNamespaceByClassLoader(JNIEnv* env, jobject class_loader) {
Dimitry Ivanov24db75c2016-05-12 15:34:41 -0700559 jobject parent_class_loader = GetParentClassLoader(env, class_loader);
560
561 while (parent_class_loader != nullptr) {
Victor Khimenko1443ec42018-07-09 17:01:22 +0200562 NativeLoaderNamespace* ns;
563 if ((ns = FindNamespaceByClassLoader(env, parent_class_loader)) != nullptr) {
564 return ns;
Dimitry Ivanov24db75c2016-05-12 15:34:41 -0700565 }
566
567 parent_class_loader = GetParentClassLoader(env, parent_class_loader);
568 }
Zhenhua WANGf2804e52016-05-30 11:16:08 +0800569
Victor Khimenko1443ec42018-07-09 17:01:22 +0200570 return nullptr;
Dimitry Ivanov24db75c2016-05-12 15:34:41 -0700571 }
572
Dimitry Ivanovac1b1912015-12-01 13:56:44 -0800573 bool initialized_;
Victor Khimenko1443ec42018-07-09 17:01:22 +0200574 std::list<std::pair<jweak, NativeLoaderNamespace>> namespaces_;
Dimitry Ivanov882cad22017-05-01 15:12:49 -0700575 std::string system_public_libraries_;
576 std::string vendor_public_libraries_;
Jiyong Parke0319942018-01-15 21:14:53 +0900577 std::string oem_public_libraries_;
Inseob Kim67cb0562018-05-04 11:39:12 +0900578 std::string product_public_libraries_;
Jiyong Parka07f3052017-08-22 10:26:10 +0900579 std::string system_llndk_libraries_;
580 std::string system_vndksp_libraries_;
Dimitry Ivanovac1b1912015-12-01 13:56:44 -0800581
582 DISALLOW_COPY_AND_ASSIGN(LibraryNamespaces);
583};
584
Dimitry Ivanov34d5a202016-02-29 13:21:43 -0800585static std::mutex g_namespaces_mutex;
Dimitry Ivanovac1b1912015-12-01 13:56:44 -0800586static LibraryNamespaces* g_namespaces = new LibraryNamespaces;
587#endif
588
Dimitry Ivanov4b0e9632016-03-15 13:51:26 -0700589void InitializeNativeLoader() {
Dimitry Ivanov426799d2016-02-22 11:27:48 -0800590#if defined(__ANDROID__)
Dimitry Ivanov34d5a202016-02-29 13:21:43 -0800591 std::lock_guard<std::mutex> guard(g_namespaces_mutex);
Dimitry Ivanov4b0e9632016-03-15 13:51:26 -0700592 g_namespaces->Initialize();
Dimitry Ivanov426799d2016-02-22 11:27:48 -0800593#endif
594}
595
Dimitry Ivanovbe4ca3a2016-05-02 10:43:16 -0700596void ResetNativeLoader() {
597#if defined(__ANDROID__)
598 std::lock_guard<std::mutex> guard(g_namespaces_mutex);
599 g_namespaces->Reset();
600#endif
601}
Dimitry Ivanovac1b1912015-12-01 13:56:44 -0800602
Dimitry Ivanovd047c922016-02-23 14:23:51 -0800603jstring CreateClassLoaderNamespace(JNIEnv* env,
604 int32_t target_sdk_version,
605 jobject class_loader,
606 bool is_shared,
Jiyong Parka07f3052017-08-22 10:26:10 +0900607 bool is_for_vendor,
Dimitry Ivanovd047c922016-02-23 14:23:51 -0800608 jstring library_path,
609 jstring permitted_path) {
Dimitry Ivanovac1b1912015-12-01 13:56:44 -0800610#if defined(__ANDROID__)
Dimitry Ivanov34d5a202016-02-29 13:21:43 -0800611 std::lock_guard<std::mutex> guard(g_namespaces_mutex);
Zhenhua WANGf2804e52016-05-30 11:16:08 +0800612
613 std::string error_msg;
Zhenhua WANGf2804e52016-05-30 11:16:08 +0800614 bool success = g_namespaces->Create(env,
Dimitry Ivanov9e253ce2017-05-08 22:24:24 -0700615 target_sdk_version,
Zhenhua WANGf2804e52016-05-30 11:16:08 +0800616 class_loader,
617 is_shared,
Jiyong Parka07f3052017-08-22 10:26:10 +0900618 is_for_vendor,
Zhenhua WANGf2804e52016-05-30 11:16:08 +0800619 library_path,
620 permitted_path,
Victor Khimenko1443ec42018-07-09 17:01:22 +0200621 &error_msg) != nullptr;
Zhenhua WANGf2804e52016-05-30 11:16:08 +0800622 if (!success) {
623 return env->NewStringUTF(error_msg.c_str());
Dimitry Ivanovd047c922016-02-23 14:23:51 -0800624 }
625#else
Jiyong Parka07f3052017-08-22 10:26:10 +0900626 UNUSED(env, target_sdk_version, class_loader, is_shared, is_for_vendor,
Dimitry Ivanovd047c922016-02-23 14:23:51 -0800627 library_path, permitted_path);
628#endif
629 return nullptr;
630}
631
Nicolas Geoffray890e3bf2019-01-22 09:11:57 +0000632#if defined(__ANDROID__)
633static android_namespace_t* FindExportedNamespace(const char* caller_location) {
634 std::string location = caller_location;
635 // Lots of implicit assumptions here: we expect `caller_location` to be of the form:
636 // /apex/com.android...modulename/...
637 //
638 // And we extract from it 'modulename', which is the name of the linker namespace.
639 if (android::base::StartsWith(location, kApexPath)) {
640 size_t slash_index = location.find_first_of('/', strlen(kApexPath));
641 LOG_ALWAYS_FATAL_IF((slash_index == std::string::npos),
642 "Error finding namespace of apex: no slash in path %s", caller_location);
643 size_t dot_index = location.find_last_of('.', slash_index);
644 LOG_ALWAYS_FATAL_IF((dot_index == std::string::npos),
645 "Error finding namespace of apex: no dot in apex name %s", caller_location);
646 std::string name = location.substr(dot_index + 1, slash_index - dot_index - 1);
647 android_namespace_t* boot_namespace = android_get_exported_namespace(name.c_str());
648 LOG_ALWAYS_FATAL_IF((boot_namespace == nullptr),
649 "Error finding namespace of apex: no namespace called %s", name.c_str());
650 return boot_namespace;
651 }
652 return nullptr;
653}
654#endif
655
Nicolas Geoffrayc3a73dc2019-01-12 15:01:20 +0000656void* OpenNativeLibrary(JNIEnv* env, int32_t target_sdk_version, const char* path,
Nicolas Geoffray53535022019-01-18 10:05:13 +0000657 jobject class_loader, const char* caller_location, jstring library_path,
658 bool* needs_native_bridge, char** error_msg) {
Dimitry Ivanovd047c922016-02-23 14:23:51 -0800659#if defined(__ANDROID__)
Dimitry Ivanov5539db02016-04-20 16:07:30 -0700660 UNUSED(target_sdk_version);
661 if (class_loader == nullptr) {
Zhenhua WANGf2804e52016-05-30 11:16:08 +0800662 *needs_native_bridge = false;
Nicolas Geoffray890e3bf2019-01-22 09:11:57 +0000663 if (caller_location != nullptr) {
664 android_namespace_t* boot_namespace = FindExportedNamespace(caller_location);
665 if (boot_namespace != nullptr) {
666 const android_dlextinfo dlextinfo = {
667 .flags = ANDROID_DLEXT_USE_NAMESPACE,
668 .library_namespace = boot_namespace,
669 };
670 void* handle = android_dlopen_ext(path, RTLD_NOW, &dlextinfo);
671 if (handle == nullptr) {
672 *error_msg = strdup(dlerror());
673 }
674 return handle;
675 }
676 }
Pete Bentley632f1422018-12-19 13:33:33 +0000677 void* handle = dlopen(path, RTLD_NOW);
678 if (handle == nullptr) {
Nicolas Geoffrayd06cb942019-01-16 20:20:27 +0000679 *error_msg = strdup(dlerror());
Pete Bentley632f1422018-12-19 13:33:33 +0000680 }
681 return handle;
Dimitry Ivanovac1b1912015-12-01 13:56:44 -0800682 }
683
Dimitry Ivanov34d5a202016-02-29 13:21:43 -0800684 std::lock_guard<std::mutex> guard(g_namespaces_mutex);
Victor Khimenko1443ec42018-07-09 17:01:22 +0200685 NativeLoaderNamespace* ns;
Dimitry Ivanovac1b1912015-12-01 13:56:44 -0800686
Victor Khimenko1443ec42018-07-09 17:01:22 +0200687 if ((ns = g_namespaces->FindNamespaceByClassLoader(env, class_loader)) == nullptr) {
Dimitry Ivanovd047c922016-02-23 14:23:51 -0800688 // This is the case where the classloader was not created by ApplicationLoaders
689 // In this case we create an isolated not-shared namespace for it.
Nicolas Geoffrayc3a73dc2019-01-12 15:01:20 +0000690 std::string create_error_msg;
691 if ((ns = g_namespaces->Create(env, target_sdk_version, class_loader, false /* is_shared */,
692 false /* is_for_vendor */, library_path, nullptr,
693 &create_error_msg)) == nullptr) {
694 *error_msg = strdup(create_error_msg.c_str());
Dimitry Ivanovd047c922016-02-23 14:23:51 -0800695 return nullptr;
696 }
Dimitry Ivanovac1b1912015-12-01 13:56:44 -0800697 }
698
Nicolas Geoffrayc3a73dc2019-01-12 15:01:20 +0000699 return OpenNativeLibraryInNamespace(ns, path, needs_native_bridge, error_msg);
Dimitry Ivanovac1b1912015-12-01 13:56:44 -0800700#else
Nicolas Geoffray53535022019-01-18 10:05:13 +0000701 UNUSED(env, target_sdk_version, class_loader, caller_location);
Andreas Gampe5c7d5822017-12-28 19:08:13 -0800702
703 // Do some best effort to emulate library-path support. It will not
704 // work for dependencies.
705 //
706 // Note: null has a special meaning and must be preserved.
707 std::string c_library_path; // Empty string by default.
708 if (library_path != nullptr && path != nullptr && path[0] != '/') {
709 ScopedUtfChars library_path_utf_chars(env, library_path);
710 c_library_path = library_path_utf_chars.c_str();
711 }
712
713 std::vector<std::string> library_paths = base::Split(c_library_path, ":");
714
715 for (const std::string& lib_path : library_paths) {
716 *needs_native_bridge = false;
717 const char* path_arg;
718 std::string complete_path;
719 if (path == nullptr) {
720 // Preserve null.
721 path_arg = nullptr;
Zhenhua WANGf2804e52016-05-30 11:16:08 +0800722 } else {
Andreas Gampe5c7d5822017-12-28 19:08:13 -0800723 complete_path = lib_path;
724 if (!complete_path.empty()) {
725 complete_path.append("/");
726 }
727 complete_path.append(path);
728 path_arg = complete_path.c_str();
729 }
730 void* handle = dlopen(path_arg, RTLD_NOW);
731 if (handle != nullptr) {
732 return handle;
733 }
734 if (NativeBridgeIsSupported(path_arg)) {
735 *needs_native_bridge = true;
736 handle = NativeBridgeLoadLibrary(path_arg, RTLD_NOW);
737 if (handle != nullptr) {
738 return handle;
739 }
Nicolas Geoffrayc3a73dc2019-01-12 15:01:20 +0000740 *error_msg = strdup(NativeBridgeGetError());
Andreas Gampe5c7d5822017-12-28 19:08:13 -0800741 } else {
Nicolas Geoffrayc3a73dc2019-01-12 15:01:20 +0000742 *error_msg = strdup(dlerror());
Zhenhua WANGf2804e52016-05-30 11:16:08 +0800743 }
744 }
Andreas Gampe5c7d5822017-12-28 19:08:13 -0800745 return nullptr;
Dimitry Ivanovac1b1912015-12-01 13:56:44 -0800746#endif
747}
748
Nicolas Geoffrayc3a73dc2019-01-12 15:01:20 +0000749bool CloseNativeLibrary(void* handle, const bool needs_native_bridge, char** error_msg) {
dimitry3150f7c2018-09-12 01:09:19 +0200750 bool success;
751 if (needs_native_bridge) {
752 success = (NativeBridgeUnloadLibrary(handle) == 0);
753 if (!success) {
Nicolas Geoffrayc3a73dc2019-01-12 15:01:20 +0000754 *error_msg = strdup(NativeBridgeGetError());
dimitry3150f7c2018-09-12 01:09:19 +0200755 }
756 } else {
757 success = (dlclose(handle) == 0);
758 if (!success) {
Nicolas Geoffrayc3a73dc2019-01-12 15:01:20 +0000759 *error_msg = strdup(dlerror());
dimitry3150f7c2018-09-12 01:09:19 +0200760 }
761 }
762
763 return success;
Dimitry Ivanov09a516b2016-05-03 14:55:25 -0700764}
765
Nicolas Geoffrayc3a73dc2019-01-12 15:01:20 +0000766void NativeLoaderFreeErrorMessage(char* msg) {
767 // The error messages get allocated through strdup, so we must call free on them.
768 free(msg);
769}
770
Dimitry Ivanovf44ecde2016-02-22 13:48:22 -0800771#if defined(__ANDROID__)
Nicolas Geoffrayc3a73dc2019-01-12 15:01:20 +0000772void* OpenNativeLibraryInNamespace(NativeLoaderNamespace* ns, const char* path,
773 bool* needs_native_bridge, char** error_msg) {
Victor Khimenko1443ec42018-07-09 17:01:22 +0200774 if (ns->is_android_namespace()) {
775 android_dlextinfo extinfo;
776 extinfo.flags = ANDROID_DLEXT_USE_NAMESPACE;
777 extinfo.library_namespace = ns->get_android_ns();
778
779 void* handle = android_dlopen_ext(path, RTLD_NOW, &extinfo);
780 if (handle == nullptr) {
Nicolas Geoffrayc3a73dc2019-01-12 15:01:20 +0000781 *error_msg = strdup(dlerror());
Victor Khimenko1443ec42018-07-09 17:01:22 +0200782 }
783 *needs_native_bridge = false;
784 return handle;
785 } else {
786 void* handle = NativeBridgeLoadLibraryExt(path, RTLD_NOW, ns->get_native_bridge_ns());
787 if (handle == nullptr) {
Nicolas Geoffrayc3a73dc2019-01-12 15:01:20 +0000788 *error_msg = strdup(NativeBridgeGetError());
Victor Khimenko1443ec42018-07-09 17:01:22 +0200789 }
790 *needs_native_bridge = true;
791 return handle;
792 }
793}
794
Dimitry Ivanov800083d2016-11-01 14:17:00 -0700795// native_bridge_namespaces are not supported for callers of this function.
796// This function will return nullptr in the case when application is running
797// on native bridge.
Dimitry Ivanovf44ecde2016-02-22 13:48:22 -0800798android_namespace_t* FindNamespaceByClassLoader(JNIEnv* env, jobject class_loader) {
Dimitry Ivanov34d5a202016-02-29 13:21:43 -0800799 std::lock_guard<std::mutex> guard(g_namespaces_mutex);
Victor Khimenko1443ec42018-07-09 17:01:22 +0200800 NativeLoaderNamespace* ns = g_namespaces->FindNamespaceByClassLoader(env, class_loader);
801 if (ns != nullptr) {
802 return ns->is_android_namespace() ? ns->get_android_ns() : nullptr;
Zhenhua WANGf2804e52016-05-30 11:16:08 +0800803 }
804
805 return nullptr;
Dimitry Ivanovf44ecde2016-02-22 13:48:22 -0800806}
Victor Khimenko1443ec42018-07-09 17:01:22 +0200807NativeLoaderNamespace* FindNativeLoaderNamespaceByClassLoader(JNIEnv* env, jobject class_loader) {
808 std::lock_guard<std::mutex> guard(g_namespaces_mutex);
809 return g_namespaces->FindNamespaceByClassLoader(env, class_loader);
810}
Dimitry Ivanovf44ecde2016-02-22 13:48:22 -0800811#endif
812
Dimitry Ivanovac1b1912015-12-01 13:56:44 -0800813}; // android namespace