blob: f63497bd292659aeea2182ab05ddca413ebd3e63 [file] [log] [blame]
Calin Juravle961ae122014-08-11 16:11:59 +01001/*
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#include "nativebridge/native_bridge.h"
18
jgu21ab0da5a2014-09-10 06:58:32 -040019#include <cstring>
Andreas Gampe049249c2014-08-19 22:31:31 -070020#include <cutils/log.h>
Calin Juravle961ae122014-08-11 16:11:59 +010021#include <dlfcn.h>
jgu21ab0da5a2014-09-10 06:58:32 -040022#include <errno.h>
23#include <fcntl.h>
Calin Juravle961ae122014-08-11 16:11:59 +010024#include <stdio.h>
jgu21ab0da5a2014-09-10 06:58:32 -040025#include <sys/mount.h>
26#include <sys/stat.h>
Calin Juravle961ae122014-08-11 16:11:59 +010027
28
29namespace android {
30
jgu21ab0da5a2014-09-10 06:58:32 -040031// Environment values required by the apps running with native bridge.
32struct NativeBridgeRuntimeValues {
33 const char* os_arch;
34 const char* cpu_abi;
35 const char* cpu_abi2;
36 const char* *supported_abis;
37 int32_t abi_count;
38};
39
Calin Juravle961ae122014-08-11 16:11:59 +010040// The symbol name exposed by native-bridge with the type of NativeBridgeCallbacks.
41static constexpr const char* kNativeBridgeInterfaceSymbol = "NativeBridgeItf";
42
Andreas Gampe035bd752014-09-02 21:17:03 -070043enum class NativeBridgeState {
44 kNotSetup, // Initial state.
45 kOpened, // After successful dlopen.
Calin Juravlef9d9e2a2014-10-17 13:45:39 +010046 kPreInitialized, // After successful pre-initialization.
Andreas Gampe035bd752014-09-02 21:17:03 -070047 kInitialized, // After successful initialization.
48 kClosed // Closed or errors.
49};
Calin Juravle961ae122014-08-11 16:11:59 +010050
Calin Juravlef9d9e2a2014-10-17 13:45:39 +010051static constexpr const char* kNotSetupString = "kNotSetup";
52static constexpr const char* kOpenedString = "kOpened";
53static constexpr const char* kPreInitializedString = "kPreInitialized";
54static constexpr const char* kInitializedString = "kInitialized";
55static constexpr const char* kClosedString = "kClosed";
Andreas Gampe035bd752014-09-02 21:17:03 -070056
57static const char* GetNativeBridgeStateString(NativeBridgeState state) {
58 switch (state) {
59 case NativeBridgeState::kNotSetup:
60 return kNotSetupString;
61
62 case NativeBridgeState::kOpened:
63 return kOpenedString;
64
Calin Juravlef9d9e2a2014-10-17 13:45:39 +010065 case NativeBridgeState::kPreInitialized:
66 return kPreInitializedString;
67
Andreas Gampe035bd752014-09-02 21:17:03 -070068 case NativeBridgeState::kInitialized:
69 return kInitializedString;
70
71 case NativeBridgeState::kClosed:
72 return kClosedString;
73 }
74}
75
76// Current state of the native bridge.
77static NativeBridgeState state = NativeBridgeState::kNotSetup;
78
Andreas Gampe049249c2014-08-19 22:31:31 -070079// Whether we had an error at some point.
80static bool had_error = false;
Calin Juravle961ae122014-08-11 16:11:59 +010081
Andreas Gampe035bd752014-09-02 21:17:03 -070082// Handle of the loaded library.
83static void* native_bridge_handle = nullptr;
84// Pointer to the callbacks. Available as soon as LoadNativeBridge succeeds, but only initialized
85// later.
Andreas Gampea6ac9ce2015-04-30 20:39:12 -070086static const NativeBridgeCallbacks* callbacks = nullptr;
Andreas Gampe035bd752014-09-02 21:17:03 -070087// Callbacks provided by the environment to the bridge. Passed to LoadNativeBridge.
Calin Juravle961ae122014-08-11 16:11:59 +010088static const NativeBridgeRuntimeCallbacks* runtime_callbacks = nullptr;
89
Calin Juravlef9d9e2a2014-10-17 13:45:39 +010090// The app's code cache directory.
91static char* app_code_cache_dir = nullptr;
92
93// Code cache directory (relative to the application private directory)
94// Ideally we'd like to call into framework to retrieve this name. However that's considered an
95// implementation detail and will require either hacks or consistent refactorings. We compromise
96// and hard code the directory name again here.
97static constexpr const char* kCodeCacheDir = "code_cache";
jgu21ab0da5a2014-09-10 06:58:32 -040098
Andreas Gampea6ac9ce2015-04-30 20:39:12 -070099static constexpr uint32_t kLibNativeBridgeVersion = 2;
jgu21ab0da5a2014-09-10 06:58:32 -0400100
Andreas Gampe049249c2014-08-19 22:31:31 -0700101// Characters allowed in a native bridge filename. The first character must
102// be in [a-zA-Z] (expected 'l' for "libx"). The rest must be in [a-zA-Z0-9._-].
103static bool CharacterAllowed(char c, bool first) {
104 if (first) {
105 return ('a' <= c && c <= 'z') || ('A' <= c && c <= 'Z');
106 } else {
107 return ('a' <= c && c <= 'z') || ('A' <= c && c <= 'Z') || ('0' <= c && c <= '9') ||
108 (c == '.') || (c == '_') || (c == '-');
109 }
110}
111
112// We only allow simple names for the library. It is supposed to be a file in
113// /system/lib or /vendor/lib. Only allow a small range of characters, that is
114// names consisting of [a-zA-Z0-9._-] and starting with [a-zA-Z].
115bool NativeBridgeNameAcceptable(const char* nb_library_filename) {
116 const char* ptr = nb_library_filename;
117 if (*ptr == 0) {
118 // Emptry string. Allowed, means no native bridge.
119 return true;
120 } else {
121 // First character must be [a-zA-Z].
122 if (!CharacterAllowed(*ptr, true)) {
123 // Found an invalid fist character, don't accept.
Andreas Gampea6ac9ce2015-04-30 20:39:12 -0700124 ALOGE("Native bridge library %s has been rejected for first character %c",
125 nb_library_filename,
126 *ptr);
Andreas Gampe049249c2014-08-19 22:31:31 -0700127 return false;
128 } else {
129 // For the rest, be more liberal.
130 ptr++;
131 while (*ptr != 0) {
132 if (!CharacterAllowed(*ptr, false)) {
133 // Found an invalid character, don't accept.
134 ALOGE("Native bridge library %s has been rejected for %c", nb_library_filename, *ptr);
135 return false;
136 }
137 ptr++;
138 }
139 }
140 return true;
141 }
142}
143
Andreas Gampea6ac9ce2015-04-30 20:39:12 -0700144static bool VersionCheck(const NativeBridgeCallbacks* cb) {
145 // Libnativebridge is now designed to be forward-compatible. So only "0" is an unsupported
146 // version.
147 if (cb == nullptr || cb->version == 0) {
148 return false;
149 }
150
151 // If this is a v2+ bridge, it may not be forwards- or backwards-compatible. Check.
152 if (cb->version >= 2) {
153 if (!callbacks->isCompatibleWith(kLibNativeBridgeVersion)) {
154 // TODO: Scan which version is supported, and fall back to handle it.
155 return false;
156 }
157 }
158
159 return true;
jgu21ab0da5a2014-09-10 06:58:32 -0400160}
161
Calin Juravlef9d9e2a2014-10-17 13:45:39 +0100162static void CloseNativeBridge(bool with_error) {
163 state = NativeBridgeState::kClosed;
164 had_error |= with_error;
165 delete[] app_code_cache_dir;
166 app_code_cache_dir = nullptr;
167}
168
Andreas Gampe035bd752014-09-02 21:17:03 -0700169bool LoadNativeBridge(const char* nb_library_filename,
170 const NativeBridgeRuntimeCallbacks* runtime_cbs) {
171 // We expect only one place that calls LoadNativeBridge: Runtime::Init. At that point we are not
172 // multi-threaded, so we do not need locking here.
Calin Juravle961ae122014-08-11 16:11:59 +0100173
Andreas Gampe035bd752014-09-02 21:17:03 -0700174 if (state != NativeBridgeState::kNotSetup) {
Andreas Gampe049249c2014-08-19 22:31:31 -0700175 // Setup has been called before. Ignore this call.
jgu21ab0da5a2014-09-10 06:58:32 -0400176 if (nb_library_filename != nullptr) { // Avoids some log-spam for dalvikvm.
177 ALOGW("Called LoadNativeBridge for an already set up native bridge. State is %s.",
178 GetNativeBridgeStateString(state));
179 }
Andreas Gampe049249c2014-08-19 22:31:31 -0700180 // Note: counts as an error, even though the bridge may be functional.
181 had_error = true;
Andreas Gampe049249c2014-08-19 22:31:31 -0700182 return false;
183 }
184
Andreas Gampe035bd752014-09-02 21:17:03 -0700185 if (nb_library_filename == nullptr || *nb_library_filename == 0) {
Calin Juravlef9d9e2a2014-10-17 13:45:39 +0100186 CloseNativeBridge(false);
187 return false;
Andreas Gampe035bd752014-09-02 21:17:03 -0700188 } else {
189 if (!NativeBridgeNameAcceptable(nb_library_filename)) {
Calin Juravlef9d9e2a2014-10-17 13:45:39 +0100190 CloseNativeBridge(true);
Andreas Gampe035bd752014-09-02 21:17:03 -0700191 } else {
192 // Try to open the library.
193 void* handle = dlopen(nb_library_filename, RTLD_LAZY);
194 if (handle != nullptr) {
195 callbacks = reinterpret_cast<NativeBridgeCallbacks*>(dlsym(handle,
196 kNativeBridgeInterfaceSymbol));
197 if (callbacks != nullptr) {
jgu21ab0da5a2014-09-10 06:58:32 -0400198 if (VersionCheck(callbacks)) {
199 // Store the handle for later.
200 native_bridge_handle = handle;
201 } else {
202 callbacks = nullptr;
203 dlclose(handle);
204 ALOGW("Unsupported native bridge interface.");
205 }
Andreas Gampe035bd752014-09-02 21:17:03 -0700206 } else {
207 dlclose(handle);
208 }
209 }
210
211 // Two failure conditions: could not find library (dlopen failed), or could not find native
212 // bridge interface (dlsym failed). Both are an error and close the native bridge.
213 if (callbacks == nullptr) {
Calin Juravlef9d9e2a2014-10-17 13:45:39 +0100214 CloseNativeBridge(true);
Andreas Gampe035bd752014-09-02 21:17:03 -0700215 } else {
216 runtime_callbacks = runtime_cbs;
217 state = NativeBridgeState::kOpened;
218 }
219 }
220 return state == NativeBridgeState::kOpened;
221 }
222}
223
jgu21ab0da5a2014-09-10 06:58:32 -0400224#if defined(__arm__)
225static const char* kRuntimeISA = "arm";
226#elif defined(__aarch64__)
227static const char* kRuntimeISA = "arm64";
228#elif defined(__mips__)
229static const char* kRuntimeISA = "mips";
230#elif defined(__i386__)
231static const char* kRuntimeISA = "x86";
232#elif defined(__x86_64__)
233static const char* kRuntimeISA = "x86_64";
234#else
235static const char* kRuntimeISA = "unknown";
236#endif
237
238
239bool NeedsNativeBridge(const char* instruction_set) {
Andreas Gampe04054e22014-09-25 22:33:01 -0700240 if (instruction_set == nullptr) {
241 ALOGE("Null instruction set in NeedsNativeBridge.");
242 return false;
243 }
Andreas Gampe2f71cb22014-09-25 21:34:25 -0700244 return strncmp(instruction_set, kRuntimeISA, strlen(kRuntimeISA) + 1) != 0;
jgu21ab0da5a2014-09-10 06:58:32 -0400245}
246
Andreas Gampe4390a632014-09-24 18:53:26 -0700247#ifdef __APPLE__
248template<typename T> void UNUSED(const T&) {}
249#endif
250
Calin Juravlef9d9e2a2014-10-17 13:45:39 +0100251bool PreInitializeNativeBridge(const char* app_data_dir_in, const char* instruction_set) {
252 if (state != NativeBridgeState::kOpened) {
253 ALOGE("Invalid state: native bridge is expected to be opened.");
254 CloseNativeBridge(true);
255 return false;
jgu21ab0da5a2014-09-10 06:58:32 -0400256 }
257
Calin Juravlef9d9e2a2014-10-17 13:45:39 +0100258 if (app_data_dir_in == nullptr) {
259 ALOGE("Application private directory cannot be null.");
260 CloseNativeBridge(true);
261 return false;
262 }
263
264 // Create the path to the application code cache directory.
265 // The memory will be release after Initialization or when the native bridge is closed.
266 const size_t len = strlen(app_data_dir_in) + strlen(kCodeCacheDir) + 2; // '\0' + '/'
267 app_code_cache_dir = new char[len];
268 snprintf(app_code_cache_dir, len, "%s/%s", app_data_dir_in, kCodeCacheDir);
269
270 // Bind-mount /system/lib{,64}/<isa>/cpuinfo to /proc/cpuinfo.
271 // Failure is not fatal and will keep the native bridge in kPreInitialized.
272 state = NativeBridgeState::kPreInitialized;
jgu21ab0da5a2014-09-10 06:58:32 -0400273
Andreas Gampe962eb402014-09-24 16:36:17 -0700274#ifndef __APPLE__
jgu21ab0da5a2014-09-10 06:58:32 -0400275 if (instruction_set == nullptr) {
Calin Juravlef9d9e2a2014-10-17 13:45:39 +0100276 return true;
jgu21ab0da5a2014-09-10 06:58:32 -0400277 }
278 size_t isa_len = strlen(instruction_set);
279 if (isa_len > 10) {
280 // 10 is a loose upper bound on the currently known instruction sets (a tight bound is 7 for
281 // x86_64 [including the trailing \0]). This is so we don't have to change here if there will
282 // be another instruction set in the future.
Andreas Gampe2f71cb22014-09-25 21:34:25 -0700283 ALOGW("Instruction set %s is malformed, must be less than or equal to 10 characters.",
284 instruction_set);
Calin Juravlef9d9e2a2014-10-17 13:45:39 +0100285 return true;
jgu21ab0da5a2014-09-10 06:58:32 -0400286 }
287
Calin Juravlef9d9e2a2014-10-17 13:45:39 +0100288 // If the file does not exist, the mount command will fail,
289 // so we save the extra file existence check.
jgu21ab0da5a2014-09-10 06:58:32 -0400290 char cpuinfo_path[1024];
291
Andreas Gampe04054e22014-09-25 22:33:01 -0700292#ifdef HAVE_ANDROID_OS
293 snprintf(cpuinfo_path, sizeof(cpuinfo_path), "/system/lib"
jgu21ab0da5a2014-09-10 06:58:32 -0400294#ifdef __LP64__
Andreas Gampe04054e22014-09-25 22:33:01 -0700295 "64"
296#endif // __LP64__
297 "/%s/cpuinfo", instruction_set);
298#else // !HAVE_ANDROID_OS
299 // To be able to test on the host, we hardwire a relative path.
300 snprintf(cpuinfo_path, sizeof(cpuinfo_path), "./cpuinfo");
jgu21ab0da5a2014-09-10 06:58:32 -0400301#endif
jgu21ab0da5a2014-09-10 06:58:32 -0400302
303 // Bind-mount.
Andreas Gampe2f71cb22014-09-25 21:34:25 -0700304 if (TEMP_FAILURE_RETRY(mount(cpuinfo_path, // Source.
305 "/proc/cpuinfo", // Target.
306 nullptr, // FS type.
307 MS_BIND, // Mount flags: bind mount.
308 nullptr)) == -1) { // "Data."
Andreas Gampe04054e22014-09-25 22:33:01 -0700309 ALOGW("Failed to bind-mount %s as /proc/cpuinfo: %s", cpuinfo_path, strerror(errno));
jgu21ab0da5a2014-09-10 06:58:32 -0400310 }
Calin Juravlef9d9e2a2014-10-17 13:45:39 +0100311#else // __APPLE__
Andreas Gampe4390a632014-09-24 18:53:26 -0700312 UNUSED(instruction_set);
Andreas Gampe962eb402014-09-24 16:36:17 -0700313 ALOGW("Mac OS does not support bind-mounting. Host simulation of native bridge impossible.");
314#endif
Calin Juravlef9d9e2a2014-10-17 13:45:39 +0100315
316 return true;
jgu21ab0da5a2014-09-10 06:58:32 -0400317}
318
319static void SetCpuAbi(JNIEnv* env, jclass build_class, const char* field, const char* value) {
320 if (value != nullptr) {
321 jfieldID field_id = env->GetStaticFieldID(build_class, field, "Ljava/lang/String;");
322 if (field_id == nullptr) {
323 env->ExceptionClear();
324 ALOGW("Could not find %s field.", field);
325 return;
326 }
327
328 jstring str = env->NewStringUTF(value);
329 if (str == nullptr) {
330 env->ExceptionClear();
331 ALOGW("Could not create string %s.", value);
332 return;
333 }
334
335 env->SetStaticObjectField(build_class, field_id, str);
336 }
337}
338
jgu21ab0da5a2014-09-10 06:58:32 -0400339// Set up the environment for the bridged app.
Andreas Gampea6ac9ce2015-04-30 20:39:12 -0700340static void SetupEnvironment(const NativeBridgeCallbacks* callbacks, JNIEnv* env, const char* isa) {
jgu21ab0da5a2014-09-10 06:58:32 -0400341 // Need a JNIEnv* to do anything.
342 if (env == nullptr) {
343 ALOGW("No JNIEnv* to set up app environment.");
344 return;
345 }
346
347 // Query the bridge for environment values.
348 const struct NativeBridgeRuntimeValues* env_values = callbacks->getAppEnv(isa);
349 if (env_values == nullptr) {
350 return;
351 }
352
353 // Keep the JNIEnv clean.
354 jint success = env->PushLocalFrame(16); // That should be small and large enough.
355 if (success < 0) {
356 // Out of memory, really borked.
357 ALOGW("Out of memory while setting up app environment.");
358 env->ExceptionClear();
359 return;
360 }
361
362 // Reset CPU_ABI & CPU_ABI2 to values required by the apps running with native bridge.
363 if (env_values->cpu_abi != nullptr || env_values->cpu_abi2 != nullptr ||
364 env_values->abi_count >= 0) {
365 jclass bclass_id = env->FindClass("android/os/Build");
366 if (bclass_id != nullptr) {
367 SetCpuAbi(env, bclass_id, "CPU_ABI", env_values->cpu_abi);
368 SetCpuAbi(env, bclass_id, "CPU_ABI2", env_values->cpu_abi2);
jgu21ab0da5a2014-09-10 06:58:32 -0400369 } else {
370 // For example in a host test environment.
371 env->ExceptionClear();
372 ALOGW("Could not find Build class.");
373 }
374 }
375
376 if (env_values->os_arch != nullptr) {
377 jclass sclass_id = env->FindClass("java/lang/System");
378 if (sclass_id != nullptr) {
Narayan Kamath484c55b2015-02-10 15:33:36 +0000379 jmethodID set_prop_id = env->GetStaticMethodID(sclass_id, "setUnchangeableSystemProperty",
Calin Juravlec3eb4312014-10-01 17:29:19 +0100380 "(Ljava/lang/String;Ljava/lang/String;)V");
jgu21ab0da5a2014-09-10 06:58:32 -0400381 if (set_prop_id != nullptr) {
Calin Juravlec3eb4312014-10-01 17:29:19 +0100382 // Init os.arch to the value reqired by the apps running with native bridge.
383 env->CallStaticVoidMethod(sclass_id, set_prop_id, env->NewStringUTF("os.arch"),
jgu21ab0da5a2014-09-10 06:58:32 -0400384 env->NewStringUTF(env_values->os_arch));
385 } else {
386 env->ExceptionClear();
Narayan Kamath484c55b2015-02-10 15:33:36 +0000387 ALOGW("Could not find System#setUnchangeableSystemProperty.");
jgu21ab0da5a2014-09-10 06:58:32 -0400388 }
389 } else {
390 env->ExceptionClear();
391 ALOGW("Could not find System class.");
392 }
393 }
394
395 // Make it pristine again.
396 env->PopLocalFrame(nullptr);
397}
398
399bool InitializeNativeBridge(JNIEnv* env, const char* instruction_set) {
Andreas Gampe035bd752014-09-02 21:17:03 -0700400 // We expect only one place that calls InitializeNativeBridge: Runtime::DidForkFromZygote. At that
401 // point we are not multi-threaded, so we do not need locking here.
402
Calin Juravlef9d9e2a2014-10-17 13:45:39 +0100403 if (state == NativeBridgeState::kPreInitialized) {
404 // Check for code cache: if it doesn't exist try to create it.
405 struct stat st;
406 if (stat(app_code_cache_dir, &st) == -1) {
407 if (errno == ENOENT) {
408 if (mkdir(app_code_cache_dir, S_IRWXU | S_IRWXG | S_IXOTH) == -1) {
409 ALOGE("Cannot create code cache directory %s: %s.", app_code_cache_dir, strerror(errno));
410 CloseNativeBridge(true);
411 }
412 } else {
413 ALOGE("Cannot stat code cache directory %s: %s.", app_code_cache_dir, strerror(errno));
414 CloseNativeBridge(true);
415 }
416 } else if (!S_ISDIR(st.st_mode)) {
417 ALOGE("Code cache is not a directory %s.", app_code_cache_dir);
418 CloseNativeBridge(true);
419 }
420
421 // If we're still PreInitialized (dind't fail the code cache checks) try to initialize.
422 if (state == NativeBridgeState::kPreInitialized) {
423 if (callbacks->initialize(runtime_callbacks, app_code_cache_dir, instruction_set)) {
424 SetupEnvironment(callbacks, env, instruction_set);
425 state = NativeBridgeState::kInitialized;
426 // We no longer need the code cache path, release the memory.
427 delete[] app_code_cache_dir;
428 app_code_cache_dir = nullptr;
429 } else {
430 // Unload the library.
431 dlclose(native_bridge_handle);
432 CloseNativeBridge(true);
433 }
Calin Juravle961ae122014-08-11 16:11:59 +0100434 }
Andreas Gampe049249c2014-08-19 22:31:31 -0700435 } else {
Calin Juravlef9d9e2a2014-10-17 13:45:39 +0100436 CloseNativeBridge(true);
Calin Juravle961ae122014-08-11 16:11:59 +0100437 }
438
Andreas Gampe035bd752014-09-02 21:17:03 -0700439 return state == NativeBridgeState::kInitialized;
440}
Calin Juravle961ae122014-08-11 16:11:59 +0100441
Andreas Gampe035bd752014-09-02 21:17:03 -0700442void UnloadNativeBridge() {
443 // We expect only one place that calls UnloadNativeBridge: Runtime::DidForkFromZygote. At that
444 // point we are not multi-threaded, so we do not need locking here.
445
446 switch(state) {
447 case NativeBridgeState::kOpened:
Calin Juravlef9d9e2a2014-10-17 13:45:39 +0100448 case NativeBridgeState::kPreInitialized:
Andreas Gampe035bd752014-09-02 21:17:03 -0700449 case NativeBridgeState::kInitialized:
450 // Unload.
451 dlclose(native_bridge_handle);
Calin Juravlef9d9e2a2014-10-17 13:45:39 +0100452 CloseNativeBridge(false);
Andreas Gampe035bd752014-09-02 21:17:03 -0700453 break;
454
455 case NativeBridgeState::kNotSetup:
456 // Not even set up. Error.
Calin Juravlef9d9e2a2014-10-17 13:45:39 +0100457 CloseNativeBridge(true);
Andreas Gampe035bd752014-09-02 21:17:03 -0700458 break;
459
460 case NativeBridgeState::kClosed:
461 // Ignore.
462 break;
463 }
Calin Juravle961ae122014-08-11 16:11:59 +0100464}
465
Andreas Gampe049249c2014-08-19 22:31:31 -0700466bool NativeBridgeError() {
467 return had_error;
468}
469
470bool NativeBridgeAvailable() {
Calin Juravlef9d9e2a2014-10-17 13:45:39 +0100471 return state == NativeBridgeState::kOpened
472 || state == NativeBridgeState::kPreInitialized
473 || state == NativeBridgeState::kInitialized;
Andreas Gampe035bd752014-09-02 21:17:03 -0700474}
475
476bool NativeBridgeInitialized() {
477 // Calls of this are supposed to happen in a state where the native bridge is stable, i.e., after
478 // Runtime::DidForkFromZygote. In that case we do not need a lock.
479 return state == NativeBridgeState::kInitialized;
Andreas Gampe049249c2014-08-19 22:31:31 -0700480}
481
Calin Juravle961ae122014-08-11 16:11:59 +0100482void* NativeBridgeLoadLibrary(const char* libpath, int flag) {
Andreas Gampe035bd752014-09-02 21:17:03 -0700483 if (NativeBridgeInitialized()) {
Calin Juravle961ae122014-08-11 16:11:59 +0100484 return callbacks->loadLibrary(libpath, flag);
485 }
486 return nullptr;
487}
488
489void* NativeBridgeGetTrampoline(void* handle, const char* name, const char* shorty,
490 uint32_t len) {
Andreas Gampe035bd752014-09-02 21:17:03 -0700491 if (NativeBridgeInitialized()) {
Calin Juravle961ae122014-08-11 16:11:59 +0100492 return callbacks->getTrampoline(handle, name, shorty, len);
493 }
494 return nullptr;
495}
496
497bool NativeBridgeIsSupported(const char* libpath) {
Andreas Gampe035bd752014-09-02 21:17:03 -0700498 if (NativeBridgeInitialized()) {
Calin Juravle961ae122014-08-11 16:11:59 +0100499 return callbacks->isSupported(libpath);
500 }
501 return false;
502}
503
Andreas Gampea6ac9ce2015-04-30 20:39:12 -0700504uint32_t NativeBridgeGetVersion() {
505 if (NativeBridgeAvailable()) {
506 return callbacks->version;
507 }
508 return 0;
509}
510
511NativeBridgeSignalHandlerFn NativeBridgeGetSignalHandler(int signal) {
512 if (NativeBridgeInitialized() && callbacks->version >= 2) {
513 return callbacks->getSignalHandler(signal);
514 }
515 return nullptr;
516}
517
Calin Juravle961ae122014-08-11 16:11:59 +0100518}; // namespace android