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