blob: aaff64c357e76f8b651be8119b431173aef3e2c8 [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:"
Dimitry Ivanov3d5a4342016-01-11 14:53:59 -080039 // TODO (dimitry): This is a workaround for http://b/26436837
40 // will be removed before the release.
41 "libart.so:"
42 // END OF WORKAROUND
Dimitry Ivanovac1b1912015-12-01 13:56:44 -080043 "libc.so:"
Dimitry Ivanov73d2bc02016-01-26 10:47:49 -080044 "libcamera2ndk.so:"
Dimitry Ivanovac1b1912015-12-01 13:56:44 -080045 "libdl.so:"
46 "libEGL.so:"
47 "libGLESv1_CM.so:"
48 "libGLESv2.so:"
49 "libGLESv3.so:"
Dimitry Ivanovc24ca892016-02-02 10:40:08 -080050 "libicui18n.so:"
51 "libicuuc.so:"
Dimitry Ivanovac1b1912015-12-01 13:56:44 -080052 "libjnigraphics.so:"
53 "liblog.so:"
54 "libmediandk.so:"
55 "libm.so:"
56 "libOpenMAXAL.so:"
57 "libOpenSLES.so:"
Dimitry Ivanovcdb6fee2016-01-13 15:19:35 -080058 "libRS.so:"
Dimitry Ivanovac1b1912015-12-01 13:56:44 -080059 "libstdc++.so:"
Dimitry Ivanov90bf68e2016-01-08 12:35:38 -080060 "libwebviewchromium_plat_support.so:"
Dimitry Ivanovac1b1912015-12-01 13:56:44 -080061 "libz.so";
62
63class LibraryNamespaces {
64 public:
Dimitry Ivanovd68c8e92016-02-10 14:09:22 -080065 LibraryNamespaces() : initialized_(false) {
66 PreloadPublicLibraries();
67 }
Dimitry Ivanovac1b1912015-12-01 13:56:44 -080068
Dimitry Ivanov0d6e5942015-12-08 11:16:56 -080069 android_namespace_t* GetOrCreate(JNIEnv* env, jobject class_loader,
Dimitry Ivanovd2a62202015-12-15 11:06:57 -080070 bool is_shared,
Dimitry Ivanov0d6e5942015-12-08 11:16:56 -080071 jstring java_library_path,
72 jstring java_permitted_path) {
73 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
81 if (!initialized_ && !InitPublicNamespace(library_path.c_str())) {
Dimitry Ivanovac1b1912015-12-01 13:56:44 -080082 return nullptr;
83 }
84
85 std::lock_guard<std::mutex> guard(mutex_);
86
87 auto it = FindNamespaceByClassLoader(env, class_loader);
88
89 if (it != namespaces_.end()) {
90 return it->second;
91 }
92
Dimitry Ivanovd2a62202015-12-15 11:06:57 -080093 uint64_t namespace_type = ANDROID_NAMESPACE_TYPE_ISOLATED;
94 if (is_shared) {
95 namespace_type |= ANDROID_NAMESPACE_TYPE_SHARED;
96 }
97
Dimitry Ivanovac1b1912015-12-01 13:56:44 -080098 android_namespace_t* ns =
99 android_create_namespace("classloader-namespace",
100 nullptr,
Dimitry Ivanov0d6e5942015-12-08 11:16:56 -0800101 library_path.c_str(),
Dimitry Ivanovd2a62202015-12-15 11:06:57 -0800102 namespace_type,
Dimitry Ivanov0d6e5942015-12-08 11:16:56 -0800103 java_permitted_path != nullptr ?
104 permitted_path.c_str() :
105 nullptr);
Dimitry Ivanovac1b1912015-12-01 13:56:44 -0800106
107 namespaces_.push_back(std::make_pair(env->NewWeakGlobalRef(class_loader), ns));
108
109 return ns;
110 }
111
112 private:
Dimitry Ivanovd68c8e92016-02-10 14:09:22 -0800113 void PreloadPublicLibraries() {
114 // android_init_namespaces() expects all the public libraries
115 // to be loaded so that they can be found by soname alone.
Dimitry Ivanovac1b1912015-12-01 13:56:44 -0800116 std::vector<std::string> sonames = android::base::Split(kPublicNativeLibraries, ":");
117 for (const auto& soname : sonames) {
Dimitry Ivanovd68c8e92016-02-10 14:09:22 -0800118 dlopen(soname.c_str(), RTLD_NOW | RTLD_NODELETE);
Dimitry Ivanovac1b1912015-12-01 13:56:44 -0800119 }
Dimitry Ivanovd68c8e92016-02-10 14:09:22 -0800120 }
Dimitry Ivanovac1b1912015-12-01 13:56:44 -0800121
Dimitry Ivanovd68c8e92016-02-10 14:09:22 -0800122 bool InitPublicNamespace(const char* library_path) {
Dimitry Ivanovac1b1912015-12-01 13:56:44 -0800123 // Some apps call dlopen from generated code unknown to linker in which
124 // case linker uses anonymous namespace. See b/25844435 for details.
125 initialized_ = android_init_namespaces(kPublicNativeLibraries, library_path);
126
127 return initialized_;
128 }
129
130 std::vector<std::pair<jweak, android_namespace_t*>>::const_iterator
131 FindNamespaceByClassLoader(JNIEnv* env, jobject class_loader) {
132 return std::find_if(namespaces_.begin(), namespaces_.end(),
133 [&](const std::pair<jweak, android_namespace_t*>& value) {
134 return env->IsSameObject(value.first, class_loader);
135 });
136 }
137
138 bool initialized_;
139 std::mutex mutex_;
140 std::vector<std::pair<jweak, android_namespace_t*>> namespaces_;
141
142 DISALLOW_COPY_AND_ASSIGN(LibraryNamespaces);
143};
144
145static LibraryNamespaces* g_namespaces = new LibraryNamespaces;
146#endif
147
148
149void* OpenNativeLibrary(JNIEnv* env, int32_t target_sdk_version, const char* path,
Dimitry Ivanovd2a62202015-12-15 11:06:57 -0800150 jobject class_loader, bool is_shared, jstring java_library_path,
Dimitry Ivanov0d6e5942015-12-08 11:16:56 -0800151 jstring java_permitted_path) {
Dimitry Ivanovac1b1912015-12-01 13:56:44 -0800152#if defined(__ANDROID__)
Dimitry Ivanov619ffb42015-12-15 15:55:50 -0800153 if (target_sdk_version == 0 || class_loader == nullptr) {
Dimitry Ivanovac1b1912015-12-01 13:56:44 -0800154 return dlopen(path, RTLD_NOW);
155 }
156
Dimitry Ivanov0d6e5942015-12-08 11:16:56 -0800157 android_namespace_t* ns =
Dimitry Ivanovd2a62202015-12-15 11:06:57 -0800158 g_namespaces->GetOrCreate(env, class_loader, is_shared,
159 java_library_path, java_permitted_path);
Dimitry Ivanovac1b1912015-12-01 13:56:44 -0800160
161 if (ns == nullptr) {
162 return nullptr;
163 }
164
165 android_dlextinfo extinfo;
166 extinfo.flags = ANDROID_DLEXT_USE_NAMESPACE;
167 extinfo.library_namespace = ns;
168
169 return android_dlopen_ext(path, RTLD_NOW, &extinfo);
170#else
Dimitry Ivanovd2a62202015-12-15 11:06:57 -0800171 UNUSED(env, target_sdk_version, class_loader, is_shared,
Dimitry Ivanov0d6e5942015-12-08 11:16:56 -0800172 java_library_path, java_permitted_path);
Dimitry Ivanovac1b1912015-12-01 13:56:44 -0800173 return dlopen(path, RTLD_NOW);
174#endif
175}
176
177}; // android namespace