blob: 0ebb226474e153c3e37942491c1646adddf61b28 [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>
Jiyong Parkd1006fe2017-11-08 18:44:09 +090032#include <memory>
Dimitry Ivanovac1b1912015-12-01 13:56:44 -080033#include <mutex>
Jiyong Parkd1006fe2017-11-08 18:44:09 +090034#include <string>
35#include <vector>
Dimitry Ivanovac1b1912015-12-01 13:56:44 -080036
Mark Salyzynff2dcd92016-09-28 15:54:45 -070037#include <android-base/file.h>
38#include <android-base/macros.h>
39#include <android-base/strings.h>
Dimitry Ivanovac1b1912015-12-01 13:56:44 -080040
Justin Yun4a1d1102017-11-27 17:04:14 +090041#ifdef __BIONIC__
42#include <android-base/properties.h>
43#endif
44
Zhenhua WANGf2804e52016-05-30 11:16:08 +080045#define CHECK(predicate) LOG_ALWAYS_FATAL_IF(!(predicate),\
46 "%s:%d: %s CHECK '" #predicate "' failed.",\
47 __FILE__, __LINE__, __FUNCTION__)
48
Dimitry Ivanovac1b1912015-12-01 13:56:44 -080049namespace android {
50
Dimitry Ivanov4b0e9632016-03-15 13:51:26 -070051#if defined(__ANDROID__)
Zhenhua WANGf2804e52016-05-30 11:16:08 +080052class NativeLoaderNamespace {
53 public:
54 NativeLoaderNamespace()
55 : android_ns_(nullptr), native_bridge_ns_(nullptr) { }
56
57 explicit NativeLoaderNamespace(android_namespace_t* ns)
58 : android_ns_(ns), native_bridge_ns_(nullptr) { }
59
60 explicit NativeLoaderNamespace(native_bridge_namespace_t* ns)
61 : android_ns_(nullptr), native_bridge_ns_(ns) { }
62
63 NativeLoaderNamespace(NativeLoaderNamespace&& that) = default;
64 NativeLoaderNamespace(const NativeLoaderNamespace& that) = default;
65
66 NativeLoaderNamespace& operator=(const NativeLoaderNamespace& that) = default;
67
68 android_namespace_t* get_android_ns() const {
69 CHECK(native_bridge_ns_ == nullptr);
70 return android_ns_;
71 }
72
73 native_bridge_namespace_t* get_native_bridge_ns() const {
74 CHECK(android_ns_ == nullptr);
75 return native_bridge_ns_;
76 }
77
78 bool is_android_namespace() const {
79 return native_bridge_ns_ == nullptr;
80 }
81
82 private:
83 // Only one of them can be not null
84 android_namespace_t* android_ns_;
85 native_bridge_namespace_t* native_bridge_ns_;
86};
87
Jiyong Parkd1006fe2017-11-08 18:44:09 +090088static constexpr const char kPublicNativeLibrariesSystemConfigPathFromRoot[] =
89 "/etc/public.libraries.txt";
90static constexpr const char kPublicNativeLibrariesExtensionConfigPrefix[] = "public.libraries-";
91static constexpr const size_t kPublicNativeLibrariesExtensionConfigPrefixLen =
92 sizeof(kPublicNativeLibrariesExtensionConfigPrefix) - 1;
93static constexpr const char kPublicNativeLibrariesExtensionConfigSuffix[] = ".txt";
94static constexpr const size_t kPublicNativeLibrariesExtensionConfigSuffixLen =
95 sizeof(kPublicNativeLibrariesExtensionConfigSuffix) - 1;
96static constexpr const char kPublicNativeLibrariesVendorConfig[] =
97 "/vendor/etc/public.libraries.txt";
98static constexpr const char kLlndkNativeLibrariesSystemConfigPathFromRoot[] =
99 "/etc/llndk.libraries.txt";
100static constexpr const char kVndkspNativeLibrariesSystemConfigPathFromRoot[] =
101 "/etc/vndksp.libraries.txt";
Dimitry Ivanovac1b1912015-12-01 13:56:44 -0800102
Dimitry Ivanov882cad22017-05-01 15:12:49 -0700103// The device may be configured to have the vendor libraries loaded to a separate namespace.
104// For historical reasons this namespace was named sphal but effectively it is intended
105// to use to load vendor libraries to separate namespace with controlled interface between
106// vendor and system namespaces.
107static constexpr const char* kVendorNamespaceName = "sphal";
108
Jiyong Parka07f3052017-08-22 10:26:10 +0900109static constexpr const char* kVndkNamespaceName = "vndk";
110
111static constexpr const char* kClassloaderNamespaceName = "classloader-namespace";
112static constexpr const char* kVendorClassloaderNamespaceName = "vendor-classloader-namespace";
113
Dimitry Ivanovf334cbf2016-05-10 10:39:48 -0700114// (http://b/27588281) This is a workaround for apps using custom classloaders and calling
115// System.load() with an absolute path which is outside of the classloader library search path.
116// This list includes all directories app is allowed to access this way.
117static constexpr const char* kWhitelistedDirectories = "/data:/mnt/expand";
118
Dimitry Ivanov7d028292016-05-05 17:30:24 -0700119static bool is_debuggable() {
120 char debuggable[PROP_VALUE_MAX];
121 property_get("ro.debuggable", debuggable, "0");
122 return std::string(debuggable) == "1";
123}
124
Justin Yun4a1d1102017-11-27 17:04:14 +0900125static std::string vndk_version_str() {
126#ifdef __BIONIC__
127 std::string version = android::base::GetProperty("ro.vndk.version", "");
128 if (version != "" && version != "current") {
129 return "." + version;
130 }
131#endif
132 return "";
133}
134
135static void insert_vndk_version_str(std::string* file_name) {
136 CHECK(file_name != nullptr);
137 size_t insert_pos = file_name->find_last_of(".");
138 if (insert_pos == std::string::npos) {
139 insert_pos = file_name->length();
140 }
141 file_name->insert(insert_pos, vndk_version_str());
142}
143
Jiyong Parkd1006fe2017-11-08 18:44:09 +0900144static const std::function<bool(const std::string&, std::string*)> always_true =
145 [](const std::string&, std::string*) { return true; };
146
Dimitry Ivanovac1b1912015-12-01 13:56:44 -0800147class LibraryNamespaces {
148 public:
Dimitry Ivanov426799d2016-02-22 11:27:48 -0800149 LibraryNamespaces() : initialized_(false) { }
Dimitry Ivanovac1b1912015-12-01 13:56:44 -0800150
Zhenhua WANGf2804e52016-05-30 11:16:08 +0800151 bool Create(JNIEnv* env,
Dimitry Ivanov9e253ce2017-05-08 22:24:24 -0700152 uint32_t target_sdk_version,
Zhenhua WANGf2804e52016-05-30 11:16:08 +0800153 jobject class_loader,
154 bool is_shared,
Jiyong Parka07f3052017-08-22 10:26:10 +0900155 bool is_for_vendor,
Zhenhua WANGf2804e52016-05-30 11:16:08 +0800156 jstring java_library_path,
157 jstring java_permitted_path,
158 NativeLoaderNamespace* ns,
159 std::string* error_msg) {
Dimitry Ivanovcf9892b2016-05-09 10:55:50 -0700160 std::string library_path; // empty string by default.
161
162 if (java_library_path != nullptr) {
163 ScopedUtfChars library_path_utf_chars(env, java_library_path);
164 library_path = library_path_utf_chars.c_str();
165 }
Dimitry Ivanovac1b1912015-12-01 13:56:44 -0800166
Dimitry Ivanovf334cbf2016-05-10 10:39:48 -0700167 // (http://b/27588281) This is a workaround for apps using custom
168 // classloaders and calling System.load() with an absolute path which
169 // is outside of the classloader library search path.
170 //
171 // This part effectively allows such a classloader to access anything
172 // under /data and /mnt/expand
173 std::string permitted_path = kWhitelistedDirectories;
174
Dimitry Ivanov0d6e5942015-12-08 11:16:56 -0800175 if (java_permitted_path != nullptr) {
176 ScopedUtfChars path(env, java_permitted_path);
Dimitry Ivanovd0b15312016-05-10 16:21:25 -0700177 if (path.c_str() != nullptr && path.size() > 0) {
178 permitted_path = permitted_path + ":" + path.c_str();
179 }
Dimitry Ivanov0d6e5942015-12-08 11:16:56 -0800180 }
181
Zhenhua WANGf2804e52016-05-30 11:16:08 +0800182 if (!initialized_ && !InitPublicNamespace(library_path.c_str(), error_msg)) {
183 return false;
Dimitry Ivanovac1b1912015-12-01 13:56:44 -0800184 }
185
Zhenhua WANGf2804e52016-05-30 11:16:08 +0800186 bool found = FindNamespaceByClassLoader(env, class_loader, nullptr);
Dimitry Ivanovac1b1912015-12-01 13:56:44 -0800187
Zhenhua WANGf2804e52016-05-30 11:16:08 +0800188 LOG_ALWAYS_FATAL_IF(found,
Dimitry Ivanov4b0e9632016-03-15 13:51:26 -0700189 "There is already a namespace associated with this classloader");
Dimitry Ivanovac1b1912015-12-01 13:56:44 -0800190
Dimitry Ivanovd2a62202015-12-15 11:06:57 -0800191 uint64_t namespace_type = ANDROID_NAMESPACE_TYPE_ISOLATED;
192 if (is_shared) {
193 namespace_type |= ANDROID_NAMESPACE_TYPE_SHARED;
194 }
195
Dimitry Ivanov9e253ce2017-05-08 22:24:24 -0700196 if (target_sdk_version < 24) {
197 namespace_type |= ANDROID_NAMESPACE_TYPE_GREYLIST_ENABLED;
198 }
199
Zhenhua WANGf2804e52016-05-30 11:16:08 +0800200 NativeLoaderNamespace parent_ns;
201 bool found_parent_namespace = FindParentNamespaceByClassLoader(env, class_loader, &parent_ns);
Dimitry Ivanov24db75c2016-05-12 15:34:41 -0700202
Zhenhua WANGf2804e52016-05-30 11:16:08 +0800203 bool is_native_bridge = false;
Dimitry Ivanovac1b1912015-12-01 13:56:44 -0800204
Zhenhua WANGf2804e52016-05-30 11:16:08 +0800205 if (found_parent_namespace) {
206 is_native_bridge = !parent_ns.is_android_namespace();
207 } else if (!library_path.empty()) {
208 is_native_bridge = NativeBridgeIsPathSupported(library_path.c_str());
Dimitry Ivanovd047c922016-02-23 14:23:51 -0800209 }
Dimitry Ivanovac1b1912015-12-01 13:56:44 -0800210
Jiyong Parka07f3052017-08-22 10:26:10 +0900211 std::string system_exposed_libraries = system_public_libraries_;
212 const char* namespace_name = kClassloaderNamespaceName;
213 android_namespace_t* vndk_ns = nullptr;
214 if (is_for_vendor && !is_shared) {
215 LOG_FATAL_IF(is_native_bridge, "Unbundled vendor apk must not use translated architecture");
216
217 // For vendor apks, give access to the vendor lib even though
218 // they are treated as unbundled; the libs and apks are still bundled
219 // together in the vendor partition.
220#if defined(__LP64__)
221 std::string vendor_lib_path = "/vendor/lib64";
222#else
223 std::string vendor_lib_path = "/vendor/lib";
224#endif
225 library_path = library_path + ":" + vendor_lib_path.c_str();
226 permitted_path = permitted_path + ":" + vendor_lib_path.c_str();
227
228 // Also give access to LLNDK libraries since they are available to vendors
229 system_exposed_libraries = system_exposed_libraries + ":" + system_llndk_libraries_.c_str();
230
231 // Give access to VNDK-SP libraries from the 'vndk' namespace.
232 vndk_ns = android_get_exported_namespace(kVndkNamespaceName);
233 LOG_ALWAYS_FATAL_IF(vndk_ns == nullptr,
234 "Cannot find \"%s\" namespace for vendor apks", kVndkNamespaceName);
235
236 // Different name is useful for debugging
237 namespace_name = kVendorClassloaderNamespaceName;
238 ALOGD("classloader namespace configured for unbundled vendor apk. library_path=%s", library_path.c_str());
Jiyong Parke0319942018-01-15 21:14:53 +0900239 } else if (!oem_public_libraries_.empty()) {
240 // oem_public_libraries are NOT available to vendor apks, otherwise it
241 // would be system->vendor violation.
242 system_exposed_libraries = system_exposed_libraries + ":" + oem_public_libraries_.c_str();
Jiyong Parka07f3052017-08-22 10:26:10 +0900243 }
244
Zhenhua WANGf2804e52016-05-30 11:16:08 +0800245 NativeLoaderNamespace native_loader_ns;
246 if (!is_native_bridge) {
Jiyong Parka07f3052017-08-22 10:26:10 +0900247 android_namespace_t* ns = android_create_namespace(namespace_name,
Zhenhua WANGf2804e52016-05-30 11:16:08 +0800248 nullptr,
249 library_path.c_str(),
250 namespace_type,
251 permitted_path.c_str(),
252 parent_ns.get_android_ns());
253 if (ns == nullptr) {
254 *error_msg = dlerror();
255 return false;
256 }
257
Dimitry Ivanov882cad22017-05-01 15:12:49 -0700258 // Note that when vendor_ns is not configured this function will return nullptr
259 // and it will result in linking vendor_public_libraries_ to the default namespace
260 // which is expected behavior in this case.
261 android_namespace_t* vendor_ns = android_get_exported_namespace(kVendorNamespaceName);
262
Jiyong Parka07f3052017-08-22 10:26:10 +0900263 if (!android_link_namespaces(ns, nullptr, system_exposed_libraries.c_str())) {
Dimitry Ivanov26e1a842017-02-03 14:11:27 -0800264 *error_msg = dlerror();
265 return false;
266 }
267
Jiyong Parka07f3052017-08-22 10:26:10 +0900268 if (vndk_ns != nullptr && !system_vndksp_libraries_.empty()) {
269 // vendor apks are allowed to use VNDK-SP libraries.
270 if (!android_link_namespaces(ns, vndk_ns, system_vndksp_libraries_.c_str())) {
271 *error_msg = dlerror();
272 return false;
273 }
274 }
275
Dimitry Ivanov882cad22017-05-01 15:12:49 -0700276 if (!vendor_public_libraries_.empty()) {
277 if (!android_link_namespaces(ns, vendor_ns, vendor_public_libraries_.c_str())) {
278 *error_msg = dlerror();
279 return false;
280 }
281 }
282
Zhenhua WANGf2804e52016-05-30 11:16:08 +0800283 native_loader_ns = NativeLoaderNamespace(ns);
284 } else {
Jiyong Parka07f3052017-08-22 10:26:10 +0900285 native_bridge_namespace_t* ns = NativeBridgeCreateNamespace(namespace_name,
Zhenhua WANGf2804e52016-05-30 11:16:08 +0800286 nullptr,
287 library_path.c_str(),
288 namespace_type,
289 permitted_path.c_str(),
290 parent_ns.get_native_bridge_ns());
Dimitry Ivanov882cad22017-05-01 15:12:49 -0700291
Zhenhua WANGf2804e52016-05-30 11:16:08 +0800292 if (ns == nullptr) {
293 *error_msg = NativeBridgeGetError();
294 return false;
295 }
296
Dimitry Ivanov882cad22017-05-01 15:12:49 -0700297 native_bridge_namespace_t* vendor_ns = NativeBridgeGetVendorNamespace();
298
Jiyong Parka07f3052017-08-22 10:26:10 +0900299 if (!NativeBridgeLinkNamespaces(ns, nullptr, system_exposed_libraries.c_str())) {
Zhenhua WANGe8fb11d2017-02-27 10:14:45 +0800300 *error_msg = NativeBridgeGetError();
301 return false;
302 }
303
Dimitry Ivanov882cad22017-05-01 15:12:49 -0700304 if (!vendor_public_libraries_.empty()) {
305 if (!NativeBridgeLinkNamespaces(ns, vendor_ns, vendor_public_libraries_.c_str())) {
306 *error_msg = NativeBridgeGetError();
307 return false;
308 }
309 }
310
Zhenhua WANGf2804e52016-05-30 11:16:08 +0800311 native_loader_ns = NativeLoaderNamespace(ns);
312 }
313
314 namespaces_.push_back(std::make_pair(env->NewWeakGlobalRef(class_loader), native_loader_ns));
315
316 *ns = native_loader_ns;
317 return true;
Dimitry Ivanovac1b1912015-12-01 13:56:44 -0800318 }
319
Zhenhua WANGf2804e52016-05-30 11:16:08 +0800320 bool FindNamespaceByClassLoader(JNIEnv* env, jobject class_loader, NativeLoaderNamespace* ns) {
Dimitry Ivanovf44ecde2016-02-22 13:48:22 -0800321 auto it = std::find_if(namespaces_.begin(), namespaces_.end(),
Zhenhua WANGf2804e52016-05-30 11:16:08 +0800322 [&](const std::pair<jweak, NativeLoaderNamespace>& value) {
Dimitry Ivanovf44ecde2016-02-22 13:48:22 -0800323 return env->IsSameObject(value.first, class_loader);
324 });
Zhenhua WANGf2804e52016-05-30 11:16:08 +0800325 if (it != namespaces_.end()) {
326 if (ns != nullptr) {
327 *ns = it->second;
328 }
329
330 return true;
331 }
332
333 return false;
Dimitry Ivanovf44ecde2016-02-22 13:48:22 -0800334 }
335
Dimitry Ivanov4b0e9632016-03-15 13:51:26 -0700336 void Initialize() {
Dimitry Ivanov80ddb8f2016-05-09 18:12:00 -0700337 // Once public namespace is initialized there is no
338 // point in running this code - it will have no effect
339 // on the current list of public libraries.
340 if (initialized_) {
341 return;
342 }
343
Dimitry Ivanovb6140452016-04-06 18:24:08 -0700344 std::vector<std::string> sonames;
Dimitry Ivanov0b5651e2016-04-21 16:42:48 -0700345 const char* android_root_env = getenv("ANDROID_ROOT");
346 std::string root_dir = android_root_env != nullptr ? android_root_env : "/system";
347 std::string public_native_libraries_system_config =
348 root_dir + kPublicNativeLibrariesSystemConfigPathFromRoot;
Jiyong Parka07f3052017-08-22 10:26:10 +0900349 std::string llndk_native_libraries_system_config =
350 root_dir + kLlndkNativeLibrariesSystemConfigPathFromRoot;
351 std::string vndksp_native_libraries_system_config =
352 root_dir + kVndkspNativeLibrariesSystemConfigPathFromRoot;
Dimitry Ivanovb6140452016-04-06 18:24:08 -0700353
Christopher Ferris39da84b2016-06-21 16:11:23 -0700354 std::string error_msg;
Jiyong Parkd1006fe2017-11-08 18:44:09 +0900355 LOG_ALWAYS_FATAL_IF(
356 !ReadConfig(public_native_libraries_system_config, &sonames, always_true, &error_msg),
357 "Error reading public native library list from \"%s\": %s",
358 public_native_libraries_system_config.c_str(), error_msg.c_str());
359
Jiyong Parke0319942018-01-15 21:14:53 +0900360 // For debuggable platform builds use ANDROID_ADDITIONAL_PUBLIC_LIBRARIES environment
361 // variable to add libraries to the list. This is intended for platform tests only.
362 if (is_debuggable()) {
363 const char* additional_libs = getenv("ANDROID_ADDITIONAL_PUBLIC_LIBRARIES");
364 if (additional_libs != nullptr && additional_libs[0] != '\0') {
365 std::vector<std::string> additional_libs_vector = base::Split(additional_libs, ":");
366 std::copy(additional_libs_vector.begin(), additional_libs_vector.end(),
367 std::back_inserter(sonames));
368 }
369 }
370
371 // android_init_namespaces() expects all the public libraries
372 // to be loaded so that they can be found by soname alone.
373 //
374 // TODO(dimitry): this is a bit misleading since we do not know
375 // if the vendor public library is going to be opened from /vendor/lib
376 // we might as well end up loading them from /system/lib
377 // For now we rely on CTS test to catch things like this but
378 // it should probably be addressed in the future.
379 for (const auto& soname : sonames) {
380 LOG_ALWAYS_FATAL_IF(dlopen(soname.c_str(), RTLD_NOW | RTLD_NODELETE) == nullptr,
381 "Error preloading public library %s: %s", soname.c_str(), dlerror());
382 }
383
384 system_public_libraries_ = base::Join(sonames, ':');
385
Jiyong Parkd1006fe2017-11-08 18:44:09 +0900386 // read /system/etc/public.libraries-<companyname>.txt which contain partner defined
387 // system libs that are exposed to apps. The libs in the txt files must be
388 // named as lib<name>.<companyname>.so.
Jiyong Parke0319942018-01-15 21:14:53 +0900389 sonames.clear();
Jiyong Parkd1006fe2017-11-08 18:44:09 +0900390 std::string dirname = base::Dirname(public_native_libraries_system_config);
391 std::unique_ptr<DIR, decltype(&closedir)> dir(opendir(dirname.c_str()), closedir);
392 if (dir != nullptr) {
393 // Failing to opening the dir is not an error, which can happen in
394 // webview_zygote.
395 struct dirent* ent;
396 while ((ent = readdir(dir.get())) != nullptr) {
397 if (ent->d_type != DT_REG && ent->d_type != DT_LNK) {
398 continue;
399 }
400 const std::string filename(ent->d_name);
401 if (android::base::StartsWith(filename, kPublicNativeLibrariesExtensionConfigPrefix) &&
402 android::base::EndsWith(filename, kPublicNativeLibrariesExtensionConfigSuffix)) {
403 const size_t start = kPublicNativeLibrariesExtensionConfigPrefixLen;
404 const size_t end = filename.size() - kPublicNativeLibrariesExtensionConfigSuffixLen;
405 const std::string company_name = filename.substr(start, end - start);
406 const std::string config_file_path = dirname + "/" + filename;
407 LOG_ALWAYS_FATAL_IF(
408 company_name.empty(),
409 "Error extracting company name from public native library list file path \"%s\"",
410 config_file_path.c_str());
411 LOG_ALWAYS_FATAL_IF(
412 !ReadConfig(
413 config_file_path, &sonames,
414 [&company_name](const std::string& soname, std::string* error_msg) {
415 if (android::base::StartsWith(soname, "lib") &&
Elliott Hughes579e6822017-12-20 09:41:00 -0800416 android::base::EndsWith(soname, "." + company_name + ".so")) {
Jiyong Parkd1006fe2017-11-08 18:44:09 +0900417 return true;
418 } else {
419 *error_msg = "Library name \"" + soname +
420 "\" does not end with the company name: " + company_name + ".";
421 return false;
422 }
423 },
424 &error_msg),
425 "Error reading public native library list from \"%s\": %s", config_file_path.c_str(),
426 error_msg.c_str());
427 }
428 }
429 }
Jiyong Parke0319942018-01-15 21:14:53 +0900430 oem_public_libraries_ = base::Join(sonames, ':');
Dimitry Ivanov7d028292016-05-05 17:30:24 -0700431
Justin Yun4a1d1102017-11-27 17:04:14 +0900432 // Insert VNDK version to llndk and vndksp config file names.
433 insert_vndk_version_str(&llndk_native_libraries_system_config);
434 insert_vndk_version_str(&vndksp_native_libraries_system_config);
435
Dimitry Ivanov882cad22017-05-01 15:12:49 -0700436 sonames.clear();
Jiyong Parkd1006fe2017-11-08 18:44:09 +0900437 ReadConfig(llndk_native_libraries_system_config, &sonames, always_true);
Jiyong Parka07f3052017-08-22 10:26:10 +0900438 system_llndk_libraries_ = base::Join(sonames, ':');
439
440 sonames.clear();
Jiyong Parkd1006fe2017-11-08 18:44:09 +0900441 ReadConfig(vndksp_native_libraries_system_config, &sonames, always_true);
Jiyong Parka07f3052017-08-22 10:26:10 +0900442 system_vndksp_libraries_ = base::Join(sonames, ':');
443
444 sonames.clear();
Dimitry Ivanov882cad22017-05-01 15:12:49 -0700445 // This file is optional, quietly ignore if the file does not exist.
Jiyong Parkd1006fe2017-11-08 18:44:09 +0900446 ReadConfig(kPublicNativeLibrariesVendorConfig, &sonames, always_true, nullptr);
Dimitry Ivanov882cad22017-05-01 15:12:49 -0700447
448 vendor_public_libraries_ = base::Join(sonames, ':');
Dimitry Ivanovb6140452016-04-06 18:24:08 -0700449 }
450
Dimitry Ivanovbe4ca3a2016-05-02 10:43:16 -0700451 void Reset() {
452 namespaces_.clear();
453 }
454
Dimitry Ivanovb6140452016-04-06 18:24:08 -0700455 private:
Christopher Ferris39da84b2016-06-21 16:11:23 -0700456 bool ReadConfig(const std::string& configFile, std::vector<std::string>* sonames,
Jiyong Parkd1006fe2017-11-08 18:44:09 +0900457 const std::function<bool(const std::string& /* soname */,
458 std::string* /* error_msg */)>& check_soname,
Christopher Ferris39da84b2016-06-21 16:11:23 -0700459 std::string* error_msg = nullptr) {
Dimitry Ivanov4b0e9632016-03-15 13:51:26 -0700460 // Read list of public native libraries from the config file.
461 std::string file_content;
Dimitry Ivanovb6140452016-04-06 18:24:08 -0700462 if(!base::ReadFileToString(configFile, &file_content)) {
Christopher Ferris39da84b2016-06-21 16:11:23 -0700463 if (error_msg) *error_msg = strerror(errno);
Dimitry Ivanovb6140452016-04-06 18:24:08 -0700464 return false;
465 }
Dimitry Ivanov4b0e9632016-03-15 13:51:26 -0700466
467 std::vector<std::string> lines = base::Split(file_content, "\n");
468
Christopher Ferris39da84b2016-06-21 16:11:23 -0700469 for (auto& line : lines) {
Dimitry Ivanov4b0e9632016-03-15 13:51:26 -0700470 auto trimmed_line = base::Trim(line);
471 if (trimmed_line[0] == '#' || trimmed_line.empty()) {
472 continue;
473 }
Christopher Ferris39da84b2016-06-21 16:11:23 -0700474 size_t space_pos = trimmed_line.rfind(' ');
475 if (space_pos != std::string::npos) {
476 std::string type = trimmed_line.substr(space_pos + 1);
477 if (type != "32" && type != "64") {
478 if (error_msg) *error_msg = "Malformed line: " + line;
479 return false;
480 }
481#if defined(__LP64__)
482 // Skip 32 bit public library.
483 if (type == "32") {
484 continue;
485 }
486#else
487 // Skip 64 bit public library.
488 if (type == "64") {
489 continue;
490 }
491#endif
492 trimmed_line.resize(space_pos);
493 }
Dimitry Ivanov4b0e9632016-03-15 13:51:26 -0700494
Jiyong Parkd1006fe2017-11-08 18:44:09 +0900495 if (check_soname(trimmed_line, error_msg)) {
496 sonames->push_back(trimmed_line);
497 } else {
498 return false;
499 }
Dimitry Ivanov4b0e9632016-03-15 13:51:26 -0700500 }
501
Dimitry Ivanovb6140452016-04-06 18:24:08 -0700502 return true;
Dimitry Ivanovd68c8e92016-02-10 14:09:22 -0800503 }
Dimitry Ivanovac1b1912015-12-01 13:56:44 -0800504
Zhenhua WANGf2804e52016-05-30 11:16:08 +0800505 bool InitPublicNamespace(const char* library_path, std::string* error_msg) {
506 // Ask native bride if this apps library path should be handled by it
507 bool is_native_bridge = NativeBridgeIsPathSupported(library_path);
508
Dimitry Ivanov4b0e9632016-03-15 13:51:26 -0700509 // (http://b/25844435) - Some apps call dlopen from generated code (mono jited
510 // code is one example) unknown to linker in which case linker uses anonymous
511 // namespace. The second argument specifies the search path for the anonymous
512 // namespace which is the library_path of the classloader.
Dimitry Ivanov882cad22017-05-01 15:12:49 -0700513 initialized_ = android_init_anonymous_namespace(system_public_libraries_.c_str(),
Dimitry Ivanov26e1a842017-02-03 14:11:27 -0800514 is_native_bridge ? nullptr : library_path);
Dimitry Ivanovd836ab02016-11-02 18:03:10 -0700515 if (!initialized_) {
516 *error_msg = dlerror();
517 return false;
518 }
519
520 // and now initialize native bridge namespaces if necessary.
521 if (NativeBridgeInitialized()) {
Dimitry Ivanov882cad22017-05-01 15:12:49 -0700522 initialized_ = NativeBridgeInitAnonymousNamespace(system_public_libraries_.c_str(),
Zhenhua WANGe8fb11d2017-02-27 10:14:45 +0800523 is_native_bridge ? library_path : nullptr);
Zhenhua WANGf2804e52016-05-30 11:16:08 +0800524 if (!initialized_) {
525 *error_msg = NativeBridgeGetError();
526 }
527 }
Dimitry Ivanovac1b1912015-12-01 13:56:44 -0800528
529 return initialized_;
530 }
531
Dimitry Ivanov24db75c2016-05-12 15:34:41 -0700532 jobject GetParentClassLoader(JNIEnv* env, jobject class_loader) {
533 jclass class_loader_class = env->FindClass("java/lang/ClassLoader");
534 jmethodID get_parent = env->GetMethodID(class_loader_class,
535 "getParent",
536 "()Ljava/lang/ClassLoader;");
537
538 return env->CallObjectMethod(class_loader, get_parent);
539 }
540
Zhenhua WANGf2804e52016-05-30 11:16:08 +0800541 bool FindParentNamespaceByClassLoader(JNIEnv* env,
542 jobject class_loader,
543 NativeLoaderNamespace* ns) {
Dimitry Ivanov24db75c2016-05-12 15:34:41 -0700544 jobject parent_class_loader = GetParentClassLoader(env, class_loader);
545
546 while (parent_class_loader != nullptr) {
Zhenhua WANGf2804e52016-05-30 11:16:08 +0800547 if (FindNamespaceByClassLoader(env, parent_class_loader, ns)) {
548 return true;
Dimitry Ivanov24db75c2016-05-12 15:34:41 -0700549 }
550
551 parent_class_loader = GetParentClassLoader(env, parent_class_loader);
552 }
Zhenhua WANGf2804e52016-05-30 11:16:08 +0800553
554 return false;
Dimitry Ivanov24db75c2016-05-12 15:34:41 -0700555 }
556
Dimitry Ivanovac1b1912015-12-01 13:56:44 -0800557 bool initialized_;
Zhenhua WANGf2804e52016-05-30 11:16:08 +0800558 std::vector<std::pair<jweak, NativeLoaderNamespace>> namespaces_;
Dimitry Ivanov882cad22017-05-01 15:12:49 -0700559 std::string system_public_libraries_;
560 std::string vendor_public_libraries_;
Jiyong Parke0319942018-01-15 21:14:53 +0900561 std::string oem_public_libraries_;
Jiyong Parka07f3052017-08-22 10:26:10 +0900562 std::string system_llndk_libraries_;
563 std::string system_vndksp_libraries_;
Dimitry Ivanovac1b1912015-12-01 13:56:44 -0800564
565 DISALLOW_COPY_AND_ASSIGN(LibraryNamespaces);
566};
567
Dimitry Ivanov34d5a202016-02-29 13:21:43 -0800568static std::mutex g_namespaces_mutex;
Dimitry Ivanovac1b1912015-12-01 13:56:44 -0800569static LibraryNamespaces* g_namespaces = new LibraryNamespaces;
570#endif
571
Dimitry Ivanov4b0e9632016-03-15 13:51:26 -0700572void InitializeNativeLoader() {
Dimitry Ivanov426799d2016-02-22 11:27:48 -0800573#if defined(__ANDROID__)
Dimitry Ivanov34d5a202016-02-29 13:21:43 -0800574 std::lock_guard<std::mutex> guard(g_namespaces_mutex);
Dimitry Ivanov4b0e9632016-03-15 13:51:26 -0700575 g_namespaces->Initialize();
Dimitry Ivanov426799d2016-02-22 11:27:48 -0800576#endif
577}
578
Dimitry Ivanovbe4ca3a2016-05-02 10:43:16 -0700579void ResetNativeLoader() {
580#if defined(__ANDROID__)
581 std::lock_guard<std::mutex> guard(g_namespaces_mutex);
582 g_namespaces->Reset();
583#endif
584}
Dimitry Ivanovac1b1912015-12-01 13:56:44 -0800585
Dimitry Ivanovd047c922016-02-23 14:23:51 -0800586jstring CreateClassLoaderNamespace(JNIEnv* env,
587 int32_t target_sdk_version,
588 jobject class_loader,
589 bool is_shared,
Jiyong Parka07f3052017-08-22 10:26:10 +0900590 bool is_for_vendor,
Dimitry Ivanovd047c922016-02-23 14:23:51 -0800591 jstring library_path,
592 jstring permitted_path) {
Dimitry Ivanovac1b1912015-12-01 13:56:44 -0800593#if defined(__ANDROID__)
Dimitry Ivanov34d5a202016-02-29 13:21:43 -0800594 std::lock_guard<std::mutex> guard(g_namespaces_mutex);
Zhenhua WANGf2804e52016-05-30 11:16:08 +0800595
596 std::string error_msg;
597 NativeLoaderNamespace ns;
598 bool success = g_namespaces->Create(env,
Dimitry Ivanov9e253ce2017-05-08 22:24:24 -0700599 target_sdk_version,
Zhenhua WANGf2804e52016-05-30 11:16:08 +0800600 class_loader,
601 is_shared,
Jiyong Parka07f3052017-08-22 10:26:10 +0900602 is_for_vendor,
Zhenhua WANGf2804e52016-05-30 11:16:08 +0800603 library_path,
604 permitted_path,
605 &ns,
606 &error_msg);
607 if (!success) {
608 return env->NewStringUTF(error_msg.c_str());
Dimitry Ivanovd047c922016-02-23 14:23:51 -0800609 }
610#else
Jiyong Parka07f3052017-08-22 10:26:10 +0900611 UNUSED(env, target_sdk_version, class_loader, is_shared, is_for_vendor,
Dimitry Ivanovd047c922016-02-23 14:23:51 -0800612 library_path, permitted_path);
613#endif
614 return nullptr;
615}
616
617void* OpenNativeLibrary(JNIEnv* env,
618 int32_t target_sdk_version,
619 const char* path,
620 jobject class_loader,
Zhenhua WANGf2804e52016-05-30 11:16:08 +0800621 jstring library_path,
622 bool* needs_native_bridge,
623 std::string* error_msg) {
Dimitry Ivanovd047c922016-02-23 14:23:51 -0800624#if defined(__ANDROID__)
Dimitry Ivanov5539db02016-04-20 16:07:30 -0700625 UNUSED(target_sdk_version);
626 if (class_loader == nullptr) {
Zhenhua WANGf2804e52016-05-30 11:16:08 +0800627 *needs_native_bridge = false;
Dimitry Ivanovac1b1912015-12-01 13:56:44 -0800628 return dlopen(path, RTLD_NOW);
629 }
630
Dimitry Ivanov34d5a202016-02-29 13:21:43 -0800631 std::lock_guard<std::mutex> guard(g_namespaces_mutex);
Zhenhua WANGf2804e52016-05-30 11:16:08 +0800632 NativeLoaderNamespace ns;
Dimitry Ivanovac1b1912015-12-01 13:56:44 -0800633
Zhenhua WANGf2804e52016-05-30 11:16:08 +0800634 if (!g_namespaces->FindNamespaceByClassLoader(env, class_loader, &ns)) {
Dimitry Ivanovd047c922016-02-23 14:23:51 -0800635 // This is the case where the classloader was not created by ApplicationLoaders
636 // In this case we create an isolated not-shared namespace for it.
Dimitry Ivanov9e253ce2017-05-08 22:24:24 -0700637 if (!g_namespaces->Create(env,
638 target_sdk_version,
639 class_loader,
Jiyong Parka07f3052017-08-22 10:26:10 +0900640 false /* is_shared */,
641 false /* is_for_vendor */,
Dimitry Ivanov9e253ce2017-05-08 22:24:24 -0700642 library_path,
643 nullptr,
644 &ns,
645 error_msg)) {
Dimitry Ivanovd047c922016-02-23 14:23:51 -0800646 return nullptr;
647 }
Dimitry Ivanovac1b1912015-12-01 13:56:44 -0800648 }
649
Zhenhua WANGf2804e52016-05-30 11:16:08 +0800650 if (ns.is_android_namespace()) {
651 android_dlextinfo extinfo;
652 extinfo.flags = ANDROID_DLEXT_USE_NAMESPACE;
653 extinfo.library_namespace = ns.get_android_ns();
Dimitry Ivanovac1b1912015-12-01 13:56:44 -0800654
Zhenhua WANGf2804e52016-05-30 11:16:08 +0800655 void* handle = android_dlopen_ext(path, RTLD_NOW, &extinfo);
656 if (handle == nullptr) {
657 *error_msg = dlerror();
658 }
659 *needs_native_bridge = false;
660 return handle;
661 } else {
662 void* handle = NativeBridgeLoadLibraryExt(path, RTLD_NOW, ns.get_native_bridge_ns());
663 if (handle == nullptr) {
664 *error_msg = NativeBridgeGetError();
665 }
666 *needs_native_bridge = true;
667 return handle;
668 }
Dimitry Ivanovac1b1912015-12-01 13:56:44 -0800669#else
Andreas Gampe5c7d5822017-12-28 19:08:13 -0800670 UNUSED(env, target_sdk_version, class_loader);
671
672 // Do some best effort to emulate library-path support. It will not
673 // work for dependencies.
674 //
675 // Note: null has a special meaning and must be preserved.
676 std::string c_library_path; // Empty string by default.
677 if (library_path != nullptr && path != nullptr && path[0] != '/') {
678 ScopedUtfChars library_path_utf_chars(env, library_path);
679 c_library_path = library_path_utf_chars.c_str();
680 }
681
682 std::vector<std::string> library_paths = base::Split(c_library_path, ":");
683
684 for (const std::string& lib_path : library_paths) {
685 *needs_native_bridge = false;
686 const char* path_arg;
687 std::string complete_path;
688 if (path == nullptr) {
689 // Preserve null.
690 path_arg = nullptr;
Zhenhua WANGf2804e52016-05-30 11:16:08 +0800691 } else {
Andreas Gampe5c7d5822017-12-28 19:08:13 -0800692 complete_path = lib_path;
693 if (!complete_path.empty()) {
694 complete_path.append("/");
695 }
696 complete_path.append(path);
697 path_arg = complete_path.c_str();
698 }
699 void* handle = dlopen(path_arg, RTLD_NOW);
700 if (handle != nullptr) {
701 return handle;
702 }
703 if (NativeBridgeIsSupported(path_arg)) {
704 *needs_native_bridge = true;
705 handle = NativeBridgeLoadLibrary(path_arg, RTLD_NOW);
706 if (handle != nullptr) {
707 return handle;
708 }
709 *error_msg = NativeBridgeGetError();
710 } else {
Zhenhua WANGf2804e52016-05-30 11:16:08 +0800711 *error_msg = dlerror();
712 }
713 }
Andreas Gampe5c7d5822017-12-28 19:08:13 -0800714 return nullptr;
Dimitry Ivanovac1b1912015-12-01 13:56:44 -0800715#endif
716}
717
Zhenhua WANGf2804e52016-05-30 11:16:08 +0800718bool CloseNativeLibrary(void* handle, const bool needs_native_bridge) {
719 return needs_native_bridge ? NativeBridgeUnloadLibrary(handle) :
720 dlclose(handle);
Dimitry Ivanov09a516b2016-05-03 14:55:25 -0700721}
722
Dimitry Ivanovf44ecde2016-02-22 13:48:22 -0800723#if defined(__ANDROID__)
Dimitry Ivanov800083d2016-11-01 14:17:00 -0700724// native_bridge_namespaces are not supported for callers of this function.
725// This function will return nullptr in the case when application is running
726// on native bridge.
Dimitry Ivanovf44ecde2016-02-22 13:48:22 -0800727android_namespace_t* FindNamespaceByClassLoader(JNIEnv* env, jobject class_loader) {
Dimitry Ivanov34d5a202016-02-29 13:21:43 -0800728 std::lock_guard<std::mutex> guard(g_namespaces_mutex);
Zhenhua WANGf2804e52016-05-30 11:16:08 +0800729 NativeLoaderNamespace ns;
730 if (g_namespaces->FindNamespaceByClassLoader(env, class_loader, &ns)) {
Dimitry Ivanov800083d2016-11-01 14:17:00 -0700731 return ns.is_android_namespace() ? ns.get_android_ns() : nullptr;
Zhenhua WANGf2804e52016-05-30 11:16:08 +0800732 }
733
734 return nullptr;
Dimitry Ivanovf44ecde2016-02-22 13:48:22 -0800735}
736#endif
737
Dimitry Ivanovac1b1912015-12-01 13:56:44 -0800738}; // android namespace