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__ |
| 22 | #include <android/dlext.h> |
| 23 | #include "cutils/properties.h" |
Dimitry Ivanov | d047c92 | 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 | |
Elliott Hughes | 8b04714 | 2015-12-08 10:38:59 -0800 | [diff] [blame] | 32 | #include "android-base/macros.h" |
| 33 | #include "android-base/strings.h" |
Dimitry Ivanov | ac1b191 | 2015-12-01 13:56:44 -0800 | [diff] [blame] | 34 | |
| 35 | namespace android { |
| 36 | |
| 37 | #ifdef __ANDROID__ |
| 38 | // TODO(dimitry): move this to system properties. |
| 39 | static const char* kPublicNativeLibraries = "libandroid.so:" |
| 40 | "libc.so:" |
Dimitry Ivanov | 73d2bc0 | 2016-01-26 10:47:49 -0800 | [diff] [blame] | 41 | "libcamera2ndk.so:" |
Dimitry Ivanov | ac1b191 | 2015-12-01 13:56:44 -0800 | [diff] [blame] | 42 | "libdl.so:" |
| 43 | "libEGL.so:" |
| 44 | "libGLESv1_CM.so:" |
| 45 | "libGLESv2.so:" |
| 46 | "libGLESv3.so:" |
Dimitry Ivanov | c24ca89 | 2016-02-02 10:40:08 -0800 | [diff] [blame] | 47 | "libicui18n.so:" |
| 48 | "libicuuc.so:" |
Dimitry Ivanov | ac1b191 | 2015-12-01 13:56:44 -0800 | [diff] [blame] | 49 | "libjnigraphics.so:" |
| 50 | "liblog.so:" |
| 51 | "libmediandk.so:" |
| 52 | "libm.so:" |
| 53 | "libOpenMAXAL.so:" |
| 54 | "libOpenSLES.so:" |
Dimitry Ivanov | cdb6fee | 2016-01-13 15:19:35 -0800 | [diff] [blame] | 55 | "libRS.so:" |
Dimitry Ivanov | ac1b191 | 2015-12-01 13:56:44 -0800 | [diff] [blame] | 56 | "libstdc++.so:" |
Jesse Hall | dee3d96 | 2016-02-23 16:16:47 -0800 | [diff] [blame] | 57 | "libvulkan.so:" |
Dimitry Ivanov | 90bf68e | 2016-01-08 12:35:38 -0800 | [diff] [blame] | 58 | "libwebviewchromium_plat_support.so:" |
Dimitry Ivanov | ac1b191 | 2015-12-01 13:56:44 -0800 | [diff] [blame] | 59 | "libz.so"; |
| 60 | |
| 61 | class LibraryNamespaces { |
| 62 | public: |
Dimitry Ivanov | 426799d | 2016-02-22 11:27:48 -0800 | [diff] [blame] | 63 | LibraryNamespaces() : initialized_(false) { } |
Dimitry Ivanov | ac1b191 | 2015-12-01 13:56:44 -0800 | [diff] [blame] | 64 | |
Dimitry Ivanov | d047c92 | 2016-02-23 14:23:51 -0800 | [diff] [blame] | 65 | android_namespace_t* Create(JNIEnv* env, |
| 66 | jobject class_loader, |
| 67 | bool is_shared, |
| 68 | jstring java_library_path, |
| 69 | jstring java_permitted_path, |
| 70 | int32_t target_sdk_version) { |
Dimitry Ivanov | 0d6e594 | 2015-12-08 11:16:56 -0800 | [diff] [blame] | 71 | ScopedUtfChars library_path(env, java_library_path); |
Dimitry Ivanov | ac1b191 | 2015-12-01 13:56:44 -0800 | [diff] [blame] | 72 | |
Dimitry Ivanov | 0d6e594 | 2015-12-08 11:16:56 -0800 | [diff] [blame] | 73 | std::string permitted_path; |
| 74 | if (java_permitted_path != nullptr) { |
| 75 | ScopedUtfChars path(env, java_permitted_path); |
| 76 | permitted_path = path.c_str(); |
| 77 | } |
| 78 | |
Dimitry Ivanov | dab5673 | 2016-02-17 17:20:15 -0800 | [diff] [blame] | 79 | if (!initialized_ && !InitPublicNamespace(library_path.c_str(), target_sdk_version)) { |
Dimitry Ivanov | ac1b191 | 2015-12-01 13:56:44 -0800 | [diff] [blame] | 80 | return nullptr; |
| 81 | } |
| 82 | |
Dimitry Ivanov | c914ebd | 2016-02-22 13:02:35 -0800 | [diff] [blame] | 83 | android_namespace_t* ns = FindNamespaceByClassLoader(env, class_loader); |
Dimitry Ivanov | ac1b191 | 2015-12-01 13:56:44 -0800 | [diff] [blame] | 84 | |
Dimitry Ivanov | d047c92 | 2016-02-23 14:23:51 -0800 | [diff] [blame] | 85 | LOG_FATAL_IF(ns != nullptr, "There is already a namespace associated with this classloader"); |
Dimitry Ivanov | ac1b191 | 2015-12-01 13:56:44 -0800 | [diff] [blame] | 86 | |
Dimitry Ivanov | d2a6220 | 2015-12-15 11:06:57 -0800 | [diff] [blame] | 87 | uint64_t namespace_type = ANDROID_NAMESPACE_TYPE_ISOLATED; |
| 88 | if (is_shared) { |
| 89 | namespace_type |= ANDROID_NAMESPACE_TYPE_SHARED; |
| 90 | } |
| 91 | |
Dimitry Ivanov | c914ebd | 2016-02-22 13:02:35 -0800 | [diff] [blame] | 92 | ns = android_create_namespace("classloader-namespace", |
| 93 | nullptr, |
| 94 | library_path.c_str(), |
| 95 | namespace_type, |
| 96 | java_permitted_path != nullptr ? |
| 97 | permitted_path.c_str() : |
| 98 | nullptr); |
Dimitry Ivanov | ac1b191 | 2015-12-01 13:56:44 -0800 | [diff] [blame] | 99 | |
Dimitry Ivanov | d047c92 | 2016-02-23 14:23:51 -0800 | [diff] [blame] | 100 | if (ns != nullptr) { |
| 101 | namespaces_.push_back(std::make_pair(env->NewWeakGlobalRef(class_loader), ns)); |
| 102 | } |
Dimitry Ivanov | ac1b191 | 2015-12-01 13:56:44 -0800 | [diff] [blame] | 103 | |
| 104 | return ns; |
| 105 | } |
| 106 | |
Dimitry Ivanov | f44ecde | 2016-02-22 13:48:22 -0800 | [diff] [blame] | 107 | android_namespace_t* FindNamespaceByClassLoader(JNIEnv* env, jobject class_loader) { |
| 108 | auto it = std::find_if(namespaces_.begin(), namespaces_.end(), |
| 109 | [&](const std::pair<jweak, android_namespace_t*>& value) { |
| 110 | return env->IsSameObject(value.first, class_loader); |
| 111 | }); |
| 112 | return it != namespaces_.end() ? it->second : nullptr; |
| 113 | } |
| 114 | |
Dimitry Ivanov | d68c8e9 | 2016-02-10 14:09:22 -0800 | [diff] [blame] | 115 | void PreloadPublicLibraries() { |
| 116 | // android_init_namespaces() expects all the public libraries |
| 117 | // to be loaded so that they can be found by soname alone. |
Dimitry Ivanov | ac1b191 | 2015-12-01 13:56:44 -0800 | [diff] [blame] | 118 | std::vector<std::string> sonames = android::base::Split(kPublicNativeLibraries, ":"); |
| 119 | for (const auto& soname : sonames) { |
Dimitry Ivanov | d68c8e9 | 2016-02-10 14:09:22 -0800 | [diff] [blame] | 120 | dlopen(soname.c_str(), RTLD_NOW | RTLD_NODELETE); |
Dimitry Ivanov | ac1b191 | 2015-12-01 13:56:44 -0800 | [diff] [blame] | 121 | } |
Dimitry Ivanov | d68c8e9 | 2016-02-10 14:09:22 -0800 | [diff] [blame] | 122 | } |
Dimitry Ivanov | ac1b191 | 2015-12-01 13:56:44 -0800 | [diff] [blame] | 123 | |
Dimitry Ivanov | 426799d | 2016-02-22 11:27:48 -0800 | [diff] [blame] | 124 | private: |
Dimitry Ivanov | dab5673 | 2016-02-17 17:20:15 -0800 | [diff] [blame] | 125 | bool InitPublicNamespace(const char* library_path, int32_t target_sdk_version) { |
Dimitry Ivanov | ac1b191 | 2015-12-01 13:56:44 -0800 | [diff] [blame] | 126 | // Some apps call dlopen from generated code unknown to linker in which |
| 127 | // case linker uses anonymous namespace. See b/25844435 for details. |
Dimitry Ivanov | dab5673 | 2016-02-17 17:20:15 -0800 | [diff] [blame] | 128 | std::string publicNativeLibraries = kPublicNativeLibraries; |
| 129 | |
| 130 | // TODO (dimitry): This is a workaround for http://b/26436837 |
| 131 | // will be removed before the release. |
| 132 | if (target_sdk_version <= 23) { |
| 133 | publicNativeLibraries += ":libart.so"; |
| 134 | } |
| 135 | // END OF WORKAROUND |
| 136 | |
| 137 | initialized_ = android_init_namespaces(publicNativeLibraries.c_str(), library_path); |
Dimitry Ivanov | ac1b191 | 2015-12-01 13:56:44 -0800 | [diff] [blame] | 138 | |
| 139 | return initialized_; |
| 140 | } |
| 141 | |
Dimitry Ivanov | ac1b191 | 2015-12-01 13:56:44 -0800 | [diff] [blame] | 142 | bool initialized_; |
Dimitry Ivanov | ac1b191 | 2015-12-01 13:56:44 -0800 | [diff] [blame] | 143 | std::vector<std::pair<jweak, android_namespace_t*>> namespaces_; |
| 144 | |
| 145 | DISALLOW_COPY_AND_ASSIGN(LibraryNamespaces); |
| 146 | }; |
| 147 | |
Dimitry Ivanov | 34d5a20 | 2016-02-29 13:21:43 -0800 | [diff] [blame] | 148 | static std::mutex g_namespaces_mutex; |
Dimitry Ivanov | ac1b191 | 2015-12-01 13:56:44 -0800 | [diff] [blame] | 149 | static LibraryNamespaces* g_namespaces = new LibraryNamespaces; |
Dimitry Ivanov | d047c92 | 2016-02-23 14:23:51 -0800 | [diff] [blame] | 150 | |
| 151 | static bool namespaces_enabled(uint32_t target_sdk_version) { |
| 152 | return target_sdk_version > 0; |
| 153 | } |
Dimitry Ivanov | ac1b191 | 2015-12-01 13:56:44 -0800 | [diff] [blame] | 154 | #endif |
| 155 | |
Dimitry Ivanov | 426799d | 2016-02-22 11:27:48 -0800 | [diff] [blame] | 156 | void PreloadPublicNativeLibraries() { |
| 157 | #if defined(__ANDROID__) |
Dimitry Ivanov | 34d5a20 | 2016-02-29 13:21:43 -0800 | [diff] [blame] | 158 | std::lock_guard<std::mutex> guard(g_namespaces_mutex); |
Dimitry Ivanov | 426799d | 2016-02-22 11:27:48 -0800 | [diff] [blame] | 159 | g_namespaces->PreloadPublicLibraries(); |
| 160 | #endif |
| 161 | } |
| 162 | |
Dimitry Ivanov | ac1b191 | 2015-12-01 13:56:44 -0800 | [diff] [blame] | 163 | |
Dimitry Ivanov | d047c92 | 2016-02-23 14:23:51 -0800 | [diff] [blame] | 164 | jstring CreateClassLoaderNamespace(JNIEnv* env, |
| 165 | int32_t target_sdk_version, |
| 166 | jobject class_loader, |
| 167 | bool is_shared, |
| 168 | jstring library_path, |
| 169 | jstring permitted_path) { |
Dimitry Ivanov | ac1b191 | 2015-12-01 13:56:44 -0800 | [diff] [blame] | 170 | #if defined(__ANDROID__) |
Dimitry Ivanov | d047c92 | 2016-02-23 14:23:51 -0800 | [diff] [blame] | 171 | if (!namespaces_enabled(target_sdk_version)) { |
| 172 | return nullptr; |
| 173 | } |
| 174 | |
Dimitry Ivanov | 34d5a20 | 2016-02-29 13:21:43 -0800 | [diff] [blame] | 175 | std::lock_guard<std::mutex> guard(g_namespaces_mutex); |
Dimitry Ivanov | d047c92 | 2016-02-23 14:23:51 -0800 | [diff] [blame] | 176 | android_namespace_t* ns = g_namespaces->Create(env, |
| 177 | class_loader, |
| 178 | is_shared, |
| 179 | library_path, |
| 180 | permitted_path, |
| 181 | target_sdk_version); |
| 182 | if (ns == nullptr) { |
| 183 | return env->NewStringUTF(dlerror()); |
| 184 | } |
| 185 | #else |
| 186 | UNUSED(env, target_sdk_version, class_loader, is_shared, |
| 187 | library_path, permitted_path); |
| 188 | #endif |
| 189 | return nullptr; |
| 190 | } |
| 191 | |
| 192 | void* OpenNativeLibrary(JNIEnv* env, |
| 193 | int32_t target_sdk_version, |
| 194 | const char* path, |
| 195 | jobject class_loader, |
| 196 | jstring library_path) { |
| 197 | #if defined(__ANDROID__) |
| 198 | if (!namespaces_enabled(target_sdk_version) || class_loader == nullptr) { |
Dimitry Ivanov | ac1b191 | 2015-12-01 13:56:44 -0800 | [diff] [blame] | 199 | return dlopen(path, RTLD_NOW); |
| 200 | } |
| 201 | |
Dimitry Ivanov | 34d5a20 | 2016-02-29 13:21:43 -0800 | [diff] [blame] | 202 | std::lock_guard<std::mutex> guard(g_namespaces_mutex); |
Dimitry Ivanov | d047c92 | 2016-02-23 14:23:51 -0800 | [diff] [blame] | 203 | android_namespace_t* ns = g_namespaces->FindNamespaceByClassLoader(env, class_loader); |
Dimitry Ivanov | ac1b191 | 2015-12-01 13:56:44 -0800 | [diff] [blame] | 204 | |
| 205 | if (ns == nullptr) { |
Dimitry Ivanov | d047c92 | 2016-02-23 14:23:51 -0800 | [diff] [blame] | 206 | // This is the case where the classloader was not created by ApplicationLoaders |
| 207 | // In this case we create an isolated not-shared namespace for it. |
| 208 | ns = g_namespaces->Create(env, class_loader, false, library_path, nullptr, target_sdk_version); |
| 209 | if (ns == nullptr) { |
| 210 | return nullptr; |
| 211 | } |
Dimitry Ivanov | ac1b191 | 2015-12-01 13:56:44 -0800 | [diff] [blame] | 212 | } |
| 213 | |
| 214 | android_dlextinfo extinfo; |
| 215 | extinfo.flags = ANDROID_DLEXT_USE_NAMESPACE; |
| 216 | extinfo.library_namespace = ns; |
| 217 | |
| 218 | return android_dlopen_ext(path, RTLD_NOW, &extinfo); |
| 219 | #else |
Dimitry Ivanov | d047c92 | 2016-02-23 14:23:51 -0800 | [diff] [blame] | 220 | UNUSED(env, target_sdk_version, class_loader, library_path); |
Dimitry Ivanov | ac1b191 | 2015-12-01 13:56:44 -0800 | [diff] [blame] | 221 | return dlopen(path, RTLD_NOW); |
| 222 | #endif |
| 223 | } |
| 224 | |
Dimitry Ivanov | f44ecde | 2016-02-22 13:48:22 -0800 | [diff] [blame] | 225 | #if defined(__ANDROID__) |
| 226 | android_namespace_t* FindNamespaceByClassLoader(JNIEnv* env, jobject class_loader) { |
Dimitry Ivanov | 34d5a20 | 2016-02-29 13:21:43 -0800 | [diff] [blame] | 227 | std::lock_guard<std::mutex> guard(g_namespaces_mutex); |
Dimitry Ivanov | f44ecde | 2016-02-22 13:48:22 -0800 | [diff] [blame] | 228 | return g_namespaces->FindNamespaceByClassLoader(env, class_loader); |
| 229 | } |
| 230 | #endif |
| 231 | |
Dimitry Ivanov | ac1b191 | 2015-12-01 13:56:44 -0800 | [diff] [blame] | 232 | }; // android namespace |