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