blob: 7600780f79335e7d30a94e3a08d9549d382172ac [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);
Martin Stjernholmae3aa6c2021-04-17 17:09:02 +0100238 ALOGW("Unsupported native bridge API in %s (is version %d not compatible with %d)",
239 nb_library_filename, callbacks->version, NAMESPACE_VERSION);
Orion Hodson9b16e342019-10-09 13:29:16 +0100240 }
241 } else {
242 dlclose(handle);
Martin Stjernholmae3aa6c2021-04-17 17:09:02 +0100243 ALOGW("Unsupported native bridge API in %s: %s not found",
244 nb_library_filename, kNativeBridgeInterfaceSymbol);
Orion Hodson9b16e342019-10-09 13:29:16 +0100245 }
Martin Stjernholmae3aa6c2021-04-17 17:09:02 +0100246 } else {
247 ALOGW("Failed to load native bridge implementation: %s", dlerror());
Orion Hodson9b16e342019-10-09 13:29:16 +0100248 }
249
250 // Two failure conditions: could not find library (dlopen failed), or could not find native
251 // bridge interface (dlsym failed). Both are an error and close the native bridge.
252 if (callbacks == nullptr) {
253 CloseNativeBridge(true);
254 } else {
255 runtime_callbacks = runtime_cbs;
256 state = NativeBridgeState::kOpened;
257 }
258 }
259 return state == NativeBridgeState::kOpened;
260 }
261}
262
263bool NeedsNativeBridge(const char* instruction_set) {
264 if (instruction_set == nullptr) {
265 ALOGE("Null instruction set in NeedsNativeBridge.");
266 return false;
267 }
268 return strncmp(instruction_set, ABI_STRING, strlen(ABI_STRING) + 1) != 0;
269}
270
Evgeny Eltsin662cee92021-02-10 07:19:14 +0100271#ifndef __APPLE__
272static bool MountCpuinfo(const char* cpuinfo_path) {
273 // If the file does not exist, the mount command will fail,
274 // so we save the extra file existence check.
275 if (TEMP_FAILURE_RETRY(mount(cpuinfo_path, // Source.
276 "/proc/cpuinfo", // Target.
277 nullptr, // FS type.
278 MS_BIND, // Mount flags: bind mount.
279 nullptr)) == -1) { // "Data."
280 ALOGW("Failed to bind-mount %s as /proc/cpuinfo: %s", cpuinfo_path, strerror(errno));
281 return false;
282 }
283 return true;
284}
285#endif
286
287static void MountCpuinfoForInstructionSet(const char* instruction_set) {
288 if (instruction_set == nullptr) {
289 return;
290 }
291
292 size_t isa_len = strlen(instruction_set);
293 if (isa_len > 10) {
294 // 10 is a loose upper bound on the currently known instruction sets (a tight bound is 7 for
295 // x86_64 [including the trailing \0]). This is so we don't have to change here if there will
296 // be another instruction set in the future.
297 ALOGW("Instruction set %s is malformed, must be less than or equal to 10 characters.",
298 instruction_set);
299 return;
300 }
301
302#if defined(__APPLE__)
303 ALOGW("Mac OS does not support bind-mounting. Host simulation of native bridge impossible.");
304
305#elif !defined(__ANDROID__)
306 // To be able to test on the host, we hardwire a relative path.
307 MountCpuinfo("./cpuinfo");
308
309#else // __ANDROID__
310 char cpuinfo_path[1024];
311
312 // Bind-mount /system/etc/cpuinfo.<isa>.txt to /proc/cpuinfo.
313 snprintf(cpuinfo_path, sizeof(cpuinfo_path), "/system/etc/cpuinfo.%s.txt", instruction_set);
314 if (MountCpuinfo(cpuinfo_path)) {
315 return;
316 }
317
318 // Bind-mount /system/lib{,64}/<isa>/cpuinfo to /proc/cpuinfo.
319 // TODO(b/179753190): remove when all implementations migrate to system/etc!
320#ifdef __LP64__
321 snprintf(cpuinfo_path, sizeof(cpuinfo_path), "/system/lib64/%s/cpuinfo", instruction_set);
322#else
323 snprintf(cpuinfo_path, sizeof(cpuinfo_path), "/system/lib/%s/cpuinfo", instruction_set);
324#endif // __LP64__
325 MountCpuinfo(cpuinfo_path);
326
327#endif
328}
329
Orion Hodson9b16e342019-10-09 13:29:16 +0100330bool PreInitializeNativeBridge(const char* app_data_dir_in, const char* instruction_set) {
331 if (state != NativeBridgeState::kOpened) {
332 ALOGE("Invalid state: native bridge is expected to be opened.");
333 CloseNativeBridge(true);
334 return false;
335 }
336
Lev Rumyantsev7ec3e602019-12-13 15:49:37 -0800337 if (app_data_dir_in != nullptr) {
338 // Create the path to the application code cache directory.
339 // The memory will be release after Initialization or when the native bridge is closed.
340 const size_t len = strlen(app_data_dir_in) + strlen(kCodeCacheDir) + 2; // '\0' + '/'
341 app_code_cache_dir = new char[len];
342 snprintf(app_code_cache_dir, len, "%s/%s", app_data_dir_in, kCodeCacheDir);
343 } else {
344 ALOGW("Application private directory isn't available.");
345 app_code_cache_dir = nullptr;
Orion Hodson9b16e342019-10-09 13:29:16 +0100346 }
347
Evgeny Eltsin662cee92021-02-10 07:19:14 +0100348 // Mount cpuinfo that corresponds to the instruction set.
349 // Failure is not fatal.
350 MountCpuinfoForInstructionSet(instruction_set);
351
Orion Hodson9b16e342019-10-09 13:29:16 +0100352 state = NativeBridgeState::kPreInitialized;
Orion Hodson9b16e342019-10-09 13:29:16 +0100353 return true;
354}
355
Lev Rumyantsevabafbe72019-12-13 15:49:37 -0800356void PreZygoteForkNativeBridge() {
357 if (NativeBridgeInitialized()) {
358 if (isCompatibleWith(PRE_ZYGOTE_FORK_VERSION)) {
359 return callbacks->preZygoteFork();
360 } else {
361 ALOGE("not compatible with version %d, preZygoteFork() isn't invoked",
362 PRE_ZYGOTE_FORK_VERSION);
363 }
364 }
365}
366
Orion Hodson9b16e342019-10-09 13:29:16 +0100367static void SetCpuAbi(JNIEnv* env, jclass build_class, const char* field, const char* value) {
368 if (value != nullptr) {
369 jfieldID field_id = env->GetStaticFieldID(build_class, field, "Ljava/lang/String;");
370 if (field_id == nullptr) {
371 env->ExceptionClear();
372 ALOGW("Could not find %s field.", field);
373 return;
374 }
375
376 jstring str = env->NewStringUTF(value);
377 if (str == nullptr) {
378 env->ExceptionClear();
379 ALOGW("Could not create string %s.", value);
380 return;
381 }
382
383 env->SetStaticObjectField(build_class, field_id, str);
384 }
385}
386
387// Set up the environment for the bridged app.
Martin Stjernholm3bb009a2019-10-17 21:29:01 +0100388static void SetupEnvironment(const NativeBridgeCallbacks* cbs, JNIEnv* env, const char* isa) {
Orion Hodson9b16e342019-10-09 13:29:16 +0100389 // Need a JNIEnv* to do anything.
390 if (env == nullptr) {
391 ALOGW("No JNIEnv* to set up app environment.");
392 return;
393 }
394
395 // Query the bridge for environment values.
Martin Stjernholm3bb009a2019-10-17 21:29:01 +0100396 const struct NativeBridgeRuntimeValues* env_values = cbs->getAppEnv(isa);
Orion Hodson9b16e342019-10-09 13:29:16 +0100397 if (env_values == nullptr) {
398 return;
399 }
400
401 // Keep the JNIEnv clean.
402 jint success = env->PushLocalFrame(16); // That should be small and large enough.
403 if (success < 0) {
404 // Out of memory, really borked.
405 ALOGW("Out of memory while setting up app environment.");
406 env->ExceptionClear();
407 return;
408 }
409
410 // Reset CPU_ABI & CPU_ABI2 to values required by the apps running with native bridge.
411 if (env_values->cpu_abi != nullptr || env_values->cpu_abi2 != nullptr ||
412 env_values->abi_count >= 0) {
413 jclass bclass_id = env->FindClass("android/os/Build");
414 if (bclass_id != nullptr) {
415 SetCpuAbi(env, bclass_id, "CPU_ABI", env_values->cpu_abi);
416 SetCpuAbi(env, bclass_id, "CPU_ABI2", env_values->cpu_abi2);
417 } else {
418 // For example in a host test environment.
419 env->ExceptionClear();
420 ALOGW("Could not find Build class.");
421 }
422 }
423
424 if (env_values->os_arch != nullptr) {
425 jclass sclass_id = env->FindClass("java/lang/System");
426 if (sclass_id != nullptr) {
427 jmethodID set_prop_id = env->GetStaticMethodID(sclass_id, "setUnchangeableSystemProperty",
428 "(Ljava/lang/String;Ljava/lang/String;)V");
429 if (set_prop_id != nullptr) {
430 // Init os.arch to the value reqired by the apps running with native bridge.
431 env->CallStaticVoidMethod(sclass_id, set_prop_id, env->NewStringUTF("os.arch"),
432 env->NewStringUTF(env_values->os_arch));
433 } else {
434 env->ExceptionClear();
435 ALOGW("Could not find System#setUnchangeableSystemProperty.");
436 }
437 } else {
438 env->ExceptionClear();
439 ALOGW("Could not find System class.");
440 }
441 }
442
443 // Make it pristine again.
444 env->PopLocalFrame(nullptr);
445}
446
447bool InitializeNativeBridge(JNIEnv* env, const char* instruction_set) {
448 // We expect only one place that calls InitializeNativeBridge: Runtime::DidForkFromZygote. At that
449 // point we are not multi-threaded, so we do not need locking here.
450
451 if (state == NativeBridgeState::kPreInitialized) {
Lev Rumyantsev7ec3e602019-12-13 15:49:37 -0800452 if (app_code_cache_dir != nullptr) {
453 // Check for code cache: if it doesn't exist try to create it.
454 struct stat st;
455 if (stat(app_code_cache_dir, &st) == -1) {
456 if (errno == ENOENT) {
457 if (mkdir(app_code_cache_dir, S_IRWXU | S_IRWXG | S_IXOTH) == -1) {
458 ALOGW("Cannot create code cache directory %s: %s.",
459 app_code_cache_dir, strerror(errno));
460 ReleaseAppCodeCacheDir();
461 }
462 } else {
463 ALOGW("Cannot stat code cache directory %s: %s.",
464 app_code_cache_dir, strerror(errno));
Orion Hodson9b16e342019-10-09 13:29:16 +0100465 ReleaseAppCodeCacheDir();
466 }
Lev Rumyantsev7ec3e602019-12-13 15:49:37 -0800467 } else if (!S_ISDIR(st.st_mode)) {
468 ALOGW("Code cache is not a directory %s.", app_code_cache_dir);
Orion Hodson9b16e342019-10-09 13:29:16 +0100469 ReleaseAppCodeCacheDir();
470 }
Orion Hodson9b16e342019-10-09 13:29:16 +0100471 }
472
Lev Rumyantsev7ec3e602019-12-13 15:49:37 -0800473 // If we're still PreInitialized (didn't fail the code cache checks) try to initialize.
Orion Hodson9b16e342019-10-09 13:29:16 +0100474 if (state == NativeBridgeState::kPreInitialized) {
475 if (callbacks->initialize(runtime_callbacks, app_code_cache_dir, instruction_set)) {
476 SetupEnvironment(callbacks, env, instruction_set);
477 state = NativeBridgeState::kInitialized;
478 // We no longer need the code cache path, release the memory.
479 ReleaseAppCodeCacheDir();
480 } else {
481 // Unload the library.
482 dlclose(native_bridge_handle);
483 CloseNativeBridge(true);
484 }
485 }
486 } else {
487 CloseNativeBridge(true);
488 }
489
490 return state == NativeBridgeState::kInitialized;
491}
492
493void UnloadNativeBridge() {
494 // We expect only one place that calls UnloadNativeBridge: Runtime::DidForkFromZygote. At that
495 // point we are not multi-threaded, so we do not need locking here.
496
Orion Hodson31b3ffa2019-10-14 10:27:00 +0100497 switch (state) {
Orion Hodson9b16e342019-10-09 13:29:16 +0100498 case NativeBridgeState::kOpened:
499 case NativeBridgeState::kPreInitialized:
500 case NativeBridgeState::kInitialized:
501 // Unload.
502 dlclose(native_bridge_handle);
503 CloseNativeBridge(false);
504 break;
505
506 case NativeBridgeState::kNotSetup:
507 // Not even set up. Error.
508 CloseNativeBridge(true);
509 break;
510
511 case NativeBridgeState::kClosed:
512 // Ignore.
513 break;
514 }
515}
516
517bool NativeBridgeError() {
518 return had_error;
519}
520
521bool NativeBridgeAvailable() {
522 return state == NativeBridgeState::kOpened
523 || state == NativeBridgeState::kPreInitialized
524 || state == NativeBridgeState::kInitialized;
525}
526
527bool NativeBridgeInitialized() {
528 // Calls of this are supposed to happen in a state where the native bridge is stable, i.e., after
529 // Runtime::DidForkFromZygote. In that case we do not need a lock.
530 return state == NativeBridgeState::kInitialized;
531}
532
533void* NativeBridgeLoadLibrary(const char* libpath, int flag) {
534 if (NativeBridgeInitialized()) {
535 return callbacks->loadLibrary(libpath, flag);
536 }
537 return nullptr;
538}
539
540void* NativeBridgeGetTrampoline(void* handle, const char* name, const char* shorty,
541 uint32_t len) {
542 if (NativeBridgeInitialized()) {
543 return callbacks->getTrampoline(handle, name, shorty, len);
544 }
545 return nullptr;
546}
547
548bool NativeBridgeIsSupported(const char* libpath) {
549 if (NativeBridgeInitialized()) {
550 return callbacks->isSupported(libpath);
551 }
552 return false;
553}
554
555uint32_t NativeBridgeGetVersion() {
556 if (NativeBridgeAvailable()) {
557 return callbacks->version;
558 }
559 return 0;
560}
561
562NativeBridgeSignalHandlerFn NativeBridgeGetSignalHandler(int signal) {
563 if (NativeBridgeInitialized()) {
564 if (isCompatibleWith(SIGNAL_VERSION)) {
565 return callbacks->getSignalHandler(signal);
566 } else {
567 ALOGE("not compatible with version %d, cannot get signal handler", SIGNAL_VERSION);
568 }
569 }
570 return nullptr;
571}
572
573int NativeBridgeUnloadLibrary(void* handle) {
574 if (NativeBridgeInitialized()) {
575 if (isCompatibleWith(NAMESPACE_VERSION)) {
576 return callbacks->unloadLibrary(handle);
577 } else {
578 ALOGE("not compatible with version %d, cannot unload library", NAMESPACE_VERSION);
579 }
580 }
581 return -1;
582}
583
584const char* NativeBridgeGetError() {
585 if (NativeBridgeInitialized()) {
586 if (isCompatibleWith(NAMESPACE_VERSION)) {
587 return callbacks->getError();
588 } else {
589 return "native bridge implementation is not compatible with version 3, cannot get message";
590 }
591 }
592 return "native bridge is not initialized";
593}
594
595bool NativeBridgeIsPathSupported(const char* path) {
596 if (NativeBridgeInitialized()) {
597 if (isCompatibleWith(NAMESPACE_VERSION)) {
598 return callbacks->isPathSupported(path);
599 } else {
600 ALOGE("not compatible with version %d, cannot check via library path", NAMESPACE_VERSION);
601 }
602 }
603 return false;
604}
605
606bool NativeBridgeInitAnonymousNamespace(const char* public_ns_sonames,
607 const char* anon_ns_library_path) {
608 if (NativeBridgeInitialized()) {
609 if (isCompatibleWith(NAMESPACE_VERSION)) {
610 return callbacks->initAnonymousNamespace(public_ns_sonames, anon_ns_library_path);
611 } else {
612 ALOGE("not compatible with version %d, cannot init namespace", NAMESPACE_VERSION);
613 }
614 }
615
616 return false;
617}
618
619native_bridge_namespace_t* NativeBridgeCreateNamespace(const char* name,
620 const char* ld_library_path,
621 const char* default_library_path,
622 uint64_t type,
623 const char* permitted_when_isolated_path,
624 native_bridge_namespace_t* parent_ns) {
625 if (NativeBridgeInitialized()) {
626 if (isCompatibleWith(NAMESPACE_VERSION)) {
627 return callbacks->createNamespace(name,
628 ld_library_path,
629 default_library_path,
630 type,
631 permitted_when_isolated_path,
632 parent_ns);
633 } else {
634 ALOGE("not compatible with version %d, cannot create namespace %s", NAMESPACE_VERSION, name);
635 }
636 }
637
638 return nullptr;
639}
640
641bool NativeBridgeLinkNamespaces(native_bridge_namespace_t* from, native_bridge_namespace_t* to,
642 const char* shared_libs_sonames) {
643 if (NativeBridgeInitialized()) {
644 if (isCompatibleWith(NAMESPACE_VERSION)) {
645 return callbacks->linkNamespaces(from, to, shared_libs_sonames);
646 } else {
647 ALOGE("not compatible with version %d, cannot init namespace", NAMESPACE_VERSION);
648 }
649 }
650
651 return false;
652}
653
654native_bridge_namespace_t* NativeBridgeGetExportedNamespace(const char* name) {
655 if (!NativeBridgeInitialized()) {
656 return nullptr;
657 }
658
659 if (isCompatibleWith(RUNTIME_NAMESPACE_VERSION)) {
660 return callbacks->getExportedNamespace(name);
661 }
662
663 // sphal is vendor namespace name -> use v4 callback in the case NB callbacks
664 // are not compatible with v5
665 if (isCompatibleWith(VENDOR_NAMESPACE_VERSION) && name != nullptr && strcmp("sphal", name) == 0) {
666 return callbacks->getVendorNamespace();
667 }
668
669 return nullptr;
670}
671
672void* NativeBridgeLoadLibraryExt(const char* libpath, int flag, native_bridge_namespace_t* ns) {
673 if (NativeBridgeInitialized()) {
674 if (isCompatibleWith(NAMESPACE_VERSION)) {
675 return callbacks->loadLibraryExt(libpath, flag, ns);
676 } else {
677 ALOGE("not compatible with version %d, cannot load library in namespace", NAMESPACE_VERSION);
678 }
679 }
680 return nullptr;
681}
682
683} // extern "C"
684
685} // namespace android