fix(libc): reserve the guest arena around what Windows already placed in it #196

Closed
MichonGoddijn231849 wants to merge 1 commit from upstream-pr/196 into main
MichonGoddijn231849 commented 2026-09-30 21:11:57 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by MichonGoddijn231849 on 2026-09-30T21:11:57Z.


What

On Windows the guest arena wanted one free range starting at 0x200000000, and took the first base and size that fit. Windows places thread stacks and other reservations at random low addresses before libc starts, so the range was often taken. The arena then moved up (to 0x2200000000 in the failing runs) or shrank to 0x1c00000000. Guest mappings at the fixed low addresses titles use then fell outside it, and the process aborted with shared mapping outside the guest arena. The failure came and went between launches of the same title and build.

  • reserveAround walks [0x200000000, 0x7200000000). It reserves every free, allocation-granularity-aligned piece as a placeholder, and records what lies between the pieces as used holes, as the arena already did for the system-reserved range.
  • Release puts back any hole a released range overlapped, so a hole never becomes allocatable.
  • The previous search for another base is kept as the fallback for when less than the minimum arena size is free in the range.

Tested

Windows 11 Pro, AMD Ryzen 5 7600, NVIDIA GeForce RTX 5080 (driver 32.0.16.1074), 32 GB DDR5-5600, MinGW-w64 GCC 15.2.0.

  • GTA V (PPSA04264), repeated launches with a temporary log of the arena placement and the stack of the thread that creates it:
    • Before: 3 of 4 launches aborted at startup. The arena had moved to 0x2200000000 because stacks sat at 0x16b9xxxxxx, and one surviving launch had only 0x1c00000000 bytes.
    • With this change: 8 of 8 launches start. The arena is always 0x200000000–0x7200000000. In 4 of them thread stacks were inside the range and became holes (16 each).

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
  • New third-party code is a submodule built from source
  • Generic behaviour, not specific to one title
  • Depends on: none
  • AI-assisted: yes. Written with Claude (Anthropic) under my direction; I reviewed and tested it.

🤖 Generated with Claude Code

<!-- greyfinch-sync:boykopovar/AnyPS5:issue:4696078724 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/196). Originally posted by [MichonGoddijn231849](https://github.com/MichonGoddijn231849) on 2026-09-30T21:11:57Z. --- ### What On Windows the guest arena wanted one free range starting at 0x200000000, and took the first base and size that fit. Windows places thread stacks and other reservations at random low addresses before libc starts, so the range was often taken. The arena then moved up (to 0x2200000000 in the failing runs) or shrank to 0x1c00000000. Guest mappings at the fixed low addresses titles use then fell outside it, and the process aborted with `shared mapping outside the guest arena`. The failure came and went between launches of the same title and build. - `reserveAround` walks [0x200000000, 0x7200000000). It reserves every free, allocation-granularity-aligned piece as a placeholder, and records what lies between the pieces as used holes, as the arena already did for the system-reserved range. - `Release` puts back any hole a released range overlapped, so a hole never becomes allocatable. - The previous search for another base is kept as the fallback for when less than the minimum arena size is free in the range. ### Tested Windows 11 Pro, AMD Ryzen 5 7600, NVIDIA GeForce RTX 5080 (driver 32.0.16.1074), 32 GB DDR5-5600, MinGW-w64 GCC 15.2.0. - GTA V (PPSA04264), repeated launches with a temporary log of the arena placement and the stack of the thread that creates it: - Before: 3 of 4 launches aborted at startup. The arena had moved to 0x2200000000 because stacks sat at 0x16b9xxxxxx, and one surviving launch had only 0x1c00000000 bytes. - With this change: 8 of 8 launches start. The arena is always 0x200000000–0x7200000000. In 4 of them thread stacks were inside the range and became holes (16 each). ### 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 - [x] Unimplemented paths throw (`NotImplemented_nid_no_patch`); silent stubs are listed in TechnicalDebt - [x] No notes, investigation `.md` files or images added to the repository - [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. Written with Claude (Anthropic) under my direction; I reviewed and tested it. 🤖 Generated with [Claude Code](https://claude.com/claude-code)
boykopovar commented 2026-10-01 08:17:56 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by boykopovar on 2026-10-01T08:17:56Z.


This adds another fallback chain: if reserveAround reserves too little memory, it falls back to relocating or shrinking the arena. It also silently treats VirtualQuery and reservation failures as occupied holes.

These fallbacks are not allowed. Please implement one consistent arena reservation path that preserves the required address range. Occupied regions may be tracked as holes, but query or reservation failures must remain explicit errors. If the required arena cannot be established, throw instead of switching to a different layout.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:5927548746 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/196#issuecomment-5927548746). Originally posted by [boykopovar](https://github.com/boykopovar) on 2026-10-01T08:17:56Z. --- This adds another fallback chain: if `reserveAround` reserves too little memory, it falls back to relocating or shrinking the arena. It also silently treats `VirtualQuery` and reservation failures as occupied holes. These fallbacks are not allowed. Please implement one consistent arena reservation path that preserves the required address range. Occupied regions may be tracked as holes, but query or reservation failures must remain explicit errors. If the required arena cannot be established, throw instead of switching to a different layout.
MichonGoddijn231849 commented 2026-10-01 17:26:26 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by MichonGoddijn231849 on 2026-10-01T17:26:26Z.


Thanks for the review. Reworked as #231: the arena is always [0x200000000, 0x7200000000), occupied regions are holes, and a failed VirtualQuery or reservation throws; no relocating or shrinking.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:5936801950 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/196#issuecomment-5936801950). Originally posted by [MichonGoddijn231849](https://github.com/MichonGoddijn231849) on 2026-10-01T17:26:26Z. --- Thanks for the review. Reworked as #231: the arena is always [0x200000000, 0x7200000000), occupied regions are holes, and a failed VirtualQuery or reservation throws; no relocating or shrinking.

Pull request closed

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!196
No description provided.