diff options
author | 2021-02-10 07:19:14 +0100 | |
---|---|---|
committer | 2021-02-11 03:56:00 +0000 | |
commit | 662cee96aceb3591f6804b71a8d1c93466203165 (patch) | |
tree | 93b23635549b8761d10d6f272dda6a57b2cb3ca3 /libnativebridge/native_bridge.cc | |
parent | d4ff5bb50e252b004cfa1b4bc330b017208552a5 (diff) |
libnativebridge: mount /system/etc/cpuinfo.<isa>.txt as /proc/cpuinfo
Bug: 179753190
Test: m
Change-Id: I0facaa0da9872d78ac40f6bde78bdc32ac07d597
Diffstat (limited to 'libnativebridge/native_bridge.cc')
-rw-r--r-- | libnativebridge/native_bridge.cc | 102 |
1 files changed, 60 insertions, 42 deletions
diff --git a/libnativebridge/native_bridge.cc b/libnativebridge/native_bridge.cc index b24d14ade2..e9b2a91706 100644 --- a/libnativebridge/native_bridge.cc +++ b/libnativebridge/native_bridge.cc @@ -263,32 +263,27 @@ bool NeedsNativeBridge(const char* instruction_set) { return strncmp(instruction_set, ABI_STRING, strlen(ABI_STRING) + 1) != 0; } -bool PreInitializeNativeBridge(const char* app_data_dir_in, const char* instruction_set) { - if (state != NativeBridgeState::kOpened) { - ALOGE("Invalid state: native bridge is expected to be opened."); - CloseNativeBridge(true); +#ifndef __APPLE__ +static bool MountCpuinfo(const char* cpuinfo_path) { + // If the file does not exist, the mount command will fail, + // so we save the extra file existence check. + if (TEMP_FAILURE_RETRY(mount(cpuinfo_path, // Source. + "/proc/cpuinfo", // Target. + nullptr, // FS type. + MS_BIND, // Mount flags: bind mount. + nullptr)) == -1) { // "Data." + ALOGW("Failed to bind-mount %s as /proc/cpuinfo: %s", cpuinfo_path, strerror(errno)); return false; } + return true; +} +#endif - if (app_data_dir_in != nullptr) { - // Create the path to the application code cache directory. - // The memory will be release after Initialization or when the native bridge is closed. - const size_t len = strlen(app_data_dir_in) + strlen(kCodeCacheDir) + 2; // '\0' + '/' - app_code_cache_dir = new char[len]; - snprintf(app_code_cache_dir, len, "%s/%s", app_data_dir_in, kCodeCacheDir); - } else { - ALOGW("Application private directory isn't available."); - app_code_cache_dir = nullptr; - } - - // Bind-mount /system/lib{,64}/<isa>/cpuinfo to /proc/cpuinfo. - // Failure is not fatal and will keep the native bridge in kPreInitialized. - state = NativeBridgeState::kPreInitialized; - -#ifndef __APPLE__ +static void MountCpuinfoForInstructionSet(const char* instruction_set) { if (instruction_set == nullptr) { - return true; + return; } + size_t isa_len = strlen(instruction_set); if (isa_len > 10) { // 10 is a loose upper bound on the currently known instruction sets (a tight bound is 7 for @@ -296,37 +291,60 @@ bool PreInitializeNativeBridge(const char* app_data_dir_in, const char* instruct // be another instruction set in the future. ALOGW("Instruction set %s is malformed, must be less than or equal to 10 characters.", instruction_set); - return true; + return; } - // If the file does not exist, the mount command will fail, - // so we save the extra file existence check. +#if defined(__APPLE__) + ALOGW("Mac OS does not support bind-mounting. Host simulation of native bridge impossible."); + +#elif !defined(__ANDROID__) + // To be able to test on the host, we hardwire a relative path. + MountCpuinfo("./cpuinfo"); + +#else // __ANDROID__ char cpuinfo_path[1024]; -#if defined(__ANDROID__) - snprintf(cpuinfo_path, sizeof(cpuinfo_path), "/system/lib" + // Bind-mount /system/etc/cpuinfo.<isa>.txt to /proc/cpuinfo. + snprintf(cpuinfo_path, sizeof(cpuinfo_path), "/system/etc/cpuinfo.%s.txt", instruction_set); + if (MountCpuinfo(cpuinfo_path)) { + return; + } + + // Bind-mount /system/lib{,64}/<isa>/cpuinfo to /proc/cpuinfo. + // TODO(b/179753190): remove when all implementations migrate to system/etc! #ifdef __LP64__ - "64" + snprintf(cpuinfo_path, sizeof(cpuinfo_path), "/system/lib64/%s/cpuinfo", instruction_set); +#else + snprintf(cpuinfo_path, sizeof(cpuinfo_path), "/system/lib/%s/cpuinfo", instruction_set); #endif // __LP64__ - "/%s/cpuinfo", instruction_set); -#else // !__ANDROID__ - // To be able to test on the host, we hardwire a relative path. - snprintf(cpuinfo_path, sizeof(cpuinfo_path), "./cpuinfo"); + MountCpuinfo(cpuinfo_path); + #endif +} - // Bind-mount. - if (TEMP_FAILURE_RETRY(mount(cpuinfo_path, // Source. - "/proc/cpuinfo", // Target. - nullptr, // FS type. - MS_BIND, // Mount flags: bind mount. - nullptr)) == -1) { // "Data." - ALOGW("Failed to bind-mount %s as /proc/cpuinfo: %s", cpuinfo_path, strerror(errno)); +bool PreInitializeNativeBridge(const char* app_data_dir_in, const char* instruction_set) { + if (state != NativeBridgeState::kOpened) { + ALOGE("Invalid state: native bridge is expected to be opened."); + CloseNativeBridge(true); + return false; + } + + if (app_data_dir_in != nullptr) { + // Create the path to the application code cache directory. + // The memory will be release after Initialization or when the native bridge is closed. + const size_t len = strlen(app_data_dir_in) + strlen(kCodeCacheDir) + 2; // '\0' + '/' + app_code_cache_dir = new char[len]; + snprintf(app_code_cache_dir, len, "%s/%s", app_data_dir_in, kCodeCacheDir); + } else { + ALOGW("Application private directory isn't available."); + app_code_cache_dir = nullptr; } -#else // __APPLE__ - UNUSED(instruction_set); - ALOGW("Mac OS does not support bind-mounting. Host simulation of native bridge impossible."); -#endif + // Mount cpuinfo that corresponds to the instruction set. + // Failure is not fatal. + MountCpuinfoForInstructionSet(instruction_set); + + state = NativeBridgeState::kPreInitialized; return true; } |