blob: edc7848bbaa90e9b78e7c0a91665eb534bccc340 [file] [log] [blame]
Orion Hodson9b16e342019-10-09 13:29:16 +01001/*
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
26namespace android {
27
28namespace {
29
30void* 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
36template <typename FuncPtr>
37FuncPtr 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
47bool 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
53bool NeedsNativeBridge(const char* instruction_set) {
54 static auto f = GET_FUNC_PTR(NeedsNativeBridge);
55 return f(instruction_set);
56}
57
58bool 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
Lev Rumyantsevabafbe72019-12-13 15:49:37 -080063void PreZygoteForkNativeBridge() {
64 static auto f = GET_FUNC_PTR(PreZygoteForkNativeBridge);
65 return f();
66}
67
Orion Hodson9b16e342019-10-09 13:29:16 +010068bool InitializeNativeBridge(JNIEnv* env, const char* instruction_set) {
69 static auto f = GET_FUNC_PTR(InitializeNativeBridge);
70 return f(env, instruction_set);
71}
72
73void UnloadNativeBridge() {
74 static auto f = GET_FUNC_PTR(UnloadNativeBridge);
75 return f();
76}
77
78bool NativeBridgeAvailable() {
79 static auto f = GET_FUNC_PTR(NativeBridgeAvailable);
80 return f();
81}
82
83bool NativeBridgeInitialized() {
84 static auto f = GET_FUNC_PTR(NativeBridgeInitialized);
85 return f();
86}
87
88void* NativeBridgeLoadLibrary(const char* libpath, int flag) {
89 static auto f = GET_FUNC_PTR(NativeBridgeLoadLibrary);
90 return f(libpath, flag);
91}
92
93void* 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
98bool NativeBridgeIsSupported(const char* libpath) {
99 static auto f = GET_FUNC_PTR(NativeBridgeIsSupported);
100 return f(libpath);
101}
102
103uint32_t NativeBridgeGetVersion() {
104 static auto f = GET_FUNC_PTR(NativeBridgeGetVersion);
105 return f();
106}
107
108NativeBridgeSignalHandlerFn NativeBridgeGetSignalHandler(int signal) {
109 static auto f = GET_FUNC_PTR(NativeBridgeGetSignalHandler);
110 return f(signal);
111}
112
113bool NativeBridgeError() {
114 static auto f = GET_FUNC_PTR(NativeBridgeError);
115 return f();
116}
117
118bool NativeBridgeNameAcceptable(const char* native_bridge_library_filename) {
119 static auto f = GET_FUNC_PTR(NativeBridgeNameAcceptable);
120 return f(native_bridge_library_filename);
121}
122
123int NativeBridgeUnloadLibrary(void* handle) {
124 static auto f = GET_FUNC_PTR(NativeBridgeUnloadLibrary);
125 return f(handle);
126}
127
128const char* NativeBridgeGetError() {
129 static auto f = GET_FUNC_PTR(NativeBridgeGetError);
130 return f();
131}
132
133bool NativeBridgeIsPathSupported(const char* path) {
134 static auto f = GET_FUNC_PTR(NativeBridgeIsPathSupported);
135 return f(path);
136}
137
138bool 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
144struct 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
152bool 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
159void* 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
165struct 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