fix(libkernel): clamp mprotect ranges to the user map, as FreeBSD 11.4 does #27323

Merged
kiansaeidnia merged 1 commit from upstream-pr/27323 into main 2026-11-29 12:30:58 +00:00
kiansaeidnia commented 2026-11-25 20:53:54 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by kiansaeidnia on 2026-11-25T20:53:54Z.


What

mprotect now handles the range bounds as FreeBSD 11.4 does:

  • It returns EINVAL only when the page-rounded end of the range wraps, as in kern_mprotect.
  • It skips any part of the range below the first page or at or above 0x800000000000, as vm_map_protect clamps with VM_MAP_RANGE_CHECK.

So a range at NULL or wholly outside the user map now returns 0 and changes nothing. It used to give EINVAL, which came from 35ae8742a9 and was never measured. The only console measurement is the debt entry for firmware 13.60, which says unmapped ranges return 0, and this change agrees with it.

On Windows, DoMprotect's VirtualQuery walk now stops at an address above the process address space (ERROR_INVALID_PARAMETER) instead of throwing. A range that crosses 0x800000000000 therefore works there too, and sceKernelMprotect over that top window returns 0 as it already did on Linux.

The new debt entry records two remaining gaps against kern_mprotect:

  • A zero length at an unaligned address does not protect its page.
  • A length whose rounding overflows gives EINVAL.

Map() and PlaceHintInReservation are not touched (#27303).

Two open PRs touch the same lines: #15040 rewrites the VirtualQuery line this PR changes in DoMprotect, and #20978 (tests only) adds assertions next to the old mprotect(nullptr, ...) EINVAL line, which this PR replaces with 0. Neither implements the clamping or the wrap check, and whichever merges second needs a small rebase.

Tested

Windows 11, MinGW-w64 GCC 16.2, Release: these tests pass:

  • guest_memory
  • guest_mprotect_unmapped
  • guest_kernel_stack_protect
  • guest_jit_shared_memory
  • guest_low_address_memory
  • guest_kernel_mprotect_prot_none

The new assertions in guest_memory cover:

  • NULL and below-first-page ranges;
  • ranges above and crossing 0x800000000000;
  • the largest length that does not wrap (0) and the smallest that does (EINVAL);
  • the last page of the address space.

PosixMemory.cpp and DirectMemory.cpp were syntax-checked with g++ 11 on Linux (WSL Ubuntu).

Checklist

  • Based on current main; no other open PR implements the same functions
  • One topic per PR; follow-ups go in a new PR
  • No comments in code except technical debt and the licence notice of code adapted from upstream (see AGENTS.md)
  • 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 (attach them to this PR)
  • 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:5499005187 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/27323). Originally posted by [kiansaeidnia](https://github.com/kiansaeidnia) on 2026-11-25T20:53:54Z. --- ### What `mprotect` now handles the range bounds as FreeBSD 11.4 does: - It returns `EINVAL` only when the page-rounded end of the range wraps, as in `kern_mprotect`. - It skips any part of the range below the first page or at or above `0x800000000000`, as `vm_map_protect` clamps with `VM_MAP_RANGE_CHECK`. So a range at NULL or wholly outside the user map now returns 0 and changes nothing. It used to give `EINVAL`, which came from 35ae8742a96 and was never measured. The only console measurement is the debt entry for firmware 13.60, which says unmapped ranges return 0, and this change agrees with it. On Windows, `DoMprotect`'s `VirtualQuery` walk now stops at an address above the process address space (`ERROR_INVALID_PARAMETER`) instead of throwing. A range that crosses `0x800000000000` therefore works there too, and `sceKernelMprotect` over that top window returns 0 as it already did on Linux. The new debt entry records two remaining gaps against `kern_mprotect`: - A zero length at an unaligned address does not protect its page. - A length whose rounding overflows gives `EINVAL`. `Map()` and `PlaceHintInReservation` are not touched (#27303). Two open PRs touch the same lines: #15040 rewrites the `VirtualQuery` line this PR changes in `DoMprotect`, and #20978 (tests only) adds assertions next to the old `mprotect(nullptr, ...)` `EINVAL` line, which this PR replaces with 0. Neither implements the clamping or the wrap check, and whichever merges second needs a small rebase. ### Tested Windows 11, MinGW-w64 GCC 16.2, Release: these tests pass: - `guest_memory` - `guest_mprotect_unmapped` - `guest_kernel_stack_protect` - `guest_jit_shared_memory` - `guest_low_address_memory` - `guest_kernel_mprotect_prot_none` The new assertions in `guest_memory` cover: - NULL and below-first-page ranges; - ranges above and crossing `0x800000000000`; - the largest length that does not wrap (0) and the smallest that does (`EINVAL`); - the last page of the address space. `PosixMemory.cpp` and `DirectMemory.cpp` were syntax-checked with g++ 11 on Linux (WSL Ubuntu). ### Checklist <!-- Rules behind each item: https://github.com/boykopovar/AnyPS5/blob/main/CONTRIBUTING.md --> - [x] Based on current `main`; no other open PR implements the same functions - [x] One topic per PR; follow-ups go in a new PR - [x] No comments in code except [technical debt](https://github.com/boykopovar/AnyPS5/blob/main/docs/dev/CONVENTIONS.md) and the licence notice of code adapted from upstream (see AGENTS.md) - [x] Unimplemented paths throw (`NotImplemented_nid_no_patch`); silent stubs are listed under Silent stubs in their library's [technical debt](https://github.com/boykopovar/AnyPS5/tree/main/docs/dev/debt) file - [x] No notes, investigation `.md` files or images added to the repository (attach them to this PR) - [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)
MarcZX10 commented 2026-11-25 21:11:42 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-11-25T21:11:42Z.

Review: COMMENTED


Reviewed — comments

The code review found no verified blocking defect, but I did not read every changed file or run the reported checks, so approval is not justified.

What I ran

Inspected the prepared diff and targeted source ranges at head 59c15fb0dc4653a249ebd548cff4904a0b673117 using the PR reader. No tests or build checks were run. The description reports Windows 11 tests and Linux syntax checks; those results were not independently verified.

Results

Check Result Note
Diff and targeted code review passed Inspected mprotect changes and adjacent logic
Reported tests and syntax checks not run Author-reported only
Full changed-file review not run Only targeted ranges were read

Findings

Blocking

None identified in the inspected code.

Non-blocking

None.

Not verified

I read description.md, commits.md, manifest.json, and diff.patch, plus targeted ranges in PosixMemory.cpp, DirectMemory.cpp, and GuestMemory.cpp, and the complete five-line debt entry. I did not read every line of the changed C++ files, run tests, or verify the author-reported console behavior. The manifest lists four changed files, all marked included; no excluded files were listed.

To get this to a good state

Nothing is required. Optional: share the test results and console measurements when available.

<!-- greyfinch-sync:boykopovar/AnyPS5:review:6065045227 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/27323#pullrequestreview-6065045227). Originally posted by [MarcZX10](https://github.com/MarcZX10) on 2026-11-25T21:11:42Z. **Review:** COMMENTED --- # Reviewed — comments The code review found no verified blocking defect, but I did not read every changed file or run the reported checks, so approval is not justified. ## What I ran Inspected the prepared diff and targeted source ranges at head `59c15fb0dc4653a249ebd548cff4904a0b673117` using the PR reader. No tests or build checks were run. The description reports Windows 11 tests and Linux syntax checks; those results were not independently verified. ## Results | Check | Result | Note | |---|---|---| | Diff and targeted code review | passed | Inspected mprotect changes and adjacent logic | | Reported tests and syntax checks | not run | Author-reported only | | Full changed-file review | not run | Only targeted ranges were read | ## Findings ### Blocking None identified in the inspected code. ### Non-blocking None. ## Not verified I read `description.md`, `commits.md`, `manifest.json`, and `diff.patch`, plus targeted ranges in `PosixMemory.cpp`, `DirectMemory.cpp`, and `GuestMemory.cpp`, and the complete five-line debt entry. I did not read every line of the changed C++ files, run tests, or verify the author-reported console behavior. The manifest lists four changed files, all marked included; no excluded files were listed. ## To get this to a good state Nothing is required. Optional: share the test results and console measurements when available.
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!27323
No description provided.