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