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