fix(libs): allow socket receives into watched guest memory #4272

Merged
Ondra97 merged 2 commits from upstream-pr/4272 into main 2026-10-12 12:22:17 +00:00
Ondra97 commented 2026-10-12 03:42:56 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by Ondra97 on 2026-10-12T03:42:56Z.


What

Socket receives into CPU-writable guest direct memory fail after Windows write tracking protects the destination pages. libkernel reports EFAULT, while libSceNet translates the native WSAEFAULT into EIO.

Bracket native recv and recvfrom with the existing GuestArena::HostWrite guard in both libraries. The destination stays writable throughout a blocking call and remains visible to write tracking when the call finishes. A genuinely read-only destination returns EFAULT without consuming queued data. Zero-byte calls use an empty guard range so they keep their existing behavior. Socket read and sceKernelRead inherit the fix through GuestSockets::Read.

Add Windows integration tests for all six APIs over UDP and TCP, and update the write-watch debt entry. Related PR #4092 changes timeout error translation in the same SceNet functions; its patch applies cleanly alongside this fix and neither change depends on the other.

Tested

Windows x86-64, Release, supported WinLibs GCC 15.2.0 UCRT/POSIX/SEH r7; standalone tests through the real libkernel, libc and libSceNet APIs.

  • The six initial UDP regression variants failed before the fix with EFAULT/EIO.
  • Full build with BUILD_TESTING=ON, including the patched libraries, completed successfully.
  • Full CTest suite: 930 passed, 5 expected skips, 0 failures across 935 registered tests. The Windows skip checker accepts all five skips; the longest test took 16.11 seconds.
  • All 12 new UDP/TCP tests passed ten consecutive runs each. Coverage includes page-boundary writes, truncation, MSG_PEEK, zero-byte operations, errno preservation, EAGAIN, read-only destinations, dirty tracking, and collection while a receive is blocked.
  • Test registration: 684 executables, none missing from CTest.
  • Conventions checker passed for the commit, PR title and completed template.

Linux/macOS runtime and a PS5 title were not run locally.

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 in TechnicalDebt
  • 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
  • AI-assisted: yes
<!-- greyfinch-sync:boykopovar/AnyPS5:issue:4824650271 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/4272). Originally posted by [Ondra97](https://github.com/Ondra97) on 2026-10-12T03:42:56Z. --- ### What Socket receives into CPU-writable guest direct memory fail after Windows write tracking protects the destination pages. libkernel reports `EFAULT`, while libSceNet translates the native `WSAEFAULT` into `EIO`. Bracket native `recv` and `recvfrom` with the existing `GuestArena::HostWrite` guard in both libraries. The destination stays writable throughout a blocking call and remains visible to write tracking when the call finishes. A genuinely read-only destination returns `EFAULT` without consuming queued data. Zero-byte calls use an empty guard range so they keep their existing behavior. Socket `read` and `sceKernelRead` inherit the fix through `GuestSockets::Read`. Add Windows integration tests for all six APIs over UDP and TCP, and update the write-watch debt entry. Related PR #4092 changes timeout error translation in the same SceNet functions; its patch applies cleanly alongside this fix and neither change depends on the other. ### Tested Windows x86-64, Release, supported WinLibs GCC 15.2.0 UCRT/POSIX/SEH r7; standalone tests through the real libkernel, libc and libSceNet APIs. - The six initial UDP regression variants failed before the fix with `EFAULT`/`EIO`. - Full build with `BUILD_TESTING=ON`, including the patched libraries, completed successfully. - Full CTest suite: 930 passed, 5 expected skips, 0 failures across 935 registered tests. The Windows skip checker accepts all five skips; the longest test took 16.11 seconds. - All 12 new UDP/TCP tests passed ten consecutive runs each. Coverage includes page-boundary writes, truncation, `MSG_PEEK`, zero-byte operations, errno preservation, `EAGAIN`, read-only destinations, dirty tracking, and collection while a receive is blocked. - Test registration: 684 executables, none missing from CTest. - Conventions checker passed for the commit, PR title and completed template. Linux/macOS runtime and a PS5 title were not run locally. ### 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 in [TechnicalDebt](https://github.com/boykopovar/AnyPS5/blob/main/docs/dev/TechnicalDebt.md#silent-stubs) - [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] AI-assisted: yes
MarcZX10 commented 2026-10-12 03:45:09 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-10-12T03:45:09Z.

Review: COMMENTED


I found one remaining watched-output path in the recvfrom variants. The new HostWrite guard covers buf, but after a successful receive recvfrom_nid_postfix calls GuestAddress(peer, address, length) and sceNetRecvfrom calls native_to_guest_address(peer, from, fromlen); both write the address bytes and returned length directly. If either output points into write-protected watched guest memory, that write can still fault (or bypass the tracker). Could you add a regression with the optional address and length outputs in watched guest memory and guard those writes too?

<!-- greyfinch-sync:boykopovar/AnyPS5:review:5486352575 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/4272#pullrequestreview-5486352575). Originally posted by [MarcZX10](https://github.com/MarcZX10) on 2026-10-12T03:45:09Z. **Review:** COMMENTED --- I found one remaining watched-output path in the recvfrom variants. The new `HostWrite` guard covers `buf`, but after a successful receive `recvfrom_nid_postfix` calls `GuestAddress(peer, address, length)` and `sceNetRecvfrom` calls `native_to_guest_address(peer, from, fromlen)`; both write the address bytes and returned length directly. If either output points into write-protected watched guest memory, that write can still fault (or bypass the tracker). Could you add a regression with the optional address and length outputs in watched guest memory and guard those writes too?
Ondra97 commented 2026-10-12 04:12:36 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by Ondra97 on 2026-10-12T04:12:36Z.


@MarcZX100 Added the regression coverage in 1e429dc.

For both recvfrom_nid_postfix and sceNetRecvfrom, the IPv4 UDP tests now put the sender address and returned length on separate watched guest direct-memory pages, independently of the payload buffer. They cover a watched address, a watched length, and both together. Each case verifies the returned data, address and length, errno preservation, the expected dirty-page counts (4/4/8), a clean second collection, and rearmed read-only protection.

All six output scenarios pass with the current Windows implementation. These outputs are CPU writes; the existing Windows exception handler calls GuestArenaHandleWrite and resumes those writes while recording them. I did not reproduce a crash or lost tracking for CPU-writable watched output pages. This commit adds regression coverage; the production address-copy code is unchanged.

Validated with the supported WinLibs GCC 15.2.0 Release build: full build passed, all 12 UDP/TCP regression tests passed ten consecutive runs each, and conventions passed. Linux/macOS runtime behavior has not been verified locally.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6117610392 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/4272#issuecomment-6117610392). Originally posted by [Ondra97](https://github.com/Ondra97) on 2026-10-12T04:12:36Z. --- @MarcZX100 Added the regression coverage in [1e429dc](https://github.com/boykopovar/AnyPS5/pull/4272/commits/1e429dc23f527b823a4684aee289ef3858b6b063). For both `recvfrom_nid_postfix` and `sceNetRecvfrom`, the IPv4 UDP tests now put the sender address and returned length on separate watched guest direct-memory pages, independently of the payload buffer. They cover a watched address, a watched length, and both together. Each case verifies the returned data, address and length, errno preservation, the expected dirty-page counts (4/4/8), a clean second collection, and rearmed read-only protection. All six output scenarios pass with the current Windows implementation. These outputs are CPU writes; the existing [Windows exception handler](https://github.com/boykopovar/AnyPS5/blob/1e429dc23f527b823a4684aee289ef3858b6b063/core/libs/prx/libc/src/specifics/windows/CrashReport.cpp#L245) calls `GuestArenaHandleWrite` and resumes those writes while recording them. I did not reproduce a crash or lost tracking for CPU-writable watched output pages. This commit adds regression coverage; the production address-copy code is unchanged. Validated with the supported WinLibs GCC 15.2.0 Release build: full build passed, all 12 UDP/TCP regression tests passed ten consecutive runs each, and conventions passed. Linux/macOS runtime behavior has not been verified locally.
MarcZX10 commented 2026-10-12 04:14:25 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-10-12T04:14:25Z.

Review: COMMENTED


Thanks for adding the watched address and length cases. I rechecked the Windows write-watch path: GuestArenaHandleWrite records these CPU writes and resumes execution, and your new regressions exercise the address and length outputs independently and together. That resolves my earlier concern; I have no further issue on those outputs.

<!-- greyfinch-sync:boykopovar/AnyPS5:review:5486491244 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/4272#pullrequestreview-5486491244). Originally posted by [MarcZX10](https://github.com/MarcZX10) on 2026-10-12T04:14:25Z. **Review:** COMMENTED --- Thanks for adding the watched address and length cases. I rechecked the Windows write-watch path: `GuestArenaHandleWrite` records these CPU writes and resumes execution, and your new regressions exercise the address and length outputs independently and together. That resolves my earlier concern; I have no further issue on those outputs.
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!4272
No description provided.