blob: 899c98c55c3b20482e2304dc5444cac82b671c75 [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 Ivanov4cd07682016-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
Dimitry Ivanovd1fdb982016-03-15 13:51:26 -070032#include "android-base/file.h"
Elliott Hughes8b047142015-12-08 10:38:59 -080033#include "android-base/macros.h"
34#include "android-base/strings.h"
Dimitry Ivanovac1b1912015-12-01 13:56:44 -080035
36namespace android {
37
Dimitry Ivanovd1fdb982016-03-15 13:51:26 -070038#if defined(__ANDROID__)
39static constexpr const char* kPublicNativeLibrariesConfig = "/system/etc/public.libraries.txt";
Dimitry Ivanovac1b1912015-12-01 13:56:44 -080040
41class LibraryNamespaces {
42 public:
Dimitry Ivanov6f800222016-02-22 11:27:48 -080043 LibraryNamespaces() : initialized_(false) { }
Dimitry Ivanovac1b1912015-12-01 13:56:44 -080044
Dimitry Ivanov4cd07682016-02-23 14:23:51 -080045 android_namespace_t* Create(JNIEnv* env,
46 jobject class_loader,
47 bool is_shared,
48 jstring java_library_path,
49 jstring java_permitted_path) {
Dimitry Ivanov0d6e5942015-12-08 11:16:56 -080050 ScopedUtfChars library_path(env, java_library_path);
Dimitry Ivanovac1b1912015-12-01 13:56:44 -080051
Dimitry Ivanov0d6e5942015-12-08 11:16:56 -080052 std::string permitted_path;
53 if (java_permitted_path != nullptr) {
54 ScopedUtfChars path(env, java_permitted_path);
55 permitted_path = path.c_str();
56 }
57
58 if (!initialized_ && !InitPublicNamespace(library_path.c_str())) {
Dimitry Ivanovac1b1912015-12-01 13:56:44 -080059 return nullptr;
60 }
61
Dimitry Ivanov34fa7042016-02-22 13:02:35 -080062 android_namespace_t* ns = FindNamespaceByClassLoader(env, class_loader);
Dimitry Ivanovac1b1912015-12-01 13:56:44 -080063
Dimitry Ivanovd1fdb982016-03-15 13:51:26 -070064 LOG_ALWAYS_FATAL_IF(ns != nullptr,
65 "There is already a namespace associated with this classloader");
Dimitry Ivanovac1b1912015-12-01 13:56:44 -080066
Dimitry Ivanovd2a62202015-12-15 11:06:57 -080067 uint64_t namespace_type = ANDROID_NAMESPACE_TYPE_ISOLATED;
68 if (is_shared) {
69 namespace_type |= ANDROID_NAMESPACE_TYPE_SHARED;
70 }
71
Dimitry Ivanov34fa7042016-02-22 13:02:35 -080072 ns = android_create_namespace("classloader-namespace",
73 nullptr,
74 library_path.c_str(),
75 namespace_type,
76 java_permitted_path != nullptr ?
77 permitted_path.c_str() :
78 nullptr);
Dimitry Ivanovac1b1912015-12-01 13:56:44 -080079
Dimitry Ivanov4cd07682016-02-23 14:23:51 -080080 if (ns != nullptr) {
81 namespaces_.push_back(std::make_pair(env->NewWeakGlobalRef(class_loader), ns));
82 }
Dimitry Ivanovac1b1912015-12-01 13:56:44 -080083
84 return ns;
85 }
86
Dimitry Ivanov0cd10d82016-02-22 13:48:22 -080087 android_namespace_t* FindNamespaceByClassLoader(JNIEnv* env, jobject class_loader) {
88 auto it = std::find_if(namespaces_.begin(), namespaces_.end(),
89 [&](const std::pair<jweak, android_namespace_t*>& value) {
90 return env->IsSameObject(value.first, class_loader);
91 });
92 return it != namespaces_.end() ? it->second : nullptr;
93 }
94
Dimitry Ivanovd1fdb982016-03-15 13:51:26 -070095 void Initialize() {
96 // Read list of public native libraries from the config file.
97 std::string file_content;
98 LOG_ALWAYS_FATAL_IF(!base::ReadFileToString(kPublicNativeLibrariesConfig, &file_content),
99 "Error reading public native library list from \"%s\": %s",
100 kPublicNativeLibrariesConfig, strerror(errno));
101
102 std::vector<std::string> lines = base::Split(file_content, "\n");
103
104 std::vector<std::string> sonames;
105
106 for (const auto& line : lines) {
107 auto trimmed_line = base::Trim(line);
108 if (trimmed_line[0] == '#' || trimmed_line.empty()) {
109 continue;
110 }
111
112 sonames.push_back(trimmed_line);
113 }
114
115 public_libraries_ = base::Join(sonames, ':');
116
Dimitry Ivanov55bbb0d2016-02-10 14:09:22 -0800117 // android_init_namespaces() expects all the public libraries
118 // to be loaded so that they can be found by soname alone.
Dimitry Ivanovac1b1912015-12-01 13:56:44 -0800119 for (const auto& soname : sonames) {
Dimitry Ivanov55bbb0d2016-02-10 14:09:22 -0800120 dlopen(soname.c_str(), RTLD_NOW | RTLD_NODELETE);
Dimitry Ivanovac1b1912015-12-01 13:56:44 -0800121 }
Dimitry Ivanov55bbb0d2016-02-10 14:09:22 -0800122 }
Dimitry Ivanovac1b1912015-12-01 13:56:44 -0800123
Dimitry Ivanov6f800222016-02-22 11:27:48 -0800124 private:
Dimitry Ivanov55bbb0d2016-02-10 14:09:22 -0800125 bool InitPublicNamespace(const char* library_path) {
Dimitry Ivanovd1fdb982016-03-15 13:51:26 -0700126 // (http://b/25844435) - Some apps call dlopen from generated code (mono jited
127 // code is one example) unknown to linker in which case linker uses anonymous
128 // namespace. The second argument specifies the search path for the anonymous
129 // namespace which is the library_path of the classloader.
130 initialized_ = android_init_namespaces(public_libraries_.c_str(), library_path);
Dimitry Ivanovac1b1912015-12-01 13:56:44 -0800131
132 return initialized_;
133 }
134
Dimitry Ivanovac1b1912015-12-01 13:56:44 -0800135 bool initialized_;
Dimitry Ivanovac1b1912015-12-01 13:56:44 -0800136 std::vector<std::pair<jweak, android_namespace_t*>> namespaces_;
Dimitry Ivanovd1fdb982016-03-15 13:51:26 -0700137 std::string public_libraries_;
138
Dimitry Ivanovac1b1912015-12-01 13:56:44 -0800139
140 DISALLOW_COPY_AND_ASSIGN(LibraryNamespaces);
141};
142
Dimitry Ivanov1dff1aa2016-02-29 13:21:43 -0800143static std::mutex g_namespaces_mutex;
Dimitry Ivanovac1b1912015-12-01 13:56:44 -0800144static LibraryNamespaces* g_namespaces = new LibraryNamespaces;
Dimitry Ivanov4cd07682016-02-23 14:23:51 -0800145
146static bool namespaces_enabled(uint32_t target_sdk_version) {
147 return target_sdk_version > 0;
148}
Dimitry Ivanovac1b1912015-12-01 13:56:44 -0800149#endif
150
Dimitry Ivanovd1fdb982016-03-15 13:51:26 -0700151void InitializeNativeLoader() {
Dimitry Ivanov6f800222016-02-22 11:27:48 -0800152#if defined(__ANDROID__)
Dimitry Ivanov1dff1aa2016-02-29 13:21:43 -0800153 std::lock_guard<std::mutex> guard(g_namespaces_mutex);
Dimitry Ivanovd1fdb982016-03-15 13:51:26 -0700154 g_namespaces->Initialize();
Dimitry Ivanov6f800222016-02-22 11:27:48 -0800155#endif
156}
157
Dimitry Ivanovac1b1912015-12-01 13:56:44 -0800158
Dimitry Ivanov4cd07682016-02-23 14:23:51 -0800159jstring CreateClassLoaderNamespace(JNIEnv* env,
160 int32_t target_sdk_version,
161 jobject class_loader,
162 bool is_shared,
163 jstring library_path,
164 jstring permitted_path) {
Dimitry Ivanovac1b1912015-12-01 13:56:44 -0800165#if defined(__ANDROID__)
Dimitry Ivanov4cd07682016-02-23 14:23:51 -0800166 if (!namespaces_enabled(target_sdk_version)) {
167 return nullptr;
168 }
169
Dimitry Ivanov1dff1aa2016-02-29 13:21:43 -0800170 std::lock_guard<std::mutex> guard(g_namespaces_mutex);
Dimitry Ivanov4cd07682016-02-23 14:23:51 -0800171 android_namespace_t* ns = g_namespaces->Create(env,
172 class_loader,
173 is_shared,
174 library_path,
175 permitted_path);
176 if (ns == nullptr) {
177 return env->NewStringUTF(dlerror());
178 }
179#else
180 UNUSED(env, target_sdk_version, class_loader, is_shared,
181 library_path, permitted_path);
182#endif
183 return nullptr;
184}
185
186void* OpenNativeLibrary(JNIEnv* env,
187 int32_t target_sdk_version,
188 const char* path,
189 jobject class_loader,
190 jstring library_path) {
191#if defined(__ANDROID__)
192 if (!namespaces_enabled(target_sdk_version) || class_loader == nullptr) {
Dimitry Ivanovac1b1912015-12-01 13:56:44 -0800193 return dlopen(path, RTLD_NOW);
194 }
195
Dimitry Ivanov1dff1aa2016-02-29 13:21:43 -0800196 std::lock_guard<std::mutex> guard(g_namespaces_mutex);
Dimitry Ivanov4cd07682016-02-23 14:23:51 -0800197 android_namespace_t* ns = g_namespaces->FindNamespaceByClassLoader(env, class_loader);
Dimitry Ivanovac1b1912015-12-01 13:56:44 -0800198
199 if (ns == nullptr) {
Dimitry Ivanov4cd07682016-02-23 14:23:51 -0800200 // This is the case where the classloader was not created by ApplicationLoaders
201 // In this case we create an isolated not-shared namespace for it.
202 ns = g_namespaces->Create(env, class_loader, false, library_path, nullptr);
203 if (ns == nullptr) {
204 return nullptr;
205 }
Dimitry Ivanovac1b1912015-12-01 13:56:44 -0800206 }
207
208 android_dlextinfo extinfo;
209 extinfo.flags = ANDROID_DLEXT_USE_NAMESPACE;
210 extinfo.library_namespace = ns;
211
212 return android_dlopen_ext(path, RTLD_NOW, &extinfo);
213#else
Dimitry Ivanov4cd07682016-02-23 14:23:51 -0800214 UNUSED(env, target_sdk_version, class_loader, library_path);
Dimitry Ivanovac1b1912015-12-01 13:56:44 -0800215 return dlopen(path, RTLD_NOW);
216#endif
217}
218
Dimitry Ivanov0cd10d82016-02-22 13:48:22 -0800219#if defined(__ANDROID__)
220android_namespace_t* FindNamespaceByClassLoader(JNIEnv* env, jobject class_loader) {
Dimitry Ivanov1dff1aa2016-02-29 13:21:43 -0800221 std::lock_guard<std::mutex> guard(g_namespaces_mutex);
Dimitry Ivanov0cd10d82016-02-22 13:48:22 -0800222 return g_namespaces->FindNamespaceByClassLoader(env, class_loader);
223}
224#endif
225
Dimitry Ivanovac1b1912015-12-01 13:56:44 -0800226}; // android namespace