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