fix(libc): preserve errno in GuestPtr copies #21639

Closed
Ethan-Hainline wants to merge 1 commit from upstream-pr/21639 into main
Ethan-Hainline commented 2026-11-16 13:56:10 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by Ethan-Hainline on 2026-11-16T13:56:10Z.


What

GuestPtr and GuestBytes call the guest copy helpers without preserving errno, unlike the hand-written conversions elsewhere in the tree, which save and restore it around the same helpers.

On Linux the helper's own save/restore is not enough: CopyGuestBytes takes its saved errno after GuestArenaPrepareHostRead, so a copy can return with the post-prepare value instead of the caller's. On Windows ReadProcessMemory does not touch errno, but the prepare step still can.

This wraps every GuestCopyIn_nid_no_patch / GuestCopyOut_nid_no_patch call in GuestPtr and GuestBytes with a save and restore, including the array and byte forms. The zero-length and null-address paths never call a helper, so they leave errno alone.

The libSceVideoOut conversion in #19827 converts several exports to these types, so this makes its failure paths match the errno-preserving copies main added for sceVideoOutGetEventData and sceVideoOutInitializeOutputOptions.

Tested

Windows 11 Pro, Ryzen 5 7530U, MinGW-w64 GCC 16.2.0 UCRT.

  • guest_ptr_tests builds and the new CheckErrno covers a failed and a successful value, array and byte copy, each with errno = 71 before the call and an unchanged check after. The test compiles; I could not run the freshly linked executable locally because Smart App Control blocks new unsigned binaries on this machine, so CI is the run.
  • python tools/check_conventions.py --base upstream/main --head HEAD passes.

Checklist

  • Based on current main; no other open PR implements the same change
  • 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:5338310680 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/21639). Originally posted by [Ethan-Hainline](https://github.com/Ethan-Hainline) on 2026-11-16T13:56:10Z. --- ### What `GuestPtr` and `GuestBytes` call the guest copy helpers without preserving `errno`, unlike the hand-written conversions elsewhere in the tree, which save and restore it around the same helpers. On Linux the helper's own save/restore is not enough: `CopyGuestBytes` takes its saved `errno` **after** `GuestArenaPrepareHostRead`, so a copy can return with the post-prepare value instead of the caller's. On Windows `ReadProcessMemory` does not touch `errno`, but the prepare step still can. This wraps every `GuestCopyIn_nid_no_patch` / `GuestCopyOut_nid_no_patch` call in `GuestPtr` and `GuestBytes` with a save and restore, including the array and byte forms. The zero-length and null-address paths never call a helper, so they leave `errno` alone. The `libSceVideoOut` conversion in #19827 converts several exports to these types, so this makes its failure paths match the errno-preserving copies main added for `sceVideoOutGetEventData` and `sceVideoOutInitializeOutputOptions`. ### Tested Windows 11 Pro, Ryzen 5 7530U, MinGW-w64 GCC 16.2.0 UCRT. - `guest_ptr_tests` builds and the new `CheckErrno` covers a failed and a successful value, array and byte copy, each with `errno = 71` before the call and an unchanged check after. The test compiles; I could not run the freshly linked executable locally because Smart App Control blocks new unsigned binaries on this machine, so CI is the run. - `python tools/check_conventions.py --base upstream/main --head HEAD` passes. ### Checklist - [x] Based on current `main`; no other open PR implements the same change - [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
PrivacyOnion commented 2026-11-16 14:02:05 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by PrivacyOnion on 2026-11-16T14:02:05Z.

Review: CHANGES_REQUESTED


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

  • Conventions: tools/check_conventions.py passed; git diff --check clean.
  • Build: guest_ptr_tests built cleanly.
  • CTest (guest_ptr):
    1/1 Test #5397: guest_ptr ........................   Passed    0.01 sec
    100% tests passed, 0 tests failed out of 1
    
  • Negative control: the tests still pass with the change reverted, so they do not cover it

Requesting changes for the points above.

<!-- greyfinch-sync:boykopovar/AnyPS5:review:5921237130 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/21639#pullrequestreview-5921237130). Originally posted by [PrivacyOnion](https://github.com/PrivacyOnion) on 2026-11-16T14:02:05Z. **Review:** CHANGES_REQUESTED --- Verified on Linux (Ubuntu 24.04, GCC 13.3, Ninja, Release) against `22310b01b4`: - **Conventions**: `tools/check_conventions.py` passed; `git diff --check` clean. - **Build**: `guest_ptr_tests` built cleanly. - **CTest** (`guest_ptr`): ``` 1/1 Test #5397: guest_ptr ........................ Passed 0.01 sec 100% tests passed, 0 tests failed out of 1 ``` - **Negative control**: the tests still pass with the change reverted, so they do not cover it Requesting changes for the points above.

Pull request closed

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!21639
No description provided.