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