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