blob: 330903d028c84acb6441e4494acb1015e5192ad6 [file] [log] [blame]
Orion Hodson9b16e342019-10-09 13:29:16 +01001/*
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 */
Nicolas Geoffray7ca8b672020-04-24 15:43:48 +010016
17#if defined(ART_TARGET_ANDROID)
18
Orion Hodson9b16e342019-10-09 13:29:16 +010019#include "library_namespaces.h"
20
21#include <dirent.h>
22#include <dlfcn.h>
23
24#include <regex>
25#include <string>
26#include <vector>
27
28#include <android-base/file.h>
29#include <android-base/logging.h>
30#include <android-base/macros.h>
31#include <android-base/properties.h>
Jooyung Han538f99a2020-03-03 00:46:50 +090032#include <android-base/result.h>
Orion Hodson9b16e342019-10-09 13:29:16 +010033#include <android-base/strings.h>
Orion Hodson6dc0a432020-02-06 14:28:28 +000034#include <nativehelper/scoped_utf_chars.h>
Orion Hodson9b16e342019-10-09 13:29:16 +010035
36#include "nativeloader/dlext_namespaces.h"
37#include "public_libraries.h"
38#include "utils.h"
39
Orion Hodson9b16e342019-10-09 13:29:16 +010040namespace android::nativeloader {
41
42namespace {
Jooyung Han538f99a2020-03-03 00:46:50 +090043
44constexpr const char* kApexPath = "/apex/";
45
Orion Hodson9b16e342019-10-09 13:29:16 +010046// The device may be configured to have the vendor libraries loaded to a separate namespace.
47// For historical reasons this namespace was named sphal but effectively it is intended
48// to use to load vendor libraries to separate namespace with controlled interface between
49// vendor and system namespaces.
50constexpr const char* kVendorNamespaceName = "sphal";
51constexpr const char* kVndkNamespaceName = "vndk";
Justin Yuneb4f08c2020-02-18 11:29:07 +090052constexpr const char* kVndkProductNamespaceName = "vndk_product";
Kiyoung Kim272b36d2020-02-19 16:08:47 +090053constexpr const char* kArtNamespaceName = "com_android_art";
Victor Changd20e51d2020-05-05 16:01:19 +010054constexpr const char* kI18nNamespaceName = "com_android_i18n";
Kiyoung Kim272b36d2020-02-19 16:08:47 +090055constexpr const char* kNeuralNetworksNamespaceName = "com_android_neuralnetworks";
Kiyoung Kim272b36d2020-02-19 16:08:47 +090056constexpr const char* kStatsdNamespaceName = "com_android_os_statsd";
Orion Hodson9b16e342019-10-09 13:29:16 +010057
58// classloader-namespace is a linker namespace that is created for the loaded
59// app. To be specific, it is created for the app classloader. When
60// System.load() is called from a Java class that is loaded from the
61// classloader, the classloader-namespace namespace associated with that
62// classloader is selected for dlopen. The namespace is configured so that its
63// search path is set to the app-local JNI directory and it is linked to the
Kiyoung Kim99c19ca2020-01-29 16:09:38 +090064// system namespace with the names of libs listed in the public.libraries.txt.
Orion Hodson9b16e342019-10-09 13:29:16 +010065// This way an app can only load its own JNI libraries along with the public libs.
66constexpr const char* kClassloaderNamespaceName = "classloader-namespace";
67// Same thing for vendor APKs.
68constexpr const char* kVendorClassloaderNamespaceName = "vendor-classloader-namespace";
Martin Stjernholm94fd9ea2019-10-24 16:57:34 +010069// If the namespace is shared then add this suffix to form
70// "classloader-namespace-shared" or "vendor-classloader-namespace-shared",
71// respectively. A shared namespace (cf. ANDROID_NAMESPACE_TYPE_SHARED) has
72// inherited all the libraries of the parent classloader namespace, or the
Kiyoung Kim99c19ca2020-01-29 16:09:38 +090073// system namespace for the main app classloader. It is used to give full
Martin Stjernholm94fd9ea2019-10-24 16:57:34 +010074// access to the platform libraries for apps bundled in the system image,
75// including their later updates installed in /data.
76constexpr const char* kSharedNamespaceSuffix = "-shared";
Orion Hodson9b16e342019-10-09 13:29:16 +010077
78// (http://b/27588281) This is a workaround for apps using custom classloaders and calling
79// System.load() with an absolute path which is outside of the classloader library search path.
80// This list includes all directories app is allowed to access this way.
81constexpr const char* kWhitelistedDirectories = "/data:/mnt/expand";
82
83constexpr const char* kVendorLibPath = "/vendor/" LIB;
84constexpr const char* kProductLibPath = "/product/" LIB ":/system/product/" LIB;
85
86const std::regex kVendorDexPathRegex("(^|:)/vendor/");
87const std::regex kProductDexPathRegex("(^|:)(/system)?/product/");
88
89// Define origin of APK if it is from vendor partition or product partition
Martin Stjernholm3bb009a2019-10-17 21:29:01 +010090using ApkOrigin = enum {
Orion Hodson9b16e342019-10-09 13:29:16 +010091 APK_ORIGIN_DEFAULT = 0,
92 APK_ORIGIN_VENDOR = 1,
93 APK_ORIGIN_PRODUCT = 2,
Martin Stjernholm3bb009a2019-10-17 21:29:01 +010094};
Orion Hodson9b16e342019-10-09 13:29:16 +010095
96jobject GetParentClassLoader(JNIEnv* env, jobject class_loader) {
97 jclass class_loader_class = env->FindClass("java/lang/ClassLoader");
98 jmethodID get_parent =
99 env->GetMethodID(class_loader_class, "getParent", "()Ljava/lang/ClassLoader;");
100
101 return env->CallObjectMethod(class_loader, get_parent);
102}
103
Jooyung Han538f99a2020-03-03 00:46:50 +0900104ApkOrigin GetApkOriginFromDexPath(const std::string& dex_path) {
Orion Hodson9b16e342019-10-09 13:29:16 +0100105 ApkOrigin apk_origin = APK_ORIGIN_DEFAULT;
Jooyung Han538f99a2020-03-03 00:46:50 +0900106 if (std::regex_search(dex_path, kVendorDexPathRegex)) {
107 apk_origin = APK_ORIGIN_VENDOR;
108 }
109 if (std::regex_search(dex_path, kProductDexPathRegex)) {
110 LOG_ALWAYS_FATAL_IF(apk_origin == APK_ORIGIN_VENDOR,
111 "Dex path contains both vendor and product partition : %s",
112 dex_path.c_str());
Orion Hodson9b16e342019-10-09 13:29:16 +0100113
Jooyung Han538f99a2020-03-03 00:46:50 +0900114 apk_origin = APK_ORIGIN_PRODUCT;
Orion Hodson9b16e342019-10-09 13:29:16 +0100115 }
116 return apk_origin;
117}
118
119} // namespace
120
121void LibraryNamespaces::Initialize() {
122 // Once public namespace is initialized there is no
123 // point in running this code - it will have no effect
124 // on the current list of public libraries.
125 if (initialized_) {
126 return;
127 }
128
129 // android_init_namespaces() expects all the public libraries
130 // to be loaded so that they can be found by soname alone.
131 //
132 // TODO(dimitry): this is a bit misleading since we do not know
133 // if the vendor public library is going to be opened from /vendor/lib
134 // we might as well end up loading them from /system/lib or /product/lib
135 // For now we rely on CTS test to catch things like this but
136 // it should probably be addressed in the future.
137 for (const auto& soname : android::base::Split(preloadable_public_libraries(), ":")) {
138 LOG_ALWAYS_FATAL_IF(dlopen(soname.c_str(), RTLD_NOW | RTLD_NODELETE) == nullptr,
139 "Error preloading public library %s: %s", soname.c_str(), dlerror());
140 }
141}
142
Jiyong Park14626a72020-07-02 23:17:58 +0900143// "ALL" is a magic name that allows all public libraries even when the
144// target SDK is > 30. Currently this is used for (Java) shared libraries
145// which don't use <uses-native-library>
146// TODO(b/142191088) remove this hack
147static constexpr const char LIBRARY_ALL[] = "ALL";
148
149// Returns the colon-separated list of library names by filtering uses_libraries from
150// public_libraries. The returned names will actually be available to the app. If the app is pre-S
151// (<= 30), the filtering is not done; the entire public_libraries are provided.
152static const std::string filter_public_libraries(
153 uint32_t target_sdk_version, const std::vector<std::string>& uses_libraries,
154 const std::string& public_libraries) {
155 // Apps targeting Android 11 or earlier gets all public libraries
156 if (target_sdk_version <= 30) {
157 return public_libraries;
158 }
159 if (std::find(uses_libraries.begin(), uses_libraries.end(), LIBRARY_ALL) !=
160 uses_libraries.end()) {
161 return public_libraries;
162 }
163 std::vector<std::string> filtered;
164 std::vector<std::string> orig = android::base::Split(public_libraries, ":");
165 for (const auto& lib : uses_libraries) {
166 if (std::find(orig.begin(), orig.end(), lib) != orig.end()) {
167 filtered.emplace_back(lib);
168 }
169 }
170 return android::base::Join(filtered, ":");
171}
172
Orion Hodson9b16e342019-10-09 13:29:16 +0100173Result<NativeLoaderNamespace*> LibraryNamespaces::Create(JNIEnv* env, uint32_t target_sdk_version,
174 jobject class_loader, bool is_shared,
Jooyung Han538f99a2020-03-03 00:46:50 +0900175 jstring dex_path_j,
Orion Hodson9b16e342019-10-09 13:29:16 +0100176 jstring java_library_path,
Jiyong Park14626a72020-07-02 23:17:58 +0900177 jstring java_permitted_path,
178 jstring uses_library_list) {
Orion Hodson9b16e342019-10-09 13:29:16 +0100179 std::string library_path; // empty string by default.
Jooyung Han538f99a2020-03-03 00:46:50 +0900180 std::string dex_path;
Orion Hodson9b16e342019-10-09 13:29:16 +0100181
182 if (java_library_path != nullptr) {
183 ScopedUtfChars library_path_utf_chars(env, java_library_path);
184 library_path = library_path_utf_chars.c_str();
185 }
186
Jooyung Han538f99a2020-03-03 00:46:50 +0900187 if (dex_path_j != nullptr) {
188 ScopedUtfChars dex_path_chars(env, dex_path_j);
189 dex_path = dex_path_chars.c_str();
190 }
191
Jiyong Park14626a72020-07-02 23:17:58 +0900192 std::vector<std::string> uses_libraries;
193 if (uses_library_list != nullptr) {
194 ScopedUtfChars names(env, uses_library_list);
195 uses_libraries = android::base::Split(names.c_str(), ":");
196 } else {
197 // uses_library_list could be nullptr when System.loadLibrary is called from a
198 // custom classloader. In that case, we don't know the list of public
199 // libraries because we don't know which apk the classloader is for. Only
200 // choices we can have are 1) allowing all public libs (as before), or 2)
201 // not allowing all but NDK libs. Here we take #1 because #2 would surprise
202 // developers unnecessarily.
203 // TODO(b/142191088) finalize the policy here. We could either 1) allow all
204 // public libs, 2) disallow any lib, or 3) use the libs that were granted to
205 // the first (i.e. app main) classloader.
206 uses_libraries.emplace_back(LIBRARY_ALL);
207 }
208
Jooyung Han538f99a2020-03-03 00:46:50 +0900209 ApkOrigin apk_origin = GetApkOriginFromDexPath(dex_path);
Orion Hodson9b16e342019-10-09 13:29:16 +0100210
211 // (http://b/27588281) This is a workaround for apps using custom
212 // classloaders and calling System.load() with an absolute path which
213 // is outside of the classloader library search path.
214 //
215 // This part effectively allows such a classloader to access anything
216 // under /data and /mnt/expand
217 std::string permitted_path = kWhitelistedDirectories;
218
219 if (java_permitted_path != nullptr) {
220 ScopedUtfChars path(env, java_permitted_path);
221 if (path.c_str() != nullptr && path.size() > 0) {
222 permitted_path = permitted_path + ":" + path.c_str();
223 }
224 }
225
226 LOG_ALWAYS_FATAL_IF(FindNamespaceByClassLoader(env, class_loader) != nullptr,
227 "There is already a namespace associated with this classloader");
228
229 std::string system_exposed_libraries = default_public_libraries();
Martin Stjernholm94fd9ea2019-10-24 16:57:34 +0100230 std::string namespace_name = kClassloaderNamespaceName;
Justin Yuneb4f08c2020-02-18 11:29:07 +0900231 ApkOrigin unbundled_app_origin = APK_ORIGIN_DEFAULT;
Orion Hodson9b16e342019-10-09 13:29:16 +0100232 if ((apk_origin == APK_ORIGIN_VENDOR ||
Justin Yun3db26d52019-12-16 14:09:39 +0900233 (apk_origin == APK_ORIGIN_PRODUCT &&
234 is_product_vndk_version_defined())) &&
Orion Hodson9b16e342019-10-09 13:29:16 +0100235 !is_shared) {
Justin Yuneb4f08c2020-02-18 11:29:07 +0900236 unbundled_app_origin = apk_origin;
Orion Hodson9b16e342019-10-09 13:29:16 +0100237 // For vendor / product apks, give access to the vendor / product lib even though
238 // they are treated as unbundled; the libs and apks are still bundled
239 // together in the vendor / product partition.
240 const char* origin_partition;
241 const char* origin_lib_path;
Justin Yun089c1352020-02-06 16:53:08 +0900242 const char* llndk_libraries;
Orion Hodson9b16e342019-10-09 13:29:16 +0100243
244 switch (apk_origin) {
245 case APK_ORIGIN_VENDOR:
246 origin_partition = "vendor";
247 origin_lib_path = kVendorLibPath;
Justin Yun089c1352020-02-06 16:53:08 +0900248 llndk_libraries = llndk_libraries_vendor().c_str();
Orion Hodson9b16e342019-10-09 13:29:16 +0100249 break;
250 case APK_ORIGIN_PRODUCT:
251 origin_partition = "product";
252 origin_lib_path = kProductLibPath;
Justin Yun089c1352020-02-06 16:53:08 +0900253 llndk_libraries = llndk_libraries_product().c_str();
Orion Hodson9b16e342019-10-09 13:29:16 +0100254 break;
255 default:
256 origin_partition = "unknown";
257 origin_lib_path = "";
Justin Yun089c1352020-02-06 16:53:08 +0900258 llndk_libraries = "";
Orion Hodson9b16e342019-10-09 13:29:16 +0100259 }
260 library_path = library_path + ":" + origin_lib_path;
261 permitted_path = permitted_path + ":" + origin_lib_path;
262
Justin Yun089c1352020-02-06 16:53:08 +0900263 // Also give access to LLNDK libraries since they are available to vendor or product
264 system_exposed_libraries = system_exposed_libraries + ":" + llndk_libraries;
Orion Hodson9b16e342019-10-09 13:29:16 +0100265
266 // Different name is useful for debugging
267 namespace_name = kVendorClassloaderNamespaceName;
268 ALOGD("classloader namespace configured for unbundled %s apk. library_path=%s",
269 origin_partition, library_path.c_str());
270 } else {
Jiyong Park14626a72020-07-02 23:17:58 +0900271 auto libs = filter_public_libraries(target_sdk_version, uses_libraries,
272 extended_public_libraries());
Orion Hodson9b16e342019-10-09 13:29:16 +0100273 // extended public libraries are NOT available to vendor apks, otherwise it
274 // would be system->vendor violation.
Jiyong Park14626a72020-07-02 23:17:58 +0900275 if (!libs.empty()) {
276 system_exposed_libraries = system_exposed_libraries + ':' + libs;
Orion Hodson9b16e342019-10-09 13:29:16 +0100277 }
278 }
279
Martin Stjernholm94fd9ea2019-10-24 16:57:34 +0100280 if (is_shared) {
281 // Show in the name that the namespace was created as shared, for debugging
282 // purposes.
283 namespace_name = namespace_name + kSharedNamespaceSuffix;
284 }
285
Orion Hodson9b16e342019-10-09 13:29:16 +0100286 // Create the app namespace
287 NativeLoaderNamespace* parent_ns = FindParentNamespaceByClassLoader(env, class_loader);
288 // Heuristic: the first classloader with non-empty library_path is assumed to
289 // be the main classloader for app
290 // TODO(b/139178525) remove this heuristic by determining this in LoadedApk (or its
291 // friends) and then passing it down to here.
292 bool is_main_classloader = app_main_namespace_ == nullptr && !library_path.empty();
293 // Policy: the namespace for the main classloader is also used as the
294 // anonymous namespace.
295 bool also_used_as_anonymous = is_main_classloader;
296 // Note: this function is executed with g_namespaces_mutex held, thus no
297 // racing here.
298 auto app_ns = NativeLoaderNamespace::Create(
299 namespace_name, library_path, permitted_path, parent_ns, is_shared,
300 target_sdk_version < 24 /* is_greylist_enabled */, also_used_as_anonymous);
Bernie Innocenti4bd58952020-02-06 15:43:57 +0900301 if (!app_ns.ok()) {
Orion Hodson9b16e342019-10-09 13:29:16 +0100302 return app_ns.error();
303 }
304 // ... and link to other namespaces to allow access to some public libraries
305 bool is_bridged = app_ns->IsBridged();
306
Martin Stjernholmf0e99bd2020-02-11 22:57:14 +0000307 auto system_ns = NativeLoaderNamespace::GetSystemNamespace(is_bridged);
308 if (!system_ns.ok()) {
309 return system_ns.error();
Orion Hodson9b16e342019-10-09 13:29:16 +0100310 }
311
Martin Stjernholmf0e99bd2020-02-11 22:57:14 +0000312 auto linked = app_ns->Link(*system_ns, system_exposed_libraries);
Bernie Innocenti4bd58952020-02-06 15:43:57 +0900313 if (!linked.ok()) {
Orion Hodson9b16e342019-10-09 13:29:16 +0100314 return linked.error();
315 }
316
317 auto art_ns = NativeLoaderNamespace::GetExportedNamespace(kArtNamespaceName, is_bridged);
318 // ART APEX does not exist on host, and under certain build conditions.
Bernie Innocenti4bd58952020-02-06 15:43:57 +0900319 if (art_ns.ok()) {
Orion Hodson9b16e342019-10-09 13:29:16 +0100320 linked = app_ns->Link(*art_ns, art_public_libraries());
Bernie Innocenti4bd58952020-02-06 15:43:57 +0900321 if (!linked.ok()) {
Orion Hodson9b16e342019-10-09 13:29:16 +0100322 return linked.error();
323 }
324 }
325
Victor Changd20e51d2020-05-05 16:01:19 +0100326 auto i18n_ns = NativeLoaderNamespace::GetExportedNamespace(kI18nNamespaceName, is_bridged);
327 // i18n APEX does not exist on host, and under certain build conditions.
328 if (i18n_ns.ok()) {
329 linked = app_ns->Link(*i18n_ns, i18n_public_libraries());
330 if (!linked.ok()) {
331 return linked.error();
332 }
333 }
334
Orion Hodson9b16e342019-10-09 13:29:16 +0100335 // Give access to NNAPI libraries (apex-updated LLNDK library).
336 auto nnapi_ns =
337 NativeLoaderNamespace::GetExportedNamespace(kNeuralNetworksNamespaceName, is_bridged);
Bernie Innocenti4bd58952020-02-06 15:43:57 +0900338 if (nnapi_ns.ok()) {
Orion Hodson9b16e342019-10-09 13:29:16 +0100339 linked = app_ns->Link(*nnapi_ns, neuralnetworks_public_libraries());
Bernie Innocenti4bd58952020-02-06 15:43:57 +0900340 if (!linked.ok()) {
Orion Hodson9b16e342019-10-09 13:29:16 +0100341 return linked.error();
342 }
343 }
344
Justin Yuneb4f08c2020-02-18 11:29:07 +0900345 // Give access to VNDK-SP libraries from the 'vndk' namespace for unbundled vendor apps.
346 if (unbundled_app_origin == APK_ORIGIN_VENDOR && !vndksp_libraries_vendor().empty()) {
Orion Hodson9b16e342019-10-09 13:29:16 +0100347 auto vndk_ns = NativeLoaderNamespace::GetExportedNamespace(kVndkNamespaceName, is_bridged);
Bernie Innocenti4bd58952020-02-06 15:43:57 +0900348 if (vndk_ns.ok()) {
Justin Yuneb4f08c2020-02-18 11:29:07 +0900349 linked = app_ns->Link(*vndk_ns, vndksp_libraries_vendor());
350 if (!linked.ok()) {
351 return linked.error();
352 }
353 }
354 }
355
356 // Give access to VNDK-SP libraries from the 'vndk_product' namespace for unbundled product apps.
357 if (unbundled_app_origin == APK_ORIGIN_PRODUCT && !vndksp_libraries_product().empty()) {
358 auto vndk_ns = NativeLoaderNamespace::GetExportedNamespace(kVndkProductNamespaceName, is_bridged);
359 if (vndk_ns.ok()) {
360 linked = app_ns->Link(*vndk_ns, vndksp_libraries_product());
Bernie Innocenti4bd58952020-02-06 15:43:57 +0900361 if (!linked.ok()) {
Orion Hodson9b16e342019-10-09 13:29:16 +0100362 return linked.error();
363 }
364 }
365 }
366
Jooyung Han538f99a2020-03-03 00:46:50 +0900367 auto apex_ns_name = FindApexNamespaceName(dex_path);
368 if (apex_ns_name.ok()) {
369 const auto& jni_libs = apex_jni_libraries(*apex_ns_name);
370 if (jni_libs != "") {
371 auto apex_ns = NativeLoaderNamespace::GetExportedNamespace(*apex_ns_name, is_bridged);
372 if (apex_ns.ok()) {
373 auto link = app_ns->Link(*apex_ns, jni_libs);
374 if (!link.ok()) {
375 return linked.error();
376 }
377 }
378 }
379 }
380
Jeffrey Huang52575032020-02-11 17:33:45 -0800381 // Give access to StatsdAPI libraries
382 auto statsd_ns =
383 NativeLoaderNamespace::GetExportedNamespace(kStatsdNamespaceName, is_bridged);
384 if (statsd_ns.ok()) {
385 linked = app_ns->Link(*statsd_ns, statsd_public_libraries());
386 if (!linked.ok()) {
387 return linked.error();
388 }
389 }
390
Jiyong Park14626a72020-07-02 23:17:58 +0900391 auto vendor_libs = filter_public_libraries(target_sdk_version, uses_libraries,
392 vendor_public_libraries());
393 if (!vendor_libs.empty()) {
Orion Hodson9b16e342019-10-09 13:29:16 +0100394 auto vendor_ns = NativeLoaderNamespace::GetExportedNamespace(kVendorNamespaceName, is_bridged);
Kiyoung Kim99c19ca2020-01-29 16:09:38 +0900395 // when vendor_ns is not configured, link to the system namespace
Martin Stjernholmf0e99bd2020-02-11 22:57:14 +0000396 auto target_ns = vendor_ns.ok() ? vendor_ns : system_ns;
Bernie Innocenti4bd58952020-02-06 15:43:57 +0900397 if (target_ns.ok()) {
Jiyong Park14626a72020-07-02 23:17:58 +0900398 linked = app_ns->Link(*target_ns, vendor_libs);
Bernie Innocenti4bd58952020-02-06 15:43:57 +0900399 if (!linked.ok()) {
Orion Hodson9b16e342019-10-09 13:29:16 +0100400 return linked.error();
401 }
402 }
403 }
404
Orion Hodsonc20ab9a2020-02-06 12:32:08 +0000405 auto& emplaced = namespaces_.emplace_back(
406 std::make_pair(env->NewWeakGlobalRef(class_loader), *app_ns));
Orion Hodson9b16e342019-10-09 13:29:16 +0100407 if (is_main_classloader) {
Orion Hodsonc20ab9a2020-02-06 12:32:08 +0000408 app_main_namespace_ = &emplaced.second;
Orion Hodson9b16e342019-10-09 13:29:16 +0100409 }
Orion Hodsonc20ab9a2020-02-06 12:32:08 +0000410 return &emplaced.second;
Orion Hodson9b16e342019-10-09 13:29:16 +0100411}
412
413NativeLoaderNamespace* LibraryNamespaces::FindNamespaceByClassLoader(JNIEnv* env,
414 jobject class_loader) {
415 auto it = std::find_if(namespaces_.begin(), namespaces_.end(),
416 [&](const std::pair<jweak, NativeLoaderNamespace>& value) {
417 return env->IsSameObject(value.first, class_loader);
418 });
419 if (it != namespaces_.end()) {
420 return &it->second;
421 }
422
423 return nullptr;
424}
425
426NativeLoaderNamespace* LibraryNamespaces::FindParentNamespaceByClassLoader(JNIEnv* env,
427 jobject class_loader) {
428 jobject parent_class_loader = GetParentClassLoader(env, class_loader);
429
430 while (parent_class_loader != nullptr) {
431 NativeLoaderNamespace* ns;
432 if ((ns = FindNamespaceByClassLoader(env, parent_class_loader)) != nullptr) {
433 return ns;
434 }
435
436 parent_class_loader = GetParentClassLoader(env, parent_class_loader);
437 }
438
439 return nullptr;
440}
441
Jooyung Han538f99a2020-03-03 00:46:50 +0900442base::Result<std::string> FindApexNamespaceName(const std::string& location) {
443 // Lots of implicit assumptions here: we expect `location` to be of the form:
444 // /apex/modulename/...
445 //
446 // And we extract from it 'modulename', and then apply mangling rule to get namespace name for it.
447 if (android::base::StartsWith(location, kApexPath)) {
448 size_t start_index = strlen(kApexPath);
449 size_t slash_index = location.find_first_of('/', start_index);
450 LOG_ALWAYS_FATAL_IF((slash_index == std::string::npos),
451 "Error finding namespace of apex: no slash in path %s", location.c_str());
452 std::string name = location.substr(start_index, slash_index - start_index);
453 std::replace(name.begin(), name.end(), '.', '_');
454 return name;
455 }
456 return base::Error();
457}
458
Orion Hodson9b16e342019-10-09 13:29:16 +0100459} // namespace android::nativeloader
Nicolas Geoffray7ca8b672020-04-24 15:43:48 +0100460
461#endif // defined(ART_TARGET_ANDROID)