Orion Hodson | 9b16e34 | 2019-10-09 13:29:16 +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 | |
| 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> |
| 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 | |
| 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, |
Lev Rumyantsev | abafbe7 | 2019-12-13 15:49:37 -0800 | [diff] [blame] | 106 | // The version with pre-zygote-fork hook to support app-zygotes. |
| 107 | PRE_ZYGOTE_FORK_VERSION = 6, |
Orion Hodson | 9b16e34 | 2019-10-09 13:29:16 +0100 | [diff] [blame] | 108 | }; |
| 109 | |
| 110 | // Whether we had an error at some point. |
| 111 | static bool had_error = false; |
| 112 | |
| 113 | // Handle of the loaded library. |
| 114 | static void* native_bridge_handle = nullptr; |
| 115 | // Pointer to the callbacks. Available as soon as LoadNativeBridge succeeds, but only initialized |
| 116 | // later. |
| 117 | static const NativeBridgeCallbacks* callbacks = nullptr; |
| 118 | // Callbacks provided by the environment to the bridge. Passed to LoadNativeBridge. |
| 119 | static const NativeBridgeRuntimeCallbacks* runtime_callbacks = nullptr; |
| 120 | |
| 121 | // The app's code cache directory. |
| 122 | static 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. |
| 128 | static 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._-]. |
| 132 | static 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 | |
| 141 | static 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]. |
| 151 | bool 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. |
| 182 | static 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 | |
| 197 | static void CloseNativeBridge(bool with_error) { |
| 198 | state = NativeBridgeState::kClosed; |
| 199 | had_error |= with_error; |
| 200 | ReleaseAppCodeCacheDir(); |
| 201 | } |
| 202 | |
| 203 | bool 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 | |
| 258 | bool 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 | |
| 266 | bool PreInitializeNativeBridge(const char* app_data_dir_in, const char* instruction_set) { |
| 267 | if (state != NativeBridgeState::kOpened) { |
| 268 | ALOGE("Invalid state: native bridge is expected to be opened."); |
| 269 | CloseNativeBridge(true); |
| 270 | return false; |
| 271 | } |
| 272 | |
Lev Rumyantsev | 7ec3e60 | 2019-12-13 15:49:37 -0800 | [diff] [blame] | 273 | if (app_data_dir_in != nullptr) { |
| 274 | // Create the path to the application code cache directory. |
| 275 | // The memory will be release after Initialization or when the native bridge is closed. |
| 276 | const size_t len = strlen(app_data_dir_in) + strlen(kCodeCacheDir) + 2; // '\0' + '/' |
| 277 | app_code_cache_dir = new char[len]; |
| 278 | snprintf(app_code_cache_dir, len, "%s/%s", app_data_dir_in, kCodeCacheDir); |
| 279 | } else { |
| 280 | ALOGW("Application private directory isn't available."); |
| 281 | app_code_cache_dir = nullptr; |
Orion Hodson | 9b16e34 | 2019-10-09 13:29:16 +0100 | [diff] [blame] | 282 | } |
| 283 | |
Orion Hodson | 9b16e34 | 2019-10-09 13:29:16 +0100 | [diff] [blame] | 284 | // Bind-mount /system/lib{,64}/<isa>/cpuinfo to /proc/cpuinfo. |
| 285 | // Failure is not fatal and will keep the native bridge in kPreInitialized. |
| 286 | state = NativeBridgeState::kPreInitialized; |
| 287 | |
| 288 | #ifndef __APPLE__ |
| 289 | if (instruction_set == nullptr) { |
| 290 | return true; |
| 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 true; |
| 300 | } |
| 301 | |
| 302 | // If the file does not exist, the mount command will fail, |
| 303 | // so we save the extra file existence check. |
| 304 | char cpuinfo_path[1024]; |
| 305 | |
| 306 | #if defined(__ANDROID__) |
| 307 | snprintf(cpuinfo_path, sizeof(cpuinfo_path), "/system/lib" |
| 308 | #ifdef __LP64__ |
| 309 | "64" |
| 310 | #endif // __LP64__ |
| 311 | "/%s/cpuinfo", instruction_set); |
| 312 | #else // !__ANDROID__ |
| 313 | // To be able to test on the host, we hardwire a relative path. |
| 314 | snprintf(cpuinfo_path, sizeof(cpuinfo_path), "./cpuinfo"); |
| 315 | #endif |
| 316 | |
| 317 | // Bind-mount. |
| 318 | if (TEMP_FAILURE_RETRY(mount(cpuinfo_path, // Source. |
| 319 | "/proc/cpuinfo", // Target. |
| 320 | nullptr, // FS type. |
| 321 | MS_BIND, // Mount flags: bind mount. |
| 322 | nullptr)) == -1) { // "Data." |
| 323 | ALOGW("Failed to bind-mount %s as /proc/cpuinfo: %s", cpuinfo_path, strerror(errno)); |
| 324 | } |
| 325 | #else // __APPLE__ |
| 326 | UNUSED(instruction_set); |
| 327 | ALOGW("Mac OS does not support bind-mounting. Host simulation of native bridge impossible."); |
| 328 | #endif |
| 329 | |
| 330 | return true; |
| 331 | } |
| 332 | |
Lev Rumyantsev | abafbe7 | 2019-12-13 15:49:37 -0800 | [diff] [blame] | 333 | void PreZygoteForkNativeBridge() { |
| 334 | if (NativeBridgeInitialized()) { |
| 335 | if (isCompatibleWith(PRE_ZYGOTE_FORK_VERSION)) { |
| 336 | return callbacks->preZygoteFork(); |
| 337 | } else { |
| 338 | ALOGE("not compatible with version %d, preZygoteFork() isn't invoked", |
| 339 | PRE_ZYGOTE_FORK_VERSION); |
| 340 | } |
| 341 | } |
| 342 | } |
| 343 | |
Orion Hodson | 9b16e34 | 2019-10-09 13:29:16 +0100 | [diff] [blame] | 344 | static void SetCpuAbi(JNIEnv* env, jclass build_class, const char* field, const char* value) { |
| 345 | if (value != nullptr) { |
| 346 | jfieldID field_id = env->GetStaticFieldID(build_class, field, "Ljava/lang/String;"); |
| 347 | if (field_id == nullptr) { |
| 348 | env->ExceptionClear(); |
| 349 | ALOGW("Could not find %s field.", field); |
| 350 | return; |
| 351 | } |
| 352 | |
| 353 | jstring str = env->NewStringUTF(value); |
| 354 | if (str == nullptr) { |
| 355 | env->ExceptionClear(); |
| 356 | ALOGW("Could not create string %s.", value); |
| 357 | return; |
| 358 | } |
| 359 | |
| 360 | env->SetStaticObjectField(build_class, field_id, str); |
| 361 | } |
| 362 | } |
| 363 | |
| 364 | // Set up the environment for the bridged app. |
Martin Stjernholm | 3bb009a | 2019-10-17 21:29:01 +0100 | [diff] [blame] | 365 | static void SetupEnvironment(const NativeBridgeCallbacks* cbs, JNIEnv* env, const char* isa) { |
Orion Hodson | 9b16e34 | 2019-10-09 13:29:16 +0100 | [diff] [blame] | 366 | // Need a JNIEnv* to do anything. |
| 367 | if (env == nullptr) { |
| 368 | ALOGW("No JNIEnv* to set up app environment."); |
| 369 | return; |
| 370 | } |
| 371 | |
| 372 | // Query the bridge for environment values. |
Martin Stjernholm | 3bb009a | 2019-10-17 21:29:01 +0100 | [diff] [blame] | 373 | const struct NativeBridgeRuntimeValues* env_values = cbs->getAppEnv(isa); |
Orion Hodson | 9b16e34 | 2019-10-09 13:29:16 +0100 | [diff] [blame] | 374 | if (env_values == nullptr) { |
| 375 | return; |
| 376 | } |
| 377 | |
| 378 | // Keep the JNIEnv clean. |
| 379 | jint success = env->PushLocalFrame(16); // That should be small and large enough. |
| 380 | if (success < 0) { |
| 381 | // Out of memory, really borked. |
| 382 | ALOGW("Out of memory while setting up app environment."); |
| 383 | env->ExceptionClear(); |
| 384 | return; |
| 385 | } |
| 386 | |
| 387 | // Reset CPU_ABI & CPU_ABI2 to values required by the apps running with native bridge. |
| 388 | if (env_values->cpu_abi != nullptr || env_values->cpu_abi2 != nullptr || |
| 389 | env_values->abi_count >= 0) { |
| 390 | jclass bclass_id = env->FindClass("android/os/Build"); |
| 391 | if (bclass_id != nullptr) { |
| 392 | SetCpuAbi(env, bclass_id, "CPU_ABI", env_values->cpu_abi); |
| 393 | SetCpuAbi(env, bclass_id, "CPU_ABI2", env_values->cpu_abi2); |
| 394 | } else { |
| 395 | // For example in a host test environment. |
| 396 | env->ExceptionClear(); |
| 397 | ALOGW("Could not find Build class."); |
| 398 | } |
| 399 | } |
| 400 | |
| 401 | if (env_values->os_arch != nullptr) { |
| 402 | jclass sclass_id = env->FindClass("java/lang/System"); |
| 403 | if (sclass_id != nullptr) { |
| 404 | jmethodID set_prop_id = env->GetStaticMethodID(sclass_id, "setUnchangeableSystemProperty", |
| 405 | "(Ljava/lang/String;Ljava/lang/String;)V"); |
| 406 | if (set_prop_id != nullptr) { |
| 407 | // Init os.arch to the value reqired by the apps running with native bridge. |
| 408 | env->CallStaticVoidMethod(sclass_id, set_prop_id, env->NewStringUTF("os.arch"), |
| 409 | env->NewStringUTF(env_values->os_arch)); |
| 410 | } else { |
| 411 | env->ExceptionClear(); |
| 412 | ALOGW("Could not find System#setUnchangeableSystemProperty."); |
| 413 | } |
| 414 | } else { |
| 415 | env->ExceptionClear(); |
| 416 | ALOGW("Could not find System class."); |
| 417 | } |
| 418 | } |
| 419 | |
| 420 | // Make it pristine again. |
| 421 | env->PopLocalFrame(nullptr); |
| 422 | } |
| 423 | |
| 424 | bool InitializeNativeBridge(JNIEnv* env, const char* instruction_set) { |
| 425 | // We expect only one place that calls InitializeNativeBridge: Runtime::DidForkFromZygote. At that |
| 426 | // point we are not multi-threaded, so we do not need locking here. |
| 427 | |
| 428 | if (state == NativeBridgeState::kPreInitialized) { |
Lev Rumyantsev | 7ec3e60 | 2019-12-13 15:49:37 -0800 | [diff] [blame] | 429 | if (app_code_cache_dir != nullptr) { |
| 430 | // Check for code cache: if it doesn't exist try to create it. |
| 431 | struct stat st; |
| 432 | if (stat(app_code_cache_dir, &st) == -1) { |
| 433 | if (errno == ENOENT) { |
| 434 | if (mkdir(app_code_cache_dir, S_IRWXU | S_IRWXG | S_IXOTH) == -1) { |
| 435 | ALOGW("Cannot create code cache directory %s: %s.", |
| 436 | app_code_cache_dir, strerror(errno)); |
| 437 | ReleaseAppCodeCacheDir(); |
| 438 | } |
| 439 | } else { |
| 440 | ALOGW("Cannot stat code cache directory %s: %s.", |
| 441 | app_code_cache_dir, strerror(errno)); |
Orion Hodson | 9b16e34 | 2019-10-09 13:29:16 +0100 | [diff] [blame] | 442 | ReleaseAppCodeCacheDir(); |
| 443 | } |
Lev Rumyantsev | 7ec3e60 | 2019-12-13 15:49:37 -0800 | [diff] [blame] | 444 | } else if (!S_ISDIR(st.st_mode)) { |
| 445 | ALOGW("Code cache is not a directory %s.", app_code_cache_dir); |
Orion Hodson | 9b16e34 | 2019-10-09 13:29:16 +0100 | [diff] [blame] | 446 | ReleaseAppCodeCacheDir(); |
| 447 | } |
Orion Hodson | 9b16e34 | 2019-10-09 13:29:16 +0100 | [diff] [blame] | 448 | } |
| 449 | |
Lev Rumyantsev | 7ec3e60 | 2019-12-13 15:49:37 -0800 | [diff] [blame] | 450 | // If we're still PreInitialized (didn't fail the code cache checks) try to initialize. |
Orion Hodson | 9b16e34 | 2019-10-09 13:29:16 +0100 | [diff] [blame] | 451 | if (state == NativeBridgeState::kPreInitialized) { |
| 452 | if (callbacks->initialize(runtime_callbacks, app_code_cache_dir, instruction_set)) { |
| 453 | SetupEnvironment(callbacks, env, instruction_set); |
| 454 | state = NativeBridgeState::kInitialized; |
| 455 | // We no longer need the code cache path, release the memory. |
| 456 | ReleaseAppCodeCacheDir(); |
| 457 | } else { |
| 458 | // Unload the library. |
| 459 | dlclose(native_bridge_handle); |
| 460 | CloseNativeBridge(true); |
| 461 | } |
| 462 | } |
| 463 | } else { |
| 464 | CloseNativeBridge(true); |
| 465 | } |
| 466 | |
| 467 | return state == NativeBridgeState::kInitialized; |
| 468 | } |
| 469 | |
| 470 | void UnloadNativeBridge() { |
| 471 | // We expect only one place that calls UnloadNativeBridge: Runtime::DidForkFromZygote. At that |
| 472 | // point we are not multi-threaded, so we do not need locking here. |
| 473 | |
Orion Hodson | 31b3ffa | 2019-10-14 10:27:00 +0100 | [diff] [blame] | 474 | switch (state) { |
Orion Hodson | 9b16e34 | 2019-10-09 13:29:16 +0100 | [diff] [blame] | 475 | case NativeBridgeState::kOpened: |
| 476 | case NativeBridgeState::kPreInitialized: |
| 477 | case NativeBridgeState::kInitialized: |
| 478 | // Unload. |
| 479 | dlclose(native_bridge_handle); |
| 480 | CloseNativeBridge(false); |
| 481 | break; |
| 482 | |
| 483 | case NativeBridgeState::kNotSetup: |
| 484 | // Not even set up. Error. |
| 485 | CloseNativeBridge(true); |
| 486 | break; |
| 487 | |
| 488 | case NativeBridgeState::kClosed: |
| 489 | // Ignore. |
| 490 | break; |
| 491 | } |
| 492 | } |
| 493 | |
| 494 | bool NativeBridgeError() { |
| 495 | return had_error; |
| 496 | } |
| 497 | |
| 498 | bool NativeBridgeAvailable() { |
| 499 | return state == NativeBridgeState::kOpened |
| 500 | || state == NativeBridgeState::kPreInitialized |
| 501 | || state == NativeBridgeState::kInitialized; |
| 502 | } |
| 503 | |
| 504 | bool NativeBridgeInitialized() { |
| 505 | // Calls of this are supposed to happen in a state where the native bridge is stable, i.e., after |
| 506 | // Runtime::DidForkFromZygote. In that case we do not need a lock. |
| 507 | return state == NativeBridgeState::kInitialized; |
| 508 | } |
| 509 | |
| 510 | void* NativeBridgeLoadLibrary(const char* libpath, int flag) { |
| 511 | if (NativeBridgeInitialized()) { |
| 512 | return callbacks->loadLibrary(libpath, flag); |
| 513 | } |
| 514 | return nullptr; |
| 515 | } |
| 516 | |
| 517 | void* NativeBridgeGetTrampoline(void* handle, const char* name, const char* shorty, |
| 518 | uint32_t len) { |
| 519 | if (NativeBridgeInitialized()) { |
| 520 | return callbacks->getTrampoline(handle, name, shorty, len); |
| 521 | } |
| 522 | return nullptr; |
| 523 | } |
| 524 | |
| 525 | bool NativeBridgeIsSupported(const char* libpath) { |
| 526 | if (NativeBridgeInitialized()) { |
| 527 | return callbacks->isSupported(libpath); |
| 528 | } |
| 529 | return false; |
| 530 | } |
| 531 | |
| 532 | uint32_t NativeBridgeGetVersion() { |
| 533 | if (NativeBridgeAvailable()) { |
| 534 | return callbacks->version; |
| 535 | } |
| 536 | return 0; |
| 537 | } |
| 538 | |
| 539 | NativeBridgeSignalHandlerFn NativeBridgeGetSignalHandler(int signal) { |
| 540 | if (NativeBridgeInitialized()) { |
| 541 | if (isCompatibleWith(SIGNAL_VERSION)) { |
| 542 | return callbacks->getSignalHandler(signal); |
| 543 | } else { |
| 544 | ALOGE("not compatible with version %d, cannot get signal handler", SIGNAL_VERSION); |
| 545 | } |
| 546 | } |
| 547 | return nullptr; |
| 548 | } |
| 549 | |
| 550 | int NativeBridgeUnloadLibrary(void* handle) { |
| 551 | if (NativeBridgeInitialized()) { |
| 552 | if (isCompatibleWith(NAMESPACE_VERSION)) { |
| 553 | return callbacks->unloadLibrary(handle); |
| 554 | } else { |
| 555 | ALOGE("not compatible with version %d, cannot unload library", NAMESPACE_VERSION); |
| 556 | } |
| 557 | } |
| 558 | return -1; |
| 559 | } |
| 560 | |
| 561 | const char* NativeBridgeGetError() { |
| 562 | if (NativeBridgeInitialized()) { |
| 563 | if (isCompatibleWith(NAMESPACE_VERSION)) { |
| 564 | return callbacks->getError(); |
| 565 | } else { |
| 566 | return "native bridge implementation is not compatible with version 3, cannot get message"; |
| 567 | } |
| 568 | } |
| 569 | return "native bridge is not initialized"; |
| 570 | } |
| 571 | |
| 572 | bool NativeBridgeIsPathSupported(const char* path) { |
| 573 | if (NativeBridgeInitialized()) { |
| 574 | if (isCompatibleWith(NAMESPACE_VERSION)) { |
| 575 | return callbacks->isPathSupported(path); |
| 576 | } else { |
| 577 | ALOGE("not compatible with version %d, cannot check via library path", NAMESPACE_VERSION); |
| 578 | } |
| 579 | } |
| 580 | return false; |
| 581 | } |
| 582 | |
| 583 | bool NativeBridgeInitAnonymousNamespace(const char* public_ns_sonames, |
| 584 | const char* anon_ns_library_path) { |
| 585 | if (NativeBridgeInitialized()) { |
| 586 | if (isCompatibleWith(NAMESPACE_VERSION)) { |
| 587 | return callbacks->initAnonymousNamespace(public_ns_sonames, anon_ns_library_path); |
| 588 | } else { |
| 589 | ALOGE("not compatible with version %d, cannot init namespace", NAMESPACE_VERSION); |
| 590 | } |
| 591 | } |
| 592 | |
| 593 | return false; |
| 594 | } |
| 595 | |
| 596 | native_bridge_namespace_t* NativeBridgeCreateNamespace(const char* name, |
| 597 | const char* ld_library_path, |
| 598 | const char* default_library_path, |
| 599 | uint64_t type, |
| 600 | const char* permitted_when_isolated_path, |
| 601 | native_bridge_namespace_t* parent_ns) { |
| 602 | if (NativeBridgeInitialized()) { |
| 603 | if (isCompatibleWith(NAMESPACE_VERSION)) { |
| 604 | return callbacks->createNamespace(name, |
| 605 | ld_library_path, |
| 606 | default_library_path, |
| 607 | type, |
| 608 | permitted_when_isolated_path, |
| 609 | parent_ns); |
| 610 | } else { |
| 611 | ALOGE("not compatible with version %d, cannot create namespace %s", NAMESPACE_VERSION, name); |
| 612 | } |
| 613 | } |
| 614 | |
| 615 | return nullptr; |
| 616 | } |
| 617 | |
| 618 | bool NativeBridgeLinkNamespaces(native_bridge_namespace_t* from, native_bridge_namespace_t* to, |
| 619 | const char* shared_libs_sonames) { |
| 620 | if (NativeBridgeInitialized()) { |
| 621 | if (isCompatibleWith(NAMESPACE_VERSION)) { |
| 622 | return callbacks->linkNamespaces(from, to, shared_libs_sonames); |
| 623 | } else { |
| 624 | ALOGE("not compatible with version %d, cannot init namespace", NAMESPACE_VERSION); |
| 625 | } |
| 626 | } |
| 627 | |
| 628 | return false; |
| 629 | } |
| 630 | |
| 631 | native_bridge_namespace_t* NativeBridgeGetExportedNamespace(const char* name) { |
| 632 | if (!NativeBridgeInitialized()) { |
| 633 | return nullptr; |
| 634 | } |
| 635 | |
| 636 | if (isCompatibleWith(RUNTIME_NAMESPACE_VERSION)) { |
| 637 | return callbacks->getExportedNamespace(name); |
| 638 | } |
| 639 | |
| 640 | // sphal is vendor namespace name -> use v4 callback in the case NB callbacks |
| 641 | // are not compatible with v5 |
| 642 | if (isCompatibleWith(VENDOR_NAMESPACE_VERSION) && name != nullptr && strcmp("sphal", name) == 0) { |
| 643 | return callbacks->getVendorNamespace(); |
| 644 | } |
| 645 | |
| 646 | return nullptr; |
| 647 | } |
| 648 | |
| 649 | void* NativeBridgeLoadLibraryExt(const char* libpath, int flag, native_bridge_namespace_t* ns) { |
| 650 | if (NativeBridgeInitialized()) { |
| 651 | if (isCompatibleWith(NAMESPACE_VERSION)) { |
| 652 | return callbacks->loadLibraryExt(libpath, flag, ns); |
| 653 | } else { |
| 654 | ALOGE("not compatible with version %d, cannot load library in namespace", NAMESPACE_VERSION); |
| 655 | } |
| 656 | } |
| 657 | return nullptr; |
| 658 | } |
| 659 | |
| 660 | } // extern "C" |
| 661 | |
| 662 | } // namespace android |