fix(libkernel): fail sceKernelMprotect over an unmapped range with EINVAL instead of aborting #5143
Loading…
Reference in a new issue
No description provided.
Delete branch "upstream-pr/5143"
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 xXPBSXx on 2026-10-13T12:49:36Z.
What
sceKernelMprotectover a range that is not completely registered in the allocation tracker (an unmapped gap inside or at the end of the range) threwguest protection or unmap range has a hole(or... is not registered) fromGuestAllocations::Protect; nothing caught it, so the title was terminated. The POSIXmprotectwrapper caught the exception and returned-1withEINVAL.FreeBSD 11.4's
vm_map_protecthas no check for holes: its loop runs over the map entries that exist, so the gaps of a range are skipped and the mapped parts are protected.DoMprotectnow does the same: a newGuestAllocations::ForRegisteredhands out the registered parts of the range,DoMprotectprotects each of them and returns 0; a range with no registered part at all returnsSCE_KERNEL_ERROR_EINVAL(mprotect:EINVAL), as the existingGuestMemory.cpptest for an unmapped start expects. FreeBSD does not answerEINVALthere: with nothing mapped in the rangevm_map_protectfinds no entry and returnsKERN_SUCCESS, whichkern_mprotectturns into 0 (both read inrelease/11.4.0); theEINVALstays because that test pins it, not because of the source.DoMtypeprotectreturned the result ofDoMprotectonly at the very end after retyping the direct mappings; it now returns early on failure (no test covers that failure path). Every part is first checked for a GPU or image pin (RequireUnpinned), so a pinned part fails the call before the first part changes; the parts are then protected one after the other, so a host protection failure in a later part leaves the earlier parts changed (listed in the TechnicalDebt entry for libkernel together with the missing console measurement).GuestAllocations::Protectitself is unchanged and still throws on a hole;libSceAgcDriver/tests/GuestAllocations.cpp:159keeps pinning that.A range whose start is unmapped keeps answering
EINVALeven if later pages are mapped (pinned by the existingGuestMemory.cpptest). FreeBSD would take the next entry there (vm_map_protect:entry = entry->nextwhen the start lies in no entry) and protect the later pages, so only a gap inside or at the end of a range follows the FreeBSD source; the unmapped start is a deliberate difference.Not covered:
DoMprotectstill throwsinvalid_argumentfor address 0, length 0 or an overflowing range (first lines of the function), sosceKernelMprotect(NULL, ...)or a zero length ends the title as before (the POSIXmprotectwrapper catches it); a pinned range still throws fromRequireUnpinned; the guest-stack branch runs before the new code.Title evidence, and what is NOT shown: in saved runs of PPSA03001 the title stops with exactly
what(): guest protection or unmap range has a hole(11 run logs, 22 counted lines, e.g.PPSA03001_r/run_021704.err:4). The logs have no guest frame and not the arguments of the call, so that it is asceKernelMprotectover an unmapped gap is a hypothesis (the other caller ofProtect, the fixed remap path, checksCoversfirst). With this change such a call protects the mapped parts and returns 0; whether PPSA03001 then runs on is not tested.Tested
Windows 10 (19045), MinGW-w64 GCC 15.2.0 (WinLibs r7), Release, first run on
main0f2683cb7(the re-run with the current head andmainis the bullet "Re-run on 14 October 2026" below):guest_mprotect_unmapped(new): three pages mapped, the middle one unmapped:sceKernelMprotect,mprotectandsceKernelMtypeprotectover all three return 0 andsceKernelVirtualQueryshows the protection changed on both sides of the gap; unaligned ranges inside and across the gap work; a range that is completely unmapped returnsEINVAL(22 formprotect); in a second mapping, a range that ends in an unmapped page protects the mapped page.DirectMemory.cpp(sceKernelMprotect threw: guest protection or unmap range has a hole, the message of the title run).guest_memorypasses.git merge-tree: #4725 conflicts only in the test registration ofcore/libs/prx/libkernel/CMakeLists.txt(both add a test there); #1360 and #1330 already conflict withmainon their own; #5109 (sceKernelMprotecton foreign module images on Windows) editsIsGuestModuleImagein the same file for the Windows branch ofDoMprotect;git merge-treeof the two heads is clean (not built together), and the two changes look independent (it decides which images may be protected, this PR what happens to an unregistered gap inside a range); no other open PR touches the function. #4981 (floating-point environment for Windows threads) conflicts with this PR indocs/dev/debt/libkernel.md(git merge-treeof the two heads, re-checked; each is clean againstmain30f961266); whichever lands second rebases the debt entry.-j4), aftermainfeddf7f4cwas merged into the head (merge commite528ac9c5; the only conflict was the two independent test registrations incore/libs/prx/libkernel/CMakeLists.txtnext toguest_memory,guest_mprotect_unmappedand the newguest_flexible_memory_*tests, both kept and built):guest_mprotect_unmappedpasses (0.03 s, ctest) together withguest_memory, the fiveguest_flexible_memory_*cases,guest_mlock,guest_kernel_sysandguest_filesystem(10 of 10, 0 failed); withDirectMemory.cpptaken back tomainand the new test kept it fails (sceKernelMprotect threw: guest protection or unmap range has a hole, exit code0xc0000409). The commit238fe1eec(pin check for every part before the first change, after a review) was built and run afterwards on the samemain: the same eight tests (guest_mprotect_unmapped,guest_memory, the fiveguest_flexible_memory_*,guest_mlock) pass (8 of 8); it has no test of its own, because a pinned part next to a free part needs a GPU lease that only the AGC driver tests create.python tools/check_conventions.py --base upstream/mainpasses. This replaces the earlier figures. Not run: the socket tests, Linux, a console, a title.docs/dev/debt/libkernel.md(git merge-treeof the two heads, re-checked againstmaince5c83953; both heads are clean againstmain); whichever lands second rebases the debt entry.Checklist
main; no other open PR implements the same functionsNotImplemented_nid_no_patch); no silent stub added.mdfiles or images added to the repositoryAI-assisted (Claude Code).
🤖 Generated with Claude Code
Imported from boykopovar/AnyPS5. Originally posted by CnRJay on 2026-10-14T01:15:31Z.
Review: COMMENTED
Skipping the holes matches
vm_map_protectfor a range that starts in a mapping. One ordering difference, though: the new loop applies protection span by span, and a later span can still throw (RequireUnpinned, a hostmprotectfailure). The earlier spans then keep their new protection while the call fails.FreeBSD's
vm_map_protectworks in two passes over the entries. The first pass checks every entry in the range (max_protection, submap) and returnsKERN_PROTECTION_FAILUREbefore changing anything. Only the second pass applies the change. So a failedmprotectleaves the whole range unchanged. Collecting the spans (already done), running the checks that can fail (RequireUnpinnedper span) for all of them first, and only then callingProtectwould keep that property. Otherwise a title that retries after an error sees part of the range already changed.On the evidence: as the description says, the PPSA03001 logs show the throw but not the call, and the remap path is the other caller of
Protect. A stack (APS5_TRACE_*, or the crash report's guest frames) from one of those runs would confirm that it issceKernelMprotectover a gap before this is counted as the fix for that title.Imported from boykopovar/AnyPS5. Originally posted by xXPBSXx on 2026-10-14T02:25:16Z.
Thanks, both points are right.
Two passes:
vm_map_protect(release/11.4.0) checks every entry first and changes nothing if one fails (max_protection, submap), then applies.DoMprotectnow does the part of that which can fail on our side before it changes anything: commit238fe1eecrunsRequireUnpinnedfor every span first (a span pinned by GPU work or an image is the failure that can occur in the loop), and only then protects the spans one after the other. A hostmprotectfailure in a later span can still leave the earlier spans changed; thevm_map_protectsecond pass cannot fail, and there is no way to undo a host protection here, so the TechnicalDebt entry says it (updated in the same commit). No new test: a pinned span next to a free span would need a GPU lease from libkernel's test, which only the AGC driver tests create. The existing tests (guest_mprotect_unmapped,guest_memory,guest_flexible_memory_*,guest_mlock) pass with the commit (8 of 8, Windows).Evidence: I ran PPSA03001 on plain
mainfc1e388ed(90 s, 14 October): thehas a holeexception does not occur there (log withouthole|what()|FATAL|unhandled|terminate). The 11 runs that showed it are from 11 and 12 October on an oldermain, and they have no guest frame or arguments. So this PR does not count as the fix for PPSA03001; the description already says so.AI-assisted (Claude Code).
Imported from boykopovar/AnyPS5. Originally posted by github-actions[bot] on 2026-10-14T10:00:12Z.
Other open pull requests touch the same code. Merging one will break the other, or both implement the same thing. If this one builds on them, list them in Depends on.
main)docs/dev/debt/libkernel.mdL105Imported from boykopovar/AnyPS5. Originally posted by adriadam10 on 2026-10-14T17:49:52Z.
Checked on Linux: scoped build and ctest green (
guest_current_cpuaborted once in three runs, unrelated to this change);guest_mprotect_unmappedpasses and fails without theDirectMemory.cpp/GuestAllocationschange. The range handling (ForRegistered, unmapped start stillEINVAL, pin check before the first change) and the debt entry look right; merging once the full Build is green.