fix(libkernel): round an unaligned munmap address down like FreeBSD #10550

Merged
GitamarT045T3R merged 2 commits from upstream-pr/10550 into main 2026-10-27 08:11:20 +00:00
GitamarT045T3R commented 2026-10-24 09:34:03 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by GitamarT045T3R on 2026-10-24T09:34:03Z.


What

munmap failed with EINVAL for an address that is not page aligned. FreeBSD 11's sys_munmap rejects only a zero length and an address wrap. Otherwise it moves the address down to its page and grows the length by the same offset before rounding it up (pageoff = addr & PAGE_MASK; addr -= pageoff; size += pageoff), so every page the range touches is unmapped. munmap now does the same with the 16 KiB guest page.

The zero-length check still comes first, so munmap(addr + 1, 0) still fails with EINVAL, as in FreeBSD. The existing checks for a null address, an overflowing length and a range past the address space are unchanged. sceKernelMunmap, which has its own path, is not touched.

guest_memory checked that munmap(memory + 1, page) fails with EINVAL. That came with the original anonymous-mapping implementation (1bbdf7daa), not from a console measurement. It now checks the zero-length case at that address instead. The next check, which unmapped page 1 with munmap(memory + page, 1), now does it through memory + page + 1. The checks after it are unchanged: pages 0 and 2 are intact and mincore sees page 1 gone. Unaligned munmap on the console itself has not been measured.

Tested

Windows 10 Pro 22H2 (19045), MinGW-w64 GCC 15.2.0 (WinLibs r7), Ninja, Release, BUILD_TESTING=ON, on main 19cb822f8:

  • guest_memory fails with main's PosixMemory.cpp and passes with this change.
  • guest_flexible_memory_budget, guest_mprotect_unmapped, guest_low_address_memory, guest_msync and guest_jit_shared_memory still pass.
  • python tools/check_conventions.py --base origin/main passes.
  • git merge-tree against the 13 open PRs that touch these two files is clean. #6321 and #5021 already conflict with main there.
  • Not built or run on Linux or macOS.

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
  • Fixes: none
  • Unblocks: none
  • GPUs tested: n/a
  • AI-assisted: yes
<!-- greyfinch-sync:boykopovar/AnyPS5:issue:4994273146 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/10550). Originally posted by [GitamarT045T3R](https://github.com/GitamarT045T3R) on 2026-10-24T09:34:03Z. --- ### What `munmap` failed with `EINVAL` for an address that is not page aligned. FreeBSD 11's [`sys_munmap`](https://github.com/freebsd/freebsd-src/blob/34bd34750187bae02a0086402c78c031c59dd8b3/sys/vm/vm_mmap.c#L519-L583) rejects only a zero length and an address wrap. Otherwise it moves the address down to its page and grows the length by the same offset before rounding it up (`pageoff = addr & PAGE_MASK; addr -= pageoff; size += pageoff`), so every page the range touches is unmapped. `munmap` now does the same with the 16 KiB guest page. The zero-length check still comes first, so `munmap(addr + 1, 0)` still fails with `EINVAL`, as in FreeBSD. The existing checks for a null address, an overflowing length and a range past the address space are unchanged. `sceKernelMunmap`, which has its own path, is not touched. `guest_memory` checked that `munmap(memory + 1, page)` fails with `EINVAL`. That came with the original anonymous-mapping implementation (1bbdf7daa), not from a console measurement. It now checks the zero-length case at that address instead. The next check, which unmapped page 1 with `munmap(memory + page, 1)`, now does it through `memory + page + 1`. The checks after it are unchanged: pages 0 and 2 are intact and `mincore` sees page 1 gone. Unaligned `munmap` on the console itself has not been measured. ### Tested Windows 10 Pro 22H2 (19045), MinGW-w64 GCC 15.2.0 (WinLibs r7), Ninja, Release, `BUILD_TESTING=ON`, on `main` `19cb822f8`: - `guest_memory` fails with `main`'s `PosixMemory.cpp` and passes with this change. - `guest_flexible_memory_budget`, `guest_mprotect_unmapped`, `guest_low_address_memory`, `guest_msync` and `guest_jit_shared_memory` still pass. - `python tools/check_conventions.py --base origin/main` passes. - `git merge-tree` against the 13 open PRs that touch these two files is clean. #6321 and #5021 already conflict with `main` there. - Not built or run on Linux or macOS. ### Checklist - [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) - [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
MarcZX10 commented 2026-10-24 10:37:09 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-10-24T10:37:09Z.

Review: APPROVED


Reviewed the range arithmetic against the stated FreeBSD rule: the page offset is subtracted from the start, added to length with overflow protection, and the rounded span retains the existing zero/null/end-overflow checks. The test now preserves the zero-length error at an unaligned address and verifies that an unaligned start removes the touched page while adjacent pages remain mapped. The contributor reports the focused Windows guest-memory tests and conventions check passing. Hosted eligibility, conventions, intake, labels, and report pass; platform build jobs were skipped by the eligibility gate. I did not rerun the test locally.

<!-- greyfinch-sync:boykopovar/AnyPS5:review:5637947311 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/10550#pullrequestreview-5637947311). Originally posted by [MarcZX10](https://github.com/MarcZX10) on 2026-10-24T10:37:09Z. **Review:** APPROVED --- Reviewed the range arithmetic against the stated FreeBSD rule: the page offset is subtracted from the start, added to length with overflow protection, and the rounded span retains the existing zero/null/end-overflow checks. The test now preserves the zero-length error at an unaligned address and verifies that an unaligned start removes the touched page while adjacent pages remain mapped. The contributor reports the focused Windows guest-memory tests and conventions check passing. Hosted eligibility, conventions, intake, labels, and report pass; platform build jobs were skipped by the eligibility gate. I did not rerun the test locally.
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!10550
No description provided.