Skip to content

feat(libkernel): implement POSIX mprotect - #1094

Merged
adriadam10 merged 2 commits into
boykopovar:mainfrom
kjhq:feat/libkernel-mprotect
Oct 8, 2026
Merged

adriadam10 merged 2 commits into
boykopovar:mainfrom
kjhq:feat/libkernel-mprotect

Conversation

@kjhq

@kjhq kjhq commented Oct 7, 2026 •

Copy link
Copy Markdown
Contributor

What

Adds the POSIX mprotect export next to mmap and munmap in PosixMemory.cpp. It calls DoMprotect, the same path as sceKernelMprotect (which rounds the range out to guest pages, updates the allocation tracker and the host protection), and follows the POSIX error convention of its neighbours: -1 with errno.

  • zero length: 0, nothing changes (as FreeBSD)
  • null address or a range that wraps: EINVAL
  • a range DoMprotect rejects (it throws): EINVAL; std::bad_alloc: ENOMEM

The protection value is passed through unchanged, as sceKernelMprotect does.

guest_memory gains checks: zero length succeeds, a null address is EINVAL, an unaligned one-byte range makes its page read-only (the tracker reports it readable and not writable, and it can still be read), and protection can be restored to read-write.

Title

PPSA17221 (Minecraft) needs #716 to load the modules in its app root, so every run below is current main + #716 + the PRs of this set (#1088, #1090–#1096), plus local changes outside it: dialog, NpAuth and TextToSpeech2 stubs that open PRs cover (#560, #925, #301) and a GPU end-of-pipe change that is not submitted. With all of them the title loads a world. The line below is from a run with only this PR left out, Windows 11, WinLibs GCC, normal user session.

Without it startup stops at load:

FAIL: unresolved ELF import YQOfxL4QfeU from libkernel.prx

YQOfxL4QfeU is mprotect, imported by the title's libcohtml.Prospero.prx.

Tested

Debian 13 (WSL2), GCC 14.2.0, Release: full ctest 390/390 passed (GPU tests skipped: no Vulkan device in WSL), including guest_memory.

Windows 11, WinLibs GCC: PPSA17221 with this PR and the rest of the set loads a world (runs described above). Not tested with Windows ctest.

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 in TechnicalDebt
  • 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
  • AI-assisted: yes

🤖 Generated with Claude Code

@github-actions

github-actions Bot commented Oct 8, 2026 •

Copy link
Copy Markdown

Progress report (c3f4db7 → 53a31b5)

➖ System libraries: 85.22% (+0.0%, +1 implemented functions)

✅ 1 implemented
Library Name
libkernel mprotect_nid_postfix

kjhq and others added 2 commits October 8, 2026 08:24
mprotect changes the protection of guest memory through the same path
as sceKernelMprotect and reports failures through errno: EINVAL for a
null or overflowing range or a range the allocation tracker rejects,
ENOMEM when allocation fails. A zero length succeeds.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Both return -1/EINVAL: FreeBSD kern_mprotect (sys/vm/vm_mmap.c) maps only
PROTECTION_FAILURE, RESOURCE_SHORTAGE and OUT_OF_BOUNDS to their own errno, so
KERN_INVALID_ADDRESS falls to EINVAL.

Co-Authored-By: Claude <noreply@anthropic.com>
@adriadam10

Copy link
Copy Markdown
Collaborator

Checked build + full ctest on current main with this PR (419/419); guest_memory fails to link with the PosixMemory.cpp change reverted. Error codes follow FreeBSD kern_mprotect (sys/vm/vm_mmap.c): only protection failure, resource shortage and out-of-bounds get their own errno, so an unmapped range ends up as EINVAL (ENOMEM is the Linux behaviour).

Added one commit on top (tests only): mprotect on an unmapped range and on a range that wraps the address space both return -1/EINVAL.

Merging once CI is green.

@adriadam10
adriadam10 force-pushed the feat/libkernel-mprotect branch from 05fcc90 to 53a31b5 Compare October 8, 2026 06:43
@adriadam10
adriadam10 merged commit f830fea into boykopovar:main Oct 8, 2026
4 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.

2 participants