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" |
| 24 | #endif |
| 25 | |
| 26 | #include <algorithm> |
| 27 | #include <vector> |
| 28 | #include <string> |
| 29 | #include <mutex> |
| 30 | |
Elliott Hughes | 8b04714 | 2015-12-08 10:38:59 -0800 | [diff] [blame] | 31 | #include "android-base/macros.h" |
| 32 | #include "android-base/strings.h" |
Dimitry Ivanov | ac1b191 | 2015-12-01 13:56:44 -0800 | [diff] [blame] | 33 | |
| 34 | namespace android { |
| 35 | |
| 36 | #ifdef __ANDROID__ |
| 37 | // TODO(dimitry): move this to system properties. |
| 38 | static const char* kPublicNativeLibraries = "libandroid.so:" |
Dimitry Ivanov | 3d5a434 | 2016-01-11 14:53:59 -0800 | [diff] [blame] | 39 | // TODO (dimitry): This is a workaround for http://b/26436837 |
| 40 | // will be removed before the release. |
| 41 | "libart.so:" |
| 42 | // END OF WORKAROUND |
Dimitry Ivanov | ac1b191 | 2015-12-01 13:56:44 -0800 | [diff] [blame] | 43 | "libc.so:" |
| 44 | "libdl.so:" |
| 45 | "libEGL.so:" |
| 46 | "libGLESv1_CM.so:" |
| 47 | "libGLESv2.so:" |
| 48 | "libGLESv3.so:" |
| 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:" |
Dimitry Ivanov | 90bf68e | 2016-01-08 12:35:38 -0800 | [diff] [blame] | 57 | "libwebviewchromium_plat_support.so:" |
Dimitry Ivanov | ac1b191 | 2015-12-01 13:56:44 -0800 | [diff] [blame] | 58 | "libz.so"; |
| 59 | |
| 60 | class LibraryNamespaces { |
| 61 | public: |
| 62 | LibraryNamespaces() : initialized_(false) { } |
| 63 | |
Dimitry Ivanov | 0d6e594 | 2015-12-08 11:16:56 -0800 | [diff] [blame] | 64 | android_namespace_t* GetOrCreate(JNIEnv* env, jobject class_loader, |
Dimitry Ivanov | d2a6220 | 2015-12-15 11:06:57 -0800 | [diff] [blame] | 65 | bool is_shared, |
Dimitry Ivanov | 0d6e594 | 2015-12-08 11:16:56 -0800 | [diff] [blame] | 66 | jstring java_library_path, |
| 67 | jstring java_permitted_path) { |
| 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 | |
| 80 | std::lock_guard<std::mutex> guard(mutex_); |
| 81 | |
| 82 | auto it = FindNamespaceByClassLoader(env, class_loader); |
| 83 | |
| 84 | if (it != namespaces_.end()) { |
| 85 | return it->second; |
| 86 | } |
| 87 | |
Dimitry Ivanov | d2a6220 | 2015-12-15 11:06:57 -0800 | [diff] [blame] | 88 | uint64_t namespace_type = ANDROID_NAMESPACE_TYPE_ISOLATED; |
| 89 | if (is_shared) { |
| 90 | namespace_type |= ANDROID_NAMESPACE_TYPE_SHARED; |
| 91 | } |
| 92 | |
Dimitry Ivanov | ac1b191 | 2015-12-01 13:56:44 -0800 | [diff] [blame] | 93 | android_namespace_t* ns = |
| 94 | android_create_namespace("classloader-namespace", |
| 95 | nullptr, |
Dimitry Ivanov | 0d6e594 | 2015-12-08 11:16:56 -0800 | [diff] [blame] | 96 | library_path.c_str(), |
Dimitry Ivanov | d2a6220 | 2015-12-15 11:06:57 -0800 | [diff] [blame] | 97 | namespace_type, |
Dimitry Ivanov | 0d6e594 | 2015-12-08 11:16:56 -0800 | [diff] [blame] | 98 | java_permitted_path != nullptr ? |
| 99 | permitted_path.c_str() : |
| 100 | nullptr); |
Dimitry Ivanov | ac1b191 | 2015-12-01 13:56:44 -0800 | [diff] [blame] | 101 | |
| 102 | namespaces_.push_back(std::make_pair(env->NewWeakGlobalRef(class_loader), ns)); |
| 103 | |
| 104 | return ns; |
| 105 | } |
| 106 | |
| 107 | private: |
| 108 | bool InitPublicNamespace(const char* library_path) { |
| 109 | // Make sure all the public libraries are loaded |
| 110 | std::vector<std::string> sonames = android::base::Split(kPublicNativeLibraries, ":"); |
| 111 | for (const auto& soname : sonames) { |
| 112 | if (dlopen(soname.c_str(), RTLD_NOW | RTLD_NODELETE) == nullptr) { |
| 113 | return false; |
| 114 | } |
| 115 | } |
| 116 | |
| 117 | // Some apps call dlopen from generated code unknown to linker in which |
| 118 | // case linker uses anonymous namespace. See b/25844435 for details. |
| 119 | initialized_ = android_init_namespaces(kPublicNativeLibraries, library_path); |
| 120 | |
| 121 | return initialized_; |
| 122 | } |
| 123 | |
| 124 | std::vector<std::pair<jweak, android_namespace_t*>>::const_iterator |
| 125 | FindNamespaceByClassLoader(JNIEnv* env, jobject class_loader) { |
| 126 | return std::find_if(namespaces_.begin(), namespaces_.end(), |
| 127 | [&](const std::pair<jweak, android_namespace_t*>& value) { |
| 128 | return env->IsSameObject(value.first, class_loader); |
| 129 | }); |
| 130 | } |
| 131 | |
| 132 | bool initialized_; |
| 133 | std::mutex mutex_; |
| 134 | std::vector<std::pair<jweak, android_namespace_t*>> namespaces_; |
| 135 | |
| 136 | DISALLOW_COPY_AND_ASSIGN(LibraryNamespaces); |
| 137 | }; |
| 138 | |
| 139 | static LibraryNamespaces* g_namespaces = new LibraryNamespaces; |
| 140 | #endif |
| 141 | |
| 142 | |
| 143 | void* OpenNativeLibrary(JNIEnv* env, int32_t target_sdk_version, const char* path, |
Dimitry Ivanov | d2a6220 | 2015-12-15 11:06:57 -0800 | [diff] [blame] | 144 | jobject class_loader, bool is_shared, jstring java_library_path, |
Dimitry Ivanov | 0d6e594 | 2015-12-08 11:16:56 -0800 | [diff] [blame] | 145 | jstring java_permitted_path) { |
Dimitry Ivanov | ac1b191 | 2015-12-01 13:56:44 -0800 | [diff] [blame] | 146 | #if defined(__ANDROID__) |
Dimitry Ivanov | 619ffb4 | 2015-12-15 15:55:50 -0800 | [diff] [blame] | 147 | if (target_sdk_version == 0 || class_loader == nullptr) { |
Dimitry Ivanov | ac1b191 | 2015-12-01 13:56:44 -0800 | [diff] [blame] | 148 | return dlopen(path, RTLD_NOW); |
| 149 | } |
| 150 | |
Dimitry Ivanov | 0d6e594 | 2015-12-08 11:16:56 -0800 | [diff] [blame] | 151 | android_namespace_t* ns = |
Dimitry Ivanov | d2a6220 | 2015-12-15 11:06:57 -0800 | [diff] [blame] | 152 | g_namespaces->GetOrCreate(env, class_loader, is_shared, |
| 153 | java_library_path, java_permitted_path); |
Dimitry Ivanov | ac1b191 | 2015-12-01 13:56:44 -0800 | [diff] [blame] | 154 | |
| 155 | if (ns == nullptr) { |
| 156 | return nullptr; |
| 157 | } |
| 158 | |
| 159 | android_dlextinfo extinfo; |
| 160 | extinfo.flags = ANDROID_DLEXT_USE_NAMESPACE; |
| 161 | extinfo.library_namespace = ns; |
| 162 | |
| 163 | return android_dlopen_ext(path, RTLD_NOW, &extinfo); |
| 164 | #else |
Dimitry Ivanov | d2a6220 | 2015-12-15 11:06:57 -0800 | [diff] [blame] | 165 | UNUSED(env, target_sdk_version, class_loader, is_shared, |
Dimitry Ivanov | 0d6e594 | 2015-12-08 11:16:56 -0800 | [diff] [blame] | 166 | java_library_path, java_permitted_path); |
Dimitry Ivanov | ac1b191 | 2015-12-01 13:56:44 -0800 | [diff] [blame] | 167 | return dlopen(path, RTLD_NOW); |
| 168 | #endif |
| 169 | } |
| 170 | |
| 171 | }; // android namespace |