fix(libkernel): refuse mlock on PROT_NONE ranges on macOS #10889

Merged
krishalivaibhav merged 1 commit from upstream-pr/10889 into main 2026-10-27 06:58:58 +00:00
krishalivaibhav commented 2026-10-25 07:36:15 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by krishalivaibhav on 2026-10-25T07:36:15Z.


What

On macOS, mlock on a range that includes PROT_NONE pages succeeds, while the PS5 kernel refuses it. guest_memory aborted because sceKernelMlock returned 0 where the guest expects an error.

Under __APPLE__, LockHostPages now walks the range with mach_vm_region before calling ::mlock. It returns SCE_KERNEL_ERROR_ENOMEM if any part is unmapped or has neither read nor execute permission, as in FreeBSD's vm_map_wire_locked. Any other mach_vm_region failure 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_memory passes (it aborted before). This also needs a two-line sceKernelMunmap cast in #7631's test, which I reported on #7631;
  • ctest -R '^(guest_|libc_|libkernel_)': no new failures;
  • guest_mlock also needs #10898 (sceKernelMunlockall on macOS).

Building on macOS needs #10552 and the PRs listed above.

Linux/Windows: not built locally. The change is __APPLE__-only.

Checklist

  • Based on current main. Overlap: #5021 (HasInaccessiblePages) and #7201 (HostRangeAccessible) add the same preflight in LockHostPages; #10898 builds on this PR
  • One topic per PR; follow-ups go in a new PR
  • No comments in code except technical debt
  • Unimplemented paths throw (NotImplemented_nid_no_patch); silent stubs are listed under Silent stubs in their library's technical debt file
  • No notes, investigation .md files or images added to the repository
  • New third-party code is a submodule built from source
  • Generic behaviour, not specific to one title
  • Depends on: none
  • Fixes: none
  • Unblocks: none
  • GPUs tested: n/a
  • AI-assisted: yes

🤖 Generated with Claude Code

<!-- greyfinch-sync:boykopovar/AnyPS5:issue:5002794479 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/10889). Originally posted by [krishalivaibhav](https://github.com/krishalivaibhav) on 2026-10-25T07:36:15Z. --- ### What On macOS, `mlock` on a range that includes `PROT_NONE` pages succeeds, while the PS5 kernel refuses it. `guest_memory` aborted because `sceKernelMlock` returned 0 where the guest expects an error. Under `__APPLE__`, `LockHostPages` now walks the range with `mach_vm_region` before calling `::mlock`. It returns `SCE_KERNEL_ERROR_ENOMEM` if any part is unmapped or has neither read nor execute permission, as in FreeBSD's `vm_map_wire_locked`. Any other `mach_vm_region` failure 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_memory` passes (it aborted before). This also needs a two-line `sceKernelMunmap` cast in #7631's test, which I reported on #7631; - `ctest -R '^(guest_|libc_|libkernel_)'`: no new failures; - `guest_mlock` also needs #10898 (`sceKernelMunlockall` on macOS). Building on macOS needs #10552 and the PRs listed above. Linux/Windows: not built locally. The change is `__APPLE__`-only. ### Checklist - [x] Based on current `main`. Overlap: #5021 (`HasInaccessiblePages`) and #7201 (`HostRangeAccessible`) add the same preflight in `LockHostPages`; #10898 builds on this PR - [x] One topic per PR; follow-ups go in a new PR - [x] No comments in code except technical debt - [x] Unimplemented paths throw (`NotImplemented_nid_no_patch`); silent stubs are listed under Silent stubs in their library's technical debt file - [x] No notes, investigation `.md` files or images added to the repository - [x] New third-party code is a submodule built from source - [x] Generic behaviour, not specific to one title - [x] Depends on: none - [x] Fixes: none - [x] Unblocks: none - [x] GPUs tested: n/a - [x] AI-assisted: yes 🤖 Generated with [Claude Code](https://claude.com/claude-code)
imellag commented 2026-10-25 08:56:14 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by imellag on 2026-10-25T08:56:14Z.

Review: APPROVED


Tested head 525c56b095 on Apple M4 Pro, macOS 27.0.1, Apple clang 21, x86-64 under Rosetta 2. The tree is main 61fc572cee plus the open macOS build PRs (with the two sceKernelMunmap casts in #7631's test) and the parts of mugurc's series that #7201 also carries. Only the __APPLE__ loop in LockHostPages was toggled.

  • Without it: guest_memory and guest_mlock abort, in 2 runs out of 2.
  • With it: guest_memory passes 3 runs out of 3.
  • guest_mlock also passes 3 out of 3 here, but it needs #10898 on top as well (the sceKernelMunlockall part of mugurc's f66a4df741). With this PR alone it still fails.
  • guest_system_configuration, guest_kernel_is_stack and guest_credentials pass in both trees.

The walk stops at the first gap (region > cursor) or VM_PROT_NONE region, and a failing mach_vm_region past the last mapping also returns ENOMEM, 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

<!-- greyfinch-sync:boykopovar/AnyPS5:review:5642354409 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/10889#pullrequestreview-5642354409). Originally posted by [imellag](https://github.com/imellag) on 2026-10-25T08:56:14Z. **Review:** APPROVED --- Tested head `525c56b095` on Apple M4 Pro, macOS 27.0.1, Apple clang 21, x86-64 under Rosetta 2. The tree is `main` `61fc572cee` plus the open macOS build PRs (with the two `sceKernelMunmap` casts in #7631's test) and the parts of mugurc's series that #7201 also carries. Only the `__APPLE__` loop in `LockHostPages` was toggled. - **Without it:** `guest_memory` and `guest_mlock` abort, in 2 runs out of 2. - **With it:** `guest_memory` passes 3 runs out of 3. - `guest_mlock` also passes 3 out of 3 here, but it needs #10898 on top as well (the `sceKernelMunlockall` part of mugurc's `f66a4df741`). With this PR alone it still fails. - `guest_system_configuration`, `guest_kernel_is_stack` and `guest_credentials` pass in both trees. The walk stops at the first gap (`region > cursor`) or `VM_PROT_NONE` region, and a failing `mach_vm_region` past the last mapping also returns `ENOMEM`, 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
maximilianfv commented 2026-10-25 09:56:32 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by maximilianfv on 2026-10-25T09:56:32Z.

Review: COMMENTED


The code reads correct, and ENOMEM matches what CheckMlock expects. 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 with main at 2742004ed2. Both hunks conflict because that series already has the same mach_vm_region walk in LockHostPages: a gap or a VM_PROT_NONE region returns SCE_KERNEL_ERROR_ENOMEM before mlock. I did not rewrite the patch to force it in.

guest_memory guest_mlock
Tree with the series' equivalent check (not this PR's diff) Passed 3/3 Passed 3/3

These 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

  1. Checklist line 1 is not accurate, and Depends on is not in the form CONTRIBUTING asks for. #5021 (HasInaccessiblePages) and #7201 (HostRangeAccessible) both add a preflight before mlock in this same LockHostPages, 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 write Depends on: #N or none. Fix: name #5021 and #7201 on line 1. Agree the order with their authors, as MarcZX10 asked. Write Depends on: as #N or none, and move the macOS build prerequisites into Tested.
  2. Optional: Export.cpp:694 is looser than FreeBSD. FreeBSD's vm_map_wire_locked (freebsd-src main) 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: LinuxProtFromSce adds read access whenever it sets write or execute. (info.protection & (VM_PROT_READ | VM_PROT_EXECUTE)) == 0 would state FreeBSD's rule exactly.
  3. Optional: Export.cpp:692-693 turns every mach_vm_region failure into ENOMEM. A range past the last mapping returns KERN_INVALID_ADDRESS, and ENOMEM is right for that case. Any other status is a host error, which the same function throws for unexpected mlock errors (Export.cpp:718). Fix: return ENOMEM only for KERN_INVALID_ADDRESS and throw std::system_error otherwise.
  4. Nit: the commit subject is fix(macos): ... and the PR title is fix(libkernel): .... Rewording the commit to fix(libkernel) would keep the history consistent with the title.

sceKernelMunlockall still calls ::munlockall() on macOS, which returns -1 with ENOSYS there, so ranges locked through this path are not unwired. This was already the case on main, and #10898 fixes it, so it is not this PR's job. It is also why guest_mlock needs #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).

<!-- greyfinch-sync:boykopovar/AnyPS5:review:5642616428 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/10889#pullrequestreview-5642616428). Originally posted by [maximilianfv](https://github.com/maximilianfv) on 2026-10-25T09:56:32Z. **Review:** COMMENTED --- The code reads correct, and `ENOMEM` matches what `CheckMlock` expects. 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 with `main` at `2742004ed2`. Both hunks conflict because that series already has the same `mach_vm_region` walk in `LockHostPages`: a gap or a `VM_PROT_NONE` region returns `SCE_KERNEL_ERROR_ENOMEM` before `mlock`. I did not rewrite the patch to force it in. | | `guest_memory` | `guest_mlock` | |---|---|---| | Tree with the series' equivalent check (not this PR's diff) | Passed 3/3 | Passed 3/3 | These 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 1. **Checklist line 1 is not accurate, and `Depends on` is not in the form CONTRIBUTING asks for.** #5021 (`HasInaccessiblePages`) and #7201 (`HostRangeAccessible`) both add a preflight before `mlock` in this same `LockHostPages`, 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 write `Depends on: #N` or `none`. Fix: name #5021 and #7201 on line 1. Agree the order with their authors, as MarcZX10 asked. Write `Depends on:` as `#N` or `none`, and move the macOS build prerequisites into `Tested`. 2. **Optional: `Export.cpp:694` is looser than FreeBSD.** FreeBSD's `vm_map_wire_locked` (freebsd-src `main`) 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: `LinuxProtFromSce` adds read access whenever it sets write or execute. `(info.protection & (VM_PROT_READ | VM_PROT_EXECUTE)) == 0` would state FreeBSD's rule exactly. 3. **Optional: `Export.cpp:692-693` turns every `mach_vm_region` failure into `ENOMEM`.** A range past the last mapping returns `KERN_INVALID_ADDRESS`, and `ENOMEM` is right for that case. Any other status is a host error, which the same function throws for unexpected `mlock` errors (`Export.cpp:718`). Fix: return `ENOMEM` only for `KERN_INVALID_ADDRESS` and throw `std::system_error` otherwise. 4. **Nit: the commit subject is `fix(macos): ...` and the PR title is `fix(libkernel): ...`.** Rewording the commit to `fix(libkernel)` would keep the history consistent with the title. `sceKernelMunlockall` still calls `::munlockall()` on macOS, which returns -1 with `ENOSYS` there, so ranges locked through this path are not unwired. This was already the case on `main`, and #10898 fixes it, so it is not this PR's job. It is also why `guest_mlock` needs #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).
maximilianfv commented 2026-10-25 12:09:49 +00:00 (Migrated from github.com)

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 under Tested), 2 ((protection & (READ | EXECUTE)) == 0), 3 (ENOMEM only for KERN_INVALID_ADDRESS, throw otherwise; std::runtime_error is fine for a kern_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 04638bf776 the diff still does not apply as-is to mugurc's macOS series (27e968974) merged with main at 2742004ed2. 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 from LockHostPages; 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_memory guest_mlock
Without this PR Aborted 3/3: Require in CheckMlock at GuestMemory.cpp:1656 (sceKernelMlock on a reserved range expects ENOMEM) Aborted 3/3: GuestMlock.cpp:156 (mlock on a reserved range expects -1 with errno 12)
With this PR Passed 3/3 Passed 3/3

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

  1. Nit: the PR title is narrower than the code. The title says PROT_NONE ranges, but Export.cpp:693 and :695 also 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).

<!-- greyfinch-sync:boykopovar/AnyPS5:review:5643144132 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/10889#pullrequestreview-5643144132). Originally posted by [maximilianfv](https://github.com/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 under `Tested`), 2 (`(protection & (READ | EXECUTE)) == 0`), 3 (`ENOMEM` only for `KERN_INVALID_ADDRESS`, throw otherwise; `std::runtime_error` is fine for a `kern_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 `04638bf776` the diff still does not apply as-is to mugurc's macOS series (`27e968974`) merged with `main` at `2742004ed2`. 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 from `LockHostPages`; 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_memory` | `guest_mlock` | |---|---|---| | Without this PR | Aborted 3/3: `Require` in `CheckMlock` at `GuestMemory.cpp:1656` (`sceKernelMlock` on a reserved range expects `ENOMEM`) | Aborted 3/3: `GuestMlock.cpp:156` (`mlock` on a reserved range expects -1 with `errno` 12) | | With this PR | Passed 3/3 | Passed 3/3 | 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 1. **Nit: the PR title is narrower than the code.** The title says `PROT_NONE ranges`, but `Export.cpp:693` and `:695` also 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).
Sign in to join this conversation.
No reviewers
No labels
No milestone
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Reference
boykopovar/AnyPS5!10889
No description provided.