blob: 5394d7eae16d61fe4662c3d80ae695d51695bbb7 [file] [log] [blame]
Dimitry Ivanovac1b1912015-12-01 13:56:44 -08001/*
2 * Copyright (C) 2015 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#include "nativeloader/native_loader.h"
Steven Moreland00fe3ad2017-07-18 16:53:54 -070018#include <nativehelper/ScopedUtfChars.h>
Dimitry Ivanovac1b1912015-12-01 13:56:44 -080019
20#include <dlfcn.h>
21#ifdef __ANDROID__
Dimitry Ivanov7f9a1aa2016-03-22 13:59:59 -070022#define LOG_TAG "libnativeloader"
Jesse Hallb75d82b2017-01-09 16:04:28 -080023#include "nativeloader/dlext_namespaces.h"
Mark Salyzynff2dcd92016-09-28 15:54:45 -070024#include "cutils/properties.h"
Mark Salyzyn30f991f2017-01-10 13:19:54 -080025#include "log/log.h"
Dimitry Ivanovac1b1912015-12-01 13:56:44 -080026#endif
Jiyong Parkd1006fe2017-11-08 18:44:09 +090027#include <dirent.h>
28#include <sys/types.h>
Zhenhua WANGf2804e52016-05-30 11:16:08 +080029#include "nativebridge/native_bridge.h"
Dimitry Ivanovac1b1912015-12-01 13:56:44 -080030
31#include <algorithm>
Victor Khimenko1443ec42018-07-09 17:01:22 +020032#include <list>
Jiyong Parkd1006fe2017-11-08 18:44:09 +090033#include <memory>
Dimitry Ivanovac1b1912015-12-01 13:56:44 -080034#include <mutex>
Jiyong Parkd1006fe2017-11-08 18:44:09 +090035#include <string>
36#include <vector>
Dimitry Ivanovac1b1912015-12-01 13:56:44 -080037
Mark Salyzynff2dcd92016-09-28 15:54:45 -070038#include <android-base/file.h>
39#include <android-base/macros.h>
40#include <android-base/strings.h>
Dimitry Ivanovac1b1912015-12-01 13:56:44 -080041
Justin Yun4a1d1102017-11-27 17:04:14 +090042#ifdef __BIONIC__
43#include <android-base/properties.h>
44#endif
45
Zhenhua WANGf2804e52016-05-30 11:16:08 +080046#define CHECK(predicate) LOG_ALWAYS_FATAL_IF(!(predicate),\
47 "%s:%d: %s CHECK '" #predicate "' failed.",\
48 __FILE__, __LINE__, __FUNCTION__)
49
Inseob Kim67cb0562018-05-04 11:39:12 +090050using namespace std::string_literals;
51
Dimitry Ivanovac1b1912015-12-01 13:56:44 -080052namespace android {
53
Dimitry Ivanov4b0e9632016-03-15 13:51:26 -070054#if defined(__ANDROID__)
Nicolas Geoffrayc3a73dc2019-01-12 15:01:20 +000055struct NativeLoaderNamespace {
Zhenhua WANGf2804e52016-05-30 11:16:08 +080056 public:
57 NativeLoaderNamespace()
58 : android_ns_(nullptr), native_bridge_ns_(nullptr) { }
59
60 explicit NativeLoaderNamespace(android_namespace_t* ns)
61 : android_ns_(ns), native_bridge_ns_(nullptr) { }
62
63 explicit NativeLoaderNamespace(native_bridge_namespace_t* ns)
64 : android_ns_(nullptr), native_bridge_ns_(ns) { }
65
66 NativeLoaderNamespace(NativeLoaderNamespace&& that) = default;
67 NativeLoaderNamespace(const NativeLoaderNamespace& that) = default;
68
69 NativeLoaderNamespace& operator=(const NativeLoaderNamespace& that) = default;
70
71 android_namespace_t* get_android_ns() const {
72 CHECK(native_bridge_ns_ == nullptr);
73 return android_ns_;
74 }
75
76 native_bridge_namespace_t* get_native_bridge_ns() const {
77 CHECK(android_ns_ == nullptr);
78 return native_bridge_ns_;
79 }
80
81 bool is_android_namespace() const {
82 return native_bridge_ns_ == nullptr;
83 }
84
85 private:
86 // Only one of them can be not null
87 android_namespace_t* android_ns_;
88 native_bridge_namespace_t* native_bridge_ns_;
89};
90
Jiyong Parkd1006fe2017-11-08 18:44:09 +090091static constexpr const char kPublicNativeLibrariesSystemConfigPathFromRoot[] =
92 "/etc/public.libraries.txt";
93static constexpr const char kPublicNativeLibrariesExtensionConfigPrefix[] = "public.libraries-";
94static constexpr const size_t kPublicNativeLibrariesExtensionConfigPrefixLen =
95 sizeof(kPublicNativeLibrariesExtensionConfigPrefix) - 1;
96static constexpr const char kPublicNativeLibrariesExtensionConfigSuffix[] = ".txt";
97static constexpr const size_t kPublicNativeLibrariesExtensionConfigSuffixLen =
98 sizeof(kPublicNativeLibrariesExtensionConfigSuffix) - 1;
99static constexpr const char kPublicNativeLibrariesVendorConfig[] =
100 "/vendor/etc/public.libraries.txt";
101static constexpr const char kLlndkNativeLibrariesSystemConfigPathFromRoot[] =
102 "/etc/llndk.libraries.txt";
103static constexpr const char kVndkspNativeLibrariesSystemConfigPathFromRoot[] =
104 "/etc/vndksp.libraries.txt";
Dimitry Ivanovac1b1912015-12-01 13:56:44 -0800105
Victor Chang7a20a902019-01-28 18:43:24 +0000106static const std::vector<const std::string> kRuntimePublicLibraries = {
107 "libicuuc.so",
108 "libicui18n.so",
109};
110
Dimitry Ivanov882cad22017-05-01 15:12:49 -0700111// The device may be configured to have the vendor libraries loaded to a separate namespace.
112// For historical reasons this namespace was named sphal but effectively it is intended
113// to use to load vendor libraries to separate namespace with controlled interface between
114// vendor and system namespaces.
115static constexpr const char* kVendorNamespaceName = "sphal";
116
Jiyong Parka07f3052017-08-22 10:26:10 +0900117static constexpr const char* kVndkNamespaceName = "vndk";
118
Victor Chang7a20a902019-01-28 18:43:24 +0000119static constexpr const char* kRuntimeNamespaceName = "runtime";
120
Martin Stjernholmb78f6ec2019-02-06 21:47:26 +0000121// classloader-namespace is a linker namespace that is created for the loaded
122// app. To be specific, it is created for the app classloader. When
123// System.load() is called from a Java class that is loaded from the
124// classloader, the classloader-namespace namespace associated with that
125// classloader is selected for dlopen. The namespace is configured so that its
126// search path is set to the app-local JNI directory and it is linked to the
127// default namespace with the names of libs listed in the public.libraries.txt.
128// This way an app can only load its own JNI libraries along with the public libs.
Jiyong Parka07f3052017-08-22 10:26:10 +0900129static constexpr const char* kClassloaderNamespaceName = "classloader-namespace";
Martin Stjernholmb78f6ec2019-02-06 21:47:26 +0000130// Same thing for vendor APKs.
Jiyong Parka07f3052017-08-22 10:26:10 +0900131static constexpr const char* kVendorClassloaderNamespaceName = "vendor-classloader-namespace";
132
Dimitry Ivanovf334cbf2016-05-10 10:39:48 -0700133// (http://b/27588281) This is a workaround for apps using custom classloaders and calling
134// System.load() with an absolute path which is outside of the classloader library search path.
135// This list includes all directories app is allowed to access this way.
136static constexpr const char* kWhitelistedDirectories = "/data:/mnt/expand";
137
Nicolas Geoffray890e3bf2019-01-22 09:11:57 +0000138static constexpr const char* kApexPath = "/apex/";
139
Dimitry Ivanov7d028292016-05-05 17:30:24 -0700140static bool is_debuggable() {
141 char debuggable[PROP_VALUE_MAX];
142 property_get("ro.debuggable", debuggable, "0");
143 return std::string(debuggable) == "1";
144}
145
Justin Yun4a1d1102017-11-27 17:04:14 +0900146static std::string vndk_version_str() {
147#ifdef __BIONIC__
148 std::string version = android::base::GetProperty("ro.vndk.version", "");
149 if (version != "" && version != "current") {
150 return "." + version;
151 }
152#endif
153 return "";
154}
155
156static void insert_vndk_version_str(std::string* file_name) {
157 CHECK(file_name != nullptr);
158 size_t insert_pos = file_name->find_last_of(".");
159 if (insert_pos == std::string::npos) {
160 insert_pos = file_name->length();
161 }
162 file_name->insert(insert_pos, vndk_version_str());
163}
164
Jiyong Parkd1006fe2017-11-08 18:44:09 +0900165static const std::function<bool(const std::string&, std::string*)> always_true =
166 [](const std::string&, std::string*) { return true; };
167
Dimitry Ivanovac1b1912015-12-01 13:56:44 -0800168class LibraryNamespaces {
169 public:
Dimitry Ivanov426799d2016-02-22 11:27:48 -0800170 LibraryNamespaces() : initialized_(false) { }
Dimitry Ivanovac1b1912015-12-01 13:56:44 -0800171
Nicolas Geoffrayc3a73dc2019-01-12 15:01:20 +0000172 NativeLoaderNamespace* Create(JNIEnv* env, uint32_t target_sdk_version, jobject class_loader,
173 bool is_shared, bool is_for_vendor, jstring java_library_path,
174 jstring java_permitted_path, std::string* error_msg) {
Dimitry Ivanovcf9892b2016-05-09 10:55:50 -0700175 std::string library_path; // empty string by default.
176
177 if (java_library_path != nullptr) {
178 ScopedUtfChars library_path_utf_chars(env, java_library_path);
179 library_path = library_path_utf_chars.c_str();
180 }
Dimitry Ivanovac1b1912015-12-01 13:56:44 -0800181
Dimitry Ivanovf334cbf2016-05-10 10:39:48 -0700182 // (http://b/27588281) This is a workaround for apps using custom
183 // classloaders and calling System.load() with an absolute path which
184 // is outside of the classloader library search path.
185 //
186 // This part effectively allows such a classloader to access anything
187 // under /data and /mnt/expand
188 std::string permitted_path = kWhitelistedDirectories;
189
Dimitry Ivanov0d6e5942015-12-08 11:16:56 -0800190 if (java_permitted_path != nullptr) {
191 ScopedUtfChars path(env, java_permitted_path);
Dimitry Ivanovd0b15312016-05-10 16:21:25 -0700192 if (path.c_str() != nullptr && path.size() > 0) {
193 permitted_path = permitted_path + ":" + path.c_str();
194 }
Dimitry Ivanov0d6e5942015-12-08 11:16:56 -0800195 }
196
Zhenhua WANGf2804e52016-05-30 11:16:08 +0800197 if (!initialized_ && !InitPublicNamespace(library_path.c_str(), error_msg)) {
Victor Khimenko1443ec42018-07-09 17:01:22 +0200198 return nullptr;
Dimitry Ivanovac1b1912015-12-01 13:56:44 -0800199 }
200
Victor Khimenko1443ec42018-07-09 17:01:22 +0200201 bool found = FindNamespaceByClassLoader(env, class_loader);
Dimitry Ivanovac1b1912015-12-01 13:56:44 -0800202
Zhenhua WANGf2804e52016-05-30 11:16:08 +0800203 LOG_ALWAYS_FATAL_IF(found,
Dimitry Ivanov4b0e9632016-03-15 13:51:26 -0700204 "There is already a namespace associated with this classloader");
Dimitry Ivanovac1b1912015-12-01 13:56:44 -0800205
Dimitry Ivanovd2a62202015-12-15 11:06:57 -0800206 uint64_t namespace_type = ANDROID_NAMESPACE_TYPE_ISOLATED;
207 if (is_shared) {
208 namespace_type |= ANDROID_NAMESPACE_TYPE_SHARED;
209 }
210
Dimitry Ivanov9e253ce2017-05-08 22:24:24 -0700211 if (target_sdk_version < 24) {
212 namespace_type |= ANDROID_NAMESPACE_TYPE_GREYLIST_ENABLED;
213 }
214
Victor Khimenko1443ec42018-07-09 17:01:22 +0200215 NativeLoaderNamespace* parent_ns = FindParentNamespaceByClassLoader(env, class_loader);
Dimitry Ivanov24db75c2016-05-12 15:34:41 -0700216
Zhenhua WANGf2804e52016-05-30 11:16:08 +0800217 bool is_native_bridge = false;
Dimitry Ivanovac1b1912015-12-01 13:56:44 -0800218
Victor Khimenko1443ec42018-07-09 17:01:22 +0200219 if (parent_ns != nullptr) {
220 is_native_bridge = !parent_ns->is_android_namespace();
Zhenhua WANGf2804e52016-05-30 11:16:08 +0800221 } else if (!library_path.empty()) {
222 is_native_bridge = NativeBridgeIsPathSupported(library_path.c_str());
Dimitry Ivanovd047c922016-02-23 14:23:51 -0800223 }
Dimitry Ivanovac1b1912015-12-01 13:56:44 -0800224
Jiyong Parka07f3052017-08-22 10:26:10 +0900225 std::string system_exposed_libraries = system_public_libraries_;
226 const char* namespace_name = kClassloaderNamespaceName;
227 android_namespace_t* vndk_ns = nullptr;
228 if (is_for_vendor && !is_shared) {
229 LOG_FATAL_IF(is_native_bridge, "Unbundled vendor apk must not use translated architecture");
230
231 // For vendor apks, give access to the vendor lib even though
232 // they are treated as unbundled; the libs and apks are still bundled
233 // together in the vendor partition.
234#if defined(__LP64__)
235 std::string vendor_lib_path = "/vendor/lib64";
236#else
237 std::string vendor_lib_path = "/vendor/lib";
238#endif
239 library_path = library_path + ":" + vendor_lib_path.c_str();
240 permitted_path = permitted_path + ":" + vendor_lib_path.c_str();
241
242 // Also give access to LLNDK libraries since they are available to vendors
243 system_exposed_libraries = system_exposed_libraries + ":" + system_llndk_libraries_.c_str();
244
245 // Give access to VNDK-SP libraries from the 'vndk' namespace.
246 vndk_ns = android_get_exported_namespace(kVndkNamespaceName);
247 LOG_ALWAYS_FATAL_IF(vndk_ns == nullptr,
248 "Cannot find \"%s\" namespace for vendor apks", kVndkNamespaceName);
249
250 // Different name is useful for debugging
251 namespace_name = kVendorClassloaderNamespaceName;
252 ALOGD("classloader namespace configured for unbundled vendor apk. library_path=%s", library_path.c_str());
Inseob Kim67cb0562018-05-04 11:39:12 +0900253 } else {
254 // oem and product public libraries are NOT available to vendor apks, otherwise it
Jiyong Parke0319942018-01-15 21:14:53 +0900255 // would be system->vendor violation.
Inseob Kim67cb0562018-05-04 11:39:12 +0900256 if (!oem_public_libraries_.empty()) {
257 system_exposed_libraries = system_exposed_libraries + ':' + oem_public_libraries_;
258 }
259 if (!product_public_libraries_.empty()) {
260 system_exposed_libraries = system_exposed_libraries + ':' + product_public_libraries_;
261 }
Jiyong Parka07f3052017-08-22 10:26:10 +0900262 }
263
Victor Chang7a20a902019-01-28 18:43:24 +0000264 std::string runtime_exposed_libraries = base::Join(kRuntimePublicLibraries, ":");
265
Zhenhua WANGf2804e52016-05-30 11:16:08 +0800266 NativeLoaderNamespace native_loader_ns;
267 if (!is_native_bridge) {
Victor Khimenko1443ec42018-07-09 17:01:22 +0200268 android_namespace_t* android_parent_ns =
269 parent_ns == nullptr ? nullptr : parent_ns->get_android_ns();
Jiyong Parka07f3052017-08-22 10:26:10 +0900270 android_namespace_t* ns = android_create_namespace(namespace_name,
Zhenhua WANGf2804e52016-05-30 11:16:08 +0800271 nullptr,
272 library_path.c_str(),
273 namespace_type,
274 permitted_path.c_str(),
Victor Khimenko1443ec42018-07-09 17:01:22 +0200275 android_parent_ns);
Zhenhua WANGf2804e52016-05-30 11:16:08 +0800276 if (ns == nullptr) {
277 *error_msg = dlerror();
Victor Khimenko1443ec42018-07-09 17:01:22 +0200278 return nullptr;
Zhenhua WANGf2804e52016-05-30 11:16:08 +0800279 }
280
Dimitry Ivanov882cad22017-05-01 15:12:49 -0700281 // Note that when vendor_ns is not configured this function will return nullptr
282 // and it will result in linking vendor_public_libraries_ to the default namespace
283 // which is expected behavior in this case.
284 android_namespace_t* vendor_ns = android_get_exported_namespace(kVendorNamespaceName);
285
Victor Chang7a20a902019-01-28 18:43:24 +0000286 android_namespace_t* runtime_ns = android_get_exported_namespace(kRuntimeNamespaceName);
287
Jiyong Parka07f3052017-08-22 10:26:10 +0900288 if (!android_link_namespaces(ns, nullptr, system_exposed_libraries.c_str())) {
Dimitry Ivanov26e1a842017-02-03 14:11:27 -0800289 *error_msg = dlerror();
Victor Khimenko1443ec42018-07-09 17:01:22 +0200290 return nullptr;
Dimitry Ivanov26e1a842017-02-03 14:11:27 -0800291 }
292
Victor Chang7a20a902019-01-28 18:43:24 +0000293 // Runtime apex does not exist in host, and under certain build conditions.
294 if (runtime_ns != nullptr) {
295 if (!android_link_namespaces(ns, runtime_ns, runtime_exposed_libraries.c_str())) {
296 *error_msg = dlerror();
297 return nullptr;
298 }
299 }
300
Jiyong Parka07f3052017-08-22 10:26:10 +0900301 if (vndk_ns != nullptr && !system_vndksp_libraries_.empty()) {
302 // vendor apks are allowed to use VNDK-SP libraries.
303 if (!android_link_namespaces(ns, vndk_ns, system_vndksp_libraries_.c_str())) {
304 *error_msg = dlerror();
Victor Khimenko1443ec42018-07-09 17:01:22 +0200305 return nullptr;
Jiyong Parka07f3052017-08-22 10:26:10 +0900306 }
307 }
308
Dimitry Ivanov882cad22017-05-01 15:12:49 -0700309 if (!vendor_public_libraries_.empty()) {
310 if (!android_link_namespaces(ns, vendor_ns, vendor_public_libraries_.c_str())) {
311 *error_msg = dlerror();
Victor Khimenko1443ec42018-07-09 17:01:22 +0200312 return nullptr;
Dimitry Ivanov882cad22017-05-01 15:12:49 -0700313 }
314 }
315
Zhenhua WANGf2804e52016-05-30 11:16:08 +0800316 native_loader_ns = NativeLoaderNamespace(ns);
317 } else {
Victor Khimenko1443ec42018-07-09 17:01:22 +0200318 native_bridge_namespace_t* native_bridge_parent_namespace =
319 parent_ns == nullptr ? nullptr : parent_ns->get_native_bridge_ns();
Jiyong Parka07f3052017-08-22 10:26:10 +0900320 native_bridge_namespace_t* ns = NativeBridgeCreateNamespace(namespace_name,
Zhenhua WANGf2804e52016-05-30 11:16:08 +0800321 nullptr,
322 library_path.c_str(),
323 namespace_type,
324 permitted_path.c_str(),
Victor Khimenko1443ec42018-07-09 17:01:22 +0200325 native_bridge_parent_namespace);
Zhenhua WANGf2804e52016-05-30 11:16:08 +0800326 if (ns == nullptr) {
327 *error_msg = NativeBridgeGetError();
Victor Khimenko1443ec42018-07-09 17:01:22 +0200328 return nullptr;
Zhenhua WANGf2804e52016-05-30 11:16:08 +0800329 }
330
dimitryd2ace382019-02-04 15:06:43 +0100331 native_bridge_namespace_t* vendor_ns = NativeBridgeGetExportedNamespace(kVendorNamespaceName);
Victor Chang7a20a902019-01-28 18:43:24 +0000332 native_bridge_namespace_t* runtime_ns =
333 NativeBridgeGetExportedNamespace(kRuntimeNamespaceName);
Dimitry Ivanov882cad22017-05-01 15:12:49 -0700334
Jiyong Parka07f3052017-08-22 10:26:10 +0900335 if (!NativeBridgeLinkNamespaces(ns, nullptr, system_exposed_libraries.c_str())) {
Zhenhua WANGe8fb11d2017-02-27 10:14:45 +0800336 *error_msg = NativeBridgeGetError();
Victor Khimenko1443ec42018-07-09 17:01:22 +0200337 return nullptr;
Zhenhua WANGe8fb11d2017-02-27 10:14:45 +0800338 }
339
Victor Chang7a20a902019-01-28 18:43:24 +0000340 // Runtime apex does not exist in host, and under certain build conditions.
341 if (runtime_ns != nullptr) {
342 if (!NativeBridgeLinkNamespaces(ns, runtime_ns, runtime_exposed_libraries.c_str())) {
343 *error_msg = NativeBridgeGetError();
344 return nullptr;
345 }
346 }
Dimitry Ivanov882cad22017-05-01 15:12:49 -0700347 if (!vendor_public_libraries_.empty()) {
348 if (!NativeBridgeLinkNamespaces(ns, vendor_ns, vendor_public_libraries_.c_str())) {
349 *error_msg = NativeBridgeGetError();
Victor Khimenko1443ec42018-07-09 17:01:22 +0200350 return nullptr;
Dimitry Ivanov882cad22017-05-01 15:12:49 -0700351 }
352 }
353
Zhenhua WANGf2804e52016-05-30 11:16:08 +0800354 native_loader_ns = NativeLoaderNamespace(ns);
355 }
356
357 namespaces_.push_back(std::make_pair(env->NewWeakGlobalRef(class_loader), native_loader_ns));
358
Victor Khimenko1443ec42018-07-09 17:01:22 +0200359 return &(namespaces_.back().second);
Dimitry Ivanovac1b1912015-12-01 13:56:44 -0800360 }
361
Victor Khimenko1443ec42018-07-09 17:01:22 +0200362 NativeLoaderNamespace* FindNamespaceByClassLoader(JNIEnv* env, jobject class_loader) {
Dimitry Ivanovf44ecde2016-02-22 13:48:22 -0800363 auto it = std::find_if(namespaces_.begin(), namespaces_.end(),
Zhenhua WANGf2804e52016-05-30 11:16:08 +0800364 [&](const std::pair<jweak, NativeLoaderNamespace>& value) {
Dimitry Ivanovf44ecde2016-02-22 13:48:22 -0800365 return env->IsSameObject(value.first, class_loader);
366 });
Zhenhua WANGf2804e52016-05-30 11:16:08 +0800367 if (it != namespaces_.end()) {
Victor Khimenko1443ec42018-07-09 17:01:22 +0200368 return &it->second;
Zhenhua WANGf2804e52016-05-30 11:16:08 +0800369 }
370
Victor Khimenko1443ec42018-07-09 17:01:22 +0200371 return nullptr;
Dimitry Ivanovf44ecde2016-02-22 13:48:22 -0800372 }
373
Dimitry Ivanov4b0e9632016-03-15 13:51:26 -0700374 void Initialize() {
Dimitry Ivanov80ddb8f2016-05-09 18:12:00 -0700375 // Once public namespace is initialized there is no
376 // point in running this code - it will have no effect
377 // on the current list of public libraries.
378 if (initialized_) {
379 return;
380 }
381
Dimitry Ivanovb6140452016-04-06 18:24:08 -0700382 std::vector<std::string> sonames;
Dimitry Ivanov0b5651e2016-04-21 16:42:48 -0700383 const char* android_root_env = getenv("ANDROID_ROOT");
384 std::string root_dir = android_root_env != nullptr ? android_root_env : "/system";
385 std::string public_native_libraries_system_config =
386 root_dir + kPublicNativeLibrariesSystemConfigPathFromRoot;
Jiyong Parka07f3052017-08-22 10:26:10 +0900387 std::string llndk_native_libraries_system_config =
388 root_dir + kLlndkNativeLibrariesSystemConfigPathFromRoot;
389 std::string vndksp_native_libraries_system_config =
390 root_dir + kVndkspNativeLibrariesSystemConfigPathFromRoot;
Dimitry Ivanovb6140452016-04-06 18:24:08 -0700391
Inseob Kim67cb0562018-05-04 11:39:12 +0900392 std::string product_public_native_libraries_dir = "/product/etc";
393
Christopher Ferris39da84b2016-06-21 16:11:23 -0700394 std::string error_msg;
Jiyong Parkd1006fe2017-11-08 18:44:09 +0900395 LOG_ALWAYS_FATAL_IF(
396 !ReadConfig(public_native_libraries_system_config, &sonames, always_true, &error_msg),
397 "Error reading public native library list from \"%s\": %s",
398 public_native_libraries_system_config.c_str(), error_msg.c_str());
399
Jiyong Parke0319942018-01-15 21:14:53 +0900400 // For debuggable platform builds use ANDROID_ADDITIONAL_PUBLIC_LIBRARIES environment
401 // variable to add libraries to the list. This is intended for platform tests only.
402 if (is_debuggable()) {
403 const char* additional_libs = getenv("ANDROID_ADDITIONAL_PUBLIC_LIBRARIES");
404 if (additional_libs != nullptr && additional_libs[0] != '\0') {
405 std::vector<std::string> additional_libs_vector = base::Split(additional_libs, ":");
406 std::copy(additional_libs_vector.begin(), additional_libs_vector.end(),
407 std::back_inserter(sonames));
408 }
409 }
410
411 // android_init_namespaces() expects all the public libraries
412 // to be loaded so that they can be found by soname alone.
413 //
414 // TODO(dimitry): this is a bit misleading since we do not know
415 // if the vendor public library is going to be opened from /vendor/lib
Inseob Kim67cb0562018-05-04 11:39:12 +0900416 // we might as well end up loading them from /system/lib or /product/lib
Jiyong Parke0319942018-01-15 21:14:53 +0900417 // For now we rely on CTS test to catch things like this but
418 // it should probably be addressed in the future.
419 for (const auto& soname : sonames) {
420 LOG_ALWAYS_FATAL_IF(dlopen(soname.c_str(), RTLD_NOW | RTLD_NODELETE) == nullptr,
421 "Error preloading public library %s: %s", soname.c_str(), dlerror());
422 }
423
424 system_public_libraries_ = base::Join(sonames, ':');
425
Jiyong Parkd1006fe2017-11-08 18:44:09 +0900426 // read /system/etc/public.libraries-<companyname>.txt which contain partner defined
427 // system libs that are exposed to apps. The libs in the txt files must be
428 // named as lib<name>.<companyname>.so.
Jiyong Parke0319942018-01-15 21:14:53 +0900429 sonames.clear();
Inseob Kim67cb0562018-05-04 11:39:12 +0900430 ReadExtensionLibraries(base::Dirname(public_native_libraries_system_config).c_str(), &sonames);
Jiyong Parke0319942018-01-15 21:14:53 +0900431 oem_public_libraries_ = base::Join(sonames, ':');
Dimitry Ivanov7d028292016-05-05 17:30:24 -0700432
Inseob Kim67cb0562018-05-04 11:39:12 +0900433 // read /product/etc/public.libraries-<companyname>.txt which contain partner defined
434 // product libs that are exposed to apps.
435 sonames.clear();
436 ReadExtensionLibraries(product_public_native_libraries_dir.c_str(), &sonames);
437 product_public_libraries_ = base::Join(sonames, ':');
438
Justin Yun4a1d1102017-11-27 17:04:14 +0900439 // Insert VNDK version to llndk and vndksp config file names.
440 insert_vndk_version_str(&llndk_native_libraries_system_config);
441 insert_vndk_version_str(&vndksp_native_libraries_system_config);
442
Dimitry Ivanov882cad22017-05-01 15:12:49 -0700443 sonames.clear();
Jiyong Parkd1006fe2017-11-08 18:44:09 +0900444 ReadConfig(llndk_native_libraries_system_config, &sonames, always_true);
Jiyong Parka07f3052017-08-22 10:26:10 +0900445 system_llndk_libraries_ = base::Join(sonames, ':');
446
447 sonames.clear();
Jiyong Parkd1006fe2017-11-08 18:44:09 +0900448 ReadConfig(vndksp_native_libraries_system_config, &sonames, always_true);
Jiyong Parka07f3052017-08-22 10:26:10 +0900449 system_vndksp_libraries_ = base::Join(sonames, ':');
450
451 sonames.clear();
Dimitry Ivanov882cad22017-05-01 15:12:49 -0700452 // This file is optional, quietly ignore if the file does not exist.
Jiyong Parkd1006fe2017-11-08 18:44:09 +0900453 ReadConfig(kPublicNativeLibrariesVendorConfig, &sonames, always_true, nullptr);
Dimitry Ivanov882cad22017-05-01 15:12:49 -0700454
455 vendor_public_libraries_ = base::Join(sonames, ':');
Dimitry Ivanovb6140452016-04-06 18:24:08 -0700456 }
457
Inseob Kim67cb0562018-05-04 11:39:12 +0900458 void Reset() { namespaces_.clear(); }
Dimitry Ivanovbe4ca3a2016-05-02 10:43:16 -0700459
Dimitry Ivanovb6140452016-04-06 18:24:08 -0700460 private:
Inseob Kim67cb0562018-05-04 11:39:12 +0900461 void ReadExtensionLibraries(const char* dirname, std::vector<std::string>* sonames) {
462 std::unique_ptr<DIR, decltype(&closedir)> dir(opendir(dirname), closedir);
463 if (dir != nullptr) {
464 // Failing to opening the dir is not an error, which can happen in
465 // webview_zygote.
466 while (struct dirent* ent = readdir(dir.get())) {
467 if (ent->d_type != DT_REG && ent->d_type != DT_LNK) {
468 continue;
469 }
470 const std::string filename(ent->d_name);
471 if (android::base::StartsWith(filename, kPublicNativeLibrariesExtensionConfigPrefix) &&
472 android::base::EndsWith(filename, kPublicNativeLibrariesExtensionConfigSuffix)) {
473 const size_t start = kPublicNativeLibrariesExtensionConfigPrefixLen;
474 const size_t end = filename.size() - kPublicNativeLibrariesExtensionConfigSuffixLen;
475 const std::string company_name = filename.substr(start, end - start);
476 const std::string config_file_path = dirname + "/"s + filename;
477 LOG_ALWAYS_FATAL_IF(
478 company_name.empty(),
479 "Error extracting company name from public native library list file path \"%s\"",
480 config_file_path.c_str());
481
482 std::string error_msg;
483
484 LOG_ALWAYS_FATAL_IF(
485 !ReadConfig(
486 config_file_path, sonames,
487 [&company_name](const std::string& soname, std::string* error_msg) {
488 if (android::base::StartsWith(soname, "lib") &&
489 android::base::EndsWith(soname, "." + company_name + ".so")) {
490 return true;
491 } else {
492 *error_msg = "Library name \"" + soname +
493 "\" does not end with the company name: " + company_name + ".";
494 return false;
495 }
496 },
497 &error_msg),
498 "Error reading public native library list from \"%s\": %s", config_file_path.c_str(),
499 error_msg.c_str());
500 }
501 }
502 }
503 }
504
505
Christopher Ferris39da84b2016-06-21 16:11:23 -0700506 bool ReadConfig(const std::string& configFile, std::vector<std::string>* sonames,
Jiyong Parkd1006fe2017-11-08 18:44:09 +0900507 const std::function<bool(const std::string& /* soname */,
508 std::string* /* error_msg */)>& check_soname,
Christopher Ferris39da84b2016-06-21 16:11:23 -0700509 std::string* error_msg = nullptr) {
Dimitry Ivanov4b0e9632016-03-15 13:51:26 -0700510 // Read list of public native libraries from the config file.
511 std::string file_content;
Dimitry Ivanovb6140452016-04-06 18:24:08 -0700512 if(!base::ReadFileToString(configFile, &file_content)) {
Christopher Ferris39da84b2016-06-21 16:11:23 -0700513 if (error_msg) *error_msg = strerror(errno);
Dimitry Ivanovb6140452016-04-06 18:24:08 -0700514 return false;
515 }
Dimitry Ivanov4b0e9632016-03-15 13:51:26 -0700516
517 std::vector<std::string> lines = base::Split(file_content, "\n");
518
Christopher Ferris39da84b2016-06-21 16:11:23 -0700519 for (auto& line : lines) {
Dimitry Ivanov4b0e9632016-03-15 13:51:26 -0700520 auto trimmed_line = base::Trim(line);
521 if (trimmed_line[0] == '#' || trimmed_line.empty()) {
522 continue;
523 }
Christopher Ferris39da84b2016-06-21 16:11:23 -0700524 size_t space_pos = trimmed_line.rfind(' ');
525 if (space_pos != std::string::npos) {
526 std::string type = trimmed_line.substr(space_pos + 1);
527 if (type != "32" && type != "64") {
528 if (error_msg) *error_msg = "Malformed line: " + line;
529 return false;
530 }
531#if defined(__LP64__)
532 // Skip 32 bit public library.
533 if (type == "32") {
534 continue;
535 }
536#else
537 // Skip 64 bit public library.
538 if (type == "64") {
539 continue;
540 }
541#endif
542 trimmed_line.resize(space_pos);
543 }
Dimitry Ivanov4b0e9632016-03-15 13:51:26 -0700544
Jiyong Parkd1006fe2017-11-08 18:44:09 +0900545 if (check_soname(trimmed_line, error_msg)) {
546 sonames->push_back(trimmed_line);
547 } else {
548 return false;
549 }
Dimitry Ivanov4b0e9632016-03-15 13:51:26 -0700550 }
551
Dimitry Ivanovb6140452016-04-06 18:24:08 -0700552 return true;
Dimitry Ivanovd68c8e92016-02-10 14:09:22 -0800553 }
Dimitry Ivanovac1b1912015-12-01 13:56:44 -0800554
Zhenhua WANGf2804e52016-05-30 11:16:08 +0800555 bool InitPublicNamespace(const char* library_path, std::string* error_msg) {
556 // Ask native bride if this apps library path should be handled by it
557 bool is_native_bridge = NativeBridgeIsPathSupported(library_path);
558
Dimitry Ivanov4b0e9632016-03-15 13:51:26 -0700559 // (http://b/25844435) - Some apps call dlopen from generated code (mono jited
560 // code is one example) unknown to linker in which case linker uses anonymous
561 // namespace. The second argument specifies the search path for the anonymous
562 // namespace which is the library_path of the classloader.
Dimitry Ivanov882cad22017-05-01 15:12:49 -0700563 initialized_ = android_init_anonymous_namespace(system_public_libraries_.c_str(),
Dimitry Ivanov26e1a842017-02-03 14:11:27 -0800564 is_native_bridge ? nullptr : library_path);
Dimitry Ivanovd836ab02016-11-02 18:03:10 -0700565 if (!initialized_) {
566 *error_msg = dlerror();
567 return false;
568 }
569
570 // and now initialize native bridge namespaces if necessary.
571 if (NativeBridgeInitialized()) {
Dimitry Ivanov882cad22017-05-01 15:12:49 -0700572 initialized_ = NativeBridgeInitAnonymousNamespace(system_public_libraries_.c_str(),
Zhenhua WANGe8fb11d2017-02-27 10:14:45 +0800573 is_native_bridge ? library_path : nullptr);
Zhenhua WANGf2804e52016-05-30 11:16:08 +0800574 if (!initialized_) {
575 *error_msg = NativeBridgeGetError();
576 }
577 }
Dimitry Ivanovac1b1912015-12-01 13:56:44 -0800578
579 return initialized_;
580 }
581
Dimitry Ivanov24db75c2016-05-12 15:34:41 -0700582 jobject GetParentClassLoader(JNIEnv* env, jobject class_loader) {
583 jclass class_loader_class = env->FindClass("java/lang/ClassLoader");
584 jmethodID get_parent = env->GetMethodID(class_loader_class,
585 "getParent",
586 "()Ljava/lang/ClassLoader;");
587
588 return env->CallObjectMethod(class_loader, get_parent);
589 }
590
Victor Khimenko1443ec42018-07-09 17:01:22 +0200591 NativeLoaderNamespace* FindParentNamespaceByClassLoader(JNIEnv* env, jobject class_loader) {
Dimitry Ivanov24db75c2016-05-12 15:34:41 -0700592 jobject parent_class_loader = GetParentClassLoader(env, class_loader);
593
594 while (parent_class_loader != nullptr) {
Victor Khimenko1443ec42018-07-09 17:01:22 +0200595 NativeLoaderNamespace* ns;
596 if ((ns = FindNamespaceByClassLoader(env, parent_class_loader)) != nullptr) {
597 return ns;
Dimitry Ivanov24db75c2016-05-12 15:34:41 -0700598 }
599
600 parent_class_loader = GetParentClassLoader(env, parent_class_loader);
601 }
Zhenhua WANGf2804e52016-05-30 11:16:08 +0800602
Victor Khimenko1443ec42018-07-09 17:01:22 +0200603 return nullptr;
Dimitry Ivanov24db75c2016-05-12 15:34:41 -0700604 }
605
Dimitry Ivanovac1b1912015-12-01 13:56:44 -0800606 bool initialized_;
Victor Khimenko1443ec42018-07-09 17:01:22 +0200607 std::list<std::pair<jweak, NativeLoaderNamespace>> namespaces_;
Dimitry Ivanov882cad22017-05-01 15:12:49 -0700608 std::string system_public_libraries_;
609 std::string vendor_public_libraries_;
Jiyong Parke0319942018-01-15 21:14:53 +0900610 std::string oem_public_libraries_;
Inseob Kim67cb0562018-05-04 11:39:12 +0900611 std::string product_public_libraries_;
Jiyong Parka07f3052017-08-22 10:26:10 +0900612 std::string system_llndk_libraries_;
613 std::string system_vndksp_libraries_;
Dimitry Ivanovac1b1912015-12-01 13:56:44 -0800614
615 DISALLOW_COPY_AND_ASSIGN(LibraryNamespaces);
616};
617
Dimitry Ivanov34d5a202016-02-29 13:21:43 -0800618static std::mutex g_namespaces_mutex;
Dimitry Ivanovac1b1912015-12-01 13:56:44 -0800619static LibraryNamespaces* g_namespaces = new LibraryNamespaces;
620#endif
621
Dimitry Ivanov4b0e9632016-03-15 13:51:26 -0700622void InitializeNativeLoader() {
Dimitry Ivanov426799d2016-02-22 11:27:48 -0800623#if defined(__ANDROID__)
Dimitry Ivanov34d5a202016-02-29 13:21:43 -0800624 std::lock_guard<std::mutex> guard(g_namespaces_mutex);
Dimitry Ivanov4b0e9632016-03-15 13:51:26 -0700625 g_namespaces->Initialize();
Dimitry Ivanov426799d2016-02-22 11:27:48 -0800626#endif
627}
628
Dimitry Ivanovbe4ca3a2016-05-02 10:43:16 -0700629void ResetNativeLoader() {
630#if defined(__ANDROID__)
631 std::lock_guard<std::mutex> guard(g_namespaces_mutex);
632 g_namespaces->Reset();
633#endif
634}
Dimitry Ivanovac1b1912015-12-01 13:56:44 -0800635
Dimitry Ivanovd047c922016-02-23 14:23:51 -0800636jstring CreateClassLoaderNamespace(JNIEnv* env,
637 int32_t target_sdk_version,
638 jobject class_loader,
639 bool is_shared,
Jiyong Parka07f3052017-08-22 10:26:10 +0900640 bool is_for_vendor,
Dimitry Ivanovd047c922016-02-23 14:23:51 -0800641 jstring library_path,
642 jstring permitted_path) {
Dimitry Ivanovac1b1912015-12-01 13:56:44 -0800643#if defined(__ANDROID__)
Dimitry Ivanov34d5a202016-02-29 13:21:43 -0800644 std::lock_guard<std::mutex> guard(g_namespaces_mutex);
Zhenhua WANGf2804e52016-05-30 11:16:08 +0800645
646 std::string error_msg;
Zhenhua WANGf2804e52016-05-30 11:16:08 +0800647 bool success = g_namespaces->Create(env,
Dimitry Ivanov9e253ce2017-05-08 22:24:24 -0700648 target_sdk_version,
Zhenhua WANGf2804e52016-05-30 11:16:08 +0800649 class_loader,
650 is_shared,
Jiyong Parka07f3052017-08-22 10:26:10 +0900651 is_for_vendor,
Zhenhua WANGf2804e52016-05-30 11:16:08 +0800652 library_path,
653 permitted_path,
Victor Khimenko1443ec42018-07-09 17:01:22 +0200654 &error_msg) != nullptr;
Zhenhua WANGf2804e52016-05-30 11:16:08 +0800655 if (!success) {
656 return env->NewStringUTF(error_msg.c_str());
Dimitry Ivanovd047c922016-02-23 14:23:51 -0800657 }
658#else
Jiyong Parka07f3052017-08-22 10:26:10 +0900659 UNUSED(env, target_sdk_version, class_loader, is_shared, is_for_vendor,
Dimitry Ivanovd047c922016-02-23 14:23:51 -0800660 library_path, permitted_path);
661#endif
662 return nullptr;
663}
664
Nicolas Geoffray890e3bf2019-01-22 09:11:57 +0000665#if defined(__ANDROID__)
666static android_namespace_t* FindExportedNamespace(const char* caller_location) {
667 std::string location = caller_location;
668 // Lots of implicit assumptions here: we expect `caller_location` to be of the form:
669 // /apex/com.android...modulename/...
670 //
671 // And we extract from it 'modulename', which is the name of the linker namespace.
672 if (android::base::StartsWith(location, kApexPath)) {
673 size_t slash_index = location.find_first_of('/', strlen(kApexPath));
674 LOG_ALWAYS_FATAL_IF((slash_index == std::string::npos),
675 "Error finding namespace of apex: no slash in path %s", caller_location);
676 size_t dot_index = location.find_last_of('.', slash_index);
677 LOG_ALWAYS_FATAL_IF((dot_index == std::string::npos),
678 "Error finding namespace of apex: no dot in apex name %s", caller_location);
679 std::string name = location.substr(dot_index + 1, slash_index - dot_index - 1);
680 android_namespace_t* boot_namespace = android_get_exported_namespace(name.c_str());
681 LOG_ALWAYS_FATAL_IF((boot_namespace == nullptr),
682 "Error finding namespace of apex: no namespace called %s", name.c_str());
683 return boot_namespace;
684 }
685 return nullptr;
686}
687#endif
688
Nicolas Geoffrayc3a73dc2019-01-12 15:01:20 +0000689void* OpenNativeLibrary(JNIEnv* env, int32_t target_sdk_version, const char* path,
Nicolas Geoffray53535022019-01-18 10:05:13 +0000690 jobject class_loader, const char* caller_location, jstring library_path,
691 bool* needs_native_bridge, char** error_msg) {
Dimitry Ivanovd047c922016-02-23 14:23:51 -0800692#if defined(__ANDROID__)
Dimitry Ivanov5539db02016-04-20 16:07:30 -0700693 UNUSED(target_sdk_version);
694 if (class_loader == nullptr) {
Zhenhua WANGf2804e52016-05-30 11:16:08 +0800695 *needs_native_bridge = false;
Nicolas Geoffray890e3bf2019-01-22 09:11:57 +0000696 if (caller_location != nullptr) {
697 android_namespace_t* boot_namespace = FindExportedNamespace(caller_location);
698 if (boot_namespace != nullptr) {
699 const android_dlextinfo dlextinfo = {
700 .flags = ANDROID_DLEXT_USE_NAMESPACE,
701 .library_namespace = boot_namespace,
702 };
703 void* handle = android_dlopen_ext(path, RTLD_NOW, &dlextinfo);
704 if (handle == nullptr) {
705 *error_msg = strdup(dlerror());
706 }
707 return handle;
708 }
709 }
Pete Bentley632f1422018-12-19 13:33:33 +0000710 void* handle = dlopen(path, RTLD_NOW);
711 if (handle == nullptr) {
Nicolas Geoffrayd06cb942019-01-16 20:20:27 +0000712 *error_msg = strdup(dlerror());
Pete Bentley632f1422018-12-19 13:33:33 +0000713 }
714 return handle;
Dimitry Ivanovac1b1912015-12-01 13:56:44 -0800715 }
716
Dimitry Ivanov34d5a202016-02-29 13:21:43 -0800717 std::lock_guard<std::mutex> guard(g_namespaces_mutex);
Victor Khimenko1443ec42018-07-09 17:01:22 +0200718 NativeLoaderNamespace* ns;
Dimitry Ivanovac1b1912015-12-01 13:56:44 -0800719
Victor Khimenko1443ec42018-07-09 17:01:22 +0200720 if ((ns = g_namespaces->FindNamespaceByClassLoader(env, class_loader)) == nullptr) {
Dimitry Ivanovd047c922016-02-23 14:23:51 -0800721 // This is the case where the classloader was not created by ApplicationLoaders
722 // In this case we create an isolated not-shared namespace for it.
Nicolas Geoffrayc3a73dc2019-01-12 15:01:20 +0000723 std::string create_error_msg;
724 if ((ns = g_namespaces->Create(env, target_sdk_version, class_loader, false /* is_shared */,
725 false /* is_for_vendor */, library_path, nullptr,
726 &create_error_msg)) == nullptr) {
727 *error_msg = strdup(create_error_msg.c_str());
Dimitry Ivanovd047c922016-02-23 14:23:51 -0800728 return nullptr;
729 }
Dimitry Ivanovac1b1912015-12-01 13:56:44 -0800730 }
731
Nicolas Geoffrayc3a73dc2019-01-12 15:01:20 +0000732 return OpenNativeLibraryInNamespace(ns, path, needs_native_bridge, error_msg);
Dimitry Ivanovac1b1912015-12-01 13:56:44 -0800733#else
Nicolas Geoffray53535022019-01-18 10:05:13 +0000734 UNUSED(env, target_sdk_version, class_loader, caller_location);
Andreas Gampe5c7d5822017-12-28 19:08:13 -0800735
736 // Do some best effort to emulate library-path support. It will not
737 // work for dependencies.
738 //
739 // Note: null has a special meaning and must be preserved.
740 std::string c_library_path; // Empty string by default.
741 if (library_path != nullptr && path != nullptr && path[0] != '/') {
742 ScopedUtfChars library_path_utf_chars(env, library_path);
743 c_library_path = library_path_utf_chars.c_str();
744 }
745
746 std::vector<std::string> library_paths = base::Split(c_library_path, ":");
747
748 for (const std::string& lib_path : library_paths) {
749 *needs_native_bridge = false;
750 const char* path_arg;
751 std::string complete_path;
752 if (path == nullptr) {
753 // Preserve null.
754 path_arg = nullptr;
Zhenhua WANGf2804e52016-05-30 11:16:08 +0800755 } else {
Andreas Gampe5c7d5822017-12-28 19:08:13 -0800756 complete_path = lib_path;
757 if (!complete_path.empty()) {
758 complete_path.append("/");
759 }
760 complete_path.append(path);
761 path_arg = complete_path.c_str();
762 }
763 void* handle = dlopen(path_arg, RTLD_NOW);
764 if (handle != nullptr) {
765 return handle;
766 }
767 if (NativeBridgeIsSupported(path_arg)) {
768 *needs_native_bridge = true;
769 handle = NativeBridgeLoadLibrary(path_arg, RTLD_NOW);
770 if (handle != nullptr) {
771 return handle;
772 }
Nicolas Geoffrayc3a73dc2019-01-12 15:01:20 +0000773 *error_msg = strdup(NativeBridgeGetError());
Andreas Gampe5c7d5822017-12-28 19:08:13 -0800774 } else {
Nicolas Geoffrayc3a73dc2019-01-12 15:01:20 +0000775 *error_msg = strdup(dlerror());
Zhenhua WANGf2804e52016-05-30 11:16:08 +0800776 }
777 }
Andreas Gampe5c7d5822017-12-28 19:08:13 -0800778 return nullptr;
Dimitry Ivanovac1b1912015-12-01 13:56:44 -0800779#endif
780}
781
Nicolas Geoffrayc3a73dc2019-01-12 15:01:20 +0000782bool CloseNativeLibrary(void* handle, const bool needs_native_bridge, char** error_msg) {
dimitry3150f7c2018-09-12 01:09:19 +0200783 bool success;
784 if (needs_native_bridge) {
785 success = (NativeBridgeUnloadLibrary(handle) == 0);
786 if (!success) {
Nicolas Geoffrayc3a73dc2019-01-12 15:01:20 +0000787 *error_msg = strdup(NativeBridgeGetError());
dimitry3150f7c2018-09-12 01:09:19 +0200788 }
789 } else {
790 success = (dlclose(handle) == 0);
791 if (!success) {
Nicolas Geoffrayc3a73dc2019-01-12 15:01:20 +0000792 *error_msg = strdup(dlerror());
dimitry3150f7c2018-09-12 01:09:19 +0200793 }
794 }
795
796 return success;
Dimitry Ivanov09a516b2016-05-03 14:55:25 -0700797}
798
Nicolas Geoffrayc3a73dc2019-01-12 15:01:20 +0000799void NativeLoaderFreeErrorMessage(char* msg) {
800 // The error messages get allocated through strdup, so we must call free on them.
801 free(msg);
802}
803
Dimitry Ivanovf44ecde2016-02-22 13:48:22 -0800804#if defined(__ANDROID__)
Nicolas Geoffrayc3a73dc2019-01-12 15:01:20 +0000805void* OpenNativeLibraryInNamespace(NativeLoaderNamespace* ns, const char* path,
806 bool* needs_native_bridge, char** error_msg) {
Victor Khimenko1443ec42018-07-09 17:01:22 +0200807 if (ns->is_android_namespace()) {
808 android_dlextinfo extinfo;
809 extinfo.flags = ANDROID_DLEXT_USE_NAMESPACE;
810 extinfo.library_namespace = ns->get_android_ns();
811
812 void* handle = android_dlopen_ext(path, RTLD_NOW, &extinfo);
813 if (handle == nullptr) {
Nicolas Geoffrayc3a73dc2019-01-12 15:01:20 +0000814 *error_msg = strdup(dlerror());
Victor Khimenko1443ec42018-07-09 17:01:22 +0200815 }
816 *needs_native_bridge = false;
817 return handle;
818 } else {
819 void* handle = NativeBridgeLoadLibraryExt(path, RTLD_NOW, ns->get_native_bridge_ns());
820 if (handle == nullptr) {
Nicolas Geoffrayc3a73dc2019-01-12 15:01:20 +0000821 *error_msg = strdup(NativeBridgeGetError());
Victor Khimenko1443ec42018-07-09 17:01:22 +0200822 }
823 *needs_native_bridge = true;
824 return handle;
825 }
826}
827
Dimitry Ivanov800083d2016-11-01 14:17:00 -0700828// native_bridge_namespaces are not supported for callers of this function.
829// This function will return nullptr in the case when application is running
830// on native bridge.
Dimitry Ivanovf44ecde2016-02-22 13:48:22 -0800831android_namespace_t* FindNamespaceByClassLoader(JNIEnv* env, jobject class_loader) {
Dimitry Ivanov34d5a202016-02-29 13:21:43 -0800832 std::lock_guard<std::mutex> guard(g_namespaces_mutex);
Victor Khimenko1443ec42018-07-09 17:01:22 +0200833 NativeLoaderNamespace* ns = g_namespaces->FindNamespaceByClassLoader(env, class_loader);
834 if (ns != nullptr) {
835 return ns->is_android_namespace() ? ns->get_android_ns() : nullptr;
Zhenhua WANGf2804e52016-05-30 11:16:08 +0800836 }
837
838 return nullptr;
Dimitry Ivanovf44ecde2016-02-22 13:48:22 -0800839}
Victor Khimenko1443ec42018-07-09 17:01:22 +0200840NativeLoaderNamespace* FindNativeLoaderNamespaceByClassLoader(JNIEnv* env, jobject class_loader) {
841 std::lock_guard<std::mutex> guard(g_namespaces_mutex);
842 return g_namespaces->FindNamespaceByClassLoader(env, class_loader);
843}
Dimitry Ivanovf44ecde2016-02-22 13:48:22 -0800844#endif
845
Dimitry Ivanovac1b1912015-12-01 13:56:44 -0800846}; // android namespace