Dimitry Ivanov | ac1b191 | 2015-12-01 13:56:44 -0800 | [diff] [blame] | 1 | /* |
| 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" |
Jiyong Park | 6291da2 | 2019-04-26 18:55:48 +0900 | [diff] [blame] | 18 | #define LOG_TAG "nativeloader" |
Dimitry Ivanov | ac1b191 | 2015-12-01 13:56:44 -0800 | [diff] [blame] | 19 | |
| 20 | #include <dlfcn.h> |
Jiyong Park | d1006fe | 2017-11-08 18:44:09 +0900 | [diff] [blame] | 21 | #include <sys/types.h> |
Dimitry Ivanov | ac1b191 | 2015-12-01 13:56:44 -0800 | [diff] [blame] | 22 | |
Jiyong Park | d1006fe | 2017-11-08 18:44:09 +0900 | [diff] [blame] | 23 | #include <memory> |
Dimitry Ivanov | ac1b191 | 2015-12-01 13:56:44 -0800 | [diff] [blame] | 24 | #include <mutex> |
Jiyong Park | d1006fe | 2017-11-08 18:44:09 +0900 | [diff] [blame] | 25 | #include <string> |
| 26 | #include <vector> |
Dimitry Ivanov | ac1b191 | 2015-12-01 13:56:44 -0800 | [diff] [blame] | 27 | |
Jiyong Park | 6291da2 | 2019-04-26 18:55:48 +0900 | [diff] [blame] | 28 | #include "android-base/file.h" |
| 29 | #include "android-base/macros.h" |
| 30 | #include "android-base/strings.h" |
| 31 | #ifdef __ANDROID__ |
| 32 | #include "library_namespaces.h" |
| 33 | #include "log/log.h" |
| 34 | #include "nativeloader/dlext_namespaces.h" |
Justin Yun | 4a1d110 | 2017-11-27 17:04:14 +0900 | [diff] [blame] | 35 | #endif |
Jiyong Park | 6291da2 | 2019-04-26 18:55:48 +0900 | [diff] [blame] | 36 | #include "nativebridge/native_bridge.h" |
| 37 | #include "nativehelper/ScopedUtfChars.h" |
Inseob Kim | 67cb056 | 2018-05-04 11:39:12 +0900 | [diff] [blame] | 38 | |
Dimitry Ivanov | ac1b191 | 2015-12-01 13:56:44 -0800 | [diff] [blame] | 39 | namespace android { |
| 40 | |
Jiyong Park | 6291da2 | 2019-04-26 18:55:48 +0900 | [diff] [blame] | 41 | namespace { |
Dimitry Ivanov | 4b0e963 | 2016-03-15 13:51:26 -0700 | [diff] [blame] | 42 | #if defined(__ANDROID__) |
Jiyong Park | 40a6077 | 2019-05-03 16:21:31 +0900 | [diff] [blame] | 43 | using android::nativeloader::LibraryNamespaces; |
| 44 | |
Jiyong Park | 6291da2 | 2019-04-26 18:55:48 +0900 | [diff] [blame] | 45 | constexpr const char* kApexPath = "/apex/"; |
Zhenhua WANG | f2804e5 | 2016-05-30 11:16:08 +0800 | [diff] [blame] | 46 | |
Jiyong Park | 6291da2 | 2019-04-26 18:55:48 +0900 | [diff] [blame] | 47 | std::mutex g_namespaces_mutex; |
| 48 | LibraryNamespaces* g_namespaces = new LibraryNamespaces; |
Zhenhua WANG | f2804e5 | 2016-05-30 11:16:08 +0800 | [diff] [blame] | 49 | |
Jiyong Park | 6291da2 | 2019-04-26 18:55:48 +0900 | [diff] [blame] | 50 | android_namespace_t* FindExportedNamespace(const char* caller_location) { |
| 51 | std::string location = caller_location; |
| 52 | // Lots of implicit assumptions here: we expect `caller_location` to be of the form: |
| 53 | // /apex/com.android...modulename/... |
| 54 | // |
| 55 | // And we extract from it 'modulename', which is the name of the linker namespace. |
| 56 | if (android::base::StartsWith(location, kApexPath)) { |
| 57 | size_t slash_index = location.find_first_of('/', strlen(kApexPath)); |
| 58 | LOG_ALWAYS_FATAL_IF((slash_index == std::string::npos), |
| 59 | "Error finding namespace of apex: no slash in path %s", caller_location); |
| 60 | size_t dot_index = location.find_last_of('.', slash_index); |
| 61 | LOG_ALWAYS_FATAL_IF((dot_index == std::string::npos), |
| 62 | "Error finding namespace of apex: no dot in apex name %s", caller_location); |
| 63 | std::string name = location.substr(dot_index + 1, slash_index - dot_index - 1); |
| 64 | android_namespace_t* boot_namespace = android_get_exported_namespace(name.c_str()); |
| 65 | LOG_ALWAYS_FATAL_IF((boot_namespace == nullptr), |
| 66 | "Error finding namespace of apex: no namespace called %s", name.c_str()); |
| 67 | return boot_namespace; |
Zhenhua WANG | f2804e5 | 2016-05-30 11:16:08 +0800 | [diff] [blame] | 68 | } |
Jiyong Park | 6291da2 | 2019-04-26 18:55:48 +0900 | [diff] [blame] | 69 | return nullptr; |
Dimitry Ivanov | 7d02829 | 2016-05-05 17:30:24 -0700 | [diff] [blame] | 70 | } |
Jiyong Park | 6291da2 | 2019-04-26 18:55:48 +0900 | [diff] [blame] | 71 | #endif // #if defined(__ANDROID__) |
| 72 | } // namespace |
Dimitry Ivanov | ac1b191 | 2015-12-01 13:56:44 -0800 | [diff] [blame] | 73 | |
Dimitry Ivanov | 4b0e963 | 2016-03-15 13:51:26 -0700 | [diff] [blame] | 74 | void InitializeNativeLoader() { |
Dimitry Ivanov | 426799d | 2016-02-22 11:27:48 -0800 | [diff] [blame] | 75 | #if defined(__ANDROID__) |
Dimitry Ivanov | 34d5a20 | 2016-02-29 13:21:43 -0800 | [diff] [blame] | 76 | std::lock_guard<std::mutex> guard(g_namespaces_mutex); |
Dimitry Ivanov | 4b0e963 | 2016-03-15 13:51:26 -0700 | [diff] [blame] | 77 | g_namespaces->Initialize(); |
Dimitry Ivanov | 426799d | 2016-02-22 11:27:48 -0800 | [diff] [blame] | 78 | #endif |
| 79 | } |
| 80 | |
Dimitry Ivanov | be4ca3a | 2016-05-02 10:43:16 -0700 | [diff] [blame] | 81 | void ResetNativeLoader() { |
| 82 | #if defined(__ANDROID__) |
| 83 | std::lock_guard<std::mutex> guard(g_namespaces_mutex); |
| 84 | g_namespaces->Reset(); |
| 85 | #endif |
| 86 | } |
Dimitry Ivanov | ac1b191 | 2015-12-01 13:56:44 -0800 | [diff] [blame] | 87 | |
Kiyoung Kim | 4639f69 | 2019-02-20 18:04:39 +0900 | [diff] [blame] | 88 | jstring CreateClassLoaderNamespace(JNIEnv* env, int32_t target_sdk_version, jobject class_loader, |
| 89 | bool is_shared, jstring dex_path, jstring library_path, |
Dimitry Ivanov | d047c92 | 2016-02-23 14:23:51 -0800 | [diff] [blame] | 90 | jstring permitted_path) { |
Dimitry Ivanov | ac1b191 | 2015-12-01 13:56:44 -0800 | [diff] [blame] | 91 | #if defined(__ANDROID__) |
Dimitry Ivanov | 34d5a20 | 2016-02-29 13:21:43 -0800 | [diff] [blame] | 92 | std::lock_guard<std::mutex> guard(g_namespaces_mutex); |
Zhenhua WANG | f2804e5 | 2016-05-30 11:16:08 +0800 | [diff] [blame] | 93 | |
| 94 | std::string error_msg; |
Kiyoung Kim | 4639f69 | 2019-02-20 18:04:39 +0900 | [diff] [blame] | 95 | bool success = g_namespaces->Create(env, target_sdk_version, class_loader, is_shared, dex_path, |
| 96 | library_path, permitted_path, &error_msg) != nullptr; |
Zhenhua WANG | f2804e5 | 2016-05-30 11:16:08 +0800 | [diff] [blame] | 97 | if (!success) { |
| 98 | return env->NewStringUTF(error_msg.c_str()); |
Dimitry Ivanov | d047c92 | 2016-02-23 14:23:51 -0800 | [diff] [blame] | 99 | } |
| 100 | #else |
Kiyoung Kim | 4639f69 | 2019-02-20 18:04:39 +0900 | [diff] [blame] | 101 | UNUSED(env, target_sdk_version, class_loader, is_shared, dex_path, library_path, permitted_path); |
Dimitry Ivanov | d047c92 | 2016-02-23 14:23:51 -0800 | [diff] [blame] | 102 | #endif |
| 103 | return nullptr; |
| 104 | } |
| 105 | |
Nicolas Geoffray | c3a73dc | 2019-01-12 15:01:20 +0000 | [diff] [blame] | 106 | void* OpenNativeLibrary(JNIEnv* env, int32_t target_sdk_version, const char* path, |
Nicolas Geoffray | 5353502 | 2019-01-18 10:05:13 +0000 | [diff] [blame] | 107 | jobject class_loader, const char* caller_location, jstring library_path, |
| 108 | bool* needs_native_bridge, char** error_msg) { |
Dimitry Ivanov | d047c92 | 2016-02-23 14:23:51 -0800 | [diff] [blame] | 109 | #if defined(__ANDROID__) |
Dimitry Ivanov | 5539db0 | 2016-04-20 16:07:30 -0700 | [diff] [blame] | 110 | UNUSED(target_sdk_version); |
| 111 | if (class_loader == nullptr) { |
Zhenhua WANG | f2804e5 | 2016-05-30 11:16:08 +0800 | [diff] [blame] | 112 | *needs_native_bridge = false; |
Nicolas Geoffray | 890e3bf | 2019-01-22 09:11:57 +0000 | [diff] [blame] | 113 | if (caller_location != nullptr) { |
| 114 | android_namespace_t* boot_namespace = FindExportedNamespace(caller_location); |
| 115 | if (boot_namespace != nullptr) { |
| 116 | const android_dlextinfo dlextinfo = { |
| 117 | .flags = ANDROID_DLEXT_USE_NAMESPACE, |
| 118 | .library_namespace = boot_namespace, |
| 119 | }; |
| 120 | void* handle = android_dlopen_ext(path, RTLD_NOW, &dlextinfo); |
| 121 | if (handle == nullptr) { |
| 122 | *error_msg = strdup(dlerror()); |
| 123 | } |
| 124 | return handle; |
| 125 | } |
| 126 | } |
Pete Bentley | 632f142 | 2018-12-19 13:33:33 +0000 | [diff] [blame] | 127 | void* handle = dlopen(path, RTLD_NOW); |
| 128 | if (handle == nullptr) { |
Nicolas Geoffray | d06cb94 | 2019-01-16 20:20:27 +0000 | [diff] [blame] | 129 | *error_msg = strdup(dlerror()); |
Pete Bentley | 632f142 | 2018-12-19 13:33:33 +0000 | [diff] [blame] | 130 | } |
| 131 | return handle; |
Dimitry Ivanov | ac1b191 | 2015-12-01 13:56:44 -0800 | [diff] [blame] | 132 | } |
| 133 | |
Dimitry Ivanov | 34d5a20 | 2016-02-29 13:21:43 -0800 | [diff] [blame] | 134 | std::lock_guard<std::mutex> guard(g_namespaces_mutex); |
Victor Khimenko | 1443ec4 | 2018-07-09 17:01:22 +0200 | [diff] [blame] | 135 | NativeLoaderNamespace* ns; |
Dimitry Ivanov | ac1b191 | 2015-12-01 13:56:44 -0800 | [diff] [blame] | 136 | |
Victor Khimenko | 1443ec4 | 2018-07-09 17:01:22 +0200 | [diff] [blame] | 137 | if ((ns = g_namespaces->FindNamespaceByClassLoader(env, class_loader)) == nullptr) { |
Dimitry Ivanov | d047c92 | 2016-02-23 14:23:51 -0800 | [diff] [blame] | 138 | // This is the case where the classloader was not created by ApplicationLoaders |
| 139 | // In this case we create an isolated not-shared namespace for it. |
Nicolas Geoffray | c3a73dc | 2019-01-12 15:01:20 +0000 | [diff] [blame] | 140 | std::string create_error_msg; |
| 141 | if ((ns = g_namespaces->Create(env, target_sdk_version, class_loader, false /* is_shared */, |
Kiyoung Kim | 4639f69 | 2019-02-20 18:04:39 +0900 | [diff] [blame] | 142 | nullptr, library_path, nullptr, &create_error_msg)) == nullptr) { |
Nicolas Geoffray | c3a73dc | 2019-01-12 15:01:20 +0000 | [diff] [blame] | 143 | *error_msg = strdup(create_error_msg.c_str()); |
Dimitry Ivanov | d047c92 | 2016-02-23 14:23:51 -0800 | [diff] [blame] | 144 | return nullptr; |
| 145 | } |
Dimitry Ivanov | ac1b191 | 2015-12-01 13:56:44 -0800 | [diff] [blame] | 146 | } |
| 147 | |
Nicolas Geoffray | c3a73dc | 2019-01-12 15:01:20 +0000 | [diff] [blame] | 148 | return OpenNativeLibraryInNamespace(ns, path, needs_native_bridge, error_msg); |
Dimitry Ivanov | ac1b191 | 2015-12-01 13:56:44 -0800 | [diff] [blame] | 149 | #else |
Nicolas Geoffray | 5353502 | 2019-01-18 10:05:13 +0000 | [diff] [blame] | 150 | UNUSED(env, target_sdk_version, class_loader, caller_location); |
Andreas Gampe | 5c7d582 | 2017-12-28 19:08:13 -0800 | [diff] [blame] | 151 | |
| 152 | // Do some best effort to emulate library-path support. It will not |
| 153 | // work for dependencies. |
| 154 | // |
| 155 | // Note: null has a special meaning and must be preserved. |
| 156 | std::string c_library_path; // Empty string by default. |
| 157 | if (library_path != nullptr && path != nullptr && path[0] != '/') { |
| 158 | ScopedUtfChars library_path_utf_chars(env, library_path); |
| 159 | c_library_path = library_path_utf_chars.c_str(); |
| 160 | } |
| 161 | |
| 162 | std::vector<std::string> library_paths = base::Split(c_library_path, ":"); |
| 163 | |
| 164 | for (const std::string& lib_path : library_paths) { |
| 165 | *needs_native_bridge = false; |
| 166 | const char* path_arg; |
| 167 | std::string complete_path; |
| 168 | if (path == nullptr) { |
| 169 | // Preserve null. |
| 170 | path_arg = nullptr; |
Zhenhua WANG | f2804e5 | 2016-05-30 11:16:08 +0800 | [diff] [blame] | 171 | } else { |
Andreas Gampe | 5c7d582 | 2017-12-28 19:08:13 -0800 | [diff] [blame] | 172 | complete_path = lib_path; |
| 173 | if (!complete_path.empty()) { |
| 174 | complete_path.append("/"); |
| 175 | } |
| 176 | complete_path.append(path); |
| 177 | path_arg = complete_path.c_str(); |
| 178 | } |
| 179 | void* handle = dlopen(path_arg, RTLD_NOW); |
| 180 | if (handle != nullptr) { |
| 181 | return handle; |
| 182 | } |
| 183 | if (NativeBridgeIsSupported(path_arg)) { |
| 184 | *needs_native_bridge = true; |
| 185 | handle = NativeBridgeLoadLibrary(path_arg, RTLD_NOW); |
| 186 | if (handle != nullptr) { |
| 187 | return handle; |
| 188 | } |
Nicolas Geoffray | c3a73dc | 2019-01-12 15:01:20 +0000 | [diff] [blame] | 189 | *error_msg = strdup(NativeBridgeGetError()); |
Andreas Gampe | 5c7d582 | 2017-12-28 19:08:13 -0800 | [diff] [blame] | 190 | } else { |
Nicolas Geoffray | c3a73dc | 2019-01-12 15:01:20 +0000 | [diff] [blame] | 191 | *error_msg = strdup(dlerror()); |
Zhenhua WANG | f2804e5 | 2016-05-30 11:16:08 +0800 | [diff] [blame] | 192 | } |
| 193 | } |
Andreas Gampe | 5c7d582 | 2017-12-28 19:08:13 -0800 | [diff] [blame] | 194 | return nullptr; |
Dimitry Ivanov | ac1b191 | 2015-12-01 13:56:44 -0800 | [diff] [blame] | 195 | #endif |
| 196 | } |
| 197 | |
Nicolas Geoffray | c3a73dc | 2019-01-12 15:01:20 +0000 | [diff] [blame] | 198 | bool CloseNativeLibrary(void* handle, const bool needs_native_bridge, char** error_msg) { |
dimitry | 3150f7c | 2018-09-12 01:09:19 +0200 | [diff] [blame] | 199 | bool success; |
| 200 | if (needs_native_bridge) { |
| 201 | success = (NativeBridgeUnloadLibrary(handle) == 0); |
| 202 | if (!success) { |
Nicolas Geoffray | c3a73dc | 2019-01-12 15:01:20 +0000 | [diff] [blame] | 203 | *error_msg = strdup(NativeBridgeGetError()); |
dimitry | 3150f7c | 2018-09-12 01:09:19 +0200 | [diff] [blame] | 204 | } |
| 205 | } else { |
| 206 | success = (dlclose(handle) == 0); |
| 207 | if (!success) { |
Nicolas Geoffray | c3a73dc | 2019-01-12 15:01:20 +0000 | [diff] [blame] | 208 | *error_msg = strdup(dlerror()); |
dimitry | 3150f7c | 2018-09-12 01:09:19 +0200 | [diff] [blame] | 209 | } |
| 210 | } |
| 211 | |
| 212 | return success; |
Dimitry Ivanov | 09a516b | 2016-05-03 14:55:25 -0700 | [diff] [blame] | 213 | } |
| 214 | |
Nicolas Geoffray | c3a73dc | 2019-01-12 15:01:20 +0000 | [diff] [blame] | 215 | void NativeLoaderFreeErrorMessage(char* msg) { |
| 216 | // The error messages get allocated through strdup, so we must call free on them. |
| 217 | free(msg); |
| 218 | } |
| 219 | |
Dimitry Ivanov | f44ecde | 2016-02-22 13:48:22 -0800 | [diff] [blame] | 220 | #if defined(__ANDROID__) |
Nicolas Geoffray | c3a73dc | 2019-01-12 15:01:20 +0000 | [diff] [blame] | 221 | void* OpenNativeLibraryInNamespace(NativeLoaderNamespace* ns, const char* path, |
| 222 | bool* needs_native_bridge, char** error_msg) { |
Jiyong Park | 8537781 | 2019-05-04 00:30:23 +0900 | [diff] [blame^] | 223 | void* handle = ns->Load(path); |
| 224 | if (handle == nullptr) { |
| 225 | *error_msg = ns->GetError(); |
Victor Khimenko | 1443ec4 | 2018-07-09 17:01:22 +0200 | [diff] [blame] | 226 | } |
Jiyong Park | 8537781 | 2019-05-04 00:30:23 +0900 | [diff] [blame^] | 227 | *needs_native_bridge = ns->IsBridged(); |
| 228 | return handle; |
Victor Khimenko | 1443ec4 | 2018-07-09 17:01:22 +0200 | [diff] [blame] | 229 | } |
| 230 | |
Dimitry Ivanov | 800083d | 2016-11-01 14:17:00 -0700 | [diff] [blame] | 231 | // native_bridge_namespaces are not supported for callers of this function. |
| 232 | // This function will return nullptr in the case when application is running |
| 233 | // on native bridge. |
Dimitry Ivanov | f44ecde | 2016-02-22 13:48:22 -0800 | [diff] [blame] | 234 | android_namespace_t* FindNamespaceByClassLoader(JNIEnv* env, jobject class_loader) { |
Dimitry Ivanov | 34d5a20 | 2016-02-29 13:21:43 -0800 | [diff] [blame] | 235 | std::lock_guard<std::mutex> guard(g_namespaces_mutex); |
Victor Khimenko | 1443ec4 | 2018-07-09 17:01:22 +0200 | [diff] [blame] | 236 | NativeLoaderNamespace* ns = g_namespaces->FindNamespaceByClassLoader(env, class_loader); |
Jiyong Park | 8537781 | 2019-05-04 00:30:23 +0900 | [diff] [blame^] | 237 | if (ns != nullptr && !ns->IsBridged()) { |
| 238 | return ns->ToRawAndroidNamespace(); |
Zhenhua WANG | f2804e5 | 2016-05-30 11:16:08 +0800 | [diff] [blame] | 239 | } |
Zhenhua WANG | f2804e5 | 2016-05-30 11:16:08 +0800 | [diff] [blame] | 240 | return nullptr; |
Dimitry Ivanov | f44ecde | 2016-02-22 13:48:22 -0800 | [diff] [blame] | 241 | } |
Jiyong Park | 6291da2 | 2019-04-26 18:55:48 +0900 | [diff] [blame] | 242 | |
Victor Khimenko | 1443ec4 | 2018-07-09 17:01:22 +0200 | [diff] [blame] | 243 | NativeLoaderNamespace* FindNativeLoaderNamespaceByClassLoader(JNIEnv* env, jobject class_loader) { |
| 244 | std::lock_guard<std::mutex> guard(g_namespaces_mutex); |
| 245 | return g_namespaces->FindNamespaceByClassLoader(env, class_loader); |
| 246 | } |
Dimitry Ivanov | f44ecde | 2016-02-22 13:48:22 -0800 | [diff] [blame] | 247 | #endif |
| 248 | |
Jiyong Park | 6291da2 | 2019-04-26 18:55:48 +0900 | [diff] [blame] | 249 | }; // namespace android |