blob: c7e479ab081eef05956ed05181476cfb38ebe332 [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 Ivanov617f4952016-04-06 18:24:08 -070039static constexpr const char* kPublicNativeLibrariesSystemConfig = "/system/etc/public.libraries.txt";
40static constexpr const char* kPublicNativeLibrariesVendorConfig = "/vendor/etc/public.libraries.txt";
Dimitry Ivanovac1b1912015-12-01 13:56:44 -080041
42class LibraryNamespaces {
43 public:
Dimitry Ivanov6f800222016-02-22 11:27:48 -080044 LibraryNamespaces() : initialized_(false) { }
Dimitry Ivanovac1b1912015-12-01 13:56:44 -080045
Dimitry Ivanov4cd07682016-02-23 14:23:51 -080046 android_namespace_t* Create(JNIEnv* env,
47 jobject class_loader,
48 bool is_shared,
49 jstring java_library_path,
50 jstring java_permitted_path) {
Dimitry Ivanov0d6e5942015-12-08 11:16:56 -080051 ScopedUtfChars library_path(env, java_library_path);
Dimitry Ivanovac1b1912015-12-01 13:56:44 -080052
Dimitry Ivanov0d6e5942015-12-08 11:16:56 -080053 std::string permitted_path;
54 if (java_permitted_path != nullptr) {
55 ScopedUtfChars path(env, java_permitted_path);
56 permitted_path = path.c_str();
57 }
58
59 if (!initialized_ && !InitPublicNamespace(library_path.c_str())) {
Dimitry Ivanovac1b1912015-12-01 13:56:44 -080060 return nullptr;
61 }
62
Dimitry Ivanov34fa7042016-02-22 13:02:35 -080063 android_namespace_t* ns = FindNamespaceByClassLoader(env, class_loader);
Dimitry Ivanovac1b1912015-12-01 13:56:44 -080064
Dimitry Ivanovd1fdb982016-03-15 13:51:26 -070065 LOG_ALWAYS_FATAL_IF(ns != nullptr,
66 "There is already a namespace associated with this classloader");
Dimitry Ivanovac1b1912015-12-01 13:56:44 -080067
Dimitry Ivanovd2a62202015-12-15 11:06:57 -080068 uint64_t namespace_type = ANDROID_NAMESPACE_TYPE_ISOLATED;
69 if (is_shared) {
70 namespace_type |= ANDROID_NAMESPACE_TYPE_SHARED;
71 }
72
Dimitry Ivanov34fa7042016-02-22 13:02:35 -080073 ns = android_create_namespace("classloader-namespace",
74 nullptr,
75 library_path.c_str(),
76 namespace_type,
77 java_permitted_path != nullptr ?
78 permitted_path.c_str() :
79 nullptr);
Dimitry Ivanovac1b1912015-12-01 13:56:44 -080080
Dimitry Ivanov4cd07682016-02-23 14:23:51 -080081 if (ns != nullptr) {
82 namespaces_.push_back(std::make_pair(env->NewWeakGlobalRef(class_loader), ns));
83 }
Dimitry Ivanovac1b1912015-12-01 13:56:44 -080084
85 return ns;
86 }
87
Dimitry Ivanov0cd10d82016-02-22 13:48:22 -080088 android_namespace_t* FindNamespaceByClassLoader(JNIEnv* env, jobject class_loader) {
89 auto it = std::find_if(namespaces_.begin(), namespaces_.end(),
90 [&](const std::pair<jweak, android_namespace_t*>& value) {
91 return env->IsSameObject(value.first, class_loader);
92 });
93 return it != namespaces_.end() ? it->second : nullptr;
94 }
95
Dimitry Ivanovd1fdb982016-03-15 13:51:26 -070096 void Initialize() {
Dimitry Ivanov617f4952016-04-06 18:24:08 -070097 std::vector<std::string> sonames;
98
99 LOG_ALWAYS_FATAL_IF(!ReadConfig(kPublicNativeLibrariesSystemConfig, &sonames),
100 "Error reading public native library list from \"%s\": %s",
101 kPublicNativeLibrariesSystemConfig, strerror(errno));
102 // This file is optional, quietly ignore if the file does not exist.
103 ReadConfig(kPublicNativeLibrariesVendorConfig, &sonames);
104
105 // android_init_namespaces() expects all the public libraries
106 // to be loaded so that they can be found by soname alone.
107 //
108 // TODO(dimitry): this is a bit misleading since we do not know
109 // if the vendor public library is going to be opened from /vendor/lib
110 // we might as well end up loading them from /system/lib
111 // For now we rely on CTS test to catch things like this but
112 // it should probably be addressed in the future.
113 for (const auto& soname : sonames) {
114 dlopen(soname.c_str(), RTLD_NOW | RTLD_NODELETE);
115 }
116
117 public_libraries_ = base::Join(sonames, ':');
118 }
119
120 private:
121 bool ReadConfig(const std::string& configFile, std::vector<std::string>* sonames) {
Dimitry Ivanovd1fdb982016-03-15 13:51:26 -0700122 // Read list of public native libraries from the config file.
123 std::string file_content;
Dimitry Ivanov617f4952016-04-06 18:24:08 -0700124 if(!base::ReadFileToString(configFile, &file_content)) {
125 return false;
126 }
Dimitry Ivanovd1fdb982016-03-15 13:51:26 -0700127
128 std::vector<std::string> lines = base::Split(file_content, "\n");
129
Dimitry Ivanovd1fdb982016-03-15 13:51:26 -0700130 for (const auto& line : lines) {
131 auto trimmed_line = base::Trim(line);
132 if (trimmed_line[0] == '#' || trimmed_line.empty()) {
133 continue;
134 }
135
Dimitry Ivanov617f4952016-04-06 18:24:08 -0700136 sonames->push_back(trimmed_line);
Dimitry Ivanovd1fdb982016-03-15 13:51:26 -0700137 }
138
Dimitry Ivanov617f4952016-04-06 18:24:08 -0700139 return true;
Dimitry Ivanov55bbb0d2016-02-10 14:09:22 -0800140 }
Dimitry Ivanovac1b1912015-12-01 13:56:44 -0800141
Dimitry Ivanov55bbb0d2016-02-10 14:09:22 -0800142 bool InitPublicNamespace(const char* library_path) {
Dimitry Ivanovd1fdb982016-03-15 13:51:26 -0700143 // (http://b/25844435) - Some apps call dlopen from generated code (mono jited
144 // code is one example) unknown to linker in which case linker uses anonymous
145 // namespace. The second argument specifies the search path for the anonymous
146 // namespace which is the library_path of the classloader.
147 initialized_ = android_init_namespaces(public_libraries_.c_str(), library_path);
Dimitry Ivanovac1b1912015-12-01 13:56:44 -0800148
149 return initialized_;
150 }
151
Dimitry Ivanovac1b1912015-12-01 13:56:44 -0800152 bool initialized_;
Dimitry Ivanovac1b1912015-12-01 13:56:44 -0800153 std::vector<std::pair<jweak, android_namespace_t*>> namespaces_;
Dimitry Ivanovd1fdb982016-03-15 13:51:26 -0700154 std::string public_libraries_;
155
Dimitry Ivanovac1b1912015-12-01 13:56:44 -0800156
157 DISALLOW_COPY_AND_ASSIGN(LibraryNamespaces);
158};
159
Dimitry Ivanov1dff1aa2016-02-29 13:21:43 -0800160static std::mutex g_namespaces_mutex;
Dimitry Ivanovac1b1912015-12-01 13:56:44 -0800161static LibraryNamespaces* g_namespaces = new LibraryNamespaces;
162#endif
163
Dimitry Ivanovd1fdb982016-03-15 13:51:26 -0700164void InitializeNativeLoader() {
Dimitry Ivanov6f800222016-02-22 11:27:48 -0800165#if defined(__ANDROID__)
Dimitry Ivanov1dff1aa2016-02-29 13:21:43 -0800166 std::lock_guard<std::mutex> guard(g_namespaces_mutex);
Dimitry Ivanovd1fdb982016-03-15 13:51:26 -0700167 g_namespaces->Initialize();
Dimitry Ivanov6f800222016-02-22 11:27:48 -0800168#endif
169}
170
Dimitry Ivanovac1b1912015-12-01 13:56:44 -0800171
Dimitry Ivanov4cd07682016-02-23 14:23:51 -0800172jstring CreateClassLoaderNamespace(JNIEnv* env,
173 int32_t target_sdk_version,
174 jobject class_loader,
175 bool is_shared,
176 jstring library_path,
177 jstring permitted_path) {
Dimitry Ivanovac1b1912015-12-01 13:56:44 -0800178#if defined(__ANDROID__)
Dimitry Ivanov213676b2016-04-20 16:07:30 -0700179 UNUSED(target_sdk_version);
Dimitry Ivanov1dff1aa2016-02-29 13:21:43 -0800180 std::lock_guard<std::mutex> guard(g_namespaces_mutex);
Dimitry Ivanov4cd07682016-02-23 14:23:51 -0800181 android_namespace_t* ns = g_namespaces->Create(env,
182 class_loader,
183 is_shared,
184 library_path,
185 permitted_path);
186 if (ns == nullptr) {
187 return env->NewStringUTF(dlerror());
188 }
189#else
190 UNUSED(env, target_sdk_version, class_loader, is_shared,
191 library_path, permitted_path);
192#endif
193 return nullptr;
194}
195
196void* OpenNativeLibrary(JNIEnv* env,
197 int32_t target_sdk_version,
198 const char* path,
199 jobject class_loader,
200 jstring library_path) {
201#if defined(__ANDROID__)
Dimitry Ivanov213676b2016-04-20 16:07:30 -0700202 UNUSED(target_sdk_version);
203 if (class_loader == nullptr) {
Dimitry Ivanovac1b1912015-12-01 13:56:44 -0800204 return dlopen(path, RTLD_NOW);
205 }
206
Dimitry Ivanov1dff1aa2016-02-29 13:21:43 -0800207 std::lock_guard<std::mutex> guard(g_namespaces_mutex);
Dimitry Ivanov4cd07682016-02-23 14:23:51 -0800208 android_namespace_t* ns = g_namespaces->FindNamespaceByClassLoader(env, class_loader);
Dimitry Ivanovac1b1912015-12-01 13:56:44 -0800209
210 if (ns == nullptr) {
Dimitry Ivanov4cd07682016-02-23 14:23:51 -0800211 // This is the case where the classloader was not created by ApplicationLoaders
212 // In this case we create an isolated not-shared namespace for it.
213 ns = g_namespaces->Create(env, class_loader, false, library_path, nullptr);
214 if (ns == nullptr) {
215 return nullptr;
216 }
Dimitry Ivanovac1b1912015-12-01 13:56:44 -0800217 }
218
219 android_dlextinfo extinfo;
220 extinfo.flags = ANDROID_DLEXT_USE_NAMESPACE;
221 extinfo.library_namespace = ns;
222
223 return android_dlopen_ext(path, RTLD_NOW, &extinfo);
224#else
Dimitry Ivanov4cd07682016-02-23 14:23:51 -0800225 UNUSED(env, target_sdk_version, class_loader, library_path);
Dimitry Ivanovac1b1912015-12-01 13:56:44 -0800226 return dlopen(path, RTLD_NOW);
227#endif
228}
229
Dimitry Ivanov0cd10d82016-02-22 13:48:22 -0800230#if defined(__ANDROID__)
231android_namespace_t* FindNamespaceByClassLoader(JNIEnv* env, jobject class_loader) {
Dimitry Ivanov1dff1aa2016-02-29 13:21:43 -0800232 std::lock_guard<std::mutex> guard(g_namespaces_mutex);
Dimitry Ivanov0cd10d82016-02-22 13:48:22 -0800233 return g_namespaces->FindNamespaceByClassLoader(env, class_loader);
234}
235#endif
236
Dimitry Ivanovac1b1912015-12-01 13:56:44 -0800237}; // android namespace