fix(libkernel): refuse mlock on PROT_NONE ranges on macOS #10889
Loading…
Reference in a new issue
No description provided.
Delete branch "upstream-pr/10889"
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 krishalivaibhav on 2026-10-25T07:36:15Z.
What
On macOS,
mlockon a range that includesPROT_NONEpages succeeds, while the PS5 kernel refuses it.guest_memoryaborted becausesceKernelMlockreturned 0 where the guest expects an error.Under
__APPLE__,LockHostPagesnow walks the range withmach_vm_regionbefore calling::mlock. It returnsSCE_KERNEL_ERROR_ENOMEMif any part is unmapped or has neither read nor execute permission, as in FreeBSD'svm_map_wire_locked. Any othermach_vm_regionfailure throws. Linux and Windows are unchanged.The change follows iamthehimansh's
feat/macos-port(#8658), and iamthehimansh is credited as co-author.Tested
macOS: Apple Silicon M2, macOS 27.2, AppleClang, x86_64 Release, on a local integration of
main+ #10419 + #10366 + #10369 + #7631 + #7804 + #4630 + #10552 + #10853 + #10876 + #10877 + this commit:guest_memorypasses (it aborted before). This also needs a two-linesceKernelMunmapcast in #7631's test, which I reported on #7631;ctest -R '^(guest_|libc_|libkernel_)': no new failures;guest_mlockalso needs #10898 (sceKernelMunlockallon macOS).Building on macOS needs #10552 and the PRs listed above.
Linux/Windows: not built locally. The change is
__APPLE__-only.Checklist
main. Overlap: #5021 (HasInaccessiblePages) and #7201 (HostRangeAccessible) add the same preflight inLockHostPages; #10898 builds on this PRNotImplemented_nid_no_patch); silent stubs are listed under Silent stubs in their library's technical debt file.mdfiles or images added to the repository🤖 Generated with Claude Code
Imported from boykopovar/AnyPS5. Originally posted by imellag on 2026-10-25T08:56:14Z.
Review: APPROVED
Tested head
525c56b095on Apple M4 Pro, macOS 27.0.1, Apple clang 21, x86-64 under Rosetta 2. The tree ismain61fc572ceeplus the open macOS build PRs (with the twosceKernelMunmapcasts in #7631's test) and the parts of mugurc's series that #7201 also carries. Only the__APPLE__loop inLockHostPageswas toggled.guest_memoryandguest_mlockabort, in 2 runs out of 2.guest_memorypasses 3 runs out of 3.guest_mlockalso passes 3 out of 3 here, but it needs #10898 on top as well (thesceKernelMunlockallpart of mugurc'sf66a4df741). With this PR alone it still fails.guest_system_configuration,guest_kernel_is_stackandguest_credentialspass in both trees.The walk stops at the first gap (
region > cursor) orVM_PROT_NONEregion, and a failingmach_vm_regionpast the last mapping also returnsENOMEM, so a range that runs off the end of the address space is refused too. #10898 builds on this PR and drops its own version of the check.AI-assisted: yes
Imported from boykopovar/AnyPS5. Originally posted by maximilianfv on 2026-10-25T09:56:32Z.
Review: COMMENTED
The code reads correct, and
ENOMEMmatches whatCheckMlockexpects. I could not A/B this PR on my macOS tree because the tree already carries the same check, so this is a comment, not an approval. One checklist line needs fixing.Tested on Apple M1 Pro, macOS 27.0.1 (x86_64 build under Rosetta 2)
At
525c56b095, the diff does not apply to mugurc's macOS series (27e968974) merged withmainat2742004ed2. Both hunks conflict because that series already has the samemach_vm_regionwalk inLockHostPages: a gap or aVM_PROT_NONEregion returnsSCE_KERNEL_ERROR_ENOMEMbeforemlock. I did not rewrite the patch to force it in.guest_memoryguest_mlockThese runs show that the logic passes both tests on macOS. They are not an A/B of this PR. imellag's review above has an A/B that toggles only this loop.
Findings
Depends onis not in the form CONTRIBUTING asks for. #5021 (HasInaccessiblePages) and #7201 (HostRangeAccessible) both add a preflight beforemlockin this sameLockHostPages, and #10898 carries this change as its first commit. CONTRIBUTING asks for a check that no open PR implements the same functions. For an overlap, it asks you to agree the order with the other authors and writeDepends on: #Nornone. Fix: name #5021 and #7201 on line 1. Agree the order with their authors, as MarcZX10 asked. WriteDepends on:as#Nornone, and move the macOS build prerequisites intoTested.Export.cpp:694is looser than FreeBSD. FreeBSD'svm_map_wire_locked(freebsd-srcmain) refuses an entry when(protection & (VM_PROT_READ | VM_PROT_EXECUTE)) == 0, so it also refuses write-only regions. I found no current input that reaches the difference:LinuxProtFromSceadds read access whenever it sets write or execute.(info.protection & (VM_PROT_READ | VM_PROT_EXECUTE)) == 0would state FreeBSD's rule exactly.Export.cpp:692-693turns everymach_vm_regionfailure intoENOMEM. A range past the last mapping returnsKERN_INVALID_ADDRESS, andENOMEMis right for that case. Any other status is a host error, which the same function throws for unexpectedmlockerrors (Export.cpp:718). Fix: returnENOMEMonly forKERN_INVALID_ADDRESSand throwstd::system_errorotherwise.fix(macos): ...and the PR title isfix(libkernel): .... Rewording the commit tofix(libkernel)would keep the history consistent with the title.sceKernelMunlockallstill calls::munlockall()on macOS, which returns -1 withENOSYSthere, so ranges locked through this path are not unwired. This was already the case onmain, and #10898 fixes it, so it is not this PR's job. It is also whyguest_mlockneeds #10898 on top, as imellag notes.AI-assisted: yes (Claude Code tried to apply the PR, ran the tests, read the code and wrote this review).
Imported from boykopovar/AnyPS5. Originally posted by maximilianfv on 2026-10-25T12:09:49Z.
Review: APPROVED
Approve. All four points from my earlier review are fixed at
04638bf776, the code reads correct, and this time the A/B toggles only this PR's check: both tests abort without it and pass with it.Earlier points: 1 (checklist names #5021 and #7201,
Depends on: none, build prerequisites underTested), 2 ((protection & (READ | EXECUTE)) == 0), 3 (ENOMEMonly forKERN_INVALID_ADDRESS, throw otherwise;std::runtime_erroris fine for akern_return_t) and 4 (commit scope) are all resolved.Tested on Apple M1 Pro, macOS 27.0.1 (x86_64 build under Rosetta 2)
At
04638bf776the diff still does not apply as-is to mugurc's macOS series (27e968974) merged withmainat2742004ed2. The tree now also carries #10898 (08fcb44bb0), whose first commit is this PR's earlier loop. That makes a clean swap possible: without = that loop removed fromLockHostPages; with = the loop from this head, copied verbatim, in its place. Nothing else changed. Both sides built, and each test ran 3 times per side.guest_memoryguest_mlockRequireinCheckMlockatGuestMemory.cpp:1656(sceKernelMlockon a reserved range expectsENOMEM)GuestMlock.cpp:156(mlockon a reserved range expects -1 witherrno12)Last time I could not toggle this PR because the series had its own helper (
HasInaccessiblePages) in the same place. #10898 replaces that helper with this PR's loop, so the loop can now be swapped directly.Findings
PROT_NONE ranges, butExport.cpp:693and:695also refuse unmapped and write-only ranges. The commit subject (refuse mlock on unreadable and unmapped pages on macOS) describes it accurately, and a squash merge uses the title. Fix: use the commit subject as the title.The landing order with #5021 and #7201 is still waiting on their authors. It does not block this review.
AI-assisted: yes (Claude Code ran the swap A/B, read the code and wrote this review).