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   auto 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 
InitializeNativeLoader()47 void InitializeNativeLoader() {
48   static auto f = GET_FUNC_PTR(InitializeNativeLoader);
49   return f();
50 }
51 
CreateClassLoaderNamespace(JNIEnv * env,int32_t target_sdk_version,jobject class_loader,bool is_shared,jstring dex_path,jstring library_path,jstring permitted_path)52 jstring CreateClassLoaderNamespace(JNIEnv* env, int32_t target_sdk_version, jobject class_loader,
53                                    bool is_shared, jstring dex_path, jstring library_path,
54                                    jstring permitted_path) {
55   static auto f = GET_FUNC_PTR(CreateClassLoaderNamespace);
56   return f(env, target_sdk_version, class_loader, is_shared, dex_path, library_path,
57            permitted_path);
58 }
59 
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)60 void* OpenNativeLibrary(JNIEnv* env, int32_t target_sdk_version, const char* path,
61                         jobject class_loader, const char* caller_location, jstring library_path,
62                         bool* needs_native_bridge, char** error_msg) {
63   static auto f = GET_FUNC_PTR(OpenNativeLibrary);
64   return f(env, target_sdk_version, path, class_loader, caller_location, library_path,
65            needs_native_bridge, error_msg);
66 }
67 
CloseNativeLibrary(void * handle,const bool needs_native_bridge,char ** error_msg)68 bool CloseNativeLibrary(void* handle, const bool needs_native_bridge, char** error_msg) {
69   static auto f = GET_FUNC_PTR(CloseNativeLibrary);
70   return f(handle, needs_native_bridge, error_msg);
71 }
72 
NativeLoaderFreeErrorMessage(char * msg)73 void NativeLoaderFreeErrorMessage(char* msg) {
74   static auto f = GET_FUNC_PTR(NativeLoaderFreeErrorMessage);
75   return f(msg);
76 }
77 
FindNamespaceByClassLoader(JNIEnv * env,jobject class_loader)78 struct android_namespace_t* FindNamespaceByClassLoader(JNIEnv* env, jobject class_loader) {
79   static auto f = GET_FUNC_PTR(FindNamespaceByClassLoader);
80   return f(env, class_loader);
81 }
82 
FindNativeLoaderNamespaceByClassLoader(JNIEnv * env,jobject class_loader)83 struct NativeLoaderNamespace* FindNativeLoaderNamespaceByClassLoader(JNIEnv* env,
84                                                                      jobject class_loader) {
85   static auto f = GET_FUNC_PTR(FindNativeLoaderNamespaceByClassLoader);
86   return f(env, class_loader);
87 }
88 
OpenNativeLibraryInNamespace(struct NativeLoaderNamespace * ns,const char * path,bool * needs_native_bridge,char ** error_msg)89 void* OpenNativeLibraryInNamespace(struct NativeLoaderNamespace* ns, const char* path,
90                                    bool* needs_native_bridge, char** error_msg) {
91   static auto f = GET_FUNC_PTR(OpenNativeLibraryInNamespace);
92   return f(ns, path, needs_native_bridge, error_msg);
93 }
94 
ResetNativeLoader()95 void ResetNativeLoader() {
96   static auto f = GET_FUNC_PTR(ResetNativeLoader);
97   return f();
98 }
99 
100 #undef GET_FUNC_PTR
101 
102 }  // namespace android
103