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" |
| 18 | #include "ScopedUtfChars.h" |
| 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 |
Zhenhua WANG | f2804e5 | 2016-05-30 11:16:08 +0800 | [diff] [blame] | 27 | #include "nativebridge/native_bridge.h" |
Dimitry Ivanov | ac1b191 | 2015-12-01 13:56:44 -0800 | [diff] [blame] | 28 | |
| 29 | #include <algorithm> |
| 30 | #include <vector> |
| 31 | #include <string> |
| 32 | #include <mutex> |
| 33 | |
Mark Salyzyn | ff2dcd9 | 2016-09-28 15:54:45 -0700 | [diff] [blame] | 34 | #include <android-base/file.h> |
| 35 | #include <android-base/macros.h> |
| 36 | #include <android-base/strings.h> |
Dimitry Ivanov | ac1b191 | 2015-12-01 13:56:44 -0800 | [diff] [blame] | 37 | |
Zhenhua WANG | f2804e5 | 2016-05-30 11:16:08 +0800 | [diff] [blame] | 38 | #define CHECK(predicate) LOG_ALWAYS_FATAL_IF(!(predicate),\ |
| 39 | "%s:%d: %s CHECK '" #predicate "' failed.",\ |
| 40 | __FILE__, __LINE__, __FUNCTION__) |
| 41 | |
Dimitry Ivanov | ac1b191 | 2015-12-01 13:56:44 -0800 | [diff] [blame] | 42 | namespace android { |
| 43 | |
Dimitry Ivanov | 4b0e963 | 2016-03-15 13:51:26 -0700 | [diff] [blame] | 44 | #if defined(__ANDROID__) |
Zhenhua WANG | f2804e5 | 2016-05-30 11:16:08 +0800 | [diff] [blame] | 45 | class NativeLoaderNamespace { |
| 46 | public: |
| 47 | NativeLoaderNamespace() |
| 48 | : android_ns_(nullptr), native_bridge_ns_(nullptr) { } |
| 49 | |
| 50 | explicit NativeLoaderNamespace(android_namespace_t* ns) |
| 51 | : android_ns_(ns), native_bridge_ns_(nullptr) { } |
| 52 | |
| 53 | explicit NativeLoaderNamespace(native_bridge_namespace_t* ns) |
| 54 | : android_ns_(nullptr), native_bridge_ns_(ns) { } |
| 55 | |
| 56 | NativeLoaderNamespace(NativeLoaderNamespace&& that) = default; |
| 57 | NativeLoaderNamespace(const NativeLoaderNamespace& that) = default; |
| 58 | |
| 59 | NativeLoaderNamespace& operator=(const NativeLoaderNamespace& that) = default; |
| 60 | |
| 61 | android_namespace_t* get_android_ns() const { |
| 62 | CHECK(native_bridge_ns_ == nullptr); |
| 63 | return android_ns_; |
| 64 | } |
| 65 | |
| 66 | native_bridge_namespace_t* get_native_bridge_ns() const { |
| 67 | CHECK(android_ns_ == nullptr); |
| 68 | return native_bridge_ns_; |
| 69 | } |
| 70 | |
| 71 | bool is_android_namespace() const { |
| 72 | return native_bridge_ns_ == nullptr; |
| 73 | } |
| 74 | |
| 75 | private: |
| 76 | // Only one of them can be not null |
| 77 | android_namespace_t* android_ns_; |
| 78 | native_bridge_namespace_t* native_bridge_ns_; |
| 79 | }; |
| 80 | |
| 81 | static constexpr const char* kPublicNativeLibrariesSystemConfigPathFromRoot = |
| 82 | "/etc/public.libraries.txt"; |
| 83 | static constexpr const char* kPublicNativeLibrariesVendorConfig = |
| 84 | "/vendor/etc/public.libraries.txt"; |
Dimitry Ivanov | ac1b191 | 2015-12-01 13:56:44 -0800 | [diff] [blame] | 85 | |
Dimitry Ivanov | f334cbf | 2016-05-10 10:39:48 -0700 | [diff] [blame] | 86 | // (http://b/27588281) This is a workaround for apps using custom classloaders and calling |
| 87 | // System.load() with an absolute path which is outside of the classloader library search path. |
| 88 | // This list includes all directories app is allowed to access this way. |
| 89 | static constexpr const char* kWhitelistedDirectories = "/data:/mnt/expand"; |
| 90 | |
Dimitry Ivanov | 7d02829 | 2016-05-05 17:30:24 -0700 | [diff] [blame] | 91 | static bool is_debuggable() { |
| 92 | char debuggable[PROP_VALUE_MAX]; |
| 93 | property_get("ro.debuggable", debuggable, "0"); |
| 94 | return std::string(debuggable) == "1"; |
| 95 | } |
| 96 | |
Dimitry Ivanov | ac1b191 | 2015-12-01 13:56:44 -0800 | [diff] [blame] | 97 | class LibraryNamespaces { |
| 98 | public: |
Dimitry Ivanov | 426799d | 2016-02-22 11:27:48 -0800 | [diff] [blame] | 99 | LibraryNamespaces() : initialized_(false) { } |
Dimitry Ivanov | ac1b191 | 2015-12-01 13:56:44 -0800 | [diff] [blame] | 100 | |
Zhenhua WANG | f2804e5 | 2016-05-30 11:16:08 +0800 | [diff] [blame] | 101 | bool Create(JNIEnv* env, |
Dimitry Ivanov | 9e253ce | 2017-05-08 22:24:24 -0700 | [diff] [blame] | 102 | uint32_t target_sdk_version, |
Zhenhua WANG | f2804e5 | 2016-05-30 11:16:08 +0800 | [diff] [blame] | 103 | jobject class_loader, |
| 104 | bool is_shared, |
| 105 | jstring java_library_path, |
| 106 | jstring java_permitted_path, |
| 107 | NativeLoaderNamespace* ns, |
| 108 | std::string* error_msg) { |
Dimitry Ivanov | cf9892b | 2016-05-09 10:55:50 -0700 | [diff] [blame] | 109 | std::string library_path; // empty string by default. |
| 110 | |
| 111 | if (java_library_path != nullptr) { |
| 112 | ScopedUtfChars library_path_utf_chars(env, java_library_path); |
| 113 | library_path = library_path_utf_chars.c_str(); |
| 114 | } |
Dimitry Ivanov | ac1b191 | 2015-12-01 13:56:44 -0800 | [diff] [blame] | 115 | |
Dimitry Ivanov | f334cbf | 2016-05-10 10:39:48 -0700 | [diff] [blame] | 116 | // (http://b/27588281) This is a workaround for apps using custom |
| 117 | // classloaders and calling System.load() with an absolute path which |
| 118 | // is outside of the classloader library search path. |
| 119 | // |
| 120 | // This part effectively allows such a classloader to access anything |
| 121 | // under /data and /mnt/expand |
| 122 | std::string permitted_path = kWhitelistedDirectories; |
| 123 | |
Dimitry Ivanov | 0d6e594 | 2015-12-08 11:16:56 -0800 | [diff] [blame] | 124 | if (java_permitted_path != nullptr) { |
| 125 | ScopedUtfChars path(env, java_permitted_path); |
Dimitry Ivanov | d0b1531 | 2016-05-10 16:21:25 -0700 | [diff] [blame] | 126 | if (path.c_str() != nullptr && path.size() > 0) { |
| 127 | permitted_path = permitted_path + ":" + path.c_str(); |
| 128 | } |
Dimitry Ivanov | 0d6e594 | 2015-12-08 11:16:56 -0800 | [diff] [blame] | 129 | } |
| 130 | |
Zhenhua WANG | f2804e5 | 2016-05-30 11:16:08 +0800 | [diff] [blame] | 131 | if (!initialized_ && !InitPublicNamespace(library_path.c_str(), error_msg)) { |
| 132 | return false; |
Dimitry Ivanov | ac1b191 | 2015-12-01 13:56:44 -0800 | [diff] [blame] | 133 | } |
| 134 | |
Zhenhua WANG | f2804e5 | 2016-05-30 11:16:08 +0800 | [diff] [blame] | 135 | bool found = FindNamespaceByClassLoader(env, class_loader, nullptr); |
Dimitry Ivanov | ac1b191 | 2015-12-01 13:56:44 -0800 | [diff] [blame] | 136 | |
Zhenhua WANG | f2804e5 | 2016-05-30 11:16:08 +0800 | [diff] [blame] | 137 | LOG_ALWAYS_FATAL_IF(found, |
Dimitry Ivanov | 4b0e963 | 2016-03-15 13:51:26 -0700 | [diff] [blame] | 138 | "There is already a namespace associated with this classloader"); |
Dimitry Ivanov | ac1b191 | 2015-12-01 13:56:44 -0800 | [diff] [blame] | 139 | |
Dimitry Ivanov | d2a6220 | 2015-12-15 11:06:57 -0800 | [diff] [blame] | 140 | uint64_t namespace_type = ANDROID_NAMESPACE_TYPE_ISOLATED; |
| 141 | if (is_shared) { |
| 142 | namespace_type |= ANDROID_NAMESPACE_TYPE_SHARED; |
| 143 | } |
| 144 | |
Dimitry Ivanov | 9e253ce | 2017-05-08 22:24:24 -0700 | [diff] [blame] | 145 | if (target_sdk_version < 24) { |
| 146 | namespace_type |= ANDROID_NAMESPACE_TYPE_GREYLIST_ENABLED; |
| 147 | } |
| 148 | |
Zhenhua WANG | f2804e5 | 2016-05-30 11:16:08 +0800 | [diff] [blame] | 149 | NativeLoaderNamespace parent_ns; |
| 150 | bool found_parent_namespace = FindParentNamespaceByClassLoader(env, class_loader, &parent_ns); |
Dimitry Ivanov | 24db75c | 2016-05-12 15:34:41 -0700 | [diff] [blame] | 151 | |
Zhenhua WANG | f2804e5 | 2016-05-30 11:16:08 +0800 | [diff] [blame] | 152 | bool is_native_bridge = false; |
Dimitry Ivanov | ac1b191 | 2015-12-01 13:56:44 -0800 | [diff] [blame] | 153 | |
Zhenhua WANG | f2804e5 | 2016-05-30 11:16:08 +0800 | [diff] [blame] | 154 | if (found_parent_namespace) { |
| 155 | is_native_bridge = !parent_ns.is_android_namespace(); |
| 156 | } else if (!library_path.empty()) { |
| 157 | is_native_bridge = NativeBridgeIsPathSupported(library_path.c_str()); |
Dimitry Ivanov | d047c92 | 2016-02-23 14:23:51 -0800 | [diff] [blame] | 158 | } |
Dimitry Ivanov | ac1b191 | 2015-12-01 13:56:44 -0800 | [diff] [blame] | 159 | |
Zhenhua WANG | f2804e5 | 2016-05-30 11:16:08 +0800 | [diff] [blame] | 160 | NativeLoaderNamespace native_loader_ns; |
| 161 | if (!is_native_bridge) { |
| 162 | android_namespace_t* ns = android_create_namespace("classloader-namespace", |
| 163 | nullptr, |
| 164 | library_path.c_str(), |
| 165 | namespace_type, |
| 166 | permitted_path.c_str(), |
| 167 | parent_ns.get_android_ns()); |
| 168 | if (ns == nullptr) { |
| 169 | *error_msg = dlerror(); |
| 170 | return false; |
| 171 | } |
| 172 | |
Dimitry Ivanov | 26e1a84 | 2017-02-03 14:11:27 -0800 | [diff] [blame] | 173 | if (!android_link_namespaces(ns, nullptr, public_libraries_.c_str())) { |
| 174 | *error_msg = dlerror(); |
| 175 | return false; |
| 176 | } |
| 177 | |
Zhenhua WANG | f2804e5 | 2016-05-30 11:16:08 +0800 | [diff] [blame] | 178 | native_loader_ns = NativeLoaderNamespace(ns); |
| 179 | } else { |
| 180 | native_bridge_namespace_t* ns = NativeBridgeCreateNamespace("classloader-namespace", |
| 181 | nullptr, |
| 182 | library_path.c_str(), |
| 183 | namespace_type, |
| 184 | permitted_path.c_str(), |
| 185 | parent_ns.get_native_bridge_ns()); |
| 186 | if (ns == nullptr) { |
| 187 | *error_msg = NativeBridgeGetError(); |
| 188 | return false; |
| 189 | } |
| 190 | |
Zhenhua WANG | e8fb11d | 2017-02-27 10:14:45 +0800 | [diff] [blame] | 191 | if (!NativeBridgeLinkNamespaces(ns, nullptr, public_libraries_.c_str())) { |
| 192 | *error_msg = NativeBridgeGetError(); |
| 193 | return false; |
| 194 | } |
| 195 | |
Zhenhua WANG | f2804e5 | 2016-05-30 11:16:08 +0800 | [diff] [blame] | 196 | native_loader_ns = NativeLoaderNamespace(ns); |
| 197 | } |
| 198 | |
| 199 | namespaces_.push_back(std::make_pair(env->NewWeakGlobalRef(class_loader), native_loader_ns)); |
| 200 | |
| 201 | *ns = native_loader_ns; |
| 202 | return true; |
Dimitry Ivanov | ac1b191 | 2015-12-01 13:56:44 -0800 | [diff] [blame] | 203 | } |
| 204 | |
Zhenhua WANG | f2804e5 | 2016-05-30 11:16:08 +0800 | [diff] [blame] | 205 | bool FindNamespaceByClassLoader(JNIEnv* env, jobject class_loader, NativeLoaderNamespace* ns) { |
Dimitry Ivanov | f44ecde | 2016-02-22 13:48:22 -0800 | [diff] [blame] | 206 | auto it = std::find_if(namespaces_.begin(), namespaces_.end(), |
Zhenhua WANG | f2804e5 | 2016-05-30 11:16:08 +0800 | [diff] [blame] | 207 | [&](const std::pair<jweak, NativeLoaderNamespace>& value) { |
Dimitry Ivanov | f44ecde | 2016-02-22 13:48:22 -0800 | [diff] [blame] | 208 | return env->IsSameObject(value.first, class_loader); |
| 209 | }); |
Zhenhua WANG | f2804e5 | 2016-05-30 11:16:08 +0800 | [diff] [blame] | 210 | if (it != namespaces_.end()) { |
| 211 | if (ns != nullptr) { |
| 212 | *ns = it->second; |
| 213 | } |
| 214 | |
| 215 | return true; |
| 216 | } |
| 217 | |
| 218 | return false; |
Dimitry Ivanov | f44ecde | 2016-02-22 13:48:22 -0800 | [diff] [blame] | 219 | } |
| 220 | |
Dimitry Ivanov | 4b0e963 | 2016-03-15 13:51:26 -0700 | [diff] [blame] | 221 | void Initialize() { |
Dimitry Ivanov | 80ddb8f | 2016-05-09 18:12:00 -0700 | [diff] [blame] | 222 | // Once public namespace is initialized there is no |
| 223 | // point in running this code - it will have no effect |
| 224 | // on the current list of public libraries. |
| 225 | if (initialized_) { |
| 226 | return; |
| 227 | } |
| 228 | |
Dimitry Ivanov | b614045 | 2016-04-06 18:24:08 -0700 | [diff] [blame] | 229 | std::vector<std::string> sonames; |
Dimitry Ivanov | 0b5651e | 2016-04-21 16:42:48 -0700 | [diff] [blame] | 230 | const char* android_root_env = getenv("ANDROID_ROOT"); |
| 231 | std::string root_dir = android_root_env != nullptr ? android_root_env : "/system"; |
| 232 | std::string public_native_libraries_system_config = |
| 233 | root_dir + kPublicNativeLibrariesSystemConfigPathFromRoot; |
Dimitry Ivanov | b614045 | 2016-04-06 18:24:08 -0700 | [diff] [blame] | 234 | |
Christopher Ferris | 39da84b | 2016-06-21 16:11:23 -0700 | [diff] [blame] | 235 | std::string error_msg; |
| 236 | LOG_ALWAYS_FATAL_IF(!ReadConfig(public_native_libraries_system_config, &sonames, &error_msg), |
Dimitry Ivanov | b614045 | 2016-04-06 18:24:08 -0700 | [diff] [blame] | 237 | "Error reading public native library list from \"%s\": %s", |
Christopher Ferris | 39da84b | 2016-06-21 16:11:23 -0700 | [diff] [blame] | 238 | public_native_libraries_system_config.c_str(), error_msg.c_str()); |
Dimitry Ivanov | 7d02829 | 2016-05-05 17:30:24 -0700 | [diff] [blame] | 239 | |
| 240 | // For debuggable platform builds use ANDROID_ADDITIONAL_PUBLIC_LIBRARIES environment |
| 241 | // variable to add libraries to the list. This is intended for platform tests only. |
| 242 | if (is_debuggable()) { |
| 243 | const char* additional_libs = getenv("ANDROID_ADDITIONAL_PUBLIC_LIBRARIES"); |
| 244 | if (additional_libs != nullptr && additional_libs[0] != '\0') { |
| 245 | std::vector<std::string> additional_libs_vector = base::Split(additional_libs, ":"); |
| 246 | std::copy(additional_libs_vector.begin(), |
| 247 | additional_libs_vector.end(), |
| 248 | std::back_inserter(sonames)); |
| 249 | } |
| 250 | } |
| 251 | |
Dimitry Ivanov | b614045 | 2016-04-06 18:24:08 -0700 | [diff] [blame] | 252 | // This file is optional, quietly ignore if the file does not exist. |
| 253 | ReadConfig(kPublicNativeLibrariesVendorConfig, &sonames); |
| 254 | |
| 255 | // android_init_namespaces() expects all the public libraries |
| 256 | // to be loaded so that they can be found by soname alone. |
| 257 | // |
| 258 | // TODO(dimitry): this is a bit misleading since we do not know |
| 259 | // if the vendor public library is going to be opened from /vendor/lib |
| 260 | // we might as well end up loading them from /system/lib |
| 261 | // For now we rely on CTS test to catch things like this but |
| 262 | // it should probably be addressed in the future. |
| 263 | for (const auto& soname : sonames) { |
Evan Ralston | 15a264e | 2017-02-03 17:09:46 -0800 | [diff] [blame] | 264 | LOG_ALWAYS_FATAL_IF(dlopen(soname.c_str(), RTLD_NOW | RTLD_NODELETE) == nullptr, |
| 265 | "Error preloading public library %s: %s", |
| 266 | soname.c_str(), dlerror()); |
Dimitry Ivanov | b614045 | 2016-04-06 18:24:08 -0700 | [diff] [blame] | 267 | } |
| 268 | |
| 269 | public_libraries_ = base::Join(sonames, ':'); |
| 270 | } |
| 271 | |
Dimitry Ivanov | be4ca3a | 2016-05-02 10:43:16 -0700 | [diff] [blame] | 272 | void Reset() { |
| 273 | namespaces_.clear(); |
| 274 | } |
| 275 | |
Dimitry Ivanov | b614045 | 2016-04-06 18:24:08 -0700 | [diff] [blame] | 276 | private: |
Christopher Ferris | 39da84b | 2016-06-21 16:11:23 -0700 | [diff] [blame] | 277 | bool ReadConfig(const std::string& configFile, std::vector<std::string>* sonames, |
| 278 | std::string* error_msg = nullptr) { |
Dimitry Ivanov | 4b0e963 | 2016-03-15 13:51:26 -0700 | [diff] [blame] | 279 | // Read list of public native libraries from the config file. |
| 280 | std::string file_content; |
Dimitry Ivanov | b614045 | 2016-04-06 18:24:08 -0700 | [diff] [blame] | 281 | if(!base::ReadFileToString(configFile, &file_content)) { |
Christopher Ferris | 39da84b | 2016-06-21 16:11:23 -0700 | [diff] [blame] | 282 | if (error_msg) *error_msg = strerror(errno); |
Dimitry Ivanov | b614045 | 2016-04-06 18:24:08 -0700 | [diff] [blame] | 283 | return false; |
| 284 | } |
Dimitry Ivanov | 4b0e963 | 2016-03-15 13:51:26 -0700 | [diff] [blame] | 285 | |
| 286 | std::vector<std::string> lines = base::Split(file_content, "\n"); |
| 287 | |
Christopher Ferris | 39da84b | 2016-06-21 16:11:23 -0700 | [diff] [blame] | 288 | for (auto& line : lines) { |
Dimitry Ivanov | 4b0e963 | 2016-03-15 13:51:26 -0700 | [diff] [blame] | 289 | auto trimmed_line = base::Trim(line); |
| 290 | if (trimmed_line[0] == '#' || trimmed_line.empty()) { |
| 291 | continue; |
| 292 | } |
Christopher Ferris | 39da84b | 2016-06-21 16:11:23 -0700 | [diff] [blame] | 293 | size_t space_pos = trimmed_line.rfind(' '); |
| 294 | if (space_pos != std::string::npos) { |
| 295 | std::string type = trimmed_line.substr(space_pos + 1); |
| 296 | if (type != "32" && type != "64") { |
| 297 | if (error_msg) *error_msg = "Malformed line: " + line; |
| 298 | return false; |
| 299 | } |
| 300 | #if defined(__LP64__) |
| 301 | // Skip 32 bit public library. |
| 302 | if (type == "32") { |
| 303 | continue; |
| 304 | } |
| 305 | #else |
| 306 | // Skip 64 bit public library. |
| 307 | if (type == "64") { |
| 308 | continue; |
| 309 | } |
| 310 | #endif |
| 311 | trimmed_line.resize(space_pos); |
| 312 | } |
Dimitry Ivanov | 4b0e963 | 2016-03-15 13:51:26 -0700 | [diff] [blame] | 313 | |
Dimitry Ivanov | b614045 | 2016-04-06 18:24:08 -0700 | [diff] [blame] | 314 | sonames->push_back(trimmed_line); |
Dimitry Ivanov | 4b0e963 | 2016-03-15 13:51:26 -0700 | [diff] [blame] | 315 | } |
| 316 | |
Dimitry Ivanov | b614045 | 2016-04-06 18:24:08 -0700 | [diff] [blame] | 317 | return true; |
Dimitry Ivanov | d68c8e9 | 2016-02-10 14:09:22 -0800 | [diff] [blame] | 318 | } |
Dimitry Ivanov | ac1b191 | 2015-12-01 13:56:44 -0800 | [diff] [blame] | 319 | |
Zhenhua WANG | f2804e5 | 2016-05-30 11:16:08 +0800 | [diff] [blame] | 320 | bool InitPublicNamespace(const char* library_path, std::string* error_msg) { |
| 321 | // Ask native bride if this apps library path should be handled by it |
| 322 | bool is_native_bridge = NativeBridgeIsPathSupported(library_path); |
| 323 | |
Dimitry Ivanov | 4b0e963 | 2016-03-15 13:51:26 -0700 | [diff] [blame] | 324 | // (http://b/25844435) - Some apps call dlopen from generated code (mono jited |
| 325 | // code is one example) unknown to linker in which case linker uses anonymous |
| 326 | // namespace. The second argument specifies the search path for the anonymous |
| 327 | // namespace which is the library_path of the classloader. |
Dimitry Ivanov | 26e1a84 | 2017-02-03 14:11:27 -0800 | [diff] [blame] | 328 | initialized_ = android_init_anonymous_namespace(public_libraries_.c_str(), |
| 329 | is_native_bridge ? nullptr : library_path); |
Dimitry Ivanov | d836ab0 | 2016-11-02 18:03:10 -0700 | [diff] [blame] | 330 | if (!initialized_) { |
| 331 | *error_msg = dlerror(); |
| 332 | return false; |
| 333 | } |
| 334 | |
| 335 | // and now initialize native bridge namespaces if necessary. |
| 336 | if (NativeBridgeInitialized()) { |
Zhenhua WANG | e8fb11d | 2017-02-27 10:14:45 +0800 | [diff] [blame] | 337 | initialized_ = NativeBridgeInitAnonymousNamespace(public_libraries_.c_str(), |
| 338 | is_native_bridge ? library_path : nullptr); |
Zhenhua WANG | f2804e5 | 2016-05-30 11:16:08 +0800 | [diff] [blame] | 339 | if (!initialized_) { |
| 340 | *error_msg = NativeBridgeGetError(); |
| 341 | } |
| 342 | } |
Dimitry Ivanov | ac1b191 | 2015-12-01 13:56:44 -0800 | [diff] [blame] | 343 | |
| 344 | return initialized_; |
| 345 | } |
| 346 | |
Dimitry Ivanov | 24db75c | 2016-05-12 15:34:41 -0700 | [diff] [blame] | 347 | jobject GetParentClassLoader(JNIEnv* env, jobject class_loader) { |
| 348 | jclass class_loader_class = env->FindClass("java/lang/ClassLoader"); |
| 349 | jmethodID get_parent = env->GetMethodID(class_loader_class, |
| 350 | "getParent", |
| 351 | "()Ljava/lang/ClassLoader;"); |
| 352 | |
| 353 | return env->CallObjectMethod(class_loader, get_parent); |
| 354 | } |
| 355 | |
Zhenhua WANG | f2804e5 | 2016-05-30 11:16:08 +0800 | [diff] [blame] | 356 | bool FindParentNamespaceByClassLoader(JNIEnv* env, |
| 357 | jobject class_loader, |
| 358 | NativeLoaderNamespace* ns) { |
Dimitry Ivanov | 24db75c | 2016-05-12 15:34:41 -0700 | [diff] [blame] | 359 | jobject parent_class_loader = GetParentClassLoader(env, class_loader); |
| 360 | |
| 361 | while (parent_class_loader != nullptr) { |
Zhenhua WANG | f2804e5 | 2016-05-30 11:16:08 +0800 | [diff] [blame] | 362 | if (FindNamespaceByClassLoader(env, parent_class_loader, ns)) { |
| 363 | return true; |
Dimitry Ivanov | 24db75c | 2016-05-12 15:34:41 -0700 | [diff] [blame] | 364 | } |
| 365 | |
| 366 | parent_class_loader = GetParentClassLoader(env, parent_class_loader); |
| 367 | } |
Zhenhua WANG | f2804e5 | 2016-05-30 11:16:08 +0800 | [diff] [blame] | 368 | |
| 369 | return false; |
Dimitry Ivanov | 24db75c | 2016-05-12 15:34:41 -0700 | [diff] [blame] | 370 | } |
| 371 | |
Dimitry Ivanov | ac1b191 | 2015-12-01 13:56:44 -0800 | [diff] [blame] | 372 | bool initialized_; |
Zhenhua WANG | f2804e5 | 2016-05-30 11:16:08 +0800 | [diff] [blame] | 373 | std::vector<std::pair<jweak, NativeLoaderNamespace>> namespaces_; |
Dimitry Ivanov | 4b0e963 | 2016-03-15 13:51:26 -0700 | [diff] [blame] | 374 | std::string public_libraries_; |
| 375 | |
Dimitry Ivanov | ac1b191 | 2015-12-01 13:56:44 -0800 | [diff] [blame] | 376 | |
| 377 | DISALLOW_COPY_AND_ASSIGN(LibraryNamespaces); |
| 378 | }; |
| 379 | |
Dimitry Ivanov | 34d5a20 | 2016-02-29 13:21:43 -0800 | [diff] [blame] | 380 | static std::mutex g_namespaces_mutex; |
Dimitry Ivanov | ac1b191 | 2015-12-01 13:56:44 -0800 | [diff] [blame] | 381 | static LibraryNamespaces* g_namespaces = new LibraryNamespaces; |
| 382 | #endif |
| 383 | |
Dimitry Ivanov | 4b0e963 | 2016-03-15 13:51:26 -0700 | [diff] [blame] | 384 | void InitializeNativeLoader() { |
Dimitry Ivanov | 426799d | 2016-02-22 11:27:48 -0800 | [diff] [blame] | 385 | #if defined(__ANDROID__) |
Dimitry Ivanov | 34d5a20 | 2016-02-29 13:21:43 -0800 | [diff] [blame] | 386 | std::lock_guard<std::mutex> guard(g_namespaces_mutex); |
Dimitry Ivanov | 4b0e963 | 2016-03-15 13:51:26 -0700 | [diff] [blame] | 387 | g_namespaces->Initialize(); |
Dimitry Ivanov | 426799d | 2016-02-22 11:27:48 -0800 | [diff] [blame] | 388 | #endif |
| 389 | } |
| 390 | |
Dimitry Ivanov | be4ca3a | 2016-05-02 10:43:16 -0700 | [diff] [blame] | 391 | void ResetNativeLoader() { |
| 392 | #if defined(__ANDROID__) |
| 393 | std::lock_guard<std::mutex> guard(g_namespaces_mutex); |
| 394 | g_namespaces->Reset(); |
| 395 | #endif |
| 396 | } |
Dimitry Ivanov | ac1b191 | 2015-12-01 13:56:44 -0800 | [diff] [blame] | 397 | |
Dimitry Ivanov | d047c92 | 2016-02-23 14:23:51 -0800 | [diff] [blame] | 398 | jstring CreateClassLoaderNamespace(JNIEnv* env, |
| 399 | int32_t target_sdk_version, |
| 400 | jobject class_loader, |
| 401 | bool is_shared, |
| 402 | jstring library_path, |
| 403 | jstring permitted_path) { |
Dimitry Ivanov | ac1b191 | 2015-12-01 13:56:44 -0800 | [diff] [blame] | 404 | #if defined(__ANDROID__) |
Dimitry Ivanov | 34d5a20 | 2016-02-29 13:21:43 -0800 | [diff] [blame] | 405 | std::lock_guard<std::mutex> guard(g_namespaces_mutex); |
Zhenhua WANG | f2804e5 | 2016-05-30 11:16:08 +0800 | [diff] [blame] | 406 | |
| 407 | std::string error_msg; |
| 408 | NativeLoaderNamespace ns; |
| 409 | bool success = g_namespaces->Create(env, |
Dimitry Ivanov | 9e253ce | 2017-05-08 22:24:24 -0700 | [diff] [blame] | 410 | target_sdk_version, |
Zhenhua WANG | f2804e5 | 2016-05-30 11:16:08 +0800 | [diff] [blame] | 411 | class_loader, |
| 412 | is_shared, |
| 413 | library_path, |
| 414 | permitted_path, |
| 415 | &ns, |
| 416 | &error_msg); |
| 417 | if (!success) { |
| 418 | return env->NewStringUTF(error_msg.c_str()); |
Dimitry Ivanov | d047c92 | 2016-02-23 14:23:51 -0800 | [diff] [blame] | 419 | } |
| 420 | #else |
| 421 | UNUSED(env, target_sdk_version, class_loader, is_shared, |
| 422 | library_path, permitted_path); |
| 423 | #endif |
| 424 | return nullptr; |
| 425 | } |
| 426 | |
| 427 | void* OpenNativeLibrary(JNIEnv* env, |
| 428 | int32_t target_sdk_version, |
| 429 | const char* path, |
| 430 | jobject class_loader, |
Zhenhua WANG | f2804e5 | 2016-05-30 11:16:08 +0800 | [diff] [blame] | 431 | jstring library_path, |
| 432 | bool* needs_native_bridge, |
| 433 | std::string* error_msg) { |
Dimitry Ivanov | d047c92 | 2016-02-23 14:23:51 -0800 | [diff] [blame] | 434 | #if defined(__ANDROID__) |
Dimitry Ivanov | 5539db0 | 2016-04-20 16:07:30 -0700 | [diff] [blame] | 435 | UNUSED(target_sdk_version); |
| 436 | if (class_loader == nullptr) { |
Zhenhua WANG | f2804e5 | 2016-05-30 11:16:08 +0800 | [diff] [blame] | 437 | *needs_native_bridge = false; |
Dimitry Ivanov | ac1b191 | 2015-12-01 13:56:44 -0800 | [diff] [blame] | 438 | return dlopen(path, RTLD_NOW); |
| 439 | } |
| 440 | |
Dimitry Ivanov | 34d5a20 | 2016-02-29 13:21:43 -0800 | [diff] [blame] | 441 | std::lock_guard<std::mutex> guard(g_namespaces_mutex); |
Zhenhua WANG | f2804e5 | 2016-05-30 11:16:08 +0800 | [diff] [blame] | 442 | NativeLoaderNamespace ns; |
Dimitry Ivanov | ac1b191 | 2015-12-01 13:56:44 -0800 | [diff] [blame] | 443 | |
Zhenhua WANG | f2804e5 | 2016-05-30 11:16:08 +0800 | [diff] [blame] | 444 | if (!g_namespaces->FindNamespaceByClassLoader(env, class_loader, &ns)) { |
Dimitry Ivanov | d047c92 | 2016-02-23 14:23:51 -0800 | [diff] [blame] | 445 | // This is the case where the classloader was not created by ApplicationLoaders |
| 446 | // In this case we create an isolated not-shared namespace for it. |
Dimitry Ivanov | 9e253ce | 2017-05-08 22:24:24 -0700 | [diff] [blame] | 447 | if (!g_namespaces->Create(env, |
| 448 | target_sdk_version, |
| 449 | class_loader, |
| 450 | false, |
| 451 | library_path, |
| 452 | nullptr, |
| 453 | &ns, |
| 454 | error_msg)) { |
Dimitry Ivanov | d047c92 | 2016-02-23 14:23:51 -0800 | [diff] [blame] | 455 | return nullptr; |
| 456 | } |
Dimitry Ivanov | ac1b191 | 2015-12-01 13:56:44 -0800 | [diff] [blame] | 457 | } |
| 458 | |
Zhenhua WANG | f2804e5 | 2016-05-30 11:16:08 +0800 | [diff] [blame] | 459 | if (ns.is_android_namespace()) { |
| 460 | android_dlextinfo extinfo; |
| 461 | extinfo.flags = ANDROID_DLEXT_USE_NAMESPACE; |
| 462 | extinfo.library_namespace = ns.get_android_ns(); |
Dimitry Ivanov | ac1b191 | 2015-12-01 13:56:44 -0800 | [diff] [blame] | 463 | |
Zhenhua WANG | f2804e5 | 2016-05-30 11:16:08 +0800 | [diff] [blame] | 464 | void* handle = android_dlopen_ext(path, RTLD_NOW, &extinfo); |
| 465 | if (handle == nullptr) { |
| 466 | *error_msg = dlerror(); |
| 467 | } |
| 468 | *needs_native_bridge = false; |
| 469 | return handle; |
| 470 | } else { |
| 471 | void* handle = NativeBridgeLoadLibraryExt(path, RTLD_NOW, ns.get_native_bridge_ns()); |
| 472 | if (handle == nullptr) { |
| 473 | *error_msg = NativeBridgeGetError(); |
| 474 | } |
| 475 | *needs_native_bridge = true; |
| 476 | return handle; |
| 477 | } |
Dimitry Ivanov | ac1b191 | 2015-12-01 13:56:44 -0800 | [diff] [blame] | 478 | #else |
Dimitry Ivanov | d047c92 | 2016-02-23 14:23:51 -0800 | [diff] [blame] | 479 | UNUSED(env, target_sdk_version, class_loader, library_path); |
Zhenhua WANG | f2804e5 | 2016-05-30 11:16:08 +0800 | [diff] [blame] | 480 | *needs_native_bridge = false; |
| 481 | void* handle = dlopen(path, RTLD_NOW); |
| 482 | if (handle == nullptr) { |
| 483 | if (NativeBridgeIsSupported(path)) { |
| 484 | *needs_native_bridge = true; |
| 485 | handle = NativeBridgeLoadLibrary(path, RTLD_NOW); |
| 486 | if (handle == nullptr) { |
| 487 | *error_msg = NativeBridgeGetError(); |
| 488 | } |
| 489 | } else { |
| 490 | *needs_native_bridge = false; |
| 491 | *error_msg = dlerror(); |
| 492 | } |
| 493 | } |
| 494 | return handle; |
Dimitry Ivanov | ac1b191 | 2015-12-01 13:56:44 -0800 | [diff] [blame] | 495 | #endif |
| 496 | } |
| 497 | |
Zhenhua WANG | f2804e5 | 2016-05-30 11:16:08 +0800 | [diff] [blame] | 498 | bool CloseNativeLibrary(void* handle, const bool needs_native_bridge) { |
| 499 | return needs_native_bridge ? NativeBridgeUnloadLibrary(handle) : |
| 500 | dlclose(handle); |
Dimitry Ivanov | 09a516b | 2016-05-03 14:55:25 -0700 | [diff] [blame] | 501 | } |
| 502 | |
Dimitry Ivanov | f44ecde | 2016-02-22 13:48:22 -0800 | [diff] [blame] | 503 | #if defined(__ANDROID__) |
Dimitry Ivanov | 800083d | 2016-11-01 14:17:00 -0700 | [diff] [blame] | 504 | // native_bridge_namespaces are not supported for callers of this function. |
| 505 | // This function will return nullptr in the case when application is running |
| 506 | // on native bridge. |
Dimitry Ivanov | f44ecde | 2016-02-22 13:48:22 -0800 | [diff] [blame] | 507 | android_namespace_t* FindNamespaceByClassLoader(JNIEnv* env, jobject class_loader) { |
Dimitry Ivanov | 34d5a20 | 2016-02-29 13:21:43 -0800 | [diff] [blame] | 508 | std::lock_guard<std::mutex> guard(g_namespaces_mutex); |
Zhenhua WANG | f2804e5 | 2016-05-30 11:16:08 +0800 | [diff] [blame] | 509 | NativeLoaderNamespace ns; |
| 510 | if (g_namespaces->FindNamespaceByClassLoader(env, class_loader, &ns)) { |
Dimitry Ivanov | 800083d | 2016-11-01 14:17:00 -0700 | [diff] [blame] | 511 | return ns.is_android_namespace() ? ns.get_android_ns() : nullptr; |
Zhenhua WANG | f2804e5 | 2016-05-30 11:16:08 +0800 | [diff] [blame] | 512 | } |
| 513 | |
| 514 | return nullptr; |
Dimitry Ivanov | f44ecde | 2016-02-22 13:48:22 -0800 | [diff] [blame] | 515 | } |
| 516 | #endif |
| 517 | |
Dimitry Ivanov | ac1b191 | 2015-12-01 13:56:44 -0800 | [diff] [blame] | 518 | }; // android namespace |