native_loader.cpp revision 426799d77014888315233253e96277bef13158eb
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 31#include "android-base/macros.h" 32#include "android-base/strings.h" 33 34namespace android { 35 36#ifdef __ANDROID__ 37// TODO(dimitry): move this to system properties. 38static const char* kPublicNativeLibraries = "libandroid.so:" 39 "libc.so:" 40 "libcamera2ndk.so:" 41 "libdl.so:" 42 "libEGL.so:" 43 "libGLESv1_CM.so:" 44 "libGLESv2.so:" 45 "libGLESv3.so:" 46 "libicui18n.so:" 47 "libicuuc.so:" 48 "libjnigraphics.so:" 49 "liblog.so:" 50 "libmediandk.so:" 51 "libm.so:" 52 "libOpenMAXAL.so:" 53 "libOpenSLES.so:" 54 "libRS.so:" 55 "libstdc++.so:" 56 "libvulkan.so:" 57 "libwebviewchromium_plat_support.so:" 58 "libz.so"; 59 60class LibraryNamespaces { 61 public: 62 LibraryNamespaces() : initialized_(false) { } 63 64 android_namespace_t* GetOrCreate(JNIEnv* env, jobject class_loader, 65 bool is_shared, 66 jstring java_library_path, 67 jstring java_permitted_path, 68 int32_t target_sdk_version) { 69 ScopedUtfChars library_path(env, java_library_path); 70 71 std::string permitted_path; 72 if (java_permitted_path != nullptr) { 73 ScopedUtfChars path(env, java_permitted_path); 74 permitted_path = path.c_str(); 75 } 76 77 if (!initialized_ && !InitPublicNamespace(library_path.c_str(), target_sdk_version)) { 78 return nullptr; 79 } 80 81 std::lock_guard<std::mutex> guard(mutex_); 82 83 android_namespace_t* ns = FindNamespaceByClassLoader(env, class_loader); 84 85 if (ns != nullptr) { 86 return ns; 87 } 88 89 uint64_t namespace_type = ANDROID_NAMESPACE_TYPE_ISOLATED; 90 if (is_shared) { 91 namespace_type |= ANDROID_NAMESPACE_TYPE_SHARED; 92 } 93 94 ns = android_create_namespace("classloader-namespace", 95 nullptr, 96 library_path.c_str(), 97 namespace_type, 98 java_permitted_path != nullptr ? 99 permitted_path.c_str() : 100 nullptr); 101 102 namespaces_.push_back(std::make_pair(env->NewWeakGlobalRef(class_loader), ns)); 103 104 return ns; 105 } 106 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 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. 118 std::vector<std::string> sonames = android::base::Split(kPublicNativeLibraries, ":"); 119 for (const auto& soname : sonames) { 120 dlopen(soname.c_str(), RTLD_NOW | RTLD_NODELETE); 121 } 122 } 123 124 private: 125 bool InitPublicNamespace(const char* library_path, int32_t target_sdk_version) { 126 // Some apps call dlopen from generated code unknown to linker in which 127 // case linker uses anonymous namespace. See b/25844435 for details. 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); 138 139 return initialized_; 140 } 141 142 bool initialized_; 143 std::mutex mutex_; 144 std::vector<std::pair<jweak, android_namespace_t*>> namespaces_; 145 146 DISALLOW_COPY_AND_ASSIGN(LibraryNamespaces); 147}; 148 149static LibraryNamespaces* g_namespaces = new LibraryNamespaces; 150#endif 151 152void PreloadPublicNativeLibraries() { 153#if defined(__ANDROID__) 154 g_namespaces->PreloadPublicLibraries(); 155#endif 156} 157 158 159void* OpenNativeLibrary(JNIEnv* env, int32_t target_sdk_version, const char* path, 160 jobject class_loader, bool is_shared, jstring java_library_path, 161 jstring java_permitted_path) { 162#if defined(__ANDROID__) 163 if (target_sdk_version == 0 || class_loader == nullptr) { 164 return dlopen(path, RTLD_NOW); 165 } 166 167 android_namespace_t* ns = 168 g_namespaces->GetOrCreate(env, class_loader, is_shared, 169 java_library_path, java_permitted_path, target_sdk_version); 170 171 if (ns == nullptr) { 172 return nullptr; 173 } 174 175 android_dlextinfo extinfo; 176 extinfo.flags = ANDROID_DLEXT_USE_NAMESPACE; 177 extinfo.library_namespace = ns; 178 179 return android_dlopen_ext(path, RTLD_NOW, &extinfo); 180#else 181 UNUSED(env, target_sdk_version, class_loader, is_shared, 182 java_library_path, java_permitted_path); 183 return dlopen(path, RTLD_NOW); 184#endif 185} 186 187#if defined(__ANDROID__) 188android_namespace_t* FindNamespaceByClassLoader(JNIEnv* env, jobject class_loader) { 189 return g_namespaces->FindNamespaceByClassLoader(env, class_loader); 190} 191#endif 192 193}; // android namespace 194