fix(libkernel): reserve the AMM ranges below host regions at the top of the guest arena #3683
Loading…
Reference in a new issue
No description provided.
Delete branch "upstream-pr/3683"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
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 throwsfixed guest mapping 0xec00000000+0x1000000000 overlaps a host region in the guest arena. The regions were identified in #3371: 2 MiBMEM_PRIVATEreservations with committed top pages and aPAGE_GUARDpage, at random addresses in failing runs and absent from passing ones.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.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 same0xec00000000as 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.guest_amm_placementmarks 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'smultimapEnd == arenaBase + arenaBytesbecomes "inside the arena and above the hinted maps".Tested
Windows 11 Home (26200), WinLibs GCC 15.2.0 r7, Release, RTX 4070 Laptop, on
maina11c8f7f:guest_amm_placement: 200/200 with the change. Without theApr.cppchange it fails 5/5 with the #3371 message, 2 of them on a real stack (overlaps a host region).guest_ampr_markersalone: 40/40 with the change; onmainit failed 3 of 40 to 1 of 15 here.ctest -j6 --repeat until-fail:3: 832/837. The five failures also fail on plainmaina11c8f7fhere:pr_overlap_encoding(Python 3.10),windows_exceptions(#3411), andguest_kernel_sys,guest_filesystemandagc_driver_emulated_color_compare(each 3/3 onmain). Conventions check passes.Checklist
main; no other open PR implements the same functionsNotImplemented_nid_no_patch); silent stubs are listed in TechnicalDebt.mdfiles or images added to the repositoryImported from boykopovar/AnyPS5. Originally posted by screamm on 2026-10-11T15:59:58Z.
Windows validation of
3c6c5d53onmaina11c8f7f: Windows 11 Pro 26220, WinLibs MinGW-w64 GCC 15.2.0 r7, Ninja, Release,BUILD_TESTING=ON, full build includinglibs. I ran it as a cherry-pick offf8a615bb(the commit from #3371) onto the samemain; 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_placementonmain(theApr.cppandGuestArenachanges reverted, the test kept): fails 10/10 withfixed guest mapping 0xec00000000+0x1000000000 overlaps a guest arena range, so the test guards the bug.guest_amm_placementis flaky on Windows as is. With this head it fails 21/200 here (and 5/80 in a second batch), always atAMM ranges are not at the top of the free guest arena. I added a temporaryVirtualQuerydump of[multimapEnd, arenaEnd)to the test: in every failing run a real Windows thread stack sat just above the AMM range (MEM_PRIVATE2 MiB reservations with aPAGE_GUARD | PAGE_READWRITEpage, at0xf9c7600000,0xed2fe00000and0xf125c00000in three failing runs), so the fix correctly placed the AMM 9–60 GiB below the arena top, and the 1 GiB bound inGuestAmmPlacement.cppis 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:
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) andguest_filesystem(#3518), plus load-related timeouts inguest_pthread_start,guest_raise_exceptionandguest_pthread_rwlock(other builds were running); seriallyguest_pthread_startpassed 3/3 andguest_raise_exception2/3, its third run timing out while the machine was still loaded. Unrelated to this PR.check_conventions.py --base origin/mainandcheck_test_registration.pyare clean.#3588 also touches
Apr.cppandGuestAmpr.cpp, without overlapping hunks.AI-assisted (Claude Code); validation run on my machine.
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_placement200/200 with it, and withApr.cppback tomainit fails 5/5 (overlaps a guest arena range, twiceoverlaps a host regionon a real stack), so it still guards the bug. Description updated.