fix(libkernel): fail the stat family with EFAULT when the output struct is not writable guest memory #9618

Merged
xXPBSXx merged 2 commits from upstream-pr/9618 into main 2026-10-25 10:52:21 +00:00
xXPBSXx commented 2026-10-22 04:40:10 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by xXPBSXx on 2026-10-22T04:40:10Z.


What

Fill a local struct and copy it out with a checked write, GuestCopyOut_nid_no_patch (new, libc General.cpp: HostWrite guard, then process_vm_writev on Linux and a VirtualQuery writability walk on Windows), failing with EFAULT. Applied to fstat_nid_disambig1/_fstat, sceKernelFstat, lstat, sceKernelStat (so stat) and _fstatfs. The order of checks is unchanged: descriptor and path errors still come first, the copy-out is last, as in sys_fstat/sys_stat/sys_fstatfs.

Fixes: #9617

Tested

Head c424bd17c8 (two commits, 83f28bd67f and c424bd17c8), rebased onto main f26434afa3; git merge-tree against current main 38d463a6cf is clean. The diff is 5 files, +148/-12: General.hpp +1, General.cpp +40, Open.cpp +3/-1, Stdio.cpp +15/-11, GuestFstatFault.cpp +89.

  • guest_fstat_fault and the six tests of the earlier description (guest_file_timestamps, guest_kernel_sys, guest_filesystem, guest_posix_filesystem, guest_socket_descriptors, guest_socket_pair), 7 tests, on main f26434afa3 with this head:
    • Windows 10, MinGW-w64 GCC 15.2, Release: 7 of 7 pass. With General.hpp, General.cpp, Open.cpp and Stdio.cpp taken from main and the test kept, guest_fstat_fault fails with a SegFault.
    • Ubuntu 24.04 under WSL2 (a virtualised kernel, not bare-metal), GCC 13.3, Release: 7 of 7 pass. With only General.cpp put back to its first-commit version (same tree, same build directory, test kept), guest_fstat_fault aborts at the EPERM case.
  • Measured by others, on the earlier head faa3926cdf (before this rebase): jotitadev on Linux (Ubuntu 26.04, GCC 15.2, head bd2c029ef merged onto main 17f533556), review; jotitadev on macOS (Mac M2 Max, macOS 27.2, x86_64 under Rosetta), on the #8658 port branch dc0c8db500 merged with faa3926cdf, not on main, review. I have not rerun macOS.
  • The rebase changed only the test file: its local extern "C" declarations were dropped because main now declares them in the headers, and the _fstatfs call casts to KernelStatfs*. The source hunks are unchanged.
  • check_conventions --base upstream/main passes.
  • Not tested by me: macOS; a stat buffer in write-watched memory on macOS.

Relation to other work

#9423 adds CopyGuestPath_nid_no_patch, a checked read of guest path strings. GuestCopyOut_nid_no_patch here is the write counterpart for output structs; the two touch different functions and do not overlap, but both use the same host mechanism (process_vm_* on Linux, a VirtualQuery walk on Windows) and could share one primitive later. If you prefer the check inline in each wrapper instead of a shared helper, say so and I will fold it.

Merge order with #9478 and #7447 (they share Stdio.cpp) is in the comments; this PR does not depend on them.

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

AI-assisted (Claude Code).

🤖 Generated with Claude Code

<!-- greyfinch-sync:boykopovar/AnyPS5:issue:4962197432 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/9618). Originally posted by [xXPBSXx](https://github.com/xXPBSXx) on 2026-10-22T04:40:10Z. --- ### What Fill a local struct and copy it out with a checked write, `GuestCopyOut_nid_no_patch` (new, libc General.cpp: `HostWrite` guard, then `process_vm_writev` on Linux and a `VirtualQuery` writability walk on Windows), failing with EFAULT. Applied to `fstat_nid_disambig1`/`_fstat`, `sceKernelFstat`, `lstat`, `sceKernelStat` (so `stat`) and `_fstatfs`. The order of checks is unchanged: descriptor and path errors still come first, the copy-out is last, as in `sys_fstat`/`sys_stat`/`sys_fstatfs`. Fixes: #9617 ### Tested Head `c424bd17c8` (two commits, `83f28bd67f` and `c424bd17c8`), rebased onto `main` `f26434afa3`; `git merge-tree` against current `main` `38d463a6cf` is clean. The diff is 5 files, +148/-12: General.hpp +1, General.cpp +40, Open.cpp +3/-1, Stdio.cpp +15/-11, GuestFstatFault.cpp +89. - `guest_fstat_fault` and the six tests of the earlier description (`guest_file_timestamps`, `guest_kernel_sys`, `guest_filesystem`, `guest_posix_filesystem`, `guest_socket_descriptors`, `guest_socket_pair`), 7 tests, on `main` `f26434afa3` with this head: - Windows 10, MinGW-w64 GCC 15.2, Release: 7 of 7 pass. With `General.hpp`, `General.cpp`, `Open.cpp` and `Stdio.cpp` taken from `main` and the test kept, `guest_fstat_fault` fails with a SegFault. - Ubuntu 24.04 under WSL2 (a virtualised kernel, not bare-metal), GCC 13.3, Release: 7 of 7 pass. With only `General.cpp` put back to its first-commit version (same tree, same build directory, test kept), `guest_fstat_fault` aborts at the EPERM case. - Measured by others, on the earlier head `faa3926cdf` (before this rebase): jotitadev on Linux (Ubuntu 26.04, GCC 15.2, head `bd2c029ef` merged onto `main` `17f533556`), [review](https://github.com/boykopovar/AnyPS5/pull/9618#pullrequestreview-5609363219); jotitadev on macOS (Mac M2 Max, macOS 27.2, x86_64 under Rosetta), on the #8658 port branch `dc0c8db500` merged with `faa3926cdf`, not on `main`, [review](https://github.com/boykopovar/AnyPS5/pull/9618#pullrequestreview-5623727207). I have not rerun macOS. - The rebase changed only the test file: its local `extern "C"` declarations were dropped because `main` now declares them in the headers, and the `_fstatfs` call casts to `KernelStatfs*`. The source hunks are unchanged. - `check_conventions --base upstream/main` passes. - Not tested by me: macOS; a stat buffer in write-watched memory on macOS. ### Relation to other work #9423 adds `CopyGuestPath_nid_no_patch`, a checked read of guest path strings. `GuestCopyOut_nid_no_patch` here is the write counterpart for output structs; the two touch different functions and do not overlap, but both use the same host mechanism (`process_vm_*` on Linux, a `VirtualQuery` walk on Windows) and could share one primitive later. If you prefer the check inline in each wrapper instead of a shared helper, say so and I will fold it. Merge order with #9478 and #7447 (they share `Stdio.cpp`) is in the comments; this PR does not depend on them. ### 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 #9617 - [x] Unblocks: none - [x] GPUs tested: n/a - [x] AI-assisted: yes AI-assisted (Claude Code). 🤖 Generated with [Claude Code](https://claude.com/claude-code)
jotitadev commented 2026-10-22 04:44:21 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by jotitadev on 2026-10-22T04:44:21Z.

Review: COMMENTED


Linux run of the case listed as not tested: an output struct on a mapped page that is not writable.

Machine: Linux x86_64, i7-8700, Ubuntu 26.04 (kernel 7.0.0-38), GCC 15.2, Release, BUILD_TESTING=ON. Head bd2c029ef merged --no-commit onto main 17f533556 (clean).

  • The seven tests from the description pass: guest_fstat_fault, guest_file_timestamps, guest_kernel_sys, guest_filesystem, guest_posix_filesystem, guest_socket_descriptors and guest_socket_pair.

  • Read-only and inaccessible pages: I added a local probe to guest_fstat_fault (not pushed). It maps two anonymous pages filled with 0xa5, then mprotects the second one:

    output pointer result
    start of a PROT_READ page fstat -1/EFAULT, stat -1/EFAULT, sceKernelFstat 0x8002000e, _fstatfs -1. No crash.
    start of a PROT_NONE page fstat -1/EFAULT
    16 bytes before a PROT_READ page (sizeof(FileStat) is 120) fstat -1/EFAULT, and the 16 writable bytes are overwritten

    So process_vm_writev does reject a mapped page that isn't writable, as the description expects, and that case could be asserted the same way as the bad-address cases. The last row is a difference between hosts. On Linux, process_vm_writev writes up to the fault, so a struct that straddles into a non-writable page gets partly written before EFAULT. The Windows VirtualQuery walk checks the whole range before its memcpy, so it writes nothing. FreeBSD's copyout can also stop partway, so this may be acceptable, but the two hosts don't give the same result.

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

<!-- greyfinch-sync:boykopovar/AnyPS5:review:5609363219 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/9618#pullrequestreview-5609363219). Originally posted by [jotitadev](https://github.com/jotitadev) on 2026-10-22T04:44:21Z. **Review:** COMMENTED --- Linux run of the case listed as not tested: an output struct on a mapped page that is not writable. Machine: Linux x86_64, i7-8700, Ubuntu 26.04 (kernel 7.0.0-38), GCC 15.2, Release, `BUILD_TESTING=ON`. Head `bd2c029ef` merged `--no-commit` onto `main` `17f533556` (clean). - The seven tests from the description pass: `guest_fstat_fault`, `guest_file_timestamps`, `guest_kernel_sys`, `guest_filesystem`, `guest_posix_filesystem`, `guest_socket_descriptors` and `guest_socket_pair`. - Read-only and inaccessible pages: I added a local probe to `guest_fstat_fault` (not pushed). It maps two anonymous pages filled with `0xa5`, then `mprotect`s the second one: | output pointer | result | |---|---| | start of a `PROT_READ` page | `fstat` -1/`EFAULT`, `stat` -1/`EFAULT`, `sceKernelFstat` `0x8002000e`, `_fstatfs` -1. No crash. | | start of a `PROT_NONE` page | `fstat` -1/`EFAULT` | | 16 bytes before a `PROT_READ` page (`sizeof(FileStat)` is 120) | `fstat` -1/`EFAULT`, and the 16 writable bytes are overwritten | So `process_vm_writev` does reject a mapped page that isn't writable, as the description expects, and that case could be asserted the same way as the bad-address cases. The last row is a difference between hosts. On Linux, `process_vm_writev` writes up to the fault, so a struct that straddles into a non-writable page gets partly written before `EFAULT`. The Windows `VirtualQuery` walk checks the whole range before its `memcpy`, so it writes nothing. FreeBSD's `copyout` can also stop partway, so this may be acceptable, but the two hosts don't give the same result. Written with AI assistance (Claude Code). I ran the builds and tests above on my machine.
MarcZX10 commented 2026-10-22 09:27:21 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-10-22T09:27:21Z.

Review: CHANGES_REQUESTED


The main fix is well targeted: metadata is filled into local FileStat/KernelStatfs values, check precedence is retained, and the exact-head tests exercise all six stat-family wrappers with invalid pointers. Current-main conventions and whitespace pass; hosted conventions, eligibility, intake, labels, relinker, registration and ASan checks pass, while product Linux/Windows jobs are skipped. The author reports Windows and WSL red/green runs; an independent Linux reviewer confirms the same focused suite and additionally tested read-only and straddling pages. I did not rerun the full runtime tests locally.

Two safety gaps remain in GuestCopyOut_nid_no_patch: the non-Windows fallback can still dereference invalid output pointers, and Linux errors other than EFAULT can fall back to unchecked memcpy. Please add a checked copy-out path or fail safely, and cover read-only and partial-write behavior.

The linked #9617 is a generic invalid-pointer sweep, not a title-specific stop.

I’ve noticed that holding this PR because of #5239 isn’t necessary. It’s fine for PRs to keep coming in; they’ll be handled in due course. Please disregard my earlier request to hold or defer it.

<!-- greyfinch-sync:boykopovar/AnyPS5:review:5612045570 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/9618#pullrequestreview-5612045570). Originally posted by [MarcZX10](https://github.com/MarcZX10) on 2026-10-22T09:27:21Z. **Review:** CHANGES_REQUESTED --- The main fix is well targeted: metadata is filled into local `FileStat`/`KernelStatfs` values, check precedence is retained, and the exact-head tests exercise all six stat-family wrappers with invalid pointers. Current-main conventions and whitespace pass; hosted conventions, eligibility, intake, labels, relinker, registration and ASan checks pass, while product Linux/Windows jobs are skipped. The author reports Windows and WSL red/green runs; an independent Linux reviewer confirms the same focused suite and additionally tested read-only and straddling pages. I did not rerun the full runtime tests locally. Two safety gaps remain in `GuestCopyOut_nid_no_patch`: the non-Windows fallback can still dereference invalid output pointers, and Linux errors other than `EFAULT` can fall back to unchecked `memcpy`. Please add a checked copy-out path or fail safely, and cover read-only and partial-write behavior. The linked #9617 is a generic invalid-pointer sweep, not a title-specific stop. I’ve noticed that holding this PR because of #5239 isn’t necessary. It’s fine for PRs to keep coming in; they’ll be handled in due course. Please disregard my earlier request to hold or defer it.
EagleBearerYS commented 2026-10-22 11:23:22 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by EagleBearerYS on 2026-10-22T11:23:22Z.


@xXPBSXx a merge-order note: #9478 changes the same ResolvePathError_nid_no_patch call in lstat that this PR reworks. It passes the resolver's module flag, ResolvePathError_nid_no_patch(path, native, true), so lstat of an original .prx name finds its relinked .guest.prx. This PR fills a local FileStat and copies it out with GuestCopyOut_nid_no_patch. They compose as this PR's version of the function with the flag kept. Since #9478 is a one-line change, I'd propose it goes first and this PR keeps the flag when it rebases; if you'd rather this one went first, #9478 rebases over it instead. #7447 (qpavyros) sets the same call's archived flag too, so the combined call keeps both.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6339536139 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/9618#issuecomment-6339536139). Originally posted by [EagleBearerYS](https://github.com/EagleBearerYS) on 2026-10-22T11:23:22Z. --- @xXPBSXx a merge-order note: #9478 changes the same `ResolvePathError_nid_no_patch` call in `lstat` that this PR reworks. It passes the resolver's `module` flag, `ResolvePathError_nid_no_patch(path, native, true)`, so `lstat` of an original `.prx` name finds its relinked `.guest.prx`. This PR fills a local `FileStat` and copies it out with `GuestCopyOut_nid_no_patch`. They compose as this PR's version of the function with the flag kept. Since #9478 is a one-line change, I'd propose it goes first and this PR keeps the flag when it rebases; if you'd rather this one went first, #9478 rebases over it instead. #7447 (qpavyros) sets the same call's `archived` flag too, so the combined call keeps both.
MarcZX10 commented 2026-10-22 13:13:12 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-10-22T13:13:12Z.

Review: CHANGES_REQUESTED


I withdraw the earlier #5239 intake-hold objection. I rechecked exact head bd2c029ef713a8d1cbbb582dc5a621e39fa0d207 against current main 3ca07d086d5f44fbe3c00370e7f03953ab46ca1b: whitespace and conventions pass, the new regression source and all three changed implementation translation units pass local GCC 12.2 C++20 syntax-only compilation, and the current-main merge tree is clean. The author reports Windows and WSL red/green runs; an independent Linux reviewer confirms the seven focused tests, plus read-only and inaccessible-page probes. I did not run the runtime suite locally. Hosted relinker/register-context/write-watch checks pass; full product jobs are skipped. The exact-head safety issues from the prior review remain: 1. On macOS and other non-Windows/non-Linux hosts, GuestCopyOut_nid_no_patch falls through to memcpy. GuestArenaBeginHostWrite_nid_postfix returns true on non-Windows hosts, so this guard does not validate an arbitrary destination; a bad pointer can still crash the host. Please add a checked copy-out implementation for supported hosts or fail safely without dereferencing invalid guest memory. 2. On Linux, every process_vm_writev error other than EFAULT falls back to memcpy. Errors such as EPERM (for example, syscall filtering) and ENOSYS do not establish that the destination is writable. Please remove the unchecked fallback or restrict it to a condition that proves the full destination safe. 3. Add a read-only destination regression, as the independent reviewer already probed, and document/assert the partial-write behavior if a structure straddles into an inaccessible page. The branch base 17f533556272e5f6833ed730172f12308eee08d2 is behind current main; the repository requires keeping branches up to date. Please rebase/update and rerun the focused tests on that head. The overlap bot reports #9478's shared Stdio.cpp comparison is incomplete because it conflicts with main. The author's proposed merge order is reasonable, but preserve the ResolvePathError_nid_no_patch(path, native, true) module flag when combining it with #9478, and retain #7447's archived flag if applicable.

I’ve noticed that holding this PR because of #5239 isn’t necessary. It’s fine for PRs to keep coming in; they’ll be handled in due course. Please disregard my earlier request to hold or defer it.

<!-- greyfinch-sync:boykopovar/AnyPS5:review:5614770249 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/9618#pullrequestreview-5614770249). Originally posted by [MarcZX10](https://github.com/MarcZX10) on 2026-10-22T13:13:12Z. **Review:** CHANGES_REQUESTED --- I withdraw the earlier #5239 intake-hold objection. I rechecked exact head `bd2c029ef713a8d1cbbb582dc5a621e39fa0d207` against current main `3ca07d086d5f44fbe3c00370e7f03953ab46ca1b`: whitespace and conventions pass, the new regression source and all three changed implementation translation units pass local GCC 12.2 C++20 syntax-only compilation, and the current-main merge tree is clean. The author reports Windows and WSL red/green runs; an independent Linux reviewer confirms the seven focused tests, plus read-only and inaccessible-page probes. I did not run the runtime suite locally. Hosted relinker/register-context/write-watch checks pass; full product jobs are skipped. The exact-head safety issues from the prior review remain: 1. On macOS and other non-Windows/non-Linux hosts, `GuestCopyOut_nid_no_patch` falls through to `memcpy`. `GuestArenaBeginHostWrite_nid_postfix` returns true on non-Windows hosts, so this guard does not validate an arbitrary destination; a bad pointer can still crash the host. Please add a checked copy-out implementation for supported hosts or fail safely without dereferencing invalid guest memory. 2. On Linux, every `process_vm_writev` error other than `EFAULT` falls back to `memcpy`. Errors such as `EPERM` (for example, syscall filtering) and `ENOSYS` do not establish that the destination is writable. Please remove the unchecked fallback or restrict it to a condition that proves the full destination safe. 3. Add a read-only destination regression, as the independent reviewer already probed, and document/assert the partial-write behavior if a structure straddles into an inaccessible page. The branch base `17f533556272e5f6833ed730172f12308eee08d2` is behind current main; the repository requires keeping branches up to date. Please rebase/update and rerun the focused tests on that head. The overlap bot reports #9478's shared `Stdio.cpp` comparison is incomplete because it conflicts with main. The author's proposed merge order is reasonable, but preserve the `ResolvePathError_nid_no_patch(path, native, true)` module flag when combining it with #9478, and retain #7447's archived flag if applicable. I’ve noticed that holding this PR because of #5239 isn’t necessary. It’s fine for PRs to keep coming in; they’ll be handled in due course. Please disregard my earlier request to hold or defer it.
adriadam10 commented 2026-10-22 18:56:40 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by adriadam10 on 2026-10-22T18:56:40Z.

Review: CHANGES_REQUESTED


Thanks, the approach is right: local struct, copy-out last, error order unchanged as in sys_fstat/sys_stat/sys_fstatfs. Two things still defeat the purpose of the helper (same as @MarcZX10's review):

  • core/libs/prx/libc/src/General.cpp:706-709: any process_vm_writev error other than EFAULT (EPERM under seccomp, ENOSYS) falls back to an unchecked memcpy, which crashes on a bad pointer. Return false or throw on those instead (#9161 throws on host failures other than EFAULT).
  • General.cpp:712-714: on macOS and other hosts the helper is a plain memcpy after HostWrite, whose Open() is true for any non-arena pointer on non-Windows hosts. Either add a checked path for those hosts (e.g. mach_vm_write/vm_region on macOS) or NotImplemented_nid_no_patch there so it does not dereference a bad pointer silently.
  • GuestFstatFault.cpp: add a read-only page case (mmap two pages, mprotect the second PROT_READ, expect -1/EFAULT at its start), as @jotitadev probed. Also note in the PR which behaviour you want when the struct straddles into a non-writable page: Linux writes up to the fault, the Windows walk writes nothing. Pick one and assert it, or state that it is unspecified like copyout.
  • Merge order with #9478: keep the module flag in lstat's ResolvePathError_nid_no_patch(path, native, true) when you rebase over it.

Please also rebase onto current main and rerun the focused tests.

<!-- greyfinch-sync:boykopovar/AnyPS5:review:5619356014 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/9618#pullrequestreview-5619356014). Originally posted by [adriadam10](https://github.com/adriadam10) on 2026-10-22T18:56:40Z. **Review:** CHANGES_REQUESTED --- Thanks, the approach is right: local struct, copy-out last, error order unchanged as in `sys_fstat`/`sys_stat`/`sys_fstatfs`. Two things still defeat the purpose of the helper (same as @MarcZX10's review): - `core/libs/prx/libc/src/General.cpp:706-709`: any `process_vm_writev` error other than `EFAULT` (`EPERM` under seccomp, `ENOSYS`) falls back to an unchecked `memcpy`, which crashes on a bad pointer. Return false or throw on those instead (#9161 throws on host failures other than `EFAULT`). - `General.cpp:712-714`: on macOS and other hosts the helper is a plain `memcpy` after `HostWrite`, whose `Open()` is true for any non-arena pointer on non-Windows hosts. Either add a checked path for those hosts (e.g. `mach_vm_write`/`vm_region` on macOS) or `NotImplemented_nid_no_patch` there so it does not dereference a bad pointer silently. - `GuestFstatFault.cpp`: add a read-only page case (mmap two pages, `mprotect` the second `PROT_READ`, expect -1/`EFAULT` at its start), as @jotitadev probed. Also note in the PR which behaviour you want when the struct straddles into a non-writable page: Linux writes up to the fault, the Windows walk writes nothing. Pick one and assert it, or state that it is unspecified like `copyout`. - Merge order with #9478: keep the `module` flag in `lstat`'s `ResolvePathError_nid_no_patch(path, native, true)` when you rebase over it. Please also rebase onto current `main` and rerun the focused tests.
MarcZX10 commented 2026-10-22 23:43:48 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-10-22T23:43:48Z.

Review: CHANGES_REQUESTED


Blocking correctness issue: GuestCopyOut_nid_no_patch can still fault instead of returning EFAULT on non-Windows hosts. On Linux, when process_vm_writev fails with anything other than EFAULT (for example EPERM under seccomp or ENOSYS), lines 706–709 fall back to std::memcpy; the non-Linux/non-Windows branch at lines 712–714 always does so. GuestArenaBeginHostWrite_nid_postfix does not validate writability on these platforms (it calls the write-watch hook and returns true), so an invalid or read-only destination can still crash in those branches. Please use a copy-out path that safely detects these failures, or fail conservatively when the safe syscall is unavailable, and add regression coverage for a non-EFAULT syscall failure and the macOS path before considering this fixed.

<!-- greyfinch-sync:boykopovar/AnyPS5:review:5622259946 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/9618#pullrequestreview-5622259946). Originally posted by [MarcZX10](https://github.com/MarcZX10) on 2026-10-22T23:43:48Z. **Review:** CHANGES_REQUESTED --- Blocking correctness issue: `GuestCopyOut_nid_no_patch` can still fault instead of returning `EFAULT` on non-Windows hosts. On Linux, when `process_vm_writev` fails with anything other than `EFAULT` (for example `EPERM` under seccomp or `ENOSYS`), lines 706–709 fall back to `std::memcpy`; the non-Linux/non-Windows branch at lines 712–714 always does so. `GuestArenaBeginHostWrite_nid_postfix` does not validate writability on these platforms (it calls the write-watch hook and returns true), so an invalid or read-only destination can still crash in those branches. Please use a copy-out path that safely detects these failures, or fail conservatively when the safe syscall is unavailable, and add regression coverage for a non-`EFAULT` syscall failure and the macOS path before considering this fixed.
jotitadev commented 2026-10-23 04:00:41 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by jotitadev on 2026-10-23T04:00:41Z.

Review: COMMENTED


macOS run of the new mach_vm_write branch (listed as not tested).

Mac M2 Max, macOS 27.2, Apple clang, x86_64 under Rosetta, Release. main does not build libc/libkernel on macOS yet, so the tree is #8658 (feat/macos-port) @ dc0c8db500 merged with this PR @ faa3926cdf (clean merge).

  • guest_fstat_fault, guest_file_timestamps, guest_kernel_sys, guest_filesystem, guest_posix_filesystem, guest_socket_descriptors and guest_socket_pair: 7/7 pass. guest_fstat_fault 3/3 runs.
  • Red/green: with GuestCopyOut_nid_no_patch put back to the bd2c029ef version (memcpy on non-Linux, non-Windows hosts), guest_fstat_fault fails with a SegFault on macOS.
  • mach_vm_write on this host (standalone check, arm64 and Rosetta give the same results): addresses 1 and ~0xfff, a PROT_READ page and a PROT_NONE page all fail. An unaligned stack buffer and a write across two untouched pages succeed. A write that crosses from a writable page into a read-only one fails but leaves the bytes before the boundary written. process_vm_writev behaves the same way.

The HostWrite guard before mach_vm_write is required on macOS, and no test covers it. On macOS, write watch protects watched pages with mprotect (Linux uses userfaultfd async WP instead). mach_vm_write does not raise the fault the watch handler relies on; it returns KERN_PROTECTION_FAILURE. In a local check I registered a page with GuestWriteWatchRegister_nid_postfix and armed it with one Collect:

  • raw mach_vm_write into the page: kr=1, and no write is collected;
  • fstat and sceKernelFstat into the page through this PR: 0, st_size correct, and the page is collected as written;
  • the same with the guard limited to _WIN32: fstat returns -1/EFAULT and sceKernelFstat returns 0x8002000E, yet guest_fstat_fault still passes.

So a stat buffer in watched (imported GPU) memory would get a spurious EFAULT on macOS if the guard were ever moved or dropped. A small case in guest_fstat_fault would catch that on macOS: register and arm a page, fstat into it, and expect success plus one collected write. It is optional and does not block this PR.

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

<!-- greyfinch-sync:boykopovar/AnyPS5:review:5623727207 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/9618#pullrequestreview-5623727207). Originally posted by [jotitadev](https://github.com/jotitadev) on 2026-10-23T04:00:41Z. **Review:** COMMENTED --- macOS run of the new `mach_vm_write` branch (listed as not tested). Mac M2 Max, macOS 27.2, Apple clang, x86_64 under Rosetta, Release. `main` does not build libc/libkernel on macOS yet, so the tree is #8658 (`feat/macos-port`) @ dc0c8db500 merged with this PR @ faa3926cdf (clean merge). - `guest_fstat_fault`, `guest_file_timestamps`, `guest_kernel_sys`, `guest_filesystem`, `guest_posix_filesystem`, `guest_socket_descriptors` and `guest_socket_pair`: 7/7 pass. `guest_fstat_fault` 3/3 runs. - Red/green: with `GuestCopyOut_nid_no_patch` put back to the bd2c029ef version (memcpy on non-Linux, non-Windows hosts), `guest_fstat_fault` fails with a SegFault on macOS. - `mach_vm_write` on this host (standalone check, arm64 and Rosetta give the same results): addresses `1` and `~0xfff`, a `PROT_READ` page and a `PROT_NONE` page all fail. An unaligned stack buffer and a write across two untouched pages succeed. A write that crosses from a writable page into a read-only one fails but leaves the bytes before the boundary written. `process_vm_writev` behaves the same way. The `HostWrite` guard before `mach_vm_write` is required on macOS, and no test covers it. On macOS, write watch protects watched pages with `mprotect` (Linux uses userfaultfd async WP instead). `mach_vm_write` does not raise the fault the watch handler relies on; it returns `KERN_PROTECTION_FAILURE`. In a local check I registered a page with `GuestWriteWatchRegister_nid_postfix` and armed it with one `Collect`: - raw `mach_vm_write` into the page: `kr=1`, and no write is collected; - `fstat` and `sceKernelFstat` into the page through this PR: `0`, `st_size` correct, and the page is collected as written; - the same with the guard limited to `_WIN32`: `fstat` returns -1/EFAULT and `sceKernelFstat` returns `0x8002000E`, yet `guest_fstat_fault` still passes. So a stat buffer in watched (imported GPU) memory would get a spurious EFAULT on macOS if the guard were ever moved or dropped. A small case in `guest_fstat_fault` would catch that on macOS: register and arm a page, `fstat` into it, and expect success plus one collected write. It is optional and does not block this PR. Written with AI assistance (Claude Code). I ran the builds and tests above on my machine.
MarcZX10 commented 2026-10-23 20:24:20 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-10-23T20:24:20Z.

Review: DISMISSED


The follow-up addresses the safety blockers from my earlier review: non-EFAULT Linux errors no longer fall through to memcpy, macOS uses mach_vm_write, and the test now covers read-only/PAGE_NOACCESS or PROT_NONE destinations plus Linux seccomp EPERM. The independent macOS report also confirms the focused regression passes there.

Two repository-checklist items still need updating before I can approve:

  1. The PR body still describes the first commit (bd2c029ef7) as the tested change and says macOS/read-only destinations were not tested. The follow-up at current head faa3926cdf5473ae5d3bf1e39b09a1511f2ce37a changes both the implementation and tests. Please refresh Tested with the follow-up results and their exact head/base, including that the macOS validation used the #8658 port branch rather than current main.
  2. The overlap discussion on this PR records a merge conflict with open #9478 and #7447 in Stdio.cpp and proposes preserving both resolver flags, but the checklist still says Depends on: none. Please agree the merge order with those authors and record the actual dependency/order in the checklist. Preserve module=true from #9478 and archived=true from #7447 when composing ResolvePathError_nid_no_patch.

I verified this PR alone merges cleanly with current upstream/main; the requested coordination is for the reported combined conflicts. AI-assisted review.

<!-- greyfinch-sync:boykopovar/AnyPS5:review:5634026725 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/9618#pullrequestreview-5634026725). Originally posted by [MarcZX10](https://github.com/MarcZX10) on 2026-10-23T20:24:20Z. **Review:** DISMISSED --- The follow-up addresses the safety blockers from my earlier review: non-EFAULT Linux errors no longer fall through to `memcpy`, macOS uses `mach_vm_write`, and the test now covers read-only/PAGE_NOACCESS or PROT_NONE destinations plus Linux seccomp EPERM. The independent macOS report also confirms the focused regression passes there. Two repository-checklist items still need updating before I can approve: 1. The PR body still describes the first commit (`bd2c029ef7`) as the tested change and says macOS/read-only destinations were not tested. The follow-up at current head `faa3926cdf5473ae5d3bf1e39b09a1511f2ce37a` changes both the implementation and tests. Please refresh **Tested** with the follow-up results and their exact head/base, including that the macOS validation used the #8658 port branch rather than current `main`. 2. The overlap discussion on this PR records a merge conflict with open #9478 and #7447 in `Stdio.cpp` and proposes preserving both resolver flags, but the checklist still says `Depends on: none`. Please agree the merge order with those authors and record the actual dependency/order in the checklist. Preserve `module=true` from #9478 and `archived=true` from #7447 when composing `ResolvePathError_nid_no_patch`. I verified this PR alone merges cleanly with current `upstream/main`; the requested coordination is for the reported combined conflicts. AI-assisted review.
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!9618
No description provided.