fix(libc): keep errno across the host read and write hooks of checked guest copies #21243

Merged
milescoviello merged 1 commit from upstream-pr/21243 into main 2026-11-16 05:25:47 +00:00
milescoviello commented 2026-11-16 01:24:57 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by milescoviello on 2026-11-16T01:24:57Z.


What

GuestCopyIn_nid_no_patch, GuestCopyInUnsynced_nid_no_patch and GuestCopyOut_nid_no_patch keep errno (dfb9b774fc, "keep errno when a checked guest copy faults on Linux"). They didn't, however, keep it across the hooks they run before the copy:

  • CopyGuestBytes calls GuestArenaPrepareHostRead_nid_no_patch, which runs the host read hook, before it saves errno.
  • GuestCopyOut_nid_no_patch opens a GuestArena::HostWrite, which runs the host write hook, before its save. That bracket also closes, through GuestArenaEndHostWrite_nid_postfix, after the restore.

libSceAgcDriver installs both hooks (GuestMemory.cpp PrepareHostRead, and Lifecycle.cpp with GuestMemory::PrepareHostWrite). They run driver code that waits on and flushes GPU work, which may change errno. When a hook does, the change reaches the guest through every export that reports a checked-copy failure as a return code. #20829 works around this per function with an errno guard in each export.

This keeps errno once, for the whole copy. CopyGuestBytes and GuestCopyOut_nid_no_patch now hold a RestoreErrno (GuestWritable_nid_no_patch's local struct, moved to the file's anonymous namespace so all three use it) from before the hooks run until after the write bracket closes. The existing saves around process_vm_readv/process_vm_writev stay; they are now redundant but harmless, and leaving them keeps this from touching lines other open PRs change (#21156 edits next to them).

Tested

Linux (Ubuntu 26.04.1, kernel 7.0), GCC 15.2, Ninja, Release, -DBUILD_TESTING=ON:

  • New guest_copy_hook_errno installs host read and write hooks that set errno to 99 and count their calls. It then checks that GuestCopyIn_nid_no_patch and GuestCopyOut_nid_no_patch keep errno at 71 for a good copy and for an unmapped 0x10, both ways, and that each hook ran twice. Passes.
  • Negative control: with main's General.cpp and this test, guest_copy_hook_errno stops at line 37: GuestCopyIn_nid_no_patch leaves the read hook's 99 in errno.
  • guest_output_pointer_fault (unmapped and kernel-space addresses keep errno, plus the seccomp case that must throw): passes.

Windows 11 (VM, 4 vCPU, WinLibs GCC 15.2, Release): guest_copy_hook_errno and guest_output_pointer_fault pass, 3 of 3 runs.

tools/check_conventions.py passes on the branch and this body. A merge check against the open PRs that change General.cpp (#21156, #20832, #20678, #20812, #20813, #18112, #11446, #9371, #20315, #15973) finds no new conflicts: each has the same conflicts with this branch as with main.

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 and the licence notice of code adapted from upstream (see AGENTS.md)
  • 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:5329413112 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/21243). Originally posted by [milescoviello](https://github.com/milescoviello) on 2026-11-16T01:24:57Z. --- ### What `GuestCopyIn_nid_no_patch`, `GuestCopyInUnsynced_nid_no_patch` and `GuestCopyOut_nid_no_patch` keep `errno` (dfb9b774fc, "keep errno when a checked guest copy faults on Linux"). They didn't, however, keep it across the hooks they run before the copy: - `CopyGuestBytes` calls `GuestArenaPrepareHostRead_nid_no_patch`, which runs the host read hook, before it saves `errno`. - `GuestCopyOut_nid_no_patch` opens a `GuestArena::HostWrite`, which runs the host write hook, before its save. That bracket also closes, through `GuestArenaEndHostWrite_nid_postfix`, after the restore. libSceAgcDriver installs both hooks (`GuestMemory.cpp` `PrepareHostRead`, and `Lifecycle.cpp` with `GuestMemory::PrepareHostWrite`). They run driver code that waits on and flushes GPU work, which may change `errno`. When a hook does, the change reaches the guest through every export that reports a checked-copy failure as a return code. #20829 works around this per function with an `errno` guard in each export. This keeps `errno` once, for the whole copy. `CopyGuestBytes` and `GuestCopyOut_nid_no_patch` now hold a `RestoreErrno` (`GuestWritable_nid_no_patch`'s local struct, moved to the file's anonymous namespace so all three use it) from before the hooks run until after the write bracket closes. The existing saves around `process_vm_readv`/`process_vm_writev` stay; they are now redundant but harmless, and leaving them keeps this from touching lines other open PRs change (#21156 edits next to them). ### Tested Linux (Ubuntu 26.04.1, kernel 7.0), GCC 15.2, Ninja, Release, `-DBUILD_TESTING=ON`: - New `guest_copy_hook_errno` installs host read and write hooks that set `errno` to 99 and count their calls. It then checks that `GuestCopyIn_nid_no_patch` and `GuestCopyOut_nid_no_patch` keep `errno` at 71 for a good copy and for an unmapped `0x10`, both ways, and that each hook ran twice. Passes. - Negative control: with `main`'s `General.cpp` and this test, `guest_copy_hook_errno` stops at line 37: `GuestCopyIn_nid_no_patch` leaves the read hook's 99 in `errno`. - `guest_output_pointer_fault` (unmapped and kernel-space addresses keep `errno`, plus the seccomp case that must throw): passes. Windows 11 (VM, 4 vCPU, WinLibs GCC 15.2, Release): `guest_copy_hook_errno` and `guest_output_pointer_fault` pass, 3 of 3 runs. `tools/check_conventions.py` passes on the branch and this body. A merge check against the open PRs that change `General.cpp` (#21156, #20832, #20678, #20812, #20813, #18112, #11446, #9371, #20315, #15973) finds no new conflicts: each has the same conflicts with this branch as with `main`. ### 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) and the licence notice of code adapted from upstream (see AGENTS.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
PrivacyOnion commented 2026-11-16 01:33:37 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by PrivacyOnion on 2026-11-16T01:33:37Z.

Review: APPROVED


Verified on Linux (Ubuntu 24.04, GCC 13.3, Ninja, Release) against ea637fef4e:

  • Conventions: tools/check_conventions.py passed; git diff --check clean.
  • Build: guest_copy_hook_errno_tests built cleanly.
  • CTest (guest_copy_hook_errno):
    1/1 Test #1336: guest_copy_hook_errno ............   Passed    0.01 sec
    100% tests passed, 0 tests failed out of 1
    
  • Negative control: with core/libs/prx/libc/src/General.cpp reverted to main and the new tests kept, the suite fails:
    1/1 Test #1336: guest_copy_hook_errno ............Subprocess aborted***Exception:   0.47 sec
    0% tests passed, 1 tests failed out of 1
    
    Restoring the change makes it pass again.
  • Code reading:
    • Verified RestoreErrno in core/libs/prx/libc/src/General.cpp (lines 88-91) captures errno on construction and restores it on destruction across all return and exception paths.
    • Verified CopyGuestBytes in core/libs/prx/libc/src/General.cpp (line 94) constructs RestoreErrno before GuestArenaPrepareHostRead_nid_no_patch(source, bytes), preserving errno across the host read hook on Linux, Windows, and macOS.
    • Verified GuestWritable_nid_no_patch in core/libs/prx/libc/src/General.cpp (line 834) uses the anonymous-namespace RestoreErrno with identical RAII semantics as before.
    • Verified GuestCopyOut_nid_no_patch in core/libs/prx/libc/src/General.cpp (lines 871-873) declares RestoreErrno before GuestArena::HostWrite guard(destination, bytes), ensuring destruction order closes guard (and GuestArenaEndHostWrite_nid_postfix) before restore restores errno.
    • Verified core/libs/tests/GuestCopyHookErrno.cpp installs both read and write hooks that clobber errno to 99, tests both valid and unmapped (0x10) pointers for GuestCopyIn_nid_no_patch and GuestCopyOut_nid_no_patch, verifies errno == 71 and hook call counts (g_reads == 2 && g_writes == 2), and resets both hooks to nullptr before exit.

Looks Good To Me

<!-- greyfinch-sync:boykopovar/AnyPS5:review:5914085872 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/21243#pullrequestreview-5914085872). Originally posted by [PrivacyOnion](https://github.com/PrivacyOnion) on 2026-11-16T01:33:37Z. **Review:** APPROVED --- Verified on Linux (Ubuntu 24.04, GCC 13.3, Ninja, Release) against `ea637fef4e`: - **Conventions**: `tools/check_conventions.py` passed; `git diff --check` clean. - **Build**: `guest_copy_hook_errno_tests` built cleanly. - **CTest** (`guest_copy_hook_errno`): ``` 1/1 Test #1336: guest_copy_hook_errno ............ Passed 0.01 sec 100% tests passed, 0 tests failed out of 1 ``` - **Negative control**: with `core/libs/prx/libc/src/General.cpp` reverted to `main` and the new tests kept, the suite fails: ``` 1/1 Test #1336: guest_copy_hook_errno ............Subprocess aborted***Exception: 0.47 sec 0% tests passed, 1 tests failed out of 1 ``` Restoring the change makes it pass again. - **Code reading**: - Verified `RestoreErrno` in `core/libs/prx/libc/src/General.cpp` (lines 88-91) captures `errno` on construction and restores it on destruction across all return and exception paths. - Verified `CopyGuestBytes` in `core/libs/prx/libc/src/General.cpp` (line 94) constructs `RestoreErrno` before `GuestArenaPrepareHostRead_nid_no_patch(source, bytes)`, preserving `errno` across the host read hook on Linux, Windows, and macOS. - Verified `GuestWritable_nid_no_patch` in `core/libs/prx/libc/src/General.cpp` (line 834) uses the anonymous-namespace `RestoreErrno` with identical RAII semantics as before. - Verified `GuestCopyOut_nid_no_patch` in `core/libs/prx/libc/src/General.cpp` (lines 871-873) declares `RestoreErrno` before `GuestArena::HostWrite guard(destination, bytes)`, ensuring destruction order closes `guard` (and `GuestArenaEndHostWrite_nid_postfix`) before `restore` restores `errno`. - Verified `core/libs/tests/GuestCopyHookErrno.cpp` installs both read and write hooks that clobber `errno` to `99`, tests both valid and unmapped (`0x10`) pointers for `GuestCopyIn_nid_no_patch` and `GuestCopyOut_nid_no_patch`, verifies `errno == 71` and hook call counts (`g_reads == 2 && g_writes == 2`), and resets both hooks to `nullptr` before exit. Looks Good To Me
MarcZX10 commented 2026-11-16 02:14:30 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-11-16T02:14:30Z.

Review: COMMENTED


Reviewed — no blocking findings

The errno guards cover the host hooks and the write bracket, and the added test exercises successful and faulting copies; I found no verified blocking defect.

What I ran

Inspected head ea637fef4ecd331766938ebb6b7399932a55ea20 using repo_read.py. No build or tests were run in this review; test results in the PR description are author-reported.

Results

Check Result Note
Diff and changed code passed Three changed files inspected
Tests not run Author reports Linux and Windows passes
Negative control not run Author reports the test failed against the prior implementation

Findings

Blocking

None identified in the inspected code.

Non-blocking

None.

Sources

  • core/libs/prx/libc/src/General.cpp: RestoreErrno spans the read hook and copy, and the output-copy guard spans HostWrite destruction.
  • core/libs/tests/GuestCopyHookErrno.cpp: exercises errno preservation for successful and unmapped input and output copies.
  • core/libs/prx/libc/tests/cmake/guest_copy_hook_errno.cmake: registers the new test.

Not verified

Did not run tests, build, or independently verify the author-reported environment and results. Read the changed General.cpp ranges around CopyGuestBytes, GuestWritable_nid_no_patch, and the guest copy functions; read both added test files in full. Also read GuestArena.hpp and General.hpp for relevant declarations. No changed files were excluded from the review folder listing; the remainder of General.cpp was not read.

To get this to a good state

Nothing is required. Optional: run the reported checks on this head in CI or locally.

<!-- greyfinch-sync:boykopovar/AnyPS5:review:5914301489 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/21243#pullrequestreview-5914301489). Originally posted by [MarcZX10](https://github.com/MarcZX10) on 2026-11-16T02:14:30Z. **Review:** COMMENTED --- # Reviewed — no blocking findings The errno guards cover the host hooks and the write bracket, and the added test exercises successful and faulting copies; I found no verified blocking defect. ## What I ran Inspected head `ea637fef4ecd331766938ebb6b7399932a55ea20` using `repo_read.py`. No build or tests were run in this review; test results in the PR description are author-reported. ## Results | Check | Result | Note | |---|---|---| | Diff and changed code | passed | Three changed files inspected | | Tests | not run | Author reports Linux and Windows passes | | Negative control | not run | Author reports the test failed against the prior implementation | ## Findings ### Blocking None identified in the inspected code. ### Non-blocking None. ## Sources - `core/libs/prx/libc/src/General.cpp`: `RestoreErrno` spans the read hook and copy, and the output-copy guard spans `HostWrite` destruction. - `core/libs/tests/GuestCopyHookErrno.cpp`: exercises errno preservation for successful and unmapped input and output copies. - `core/libs/prx/libc/tests/cmake/guest_copy_hook_errno.cmake`: registers the new test. ## Not verified Did not run tests, build, or independently verify the author-reported environment and results. Read the changed `General.cpp` ranges around `CopyGuestBytes`, `GuestWritable_nid_no_patch`, and the guest copy functions; read both added test files in full. Also read `GuestArena.hpp` and `General.hpp` for relevant declarations. No changed files were excluded from the review folder listing; the remainder of `General.cpp` was not read. ## To get this to a good state Nothing is required. Optional: run the reported checks on this head in CI or 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!21243
No description provided.