1ac1b1919f8d655f652e77f59cc52813199085cd9Dimitry Ivanov/* 2ac1b1919f8d655f652e77f59cc52813199085cd9Dimitry Ivanov * Copyright (C) 2015 The Android Open Source Project 3ac1b1919f8d655f652e77f59cc52813199085cd9Dimitry Ivanov * 4ac1b1919f8d655f652e77f59cc52813199085cd9Dimitry Ivanov * Licensed under the Apache License, Version 2.0 (the "License"); 5ac1b1919f8d655f652e77f59cc52813199085cd9Dimitry Ivanov * you may not use this file except in compliance with the License. 6ac1b1919f8d655f652e77f59cc52813199085cd9Dimitry Ivanov * You may obtain a copy of the License at 7ac1b1919f8d655f652e77f59cc52813199085cd9Dimitry Ivanov * 8ac1b1919f8d655f652e77f59cc52813199085cd9Dimitry Ivanov * http://www.apache.org/licenses/LICENSE-2.0 9ac1b1919f8d655f652e77f59cc52813199085cd9Dimitry Ivanov * 10ac1b1919f8d655f652e77f59cc52813199085cd9Dimitry Ivanov * Unless required by applicable law or agreed to in writing, software 11ac1b1919f8d655f652e77f59cc52813199085cd9Dimitry Ivanov * distributed under the License is distributed on an "AS IS" BASIS, 12ac1b1919f8d655f652e77f59cc52813199085cd9Dimitry Ivanov * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13ac1b1919f8d655f652e77f59cc52813199085cd9Dimitry Ivanov * See the License for the specific language governing permissions and 14ac1b1919f8d655f652e77f59cc52813199085cd9Dimitry Ivanov * limitations under the License. 15ac1b1919f8d655f652e77f59cc52813199085cd9Dimitry Ivanov */ 16ac1b1919f8d655f652e77f59cc52813199085cd9Dimitry Ivanov 17ac1b1919f8d655f652e77f59cc52813199085cd9Dimitry Ivanov#include "nativeloader/native_loader.h" 18ac1b1919f8d655f652e77f59cc52813199085cd9Dimitry Ivanov#include "ScopedUtfChars.h" 19ac1b1919f8d655f652e77f59cc52813199085cd9Dimitry Ivanov 20ac1b1919f8d655f652e77f59cc52813199085cd9Dimitry Ivanov#include <dlfcn.h> 21ac1b1919f8d655f652e77f59cc52813199085cd9Dimitry Ivanov#ifdef __ANDROID__ 22c337cae9adcb538a4562641f97bdde933d085a82Dimitry Ivanov#include "dlext_namespaces.h" 23ac1b1919f8d655f652e77f59cc52813199085cd9Dimitry Ivanov#include "cutils/properties.h" 247f9a1aaf05dda20904f08a667dadcf555a41477eDimitry Ivanov#define LOG_TAG "libnativeloader" 25d047c925af62e1fe28fcd1c1940df4afe18d458aDimitry Ivanov#include "log/log.h" 26ac1b1919f8d655f652e77f59cc52813199085cd9Dimitry Ivanov#endif 27ac1b1919f8d655f652e77f59cc52813199085cd9Dimitry Ivanov 28ac1b1919f8d655f652e77f59cc52813199085cd9Dimitry Ivanov#include <algorithm> 29ac1b1919f8d655f652e77f59cc52813199085cd9Dimitry Ivanov#include <vector> 30ac1b1919f8d655f652e77f59cc52813199085cd9Dimitry Ivanov#include <string> 31ac1b1919f8d655f652e77f59cc52813199085cd9Dimitry Ivanov#include <mutex> 32ac1b1919f8d655f652e77f59cc52813199085cd9Dimitry Ivanov 334b0e963872715775a63f36b385150cba4801b1d0Dimitry Ivanov#include "android-base/file.h" 348b0471462e5bb9e81652a1a53cc786df9848feefElliott Hughes#include "android-base/macros.h" 358b0471462e5bb9e81652a1a53cc786df9848feefElliott Hughes#include "android-base/strings.h" 36ac1b1919f8d655f652e77f59cc52813199085cd9Dimitry Ivanov 37ac1b1919f8d655f652e77f59cc52813199085cd9Dimitry Ivanovnamespace android { 38ac1b1919f8d655f652e77f59cc52813199085cd9Dimitry Ivanov 394b0e963872715775a63f36b385150cba4801b1d0Dimitry Ivanov#if defined(__ANDROID__) 400b5651e1588c2fee6a587314993a166a77068974Dimitry Ivanovstatic constexpr const char* kPublicNativeLibrariesSystemConfigPathFromRoot = "/etc/public.libraries.txt"; 41b614045894c99fb1d35179d3e0a0bab0d5dec666Dimitry Ivanovstatic constexpr const char* kPublicNativeLibrariesVendorConfig = "/vendor/etc/public.libraries.txt"; 42ac1b1919f8d655f652e77f59cc52813199085cd9Dimitry Ivanov 43f334cbf0e1425633bef96a21b0ce9e30f4c6ffa9Dimitry Ivanov// (http://b/27588281) This is a workaround for apps using custom classloaders and calling 44f334cbf0e1425633bef96a21b0ce9e30f4c6ffa9Dimitry Ivanov// System.load() with an absolute path which is outside of the classloader library search path. 45f334cbf0e1425633bef96a21b0ce9e30f4c6ffa9Dimitry Ivanov// This list includes all directories app is allowed to access this way. 46f334cbf0e1425633bef96a21b0ce9e30f4c6ffa9Dimitry Ivanovstatic constexpr const char* kWhitelistedDirectories = "/data:/mnt/expand"; 47f334cbf0e1425633bef96a21b0ce9e30f4c6ffa9Dimitry Ivanov 487d02829636cfb4d23c5665b1481ccb3d20f27b56Dimitry Ivanovstatic bool is_debuggable() { 497d02829636cfb4d23c5665b1481ccb3d20f27b56Dimitry Ivanov char debuggable[PROP_VALUE_MAX]; 507d02829636cfb4d23c5665b1481ccb3d20f27b56Dimitry Ivanov property_get("ro.debuggable", debuggable, "0"); 517d02829636cfb4d23c5665b1481ccb3d20f27b56Dimitry Ivanov return std::string(debuggable) == "1"; 527d02829636cfb4d23c5665b1481ccb3d20f27b56Dimitry Ivanov} 537d02829636cfb4d23c5665b1481ccb3d20f27b56Dimitry Ivanov 54ac1b1919f8d655f652e77f59cc52813199085cd9Dimitry Ivanovclass LibraryNamespaces { 55ac1b1919f8d655f652e77f59cc52813199085cd9Dimitry Ivanov public: 56426799d77014888315233253e96277bef13158ebDimitry Ivanov LibraryNamespaces() : initialized_(false) { } 57ac1b1919f8d655f652e77f59cc52813199085cd9Dimitry Ivanov 58d047c925af62e1fe28fcd1c1940df4afe18d458aDimitry Ivanov android_namespace_t* Create(JNIEnv* env, 59d047c925af62e1fe28fcd1c1940df4afe18d458aDimitry Ivanov jobject class_loader, 60d047c925af62e1fe28fcd1c1940df4afe18d458aDimitry Ivanov bool is_shared, 61d047c925af62e1fe28fcd1c1940df4afe18d458aDimitry Ivanov jstring java_library_path, 6294ee4e690dc4e529a27561b9e0d1e52a0dddc7d5Dimitry Ivanov jstring java_permitted_path) { 63cf9892b6d1b0138bdf2341aaa0670c43af27cb85Dimitry Ivanov std::string library_path; // empty string by default. 64cf9892b6d1b0138bdf2341aaa0670c43af27cb85Dimitry Ivanov 65cf9892b6d1b0138bdf2341aaa0670c43af27cb85Dimitry Ivanov if (java_library_path != nullptr) { 66cf9892b6d1b0138bdf2341aaa0670c43af27cb85Dimitry Ivanov ScopedUtfChars library_path_utf_chars(env, java_library_path); 67cf9892b6d1b0138bdf2341aaa0670c43af27cb85Dimitry Ivanov library_path = library_path_utf_chars.c_str(); 68cf9892b6d1b0138bdf2341aaa0670c43af27cb85Dimitry Ivanov } 690d6e59407d7b57805a72fe4d3df03c5249ea7ae8Dimitry Ivanov 70f334cbf0e1425633bef96a21b0ce9e30f4c6ffa9Dimitry Ivanov // (http://b/27588281) This is a workaround for apps using custom 71f334cbf0e1425633bef96a21b0ce9e30f4c6ffa9Dimitry Ivanov // classloaders and calling System.load() with an absolute path which 72f334cbf0e1425633bef96a21b0ce9e30f4c6ffa9Dimitry Ivanov // is outside of the classloader library search path. 73f334cbf0e1425633bef96a21b0ce9e30f4c6ffa9Dimitry Ivanov // 74f334cbf0e1425633bef96a21b0ce9e30f4c6ffa9Dimitry Ivanov // This part effectively allows such a classloader to access anything 75f334cbf0e1425633bef96a21b0ce9e30f4c6ffa9Dimitry Ivanov // under /data and /mnt/expand 76f334cbf0e1425633bef96a21b0ce9e30f4c6ffa9Dimitry Ivanov std::string permitted_path = kWhitelistedDirectories; 77f334cbf0e1425633bef96a21b0ce9e30f4c6ffa9Dimitry Ivanov 780d6e59407d7b57805a72fe4d3df03c5249ea7ae8Dimitry Ivanov if (java_permitted_path != nullptr) { 790d6e59407d7b57805a72fe4d3df03c5249ea7ae8Dimitry Ivanov ScopedUtfChars path(env, java_permitted_path); 80d0b1531929d76411d964d4077d441d751e2c01fbDimitry Ivanov if (path.c_str() != nullptr && path.size() > 0) { 81d0b1531929d76411d964d4077d441d751e2c01fbDimitry Ivanov permitted_path = permitted_path + ":" + path.c_str(); 82d0b1531929d76411d964d4077d441d751e2c01fbDimitry Ivanov } 830d6e59407d7b57805a72fe4d3df03c5249ea7ae8Dimitry Ivanov } 84ac1b1919f8d655f652e77f59cc52813199085cd9Dimitry Ivanov 8594ee4e690dc4e529a27561b9e0d1e52a0dddc7d5Dimitry Ivanov if (!initialized_ && !InitPublicNamespace(library_path.c_str())) { 86ac1b1919f8d655f652e77f59cc52813199085cd9Dimitry Ivanov return nullptr; 87ac1b1919f8d655f652e77f59cc52813199085cd9Dimitry Ivanov } 88ac1b1919f8d655f652e77f59cc52813199085cd9Dimitry Ivanov 89c914ebd4841ba9e5b8f446dfc5b6dfeecb56531fDimitry Ivanov android_namespace_t* ns = FindNamespaceByClassLoader(env, class_loader); 90ac1b1919f8d655f652e77f59cc52813199085cd9Dimitry Ivanov 914b0e963872715775a63f36b385150cba4801b1d0Dimitry Ivanov LOG_ALWAYS_FATAL_IF(ns != nullptr, 924b0e963872715775a63f36b385150cba4801b1d0Dimitry Ivanov "There is already a namespace associated with this classloader"); 93ac1b1919f8d655f652e77f59cc52813199085cd9Dimitry Ivanov 94d2a6220001c9693664f8cf514f0653a4e1b859eaDimitry Ivanov uint64_t namespace_type = ANDROID_NAMESPACE_TYPE_ISOLATED; 95d2a6220001c9693664f8cf514f0653a4e1b859eaDimitry Ivanov if (is_shared) { 96d2a6220001c9693664f8cf514f0653a4e1b859eaDimitry Ivanov namespace_type |= ANDROID_NAMESPACE_TYPE_SHARED; 97d2a6220001c9693664f8cf514f0653a4e1b859eaDimitry Ivanov } 98d2a6220001c9693664f8cf514f0653a4e1b859eaDimitry Ivanov 99ade364b4566212a0c16920443a84aa85ac31f781Dimitry Ivanov android_namespace_t* parent_ns = FindParentNamespaceByClassLoader(env, class_loader); 100ade364b4566212a0c16920443a84aa85ac31f781Dimitry Ivanov 101c914ebd4841ba9e5b8f446dfc5b6dfeecb56531fDimitry Ivanov ns = android_create_namespace("classloader-namespace", 102c914ebd4841ba9e5b8f446dfc5b6dfeecb56531fDimitry Ivanov nullptr, 103c914ebd4841ba9e5b8f446dfc5b6dfeecb56531fDimitry Ivanov library_path.c_str(), 104c914ebd4841ba9e5b8f446dfc5b6dfeecb56531fDimitry Ivanov namespace_type, 105ade364b4566212a0c16920443a84aa85ac31f781Dimitry Ivanov permitted_path.c_str(), 106ade364b4566212a0c16920443a84aa85ac31f781Dimitry Ivanov parent_ns); 107ac1b1919f8d655f652e77f59cc52813199085cd9Dimitry Ivanov 108d047c925af62e1fe28fcd1c1940df4afe18d458aDimitry Ivanov if (ns != nullptr) { 109d047c925af62e1fe28fcd1c1940df4afe18d458aDimitry Ivanov namespaces_.push_back(std::make_pair(env->NewWeakGlobalRef(class_loader), ns)); 110d047c925af62e1fe28fcd1c1940df4afe18d458aDimitry Ivanov } 111ac1b1919f8d655f652e77f59cc52813199085cd9Dimitry Ivanov 112ac1b1919f8d655f652e77f59cc52813199085cd9Dimitry Ivanov return ns; 113ac1b1919f8d655f652e77f59cc52813199085cd9Dimitry Ivanov } 114ac1b1919f8d655f652e77f59cc52813199085cd9Dimitry Ivanov 115f44ecde58832372ca0edf053eeee44ad56f69944Dimitry Ivanov android_namespace_t* FindNamespaceByClassLoader(JNIEnv* env, jobject class_loader) { 116f44ecde58832372ca0edf053eeee44ad56f69944Dimitry Ivanov auto it = std::find_if(namespaces_.begin(), namespaces_.end(), 117f44ecde58832372ca0edf053eeee44ad56f69944Dimitry Ivanov [&](const std::pair<jweak, android_namespace_t*>& value) { 118f44ecde58832372ca0edf053eeee44ad56f69944Dimitry Ivanov return env->IsSameObject(value.first, class_loader); 119f44ecde58832372ca0edf053eeee44ad56f69944Dimitry Ivanov }); 120f44ecde58832372ca0edf053eeee44ad56f69944Dimitry Ivanov return it != namespaces_.end() ? it->second : nullptr; 121f44ecde58832372ca0edf053eeee44ad56f69944Dimitry Ivanov } 122f44ecde58832372ca0edf053eeee44ad56f69944Dimitry Ivanov 1234b0e963872715775a63f36b385150cba4801b1d0Dimitry Ivanov void Initialize() { 124b614045894c99fb1d35179d3e0a0bab0d5dec666Dimitry Ivanov std::vector<std::string> sonames; 1250b5651e1588c2fee6a587314993a166a77068974Dimitry Ivanov const char* android_root_env = getenv("ANDROID_ROOT"); 1260b5651e1588c2fee6a587314993a166a77068974Dimitry Ivanov std::string root_dir = android_root_env != nullptr ? android_root_env : "/system"; 1270b5651e1588c2fee6a587314993a166a77068974Dimitry Ivanov std::string public_native_libraries_system_config = 1280b5651e1588c2fee6a587314993a166a77068974Dimitry Ivanov root_dir + kPublicNativeLibrariesSystemConfigPathFromRoot; 129b614045894c99fb1d35179d3e0a0bab0d5dec666Dimitry Ivanov 1300b5651e1588c2fee6a587314993a166a77068974Dimitry Ivanov LOG_ALWAYS_FATAL_IF(!ReadConfig(public_native_libraries_system_config, &sonames), 131b614045894c99fb1d35179d3e0a0bab0d5dec666Dimitry Ivanov "Error reading public native library list from \"%s\": %s", 1320b5651e1588c2fee6a587314993a166a77068974Dimitry Ivanov public_native_libraries_system_config.c_str(), strerror(errno)); 1337d02829636cfb4d23c5665b1481ccb3d20f27b56Dimitry Ivanov 1347d02829636cfb4d23c5665b1481ccb3d20f27b56Dimitry Ivanov // For debuggable platform builds use ANDROID_ADDITIONAL_PUBLIC_LIBRARIES environment 1357d02829636cfb4d23c5665b1481ccb3d20f27b56Dimitry Ivanov // variable to add libraries to the list. This is intended for platform tests only. 1367d02829636cfb4d23c5665b1481ccb3d20f27b56Dimitry Ivanov if (is_debuggable()) { 1377d02829636cfb4d23c5665b1481ccb3d20f27b56Dimitry Ivanov const char* additional_libs = getenv("ANDROID_ADDITIONAL_PUBLIC_LIBRARIES"); 1387d02829636cfb4d23c5665b1481ccb3d20f27b56Dimitry Ivanov if (additional_libs != nullptr && additional_libs[0] != '\0') { 1397d02829636cfb4d23c5665b1481ccb3d20f27b56Dimitry Ivanov std::vector<std::string> additional_libs_vector = base::Split(additional_libs, ":"); 1407d02829636cfb4d23c5665b1481ccb3d20f27b56Dimitry Ivanov std::copy(additional_libs_vector.begin(), 1417d02829636cfb4d23c5665b1481ccb3d20f27b56Dimitry Ivanov additional_libs_vector.end(), 1427d02829636cfb4d23c5665b1481ccb3d20f27b56Dimitry Ivanov std::back_inserter(sonames)); 1437d02829636cfb4d23c5665b1481ccb3d20f27b56Dimitry Ivanov } 1447d02829636cfb4d23c5665b1481ccb3d20f27b56Dimitry Ivanov } 1457d02829636cfb4d23c5665b1481ccb3d20f27b56Dimitry Ivanov 146b614045894c99fb1d35179d3e0a0bab0d5dec666Dimitry Ivanov // This file is optional, quietly ignore if the file does not exist. 147b614045894c99fb1d35179d3e0a0bab0d5dec666Dimitry Ivanov ReadConfig(kPublicNativeLibrariesVendorConfig, &sonames); 148b614045894c99fb1d35179d3e0a0bab0d5dec666Dimitry Ivanov 149b614045894c99fb1d35179d3e0a0bab0d5dec666Dimitry Ivanov // android_init_namespaces() expects all the public libraries 150b614045894c99fb1d35179d3e0a0bab0d5dec666Dimitry Ivanov // to be loaded so that they can be found by soname alone. 151b614045894c99fb1d35179d3e0a0bab0d5dec666Dimitry Ivanov // 152b614045894c99fb1d35179d3e0a0bab0d5dec666Dimitry Ivanov // TODO(dimitry): this is a bit misleading since we do not know 153b614045894c99fb1d35179d3e0a0bab0d5dec666Dimitry Ivanov // if the vendor public library is going to be opened from /vendor/lib 154b614045894c99fb1d35179d3e0a0bab0d5dec666Dimitry Ivanov // we might as well end up loading them from /system/lib 155b614045894c99fb1d35179d3e0a0bab0d5dec666Dimitry Ivanov // For now we rely on CTS test to catch things like this but 156b614045894c99fb1d35179d3e0a0bab0d5dec666Dimitry Ivanov // it should probably be addressed in the future. 157b614045894c99fb1d35179d3e0a0bab0d5dec666Dimitry Ivanov for (const auto& soname : sonames) { 158b614045894c99fb1d35179d3e0a0bab0d5dec666Dimitry Ivanov dlopen(soname.c_str(), RTLD_NOW | RTLD_NODELETE); 159b614045894c99fb1d35179d3e0a0bab0d5dec666Dimitry Ivanov } 160b614045894c99fb1d35179d3e0a0bab0d5dec666Dimitry Ivanov 161b614045894c99fb1d35179d3e0a0bab0d5dec666Dimitry Ivanov public_libraries_ = base::Join(sonames, ':'); 162b614045894c99fb1d35179d3e0a0bab0d5dec666Dimitry Ivanov } 163b614045894c99fb1d35179d3e0a0bab0d5dec666Dimitry Ivanov 164be4ca3afc0636b314d676480156eb6977739dd00Dimitry Ivanov void Reset() { 165be4ca3afc0636b314d676480156eb6977739dd00Dimitry Ivanov namespaces_.clear(); 166be4ca3afc0636b314d676480156eb6977739dd00Dimitry Ivanov } 167be4ca3afc0636b314d676480156eb6977739dd00Dimitry Ivanov 168b614045894c99fb1d35179d3e0a0bab0d5dec666Dimitry Ivanov private: 169b614045894c99fb1d35179d3e0a0bab0d5dec666Dimitry Ivanov bool ReadConfig(const std::string& configFile, std::vector<std::string>* sonames) { 1704b0e963872715775a63f36b385150cba4801b1d0Dimitry Ivanov // Read list of public native libraries from the config file. 1714b0e963872715775a63f36b385150cba4801b1d0Dimitry Ivanov std::string file_content; 172b614045894c99fb1d35179d3e0a0bab0d5dec666Dimitry Ivanov if(!base::ReadFileToString(configFile, &file_content)) { 173b614045894c99fb1d35179d3e0a0bab0d5dec666Dimitry Ivanov return false; 174b614045894c99fb1d35179d3e0a0bab0d5dec666Dimitry Ivanov } 1754b0e963872715775a63f36b385150cba4801b1d0Dimitry Ivanov 1764b0e963872715775a63f36b385150cba4801b1d0Dimitry Ivanov std::vector<std::string> lines = base::Split(file_content, "\n"); 1774b0e963872715775a63f36b385150cba4801b1d0Dimitry Ivanov 1784b0e963872715775a63f36b385150cba4801b1d0Dimitry Ivanov for (const auto& line : lines) { 1794b0e963872715775a63f36b385150cba4801b1d0Dimitry Ivanov auto trimmed_line = base::Trim(line); 1804b0e963872715775a63f36b385150cba4801b1d0Dimitry Ivanov if (trimmed_line[0] == '#' || trimmed_line.empty()) { 1814b0e963872715775a63f36b385150cba4801b1d0Dimitry Ivanov continue; 1824b0e963872715775a63f36b385150cba4801b1d0Dimitry Ivanov } 1834b0e963872715775a63f36b385150cba4801b1d0Dimitry Ivanov 184b614045894c99fb1d35179d3e0a0bab0d5dec666Dimitry Ivanov sonames->push_back(trimmed_line); 1854b0e963872715775a63f36b385150cba4801b1d0Dimitry Ivanov } 1864b0e963872715775a63f36b385150cba4801b1d0Dimitry Ivanov 187b614045894c99fb1d35179d3e0a0bab0d5dec666Dimitry Ivanov return true; 188d68c8e9f84557484ef3e8a3ee03398d22f109fa8Dimitry Ivanov } 189ac1b1919f8d655f652e77f59cc52813199085cd9Dimitry Ivanov 19094ee4e690dc4e529a27561b9e0d1e52a0dddc7d5Dimitry Ivanov bool InitPublicNamespace(const char* library_path) { 1914b0e963872715775a63f36b385150cba4801b1d0Dimitry Ivanov // (http://b/25844435) - Some apps call dlopen from generated code (mono jited 1924b0e963872715775a63f36b385150cba4801b1d0Dimitry Ivanov // code is one example) unknown to linker in which case linker uses anonymous 1934b0e963872715775a63f36b385150cba4801b1d0Dimitry Ivanov // namespace. The second argument specifies the search path for the anonymous 1944b0e963872715775a63f36b385150cba4801b1d0Dimitry Ivanov // namespace which is the library_path of the classloader. 19594ee4e690dc4e529a27561b9e0d1e52a0dddc7d5Dimitry Ivanov initialized_ = android_init_namespaces(public_libraries_.c_str(), library_path); 196ac1b1919f8d655f652e77f59cc52813199085cd9Dimitry Ivanov 197ac1b1919f8d655f652e77f59cc52813199085cd9Dimitry Ivanov return initialized_; 198ac1b1919f8d655f652e77f59cc52813199085cd9Dimitry Ivanov } 199ade364b4566212a0c16920443a84aa85ac31f781Dimitry Ivanov 200ade364b4566212a0c16920443a84aa85ac31f781Dimitry Ivanov jobject GetParentClassLoader(JNIEnv* env, jobject class_loader) { 201ade364b4566212a0c16920443a84aa85ac31f781Dimitry Ivanov jclass class_loader_class = env->FindClass("java/lang/ClassLoader"); 202ade364b4566212a0c16920443a84aa85ac31f781Dimitry Ivanov jmethodID get_parent = env->GetMethodID(class_loader_class, 203ade364b4566212a0c16920443a84aa85ac31f781Dimitry Ivanov "getParent", 204ade364b4566212a0c16920443a84aa85ac31f781Dimitry Ivanov "()Ljava/lang/ClassLoader;"); 205ade364b4566212a0c16920443a84aa85ac31f781Dimitry Ivanov 206ade364b4566212a0c16920443a84aa85ac31f781Dimitry Ivanov return env->CallObjectMethod(class_loader, get_parent); 207ade364b4566212a0c16920443a84aa85ac31f781Dimitry Ivanov } 208ade364b4566212a0c16920443a84aa85ac31f781Dimitry Ivanov 209ade364b4566212a0c16920443a84aa85ac31f781Dimitry Ivanov android_namespace_t* FindParentNamespaceByClassLoader(JNIEnv* env, jobject class_loader) { 210ade364b4566212a0c16920443a84aa85ac31f781Dimitry Ivanov jobject parent_class_loader = GetParentClassLoader(env, class_loader); 211ade364b4566212a0c16920443a84aa85ac31f781Dimitry Ivanov 212ade364b4566212a0c16920443a84aa85ac31f781Dimitry Ivanov while (parent_class_loader != nullptr) { 213ade364b4566212a0c16920443a84aa85ac31f781Dimitry Ivanov android_namespace_t* ns = FindNamespaceByClassLoader(env, parent_class_loader); 214ade364b4566212a0c16920443a84aa85ac31f781Dimitry Ivanov if (ns != nullptr) { 215ade364b4566212a0c16920443a84aa85ac31f781Dimitry Ivanov return ns; 216ade364b4566212a0c16920443a84aa85ac31f781Dimitry Ivanov } 217ade364b4566212a0c16920443a84aa85ac31f781Dimitry Ivanov 218ade364b4566212a0c16920443a84aa85ac31f781Dimitry Ivanov parent_class_loader = GetParentClassLoader(env, parent_class_loader); 219ade364b4566212a0c16920443a84aa85ac31f781Dimitry Ivanov } 220ade364b4566212a0c16920443a84aa85ac31f781Dimitry Ivanov return nullptr; 221ade364b4566212a0c16920443a84aa85ac31f781Dimitry Ivanov } 222ac1b1919f8d655f652e77f59cc52813199085cd9Dimitry Ivanov 223ac1b1919f8d655f652e77f59cc52813199085cd9Dimitry Ivanov bool initialized_; 224ac1b1919f8d655f652e77f59cc52813199085cd9Dimitry Ivanov std::vector<std::pair<jweak, android_namespace_t*>> namespaces_; 2254b0e963872715775a63f36b385150cba4801b1d0Dimitry Ivanov std::string public_libraries_; 2264b0e963872715775a63f36b385150cba4801b1d0Dimitry Ivanov 227ac1b1919f8d655f652e77f59cc52813199085cd9Dimitry Ivanov 228ac1b1919f8d655f652e77f59cc52813199085cd9Dimitry Ivanov DISALLOW_COPY_AND_ASSIGN(LibraryNamespaces); 229ac1b1919f8d655f652e77f59cc52813199085cd9Dimitry Ivanov}; 230ac1b1919f8d655f652e77f59cc52813199085cd9Dimitry Ivanov 23134d5a20c8bb57adae8711c7f9d90a77fbd4043c7Dimitry Ivanovstatic std::mutex g_namespaces_mutex; 232ac1b1919f8d655f652e77f59cc52813199085cd9Dimitry Ivanovstatic LibraryNamespaces* g_namespaces = new LibraryNamespaces; 233ac1b1919f8d655f652e77f59cc52813199085cd9Dimitry Ivanov#endif 234ac1b1919f8d655f652e77f59cc52813199085cd9Dimitry Ivanov 2354b0e963872715775a63f36b385150cba4801b1d0Dimitry Ivanovvoid InitializeNativeLoader() { 236426799d77014888315233253e96277bef13158ebDimitry Ivanov#if defined(__ANDROID__) 23734d5a20c8bb57adae8711c7f9d90a77fbd4043c7Dimitry Ivanov std::lock_guard<std::mutex> guard(g_namespaces_mutex); 2384b0e963872715775a63f36b385150cba4801b1d0Dimitry Ivanov g_namespaces->Initialize(); 239426799d77014888315233253e96277bef13158ebDimitry Ivanov#endif 240426799d77014888315233253e96277bef13158ebDimitry Ivanov} 241426799d77014888315233253e96277bef13158ebDimitry Ivanov 242be4ca3afc0636b314d676480156eb6977739dd00Dimitry Ivanovvoid ResetNativeLoader() { 243be4ca3afc0636b314d676480156eb6977739dd00Dimitry Ivanov#if defined(__ANDROID__) 244be4ca3afc0636b314d676480156eb6977739dd00Dimitry Ivanov std::lock_guard<std::mutex> guard(g_namespaces_mutex); 245be4ca3afc0636b314d676480156eb6977739dd00Dimitry Ivanov g_namespaces->Reset(); 246be4ca3afc0636b314d676480156eb6977739dd00Dimitry Ivanov#endif 247be4ca3afc0636b314d676480156eb6977739dd00Dimitry Ivanov} 248ac1b1919f8d655f652e77f59cc52813199085cd9Dimitry Ivanov 249d047c925af62e1fe28fcd1c1940df4afe18d458aDimitry Ivanovjstring CreateClassLoaderNamespace(JNIEnv* env, 250d047c925af62e1fe28fcd1c1940df4afe18d458aDimitry Ivanov int32_t target_sdk_version, 251d047c925af62e1fe28fcd1c1940df4afe18d458aDimitry Ivanov jobject class_loader, 252d047c925af62e1fe28fcd1c1940df4afe18d458aDimitry Ivanov bool is_shared, 253d047c925af62e1fe28fcd1c1940df4afe18d458aDimitry Ivanov jstring library_path, 254d047c925af62e1fe28fcd1c1940df4afe18d458aDimitry Ivanov jstring permitted_path) { 255ac1b1919f8d655f652e77f59cc52813199085cd9Dimitry Ivanov#if defined(__ANDROID__) 2565539db0b4f915bf15de742d1378904a553dc80dcDimitry Ivanov UNUSED(target_sdk_version); 25734d5a20c8bb57adae8711c7f9d90a77fbd4043c7Dimitry Ivanov std::lock_guard<std::mutex> guard(g_namespaces_mutex); 258d047c925af62e1fe28fcd1c1940df4afe18d458aDimitry Ivanov android_namespace_t* ns = g_namespaces->Create(env, 259d047c925af62e1fe28fcd1c1940df4afe18d458aDimitry Ivanov class_loader, 260d047c925af62e1fe28fcd1c1940df4afe18d458aDimitry Ivanov is_shared, 261d047c925af62e1fe28fcd1c1940df4afe18d458aDimitry Ivanov library_path, 26294ee4e690dc4e529a27561b9e0d1e52a0dddc7d5Dimitry Ivanov permitted_path); 263d047c925af62e1fe28fcd1c1940df4afe18d458aDimitry Ivanov if (ns == nullptr) { 264d047c925af62e1fe28fcd1c1940df4afe18d458aDimitry Ivanov return env->NewStringUTF(dlerror()); 265d047c925af62e1fe28fcd1c1940df4afe18d458aDimitry Ivanov } 266d047c925af62e1fe28fcd1c1940df4afe18d458aDimitry Ivanov#else 267d047c925af62e1fe28fcd1c1940df4afe18d458aDimitry Ivanov UNUSED(env, target_sdk_version, class_loader, is_shared, 268d047c925af62e1fe28fcd1c1940df4afe18d458aDimitry Ivanov library_path, permitted_path); 269d047c925af62e1fe28fcd1c1940df4afe18d458aDimitry Ivanov#endif 270d047c925af62e1fe28fcd1c1940df4afe18d458aDimitry Ivanov return nullptr; 271d047c925af62e1fe28fcd1c1940df4afe18d458aDimitry Ivanov} 272d047c925af62e1fe28fcd1c1940df4afe18d458aDimitry Ivanov 273d047c925af62e1fe28fcd1c1940df4afe18d458aDimitry Ivanovvoid* OpenNativeLibrary(JNIEnv* env, 274d047c925af62e1fe28fcd1c1940df4afe18d458aDimitry Ivanov int32_t target_sdk_version, 275d047c925af62e1fe28fcd1c1940df4afe18d458aDimitry Ivanov const char* path, 276d047c925af62e1fe28fcd1c1940df4afe18d458aDimitry Ivanov jobject class_loader, 277d047c925af62e1fe28fcd1c1940df4afe18d458aDimitry Ivanov jstring library_path) { 278d047c925af62e1fe28fcd1c1940df4afe18d458aDimitry Ivanov#if defined(__ANDROID__) 2795539db0b4f915bf15de742d1378904a553dc80dcDimitry Ivanov UNUSED(target_sdk_version); 2805539db0b4f915bf15de742d1378904a553dc80dcDimitry Ivanov if (class_loader == nullptr) { 281ac1b1919f8d655f652e77f59cc52813199085cd9Dimitry Ivanov return dlopen(path, RTLD_NOW); 282ac1b1919f8d655f652e77f59cc52813199085cd9Dimitry Ivanov } 283ac1b1919f8d655f652e77f59cc52813199085cd9Dimitry Ivanov 28434d5a20c8bb57adae8711c7f9d90a77fbd4043c7Dimitry Ivanov std::lock_guard<std::mutex> guard(g_namespaces_mutex); 285d047c925af62e1fe28fcd1c1940df4afe18d458aDimitry Ivanov android_namespace_t* ns = g_namespaces->FindNamespaceByClassLoader(env, class_loader); 286ac1b1919f8d655f652e77f59cc52813199085cd9Dimitry Ivanov 287ac1b1919f8d655f652e77f59cc52813199085cd9Dimitry Ivanov if (ns == nullptr) { 288d047c925af62e1fe28fcd1c1940df4afe18d458aDimitry Ivanov // This is the case where the classloader was not created by ApplicationLoaders 289d047c925af62e1fe28fcd1c1940df4afe18d458aDimitry Ivanov // In this case we create an isolated not-shared namespace for it. 29094ee4e690dc4e529a27561b9e0d1e52a0dddc7d5Dimitry Ivanov ns = g_namespaces->Create(env, class_loader, false, library_path, nullptr); 291d047c925af62e1fe28fcd1c1940df4afe18d458aDimitry Ivanov if (ns == nullptr) { 292d047c925af62e1fe28fcd1c1940df4afe18d458aDimitry Ivanov return nullptr; 293d047c925af62e1fe28fcd1c1940df4afe18d458aDimitry Ivanov } 294ac1b1919f8d655f652e77f59cc52813199085cd9Dimitry Ivanov } 295ac1b1919f8d655f652e77f59cc52813199085cd9Dimitry Ivanov 296ac1b1919f8d655f652e77f59cc52813199085cd9Dimitry Ivanov android_dlextinfo extinfo; 297ac1b1919f8d655f652e77f59cc52813199085cd9Dimitry Ivanov extinfo.flags = ANDROID_DLEXT_USE_NAMESPACE; 298ac1b1919f8d655f652e77f59cc52813199085cd9Dimitry Ivanov extinfo.library_namespace = ns; 299ac1b1919f8d655f652e77f59cc52813199085cd9Dimitry Ivanov 300ac1b1919f8d655f652e77f59cc52813199085cd9Dimitry Ivanov return android_dlopen_ext(path, RTLD_NOW, &extinfo); 301ac1b1919f8d655f652e77f59cc52813199085cd9Dimitry Ivanov#else 302d047c925af62e1fe28fcd1c1940df4afe18d458aDimitry Ivanov UNUSED(env, target_sdk_version, class_loader, library_path); 303ac1b1919f8d655f652e77f59cc52813199085cd9Dimitry Ivanov return dlopen(path, RTLD_NOW); 304ac1b1919f8d655f652e77f59cc52813199085cd9Dimitry Ivanov#endif 305ac1b1919f8d655f652e77f59cc52813199085cd9Dimitry Ivanov} 306ac1b1919f8d655f652e77f59cc52813199085cd9Dimitry Ivanov 30709a516bf161c5cabdaa3a67df5aa7fbac667f5f9Dimitry Ivanovbool CloseNativeLibrary(void* handle) { 30809a516bf161c5cabdaa3a67df5aa7fbac667f5f9Dimitry Ivanov return dlclose(handle) == 0; 30909a516bf161c5cabdaa3a67df5aa7fbac667f5f9Dimitry Ivanov} 31009a516bf161c5cabdaa3a67df5aa7fbac667f5f9Dimitry Ivanov 311f44ecde58832372ca0edf053eeee44ad56f69944Dimitry Ivanov#if defined(__ANDROID__) 312f44ecde58832372ca0edf053eeee44ad56f69944Dimitry Ivanovandroid_namespace_t* FindNamespaceByClassLoader(JNIEnv* env, jobject class_loader) { 31334d5a20c8bb57adae8711c7f9d90a77fbd4043c7Dimitry Ivanov std::lock_guard<std::mutex> guard(g_namespaces_mutex); 314f44ecde58832372ca0edf053eeee44ad56f69944Dimitry Ivanov return g_namespaces->FindNamespaceByClassLoader(env, class_loader); 315f44ecde58832372ca0edf053eeee44ad56f69944Dimitry Ivanov} 316f44ecde58832372ca0edf053eeee44ad56f69944Dimitry Ivanov#endif 317f44ecde58832372ca0edf053eeee44ad56f69944Dimitry Ivanov 318ac1b1919f8d655f652e77f59cc52813199085cd9Dimitry Ivanov}; // android namespace 319