Calin Juravle | 961ae12 | 2014-08-11 16:11:59 +0100 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2014 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 | #ifndef NATIVE_BRIDGE_H_ |
| 18 | #define NATIVE_BRIDGE_H_ |
| 19 | |
Andreas Gampe | a6ac9ce | 2015-04-30 20:39:12 -0700 | [diff] [blame] | 20 | #include <signal.h> |
Nicolas Geoffray | d9b4d9b | 2019-01-10 16:27:54 +0000 | [diff] [blame] | 21 | #include <stdbool.h> |
Calin Juravle | 961ae12 | 2014-08-11 16:11:59 +0100 | [diff] [blame] | 22 | #include <stdint.h> |
jgu21 | ab0da5a | 2014-09-10 06:58:32 -0400 | [diff] [blame] | 23 | #include <sys/types.h> |
Calin Juravle | 961ae12 | 2014-08-11 16:11:59 +0100 | [diff] [blame] | 24 | |
Nicolas Geoffray | d9b4d9b | 2019-01-10 16:27:54 +0000 | [diff] [blame] | 25 | #include "jni.h" |
| 26 | |
| 27 | #ifdef __cplusplus |
Calin Juravle | 961ae12 | 2014-08-11 16:11:59 +0100 | [diff] [blame] | 28 | namespace android { |
Nicolas Geoffray | d9b4d9b | 2019-01-10 16:27:54 +0000 | [diff] [blame] | 29 | extern "C" { |
| 30 | #endif // __cplusplus |
Calin Juravle | 961ae12 | 2014-08-11 16:11:59 +0100 | [diff] [blame] | 31 | |
| 32 | struct NativeBridgeRuntimeCallbacks; |
jgu21 | ab0da5a | 2014-09-10 06:58:32 -0400 | [diff] [blame] | 33 | struct NativeBridgeRuntimeValues; |
Calin Juravle | 961ae12 | 2014-08-11 16:11:59 +0100 | [diff] [blame] | 34 | |
Andreas Gampe | a6ac9ce | 2015-04-30 20:39:12 -0700 | [diff] [blame] | 35 | // Function pointer type for sigaction. This is mostly the signature of a signal handler, except |
| 36 | // for the return type. The runtime needs to know whether the signal was handled or should be given |
| 37 | // to the chain. |
| 38 | typedef bool (*NativeBridgeSignalHandlerFn)(int, siginfo_t*, void*); |
| 39 | |
Andreas Gampe | 035bd75 | 2014-09-02 21:17:03 -0700 | [diff] [blame] | 40 | // Open the native bridge, if any. Should be called by Runtime::Init(). A null library filename |
| 41 | // signals that we do not want to load a native bridge. |
| 42 | bool LoadNativeBridge(const char* native_bridge_library_filename, |
Nicolas Geoffray | d9b4d9b | 2019-01-10 16:27:54 +0000 | [diff] [blame] | 43 | const struct NativeBridgeRuntimeCallbacks* runtime_callbacks); |
Andreas Gampe | 035bd75 | 2014-09-02 21:17:03 -0700 | [diff] [blame] | 44 | |
jgu21 | ab0da5a | 2014-09-10 06:58:32 -0400 | [diff] [blame] | 45 | // Quick check whether a native bridge will be needed. This is based off of the instruction set |
| 46 | // of the process. |
| 47 | bool NeedsNativeBridge(const char* instruction_set); |
| 48 | |
| 49 | // Do the early initialization part of the native bridge, if necessary. This should be done under |
| 50 | // high privileges. |
Calin Juravle | f9d9e2a | 2014-10-17 13:45:39 +0100 | [diff] [blame] | 51 | bool PreInitializeNativeBridge(const char* app_data_dir, const char* instruction_set); |
jgu21 | ab0da5a | 2014-09-10 06:58:32 -0400 | [diff] [blame] | 52 | |
| 53 | // Initialize the native bridge, if any. Should be called by Runtime::DidForkFromZygote. The JNIEnv* |
| 54 | // will be used to modify the app environment for the bridge. |
| 55 | bool InitializeNativeBridge(JNIEnv* env, const char* instruction_set); |
Andreas Gampe | 035bd75 | 2014-09-02 21:17:03 -0700 | [diff] [blame] | 56 | |
| 57 | // Unload the native bridge, if any. Should be called by Runtime::DidForkFromZygote. |
| 58 | void UnloadNativeBridge(); |
| 59 | |
| 60 | // Check whether a native bridge is available (opened or initialized). Requires a prior call to |
| 61 | // LoadNativeBridge. |
| 62 | bool NativeBridgeAvailable(); |
Calin Juravle | 961ae12 | 2014-08-11 16:11:59 +0100 | [diff] [blame] | 63 | |
Andreas Gampe | 049249c | 2014-08-19 22:31:31 -0700 | [diff] [blame] | 64 | // Check whether a native bridge is available (initialized). Requires a prior call to |
Andreas Gampe | 035bd75 | 2014-09-02 21:17:03 -0700 | [diff] [blame] | 65 | // LoadNativeBridge & InitializeNativeBridge. |
| 66 | bool NativeBridgeInitialized(); |
Andreas Gampe | 049249c | 2014-08-19 22:31:31 -0700 | [diff] [blame] | 67 | |
Calin Juravle | 961ae12 | 2014-08-11 16:11:59 +0100 | [diff] [blame] | 68 | // Load a shared library that is supported by the native bridge. |
Zhenhua WANG | f2804e5 | 2016-05-30 11:16:08 +0800 | [diff] [blame] | 69 | // |
| 70 | // Starting with v3, NativeBridge has two scenarios: with/without namespace. |
| 71 | // Use NativeBridgeLoadLibraryExt() instead in namespace scenario. |
Calin Juravle | 961ae12 | 2014-08-11 16:11:59 +0100 | [diff] [blame] | 72 | void* NativeBridgeLoadLibrary(const char* libpath, int flag); |
| 73 | |
| 74 | // Get a native bridge trampoline for specified native method. |
| 75 | void* NativeBridgeGetTrampoline(void* handle, const char* name, const char* shorty, uint32_t len); |
| 76 | |
Zhenhua WANG | f2804e5 | 2016-05-30 11:16:08 +0800 | [diff] [blame] | 77 | // True if native library paths are valid and is for an ABI that is supported by native bridge. |
| 78 | // The *libpath* must point to a library. |
| 79 | // |
| 80 | // Starting with v3, NativeBridge has two scenarios: with/without namespace. |
| 81 | // Use NativeBridgeIsPathSupported() instead in namespace scenario. |
Calin Juravle | 961ae12 | 2014-08-11 16:11:59 +0100 | [diff] [blame] | 82 | bool NativeBridgeIsSupported(const char* libpath); |
| 83 | |
Andreas Gampe | a6ac9ce | 2015-04-30 20:39:12 -0700 | [diff] [blame] | 84 | // Returns the version number of the native bridge. This information is available after a |
| 85 | // successful LoadNativeBridge() and before closing it, that is, as long as NativeBridgeAvailable() |
| 86 | // returns true. Returns 0 otherwise. |
| 87 | uint32_t NativeBridgeGetVersion(); |
| 88 | |
| 89 | // Returns a signal handler that the bridge would like to be managed. Only valid for a native |
| 90 | // bridge supporting the version 2 interface. Will return null if the bridge does not support |
| 91 | // version 2, or if it doesn't have a signal handler it wants to be known. |
| 92 | NativeBridgeSignalHandlerFn NativeBridgeGetSignalHandler(int signal); |
| 93 | |
Andreas Gampe | 049249c | 2014-08-19 22:31:31 -0700 | [diff] [blame] | 94 | // Returns whether we have seen a native bridge error. This could happen because the library |
| 95 | // was not found, rejected, could not be initialized and so on. |
| 96 | // |
| 97 | // This functionality is mainly for testing. |
| 98 | bool NativeBridgeError(); |
| 99 | |
| 100 | // Returns whether a given string is acceptable as a native bridge library filename. |
| 101 | // |
| 102 | // This functionality is exposed mainly for testing. |
| 103 | bool NativeBridgeNameAcceptable(const char* native_bridge_library_filename); |
| 104 | |
Zhenhua WANG | f2804e5 | 2016-05-30 11:16:08 +0800 | [diff] [blame] | 105 | // Decrements the reference count on the dynamic library handler. If the reference count drops |
dimitry | 3150f7c | 2018-09-12 01:09:19 +0200 | [diff] [blame] | 106 | // to zero then the dynamic library is unloaded. Returns 0 on success and non-zero on error. |
Zhenhua WANG | f2804e5 | 2016-05-30 11:16:08 +0800 | [diff] [blame] | 107 | int NativeBridgeUnloadLibrary(void* handle); |
| 108 | |
| 109 | // Get last error message of native bridge when fail to load library or search symbol. |
| 110 | // This is reflection of dlerror() for native bridge. |
Dimitry Ivanov | d836ab0 | 2016-11-02 18:03:10 -0700 | [diff] [blame] | 111 | const char* NativeBridgeGetError(); |
Zhenhua WANG | f2804e5 | 2016-05-30 11:16:08 +0800 | [diff] [blame] | 112 | |
| 113 | struct native_bridge_namespace_t; |
| 114 | |
| 115 | // True if native library paths are valid and is for an ABI that is supported by native bridge. |
| 116 | // Different from NativeBridgeIsSupported(), the *path* here must be a directory containing |
| 117 | // libraries of an ABI. |
| 118 | // |
| 119 | // Starting with v3, NativeBridge has two scenarios: with/without namespace. |
| 120 | // Use NativeBridgeIsSupported() instead in non-namespace scenario. |
| 121 | bool NativeBridgeIsPathSupported(const char* path); |
| 122 | |
Zhenhua WANG | e8fb11d | 2017-02-27 10:14:45 +0800 | [diff] [blame] | 123 | // Initializes anonymous namespace. |
| 124 | // NativeBridge's peer of android_init_anonymous_namespace() of dynamic linker. |
| 125 | // |
| 126 | // The anonymous namespace is used in the case when a NativeBridge implementation |
| 127 | // cannot identify the caller of dlopen/dlsym which happens for the code not loaded |
| 128 | // by dynamic linker; for example calls from the mono-compiled code. |
Zhenhua WANG | f2804e5 | 2016-05-30 11:16:08 +0800 | [diff] [blame] | 129 | // |
| 130 | // Starting with v3, NativeBridge has two scenarios: with/without namespace. |
| 131 | // Should not use in non-namespace scenario. |
Zhenhua WANG | e8fb11d | 2017-02-27 10:14:45 +0800 | [diff] [blame] | 132 | bool NativeBridgeInitAnonymousNamespace(const char* public_ns_sonames, |
| 133 | const char* anon_ns_library_path); |
Zhenhua WANG | f2804e5 | 2016-05-30 11:16:08 +0800 | [diff] [blame] | 134 | |
Zhenhua WANG | e8fb11d | 2017-02-27 10:14:45 +0800 | [diff] [blame] | 135 | // Create new namespace in which native libraries will be loaded. |
| 136 | // NativeBridge's peer of android_create_namespace() of dynamic linker. |
| 137 | // |
| 138 | // The libraries in the namespace are searched by folowing order: |
| 139 | // 1. ld_library_path (Think of this as namespace-local LD_LIBRARY_PATH) |
| 140 | // 2. In directories specified by DT_RUNPATH of the "needed by" binary. |
| 141 | // 3. deault_library_path (This of this as namespace-local default library path) |
Zhenhua WANG | f2804e5 | 2016-05-30 11:16:08 +0800 | [diff] [blame] | 142 | // |
| 143 | // Starting with v3, NativeBridge has two scenarios: with/without namespace. |
| 144 | // Should not use in non-namespace scenario. |
Nicolas Geoffray | d9b4d9b | 2019-01-10 16:27:54 +0000 | [diff] [blame] | 145 | struct native_bridge_namespace_t* NativeBridgeCreateNamespace( |
| 146 | const char* name, const char* ld_library_path, const char* default_library_path, uint64_t type, |
| 147 | const char* permitted_when_isolated_path, struct native_bridge_namespace_t* parent_ns); |
Zhenhua WANG | f2804e5 | 2016-05-30 11:16:08 +0800 | [diff] [blame] | 148 | |
Zhenhua WANG | e8fb11d | 2017-02-27 10:14:45 +0800 | [diff] [blame] | 149 | // Creates a link which shares some libraries from one namespace to another. |
| 150 | // NativeBridge's peer of android_link_namespaces() of dynamic linker. |
| 151 | // |
| 152 | // Starting with v3, NativeBridge has two scenarios: with/without namespace. |
| 153 | // Should not use in non-namespace scenario. |
Nicolas Geoffray | d9b4d9b | 2019-01-10 16:27:54 +0000 | [diff] [blame] | 154 | bool NativeBridgeLinkNamespaces(struct native_bridge_namespace_t* from, |
| 155 | struct native_bridge_namespace_t* to, |
Zhenhua WANG | e8fb11d | 2017-02-27 10:14:45 +0800 | [diff] [blame] | 156 | const char* shared_libs_sonames); |
| 157 | |
Zhenhua WANG | f2804e5 | 2016-05-30 11:16:08 +0800 | [diff] [blame] | 158 | // Load a shared library with namespace key that is supported by the native bridge. |
Zhenhua WANG | e8fb11d | 2017-02-27 10:14:45 +0800 | [diff] [blame] | 159 | // NativeBridge's peer of android_dlopen_ext() of dynamic linker, only supports namespace |
| 160 | // extension. |
Zhenhua WANG | f2804e5 | 2016-05-30 11:16:08 +0800 | [diff] [blame] | 161 | // |
| 162 | // Starting with v3, NativeBridge has two scenarios: with/without namespace. |
| 163 | // Use NativeBridgeLoadLibrary() instead in non-namespace scenario. |
Nicolas Geoffray | d9b4d9b | 2019-01-10 16:27:54 +0000 | [diff] [blame] | 164 | void* NativeBridgeLoadLibraryExt(const char* libpath, int flag, |
| 165 | struct native_bridge_namespace_t* ns); |
Zhenhua WANG | f2804e5 | 2016-05-30 11:16:08 +0800 | [diff] [blame] | 166 | |
dimitry | d2ace38 | 2019-02-04 15:06:43 +0100 | [diff] [blame] | 167 | // Returns exported namespace by the name. This is a reflection of |
| 168 | // android_get_exported_namespace function. Introduced in v5. |
| 169 | struct native_bridge_namespace_t* NativeBridgeGetExportedNamespace(const char* name); |
Dimitry Ivanov | 882cad2 | 2017-05-01 15:12:49 -0700 | [diff] [blame] | 170 | |
Calin Juravle | 961ae12 | 2014-08-11 16:11:59 +0100 | [diff] [blame] | 171 | // Native bridge interfaces to runtime. |
| 172 | struct NativeBridgeCallbacks { |
jgu21 | ab0da5a | 2014-09-10 06:58:32 -0400 | [diff] [blame] | 173 | // Version number of the interface. |
| 174 | uint32_t version; |
| 175 | |
Calin Juravle | 961ae12 | 2014-08-11 16:11:59 +0100 | [diff] [blame] | 176 | // Initialize native bridge. Native bridge's internal implementation must ensure MT safety and |
| 177 | // that the native bridge is initialized only once. Thus it is OK to call this interface for an |
| 178 | // already initialized native bridge. |
| 179 | // |
| 180 | // Parameters: |
| 181 | // runtime_cbs [IN] the pointer to NativeBridgeRuntimeCallbacks. |
| 182 | // Returns: |
Zhenhua WANG | e8fb11d | 2017-02-27 10:14:45 +0800 | [diff] [blame] | 183 | // true if initialization was successful. |
Nicolas Geoffray | d9b4d9b | 2019-01-10 16:27:54 +0000 | [diff] [blame] | 184 | bool (*initialize)(const struct NativeBridgeRuntimeCallbacks* runtime_cbs, |
| 185 | const char* private_dir, const char* instruction_set); |
Calin Juravle | 961ae12 | 2014-08-11 16:11:59 +0100 | [diff] [blame] | 186 | |
| 187 | // Load a shared library that is supported by the native bridge. |
| 188 | // |
| 189 | // Parameters: |
| 190 | // libpath [IN] path to the shared library |
| 191 | // flag [IN] the stardard RTLD_XXX defined in bionic dlfcn.h |
| 192 | // Returns: |
| 193 | // The opaque handle of the shared library if sucessful, otherwise NULL |
Zhenhua WANG | f2804e5 | 2016-05-30 11:16:08 +0800 | [diff] [blame] | 194 | // |
| 195 | // Starting with v3, NativeBridge has two scenarios: with/without namespace. |
| 196 | // Use loadLibraryExt instead in namespace scenario. |
Calin Juravle | 961ae12 | 2014-08-11 16:11:59 +0100 | [diff] [blame] | 197 | void* (*loadLibrary)(const char* libpath, int flag); |
| 198 | |
| 199 | // Get a native bridge trampoline for specified native method. The trampoline has same |
| 200 | // sigature as the native method. |
| 201 | // |
| 202 | // Parameters: |
| 203 | // handle [IN] the handle returned from loadLibrary |
| 204 | // shorty [IN] short descriptor of native method |
| 205 | // len [IN] length of shorty |
| 206 | // Returns: |
| 207 | // address of trampoline if successful, otherwise NULL |
| 208 | void* (*getTrampoline)(void* handle, const char* name, const char* shorty, uint32_t len); |
| 209 | |
| 210 | // Check whether native library is valid and is for an ABI that is supported by native bridge. |
| 211 | // |
| 212 | // Parameters: |
| 213 | // libpath [IN] path to the shared library |
| 214 | // Returns: |
| 215 | // TRUE if library is supported by native bridge, FALSE otherwise |
Zhenhua WANG | f2804e5 | 2016-05-30 11:16:08 +0800 | [diff] [blame] | 216 | // |
| 217 | // Starting with v3, NativeBridge has two scenarios: with/without namespace. |
| 218 | // Use isPathSupported instead in namespace scenario. |
Calin Juravle | 961ae12 | 2014-08-11 16:11:59 +0100 | [diff] [blame] | 219 | bool (*isSupported)(const char* libpath); |
jgu21 | ab0da5a | 2014-09-10 06:58:32 -0400 | [diff] [blame] | 220 | |
| 221 | // Provide environment values required by the app running with native bridge according to the |
| 222 | // instruction set. |
| 223 | // |
| 224 | // Parameters: |
Zhenhua WANG | e8fb11d | 2017-02-27 10:14:45 +0800 | [diff] [blame] | 225 | // instruction_set [IN] the instruction set of the app |
jgu21 | ab0da5a | 2014-09-10 06:58:32 -0400 | [diff] [blame] | 226 | // Returns: |
Zhenhua WANG | e8fb11d | 2017-02-27 10:14:45 +0800 | [diff] [blame] | 227 | // NULL if not supported by native bridge. |
| 228 | // Otherwise, return all environment values to be set after fork. |
jgu21 | ab0da5a | 2014-09-10 06:58:32 -0400 | [diff] [blame] | 229 | const struct NativeBridgeRuntimeValues* (*getAppEnv)(const char* instruction_set); |
Andreas Gampe | a6ac9ce | 2015-04-30 20:39:12 -0700 | [diff] [blame] | 230 | |
| 231 | // Added callbacks in version 2. |
| 232 | |
| 233 | // Check whether the bridge is compatible with the given version. A bridge may decide not to be |
| 234 | // forwards- or backwards-compatible, and libnativebridge will then stop using it. |
| 235 | // |
| 236 | // Parameters: |
Zhenhua WANG | e8fb11d | 2017-02-27 10:14:45 +0800 | [diff] [blame] | 237 | // bridge_version [IN] the version of libnativebridge. |
Andreas Gampe | a6ac9ce | 2015-04-30 20:39:12 -0700 | [diff] [blame] | 238 | // Returns: |
Zhenhua WANG | e8fb11d | 2017-02-27 10:14:45 +0800 | [diff] [blame] | 239 | // true if the native bridge supports the given version of libnativebridge. |
Andreas Gampe | a6ac9ce | 2015-04-30 20:39:12 -0700 | [diff] [blame] | 240 | bool (*isCompatibleWith)(uint32_t bridge_version); |
| 241 | |
| 242 | // A callback to retrieve a native bridge's signal handler for the specified signal. The runtime |
| 243 | // will ensure that the signal handler is being called after the runtime's own handler, but before |
| 244 | // all chained handlers. The native bridge should not try to install the handler by itself, as |
| 245 | // that will potentially lead to cycles. |
| 246 | // |
| 247 | // Parameters: |
Zhenhua WANG | e8fb11d | 2017-02-27 10:14:45 +0800 | [diff] [blame] | 248 | // signal [IN] the signal for which the handler is asked for. Currently, only SIGSEGV is |
Andreas Gampe | a6ac9ce | 2015-04-30 20:39:12 -0700 | [diff] [blame] | 249 | // supported by the runtime. |
| 250 | // Returns: |
Zhenhua WANG | e8fb11d | 2017-02-27 10:14:45 +0800 | [diff] [blame] | 251 | // NULL if the native bridge doesn't use a handler or doesn't want it to be managed by the |
| 252 | // runtime. |
| 253 | // Otherwise, a pointer to the signal handler. |
Andreas Gampe | a6ac9ce | 2015-04-30 20:39:12 -0700 | [diff] [blame] | 254 | NativeBridgeSignalHandlerFn (*getSignalHandler)(int signal); |
Zhenhua WANG | f2804e5 | 2016-05-30 11:16:08 +0800 | [diff] [blame] | 255 | |
| 256 | // Added callbacks in version 3. |
| 257 | |
| 258 | // Decrements the reference count on the dynamic library handler. If the reference count drops |
| 259 | // to zero then the dynamic library is unloaded. |
| 260 | // |
| 261 | // Parameters: |
Zhenhua WANG | e8fb11d | 2017-02-27 10:14:45 +0800 | [diff] [blame] | 262 | // handle [IN] the handler of a dynamic library. |
Zhenhua WANG | f2804e5 | 2016-05-30 11:16:08 +0800 | [diff] [blame] | 263 | // |
| 264 | // Returns: |
| 265 | // 0 on success, and nonzero on error. |
| 266 | int (*unloadLibrary)(void* handle); |
| 267 | |
| 268 | // Dump the last failure message of native bridge when fail to load library or search symbol. |
| 269 | // |
| 270 | // Parameters: |
| 271 | // |
| 272 | // Returns: |
| 273 | // A string describing the most recent error that occurred when load library |
| 274 | // or lookup symbol via native bridge. |
Dimitry Ivanov | d836ab0 | 2016-11-02 18:03:10 -0700 | [diff] [blame] | 275 | const char* (*getError)(); |
Zhenhua WANG | f2804e5 | 2016-05-30 11:16:08 +0800 | [diff] [blame] | 276 | |
| 277 | // Check whether library paths are supported by native bridge. |
| 278 | // |
| 279 | // Parameters: |
| 280 | // library_path [IN] search paths for native libraries (directories separated by ':') |
| 281 | // Returns: |
| 282 | // TRUE if libraries within search paths are supported by native bridge, FALSE otherwise |
| 283 | // |
| 284 | // Starting with v3, NativeBridge has two scenarios: with/without namespace. |
| 285 | // Use isSupported instead in non-namespace scenario. |
| 286 | bool (*isPathSupported)(const char* library_path); |
| 287 | |
Zhenhua WANG | e8fb11d | 2017-02-27 10:14:45 +0800 | [diff] [blame] | 288 | // Initializes anonymous namespace at native bridge side. |
| 289 | // NativeBridge's peer of android_init_anonymous_namespace() of dynamic linker. |
| 290 | // |
| 291 | // The anonymous namespace is used in the case when a NativeBridge implementation |
| 292 | // cannot identify the caller of dlopen/dlsym which happens for the code not loaded |
| 293 | // by dynamic linker; for example calls from the mono-compiled code. |
Zhenhua WANG | f2804e5 | 2016-05-30 11:16:08 +0800 | [diff] [blame] | 294 | // |
| 295 | // Parameters: |
Zhenhua WANG | e8fb11d | 2017-02-27 10:14:45 +0800 | [diff] [blame] | 296 | // public_ns_sonames [IN] the name of "public" libraries. |
| 297 | // anon_ns_library_path [IN] the library search path of (anonymous) namespace. |
Zhenhua WANG | f2804e5 | 2016-05-30 11:16:08 +0800 | [diff] [blame] | 298 | // Returns: |
Zhenhua WANG | e8fb11d | 2017-02-27 10:14:45 +0800 | [diff] [blame] | 299 | // true if the pass is ok. |
| 300 | // Otherwise, false. |
Zhenhua WANG | f2804e5 | 2016-05-30 11:16:08 +0800 | [diff] [blame] | 301 | // |
| 302 | // Starting with v3, NativeBridge has two scenarios: with/without namespace. |
| 303 | // Should not use in non-namespace scenario. |
Zhenhua WANG | e8fb11d | 2017-02-27 10:14:45 +0800 | [diff] [blame] | 304 | bool (*initAnonymousNamespace)(const char* public_ns_sonames, const char* anon_ns_library_path); |
Zhenhua WANG | f2804e5 | 2016-05-30 11:16:08 +0800 | [diff] [blame] | 305 | |
Zhenhua WANG | e8fb11d | 2017-02-27 10:14:45 +0800 | [diff] [blame] | 306 | // Create new namespace in which native libraries will be loaded. |
| 307 | // NativeBridge's peer of android_create_namespace() of dynamic linker. |
Zhenhua WANG | f2804e5 | 2016-05-30 11:16:08 +0800 | [diff] [blame] | 308 | // |
| 309 | // Parameters: |
Zhenhua WANG | e8fb11d | 2017-02-27 10:14:45 +0800 | [diff] [blame] | 310 | // name [IN] the name of the namespace. |
| 311 | // ld_library_path [IN] the first set of library search paths of the namespace. |
| 312 | // default_library_path [IN] the second set of library search path of the namespace. |
| 313 | // type [IN] the attribute of the namespace. |
| 314 | // permitted_when_isolated_path [IN] the permitted path for isolated namespace(if it is). |
| 315 | // parent_ns [IN] the pointer of the parent namespace to be inherited from. |
Zhenhua WANG | f2804e5 | 2016-05-30 11:16:08 +0800 | [diff] [blame] | 316 | // Returns: |
Zhenhua WANG | e8fb11d | 2017-02-27 10:14:45 +0800 | [diff] [blame] | 317 | // native_bridge_namespace_t* for created namespace or nullptr in the case of error. |
Zhenhua WANG | f2804e5 | 2016-05-30 11:16:08 +0800 | [diff] [blame] | 318 | // |
| 319 | // Starting with v3, NativeBridge has two scenarios: with/without namespace. |
| 320 | // Should not use in non-namespace scenario. |
Nicolas Geoffray | d9b4d9b | 2019-01-10 16:27:54 +0000 | [diff] [blame] | 321 | struct native_bridge_namespace_t* (*createNamespace)(const char* name, |
| 322 | const char* ld_library_path, |
| 323 | const char* default_library_path, |
| 324 | uint64_t type, |
| 325 | const char* permitted_when_isolated_path, |
| 326 | struct native_bridge_namespace_t* parent_ns); |
Zhenhua WANG | f2804e5 | 2016-05-30 11:16:08 +0800 | [diff] [blame] | 327 | |
Zhenhua WANG | e8fb11d | 2017-02-27 10:14:45 +0800 | [diff] [blame] | 328 | // Creates a link which shares some libraries from one namespace to another. |
| 329 | // NativeBridge's peer of android_link_namespaces() of dynamic linker. |
| 330 | // |
| 331 | // Parameters: |
| 332 | // from [IN] the namespace where libraries are accessed. |
| 333 | // to [IN] the namespace where libraries are loaded. |
| 334 | // shared_libs_sonames [IN] the libraries to be shared. |
| 335 | // |
| 336 | // Returns: |
| 337 | // Whether successed or not. |
| 338 | // |
| 339 | // Starting with v3, NativeBridge has two scenarios: with/without namespace. |
| 340 | // Should not use in non-namespace scenario. |
Nicolas Geoffray | d9b4d9b | 2019-01-10 16:27:54 +0000 | [diff] [blame] | 341 | bool (*linkNamespaces)(struct native_bridge_namespace_t* from, |
| 342 | struct native_bridge_namespace_t* to, const char* shared_libs_sonames); |
Zhenhua WANG | e8fb11d | 2017-02-27 10:14:45 +0800 | [diff] [blame] | 343 | |
Zhenhua WANG | f2804e5 | 2016-05-30 11:16:08 +0800 | [diff] [blame] | 344 | // Load a shared library within a namespace. |
Zhenhua WANG | e8fb11d | 2017-02-27 10:14:45 +0800 | [diff] [blame] | 345 | // NativeBridge's peer of android_dlopen_ext() of dynamic linker, only supports namespace |
| 346 | // extension. |
Zhenhua WANG | f2804e5 | 2016-05-30 11:16:08 +0800 | [diff] [blame] | 347 | // |
| 348 | // Parameters: |
| 349 | // libpath [IN] path to the shared library |
| 350 | // flag [IN] the stardard RTLD_XXX defined in bionic dlfcn.h |
| 351 | // ns [IN] the pointer of the namespace in which the library should be loaded. |
| 352 | // Returns: |
| 353 | // The opaque handle of the shared library if sucessful, otherwise NULL |
| 354 | // |
| 355 | // Starting with v3, NativeBridge has two scenarios: with/without namespace. |
| 356 | // Use loadLibrary instead in non-namespace scenario. |
Nicolas Geoffray | d9b4d9b | 2019-01-10 16:27:54 +0000 | [diff] [blame] | 357 | void* (*loadLibraryExt)(const char* libpath, int flag, struct native_bridge_namespace_t* ns); |
Dimitry Ivanov | 882cad2 | 2017-05-01 15:12:49 -0700 | [diff] [blame] | 358 | |
| 359 | // Get native bridge version of vendor namespace. |
| 360 | // The vendor namespace is the namespace used to load vendor public libraries. |
| 361 | // With O release this namespace can be different from the default namespace. |
| 362 | // For the devices without enable vendor namespaces this function should return null |
| 363 | // |
| 364 | // Returns: |
| 365 | // vendor namespace or null if it was not set up for the device |
dimitry | d2ace38 | 2019-02-04 15:06:43 +0100 | [diff] [blame] | 366 | // |
| 367 | // Starting with v5 (Android Q) this function is no longer used. |
| 368 | // Use getExportedNamespace() below. |
Nicolas Geoffray | d9b4d9b | 2019-01-10 16:27:54 +0000 | [diff] [blame] | 369 | struct native_bridge_namespace_t* (*getVendorNamespace)(); |
dimitry | d2ace38 | 2019-02-04 15:06:43 +0100 | [diff] [blame] | 370 | |
| 371 | // Get native bridge version of exported namespace. Peer of |
| 372 | // android_get_exported_namespace(const char*) function. |
| 373 | // |
| 374 | // Returns: |
| 375 | // exported namespace or null if it was not set up for the device |
| 376 | struct native_bridge_namespace_t* (*getExportedNamespace)(const char* name); |
Calin Juravle | 961ae12 | 2014-08-11 16:11:59 +0100 | [diff] [blame] | 377 | }; |
| 378 | |
| 379 | // Runtime interfaces to native bridge. |
| 380 | struct NativeBridgeRuntimeCallbacks { |
| 381 | // Get shorty of a Java method. The shorty is supposed to be persistent in memory. |
| 382 | // |
| 383 | // Parameters: |
| 384 | // env [IN] pointer to JNIenv. |
| 385 | // mid [IN] Java methodID. |
| 386 | // Returns: |
| 387 | // short descriptor for method. |
| 388 | const char* (*getMethodShorty)(JNIEnv* env, jmethodID mid); |
| 389 | |
| 390 | // Get number of native methods for specified class. |
| 391 | // |
| 392 | // Parameters: |
| 393 | // env [IN] pointer to JNIenv. |
| 394 | // clazz [IN] Java class object. |
| 395 | // Returns: |
| 396 | // number of native methods. |
| 397 | uint32_t (*getNativeMethodCount)(JNIEnv* env, jclass clazz); |
| 398 | |
| 399 | // Get at most 'method_count' native methods for specified class 'clazz'. Results are outputed |
| 400 | // via 'methods' [OUT]. The signature pointer in JNINativeMethod is reused as the method shorty. |
| 401 | // |
| 402 | // Parameters: |
| 403 | // env [IN] pointer to JNIenv. |
| 404 | // clazz [IN] Java class object. |
| 405 | // methods [OUT] array of method with the name, shorty, and fnPtr. |
| 406 | // method_count [IN] max number of elements in methods. |
| 407 | // Returns: |
| 408 | // number of method it actually wrote to methods. |
| 409 | uint32_t (*getNativeMethods)(JNIEnv* env, jclass clazz, JNINativeMethod* methods, |
| 410 | uint32_t method_count); |
| 411 | }; |
| 412 | |
Nicolas Geoffray | d9b4d9b | 2019-01-10 16:27:54 +0000 | [diff] [blame] | 413 | #ifdef __cplusplus |
| 414 | } // extern "C" |
| 415 | } // namespace android |
| 416 | #endif // __cplusplus |
Calin Juravle | 961ae12 | 2014-08-11 16:11:59 +0100 | [diff] [blame] | 417 | |
| 418 | #endif // NATIVE_BRIDGE_H_ |