Orion Hodson | 9b16e34 | 2019-10-09 13:29:16 +0100 | [diff] [blame] | 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 | |
| 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> |
| 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 | |
| 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 | |
| 53 | bool NeedsNativeBridge(const char* instruction_set) { |
| 54 | static auto f = GET_FUNC_PTR(NeedsNativeBridge); |
| 55 | return f(instruction_set); |
| 56 | } |
| 57 | |
| 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 | |
Lev Rumyantsev | abafbe7 | 2019-12-13 15:49:37 -0800 | [diff] [blame] | 63 | void PreZygoteForkNativeBridge() { |
| 64 | static auto f = GET_FUNC_PTR(PreZygoteForkNativeBridge); |
| 65 | return f(); |
| 66 | } |
| 67 | |
Orion Hodson | 9b16e34 | 2019-10-09 13:29:16 +0100 | [diff] [blame] | 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 | |
| 73 | void UnloadNativeBridge() { |
| 74 | static auto f = GET_FUNC_PTR(UnloadNativeBridge); |
| 75 | return f(); |
| 76 | } |
| 77 | |
| 78 | bool NativeBridgeAvailable() { |
| 79 | static auto f = GET_FUNC_PTR(NativeBridgeAvailable); |
| 80 | return f(); |
| 81 | } |
| 82 | |
| 83 | bool NativeBridgeInitialized() { |
| 84 | static auto f = GET_FUNC_PTR(NativeBridgeInitialized); |
| 85 | return f(); |
| 86 | } |
| 87 | |
| 88 | void* NativeBridgeLoadLibrary(const char* libpath, int flag) { |
| 89 | static auto f = GET_FUNC_PTR(NativeBridgeLoadLibrary); |
| 90 | return f(libpath, flag); |
| 91 | } |
| 92 | |
| 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 | |
| 98 | bool NativeBridgeIsSupported(const char* libpath) { |
| 99 | static auto f = GET_FUNC_PTR(NativeBridgeIsSupported); |
| 100 | return f(libpath); |
| 101 | } |
| 102 | |
| 103 | uint32_t NativeBridgeGetVersion() { |
| 104 | static auto f = GET_FUNC_PTR(NativeBridgeGetVersion); |
| 105 | return f(); |
| 106 | } |
| 107 | |
| 108 | NativeBridgeSignalHandlerFn NativeBridgeGetSignalHandler(int signal) { |
| 109 | static auto f = GET_FUNC_PTR(NativeBridgeGetSignalHandler); |
| 110 | return f(signal); |
| 111 | } |
| 112 | |
| 113 | bool NativeBridgeError() { |
| 114 | static auto f = GET_FUNC_PTR(NativeBridgeError); |
| 115 | return f(); |
| 116 | } |
| 117 | |
| 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 | |
| 123 | int NativeBridgeUnloadLibrary(void* handle) { |
| 124 | static auto f = GET_FUNC_PTR(NativeBridgeUnloadLibrary); |
| 125 | return f(handle); |
| 126 | } |
| 127 | |
| 128 | const char* NativeBridgeGetError() { |
| 129 | static auto f = GET_FUNC_PTR(NativeBridgeGetError); |
| 130 | return f(); |
| 131 | } |
| 132 | |
| 133 | bool NativeBridgeIsPathSupported(const char* path) { |
| 134 | static auto f = GET_FUNC_PTR(NativeBridgeIsPathSupported); |
| 135 | return f(path); |
| 136 | } |
| 137 | |
| 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 | |
| 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 | |
| 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 | |
| 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 | |
| 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 |