| 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 | 6796522 | 2016-05-09 17:51:51 -0700 | [diff] [blame] | 22 | #include "dlext_namespaces.h" | 
| Dimitry Ivanov | 7f9a1aa | 2016-03-22 13:59:59 -0700 | [diff] [blame] | 23 | #define LOG_TAG "libnativeloader" | 
| Mark Salyzyn | ff2dcd9 | 2016-09-28 15:54:45 -0700 | [diff] [blame] | 24 | #include "android/log.h" | 
|  | 25 | #include "cutils/properties.h" | 
| Dimitry Ivanov | ac1b191 | 2015-12-01 13:56:44 -0800 | [diff] [blame] | 26 | #endif | 
|  | 27 |  | 
|  | 28 | #include <algorithm> | 
|  | 29 | #include <vector> | 
|  | 30 | #include <string> | 
|  | 31 | #include <mutex> | 
|  | 32 |  | 
| Mark Salyzyn | ff2dcd9 | 2016-09-28 15:54:45 -0700 | [diff] [blame] | 33 | #include <android-base/file.h> | 
|  | 34 | #include <android-base/macros.h> | 
|  | 35 | #include <android-base/strings.h> | 
| Dimitry Ivanov | ac1b191 | 2015-12-01 13:56:44 -0800 | [diff] [blame] | 36 |  | 
|  | 37 | namespace android { | 
|  | 38 |  | 
| Dimitry Ivanov | 4b0e963 | 2016-03-15 13:51:26 -0700 | [diff] [blame] | 39 | #if defined(__ANDROID__) | 
| Dimitry Ivanov | 0b5651e | 2016-04-21 16:42:48 -0700 | [diff] [blame] | 40 | static constexpr const char* kPublicNativeLibrariesSystemConfigPathFromRoot = "/etc/public.libraries.txt"; | 
| Dimitry Ivanov | b614045 | 2016-04-06 18:24:08 -0700 | [diff] [blame] | 41 | static constexpr const char* kPublicNativeLibrariesVendorConfig = "/vendor/etc/public.libraries.txt"; | 
| Dimitry Ivanov | ac1b191 | 2015-12-01 13:56:44 -0800 | [diff] [blame] | 42 |  | 
| Dimitry Ivanov | f334cbf | 2016-05-10 10:39:48 -0700 | [diff] [blame] | 43 | // (http://b/27588281) This is a workaround for apps using custom classloaders and calling | 
|  | 44 | // System.load() with an absolute path which is outside of the classloader library search path. | 
|  | 45 | // This list includes all directories app is allowed to access this way. | 
|  | 46 | static constexpr const char* kWhitelistedDirectories = "/data:/mnt/expand"; | 
|  | 47 |  | 
| Dimitry Ivanov | 7d02829 | 2016-05-05 17:30:24 -0700 | [diff] [blame] | 48 | static bool is_debuggable() { | 
|  | 49 | char debuggable[PROP_VALUE_MAX]; | 
|  | 50 | property_get("ro.debuggable", debuggable, "0"); | 
|  | 51 | return std::string(debuggable) == "1"; | 
|  | 52 | } | 
|  | 53 |  | 
| Dimitry Ivanov | ac1b191 | 2015-12-01 13:56:44 -0800 | [diff] [blame] | 54 | class LibraryNamespaces { | 
|  | 55 | public: | 
| Dimitry Ivanov | 426799d | 2016-02-22 11:27:48 -0800 | [diff] [blame] | 56 | LibraryNamespaces() : initialized_(false) { } | 
| Dimitry Ivanov | ac1b191 | 2015-12-01 13:56:44 -0800 | [diff] [blame] | 57 |  | 
| Dimitry Ivanov | d047c92 | 2016-02-23 14:23:51 -0800 | [diff] [blame] | 58 | android_namespace_t* Create(JNIEnv* env, | 
|  | 59 | jobject class_loader, | 
|  | 60 | bool is_shared, | 
|  | 61 | jstring java_library_path, | 
| Dimitry Ivanov | 94ee4e6 | 2016-05-24 08:37:05 -0700 | [diff] [blame] | 62 | jstring java_permitted_path) { | 
| Dimitry Ivanov | cf9892b | 2016-05-09 10:55:50 -0700 | [diff] [blame] | 63 | std::string library_path; // empty string by default. | 
|  | 64 |  | 
|  | 65 | if (java_library_path != nullptr) { | 
|  | 66 | ScopedUtfChars library_path_utf_chars(env, java_library_path); | 
|  | 67 | library_path = library_path_utf_chars.c_str(); | 
|  | 68 | } | 
| Dimitry Ivanov | ac1b191 | 2015-12-01 13:56:44 -0800 | [diff] [blame] | 69 |  | 
| Dimitry Ivanov | f334cbf | 2016-05-10 10:39:48 -0700 | [diff] [blame] | 70 | // (http://b/27588281) This is a workaround for apps using custom | 
|  | 71 | // classloaders and calling System.load() with an absolute path which | 
|  | 72 | // is outside of the classloader library search path. | 
|  | 73 | // | 
|  | 74 | // This part effectively allows such a classloader to access anything | 
|  | 75 | // under /data and /mnt/expand | 
|  | 76 | std::string permitted_path = kWhitelistedDirectories; | 
|  | 77 |  | 
| Dimitry Ivanov | 0d6e594 | 2015-12-08 11:16:56 -0800 | [diff] [blame] | 78 | if (java_permitted_path != nullptr) { | 
|  | 79 | ScopedUtfChars path(env, java_permitted_path); | 
| Dimitry Ivanov | d0b1531 | 2016-05-10 16:21:25 -0700 | [diff] [blame] | 80 | if (path.c_str() != nullptr && path.size() > 0) { | 
|  | 81 | permitted_path = permitted_path + ":" + path.c_str(); | 
|  | 82 | } | 
| Dimitry Ivanov | 0d6e594 | 2015-12-08 11:16:56 -0800 | [diff] [blame] | 83 | } | 
|  | 84 |  | 
| Dimitry Ivanov | 94ee4e6 | 2016-05-24 08:37:05 -0700 | [diff] [blame] | 85 | if (!initialized_ && !InitPublicNamespace(library_path.c_str())) { | 
| Dimitry Ivanov | ac1b191 | 2015-12-01 13:56:44 -0800 | [diff] [blame] | 86 | return nullptr; | 
|  | 87 | } | 
|  | 88 |  | 
| Dimitry Ivanov | c914ebd | 2016-02-22 13:02:35 -0800 | [diff] [blame] | 89 | android_namespace_t* ns = FindNamespaceByClassLoader(env, class_loader); | 
| Dimitry Ivanov | ac1b191 | 2015-12-01 13:56:44 -0800 | [diff] [blame] | 90 |  | 
| Dimitry Ivanov | 4b0e963 | 2016-03-15 13:51:26 -0700 | [diff] [blame] | 91 | LOG_ALWAYS_FATAL_IF(ns != nullptr, | 
|  | 92 | "There is already a namespace associated with this classloader"); | 
| Dimitry Ivanov | ac1b191 | 2015-12-01 13:56:44 -0800 | [diff] [blame] | 93 |  | 
| Dimitry Ivanov | d2a6220 | 2015-12-15 11:06:57 -0800 | [diff] [blame] | 94 | uint64_t namespace_type = ANDROID_NAMESPACE_TYPE_ISOLATED; | 
|  | 95 | if (is_shared) { | 
|  | 96 | namespace_type |= ANDROID_NAMESPACE_TYPE_SHARED; | 
|  | 97 | } | 
|  | 98 |  | 
| Dimitry Ivanov | 24db75c | 2016-05-12 15:34:41 -0700 | [diff] [blame] | 99 | android_namespace_t* parent_ns = FindParentNamespaceByClassLoader(env, class_loader); | 
|  | 100 |  | 
| Dimitry Ivanov | c914ebd | 2016-02-22 13:02:35 -0800 | [diff] [blame] | 101 | ns = android_create_namespace("classloader-namespace", | 
|  | 102 | nullptr, | 
|  | 103 | library_path.c_str(), | 
|  | 104 | namespace_type, | 
| Dimitry Ivanov | 24db75c | 2016-05-12 15:34:41 -0700 | [diff] [blame] | 105 | permitted_path.c_str(), | 
|  | 106 | parent_ns); | 
| Dimitry Ivanov | ac1b191 | 2015-12-01 13:56:44 -0800 | [diff] [blame] | 107 |  | 
| Dimitry Ivanov | d047c92 | 2016-02-23 14:23:51 -0800 | [diff] [blame] | 108 | if (ns != nullptr) { | 
|  | 109 | namespaces_.push_back(std::make_pair(env->NewWeakGlobalRef(class_loader), ns)); | 
|  | 110 | } | 
| Dimitry Ivanov | ac1b191 | 2015-12-01 13:56:44 -0800 | [diff] [blame] | 111 |  | 
|  | 112 | return ns; | 
|  | 113 | } | 
|  | 114 |  | 
| Dimitry Ivanov | f44ecde | 2016-02-22 13:48:22 -0800 | [diff] [blame] | 115 | android_namespace_t* FindNamespaceByClassLoader(JNIEnv* env, jobject class_loader) { | 
|  | 116 | auto it = std::find_if(namespaces_.begin(), namespaces_.end(), | 
|  | 117 | [&](const std::pair<jweak, android_namespace_t*>& value) { | 
|  | 118 | return env->IsSameObject(value.first, class_loader); | 
|  | 119 | }); | 
|  | 120 | return it != namespaces_.end() ? it->second : nullptr; | 
|  | 121 | } | 
|  | 122 |  | 
| Dimitry Ivanov | 4b0e963 | 2016-03-15 13:51:26 -0700 | [diff] [blame] | 123 | void Initialize() { | 
| Dimitry Ivanov | 80ddb8f | 2016-05-09 18:12:00 -0700 | [diff] [blame] | 124 | // Once public namespace is initialized there is no | 
|  | 125 | // point in running this code - it will have no effect | 
|  | 126 | // on the current list of public libraries. | 
|  | 127 | if (initialized_) { | 
|  | 128 | return; | 
|  | 129 | } | 
|  | 130 |  | 
| Dimitry Ivanov | b614045 | 2016-04-06 18:24:08 -0700 | [diff] [blame] | 131 | std::vector<std::string> sonames; | 
| Dimitry Ivanov | 0b5651e | 2016-04-21 16:42:48 -0700 | [diff] [blame] | 132 | const char* android_root_env = getenv("ANDROID_ROOT"); | 
|  | 133 | std::string root_dir = android_root_env != nullptr ? android_root_env : "/system"; | 
|  | 134 | std::string public_native_libraries_system_config = | 
|  | 135 | root_dir + kPublicNativeLibrariesSystemConfigPathFromRoot; | 
| Dimitry Ivanov | b614045 | 2016-04-06 18:24:08 -0700 | [diff] [blame] | 136 |  | 
| Christopher Ferris | 39da84b | 2016-06-21 16:11:23 -0700 | [diff] [blame] | 137 | std::string error_msg; | 
|  | 138 | 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] | 139 | "Error reading public native library list from \"%s\": %s", | 
| Christopher Ferris | 39da84b | 2016-06-21 16:11:23 -0700 | [diff] [blame] | 140 | public_native_libraries_system_config.c_str(), error_msg.c_str()); | 
| Dimitry Ivanov | 7d02829 | 2016-05-05 17:30:24 -0700 | [diff] [blame] | 141 |  | 
|  | 142 | // For debuggable platform builds use ANDROID_ADDITIONAL_PUBLIC_LIBRARIES environment | 
|  | 143 | // variable to add libraries to the list. This is intended for platform tests only. | 
|  | 144 | if (is_debuggable()) { | 
|  | 145 | const char* additional_libs = getenv("ANDROID_ADDITIONAL_PUBLIC_LIBRARIES"); | 
|  | 146 | if (additional_libs != nullptr && additional_libs[0] != '\0') { | 
|  | 147 | std::vector<std::string> additional_libs_vector = base::Split(additional_libs, ":"); | 
|  | 148 | std::copy(additional_libs_vector.begin(), | 
|  | 149 | additional_libs_vector.end(), | 
|  | 150 | std::back_inserter(sonames)); | 
|  | 151 | } | 
|  | 152 | } | 
|  | 153 |  | 
| Dimitry Ivanov | b614045 | 2016-04-06 18:24:08 -0700 | [diff] [blame] | 154 | // This file is optional, quietly ignore if the file does not exist. | 
|  | 155 | ReadConfig(kPublicNativeLibrariesVendorConfig, &sonames); | 
|  | 156 |  | 
|  | 157 | // android_init_namespaces() expects all the public libraries | 
|  | 158 | // to be loaded so that they can be found by soname alone. | 
|  | 159 | // | 
|  | 160 | // TODO(dimitry): this is a bit misleading since we do not know | 
|  | 161 | // if the vendor public library is going to be opened from /vendor/lib | 
|  | 162 | // we might as well end up loading them from /system/lib | 
|  | 163 | // For now we rely on CTS test to catch things like this but | 
|  | 164 | // it should probably be addressed in the future. | 
|  | 165 | for (const auto& soname : sonames) { | 
|  | 166 | dlopen(soname.c_str(), RTLD_NOW | RTLD_NODELETE); | 
|  | 167 | } | 
|  | 168 |  | 
|  | 169 | public_libraries_ = base::Join(sonames, ':'); | 
|  | 170 | } | 
|  | 171 |  | 
| Dimitry Ivanov | be4ca3a | 2016-05-02 10:43:16 -0700 | [diff] [blame] | 172 | void Reset() { | 
|  | 173 | namespaces_.clear(); | 
|  | 174 | } | 
|  | 175 |  | 
| Dimitry Ivanov | b614045 | 2016-04-06 18:24:08 -0700 | [diff] [blame] | 176 | private: | 
| Christopher Ferris | 39da84b | 2016-06-21 16:11:23 -0700 | [diff] [blame] | 177 | bool ReadConfig(const std::string& configFile, std::vector<std::string>* sonames, | 
|  | 178 | std::string* error_msg = nullptr) { | 
| Dimitry Ivanov | 4b0e963 | 2016-03-15 13:51:26 -0700 | [diff] [blame] | 179 | // Read list of public native libraries from the config file. | 
|  | 180 | std::string file_content; | 
| Dimitry Ivanov | b614045 | 2016-04-06 18:24:08 -0700 | [diff] [blame] | 181 | if(!base::ReadFileToString(configFile, &file_content)) { | 
| Christopher Ferris | 39da84b | 2016-06-21 16:11:23 -0700 | [diff] [blame] | 182 | if (error_msg) *error_msg = strerror(errno); | 
| Dimitry Ivanov | b614045 | 2016-04-06 18:24:08 -0700 | [diff] [blame] | 183 | return false; | 
|  | 184 | } | 
| Dimitry Ivanov | 4b0e963 | 2016-03-15 13:51:26 -0700 | [diff] [blame] | 185 |  | 
|  | 186 | std::vector<std::string> lines = base::Split(file_content, "\n"); | 
|  | 187 |  | 
| Christopher Ferris | 39da84b | 2016-06-21 16:11:23 -0700 | [diff] [blame] | 188 | for (auto& line : lines) { | 
| Dimitry Ivanov | 4b0e963 | 2016-03-15 13:51:26 -0700 | [diff] [blame] | 189 | auto trimmed_line = base::Trim(line); | 
|  | 190 | if (trimmed_line[0] == '#' || trimmed_line.empty()) { | 
|  | 191 | continue; | 
|  | 192 | } | 
| Christopher Ferris | 39da84b | 2016-06-21 16:11:23 -0700 | [diff] [blame] | 193 | size_t space_pos = trimmed_line.rfind(' '); | 
|  | 194 | if (space_pos != std::string::npos) { | 
|  | 195 | std::string type = trimmed_line.substr(space_pos + 1); | 
|  | 196 | if (type != "32" && type != "64") { | 
|  | 197 | if (error_msg) *error_msg = "Malformed line: " + line; | 
|  | 198 | return false; | 
|  | 199 | } | 
|  | 200 | #if defined(__LP64__) | 
|  | 201 | // Skip 32 bit public library. | 
|  | 202 | if (type == "32") { | 
|  | 203 | continue; | 
|  | 204 | } | 
|  | 205 | #else | 
|  | 206 | // Skip 64 bit public library. | 
|  | 207 | if (type == "64") { | 
|  | 208 | continue; | 
|  | 209 | } | 
|  | 210 | #endif | 
|  | 211 | trimmed_line.resize(space_pos); | 
|  | 212 | } | 
| Dimitry Ivanov | 4b0e963 | 2016-03-15 13:51:26 -0700 | [diff] [blame] | 213 |  | 
| Dimitry Ivanov | b614045 | 2016-04-06 18:24:08 -0700 | [diff] [blame] | 214 | sonames->push_back(trimmed_line); | 
| Dimitry Ivanov | 4b0e963 | 2016-03-15 13:51:26 -0700 | [diff] [blame] | 215 | } | 
|  | 216 |  | 
| Dimitry Ivanov | b614045 | 2016-04-06 18:24:08 -0700 | [diff] [blame] | 217 | return true; | 
| Dimitry Ivanov | d68c8e9 | 2016-02-10 14:09:22 -0800 | [diff] [blame] | 218 | } | 
| Dimitry Ivanov | ac1b191 | 2015-12-01 13:56:44 -0800 | [diff] [blame] | 219 |  | 
| Dimitry Ivanov | 94ee4e6 | 2016-05-24 08:37:05 -0700 | [diff] [blame] | 220 | bool InitPublicNamespace(const char* library_path) { | 
| Dimitry Ivanov | 4b0e963 | 2016-03-15 13:51:26 -0700 | [diff] [blame] | 221 | // (http://b/25844435) - Some apps call dlopen from generated code (mono jited | 
|  | 222 | // code is one example) unknown to linker in which  case linker uses anonymous | 
|  | 223 | // namespace. The second argument specifies the search path for the anonymous | 
|  | 224 | // namespace which is the library_path of the classloader. | 
| Dimitry Ivanov | 94ee4e6 | 2016-05-24 08:37:05 -0700 | [diff] [blame] | 225 | initialized_ = android_init_namespaces(public_libraries_.c_str(), library_path); | 
| Dimitry Ivanov | ac1b191 | 2015-12-01 13:56:44 -0800 | [diff] [blame] | 226 |  | 
|  | 227 | return initialized_; | 
|  | 228 | } | 
|  | 229 |  | 
| Dimitry Ivanov | 24db75c | 2016-05-12 15:34:41 -0700 | [diff] [blame] | 230 | jobject GetParentClassLoader(JNIEnv* env, jobject class_loader) { | 
|  | 231 | jclass class_loader_class = env->FindClass("java/lang/ClassLoader"); | 
|  | 232 | jmethodID get_parent = env->GetMethodID(class_loader_class, | 
|  | 233 | "getParent", | 
|  | 234 | "()Ljava/lang/ClassLoader;"); | 
|  | 235 |  | 
|  | 236 | return env->CallObjectMethod(class_loader, get_parent); | 
|  | 237 | } | 
|  | 238 |  | 
|  | 239 | android_namespace_t* FindParentNamespaceByClassLoader(JNIEnv* env, jobject class_loader) { | 
|  | 240 | jobject parent_class_loader = GetParentClassLoader(env, class_loader); | 
|  | 241 |  | 
|  | 242 | while (parent_class_loader != nullptr) { | 
|  | 243 | android_namespace_t* ns = FindNamespaceByClassLoader(env, parent_class_loader); | 
|  | 244 | if (ns != nullptr) { | 
|  | 245 | return ns; | 
|  | 246 | } | 
|  | 247 |  | 
|  | 248 | parent_class_loader = GetParentClassLoader(env, parent_class_loader); | 
|  | 249 | } | 
|  | 250 | return nullptr; | 
|  | 251 | } | 
|  | 252 |  | 
| Dimitry Ivanov | ac1b191 | 2015-12-01 13:56:44 -0800 | [diff] [blame] | 253 | bool initialized_; | 
| Dimitry Ivanov | ac1b191 | 2015-12-01 13:56:44 -0800 | [diff] [blame] | 254 | std::vector<std::pair<jweak, android_namespace_t*>> namespaces_; | 
| Dimitry Ivanov | 4b0e963 | 2016-03-15 13:51:26 -0700 | [diff] [blame] | 255 | std::string public_libraries_; | 
|  | 256 |  | 
| Dimitry Ivanov | ac1b191 | 2015-12-01 13:56:44 -0800 | [diff] [blame] | 257 |  | 
|  | 258 | DISALLOW_COPY_AND_ASSIGN(LibraryNamespaces); | 
|  | 259 | }; | 
|  | 260 |  | 
| Dimitry Ivanov | 34d5a20 | 2016-02-29 13:21:43 -0800 | [diff] [blame] | 261 | static std::mutex g_namespaces_mutex; | 
| Dimitry Ivanov | ac1b191 | 2015-12-01 13:56:44 -0800 | [diff] [blame] | 262 | static LibraryNamespaces* g_namespaces = new LibraryNamespaces; | 
|  | 263 | #endif | 
|  | 264 |  | 
| Dimitry Ivanov | 4b0e963 | 2016-03-15 13:51:26 -0700 | [diff] [blame] | 265 | void InitializeNativeLoader() { | 
| Dimitry Ivanov | 426799d | 2016-02-22 11:27:48 -0800 | [diff] [blame] | 266 | #if defined(__ANDROID__) | 
| Dimitry Ivanov | 34d5a20 | 2016-02-29 13:21:43 -0800 | [diff] [blame] | 267 | std::lock_guard<std::mutex> guard(g_namespaces_mutex); | 
| Dimitry Ivanov | 4b0e963 | 2016-03-15 13:51:26 -0700 | [diff] [blame] | 268 | g_namespaces->Initialize(); | 
| Dimitry Ivanov | 426799d | 2016-02-22 11:27:48 -0800 | [diff] [blame] | 269 | #endif | 
|  | 270 | } | 
|  | 271 |  | 
| Dimitry Ivanov | be4ca3a | 2016-05-02 10:43:16 -0700 | [diff] [blame] | 272 | void ResetNativeLoader() { | 
|  | 273 | #if defined(__ANDROID__) | 
|  | 274 | std::lock_guard<std::mutex> guard(g_namespaces_mutex); | 
|  | 275 | g_namespaces->Reset(); | 
|  | 276 | #endif | 
|  | 277 | } | 
| Dimitry Ivanov | ac1b191 | 2015-12-01 13:56:44 -0800 | [diff] [blame] | 278 |  | 
| Dimitry Ivanov | d047c92 | 2016-02-23 14:23:51 -0800 | [diff] [blame] | 279 | jstring CreateClassLoaderNamespace(JNIEnv* env, | 
|  | 280 | int32_t target_sdk_version, | 
|  | 281 | jobject class_loader, | 
|  | 282 | bool is_shared, | 
|  | 283 | jstring library_path, | 
|  | 284 | jstring permitted_path) { | 
| Dimitry Ivanov | ac1b191 | 2015-12-01 13:56:44 -0800 | [diff] [blame] | 285 | #if defined(__ANDROID__) | 
| Dimitry Ivanov | 5539db0 | 2016-04-20 16:07:30 -0700 | [diff] [blame] | 286 | UNUSED(target_sdk_version); | 
| Dimitry Ivanov | 34d5a20 | 2016-02-29 13:21:43 -0800 | [diff] [blame] | 287 | std::lock_guard<std::mutex> guard(g_namespaces_mutex); | 
| Dimitry Ivanov | d047c92 | 2016-02-23 14:23:51 -0800 | [diff] [blame] | 288 | android_namespace_t* ns = g_namespaces->Create(env, | 
|  | 289 | class_loader, | 
|  | 290 | is_shared, | 
|  | 291 | library_path, | 
| Dimitry Ivanov | 94ee4e6 | 2016-05-24 08:37:05 -0700 | [diff] [blame] | 292 | permitted_path); | 
| Dimitry Ivanov | d047c92 | 2016-02-23 14:23:51 -0800 | [diff] [blame] | 293 | if (ns == nullptr) { | 
|  | 294 | return env->NewStringUTF(dlerror()); | 
|  | 295 | } | 
|  | 296 | #else | 
|  | 297 | UNUSED(env, target_sdk_version, class_loader, is_shared, | 
|  | 298 | library_path, permitted_path); | 
|  | 299 | #endif | 
|  | 300 | return nullptr; | 
|  | 301 | } | 
|  | 302 |  | 
|  | 303 | void* OpenNativeLibrary(JNIEnv* env, | 
|  | 304 | int32_t target_sdk_version, | 
|  | 305 | const char* path, | 
|  | 306 | jobject class_loader, | 
|  | 307 | jstring library_path) { | 
|  | 308 | #if defined(__ANDROID__) | 
| Dimitry Ivanov | 5539db0 | 2016-04-20 16:07:30 -0700 | [diff] [blame] | 309 | UNUSED(target_sdk_version); | 
|  | 310 | if (class_loader == nullptr) { | 
| Dimitry Ivanov | ac1b191 | 2015-12-01 13:56:44 -0800 | [diff] [blame] | 311 | return dlopen(path, RTLD_NOW); | 
|  | 312 | } | 
|  | 313 |  | 
| Dimitry Ivanov | 34d5a20 | 2016-02-29 13:21:43 -0800 | [diff] [blame] | 314 | std::lock_guard<std::mutex> guard(g_namespaces_mutex); | 
| Dimitry Ivanov | d047c92 | 2016-02-23 14:23:51 -0800 | [diff] [blame] | 315 | android_namespace_t* ns = g_namespaces->FindNamespaceByClassLoader(env, class_loader); | 
| Dimitry Ivanov | ac1b191 | 2015-12-01 13:56:44 -0800 | [diff] [blame] | 316 |  | 
|  | 317 | if (ns == nullptr) { | 
| Dimitry Ivanov | d047c92 | 2016-02-23 14:23:51 -0800 | [diff] [blame] | 318 | // This is the case where the classloader was not created by ApplicationLoaders | 
|  | 319 | // In this case we create an isolated not-shared namespace for it. | 
| Dimitry Ivanov | 94ee4e6 | 2016-05-24 08:37:05 -0700 | [diff] [blame] | 320 | ns = g_namespaces->Create(env, class_loader, false, library_path, nullptr); | 
| Dimitry Ivanov | d047c92 | 2016-02-23 14:23:51 -0800 | [diff] [blame] | 321 | if (ns == nullptr) { | 
|  | 322 | return nullptr; | 
|  | 323 | } | 
| Dimitry Ivanov | ac1b191 | 2015-12-01 13:56:44 -0800 | [diff] [blame] | 324 | } | 
|  | 325 |  | 
|  | 326 | android_dlextinfo extinfo; | 
|  | 327 | extinfo.flags = ANDROID_DLEXT_USE_NAMESPACE; | 
|  | 328 | extinfo.library_namespace = ns; | 
|  | 329 |  | 
|  | 330 | return android_dlopen_ext(path, RTLD_NOW, &extinfo); | 
|  | 331 | #else | 
| Dimitry Ivanov | d047c92 | 2016-02-23 14:23:51 -0800 | [diff] [blame] | 332 | UNUSED(env, target_sdk_version, class_loader, library_path); | 
| Dimitry Ivanov | ac1b191 | 2015-12-01 13:56:44 -0800 | [diff] [blame] | 333 | return dlopen(path, RTLD_NOW); | 
|  | 334 | #endif | 
|  | 335 | } | 
|  | 336 |  | 
| Dimitry Ivanov | 09a516b | 2016-05-03 14:55:25 -0700 | [diff] [blame] | 337 | bool CloseNativeLibrary(void* handle) { | 
|  | 338 | return dlclose(handle) == 0; | 
|  | 339 | } | 
|  | 340 |  | 
| Dimitry Ivanov | f44ecde | 2016-02-22 13:48:22 -0800 | [diff] [blame] | 341 | #if defined(__ANDROID__) | 
|  | 342 | android_namespace_t* FindNamespaceByClassLoader(JNIEnv* env, jobject class_loader) { | 
| Dimitry Ivanov | 34d5a20 | 2016-02-29 13:21:43 -0800 | [diff] [blame] | 343 | std::lock_guard<std::mutex> guard(g_namespaces_mutex); | 
| Dimitry Ivanov | f44ecde | 2016-02-22 13:48:22 -0800 | [diff] [blame] | 344 | return g_namespaces->FindNamespaceByClassLoader(env, class_loader); | 
|  | 345 | } | 
|  | 346 | #endif | 
|  | 347 |  | 
| Dimitry Ivanov | ac1b191 | 2015-12-01 13:56:44 -0800 | [diff] [blame] | 348 | }; //  android namespace |