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 "nativebridge/native_bridge.h"
18 #define LOG_TAG "nativebridge"
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("libnativebridge.so", RTLD_NOW);
32   LOG_FATAL_IF(handle == nullptr, "Failed to load libnativebridge.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 
LoadNativeBridge(const char * native_bridge_library_filename,const struct NativeBridgeRuntimeCallbacks * runtime_callbacks)47 bool LoadNativeBridge(const char* native_bridge_library_filename,
48                       const struct NativeBridgeRuntimeCallbacks* runtime_callbacks) {
49   static auto f = GET_FUNC_PTR(LoadNativeBridge);
50   return f(native_bridge_library_filename, runtime_callbacks);
51 }
52 
NeedsNativeBridge(const char * instruction_set)53 bool NeedsNativeBridge(const char* instruction_set) {
54   static auto f = GET_FUNC_PTR(NeedsNativeBridge);
55   return f(instruction_set);
56 }
57 
PreInitializeNativeBridge(const char * app_data_dir,const char * instruction_set)58 bool PreInitializeNativeBridge(const char* app_data_dir, const char* instruction_set) {
59   static auto f = GET_FUNC_PTR(PreInitializeNativeBridge);
60   return f(app_data_dir, instruction_set);
61 }
62 
PreZygoteForkNativeBridge()63 void PreZygoteForkNativeBridge() {
64   static auto f = GET_FUNC_PTR(PreZygoteForkNativeBridge);
65   return f();
66 }
67 
InitializeNativeBridge(JNIEnv * env,const char * instruction_set)68 bool InitializeNativeBridge(JNIEnv* env, const char* instruction_set) {
69   static auto f = GET_FUNC_PTR(InitializeNativeBridge);
70   return f(env, instruction_set);
71 }
72 
UnloadNativeBridge()73 void UnloadNativeBridge() {
74   static auto f = GET_FUNC_PTR(UnloadNativeBridge);
75   return f();
76 }
77 
NativeBridgeAvailable()78 bool NativeBridgeAvailable() {
79   static auto f = GET_FUNC_PTR(NativeBridgeAvailable);
80   return f();
81 }
82 
NativeBridgeInitialized()83 bool NativeBridgeInitialized() {
84   static auto f = GET_FUNC_PTR(NativeBridgeInitialized);
85   return f();
86 }
87 
NativeBridgeLoadLibrary(const char * libpath,int flag)88 void* NativeBridgeLoadLibrary(const char* libpath, int flag) {
89   static auto f = GET_FUNC_PTR(NativeBridgeLoadLibrary);
90   return f(libpath, flag);
91 }
92 
NativeBridgeGetTrampoline(void * handle,const char * name,const char * shorty,uint32_t len)93 void* NativeBridgeGetTrampoline(void* handle, const char* name, const char* shorty, uint32_t len) {
94   static auto f = GET_FUNC_PTR(NativeBridgeGetTrampoline);
95   return f(handle, name, shorty, len);
96 }
97 
NativeBridgeIsSupported(const char * libpath)98 bool NativeBridgeIsSupported(const char* libpath) {
99   static auto f = GET_FUNC_PTR(NativeBridgeIsSupported);
100   return f(libpath);
101 }
102 
NativeBridgeGetVersion()103 uint32_t NativeBridgeGetVersion() {
104   static auto f = GET_FUNC_PTR(NativeBridgeGetVersion);
105   return f();
106 }
107 
NativeBridgeGetSignalHandler(int signal)108 NativeBridgeSignalHandlerFn NativeBridgeGetSignalHandler(int signal) {
109   static auto f = GET_FUNC_PTR(NativeBridgeGetSignalHandler);
110   return f(signal);
111 }
112 
NativeBridgeError()113 bool NativeBridgeError() {
114   static auto f = GET_FUNC_PTR(NativeBridgeError);
115   return f();
116 }
117 
NativeBridgeNameAcceptable(const char * native_bridge_library_filename)118 bool NativeBridgeNameAcceptable(const char* native_bridge_library_filename) {
119   static auto f = GET_FUNC_PTR(NativeBridgeNameAcceptable);
120   return f(native_bridge_library_filename);
121 }
122 
NativeBridgeUnloadLibrary(void * handle)123 int NativeBridgeUnloadLibrary(void* handle) {
124   static auto f = GET_FUNC_PTR(NativeBridgeUnloadLibrary);
125   return f(handle);
126 }
127 
NativeBridgeGetError()128 const char* NativeBridgeGetError() {
129   static auto f = GET_FUNC_PTR(NativeBridgeGetError);
130   return f();
131 }
132 
NativeBridgeIsPathSupported(const char * path)133 bool NativeBridgeIsPathSupported(const char* path) {
134   static auto f = GET_FUNC_PTR(NativeBridgeIsPathSupported);
135   return f(path);
136 }
137 
NativeBridgeInitAnonymousNamespace(const char * public_ns_sonames,const char * anon_ns_library_path)138 bool NativeBridgeInitAnonymousNamespace(const char* public_ns_sonames,
139                                         const char* anon_ns_library_path) {
140   static auto f = GET_FUNC_PTR(NativeBridgeInitAnonymousNamespace);
141   return f(public_ns_sonames, anon_ns_library_path);
142 }
143 
NativeBridgeCreateNamespace(const char * name,const char * ld_library_path,const char * default_library_path,uint64_t type,const char * permitted_when_isolated_path,struct native_bridge_namespace_t * parent_ns)144 struct native_bridge_namespace_t* NativeBridgeCreateNamespace(
145     const char* name, const char* ld_library_path, const char* default_library_path, uint64_t type,
146     const char* permitted_when_isolated_path, struct native_bridge_namespace_t* parent_ns) {
147   static auto f = GET_FUNC_PTR(NativeBridgeCreateNamespace);
148   return f(name, ld_library_path, default_library_path, type, permitted_when_isolated_path,
149            parent_ns);
150 }
151 
NativeBridgeLinkNamespaces(struct native_bridge_namespace_t * from,struct native_bridge_namespace_t * to,const char * shared_libs_sonames)152 bool NativeBridgeLinkNamespaces(struct native_bridge_namespace_t* from,
153                                 struct native_bridge_namespace_t* to,
154                                 const char* shared_libs_sonames) {
155   static auto f = GET_FUNC_PTR(NativeBridgeLinkNamespaces);
156   return f(from, to, shared_libs_sonames);
157 }
158 
NativeBridgeLoadLibraryExt(const char * libpath,int flag,struct native_bridge_namespace_t * ns)159 void* NativeBridgeLoadLibraryExt(const char* libpath, int flag,
160                                  struct native_bridge_namespace_t* ns) {
161   static auto f = GET_FUNC_PTR(NativeBridgeLoadLibraryExt);
162   return f(libpath, flag, ns);
163 }
164 
NativeBridgeGetVendorNamespace()165 struct native_bridge_namespace_t* NativeBridgeGetVendorNamespace() {
166   static auto f = GET_FUNC_PTR(NativeBridgeGetVendorNamespace);
167   return f();
168 }
169 
170 #undef GET_FUNC_PTR
171 
172 }  // namespace android
173