| Calin Juravle | 961ae12 | 2014-08-11 16:11:59 +0100 | [diff] [blame] | 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 |  | 
| Mark Salyzyn | cfd5b08 | 2016-10-17 14:28:00 -0700 | [diff] [blame] | 17 | #define LOG_TAG "nativebridge" | 
 | 18 |  | 
| Calin Juravle | 961ae12 | 2014-08-11 16:11:59 +0100 | [diff] [blame] | 19 | #include "nativebridge/native_bridge.h" | 
 | 20 |  | 
 | 21 | #include <dlfcn.h> | 
| jgu21 | ab0da5a | 2014-09-10 06:58:32 -0400 | [diff] [blame] | 22 | #include <errno.h> | 
 | 23 | #include <fcntl.h> | 
| Calin Juravle | 961ae12 | 2014-08-11 16:11:59 +0100 | [diff] [blame] | 24 | #include <stdio.h> | 
| jgu21 | ab0da5a | 2014-09-10 06:58:32 -0400 | [diff] [blame] | 25 | #include <sys/mount.h> | 
 | 26 | #include <sys/stat.h> | 
| Mark Salyzyn | cfd5b08 | 2016-10-17 14:28:00 -0700 | [diff] [blame] | 27 | #include <unistd.h> | 
| Calin Juravle | 961ae12 | 2014-08-11 16:11:59 +0100 | [diff] [blame] | 28 |  | 
| Mark Salyzyn | 66ce3e0 | 2016-09-28 10:07:20 -0700 | [diff] [blame] | 29 | #include <cstring> | 
 | 30 |  | 
| dimitry | b6ba817 | 2017-08-23 10:25:22 +0200 | [diff] [blame] | 31 | #include <android-base/macros.h> | 
| Mark Salyzyn | 30f991f | 2017-01-10 13:19:54 -0800 | [diff] [blame] | 32 | #include <log/log.h> | 
| Calin Juravle | 961ae12 | 2014-08-11 16:11:59 +0100 | [diff] [blame] | 33 |  | 
 | 34 | namespace android { | 
 | 35 |  | 
| Nicolas Geoffray | e7de616 | 2019-01-14 14:12:03 +0000 | [diff] [blame] | 36 | #ifdef __APPLE__ | 
 | 37 | template <typename T> | 
 | 38 | void UNUSED(const T&) {} | 
 | 39 | #endif | 
 | 40 |  | 
| Nicolas Geoffray | d9b4d9b | 2019-01-10 16:27:54 +0000 | [diff] [blame] | 41 | extern "C" { | 
 | 42 |  | 
| jgu21 | ab0da5a | 2014-09-10 06:58:32 -0400 | [diff] [blame] | 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 |  | 
| Calin Juravle | 961ae12 | 2014-08-11 16:11:59 +0100 | [diff] [blame] | 52 | // The symbol name exposed by native-bridge with the type of NativeBridgeCallbacks. | 
 | 53 | static constexpr const char* kNativeBridgeInterfaceSymbol = "NativeBridgeItf"; | 
 | 54 |  | 
| Andreas Gampe | 035bd75 | 2014-09-02 21:17:03 -0700 | [diff] [blame] | 55 | enum class NativeBridgeState { | 
 | 56 |   kNotSetup,                        // Initial state. | 
 | 57 |   kOpened,                          // After successful dlopen. | 
| Calin Juravle | f9d9e2a | 2014-10-17 13:45:39 +0100 | [diff] [blame] | 58 |   kPreInitialized,                  // After successful pre-initialization. | 
| Andreas Gampe | 035bd75 | 2014-09-02 21:17:03 -0700 | [diff] [blame] | 59 |   kInitialized,                     // After successful initialization. | 
 | 60 |   kClosed                           // Closed or errors. | 
 | 61 | }; | 
| Calin Juravle | 961ae12 | 2014-08-11 16:11:59 +0100 | [diff] [blame] | 62 |  | 
| Calin Juravle | f9d9e2a | 2014-10-17 13:45:39 +0100 | [diff] [blame] | 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"; | 
| Andreas Gampe | 035bd75 | 2014-09-02 21:17:03 -0700 | [diff] [blame] | 68 |  | 
 | 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 |  | 
| Calin Juravle | f9d9e2a | 2014-10-17 13:45:39 +0100 | [diff] [blame] | 77 |     case NativeBridgeState::kPreInitialized: | 
 | 78 |       return kPreInitializedString; | 
 | 79 |  | 
| Andreas Gampe | 035bd75 | 2014-09-02 21:17:03 -0700 | [diff] [blame] | 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 |  | 
| Zhenhua WANG | f2804e5 | 2016-05-30 11:16:08 +0800 | [diff] [blame] | 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 { | 
| Dimitry Ivanov | af0264b | 2017-05-01 15:12:49 -0700 | [diff] [blame] | 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, | 
| dimitry | d2ace38 | 2019-02-04 15:06:43 +0100 | [diff] [blame] | 104 |   // The version with runtime namespaces | 
 | 105 |   RUNTIME_NAMESPACE_VERSION = 5, | 
| Zhenhua WANG | f2804e5 | 2016-05-30 11:16:08 +0800 | [diff] [blame] | 106 | }; | 
 | 107 |  | 
| Andreas Gampe | 049249c | 2014-08-19 22:31:31 -0700 | [diff] [blame] | 108 | // Whether we had an error at some point. | 
 | 109 | static bool had_error = false; | 
| Calin Juravle | 961ae12 | 2014-08-11 16:11:59 +0100 | [diff] [blame] | 110 |  | 
| Andreas Gampe | 035bd75 | 2014-09-02 21:17:03 -0700 | [diff] [blame] | 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. | 
| Andreas Gampe | a6ac9ce | 2015-04-30 20:39:12 -0700 | [diff] [blame] | 115 | static const NativeBridgeCallbacks* callbacks = nullptr; | 
| Andreas Gampe | 035bd75 | 2014-09-02 21:17:03 -0700 | [diff] [blame] | 116 | // Callbacks provided by the environment to the bridge. Passed to LoadNativeBridge. | 
| Calin Juravle | 961ae12 | 2014-08-11 16:11:59 +0100 | [diff] [blame] | 117 | static const NativeBridgeRuntimeCallbacks* runtime_callbacks = nullptr; | 
 | 118 |  | 
| Calin Juravle | f9d9e2a | 2014-10-17 13:45:39 +0100 | [diff] [blame] | 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"; | 
| jgu21 | ab0da5a | 2014-09-10 06:58:32 -0400 | [diff] [blame] | 127 |  | 
| Andreas Gampe | 049249c | 2014-08-19 22:31:31 -0700 | [diff] [blame] | 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._-]. | 
 | 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 |  | 
| jgu21 | cef898f | 2015-07-02 12:02:11 +0800 | [diff] [blame] | 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 |  | 
| Andreas Gampe | 049249c | 2014-08-19 22:31:31 -0700 | [diff] [blame] | 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]. | 
 | 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. | 
| Andreas Gampe | a6ac9ce | 2015-04-30 20:39:12 -0700 | [diff] [blame] | 158 |       ALOGE("Native bridge library %s has been rejected for first character %c", | 
 | 159 |             nb_library_filename, | 
 | 160 |             *ptr); | 
| Andreas Gampe | 049249c | 2014-08-19 22:31:31 -0700 | [diff] [blame] | 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 |  | 
| Zhenhua WANG | f2804e5 | 2016-05-30 11:16:08 +0800 | [diff] [blame] | 178 | // The policy of invoking Nativebridge changed in v3 with/without namespace. | 
 | 179 | // Suggest Nativebridge implementation not maintain backward-compatible. | 
 | 180 | static bool isCompatibleWith(const uint32_t version) { | 
| Andreas Gampe | a6ac9ce | 2015-04-30 20:39:12 -0700 | [diff] [blame] | 181 |   // Libnativebridge is now designed to be forward-compatible. So only "0" is an unsupported | 
 | 182 |   // version. | 
| Zhenhua WANG | f2804e5 | 2016-05-30 11:16:08 +0800 | [diff] [blame] | 183 |   if (callbacks == nullptr || callbacks->version == 0 || version == 0) { | 
| Andreas Gampe | a6ac9ce | 2015-04-30 20:39:12 -0700 | [diff] [blame] | 184 |     return false; | 
 | 185 |   } | 
 | 186 |  | 
 | 187 |   // If this is a v2+ bridge, it may not be forwards- or backwards-compatible. Check. | 
| Zhenhua WANG | f2804e5 | 2016-05-30 11:16:08 +0800 | [diff] [blame] | 188 |   if (callbacks->version >= SIGNAL_VERSION) { | 
 | 189 |     return callbacks->isCompatibleWith(version); | 
| Andreas Gampe | a6ac9ce | 2015-04-30 20:39:12 -0700 | [diff] [blame] | 190 |   } | 
 | 191 |  | 
 | 192 |   return true; | 
| jgu21 | ab0da5a | 2014-09-10 06:58:32 -0400 | [diff] [blame] | 193 | } | 
 | 194 |  | 
| Calin Juravle | f9d9e2a | 2014-10-17 13:45:39 +0100 | [diff] [blame] | 195 | static void CloseNativeBridge(bool with_error) { | 
 | 196 |   state = NativeBridgeState::kClosed; | 
 | 197 |   had_error |= with_error; | 
| jgu21 | cef898f | 2015-07-02 12:02:11 +0800 | [diff] [blame] | 198 |   ReleaseAppCodeCacheDir(); | 
| Calin Juravle | f9d9e2a | 2014-10-17 13:45:39 +0100 | [diff] [blame] | 199 | } | 
 | 200 |  | 
| Andreas Gampe | 035bd75 | 2014-09-02 21:17:03 -0700 | [diff] [blame] | 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. | 
| Calin Juravle | 961ae12 | 2014-08-11 16:11:59 +0100 | [diff] [blame] | 205 |  | 
| Andreas Gampe | 035bd75 | 2014-09-02 21:17:03 -0700 | [diff] [blame] | 206 |   if (state != NativeBridgeState::kNotSetup) { | 
| Andreas Gampe | 049249c | 2014-08-19 22:31:31 -0700 | [diff] [blame] | 207 |     // Setup has been called before. Ignore this call. | 
| jgu21 | ab0da5a | 2014-09-10 06:58:32 -0400 | [diff] [blame] | 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 |     } | 
| Andreas Gampe | 049249c | 2014-08-19 22:31:31 -0700 | [diff] [blame] | 212 |     // Note: counts as an error, even though the bridge may be functional. | 
 | 213 |     had_error = true; | 
| Andreas Gampe | 049249c | 2014-08-19 22:31:31 -0700 | [diff] [blame] | 214 |     return false; | 
 | 215 |   } | 
 | 216 |  | 
| Andreas Gampe | 035bd75 | 2014-09-02 21:17:03 -0700 | [diff] [blame] | 217 |   if (nb_library_filename == nullptr || *nb_library_filename == 0) { | 
| Calin Juravle | f9d9e2a | 2014-10-17 13:45:39 +0100 | [diff] [blame] | 218 |     CloseNativeBridge(false); | 
 | 219 |     return false; | 
| Andreas Gampe | 035bd75 | 2014-09-02 21:17:03 -0700 | [diff] [blame] | 220 |   } else { | 
 | 221 |     if (!NativeBridgeNameAcceptable(nb_library_filename)) { | 
| Calin Juravle | f9d9e2a | 2014-10-17 13:45:39 +0100 | [diff] [blame] | 222 |       CloseNativeBridge(true); | 
| Andreas Gampe | 035bd75 | 2014-09-02 21:17:03 -0700 | [diff] [blame] | 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) { | 
| Zhenhua WANG | f2804e5 | 2016-05-30 11:16:08 +0800 | [diff] [blame] | 230 |           if (isCompatibleWith(NAMESPACE_VERSION)) { | 
| jgu21 | ab0da5a | 2014-09-10 06:58:32 -0400 | [diff] [blame] | 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 |           } | 
| Andreas Gampe | 035bd75 | 2014-09-02 21:17:03 -0700 | [diff] [blame] | 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) { | 
| Calin Juravle | f9d9e2a | 2014-10-17 13:45:39 +0100 | [diff] [blame] | 246 |         CloseNativeBridge(true); | 
| Andreas Gampe | 035bd75 | 2014-09-02 21:17:03 -0700 | [diff] [blame] | 247 |       } else { | 
 | 248 |         runtime_callbacks = runtime_cbs; | 
 | 249 |         state = NativeBridgeState::kOpened; | 
 | 250 |       } | 
 | 251 |     } | 
 | 252 |     return state == NativeBridgeState::kOpened; | 
 | 253 |   } | 
 | 254 | } | 
 | 255 |  | 
| jgu21 | ab0da5a | 2014-09-10 06:58:32 -0400 | [diff] [blame] | 256 | bool NeedsNativeBridge(const char* instruction_set) { | 
| Andreas Gampe | 04054e2 | 2014-09-25 22:33:01 -0700 | [diff] [blame] | 257 |   if (instruction_set == nullptr) { | 
 | 258 |     ALOGE("Null instruction set in NeedsNativeBridge."); | 
 | 259 |     return false; | 
 | 260 |   } | 
| dimitry | b6ba817 | 2017-08-23 10:25:22 +0200 | [diff] [blame] | 261 |   return strncmp(instruction_set, ABI_STRING, strlen(ABI_STRING) + 1) != 0; | 
| jgu21 | ab0da5a | 2014-09-10 06:58:32 -0400 | [diff] [blame] | 262 | } | 
 | 263 |  | 
| Calin Juravle | f9d9e2a | 2014-10-17 13:45:39 +0100 | [diff] [blame] | 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; | 
| jgu21 | ab0da5a | 2014-09-10 06:58:32 -0400 | [diff] [blame] | 269 |   } | 
 | 270 |  | 
| Calin Juravle | f9d9e2a | 2014-10-17 13:45:39 +0100 | [diff] [blame] | 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; | 
| jgu21 | ab0da5a | 2014-09-10 06:58:32 -0400 | [diff] [blame] | 286 |  | 
| Andreas Gampe | 962eb40 | 2014-09-24 16:36:17 -0700 | [diff] [blame] | 287 | #ifndef __APPLE__ | 
| jgu21 | ab0da5a | 2014-09-10 06:58:32 -0400 | [diff] [blame] | 288 |   if (instruction_set == nullptr) { | 
| Calin Juravle | f9d9e2a | 2014-10-17 13:45:39 +0100 | [diff] [blame] | 289 |     return true; | 
| jgu21 | ab0da5a | 2014-09-10 06:58:32 -0400 | [diff] [blame] | 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. | 
| Andreas Gampe | 2f71cb2 | 2014-09-25 21:34:25 -0700 | [diff] [blame] | 296 |     ALOGW("Instruction set %s is malformed, must be less than or equal to 10 characters.", | 
 | 297 |           instruction_set); | 
| Calin Juravle | f9d9e2a | 2014-10-17 13:45:39 +0100 | [diff] [blame] | 298 |     return true; | 
| jgu21 | ab0da5a | 2014-09-10 06:58:32 -0400 | [diff] [blame] | 299 |   } | 
 | 300 |  | 
| Calin Juravle | f9d9e2a | 2014-10-17 13:45:39 +0100 | [diff] [blame] | 301 |   // If the file does not exist, the mount command will fail, | 
 | 302 |   // so we save the extra file existence check. | 
| jgu21 | ab0da5a | 2014-09-10 06:58:32 -0400 | [diff] [blame] | 303 |   char cpuinfo_path[1024]; | 
 | 304 |  | 
| Elliott Hughes | 9b828ad | 2015-07-30 08:47:35 -0700 | [diff] [blame] | 305 | #if defined(__ANDROID__) | 
| Andreas Gampe | 04054e2 | 2014-09-25 22:33:01 -0700 | [diff] [blame] | 306 |   snprintf(cpuinfo_path, sizeof(cpuinfo_path), "/system/lib" | 
| jgu21 | ab0da5a | 2014-09-10 06:58:32 -0400 | [diff] [blame] | 307 | #ifdef __LP64__ | 
| Andreas Gampe | 04054e2 | 2014-09-25 22:33:01 -0700 | [diff] [blame] | 308 |       "64" | 
 | 309 | #endif  // __LP64__ | 
 | 310 |       "/%s/cpuinfo", instruction_set); | 
| Elliott Hughes | 9b828ad | 2015-07-30 08:47:35 -0700 | [diff] [blame] | 311 | #else   // !__ANDROID__ | 
| Andreas Gampe | 04054e2 | 2014-09-25 22:33:01 -0700 | [diff] [blame] | 312 |   // To be able to test on the host, we hardwire a relative path. | 
 | 313 |   snprintf(cpuinfo_path, sizeof(cpuinfo_path), "./cpuinfo"); | 
| jgu21 | ab0da5a | 2014-09-10 06:58:32 -0400 | [diff] [blame] | 314 | #endif | 
| jgu21 | ab0da5a | 2014-09-10 06:58:32 -0400 | [diff] [blame] | 315 |  | 
 | 316 |   // Bind-mount. | 
| Andreas Gampe | 2f71cb2 | 2014-09-25 21:34:25 -0700 | [diff] [blame] | 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." | 
| Andreas Gampe | 04054e2 | 2014-09-25 22:33:01 -0700 | [diff] [blame] | 322 |     ALOGW("Failed to bind-mount %s as /proc/cpuinfo: %s", cpuinfo_path, strerror(errno)); | 
| jgu21 | ab0da5a | 2014-09-10 06:58:32 -0400 | [diff] [blame] | 323 |   } | 
| Calin Juravle | f9d9e2a | 2014-10-17 13:45:39 +0100 | [diff] [blame] | 324 | #else  // __APPLE__ | 
| Andreas Gampe | 4390a63 | 2014-09-24 18:53:26 -0700 | [diff] [blame] | 325 |   UNUSED(instruction_set); | 
| Andreas Gampe | 962eb40 | 2014-09-24 16:36:17 -0700 | [diff] [blame] | 326 |   ALOGW("Mac OS does not support bind-mounting. Host simulation of native bridge impossible."); | 
 | 327 | #endif | 
| Calin Juravle | f9d9e2a | 2014-10-17 13:45:39 +0100 | [diff] [blame] | 328 |  | 
 | 329 |   return true; | 
| jgu21 | ab0da5a | 2014-09-10 06:58:32 -0400 | [diff] [blame] | 330 | } | 
 | 331 |  | 
 | 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 |  | 
| jgu21 | ab0da5a | 2014-09-10 06:58:32 -0400 | [diff] [blame] | 352 | // Set up the environment for the bridged app. | 
| Andreas Gampe | a6ac9ce | 2015-04-30 20:39:12 -0700 | [diff] [blame] | 353 | static void SetupEnvironment(const NativeBridgeCallbacks* callbacks, JNIEnv* env, const char* isa) { | 
| jgu21 | ab0da5a | 2014-09-10 06:58:32 -0400 | [diff] [blame] | 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); | 
| jgu21 | ab0da5a | 2014-09-10 06:58:32 -0400 | [diff] [blame] | 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) { | 
| Narayan Kamath | 484c55b | 2015-02-10 15:33:36 +0000 | [diff] [blame] | 392 |       jmethodID set_prop_id = env->GetStaticMethodID(sclass_id, "setUnchangeableSystemProperty", | 
| Calin Juravle | c3eb431 | 2014-10-01 17:29:19 +0100 | [diff] [blame] | 393 |           "(Ljava/lang/String;Ljava/lang/String;)V"); | 
| jgu21 | ab0da5a | 2014-09-10 06:58:32 -0400 | [diff] [blame] | 394 |       if (set_prop_id != nullptr) { | 
| Calin Juravle | c3eb431 | 2014-10-01 17:29:19 +0100 | [diff] [blame] | 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"), | 
| jgu21 | ab0da5a | 2014-09-10 06:58:32 -0400 | [diff] [blame] | 397 |             env->NewStringUTF(env_values->os_arch)); | 
 | 398 |       } else { | 
 | 399 |         env->ExceptionClear(); | 
| Narayan Kamath | 484c55b | 2015-02-10 15:33:36 +0000 | [diff] [blame] | 400 |         ALOGW("Could not find System#setUnchangeableSystemProperty."); | 
| jgu21 | ab0da5a | 2014-09-10 06:58:32 -0400 | [diff] [blame] | 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 |  | 
 | 412 | bool InitializeNativeBridge(JNIEnv* env, const char* instruction_set) { | 
| Andreas Gampe | 035bd75 | 2014-09-02 21:17:03 -0700 | [diff] [blame] | 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 |  | 
| Calin Juravle | f9d9e2a | 2014-10-17 13:45:39 +0100 | [diff] [blame] | 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) { | 
| jgu21 | cef898f | 2015-07-02 12:02:11 +0800 | [diff] [blame] | 422 |           ALOGW("Cannot create code cache directory %s: %s.", app_code_cache_dir, strerror(errno)); | 
 | 423 |           ReleaseAppCodeCacheDir(); | 
| Calin Juravle | f9d9e2a | 2014-10-17 13:45:39 +0100 | [diff] [blame] | 424 |         } | 
 | 425 |       } else { | 
| jgu21 | cef898f | 2015-07-02 12:02:11 +0800 | [diff] [blame] | 426 |         ALOGW("Cannot stat code cache directory %s: %s.", app_code_cache_dir, strerror(errno)); | 
 | 427 |         ReleaseAppCodeCacheDir(); | 
| Calin Juravle | f9d9e2a | 2014-10-17 13:45:39 +0100 | [diff] [blame] | 428 |       } | 
 | 429 |     } else if (!S_ISDIR(st.st_mode)) { | 
| jgu21 | cef898f | 2015-07-02 12:02:11 +0800 | [diff] [blame] | 430 |       ALOGW("Code cache is not a directory %s.", app_code_cache_dir); | 
 | 431 |       ReleaseAppCodeCacheDir(); | 
| Calin Juravle | f9d9e2a | 2014-10-17 13:45:39 +0100 | [diff] [blame] | 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. | 
| jgu21 | cef898f | 2015-07-02 12:02:11 +0800 | [diff] [blame] | 440 |         ReleaseAppCodeCacheDir(); | 
| Calin Juravle | f9d9e2a | 2014-10-17 13:45:39 +0100 | [diff] [blame] | 441 |       } else { | 
 | 442 |         // Unload the library. | 
 | 443 |         dlclose(native_bridge_handle); | 
 | 444 |         CloseNativeBridge(true); | 
 | 445 |       } | 
| Calin Juravle | 961ae12 | 2014-08-11 16:11:59 +0100 | [diff] [blame] | 446 |     } | 
| Andreas Gampe | 049249c | 2014-08-19 22:31:31 -0700 | [diff] [blame] | 447 |   } else { | 
| Calin Juravle | f9d9e2a | 2014-10-17 13:45:39 +0100 | [diff] [blame] | 448 |     CloseNativeBridge(true); | 
| Calin Juravle | 961ae12 | 2014-08-11 16:11:59 +0100 | [diff] [blame] | 449 |   } | 
 | 450 |  | 
| Andreas Gampe | 035bd75 | 2014-09-02 21:17:03 -0700 | [diff] [blame] | 451 |   return state == NativeBridgeState::kInitialized; | 
 | 452 | } | 
| Calin Juravle | 961ae12 | 2014-08-11 16:11:59 +0100 | [diff] [blame] | 453 |  | 
| Andreas Gampe | 035bd75 | 2014-09-02 21:17:03 -0700 | [diff] [blame] | 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: | 
| Calin Juravle | f9d9e2a | 2014-10-17 13:45:39 +0100 | [diff] [blame] | 460 |     case NativeBridgeState::kPreInitialized: | 
| Andreas Gampe | 035bd75 | 2014-09-02 21:17:03 -0700 | [diff] [blame] | 461 |     case NativeBridgeState::kInitialized: | 
 | 462 |       // Unload. | 
 | 463 |       dlclose(native_bridge_handle); | 
| Calin Juravle | f9d9e2a | 2014-10-17 13:45:39 +0100 | [diff] [blame] | 464 |       CloseNativeBridge(false); | 
| Andreas Gampe | 035bd75 | 2014-09-02 21:17:03 -0700 | [diff] [blame] | 465 |       break; | 
 | 466 |  | 
 | 467 |     case NativeBridgeState::kNotSetup: | 
 | 468 |       // Not even set up. Error. | 
| Calin Juravle | f9d9e2a | 2014-10-17 13:45:39 +0100 | [diff] [blame] | 469 |       CloseNativeBridge(true); | 
| Andreas Gampe | 035bd75 | 2014-09-02 21:17:03 -0700 | [diff] [blame] | 470 |       break; | 
 | 471 |  | 
 | 472 |     case NativeBridgeState::kClosed: | 
 | 473 |       // Ignore. | 
 | 474 |       break; | 
 | 475 |   } | 
| Calin Juravle | 961ae12 | 2014-08-11 16:11:59 +0100 | [diff] [blame] | 476 | } | 
 | 477 |  | 
| Andreas Gampe | 049249c | 2014-08-19 22:31:31 -0700 | [diff] [blame] | 478 | bool NativeBridgeError() { | 
 | 479 |   return had_error; | 
 | 480 | } | 
 | 481 |  | 
 | 482 | bool NativeBridgeAvailable() { | 
| Calin Juravle | f9d9e2a | 2014-10-17 13:45:39 +0100 | [diff] [blame] | 483 |   return state == NativeBridgeState::kOpened | 
 | 484 |       || state == NativeBridgeState::kPreInitialized | 
 | 485 |       || state == NativeBridgeState::kInitialized; | 
| Andreas Gampe | 035bd75 | 2014-09-02 21:17:03 -0700 | [diff] [blame] | 486 | } | 
 | 487 |  | 
 | 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; | 
| Andreas Gampe | 049249c | 2014-08-19 22:31:31 -0700 | [diff] [blame] | 492 | } | 
 | 493 |  | 
| Calin Juravle | 961ae12 | 2014-08-11 16:11:59 +0100 | [diff] [blame] | 494 | void* NativeBridgeLoadLibrary(const char* libpath, int flag) { | 
| Andreas Gampe | 035bd75 | 2014-09-02 21:17:03 -0700 | [diff] [blame] | 495 |   if (NativeBridgeInitialized()) { | 
| Calin Juravle | 961ae12 | 2014-08-11 16:11:59 +0100 | [diff] [blame] | 496 |     return callbacks->loadLibrary(libpath, flag); | 
 | 497 |   } | 
 | 498 |   return nullptr; | 
 | 499 | } | 
 | 500 |  | 
 | 501 | void* NativeBridgeGetTrampoline(void* handle, const char* name, const char* shorty, | 
 | 502 |                                 uint32_t len) { | 
| Andreas Gampe | 035bd75 | 2014-09-02 21:17:03 -0700 | [diff] [blame] | 503 |   if (NativeBridgeInitialized()) { | 
| Calin Juravle | 961ae12 | 2014-08-11 16:11:59 +0100 | [diff] [blame] | 504 |     return callbacks->getTrampoline(handle, name, shorty, len); | 
 | 505 |   } | 
 | 506 |   return nullptr; | 
 | 507 | } | 
 | 508 |  | 
 | 509 | bool NativeBridgeIsSupported(const char* libpath) { | 
| Andreas Gampe | 035bd75 | 2014-09-02 21:17:03 -0700 | [diff] [blame] | 510 |   if (NativeBridgeInitialized()) { | 
| Calin Juravle | 961ae12 | 2014-08-11 16:11:59 +0100 | [diff] [blame] | 511 |     return callbacks->isSupported(libpath); | 
 | 512 |   } | 
 | 513 |   return false; | 
 | 514 | } | 
 | 515 |  | 
| Andreas Gampe | a6ac9ce | 2015-04-30 20:39:12 -0700 | [diff] [blame] | 516 | uint32_t NativeBridgeGetVersion() { | 
 | 517 |   if (NativeBridgeAvailable()) { | 
 | 518 |     return callbacks->version; | 
 | 519 |   } | 
 | 520 |   return 0; | 
 | 521 | } | 
 | 522 |  | 
 | 523 | NativeBridgeSignalHandlerFn NativeBridgeGetSignalHandler(int signal) { | 
| Zhenhua WANG | f2804e5 | 2016-05-30 11:16:08 +0800 | [diff] [blame] | 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 |  | 
 | 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 |  | 
| Dimitry Ivanov | d836ab0 | 2016-11-02 18:03:10 -0700 | [diff] [blame] | 545 | const char* NativeBridgeGetError() { | 
| Zhenhua WANG | f2804e5 | 2016-05-30 11:16:08 +0800 | [diff] [blame] | 546 |   if (NativeBridgeInitialized()) { | 
 | 547 |     if (isCompatibleWith(NAMESPACE_VERSION)) { | 
 | 548 |       return callbacks->getError(); | 
 | 549 |     } else { | 
| Dimitry Ivanov | d836ab0 | 2016-11-02 18:03:10 -0700 | [diff] [blame] | 550 |       return "native bridge implementation is not compatible with version 3, cannot get message"; | 
| Zhenhua WANG | f2804e5 | 2016-05-30 11:16:08 +0800 | [diff] [blame] | 551 |     } | 
 | 552 |   } | 
| Dimitry Ivanov | d836ab0 | 2016-11-02 18:03:10 -0700 | [diff] [blame] | 553 |   return "native bridge is not initialized"; | 
| Zhenhua WANG | f2804e5 | 2016-05-30 11:16:08 +0800 | [diff] [blame] | 554 | } | 
 | 555 |  | 
 | 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 |  | 
| Zhenhua WANG | e8fb11d | 2017-02-27 10:14:45 +0800 | [diff] [blame] | 567 | bool NativeBridgeInitAnonymousNamespace(const char* public_ns_sonames, | 
 | 568 |                                         const char* anon_ns_library_path) { | 
| Zhenhua WANG | f2804e5 | 2016-05-30 11:16:08 +0800 | [diff] [blame] | 569 |   if (NativeBridgeInitialized()) { | 
 | 570 |     if (isCompatibleWith(NAMESPACE_VERSION)) { | 
| Zhenhua WANG | e8fb11d | 2017-02-27 10:14:45 +0800 | [diff] [blame] | 571 |       return callbacks->initAnonymousNamespace(public_ns_sonames, anon_ns_library_path); | 
| Zhenhua WANG | f2804e5 | 2016-05-30 11:16:08 +0800 | [diff] [blame] | 572 |     } else { | 
 | 573 |       ALOGE("not compatible with version %d, cannot init namespace", NAMESPACE_VERSION); | 
 | 574 |     } | 
 | 575 |   } | 
 | 576 |  | 
 | 577 |   return false; | 
 | 578 | } | 
 | 579 |  | 
 | 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 |  | 
| Zhenhua WANG | e8fb11d | 2017-02-27 10:14:45 +0800 | [diff] [blame] | 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 |  | 
| dimitry | d2ace38 | 2019-02-04 15:06:43 +0100 | [diff] [blame] | 615 | native_bridge_namespace_t* NativeBridgeGetExportedNamespace(const char* name) { | 
 | 616 |   if (!NativeBridgeInitialized()) { | 
| Dimitry Ivanov | af0264b | 2017-05-01 15:12:49 -0700 | [diff] [blame] | 617 |     return nullptr; | 
 | 618 |   } | 
 | 619 |  | 
| dimitry | d2ace38 | 2019-02-04 15:06:43 +0100 | [diff] [blame] | 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; | 
| Dimitry Ivanov | af0264b | 2017-05-01 15:12:49 -0700 | [diff] [blame] | 631 | } | 
 | 632 |  | 
| Zhenhua WANG | f2804e5 | 2016-05-30 11:16:08 +0800 | [diff] [blame] | 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 |     } | 
| Andreas Gampe | a6ac9ce | 2015-04-30 20:39:12 -0700 | [diff] [blame] | 640 |   } | 
 | 641 |   return nullptr; | 
 | 642 | } | 
 | 643 |  | 
| Nicolas Geoffray | d9b4d9b | 2019-01-10 16:27:54 +0000 | [diff] [blame] | 644 | }  // extern "C" | 
 | 645 |  | 
 | 646 | }  // namespace android |