blob: e9b2a91706541c689c68690f924696b3d51656e6 [file] [log] [blame]
Orion Hodson9b16e342019-10-09 13:29:16 +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#define LOG_TAG "nativebridge"
18
19#include "nativebridge/native_bridge.h"
20
21#include <dlfcn.h>
22#include <errno.h>
23#include <fcntl.h>
24#include <stdio.h>
25#include <sys/mount.h>
26#include <sys/stat.h>
27#include <unistd.h>
28
29#include <cstring>
30
31#include <android-base/macros.h>
32#include <log/log.h>
33
34namespace android {
35
36#ifdef __APPLE__
37template <typename T>
38void UNUSED(const T&) {}
39#endif
40
41extern "C" {
42
43// Environment values required by the apps running with native bridge.
44struct NativeBridgeRuntimeValues {
45 const char* os_arch;
46 const char* cpu_abi;
47 const char* cpu_abi2;
48 const char* *supported_abis;
49 int32_t abi_count;
50};
51
52// The symbol name exposed by native-bridge with the type of NativeBridgeCallbacks.
53static constexpr const char* kNativeBridgeInterfaceSymbol = "NativeBridgeItf";
54
55enum class NativeBridgeState {
56 kNotSetup, // Initial state.
57 kOpened, // After successful dlopen.
58 kPreInitialized, // After successful pre-initialization.
59 kInitialized, // After successful initialization.
60 kClosed // Closed or errors.
61};
62
63static constexpr const char* kNotSetupString = "kNotSetup";
64static constexpr const char* kOpenedString = "kOpened";
65static constexpr const char* kPreInitializedString = "kPreInitialized";
66static constexpr const char* kInitializedString = "kInitialized";
67static constexpr const char* kClosedString = "kClosed";
68
69static const char* GetNativeBridgeStateString(NativeBridgeState state) {
70 switch (state) {
71 case NativeBridgeState::kNotSetup:
72 return kNotSetupString;
73
74 case NativeBridgeState::kOpened:
75 return kOpenedString;
76
77 case NativeBridgeState::kPreInitialized:
78 return kPreInitializedString;
79
80 case NativeBridgeState::kInitialized:
81 return kInitializedString;
82
83 case NativeBridgeState::kClosed:
84 return kClosedString;
85 }
86}
87
88// Current state of the native bridge.
89static NativeBridgeState state = NativeBridgeState::kNotSetup;
90
91// The version of NativeBridge implementation.
92// Different Nativebridge interface needs the service of different version of
93// Nativebridge implementation.
94// Used by isCompatibleWith() which is introduced in v2.
95enum NativeBridgeImplementationVersion {
96 // first version, not used.
97 DEFAULT_VERSION = 1,
98 // The version which signal semantic is introduced.
99 SIGNAL_VERSION = 2,
100 // The version which namespace semantic is introduced.
101 NAMESPACE_VERSION = 3,
102 // The version with vendor namespaces
103 VENDOR_NAMESPACE_VERSION = 4,
104 // The version with runtime namespaces
105 RUNTIME_NAMESPACE_VERSION = 5,
Lev Rumyantsevabafbe72019-12-13 15:49:37 -0800106 // The version with pre-zygote-fork hook to support app-zygotes.
107 PRE_ZYGOTE_FORK_VERSION = 6,
Orion Hodson9b16e342019-10-09 13:29:16 +0100108};
109
110// Whether we had an error at some point.
111static bool had_error = false;
112
113// Handle of the loaded library.
114static void* native_bridge_handle = nullptr;
115// Pointer to the callbacks. Available as soon as LoadNativeBridge succeeds, but only initialized
116// later.
117static const NativeBridgeCallbacks* callbacks = nullptr;
118// Callbacks provided by the environment to the bridge. Passed to LoadNativeBridge.
119static const NativeBridgeRuntimeCallbacks* runtime_callbacks = nullptr;
120
121// The app's code cache directory.
122static char* app_code_cache_dir = nullptr;
123
124// Code cache directory (relative to the application private directory)
125// Ideally we'd like to call into framework to retrieve this name. However that's considered an
126// implementation detail and will require either hacks or consistent refactorings. We compromise
127// and hard code the directory name again here.
128static constexpr const char* kCodeCacheDir = "code_cache";
129
130// Characters allowed in a native bridge filename. The first character must
131// be in [a-zA-Z] (expected 'l' for "libx"). The rest must be in [a-zA-Z0-9._-].
132static bool CharacterAllowed(char c, bool first) {
133 if (first) {
134 return ('a' <= c && c <= 'z') || ('A' <= c && c <= 'Z');
135 } else {
136 return ('a' <= c && c <= 'z') || ('A' <= c && c <= 'Z') || ('0' <= c && c <= '9') ||
137 (c == '.') || (c == '_') || (c == '-');
138 }
139}
140
141static void ReleaseAppCodeCacheDir() {
142 if (app_code_cache_dir != nullptr) {
143 delete[] app_code_cache_dir;
144 app_code_cache_dir = nullptr;
145 }
146}
147
148// We only allow simple names for the library. It is supposed to be a file in
149// /system/lib or /vendor/lib. Only allow a small range of characters, that is
150// names consisting of [a-zA-Z0-9._-] and starting with [a-zA-Z].
151bool NativeBridgeNameAcceptable(const char* nb_library_filename) {
152 const char* ptr = nb_library_filename;
153 if (*ptr == 0) {
154 // Emptry string. Allowed, means no native bridge.
155 return true;
156 } else {
157 // First character must be [a-zA-Z].
158 if (!CharacterAllowed(*ptr, true)) {
159 // Found an invalid fist character, don't accept.
160 ALOGE("Native bridge library %s has been rejected for first character %c",
161 nb_library_filename,
162 *ptr);
163 return false;
164 } else {
165 // For the rest, be more liberal.
166 ptr++;
167 while (*ptr != 0) {
168 if (!CharacterAllowed(*ptr, false)) {
169 // Found an invalid character, don't accept.
170 ALOGE("Native bridge library %s has been rejected for %c", nb_library_filename, *ptr);
171 return false;
172 }
173 ptr++;
174 }
175 }
176 return true;
177 }
178}
179
180// The policy of invoking Nativebridge changed in v3 with/without namespace.
181// Suggest Nativebridge implementation not maintain backward-compatible.
182static bool isCompatibleWith(const uint32_t version) {
183 // Libnativebridge is now designed to be forward-compatible. So only "0" is an unsupported
184 // version.
185 if (callbacks == nullptr || callbacks->version == 0 || version == 0) {
186 return false;
187 }
188
189 // If this is a v2+ bridge, it may not be forwards- or backwards-compatible. Check.
190 if (callbacks->version >= SIGNAL_VERSION) {
191 return callbacks->isCompatibleWith(version);
192 }
193
194 return true;
195}
196
197static void CloseNativeBridge(bool with_error) {
198 state = NativeBridgeState::kClosed;
199 had_error |= with_error;
200 ReleaseAppCodeCacheDir();
201}
202
203bool LoadNativeBridge(const char* nb_library_filename,
204 const NativeBridgeRuntimeCallbacks* runtime_cbs) {
205 // We expect only one place that calls LoadNativeBridge: Runtime::Init. At that point we are not
206 // multi-threaded, so we do not need locking here.
207
208 if (state != NativeBridgeState::kNotSetup) {
209 // Setup has been called before. Ignore this call.
210 if (nb_library_filename != nullptr) { // Avoids some log-spam for dalvikvm.
211 ALOGW("Called LoadNativeBridge for an already set up native bridge. State is %s.",
212 GetNativeBridgeStateString(state));
213 }
214 // Note: counts as an error, even though the bridge may be functional.
215 had_error = true;
216 return false;
217 }
218
219 if (nb_library_filename == nullptr || *nb_library_filename == 0) {
220 CloseNativeBridge(false);
221 return false;
222 } else {
223 if (!NativeBridgeNameAcceptable(nb_library_filename)) {
224 CloseNativeBridge(true);
225 } else {
226 // Try to open the library.
227 void* handle = dlopen(nb_library_filename, RTLD_LAZY);
228 if (handle != nullptr) {
229 callbacks = reinterpret_cast<NativeBridgeCallbacks*>(dlsym(handle,
230 kNativeBridgeInterfaceSymbol));
231 if (callbacks != nullptr) {
232 if (isCompatibleWith(NAMESPACE_VERSION)) {
233 // Store the handle for later.
234 native_bridge_handle = handle;
235 } else {
236 callbacks = nullptr;
237 dlclose(handle);
238 ALOGW("Unsupported native bridge interface.");
239 }
240 } else {
241 dlclose(handle);
242 }
243 }
244
245 // Two failure conditions: could not find library (dlopen failed), or could not find native
246 // bridge interface (dlsym failed). Both are an error and close the native bridge.
247 if (callbacks == nullptr) {
248 CloseNativeBridge(true);
249 } else {
250 runtime_callbacks = runtime_cbs;
251 state = NativeBridgeState::kOpened;
252 }
253 }
254 return state == NativeBridgeState::kOpened;
255 }
256}
257
258bool NeedsNativeBridge(const char* instruction_set) {
259 if (instruction_set == nullptr) {
260 ALOGE("Null instruction set in NeedsNativeBridge.");
261 return false;
262 }
263 return strncmp(instruction_set, ABI_STRING, strlen(ABI_STRING) + 1) != 0;
264}
265
Evgeny Eltsin662cee92021-02-10 07:19:14 +0100266#ifndef __APPLE__
267static bool MountCpuinfo(const char* cpuinfo_path) {
268 // If the file does not exist, the mount command will fail,
269 // so we save the extra file existence check.
270 if (TEMP_FAILURE_RETRY(mount(cpuinfo_path, // Source.
271 "/proc/cpuinfo", // Target.
272 nullptr, // FS type.
273 MS_BIND, // Mount flags: bind mount.
274 nullptr)) == -1) { // "Data."
275 ALOGW("Failed to bind-mount %s as /proc/cpuinfo: %s", cpuinfo_path, strerror(errno));
276 return false;
277 }
278 return true;
279}
280#endif
281
282static void MountCpuinfoForInstructionSet(const char* instruction_set) {
283 if (instruction_set == nullptr) {
284 return;
285 }
286
287 size_t isa_len = strlen(instruction_set);
288 if (isa_len > 10) {
289 // 10 is a loose upper bound on the currently known instruction sets (a tight bound is 7 for
290 // x86_64 [including the trailing \0]). This is so we don't have to change here if there will
291 // be another instruction set in the future.
292 ALOGW("Instruction set %s is malformed, must be less than or equal to 10 characters.",
293 instruction_set);
294 return;
295 }
296
297#if defined(__APPLE__)
298 ALOGW("Mac OS does not support bind-mounting. Host simulation of native bridge impossible.");
299
300#elif !defined(__ANDROID__)
301 // To be able to test on the host, we hardwire a relative path.
302 MountCpuinfo("./cpuinfo");
303
304#else // __ANDROID__
305 char cpuinfo_path[1024];
306
307 // Bind-mount /system/etc/cpuinfo.<isa>.txt to /proc/cpuinfo.
308 snprintf(cpuinfo_path, sizeof(cpuinfo_path), "/system/etc/cpuinfo.%s.txt", instruction_set);
309 if (MountCpuinfo(cpuinfo_path)) {
310 return;
311 }
312
313 // Bind-mount /system/lib{,64}/<isa>/cpuinfo to /proc/cpuinfo.
314 // TODO(b/179753190): remove when all implementations migrate to system/etc!
315#ifdef __LP64__
316 snprintf(cpuinfo_path, sizeof(cpuinfo_path), "/system/lib64/%s/cpuinfo", instruction_set);
317#else
318 snprintf(cpuinfo_path, sizeof(cpuinfo_path), "/system/lib/%s/cpuinfo", instruction_set);
319#endif // __LP64__
320 MountCpuinfo(cpuinfo_path);
321
322#endif
323}
324
Orion Hodson9b16e342019-10-09 13:29:16 +0100325bool PreInitializeNativeBridge(const char* app_data_dir_in, const char* instruction_set) {
326 if (state != NativeBridgeState::kOpened) {
327 ALOGE("Invalid state: native bridge is expected to be opened.");
328 CloseNativeBridge(true);
329 return false;
330 }
331
Lev Rumyantsev7ec3e602019-12-13 15:49:37 -0800332 if (app_data_dir_in != nullptr) {
333 // Create the path to the application code cache directory.
334 // The memory will be release after Initialization or when the native bridge is closed.
335 const size_t len = strlen(app_data_dir_in) + strlen(kCodeCacheDir) + 2; // '\0' + '/'
336 app_code_cache_dir = new char[len];
337 snprintf(app_code_cache_dir, len, "%s/%s", app_data_dir_in, kCodeCacheDir);
338 } else {
339 ALOGW("Application private directory isn't available.");
340 app_code_cache_dir = nullptr;
Orion Hodson9b16e342019-10-09 13:29:16 +0100341 }
342
Evgeny Eltsin662cee92021-02-10 07:19:14 +0100343 // Mount cpuinfo that corresponds to the instruction set.
344 // Failure is not fatal.
345 MountCpuinfoForInstructionSet(instruction_set);
346
Orion Hodson9b16e342019-10-09 13:29:16 +0100347 state = NativeBridgeState::kPreInitialized;
Orion Hodson9b16e342019-10-09 13:29:16 +0100348 return true;
349}
350
Lev Rumyantsevabafbe72019-12-13 15:49:37 -0800351void PreZygoteForkNativeBridge() {
352 if (NativeBridgeInitialized()) {
353 if (isCompatibleWith(PRE_ZYGOTE_FORK_VERSION)) {
354 return callbacks->preZygoteFork();
355 } else {
356 ALOGE("not compatible with version %d, preZygoteFork() isn't invoked",
357 PRE_ZYGOTE_FORK_VERSION);
358 }
359 }
360}
361
Orion Hodson9b16e342019-10-09 13:29:16 +0100362static void SetCpuAbi(JNIEnv* env, jclass build_class, const char* field, const char* value) {
363 if (value != nullptr) {
364 jfieldID field_id = env->GetStaticFieldID(build_class, field, "Ljava/lang/String;");
365 if (field_id == nullptr) {
366 env->ExceptionClear();
367 ALOGW("Could not find %s field.", field);
368 return;
369 }
370
371 jstring str = env->NewStringUTF(value);
372 if (str == nullptr) {
373 env->ExceptionClear();
374 ALOGW("Could not create string %s.", value);
375 return;
376 }
377
378 env->SetStaticObjectField(build_class, field_id, str);
379 }
380}
381
382// Set up the environment for the bridged app.
Martin Stjernholm3bb009a2019-10-17 21:29:01 +0100383static void SetupEnvironment(const NativeBridgeCallbacks* cbs, JNIEnv* env, const char* isa) {
Orion Hodson9b16e342019-10-09 13:29:16 +0100384 // Need a JNIEnv* to do anything.
385 if (env == nullptr) {
386 ALOGW("No JNIEnv* to set up app environment.");
387 return;
388 }
389
390 // Query the bridge for environment values.
Martin Stjernholm3bb009a2019-10-17 21:29:01 +0100391 const struct NativeBridgeRuntimeValues* env_values = cbs->getAppEnv(isa);
Orion Hodson9b16e342019-10-09 13:29:16 +0100392 if (env_values == nullptr) {
393 return;
394 }
395
396 // Keep the JNIEnv clean.
397 jint success = env->PushLocalFrame(16); // That should be small and large enough.
398 if (success < 0) {
399 // Out of memory, really borked.
400 ALOGW("Out of memory while setting up app environment.");
401 env->ExceptionClear();
402 return;
403 }
404
405 // Reset CPU_ABI & CPU_ABI2 to values required by the apps running with native bridge.
406 if (env_values->cpu_abi != nullptr || env_values->cpu_abi2 != nullptr ||
407 env_values->abi_count >= 0) {
408 jclass bclass_id = env->FindClass("android/os/Build");
409 if (bclass_id != nullptr) {
410 SetCpuAbi(env, bclass_id, "CPU_ABI", env_values->cpu_abi);
411 SetCpuAbi(env, bclass_id, "CPU_ABI2", env_values->cpu_abi2);
412 } else {
413 // For example in a host test environment.
414 env->ExceptionClear();
415 ALOGW("Could not find Build class.");
416 }
417 }
418
419 if (env_values->os_arch != nullptr) {
420 jclass sclass_id = env->FindClass("java/lang/System");
421 if (sclass_id != nullptr) {
422 jmethodID set_prop_id = env->GetStaticMethodID(sclass_id, "setUnchangeableSystemProperty",
423 "(Ljava/lang/String;Ljava/lang/String;)V");
424 if (set_prop_id != nullptr) {
425 // Init os.arch to the value reqired by the apps running with native bridge.
426 env->CallStaticVoidMethod(sclass_id, set_prop_id, env->NewStringUTF("os.arch"),
427 env->NewStringUTF(env_values->os_arch));
428 } else {
429 env->ExceptionClear();
430 ALOGW("Could not find System#setUnchangeableSystemProperty.");
431 }
432 } else {
433 env->ExceptionClear();
434 ALOGW("Could not find System class.");
435 }
436 }
437
438 // Make it pristine again.
439 env->PopLocalFrame(nullptr);
440}
441
442bool InitializeNativeBridge(JNIEnv* env, const char* instruction_set) {
443 // We expect only one place that calls InitializeNativeBridge: Runtime::DidForkFromZygote. At that
444 // point we are not multi-threaded, so we do not need locking here.
445
446 if (state == NativeBridgeState::kPreInitialized) {
Lev Rumyantsev7ec3e602019-12-13 15:49:37 -0800447 if (app_code_cache_dir != nullptr) {
448 // Check for code cache: if it doesn't exist try to create it.
449 struct stat st;
450 if (stat(app_code_cache_dir, &st) == -1) {
451 if (errno == ENOENT) {
452 if (mkdir(app_code_cache_dir, S_IRWXU | S_IRWXG | S_IXOTH) == -1) {
453 ALOGW("Cannot create code cache directory %s: %s.",
454 app_code_cache_dir, strerror(errno));
455 ReleaseAppCodeCacheDir();
456 }
457 } else {
458 ALOGW("Cannot stat code cache directory %s: %s.",
459 app_code_cache_dir, strerror(errno));
Orion Hodson9b16e342019-10-09 13:29:16 +0100460 ReleaseAppCodeCacheDir();
461 }
Lev Rumyantsev7ec3e602019-12-13 15:49:37 -0800462 } else if (!S_ISDIR(st.st_mode)) {
463 ALOGW("Code cache is not a directory %s.", app_code_cache_dir);
Orion Hodson9b16e342019-10-09 13:29:16 +0100464 ReleaseAppCodeCacheDir();
465 }
Orion Hodson9b16e342019-10-09 13:29:16 +0100466 }
467
Lev Rumyantsev7ec3e602019-12-13 15:49:37 -0800468 // If we're still PreInitialized (didn't fail the code cache checks) try to initialize.
Orion Hodson9b16e342019-10-09 13:29:16 +0100469 if (state == NativeBridgeState::kPreInitialized) {
470 if (callbacks->initialize(runtime_callbacks, app_code_cache_dir, instruction_set)) {
471 SetupEnvironment(callbacks, env, instruction_set);
472 state = NativeBridgeState::kInitialized;
473 // We no longer need the code cache path, release the memory.
474 ReleaseAppCodeCacheDir();
475 } else {
476 // Unload the library.
477 dlclose(native_bridge_handle);
478 CloseNativeBridge(true);
479 }
480 }
481 } else {
482 CloseNativeBridge(true);
483 }
484
485 return state == NativeBridgeState::kInitialized;
486}
487
488void UnloadNativeBridge() {
489 // We expect only one place that calls UnloadNativeBridge: Runtime::DidForkFromZygote. At that
490 // point we are not multi-threaded, so we do not need locking here.
491
Orion Hodson31b3ffa2019-10-14 10:27:00 +0100492 switch (state) {
Orion Hodson9b16e342019-10-09 13:29:16 +0100493 case NativeBridgeState::kOpened:
494 case NativeBridgeState::kPreInitialized:
495 case NativeBridgeState::kInitialized:
496 // Unload.
497 dlclose(native_bridge_handle);
498 CloseNativeBridge(false);
499 break;
500
501 case NativeBridgeState::kNotSetup:
502 // Not even set up. Error.
503 CloseNativeBridge(true);
504 break;
505
506 case NativeBridgeState::kClosed:
507 // Ignore.
508 break;
509 }
510}
511
512bool NativeBridgeError() {
513 return had_error;
514}
515
516bool NativeBridgeAvailable() {
517 return state == NativeBridgeState::kOpened
518 || state == NativeBridgeState::kPreInitialized
519 || state == NativeBridgeState::kInitialized;
520}
521
522bool NativeBridgeInitialized() {
523 // Calls of this are supposed to happen in a state where the native bridge is stable, i.e., after
524 // Runtime::DidForkFromZygote. In that case we do not need a lock.
525 return state == NativeBridgeState::kInitialized;
526}
527
528void* NativeBridgeLoadLibrary(const char* libpath, int flag) {
529 if (NativeBridgeInitialized()) {
530 return callbacks->loadLibrary(libpath, flag);
531 }
532 return nullptr;
533}
534
535void* NativeBridgeGetTrampoline(void* handle, const char* name, const char* shorty,
536 uint32_t len) {
537 if (NativeBridgeInitialized()) {
538 return callbacks->getTrampoline(handle, name, shorty, len);
539 }
540 return nullptr;
541}
542
543bool NativeBridgeIsSupported(const char* libpath) {
544 if (NativeBridgeInitialized()) {
545 return callbacks->isSupported(libpath);
546 }
547 return false;
548}
549
550uint32_t NativeBridgeGetVersion() {
551 if (NativeBridgeAvailable()) {
552 return callbacks->version;
553 }
554 return 0;
555}
556
557NativeBridgeSignalHandlerFn NativeBridgeGetSignalHandler(int signal) {
558 if (NativeBridgeInitialized()) {
559 if (isCompatibleWith(SIGNAL_VERSION)) {
560 return callbacks->getSignalHandler(signal);
561 } else {
562 ALOGE("not compatible with version %d, cannot get signal handler", SIGNAL_VERSION);
563 }
564 }
565 return nullptr;
566}
567
568int NativeBridgeUnloadLibrary(void* handle) {
569 if (NativeBridgeInitialized()) {
570 if (isCompatibleWith(NAMESPACE_VERSION)) {
571 return callbacks->unloadLibrary(handle);
572 } else {
573 ALOGE("not compatible with version %d, cannot unload library", NAMESPACE_VERSION);
574 }
575 }
576 return -1;
577}
578
579const char* NativeBridgeGetError() {
580 if (NativeBridgeInitialized()) {
581 if (isCompatibleWith(NAMESPACE_VERSION)) {
582 return callbacks->getError();
583 } else {
584 return "native bridge implementation is not compatible with version 3, cannot get message";
585 }
586 }
587 return "native bridge is not initialized";
588}
589
590bool NativeBridgeIsPathSupported(const char* path) {
591 if (NativeBridgeInitialized()) {
592 if (isCompatibleWith(NAMESPACE_VERSION)) {
593 return callbacks->isPathSupported(path);
594 } else {
595 ALOGE("not compatible with version %d, cannot check via library path", NAMESPACE_VERSION);
596 }
597 }
598 return false;
599}
600
601bool NativeBridgeInitAnonymousNamespace(const char* public_ns_sonames,
602 const char* anon_ns_library_path) {
603 if (NativeBridgeInitialized()) {
604 if (isCompatibleWith(NAMESPACE_VERSION)) {
605 return callbacks->initAnonymousNamespace(public_ns_sonames, anon_ns_library_path);
606 } else {
607 ALOGE("not compatible with version %d, cannot init namespace", NAMESPACE_VERSION);
608 }
609 }
610
611 return false;
612}
613
614native_bridge_namespace_t* NativeBridgeCreateNamespace(const char* name,
615 const char* ld_library_path,
616 const char* default_library_path,
617 uint64_t type,
618 const char* permitted_when_isolated_path,
619 native_bridge_namespace_t* parent_ns) {
620 if (NativeBridgeInitialized()) {
621 if (isCompatibleWith(NAMESPACE_VERSION)) {
622 return callbacks->createNamespace(name,
623 ld_library_path,
624 default_library_path,
625 type,
626 permitted_when_isolated_path,
627 parent_ns);
628 } else {
629 ALOGE("not compatible with version %d, cannot create namespace %s", NAMESPACE_VERSION, name);
630 }
631 }
632
633 return nullptr;
634}
635
636bool NativeBridgeLinkNamespaces(native_bridge_namespace_t* from, native_bridge_namespace_t* to,
637 const char* shared_libs_sonames) {
638 if (NativeBridgeInitialized()) {
639 if (isCompatibleWith(NAMESPACE_VERSION)) {
640 return callbacks->linkNamespaces(from, to, shared_libs_sonames);
641 } else {
642 ALOGE("not compatible with version %d, cannot init namespace", NAMESPACE_VERSION);
643 }
644 }
645
646 return false;
647}
648
649native_bridge_namespace_t* NativeBridgeGetExportedNamespace(const char* name) {
650 if (!NativeBridgeInitialized()) {
651 return nullptr;
652 }
653
654 if (isCompatibleWith(RUNTIME_NAMESPACE_VERSION)) {
655 return callbacks->getExportedNamespace(name);
656 }
657
658 // sphal is vendor namespace name -> use v4 callback in the case NB callbacks
659 // are not compatible with v5
660 if (isCompatibleWith(VENDOR_NAMESPACE_VERSION) && name != nullptr && strcmp("sphal", name) == 0) {
661 return callbacks->getVendorNamespace();
662 }
663
664 return nullptr;
665}
666
667void* NativeBridgeLoadLibraryExt(const char* libpath, int flag, native_bridge_namespace_t* ns) {
668 if (NativeBridgeInitialized()) {
669 if (isCompatibleWith(NAMESPACE_VERSION)) {
670 return callbacks->loadLibraryExt(libpath, flag, ns);
671 } else {
672 ALOGE("not compatible with version %d, cannot load library in namespace", NAMESPACE_VERSION);
673 }
674 }
675 return nullptr;
676}
677
678} // extern "C"
679
680} // namespace android