fix(libkernel): place the AMM ranges in a reserved window outside the guest arena #27590
Loading…
Reference in a new issue
No description provided.
Delete branch "upstream-pr/27590"
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 brajankw on 2026-11-26T18:49:54Z.
What
Problem: the AMM range and the multimap range (
sceAmprAmmGetVirtualAddressRanges) were reserved at the top of the guest user arena, 0xb000000000 to 0xf800000000. That is address space titles use forMAP_FIXEDmaps and fixed virtual reservations, so a title's fixed reserve or map there aborted the process withfixed mapping ... overlaps a registered guest allocationas soon as AMM had been initialized.Cause:
_ammBasetook the highest free 288 GiB of the guest arena (GuestArenaHighestFree), so the AMM ranges were ordinary guest allocations inside the range a title owns.Fix:
sceKernelMapperGetParamreports on a console (PS5_Lichii, homebrew); it lies above the user arena (which ends at 0xFC00000000), so no title fixed map in the user arena can reach it and AMM addresses match the console's. The window is reserved at startup rather than on first AMM use because host mappings made while loading could otherwise take its address. A Linux x86-64 ThreadSanitizer build places it at 0x7E8000000000, inside the compiler-rt TSan application range.GuestArena::MapperAccessscope. A title's fixedmmap/sceKernelMmap(anonymous or file-backed), direct, flexible or reserve map, and its unmap, protection change, memory pool commit/decommit/batch, lock, range name or flexible release that overlaps the window callsNotImplemented_nid_no_patchonce its arguments pass validation; argument errors are still returned first. A non-fixed hint inside the window is placed in the user arena, like a hint above the arena.mmap,munmapandmprotectare no longernoexceptso that throw can reach the caller.sceKernelReleaseDirectMemory,sceKernelCheckedReleaseDirectMemory) of memory with a view in the window, or of memory given to the AMM pool, callsNotImplemented_nid_no_patchbefore it unmaps or frees anything. AMM pool gifts are serialized against direct memory releases, and a gift registers the address the allocator returned instead of reading it back from guest memory.Rebase onto
maind97bff95a7:PlaceHintInReservation: main (#27303) now rounds a hint up to its alignment. A hint inside the window is still not turned into a fixed map; the check now runs on the rounded range.mmap: the window check on a fixed target stays before main's alignment checks, and main'salignment(the page size for a fixed map) replaces the branch's own.mprotect: main's clamp to the user range (#27323) is kept, and the window check runs on the clamped range. The window lies inside that range, so the clamp does not change whether a call overlaps it.MemoryPoolExport.cpp, as in the previous rebase: main's committed-range accounting from #23301 stays inside this PR'sRequireProtectTargetandGuardedcommit/decommit.munmap:sceKernelMunmapnow rounds and validates the range itself (#24743). The window check runs on the same rounded range, and only whensceKernelMunmapwould accept it, so a null or above-user-range unmap across the window returns EINVAL again, and a length whose rounding wraps to one page inside the window throws.DoMapDirect,_mapFlexible,DoReserveVirtual,DoMunmapandDoMprotect, and main adds no new map, unmap, protect, reserve, pool or release export.guest_amm_placementcases for a fixedsceKernelMemoryPoolReservein the window at an address its alignment does not divide and with an end that wraps (both return EINVAL before the window check); the write watch is restored when the AMM sealing remap fails; and every fixed-map case in the window must callNotImplemented_nid_no_patch, not just throw.tools/pr_overlap.py --branchreports line overlaps with #15040, #17553, #18190, #24743, #27303, #27323 and #27330 (DirectMemory, Export, PosixMemory and GuestArena), and sharedmprotect/munmapexports with #18112 and #18190. It could not compare #1835, #7149, #11215, #16372, #20633, #21569 and #24924. None of them moves the AMM ranges or changes how find-next reports a reservation.This PR contains #27588 as its first commit. Review the commits after it.
Known gaps (from
docs/dev/debt/changes/fix-libkernel-amm-reserved-window.md):sceKernelMemoryPoolReserveoutput, ENOMEM for unallocated direct memory or a no-overwrite map over a used range) are replaced by the throw inside the window. The console's order is not measured.sceKernelVirtualQueryandsceKernelQueryMemoryProtectionreport the window to a title like any guest mapping; whether the console reports the mapper's range, and with which fields, is not measured.guest_amm_placementskips such a run. No TSan build is tested in CI.main, where the 8 GiB WebAssembly arena (EmscriptenArenaBytes) cannot hold the 288 GiB AMM ranges and_ammBasethrows as well.RLIMIT_ASon top of the about 976 GiB of the guest arena.mmappropagates out of the WebKitmapFileHandleinstead of itsfalseresult.Tested
Head
e138d461c0onmaind97bff95a7:x86_64-ucrt-posix-sehr7), Release,-DBUILD_TESTING=ON:ctest --test-dir build -j6 --output-on-failure -R "^(guest_amm_.*|guest_ampr_.*|MemoryPoolTest|guest_memory_pool_stats|guest_direct_memory_for_app|guest_virtual_query_arguments|guest_kernel_mapper_export_parity.*|guest_memory|guest_mmap_.*|guest_mprotect_unmapped|guest_flexible_memory_.*|guest_system_flexible_memory|guest_mlock|guest_copy_(in|out)_arena_windows|linux_read_trap_.*)$": 28/28 passed.guest_memoryincludes #20349's test. Inside them,guest_amm_placementskips its read-trap check on an AMM page (Windows does not trap reads of these pages) andguest_ampr_markersskips its symbolic link check (needs Developer Mode).munmapresolution,guest_amm_placementfails ("a munmap inside the AMM window whose rounded length wraps to one page did not call NotImplemented"); without the page-size check for fixed maps, it fails ("Invalid memory address, alignment or range").7ab118eff6, including the 12linux_read_trap_*tests. It was not rerun at this head.mainhas since moved to3d3fc2015f. The newer commits change only AGC and shader recompiler files, and the branch merges cleanly; the set was not rerun on it.guest_arena_*(GuestArenaHost.cpp) is registered only on macOS and was not run.tools/check_conventions.pyandtools/debt_fold.py --checkpass.Checklist
main; no other open PR implements the same functionsNotImplemented_nid_no_patch); silent stubs are listed under Silent stubs in their library's technical debt file.mdfiles or images added to the repository (attach them to this PR)🤖 Generated with Claude Code
Summary by CodeRabbit
Imported from boykopovar/AnyPS5. Originally posted by coderabbitai[bot] on 2026-11-26T18:59:57Z.
Review: COMMENTED
Actionable comments posted: 1
6de9d11a-ad73-43e4-880a-4d89871956b4Reviewing files that changed from the base of the PR and between
f7cfb96c4eand93d083ff6a.core/libs/prx/libSceAmpr/CMakeLists.txtcore/libs/prx/libc/include/GuestArena.hppcore/libs/prx/libc/src/GuestArena.cppcore/libs/prx/libkernel/Apr/include/AprCommandBuffer.hppcore/libs/prx/libkernel/Apr/src/Apr.cppcore/libs/prx/libkernel/DirectMemory/DirectMemory.cppcore/libs/prx/libkernel/DirectMemory/DirectMemory.hppcore/libs/prx/libkernel/DirectMemory/Export.cppcore/libs/prx/libkernel/DirectMemory/MemoryPool.cppcore/libs/prx/libkernel/DirectMemory/MemoryPoolExport.cppcore/libs/prx/libkernel/DirectMemory/PosixMemory.cppcore/libs/prx/libkernel/include/Api.hppcore/libs/tests/GuestAmmPlacement.cppcore/libs/tests/GuestAmpr.cppcore/libs/tests/GuestArenaHost.cppcore/libs/tests/GuestMemory.cppcore/libs/tests/MemoryPoolTest.cppcore/libs/tests/guest_arena/CMakeLists.txtdocs/dev/debt/changes/fix-libkernel-amm-reserved-window.mdIncluded review availability: This review used your included allowance. Your plan provides up to 10 included reviews per hour; 8 remain after this review.
Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-11-26T19:07:09Z.
Review: COMMENTED
Reviewed — comments
The review covers the prepared diff for head
93d083ff6a34f2161ae7dc7f712f58babb9b21a0; I found one concrete issue in the changed mapping logic, and did not run checks.What I ran
Read the prepared description, commits, manifest, diff and targeted head code using
repo_read.py. No test or build commands were run. The prepared description reports Windows and Linux test runs; those are author-reported, not runs performed for this review.Results
Findings
Blocking
None.
Non-blocking
core/libs/prx/libkernel/DirectMemory/MemoryPoolExport.cpp:170: The AMM-window guard only runs for fixed reserves that passFixedReserveValid. A fixedsceKernelMemoryPoolReservetargeting the AMM window with an invalid alignment or overflowing range skips this guard, copies the input toaddr_out, and returns the validation error fromDoReserveVirtual. The description says overlapping title operations throw after validation, so this may be intentional; the documented caveat says some state-dependent errors occur after the guard, but does not mention malformed fixed-reserve arguments. Please confirm the intended ordering and add a case toguest_amm_placementif these argument errors should also throw.Not verified
To get this to a good state
Nothing is required. Optional: clarify the malformed fixed-reserve argument ordering and cover it with a test if needed.
Imported from boykopovar/AnyPS5. Originally posted by coderabbitai[bot] on 2026-11-28T14:46:57Z.
Review: COMMENTED
Actionable comments posted: 3
ac1e0201-5e69-45aa-b51c-128564ed62beReviewing files that changed from the base of the PR and between
93d083ff6aand7ab118eff6.core/libs/prx/libc/include/GuestArena.hppcore/libs/prx/libc/src/GuestArena.cppcore/libs/prx/libkernel/DirectMemory/DirectMemory.cppcore/libs/prx/libkernel/DirectMemory/Export.cppcore/libs/prx/libkernel/DirectMemory/MemoryPool.cppcore/libs/prx/libkernel/DirectMemory/MemoryPoolExport.cppcore/libs/tests/GuestAmmPlacement.cppIncluded review availability: This review used your included allowance. Your plan provides up to 10 included reviews per hour; 9 remain after this review.
Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-11-28T14:50:03Z.
Review: COMMENTED
Reviewed — no blocking findings
The reviewed changes show no verified blocking defect, but I did not run the reported checks or read every changed file in full, so a formal approval is not justified.
What I ran
Reviewed head
7ab118eff6efbeeee5391311fe1c39fc6ff069b9usingrepo_read.py. No commands or tests were run.Results
Findings
Blocking
None verified in the inspected code.
Non-blocking
None.
Not verified
The PR description reports Windows and Linux test runs, convention checks, and debt checks; I did not run them. The full text of all 19 changed files was included in the review bundle, but I only read targeted source ranges in
core/libs/prx/libkernel/DirectMemory/Export.cpp,core/libs/prx/libkernel/DirectMemory/DirectMemory.cpp, andcore/libs/prx/libkernel/Apr/src/Apr.cpp. Other changed files and excluded-by-rule files were not independently verified. Console behavior and the reported host test results remain unconfirmed by this review.To get this to a good state
Nothing is required. Optional: complete a full changed-file review and run the reported checks on this head before seeking approval.
Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-11-28T16:06:14Z.
Review: COMMENTED
Reviewed — comments
The review is incomplete because only a subset of the changed implementation was inspected; no blocking defect was verified.
What I ran
Reviewed head
6785acc0b82b3f949de44616b1decb7d4df642bc. No commands or tests were run.Results
Findings
Blocking
None verified in the inspected code.
Non-blocking
None.
Not verified
The manifest lists 18 changed files, all included in the review bundle. Read the changed implementation in
core/libs/prx/libc/src/GuestArena.cpp,core/libs/prx/libkernel/Apr/src/Apr.cpp,core/libs/prx/libkernel/DirectMemory/Export.cpp,core/libs/prx/libkernel/DirectMemory/MemoryPoolExport.cpp,core/libs/prx/libkernel/DirectMemory/MemoryPool.cpp,core/libs/prx/libkernel/DirectMemory/PosixMemory.cpp, andcore/libs/prx/libkernel/DirectMemory/DirectMemory.hpp; also read the first line ofcore/libs/tests/GuestAmmPlacement.cpp. The remaining changed files and portions were not read, including most test changes. No files were reported as excluded by the manifest.To get this to a good state
Nothing is required. Optional: complete inspection of the unread changed files and run the reported checks on this head before making a formal decision.
Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-11-29T15:13:16Z.
Review: CHANGES_REQUESTED
Reviewed — changes requested
Changes are required before merge: guard WebAssembly AMM initialization so its range queries do not fail unconditionally when no mapper window is reserved.
What I ran
Reviewed head
e138d461c0993b72611fac0d38c81e78da0d09f4from the prepared patch and pinned repository reader. I did not run commands or tests.Results
Findings
Blocking
Arenaonly sets_mapperBasein its Linux/macOS and Windows constructor branches; on WebAssembly it remains zero._ammBasethrows whenwindowBase == 0, sosceAmprAmmGetVirtualAddressRangescannot succeed there. The description lists WebAssembly as an OS-error-zero case but does not identify AMM as unsupported. Add a WebAssembly-compatible reservation/placement strategy or explicitly guard the unsupported platform at the relevant API boundary. Evidence:core/libs/prx/libkernel/Apr/src/Apr.cpp:519.Non-blocking
None.
Not verified
To get this to a good state
Provide a WebAssembly behavior for the AMM mapper window, then rerun the relevant build and tests on the updated head.
Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-11-30T22:55:00Z.
Review: APPROVED
Reviewed — looks good
The post-#27588 changes at
43bedd0782926ca48f95494acbe06195182f5d5ashow no blocking defects in the inspected diff and source.What I ran
Inspected the prepared diff and manifest, and used
repo_read.pyagainst the pinned head. I did not run tests or builds.Results
Negative control
Not run; no test was modified or executed during this review.
Findings
Blocking
None.
Non-blocking
None.
Sources
Not verified
No tests, builds, or title runs were performed. The description reports Windows checks at an earlier head and Linux checks at a previous head; those results were not reproduced here. The prepared manifest lists 19 reviewed files and no excluded files.
To get this to a good state
Nothing is required.
Imported from boykopovar/AnyPS5. Originally posted by coderabbitai[bot] on 2026-11-30T22:59:35Z.
Review: COMMENTED
Architecture findings (2)
Advisory architecture findings (2)
7f53d895-0bec-4406-88c4-a64ee7ecceb8Reviewing files that changed from the base of the PR and between
e138d461c0and43bedd0782.core/libs/prx/libc/include/GuestArena.hppcore/libs/prx/libc/src/GuestArena.cppcore/libs/prx/libkernel/Apr/src/Apr.cppcore/libs/prx/libkernel/DirectMemory/MemoryPool.cppIncluded review availability: This review used your included allowance. Your plan provides up to 10 included reviews per hour; 9 remain after this review.
Imported from boykopovar/AnyPS5. Originally posted by ligmaSec on 2026-12-03T11:13:43Z.
Review: COMMENTED
Ran the paired Linux
guest_ampr_markers,guest_amm_placement, andguest_memoryCTests. All three passed on main and the submitted change. The branch tests exercised the reserved AMM window placement and next-region queries, including the reserved fragment and the flexible mapping that takes part of its address range.AI-assisted: yes.
Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-12-04T16:23:03Z.
Review: COMMENTED
Reviewed — no blocking findings
The reviewed diff at
b7dea492c01f245207d6a8899ba5da2a8e62e4a9shows no verified blocking defect; the author reports targeted tests passing on earlier revisions, but I did not run checks.What I ran
Reviewed the prepared diff and manifest for head
b7dea492c01f245207d6a8899ba5da2a8e62e4a9. No tests or build commands were run. The PR description reports test runs on earlier revisions; those are author-reported results, not runs performed for this review.Results
Negative control
Not run; no test execution was performed.
Findings
Blocking
None verified in the inspected diff.
Non-blocking
Sources
Not verified
The changed hunks in the prepared diff were inspected across all 20 files listed under
reviewedin the manifest; their full file texts were not read.files.jsonlists no excluded files. Runtime behavior, console compatibility, and the reported test results were not independently verified.To get this to a good state
Nothing is required. Optional: confirm the documented compatibility behavior with a console run.
View command line instructions
Checkout
From your project repository, check out a new branch and test the changes.Merge
Merge the changes and update on Forgejo.Warning: The "Autodetect manual merge" setting is not enabled for this repository, you will have to mark this pull request as manually merged afterwards.