From d8fffd212303ec82ffdaa213a70328083cec14f7 Mon Sep 17 00:00:00 2001 From: Justine Tunney Date: Thu, 28 Jan 2021 00:25:52 -0800 Subject: [PATCH] Change stack address on Windows I rebooted Windows 7 and noticed that KERNEL32.DLL got relocated. Microsoft needs to change and embrace the dream that was MULTICS. Until then we'll need a better way to work around their dominion. --- libc/calls/ntspawn.c | 3 +-- libc/runtime/winmain.greg.c | 19 ++++++++++--------- 2 files changed, 11 insertions(+), 11 deletions(-) diff --git a/libc/calls/ntspawn.c b/libc/calls/ntspawn.c index ae6a51abd8d..cbb2a7995ed 100644 --- a/libc/calls/ntspawn.c +++ b/libc/calls/ntspawn.c @@ -84,8 +84,7 @@ textwindows int ntspawn( mkntenvblock(block->envvars, envp) != -1) { if (CreateProcess(NULL, block->cmdline, opt_lpProcessAttributes, opt_lpThreadAttributes, bInheritHandles, - dwCreationFlags | kNtCreateNewProcessGroup | - kNtCreateUnicodeEnvironment, + dwCreationFlags | kNtCreateUnicodeEnvironment, block->envvars, opt_lpCurrentDirectory, lpStartupInfo, opt_out_lpProcessInformation)) { rc = 0; diff --git a/libc/runtime/winmain.greg.c b/libc/runtime/winmain.greg.c index 23ea5df92ca..577ae19bb59 100644 --- a/libc/runtime/winmain.greg.c +++ b/libc/runtime/winmain.greg.c @@ -46,9 +46,8 @@ /* * TODO: Why can't we allocate addresses above 4GB on Windows 7 x64? - * https://github.com/jart/cosmopolitan/issues/19 + * TODO: How can we ensure we never overlap with KERNEL32.DLL? */ -#define ADDRESS 0x77700000 /*0000*/ struct WinArgs { char *argv[4096]; @@ -101,21 +100,23 @@ static textwindows wontreturn void WinMainNew(void) { int64_t h; size_t size; int i, count; + uint64_t addr; long auxv[1][2]; struct WinArgs *wa; const char16_t *env16; NormalizeCmdExe(); *(/*unconst*/ int *)&__hostos = WINDOWS; + addr = NtGetVersion() < kNtVersionWindows10 ? 0xff00000 : 0x777000000000; size = ROUNDUP(STACKSIZE + sizeof(struct WinArgs), FRAMESIZE); - _mmi.p[0].h = __mmap$nt((char *)ADDRESS, size, - PROT_READ | PROT_WRITE | PROT_EXEC, -1, 0) - .maphandle; - _mmi.p[0].x = ADDRESS >> 16; - _mmi.p[0].y = (ADDRESS >> 16) + ((size >> 16) - 1); + _mmi.p[0].h = + __mmap$nt((char *)addr, size, PROT_READ | PROT_WRITE | PROT_EXEC, -1, 0) + .maphandle; + _mmi.p[0].x = addr >> 16; + _mmi.p[0].y = (addr >> 16) + ((size >> 16) - 1); _mmi.p[0].prot = PROT_READ | PROT_WRITE | PROT_EXEC; _mmi.p[0].flags = MAP_PRIVATE | MAP_ANONYMOUS; _mmi.i = pushpop(1L); - wa = (struct WinArgs *)(ADDRESS + size - sizeof(struct WinArgs)); + wa = (struct WinArgs *)(addr + size - sizeof(struct WinArgs)); count = GetDosArgv(GetCommandLine(), wa->argblock, ARG_MAX, wa->argv, 4096); for (i = 0; wa->argv[0][i]; ++i) { if (wa->argv[0][i] == '\\') { @@ -127,7 +128,7 @@ static textwindows wontreturn void WinMainNew(void) { FreeEnvironmentStrings(env16); auxv[0][0] = pushpop(0L); auxv[0][1] = pushpop(0L); - _jmpstack((char *)ADDRESS + STACKSIZE, _executive, count, wa->argv, wa->envp, + _jmpstack((char *)addr + STACKSIZE, _executive, count, wa->argv, wa->envp, auxv); }