blob: 8b8dee3b2281bd9e19dd383d49c646d277ccca6c [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"
24#endif
25
26#include <algorithm>
27#include <vector>
28#include <string>
29#include <mutex>
30
Elliott Hughes8b047142015-12-08 10:38:59 -080031#include "android-base/macros.h"
32#include "android-base/strings.h"
Dimitry Ivanovac1b1912015-12-01 13:56:44 -080033
34namespace android {
35
36#ifdef __ANDROID__
37// TODO(dimitry): move this to system properties.
38static const char* kPublicNativeLibraries = "libandroid.so:"
39 "libc.so:"
Dimitry Ivanov73d2bc02016-01-26 10:47:49 -080040 "libcamera2ndk.so:"
Dimitry Ivanovac1b1912015-12-01 13:56:44 -080041 "libdl.so:"
42 "libEGL.so:"
43 "libGLESv1_CM.so:"
44 "libGLESv2.so:"
45 "libGLESv3.so:"
Dimitry Ivanovc24ca892016-02-02 10:40:08 -080046 "libicui18n.so:"
47 "libicuuc.so:"
Dimitry Ivanovac1b1912015-12-01 13:56:44 -080048 "libjnigraphics.so:"
49 "liblog.so:"
50 "libmediandk.so:"
51 "libm.so:"
52 "libOpenMAXAL.so:"
53 "libOpenSLES.so:"
Dimitry Ivanovcdb6fee2016-01-13 15:19:35 -080054 "libRS.so:"
Dimitry Ivanovac1b1912015-12-01 13:56:44 -080055 "libstdc++.so:"
Dimitry Ivanov90bf68e2016-01-08 12:35:38 -080056 "libwebviewchromium_plat_support.so:"
Dimitry Ivanovac1b1912015-12-01 13:56:44 -080057 "libz.so";
58
59class LibraryNamespaces {
60 public:
Dimitry Ivanovd68c8e92016-02-10 14:09:22 -080061 LibraryNamespaces() : initialized_(false) {
62 PreloadPublicLibraries();
63 }
Dimitry Ivanovac1b1912015-12-01 13:56:44 -080064
Dimitry Ivanov0d6e5942015-12-08 11:16:56 -080065 android_namespace_t* GetOrCreate(JNIEnv* env, jobject class_loader,
Dimitry Ivanovd2a62202015-12-15 11:06:57 -080066 bool is_shared,
Dimitry Ivanov0d6e5942015-12-08 11:16:56 -080067 jstring java_library_path,
Dimitry Ivanovdab56732016-02-17 17:20:15 -080068 jstring java_permitted_path,
69 int32_t target_sdk_version) {
Dimitry Ivanov0d6e5942015-12-08 11:16:56 -080070 ScopedUtfChars library_path(env, java_library_path);
Dimitry Ivanovac1b1912015-12-01 13:56:44 -080071
Dimitry Ivanov0d6e5942015-12-08 11:16:56 -080072 std::string permitted_path;
73 if (java_permitted_path != nullptr) {
74 ScopedUtfChars path(env, java_permitted_path);
75 permitted_path = path.c_str();
76 }
77
Dimitry Ivanovdab56732016-02-17 17:20:15 -080078 if (!initialized_ && !InitPublicNamespace(library_path.c_str(), target_sdk_version)) {
Dimitry Ivanovac1b1912015-12-01 13:56:44 -080079 return nullptr;
80 }
81
82 std::lock_guard<std::mutex> guard(mutex_);
83
Dimitry Ivanovc914ebd2016-02-22 13:02:35 -080084 android_namespace_t* ns = FindNamespaceByClassLoader(env, class_loader);
Dimitry Ivanovac1b1912015-12-01 13:56:44 -080085
Dimitry Ivanovc914ebd2016-02-22 13:02:35 -080086 if (ns != nullptr) {
87 return ns;
Dimitry Ivanovac1b1912015-12-01 13:56:44 -080088 }
89
Dimitry Ivanovd2a62202015-12-15 11:06:57 -080090 uint64_t namespace_type = ANDROID_NAMESPACE_TYPE_ISOLATED;
91 if (is_shared) {
92 namespace_type |= ANDROID_NAMESPACE_TYPE_SHARED;
93 }
94
Dimitry Ivanovc914ebd2016-02-22 13:02:35 -080095 ns = android_create_namespace("classloader-namespace",
96 nullptr,
97 library_path.c_str(),
98 namespace_type,
99 java_permitted_path != nullptr ?
100 permitted_path.c_str() :
101 nullptr);
Dimitry Ivanovac1b1912015-12-01 13:56:44 -0800102
103 namespaces_.push_back(std::make_pair(env->NewWeakGlobalRef(class_loader), ns));
104
105 return ns;
106 }
107
Dimitry Ivanovf44ecde2016-02-22 13:48:22 -0800108 android_namespace_t* FindNamespaceByClassLoader(JNIEnv* env, jobject class_loader) {
109 auto it = std::find_if(namespaces_.begin(), namespaces_.end(),
110 [&](const std::pair<jweak, android_namespace_t*>& value) {
111 return env->IsSameObject(value.first, class_loader);
112 });
113 return it != namespaces_.end() ? it->second : nullptr;
114 }
115
Dimitry Ivanovac1b1912015-12-01 13:56:44 -0800116 private:
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 Ivanovdab56732016-02-17 17:20:15 -0800126 bool InitPublicNamespace(const char* library_path, int32_t target_sdk_version) {
Dimitry Ivanovac1b1912015-12-01 13:56:44 -0800127 // Some apps call dlopen from generated code unknown to linker in which
128 // case linker uses anonymous namespace. See b/25844435 for details.
Dimitry Ivanovdab56732016-02-17 17:20:15 -0800129 std::string publicNativeLibraries = kPublicNativeLibraries;
130
131 // TODO (dimitry): This is a workaround for http://b/26436837
132 // will be removed before the release.
133 if (target_sdk_version <= 23) {
134 publicNativeLibraries += ":libart.so";
135 }
136 // END OF WORKAROUND
137
138 initialized_ = android_init_namespaces(publicNativeLibraries.c_str(), library_path);
Dimitry Ivanovac1b1912015-12-01 13:56:44 -0800139
140 return initialized_;
141 }
142
Dimitry Ivanovac1b1912015-12-01 13:56:44 -0800143 bool initialized_;
144 std::mutex mutex_;
145 std::vector<std::pair<jweak, android_namespace_t*>> namespaces_;
146
147 DISALLOW_COPY_AND_ASSIGN(LibraryNamespaces);
148};
149
150static LibraryNamespaces* g_namespaces = new LibraryNamespaces;
151#endif
152
153
154void* OpenNativeLibrary(JNIEnv* env, int32_t target_sdk_version, const char* path,
Dimitry Ivanovd2a62202015-12-15 11:06:57 -0800155 jobject class_loader, bool is_shared, jstring java_library_path,
Dimitry Ivanov0d6e5942015-12-08 11:16:56 -0800156 jstring java_permitted_path) {
Dimitry Ivanovac1b1912015-12-01 13:56:44 -0800157#if defined(__ANDROID__)
Dimitry Ivanov619ffb42015-12-15 15:55:50 -0800158 if (target_sdk_version == 0 || class_loader == nullptr) {
Dimitry Ivanovac1b1912015-12-01 13:56:44 -0800159 return dlopen(path, RTLD_NOW);
160 }
161
Dimitry Ivanov0d6e5942015-12-08 11:16:56 -0800162 android_namespace_t* ns =
Dimitry Ivanovd2a62202015-12-15 11:06:57 -0800163 g_namespaces->GetOrCreate(env, class_loader, is_shared,
Dimitry Ivanovdab56732016-02-17 17:20:15 -0800164 java_library_path, java_permitted_path, target_sdk_version);
Dimitry Ivanovac1b1912015-12-01 13:56:44 -0800165
166 if (ns == nullptr) {
167 return nullptr;
168 }
169
170 android_dlextinfo extinfo;
171 extinfo.flags = ANDROID_DLEXT_USE_NAMESPACE;
172 extinfo.library_namespace = ns;
173
174 return android_dlopen_ext(path, RTLD_NOW, &extinfo);
175#else
Dimitry Ivanovd2a62202015-12-15 11:06:57 -0800176 UNUSED(env, target_sdk_version, class_loader, is_shared,
Dimitry Ivanov0d6e5942015-12-08 11:16:56 -0800177 java_library_path, java_permitted_path);
Dimitry Ivanovac1b1912015-12-01 13:56:44 -0800178 return dlopen(path, RTLD_NOW);
179#endif
180}
181
Dimitry Ivanovf44ecde2016-02-22 13:48:22 -0800182#if defined(__ANDROID__)
183android_namespace_t* FindNamespaceByClassLoader(JNIEnv* env, jobject class_loader) {
184 return g_namespaces->FindNamespaceByClassLoader(env, class_loader);
185}
186#endif
187
Dimitry Ivanovac1b1912015-12-01 13:56:44 -0800188}; // android namespace