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