From 1aabedbd4a6628da6880a0854200cb619dec3883 Mon Sep 17 00:00:00 2001 From: Elinor Fung Date: Thu, 28 Apr 2022 09:06:06 -0700 Subject: [PATCH] Fix is_emulating_x64 on Windows --- src/native/corehost/hostmisc/pal.windows.cpp | 48 +++++++++++--------- 1 file changed, 26 insertions(+), 22 deletions(-) diff --git a/src/native/corehost/hostmisc/pal.windows.cpp b/src/native/corehost/hostmisc/pal.windows.cpp index a341b27ace8939..32abe1e39dc034 100644 --- a/src/native/corehost/hostmisc/pal.windows.cpp +++ b/src/native/corehost/hostmisc/pal.windows.cpp @@ -791,39 +791,43 @@ bool pal::is_running_in_wow64() } typedef BOOL (WINAPI* is_wow64_process2)( - HANDLE hProcess, - USHORT *pProcessMachine, - USHORT *pNativeMachine + HANDLE hProcess, + USHORT *pProcessMachine, + USHORT *pNativeMachine ); bool pal::is_emulating_x64() { - USHORT pProcessMachine, pNativeMachine; +#if defined(TARGET_AMD64) auto kernel32 = LoadLibraryExW(L"kernel32.dll", NULL, LOAD_LIBRARY_SEARCH_SYSTEM32); - if (kernel32) + if (kernel32 == nullptr) { - is_wow64_process2 isWow64Process2Func = (is_wow64_process2)GetProcAddress(kernel32, "IsWow64Process2"); - if (!isWow64Process2Func) - { - // Could not find IsWow64Process2. - return false; - } - - if (!isWow64Process2Func(GetCurrentProcess(), &pProcessMachine, &pNativeMachine)) - { - // IsWow64Process2 failed. Log the error and continue. - trace::info(_X("Call to IsWow64Process2 failed: %u"), GetLastError()); - return false; - } + // Loading kernel32.dll failed, log the error and continue. + trace::info(_X("Could not load 'kernel32.dll': %u"), GetLastError()); + return false; + } - // Check if we are running an x64 process on a non-x64 windows machine. - return pProcessMachine != pNativeMachine && pProcessMachine == IMAGE_FILE_MACHINE_AMD64; + is_wow64_process2 is_wow64_process2_func = (is_wow64_process2)::GetProcAddress(kernel32, "IsWow64Process2"); + if (is_wow64_process2_func == nullptr) + { + // Could not find IsWow64Process2. + return false; } - // Loading kernel32.dll failed, log the error and continue. - trace::info(_X("Could not load 'kernel32.dll': %u"), GetLastError()); + USHORT process_machine; + USHORT native_machine; + if (!is_wow64_process2_func(GetCurrentProcess(), &process_machine, &native_machine)) + { + // IsWow64Process2 failed. Log the error and continue. + trace::info(_X("Call to IsWow64Process2 failed: %u"), GetLastError()); + return false; + } + // If we are running targeting x64 on a non-x64 machine, we are emulating + return native_machine != IMAGE_FILE_MACHINE_AMD64; +#else return false; +#endif } bool pal::are_paths_equal_with_normalized_casing(const string_t& path1, const string_t& path2)