Skip to content

Commit 7f3ae80

Browse files
jeremyd2019dscho
authored andcommitted
Cygwin: cache IsWow64Process2 host arch in wincap.
This was already used in the FAST_CWD check, and could be used in a couple other places. I found the "emulated"/process value returned from the function largely useless, so I did not cache it. It is useless because, as the docs say, it is set to IMAGE_FILE_MACHINE_UNKNOWN (0) if the process is not running under WOW64, but Microsoft also doesn't consider x64-on-ARM64 to be WOW64, so it is set to 0 regardless if the process is ARM64 or x64. You can tell the difference via GetProcessInformation(ProcessMachineTypeInfo), but for the current process even that's overkill: what we really want to know is the IMAGE_FILE_MACHINE_* constant for the Cygwin dll itself, which is conveniently located in memory already, so cache that in wincap also for easy comparisons. Signed-off-by: Jeremy Drake <[email protected]> (cherry picked from commit 46f7bcc)
1 parent 62f6088 commit 7f3ae80

File tree

3 files changed

+25
-4
lines changed

3 files changed

+25
-4
lines changed

winsup/cygwin/local_includes/wincap.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,8 @@ class wincapc
4242
RTL_OSVERSIONINFOEXW version;
4343
char osnam[40];
4444
const void *caps;
45+
USHORT host_mach;
46+
USHORT cygwin_mach;
4547
bool _is_server;
4648

4749
public:
@@ -61,6 +63,8 @@ class wincapc
6163
{ return (size_t) system_info.dwAllocationGranularity; }
6264
const char *osname () const { return osnam; }
6365
const DWORD build_number () const { return version.dwBuildNumber; }
66+
const USHORT host_machine () const { return host_mach; }
67+
const USHORT cygwin_machine () const { return cygwin_mach; }
6468

6569
#define IMPLEMENT(cap) cap() const { return ((wincaps *) this->caps)->cap; }
6670

winsup/cygwin/path.cc

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4828,14 +4828,12 @@ find_fast_cwd_pointer ()
48284828
static fcwd_access_t **
48294829
find_fast_cwd ()
48304830
{
4831-
USHORT emulated, hosted;
48324831
fcwd_access_t **f_cwd_ptr;
48334832

4834-
/* First check if we're running in WOW64 on ARM64 emulating AMD64. Skip
4833+
/* First check if we're running on an ARM64 system. Skip
48354834
fetching FAST_CWD pointer as long as there's no solution for finding
48364835
it on that system. */
4837-
if (IsWow64Process2 (GetCurrentProcess (), &emulated, &hosted)
4838-
&& hosted == IMAGE_FILE_MACHINE_ARM64)
4836+
if (wincap.host_machine () == IMAGE_FILE_MACHINE_ARM64)
48394837
return NULL;
48404838

48414839
/* Fetch the pointer but don't set the global fast_cwd_ptr yet. First

winsup/cygwin/wincap.cc

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -235,9 +235,15 @@ static const wincaps wincap_11 = {
235235

236236
wincapc wincap __attribute__((section (".cygwin_dll_common"), shared));
237237

238+
extern IMAGE_DOS_HEADER
239+
__image_base__;
240+
238241
void
239242
wincapc::init ()
240243
{
244+
PIMAGE_NT_HEADERS ntheader;
245+
USHORT emul_mach;
246+
241247
if (caps)
242248
return; // already initialized
243249

@@ -282,4 +288,17 @@ wincapc::init ()
282288

283289
__small_sprintf (osnam, "NT-%d.%d", version.dwMajorVersion,
284290
version.dwMinorVersion);
291+
292+
if (!IsWow64Process2 (GetCurrentProcess (), &emul_mach, &host_mach))
293+
{
294+
/* If IsWow64Process2 succeeded, it filled in host_mach. Assume the only
295+
way it fails for the current process is that we're running on an OS
296+
version where it's not implemented yet. As such, the only realistic
297+
option for host_mach is AMD64 */
298+
host_mach = IMAGE_FILE_MACHINE_AMD64;
299+
}
300+
301+
ntheader = (PIMAGE_NT_HEADERS)((LPBYTE) &__image_base__
302+
+ __image_base__.e_lfanew);
303+
cygwin_mach = ntheader->FileHeader.Machine;
285304
}

0 commit comments

Comments
 (0)