Skip to content

fix(libkernel): return 0 from sceKernelMprotect and sceKernelMtypeprotect for a zero length - #7033

Merged
oneandonlydean merged 1 commit into
boykopovar:mainfrom
milescoviello:fix/sce-mprotect-zero-length
Oct 17, 2026
Merged

oneandonlydean merged 1 commit into
boykopovar:mainfrom
milescoviello:fix/sce-mprotect-zero-length

Conversation

@milescoviello

@milescoviello milescoviello commented Oct 17, 2026 •

Copy link
Copy Markdown
Contributor

What

mprotect returns 0 for a zero length without looking at the address. sceKernelMprotect and sceKernelMtypeprotect passed a zero length on to DoMprotect, which throws std::invalid_argument (Invalid guest memory protection range) for an empty range. Both now return 0 for a zero length, as mprotect does, and change nothing.

On FreeBSD 11, sys_mprotect rounds the range to pages and passes it to vm_map_protect, which returns success for an empty range. So a zero length at a page-aligned address succeeds whether or not anything is mapped there. Not checked on a console.

I don't know of a title that hits this. I found it while going through the throw sites in libkernel.

Tested

Ubuntu 24.04, GCC 13.3, Release, on main 6a32550.

  • guest_mprotect_unmapped: new checks that a zero length succeeds and changes nothing, on a mapped page, on an unmapped page and at address 0, for sceKernelMprotect, sceKernelMtypeprotect and mprotect. With main's Export.cpp the test aborts on the first of these (sceKernelMprotect threw: Invalid guest memory protection range). With this change it passes 3 of 3.
  • ctest -R '^guest_|libc': 401 of 401 pass, with this and one other small libkernel fix applied together.
  • tools/check_conventions.py passes. tools/pr_overlap.py finds no conflicting lines. It couldn't compare against feat(aot): consolidate native ARM64 compiler, runtime and consumers #5021, test: organize library tests and label CTest suites #6044 and Macos m4 #6222, which conflict with main. None of their diffs touches sceKernelMprotect or sceKernelMtypeprotect. fix(libkernel): keep a protected host stack's guard page, and protect the Linux main stack #6443 changes DoMprotect itself, which this PR leaves alone.
  • Windows 11 Pro, Ryzen 7 5700X, WinLibs GCC 15.2.0 r7, Release, with the ten PRs of this series merged in order: this PR's test passes 3 of 3, and with only this PR's source reverted it fails with main's behaviour. (series run by @screamm)
  • Windows 11, Intel Core i7-13700HX, MinGW GCC 15.2.0, Release, at cbf006c on main 6a32550: guest_mprotect_unmapped passes 20 of 20, and with main's Export.cpp it aborts on Invalid guest memory protection range (review by @fogydes).

Not checked: macOS, a console.

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
  • 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
  • Unblocks: none
  • GPUs tested: n/a
  • AI-assisted: yes

🤖 Generated with Claude Code

…tect for a zero length

mprotect already returns 0 for a zero length. The sceKernel entry
points passed it to DoMprotect, which throws for an empty range.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@github-actions

Copy link
Copy Markdown

Some comparisons could not be completed because a pull request conflicts with the base branch. Review these shared files manually before concluding that there is no overlap.

Pull request Shared files not compared
PR 5021 core/libs/prx/libkernel/DirectMemory/Export.cpp
PR 6044 core/libs/tests/GuestMprotectUnmapped.cpp
PR 6222 core/libs/prx/libkernel/DirectMemory/Export.cpp

@screamm

screamm commented Oct 17, 2026

Copy link
Copy Markdown
Contributor

Windows run in #7023 (comment): guest_mprotect_unmapped 3/3. A small note: FreeBSD's sys_mprotect aligns addr down and rounds len + pageoff up, so a zero length at an unaligned address covers that page there. This PR (like main's mprotect) returns 0 for any zero length. It doesn't block.

Written with Claude Code assistance (validation by a subagent, reviewed in this session).

@milescoviello

Copy link
Copy Markdown
Contributor Author

Right, thanks @screamm. FreeBSD's kern_mprotect moves an unaligned addr down to its page and rounds len + pageoff up, so a zero length at an unaligned address changes that one page. This PR returns 0 for every zero length, as main's POSIX mprotect already does, which keeps the two entry points consistent. If a maintainer prefers the FreeBSD rounding for both, I'll do that as a follow-up rather than widen this one.

AI-assisted: yes (Claude Code)

@fogydes fogydes left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Native Windows x86-64 build and verification on exact head cbf006cb4b1ce82d6f0d065fc607bac0205fe00d applied on main 6a32550fd1.

Environment: Windows 11 (build 26300), 13th Gen Intel Core i7-13700HX, MinGW GCC 15.2.0, Ninja Release, BUILD_TESTING=ON.

  • guest_mprotect_unmapped passes cleanly: ctest -R "^guest_mprotect_unmapped$" --repeat until-fail:20 (20/20 runs passed).
  • Negative test verification: Reverting core/libs/prx/libkernel/DirectMemory/Export.cpp to main's implementation reproduces the abort on Windows as expected:
    sceKernelMprotect threw: Invalid guest memory protection range
    FATAL: abort() (SIGABRT)
    
    With this PR applied, empty range protections for sceKernelMprotect and sceKernelMtypeprotect succeed without modifying page permissions.
  • python tools/check_conventions.py --base origin/main: passed with 0 findings.
  • python tools/pr_overlap.py --branch pr-7033-review: 0 confirmed collisions.

Written with AI assistance (Claude Code). I ran the builds and tests above locally on my Windows machine.

@milescoviello

Copy link
Copy Markdown
Contributor Author

Thanks @fogydes, the 20-of-20 run and the negative control on Windows are added to the description.

AI-assisted: yes (Claude Code)

@oneandonlydean
oneandonlydean merged commit 4146636 into boykopovar:main Oct 17, 2026
11 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants