blob: 9a24714bd8b863f8e50916a2856b40348530e9fe [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__)
Dimitry Ivanov7e8cee82016-04-21 16:42:48 -070039static constexpr const char* kPublicNativeLibrariesSystemConfigPathFromRoot = "/etc/public.libraries.txt";
Dimitry Ivanov617f4952016-04-06 18:24:08 -070040static constexpr const char* kPublicNativeLibrariesVendorConfig = "/vendor/etc/public.libraries.txt";
Dimitry Ivanovac1b1912015-12-01 13:56:44 -080041
Dimitry Ivanov4ddabd02016-05-05 17:30:24 -070042static bool is_debuggable() {
43 char debuggable[PROP_VALUE_MAX];
44 property_get("ro.debuggable", debuggable, "0");
45 return std::string(debuggable) == "1";
46}
47
Dimitry Ivanovac1b1912015-12-01 13:56:44 -080048class LibraryNamespaces {
49 public:
Dimitry Ivanov6f800222016-02-22 11:27:48 -080050 LibraryNamespaces() : initialized_(false) { }
Dimitry Ivanovac1b1912015-12-01 13:56:44 -080051
Dimitry Ivanov4cd07682016-02-23 14:23:51 -080052 android_namespace_t* Create(JNIEnv* env,
53 jobject class_loader,
54 bool is_shared,
55 jstring java_library_path,
56 jstring java_permitted_path) {
Dimitry Ivanov8a0425b2016-05-09 10:55:50 -070057 std::string library_path; // empty string by default.
58
59 if (java_library_path != nullptr) {
60 ScopedUtfChars library_path_utf_chars(env, java_library_path);
61 library_path = library_path_utf_chars.c_str();
62 }
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
Dimitry Ivanov34fa7042016-02-22 13:02:35 -080074 android_namespace_t* ns = FindNamespaceByClassLoader(env, class_loader);
Dimitry Ivanovac1b1912015-12-01 13:56:44 -080075
Dimitry Ivanovd1fdb982016-03-15 13:51:26 -070076 LOG_ALWAYS_FATAL_IF(ns != nullptr,
77 "There is already a namespace associated with this classloader");
Dimitry Ivanovac1b1912015-12-01 13:56:44 -080078
Dimitry Ivanovd2a62202015-12-15 11:06:57 -080079 uint64_t namespace_type = ANDROID_NAMESPACE_TYPE_ISOLATED;
80 if (is_shared) {
81 namespace_type |= ANDROID_NAMESPACE_TYPE_SHARED;
82 }
83
Dimitry Ivanov34fa7042016-02-22 13:02:35 -080084 ns = android_create_namespace("classloader-namespace",
85 nullptr,
86 library_path.c_str(),
87 namespace_type,
88 java_permitted_path != nullptr ?
89 permitted_path.c_str() :
90 nullptr);
Dimitry Ivanovac1b1912015-12-01 13:56:44 -080091
Dimitry Ivanov4cd07682016-02-23 14:23:51 -080092 if (ns != nullptr) {
93 namespaces_.push_back(std::make_pair(env->NewWeakGlobalRef(class_loader), ns));
94 }
Dimitry Ivanovac1b1912015-12-01 13:56:44 -080095
96 return ns;
97 }
98
Dimitry Ivanov0cd10d82016-02-22 13:48:22 -080099 android_namespace_t* FindNamespaceByClassLoader(JNIEnv* env, jobject class_loader) {
100 auto it = std::find_if(namespaces_.begin(), namespaces_.end(),
101 [&](const std::pair<jweak, android_namespace_t*>& value) {
102 return env->IsSameObject(value.first, class_loader);
103 });
104 return it != namespaces_.end() ? it->second : nullptr;
105 }
106
Dimitry Ivanovd1fdb982016-03-15 13:51:26 -0700107 void Initialize() {
Dimitry Ivanov80ddb8f2016-05-09 18:12:00 -0700108 // Once public namespace is initialized there is no
109 // point in running this code - it will have no effect
110 // on the current list of public libraries.
111 if (initialized_) {
112 return;
113 }
114
Dimitry Ivanov617f4952016-04-06 18:24:08 -0700115 std::vector<std::string> sonames;
Dimitry Ivanov7e8cee82016-04-21 16:42:48 -0700116 const char* android_root_env = getenv("ANDROID_ROOT");
117 std::string root_dir = android_root_env != nullptr ? android_root_env : "/system";
118 std::string public_native_libraries_system_config =
119 root_dir + kPublicNativeLibrariesSystemConfigPathFromRoot;
Dimitry Ivanov617f4952016-04-06 18:24:08 -0700120
Dimitry Ivanov7e8cee82016-04-21 16:42:48 -0700121 LOG_ALWAYS_FATAL_IF(!ReadConfig(public_native_libraries_system_config, &sonames),
Dimitry Ivanov617f4952016-04-06 18:24:08 -0700122 "Error reading public native library list from \"%s\": %s",
Dimitry Ivanov7e8cee82016-04-21 16:42:48 -0700123 public_native_libraries_system_config.c_str(), strerror(errno));
Dimitry Ivanov4ddabd02016-05-05 17:30:24 -0700124
125 // For debuggable platform builds use ANDROID_ADDITIONAL_PUBLIC_LIBRARIES environment
126 // variable to add libraries to the list. This is intended for platform tests only.
127 if (is_debuggable()) {
128 const char* additional_libs = getenv("ANDROID_ADDITIONAL_PUBLIC_LIBRARIES");
129 if (additional_libs != nullptr && additional_libs[0] != '\0') {
130 std::vector<std::string> additional_libs_vector = base::Split(additional_libs, ":");
131 std::copy(additional_libs_vector.begin(),
132 additional_libs_vector.end(),
133 std::back_inserter(sonames));
134 }
135 }
136
Dimitry Ivanov617f4952016-04-06 18:24:08 -0700137 // This file is optional, quietly ignore if the file does not exist.
138 ReadConfig(kPublicNativeLibrariesVendorConfig, &sonames);
139
140 // android_init_namespaces() expects all the public libraries
141 // to be loaded so that they can be found by soname alone.
142 //
143 // TODO(dimitry): this is a bit misleading since we do not know
144 // if the vendor public library is going to be opened from /vendor/lib
145 // we might as well end up loading them from /system/lib
146 // For now we rely on CTS test to catch things like this but
147 // it should probably be addressed in the future.
148 for (const auto& soname : sonames) {
149 dlopen(soname.c_str(), RTLD_NOW | RTLD_NODELETE);
150 }
151
152 public_libraries_ = base::Join(sonames, ':');
153 }
154
Dimitry Ivanov911472d2016-05-02 10:43:16 -0700155 void Reset() {
156 namespaces_.clear();
157 }
158
Dimitry Ivanov617f4952016-04-06 18:24:08 -0700159 private:
160 bool ReadConfig(const std::string& configFile, std::vector<std::string>* sonames) {
Dimitry Ivanovd1fdb982016-03-15 13:51:26 -0700161 // Read list of public native libraries from the config file.
162 std::string file_content;
Dimitry Ivanov617f4952016-04-06 18:24:08 -0700163 if(!base::ReadFileToString(configFile, &file_content)) {
164 return false;
165 }
Dimitry Ivanovd1fdb982016-03-15 13:51:26 -0700166
167 std::vector<std::string> lines = base::Split(file_content, "\n");
168
Dimitry Ivanovd1fdb982016-03-15 13:51:26 -0700169 for (const auto& line : lines) {
170 auto trimmed_line = base::Trim(line);
171 if (trimmed_line[0] == '#' || trimmed_line.empty()) {
172 continue;
173 }
174
Dimitry Ivanov617f4952016-04-06 18:24:08 -0700175 sonames->push_back(trimmed_line);
Dimitry Ivanovd1fdb982016-03-15 13:51:26 -0700176 }
177
Dimitry Ivanov617f4952016-04-06 18:24:08 -0700178 return true;
Dimitry Ivanov55bbb0d2016-02-10 14:09:22 -0800179 }
Dimitry Ivanovac1b1912015-12-01 13:56:44 -0800180
Dimitry Ivanov55bbb0d2016-02-10 14:09:22 -0800181 bool InitPublicNamespace(const char* library_path) {
Dimitry Ivanovd1fdb982016-03-15 13:51:26 -0700182 // (http://b/25844435) - Some apps call dlopen from generated code (mono jited
183 // code is one example) unknown to linker in which case linker uses anonymous
184 // namespace. The second argument specifies the search path for the anonymous
185 // namespace which is the library_path of the classloader.
186 initialized_ = android_init_namespaces(public_libraries_.c_str(), library_path);
Dimitry Ivanovac1b1912015-12-01 13:56:44 -0800187
188 return initialized_;
189 }
190
Dimitry Ivanovac1b1912015-12-01 13:56:44 -0800191 bool initialized_;
Dimitry Ivanovac1b1912015-12-01 13:56:44 -0800192 std::vector<std::pair<jweak, android_namespace_t*>> namespaces_;
Dimitry Ivanovd1fdb982016-03-15 13:51:26 -0700193 std::string public_libraries_;
194
Dimitry Ivanovac1b1912015-12-01 13:56:44 -0800195
196 DISALLOW_COPY_AND_ASSIGN(LibraryNamespaces);
197};
198
Dimitry Ivanov1dff1aa2016-02-29 13:21:43 -0800199static std::mutex g_namespaces_mutex;
Dimitry Ivanovac1b1912015-12-01 13:56:44 -0800200static LibraryNamespaces* g_namespaces = new LibraryNamespaces;
201#endif
202
Dimitry Ivanovd1fdb982016-03-15 13:51:26 -0700203void InitializeNativeLoader() {
Dimitry Ivanov6f800222016-02-22 11:27:48 -0800204#if defined(__ANDROID__)
Dimitry Ivanov1dff1aa2016-02-29 13:21:43 -0800205 std::lock_guard<std::mutex> guard(g_namespaces_mutex);
Dimitry Ivanovd1fdb982016-03-15 13:51:26 -0700206 g_namespaces->Initialize();
Dimitry Ivanov6f800222016-02-22 11:27:48 -0800207#endif
208}
209
Dimitry Ivanov911472d2016-05-02 10:43:16 -0700210void ResetNativeLoader() {
211#if defined(__ANDROID__)
212 std::lock_guard<std::mutex> guard(g_namespaces_mutex);
213 g_namespaces->Reset();
214#endif
215}
Dimitry Ivanovac1b1912015-12-01 13:56:44 -0800216
Dimitry Ivanov4cd07682016-02-23 14:23:51 -0800217jstring CreateClassLoaderNamespace(JNIEnv* env,
218 int32_t target_sdk_version,
219 jobject class_loader,
220 bool is_shared,
221 jstring library_path,
222 jstring permitted_path) {
Dimitry Ivanovac1b1912015-12-01 13:56:44 -0800223#if defined(__ANDROID__)
Dimitry Ivanov213676b2016-04-20 16:07:30 -0700224 UNUSED(target_sdk_version);
Dimitry Ivanov1dff1aa2016-02-29 13:21:43 -0800225 std::lock_guard<std::mutex> guard(g_namespaces_mutex);
Dimitry Ivanov4cd07682016-02-23 14:23:51 -0800226 android_namespace_t* ns = g_namespaces->Create(env,
227 class_loader,
228 is_shared,
229 library_path,
230 permitted_path);
231 if (ns == nullptr) {
232 return env->NewStringUTF(dlerror());
233 }
234#else
235 UNUSED(env, target_sdk_version, class_loader, is_shared,
236 library_path, permitted_path);
237#endif
238 return nullptr;
239}
240
241void* OpenNativeLibrary(JNIEnv* env,
242 int32_t target_sdk_version,
243 const char* path,
244 jobject class_loader,
245 jstring library_path) {
246#if defined(__ANDROID__)
Dimitry Ivanov213676b2016-04-20 16:07:30 -0700247 UNUSED(target_sdk_version);
248 if (class_loader == nullptr) {
Dimitry Ivanovac1b1912015-12-01 13:56:44 -0800249 return dlopen(path, RTLD_NOW);
250 }
251
Dimitry Ivanov1dff1aa2016-02-29 13:21:43 -0800252 std::lock_guard<std::mutex> guard(g_namespaces_mutex);
Dimitry Ivanov4cd07682016-02-23 14:23:51 -0800253 android_namespace_t* ns = g_namespaces->FindNamespaceByClassLoader(env, class_loader);
Dimitry Ivanovac1b1912015-12-01 13:56:44 -0800254
255 if (ns == nullptr) {
Dimitry Ivanov4cd07682016-02-23 14:23:51 -0800256 // This is the case where the classloader was not created by ApplicationLoaders
257 // In this case we create an isolated not-shared namespace for it.
258 ns = g_namespaces->Create(env, class_loader, false, library_path, nullptr);
259 if (ns == nullptr) {
260 return nullptr;
261 }
Dimitry Ivanovac1b1912015-12-01 13:56:44 -0800262 }
263
264 android_dlextinfo extinfo;
265 extinfo.flags = ANDROID_DLEXT_USE_NAMESPACE;
266 extinfo.library_namespace = ns;
267
268 return android_dlopen_ext(path, RTLD_NOW, &extinfo);
269#else
Dimitry Ivanov4cd07682016-02-23 14:23:51 -0800270 UNUSED(env, target_sdk_version, class_loader, library_path);
Dimitry Ivanovac1b1912015-12-01 13:56:44 -0800271 return dlopen(path, RTLD_NOW);
272#endif
273}
274
Dimitry Ivanov7a1f9512016-05-03 14:55:25 -0700275bool CloseNativeLibrary(void* handle) {
276 return dlclose(handle) == 0;
277}
278
Dimitry Ivanov0cd10d82016-02-22 13:48:22 -0800279#if defined(__ANDROID__)
280android_namespace_t* FindNamespaceByClassLoader(JNIEnv* env, jobject class_loader) {
Dimitry Ivanov1dff1aa2016-02-29 13:21:43 -0800281 std::lock_guard<std::mutex> guard(g_namespaces_mutex);
Dimitry Ivanov0cd10d82016-02-22 13:48:22 -0800282 return g_namespaces->FindNamespaceByClassLoader(env, class_loader);
283}
284#endif
285
Dimitry Ivanovac1b1912015-12-01 13:56:44 -0800286}; // android namespace