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 | ff2dcd9 | 2016-09-28 15:54:45 -0700 | [diff] [blame] | 24 | #include "cutils/properties.h" |
Mark Salyzyn | 30f991f | 2017-01-10 13:19:54 -0800 | [diff] [blame] | 25 | #include "log/log.h" |
Dimitry Ivanov | ac1b191 | 2015-12-01 13:56:44 -0800 | [diff] [blame] | 26 | #endif |
Jiyong Park | d1006fe | 2017-11-08 18:44:09 +0900 | [diff] [blame] | 27 | #include <dirent.h> |
| 28 | #include <sys/types.h> |
Zhenhua WANG | f2804e5 | 2016-05-30 11:16:08 +0800 | [diff] [blame] | 29 | #include "nativebridge/native_bridge.h" |
Dimitry Ivanov | ac1b191 | 2015-12-01 13:56:44 -0800 | [diff] [blame] | 30 | |
| 31 | #include <algorithm> |
Victor Khimenko | 1443ec4 | 2018-07-09 17:01:22 +0200 | [diff] [blame] | 32 | #include <list> |
Jiyong Park | d1006fe | 2017-11-08 18:44:09 +0900 | [diff] [blame] | 33 | #include <memory> |
Dimitry Ivanov | ac1b191 | 2015-12-01 13:56:44 -0800 | [diff] [blame] | 34 | #include <mutex> |
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__) |
Zhenhua WANG | f2804e5 | 2016-05-30 11:16:08 +0800 | [diff] [blame] | 55 | class NativeLoaderNamespace { |
| 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 | |
Dimitry Ivanov | 882cad2 | 2017-05-01 15:12:49 -0700 | [diff] [blame] | 106 | // The device may be configured to have the vendor libraries loaded to a separate namespace. |
| 107 | // For historical reasons this namespace was named sphal but effectively it is intended |
| 108 | // to use to load vendor libraries to separate namespace with controlled interface between |
| 109 | // vendor and system namespaces. |
| 110 | static constexpr const char* kVendorNamespaceName = "sphal"; |
| 111 | |
Jiyong Park | a07f305 | 2017-08-22 10:26:10 +0900 | [diff] [blame] | 112 | static constexpr const char* kVndkNamespaceName = "vndk"; |
| 113 | |
| 114 | static constexpr const char* kClassloaderNamespaceName = "classloader-namespace"; |
| 115 | static constexpr const char* kVendorClassloaderNamespaceName = "vendor-classloader-namespace"; |
| 116 | |
Dimitry Ivanov | f334cbf | 2016-05-10 10:39:48 -0700 | [diff] [blame] | 117 | // (http://b/27588281) This is a workaround for apps using custom classloaders and calling |
| 118 | // System.load() with an absolute path which is outside of the classloader library search path. |
| 119 | // This list includes all directories app is allowed to access this way. |
| 120 | static constexpr const char* kWhitelistedDirectories = "/data:/mnt/expand"; |
| 121 | |
Dimitry Ivanov | 7d02829 | 2016-05-05 17:30:24 -0700 | [diff] [blame] | 122 | static bool is_debuggable() { |
| 123 | char debuggable[PROP_VALUE_MAX]; |
| 124 | property_get("ro.debuggable", debuggable, "0"); |
| 125 | return std::string(debuggable) == "1"; |
| 126 | } |
| 127 | |
Justin Yun | 4a1d110 | 2017-11-27 17:04:14 +0900 | [diff] [blame] | 128 | static std::string vndk_version_str() { |
| 129 | #ifdef __BIONIC__ |
| 130 | std::string version = android::base::GetProperty("ro.vndk.version", ""); |
| 131 | if (version != "" && version != "current") { |
| 132 | return "." + version; |
| 133 | } |
| 134 | #endif |
| 135 | return ""; |
| 136 | } |
| 137 | |
| 138 | static void insert_vndk_version_str(std::string* file_name) { |
| 139 | CHECK(file_name != nullptr); |
| 140 | size_t insert_pos = file_name->find_last_of("."); |
| 141 | if (insert_pos == std::string::npos) { |
| 142 | insert_pos = file_name->length(); |
| 143 | } |
| 144 | file_name->insert(insert_pos, vndk_version_str()); |
| 145 | } |
| 146 | |
Jiyong Park | d1006fe | 2017-11-08 18:44:09 +0900 | [diff] [blame] | 147 | static const std::function<bool(const std::string&, std::string*)> always_true = |
| 148 | [](const std::string&, std::string*) { return true; }; |
| 149 | |
Dimitry Ivanov | ac1b191 | 2015-12-01 13:56:44 -0800 | [diff] [blame] | 150 | class LibraryNamespaces { |
| 151 | public: |
Dimitry Ivanov | 426799d | 2016-02-22 11:27:48 -0800 | [diff] [blame] | 152 | LibraryNamespaces() : initialized_(false) { } |
Dimitry Ivanov | ac1b191 | 2015-12-01 13:56:44 -0800 | [diff] [blame] | 153 | |
Victor Khimenko | 1443ec4 | 2018-07-09 17:01:22 +0200 | [diff] [blame] | 154 | NativeLoaderNamespace* Create(JNIEnv* env, |
| 155 | uint32_t target_sdk_version, |
| 156 | jobject class_loader, |
| 157 | bool is_shared, |
| 158 | bool is_for_vendor, |
| 159 | jstring java_library_path, |
| 160 | jstring java_permitted_path, |
| 161 | std::string* error_msg) { |
Dimitry Ivanov | cf9892b | 2016-05-09 10:55:50 -0700 | [diff] [blame] | 162 | std::string library_path; // empty string by default. |
| 163 | |
| 164 | if (java_library_path != nullptr) { |
| 165 | ScopedUtfChars library_path_utf_chars(env, java_library_path); |
| 166 | library_path = library_path_utf_chars.c_str(); |
| 167 | } |
Dimitry Ivanov | ac1b191 | 2015-12-01 13:56:44 -0800 | [diff] [blame] | 168 | |
Dimitry Ivanov | f334cbf | 2016-05-10 10:39:48 -0700 | [diff] [blame] | 169 | // (http://b/27588281) This is a workaround for apps using custom |
| 170 | // classloaders and calling System.load() with an absolute path which |
| 171 | // is outside of the classloader library search path. |
| 172 | // |
| 173 | // This part effectively allows such a classloader to access anything |
| 174 | // under /data and /mnt/expand |
| 175 | std::string permitted_path = kWhitelistedDirectories; |
| 176 | |
Dimitry Ivanov | 0d6e594 | 2015-12-08 11:16:56 -0800 | [diff] [blame] | 177 | if (java_permitted_path != nullptr) { |
| 178 | ScopedUtfChars path(env, java_permitted_path); |
Dimitry Ivanov | d0b1531 | 2016-05-10 16:21:25 -0700 | [diff] [blame] | 179 | if (path.c_str() != nullptr && path.size() > 0) { |
| 180 | permitted_path = permitted_path + ":" + path.c_str(); |
| 181 | } |
Dimitry Ivanov | 0d6e594 | 2015-12-08 11:16:56 -0800 | [diff] [blame] | 182 | } |
| 183 | |
Zhenhua WANG | f2804e5 | 2016-05-30 11:16:08 +0800 | [diff] [blame] | 184 | if (!initialized_ && !InitPublicNamespace(library_path.c_str(), error_msg)) { |
Victor Khimenko | 1443ec4 | 2018-07-09 17:01:22 +0200 | [diff] [blame] | 185 | return nullptr; |
Dimitry Ivanov | ac1b191 | 2015-12-01 13:56:44 -0800 | [diff] [blame] | 186 | } |
| 187 | |
Victor Khimenko | 1443ec4 | 2018-07-09 17:01:22 +0200 | [diff] [blame] | 188 | bool found = FindNamespaceByClassLoader(env, class_loader); |
Dimitry Ivanov | ac1b191 | 2015-12-01 13:56:44 -0800 | [diff] [blame] | 189 | |
Zhenhua WANG | f2804e5 | 2016-05-30 11:16:08 +0800 | [diff] [blame] | 190 | LOG_ALWAYS_FATAL_IF(found, |
Dimitry Ivanov | 4b0e963 | 2016-03-15 13:51:26 -0700 | [diff] [blame] | 191 | "There is already a namespace associated with this classloader"); |
Dimitry Ivanov | ac1b191 | 2015-12-01 13:56:44 -0800 | [diff] [blame] | 192 | |
Dimitry Ivanov | d2a6220 | 2015-12-15 11:06:57 -0800 | [diff] [blame] | 193 | uint64_t namespace_type = ANDROID_NAMESPACE_TYPE_ISOLATED; |
| 194 | if (is_shared) { |
| 195 | namespace_type |= ANDROID_NAMESPACE_TYPE_SHARED; |
| 196 | } |
| 197 | |
Dimitry Ivanov | 9e253ce | 2017-05-08 22:24:24 -0700 | [diff] [blame] | 198 | if (target_sdk_version < 24) { |
| 199 | namespace_type |= ANDROID_NAMESPACE_TYPE_GREYLIST_ENABLED; |
| 200 | } |
| 201 | |
Victor Khimenko | 1443ec4 | 2018-07-09 17:01:22 +0200 | [diff] [blame] | 202 | NativeLoaderNamespace* parent_ns = FindParentNamespaceByClassLoader(env, class_loader); |
Dimitry Ivanov | 24db75c | 2016-05-12 15:34:41 -0700 | [diff] [blame] | 203 | |
Zhenhua WANG | f2804e5 | 2016-05-30 11:16:08 +0800 | [diff] [blame] | 204 | bool is_native_bridge = false; |
Dimitry Ivanov | ac1b191 | 2015-12-01 13:56:44 -0800 | [diff] [blame] | 205 | |
Victor Khimenko | 1443ec4 | 2018-07-09 17:01:22 +0200 | [diff] [blame] | 206 | if (parent_ns != nullptr) { |
| 207 | is_native_bridge = !parent_ns->is_android_namespace(); |
Zhenhua WANG | f2804e5 | 2016-05-30 11:16:08 +0800 | [diff] [blame] | 208 | } else if (!library_path.empty()) { |
| 209 | is_native_bridge = NativeBridgeIsPathSupported(library_path.c_str()); |
Dimitry Ivanov | d047c92 | 2016-02-23 14:23:51 -0800 | [diff] [blame] | 210 | } |
Dimitry Ivanov | ac1b191 | 2015-12-01 13:56:44 -0800 | [diff] [blame] | 211 | |
Jiyong Park | a07f305 | 2017-08-22 10:26:10 +0900 | [diff] [blame] | 212 | std::string system_exposed_libraries = system_public_libraries_; |
| 213 | const char* namespace_name = kClassloaderNamespaceName; |
| 214 | android_namespace_t* vndk_ns = nullptr; |
| 215 | if (is_for_vendor && !is_shared) { |
| 216 | LOG_FATAL_IF(is_native_bridge, "Unbundled vendor apk must not use translated architecture"); |
| 217 | |
| 218 | // For vendor apks, give access to the vendor lib even though |
| 219 | // they are treated as unbundled; the libs and apks are still bundled |
| 220 | // together in the vendor partition. |
| 221 | #if defined(__LP64__) |
| 222 | std::string vendor_lib_path = "/vendor/lib64"; |
| 223 | #else |
| 224 | std::string vendor_lib_path = "/vendor/lib"; |
| 225 | #endif |
| 226 | library_path = library_path + ":" + vendor_lib_path.c_str(); |
| 227 | permitted_path = permitted_path + ":" + vendor_lib_path.c_str(); |
| 228 | |
| 229 | // Also give access to LLNDK libraries since they are available to vendors |
| 230 | system_exposed_libraries = system_exposed_libraries + ":" + system_llndk_libraries_.c_str(); |
| 231 | |
| 232 | // Give access to VNDK-SP libraries from the 'vndk' namespace. |
| 233 | vndk_ns = android_get_exported_namespace(kVndkNamespaceName); |
| 234 | LOG_ALWAYS_FATAL_IF(vndk_ns == nullptr, |
| 235 | "Cannot find \"%s\" namespace for vendor apks", kVndkNamespaceName); |
| 236 | |
| 237 | // Different name is useful for debugging |
| 238 | namespace_name = kVendorClassloaderNamespaceName; |
| 239 | ALOGD("classloader namespace configured for unbundled vendor apk. library_path=%s", library_path.c_str()); |
Inseob Kim | 67cb056 | 2018-05-04 11:39:12 +0900 | [diff] [blame] | 240 | } else { |
| 241 | // 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] | 242 | // would be system->vendor violation. |
Inseob Kim | 67cb056 | 2018-05-04 11:39:12 +0900 | [diff] [blame] | 243 | if (!oem_public_libraries_.empty()) { |
| 244 | system_exposed_libraries = system_exposed_libraries + ':' + oem_public_libraries_; |
| 245 | } |
| 246 | if (!product_public_libraries_.empty()) { |
| 247 | system_exposed_libraries = system_exposed_libraries + ':' + product_public_libraries_; |
| 248 | } |
Jiyong Park | a07f305 | 2017-08-22 10:26:10 +0900 | [diff] [blame] | 249 | } |
| 250 | |
Zhenhua WANG | f2804e5 | 2016-05-30 11:16:08 +0800 | [diff] [blame] | 251 | NativeLoaderNamespace native_loader_ns; |
| 252 | if (!is_native_bridge) { |
Victor Khimenko | 1443ec4 | 2018-07-09 17:01:22 +0200 | [diff] [blame] | 253 | android_namespace_t* android_parent_ns = |
| 254 | parent_ns == nullptr ? nullptr : parent_ns->get_android_ns(); |
Jiyong Park | a07f305 | 2017-08-22 10:26:10 +0900 | [diff] [blame] | 255 | android_namespace_t* ns = android_create_namespace(namespace_name, |
Zhenhua WANG | f2804e5 | 2016-05-30 11:16:08 +0800 | [diff] [blame] | 256 | nullptr, |
| 257 | library_path.c_str(), |
| 258 | namespace_type, |
| 259 | permitted_path.c_str(), |
Victor Khimenko | 1443ec4 | 2018-07-09 17:01:22 +0200 | [diff] [blame] | 260 | android_parent_ns); |
Zhenhua WANG | f2804e5 | 2016-05-30 11:16:08 +0800 | [diff] [blame] | 261 | if (ns == nullptr) { |
| 262 | *error_msg = dlerror(); |
Victor Khimenko | 1443ec4 | 2018-07-09 17:01:22 +0200 | [diff] [blame] | 263 | return nullptr; |
Zhenhua WANG | f2804e5 | 2016-05-30 11:16:08 +0800 | [diff] [blame] | 264 | } |
| 265 | |
Dimitry Ivanov | 882cad2 | 2017-05-01 15:12:49 -0700 | [diff] [blame] | 266 | // Note that when vendor_ns is not configured this function will return nullptr |
| 267 | // and it will result in linking vendor_public_libraries_ to the default namespace |
| 268 | // which is expected behavior in this case. |
| 269 | android_namespace_t* vendor_ns = android_get_exported_namespace(kVendorNamespaceName); |
| 270 | |
Jiyong Park | a07f305 | 2017-08-22 10:26:10 +0900 | [diff] [blame] | 271 | if (!android_link_namespaces(ns, nullptr, system_exposed_libraries.c_str())) { |
Dimitry Ivanov | 26e1a84 | 2017-02-03 14:11:27 -0800 | [diff] [blame] | 272 | *error_msg = dlerror(); |
Victor Khimenko | 1443ec4 | 2018-07-09 17:01:22 +0200 | [diff] [blame] | 273 | return nullptr; |
Dimitry Ivanov | 26e1a84 | 2017-02-03 14:11:27 -0800 | [diff] [blame] | 274 | } |
| 275 | |
Jiyong Park | a07f305 | 2017-08-22 10:26:10 +0900 | [diff] [blame] | 276 | if (vndk_ns != nullptr && !system_vndksp_libraries_.empty()) { |
| 277 | // vendor apks are allowed to use VNDK-SP libraries. |
| 278 | if (!android_link_namespaces(ns, vndk_ns, system_vndksp_libraries_.c_str())) { |
| 279 | *error_msg = dlerror(); |
Victor Khimenko | 1443ec4 | 2018-07-09 17:01:22 +0200 | [diff] [blame] | 280 | return nullptr; |
Jiyong Park | a07f305 | 2017-08-22 10:26:10 +0900 | [diff] [blame] | 281 | } |
| 282 | } |
| 283 | |
Dimitry Ivanov | 882cad2 | 2017-05-01 15:12:49 -0700 | [diff] [blame] | 284 | if (!vendor_public_libraries_.empty()) { |
| 285 | if (!android_link_namespaces(ns, vendor_ns, vendor_public_libraries_.c_str())) { |
| 286 | *error_msg = dlerror(); |
Victor Khimenko | 1443ec4 | 2018-07-09 17:01:22 +0200 | [diff] [blame] | 287 | return nullptr; |
Dimitry Ivanov | 882cad2 | 2017-05-01 15:12:49 -0700 | [diff] [blame] | 288 | } |
| 289 | } |
| 290 | |
Zhenhua WANG | f2804e5 | 2016-05-30 11:16:08 +0800 | [diff] [blame] | 291 | native_loader_ns = NativeLoaderNamespace(ns); |
| 292 | } else { |
Victor Khimenko | 1443ec4 | 2018-07-09 17:01:22 +0200 | [diff] [blame] | 293 | native_bridge_namespace_t* native_bridge_parent_namespace = |
| 294 | parent_ns == nullptr ? nullptr : parent_ns->get_native_bridge_ns(); |
Jiyong Park | a07f305 | 2017-08-22 10:26:10 +0900 | [diff] [blame] | 295 | native_bridge_namespace_t* ns = NativeBridgeCreateNamespace(namespace_name, |
Zhenhua WANG | f2804e5 | 2016-05-30 11:16:08 +0800 | [diff] [blame] | 296 | nullptr, |
| 297 | library_path.c_str(), |
| 298 | namespace_type, |
| 299 | permitted_path.c_str(), |
Victor Khimenko | 1443ec4 | 2018-07-09 17:01:22 +0200 | [diff] [blame] | 300 | native_bridge_parent_namespace); |
Zhenhua WANG | f2804e5 | 2016-05-30 11:16:08 +0800 | [diff] [blame] | 301 | if (ns == nullptr) { |
| 302 | *error_msg = NativeBridgeGetError(); |
Victor Khimenko | 1443ec4 | 2018-07-09 17:01:22 +0200 | [diff] [blame] | 303 | return nullptr; |
Zhenhua WANG | f2804e5 | 2016-05-30 11:16:08 +0800 | [diff] [blame] | 304 | } |
| 305 | |
Dimitry Ivanov | 882cad2 | 2017-05-01 15:12:49 -0700 | [diff] [blame] | 306 | native_bridge_namespace_t* vendor_ns = NativeBridgeGetVendorNamespace(); |
| 307 | |
Jiyong Park | a07f305 | 2017-08-22 10:26:10 +0900 | [diff] [blame] | 308 | if (!NativeBridgeLinkNamespaces(ns, nullptr, system_exposed_libraries.c_str())) { |
Zhenhua WANG | e8fb11d | 2017-02-27 10:14:45 +0800 | [diff] [blame] | 309 | *error_msg = NativeBridgeGetError(); |
Victor Khimenko | 1443ec4 | 2018-07-09 17:01:22 +0200 | [diff] [blame] | 310 | return nullptr; |
Zhenhua WANG | e8fb11d | 2017-02-27 10:14:45 +0800 | [diff] [blame] | 311 | } |
| 312 | |
Dimitry Ivanov | 882cad2 | 2017-05-01 15:12:49 -0700 | [diff] [blame] | 313 | if (!vendor_public_libraries_.empty()) { |
| 314 | if (!NativeBridgeLinkNamespaces(ns, vendor_ns, vendor_public_libraries_.c_str())) { |
| 315 | *error_msg = NativeBridgeGetError(); |
Victor Khimenko | 1443ec4 | 2018-07-09 17:01:22 +0200 | [diff] [blame] | 316 | return nullptr; |
Dimitry Ivanov | 882cad2 | 2017-05-01 15:12:49 -0700 | [diff] [blame] | 317 | } |
| 318 | } |
| 319 | |
Zhenhua WANG | f2804e5 | 2016-05-30 11:16:08 +0800 | [diff] [blame] | 320 | native_loader_ns = NativeLoaderNamespace(ns); |
| 321 | } |
| 322 | |
| 323 | namespaces_.push_back(std::make_pair(env->NewWeakGlobalRef(class_loader), native_loader_ns)); |
| 324 | |
Victor Khimenko | 1443ec4 | 2018-07-09 17:01:22 +0200 | [diff] [blame] | 325 | return &(namespaces_.back().second); |
Dimitry Ivanov | ac1b191 | 2015-12-01 13:56:44 -0800 | [diff] [blame] | 326 | } |
| 327 | |
Victor Khimenko | 1443ec4 | 2018-07-09 17:01:22 +0200 | [diff] [blame] | 328 | NativeLoaderNamespace* FindNamespaceByClassLoader(JNIEnv* env, jobject class_loader) { |
Dimitry Ivanov | f44ecde | 2016-02-22 13:48:22 -0800 | [diff] [blame] | 329 | auto it = std::find_if(namespaces_.begin(), namespaces_.end(), |
Zhenhua WANG | f2804e5 | 2016-05-30 11:16:08 +0800 | [diff] [blame] | 330 | [&](const std::pair<jweak, NativeLoaderNamespace>& value) { |
Dimitry Ivanov | f44ecde | 2016-02-22 13:48:22 -0800 | [diff] [blame] | 331 | return env->IsSameObject(value.first, class_loader); |
| 332 | }); |
Zhenhua WANG | f2804e5 | 2016-05-30 11:16:08 +0800 | [diff] [blame] | 333 | if (it != namespaces_.end()) { |
Victor Khimenko | 1443ec4 | 2018-07-09 17:01:22 +0200 | [diff] [blame] | 334 | return &it->second; |
Zhenhua WANG | f2804e5 | 2016-05-30 11:16:08 +0800 | [diff] [blame] | 335 | } |
| 336 | |
Victor Khimenko | 1443ec4 | 2018-07-09 17:01:22 +0200 | [diff] [blame] | 337 | return nullptr; |
Dimitry Ivanov | f44ecde | 2016-02-22 13:48:22 -0800 | [diff] [blame] | 338 | } |
| 339 | |
Dimitry Ivanov | 4b0e963 | 2016-03-15 13:51:26 -0700 | [diff] [blame] | 340 | void Initialize() { |
Dimitry Ivanov | 80ddb8f | 2016-05-09 18:12:00 -0700 | [diff] [blame] | 341 | // Once public namespace is initialized there is no |
| 342 | // point in running this code - it will have no effect |
| 343 | // on the current list of public libraries. |
| 344 | if (initialized_) { |
| 345 | return; |
| 346 | } |
| 347 | |
Dimitry Ivanov | b614045 | 2016-04-06 18:24:08 -0700 | [diff] [blame] | 348 | std::vector<std::string> sonames; |
Dimitry Ivanov | 0b5651e | 2016-04-21 16:42:48 -0700 | [diff] [blame] | 349 | const char* android_root_env = getenv("ANDROID_ROOT"); |
| 350 | std::string root_dir = android_root_env != nullptr ? android_root_env : "/system"; |
| 351 | std::string public_native_libraries_system_config = |
| 352 | root_dir + kPublicNativeLibrariesSystemConfigPathFromRoot; |
Jiyong Park | a07f305 | 2017-08-22 10:26:10 +0900 | [diff] [blame] | 353 | std::string llndk_native_libraries_system_config = |
| 354 | root_dir + kLlndkNativeLibrariesSystemConfigPathFromRoot; |
| 355 | std::string vndksp_native_libraries_system_config = |
| 356 | root_dir + kVndkspNativeLibrariesSystemConfigPathFromRoot; |
Dimitry Ivanov | b614045 | 2016-04-06 18:24:08 -0700 | [diff] [blame] | 357 | |
Inseob Kim | 67cb056 | 2018-05-04 11:39:12 +0900 | [diff] [blame] | 358 | std::string product_public_native_libraries_dir = "/product/etc"; |
| 359 | |
Christopher Ferris | 39da84b | 2016-06-21 16:11:23 -0700 | [diff] [blame] | 360 | std::string error_msg; |
Jiyong Park | d1006fe | 2017-11-08 18:44:09 +0900 | [diff] [blame] | 361 | LOG_ALWAYS_FATAL_IF( |
| 362 | !ReadConfig(public_native_libraries_system_config, &sonames, always_true, &error_msg), |
| 363 | "Error reading public native library list from \"%s\": %s", |
| 364 | public_native_libraries_system_config.c_str(), error_msg.c_str()); |
| 365 | |
Jiyong Park | e031994 | 2018-01-15 21:14:53 +0900 | [diff] [blame] | 366 | // For debuggable platform builds use ANDROID_ADDITIONAL_PUBLIC_LIBRARIES environment |
| 367 | // variable to add libraries to the list. This is intended for platform tests only. |
| 368 | if (is_debuggable()) { |
| 369 | const char* additional_libs = getenv("ANDROID_ADDITIONAL_PUBLIC_LIBRARIES"); |
| 370 | if (additional_libs != nullptr && additional_libs[0] != '\0') { |
| 371 | std::vector<std::string> additional_libs_vector = base::Split(additional_libs, ":"); |
| 372 | std::copy(additional_libs_vector.begin(), additional_libs_vector.end(), |
| 373 | std::back_inserter(sonames)); |
| 374 | } |
| 375 | } |
| 376 | |
| 377 | // android_init_namespaces() expects all the public libraries |
| 378 | // to be loaded so that they can be found by soname alone. |
| 379 | // |
| 380 | // TODO(dimitry): this is a bit misleading since we do not know |
| 381 | // 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] | 382 | // 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] | 383 | // For now we rely on CTS test to catch things like this but |
| 384 | // it should probably be addressed in the future. |
| 385 | for (const auto& soname : sonames) { |
| 386 | LOG_ALWAYS_FATAL_IF(dlopen(soname.c_str(), RTLD_NOW | RTLD_NODELETE) == nullptr, |
| 387 | "Error preloading public library %s: %s", soname.c_str(), dlerror()); |
| 388 | } |
| 389 | |
| 390 | system_public_libraries_ = base::Join(sonames, ':'); |
| 391 | |
Jiyong Park | d1006fe | 2017-11-08 18:44:09 +0900 | [diff] [blame] | 392 | // read /system/etc/public.libraries-<companyname>.txt which contain partner defined |
| 393 | // system libs that are exposed to apps. The libs in the txt files must be |
| 394 | // named as lib<name>.<companyname>.so. |
Jiyong Park | e031994 | 2018-01-15 21:14:53 +0900 | [diff] [blame] | 395 | sonames.clear(); |
Inseob Kim | 67cb056 | 2018-05-04 11:39:12 +0900 | [diff] [blame] | 396 | ReadExtensionLibraries(base::Dirname(public_native_libraries_system_config).c_str(), &sonames); |
Jiyong Park | e031994 | 2018-01-15 21:14:53 +0900 | [diff] [blame] | 397 | oem_public_libraries_ = base::Join(sonames, ':'); |
Dimitry Ivanov | 7d02829 | 2016-05-05 17:30:24 -0700 | [diff] [blame] | 398 | |
Inseob Kim | 67cb056 | 2018-05-04 11:39:12 +0900 | [diff] [blame] | 399 | // read /product/etc/public.libraries-<companyname>.txt which contain partner defined |
| 400 | // product libs that are exposed to apps. |
| 401 | sonames.clear(); |
| 402 | ReadExtensionLibraries(product_public_native_libraries_dir.c_str(), &sonames); |
| 403 | product_public_libraries_ = base::Join(sonames, ':'); |
| 404 | |
Justin Yun | 4a1d110 | 2017-11-27 17:04:14 +0900 | [diff] [blame] | 405 | // Insert VNDK version to llndk and vndksp config file names. |
| 406 | insert_vndk_version_str(&llndk_native_libraries_system_config); |
| 407 | insert_vndk_version_str(&vndksp_native_libraries_system_config); |
| 408 | |
Dimitry Ivanov | 882cad2 | 2017-05-01 15:12:49 -0700 | [diff] [blame] | 409 | sonames.clear(); |
Jiyong Park | d1006fe | 2017-11-08 18:44:09 +0900 | [diff] [blame] | 410 | ReadConfig(llndk_native_libraries_system_config, &sonames, always_true); |
Jiyong Park | a07f305 | 2017-08-22 10:26:10 +0900 | [diff] [blame] | 411 | system_llndk_libraries_ = base::Join(sonames, ':'); |
| 412 | |
| 413 | sonames.clear(); |
Jiyong Park | d1006fe | 2017-11-08 18:44:09 +0900 | [diff] [blame] | 414 | ReadConfig(vndksp_native_libraries_system_config, &sonames, always_true); |
Jiyong Park | a07f305 | 2017-08-22 10:26:10 +0900 | [diff] [blame] | 415 | system_vndksp_libraries_ = base::Join(sonames, ':'); |
| 416 | |
| 417 | sonames.clear(); |
Dimitry Ivanov | 882cad2 | 2017-05-01 15:12:49 -0700 | [diff] [blame] | 418 | // This file is optional, quietly ignore if the file does not exist. |
Jiyong Park | d1006fe | 2017-11-08 18:44:09 +0900 | [diff] [blame] | 419 | ReadConfig(kPublicNativeLibrariesVendorConfig, &sonames, always_true, nullptr); |
Dimitry Ivanov | 882cad2 | 2017-05-01 15:12:49 -0700 | [diff] [blame] | 420 | |
| 421 | vendor_public_libraries_ = base::Join(sonames, ':'); |
Dimitry Ivanov | b614045 | 2016-04-06 18:24:08 -0700 | [diff] [blame] | 422 | } |
| 423 | |
Inseob Kim | 67cb056 | 2018-05-04 11:39:12 +0900 | [diff] [blame] | 424 | void Reset() { namespaces_.clear(); } |
Dimitry Ivanov | be4ca3a | 2016-05-02 10:43:16 -0700 | [diff] [blame] | 425 | |
Dimitry Ivanov | b614045 | 2016-04-06 18:24:08 -0700 | [diff] [blame] | 426 | private: |
Inseob Kim | 67cb056 | 2018-05-04 11:39:12 +0900 | [diff] [blame] | 427 | void ReadExtensionLibraries(const char* dirname, std::vector<std::string>* sonames) { |
| 428 | std::unique_ptr<DIR, decltype(&closedir)> dir(opendir(dirname), closedir); |
| 429 | if (dir != nullptr) { |
| 430 | // Failing to opening the dir is not an error, which can happen in |
| 431 | // webview_zygote. |
| 432 | while (struct dirent* ent = readdir(dir.get())) { |
| 433 | if (ent->d_type != DT_REG && ent->d_type != DT_LNK) { |
| 434 | continue; |
| 435 | } |
| 436 | const std::string filename(ent->d_name); |
| 437 | if (android::base::StartsWith(filename, kPublicNativeLibrariesExtensionConfigPrefix) && |
| 438 | android::base::EndsWith(filename, kPublicNativeLibrariesExtensionConfigSuffix)) { |
| 439 | const size_t start = kPublicNativeLibrariesExtensionConfigPrefixLen; |
| 440 | const size_t end = filename.size() - kPublicNativeLibrariesExtensionConfigSuffixLen; |
| 441 | const std::string company_name = filename.substr(start, end - start); |
| 442 | const std::string config_file_path = dirname + "/"s + filename; |
| 443 | LOG_ALWAYS_FATAL_IF( |
| 444 | company_name.empty(), |
| 445 | "Error extracting company name from public native library list file path \"%s\"", |
| 446 | config_file_path.c_str()); |
| 447 | |
| 448 | std::string error_msg; |
| 449 | |
| 450 | LOG_ALWAYS_FATAL_IF( |
| 451 | !ReadConfig( |
| 452 | config_file_path, sonames, |
| 453 | [&company_name](const std::string& soname, std::string* error_msg) { |
| 454 | if (android::base::StartsWith(soname, "lib") && |
| 455 | android::base::EndsWith(soname, "." + company_name + ".so")) { |
| 456 | return true; |
| 457 | } else { |
| 458 | *error_msg = "Library name \"" + soname + |
| 459 | "\" does not end with the company name: " + company_name + "."; |
| 460 | return false; |
| 461 | } |
| 462 | }, |
| 463 | &error_msg), |
| 464 | "Error reading public native library list from \"%s\": %s", config_file_path.c_str(), |
| 465 | error_msg.c_str()); |
| 466 | } |
| 467 | } |
| 468 | } |
| 469 | } |
| 470 | |
| 471 | |
Christopher Ferris | 39da84b | 2016-06-21 16:11:23 -0700 | [diff] [blame] | 472 | bool ReadConfig(const std::string& configFile, std::vector<std::string>* sonames, |
Jiyong Park | d1006fe | 2017-11-08 18:44:09 +0900 | [diff] [blame] | 473 | const std::function<bool(const std::string& /* soname */, |
| 474 | std::string* /* error_msg */)>& check_soname, |
Christopher Ferris | 39da84b | 2016-06-21 16:11:23 -0700 | [diff] [blame] | 475 | std::string* error_msg = nullptr) { |
Dimitry Ivanov | 4b0e963 | 2016-03-15 13:51:26 -0700 | [diff] [blame] | 476 | // Read list of public native libraries from the config file. |
| 477 | std::string file_content; |
Dimitry Ivanov | b614045 | 2016-04-06 18:24:08 -0700 | [diff] [blame] | 478 | if(!base::ReadFileToString(configFile, &file_content)) { |
Christopher Ferris | 39da84b | 2016-06-21 16:11:23 -0700 | [diff] [blame] | 479 | if (error_msg) *error_msg = strerror(errno); |
Dimitry Ivanov | b614045 | 2016-04-06 18:24:08 -0700 | [diff] [blame] | 480 | return false; |
| 481 | } |
Dimitry Ivanov | 4b0e963 | 2016-03-15 13:51:26 -0700 | [diff] [blame] | 482 | |
| 483 | std::vector<std::string> lines = base::Split(file_content, "\n"); |
| 484 | |
Christopher Ferris | 39da84b | 2016-06-21 16:11:23 -0700 | [diff] [blame] | 485 | for (auto& line : lines) { |
Dimitry Ivanov | 4b0e963 | 2016-03-15 13:51:26 -0700 | [diff] [blame] | 486 | auto trimmed_line = base::Trim(line); |
| 487 | if (trimmed_line[0] == '#' || trimmed_line.empty()) { |
| 488 | continue; |
| 489 | } |
Christopher Ferris | 39da84b | 2016-06-21 16:11:23 -0700 | [diff] [blame] | 490 | size_t space_pos = trimmed_line.rfind(' '); |
| 491 | if (space_pos != std::string::npos) { |
| 492 | std::string type = trimmed_line.substr(space_pos + 1); |
| 493 | if (type != "32" && type != "64") { |
| 494 | if (error_msg) *error_msg = "Malformed line: " + line; |
| 495 | return false; |
| 496 | } |
| 497 | #if defined(__LP64__) |
| 498 | // Skip 32 bit public library. |
| 499 | if (type == "32") { |
| 500 | continue; |
| 501 | } |
| 502 | #else |
| 503 | // Skip 64 bit public library. |
| 504 | if (type == "64") { |
| 505 | continue; |
| 506 | } |
| 507 | #endif |
| 508 | trimmed_line.resize(space_pos); |
| 509 | } |
Dimitry Ivanov | 4b0e963 | 2016-03-15 13:51:26 -0700 | [diff] [blame] | 510 | |
Jiyong Park | d1006fe | 2017-11-08 18:44:09 +0900 | [diff] [blame] | 511 | if (check_soname(trimmed_line, error_msg)) { |
| 512 | sonames->push_back(trimmed_line); |
| 513 | } else { |
| 514 | return false; |
| 515 | } |
Dimitry Ivanov | 4b0e963 | 2016-03-15 13:51:26 -0700 | [diff] [blame] | 516 | } |
| 517 | |
Dimitry Ivanov | b614045 | 2016-04-06 18:24:08 -0700 | [diff] [blame] | 518 | return true; |
Dimitry Ivanov | d68c8e9 | 2016-02-10 14:09:22 -0800 | [diff] [blame] | 519 | } |
Dimitry Ivanov | ac1b191 | 2015-12-01 13:56:44 -0800 | [diff] [blame] | 520 | |
Zhenhua WANG | f2804e5 | 2016-05-30 11:16:08 +0800 | [diff] [blame] | 521 | bool InitPublicNamespace(const char* library_path, std::string* error_msg) { |
| 522 | // Ask native bride if this apps library path should be handled by it |
| 523 | bool is_native_bridge = NativeBridgeIsPathSupported(library_path); |
| 524 | |
Dimitry Ivanov | 4b0e963 | 2016-03-15 13:51:26 -0700 | [diff] [blame] | 525 | // (http://b/25844435) - Some apps call dlopen from generated code (mono jited |
| 526 | // code is one example) unknown to linker in which case linker uses anonymous |
| 527 | // namespace. The second argument specifies the search path for the anonymous |
| 528 | // namespace which is the library_path of the classloader. |
Dimitry Ivanov | 882cad2 | 2017-05-01 15:12:49 -0700 | [diff] [blame] | 529 | initialized_ = android_init_anonymous_namespace(system_public_libraries_.c_str(), |
Dimitry Ivanov | 26e1a84 | 2017-02-03 14:11:27 -0800 | [diff] [blame] | 530 | is_native_bridge ? nullptr : library_path); |
Dimitry Ivanov | d836ab0 | 2016-11-02 18:03:10 -0700 | [diff] [blame] | 531 | if (!initialized_) { |
| 532 | *error_msg = dlerror(); |
| 533 | return false; |
| 534 | } |
| 535 | |
| 536 | // and now initialize native bridge namespaces if necessary. |
| 537 | if (NativeBridgeInitialized()) { |
Dimitry Ivanov | 882cad2 | 2017-05-01 15:12:49 -0700 | [diff] [blame] | 538 | initialized_ = NativeBridgeInitAnonymousNamespace(system_public_libraries_.c_str(), |
Zhenhua WANG | e8fb11d | 2017-02-27 10:14:45 +0800 | [diff] [blame] | 539 | is_native_bridge ? library_path : nullptr); |
Zhenhua WANG | f2804e5 | 2016-05-30 11:16:08 +0800 | [diff] [blame] | 540 | if (!initialized_) { |
| 541 | *error_msg = NativeBridgeGetError(); |
| 542 | } |
| 543 | } |
Dimitry Ivanov | ac1b191 | 2015-12-01 13:56:44 -0800 | [diff] [blame] | 544 | |
| 545 | return initialized_; |
| 546 | } |
| 547 | |
Dimitry Ivanov | 24db75c | 2016-05-12 15:34:41 -0700 | [diff] [blame] | 548 | jobject GetParentClassLoader(JNIEnv* env, jobject class_loader) { |
| 549 | jclass class_loader_class = env->FindClass("java/lang/ClassLoader"); |
| 550 | jmethodID get_parent = env->GetMethodID(class_loader_class, |
| 551 | "getParent", |
| 552 | "()Ljava/lang/ClassLoader;"); |
| 553 | |
| 554 | return env->CallObjectMethod(class_loader, get_parent); |
| 555 | } |
| 556 | |
Victor Khimenko | 1443ec4 | 2018-07-09 17:01:22 +0200 | [diff] [blame] | 557 | NativeLoaderNamespace* FindParentNamespaceByClassLoader(JNIEnv* env, jobject class_loader) { |
Dimitry Ivanov | 24db75c | 2016-05-12 15:34:41 -0700 | [diff] [blame] | 558 | jobject parent_class_loader = GetParentClassLoader(env, class_loader); |
| 559 | |
| 560 | while (parent_class_loader != nullptr) { |
Victor Khimenko | 1443ec4 | 2018-07-09 17:01:22 +0200 | [diff] [blame] | 561 | NativeLoaderNamespace* ns; |
| 562 | if ((ns = FindNamespaceByClassLoader(env, parent_class_loader)) != nullptr) { |
| 563 | return ns; |
Dimitry Ivanov | 24db75c | 2016-05-12 15:34:41 -0700 | [diff] [blame] | 564 | } |
| 565 | |
| 566 | parent_class_loader = GetParentClassLoader(env, parent_class_loader); |
| 567 | } |
Zhenhua WANG | f2804e5 | 2016-05-30 11:16:08 +0800 | [diff] [blame] | 568 | |
Victor Khimenko | 1443ec4 | 2018-07-09 17:01:22 +0200 | [diff] [blame] | 569 | return nullptr; |
Dimitry Ivanov | 24db75c | 2016-05-12 15:34:41 -0700 | [diff] [blame] | 570 | } |
| 571 | |
Dimitry Ivanov | ac1b191 | 2015-12-01 13:56:44 -0800 | [diff] [blame] | 572 | bool initialized_; |
Victor Khimenko | 1443ec4 | 2018-07-09 17:01:22 +0200 | [diff] [blame] | 573 | std::list<std::pair<jweak, NativeLoaderNamespace>> namespaces_; |
Dimitry Ivanov | 882cad2 | 2017-05-01 15:12:49 -0700 | [diff] [blame] | 574 | std::string system_public_libraries_; |
| 575 | std::string vendor_public_libraries_; |
Jiyong Park | e031994 | 2018-01-15 21:14:53 +0900 | [diff] [blame] | 576 | std::string oem_public_libraries_; |
Inseob Kim | 67cb056 | 2018-05-04 11:39:12 +0900 | [diff] [blame] | 577 | std::string product_public_libraries_; |
Jiyong Park | a07f305 | 2017-08-22 10:26:10 +0900 | [diff] [blame] | 578 | std::string system_llndk_libraries_; |
| 579 | std::string system_vndksp_libraries_; |
Dimitry Ivanov | ac1b191 | 2015-12-01 13:56:44 -0800 | [diff] [blame] | 580 | |
| 581 | DISALLOW_COPY_AND_ASSIGN(LibraryNamespaces); |
| 582 | }; |
| 583 | |
Dimitry Ivanov | 34d5a20 | 2016-02-29 13:21:43 -0800 | [diff] [blame] | 584 | static std::mutex g_namespaces_mutex; |
Dimitry Ivanov | ac1b191 | 2015-12-01 13:56:44 -0800 | [diff] [blame] | 585 | static LibraryNamespaces* g_namespaces = new LibraryNamespaces; |
| 586 | #endif |
| 587 | |
Dimitry Ivanov | 4b0e963 | 2016-03-15 13:51:26 -0700 | [diff] [blame] | 588 | void InitializeNativeLoader() { |
Dimitry Ivanov | 426799d | 2016-02-22 11:27:48 -0800 | [diff] [blame] | 589 | #if defined(__ANDROID__) |
Dimitry Ivanov | 34d5a20 | 2016-02-29 13:21:43 -0800 | [diff] [blame] | 590 | std::lock_guard<std::mutex> guard(g_namespaces_mutex); |
Dimitry Ivanov | 4b0e963 | 2016-03-15 13:51:26 -0700 | [diff] [blame] | 591 | g_namespaces->Initialize(); |
Dimitry Ivanov | 426799d | 2016-02-22 11:27:48 -0800 | [diff] [blame] | 592 | #endif |
| 593 | } |
| 594 | |
Dimitry Ivanov | be4ca3a | 2016-05-02 10:43:16 -0700 | [diff] [blame] | 595 | void ResetNativeLoader() { |
| 596 | #if defined(__ANDROID__) |
| 597 | std::lock_guard<std::mutex> guard(g_namespaces_mutex); |
| 598 | g_namespaces->Reset(); |
| 599 | #endif |
| 600 | } |
Dimitry Ivanov | ac1b191 | 2015-12-01 13:56:44 -0800 | [diff] [blame] | 601 | |
Dimitry Ivanov | d047c92 | 2016-02-23 14:23:51 -0800 | [diff] [blame] | 602 | jstring CreateClassLoaderNamespace(JNIEnv* env, |
| 603 | int32_t target_sdk_version, |
| 604 | jobject class_loader, |
| 605 | bool is_shared, |
Jiyong Park | a07f305 | 2017-08-22 10:26:10 +0900 | [diff] [blame] | 606 | bool is_for_vendor, |
Dimitry Ivanov | d047c92 | 2016-02-23 14:23:51 -0800 | [diff] [blame] | 607 | jstring library_path, |
| 608 | jstring permitted_path) { |
Dimitry Ivanov | ac1b191 | 2015-12-01 13:56:44 -0800 | [diff] [blame] | 609 | #if defined(__ANDROID__) |
Dimitry Ivanov | 34d5a20 | 2016-02-29 13:21:43 -0800 | [diff] [blame] | 610 | std::lock_guard<std::mutex> guard(g_namespaces_mutex); |
Zhenhua WANG | f2804e5 | 2016-05-30 11:16:08 +0800 | [diff] [blame] | 611 | |
| 612 | std::string error_msg; |
Zhenhua WANG | f2804e5 | 2016-05-30 11:16:08 +0800 | [diff] [blame] | 613 | bool success = g_namespaces->Create(env, |
Dimitry Ivanov | 9e253ce | 2017-05-08 22:24:24 -0700 | [diff] [blame] | 614 | target_sdk_version, |
Zhenhua WANG | f2804e5 | 2016-05-30 11:16:08 +0800 | [diff] [blame] | 615 | class_loader, |
| 616 | is_shared, |
Jiyong Park | a07f305 | 2017-08-22 10:26:10 +0900 | [diff] [blame] | 617 | is_for_vendor, |
Zhenhua WANG | f2804e5 | 2016-05-30 11:16:08 +0800 | [diff] [blame] | 618 | library_path, |
| 619 | permitted_path, |
Victor Khimenko | 1443ec4 | 2018-07-09 17:01:22 +0200 | [diff] [blame] | 620 | &error_msg) != nullptr; |
Zhenhua WANG | f2804e5 | 2016-05-30 11:16:08 +0800 | [diff] [blame] | 621 | if (!success) { |
| 622 | return env->NewStringUTF(error_msg.c_str()); |
Dimitry Ivanov | d047c92 | 2016-02-23 14:23:51 -0800 | [diff] [blame] | 623 | } |
| 624 | #else |
Jiyong Park | a07f305 | 2017-08-22 10:26:10 +0900 | [diff] [blame] | 625 | UNUSED(env, target_sdk_version, class_loader, is_shared, is_for_vendor, |
Dimitry Ivanov | d047c92 | 2016-02-23 14:23:51 -0800 | [diff] [blame] | 626 | library_path, permitted_path); |
| 627 | #endif |
| 628 | return nullptr; |
| 629 | } |
| 630 | |
| 631 | void* OpenNativeLibrary(JNIEnv* env, |
| 632 | int32_t target_sdk_version, |
| 633 | const char* path, |
| 634 | jobject class_loader, |
Zhenhua WANG | f2804e5 | 2016-05-30 11:16:08 +0800 | [diff] [blame] | 635 | jstring library_path, |
| 636 | bool* needs_native_bridge, |
| 637 | std::string* error_msg) { |
Dimitry Ivanov | d047c92 | 2016-02-23 14:23:51 -0800 | [diff] [blame] | 638 | #if defined(__ANDROID__) |
Dimitry Ivanov | 5539db0 | 2016-04-20 16:07:30 -0700 | [diff] [blame] | 639 | UNUSED(target_sdk_version); |
| 640 | if (class_loader == nullptr) { |
Zhenhua WANG | f2804e5 | 2016-05-30 11:16:08 +0800 | [diff] [blame] | 641 | *needs_native_bridge = false; |
Pete Bentley | 632f142 | 2018-12-19 13:33:33 +0000 | [diff] [blame^] | 642 | void* handle = dlopen(path, RTLD_NOW); |
| 643 | if (handle == nullptr) { |
| 644 | *error_msg = dlerror(); |
| 645 | } |
| 646 | return handle; |
Dimitry Ivanov | ac1b191 | 2015-12-01 13:56:44 -0800 | [diff] [blame] | 647 | } |
| 648 | |
Dimitry Ivanov | 34d5a20 | 2016-02-29 13:21:43 -0800 | [diff] [blame] | 649 | std::lock_guard<std::mutex> guard(g_namespaces_mutex); |
Victor Khimenko | 1443ec4 | 2018-07-09 17:01:22 +0200 | [diff] [blame] | 650 | NativeLoaderNamespace* ns; |
Dimitry Ivanov | ac1b191 | 2015-12-01 13:56:44 -0800 | [diff] [blame] | 651 | |
Victor Khimenko | 1443ec4 | 2018-07-09 17:01:22 +0200 | [diff] [blame] | 652 | if ((ns = g_namespaces->FindNamespaceByClassLoader(env, class_loader)) == nullptr) { |
Dimitry Ivanov | d047c92 | 2016-02-23 14:23:51 -0800 | [diff] [blame] | 653 | // This is the case where the classloader was not created by ApplicationLoaders |
| 654 | // In this case we create an isolated not-shared namespace for it. |
Victor Khimenko | 1443ec4 | 2018-07-09 17:01:22 +0200 | [diff] [blame] | 655 | if ((ns = g_namespaces->Create(env, |
| 656 | target_sdk_version, |
| 657 | class_loader, |
| 658 | false /* is_shared */, |
| 659 | false /* is_for_vendor */, |
| 660 | library_path, |
| 661 | nullptr, |
| 662 | error_msg)) == nullptr) { |
Dimitry Ivanov | d047c92 | 2016-02-23 14:23:51 -0800 | [diff] [blame] | 663 | return nullptr; |
| 664 | } |
Dimitry Ivanov | ac1b191 | 2015-12-01 13:56:44 -0800 | [diff] [blame] | 665 | } |
| 666 | |
Victor Khimenko | 1443ec4 | 2018-07-09 17:01:22 +0200 | [diff] [blame] | 667 | return OpenNativeLibrary(ns, path, needs_native_bridge, error_msg); |
Dimitry Ivanov | ac1b191 | 2015-12-01 13:56:44 -0800 | [diff] [blame] | 668 | #else |
Andreas Gampe | 5c7d582 | 2017-12-28 19:08:13 -0800 | [diff] [blame] | 669 | UNUSED(env, target_sdk_version, class_loader); |
| 670 | |
| 671 | // Do some best effort to emulate library-path support. It will not |
| 672 | // work for dependencies. |
| 673 | // |
| 674 | // Note: null has a special meaning and must be preserved. |
| 675 | std::string c_library_path; // Empty string by default. |
| 676 | if (library_path != nullptr && path != nullptr && path[0] != '/') { |
| 677 | ScopedUtfChars library_path_utf_chars(env, library_path); |
| 678 | c_library_path = library_path_utf_chars.c_str(); |
| 679 | } |
| 680 | |
| 681 | std::vector<std::string> library_paths = base::Split(c_library_path, ":"); |
| 682 | |
| 683 | for (const std::string& lib_path : library_paths) { |
| 684 | *needs_native_bridge = false; |
| 685 | const char* path_arg; |
| 686 | std::string complete_path; |
| 687 | if (path == nullptr) { |
| 688 | // Preserve null. |
| 689 | path_arg = nullptr; |
Zhenhua WANG | f2804e5 | 2016-05-30 11:16:08 +0800 | [diff] [blame] | 690 | } else { |
Andreas Gampe | 5c7d582 | 2017-12-28 19:08:13 -0800 | [diff] [blame] | 691 | complete_path = lib_path; |
| 692 | if (!complete_path.empty()) { |
| 693 | complete_path.append("/"); |
| 694 | } |
| 695 | complete_path.append(path); |
| 696 | path_arg = complete_path.c_str(); |
| 697 | } |
| 698 | void* handle = dlopen(path_arg, RTLD_NOW); |
| 699 | if (handle != nullptr) { |
| 700 | return handle; |
| 701 | } |
| 702 | if (NativeBridgeIsSupported(path_arg)) { |
| 703 | *needs_native_bridge = true; |
| 704 | handle = NativeBridgeLoadLibrary(path_arg, RTLD_NOW); |
| 705 | if (handle != nullptr) { |
| 706 | return handle; |
| 707 | } |
| 708 | *error_msg = NativeBridgeGetError(); |
| 709 | } else { |
Zhenhua WANG | f2804e5 | 2016-05-30 11:16:08 +0800 | [diff] [blame] | 710 | *error_msg = dlerror(); |
| 711 | } |
| 712 | } |
Andreas Gampe | 5c7d582 | 2017-12-28 19:08:13 -0800 | [diff] [blame] | 713 | return nullptr; |
Dimitry Ivanov | ac1b191 | 2015-12-01 13:56:44 -0800 | [diff] [blame] | 714 | #endif |
| 715 | } |
| 716 | |
dimitry | 3150f7c | 2018-09-12 01:09:19 +0200 | [diff] [blame] | 717 | bool CloseNativeLibrary(void* handle, const bool needs_native_bridge, std::string* error_msg) { |
| 718 | bool success; |
| 719 | if (needs_native_bridge) { |
| 720 | success = (NativeBridgeUnloadLibrary(handle) == 0); |
| 721 | if (!success) { |
| 722 | *error_msg = NativeBridgeGetError(); |
| 723 | } |
| 724 | } else { |
| 725 | success = (dlclose(handle) == 0); |
| 726 | if (!success) { |
| 727 | *error_msg = dlerror(); |
| 728 | } |
| 729 | } |
| 730 | |
| 731 | return success; |
Dimitry Ivanov | 09a516b | 2016-05-03 14:55:25 -0700 | [diff] [blame] | 732 | } |
| 733 | |
Dimitry Ivanov | f44ecde | 2016-02-22 13:48:22 -0800 | [diff] [blame] | 734 | #if defined(__ANDROID__) |
Victor Khimenko | 1443ec4 | 2018-07-09 17:01:22 +0200 | [diff] [blame] | 735 | void* OpenNativeLibrary(NativeLoaderNamespace* ns, const char* path, bool* needs_native_bridge, |
| 736 | std::string* error_msg) { |
| 737 | if (ns->is_android_namespace()) { |
| 738 | android_dlextinfo extinfo; |
| 739 | extinfo.flags = ANDROID_DLEXT_USE_NAMESPACE; |
| 740 | extinfo.library_namespace = ns->get_android_ns(); |
| 741 | |
| 742 | void* handle = android_dlopen_ext(path, RTLD_NOW, &extinfo); |
| 743 | if (handle == nullptr) { |
| 744 | *error_msg = dlerror(); |
| 745 | } |
| 746 | *needs_native_bridge = false; |
| 747 | return handle; |
| 748 | } else { |
| 749 | void* handle = NativeBridgeLoadLibraryExt(path, RTLD_NOW, ns->get_native_bridge_ns()); |
| 750 | if (handle == nullptr) { |
| 751 | *error_msg = NativeBridgeGetError(); |
| 752 | } |
| 753 | *needs_native_bridge = true; |
| 754 | return handle; |
| 755 | } |
| 756 | } |
| 757 | |
Dimitry Ivanov | 800083d | 2016-11-01 14:17:00 -0700 | [diff] [blame] | 758 | // native_bridge_namespaces are not supported for callers of this function. |
| 759 | // This function will return nullptr in the case when application is running |
| 760 | // on native bridge. |
Dimitry Ivanov | f44ecde | 2016-02-22 13:48:22 -0800 | [diff] [blame] | 761 | android_namespace_t* FindNamespaceByClassLoader(JNIEnv* env, jobject class_loader) { |
Dimitry Ivanov | 34d5a20 | 2016-02-29 13:21:43 -0800 | [diff] [blame] | 762 | std::lock_guard<std::mutex> guard(g_namespaces_mutex); |
Victor Khimenko | 1443ec4 | 2018-07-09 17:01:22 +0200 | [diff] [blame] | 763 | NativeLoaderNamespace* ns = g_namespaces->FindNamespaceByClassLoader(env, class_loader); |
| 764 | if (ns != nullptr) { |
| 765 | return ns->is_android_namespace() ? ns->get_android_ns() : nullptr; |
Zhenhua WANG | f2804e5 | 2016-05-30 11:16:08 +0800 | [diff] [blame] | 766 | } |
| 767 | |
| 768 | return nullptr; |
Dimitry Ivanov | f44ecde | 2016-02-22 13:48:22 -0800 | [diff] [blame] | 769 | } |
Victor Khimenko | 1443ec4 | 2018-07-09 17:01:22 +0200 | [diff] [blame] | 770 | NativeLoaderNamespace* FindNativeLoaderNamespaceByClassLoader(JNIEnv* env, jobject class_loader) { |
| 771 | std::lock_guard<std::mutex> guard(g_namespaces_mutex); |
| 772 | return g_namespaces->FindNamespaceByClassLoader(env, class_loader); |
| 773 | } |
Dimitry Ivanov | f44ecde | 2016-02-22 13:48:22 -0800 | [diff] [blame] | 774 | #endif |
| 775 | |
Dimitry Ivanov | ac1b191 | 2015-12-01 13:56:44 -0800 | [diff] [blame] | 776 | }; // android namespace |