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 | |
| 17 | #include "nativebridge/native_bridge.h" |
| 18 | |
jgu21 | ab0da5a | 2014-09-10 06:58:32 -0400 | [diff] [blame] | 19 | #include <cstring> |
Andreas Gampe | 049249c | 2014-08-19 22:31:31 -0700 | [diff] [blame] | 20 | #include <cutils/log.h> |
Calin Juravle | 961ae12 | 2014-08-11 16:11:59 +0100 | [diff] [blame] | 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> |
Calin Juravle | 961ae12 | 2014-08-11 16:11:59 +0100 | [diff] [blame] | 27 | |
| 28 | |
| 29 | namespace android { |
| 30 | |
jgu21 | ab0da5a | 2014-09-10 06:58:32 -0400 | [diff] [blame] | 31 | // Environment values required by the apps running with native bridge. |
| 32 | struct NativeBridgeRuntimeValues { |
| 33 | const char* os_arch; |
| 34 | const char* cpu_abi; |
| 35 | const char* cpu_abi2; |
| 36 | const char* *supported_abis; |
| 37 | int32_t abi_count; |
| 38 | }; |
| 39 | |
Calin Juravle | 961ae12 | 2014-08-11 16:11:59 +0100 | [diff] [blame] | 40 | // The symbol name exposed by native-bridge with the type of NativeBridgeCallbacks. |
| 41 | static constexpr const char* kNativeBridgeInterfaceSymbol = "NativeBridgeItf"; |
| 42 | |
Andreas Gampe | 035bd75 | 2014-09-02 21:17:03 -0700 | [diff] [blame] | 43 | enum class NativeBridgeState { |
| 44 | kNotSetup, // Initial state. |
| 45 | kOpened, // After successful dlopen. |
| 46 | kInitialized, // After successful initialization. |
| 47 | kClosed // Closed or errors. |
| 48 | }; |
Calin Juravle | 961ae12 | 2014-08-11 16:11:59 +0100 | [diff] [blame] | 49 | |
Andreas Gampe | 035bd75 | 2014-09-02 21:17:03 -0700 | [diff] [blame] | 50 | static const char* kNotSetupString = "kNotSetup"; |
| 51 | static const char* kOpenedString = "kOpened"; |
| 52 | static const char* kInitializedString = "kInitialized"; |
| 53 | static const char* kClosedString = "kClosed"; |
| 54 | |
| 55 | static const char* GetNativeBridgeStateString(NativeBridgeState state) { |
| 56 | switch (state) { |
| 57 | case NativeBridgeState::kNotSetup: |
| 58 | return kNotSetupString; |
| 59 | |
| 60 | case NativeBridgeState::kOpened: |
| 61 | return kOpenedString; |
| 62 | |
| 63 | case NativeBridgeState::kInitialized: |
| 64 | return kInitializedString; |
| 65 | |
| 66 | case NativeBridgeState::kClosed: |
| 67 | return kClosedString; |
| 68 | } |
| 69 | } |
| 70 | |
| 71 | // Current state of the native bridge. |
| 72 | static NativeBridgeState state = NativeBridgeState::kNotSetup; |
| 73 | |
Andreas Gampe | 049249c | 2014-08-19 22:31:31 -0700 | [diff] [blame] | 74 | // Whether we had an error at some point. |
| 75 | static bool had_error = false; |
Calin Juravle | 961ae12 | 2014-08-11 16:11:59 +0100 | [diff] [blame] | 76 | |
Andreas Gampe | 035bd75 | 2014-09-02 21:17:03 -0700 | [diff] [blame] | 77 | // Handle of the loaded library. |
| 78 | static void* native_bridge_handle = nullptr; |
| 79 | // Pointer to the callbacks. Available as soon as LoadNativeBridge succeeds, but only initialized |
| 80 | // later. |
Calin Juravle | 961ae12 | 2014-08-11 16:11:59 +0100 | [diff] [blame] | 81 | static NativeBridgeCallbacks* callbacks = nullptr; |
Andreas Gampe | 035bd75 | 2014-09-02 21:17:03 -0700 | [diff] [blame] | 82 | // Callbacks provided by the environment to the bridge. Passed to LoadNativeBridge. |
Calin Juravle | 961ae12 | 2014-08-11 16:11:59 +0100 | [diff] [blame] | 83 | static const NativeBridgeRuntimeCallbacks* runtime_callbacks = nullptr; |
| 84 | |
jgu21 | ab0da5a | 2014-09-10 06:58:32 -0400 | [diff] [blame] | 85 | // The app's data directory. |
| 86 | static char* app_data_dir = nullptr; |
| 87 | |
| 88 | static constexpr uint32_t kNativeBridgeCallbackVersion = 1; |
| 89 | |
Andreas Gampe | 049249c | 2014-08-19 22:31:31 -0700 | [diff] [blame] | 90 | // Characters allowed in a native bridge filename. The first character must |
| 91 | // be in [a-zA-Z] (expected 'l' for "libx"). The rest must be in [a-zA-Z0-9._-]. |
| 92 | static bool CharacterAllowed(char c, bool first) { |
| 93 | if (first) { |
| 94 | return ('a' <= c && c <= 'z') || ('A' <= c && c <= 'Z'); |
| 95 | } else { |
| 96 | return ('a' <= c && c <= 'z') || ('A' <= c && c <= 'Z') || ('0' <= c && c <= '9') || |
| 97 | (c == '.') || (c == '_') || (c == '-'); |
| 98 | } |
| 99 | } |
| 100 | |
| 101 | // We only allow simple names for the library. It is supposed to be a file in |
| 102 | // /system/lib or /vendor/lib. Only allow a small range of characters, that is |
| 103 | // names consisting of [a-zA-Z0-9._-] and starting with [a-zA-Z]. |
| 104 | bool NativeBridgeNameAcceptable(const char* nb_library_filename) { |
| 105 | const char* ptr = nb_library_filename; |
| 106 | if (*ptr == 0) { |
| 107 | // Emptry string. Allowed, means no native bridge. |
| 108 | return true; |
| 109 | } else { |
| 110 | // First character must be [a-zA-Z]. |
| 111 | if (!CharacterAllowed(*ptr, true)) { |
| 112 | // Found an invalid fist character, don't accept. |
| 113 | ALOGE("Native bridge library %s has been rejected for first character %c", nb_library_filename, *ptr); |
| 114 | return false; |
| 115 | } else { |
| 116 | // For the rest, be more liberal. |
| 117 | ptr++; |
| 118 | while (*ptr != 0) { |
| 119 | if (!CharacterAllowed(*ptr, false)) { |
| 120 | // Found an invalid character, don't accept. |
| 121 | ALOGE("Native bridge library %s has been rejected for %c", nb_library_filename, *ptr); |
| 122 | return false; |
| 123 | } |
| 124 | ptr++; |
| 125 | } |
| 126 | } |
| 127 | return true; |
| 128 | } |
| 129 | } |
| 130 | |
jgu21 | ab0da5a | 2014-09-10 06:58:32 -0400 | [diff] [blame] | 131 | static bool VersionCheck(NativeBridgeCallbacks* cb) { |
| 132 | return cb != nullptr && cb->version == kNativeBridgeCallbackVersion; |
| 133 | } |
| 134 | |
Andreas Gampe | 035bd75 | 2014-09-02 21:17:03 -0700 | [diff] [blame] | 135 | bool LoadNativeBridge(const char* nb_library_filename, |
| 136 | const NativeBridgeRuntimeCallbacks* runtime_cbs) { |
| 137 | // We expect only one place that calls LoadNativeBridge: Runtime::Init. At that point we are not |
| 138 | // multi-threaded, so we do not need locking here. |
Calin Juravle | 961ae12 | 2014-08-11 16:11:59 +0100 | [diff] [blame] | 139 | |
Andreas Gampe | 035bd75 | 2014-09-02 21:17:03 -0700 | [diff] [blame] | 140 | if (state != NativeBridgeState::kNotSetup) { |
Andreas Gampe | 049249c | 2014-08-19 22:31:31 -0700 | [diff] [blame] | 141 | // Setup has been called before. Ignore this call. |
jgu21 | ab0da5a | 2014-09-10 06:58:32 -0400 | [diff] [blame] | 142 | if (nb_library_filename != nullptr) { // Avoids some log-spam for dalvikvm. |
| 143 | ALOGW("Called LoadNativeBridge for an already set up native bridge. State is %s.", |
| 144 | GetNativeBridgeStateString(state)); |
| 145 | } |
Andreas Gampe | 049249c | 2014-08-19 22:31:31 -0700 | [diff] [blame] | 146 | // Note: counts as an error, even though the bridge may be functional. |
| 147 | had_error = true; |
Andreas Gampe | 049249c | 2014-08-19 22:31:31 -0700 | [diff] [blame] | 148 | return false; |
| 149 | } |
| 150 | |
Andreas Gampe | 035bd75 | 2014-09-02 21:17:03 -0700 | [diff] [blame] | 151 | if (nb_library_filename == nullptr || *nb_library_filename == 0) { |
| 152 | state = NativeBridgeState::kClosed; |
| 153 | return true; |
| 154 | } else { |
| 155 | if (!NativeBridgeNameAcceptable(nb_library_filename)) { |
| 156 | state = NativeBridgeState::kClosed; |
Andreas Gampe | 049249c | 2014-08-19 22:31:31 -0700 | [diff] [blame] | 157 | had_error = true; |
Andreas Gampe | 035bd75 | 2014-09-02 21:17:03 -0700 | [diff] [blame] | 158 | } else { |
| 159 | // Try to open the library. |
| 160 | void* handle = dlopen(nb_library_filename, RTLD_LAZY); |
| 161 | if (handle != nullptr) { |
| 162 | callbacks = reinterpret_cast<NativeBridgeCallbacks*>(dlsym(handle, |
| 163 | kNativeBridgeInterfaceSymbol)); |
| 164 | if (callbacks != nullptr) { |
jgu21 | ab0da5a | 2014-09-10 06:58:32 -0400 | [diff] [blame] | 165 | if (VersionCheck(callbacks)) { |
| 166 | // Store the handle for later. |
| 167 | native_bridge_handle = handle; |
| 168 | } else { |
| 169 | callbacks = nullptr; |
| 170 | dlclose(handle); |
| 171 | ALOGW("Unsupported native bridge interface."); |
| 172 | } |
Andreas Gampe | 035bd75 | 2014-09-02 21:17:03 -0700 | [diff] [blame] | 173 | } else { |
| 174 | dlclose(handle); |
| 175 | } |
| 176 | } |
| 177 | |
| 178 | // Two failure conditions: could not find library (dlopen failed), or could not find native |
| 179 | // bridge interface (dlsym failed). Both are an error and close the native bridge. |
| 180 | if (callbacks == nullptr) { |
| 181 | had_error = true; |
| 182 | state = NativeBridgeState::kClosed; |
| 183 | } else { |
| 184 | runtime_callbacks = runtime_cbs; |
| 185 | state = NativeBridgeState::kOpened; |
| 186 | } |
| 187 | } |
| 188 | return state == NativeBridgeState::kOpened; |
| 189 | } |
| 190 | } |
| 191 | |
jgu21 | ab0da5a | 2014-09-10 06:58:32 -0400 | [diff] [blame] | 192 | #if defined(__arm__) |
| 193 | static const char* kRuntimeISA = "arm"; |
| 194 | #elif defined(__aarch64__) |
| 195 | static const char* kRuntimeISA = "arm64"; |
| 196 | #elif defined(__mips__) |
| 197 | static const char* kRuntimeISA = "mips"; |
| 198 | #elif defined(__i386__) |
| 199 | static const char* kRuntimeISA = "x86"; |
| 200 | #elif defined(__x86_64__) |
| 201 | static const char* kRuntimeISA = "x86_64"; |
| 202 | #else |
| 203 | static const char* kRuntimeISA = "unknown"; |
| 204 | #endif |
| 205 | |
| 206 | |
| 207 | bool NeedsNativeBridge(const char* instruction_set) { |
Andreas Gampe | 04054e2 | 2014-09-25 22:33:01 -0700 | [diff] [blame] | 208 | if (instruction_set == nullptr) { |
| 209 | ALOGE("Null instruction set in NeedsNativeBridge."); |
| 210 | return false; |
| 211 | } |
Andreas Gampe | 2f71cb2 | 2014-09-25 21:34:25 -0700 | [diff] [blame] | 212 | return strncmp(instruction_set, kRuntimeISA, strlen(kRuntimeISA) + 1) != 0; |
jgu21 | ab0da5a | 2014-09-10 06:58:32 -0400 | [diff] [blame] | 213 | } |
| 214 | |
Andreas Gampe | 4390a63 | 2014-09-24 18:53:26 -0700 | [diff] [blame] | 215 | #ifdef __APPLE__ |
| 216 | template<typename T> void UNUSED(const T&) {} |
| 217 | #endif |
| 218 | |
jgu21 | ab0da5a | 2014-09-10 06:58:32 -0400 | [diff] [blame] | 219 | void PreInitializeNativeBridge(const char* app_data_dir_in, const char* instruction_set) { |
| 220 | if (app_data_dir_in == nullptr) { |
| 221 | return; |
| 222 | } |
| 223 | |
| 224 | const size_t len = strlen(app_data_dir_in); |
| 225 | // Make a copy for us. |
| 226 | app_data_dir = new char[len]; |
| 227 | strncpy(app_data_dir, app_data_dir_in, len); |
| 228 | |
Andreas Gampe | 962eb40 | 2014-09-24 16:36:17 -0700 | [diff] [blame] | 229 | #ifndef __APPLE__ |
jgu21 | ab0da5a | 2014-09-10 06:58:32 -0400 | [diff] [blame] | 230 | if (instruction_set == nullptr) { |
| 231 | return; |
| 232 | } |
| 233 | size_t isa_len = strlen(instruction_set); |
| 234 | if (isa_len > 10) { |
| 235 | // 10 is a loose upper bound on the currently known instruction sets (a tight bound is 7 for |
| 236 | // x86_64 [including the trailing \0]). This is so we don't have to change here if there will |
| 237 | // be another instruction set in the future. |
Andreas Gampe | 2f71cb2 | 2014-09-25 21:34:25 -0700 | [diff] [blame] | 238 | ALOGW("Instruction set %s is malformed, must be less than or equal to 10 characters.", |
| 239 | instruction_set); |
jgu21 | ab0da5a | 2014-09-10 06:58:32 -0400 | [diff] [blame] | 240 | return; |
| 241 | } |
| 242 | |
| 243 | // Bind-mount /system/lib{,64}/<isa>/cpuinfo to /proc/cpuinfo. If the file does not exist, the |
| 244 | // mount command will fail, so we safe the extra file existence check... |
| 245 | char cpuinfo_path[1024]; |
| 246 | |
Andreas Gampe | 04054e2 | 2014-09-25 22:33:01 -0700 | [diff] [blame] | 247 | #ifdef HAVE_ANDROID_OS |
| 248 | snprintf(cpuinfo_path, sizeof(cpuinfo_path), "/system/lib" |
jgu21 | ab0da5a | 2014-09-10 06:58:32 -0400 | [diff] [blame] | 249 | #ifdef __LP64__ |
Andreas Gampe | 04054e2 | 2014-09-25 22:33:01 -0700 | [diff] [blame] | 250 | "64" |
| 251 | #endif // __LP64__ |
| 252 | "/%s/cpuinfo", instruction_set); |
| 253 | #else // !HAVE_ANDROID_OS |
| 254 | // To be able to test on the host, we hardwire a relative path. |
| 255 | snprintf(cpuinfo_path, sizeof(cpuinfo_path), "./cpuinfo"); |
jgu21 | ab0da5a | 2014-09-10 06:58:32 -0400 | [diff] [blame] | 256 | #endif |
jgu21 | ab0da5a | 2014-09-10 06:58:32 -0400 | [diff] [blame] | 257 | |
| 258 | // Bind-mount. |
Andreas Gampe | 2f71cb2 | 2014-09-25 21:34:25 -0700 | [diff] [blame] | 259 | if (TEMP_FAILURE_RETRY(mount(cpuinfo_path, // Source. |
| 260 | "/proc/cpuinfo", // Target. |
| 261 | nullptr, // FS type. |
| 262 | MS_BIND, // Mount flags: bind mount. |
| 263 | nullptr)) == -1) { // "Data." |
Andreas Gampe | 04054e2 | 2014-09-25 22:33:01 -0700 | [diff] [blame] | 264 | 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] | 265 | } |
Andreas Gampe | 962eb40 | 2014-09-24 16:36:17 -0700 | [diff] [blame] | 266 | #else |
Andreas Gampe | 4390a63 | 2014-09-24 18:53:26 -0700 | [diff] [blame] | 267 | UNUSED(instruction_set); |
Andreas Gampe | 962eb40 | 2014-09-24 16:36:17 -0700 | [diff] [blame] | 268 | ALOGW("Mac OS does not support bind-mounting. Host simulation of native bridge impossible."); |
| 269 | #endif |
jgu21 | ab0da5a | 2014-09-10 06:58:32 -0400 | [diff] [blame] | 270 | } |
| 271 | |
| 272 | static void SetCpuAbi(JNIEnv* env, jclass build_class, const char* field, const char* value) { |
| 273 | if (value != nullptr) { |
| 274 | jfieldID field_id = env->GetStaticFieldID(build_class, field, "Ljava/lang/String;"); |
| 275 | if (field_id == nullptr) { |
| 276 | env->ExceptionClear(); |
| 277 | ALOGW("Could not find %s field.", field); |
| 278 | return; |
| 279 | } |
| 280 | |
| 281 | jstring str = env->NewStringUTF(value); |
| 282 | if (str == nullptr) { |
| 283 | env->ExceptionClear(); |
| 284 | ALOGW("Could not create string %s.", value); |
| 285 | return; |
| 286 | } |
| 287 | |
| 288 | env->SetStaticObjectField(build_class, field_id, str); |
| 289 | } |
| 290 | } |
| 291 | |
| 292 | static void SetSupportedAbis(JNIEnv* env, jclass build_class, const char* field, |
| 293 | const char* *values, int32_t value_count) { |
| 294 | if (value_count < 0) { |
| 295 | return; |
| 296 | } |
| 297 | if (values == nullptr && value_count > 0) { |
| 298 | ALOGW("More than zero values expected: %d.", value_count); |
| 299 | return; |
| 300 | } |
| 301 | |
| 302 | jfieldID field_id = env->GetStaticFieldID(build_class, field, "[Ljava/lang/String;"); |
| 303 | if (field_id != nullptr) { |
| 304 | // Create the array. |
| 305 | jobjectArray array = env->NewObjectArray(value_count, env->FindClass("java/lang/String"), |
| 306 | nullptr); |
| 307 | if (array == nullptr) { |
| 308 | env->ExceptionClear(); |
| 309 | ALOGW("Could not create array."); |
| 310 | return; |
| 311 | } |
| 312 | |
| 313 | // Fill the array. |
| 314 | for (int32_t i = 0; i < value_count; i++) { |
| 315 | jstring str = env->NewStringUTF(values[i]); |
| 316 | if (str == nullptr) { |
| 317 | env->ExceptionClear(); |
| 318 | ALOGW("Could not create string %s.", values[i]); |
| 319 | return; |
| 320 | } |
| 321 | |
| 322 | env->SetObjectArrayElement(array, i, str); |
| 323 | } |
| 324 | |
| 325 | env->SetStaticObjectField(build_class, field_id, array); |
| 326 | } else { |
| 327 | env->ExceptionClear(); |
| 328 | ALOGW("Could not find %s field.", field); |
| 329 | } |
| 330 | } |
| 331 | |
| 332 | // Set up the environment for the bridged app. |
| 333 | static void SetupEnvironment(NativeBridgeCallbacks* callbacks, JNIEnv* env, const char* isa) { |
| 334 | // Need a JNIEnv* to do anything. |
| 335 | if (env == nullptr) { |
| 336 | ALOGW("No JNIEnv* to set up app environment."); |
| 337 | return; |
| 338 | } |
| 339 | |
| 340 | // Query the bridge for environment values. |
| 341 | const struct NativeBridgeRuntimeValues* env_values = callbacks->getAppEnv(isa); |
| 342 | if (env_values == nullptr) { |
| 343 | return; |
| 344 | } |
| 345 | |
| 346 | // Keep the JNIEnv clean. |
| 347 | jint success = env->PushLocalFrame(16); // That should be small and large enough. |
| 348 | if (success < 0) { |
| 349 | // Out of memory, really borked. |
| 350 | ALOGW("Out of memory while setting up app environment."); |
| 351 | env->ExceptionClear(); |
| 352 | return; |
| 353 | } |
| 354 | |
| 355 | // Reset CPU_ABI & CPU_ABI2 to values required by the apps running with native bridge. |
| 356 | if (env_values->cpu_abi != nullptr || env_values->cpu_abi2 != nullptr || |
| 357 | env_values->abi_count >= 0) { |
| 358 | jclass bclass_id = env->FindClass("android/os/Build"); |
| 359 | if (bclass_id != nullptr) { |
| 360 | SetCpuAbi(env, bclass_id, "CPU_ABI", env_values->cpu_abi); |
| 361 | SetCpuAbi(env, bclass_id, "CPU_ABI2", env_values->cpu_abi2); |
| 362 | |
| 363 | SetSupportedAbis(env, bclass_id, "SUPPORTED_ABIS", env_values->supported_abis, |
| 364 | env_values->abi_count); |
| 365 | } else { |
| 366 | // For example in a host test environment. |
| 367 | env->ExceptionClear(); |
| 368 | ALOGW("Could not find Build class."); |
| 369 | } |
| 370 | } |
| 371 | |
| 372 | if (env_values->os_arch != nullptr) { |
| 373 | jclass sclass_id = env->FindClass("java/lang/System"); |
| 374 | if (sclass_id != nullptr) { |
Calin Juravle | c3eb431 | 2014-10-01 17:29:19 +0100 | [diff] [blame^] | 375 | jmethodID set_prop_id = env->GetStaticMethodID(sclass_id, "initUnchangeableSystemProperty", |
| 376 | "(Ljava/lang/String;Ljava/lang/String;)V"); |
jgu21 | ab0da5a | 2014-09-10 06:58:32 -0400 | [diff] [blame] | 377 | if (set_prop_id != nullptr) { |
Calin Juravle | c3eb431 | 2014-10-01 17:29:19 +0100 | [diff] [blame^] | 378 | // Init os.arch to the value reqired by the apps running with native bridge. |
| 379 | env->CallStaticVoidMethod(sclass_id, set_prop_id, env->NewStringUTF("os.arch"), |
jgu21 | ab0da5a | 2014-09-10 06:58:32 -0400 | [diff] [blame] | 380 | env->NewStringUTF(env_values->os_arch)); |
| 381 | } else { |
| 382 | env->ExceptionClear(); |
Calin Juravle | c3eb431 | 2014-10-01 17:29:19 +0100 | [diff] [blame^] | 383 | ALOGW("Could not find initUnchangeableSystemProperty method."); |
jgu21 | ab0da5a | 2014-09-10 06:58:32 -0400 | [diff] [blame] | 384 | } |
| 385 | } else { |
| 386 | env->ExceptionClear(); |
| 387 | ALOGW("Could not find System class."); |
| 388 | } |
| 389 | } |
| 390 | |
| 391 | // Make it pristine again. |
| 392 | env->PopLocalFrame(nullptr); |
| 393 | } |
| 394 | |
| 395 | bool InitializeNativeBridge(JNIEnv* env, const char* instruction_set) { |
Andreas Gampe | 035bd75 | 2014-09-02 21:17:03 -0700 | [diff] [blame] | 396 | // We expect only one place that calls InitializeNativeBridge: Runtime::DidForkFromZygote. At that |
| 397 | // point we are not multi-threaded, so we do not need locking here. |
| 398 | |
| 399 | if (state == NativeBridgeState::kOpened) { |
| 400 | // Try to initialize. |
jgu21 | ab0da5a | 2014-09-10 06:58:32 -0400 | [diff] [blame] | 401 | if (callbacks->initialize(runtime_callbacks, app_data_dir, instruction_set)) { |
| 402 | SetupEnvironment(callbacks, env, instruction_set); |
Andreas Gampe | 035bd75 | 2014-09-02 21:17:03 -0700 | [diff] [blame] | 403 | state = NativeBridgeState::kInitialized; |
| 404 | } else { |
| 405 | // Unload the library. |
| 406 | dlclose(native_bridge_handle); |
| 407 | had_error = true; |
| 408 | state = NativeBridgeState::kClosed; |
Calin Juravle | 961ae12 | 2014-08-11 16:11:59 +0100 | [diff] [blame] | 409 | } |
Andreas Gampe | 049249c | 2014-08-19 22:31:31 -0700 | [diff] [blame] | 410 | } else { |
Andreas Gampe | 049249c | 2014-08-19 22:31:31 -0700 | [diff] [blame] | 411 | had_error = true; |
Andreas Gampe | 035bd75 | 2014-09-02 21:17:03 -0700 | [diff] [blame] | 412 | state = NativeBridgeState::kClosed; |
Calin Juravle | 961ae12 | 2014-08-11 16:11:59 +0100 | [diff] [blame] | 413 | } |
| 414 | |
Andreas Gampe | 035bd75 | 2014-09-02 21:17:03 -0700 | [diff] [blame] | 415 | return state == NativeBridgeState::kInitialized; |
| 416 | } |
Calin Juravle | 961ae12 | 2014-08-11 16:11:59 +0100 | [diff] [blame] | 417 | |
Andreas Gampe | 035bd75 | 2014-09-02 21:17:03 -0700 | [diff] [blame] | 418 | void UnloadNativeBridge() { |
| 419 | // We expect only one place that calls UnloadNativeBridge: Runtime::DidForkFromZygote. At that |
| 420 | // point we are not multi-threaded, so we do not need locking here. |
| 421 | |
| 422 | switch(state) { |
| 423 | case NativeBridgeState::kOpened: |
| 424 | case NativeBridgeState::kInitialized: |
| 425 | // Unload. |
| 426 | dlclose(native_bridge_handle); |
| 427 | break; |
| 428 | |
| 429 | case NativeBridgeState::kNotSetup: |
| 430 | // Not even set up. Error. |
| 431 | had_error = true; |
| 432 | break; |
| 433 | |
| 434 | case NativeBridgeState::kClosed: |
| 435 | // Ignore. |
| 436 | break; |
| 437 | } |
| 438 | |
| 439 | state = NativeBridgeState::kClosed; |
Calin Juravle | 961ae12 | 2014-08-11 16:11:59 +0100 | [diff] [blame] | 440 | } |
| 441 | |
Andreas Gampe | 049249c | 2014-08-19 22:31:31 -0700 | [diff] [blame] | 442 | bool NativeBridgeError() { |
| 443 | return had_error; |
| 444 | } |
| 445 | |
| 446 | bool NativeBridgeAvailable() { |
Andreas Gampe | 035bd75 | 2014-09-02 21:17:03 -0700 | [diff] [blame] | 447 | return state == NativeBridgeState::kOpened || state == NativeBridgeState::kInitialized; |
| 448 | } |
| 449 | |
| 450 | bool NativeBridgeInitialized() { |
| 451 | // Calls of this are supposed to happen in a state where the native bridge is stable, i.e., after |
| 452 | // Runtime::DidForkFromZygote. In that case we do not need a lock. |
| 453 | return state == NativeBridgeState::kInitialized; |
Andreas Gampe | 049249c | 2014-08-19 22:31:31 -0700 | [diff] [blame] | 454 | } |
| 455 | |
Calin Juravle | 961ae12 | 2014-08-11 16:11:59 +0100 | [diff] [blame] | 456 | void* NativeBridgeLoadLibrary(const char* libpath, int flag) { |
Andreas Gampe | 035bd75 | 2014-09-02 21:17:03 -0700 | [diff] [blame] | 457 | if (NativeBridgeInitialized()) { |
Calin Juravle | 961ae12 | 2014-08-11 16:11:59 +0100 | [diff] [blame] | 458 | return callbacks->loadLibrary(libpath, flag); |
| 459 | } |
| 460 | return nullptr; |
| 461 | } |
| 462 | |
| 463 | void* NativeBridgeGetTrampoline(void* handle, const char* name, const char* shorty, |
| 464 | uint32_t len) { |
Andreas Gampe | 035bd75 | 2014-09-02 21:17:03 -0700 | [diff] [blame] | 465 | if (NativeBridgeInitialized()) { |
Calin Juravle | 961ae12 | 2014-08-11 16:11:59 +0100 | [diff] [blame] | 466 | return callbacks->getTrampoline(handle, name, shorty, len); |
| 467 | } |
| 468 | return nullptr; |
| 469 | } |
| 470 | |
| 471 | bool NativeBridgeIsSupported(const char* libpath) { |
Andreas Gampe | 035bd75 | 2014-09-02 21:17:03 -0700 | [diff] [blame] | 472 | if (NativeBridgeInitialized()) { |
Calin Juravle | 961ae12 | 2014-08-11 16:11:59 +0100 | [diff] [blame] | 473 | return callbacks->isSupported(libpath); |
| 474 | } |
| 475 | return false; |
| 476 | } |
| 477 | |
| 478 | }; // namespace android |