fix(libkernel): round sceKernelMunmap ranges to guest pages #24743
Loading…
Reference in a new issue
No description provided.
Delete branch "upstream-pr/24743"
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 nodex0 on 2026-11-21T00:58:55Z.
What
Round the start of
sceKernelMunmapdown and the end up to 16 KiB boundaries, with the arithmetic and bounds the console showed: the offset in the page is added to the length modulo 2^64, a sum that rounds to 0 returns 0 without unmapping, and a start in the first page, a wrapping range or a range that ends above 0x800000000000 returnsEINVAL. The rounded range is used for unmapping, flexible-memory accounting, locked ranges and names. The POSIXmunmapwrapper now shares this path. This resolves the hardware question recorded by #13866.Tested
Measured on 2026-11-20 and 2026-11-21 in a PS5 homebrew payload process (firmware 13.40, SDK
0x13400002, elfldr). A title process may differ. Six ranges on anonymous, flexible and direct read/write mappings of four pages all returned 0: at a page boundary, lengths 1, 16384 and 16385 removed 1, 1 and 2 pages; one byte into a page, lengths 1 and 16384 removed 1 and 2 pages; two bytes straddling a boundary removed both. Adjacent pages stayed mapped with their protection and contents. Lengths near 2^64, the error cases, and the lowest and highest accepted addresses (0x4000, and a range that ends at 0x800000000000) were measured as well. The tables, payload sources and raw output are in the first comment.On Windows (WinLibs GCC 15.2.0, Release) a harness printing the same lines as console probes 2 to 5 (probe 1 is six ranges that probe 2 repeats) differs from the console on none of 263 lines with this change (
e4485b9b1c); onmainfc973e018946 of the first 164 differ, and a length that rounds to 0 throws.guest_memory,guest_mmap_fixed_anonymousand 13 other memory tests pass. The first version of the regression failed at the one-byte unmap with the original implementation.Branch base
fc973e0189. The conventions check,debt_fold.py --checkandgit diff --checkpassed. Linux and macOS were not tested. Not measured on the PS5: the flexible budget, because a payload process has none; the host test checks its accounting.pr_overlap.py --branch(2299 open pull requests) could not compare #18190, #18571 and #21569, which conflict withmainon their own, so I read their diffs: #18190 forwardssceKernelMunmapandmunmapthrough_nid_no_patchwrappers and renames themunmapdefinition this change edits, so whichever merges second needs a rebase; #18571 (madvise tests) and #21569 (range names) do not touchsceKernelMunmapor the new test.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)Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-11-21T01:19:27Z.
Review: DISMISSED
Reviewed — changes requested
Changes are required before merge: guard the page-rounding arithmetic against unsigned overflow before using the rounded range.
What I ran
Reviewed head
e4485b9b1c9b6a03e9dab17ef602224ccaf1043dand its prepared diff. No commands or tests were run.Results
Findings
Blocking
core/libs/prx/libkernel/DirectMemory/Export.cpp:417:len + offset + maskis calculated insize_tbefore the code checks for wrapping. If it wraps to a small page-aligned value, the laterendcheck may accept it and unmap a range much smaller than requested. For example, an alignedvaddrandlen == SIZE_MAX - mask + 1make the addition wrap to zero, after which the function returns success without unmapping; other overflow values can round to a nonzero range. Detect overflow in the additions before rounding, while preserving the specifically described modulo-2^64 zero case if that is confirmed behavior.Non-blocking
None.
Not verified
No test suite or hardware probe was run. The Windows harness and PS5 measurements in the description are author-reported. Reviewed all changed files listed in the manifest:
Export.cpp,PosixMemory.cpp,GuestMemory.cpp, andfix-libkernel-munmap-page-range.md. No excluded changed files were listed.To get this to a good state
Add explicit, well-defined checks for overflow in the page-rounding calculation and regression tests for values that currently wrap to a nonzero rounded length; rerun the relevant memory tests.
Imported from boykopovar/AnyPS5. Originally posted by PrivacyOnion on 2026-11-21T01:34:05Z.
Review: APPROVED
Verified on Linux (Ubuntu 24.04, GCC 13.3, Ninja, Release) against
e4485b9b1c:tools/check_conventions.pypassed.guest_memory_testsbuilt cleanly.guest_memory,guest_mmap_fixed_anonymous,guest_mmap_executable):core/libs/prx/libkernel/DirectMemory/Export.cpp,core/libs/prx/libkernel/DirectMemory/PosixMemory.cppreverted tomainand the new tests kept, the suite fails: Restoring the change makes it pass again.Looks Good To Me