• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2014 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #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 
34 namespace android {
35 
36 #ifdef __APPLE__
37 template <typename T>
UNUSED(const T &)38 void UNUSED(const T&) {}
39 #endif
40 
41 extern "C" {
42 
43 // Environment values required by the apps running with native bridge.
44 struct 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.
53 static constexpr const char* kNativeBridgeInterfaceSymbol = "NativeBridgeItf";
54 
55 enum 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 
63 static constexpr const char* kNotSetupString = "kNotSetup";
64 static constexpr const char* kOpenedString = "kOpened";
65 static constexpr const char* kPreInitializedString = "kPreInitialized";
66 static constexpr const char* kInitializedString = "kInitialized";
67 static constexpr const char* kClosedString = "kClosed";
68 
GetNativeBridgeStateString(NativeBridgeState state)69 static 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.
89 static 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.
95 enum 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,
106 };
107 
108 // Whether we had an error at some point.
109 static bool had_error = false;
110 
111 // Handle of the loaded library.
112 static void* native_bridge_handle = nullptr;
113 // Pointer to the callbacks. Available as soon as LoadNativeBridge succeeds, but only initialized
114 // later.
115 static const NativeBridgeCallbacks* callbacks = nullptr;
116 // Callbacks provided by the environment to the bridge. Passed to LoadNativeBridge.
117 static const NativeBridgeRuntimeCallbacks* runtime_callbacks = nullptr;
118 
119 // The app's code cache directory.
120 static char* app_code_cache_dir = nullptr;
121 
122 // Code cache directory (relative to the application private directory)
123 // Ideally we'd like to call into framework to retrieve this name. However that's considered an
124 // implementation detail and will require either hacks or consistent refactorings. We compromise
125 // and hard code the directory name again here.
126 static constexpr const char* kCodeCacheDir = "code_cache";
127 
128 // Characters allowed in a native bridge filename. The first character must
129 // be in [a-zA-Z] (expected 'l' for "libx"). The rest must be in [a-zA-Z0-9._-].
CharacterAllowed(char c,bool first)130 static bool CharacterAllowed(char c, bool first) {
131   if (first) {
132     return ('a' <= c && c <= 'z') || ('A' <= c && c <= 'Z');
133   } else {
134     return ('a' <= c && c <= 'z') || ('A' <= c && c <= 'Z') || ('0' <= c && c <= '9') ||
135            (c == '.') || (c == '_') || (c == '-');
136   }
137 }
138 
ReleaseAppCodeCacheDir()139 static void ReleaseAppCodeCacheDir() {
140   if (app_code_cache_dir != nullptr) {
141     delete[] app_code_cache_dir;
142     app_code_cache_dir = nullptr;
143   }
144 }
145 
146 // We only allow simple names for the library. It is supposed to be a file in
147 // /system/lib or /vendor/lib. Only allow a small range of characters, that is
148 // names consisting of [a-zA-Z0-9._-] and starting with [a-zA-Z].
NativeBridgeNameAcceptable(const char * nb_library_filename)149 bool NativeBridgeNameAcceptable(const char* nb_library_filename) {
150   const char* ptr = nb_library_filename;
151   if (*ptr == 0) {
152     // Emptry string. Allowed, means no native bridge.
153     return true;
154   } else {
155     // First character must be [a-zA-Z].
156     if (!CharacterAllowed(*ptr, true))  {
157       // Found an invalid fist character, don't accept.
158       ALOGE("Native bridge library %s has been rejected for first character %c",
159             nb_library_filename,
160             *ptr);
161       return false;
162     } else {
163       // For the rest, be more liberal.
164       ptr++;
165       while (*ptr != 0) {
166         if (!CharacterAllowed(*ptr, false)) {
167           // Found an invalid character, don't accept.
168           ALOGE("Native bridge library %s has been rejected for %c", nb_library_filename, *ptr);
169           return false;
170         }
171         ptr++;
172       }
173     }
174     return true;
175   }
176 }
177 
178 // The policy of invoking Nativebridge changed in v3 with/without namespace.
179 // Suggest Nativebridge implementation not maintain backward-compatible.
isCompatibleWith(const uint32_t version)180 static bool isCompatibleWith(const uint32_t version) {
181   // Libnativebridge is now designed to be forward-compatible. So only "0" is an unsupported
182   // version.
183   if (callbacks == nullptr || callbacks->version == 0 || version == 0) {
184     return false;
185   }
186 
187   // If this is a v2+ bridge, it may not be forwards- or backwards-compatible. Check.
188   if (callbacks->version >= SIGNAL_VERSION) {
189     return callbacks->isCompatibleWith(version);
190   }
191 
192   return true;
193 }
194 
CloseNativeBridge(bool with_error)195 static void CloseNativeBridge(bool with_error) {
196   state = NativeBridgeState::kClosed;
197   had_error |= with_error;
198   ReleaseAppCodeCacheDir();
199 }
200 
LoadNativeBridge(const char * nb_library_filename,const NativeBridgeRuntimeCallbacks * runtime_cbs)201 bool LoadNativeBridge(const char* nb_library_filename,
202                       const NativeBridgeRuntimeCallbacks* runtime_cbs) {
203   // We expect only one place that calls LoadNativeBridge: Runtime::Init. At that point we are not
204   // multi-threaded, so we do not need locking here.
205 
206   if (state != NativeBridgeState::kNotSetup) {
207     // Setup has been called before. Ignore this call.
208     if (nb_library_filename != nullptr) {  // Avoids some log-spam for dalvikvm.
209       ALOGW("Called LoadNativeBridge for an already set up native bridge. State is %s.",
210             GetNativeBridgeStateString(state));
211     }
212     // Note: counts as an error, even though the bridge may be functional.
213     had_error = true;
214     return false;
215   }
216 
217   if (nb_library_filename == nullptr || *nb_library_filename == 0) {
218     CloseNativeBridge(false);
219     return false;
220   } else {
221     if (!NativeBridgeNameAcceptable(nb_library_filename)) {
222       CloseNativeBridge(true);
223     } else {
224       // Try to open the library.
225       void* handle = dlopen(nb_library_filename, RTLD_LAZY);
226       if (handle != nullptr) {
227         callbacks = reinterpret_cast<NativeBridgeCallbacks*>(dlsym(handle,
228                                                                    kNativeBridgeInterfaceSymbol));
229         if (callbacks != nullptr) {
230           if (isCompatibleWith(NAMESPACE_VERSION)) {
231             // Store the handle for later.
232             native_bridge_handle = handle;
233           } else {
234             callbacks = nullptr;
235             dlclose(handle);
236             ALOGW("Unsupported native bridge interface.");
237           }
238         } else {
239           dlclose(handle);
240         }
241       }
242 
243       // Two failure conditions: could not find library (dlopen failed), or could not find native
244       // bridge interface (dlsym failed). Both are an error and close the native bridge.
245       if (callbacks == nullptr) {
246         CloseNativeBridge(true);
247       } else {
248         runtime_callbacks = runtime_cbs;
249         state = NativeBridgeState::kOpened;
250       }
251     }
252     return state == NativeBridgeState::kOpened;
253   }
254 }
255 
NeedsNativeBridge(const char * instruction_set)256 bool NeedsNativeBridge(const char* instruction_set) {
257   if (instruction_set == nullptr) {
258     ALOGE("Null instruction set in NeedsNativeBridge.");
259     return false;
260   }
261   return strncmp(instruction_set, ABI_STRING, strlen(ABI_STRING) + 1) != 0;
262 }
263 
PreInitializeNativeBridge(const char * app_data_dir_in,const char * instruction_set)264 bool PreInitializeNativeBridge(const char* app_data_dir_in, const char* instruction_set) {
265   if (state != NativeBridgeState::kOpened) {
266     ALOGE("Invalid state: native bridge is expected to be opened.");
267     CloseNativeBridge(true);
268     return false;
269   }
270 
271   if (app_data_dir_in == nullptr) {
272     ALOGE("Application private directory cannot be null.");
273     CloseNativeBridge(true);
274     return false;
275   }
276 
277   // Create the path to the application code cache directory.
278   // The memory will be release after Initialization or when the native bridge is closed.
279   const size_t len = strlen(app_data_dir_in) + strlen(kCodeCacheDir) + 2; // '\0' + '/'
280   app_code_cache_dir = new char[len];
281   snprintf(app_code_cache_dir, len, "%s/%s", app_data_dir_in, kCodeCacheDir);
282 
283   // Bind-mount /system/lib{,64}/<isa>/cpuinfo to /proc/cpuinfo.
284   // Failure is not fatal and will keep the native bridge in kPreInitialized.
285   state = NativeBridgeState::kPreInitialized;
286 
287 #ifndef __APPLE__
288   if (instruction_set == nullptr) {
289     return true;
290   }
291   size_t isa_len = strlen(instruction_set);
292   if (isa_len > 10) {
293     // 10 is a loose upper bound on the currently known instruction sets (a tight bound is 7 for
294     // x86_64 [including the trailing \0]). This is so we don't have to change here if there will
295     // be another instruction set in the future.
296     ALOGW("Instruction set %s is malformed, must be less than or equal to 10 characters.",
297           instruction_set);
298     return true;
299   }
300 
301   // If the file does not exist, the mount command will fail,
302   // so we save the extra file existence check.
303   char cpuinfo_path[1024];
304 
305 #if defined(__ANDROID__)
306   snprintf(cpuinfo_path, sizeof(cpuinfo_path), "/system/lib"
307 #ifdef __LP64__
308       "64"
309 #endif  // __LP64__
310       "/%s/cpuinfo", instruction_set);
311 #else   // !__ANDROID__
312   // To be able to test on the host, we hardwire a relative path.
313   snprintf(cpuinfo_path, sizeof(cpuinfo_path), "./cpuinfo");
314 #endif
315 
316   // Bind-mount.
317   if (TEMP_FAILURE_RETRY(mount(cpuinfo_path,        // Source.
318                                "/proc/cpuinfo",     // Target.
319                                nullptr,             // FS type.
320                                MS_BIND,             // Mount flags: bind mount.
321                                nullptr)) == -1) {   // "Data."
322     ALOGW("Failed to bind-mount %s as /proc/cpuinfo: %s", cpuinfo_path, strerror(errno));
323   }
324 #else  // __APPLE__
325   UNUSED(instruction_set);
326   ALOGW("Mac OS does not support bind-mounting. Host simulation of native bridge impossible.");
327 #endif
328 
329   return true;
330 }
331 
SetCpuAbi(JNIEnv * env,jclass build_class,const char * field,const char * value)332 static void SetCpuAbi(JNIEnv* env, jclass build_class, const char* field, const char* value) {
333   if (value != nullptr) {
334     jfieldID field_id = env->GetStaticFieldID(build_class, field, "Ljava/lang/String;");
335     if (field_id == nullptr) {
336       env->ExceptionClear();
337       ALOGW("Could not find %s field.", field);
338       return;
339     }
340 
341     jstring str = env->NewStringUTF(value);
342     if (str == nullptr) {
343       env->ExceptionClear();
344       ALOGW("Could not create string %s.", value);
345       return;
346     }
347 
348     env->SetStaticObjectField(build_class, field_id, str);
349   }
350 }
351 
352 // Set up the environment for the bridged app.
SetupEnvironment(const NativeBridgeCallbacks * callbacks,JNIEnv * env,const char * isa)353 static void SetupEnvironment(const NativeBridgeCallbacks* callbacks, JNIEnv* env, const char* isa) {
354   // Need a JNIEnv* to do anything.
355   if (env == nullptr) {
356     ALOGW("No JNIEnv* to set up app environment.");
357     return;
358   }
359 
360   // Query the bridge for environment values.
361   const struct NativeBridgeRuntimeValues* env_values = callbacks->getAppEnv(isa);
362   if (env_values == nullptr) {
363     return;
364   }
365 
366   // Keep the JNIEnv clean.
367   jint success = env->PushLocalFrame(16);  // That should be small and large enough.
368   if (success < 0) {
369     // Out of memory, really borked.
370     ALOGW("Out of memory while setting up app environment.");
371     env->ExceptionClear();
372     return;
373   }
374 
375   // Reset CPU_ABI & CPU_ABI2 to values required by the apps running with native bridge.
376   if (env_values->cpu_abi != nullptr || env_values->cpu_abi2 != nullptr ||
377       env_values->abi_count >= 0) {
378     jclass bclass_id = env->FindClass("android/os/Build");
379     if (bclass_id != nullptr) {
380       SetCpuAbi(env, bclass_id, "CPU_ABI", env_values->cpu_abi);
381       SetCpuAbi(env, bclass_id, "CPU_ABI2", env_values->cpu_abi2);
382     } else {
383       // For example in a host test environment.
384       env->ExceptionClear();
385       ALOGW("Could not find Build class.");
386     }
387   }
388 
389   if (env_values->os_arch != nullptr) {
390     jclass sclass_id = env->FindClass("java/lang/System");
391     if (sclass_id != nullptr) {
392       jmethodID set_prop_id = env->GetStaticMethodID(sclass_id, "setUnchangeableSystemProperty",
393           "(Ljava/lang/String;Ljava/lang/String;)V");
394       if (set_prop_id != nullptr) {
395         // Init os.arch to the value reqired by the apps running with native bridge.
396         env->CallStaticVoidMethod(sclass_id, set_prop_id, env->NewStringUTF("os.arch"),
397             env->NewStringUTF(env_values->os_arch));
398       } else {
399         env->ExceptionClear();
400         ALOGW("Could not find System#setUnchangeableSystemProperty.");
401       }
402     } else {
403       env->ExceptionClear();
404       ALOGW("Could not find System class.");
405     }
406   }
407 
408   // Make it pristine again.
409   env->PopLocalFrame(nullptr);
410 }
411 
InitializeNativeBridge(JNIEnv * env,const char * instruction_set)412 bool InitializeNativeBridge(JNIEnv* env, const char* instruction_set) {
413   // We expect only one place that calls InitializeNativeBridge: Runtime::DidForkFromZygote. At that
414   // point we are not multi-threaded, so we do not need locking here.
415 
416   if (state == NativeBridgeState::kPreInitialized) {
417     // Check for code cache: if it doesn't exist try to create it.
418     struct stat st;
419     if (stat(app_code_cache_dir, &st) == -1) {
420       if (errno == ENOENT) {
421         if (mkdir(app_code_cache_dir, S_IRWXU | S_IRWXG | S_IXOTH) == -1) {
422           ALOGW("Cannot create code cache directory %s: %s.", app_code_cache_dir, strerror(errno));
423           ReleaseAppCodeCacheDir();
424         }
425       } else {
426         ALOGW("Cannot stat code cache directory %s: %s.", app_code_cache_dir, strerror(errno));
427         ReleaseAppCodeCacheDir();
428       }
429     } else if (!S_ISDIR(st.st_mode)) {
430       ALOGW("Code cache is not a directory %s.", app_code_cache_dir);
431       ReleaseAppCodeCacheDir();
432     }
433 
434     // If we're still PreInitialized (dind't fail the code cache checks) try to initialize.
435     if (state == NativeBridgeState::kPreInitialized) {
436       if (callbacks->initialize(runtime_callbacks, app_code_cache_dir, instruction_set)) {
437         SetupEnvironment(callbacks, env, instruction_set);
438         state = NativeBridgeState::kInitialized;
439         // We no longer need the code cache path, release the memory.
440         ReleaseAppCodeCacheDir();
441       } else {
442         // Unload the library.
443         dlclose(native_bridge_handle);
444         CloseNativeBridge(true);
445       }
446     }
447   } else {
448     CloseNativeBridge(true);
449   }
450 
451   return state == NativeBridgeState::kInitialized;
452 }
453 
UnloadNativeBridge()454 void UnloadNativeBridge() {
455   // We expect only one place that calls UnloadNativeBridge: Runtime::DidForkFromZygote. At that
456   // point we are not multi-threaded, so we do not need locking here.
457 
458   switch(state) {
459     case NativeBridgeState::kOpened:
460     case NativeBridgeState::kPreInitialized:
461     case NativeBridgeState::kInitialized:
462       // Unload.
463       dlclose(native_bridge_handle);
464       CloseNativeBridge(false);
465       break;
466 
467     case NativeBridgeState::kNotSetup:
468       // Not even set up. Error.
469       CloseNativeBridge(true);
470       break;
471 
472     case NativeBridgeState::kClosed:
473       // Ignore.
474       break;
475   }
476 }
477 
NativeBridgeError()478 bool NativeBridgeError() {
479   return had_error;
480 }
481 
NativeBridgeAvailable()482 bool NativeBridgeAvailable() {
483   return state == NativeBridgeState::kOpened
484       || state == NativeBridgeState::kPreInitialized
485       || state == NativeBridgeState::kInitialized;
486 }
487 
NativeBridgeInitialized()488 bool NativeBridgeInitialized() {
489   // Calls of this are supposed to happen in a state where the native bridge is stable, i.e., after
490   // Runtime::DidForkFromZygote. In that case we do not need a lock.
491   return state == NativeBridgeState::kInitialized;
492 }
493 
NativeBridgeLoadLibrary(const char * libpath,int flag)494 void* NativeBridgeLoadLibrary(const char* libpath, int flag) {
495   if (NativeBridgeInitialized()) {
496     return callbacks->loadLibrary(libpath, flag);
497   }
498   return nullptr;
499 }
500 
NativeBridgeGetTrampoline(void * handle,const char * name,const char * shorty,uint32_t len)501 void* NativeBridgeGetTrampoline(void* handle, const char* name, const char* shorty,
502                                 uint32_t len) {
503   if (NativeBridgeInitialized()) {
504     return callbacks->getTrampoline(handle, name, shorty, len);
505   }
506   return nullptr;
507 }
508 
NativeBridgeIsSupported(const char * libpath)509 bool NativeBridgeIsSupported(const char* libpath) {
510   if (NativeBridgeInitialized()) {
511     return callbacks->isSupported(libpath);
512   }
513   return false;
514 }
515 
NativeBridgeGetVersion()516 uint32_t NativeBridgeGetVersion() {
517   if (NativeBridgeAvailable()) {
518     return callbacks->version;
519   }
520   return 0;
521 }
522 
NativeBridgeGetSignalHandler(int signal)523 NativeBridgeSignalHandlerFn NativeBridgeGetSignalHandler(int signal) {
524   if (NativeBridgeInitialized()) {
525     if (isCompatibleWith(SIGNAL_VERSION)) {
526       return callbacks->getSignalHandler(signal);
527     } else {
528       ALOGE("not compatible with version %d, cannot get signal handler", SIGNAL_VERSION);
529     }
530   }
531   return nullptr;
532 }
533 
NativeBridgeUnloadLibrary(void * handle)534 int NativeBridgeUnloadLibrary(void* handle) {
535   if (NativeBridgeInitialized()) {
536     if (isCompatibleWith(NAMESPACE_VERSION)) {
537       return callbacks->unloadLibrary(handle);
538     } else {
539       ALOGE("not compatible with version %d, cannot unload library", NAMESPACE_VERSION);
540     }
541   }
542   return -1;
543 }
544 
NativeBridgeGetError()545 const char* NativeBridgeGetError() {
546   if (NativeBridgeInitialized()) {
547     if (isCompatibleWith(NAMESPACE_VERSION)) {
548       return callbacks->getError();
549     } else {
550       return "native bridge implementation is not compatible with version 3, cannot get message";
551     }
552   }
553   return "native bridge is not initialized";
554 }
555 
NativeBridgeIsPathSupported(const char * path)556 bool NativeBridgeIsPathSupported(const char* path) {
557   if (NativeBridgeInitialized()) {
558     if (isCompatibleWith(NAMESPACE_VERSION)) {
559       return callbacks->isPathSupported(path);
560     } else {
561       ALOGE("not compatible with version %d, cannot check via library path", NAMESPACE_VERSION);
562     }
563   }
564   return false;
565 }
566 
NativeBridgeInitAnonymousNamespace(const char * public_ns_sonames,const char * anon_ns_library_path)567 bool NativeBridgeInitAnonymousNamespace(const char* public_ns_sonames,
568                                         const char* anon_ns_library_path) {
569   if (NativeBridgeInitialized()) {
570     if (isCompatibleWith(NAMESPACE_VERSION)) {
571       return callbacks->initAnonymousNamespace(public_ns_sonames, anon_ns_library_path);
572     } else {
573       ALOGE("not compatible with version %d, cannot init namespace", NAMESPACE_VERSION);
574     }
575   }
576 
577   return false;
578 }
579 
NativeBridgeCreateNamespace(const char * name,const char * ld_library_path,const char * default_library_path,uint64_t type,const char * permitted_when_isolated_path,native_bridge_namespace_t * parent_ns)580 native_bridge_namespace_t* NativeBridgeCreateNamespace(const char* name,
581                                                        const char* ld_library_path,
582                                                        const char* default_library_path,
583                                                        uint64_t type,
584                                                        const char* permitted_when_isolated_path,
585                                                        native_bridge_namespace_t* parent_ns) {
586   if (NativeBridgeInitialized()) {
587     if (isCompatibleWith(NAMESPACE_VERSION)) {
588       return callbacks->createNamespace(name,
589                                         ld_library_path,
590                                         default_library_path,
591                                         type,
592                                         permitted_when_isolated_path,
593                                         parent_ns);
594     } else {
595       ALOGE("not compatible with version %d, cannot create namespace %s", NAMESPACE_VERSION, name);
596     }
597   }
598 
599   return nullptr;
600 }
601 
NativeBridgeLinkNamespaces(native_bridge_namespace_t * from,native_bridge_namespace_t * to,const char * shared_libs_sonames)602 bool NativeBridgeLinkNamespaces(native_bridge_namespace_t* from, native_bridge_namespace_t* to,
603                                 const char* shared_libs_sonames) {
604   if (NativeBridgeInitialized()) {
605     if (isCompatibleWith(NAMESPACE_VERSION)) {
606       return callbacks->linkNamespaces(from, to, shared_libs_sonames);
607     } else {
608       ALOGE("not compatible with version %d, cannot init namespace", NAMESPACE_VERSION);
609     }
610   }
611 
612   return false;
613 }
614 
NativeBridgeGetExportedNamespace(const char * name)615 native_bridge_namespace_t* NativeBridgeGetExportedNamespace(const char* name) {
616   if (!NativeBridgeInitialized()) {
617     return nullptr;
618   }
619 
620   if (isCompatibleWith(RUNTIME_NAMESPACE_VERSION)) {
621     return callbacks->getExportedNamespace(name);
622   }
623 
624   // sphal is vendor namespace name -> use v4 callback in the case NB callbacks
625   // are not compatible with v5
626   if (isCompatibleWith(VENDOR_NAMESPACE_VERSION) && name != nullptr && strcmp("sphal", name) == 0) {
627     return callbacks->getVendorNamespace();
628   }
629 
630   return nullptr;
631 }
632 
NativeBridgeLoadLibraryExt(const char * libpath,int flag,native_bridge_namespace_t * ns)633 void* NativeBridgeLoadLibraryExt(const char* libpath, int flag, native_bridge_namespace_t* ns) {
634   if (NativeBridgeInitialized()) {
635     if (isCompatibleWith(NAMESPACE_VERSION)) {
636       return callbacks->loadLibraryExt(libpath, flag, ns);
637     } else {
638       ALOGE("not compatible with version %d, cannot load library in namespace", NAMESPACE_VERSION);
639     }
640   }
641   return nullptr;
642 }
643 
644 }  // extern "C"
645 
646 }  // namespace android
647