1 /*
2 * Copyright (C) 2019 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 #define LOG_TAG "nativeloader"
19
20 #include <dlfcn.h>
21 #include <errno.h>
22 #include <string.h>
23
24 #include <log/log.h>
25
26 namespace android {
27
28 namespace {
29
GetLibHandle()30 void* GetLibHandle() {
31 static void* handle = dlopen("libnativeloader.so", RTLD_NOW);
32 LOG_FATAL_IF(handle == nullptr, "Failed to load libnativeloader.so: %s", dlerror());
33 return handle;
34 }
35
36 template <typename FuncPtr>
GetFuncPtr(const char * function_name)37 FuncPtr GetFuncPtr(const char* function_name) {
38 FuncPtr f = reinterpret_cast<FuncPtr>(dlsym(GetLibHandle(), function_name));
39 LOG_FATAL_IF(f == nullptr, "Failed to get address of %s: %s", function_name, dlerror());
40 return f;
41 }
42
43 #define GET_FUNC_PTR(name) GetFuncPtr<decltype(&(name))>(#name)
44
45 } // namespace
46
CreateClassLoaderNamespace(JNIEnv * env,int32_t target_sdk_version,jobject class_loader,bool is_shared,jstring dex_path,jstring library_path,jstring permitted_path,jstring uses_library_list)47 jstring CreateClassLoaderNamespace(JNIEnv* env, int32_t target_sdk_version, jobject class_loader,
48 bool is_shared, jstring dex_path, jstring library_path,
49 jstring permitted_path, jstring uses_library_list) {
50 static auto f = GET_FUNC_PTR(CreateClassLoaderNamespace);
51 return f(env, target_sdk_version, class_loader, is_shared, dex_path, library_path,
52 permitted_path, uses_library_list);
53 }
54
OpenNativeLibrary(JNIEnv * env,int32_t target_sdk_version,const char * path,jobject class_loader,const char * caller_location,jstring library_path,bool * needs_native_bridge,char ** error_msg)55 void* OpenNativeLibrary(JNIEnv* env, int32_t target_sdk_version, const char* path,
56 jobject class_loader, const char* caller_location, jstring library_path,
57 bool* needs_native_bridge, char** error_msg) {
58 static auto f = GET_FUNC_PTR(OpenNativeLibrary);
59 return f(env, target_sdk_version, path, class_loader, caller_location, library_path,
60 needs_native_bridge, error_msg);
61 }
62
CloseNativeLibrary(void * handle,const bool needs_native_bridge,char ** error_msg)63 bool CloseNativeLibrary(void* handle, const bool needs_native_bridge, char** error_msg) {
64 static auto f = GET_FUNC_PTR(CloseNativeLibrary);
65 return f(handle, needs_native_bridge, error_msg);
66 }
67
NativeLoaderFreeErrorMessage(char * msg)68 void NativeLoaderFreeErrorMessage(char* msg) {
69 static auto f = GET_FUNC_PTR(NativeLoaderFreeErrorMessage);
70 return f(msg);
71 }
72
FindNamespaceByClassLoader(JNIEnv * env,jobject class_loader)73 struct android_namespace_t* FindNamespaceByClassLoader(JNIEnv* env, jobject class_loader) {
74 static auto f = GET_FUNC_PTR(FindNamespaceByClassLoader);
75 return f(env, class_loader);
76 }
77
FindNativeLoaderNamespaceByClassLoader(JNIEnv * env,jobject class_loader)78 struct NativeLoaderNamespace* FindNativeLoaderNamespaceByClassLoader(JNIEnv* env,
79 jobject class_loader) {
80 static auto f = GET_FUNC_PTR(FindNativeLoaderNamespaceByClassLoader);
81 return f(env, class_loader);
82 }
83
OpenNativeLibraryInNamespace(struct NativeLoaderNamespace * ns,const char * path,bool * needs_native_bridge,char ** error_msg)84 void* OpenNativeLibraryInNamespace(struct NativeLoaderNamespace* ns, const char* path,
85 bool* needs_native_bridge, char** error_msg) {
86 static auto f = GET_FUNC_PTR(OpenNativeLibraryInNamespace);
87 return f(ns, path, needs_native_bridge, error_msg);
88 }
89
90 #undef GET_FUNC_PTR
91
92 } // namespace android
93