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 | |
Dimitry Ivanov | 4b0e963 | 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 | 4b0e963 | 2016-03-15 13:51:26 -0700 | [diff] [blame] | 38 | #if defined(__ANDROID__) |
| 39 | static constexpr const char* kPublicNativeLibrariesConfig = "/system/etc/public.libraries.txt"; |
Dimitry Ivanov | ac1b191 | 2015-12-01 13:56:44 -0800 | [diff] [blame] | 40 | |
| 41 | class LibraryNamespaces { |
| 42 | public: |
Dimitry Ivanov | 426799d | 2016-02-22 11:27:48 -0800 | [diff] [blame] | 43 | LibraryNamespaces() : initialized_(false) { } |
Dimitry Ivanov | ac1b191 | 2015-12-01 13:56:44 -0800 | [diff] [blame] | 44 | |
Dimitry Ivanov | d047c92 | 2016-02-23 14:23:51 -0800 | [diff] [blame] | 45 | android_namespace_t* Create(JNIEnv* env, |
| 46 | jobject class_loader, |
| 47 | bool is_shared, |
| 48 | jstring java_library_path, |
| 49 | jstring java_permitted_path, |
| 50 | int32_t target_sdk_version) { |
Dimitry Ivanov | 0d6e594 | 2015-12-08 11:16:56 -0800 | [diff] [blame] | 51 | ScopedUtfChars library_path(env, java_library_path); |
Dimitry Ivanov | ac1b191 | 2015-12-01 13:56:44 -0800 | [diff] [blame] | 52 | |
Dimitry Ivanov | 0d6e594 | 2015-12-08 11:16:56 -0800 | [diff] [blame] | 53 | std::string permitted_path; |
| 54 | if (java_permitted_path != nullptr) { |
| 55 | ScopedUtfChars path(env, java_permitted_path); |
| 56 | permitted_path = path.c_str(); |
| 57 | } |
| 58 | |
Dimitry Ivanov | dab5673 | 2016-02-17 17:20:15 -0800 | [diff] [blame] | 59 | if (!initialized_ && !InitPublicNamespace(library_path.c_str(), target_sdk_version)) { |
Dimitry Ivanov | ac1b191 | 2015-12-01 13:56:44 -0800 | [diff] [blame] | 60 | return nullptr; |
| 61 | } |
| 62 | |
Dimitry Ivanov | c914ebd | 2016-02-22 13:02:35 -0800 | [diff] [blame] | 63 | android_namespace_t* ns = FindNamespaceByClassLoader(env, class_loader); |
Dimitry Ivanov | ac1b191 | 2015-12-01 13:56:44 -0800 | [diff] [blame] | 64 | |
Dimitry Ivanov | 4b0e963 | 2016-03-15 13:51:26 -0700 | [diff] [blame] | 65 | LOG_ALWAYS_FATAL_IF(ns != nullptr, |
| 66 | "There is already a namespace associated with this classloader"); |
Dimitry Ivanov | ac1b191 | 2015-12-01 13:56:44 -0800 | [diff] [blame] | 67 | |
Dimitry Ivanov | d2a6220 | 2015-12-15 11:06:57 -0800 | [diff] [blame] | 68 | uint64_t namespace_type = ANDROID_NAMESPACE_TYPE_ISOLATED; |
| 69 | if (is_shared) { |
| 70 | namespace_type |= ANDROID_NAMESPACE_TYPE_SHARED; |
| 71 | } |
| 72 | |
Dimitry Ivanov | c914ebd | 2016-02-22 13:02:35 -0800 | [diff] [blame] | 73 | ns = android_create_namespace("classloader-namespace", |
| 74 | nullptr, |
| 75 | library_path.c_str(), |
| 76 | namespace_type, |
| 77 | java_permitted_path != nullptr ? |
| 78 | permitted_path.c_str() : |
| 79 | nullptr); |
Dimitry Ivanov | ac1b191 | 2015-12-01 13:56:44 -0800 | [diff] [blame] | 80 | |
Dimitry Ivanov | d047c92 | 2016-02-23 14:23:51 -0800 | [diff] [blame] | 81 | if (ns != nullptr) { |
| 82 | namespaces_.push_back(std::make_pair(env->NewWeakGlobalRef(class_loader), ns)); |
| 83 | } |
Dimitry Ivanov | ac1b191 | 2015-12-01 13:56:44 -0800 | [diff] [blame] | 84 | |
| 85 | return ns; |
| 86 | } |
| 87 | |
Dimitry Ivanov | f44ecde | 2016-02-22 13:48:22 -0800 | [diff] [blame] | 88 | android_namespace_t* FindNamespaceByClassLoader(JNIEnv* env, jobject class_loader) { |
| 89 | auto it = std::find_if(namespaces_.begin(), namespaces_.end(), |
| 90 | [&](const std::pair<jweak, android_namespace_t*>& value) { |
| 91 | return env->IsSameObject(value.first, class_loader); |
| 92 | }); |
| 93 | return it != namespaces_.end() ? it->second : nullptr; |
| 94 | } |
| 95 | |
Dimitry Ivanov | 4b0e963 | 2016-03-15 13:51:26 -0700 | [diff] [blame] | 96 | void Initialize() { |
| 97 | // Read list of public native libraries from the config file. |
| 98 | std::string file_content; |
| 99 | LOG_ALWAYS_FATAL_IF(!base::ReadFileToString(kPublicNativeLibrariesConfig, &file_content), |
| 100 | "Error reading public native library list from \"%s\": %s", |
| 101 | kPublicNativeLibrariesConfig, strerror(errno)); |
| 102 | |
| 103 | std::vector<std::string> lines = base::Split(file_content, "\n"); |
| 104 | |
| 105 | std::vector<std::string> sonames; |
| 106 | |
| 107 | for (const auto& line : lines) { |
| 108 | auto trimmed_line = base::Trim(line); |
| 109 | if (trimmed_line[0] == '#' || trimmed_line.empty()) { |
| 110 | continue; |
| 111 | } |
| 112 | |
| 113 | sonames.push_back(trimmed_line); |
| 114 | } |
| 115 | |
| 116 | public_libraries_ = base::Join(sonames, ':'); |
| 117 | |
Dimitry Ivanov | d68c8e9 | 2016-02-10 14:09:22 -0800 | [diff] [blame] | 118 | // android_init_namespaces() expects all the public libraries |
| 119 | // to be loaded so that they can be found by soname alone. |
Dimitry Ivanov | ac1b191 | 2015-12-01 13:56:44 -0800 | [diff] [blame] | 120 | for (const auto& soname : sonames) { |
Dimitry Ivanov | d68c8e9 | 2016-02-10 14:09:22 -0800 | [diff] [blame] | 121 | dlopen(soname.c_str(), RTLD_NOW | RTLD_NODELETE); |
Dimitry Ivanov | ac1b191 | 2015-12-01 13:56:44 -0800 | [diff] [blame] | 122 | } |
Dimitry Ivanov | d68c8e9 | 2016-02-10 14:09:22 -0800 | [diff] [blame] | 123 | } |
Dimitry Ivanov | ac1b191 | 2015-12-01 13:56:44 -0800 | [diff] [blame] | 124 | |
Dimitry Ivanov | 426799d | 2016-02-22 11:27:48 -0800 | [diff] [blame] | 125 | private: |
Dimitry Ivanov | dab5673 | 2016-02-17 17:20:15 -0800 | [diff] [blame] | 126 | bool InitPublicNamespace(const char* library_path, int32_t target_sdk_version) { |
Dimitry Ivanov | 4b0e963 | 2016-03-15 13:51:26 -0700 | [diff] [blame] | 127 | std::string publicNativeLibraries = public_libraries_; |
Dimitry Ivanov | dab5673 | 2016-02-17 17:20:15 -0800 | [diff] [blame] | 128 | |
| 129 | // TODO (dimitry): This is a workaround for http://b/26436837 |
| 130 | // will be removed before the release. |
| 131 | if (target_sdk_version <= 23) { |
Dimitry Ivanov | 5f28b84 | 2016-03-04 11:00:37 -0800 | [diff] [blame] | 132 | // check if libart.so is loaded. |
| 133 | void* handle = dlopen("libart.so", RTLD_NOW | RTLD_NOLOAD); |
| 134 | if (handle != nullptr) { |
| 135 | publicNativeLibraries += ":libart.so"; |
| 136 | dlclose(handle); |
| 137 | } |
Dimitry Ivanov | dab5673 | 2016-02-17 17:20:15 -0800 | [diff] [blame] | 138 | } |
| 139 | // END OF WORKAROUND |
| 140 | |
Dimitry Ivanov | 4b0e963 | 2016-03-15 13:51:26 -0700 | [diff] [blame] | 141 | // (http://b/25844435) - Some apps call dlopen from generated code (mono jited |
| 142 | // code is one example) unknown to linker in which case linker uses anonymous |
| 143 | // namespace. The second argument specifies the search path for the anonymous |
| 144 | // namespace which is the library_path of the classloader. |
Dimitry Ivanov | dab5673 | 2016-02-17 17:20:15 -0800 | [diff] [blame] | 145 | initialized_ = android_init_namespaces(publicNativeLibraries.c_str(), library_path); |
Dimitry Ivanov | ac1b191 | 2015-12-01 13:56:44 -0800 | [diff] [blame] | 146 | |
| 147 | return initialized_; |
| 148 | } |
| 149 | |
Dimitry Ivanov | ac1b191 | 2015-12-01 13:56:44 -0800 | [diff] [blame] | 150 | bool initialized_; |
Dimitry Ivanov | ac1b191 | 2015-12-01 13:56:44 -0800 | [diff] [blame] | 151 | std::vector<std::pair<jweak, android_namespace_t*>> namespaces_; |
Dimitry Ivanov | 4b0e963 | 2016-03-15 13:51:26 -0700 | [diff] [blame] | 152 | std::string public_libraries_; |
| 153 | |
Dimitry Ivanov | ac1b191 | 2015-12-01 13:56:44 -0800 | [diff] [blame] | 154 | |
| 155 | DISALLOW_COPY_AND_ASSIGN(LibraryNamespaces); |
| 156 | }; |
| 157 | |
Dimitry Ivanov | 34d5a20 | 2016-02-29 13:21:43 -0800 | [diff] [blame] | 158 | static std::mutex g_namespaces_mutex; |
Dimitry Ivanov | ac1b191 | 2015-12-01 13:56:44 -0800 | [diff] [blame] | 159 | static LibraryNamespaces* g_namespaces = new LibraryNamespaces; |
Dimitry Ivanov | d047c92 | 2016-02-23 14:23:51 -0800 | [diff] [blame] | 160 | |
| 161 | static bool namespaces_enabled(uint32_t target_sdk_version) { |
| 162 | return target_sdk_version > 0; |
| 163 | } |
Dimitry Ivanov | ac1b191 | 2015-12-01 13:56:44 -0800 | [diff] [blame] | 164 | #endif |
| 165 | |
Dimitry Ivanov | 4b0e963 | 2016-03-15 13:51:26 -0700 | [diff] [blame] | 166 | void InitializeNativeLoader() { |
Dimitry Ivanov | 426799d | 2016-02-22 11:27:48 -0800 | [diff] [blame] | 167 | #if defined(__ANDROID__) |
Dimitry Ivanov | 34d5a20 | 2016-02-29 13:21:43 -0800 | [diff] [blame] | 168 | std::lock_guard<std::mutex> guard(g_namespaces_mutex); |
Dimitry Ivanov | 4b0e963 | 2016-03-15 13:51:26 -0700 | [diff] [blame] | 169 | g_namespaces->Initialize(); |
Dimitry Ivanov | 426799d | 2016-02-22 11:27:48 -0800 | [diff] [blame] | 170 | #endif |
| 171 | } |
| 172 | |
Dimitry Ivanov | ac1b191 | 2015-12-01 13:56:44 -0800 | [diff] [blame] | 173 | |
Dimitry Ivanov | d047c92 | 2016-02-23 14:23:51 -0800 | [diff] [blame] | 174 | jstring CreateClassLoaderNamespace(JNIEnv* env, |
| 175 | int32_t target_sdk_version, |
| 176 | jobject class_loader, |
| 177 | bool is_shared, |
| 178 | jstring library_path, |
| 179 | jstring permitted_path) { |
Dimitry Ivanov | ac1b191 | 2015-12-01 13:56:44 -0800 | [diff] [blame] | 180 | #if defined(__ANDROID__) |
Dimitry Ivanov | d047c92 | 2016-02-23 14:23:51 -0800 | [diff] [blame] | 181 | if (!namespaces_enabled(target_sdk_version)) { |
| 182 | return nullptr; |
| 183 | } |
| 184 | |
Dimitry Ivanov | 34d5a20 | 2016-02-29 13:21:43 -0800 | [diff] [blame] | 185 | std::lock_guard<std::mutex> guard(g_namespaces_mutex); |
Dimitry Ivanov | d047c92 | 2016-02-23 14:23:51 -0800 | [diff] [blame] | 186 | android_namespace_t* ns = g_namespaces->Create(env, |
| 187 | class_loader, |
| 188 | is_shared, |
| 189 | library_path, |
| 190 | permitted_path, |
| 191 | target_sdk_version); |
| 192 | if (ns == nullptr) { |
| 193 | return env->NewStringUTF(dlerror()); |
| 194 | } |
| 195 | #else |
| 196 | UNUSED(env, target_sdk_version, class_loader, is_shared, |
| 197 | library_path, permitted_path); |
| 198 | #endif |
| 199 | return nullptr; |
| 200 | } |
| 201 | |
| 202 | void* OpenNativeLibrary(JNIEnv* env, |
| 203 | int32_t target_sdk_version, |
| 204 | const char* path, |
| 205 | jobject class_loader, |
| 206 | jstring library_path) { |
| 207 | #if defined(__ANDROID__) |
| 208 | if (!namespaces_enabled(target_sdk_version) || class_loader == nullptr) { |
Dimitry Ivanov | ac1b191 | 2015-12-01 13:56:44 -0800 | [diff] [blame] | 209 | return dlopen(path, RTLD_NOW); |
| 210 | } |
| 211 | |
Dimitry Ivanov | 34d5a20 | 2016-02-29 13:21:43 -0800 | [diff] [blame] | 212 | std::lock_guard<std::mutex> guard(g_namespaces_mutex); |
Dimitry Ivanov | d047c92 | 2016-02-23 14:23:51 -0800 | [diff] [blame] | 213 | android_namespace_t* ns = g_namespaces->FindNamespaceByClassLoader(env, class_loader); |
Dimitry Ivanov | ac1b191 | 2015-12-01 13:56:44 -0800 | [diff] [blame] | 214 | |
| 215 | if (ns == nullptr) { |
Dimitry Ivanov | d047c92 | 2016-02-23 14:23:51 -0800 | [diff] [blame] | 216 | // This is the case where the classloader was not created by ApplicationLoaders |
| 217 | // In this case we create an isolated not-shared namespace for it. |
| 218 | ns = g_namespaces->Create(env, class_loader, false, library_path, nullptr, target_sdk_version); |
| 219 | if (ns == nullptr) { |
| 220 | return nullptr; |
| 221 | } |
Dimitry Ivanov | ac1b191 | 2015-12-01 13:56:44 -0800 | [diff] [blame] | 222 | } |
| 223 | |
| 224 | android_dlextinfo extinfo; |
| 225 | extinfo.flags = ANDROID_DLEXT_USE_NAMESPACE; |
| 226 | extinfo.library_namespace = ns; |
| 227 | |
| 228 | return android_dlopen_ext(path, RTLD_NOW, &extinfo); |
| 229 | #else |
Dimitry Ivanov | d047c92 | 2016-02-23 14:23:51 -0800 | [diff] [blame] | 230 | UNUSED(env, target_sdk_version, class_loader, library_path); |
Dimitry Ivanov | ac1b191 | 2015-12-01 13:56:44 -0800 | [diff] [blame] | 231 | return dlopen(path, RTLD_NOW); |
| 232 | #endif |
| 233 | } |
| 234 | |
Dimitry Ivanov | f44ecde | 2016-02-22 13:48:22 -0800 | [diff] [blame] | 235 | #if defined(__ANDROID__) |
| 236 | android_namespace_t* FindNamespaceByClassLoader(JNIEnv* env, jobject class_loader) { |
Dimitry Ivanov | 34d5a20 | 2016-02-29 13:21:43 -0800 | [diff] [blame] | 237 | std::lock_guard<std::mutex> guard(g_namespaces_mutex); |
Dimitry Ivanov | f44ecde | 2016-02-22 13:48:22 -0800 | [diff] [blame] | 238 | return g_namespaces->FindNamespaceByClassLoader(env, class_loader); |
| 239 | } |
| 240 | #endif |
| 241 | |
Dimitry Ivanov | ac1b191 | 2015-12-01 13:56:44 -0800 | [diff] [blame] | 242 | }; // android namespace |