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