Skip to content

Commit

Permalink
Change stack address on Windows
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
jart committed Jan 28, 2021
1 parent b4dffa4 commit d8fffd2
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 11 deletions.
3 changes: 1 addition & 2 deletions libc/calls/ntspawn.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
19 changes: 10 additions & 9 deletions libc/runtime/winmain.greg.c
Original file line number Diff line number Diff line change
Expand Up @@ -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];
Expand Down Expand Up @@ -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] == '\\') {
Expand All @@ -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);
}

Expand Down

0 comments on commit d8fffd2

Please sign in to comment.