blob: 8020b41875fc88daeda6f090366272456a98829e [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:"
40 "libdl.so:"
41 "libEGL.so:"
42 "libGLESv1_CM.so:"
43 "libGLESv2.so:"
44 "libGLESv3.so:"
45 "libjnigraphics.so:"
46 "liblog.so:"
47 "libmediandk.so:"
48 "libm.so:"
49 "libOpenMAXAL.so:"
50 "libOpenSLES.so:"
51 "libstdc++.so:"
52 "libz.so";
53
54class LibraryNamespaces {
55 public:
56 LibraryNamespaces() : initialized_(false) { }
57
Dimitry Ivanov0d6e5942015-12-08 11:16:56 -080058 android_namespace_t* GetOrCreate(JNIEnv* env, jobject class_loader,
Dimitry Ivanovd2a62202015-12-15 11:06:57 -080059 bool is_shared,
Dimitry Ivanov0d6e5942015-12-08 11:16:56 -080060 jstring java_library_path,
61 jstring java_permitted_path) {
62 ScopedUtfChars library_path(env, java_library_path);
Dimitry Ivanovac1b1912015-12-01 13:56:44 -080063
Dimitry Ivanov0d6e5942015-12-08 11:16:56 -080064 std::string permitted_path;
65 if (java_permitted_path != nullptr) {
66 ScopedUtfChars path(env, java_permitted_path);
67 permitted_path = path.c_str();
68 }
69
70 if (!initialized_ && !InitPublicNamespace(library_path.c_str())) {
Dimitry Ivanovac1b1912015-12-01 13:56:44 -080071 return nullptr;
72 }
73
74 std::lock_guard<std::mutex> guard(mutex_);
75
76 auto it = FindNamespaceByClassLoader(env, class_loader);
77
78 if (it != namespaces_.end()) {
79 return it->second;
80 }
81
Dimitry Ivanovd2a62202015-12-15 11:06:57 -080082 uint64_t namespace_type = ANDROID_NAMESPACE_TYPE_ISOLATED;
83 if (is_shared) {
84 namespace_type |= ANDROID_NAMESPACE_TYPE_SHARED;
85 }
86
Dimitry Ivanovac1b1912015-12-01 13:56:44 -080087 android_namespace_t* ns =
88 android_create_namespace("classloader-namespace",
89 nullptr,
Dimitry Ivanov0d6e5942015-12-08 11:16:56 -080090 library_path.c_str(),
Dimitry Ivanovd2a62202015-12-15 11:06:57 -080091 namespace_type,
Dimitry Ivanov0d6e5942015-12-08 11:16:56 -080092 java_permitted_path != nullptr ?
93 permitted_path.c_str() :
94 nullptr);
Dimitry Ivanovac1b1912015-12-01 13:56:44 -080095
96 namespaces_.push_back(std::make_pair(env->NewWeakGlobalRef(class_loader), ns));
97
98 return ns;
99 }
100
101 private:
102 bool InitPublicNamespace(const char* library_path) {
103 // Make sure all the public libraries are loaded
104 std::vector<std::string> sonames = android::base::Split(kPublicNativeLibraries, ":");
105 for (const auto& soname : sonames) {
106 if (dlopen(soname.c_str(), RTLD_NOW | RTLD_NODELETE) == nullptr) {
107 return false;
108 }
109 }
110
111 // Some apps call dlopen from generated code unknown to linker in which
112 // case linker uses anonymous namespace. See b/25844435 for details.
113 initialized_ = android_init_namespaces(kPublicNativeLibraries, library_path);
114
115 return initialized_;
116 }
117
118 std::vector<std::pair<jweak, android_namespace_t*>>::const_iterator
119 FindNamespaceByClassLoader(JNIEnv* env, jobject class_loader) {
120 return std::find_if(namespaces_.begin(), namespaces_.end(),
121 [&](const std::pair<jweak, android_namespace_t*>& value) {
122 return env->IsSameObject(value.first, class_loader);
123 });
124 }
125
126 bool initialized_;
127 std::mutex mutex_;
128 std::vector<std::pair<jweak, android_namespace_t*>> namespaces_;
129
130 DISALLOW_COPY_AND_ASSIGN(LibraryNamespaces);
131};
132
133static LibraryNamespaces* g_namespaces = new LibraryNamespaces;
134#endif
135
136
137void* OpenNativeLibrary(JNIEnv* env, int32_t target_sdk_version, const char* path,
Dimitry Ivanovd2a62202015-12-15 11:06:57 -0800138 jobject class_loader, bool is_shared, jstring java_library_path,
Dimitry Ivanov0d6e5942015-12-08 11:16:56 -0800139 jstring java_permitted_path) {
Dimitry Ivanovac1b1912015-12-01 13:56:44 -0800140#if defined(__ANDROID__)
Dimitry Ivanovc18ac7c2015-12-11 18:15:41 -0800141 if (target_sdk_version <= INT_MAX || class_loader == nullptr) {
Dimitry Ivanovac1b1912015-12-01 13:56:44 -0800142 return dlopen(path, RTLD_NOW);
143 }
144
Dimitry Ivanov0d6e5942015-12-08 11:16:56 -0800145 android_namespace_t* ns =
Dimitry Ivanovd2a62202015-12-15 11:06:57 -0800146 g_namespaces->GetOrCreate(env, class_loader, is_shared,
147 java_library_path, java_permitted_path);
Dimitry Ivanovac1b1912015-12-01 13:56:44 -0800148
149 if (ns == nullptr) {
150 return nullptr;
151 }
152
153 android_dlextinfo extinfo;
154 extinfo.flags = ANDROID_DLEXT_USE_NAMESPACE;
155 extinfo.library_namespace = ns;
156
157 return android_dlopen_ext(path, RTLD_NOW, &extinfo);
158#else
Dimitry Ivanovd2a62202015-12-15 11:06:57 -0800159 UNUSED(env, target_sdk_version, class_loader, is_shared,
Dimitry Ivanov0d6e5942015-12-08 11:16:56 -0800160 java_library_path, java_permitted_path);
Dimitry Ivanovac1b1912015-12-01 13:56:44 -0800161 return dlopen(path, RTLD_NOW);
162#endif
163}
164
165}; // android namespace