Dimitry Ivanov | ac1b191 | 2015-12-01 13:56:44 -0800 | [diff] [blame] | 1 | /* |
| 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 Moreland | 00fe3ad | 2017-07-18 16:53:54 -0700 | [diff] [blame] | 18 | #include <nativehelper/ScopedUtfChars.h> |
Dimitry Ivanov | ac1b191 | 2015-12-01 13:56:44 -0800 | [diff] [blame] | 19 | |
| 20 | #include <dlfcn.h> |
| 21 | #ifdef __ANDROID__ |
Dimitry Ivanov | 7f9a1aa | 2016-03-22 13:59:59 -0700 | [diff] [blame] | 22 | #define LOG_TAG "libnativeloader" |
Jesse Hall | b75d82b | 2017-01-09 16:04:28 -0800 | [diff] [blame] | 23 | #include "nativeloader/dlext_namespaces.h" |
Mark Salyzyn | ff2dcd9 | 2016-09-28 15:54:45 -0700 | [diff] [blame] | 24 | #include "cutils/properties.h" |
Mark Salyzyn | 30f991f | 2017-01-10 13:19:54 -0800 | [diff] [blame] | 25 | #include "log/log.h" |
Dimitry Ivanov | ac1b191 | 2015-12-01 13:56:44 -0800 | [diff] [blame] | 26 | #endif |
Zhenhua WANG | f2804e5 | 2016-05-30 11:16:08 +0800 | [diff] [blame] | 27 | #include "nativebridge/native_bridge.h" |
Dimitry Ivanov | ac1b191 | 2015-12-01 13:56:44 -0800 | [diff] [blame] | 28 | |
| 29 | #include <algorithm> |
| 30 | #include <vector> |
| 31 | #include <string> |
| 32 | #include <mutex> |
| 33 | |
Mark Salyzyn | ff2dcd9 | 2016-09-28 15:54:45 -0700 | [diff] [blame] | 34 | #include <android-base/file.h> |
| 35 | #include <android-base/macros.h> |
| 36 | #include <android-base/strings.h> |
Dimitry Ivanov | ac1b191 | 2015-12-01 13:56:44 -0800 | [diff] [blame] | 37 | |
Justin Yun | 4a1d110 | 2017-11-27 17:04:14 +0900 | [diff] [blame] | 38 | #ifdef __BIONIC__ |
| 39 | #include <android-base/properties.h> |
| 40 | #endif |
| 41 | |
Zhenhua WANG | f2804e5 | 2016-05-30 11:16:08 +0800 | [diff] [blame] | 42 | #define CHECK(predicate) LOG_ALWAYS_FATAL_IF(!(predicate),\ |
| 43 | "%s:%d: %s CHECK '" #predicate "' failed.",\ |
| 44 | __FILE__, __LINE__, __FUNCTION__) |
| 45 | |
Dimitry Ivanov | ac1b191 | 2015-12-01 13:56:44 -0800 | [diff] [blame] | 46 | namespace android { |
| 47 | |
Dimitry Ivanov | 4b0e963 | 2016-03-15 13:51:26 -0700 | [diff] [blame] | 48 | #if defined(__ANDROID__) |
Zhenhua WANG | f2804e5 | 2016-05-30 11:16:08 +0800 | [diff] [blame] | 49 | class NativeLoaderNamespace { |
| 50 | public: |
| 51 | NativeLoaderNamespace() |
| 52 | : android_ns_(nullptr), native_bridge_ns_(nullptr) { } |
| 53 | |
| 54 | explicit NativeLoaderNamespace(android_namespace_t* ns) |
| 55 | : android_ns_(ns), native_bridge_ns_(nullptr) { } |
| 56 | |
| 57 | explicit NativeLoaderNamespace(native_bridge_namespace_t* ns) |
| 58 | : android_ns_(nullptr), native_bridge_ns_(ns) { } |
| 59 | |
| 60 | NativeLoaderNamespace(NativeLoaderNamespace&& that) = default; |
| 61 | NativeLoaderNamespace(const NativeLoaderNamespace& that) = default; |
| 62 | |
| 63 | NativeLoaderNamespace& operator=(const NativeLoaderNamespace& that) = default; |
| 64 | |
| 65 | android_namespace_t* get_android_ns() const { |
| 66 | CHECK(native_bridge_ns_ == nullptr); |
| 67 | return android_ns_; |
| 68 | } |
| 69 | |
| 70 | native_bridge_namespace_t* get_native_bridge_ns() const { |
| 71 | CHECK(android_ns_ == nullptr); |
| 72 | return native_bridge_ns_; |
| 73 | } |
| 74 | |
| 75 | bool is_android_namespace() const { |
| 76 | return native_bridge_ns_ == nullptr; |
| 77 | } |
| 78 | |
| 79 | private: |
| 80 | // Only one of them can be not null |
| 81 | android_namespace_t* android_ns_; |
| 82 | native_bridge_namespace_t* native_bridge_ns_; |
| 83 | }; |
| 84 | |
| 85 | static constexpr const char* kPublicNativeLibrariesSystemConfigPathFromRoot = |
| 86 | "/etc/public.libraries.txt"; |
| 87 | static constexpr const char* kPublicNativeLibrariesVendorConfig = |
| 88 | "/vendor/etc/public.libraries.txt"; |
Jiyong Park | a07f305 | 2017-08-22 10:26:10 +0900 | [diff] [blame] | 89 | static constexpr const char* kLlndkNativeLibrariesSystemConfigPathFromRoot = |
| 90 | "/etc/llndk.libraries.txt"; |
| 91 | static constexpr const char* kVndkspNativeLibrariesSystemConfigPathFromRoot = |
| 92 | "/etc/vndksp.libraries.txt"; |
| 93 | |
Dimitry Ivanov | ac1b191 | 2015-12-01 13:56:44 -0800 | [diff] [blame] | 94 | |
Dimitry Ivanov | 882cad2 | 2017-05-01 15:12:49 -0700 | [diff] [blame] | 95 | // The device may be configured to have the vendor libraries loaded to a separate namespace. |
| 96 | // For historical reasons this namespace was named sphal but effectively it is intended |
| 97 | // to use to load vendor libraries to separate namespace with controlled interface between |
| 98 | // vendor and system namespaces. |
| 99 | static constexpr const char* kVendorNamespaceName = "sphal"; |
| 100 | |
Jiyong Park | a07f305 | 2017-08-22 10:26:10 +0900 | [diff] [blame] | 101 | static constexpr const char* kVndkNamespaceName = "vndk"; |
| 102 | |
| 103 | static constexpr const char* kClassloaderNamespaceName = "classloader-namespace"; |
| 104 | static constexpr const char* kVendorClassloaderNamespaceName = "vendor-classloader-namespace"; |
| 105 | |
Dimitry Ivanov | f334cbf | 2016-05-10 10:39:48 -0700 | [diff] [blame] | 106 | // (http://b/27588281) This is a workaround for apps using custom classloaders and calling |
| 107 | // System.load() with an absolute path which is outside of the classloader library search path. |
| 108 | // This list includes all directories app is allowed to access this way. |
| 109 | static constexpr const char* kWhitelistedDirectories = "/data:/mnt/expand"; |
| 110 | |
Dimitry Ivanov | 7d02829 | 2016-05-05 17:30:24 -0700 | [diff] [blame] | 111 | static bool is_debuggable() { |
| 112 | char debuggable[PROP_VALUE_MAX]; |
| 113 | property_get("ro.debuggable", debuggable, "0"); |
| 114 | return std::string(debuggable) == "1"; |
| 115 | } |
| 116 | |
Justin Yun | 4a1d110 | 2017-11-27 17:04:14 +0900 | [diff] [blame] | 117 | static std::string vndk_version_str() { |
| 118 | #ifdef __BIONIC__ |
| 119 | std::string version = android::base::GetProperty("ro.vndk.version", ""); |
| 120 | if (version != "" && version != "current") { |
| 121 | return "." + version; |
| 122 | } |
| 123 | #endif |
| 124 | return ""; |
| 125 | } |
| 126 | |
| 127 | static void insert_vndk_version_str(std::string* file_name) { |
| 128 | CHECK(file_name != nullptr); |
| 129 | size_t insert_pos = file_name->find_last_of("."); |
| 130 | if (insert_pos == std::string::npos) { |
| 131 | insert_pos = file_name->length(); |
| 132 | } |
| 133 | file_name->insert(insert_pos, vndk_version_str()); |
| 134 | } |
| 135 | |
Dimitry Ivanov | ac1b191 | 2015-12-01 13:56:44 -0800 | [diff] [blame] | 136 | class LibraryNamespaces { |
| 137 | public: |
Dimitry Ivanov | 426799d | 2016-02-22 11:27:48 -0800 | [diff] [blame] | 138 | LibraryNamespaces() : initialized_(false) { } |
Dimitry Ivanov | ac1b191 | 2015-12-01 13:56:44 -0800 | [diff] [blame] | 139 | |
Zhenhua WANG | f2804e5 | 2016-05-30 11:16:08 +0800 | [diff] [blame] | 140 | bool Create(JNIEnv* env, |
Dimitry Ivanov | 9e253ce | 2017-05-08 22:24:24 -0700 | [diff] [blame] | 141 | uint32_t target_sdk_version, |
Zhenhua WANG | f2804e5 | 2016-05-30 11:16:08 +0800 | [diff] [blame] | 142 | jobject class_loader, |
| 143 | bool is_shared, |
Jiyong Park | a07f305 | 2017-08-22 10:26:10 +0900 | [diff] [blame] | 144 | bool is_for_vendor, |
Zhenhua WANG | f2804e5 | 2016-05-30 11:16:08 +0800 | [diff] [blame] | 145 | jstring java_library_path, |
| 146 | jstring java_permitted_path, |
| 147 | NativeLoaderNamespace* ns, |
| 148 | std::string* error_msg) { |
Dimitry Ivanov | cf9892b | 2016-05-09 10:55:50 -0700 | [diff] [blame] | 149 | std::string library_path; // empty string by default. |
| 150 | |
| 151 | if (java_library_path != nullptr) { |
| 152 | ScopedUtfChars library_path_utf_chars(env, java_library_path); |
| 153 | library_path = library_path_utf_chars.c_str(); |
| 154 | } |
Dimitry Ivanov | ac1b191 | 2015-12-01 13:56:44 -0800 | [diff] [blame] | 155 | |
Dimitry Ivanov | f334cbf | 2016-05-10 10:39:48 -0700 | [diff] [blame] | 156 | // (http://b/27588281) This is a workaround for apps using custom |
| 157 | // classloaders and calling System.load() with an absolute path which |
| 158 | // is outside of the classloader library search path. |
| 159 | // |
| 160 | // This part effectively allows such a classloader to access anything |
| 161 | // under /data and /mnt/expand |
| 162 | std::string permitted_path = kWhitelistedDirectories; |
| 163 | |
Dimitry Ivanov | 0d6e594 | 2015-12-08 11:16:56 -0800 | [diff] [blame] | 164 | if (java_permitted_path != nullptr) { |
| 165 | ScopedUtfChars path(env, java_permitted_path); |
Dimitry Ivanov | d0b1531 | 2016-05-10 16:21:25 -0700 | [diff] [blame] | 166 | if (path.c_str() != nullptr && path.size() > 0) { |
| 167 | permitted_path = permitted_path + ":" + path.c_str(); |
| 168 | } |
Dimitry Ivanov | 0d6e594 | 2015-12-08 11:16:56 -0800 | [diff] [blame] | 169 | } |
| 170 | |
Zhenhua WANG | f2804e5 | 2016-05-30 11:16:08 +0800 | [diff] [blame] | 171 | if (!initialized_ && !InitPublicNamespace(library_path.c_str(), error_msg)) { |
| 172 | return false; |
Dimitry Ivanov | ac1b191 | 2015-12-01 13:56:44 -0800 | [diff] [blame] | 173 | } |
| 174 | |
Zhenhua WANG | f2804e5 | 2016-05-30 11:16:08 +0800 | [diff] [blame] | 175 | bool found = FindNamespaceByClassLoader(env, class_loader, nullptr); |
Dimitry Ivanov | ac1b191 | 2015-12-01 13:56:44 -0800 | [diff] [blame] | 176 | |
Zhenhua WANG | f2804e5 | 2016-05-30 11:16:08 +0800 | [diff] [blame] | 177 | LOG_ALWAYS_FATAL_IF(found, |
Dimitry Ivanov | 4b0e963 | 2016-03-15 13:51:26 -0700 | [diff] [blame] | 178 | "There is already a namespace associated with this classloader"); |
Dimitry Ivanov | ac1b191 | 2015-12-01 13:56:44 -0800 | [diff] [blame] | 179 | |
Dimitry Ivanov | d2a6220 | 2015-12-15 11:06:57 -0800 | [diff] [blame] | 180 | uint64_t namespace_type = ANDROID_NAMESPACE_TYPE_ISOLATED; |
| 181 | if (is_shared) { |
| 182 | namespace_type |= ANDROID_NAMESPACE_TYPE_SHARED; |
| 183 | } |
| 184 | |
Dimitry Ivanov | 9e253ce | 2017-05-08 22:24:24 -0700 | [diff] [blame] | 185 | if (target_sdk_version < 24) { |
| 186 | namespace_type |= ANDROID_NAMESPACE_TYPE_GREYLIST_ENABLED; |
| 187 | } |
| 188 | |
Zhenhua WANG | f2804e5 | 2016-05-30 11:16:08 +0800 | [diff] [blame] | 189 | NativeLoaderNamespace parent_ns; |
| 190 | bool found_parent_namespace = FindParentNamespaceByClassLoader(env, class_loader, &parent_ns); |
Dimitry Ivanov | 24db75c | 2016-05-12 15:34:41 -0700 | [diff] [blame] | 191 | |
Zhenhua WANG | f2804e5 | 2016-05-30 11:16:08 +0800 | [diff] [blame] | 192 | bool is_native_bridge = false; |
Dimitry Ivanov | ac1b191 | 2015-12-01 13:56:44 -0800 | [diff] [blame] | 193 | |
Zhenhua WANG | f2804e5 | 2016-05-30 11:16:08 +0800 | [diff] [blame] | 194 | if (found_parent_namespace) { |
| 195 | is_native_bridge = !parent_ns.is_android_namespace(); |
| 196 | } else if (!library_path.empty()) { |
| 197 | is_native_bridge = NativeBridgeIsPathSupported(library_path.c_str()); |
Dimitry Ivanov | d047c92 | 2016-02-23 14:23:51 -0800 | [diff] [blame] | 198 | } |
Dimitry Ivanov | ac1b191 | 2015-12-01 13:56:44 -0800 | [diff] [blame] | 199 | |
Jiyong Park | a07f305 | 2017-08-22 10:26:10 +0900 | [diff] [blame] | 200 | std::string system_exposed_libraries = system_public_libraries_; |
| 201 | const char* namespace_name = kClassloaderNamespaceName; |
| 202 | android_namespace_t* vndk_ns = nullptr; |
| 203 | if (is_for_vendor && !is_shared) { |
| 204 | LOG_FATAL_IF(is_native_bridge, "Unbundled vendor apk must not use translated architecture"); |
| 205 | |
| 206 | // For vendor apks, give access to the vendor lib even though |
| 207 | // they are treated as unbundled; the libs and apks are still bundled |
| 208 | // together in the vendor partition. |
| 209 | #if defined(__LP64__) |
| 210 | std::string vendor_lib_path = "/vendor/lib64"; |
| 211 | #else |
| 212 | std::string vendor_lib_path = "/vendor/lib"; |
| 213 | #endif |
| 214 | library_path = library_path + ":" + vendor_lib_path.c_str(); |
| 215 | permitted_path = permitted_path + ":" + vendor_lib_path.c_str(); |
| 216 | |
| 217 | // Also give access to LLNDK libraries since they are available to vendors |
| 218 | system_exposed_libraries = system_exposed_libraries + ":" + system_llndk_libraries_.c_str(); |
| 219 | |
| 220 | // Give access to VNDK-SP libraries from the 'vndk' namespace. |
| 221 | vndk_ns = android_get_exported_namespace(kVndkNamespaceName); |
| 222 | LOG_ALWAYS_FATAL_IF(vndk_ns == nullptr, |
| 223 | "Cannot find \"%s\" namespace for vendor apks", kVndkNamespaceName); |
| 224 | |
| 225 | // Different name is useful for debugging |
| 226 | namespace_name = kVendorClassloaderNamespaceName; |
| 227 | ALOGD("classloader namespace configured for unbundled vendor apk. library_path=%s", library_path.c_str()); |
| 228 | } |
| 229 | |
Zhenhua WANG | f2804e5 | 2016-05-30 11:16:08 +0800 | [diff] [blame] | 230 | NativeLoaderNamespace native_loader_ns; |
| 231 | if (!is_native_bridge) { |
Jiyong Park | a07f305 | 2017-08-22 10:26:10 +0900 | [diff] [blame] | 232 | android_namespace_t* ns = android_create_namespace(namespace_name, |
Zhenhua WANG | f2804e5 | 2016-05-30 11:16:08 +0800 | [diff] [blame] | 233 | nullptr, |
| 234 | library_path.c_str(), |
| 235 | namespace_type, |
| 236 | permitted_path.c_str(), |
| 237 | parent_ns.get_android_ns()); |
| 238 | if (ns == nullptr) { |
| 239 | *error_msg = dlerror(); |
| 240 | return false; |
| 241 | } |
| 242 | |
Dimitry Ivanov | 882cad2 | 2017-05-01 15:12:49 -0700 | [diff] [blame] | 243 | // Note that when vendor_ns is not configured this function will return nullptr |
| 244 | // and it will result in linking vendor_public_libraries_ to the default namespace |
| 245 | // which is expected behavior in this case. |
| 246 | android_namespace_t* vendor_ns = android_get_exported_namespace(kVendorNamespaceName); |
| 247 | |
Jiyong Park | a07f305 | 2017-08-22 10:26:10 +0900 | [diff] [blame] | 248 | if (!android_link_namespaces(ns, nullptr, system_exposed_libraries.c_str())) { |
Dimitry Ivanov | 26e1a84 | 2017-02-03 14:11:27 -0800 | [diff] [blame] | 249 | *error_msg = dlerror(); |
| 250 | return false; |
| 251 | } |
| 252 | |
Jiyong Park | a07f305 | 2017-08-22 10:26:10 +0900 | [diff] [blame] | 253 | if (vndk_ns != nullptr && !system_vndksp_libraries_.empty()) { |
| 254 | // vendor apks are allowed to use VNDK-SP libraries. |
| 255 | if (!android_link_namespaces(ns, vndk_ns, system_vndksp_libraries_.c_str())) { |
| 256 | *error_msg = dlerror(); |
| 257 | return false; |
| 258 | } |
| 259 | } |
| 260 | |
Dimitry Ivanov | 882cad2 | 2017-05-01 15:12:49 -0700 | [diff] [blame] | 261 | if (!vendor_public_libraries_.empty()) { |
| 262 | if (!android_link_namespaces(ns, vendor_ns, vendor_public_libraries_.c_str())) { |
| 263 | *error_msg = dlerror(); |
| 264 | return false; |
| 265 | } |
| 266 | } |
| 267 | |
Zhenhua WANG | f2804e5 | 2016-05-30 11:16:08 +0800 | [diff] [blame] | 268 | native_loader_ns = NativeLoaderNamespace(ns); |
| 269 | } else { |
Jiyong Park | a07f305 | 2017-08-22 10:26:10 +0900 | [diff] [blame] | 270 | native_bridge_namespace_t* ns = NativeBridgeCreateNamespace(namespace_name, |
Zhenhua WANG | f2804e5 | 2016-05-30 11:16:08 +0800 | [diff] [blame] | 271 | nullptr, |
| 272 | library_path.c_str(), |
| 273 | namespace_type, |
| 274 | permitted_path.c_str(), |
| 275 | parent_ns.get_native_bridge_ns()); |
Dimitry Ivanov | 882cad2 | 2017-05-01 15:12:49 -0700 | [diff] [blame] | 276 | |
Zhenhua WANG | f2804e5 | 2016-05-30 11:16:08 +0800 | [diff] [blame] | 277 | if (ns == nullptr) { |
| 278 | *error_msg = NativeBridgeGetError(); |
| 279 | return false; |
| 280 | } |
| 281 | |
Dimitry Ivanov | 882cad2 | 2017-05-01 15:12:49 -0700 | [diff] [blame] | 282 | native_bridge_namespace_t* vendor_ns = NativeBridgeGetVendorNamespace(); |
| 283 | |
Jiyong Park | a07f305 | 2017-08-22 10:26:10 +0900 | [diff] [blame] | 284 | if (!NativeBridgeLinkNamespaces(ns, nullptr, system_exposed_libraries.c_str())) { |
Zhenhua WANG | e8fb11d | 2017-02-27 10:14:45 +0800 | [diff] [blame] | 285 | *error_msg = NativeBridgeGetError(); |
| 286 | return false; |
| 287 | } |
| 288 | |
Dimitry Ivanov | 882cad2 | 2017-05-01 15:12:49 -0700 | [diff] [blame] | 289 | if (!vendor_public_libraries_.empty()) { |
| 290 | if (!NativeBridgeLinkNamespaces(ns, vendor_ns, vendor_public_libraries_.c_str())) { |
| 291 | *error_msg = NativeBridgeGetError(); |
| 292 | return false; |
| 293 | } |
| 294 | } |
| 295 | |
Zhenhua WANG | f2804e5 | 2016-05-30 11:16:08 +0800 | [diff] [blame] | 296 | native_loader_ns = NativeLoaderNamespace(ns); |
| 297 | } |
| 298 | |
| 299 | namespaces_.push_back(std::make_pair(env->NewWeakGlobalRef(class_loader), native_loader_ns)); |
| 300 | |
| 301 | *ns = native_loader_ns; |
| 302 | return true; |
Dimitry Ivanov | ac1b191 | 2015-12-01 13:56:44 -0800 | [diff] [blame] | 303 | } |
| 304 | |
Zhenhua WANG | f2804e5 | 2016-05-30 11:16:08 +0800 | [diff] [blame] | 305 | bool FindNamespaceByClassLoader(JNIEnv* env, jobject class_loader, NativeLoaderNamespace* ns) { |
Dimitry Ivanov | f44ecde | 2016-02-22 13:48:22 -0800 | [diff] [blame] | 306 | auto it = std::find_if(namespaces_.begin(), namespaces_.end(), |
Zhenhua WANG | f2804e5 | 2016-05-30 11:16:08 +0800 | [diff] [blame] | 307 | [&](const std::pair<jweak, NativeLoaderNamespace>& value) { |
Dimitry Ivanov | f44ecde | 2016-02-22 13:48:22 -0800 | [diff] [blame] | 308 | return env->IsSameObject(value.first, class_loader); |
| 309 | }); |
Zhenhua WANG | f2804e5 | 2016-05-30 11:16:08 +0800 | [diff] [blame] | 310 | if (it != namespaces_.end()) { |
| 311 | if (ns != nullptr) { |
| 312 | *ns = it->second; |
| 313 | } |
| 314 | |
| 315 | return true; |
| 316 | } |
| 317 | |
| 318 | return false; |
Dimitry Ivanov | f44ecde | 2016-02-22 13:48:22 -0800 | [diff] [blame] | 319 | } |
| 320 | |
Dimitry Ivanov | 4b0e963 | 2016-03-15 13:51:26 -0700 | [diff] [blame] | 321 | void Initialize() { |
Dimitry Ivanov | 80ddb8f | 2016-05-09 18:12:00 -0700 | [diff] [blame] | 322 | // Once public namespace is initialized there is no |
| 323 | // point in running this code - it will have no effect |
| 324 | // on the current list of public libraries. |
| 325 | if (initialized_) { |
| 326 | return; |
| 327 | } |
| 328 | |
Dimitry Ivanov | b614045 | 2016-04-06 18:24:08 -0700 | [diff] [blame] | 329 | std::vector<std::string> sonames; |
Dimitry Ivanov | 0b5651e | 2016-04-21 16:42:48 -0700 | [diff] [blame] | 330 | const char* android_root_env = getenv("ANDROID_ROOT"); |
| 331 | std::string root_dir = android_root_env != nullptr ? android_root_env : "/system"; |
| 332 | std::string public_native_libraries_system_config = |
| 333 | root_dir + kPublicNativeLibrariesSystemConfigPathFromRoot; |
Jiyong Park | a07f305 | 2017-08-22 10:26:10 +0900 | [diff] [blame] | 334 | std::string llndk_native_libraries_system_config = |
| 335 | root_dir + kLlndkNativeLibrariesSystemConfigPathFromRoot; |
| 336 | std::string vndksp_native_libraries_system_config = |
| 337 | root_dir + kVndkspNativeLibrariesSystemConfigPathFromRoot; |
Dimitry Ivanov | b614045 | 2016-04-06 18:24:08 -0700 | [diff] [blame] | 338 | |
Christopher Ferris | 39da84b | 2016-06-21 16:11:23 -0700 | [diff] [blame] | 339 | std::string error_msg; |
| 340 | LOG_ALWAYS_FATAL_IF(!ReadConfig(public_native_libraries_system_config, &sonames, &error_msg), |
Dimitry Ivanov | b614045 | 2016-04-06 18:24:08 -0700 | [diff] [blame] | 341 | "Error reading public native library list from \"%s\": %s", |
Christopher Ferris | 39da84b | 2016-06-21 16:11:23 -0700 | [diff] [blame] | 342 | public_native_libraries_system_config.c_str(), error_msg.c_str()); |
Dimitry Ivanov | 7d02829 | 2016-05-05 17:30:24 -0700 | [diff] [blame] | 343 | |
Justin Yun | 4a1d110 | 2017-11-27 17:04:14 +0900 | [diff] [blame] | 344 | // Insert VNDK version to llndk and vndksp config file names. |
| 345 | insert_vndk_version_str(&llndk_native_libraries_system_config); |
| 346 | insert_vndk_version_str(&vndksp_native_libraries_system_config); |
| 347 | |
Dimitry Ivanov | 7d02829 | 2016-05-05 17:30:24 -0700 | [diff] [blame] | 348 | // For debuggable platform builds use ANDROID_ADDITIONAL_PUBLIC_LIBRARIES environment |
| 349 | // variable to add libraries to the list. This is intended for platform tests only. |
| 350 | if (is_debuggable()) { |
| 351 | const char* additional_libs = getenv("ANDROID_ADDITIONAL_PUBLIC_LIBRARIES"); |
| 352 | if (additional_libs != nullptr && additional_libs[0] != '\0') { |
| 353 | std::vector<std::string> additional_libs_vector = base::Split(additional_libs, ":"); |
| 354 | std::copy(additional_libs_vector.begin(), |
| 355 | additional_libs_vector.end(), |
| 356 | std::back_inserter(sonames)); |
| 357 | } |
| 358 | } |
| 359 | |
Dimitry Ivanov | b614045 | 2016-04-06 18:24:08 -0700 | [diff] [blame] | 360 | // android_init_namespaces() expects all the public libraries |
| 361 | // to be loaded so that they can be found by soname alone. |
| 362 | // |
| 363 | // TODO(dimitry): this is a bit misleading since we do not know |
| 364 | // if the vendor public library is going to be opened from /vendor/lib |
| 365 | // we might as well end up loading them from /system/lib |
| 366 | // For now we rely on CTS test to catch things like this but |
| 367 | // it should probably be addressed in the future. |
| 368 | for (const auto& soname : sonames) { |
Evan Ralston | 15a264e | 2017-02-03 17:09:46 -0800 | [diff] [blame] | 369 | LOG_ALWAYS_FATAL_IF(dlopen(soname.c_str(), RTLD_NOW | RTLD_NODELETE) == nullptr, |
| 370 | "Error preloading public library %s: %s", |
| 371 | soname.c_str(), dlerror()); |
Dimitry Ivanov | b614045 | 2016-04-06 18:24:08 -0700 | [diff] [blame] | 372 | } |
| 373 | |
Dimitry Ivanov | 882cad2 | 2017-05-01 15:12:49 -0700 | [diff] [blame] | 374 | system_public_libraries_ = base::Join(sonames, ':'); |
| 375 | |
| 376 | sonames.clear(); |
Justin Yun | 4a1d110 | 2017-11-27 17:04:14 +0900 | [diff] [blame] | 377 | ReadConfig(llndk_native_libraries_system_config, &sonames); |
Jiyong Park | a07f305 | 2017-08-22 10:26:10 +0900 | [diff] [blame] | 378 | system_llndk_libraries_ = base::Join(sonames, ':'); |
| 379 | |
| 380 | sonames.clear(); |
Justin Yun | 4a1d110 | 2017-11-27 17:04:14 +0900 | [diff] [blame] | 381 | ReadConfig(vndksp_native_libraries_system_config, &sonames); |
Jiyong Park | a07f305 | 2017-08-22 10:26:10 +0900 | [diff] [blame] | 382 | system_vndksp_libraries_ = base::Join(sonames, ':'); |
| 383 | |
| 384 | sonames.clear(); |
Dimitry Ivanov | 882cad2 | 2017-05-01 15:12:49 -0700 | [diff] [blame] | 385 | // This file is optional, quietly ignore if the file does not exist. |
| 386 | ReadConfig(kPublicNativeLibrariesVendorConfig, &sonames); |
| 387 | |
| 388 | vendor_public_libraries_ = base::Join(sonames, ':'); |
Dimitry Ivanov | b614045 | 2016-04-06 18:24:08 -0700 | [diff] [blame] | 389 | } |
| 390 | |
Dimitry Ivanov | be4ca3a | 2016-05-02 10:43:16 -0700 | [diff] [blame] | 391 | void Reset() { |
| 392 | namespaces_.clear(); |
| 393 | } |
| 394 | |
Dimitry Ivanov | b614045 | 2016-04-06 18:24:08 -0700 | [diff] [blame] | 395 | private: |
Christopher Ferris | 39da84b | 2016-06-21 16:11:23 -0700 | [diff] [blame] | 396 | bool ReadConfig(const std::string& configFile, std::vector<std::string>* sonames, |
| 397 | std::string* error_msg = nullptr) { |
Dimitry Ivanov | 4b0e963 | 2016-03-15 13:51:26 -0700 | [diff] [blame] | 398 | // Read list of public native libraries from the config file. |
| 399 | std::string file_content; |
Dimitry Ivanov | b614045 | 2016-04-06 18:24:08 -0700 | [diff] [blame] | 400 | if(!base::ReadFileToString(configFile, &file_content)) { |
Christopher Ferris | 39da84b | 2016-06-21 16:11:23 -0700 | [diff] [blame] | 401 | if (error_msg) *error_msg = strerror(errno); |
Dimitry Ivanov | b614045 | 2016-04-06 18:24:08 -0700 | [diff] [blame] | 402 | return false; |
| 403 | } |
Dimitry Ivanov | 4b0e963 | 2016-03-15 13:51:26 -0700 | [diff] [blame] | 404 | |
| 405 | std::vector<std::string> lines = base::Split(file_content, "\n"); |
| 406 | |
Christopher Ferris | 39da84b | 2016-06-21 16:11:23 -0700 | [diff] [blame] | 407 | for (auto& line : lines) { |
Dimitry Ivanov | 4b0e963 | 2016-03-15 13:51:26 -0700 | [diff] [blame] | 408 | auto trimmed_line = base::Trim(line); |
| 409 | if (trimmed_line[0] == '#' || trimmed_line.empty()) { |
| 410 | continue; |
| 411 | } |
Christopher Ferris | 39da84b | 2016-06-21 16:11:23 -0700 | [diff] [blame] | 412 | size_t space_pos = trimmed_line.rfind(' '); |
| 413 | if (space_pos != std::string::npos) { |
| 414 | std::string type = trimmed_line.substr(space_pos + 1); |
| 415 | if (type != "32" && type != "64") { |
| 416 | if (error_msg) *error_msg = "Malformed line: " + line; |
| 417 | return false; |
| 418 | } |
| 419 | #if defined(__LP64__) |
| 420 | // Skip 32 bit public library. |
| 421 | if (type == "32") { |
| 422 | continue; |
| 423 | } |
| 424 | #else |
| 425 | // Skip 64 bit public library. |
| 426 | if (type == "64") { |
| 427 | continue; |
| 428 | } |
| 429 | #endif |
| 430 | trimmed_line.resize(space_pos); |
| 431 | } |
Dimitry Ivanov | 4b0e963 | 2016-03-15 13:51:26 -0700 | [diff] [blame] | 432 | |
Dimitry Ivanov | b614045 | 2016-04-06 18:24:08 -0700 | [diff] [blame] | 433 | sonames->push_back(trimmed_line); |
Dimitry Ivanov | 4b0e963 | 2016-03-15 13:51:26 -0700 | [diff] [blame] | 434 | } |
| 435 | |
Dimitry Ivanov | b614045 | 2016-04-06 18:24:08 -0700 | [diff] [blame] | 436 | return true; |
Dimitry Ivanov | d68c8e9 | 2016-02-10 14:09:22 -0800 | [diff] [blame] | 437 | } |
Dimitry Ivanov | ac1b191 | 2015-12-01 13:56:44 -0800 | [diff] [blame] | 438 | |
Zhenhua WANG | f2804e5 | 2016-05-30 11:16:08 +0800 | [diff] [blame] | 439 | bool InitPublicNamespace(const char* library_path, std::string* error_msg) { |
| 440 | // Ask native bride if this apps library path should be handled by it |
| 441 | bool is_native_bridge = NativeBridgeIsPathSupported(library_path); |
| 442 | |
Dimitry Ivanov | 4b0e963 | 2016-03-15 13:51:26 -0700 | [diff] [blame] | 443 | // (http://b/25844435) - Some apps call dlopen from generated code (mono jited |
| 444 | // code is one example) unknown to linker in which case linker uses anonymous |
| 445 | // namespace. The second argument specifies the search path for the anonymous |
| 446 | // namespace which is the library_path of the classloader. |
Dimitry Ivanov | 882cad2 | 2017-05-01 15:12:49 -0700 | [diff] [blame] | 447 | initialized_ = android_init_anonymous_namespace(system_public_libraries_.c_str(), |
Dimitry Ivanov | 26e1a84 | 2017-02-03 14:11:27 -0800 | [diff] [blame] | 448 | is_native_bridge ? nullptr : library_path); |
Dimitry Ivanov | d836ab0 | 2016-11-02 18:03:10 -0700 | [diff] [blame] | 449 | if (!initialized_) { |
| 450 | *error_msg = dlerror(); |
| 451 | return false; |
| 452 | } |
| 453 | |
| 454 | // and now initialize native bridge namespaces if necessary. |
| 455 | if (NativeBridgeInitialized()) { |
Dimitry Ivanov | 882cad2 | 2017-05-01 15:12:49 -0700 | [diff] [blame] | 456 | initialized_ = NativeBridgeInitAnonymousNamespace(system_public_libraries_.c_str(), |
Zhenhua WANG | e8fb11d | 2017-02-27 10:14:45 +0800 | [diff] [blame] | 457 | is_native_bridge ? library_path : nullptr); |
Zhenhua WANG | f2804e5 | 2016-05-30 11:16:08 +0800 | [diff] [blame] | 458 | if (!initialized_) { |
| 459 | *error_msg = NativeBridgeGetError(); |
| 460 | } |
| 461 | } |
Dimitry Ivanov | ac1b191 | 2015-12-01 13:56:44 -0800 | [diff] [blame] | 462 | |
| 463 | return initialized_; |
| 464 | } |
| 465 | |
Dimitry Ivanov | 24db75c | 2016-05-12 15:34:41 -0700 | [diff] [blame] | 466 | jobject GetParentClassLoader(JNIEnv* env, jobject class_loader) { |
| 467 | jclass class_loader_class = env->FindClass("java/lang/ClassLoader"); |
| 468 | jmethodID get_parent = env->GetMethodID(class_loader_class, |
| 469 | "getParent", |
| 470 | "()Ljava/lang/ClassLoader;"); |
| 471 | |
| 472 | return env->CallObjectMethod(class_loader, get_parent); |
| 473 | } |
| 474 | |
Zhenhua WANG | f2804e5 | 2016-05-30 11:16:08 +0800 | [diff] [blame] | 475 | bool FindParentNamespaceByClassLoader(JNIEnv* env, |
| 476 | jobject class_loader, |
| 477 | NativeLoaderNamespace* ns) { |
Dimitry Ivanov | 24db75c | 2016-05-12 15:34:41 -0700 | [diff] [blame] | 478 | jobject parent_class_loader = GetParentClassLoader(env, class_loader); |
| 479 | |
| 480 | while (parent_class_loader != nullptr) { |
Zhenhua WANG | f2804e5 | 2016-05-30 11:16:08 +0800 | [diff] [blame] | 481 | if (FindNamespaceByClassLoader(env, parent_class_loader, ns)) { |
| 482 | return true; |
Dimitry Ivanov | 24db75c | 2016-05-12 15:34:41 -0700 | [diff] [blame] | 483 | } |
| 484 | |
| 485 | parent_class_loader = GetParentClassLoader(env, parent_class_loader); |
| 486 | } |
Zhenhua WANG | f2804e5 | 2016-05-30 11:16:08 +0800 | [diff] [blame] | 487 | |
| 488 | return false; |
Dimitry Ivanov | 24db75c | 2016-05-12 15:34:41 -0700 | [diff] [blame] | 489 | } |
| 490 | |
Dimitry Ivanov | ac1b191 | 2015-12-01 13:56:44 -0800 | [diff] [blame] | 491 | bool initialized_; |
Zhenhua WANG | f2804e5 | 2016-05-30 11:16:08 +0800 | [diff] [blame] | 492 | std::vector<std::pair<jweak, NativeLoaderNamespace>> namespaces_; |
Dimitry Ivanov | 882cad2 | 2017-05-01 15:12:49 -0700 | [diff] [blame] | 493 | std::string system_public_libraries_; |
| 494 | std::string vendor_public_libraries_; |
Jiyong Park | a07f305 | 2017-08-22 10:26:10 +0900 | [diff] [blame] | 495 | std::string system_llndk_libraries_; |
| 496 | std::string system_vndksp_libraries_; |
Dimitry Ivanov | ac1b191 | 2015-12-01 13:56:44 -0800 | [diff] [blame] | 497 | |
| 498 | DISALLOW_COPY_AND_ASSIGN(LibraryNamespaces); |
| 499 | }; |
| 500 | |
Dimitry Ivanov | 34d5a20 | 2016-02-29 13:21:43 -0800 | [diff] [blame] | 501 | static std::mutex g_namespaces_mutex; |
Dimitry Ivanov | ac1b191 | 2015-12-01 13:56:44 -0800 | [diff] [blame] | 502 | static LibraryNamespaces* g_namespaces = new LibraryNamespaces; |
| 503 | #endif |
| 504 | |
Dimitry Ivanov | 4b0e963 | 2016-03-15 13:51:26 -0700 | [diff] [blame] | 505 | void InitializeNativeLoader() { |
Dimitry Ivanov | 426799d | 2016-02-22 11:27:48 -0800 | [diff] [blame] | 506 | #if defined(__ANDROID__) |
Dimitry Ivanov | 34d5a20 | 2016-02-29 13:21:43 -0800 | [diff] [blame] | 507 | std::lock_guard<std::mutex> guard(g_namespaces_mutex); |
Dimitry Ivanov | 4b0e963 | 2016-03-15 13:51:26 -0700 | [diff] [blame] | 508 | g_namespaces->Initialize(); |
Dimitry Ivanov | 426799d | 2016-02-22 11:27:48 -0800 | [diff] [blame] | 509 | #endif |
| 510 | } |
| 511 | |
Dimitry Ivanov | be4ca3a | 2016-05-02 10:43:16 -0700 | [diff] [blame] | 512 | void ResetNativeLoader() { |
| 513 | #if defined(__ANDROID__) |
| 514 | std::lock_guard<std::mutex> guard(g_namespaces_mutex); |
| 515 | g_namespaces->Reset(); |
| 516 | #endif |
| 517 | } |
Dimitry Ivanov | ac1b191 | 2015-12-01 13:56:44 -0800 | [diff] [blame] | 518 | |
Dimitry Ivanov | d047c92 | 2016-02-23 14:23:51 -0800 | [diff] [blame] | 519 | jstring CreateClassLoaderNamespace(JNIEnv* env, |
| 520 | int32_t target_sdk_version, |
| 521 | jobject class_loader, |
| 522 | bool is_shared, |
Jiyong Park | a07f305 | 2017-08-22 10:26:10 +0900 | [diff] [blame] | 523 | bool is_for_vendor, |
Dimitry Ivanov | d047c92 | 2016-02-23 14:23:51 -0800 | [diff] [blame] | 524 | jstring library_path, |
| 525 | jstring permitted_path) { |
Dimitry Ivanov | ac1b191 | 2015-12-01 13:56:44 -0800 | [diff] [blame] | 526 | #if defined(__ANDROID__) |
Dimitry Ivanov | 34d5a20 | 2016-02-29 13:21:43 -0800 | [diff] [blame] | 527 | std::lock_guard<std::mutex> guard(g_namespaces_mutex); |
Zhenhua WANG | f2804e5 | 2016-05-30 11:16:08 +0800 | [diff] [blame] | 528 | |
| 529 | std::string error_msg; |
| 530 | NativeLoaderNamespace ns; |
| 531 | bool success = g_namespaces->Create(env, |
Dimitry Ivanov | 9e253ce | 2017-05-08 22:24:24 -0700 | [diff] [blame] | 532 | target_sdk_version, |
Zhenhua WANG | f2804e5 | 2016-05-30 11:16:08 +0800 | [diff] [blame] | 533 | class_loader, |
| 534 | is_shared, |
Jiyong Park | a07f305 | 2017-08-22 10:26:10 +0900 | [diff] [blame] | 535 | is_for_vendor, |
Zhenhua WANG | f2804e5 | 2016-05-30 11:16:08 +0800 | [diff] [blame] | 536 | library_path, |
| 537 | permitted_path, |
| 538 | &ns, |
| 539 | &error_msg); |
| 540 | if (!success) { |
| 541 | return env->NewStringUTF(error_msg.c_str()); |
Dimitry Ivanov | d047c92 | 2016-02-23 14:23:51 -0800 | [diff] [blame] | 542 | } |
| 543 | #else |
Jiyong Park | a07f305 | 2017-08-22 10:26:10 +0900 | [diff] [blame] | 544 | UNUSED(env, target_sdk_version, class_loader, is_shared, is_for_vendor, |
Dimitry Ivanov | d047c92 | 2016-02-23 14:23:51 -0800 | [diff] [blame] | 545 | library_path, permitted_path); |
| 546 | #endif |
| 547 | return nullptr; |
| 548 | } |
| 549 | |
| 550 | void* OpenNativeLibrary(JNIEnv* env, |
| 551 | int32_t target_sdk_version, |
| 552 | const char* path, |
| 553 | jobject class_loader, |
Zhenhua WANG | f2804e5 | 2016-05-30 11:16:08 +0800 | [diff] [blame] | 554 | jstring library_path, |
| 555 | bool* needs_native_bridge, |
| 556 | std::string* error_msg) { |
Dimitry Ivanov | d047c92 | 2016-02-23 14:23:51 -0800 | [diff] [blame] | 557 | #if defined(__ANDROID__) |
Dimitry Ivanov | 5539db0 | 2016-04-20 16:07:30 -0700 | [diff] [blame] | 558 | UNUSED(target_sdk_version); |
| 559 | if (class_loader == nullptr) { |
Zhenhua WANG | f2804e5 | 2016-05-30 11:16:08 +0800 | [diff] [blame] | 560 | *needs_native_bridge = false; |
Dimitry Ivanov | ac1b191 | 2015-12-01 13:56:44 -0800 | [diff] [blame] | 561 | return dlopen(path, RTLD_NOW); |
| 562 | } |
| 563 | |
Dimitry Ivanov | 34d5a20 | 2016-02-29 13:21:43 -0800 | [diff] [blame] | 564 | std::lock_guard<std::mutex> guard(g_namespaces_mutex); |
Zhenhua WANG | f2804e5 | 2016-05-30 11:16:08 +0800 | [diff] [blame] | 565 | NativeLoaderNamespace ns; |
Dimitry Ivanov | ac1b191 | 2015-12-01 13:56:44 -0800 | [diff] [blame] | 566 | |
Zhenhua WANG | f2804e5 | 2016-05-30 11:16:08 +0800 | [diff] [blame] | 567 | if (!g_namespaces->FindNamespaceByClassLoader(env, class_loader, &ns)) { |
Dimitry Ivanov | d047c92 | 2016-02-23 14:23:51 -0800 | [diff] [blame] | 568 | // This is the case where the classloader was not created by ApplicationLoaders |
| 569 | // In this case we create an isolated not-shared namespace for it. |
Dimitry Ivanov | 9e253ce | 2017-05-08 22:24:24 -0700 | [diff] [blame] | 570 | if (!g_namespaces->Create(env, |
| 571 | target_sdk_version, |
| 572 | class_loader, |
Jiyong Park | a07f305 | 2017-08-22 10:26:10 +0900 | [diff] [blame] | 573 | false /* is_shared */, |
| 574 | false /* is_for_vendor */, |
Dimitry Ivanov | 9e253ce | 2017-05-08 22:24:24 -0700 | [diff] [blame] | 575 | library_path, |
| 576 | nullptr, |
| 577 | &ns, |
| 578 | error_msg)) { |
Dimitry Ivanov | d047c92 | 2016-02-23 14:23:51 -0800 | [diff] [blame] | 579 | return nullptr; |
| 580 | } |
Dimitry Ivanov | ac1b191 | 2015-12-01 13:56:44 -0800 | [diff] [blame] | 581 | } |
| 582 | |
Zhenhua WANG | f2804e5 | 2016-05-30 11:16:08 +0800 | [diff] [blame] | 583 | if (ns.is_android_namespace()) { |
| 584 | android_dlextinfo extinfo; |
| 585 | extinfo.flags = ANDROID_DLEXT_USE_NAMESPACE; |
| 586 | extinfo.library_namespace = ns.get_android_ns(); |
Dimitry Ivanov | ac1b191 | 2015-12-01 13:56:44 -0800 | [diff] [blame] | 587 | |
Zhenhua WANG | f2804e5 | 2016-05-30 11:16:08 +0800 | [diff] [blame] | 588 | void* handle = android_dlopen_ext(path, RTLD_NOW, &extinfo); |
| 589 | if (handle == nullptr) { |
| 590 | *error_msg = dlerror(); |
| 591 | } |
| 592 | *needs_native_bridge = false; |
| 593 | return handle; |
| 594 | } else { |
| 595 | void* handle = NativeBridgeLoadLibraryExt(path, RTLD_NOW, ns.get_native_bridge_ns()); |
| 596 | if (handle == nullptr) { |
| 597 | *error_msg = NativeBridgeGetError(); |
| 598 | } |
| 599 | *needs_native_bridge = true; |
| 600 | return handle; |
| 601 | } |
Dimitry Ivanov | ac1b191 | 2015-12-01 13:56:44 -0800 | [diff] [blame] | 602 | #else |
Dimitry Ivanov | d047c92 | 2016-02-23 14:23:51 -0800 | [diff] [blame] | 603 | UNUSED(env, target_sdk_version, class_loader, library_path); |
Zhenhua WANG | f2804e5 | 2016-05-30 11:16:08 +0800 | [diff] [blame] | 604 | *needs_native_bridge = false; |
| 605 | void* handle = dlopen(path, RTLD_NOW); |
| 606 | if (handle == nullptr) { |
| 607 | if (NativeBridgeIsSupported(path)) { |
| 608 | *needs_native_bridge = true; |
| 609 | handle = NativeBridgeLoadLibrary(path, RTLD_NOW); |
| 610 | if (handle == nullptr) { |
| 611 | *error_msg = NativeBridgeGetError(); |
| 612 | } |
| 613 | } else { |
| 614 | *needs_native_bridge = false; |
| 615 | *error_msg = dlerror(); |
| 616 | } |
| 617 | } |
| 618 | return handle; |
Dimitry Ivanov | ac1b191 | 2015-12-01 13:56:44 -0800 | [diff] [blame] | 619 | #endif |
| 620 | } |
| 621 | |
Zhenhua WANG | f2804e5 | 2016-05-30 11:16:08 +0800 | [diff] [blame] | 622 | bool CloseNativeLibrary(void* handle, const bool needs_native_bridge) { |
| 623 | return needs_native_bridge ? NativeBridgeUnloadLibrary(handle) : |
| 624 | dlclose(handle); |
Dimitry Ivanov | 09a516b | 2016-05-03 14:55:25 -0700 | [diff] [blame] | 625 | } |
| 626 | |
Dimitry Ivanov | f44ecde | 2016-02-22 13:48:22 -0800 | [diff] [blame] | 627 | #if defined(__ANDROID__) |
Dimitry Ivanov | 800083d | 2016-11-01 14:17:00 -0700 | [diff] [blame] | 628 | // native_bridge_namespaces are not supported for callers of this function. |
| 629 | // This function will return nullptr in the case when application is running |
| 630 | // on native bridge. |
Dimitry Ivanov | f44ecde | 2016-02-22 13:48:22 -0800 | [diff] [blame] | 631 | android_namespace_t* FindNamespaceByClassLoader(JNIEnv* env, jobject class_loader) { |
Dimitry Ivanov | 34d5a20 | 2016-02-29 13:21:43 -0800 | [diff] [blame] | 632 | std::lock_guard<std::mutex> guard(g_namespaces_mutex); |
Zhenhua WANG | f2804e5 | 2016-05-30 11:16:08 +0800 | [diff] [blame] | 633 | NativeLoaderNamespace ns; |
| 634 | if (g_namespaces->FindNamespaceByClassLoader(env, class_loader, &ns)) { |
Dimitry Ivanov | 800083d | 2016-11-01 14:17:00 -0700 | [diff] [blame] | 635 | return ns.is_android_namespace() ? ns.get_android_ns() : nullptr; |
Zhenhua WANG | f2804e5 | 2016-05-30 11:16:08 +0800 | [diff] [blame] | 636 | } |
| 637 | |
| 638 | return nullptr; |
Dimitry Ivanov | f44ecde | 2016-02-22 13:48:22 -0800 | [diff] [blame] | 639 | } |
| 640 | #endif |
| 641 | |
Dimitry Ivanov | ac1b191 | 2015-12-01 13:56:44 -0800 | [diff] [blame] | 642 | }; // android namespace |