Orion Hodson | 9b16e34 | 2019-10-09 13:29:16 +0100 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2019 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 | #include "library_namespaces.h" |
| 17 | |
| 18 | #include <dirent.h> |
| 19 | #include <dlfcn.h> |
| 20 | |
| 21 | #include <regex> |
| 22 | #include <string> |
| 23 | #include <vector> |
| 24 | |
| 25 | #include <android-base/file.h> |
| 26 | #include <android-base/logging.h> |
| 27 | #include <android-base/macros.h> |
| 28 | #include <android-base/properties.h> |
| 29 | #include <android-base/strings.h> |
Orion Hodson | 6dc0a43 | 2020-02-06 14:28:28 +0000 | [diff] [blame] | 30 | #include <nativehelper/scoped_utf_chars.h> |
Orion Hodson | 9b16e34 | 2019-10-09 13:29:16 +0100 | [diff] [blame] | 31 | |
| 32 | #include "nativeloader/dlext_namespaces.h" |
| 33 | #include "public_libraries.h" |
| 34 | #include "utils.h" |
| 35 | |
Orion Hodson | 9b16e34 | 2019-10-09 13:29:16 +0100 | [diff] [blame] | 36 | namespace android::nativeloader { |
| 37 | |
| 38 | namespace { |
| 39 | // The device may be configured to have the vendor libraries loaded to a separate namespace. |
| 40 | // For historical reasons this namespace was named sphal but effectively it is intended |
| 41 | // to use to load vendor libraries to separate namespace with controlled interface between |
| 42 | // vendor and system namespaces. |
| 43 | constexpr const char* kVendorNamespaceName = "sphal"; |
| 44 | constexpr const char* kVndkNamespaceName = "vndk"; |
Justin Yun | eb4f08c | 2020-02-18 11:29:07 +0900 | [diff] [blame^] | 45 | constexpr const char* kVndkProductNamespaceName = "vndk_product"; |
Kiyoung Kim | 272b36d | 2020-02-19 16:08:47 +0900 | [diff] [blame] | 46 | constexpr const char* kArtNamespaceName = "com_android_art"; |
| 47 | constexpr const char* kNeuralNetworksNamespaceName = "com_android_neuralnetworks"; |
| 48 | constexpr const char* kCronetNamespaceName = "com_android_cronet"; |
| 49 | constexpr const char* kStatsdNamespaceName = "com_android_os_statsd"; |
Orion Hodson | 9b16e34 | 2019-10-09 13:29:16 +0100 | [diff] [blame] | 50 | |
| 51 | // classloader-namespace is a linker namespace that is created for the loaded |
| 52 | // app. To be specific, it is created for the app classloader. When |
| 53 | // System.load() is called from a Java class that is loaded from the |
| 54 | // classloader, the classloader-namespace namespace associated with that |
| 55 | // classloader is selected for dlopen. The namespace is configured so that its |
| 56 | // search path is set to the app-local JNI directory and it is linked to the |
Kiyoung Kim | 99c19ca | 2020-01-29 16:09:38 +0900 | [diff] [blame] | 57 | // system namespace with the names of libs listed in the public.libraries.txt. |
Orion Hodson | 9b16e34 | 2019-10-09 13:29:16 +0100 | [diff] [blame] | 58 | // This way an app can only load its own JNI libraries along with the public libs. |
| 59 | constexpr const char* kClassloaderNamespaceName = "classloader-namespace"; |
| 60 | // Same thing for vendor APKs. |
| 61 | constexpr const char* kVendorClassloaderNamespaceName = "vendor-classloader-namespace"; |
Martin Stjernholm | 94fd9ea | 2019-10-24 16:57:34 +0100 | [diff] [blame] | 62 | // If the namespace is shared then add this suffix to form |
| 63 | // "classloader-namespace-shared" or "vendor-classloader-namespace-shared", |
| 64 | // respectively. A shared namespace (cf. ANDROID_NAMESPACE_TYPE_SHARED) has |
| 65 | // inherited all the libraries of the parent classloader namespace, or the |
Kiyoung Kim | 99c19ca | 2020-01-29 16:09:38 +0900 | [diff] [blame] | 66 | // system namespace for the main app classloader. It is used to give full |
Martin Stjernholm | 94fd9ea | 2019-10-24 16:57:34 +0100 | [diff] [blame] | 67 | // access to the platform libraries for apps bundled in the system image, |
| 68 | // including their later updates installed in /data. |
| 69 | constexpr const char* kSharedNamespaceSuffix = "-shared"; |
Orion Hodson | 9b16e34 | 2019-10-09 13:29:16 +0100 | [diff] [blame] | 70 | |
| 71 | // (http://b/27588281) This is a workaround for apps using custom classloaders and calling |
| 72 | // System.load() with an absolute path which is outside of the classloader library search path. |
| 73 | // This list includes all directories app is allowed to access this way. |
| 74 | constexpr const char* kWhitelistedDirectories = "/data:/mnt/expand"; |
| 75 | |
| 76 | constexpr const char* kVendorLibPath = "/vendor/" LIB; |
| 77 | constexpr const char* kProductLibPath = "/product/" LIB ":/system/product/" LIB; |
| 78 | |
| 79 | const std::regex kVendorDexPathRegex("(^|:)/vendor/"); |
| 80 | const std::regex kProductDexPathRegex("(^|:)(/system)?/product/"); |
| 81 | |
| 82 | // Define origin of APK if it is from vendor partition or product partition |
Martin Stjernholm | 3bb009a | 2019-10-17 21:29:01 +0100 | [diff] [blame] | 83 | using ApkOrigin = enum { |
Orion Hodson | 9b16e34 | 2019-10-09 13:29:16 +0100 | [diff] [blame] | 84 | APK_ORIGIN_DEFAULT = 0, |
| 85 | APK_ORIGIN_VENDOR = 1, |
| 86 | APK_ORIGIN_PRODUCT = 2, |
Martin Stjernholm | 3bb009a | 2019-10-17 21:29:01 +0100 | [diff] [blame] | 87 | }; |
Orion Hodson | 9b16e34 | 2019-10-09 13:29:16 +0100 | [diff] [blame] | 88 | |
| 89 | jobject GetParentClassLoader(JNIEnv* env, jobject class_loader) { |
| 90 | jclass class_loader_class = env->FindClass("java/lang/ClassLoader"); |
| 91 | jmethodID get_parent = |
| 92 | env->GetMethodID(class_loader_class, "getParent", "()Ljava/lang/ClassLoader;"); |
| 93 | |
| 94 | return env->CallObjectMethod(class_loader, get_parent); |
| 95 | } |
| 96 | |
| 97 | ApkOrigin GetApkOriginFromDexPath(JNIEnv* env, jstring dex_path) { |
| 98 | ApkOrigin apk_origin = APK_ORIGIN_DEFAULT; |
| 99 | |
| 100 | if (dex_path != nullptr) { |
| 101 | ScopedUtfChars dex_path_utf_chars(env, dex_path); |
| 102 | |
| 103 | if (std::regex_search(dex_path_utf_chars.c_str(), kVendorDexPathRegex)) { |
| 104 | apk_origin = APK_ORIGIN_VENDOR; |
| 105 | } |
| 106 | |
| 107 | if (std::regex_search(dex_path_utf_chars.c_str(), kProductDexPathRegex)) { |
| 108 | LOG_ALWAYS_FATAL_IF(apk_origin == APK_ORIGIN_VENDOR, |
| 109 | "Dex path contains both vendor and product partition : %s", |
| 110 | dex_path_utf_chars.c_str()); |
| 111 | |
| 112 | apk_origin = APK_ORIGIN_PRODUCT; |
| 113 | } |
| 114 | } |
| 115 | return apk_origin; |
| 116 | } |
| 117 | |
| 118 | } // namespace |
| 119 | |
| 120 | void LibraryNamespaces::Initialize() { |
| 121 | // Once public namespace is initialized there is no |
| 122 | // point in running this code - it will have no effect |
| 123 | // on the current list of public libraries. |
| 124 | if (initialized_) { |
| 125 | return; |
| 126 | } |
| 127 | |
| 128 | // android_init_namespaces() expects all the public libraries |
| 129 | // to be loaded so that they can be found by soname alone. |
| 130 | // |
| 131 | // TODO(dimitry): this is a bit misleading since we do not know |
| 132 | // if the vendor public library is going to be opened from /vendor/lib |
| 133 | // we might as well end up loading them from /system/lib or /product/lib |
| 134 | // For now we rely on CTS test to catch things like this but |
| 135 | // it should probably be addressed in the future. |
| 136 | for (const auto& soname : android::base::Split(preloadable_public_libraries(), ":")) { |
| 137 | LOG_ALWAYS_FATAL_IF(dlopen(soname.c_str(), RTLD_NOW | RTLD_NODELETE) == nullptr, |
| 138 | "Error preloading public library %s: %s", soname.c_str(), dlerror()); |
| 139 | } |
| 140 | } |
| 141 | |
| 142 | Result<NativeLoaderNamespace*> LibraryNamespaces::Create(JNIEnv* env, uint32_t target_sdk_version, |
| 143 | jobject class_loader, bool is_shared, |
| 144 | jstring dex_path, |
| 145 | jstring java_library_path, |
| 146 | jstring java_permitted_path) { |
| 147 | std::string library_path; // empty string by default. |
| 148 | |
| 149 | if (java_library_path != nullptr) { |
| 150 | ScopedUtfChars library_path_utf_chars(env, java_library_path); |
| 151 | library_path = library_path_utf_chars.c_str(); |
| 152 | } |
| 153 | |
| 154 | ApkOrigin apk_origin = GetApkOriginFromDexPath(env, dex_path); |
| 155 | |
| 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 | |
| 164 | if (java_permitted_path != nullptr) { |
| 165 | ScopedUtfChars path(env, java_permitted_path); |
| 166 | if (path.c_str() != nullptr && path.size() > 0) { |
| 167 | permitted_path = permitted_path + ":" + path.c_str(); |
| 168 | } |
| 169 | } |
| 170 | |
| 171 | LOG_ALWAYS_FATAL_IF(FindNamespaceByClassLoader(env, class_loader) != nullptr, |
| 172 | "There is already a namespace associated with this classloader"); |
| 173 | |
| 174 | std::string system_exposed_libraries = default_public_libraries(); |
Martin Stjernholm | 94fd9ea | 2019-10-24 16:57:34 +0100 | [diff] [blame] | 175 | std::string namespace_name = kClassloaderNamespaceName; |
Justin Yun | eb4f08c | 2020-02-18 11:29:07 +0900 | [diff] [blame^] | 176 | ApkOrigin unbundled_app_origin = APK_ORIGIN_DEFAULT; |
Orion Hodson | 9b16e34 | 2019-10-09 13:29:16 +0100 | [diff] [blame] | 177 | if ((apk_origin == APK_ORIGIN_VENDOR || |
Justin Yun | 3db26d5 | 2019-12-16 14:09:39 +0900 | [diff] [blame] | 178 | (apk_origin == APK_ORIGIN_PRODUCT && |
| 179 | is_product_vndk_version_defined())) && |
Orion Hodson | 9b16e34 | 2019-10-09 13:29:16 +0100 | [diff] [blame] | 180 | !is_shared) { |
Justin Yun | eb4f08c | 2020-02-18 11:29:07 +0900 | [diff] [blame^] | 181 | unbundled_app_origin = apk_origin; |
Orion Hodson | 9b16e34 | 2019-10-09 13:29:16 +0100 | [diff] [blame] | 182 | // For vendor / product apks, give access to the vendor / product lib even though |
| 183 | // they are treated as unbundled; the libs and apks are still bundled |
| 184 | // together in the vendor / product partition. |
| 185 | const char* origin_partition; |
| 186 | const char* origin_lib_path; |
Justin Yun | 089c135 | 2020-02-06 16:53:08 +0900 | [diff] [blame] | 187 | const char* llndk_libraries; |
Orion Hodson | 9b16e34 | 2019-10-09 13:29:16 +0100 | [diff] [blame] | 188 | |
| 189 | switch (apk_origin) { |
| 190 | case APK_ORIGIN_VENDOR: |
| 191 | origin_partition = "vendor"; |
| 192 | origin_lib_path = kVendorLibPath; |
Justin Yun | 089c135 | 2020-02-06 16:53:08 +0900 | [diff] [blame] | 193 | llndk_libraries = llndk_libraries_vendor().c_str(); |
Orion Hodson | 9b16e34 | 2019-10-09 13:29:16 +0100 | [diff] [blame] | 194 | break; |
| 195 | case APK_ORIGIN_PRODUCT: |
| 196 | origin_partition = "product"; |
| 197 | origin_lib_path = kProductLibPath; |
Justin Yun | 089c135 | 2020-02-06 16:53:08 +0900 | [diff] [blame] | 198 | llndk_libraries = llndk_libraries_product().c_str(); |
Orion Hodson | 9b16e34 | 2019-10-09 13:29:16 +0100 | [diff] [blame] | 199 | break; |
| 200 | default: |
| 201 | origin_partition = "unknown"; |
| 202 | origin_lib_path = ""; |
Justin Yun | 089c135 | 2020-02-06 16:53:08 +0900 | [diff] [blame] | 203 | llndk_libraries = ""; |
Orion Hodson | 9b16e34 | 2019-10-09 13:29:16 +0100 | [diff] [blame] | 204 | } |
| 205 | library_path = library_path + ":" + origin_lib_path; |
| 206 | permitted_path = permitted_path + ":" + origin_lib_path; |
| 207 | |
Justin Yun | 089c135 | 2020-02-06 16:53:08 +0900 | [diff] [blame] | 208 | // Also give access to LLNDK libraries since they are available to vendor or product |
| 209 | system_exposed_libraries = system_exposed_libraries + ":" + llndk_libraries; |
Orion Hodson | 9b16e34 | 2019-10-09 13:29:16 +0100 | [diff] [blame] | 210 | |
| 211 | // Different name is useful for debugging |
| 212 | namespace_name = kVendorClassloaderNamespaceName; |
| 213 | ALOGD("classloader namespace configured for unbundled %s apk. library_path=%s", |
| 214 | origin_partition, library_path.c_str()); |
| 215 | } else { |
| 216 | // extended public libraries are NOT available to vendor apks, otherwise it |
| 217 | // would be system->vendor violation. |
| 218 | if (!extended_public_libraries().empty()) { |
| 219 | system_exposed_libraries = system_exposed_libraries + ':' + extended_public_libraries(); |
| 220 | } |
| 221 | } |
| 222 | |
Martin Stjernholm | 94fd9ea | 2019-10-24 16:57:34 +0100 | [diff] [blame] | 223 | if (is_shared) { |
| 224 | // Show in the name that the namespace was created as shared, for debugging |
| 225 | // purposes. |
| 226 | namespace_name = namespace_name + kSharedNamespaceSuffix; |
| 227 | } |
| 228 | |
Orion Hodson | 9b16e34 | 2019-10-09 13:29:16 +0100 | [diff] [blame] | 229 | // Create the app namespace |
| 230 | NativeLoaderNamespace* parent_ns = FindParentNamespaceByClassLoader(env, class_loader); |
| 231 | // Heuristic: the first classloader with non-empty library_path is assumed to |
| 232 | // be the main classloader for app |
| 233 | // TODO(b/139178525) remove this heuristic by determining this in LoadedApk (or its |
| 234 | // friends) and then passing it down to here. |
| 235 | bool is_main_classloader = app_main_namespace_ == nullptr && !library_path.empty(); |
| 236 | // Policy: the namespace for the main classloader is also used as the |
| 237 | // anonymous namespace. |
| 238 | bool also_used_as_anonymous = is_main_classloader; |
| 239 | // Note: this function is executed with g_namespaces_mutex held, thus no |
| 240 | // racing here. |
| 241 | auto app_ns = NativeLoaderNamespace::Create( |
| 242 | namespace_name, library_path, permitted_path, parent_ns, is_shared, |
| 243 | target_sdk_version < 24 /* is_greylist_enabled */, also_used_as_anonymous); |
Bernie Innocenti | 4bd5895 | 2020-02-06 15:43:57 +0900 | [diff] [blame] | 244 | if (!app_ns.ok()) { |
Orion Hodson | 9b16e34 | 2019-10-09 13:29:16 +0100 | [diff] [blame] | 245 | return app_ns.error(); |
| 246 | } |
| 247 | // ... and link to other namespaces to allow access to some public libraries |
| 248 | bool is_bridged = app_ns->IsBridged(); |
| 249 | |
Martin Stjernholm | f0e99bd | 2020-02-11 22:57:14 +0000 | [diff] [blame] | 250 | auto system_ns = NativeLoaderNamespace::GetSystemNamespace(is_bridged); |
| 251 | if (!system_ns.ok()) { |
| 252 | return system_ns.error(); |
Orion Hodson | 9b16e34 | 2019-10-09 13:29:16 +0100 | [diff] [blame] | 253 | } |
| 254 | |
Martin Stjernholm | f0e99bd | 2020-02-11 22:57:14 +0000 | [diff] [blame] | 255 | auto linked = app_ns->Link(*system_ns, system_exposed_libraries); |
Bernie Innocenti | 4bd5895 | 2020-02-06 15:43:57 +0900 | [diff] [blame] | 256 | if (!linked.ok()) { |
Orion Hodson | 9b16e34 | 2019-10-09 13:29:16 +0100 | [diff] [blame] | 257 | return linked.error(); |
| 258 | } |
| 259 | |
| 260 | auto art_ns = NativeLoaderNamespace::GetExportedNamespace(kArtNamespaceName, is_bridged); |
| 261 | // ART APEX does not exist on host, and under certain build conditions. |
Bernie Innocenti | 4bd5895 | 2020-02-06 15:43:57 +0900 | [diff] [blame] | 262 | if (art_ns.ok()) { |
Orion Hodson | 9b16e34 | 2019-10-09 13:29:16 +0100 | [diff] [blame] | 263 | linked = app_ns->Link(*art_ns, art_public_libraries()); |
Bernie Innocenti | 4bd5895 | 2020-02-06 15:43:57 +0900 | [diff] [blame] | 264 | if (!linked.ok()) { |
Orion Hodson | 9b16e34 | 2019-10-09 13:29:16 +0100 | [diff] [blame] | 265 | return linked.error(); |
| 266 | } |
| 267 | } |
| 268 | |
| 269 | // Give access to NNAPI libraries (apex-updated LLNDK library). |
| 270 | auto nnapi_ns = |
| 271 | NativeLoaderNamespace::GetExportedNamespace(kNeuralNetworksNamespaceName, is_bridged); |
Bernie Innocenti | 4bd5895 | 2020-02-06 15:43:57 +0900 | [diff] [blame] | 272 | if (nnapi_ns.ok()) { |
Orion Hodson | 9b16e34 | 2019-10-09 13:29:16 +0100 | [diff] [blame] | 273 | linked = app_ns->Link(*nnapi_ns, neuralnetworks_public_libraries()); |
Bernie Innocenti | 4bd5895 | 2020-02-06 15:43:57 +0900 | [diff] [blame] | 274 | if (!linked.ok()) { |
Orion Hodson | 9b16e34 | 2019-10-09 13:29:16 +0100 | [diff] [blame] | 275 | return linked.error(); |
| 276 | } |
| 277 | } |
| 278 | |
Justin Yun | eb4f08c | 2020-02-18 11:29:07 +0900 | [diff] [blame^] | 279 | // Give access to VNDK-SP libraries from the 'vndk' namespace for unbundled vendor apps. |
| 280 | if (unbundled_app_origin == APK_ORIGIN_VENDOR && !vndksp_libraries_vendor().empty()) { |
Orion Hodson | 9b16e34 | 2019-10-09 13:29:16 +0100 | [diff] [blame] | 281 | auto vndk_ns = NativeLoaderNamespace::GetExportedNamespace(kVndkNamespaceName, is_bridged); |
Bernie Innocenti | 4bd5895 | 2020-02-06 15:43:57 +0900 | [diff] [blame] | 282 | if (vndk_ns.ok()) { |
Justin Yun | eb4f08c | 2020-02-18 11:29:07 +0900 | [diff] [blame^] | 283 | linked = app_ns->Link(*vndk_ns, vndksp_libraries_vendor()); |
| 284 | if (!linked.ok()) { |
| 285 | return linked.error(); |
| 286 | } |
| 287 | } |
| 288 | } |
| 289 | |
| 290 | // Give access to VNDK-SP libraries from the 'vndk_product' namespace for unbundled product apps. |
| 291 | if (unbundled_app_origin == APK_ORIGIN_PRODUCT && !vndksp_libraries_product().empty()) { |
| 292 | auto vndk_ns = NativeLoaderNamespace::GetExportedNamespace(kVndkProductNamespaceName, is_bridged); |
| 293 | if (vndk_ns.ok()) { |
| 294 | linked = app_ns->Link(*vndk_ns, vndksp_libraries_product()); |
Bernie Innocenti | 4bd5895 | 2020-02-06 15:43:57 +0900 | [diff] [blame] | 295 | if (!linked.ok()) { |
Orion Hodson | 9b16e34 | 2019-10-09 13:29:16 +0100 | [diff] [blame] | 296 | return linked.error(); |
| 297 | } |
| 298 | } |
| 299 | } |
| 300 | |
Luke Huang | 5c01772 | 2019-12-17 10:54:26 +0800 | [diff] [blame] | 301 | // TODO(b/143733063): Remove it after library path of apex module is supported. |
| 302 | auto cronet_ns = |
| 303 | NativeLoaderNamespace::GetExportedNamespace(kCronetNamespaceName, is_bridged); |
Bernie Innocenti | 4bd5895 | 2020-02-06 15:43:57 +0900 | [diff] [blame] | 304 | if (cronet_ns.ok()) { |
Luke Huang | 5c01772 | 2019-12-17 10:54:26 +0800 | [diff] [blame] | 305 | linked = app_ns->Link(*cronet_ns, cronet_public_libraries()); |
Bernie Innocenti | 4bd5895 | 2020-02-06 15:43:57 +0900 | [diff] [blame] | 306 | if (!linked.ok()) { |
Luke Huang | 5c01772 | 2019-12-17 10:54:26 +0800 | [diff] [blame] | 307 | return linked.error(); |
| 308 | } |
| 309 | } |
| 310 | |
Jeffrey Huang | 5257503 | 2020-02-11 17:33:45 -0800 | [diff] [blame] | 311 | // Give access to StatsdAPI libraries |
| 312 | auto statsd_ns = |
| 313 | NativeLoaderNamespace::GetExportedNamespace(kStatsdNamespaceName, is_bridged); |
| 314 | if (statsd_ns.ok()) { |
| 315 | linked = app_ns->Link(*statsd_ns, statsd_public_libraries()); |
| 316 | if (!linked.ok()) { |
| 317 | return linked.error(); |
| 318 | } |
| 319 | } |
| 320 | |
Orion Hodson | 9b16e34 | 2019-10-09 13:29:16 +0100 | [diff] [blame] | 321 | if (!vendor_public_libraries().empty()) { |
| 322 | auto vendor_ns = NativeLoaderNamespace::GetExportedNamespace(kVendorNamespaceName, is_bridged); |
Kiyoung Kim | 99c19ca | 2020-01-29 16:09:38 +0900 | [diff] [blame] | 323 | // when vendor_ns is not configured, link to the system namespace |
Martin Stjernholm | f0e99bd | 2020-02-11 22:57:14 +0000 | [diff] [blame] | 324 | auto target_ns = vendor_ns.ok() ? vendor_ns : system_ns; |
Bernie Innocenti | 4bd5895 | 2020-02-06 15:43:57 +0900 | [diff] [blame] | 325 | if (target_ns.ok()) { |
Orion Hodson | 9b16e34 | 2019-10-09 13:29:16 +0100 | [diff] [blame] | 326 | linked = app_ns->Link(*target_ns, vendor_public_libraries()); |
Bernie Innocenti | 4bd5895 | 2020-02-06 15:43:57 +0900 | [diff] [blame] | 327 | if (!linked.ok()) { |
Orion Hodson | 9b16e34 | 2019-10-09 13:29:16 +0100 | [diff] [blame] | 328 | return linked.error(); |
| 329 | } |
| 330 | } |
| 331 | } |
| 332 | |
Orion Hodson | c20ab9a | 2020-02-06 12:32:08 +0000 | [diff] [blame] | 333 | auto& emplaced = namespaces_.emplace_back( |
| 334 | std::make_pair(env->NewWeakGlobalRef(class_loader), *app_ns)); |
Orion Hodson | 9b16e34 | 2019-10-09 13:29:16 +0100 | [diff] [blame] | 335 | if (is_main_classloader) { |
Orion Hodson | c20ab9a | 2020-02-06 12:32:08 +0000 | [diff] [blame] | 336 | app_main_namespace_ = &emplaced.second; |
Orion Hodson | 9b16e34 | 2019-10-09 13:29:16 +0100 | [diff] [blame] | 337 | } |
Orion Hodson | c20ab9a | 2020-02-06 12:32:08 +0000 | [diff] [blame] | 338 | return &emplaced.second; |
Orion Hodson | 9b16e34 | 2019-10-09 13:29:16 +0100 | [diff] [blame] | 339 | } |
| 340 | |
| 341 | NativeLoaderNamespace* LibraryNamespaces::FindNamespaceByClassLoader(JNIEnv* env, |
| 342 | jobject class_loader) { |
| 343 | auto it = std::find_if(namespaces_.begin(), namespaces_.end(), |
| 344 | [&](const std::pair<jweak, NativeLoaderNamespace>& value) { |
| 345 | return env->IsSameObject(value.first, class_loader); |
| 346 | }); |
| 347 | if (it != namespaces_.end()) { |
| 348 | return &it->second; |
| 349 | } |
| 350 | |
| 351 | return nullptr; |
| 352 | } |
| 353 | |
| 354 | NativeLoaderNamespace* LibraryNamespaces::FindParentNamespaceByClassLoader(JNIEnv* env, |
| 355 | jobject class_loader) { |
| 356 | jobject parent_class_loader = GetParentClassLoader(env, class_loader); |
| 357 | |
| 358 | while (parent_class_loader != nullptr) { |
| 359 | NativeLoaderNamespace* ns; |
| 360 | if ((ns = FindNamespaceByClassLoader(env, parent_class_loader)) != nullptr) { |
| 361 | return ns; |
| 362 | } |
| 363 | |
| 364 | parent_class_loader = GetParentClassLoader(env, parent_class_loader); |
| 365 | } |
| 366 | |
| 367 | return nullptr; |
| 368 | } |
| 369 | |
| 370 | } // namespace android::nativeloader |