| Dimitry Ivanov | ac1b191 | 2015-12-01 13:56:44 -0800 | [diff] [blame] | 1 | /* | 
|  | 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 Moreland | 00fe3ad | 2017-07-18 16:53:54 -0700 | [diff] [blame] | 18 | #include <nativehelper/ScopedUtfChars.h> | 
| Dimitry Ivanov | ac1b191 | 2015-12-01 13:56:44 -0800 | [diff] [blame] | 19 |  | 
|  | 20 | #include <dlfcn.h> | 
|  | 21 | #ifdef __ANDROID__ | 
| Dimitry Ivanov | 7f9a1aa | 2016-03-22 13:59:59 -0700 | [diff] [blame] | 22 | #define LOG_TAG "libnativeloader" | 
| Jesse Hall | b75d82b | 2017-01-09 16:04:28 -0800 | [diff] [blame] | 23 | #include "nativeloader/dlext_namespaces.h" | 
| Mark Salyzyn | 30f991f | 2017-01-10 13:19:54 -0800 | [diff] [blame] | 24 | #include "log/log.h" | 
| Dimitry Ivanov | ac1b191 | 2015-12-01 13:56:44 -0800 | [diff] [blame] | 25 | #endif | 
| Jiyong Park | d1006fe | 2017-11-08 18:44:09 +0900 | [diff] [blame] | 26 | #include <dirent.h> | 
|  | 27 | #include <sys/types.h> | 
| Zhenhua WANG | f2804e5 | 2016-05-30 11:16:08 +0800 | [diff] [blame] | 28 | #include "nativebridge/native_bridge.h" | 
| Dimitry Ivanov | ac1b191 | 2015-12-01 13:56:44 -0800 | [diff] [blame] | 29 |  | 
|  | 30 | #include <algorithm> | 
| Victor Khimenko | 1443ec4 | 2018-07-09 17:01:22 +0200 | [diff] [blame] | 31 | #include <list> | 
| Jiyong Park | d1006fe | 2017-11-08 18:44:09 +0900 | [diff] [blame] | 32 | #include <memory> | 
| Dimitry Ivanov | ac1b191 | 2015-12-01 13:56:44 -0800 | [diff] [blame] | 33 | #include <mutex> | 
| Kiyoung Kim | 4639f69 | 2019-02-20 18:04:39 +0900 | [diff] [blame] | 34 | #include <regex> | 
| Jiyong Park | d1006fe | 2017-11-08 18:44:09 +0900 | [diff] [blame] | 35 | #include <string> | 
|  | 36 | #include <vector> | 
| Dimitry Ivanov | ac1b191 | 2015-12-01 13:56:44 -0800 | [diff] [blame] | 37 |  | 
| Mark Salyzyn | ff2dcd9 | 2016-09-28 15:54:45 -0700 | [diff] [blame] | 38 | #include <android-base/file.h> | 
|  | 39 | #include <android-base/macros.h> | 
|  | 40 | #include <android-base/strings.h> | 
| Dimitry Ivanov | ac1b191 | 2015-12-01 13:56:44 -0800 | [diff] [blame] | 41 |  | 
| Justin Yun | 4a1d110 | 2017-11-27 17:04:14 +0900 | [diff] [blame] | 42 | #ifdef __BIONIC__ | 
|  | 43 | #include <android-base/properties.h> | 
|  | 44 | #endif | 
|  | 45 |  | 
| Zhenhua WANG | f2804e5 | 2016-05-30 11:16:08 +0800 | [diff] [blame] | 46 | #define CHECK(predicate) LOG_ALWAYS_FATAL_IF(!(predicate),\ | 
|  | 47 | "%s:%d: %s CHECK '" #predicate "' failed.",\ | 
|  | 48 | __FILE__, __LINE__, __FUNCTION__) | 
|  | 49 |  | 
| Inseob Kim | 67cb056 | 2018-05-04 11:39:12 +0900 | [diff] [blame] | 50 | using namespace std::string_literals; | 
|  | 51 |  | 
| Dimitry Ivanov | ac1b191 | 2015-12-01 13:56:44 -0800 | [diff] [blame] | 52 | namespace android { | 
|  | 53 |  | 
| Dimitry Ivanov | 4b0e963 | 2016-03-15 13:51:26 -0700 | [diff] [blame] | 54 | #if defined(__ANDROID__) | 
| Nicolas Geoffray | c3a73dc | 2019-01-12 15:01:20 +0000 | [diff] [blame] | 55 | struct NativeLoaderNamespace { | 
| Zhenhua WANG | f2804e5 | 2016-05-30 11:16:08 +0800 | [diff] [blame] | 56 | 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 Park | d1006fe | 2017-11-08 18:44:09 +0900 | [diff] [blame] | 91 | static constexpr const char kPublicNativeLibrariesSystemConfigPathFromRoot[] = | 
|  | 92 | "/etc/public.libraries.txt"; | 
|  | 93 | static constexpr const char kPublicNativeLibrariesExtensionConfigPrefix[] = "public.libraries-"; | 
|  | 94 | static constexpr const size_t kPublicNativeLibrariesExtensionConfigPrefixLen = | 
|  | 95 | sizeof(kPublicNativeLibrariesExtensionConfigPrefix) - 1; | 
|  | 96 | static constexpr const char kPublicNativeLibrariesExtensionConfigSuffix[] = ".txt"; | 
|  | 97 | static constexpr const size_t kPublicNativeLibrariesExtensionConfigSuffixLen = | 
|  | 98 | sizeof(kPublicNativeLibrariesExtensionConfigSuffix) - 1; | 
|  | 99 | static constexpr const char kPublicNativeLibrariesVendorConfig[] = | 
|  | 100 | "/vendor/etc/public.libraries.txt"; | 
|  | 101 | static constexpr const char kLlndkNativeLibrariesSystemConfigPathFromRoot[] = | 
|  | 102 | "/etc/llndk.libraries.txt"; | 
|  | 103 | static constexpr const char kVndkspNativeLibrariesSystemConfigPathFromRoot[] = | 
|  | 104 | "/etc/vndksp.libraries.txt"; | 
| Dimitry Ivanov | ac1b191 | 2015-12-01 13:56:44 -0800 | [diff] [blame] | 105 |  | 
| Victor Chang | 7a20a90 | 2019-01-28 18:43:24 +0000 | [diff] [blame] | 106 | static const std::vector<const std::string> kRuntimePublicLibraries = { | 
|  | 107 | "libicuuc.so", | 
|  | 108 | "libicui18n.so", | 
|  | 109 | }; | 
|  | 110 |  | 
| Dimitry Ivanov | 882cad2 | 2017-05-01 15:12:49 -0700 | [diff] [blame] | 111 | // 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. | 
|  | 115 | static constexpr const char* kVendorNamespaceName = "sphal"; | 
|  | 116 |  | 
| Jiyong Park | a07f305 | 2017-08-22 10:26:10 +0900 | [diff] [blame] | 117 | static constexpr const char* kVndkNamespaceName = "vndk"; | 
|  | 118 |  | 
| Martin Stjernholm | 7888b5c | 2019-02-23 02:10:14 +0000 | [diff] [blame] | 119 | static constexpr const char* kDefaultNamespaceName = "default"; | 
|  | 120 | static constexpr const char* kPlatformNamespaceName = "platform"; | 
| Victor Chang | 7a20a90 | 2019-01-28 18:43:24 +0000 | [diff] [blame] | 121 | static constexpr const char* kRuntimeNamespaceName = "runtime"; | 
|  | 122 |  | 
| Martin Stjernholm | b78f6ec | 2019-02-06 21:47:26 +0000 | [diff] [blame] | 123 | // classloader-namespace is a linker namespace that is created for the loaded | 
|  | 124 | // app. To be specific, it is created for the app classloader. When | 
|  | 125 | // System.load() is called from a Java class that is loaded from the | 
|  | 126 | // classloader, the classloader-namespace namespace associated with that | 
|  | 127 | // classloader is selected for dlopen. The namespace is configured so that its | 
|  | 128 | // search path is set to the app-local JNI directory and it is linked to the | 
|  | 129 | // default namespace with the names of libs listed in the public.libraries.txt. | 
|  | 130 | // This way an app can only load its own JNI libraries along with the public libs. | 
| Jiyong Park | a07f305 | 2017-08-22 10:26:10 +0900 | [diff] [blame] | 131 | static constexpr const char* kClassloaderNamespaceName = "classloader-namespace"; | 
| Martin Stjernholm | b78f6ec | 2019-02-06 21:47:26 +0000 | [diff] [blame] | 132 | // Same thing for vendor APKs. | 
| Jiyong Park | a07f305 | 2017-08-22 10:26:10 +0900 | [diff] [blame] | 133 | static constexpr const char* kVendorClassloaderNamespaceName = "vendor-classloader-namespace"; | 
|  | 134 |  | 
| Dimitry Ivanov | f334cbf | 2016-05-10 10:39:48 -0700 | [diff] [blame] | 135 | // (http://b/27588281) This is a workaround for apps using custom classloaders and calling | 
|  | 136 | // System.load() with an absolute path which is outside of the classloader library search path. | 
|  | 137 | // This list includes all directories app is allowed to access this way. | 
|  | 138 | static constexpr const char* kWhitelistedDirectories = "/data:/mnt/expand"; | 
|  | 139 |  | 
| Nicolas Geoffray | 890e3bf | 2019-01-22 09:11:57 +0000 | [diff] [blame] | 140 | static constexpr const char* kApexPath = "/apex/"; | 
|  | 141 |  | 
| Victor Chang | f70a2fe | 2019-02-01 20:01:27 +0000 | [diff] [blame] | 142 | #if defined(__LP64__) | 
|  | 143 | static constexpr const char* kRuntimeApexLibPath = "/apex/com.android.runtime/lib64"; | 
| Kiyoung Kim | 4639f69 | 2019-02-20 18:04:39 +0900 | [diff] [blame] | 144 | static constexpr const char* kVendorLibPath = "/vendor/lib64"; | 
|  | 145 | static constexpr const char* kProductLibPath = "/product/lib64:/system/product/lib64"; | 
| Victor Chang | f70a2fe | 2019-02-01 20:01:27 +0000 | [diff] [blame] | 146 | #else | 
|  | 147 | static constexpr const char* kRuntimeApexLibPath = "/apex/com.android.runtime/lib"; | 
| Kiyoung Kim | 4639f69 | 2019-02-20 18:04:39 +0900 | [diff] [blame] | 148 | static constexpr const char* kVendorLibPath = "/vendor/lib"; | 
|  | 149 | static constexpr const char* kProductLibPath = "/product/lib:/system/product/lib"; | 
| Victor Chang | f70a2fe | 2019-02-01 20:01:27 +0000 | [diff] [blame] | 150 | #endif | 
|  | 151 |  | 
| Kiyoung Kim | 4639f69 | 2019-02-20 18:04:39 +0900 | [diff] [blame] | 152 | static const std::regex kVendorDexPathRegex("(^|:)/vendor/"); | 
|  | 153 | static const std::regex kProductDexPathRegex("(^|:)(/system)?/product/"); | 
|  | 154 |  | 
|  | 155 | // Define origin of APK if it is from vendor partition or product partition | 
|  | 156 | typedef enum { | 
|  | 157 | APK_ORIGIN_DEFAULT = 0, | 
|  | 158 | APK_ORIGIN_VENDOR = 1, | 
|  | 159 | APK_ORIGIN_PRODUCT = 2, | 
|  | 160 | } ApkOrigin; | 
|  | 161 |  | 
| Dimitry Ivanov | 7d02829 | 2016-05-05 17:30:24 -0700 | [diff] [blame] | 162 | static bool is_debuggable() { | 
| Orion Hodson | 34b126b | 2019-02-21 17:18:44 +0000 | [diff] [blame] | 163 | bool debuggable = false; | 
|  | 164 | #ifdef __BIONIC__ | 
|  | 165 | debuggable = android::base::GetBoolProperty("ro.debuggable", false); | 
|  | 166 | #endif | 
|  | 167 | return debuggable; | 
| Dimitry Ivanov | 7d02829 | 2016-05-05 17:30:24 -0700 | [diff] [blame] | 168 | } | 
|  | 169 |  | 
| Justin Yun | 4a1d110 | 2017-11-27 17:04:14 +0900 | [diff] [blame] | 170 | static std::string vndk_version_str() { | 
|  | 171 | #ifdef __BIONIC__ | 
|  | 172 | std::string version = android::base::GetProperty("ro.vndk.version", ""); | 
|  | 173 | if (version != "" && version != "current") { | 
|  | 174 | return "." + version; | 
|  | 175 | } | 
|  | 176 | #endif | 
|  | 177 | return ""; | 
|  | 178 | } | 
|  | 179 |  | 
|  | 180 | static void insert_vndk_version_str(std::string* file_name) { | 
|  | 181 | CHECK(file_name != nullptr); | 
|  | 182 | size_t insert_pos = file_name->find_last_of("."); | 
|  | 183 | if (insert_pos == std::string::npos) { | 
|  | 184 | insert_pos = file_name->length(); | 
|  | 185 | } | 
|  | 186 | file_name->insert(insert_pos, vndk_version_str()); | 
|  | 187 | } | 
|  | 188 |  | 
| Jiyong Park | d1006fe | 2017-11-08 18:44:09 +0900 | [diff] [blame] | 189 | static const std::function<bool(const std::string&, std::string*)> always_true = | 
|  | 190 | [](const std::string&, std::string*) { return true; }; | 
|  | 191 |  | 
| Dimitry Ivanov | ac1b191 | 2015-12-01 13:56:44 -0800 | [diff] [blame] | 192 | class LibraryNamespaces { | 
|  | 193 | public: | 
| Dimitry Ivanov | 426799d | 2016-02-22 11:27:48 -0800 | [diff] [blame] | 194 | LibraryNamespaces() : initialized_(false) { } | 
| Dimitry Ivanov | ac1b191 | 2015-12-01 13:56:44 -0800 | [diff] [blame] | 195 |  | 
| Nicolas Geoffray | c3a73dc | 2019-01-12 15:01:20 +0000 | [diff] [blame] | 196 | NativeLoaderNamespace* Create(JNIEnv* env, uint32_t target_sdk_version, jobject class_loader, | 
| Kiyoung Kim | 4639f69 | 2019-02-20 18:04:39 +0900 | [diff] [blame] | 197 | bool is_shared, jstring dex_path, jstring java_library_path, | 
| Nicolas Geoffray | c3a73dc | 2019-01-12 15:01:20 +0000 | [diff] [blame] | 198 | jstring java_permitted_path, std::string* error_msg) { | 
| Dimitry Ivanov | cf9892b | 2016-05-09 10:55:50 -0700 | [diff] [blame] | 199 | std::string library_path; // empty string by default. | 
|  | 200 |  | 
|  | 201 | if (java_library_path != nullptr) { | 
|  | 202 | ScopedUtfChars library_path_utf_chars(env, java_library_path); | 
|  | 203 | library_path = library_path_utf_chars.c_str(); | 
|  | 204 | } | 
| Dimitry Ivanov | ac1b191 | 2015-12-01 13:56:44 -0800 | [diff] [blame] | 205 |  | 
| Kiyoung Kim | 4639f69 | 2019-02-20 18:04:39 +0900 | [diff] [blame] | 206 | ApkOrigin apk_origin = GetApkOriginFromDexPath(env, dex_path); | 
|  | 207 |  | 
| Dimitry Ivanov | f334cbf | 2016-05-10 10:39:48 -0700 | [diff] [blame] | 208 | // (http://b/27588281) This is a workaround for apps using custom | 
|  | 209 | // classloaders and calling System.load() with an absolute path which | 
|  | 210 | // is outside of the classloader library search path. | 
|  | 211 | // | 
|  | 212 | // This part effectively allows such a classloader to access anything | 
|  | 213 | // under /data and /mnt/expand | 
|  | 214 | std::string permitted_path = kWhitelistedDirectories; | 
|  | 215 |  | 
| Dimitry Ivanov | 0d6e594 | 2015-12-08 11:16:56 -0800 | [diff] [blame] | 216 | if (java_permitted_path != nullptr) { | 
|  | 217 | ScopedUtfChars path(env, java_permitted_path); | 
| Dimitry Ivanov | d0b1531 | 2016-05-10 16:21:25 -0700 | [diff] [blame] | 218 | if (path.c_str() != nullptr && path.size() > 0) { | 
|  | 219 | permitted_path = permitted_path + ":" + path.c_str(); | 
|  | 220 | } | 
| Dimitry Ivanov | 0d6e594 | 2015-12-08 11:16:56 -0800 | [diff] [blame] | 221 | } | 
|  | 222 |  | 
| Zhenhua WANG | f2804e5 | 2016-05-30 11:16:08 +0800 | [diff] [blame] | 223 | if (!initialized_ && !InitPublicNamespace(library_path.c_str(), error_msg)) { | 
| Victor Khimenko | 1443ec4 | 2018-07-09 17:01:22 +0200 | [diff] [blame] | 224 | return nullptr; | 
| Dimitry Ivanov | ac1b191 | 2015-12-01 13:56:44 -0800 | [diff] [blame] | 225 | } | 
|  | 226 |  | 
| Victor Khimenko | 1443ec4 | 2018-07-09 17:01:22 +0200 | [diff] [blame] | 227 | bool found = FindNamespaceByClassLoader(env, class_loader); | 
| Dimitry Ivanov | ac1b191 | 2015-12-01 13:56:44 -0800 | [diff] [blame] | 228 |  | 
| Zhenhua WANG | f2804e5 | 2016-05-30 11:16:08 +0800 | [diff] [blame] | 229 | LOG_ALWAYS_FATAL_IF(found, | 
| Dimitry Ivanov | 4b0e963 | 2016-03-15 13:51:26 -0700 | [diff] [blame] | 230 | "There is already a namespace associated with this classloader"); | 
| Dimitry Ivanov | ac1b191 | 2015-12-01 13:56:44 -0800 | [diff] [blame] | 231 |  | 
| Dimitry Ivanov | d2a6220 | 2015-12-15 11:06:57 -0800 | [diff] [blame] | 232 | uint64_t namespace_type = ANDROID_NAMESPACE_TYPE_ISOLATED; | 
|  | 233 | if (is_shared) { | 
|  | 234 | namespace_type |= ANDROID_NAMESPACE_TYPE_SHARED; | 
|  | 235 | } | 
|  | 236 |  | 
| Dimitry Ivanov | 9e253ce | 2017-05-08 22:24:24 -0700 | [diff] [blame] | 237 | if (target_sdk_version < 24) { | 
|  | 238 | namespace_type |= ANDROID_NAMESPACE_TYPE_GREYLIST_ENABLED; | 
|  | 239 | } | 
|  | 240 |  | 
| Victor Khimenko | 1443ec4 | 2018-07-09 17:01:22 +0200 | [diff] [blame] | 241 | NativeLoaderNamespace* parent_ns = FindParentNamespaceByClassLoader(env, class_loader); | 
| Dimitry Ivanov | 24db75c | 2016-05-12 15:34:41 -0700 | [diff] [blame] | 242 |  | 
| Zhenhua WANG | f2804e5 | 2016-05-30 11:16:08 +0800 | [diff] [blame] | 243 | bool is_native_bridge = false; | 
| Dimitry Ivanov | ac1b191 | 2015-12-01 13:56:44 -0800 | [diff] [blame] | 244 |  | 
| Victor Khimenko | 1443ec4 | 2018-07-09 17:01:22 +0200 | [diff] [blame] | 245 | if (parent_ns != nullptr) { | 
|  | 246 | is_native_bridge = !parent_ns->is_android_namespace(); | 
| Zhenhua WANG | f2804e5 | 2016-05-30 11:16:08 +0800 | [diff] [blame] | 247 | } else if (!library_path.empty()) { | 
|  | 248 | is_native_bridge = NativeBridgeIsPathSupported(library_path.c_str()); | 
| Dimitry Ivanov | d047c92 | 2016-02-23 14:23:51 -0800 | [diff] [blame] | 249 | } | 
| Dimitry Ivanov | ac1b191 | 2015-12-01 13:56:44 -0800 | [diff] [blame] | 250 |  | 
| Jiyong Park | a07f305 | 2017-08-22 10:26:10 +0900 | [diff] [blame] | 251 | std::string system_exposed_libraries = system_public_libraries_; | 
|  | 252 | const char* namespace_name = kClassloaderNamespaceName; | 
|  | 253 | android_namespace_t* vndk_ns = nullptr; | 
| Kiyoung Kim | 4639f69 | 2019-02-20 18:04:39 +0900 | [diff] [blame] | 254 | if ((apk_origin == APK_ORIGIN_VENDOR || | 
|  | 255 | (apk_origin == APK_ORIGIN_PRODUCT && target_sdk_version > 29)) && | 
|  | 256 | !is_shared) { | 
|  | 257 | LOG_FATAL_IF(is_native_bridge, | 
|  | 258 | "Unbundled vendor / product apk must not use translated architecture"); | 
| Jiyong Park | a07f305 | 2017-08-22 10:26:10 +0900 | [diff] [blame] | 259 |  | 
| Kiyoung Kim | 4639f69 | 2019-02-20 18:04:39 +0900 | [diff] [blame] | 260 | // For vendor / product apks, give access to the vendor / product lib even though | 
| Jiyong Park | a07f305 | 2017-08-22 10:26:10 +0900 | [diff] [blame] | 261 | // they are treated as unbundled; the libs and apks are still bundled | 
| Kiyoung Kim | 4639f69 | 2019-02-20 18:04:39 +0900 | [diff] [blame] | 262 | // together in the vendor / product partition. | 
|  | 263 | const char* origin_partition; | 
|  | 264 | const char* origin_lib_path; | 
|  | 265 |  | 
|  | 266 | switch (apk_origin) { | 
|  | 267 | case APK_ORIGIN_VENDOR: | 
|  | 268 | origin_partition = "vendor"; | 
|  | 269 | origin_lib_path = kVendorLibPath; | 
|  | 270 | break; | 
|  | 271 | case APK_ORIGIN_PRODUCT: | 
|  | 272 | origin_partition = "product"; | 
|  | 273 | origin_lib_path = kProductLibPath; | 
|  | 274 | break; | 
|  | 275 | default: | 
|  | 276 | origin_partition = "unknown"; | 
|  | 277 | origin_lib_path = ""; | 
|  | 278 | } | 
|  | 279 |  | 
|  | 280 | LOG_FATAL_IF(is_native_bridge, "Unbundled %s apk must not use translated architecture", | 
|  | 281 | origin_partition); | 
|  | 282 |  | 
|  | 283 | library_path = library_path + ":" + origin_lib_path; | 
|  | 284 | permitted_path = permitted_path + ":" + origin_lib_path; | 
| Jiyong Park | a07f305 | 2017-08-22 10:26:10 +0900 | [diff] [blame] | 285 |  | 
|  | 286 | // Also give access to LLNDK libraries since they are available to vendors | 
|  | 287 | system_exposed_libraries = system_exposed_libraries + ":" + system_llndk_libraries_.c_str(); | 
|  | 288 |  | 
|  | 289 | // Give access to VNDK-SP libraries from the 'vndk' namespace. | 
|  | 290 | vndk_ns = android_get_exported_namespace(kVndkNamespaceName); | 
| Kiyoung Kim | 23ff8eb | 2019-03-21 16:56:05 +0900 | [diff] [blame] | 291 | if (vndk_ns == nullptr) { | 
|  | 292 | ALOGW("Cannot find \"%s\" namespace for %s apks", kVndkNamespaceName, origin_partition); | 
|  | 293 | } | 
| Jiyong Park | a07f305 | 2017-08-22 10:26:10 +0900 | [diff] [blame] | 294 |  | 
|  | 295 | // Different name is useful for debugging | 
|  | 296 | namespace_name = kVendorClassloaderNamespaceName; | 
| Kiyoung Kim | 4639f69 | 2019-02-20 18:04:39 +0900 | [diff] [blame] | 297 | ALOGD("classloader namespace configured for unbundled %s apk. library_path=%s", | 
|  | 298 | origin_partition, library_path.c_str()); | 
| Inseob Kim | 67cb056 | 2018-05-04 11:39:12 +0900 | [diff] [blame] | 299 | } else { | 
|  | 300 | // oem and product public libraries are NOT available to vendor apks, otherwise it | 
| Jiyong Park | e031994 | 2018-01-15 21:14:53 +0900 | [diff] [blame] | 301 | // would be system->vendor violation. | 
| Inseob Kim | 67cb056 | 2018-05-04 11:39:12 +0900 | [diff] [blame] | 302 | if (!oem_public_libraries_.empty()) { | 
|  | 303 | system_exposed_libraries = system_exposed_libraries + ':' + oem_public_libraries_; | 
|  | 304 | } | 
|  | 305 | if (!product_public_libraries_.empty()) { | 
|  | 306 | system_exposed_libraries = system_exposed_libraries + ':' + product_public_libraries_; | 
|  | 307 | } | 
| Jiyong Park | a07f305 | 2017-08-22 10:26:10 +0900 | [diff] [blame] | 308 | } | 
|  | 309 |  | 
| Victor Chang | 7a20a90 | 2019-01-28 18:43:24 +0000 | [diff] [blame] | 310 | std::string runtime_exposed_libraries = base::Join(kRuntimePublicLibraries, ":"); | 
|  | 311 |  | 
| Zhenhua WANG | f2804e5 | 2016-05-30 11:16:08 +0800 | [diff] [blame] | 312 | NativeLoaderNamespace native_loader_ns; | 
|  | 313 | if (!is_native_bridge) { | 
| Martin Stjernholm | 7888b5c | 2019-02-23 02:10:14 +0000 | [diff] [blame] | 314 | android_namespace_t* android_parent_ns; | 
|  | 315 | if (parent_ns != nullptr) { | 
|  | 316 | android_parent_ns = parent_ns->get_android_ns(); | 
|  | 317 | } else { | 
|  | 318 | // Fall back to the platform namespace if no parent is found. It is | 
|  | 319 | // called "default" for binaries in /system and "platform" for those in | 
|  | 320 | // the Runtime APEX. Try "platform" first since "default" always exists. | 
|  | 321 | android_parent_ns = android_get_exported_namespace(kPlatformNamespaceName); | 
|  | 322 | if (android_parent_ns == nullptr) { | 
|  | 323 | android_parent_ns = android_get_exported_namespace(kDefaultNamespaceName); | 
|  | 324 | } | 
|  | 325 | } | 
|  | 326 |  | 
| Jiyong Park | a07f305 | 2017-08-22 10:26:10 +0900 | [diff] [blame] | 327 | android_namespace_t* ns = android_create_namespace(namespace_name, | 
| Zhenhua WANG | f2804e5 | 2016-05-30 11:16:08 +0800 | [diff] [blame] | 328 | nullptr, | 
|  | 329 | library_path.c_str(), | 
|  | 330 | namespace_type, | 
|  | 331 | permitted_path.c_str(), | 
| Victor Khimenko | 1443ec4 | 2018-07-09 17:01:22 +0200 | [diff] [blame] | 332 | android_parent_ns); | 
| Zhenhua WANG | f2804e5 | 2016-05-30 11:16:08 +0800 | [diff] [blame] | 333 | if (ns == nullptr) { | 
|  | 334 | *error_msg = dlerror(); | 
| Victor Khimenko | 1443ec4 | 2018-07-09 17:01:22 +0200 | [diff] [blame] | 335 | return nullptr; | 
| Zhenhua WANG | f2804e5 | 2016-05-30 11:16:08 +0800 | [diff] [blame] | 336 | } | 
|  | 337 |  | 
| Dimitry Ivanov | 882cad2 | 2017-05-01 15:12:49 -0700 | [diff] [blame] | 338 | // Note that when vendor_ns is not configured this function will return nullptr | 
|  | 339 | // and it will result in linking vendor_public_libraries_ to the default namespace | 
|  | 340 | // which is expected behavior in this case. | 
|  | 341 | android_namespace_t* vendor_ns = android_get_exported_namespace(kVendorNamespaceName); | 
|  | 342 |  | 
| Victor Chang | 7a20a90 | 2019-01-28 18:43:24 +0000 | [diff] [blame] | 343 | android_namespace_t* runtime_ns = android_get_exported_namespace(kRuntimeNamespaceName); | 
|  | 344 |  | 
| Jiyong Park | a07f305 | 2017-08-22 10:26:10 +0900 | [diff] [blame] | 345 | if (!android_link_namespaces(ns, nullptr, system_exposed_libraries.c_str())) { | 
| Dimitry Ivanov | 26e1a84 | 2017-02-03 14:11:27 -0800 | [diff] [blame] | 346 | *error_msg = dlerror(); | 
| Victor Khimenko | 1443ec4 | 2018-07-09 17:01:22 +0200 | [diff] [blame] | 347 | return nullptr; | 
| Dimitry Ivanov | 26e1a84 | 2017-02-03 14:11:27 -0800 | [diff] [blame] | 348 | } | 
|  | 349 |  | 
| Victor Chang | 7a20a90 | 2019-01-28 18:43:24 +0000 | [diff] [blame] | 350 | // Runtime apex does not exist in host, and under certain build conditions. | 
|  | 351 | if (runtime_ns != nullptr) { | 
|  | 352 | if (!android_link_namespaces(ns, runtime_ns, runtime_exposed_libraries.c_str())) { | 
|  | 353 | *error_msg = dlerror(); | 
|  | 354 | return nullptr; | 
|  | 355 | } | 
|  | 356 | } | 
|  | 357 |  | 
| Jiyong Park | a07f305 | 2017-08-22 10:26:10 +0900 | [diff] [blame] | 358 | if (vndk_ns != nullptr && !system_vndksp_libraries_.empty()) { | 
|  | 359 | // vendor apks are allowed to use VNDK-SP libraries. | 
|  | 360 | if (!android_link_namespaces(ns, vndk_ns, system_vndksp_libraries_.c_str())) { | 
|  | 361 | *error_msg = dlerror(); | 
| Victor Khimenko | 1443ec4 | 2018-07-09 17:01:22 +0200 | [diff] [blame] | 362 | return nullptr; | 
| Jiyong Park | a07f305 | 2017-08-22 10:26:10 +0900 | [diff] [blame] | 363 | } | 
|  | 364 | } | 
|  | 365 |  | 
| Dimitry Ivanov | 882cad2 | 2017-05-01 15:12:49 -0700 | [diff] [blame] | 366 | if (!vendor_public_libraries_.empty()) { | 
|  | 367 | if (!android_link_namespaces(ns, vendor_ns, vendor_public_libraries_.c_str())) { | 
|  | 368 | *error_msg = dlerror(); | 
| Victor Khimenko | 1443ec4 | 2018-07-09 17:01:22 +0200 | [diff] [blame] | 369 | return nullptr; | 
| Dimitry Ivanov | 882cad2 | 2017-05-01 15:12:49 -0700 | [diff] [blame] | 370 | } | 
|  | 371 | } | 
|  | 372 |  | 
| Zhenhua WANG | f2804e5 | 2016-05-30 11:16:08 +0800 | [diff] [blame] | 373 | native_loader_ns = NativeLoaderNamespace(ns); | 
|  | 374 | } else { | 
| Martin Stjernholm | 7888b5c | 2019-02-23 02:10:14 +0000 | [diff] [blame] | 375 | native_bridge_namespace_t* native_bridge_parent_namespace; | 
|  | 376 | if (parent_ns != nullptr) { | 
|  | 377 | native_bridge_parent_namespace = parent_ns->get_native_bridge_ns(); | 
|  | 378 | } else { | 
|  | 379 | native_bridge_parent_namespace = NativeBridgeGetExportedNamespace(kPlatformNamespaceName); | 
|  | 380 | if (native_bridge_parent_namespace == nullptr) { | 
|  | 381 | native_bridge_parent_namespace = NativeBridgeGetExportedNamespace(kDefaultNamespaceName); | 
|  | 382 | } | 
|  | 383 | } | 
|  | 384 |  | 
| Jiyong Park | a07f305 | 2017-08-22 10:26:10 +0900 | [diff] [blame] | 385 | native_bridge_namespace_t* ns = NativeBridgeCreateNamespace(namespace_name, | 
| Zhenhua WANG | f2804e5 | 2016-05-30 11:16:08 +0800 | [diff] [blame] | 386 | nullptr, | 
|  | 387 | library_path.c_str(), | 
|  | 388 | namespace_type, | 
|  | 389 | permitted_path.c_str(), | 
| Victor Khimenko | 1443ec4 | 2018-07-09 17:01:22 +0200 | [diff] [blame] | 390 | native_bridge_parent_namespace); | 
| Zhenhua WANG | f2804e5 | 2016-05-30 11:16:08 +0800 | [diff] [blame] | 391 | if (ns == nullptr) { | 
|  | 392 | *error_msg = NativeBridgeGetError(); | 
| Victor Khimenko | 1443ec4 | 2018-07-09 17:01:22 +0200 | [diff] [blame] | 393 | return nullptr; | 
| Zhenhua WANG | f2804e5 | 2016-05-30 11:16:08 +0800 | [diff] [blame] | 394 | } | 
|  | 395 |  | 
| dimitry | d2ace38 | 2019-02-04 15:06:43 +0100 | [diff] [blame] | 396 | native_bridge_namespace_t* vendor_ns = NativeBridgeGetExportedNamespace(kVendorNamespaceName); | 
| Victor Chang | 7a20a90 | 2019-01-28 18:43:24 +0000 | [diff] [blame] | 397 | native_bridge_namespace_t* runtime_ns = | 
|  | 398 | NativeBridgeGetExportedNamespace(kRuntimeNamespaceName); | 
| Dimitry Ivanov | 882cad2 | 2017-05-01 15:12:49 -0700 | [diff] [blame] | 399 |  | 
| Jiyong Park | a07f305 | 2017-08-22 10:26:10 +0900 | [diff] [blame] | 400 | if (!NativeBridgeLinkNamespaces(ns, nullptr, system_exposed_libraries.c_str())) { | 
| Zhenhua WANG | e8fb11d | 2017-02-27 10:14:45 +0800 | [diff] [blame] | 401 | *error_msg = NativeBridgeGetError(); | 
| Victor Khimenko | 1443ec4 | 2018-07-09 17:01:22 +0200 | [diff] [blame] | 402 | return nullptr; | 
| Zhenhua WANG | e8fb11d | 2017-02-27 10:14:45 +0800 | [diff] [blame] | 403 | } | 
|  | 404 |  | 
| Victor Chang | 7a20a90 | 2019-01-28 18:43:24 +0000 | [diff] [blame] | 405 | // Runtime apex does not exist in host, and under certain build conditions. | 
|  | 406 | if (runtime_ns != nullptr) { | 
|  | 407 | if (!NativeBridgeLinkNamespaces(ns, runtime_ns, runtime_exposed_libraries.c_str())) { | 
|  | 408 | *error_msg = NativeBridgeGetError(); | 
|  | 409 | return nullptr; | 
|  | 410 | } | 
|  | 411 | } | 
| Dimitry Ivanov | 882cad2 | 2017-05-01 15:12:49 -0700 | [diff] [blame] | 412 | if (!vendor_public_libraries_.empty()) { | 
|  | 413 | if (!NativeBridgeLinkNamespaces(ns, vendor_ns, vendor_public_libraries_.c_str())) { | 
|  | 414 | *error_msg = NativeBridgeGetError(); | 
| Victor Khimenko | 1443ec4 | 2018-07-09 17:01:22 +0200 | [diff] [blame] | 415 | return nullptr; | 
| Dimitry Ivanov | 882cad2 | 2017-05-01 15:12:49 -0700 | [diff] [blame] | 416 | } | 
|  | 417 | } | 
|  | 418 |  | 
| Zhenhua WANG | f2804e5 | 2016-05-30 11:16:08 +0800 | [diff] [blame] | 419 | native_loader_ns = NativeLoaderNamespace(ns); | 
|  | 420 | } | 
|  | 421 |  | 
|  | 422 | namespaces_.push_back(std::make_pair(env->NewWeakGlobalRef(class_loader), native_loader_ns)); | 
|  | 423 |  | 
| Victor Khimenko | 1443ec4 | 2018-07-09 17:01:22 +0200 | [diff] [blame] | 424 | return &(namespaces_.back().second); | 
| Dimitry Ivanov | ac1b191 | 2015-12-01 13:56:44 -0800 | [diff] [blame] | 425 | } | 
|  | 426 |  | 
| Victor Khimenko | 1443ec4 | 2018-07-09 17:01:22 +0200 | [diff] [blame] | 427 | NativeLoaderNamespace* FindNamespaceByClassLoader(JNIEnv* env, jobject class_loader) { | 
| Dimitry Ivanov | f44ecde | 2016-02-22 13:48:22 -0800 | [diff] [blame] | 428 | auto it = std::find_if(namespaces_.begin(), namespaces_.end(), | 
| Zhenhua WANG | f2804e5 | 2016-05-30 11:16:08 +0800 | [diff] [blame] | 429 | [&](const std::pair<jweak, NativeLoaderNamespace>& value) { | 
| Dimitry Ivanov | f44ecde | 2016-02-22 13:48:22 -0800 | [diff] [blame] | 430 | return env->IsSameObject(value.first, class_loader); | 
|  | 431 | }); | 
| Zhenhua WANG | f2804e5 | 2016-05-30 11:16:08 +0800 | [diff] [blame] | 432 | if (it != namespaces_.end()) { | 
| Victor Khimenko | 1443ec4 | 2018-07-09 17:01:22 +0200 | [diff] [blame] | 433 | return &it->second; | 
| Zhenhua WANG | f2804e5 | 2016-05-30 11:16:08 +0800 | [diff] [blame] | 434 | } | 
|  | 435 |  | 
| Victor Khimenko | 1443ec4 | 2018-07-09 17:01:22 +0200 | [diff] [blame] | 436 | return nullptr; | 
| Dimitry Ivanov | f44ecde | 2016-02-22 13:48:22 -0800 | [diff] [blame] | 437 | } | 
|  | 438 |  | 
| Dimitry Ivanov | 4b0e963 | 2016-03-15 13:51:26 -0700 | [diff] [blame] | 439 | void Initialize() { | 
| Dimitry Ivanov | 80ddb8f | 2016-05-09 18:12:00 -0700 | [diff] [blame] | 440 | // Once public namespace is initialized there is no | 
|  | 441 | // point in running this code - it will have no effect | 
|  | 442 | // on the current list of public libraries. | 
|  | 443 | if (initialized_) { | 
|  | 444 | return; | 
|  | 445 | } | 
|  | 446 |  | 
| Dimitry Ivanov | b614045 | 2016-04-06 18:24:08 -0700 | [diff] [blame] | 447 | std::vector<std::string> sonames; | 
| Dimitry Ivanov | 0b5651e | 2016-04-21 16:42:48 -0700 | [diff] [blame] | 448 | const char* android_root_env = getenv("ANDROID_ROOT"); | 
|  | 449 | std::string root_dir = android_root_env != nullptr ? android_root_env : "/system"; | 
|  | 450 | std::string public_native_libraries_system_config = | 
|  | 451 | root_dir + kPublicNativeLibrariesSystemConfigPathFromRoot; | 
| Jiyong Park | a07f305 | 2017-08-22 10:26:10 +0900 | [diff] [blame] | 452 | std::string llndk_native_libraries_system_config = | 
|  | 453 | root_dir + kLlndkNativeLibrariesSystemConfigPathFromRoot; | 
|  | 454 | std::string vndksp_native_libraries_system_config = | 
|  | 455 | root_dir + kVndkspNativeLibrariesSystemConfigPathFromRoot; | 
| Dimitry Ivanov | b614045 | 2016-04-06 18:24:08 -0700 | [diff] [blame] | 456 |  | 
| Inseob Kim | 67cb056 | 2018-05-04 11:39:12 +0900 | [diff] [blame] | 457 | std::string product_public_native_libraries_dir = "/product/etc"; | 
|  | 458 |  | 
| Christopher Ferris | 39da84b | 2016-06-21 16:11:23 -0700 | [diff] [blame] | 459 | std::string error_msg; | 
| Jiyong Park | d1006fe | 2017-11-08 18:44:09 +0900 | [diff] [blame] | 460 | LOG_ALWAYS_FATAL_IF( | 
|  | 461 | !ReadConfig(public_native_libraries_system_config, &sonames, always_true, &error_msg), | 
|  | 462 | "Error reading public native library list from \"%s\": %s", | 
|  | 463 | public_native_libraries_system_config.c_str(), error_msg.c_str()); | 
|  | 464 |  | 
| Jiyong Park | e031994 | 2018-01-15 21:14:53 +0900 | [diff] [blame] | 465 | // For debuggable platform builds use ANDROID_ADDITIONAL_PUBLIC_LIBRARIES environment | 
|  | 466 | // variable to add libraries to the list. This is intended for platform tests only. | 
|  | 467 | if (is_debuggable()) { | 
|  | 468 | const char* additional_libs = getenv("ANDROID_ADDITIONAL_PUBLIC_LIBRARIES"); | 
|  | 469 | if (additional_libs != nullptr && additional_libs[0] != '\0') { | 
|  | 470 | std::vector<std::string> additional_libs_vector = base::Split(additional_libs, ":"); | 
|  | 471 | std::copy(additional_libs_vector.begin(), additional_libs_vector.end(), | 
|  | 472 | std::back_inserter(sonames)); | 
|  | 473 | } | 
|  | 474 | } | 
|  | 475 |  | 
| Victor Chang | f70a2fe | 2019-02-01 20:01:27 +0000 | [diff] [blame] | 476 | // Remove the public libs in the runtime namespace. | 
|  | 477 | // These libs are listed in public.android.txt, but we don't want the rest of android | 
|  | 478 | // in default namespace to dlopen the libs. | 
|  | 479 | // For example, libicuuc.so is exposed to classloader namespace from runtime namespace. | 
|  | 480 | // Unfortunately, it does not have stable C symbols, and default namespace should only use | 
|  | 481 | // stable symbols in libandroidicu.so. http://b/120786417 | 
|  | 482 | removePublicLibsIfExistsInRuntimeApex(sonames); | 
|  | 483 |  | 
| Jiyong Park | e031994 | 2018-01-15 21:14:53 +0900 | [diff] [blame] | 484 | // android_init_namespaces() expects all the public libraries | 
|  | 485 | // to be loaded so that they can be found by soname alone. | 
|  | 486 | // | 
|  | 487 | // TODO(dimitry): this is a bit misleading since we do not know | 
|  | 488 | // if the vendor public library is going to be opened from /vendor/lib | 
| Inseob Kim | 67cb056 | 2018-05-04 11:39:12 +0900 | [diff] [blame] | 489 | // we might as well end up loading them from /system/lib or /product/lib | 
| Jiyong Park | e031994 | 2018-01-15 21:14:53 +0900 | [diff] [blame] | 490 | // For now we rely on CTS test to catch things like this but | 
|  | 491 | // it should probably be addressed in the future. | 
|  | 492 | for (const auto& soname : sonames) { | 
|  | 493 | LOG_ALWAYS_FATAL_IF(dlopen(soname.c_str(), RTLD_NOW | RTLD_NODELETE) == nullptr, | 
|  | 494 | "Error preloading public library %s: %s", soname.c_str(), dlerror()); | 
|  | 495 | } | 
|  | 496 |  | 
|  | 497 | system_public_libraries_ = base::Join(sonames, ':'); | 
|  | 498 |  | 
| Jiyong Park | d1006fe | 2017-11-08 18:44:09 +0900 | [diff] [blame] | 499 | // read /system/etc/public.libraries-<companyname>.txt which contain partner defined | 
|  | 500 | // system libs that are exposed to apps. The libs in the txt files must be | 
|  | 501 | // named as lib<name>.<companyname>.so. | 
| Jiyong Park | e031994 | 2018-01-15 21:14:53 +0900 | [diff] [blame] | 502 | sonames.clear(); | 
| Inseob Kim | 67cb056 | 2018-05-04 11:39:12 +0900 | [diff] [blame] | 503 | ReadExtensionLibraries(base::Dirname(public_native_libraries_system_config).c_str(), &sonames); | 
| Jiyong Park | e031994 | 2018-01-15 21:14:53 +0900 | [diff] [blame] | 504 | oem_public_libraries_ = base::Join(sonames, ':'); | 
| Dimitry Ivanov | 7d02829 | 2016-05-05 17:30:24 -0700 | [diff] [blame] | 505 |  | 
| Inseob Kim | 67cb056 | 2018-05-04 11:39:12 +0900 | [diff] [blame] | 506 | // read /product/etc/public.libraries-<companyname>.txt which contain partner defined | 
|  | 507 | // product libs that are exposed to apps. | 
|  | 508 | sonames.clear(); | 
|  | 509 | ReadExtensionLibraries(product_public_native_libraries_dir.c_str(), &sonames); | 
|  | 510 | product_public_libraries_ = base::Join(sonames, ':'); | 
|  | 511 |  | 
| Justin Yun | 4a1d110 | 2017-11-27 17:04:14 +0900 | [diff] [blame] | 512 | // Insert VNDK version to llndk and vndksp config file names. | 
|  | 513 | insert_vndk_version_str(&llndk_native_libraries_system_config); | 
|  | 514 | insert_vndk_version_str(&vndksp_native_libraries_system_config); | 
|  | 515 |  | 
| Dimitry Ivanov | 882cad2 | 2017-05-01 15:12:49 -0700 | [diff] [blame] | 516 | sonames.clear(); | 
| Jiyong Park | d1006fe | 2017-11-08 18:44:09 +0900 | [diff] [blame] | 517 | ReadConfig(llndk_native_libraries_system_config, &sonames, always_true); | 
| Jiyong Park | a07f305 | 2017-08-22 10:26:10 +0900 | [diff] [blame] | 518 | system_llndk_libraries_ = base::Join(sonames, ':'); | 
|  | 519 |  | 
|  | 520 | sonames.clear(); | 
| Jiyong Park | d1006fe | 2017-11-08 18:44:09 +0900 | [diff] [blame] | 521 | ReadConfig(vndksp_native_libraries_system_config, &sonames, always_true); | 
| Jiyong Park | a07f305 | 2017-08-22 10:26:10 +0900 | [diff] [blame] | 522 | system_vndksp_libraries_ = base::Join(sonames, ':'); | 
|  | 523 |  | 
|  | 524 | sonames.clear(); | 
| Dimitry Ivanov | 882cad2 | 2017-05-01 15:12:49 -0700 | [diff] [blame] | 525 | // This file is optional, quietly ignore if the file does not exist. | 
| Jiyong Park | d1006fe | 2017-11-08 18:44:09 +0900 | [diff] [blame] | 526 | ReadConfig(kPublicNativeLibrariesVendorConfig, &sonames, always_true, nullptr); | 
| Dimitry Ivanov | 882cad2 | 2017-05-01 15:12:49 -0700 | [diff] [blame] | 527 |  | 
|  | 528 | vendor_public_libraries_ = base::Join(sonames, ':'); | 
| Dimitry Ivanov | b614045 | 2016-04-06 18:24:08 -0700 | [diff] [blame] | 529 | } | 
|  | 530 |  | 
| Inseob Kim | 67cb056 | 2018-05-04 11:39:12 +0900 | [diff] [blame] | 531 | void Reset() { namespaces_.clear(); } | 
| Dimitry Ivanov | be4ca3a | 2016-05-02 10:43:16 -0700 | [diff] [blame] | 532 |  | 
| Dimitry Ivanov | b614045 | 2016-04-06 18:24:08 -0700 | [diff] [blame] | 533 | private: | 
| Inseob Kim | 67cb056 | 2018-05-04 11:39:12 +0900 | [diff] [blame] | 534 | void ReadExtensionLibraries(const char* dirname, std::vector<std::string>* sonames) { | 
|  | 535 | std::unique_ptr<DIR, decltype(&closedir)> dir(opendir(dirname), closedir); | 
|  | 536 | if (dir != nullptr) { | 
|  | 537 | // Failing to opening the dir is not an error, which can happen in | 
|  | 538 | // webview_zygote. | 
|  | 539 | while (struct dirent* ent = readdir(dir.get())) { | 
|  | 540 | if (ent->d_type != DT_REG && ent->d_type != DT_LNK) { | 
|  | 541 | continue; | 
|  | 542 | } | 
|  | 543 | const std::string filename(ent->d_name); | 
|  | 544 | if (android::base::StartsWith(filename, kPublicNativeLibrariesExtensionConfigPrefix) && | 
|  | 545 | android::base::EndsWith(filename, kPublicNativeLibrariesExtensionConfigSuffix)) { | 
|  | 546 | const size_t start = kPublicNativeLibrariesExtensionConfigPrefixLen; | 
|  | 547 | const size_t end = filename.size() - kPublicNativeLibrariesExtensionConfigSuffixLen; | 
|  | 548 | const std::string company_name = filename.substr(start, end - start); | 
|  | 549 | const std::string config_file_path = dirname + "/"s + filename; | 
|  | 550 | LOG_ALWAYS_FATAL_IF( | 
|  | 551 | company_name.empty(), | 
|  | 552 | "Error extracting company name from public native library list file path \"%s\"", | 
|  | 553 | config_file_path.c_str()); | 
|  | 554 |  | 
|  | 555 | std::string error_msg; | 
|  | 556 |  | 
|  | 557 | LOG_ALWAYS_FATAL_IF( | 
|  | 558 | !ReadConfig( | 
|  | 559 | config_file_path, sonames, | 
|  | 560 | [&company_name](const std::string& soname, std::string* error_msg) { | 
|  | 561 | if (android::base::StartsWith(soname, "lib") && | 
|  | 562 | android::base::EndsWith(soname, "." + company_name + ".so")) { | 
|  | 563 | return true; | 
|  | 564 | } else { | 
|  | 565 | *error_msg = "Library name \"" + soname + | 
|  | 566 | "\" does not end with the company name: " + company_name + "."; | 
|  | 567 | return false; | 
|  | 568 | } | 
|  | 569 | }, | 
|  | 570 | &error_msg), | 
|  | 571 | "Error reading public native library list from \"%s\": %s", config_file_path.c_str(), | 
|  | 572 | error_msg.c_str()); | 
|  | 573 | } | 
|  | 574 | } | 
|  | 575 | } | 
|  | 576 | } | 
|  | 577 |  | 
| Victor Chang | f70a2fe | 2019-02-01 20:01:27 +0000 | [diff] [blame] | 578 | /** | 
|  | 579 | * Remove the public libs in runtime namespace | 
|  | 580 | */ | 
|  | 581 | void removePublicLibsIfExistsInRuntimeApex(std::vector<std::string>& sonames) { | 
|  | 582 | for (const std::string& lib_name : kRuntimePublicLibraries) { | 
|  | 583 | std::string path(kRuntimeApexLibPath); | 
|  | 584 | path.append("/").append(lib_name); | 
|  | 585 |  | 
|  | 586 | struct stat s; | 
|  | 587 | // Do nothing if the path in /apex does not exist. | 
|  | 588 | // Runtime APEX must be mounted since libnativeloader is in the same APEX | 
|  | 589 | if (stat(path.c_str(), &s) != 0) { | 
|  | 590 | continue; | 
|  | 591 | } | 
|  | 592 |  | 
|  | 593 | auto it = std::find(sonames.begin(), sonames.end(), lib_name); | 
|  | 594 | if (it != sonames.end()) { | 
|  | 595 | sonames.erase(it); | 
|  | 596 | } | 
|  | 597 | } | 
|  | 598 | } | 
| Inseob Kim | 67cb056 | 2018-05-04 11:39:12 +0900 | [diff] [blame] | 599 |  | 
| Christopher Ferris | 39da84b | 2016-06-21 16:11:23 -0700 | [diff] [blame] | 600 | bool ReadConfig(const std::string& configFile, std::vector<std::string>* sonames, | 
| Jiyong Park | d1006fe | 2017-11-08 18:44:09 +0900 | [diff] [blame] | 601 | const std::function<bool(const std::string& /* soname */, | 
|  | 602 | std::string* /* error_msg */)>& check_soname, | 
| Christopher Ferris | 39da84b | 2016-06-21 16:11:23 -0700 | [diff] [blame] | 603 | std::string* error_msg = nullptr) { | 
| Dimitry Ivanov | 4b0e963 | 2016-03-15 13:51:26 -0700 | [diff] [blame] | 604 | // Read list of public native libraries from the config file. | 
|  | 605 | std::string file_content; | 
| Dimitry Ivanov | b614045 | 2016-04-06 18:24:08 -0700 | [diff] [blame] | 606 | if(!base::ReadFileToString(configFile, &file_content)) { | 
| Christopher Ferris | 39da84b | 2016-06-21 16:11:23 -0700 | [diff] [blame] | 607 | if (error_msg) *error_msg = strerror(errno); | 
| Dimitry Ivanov | b614045 | 2016-04-06 18:24:08 -0700 | [diff] [blame] | 608 | return false; | 
|  | 609 | } | 
| Dimitry Ivanov | 4b0e963 | 2016-03-15 13:51:26 -0700 | [diff] [blame] | 610 |  | 
|  | 611 | std::vector<std::string> lines = base::Split(file_content, "\n"); | 
|  | 612 |  | 
| Christopher Ferris | 39da84b | 2016-06-21 16:11:23 -0700 | [diff] [blame] | 613 | for (auto& line : lines) { | 
| Dimitry Ivanov | 4b0e963 | 2016-03-15 13:51:26 -0700 | [diff] [blame] | 614 | auto trimmed_line = base::Trim(line); | 
|  | 615 | if (trimmed_line[0] == '#' || trimmed_line.empty()) { | 
|  | 616 | continue; | 
|  | 617 | } | 
| Christopher Ferris | 39da84b | 2016-06-21 16:11:23 -0700 | [diff] [blame] | 618 | size_t space_pos = trimmed_line.rfind(' '); | 
|  | 619 | if (space_pos != std::string::npos) { | 
|  | 620 | std::string type = trimmed_line.substr(space_pos + 1); | 
|  | 621 | if (type != "32" && type != "64") { | 
|  | 622 | if (error_msg) *error_msg = "Malformed line: " + line; | 
|  | 623 | return false; | 
|  | 624 | } | 
|  | 625 | #if defined(__LP64__) | 
|  | 626 | // Skip 32 bit public library. | 
|  | 627 | if (type == "32") { | 
|  | 628 | continue; | 
|  | 629 | } | 
|  | 630 | #else | 
|  | 631 | // Skip 64 bit public library. | 
|  | 632 | if (type == "64") { | 
|  | 633 | continue; | 
|  | 634 | } | 
|  | 635 | #endif | 
|  | 636 | trimmed_line.resize(space_pos); | 
|  | 637 | } | 
| Dimitry Ivanov | 4b0e963 | 2016-03-15 13:51:26 -0700 | [diff] [blame] | 638 |  | 
| Jiyong Park | d1006fe | 2017-11-08 18:44:09 +0900 | [diff] [blame] | 639 | if (check_soname(trimmed_line, error_msg)) { | 
|  | 640 | sonames->push_back(trimmed_line); | 
|  | 641 | } else { | 
|  | 642 | return false; | 
|  | 643 | } | 
| Dimitry Ivanov | 4b0e963 | 2016-03-15 13:51:26 -0700 | [diff] [blame] | 644 | } | 
|  | 645 |  | 
| Dimitry Ivanov | b614045 | 2016-04-06 18:24:08 -0700 | [diff] [blame] | 646 | return true; | 
| Dimitry Ivanov | d68c8e9 | 2016-02-10 14:09:22 -0800 | [diff] [blame] | 647 | } | 
| Dimitry Ivanov | ac1b191 | 2015-12-01 13:56:44 -0800 | [diff] [blame] | 648 |  | 
| Zhenhua WANG | f2804e5 | 2016-05-30 11:16:08 +0800 | [diff] [blame] | 649 | bool InitPublicNamespace(const char* library_path, std::string* error_msg) { | 
|  | 650 | // Ask native bride if this apps library path should be handled by it | 
|  | 651 | bool is_native_bridge = NativeBridgeIsPathSupported(library_path); | 
|  | 652 |  | 
| Dimitry Ivanov | 4b0e963 | 2016-03-15 13:51:26 -0700 | [diff] [blame] | 653 | // (http://b/25844435) - Some apps call dlopen from generated code (mono jited | 
|  | 654 | // code is one example) unknown to linker in which  case linker uses anonymous | 
|  | 655 | // namespace. The second argument specifies the search path for the anonymous | 
|  | 656 | // namespace which is the library_path of the classloader. | 
| Dimitry Ivanov | 882cad2 | 2017-05-01 15:12:49 -0700 | [diff] [blame] | 657 | initialized_ = android_init_anonymous_namespace(system_public_libraries_.c_str(), | 
| Dimitry Ivanov | 26e1a84 | 2017-02-03 14:11:27 -0800 | [diff] [blame] | 658 | is_native_bridge ? nullptr : library_path); | 
| Dimitry Ivanov | d836ab0 | 2016-11-02 18:03:10 -0700 | [diff] [blame] | 659 | if (!initialized_) { | 
|  | 660 | *error_msg = dlerror(); | 
|  | 661 | return false; | 
|  | 662 | } | 
|  | 663 |  | 
|  | 664 | // and now initialize native bridge namespaces if necessary. | 
|  | 665 | if (NativeBridgeInitialized()) { | 
| Dimitry Ivanov | 882cad2 | 2017-05-01 15:12:49 -0700 | [diff] [blame] | 666 | initialized_ = NativeBridgeInitAnonymousNamespace(system_public_libraries_.c_str(), | 
| Zhenhua WANG | e8fb11d | 2017-02-27 10:14:45 +0800 | [diff] [blame] | 667 | is_native_bridge ? library_path : nullptr); | 
| Zhenhua WANG | f2804e5 | 2016-05-30 11:16:08 +0800 | [diff] [blame] | 668 | if (!initialized_) { | 
|  | 669 | *error_msg = NativeBridgeGetError(); | 
|  | 670 | } | 
|  | 671 | } | 
| Dimitry Ivanov | ac1b191 | 2015-12-01 13:56:44 -0800 | [diff] [blame] | 672 |  | 
|  | 673 | return initialized_; | 
|  | 674 | } | 
|  | 675 |  | 
| Dimitry Ivanov | 24db75c | 2016-05-12 15:34:41 -0700 | [diff] [blame] | 676 | jobject GetParentClassLoader(JNIEnv* env, jobject class_loader) { | 
|  | 677 | jclass class_loader_class = env->FindClass("java/lang/ClassLoader"); | 
|  | 678 | jmethodID get_parent = env->GetMethodID(class_loader_class, | 
|  | 679 | "getParent", | 
|  | 680 | "()Ljava/lang/ClassLoader;"); | 
|  | 681 |  | 
|  | 682 | return env->CallObjectMethod(class_loader, get_parent); | 
|  | 683 | } | 
|  | 684 |  | 
| Victor Khimenko | 1443ec4 | 2018-07-09 17:01:22 +0200 | [diff] [blame] | 685 | NativeLoaderNamespace* FindParentNamespaceByClassLoader(JNIEnv* env, jobject class_loader) { | 
| Dimitry Ivanov | 24db75c | 2016-05-12 15:34:41 -0700 | [diff] [blame] | 686 | jobject parent_class_loader = GetParentClassLoader(env, class_loader); | 
|  | 687 |  | 
|  | 688 | while (parent_class_loader != nullptr) { | 
| Victor Khimenko | 1443ec4 | 2018-07-09 17:01:22 +0200 | [diff] [blame] | 689 | NativeLoaderNamespace* ns; | 
|  | 690 | if ((ns = FindNamespaceByClassLoader(env, parent_class_loader)) != nullptr) { | 
|  | 691 | return ns; | 
| Dimitry Ivanov | 24db75c | 2016-05-12 15:34:41 -0700 | [diff] [blame] | 692 | } | 
|  | 693 |  | 
|  | 694 | parent_class_loader = GetParentClassLoader(env, parent_class_loader); | 
|  | 695 | } | 
| Zhenhua WANG | f2804e5 | 2016-05-30 11:16:08 +0800 | [diff] [blame] | 696 |  | 
| Victor Khimenko | 1443ec4 | 2018-07-09 17:01:22 +0200 | [diff] [blame] | 697 | return nullptr; | 
| Dimitry Ivanov | 24db75c | 2016-05-12 15:34:41 -0700 | [diff] [blame] | 698 | } | 
|  | 699 |  | 
| Kiyoung Kim | 4639f69 | 2019-02-20 18:04:39 +0900 | [diff] [blame] | 700 | ApkOrigin GetApkOriginFromDexPath(JNIEnv* env, jstring dex_path) { | 
|  | 701 | ApkOrigin apk_origin = APK_ORIGIN_DEFAULT; | 
|  | 702 |  | 
|  | 703 | if (dex_path != nullptr) { | 
|  | 704 | ScopedUtfChars dex_path_utf_chars(env, dex_path); | 
|  | 705 |  | 
|  | 706 | if (std::regex_search(dex_path_utf_chars.c_str(), kVendorDexPathRegex)) { | 
|  | 707 | apk_origin = APK_ORIGIN_VENDOR; | 
|  | 708 | } | 
|  | 709 |  | 
|  | 710 | if (std::regex_search(dex_path_utf_chars.c_str(), kProductDexPathRegex)) { | 
|  | 711 | LOG_ALWAYS_FATAL_IF(apk_origin == APK_ORIGIN_VENDOR, | 
|  | 712 | "Dex path contains both vendor and product partition : %s", | 
|  | 713 | dex_path_utf_chars.c_str()); | 
|  | 714 |  | 
|  | 715 | apk_origin = APK_ORIGIN_PRODUCT; | 
|  | 716 | } | 
|  | 717 | } | 
|  | 718 |  | 
|  | 719 | return apk_origin; | 
|  | 720 | } | 
|  | 721 |  | 
| Dimitry Ivanov | ac1b191 | 2015-12-01 13:56:44 -0800 | [diff] [blame] | 722 | bool initialized_; | 
| Victor Khimenko | 1443ec4 | 2018-07-09 17:01:22 +0200 | [diff] [blame] | 723 | std::list<std::pair<jweak, NativeLoaderNamespace>> namespaces_; | 
| Dimitry Ivanov | 882cad2 | 2017-05-01 15:12:49 -0700 | [diff] [blame] | 724 | std::string system_public_libraries_; | 
|  | 725 | std::string vendor_public_libraries_; | 
| Jiyong Park | e031994 | 2018-01-15 21:14:53 +0900 | [diff] [blame] | 726 | std::string oem_public_libraries_; | 
| Inseob Kim | 67cb056 | 2018-05-04 11:39:12 +0900 | [diff] [blame] | 727 | std::string product_public_libraries_; | 
| Jiyong Park | a07f305 | 2017-08-22 10:26:10 +0900 | [diff] [blame] | 728 | std::string system_llndk_libraries_; | 
|  | 729 | std::string system_vndksp_libraries_; | 
| Dimitry Ivanov | ac1b191 | 2015-12-01 13:56:44 -0800 | [diff] [blame] | 730 |  | 
|  | 731 | DISALLOW_COPY_AND_ASSIGN(LibraryNamespaces); | 
|  | 732 | }; | 
|  | 733 |  | 
| Dimitry Ivanov | 34d5a20 | 2016-02-29 13:21:43 -0800 | [diff] [blame] | 734 | static std::mutex g_namespaces_mutex; | 
| Dimitry Ivanov | ac1b191 | 2015-12-01 13:56:44 -0800 | [diff] [blame] | 735 | static LibraryNamespaces* g_namespaces = new LibraryNamespaces; | 
|  | 736 | #endif | 
|  | 737 |  | 
| Dimitry Ivanov | 4b0e963 | 2016-03-15 13:51:26 -0700 | [diff] [blame] | 738 | void InitializeNativeLoader() { | 
| Dimitry Ivanov | 426799d | 2016-02-22 11:27:48 -0800 | [diff] [blame] | 739 | #if defined(__ANDROID__) | 
| Dimitry Ivanov | 34d5a20 | 2016-02-29 13:21:43 -0800 | [diff] [blame] | 740 | std::lock_guard<std::mutex> guard(g_namespaces_mutex); | 
| Dimitry Ivanov | 4b0e963 | 2016-03-15 13:51:26 -0700 | [diff] [blame] | 741 | g_namespaces->Initialize(); | 
| Dimitry Ivanov | 426799d | 2016-02-22 11:27:48 -0800 | [diff] [blame] | 742 | #endif | 
|  | 743 | } | 
|  | 744 |  | 
| Dimitry Ivanov | be4ca3a | 2016-05-02 10:43:16 -0700 | [diff] [blame] | 745 | void ResetNativeLoader() { | 
|  | 746 | #if defined(__ANDROID__) | 
|  | 747 | std::lock_guard<std::mutex> guard(g_namespaces_mutex); | 
|  | 748 | g_namespaces->Reset(); | 
|  | 749 | #endif | 
|  | 750 | } | 
| Dimitry Ivanov | ac1b191 | 2015-12-01 13:56:44 -0800 | [diff] [blame] | 751 |  | 
| Kiyoung Kim | 4639f69 | 2019-02-20 18:04:39 +0900 | [diff] [blame] | 752 | jstring CreateClassLoaderNamespace(JNIEnv* env, int32_t target_sdk_version, jobject class_loader, | 
|  | 753 | bool is_shared, jstring dex_path, jstring library_path, | 
| Dimitry Ivanov | d047c92 | 2016-02-23 14:23:51 -0800 | [diff] [blame] | 754 | jstring permitted_path) { | 
| Dimitry Ivanov | ac1b191 | 2015-12-01 13:56:44 -0800 | [diff] [blame] | 755 | #if defined(__ANDROID__) | 
| Dimitry Ivanov | 34d5a20 | 2016-02-29 13:21:43 -0800 | [diff] [blame] | 756 | std::lock_guard<std::mutex> guard(g_namespaces_mutex); | 
| Zhenhua WANG | f2804e5 | 2016-05-30 11:16:08 +0800 | [diff] [blame] | 757 |  | 
|  | 758 | std::string error_msg; | 
| Kiyoung Kim | 4639f69 | 2019-02-20 18:04:39 +0900 | [diff] [blame] | 759 | bool success = g_namespaces->Create(env, target_sdk_version, class_loader, is_shared, dex_path, | 
|  | 760 | library_path, permitted_path, &error_msg) != nullptr; | 
| Zhenhua WANG | f2804e5 | 2016-05-30 11:16:08 +0800 | [diff] [blame] | 761 | if (!success) { | 
|  | 762 | return env->NewStringUTF(error_msg.c_str()); | 
| Dimitry Ivanov | d047c92 | 2016-02-23 14:23:51 -0800 | [diff] [blame] | 763 | } | 
|  | 764 | #else | 
| Kiyoung Kim | 4639f69 | 2019-02-20 18:04:39 +0900 | [diff] [blame] | 765 | UNUSED(env, target_sdk_version, class_loader, is_shared, dex_path, library_path, permitted_path); | 
| Dimitry Ivanov | d047c92 | 2016-02-23 14:23:51 -0800 | [diff] [blame] | 766 | #endif | 
|  | 767 | return nullptr; | 
|  | 768 | } | 
|  | 769 |  | 
| Nicolas Geoffray | 890e3bf | 2019-01-22 09:11:57 +0000 | [diff] [blame] | 770 | #if defined(__ANDROID__) | 
|  | 771 | static android_namespace_t* FindExportedNamespace(const char* caller_location) { | 
|  | 772 | std::string location = caller_location; | 
|  | 773 | // Lots of implicit assumptions here: we expect `caller_location` to be of the form: | 
|  | 774 | // /apex/com.android...modulename/... | 
|  | 775 | // | 
|  | 776 | // And we extract from it 'modulename', which is the name of the linker namespace. | 
|  | 777 | if (android::base::StartsWith(location, kApexPath)) { | 
|  | 778 | size_t slash_index = location.find_first_of('/', strlen(kApexPath)); | 
|  | 779 | LOG_ALWAYS_FATAL_IF((slash_index == std::string::npos), | 
|  | 780 | "Error finding namespace of apex: no slash in path %s", caller_location); | 
|  | 781 | size_t dot_index = location.find_last_of('.', slash_index); | 
|  | 782 | LOG_ALWAYS_FATAL_IF((dot_index == std::string::npos), | 
|  | 783 | "Error finding namespace of apex: no dot in apex name %s", caller_location); | 
|  | 784 | std::string name = location.substr(dot_index + 1, slash_index - dot_index - 1); | 
|  | 785 | android_namespace_t* boot_namespace = android_get_exported_namespace(name.c_str()); | 
|  | 786 | LOG_ALWAYS_FATAL_IF((boot_namespace == nullptr), | 
|  | 787 | "Error finding namespace of apex: no namespace called %s", name.c_str()); | 
|  | 788 | return boot_namespace; | 
|  | 789 | } | 
|  | 790 | return nullptr; | 
|  | 791 | } | 
|  | 792 | #endif | 
|  | 793 |  | 
| Nicolas Geoffray | c3a73dc | 2019-01-12 15:01:20 +0000 | [diff] [blame] | 794 | void* OpenNativeLibrary(JNIEnv* env, int32_t target_sdk_version, const char* path, | 
| Nicolas Geoffray | 5353502 | 2019-01-18 10:05:13 +0000 | [diff] [blame] | 795 | jobject class_loader, const char* caller_location, jstring library_path, | 
|  | 796 | bool* needs_native_bridge, char** error_msg) { | 
| Dimitry Ivanov | d047c92 | 2016-02-23 14:23:51 -0800 | [diff] [blame] | 797 | #if defined(__ANDROID__) | 
| Dimitry Ivanov | 5539db0 | 2016-04-20 16:07:30 -0700 | [diff] [blame] | 798 | UNUSED(target_sdk_version); | 
|  | 799 | if (class_loader == nullptr) { | 
| Zhenhua WANG | f2804e5 | 2016-05-30 11:16:08 +0800 | [diff] [blame] | 800 | *needs_native_bridge = false; | 
| Nicolas Geoffray | 890e3bf | 2019-01-22 09:11:57 +0000 | [diff] [blame] | 801 | if (caller_location != nullptr) { | 
|  | 802 | android_namespace_t* boot_namespace = FindExportedNamespace(caller_location); | 
|  | 803 | if (boot_namespace != nullptr) { | 
|  | 804 | const android_dlextinfo dlextinfo = { | 
|  | 805 | .flags = ANDROID_DLEXT_USE_NAMESPACE, | 
|  | 806 | .library_namespace = boot_namespace, | 
|  | 807 | }; | 
|  | 808 | void* handle = android_dlopen_ext(path, RTLD_NOW, &dlextinfo); | 
|  | 809 | if (handle == nullptr) { | 
|  | 810 | *error_msg = strdup(dlerror()); | 
|  | 811 | } | 
|  | 812 | return handle; | 
|  | 813 | } | 
|  | 814 | } | 
| Pete Bentley | 632f142 | 2018-12-19 13:33:33 +0000 | [diff] [blame] | 815 | void* handle = dlopen(path, RTLD_NOW); | 
|  | 816 | if (handle == nullptr) { | 
| Nicolas Geoffray | d06cb94 | 2019-01-16 20:20:27 +0000 | [diff] [blame] | 817 | *error_msg = strdup(dlerror()); | 
| Pete Bentley | 632f142 | 2018-12-19 13:33:33 +0000 | [diff] [blame] | 818 | } | 
|  | 819 | return handle; | 
| Dimitry Ivanov | ac1b191 | 2015-12-01 13:56:44 -0800 | [diff] [blame] | 820 | } | 
|  | 821 |  | 
| Dimitry Ivanov | 34d5a20 | 2016-02-29 13:21:43 -0800 | [diff] [blame] | 822 | std::lock_guard<std::mutex> guard(g_namespaces_mutex); | 
| Victor Khimenko | 1443ec4 | 2018-07-09 17:01:22 +0200 | [diff] [blame] | 823 | NativeLoaderNamespace* ns; | 
| Dimitry Ivanov | ac1b191 | 2015-12-01 13:56:44 -0800 | [diff] [blame] | 824 |  | 
| Victor Khimenko | 1443ec4 | 2018-07-09 17:01:22 +0200 | [diff] [blame] | 825 | if ((ns = g_namespaces->FindNamespaceByClassLoader(env, class_loader)) == nullptr) { | 
| Dimitry Ivanov | d047c92 | 2016-02-23 14:23:51 -0800 | [diff] [blame] | 826 | // This is the case where the classloader was not created by ApplicationLoaders | 
|  | 827 | // In this case we create an isolated not-shared namespace for it. | 
| Nicolas Geoffray | c3a73dc | 2019-01-12 15:01:20 +0000 | [diff] [blame] | 828 | std::string create_error_msg; | 
|  | 829 | if ((ns = g_namespaces->Create(env, target_sdk_version, class_loader, false /* is_shared */, | 
| Kiyoung Kim | 4639f69 | 2019-02-20 18:04:39 +0900 | [diff] [blame] | 830 | nullptr, library_path, nullptr, &create_error_msg)) == nullptr) { | 
| Nicolas Geoffray | c3a73dc | 2019-01-12 15:01:20 +0000 | [diff] [blame] | 831 | *error_msg = strdup(create_error_msg.c_str()); | 
| Dimitry Ivanov | d047c92 | 2016-02-23 14:23:51 -0800 | [diff] [blame] | 832 | return nullptr; | 
|  | 833 | } | 
| Dimitry Ivanov | ac1b191 | 2015-12-01 13:56:44 -0800 | [diff] [blame] | 834 | } | 
|  | 835 |  | 
| Nicolas Geoffray | c3a73dc | 2019-01-12 15:01:20 +0000 | [diff] [blame] | 836 | return OpenNativeLibraryInNamespace(ns, path, needs_native_bridge, error_msg); | 
| Dimitry Ivanov | ac1b191 | 2015-12-01 13:56:44 -0800 | [diff] [blame] | 837 | #else | 
| Nicolas Geoffray | 5353502 | 2019-01-18 10:05:13 +0000 | [diff] [blame] | 838 | UNUSED(env, target_sdk_version, class_loader, caller_location); | 
| Andreas Gampe | 5c7d582 | 2017-12-28 19:08:13 -0800 | [diff] [blame] | 839 |  | 
|  | 840 | // Do some best effort to emulate library-path support. It will not | 
|  | 841 | // work for dependencies. | 
|  | 842 | // | 
|  | 843 | // Note: null has a special meaning and must be preserved. | 
|  | 844 | std::string c_library_path;  // Empty string by default. | 
|  | 845 | if (library_path != nullptr && path != nullptr && path[0] != '/') { | 
|  | 846 | ScopedUtfChars library_path_utf_chars(env, library_path); | 
|  | 847 | c_library_path = library_path_utf_chars.c_str(); | 
|  | 848 | } | 
|  | 849 |  | 
|  | 850 | std::vector<std::string> library_paths = base::Split(c_library_path, ":"); | 
|  | 851 |  | 
|  | 852 | for (const std::string& lib_path : library_paths) { | 
|  | 853 | *needs_native_bridge = false; | 
|  | 854 | const char* path_arg; | 
|  | 855 | std::string complete_path; | 
|  | 856 | if (path == nullptr) { | 
|  | 857 | // Preserve null. | 
|  | 858 | path_arg = nullptr; | 
| Zhenhua WANG | f2804e5 | 2016-05-30 11:16:08 +0800 | [diff] [blame] | 859 | } else { | 
| Andreas Gampe | 5c7d582 | 2017-12-28 19:08:13 -0800 | [diff] [blame] | 860 | complete_path = lib_path; | 
|  | 861 | if (!complete_path.empty()) { | 
|  | 862 | complete_path.append("/"); | 
|  | 863 | } | 
|  | 864 | complete_path.append(path); | 
|  | 865 | path_arg = complete_path.c_str(); | 
|  | 866 | } | 
|  | 867 | void* handle = dlopen(path_arg, RTLD_NOW); | 
|  | 868 | if (handle != nullptr) { | 
|  | 869 | return handle; | 
|  | 870 | } | 
|  | 871 | if (NativeBridgeIsSupported(path_arg)) { | 
|  | 872 | *needs_native_bridge = true; | 
|  | 873 | handle = NativeBridgeLoadLibrary(path_arg, RTLD_NOW); | 
|  | 874 | if (handle != nullptr) { | 
|  | 875 | return handle; | 
|  | 876 | } | 
| Nicolas Geoffray | c3a73dc | 2019-01-12 15:01:20 +0000 | [diff] [blame] | 877 | *error_msg = strdup(NativeBridgeGetError()); | 
| Andreas Gampe | 5c7d582 | 2017-12-28 19:08:13 -0800 | [diff] [blame] | 878 | } else { | 
| Nicolas Geoffray | c3a73dc | 2019-01-12 15:01:20 +0000 | [diff] [blame] | 879 | *error_msg = strdup(dlerror()); | 
| Zhenhua WANG | f2804e5 | 2016-05-30 11:16:08 +0800 | [diff] [blame] | 880 | } | 
|  | 881 | } | 
| Andreas Gampe | 5c7d582 | 2017-12-28 19:08:13 -0800 | [diff] [blame] | 882 | return nullptr; | 
| Dimitry Ivanov | ac1b191 | 2015-12-01 13:56:44 -0800 | [diff] [blame] | 883 | #endif | 
|  | 884 | } | 
|  | 885 |  | 
| Nicolas Geoffray | c3a73dc | 2019-01-12 15:01:20 +0000 | [diff] [blame] | 886 | bool CloseNativeLibrary(void* handle, const bool needs_native_bridge, char** error_msg) { | 
| dimitry | 3150f7c | 2018-09-12 01:09:19 +0200 | [diff] [blame] | 887 | bool success; | 
|  | 888 | if (needs_native_bridge) { | 
|  | 889 | success = (NativeBridgeUnloadLibrary(handle) == 0); | 
|  | 890 | if (!success) { | 
| Nicolas Geoffray | c3a73dc | 2019-01-12 15:01:20 +0000 | [diff] [blame] | 891 | *error_msg = strdup(NativeBridgeGetError()); | 
| dimitry | 3150f7c | 2018-09-12 01:09:19 +0200 | [diff] [blame] | 892 | } | 
|  | 893 | } else { | 
|  | 894 | success = (dlclose(handle) == 0); | 
|  | 895 | if (!success) { | 
| Nicolas Geoffray | c3a73dc | 2019-01-12 15:01:20 +0000 | [diff] [blame] | 896 | *error_msg = strdup(dlerror()); | 
| dimitry | 3150f7c | 2018-09-12 01:09:19 +0200 | [diff] [blame] | 897 | } | 
|  | 898 | } | 
|  | 899 |  | 
|  | 900 | return success; | 
| Dimitry Ivanov | 09a516b | 2016-05-03 14:55:25 -0700 | [diff] [blame] | 901 | } | 
|  | 902 |  | 
| Nicolas Geoffray | c3a73dc | 2019-01-12 15:01:20 +0000 | [diff] [blame] | 903 | void NativeLoaderFreeErrorMessage(char* msg) { | 
|  | 904 | // The error messages get allocated through strdup, so we must call free on them. | 
|  | 905 | free(msg); | 
|  | 906 | } | 
|  | 907 |  | 
| Dimitry Ivanov | f44ecde | 2016-02-22 13:48:22 -0800 | [diff] [blame] | 908 | #if defined(__ANDROID__) | 
| Nicolas Geoffray | c3a73dc | 2019-01-12 15:01:20 +0000 | [diff] [blame] | 909 | void* OpenNativeLibraryInNamespace(NativeLoaderNamespace* ns, const char* path, | 
|  | 910 | bool* needs_native_bridge, char** error_msg) { | 
| Victor Khimenko | 1443ec4 | 2018-07-09 17:01:22 +0200 | [diff] [blame] | 911 | if (ns->is_android_namespace()) { | 
|  | 912 | android_dlextinfo extinfo; | 
|  | 913 | extinfo.flags = ANDROID_DLEXT_USE_NAMESPACE; | 
|  | 914 | extinfo.library_namespace = ns->get_android_ns(); | 
|  | 915 |  | 
|  | 916 | void* handle = android_dlopen_ext(path, RTLD_NOW, &extinfo); | 
|  | 917 | if (handle == nullptr) { | 
| Nicolas Geoffray | c3a73dc | 2019-01-12 15:01:20 +0000 | [diff] [blame] | 918 | *error_msg = strdup(dlerror()); | 
| Victor Khimenko | 1443ec4 | 2018-07-09 17:01:22 +0200 | [diff] [blame] | 919 | } | 
|  | 920 | *needs_native_bridge = false; | 
|  | 921 | return handle; | 
|  | 922 | } else { | 
|  | 923 | void* handle = NativeBridgeLoadLibraryExt(path, RTLD_NOW, ns->get_native_bridge_ns()); | 
|  | 924 | if (handle == nullptr) { | 
| Nicolas Geoffray | c3a73dc | 2019-01-12 15:01:20 +0000 | [diff] [blame] | 925 | *error_msg = strdup(NativeBridgeGetError()); | 
| Victor Khimenko | 1443ec4 | 2018-07-09 17:01:22 +0200 | [diff] [blame] | 926 | } | 
|  | 927 | *needs_native_bridge = true; | 
|  | 928 | return handle; | 
|  | 929 | } | 
|  | 930 | } | 
|  | 931 |  | 
| Dimitry Ivanov | 800083d | 2016-11-01 14:17:00 -0700 | [diff] [blame] | 932 | // native_bridge_namespaces are not supported for callers of this function. | 
|  | 933 | // This function will return nullptr in the case when application is running | 
|  | 934 | // on native bridge. | 
| Dimitry Ivanov | f44ecde | 2016-02-22 13:48:22 -0800 | [diff] [blame] | 935 | android_namespace_t* FindNamespaceByClassLoader(JNIEnv* env, jobject class_loader) { | 
| Dimitry Ivanov | 34d5a20 | 2016-02-29 13:21:43 -0800 | [diff] [blame] | 936 | std::lock_guard<std::mutex> guard(g_namespaces_mutex); | 
| Victor Khimenko | 1443ec4 | 2018-07-09 17:01:22 +0200 | [diff] [blame] | 937 | NativeLoaderNamespace* ns = g_namespaces->FindNamespaceByClassLoader(env, class_loader); | 
|  | 938 | if (ns != nullptr) { | 
|  | 939 | return ns->is_android_namespace() ? ns->get_android_ns() : nullptr; | 
| Zhenhua WANG | f2804e5 | 2016-05-30 11:16:08 +0800 | [diff] [blame] | 940 | } | 
|  | 941 |  | 
|  | 942 | return nullptr; | 
| Dimitry Ivanov | f44ecde | 2016-02-22 13:48:22 -0800 | [diff] [blame] | 943 | } | 
| Victor Khimenko | 1443ec4 | 2018-07-09 17:01:22 +0200 | [diff] [blame] | 944 | NativeLoaderNamespace* FindNativeLoaderNamespaceByClassLoader(JNIEnv* env, jobject class_loader) { | 
|  | 945 | std::lock_guard<std::mutex> guard(g_namespaces_mutex); | 
|  | 946 | return g_namespaces->FindNamespaceByClassLoader(env, class_loader); | 
|  | 947 | } | 
| Dimitry Ivanov | f44ecde | 2016-02-22 13:48:22 -0800 | [diff] [blame] | 948 | #endif | 
|  | 949 |  | 
| Dimitry Ivanov | ac1b191 | 2015-12-01 13:56:44 -0800 | [diff] [blame] | 950 | }; //  android namespace |