Skip to content

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

Merged
oneandonlydean merged 1 commit into
boykopovar:mainfrom
Celegans12:fix/relinker-windows-low-entropy-stacks
Oct 5, 2026
Merged

oneandonlydean merged 1 commit into
boykopovar:mainfrom
Celegans12:fix/relinker-windows-low-entropy-stacks

Conversation

@Celegans12

@Celegans12 Celegans12 commented Oct 5, 2026 •

Copy link
Copy Markdown
Contributor

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.
  • fix(libc): reach the application map area end with the guest arena #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

The PE writer marked relocatable executables HIGH_ENTROPY_VA. Windows
then places the main thread stack, TEB, PEB and the loader threads'
stacks, which exist before libc reserves the guest arena from
0x2_0000_0000, anywhere in the first TiB. Nearly half the time they
land inside the arena and stay there as holes that fixed guest
mappings cannot use.

DllCharacteristics is now 0x140 (DYNAMIC_BASE, NX_COMPAT). The image
is still relocated and bottom-up allocations keep the 8-bit
randomization, but they start near the bottom of the address space,
far below the arena. DLL placement does not depend on this flag.

windows_address_space checks the flags and, on Windows, runs a
relinked guest that returns the high dword of its stack pointer.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@Celegans12
Celegans12 force-pushed the fix/relinker-windows-low-entropy-stacks branch from 8b542bc to 823c3ca Compare October 5, 2026 15:51
@oneandonlydean
oneandonlydean merged commit 7580b9e into boykopovar:main Oct 5, 2026
3 checks passed
oneandonlydean pushed a commit that referenced this pull request Nov 13, 2026
With MinGW's default HIGH_ENTROPY_VA, Windows places the stacks of a test
process's start-up threads anywhere from 4 GiB to past 1 TiB, so they sit
inside the guest arena range in almost every run. Each is a 2 MiB
reservation on a 2 MiB boundary, and when one lands on the AGC driver
memory at 0xfe0000000, driver start-up fails with "replace guest
placeholder with private memory: Attempt to access invalid address".

#679 stopped writing HIGH_ENTROPY_VA into title executables for this
reason, so link the test executables the same way: in
add_test_executable, and for guest_video_out_wqhd_detection_exit_tests,
the one test executable built outside it that loads libc.
agc_driver_frame_replay_relaunch_tests no longer needs its own flag.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants