fix(relinker): keep Windows startup stacks below the guest arena #679

Merged
Celegans12 merged 1 commit from upstream-pr/679 into main 2026-10-05 18:57:18 +00:00
Celegans12 commented 2026-10-05 14:57:18 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by Celegans12 on 2026-10-05T14:57:18Z.


What

The relinked Windows executable is marked HIGH_ENTROPY_VA (DllCharacteristics 0x160), so Windows places the main thread's stack, TEB, PEB and the loader threads' stacks anywhere in the first TiB before libc reserves the guest arena from 0x2_0000_0000. Nearly half the time they land inside it and stay as holes; a fixed guest mapping over one throws fixed guest mapping overlaps a guest arena range.

  • WindowsPeWriter now writes 0x140 (DYNAMIC_BASE, NX_COMPAT) for relocatable images; 0x100 without relocations is unchanged. The flag only lets ASLR use the whole 64-bit space (PE format, /HIGHENTROPYVA), and the process's EnableHighEntropy ASLR policy follows it.
  • Trade-off: stacks, TEB, PEB, heaps and VirtualAlloc without an address keep only the 8-bit bottom-up randomization instead of up to a TiB, starting near the bottom of the address space. The executable and the DLLs are still relocated into the 0x7ff… image range with either flag.
  • Reserving the arena earlier cannot do this: these regions exist before the entry stub and TLS callbacks run, and the PE header only sizes the stack.
  • #652 extends the arena to 0xFC_0000_0000, over the 0x90_0000_0000-0xFC_0000_0000 range PPSA26344 v1.008 fills with fixed mappings, so the stacks then land inside it almost every time; it changes only libc.

Tested

  • Windows 11, MinGW-w64 GCC 15.2.0: configure -DBUILD_TESTING=ON, full build, --target libs, ctest:
  • 289/289 tests pass (ctest --output-on-failure)
  • New windows_address_space: relinks the optional_plt fixture with an entry returning rsp >> 32, checks DllCharacteristics 0x140 and, on Windows, runs it 8 times expecting the stack below 0x2_0000_0000. It fails on main (0x160). In two series of 200 runs on main, 195 put the stack in 0x2_0000_0000-0xFC_0000_0000 (81 and 95 below main's arena end 0x72_0000_0000); none do with this change.
  • PPSA26344 v1.008, current eboot.exe (0x160): in the thread dumps of 37 runs, the startup stacks were always in 0x2_0000_0000-0xFC_0000_0000, 17 times below 0x72_0000_0000 and 15 times in the range the title maps from 0x90_0000_0000. Not yet run relinked with this change.
  • Not tested: Linux (CI; only the flag check runs there), console behaviour.

Checklist

  • Based on current main; no other open PR implements the same functions
  • One topic per PR; follow-ups go in a new PR
  • No comments in code except technical debt
  • Unimplemented paths throw (NotImplemented_nid_no_patch); silent stubs are listed in TechnicalDebt
  • No notes, investigation .md files or images added to the repository (attach them to this PR)
  • New third-party code is a submodule built from source
  • Generic behaviour, not specific to one title
  • Depends on: none
  • AI-assisted: yes
<!-- greyfinch-sync:boykopovar/AnyPS5:issue:4746827363 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/679). Originally posted by [Celegans12](https://github.com/Celegans12) on 2026-10-05T14:57:18Z. --- ### What The relinked Windows executable is marked `HIGH_ENTROPY_VA` (DllCharacteristics 0x160), so Windows places the main thread's stack, TEB, PEB and the loader threads' stacks anywhere in the first TiB before libc reserves the guest arena from 0x2_0000_0000. Nearly half the time they land inside it and stay as holes; a fixed guest mapping over one throws `fixed guest mapping overlaps a guest arena range`. - `WindowsPeWriter` now writes 0x140 (`DYNAMIC_BASE`, `NX_COMPAT`) for relocatable images; 0x100 without relocations is unchanged. The flag only lets ASLR use the whole 64-bit space ([PE format](https://learn.microsoft.com/en-us/windows/win32/debug/pe-format#dll-characteristics), [/HIGHENTROPYVA](https://learn.microsoft.com/en-us/cpp/build/reference/highentropyva-support-64-bit-aslr)), and the process's `EnableHighEntropy` ASLR policy follows it. - Trade-off: stacks, TEB, PEB, heaps and `VirtualAlloc` without an address keep only the 8-bit bottom-up randomization instead of up to a TiB, starting near the bottom of the address space. The executable and the DLLs are still relocated into the 0x7ff… image range with either flag. - Reserving the arena earlier cannot do this: these regions exist before the entry stub and TLS callbacks run, and the PE header only sizes the stack. - #652 extends the arena to 0xFC_0000_0000, over the 0x90_0000_0000-0xFC_0000_0000 range PPSA26344 v1.008 fills with fixed mappings, so the stacks then land inside it almost every time; it changes only libc. ### Tested - Windows 11, MinGW-w64 GCC 15.2.0: configure `-DBUILD_TESTING=ON`, full build, `--target libs`, `ctest`: - 289/289 tests pass (`ctest --output-on-failure`) - New `windows_address_space`: relinks the `optional_plt` fixture with an entry returning `rsp >> 32`, checks DllCharacteristics 0x140 and, on Windows, runs it 8 times expecting the stack below 0x2_0000_0000. It fails on `main` (0x160). In two series of 200 runs on `main`, 195 put the stack in 0x2_0000_0000-0xFC_0000_0000 (81 and 95 below main's arena end 0x72_0000_0000); none do with this change. - PPSA26344 v1.008, current eboot.exe (0x160): in the thread dumps of 37 runs, the startup stacks were always in 0x2_0000_0000-0xFC_0000_0000, 17 times below 0x72_0000_0000 and 15 times in the range the title maps from 0x90_0000_0000. Not yet run relinked with this change. - Not tested: Linux (CI; only the flag check runs there), console behaviour. ### Checklist - [x] Based on current `main`; no other open PR implements the same functions - [x] One topic per PR; follow-ups go in a new PR - [x] No comments in code except [technical debt](https://github.com/boykopovar/AnyPS5/blob/main/docs/dev/CONVENTIONS.md) - [x] Unimplemented paths throw (`NotImplemented_nid_no_patch`); silent stubs are listed in [TechnicalDebt](https://github.com/boykopovar/AnyPS5/blob/main/docs/dev/TechnicalDebt.md#silent-stubs) - [x] No notes, investigation `.md` files or images added to the repository (attach them to this PR) - [x] New third-party code is a submodule built from source - [x] Generic behaviour, not specific to one title - [x] Depends on: none - [x] AI-assisted: yes
Sign in to join this conversation.
No reviewers
No labels
No milestone
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Reference
boykopovar/AnyPS5!679
No description provided.