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