blob: dfd74d34c9d3c6fe88bfdcad9ce1f00539a68044 [file] [log] [blame]
Dimitry Ivanovac1b1912015-12-01 13:56:44 -08001/*
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 Ivanovd047c922016-02-23 14:23:51 -080024#include "log/log.h"
Dimitry Ivanovac1b1912015-12-01 13:56:44 -080025#endif
26
27#include <algorithm>
28#include <vector>
29#include <string>
30#include <mutex>
31
Elliott Hughes8b047142015-12-08 10:38:59 -080032#include "android-base/macros.h"
33#include "android-base/strings.h"
Dimitry Ivanovac1b1912015-12-01 13:56:44 -080034
35namespace android {
36
37#ifdef __ANDROID__
38// TODO(dimitry): move this to system properties.
39static const char* kPublicNativeLibraries = "libandroid.so:"
40 "libc.so:"
Dimitry Ivanov73d2bc02016-01-26 10:47:49 -080041 "libcamera2ndk.so:"
Dimitry Ivanovac1b1912015-12-01 13:56:44 -080042 "libdl.so:"
43 "libEGL.so:"
44 "libGLESv1_CM.so:"
45 "libGLESv2.so:"
46 "libGLESv3.so:"
Dimitry Ivanovc24ca892016-02-02 10:40:08 -080047 "libicui18n.so:"
48 "libicuuc.so:"
Dimitry Ivanovac1b1912015-12-01 13:56:44 -080049 "libjnigraphics.so:"
50 "liblog.so:"
51 "libmediandk.so:"
52 "libm.so:"
53 "libOpenMAXAL.so:"
54 "libOpenSLES.so:"
Dimitry Ivanovcdb6fee2016-01-13 15:19:35 -080055 "libRS.so:"
Dimitry Ivanovac1b1912015-12-01 13:56:44 -080056 "libstdc++.so:"
Jesse Halldee3d962016-02-23 16:16:47 -080057 "libvulkan.so:"
Prashant Malani75dda752016-03-16 12:59:53 -070058#if !defined(IGNORE_WEBVIEW_CHROMIUM)
Dimitry Ivanov90bf68e2016-01-08 12:35:38 -080059 "libwebviewchromium_plat_support.so:"
Prashant Malani75dda752016-03-16 12:59:53 -070060#endif
Dimitry Ivanovac1b1912015-12-01 13:56:44 -080061 "libz.so";
62
63class LibraryNamespaces {
64 public:
Dimitry Ivanov426799d2016-02-22 11:27:48 -080065 LibraryNamespaces() : initialized_(false) { }
Dimitry Ivanovac1b1912015-12-01 13:56:44 -080066
Dimitry Ivanovd047c922016-02-23 14:23:51 -080067 android_namespace_t* Create(JNIEnv* env,
68 jobject class_loader,
69 bool is_shared,
70 jstring java_library_path,
71 jstring java_permitted_path,
72 int32_t target_sdk_version) {
Dimitry Ivanov0d6e5942015-12-08 11:16:56 -080073 ScopedUtfChars library_path(env, java_library_path);
Dimitry Ivanovac1b1912015-12-01 13:56:44 -080074
Dimitry Ivanov0d6e5942015-12-08 11:16:56 -080075 std::string permitted_path;
76 if (java_permitted_path != nullptr) {
77 ScopedUtfChars path(env, java_permitted_path);
78 permitted_path = path.c_str();
79 }
80
Dimitry Ivanovdab56732016-02-17 17:20:15 -080081 if (!initialized_ && !InitPublicNamespace(library_path.c_str(), target_sdk_version)) {
Dimitry Ivanovac1b1912015-12-01 13:56:44 -080082 return nullptr;
83 }
84
Dimitry Ivanovc914ebd2016-02-22 13:02:35 -080085 android_namespace_t* ns = FindNamespaceByClassLoader(env, class_loader);
Dimitry Ivanovac1b1912015-12-01 13:56:44 -080086
Dimitry Ivanovd047c922016-02-23 14:23:51 -080087 LOG_FATAL_IF(ns != nullptr, "There is already a namespace associated with this classloader");
Dimitry Ivanovac1b1912015-12-01 13:56:44 -080088
Dimitry Ivanovd2a62202015-12-15 11:06:57 -080089 uint64_t namespace_type = ANDROID_NAMESPACE_TYPE_ISOLATED;
90 if (is_shared) {
91 namespace_type |= ANDROID_NAMESPACE_TYPE_SHARED;
92 }
93
Dimitry Ivanovc914ebd2016-02-22 13:02:35 -080094 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);
Dimitry Ivanovac1b1912015-12-01 13:56:44 -0800101
Dimitry Ivanovd047c922016-02-23 14:23:51 -0800102 if (ns != nullptr) {
103 namespaces_.push_back(std::make_pair(env->NewWeakGlobalRef(class_loader), ns));
104 }
Dimitry Ivanovac1b1912015-12-01 13:56:44 -0800105
106 return ns;
107 }
108
Dimitry Ivanovf44ecde2016-02-22 13:48:22 -0800109 android_namespace_t* FindNamespaceByClassLoader(JNIEnv* env, jobject class_loader) {
110 auto it = std::find_if(namespaces_.begin(), namespaces_.end(),
111 [&](const std::pair<jweak, android_namespace_t*>& value) {
112 return env->IsSameObject(value.first, class_loader);
113 });
114 return it != namespaces_.end() ? it->second : nullptr;
115 }
116
Dimitry Ivanovd68c8e92016-02-10 14:09:22 -0800117 void PreloadPublicLibraries() {
118 // android_init_namespaces() expects all the public libraries
119 // to be loaded so that they can be found by soname alone.
Dimitry Ivanovac1b1912015-12-01 13:56:44 -0800120 std::vector<std::string> sonames = android::base::Split(kPublicNativeLibraries, ":");
121 for (const auto& soname : sonames) {
Dimitry Ivanovd68c8e92016-02-10 14:09:22 -0800122 dlopen(soname.c_str(), RTLD_NOW | RTLD_NODELETE);
Dimitry Ivanovac1b1912015-12-01 13:56:44 -0800123 }
Dimitry Ivanovd68c8e92016-02-10 14:09:22 -0800124 }
Dimitry Ivanovac1b1912015-12-01 13:56:44 -0800125
Dimitry Ivanov426799d2016-02-22 11:27:48 -0800126 private:
Dimitry Ivanovdab56732016-02-17 17:20:15 -0800127 bool InitPublicNamespace(const char* library_path, int32_t target_sdk_version) {
Dimitry Ivanovac1b1912015-12-01 13:56:44 -0800128 // Some apps call dlopen from generated code unknown to linker in which
129 // case linker uses anonymous namespace. See b/25844435 for details.
Dimitry Ivanovdab56732016-02-17 17:20:15 -0800130 std::string publicNativeLibraries = kPublicNativeLibraries;
131
132 // TODO (dimitry): This is a workaround for http://b/26436837
133 // will be removed before the release.
134 if (target_sdk_version <= 23) {
Dimitry Ivanov5f28b842016-03-04 11:00:37 -0800135 // check if libart.so is loaded.
136 void* handle = dlopen("libart.so", RTLD_NOW | RTLD_NOLOAD);
137 if (handle != nullptr) {
138 publicNativeLibraries += ":libart.so";
139 dlclose(handle);
140 }
Dimitry Ivanovdab56732016-02-17 17:20:15 -0800141 }
142 // END OF WORKAROUND
143
144 initialized_ = android_init_namespaces(publicNativeLibraries.c_str(), library_path);
Dimitry Ivanovac1b1912015-12-01 13:56:44 -0800145
146 return initialized_;
147 }
148
Dimitry Ivanovac1b1912015-12-01 13:56:44 -0800149 bool initialized_;
Dimitry Ivanovac1b1912015-12-01 13:56:44 -0800150 std::vector<std::pair<jweak, android_namespace_t*>> namespaces_;
151
152 DISALLOW_COPY_AND_ASSIGN(LibraryNamespaces);
153};
154
Dimitry Ivanov34d5a202016-02-29 13:21:43 -0800155static std::mutex g_namespaces_mutex;
Dimitry Ivanovac1b1912015-12-01 13:56:44 -0800156static LibraryNamespaces* g_namespaces = new LibraryNamespaces;
Dimitry Ivanovd047c922016-02-23 14:23:51 -0800157
158static bool namespaces_enabled(uint32_t target_sdk_version) {
159 return target_sdk_version > 0;
160}
Dimitry Ivanovac1b1912015-12-01 13:56:44 -0800161#endif
162
Dimitry Ivanov426799d2016-02-22 11:27:48 -0800163void PreloadPublicNativeLibraries() {
164#if defined(__ANDROID__)
Dimitry Ivanov34d5a202016-02-29 13:21:43 -0800165 std::lock_guard<std::mutex> guard(g_namespaces_mutex);
Dimitry Ivanov426799d2016-02-22 11:27:48 -0800166 g_namespaces->PreloadPublicLibraries();
167#endif
168}
169
Dimitry Ivanovac1b1912015-12-01 13:56:44 -0800170
Dimitry Ivanovd047c922016-02-23 14:23:51 -0800171jstring CreateClassLoaderNamespace(JNIEnv* env,
172 int32_t target_sdk_version,
173 jobject class_loader,
174 bool is_shared,
175 jstring library_path,
176 jstring permitted_path) {
Dimitry Ivanovac1b1912015-12-01 13:56:44 -0800177#if defined(__ANDROID__)
Dimitry Ivanovd047c922016-02-23 14:23:51 -0800178 if (!namespaces_enabled(target_sdk_version)) {
179 return nullptr;
180 }
181
Dimitry Ivanov34d5a202016-02-29 13:21:43 -0800182 std::lock_guard<std::mutex> guard(g_namespaces_mutex);
Dimitry Ivanovd047c922016-02-23 14:23:51 -0800183 android_namespace_t* ns = g_namespaces->Create(env,
184 class_loader,
185 is_shared,
186 library_path,
187 permitted_path,
188 target_sdk_version);
189 if (ns == nullptr) {
190 return env->NewStringUTF(dlerror());
191 }
192#else
193 UNUSED(env, target_sdk_version, class_loader, is_shared,
194 library_path, permitted_path);
195#endif
196 return nullptr;
197}
198
199void* OpenNativeLibrary(JNIEnv* env,
200 int32_t target_sdk_version,
201 const char* path,
202 jobject class_loader,
203 jstring library_path) {
204#if defined(__ANDROID__)
205 if (!namespaces_enabled(target_sdk_version) || class_loader == nullptr) {
Dimitry Ivanovac1b1912015-12-01 13:56:44 -0800206 return dlopen(path, RTLD_NOW);
207 }
208
Dimitry Ivanov34d5a202016-02-29 13:21:43 -0800209 std::lock_guard<std::mutex> guard(g_namespaces_mutex);
Dimitry Ivanovd047c922016-02-23 14:23:51 -0800210 android_namespace_t* ns = g_namespaces->FindNamespaceByClassLoader(env, class_loader);
Dimitry Ivanovac1b1912015-12-01 13:56:44 -0800211
212 if (ns == nullptr) {
Dimitry Ivanovd047c922016-02-23 14:23:51 -0800213 // This is the case where the classloader was not created by ApplicationLoaders
214 // In this case we create an isolated not-shared namespace for it.
215 ns = g_namespaces->Create(env, class_loader, false, library_path, nullptr, target_sdk_version);
216 if (ns == nullptr) {
217 return nullptr;
218 }
Dimitry Ivanovac1b1912015-12-01 13:56:44 -0800219 }
220
221 android_dlextinfo extinfo;
222 extinfo.flags = ANDROID_DLEXT_USE_NAMESPACE;
223 extinfo.library_namespace = ns;
224
225 return android_dlopen_ext(path, RTLD_NOW, &extinfo);
226#else
Dimitry Ivanovd047c922016-02-23 14:23:51 -0800227 UNUSED(env, target_sdk_version, class_loader, library_path);
Dimitry Ivanovac1b1912015-12-01 13:56:44 -0800228 return dlopen(path, RTLD_NOW);
229#endif
230}
231
Dimitry Ivanovf44ecde2016-02-22 13:48:22 -0800232#if defined(__ANDROID__)
233android_namespace_t* FindNamespaceByClassLoader(JNIEnv* env, jobject class_loader) {
Dimitry Ivanov34d5a202016-02-29 13:21:43 -0800234 std::lock_guard<std::mutex> guard(g_namespaces_mutex);
Dimitry Ivanovf44ecde2016-02-22 13:48:22 -0800235 return g_namespaces->FindNamespaceByClassLoader(env, class_loader);
236}
237#endif
238
Dimitry Ivanovac1b1912015-12-01 13:56:44 -0800239}; // android namespace