fix(libkernel): reserve the AMM ranges below host regions at the top of the guest arena #3683

Merged
Zurisen merged 2 commits from upstream-pr/3683 into main 2026-10-11 21:43:08 +00:00
Zurisen commented 2026-10-11 15:19:45 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by Zurisen on 2026-10-11T15:19:45Z.


What

Fixes #3371.

Since #3281 the AMM ranges are reserved at the last 64 GiB of the guest arena with GuestMapFixed | GuestMapNoOverwrite, and the reservation throws when that window is not free. On Windows the stacks of the threads that exist before the arena is reserved (the main thread and the loader's workers) are placed at random addresses, and the arena keeps them as holes. When one falls in the last 64 GiB, the first AMM use throws fixed guest mapping 0xec00000000+0x1000000000 overlaps a host region in the guest arena. The regions were identified in #3371: 2 MiB MEM_PRIVATE reservations with committed top pages and a PAGE_GUARD page, at random addresses in failing runs and absent from passing ones.

  • libc: GuestArenaHighestFree_nid_no_patch(bytes, alignment) returns the highest free aligned window of the arena, searching its used ranges (which include the host holes) from the top.
  • libkernel Apr.cpp: the AMM ranges are reserved at that window, 2 MiB aligned. When the last 64 GiB is free, the usual case, that is the same 0xec00000000 as now; otherwise the window ends just below the occupied part. It still throws when the arena has no free 64 GiB. This keeps #3281's reason for the top placement (the far end from the title's hinted maps); #3281 notes the console's AMM address is not known.
  • The new guest_amm_placement marks the last 4 MiB of the arena used before the AMM's first use, standing in for a stack, and requires the AMM ranges below it, with no free 64 GiB window left above them (@screamm's check: a real thread stack can sit just below the marked block, so the range may land several GiB lower). GuestAmpr.cpp's multimapEnd == arenaBase + arenaBytes becomes "inside the arena and above the hinted maps".

Tested

Windows 11 Home (26200), WinLibs GCC 15.2.0 r7, Release, RTX 4070 Laptop, on main a11c8f7f:

  • guest_amm_placement: 200/200 with the change. Without the Apr.cpp change it fails 5/5 with the #3371 message, 2 of them on a real stack (overlaps a host region).
  • guest_ampr_markers alone: 40/40 with the change; on main it failed 3 of 40 to 1 of 15 here.
  • Full ctest -j6 --repeat until-fail:3: 832/837. The five failures also fail on plain main a11c8f7f here: pr_overlap_encoding (Python 3.10), windows_exceptions (#3411), and guest_kernel_sys, guest_filesystem and agc_driver_emulated_color_compare (each 3/3 on main). Conventions check passes.
  • Linux: not run; blawesom reports on #3371 that the collision does not happen there (100/100), and the change takes the same window on Linux whenever the top is free.

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
<!-- greyfinch-sync:boykopovar/AnyPS5:issue:4820101557 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/3683). Originally posted by [Zurisen](https://github.com/Zurisen) on 2026-10-11T15:19:45Z. --- ### What Fixes #3371. Since #3281 the AMM ranges are reserved at the last 64 GiB of the guest arena with `GuestMapFixed | GuestMapNoOverwrite`, and the reservation throws when that window is not free. On Windows the stacks of the threads that exist before the arena is reserved (the main thread and the loader's workers) are placed at random addresses, and the arena keeps them as holes. When one falls in the last 64 GiB, the first AMM use throws `fixed guest mapping 0xec00000000+0x1000000000 overlaps a host region in the guest arena`. The regions were identified in #3371: 2 MiB `MEM_PRIVATE` reservations with committed top pages and a `PAGE_GUARD` page, at random addresses in failing runs and absent from passing ones. - libc: `GuestArenaHighestFree_nid_no_patch(bytes, alignment)` returns the highest free aligned window of the arena, searching its used ranges (which include the host holes) from the top. - libkernel `Apr.cpp`: the AMM ranges are reserved at that window, 2 MiB aligned. When the last 64 GiB is free, the usual case, that is the same `0xec00000000` as now; otherwise the window ends just below the occupied part. It still throws when the arena has no free 64 GiB. This keeps #3281's reason for the top placement (the far end from the title's hinted maps); #3281 notes the console's AMM address is not known. - The new `guest_amm_placement` marks the last 4 MiB of the arena used before the AMM's first use, standing in for a stack, and requires the AMM ranges below it, with no free 64 GiB window left above them (@screamm's check: a real thread stack can sit just below the marked block, so the range may land several GiB lower). `GuestAmpr.cpp`'s `multimapEnd == arenaBase + arenaBytes` becomes "inside the arena and above the hinted maps". ### Tested Windows 11 Home (26200), WinLibs GCC 15.2.0 r7, Release, RTX 4070 Laptop, on `main` `a11c8f7f`: - `guest_amm_placement`: 200/200 with the change. Without the `Apr.cpp` change it fails 5/5 with the #3371 message, 2 of them on a real stack (`overlaps a host region`). - `guest_ampr_markers` alone: 40/40 with the change; on `main` it failed 3 of 40 to 1 of 15 here. - Full `ctest -j6 --repeat until-fail:3`: 832/837. The five failures also fail on plain `main` `a11c8f7f` here: `pr_overlap_encoding` (Python 3.10), `windows_exceptions` (#3411), and `guest_kernel_sys`, `guest_filesystem` and `agc_driver_emulated_color_compare` (each 3/3 on `main`). Conventions check passes. - Linux: not run; blawesom reports on #3371 that the collision does not happen there (100/100), and the change takes the same window on Linux whenever the top is free. ### 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 - [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
screamm commented 2026-10-11 15:59:58 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by screamm on 2026-10-11T15:59:58Z.


Windows validation of 3c6c5d53 on main a11c8f7f: Windows 11 Pro 26220, WinLibs MinGW-w64 GCC 15.2.0 r7, Ninja, Release, BUILD_TESTING=ON, full build including libs. I ran it as a cherry-pick of ff8a615bb (the commit from #3371) onto the same main; the diff is the same six-file change and the resulting tree is identical to this head (254b2d8e).

  • guest_ampr_markers: 100/100 (--repeat until-fail:100).
  • guest_amm_placement on main (the Apr.cpp and GuestArena changes reverted, the test kept): fails 10/10 with fixed guest mapping 0xec00000000+0x1000000000 overlaps a guest arena range, so the test guards the bug.

guest_amm_placement is flaky on Windows as is. With this head it fails 21/200 here (and 5/80 in a second batch), always at AMM ranges are not at the top of the free guest arena. I added a temporary VirtualQuery dump of [multimapEnd, arenaEnd) to the test: in every failing run a real Windows thread stack sat just above the AMM range (MEM_PRIVATE 2 MiB reservations with a PAGE_GUARD | PAGE_READWRITE page, at 0xf9c7600000, 0xed2fe00000 and 0xf125c00000 in three failing runs), so the fix correctly placed the AMM 9–60 GiB below the arena top, and the 1 GiB bound in GuestAmmPlacement.cpp is what fails. Merging as is would trade one flaky test for another.

Suggested change, replacing only the 1 GiB check and keeping the "below the marked block" one:

Require(GuestArena::GuestArenaHighestFree_nid_no_patch(64ull << 30, 2ull << 20) < start, "AMM ranges are not at the top of the free guest arena");

It uses the helper this PR adds, so it checks the placement against the arena's own view of free space (no free 64 GiB window is left above the AMM range) rather than an absolute distance from the top. With it: 200/200 under ctest and 200/200 running the exe directly, and the test still fails on main (the AMM call throws there). Commit on my fork, cherry-pickable on top of this head: github.com/screamm/AnyPS5@b1e9b075f6. Of course feel free to do it your own way.

Full ctest -R '^guest_' -j8 (291 tests): the two failures known on this machine, guest_kernel_sys (#3510) and guest_filesystem (#3518), plus load-related timeouts in guest_pthread_start, guest_raise_exception and guest_pthread_rwlock (other builds were running); serially guest_pthread_start passed 3/3 and guest_raise_exception 2/3, its third run timing out while the machine was still loaded. Unrelated to this PR. check_conventions.py --base origin/main and check_test_registration.py are clean.

#3588 also touches Apr.cpp and GuestAmpr.cpp, without overlapping hunks.

AI-assisted (Claude Code); validation run on my machine.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6110884358 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/3683#issuecomment-6110884358). Originally posted by [screamm](https://github.com/screamm) on 2026-10-11T15:59:58Z. --- Windows validation of `3c6c5d53` on `main` `a11c8f7f`: Windows 11 Pro 26220, WinLibs MinGW-w64 GCC 15.2.0 r7, Ninja, Release, `BUILD_TESTING=ON`, full build including `libs`. I ran it as a cherry-pick of `ff8a615bb` (the commit from #3371) onto the same `main`; the diff is the same six-file change and the resulting tree is identical to this head (`254b2d8e`). - `guest_ampr_markers`: 100/100 (`--repeat until-fail:100`). - `guest_amm_placement` on `main` (the `Apr.cpp` and `GuestArena` changes reverted, the test kept): fails 10/10 with `fixed guest mapping 0xec00000000+0x1000000000 overlaps a guest arena range`, so the test guards the bug. **`guest_amm_placement` is flaky on Windows as is.** With this head it fails 21/200 here (and 5/80 in a second batch), always at `AMM ranges are not at the top of the free guest arena`. I added a temporary `VirtualQuery` dump of `[multimapEnd, arenaEnd)` to the test: in every failing run a real Windows thread stack sat just above the AMM range (`MEM_PRIVATE` 2 MiB reservations with a `PAGE_GUARD | PAGE_READWRITE` page, at `0xf9c7600000`, `0xed2fe00000` and `0xf125c00000` in three failing runs), so the fix correctly placed the AMM 9–60 GiB below the arena top, and the 1 GiB bound in `GuestAmmPlacement.cpp` is what fails. Merging as is would trade one flaky test for another. Suggested change, replacing only the 1 GiB check and keeping the "below the marked block" one: ```cpp Require(GuestArena::GuestArenaHighestFree_nid_no_patch(64ull << 30, 2ull << 20) < start, "AMM ranges are not at the top of the free guest arena"); ``` It uses the helper this PR adds, so it checks the placement against the arena's own view of free space (no free 64 GiB window is left above the AMM range) rather than an absolute distance from the top. With it: 200/200 under ctest and 200/200 running the exe directly, and the test still fails on `main` (the AMM call throws there). Commit on my fork, cherry-pickable on top of this head: https://github.com/screamm/AnyPS5/commit/b1e9b075f62f8a848b90c023aa132f9460a060cf. Of course feel free to do it your own way. Full `ctest -R '^guest_' -j8` (291 tests): the two failures known on this machine, `guest_kernel_sys` (#3510) and `guest_filesystem` (#3518), plus load-related timeouts in `guest_pthread_start`, `guest_raise_exception` and `guest_pthread_rwlock` (other builds were running); serially `guest_pthread_start` passed 3/3 and `guest_raise_exception` 2/3, its third run timing out while the machine was still loaded. Unrelated to this PR. `check_conventions.py --base origin/main` and `check_test_registration.py` are clean. #3588 also touches `Apr.cpp` and `GuestAmpr.cpp`, without overlapping hunks. AI-assisted (Claude Code); validation run on my machine.
Zurisen commented 2026-10-11 16:29:05 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by Zurisen on 2026-10-11T16:29:05Z.


@screamm Thanks, you're right: the 1 GiB bound assumed nothing real sits just below the marked block, and five runs were too few to catch it. I cherry-picked your commit as is (b2da41e5, your authorship). Here: guest_amm_placement 200/200 with it, and with Apr.cpp back to main it fails 5/5 (overlaps a guest arena range, twice overlaps a host region on a real stack), so it still guards the bug. Description updated.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6111159911 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/3683#issuecomment-6111159911). Originally posted by [Zurisen](https://github.com/Zurisen) on 2026-10-11T16:29:05Z. --- @screamm Thanks, you're right: the 1 GiB bound assumed nothing real sits just below the marked block, and five runs were too few to catch it. I cherry-picked your commit as is (`b2da41e5`, your authorship). Here: `guest_amm_placement` 200/200 with it, and with `Apr.cpp` back to `main` it fails 5/5 (`overlaps a guest arena range`, twice `overlaps a host region` on a real stack), so it still guards the bug. Description updated.
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!3683
No description provided.