Skip to content

Commit cc3b551

Browse files
[release/6.0] Use x64 directory if running x64 process on arm64 apphost (#68583)
* Use x64 directory if running x64 process on arm64 apphost Backport of #59890 * Fix is_emulating_x64 on Windows (#68671) Co-authored-by: Mateo Torres-Ruiz <[email protected]>
1 parent f95b02e commit cc3b551

File tree

3 files changed

+70
-0
lines changed

3 files changed

+70
-0
lines changed

src/native/corehost/hostmisc/pal.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -310,6 +310,7 @@ namespace pal
310310
void unload_library(dll_t library);
311311

312312
bool is_running_in_wow64();
313+
bool is_emulating_x64();
313314

314315
bool are_paths_equal_with_normalized_casing(const string_t& path1, const string_t& path2);
315316
}

src/native/corehost/hostmisc/pal.unix.cpp

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -505,6 +505,10 @@ bool pal::get_default_installation_dir(pal::string_t* recv)
505505

506506
#if defined(TARGET_OSX)
507507
recv->assign(_X("/usr/local/share/dotnet"));
508+
if (pal::is_emulating_x64())
509+
{
510+
append_path(recv, _X("x64"));
511+
}
508512
#else
509513
recv->assign(_X("/usr/share/dotnet"));
510514
#endif
@@ -979,6 +983,26 @@ bool pal::is_running_in_wow64()
979983
return false;
980984
}
981985

986+
bool pal::is_emulating_x64()
987+
{
988+
int is_translated_process = 0;
989+
#if defined(TARGET_OSX)
990+
size_t size = sizeof(is_translated_process);
991+
if (sysctlbyname("sysctl.proc_translated", &is_translated_process, &size, NULL, 0) == -1)
992+
{
993+
trace::info(_X("Could not determine whether the current process is running under Rosetta."));
994+
if (errno != ENOENT)
995+
{
996+
trace::info(_X("Call to sysctlbyname failed: %s"), strerror(errno));
997+
}
998+
999+
return false;
1000+
}
1001+
#endif
1002+
1003+
return is_translated_process == 1;
1004+
}
1005+
9821006
bool pal::are_paths_equal_with_normalized_casing(const string_t& path1, const string_t& path2)
9831007
{
9841008
#if defined(TARGET_OSX)

src/native/corehost/hostmisc/pal.windows.cpp

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -292,6 +292,11 @@ bool pal::get_default_installation_dir(pal::string_t* recv)
292292
}
293293

294294
append_path(recv, _X("dotnet"));
295+
if (pal::is_emulating_x64())
296+
{
297+
// Install location for emulated x64 should be %ProgramFiles%\dotnet\x64.
298+
append_path(recv, _X("x64"));
299+
}
295300

296301
return true;
297302
}
@@ -783,6 +788,46 @@ bool pal::is_running_in_wow64()
783788
return (fWow64Process != FALSE);
784789
}
785790

791+
typedef BOOL (WINAPI* is_wow64_process2)(
792+
HANDLE hProcess,
793+
USHORT *pProcessMachine,
794+
USHORT *pNativeMachine
795+
);
796+
797+
bool pal::is_emulating_x64()
798+
{
799+
#if defined(TARGET_AMD64)
800+
auto kernel32 = LoadLibraryExW(L"kernel32.dll", NULL, LOAD_LIBRARY_SEARCH_SYSTEM32);
801+
if (kernel32 == nullptr)
802+
{
803+
// Loading kernel32.dll failed, log the error and continue.
804+
trace::info(_X("Could not load 'kernel32.dll': %u"), GetLastError());
805+
return false;
806+
}
807+
808+
is_wow64_process2 is_wow64_process2_func = (is_wow64_process2)::GetProcAddress(kernel32, "IsWow64Process2");
809+
if (is_wow64_process2_func == nullptr)
810+
{
811+
// Could not find IsWow64Process2.
812+
return false;
813+
}
814+
815+
USHORT process_machine;
816+
USHORT native_machine;
817+
if (!is_wow64_process2_func(GetCurrentProcess(), &process_machine, &native_machine))
818+
{
819+
// IsWow64Process2 failed. Log the error and continue.
820+
trace::info(_X("Call to IsWow64Process2 failed: %u"), GetLastError());
821+
return false;
822+
}
823+
824+
// If we are running targeting x64 on a non-x64 machine, we are emulating
825+
return native_machine != IMAGE_FILE_MACHINE_AMD64;
826+
#else
827+
return false;
828+
#endif
829+
}
830+
786831
bool pal::are_paths_equal_with_normalized_casing(const string_t& path1, const string_t& path2)
787832
{
788833
// On Windows, paths are case-insensitive

0 commit comments

Comments
 (0)