native_loader.cpp revision 55bbb0d88ada4d7394411dba004d2af6086da6a4
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 "libdl.so:" 41 "libEGL.so:" 42 "libGLESv1_CM.so:" 43 "libGLESv2.so:" 44 "libGLESv3.so:" 45 "libicui18n.so:" 46 "libicuuc.so:" 47 "libjnigraphics.so:" 48 "liblog.so:" 49 "libmediandk.so:" 50 "libm.so:" 51 "libOpenMAXAL.so:" 52 "libOpenSLES.so:" 53 "libRS.so:" 54 "libstdc++.so:" 55 "libwebviewchromium_plat_support.so:" 56 "libz.so"; 57 58class LibraryNamespaces { 59 public: 60 LibraryNamespaces() : initialized_(false) { 61 PreloadPublicLibraries(); 62 } 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 ScopedUtfChars library_path(env, java_library_path); 69 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())) { 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 88 uint64_t namespace_type = ANDROID_NAMESPACE_TYPE_ISOLATED; 89 if (is_shared) { 90 namespace_type |= ANDROID_NAMESPACE_TYPE_SHARED; 91 } 92 93 android_namespace_t* ns = 94 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 private: 108 void PreloadPublicLibraries() { 109 // android_init_namespaces() expects all the public libraries 110 // to be loaded so that they can be found by soname alone. 111 std::vector<std::string> sonames = android::base::Split(kPublicNativeLibraries, ":"); 112 for (const auto& soname : sonames) { 113 dlopen(soname.c_str(), RTLD_NOW | RTLD_NODELETE); 114 } 115 } 116 117 bool InitPublicNamespace(const char* library_path) { 118 // Some apps call dlopen from generated code unknown to linker in which 119 // case linker uses anonymous namespace. See b/25844435 for details. 120 initialized_ = android_init_namespaces(kPublicNativeLibraries, library_path); 121 122 return initialized_; 123 } 124 125 std::vector<std::pair<jweak, android_namespace_t*>>::const_iterator 126 FindNamespaceByClassLoader(JNIEnv* env, jobject class_loader) { 127 return std::find_if(namespaces_.begin(), namespaces_.end(), 128 [&](const std::pair<jweak, android_namespace_t*>& value) { 129 return env->IsSameObject(value.first, class_loader); 130 }); 131 } 132 133 bool initialized_; 134 std::mutex mutex_; 135 std::vector<std::pair<jweak, android_namespace_t*>> namespaces_; 136 137 DISALLOW_COPY_AND_ASSIGN(LibraryNamespaces); 138}; 139 140static LibraryNamespaces* g_namespaces = new LibraryNamespaces; 141#endif 142 143 144void* OpenNativeLibrary(JNIEnv* env, int32_t target_sdk_version, const char* path, 145 jobject class_loader, bool is_shared, jstring java_library_path, 146 jstring java_permitted_path) { 147#if defined(__ANDROID__) 148 if (target_sdk_version == 0 || class_loader == nullptr) { 149 return dlopen(path, RTLD_NOW); 150 } 151 152 android_namespace_t* ns = 153 g_namespaces->GetOrCreate(env, class_loader, is_shared, 154 java_library_path, java_permitted_path); 155 156 if (ns == nullptr) { 157 return nullptr; 158 } 159 160 android_dlextinfo extinfo; 161 extinfo.flags = ANDROID_DLEXT_USE_NAMESPACE; 162 extinfo.library_namespace = ns; 163 164 return android_dlopen_ext(path, RTLD_NOW, &extinfo); 165#else 166 UNUSED(env, target_sdk_version, class_loader, is_shared, 167 java_library_path, java_permitted_path); 168 return dlopen(path, RTLD_NOW); 169#endif 170} 171 172}; // android namespace 173