fix(libkernel): enforce FreeBSD 11.4 error precedence and vector copyout in mincore #12854

Merged
slopgobblercobbler merged 3 commits from upstream-pr/12854 into main 2026-11-04 19:19:31 +00:00
slopgobblercobbler commented 2026-10-29 12:51:06 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by slopgobblercobbler on 2026-10-29T12:51:06Z.


What

  • In core/libs/prx/libkernel/DirectMemory/PosixMemory.cpp, validate that the rounded address range in sys_mincore does not exceed GuestUserAddressEnd (0x800000000000) or wrap around (end < first).
  • Maintain FreeBSD 11.4 error precedence: range overflow or unmapped pages return ENOMEM (12) before testing vector writability (EFAULT), and length 0 over a mapped page succeeds without dereferencing or copying out to the vector.
  • Replaced direct pointer writes (std::fill_n(vec + ...)) with chunked stack buffering and safe GuestCopyOut_nid_no_patch, returning EFAULT when vec points to unmapped or unwritable guest memory without triggering host memory access violations.
  • Document in docs/dev/debt/libkernel.md that the range is checked in full before any write, and a vector that faults partway gets no bytes.

Tested

  • Windows 11 (MSVC / Clang) and CTest suite.
  • Unit test cases in core/libs/tests/GuestMemory.cpp covering unwritable non-null vectors (reinterpret_cast<char*>(0x10)), zero-length invocations, GuestUserAddressEnd bounds violations, and unmapped page precedence over vector validity.
  • Ubuntu 26.04 Linux verification courtesy of @jotitadev (GCC 15.2, Release): guest_memory passes 3/3, 7/7 memory tests pass (guest_memory*, guest_mmap_executable, guest_mmap_fixed_anonymous, guest_mprotect_unmapped); negative control with PosixMemory.cpp from main confirmed segfaulting 3/3 in unwritable vector test.
  • Verified red-to-green behavior.
  • python tools/check_conventions.py --base upstream/main --head HEAD passed with 0 findings.

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:5068324258 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/12854). Originally posted by [slopgobblercobbler](https://github.com/slopgobblercobbler) on 2026-10-29T12:51:06Z. --- ### What - In `core/libs/prx/libkernel/DirectMemory/PosixMemory.cpp`, validate that the rounded address range in `sys_mincore` does not exceed `GuestUserAddressEnd` (`0x800000000000`) or wrap around (`end < first`). - Maintain FreeBSD 11.4 error precedence: range overflow or unmapped pages return `ENOMEM` (12) before testing vector writability (`EFAULT`), and length 0 over a mapped page succeeds without dereferencing or copying out to the vector. - Replaced direct pointer writes (`std::fill_n(vec + ...)`) with chunked stack buffering and safe `GuestCopyOut_nid_no_patch`, returning `EFAULT` when `vec` points to unmapped or unwritable guest memory without triggering host memory access violations. - Document in `docs/dev/debt/libkernel.md` that the range is checked in full before any write, and a vector that faults partway gets no bytes. ### Tested - Windows 11 (MSVC / Clang) and CTest suite. - Unit test cases in `core/libs/tests/GuestMemory.cpp` covering unwritable non-null vectors (`reinterpret_cast<char*>(0x10)`), zero-length invocations, `GuestUserAddressEnd` bounds violations, and unmapped page precedence over vector validity. - Ubuntu 26.04 Linux verification courtesy of @jotitadev (GCC 15.2, Release): `guest_memory` passes 3/3, 7/7 memory tests pass (`guest_memory*`, `guest_mmap_executable`, `guest_mmap_fixed_anonymous`, `guest_mprotect_unmapped`); negative control with `PosixMemory.cpp` from `main` confirmed segfaulting 3/3 in unwritable vector test. - Verified red-to-green behavior. - `python tools/check_conventions.py --base upstream/main --head HEAD` passed with 0 findings. ### 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
jotitadev commented 2026-10-30 14:00:00 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by jotitadev on 2026-10-30T14:00:00Z.

Review: COMMENTED


First Linux run of this head (the body and the thread have Windows only).

Ubuntu 26.04, i7-8700, GCC 15.2, Release, BUILD_TESTING=ON. Head c51719a97f merged (--no-commit) onto current main f91137142e; the merge is clean.

  • guest_memory: passes 3/3.
  • guest_memory*, guest_mmap_executable, guest_mmap_fixed_anonymous, guest_mprotect_unmapped: 7/7 pass.
  • public_headers: passes.
  • Control: with PosixMemory.cpp from main and this PR's test kept, guest_memory segfaults 3/3 (the write through the unwritable vector).

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

<!-- greyfinch-sync:boykopovar/AnyPS5:review:5713374596 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/12854#pullrequestreview-5713374596). Originally posted by [jotitadev](https://github.com/jotitadev) on 2026-10-30T14:00:00Z. **Review:** COMMENTED --- First Linux run of this head (the body and the thread have Windows only). Ubuntu 26.04, i7-8700, GCC 15.2, Release, `BUILD_TESTING=ON`. Head `c51719a97f` merged (`--no-commit`) onto current `main` `f91137142e`; the merge is clean. - `guest_memory`: passes 3/3. - `guest_memory*`, `guest_mmap_executable`, `guest_mmap_fixed_anonymous`, `guest_mprotect_unmapped`: 7/7 pass. - `public_headers`: passes. - Control: with `PosixMemory.cpp` from `main` and this PR's test kept, `guest_memory` segfaults 3/3 (the write through the unwritable vector). Written with AI assistance (Claude Code). I ran the builds and tests above on my machine.
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!12854
No description provided.