fix(libkernel): return ESPIPE for positioned socket I/O #3151

Merged
MarcZX10 merged 2 commits from upstream-pr/3151 into main 2026-10-11 07:43:16 +00:00
MarcZX10 commented 2026-10-10 23:22:14 +00:00 (Migrated from github.com)

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


What

Handle positioned I/O on guest socket descriptors before passing them to host file APIs. The POSIX pread, pwrite, preadv and pwritev exports now return -1 with errno = ESPIPE for an open guest socket and EBADF for a closed descriptor. Their sceKernel forms return the corresponding SCE kernel error values.

Regression coverage checks both error forms for scalar and vectored positioned calls.

Tested

Linux, GCC 12.2, Release build:

  • cmake --build /tmp/AnyPS5-build-socket-positioned --target guest_socket_read_write_tests -j2
  • ctest --test-dir /tmp/AnyPS5-build-socket-positioned -R "^guest_socket_read_write$" --output-on-failure (1/1 passed)
  • python3 tools/check_conventions.py --base upstream/main (passed)

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 (none added)
  • Generic behaviour, not specific to one title
  • Depends on: none
  • AI-assisted: yes
<!-- greyfinch-sync:boykopovar/AnyPS5:issue:4815155733 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/3151). Originally posted by [MarcZX10](https://github.com/MarcZX10) on 2026-10-10T23:22:14Z. --- ### What Handle positioned I/O on guest socket descriptors before passing them to host file APIs. The POSIX `pread`, `pwrite`, `preadv` and `pwritev` exports now return `-1` with `errno = ESPIPE` for an open guest socket and `EBADF` for a closed descriptor. Their `sceKernel` forms return the corresponding SCE kernel error values. Regression coverage checks both error forms for scalar and vectored positioned calls. ### Tested Linux, GCC 12.2, Release build: - `cmake --build /tmp/AnyPS5-build-socket-positioned --target guest_socket_read_write_tests -j2` - `ctest --test-dir /tmp/AnyPS5-build-socket-positioned -R "^guest_socket_read_write$" --output-on-failure` (1/1 passed) - `python3 tools/check_conventions.py --base upstream/main` (passed) ### 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 (none added) - [x] Generic behaviour, not specific to one title - [x] Depends on: none - [x] AI-assisted: yes
reyani-dev commented 2026-10-10 23:57:50 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by reyani-dev on 2026-10-10T23:57:50Z.


Windows check (Windows 11, WinLibs GCC 15.2.0 r7, Release), this head (302eae2b) merged onto main 181420f8:

  • main with only the test changes: guest_socket_read_write fails.
  • With the PR: it passes 3/3 (--repeat until-fail:3).

It conflicts with #3150, which touches the same lines, so one of the two will need a rebase after the other merges.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6103527169 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/3151#issuecomment-6103527169). Originally posted by [reyani-dev](https://github.com/reyani-dev) on 2026-10-10T23:57:50Z. --- Windows check (Windows 11, WinLibs GCC 15.2.0 r7, Release), this head (302eae2b) merged onto `main` 181420f8: - `main` with only the test changes: `guest_socket_read_write` fails. - With the PR: it passes 3/3 (`--repeat until-fail:3`). It conflicts with #3150, which touches the same lines, so one of the two will need a rebase after the other merges.
Dextroid17 commented 2026-10-11 00:29:32 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by Dextroid17 on 2026-10-11T00:29:32Z.

Review: COMMENTED


Both of these are the same kind of fix — a libkernel socket call that a valid guest argument can drive into undefined or wrong behaviour — and both are settled by the same authority: POSIX behaviour, for select in the first case and for positioned I/O on a descriptor that is not seekable in the second.

In #3152 the saturation is done the right way round. The check is a division before the multiply (seconds > remaining / MicrosecondsPerSecond), so the comparison itself cannot overflow, and the second guard subtracts an already-bounded requested from remaining, which is what keeps the residual check safe as well. Saturating at time_point::max() rather than clamping to some large constant is what makes an oversized timeval behave as an effectively infinite wait rather than a long one — and covering normal durations, int64_t maximum and the clock boundary is the set that would catch a regression in either direction.

In #3151 the two error forms are exactly the POSIX ones. ESPIPE for an open guest socket and EBADF for a closed descriptor, with the POSIX exports setting errno and returning −1 while the sceKernel forms return the translated kernel error, is the split a caller compiled against either interface expects. Answering from the guest descriptor before the host sees it is the part that matters, and the old behaviour shows why: the host call was made with the guest's own number, which is not a host descriptor at all, so it came back EBADF from a table that does not hold that object. ESPIPE for an open guest socket is the answer the guest is owed, and it can only come from the guest's own descriptor table.

The descriptor split is fixed for the process lifetime, which is what makes the guard sound. GuestSockets::FirstDescriptor is a constexpr, the table starts its counter at that value and only ever increments, and a host descriptor below the boundary is never entered into it — so a number cannot cross between the ranges, and the number alone decides which table owns the descriptor. That is worth one line in the entry, because otherwise a reader has to reconstruct it from the allocation code.

Written with AI assistance.

<!-- greyfinch-sync:boykopovar/AnyPS5:review:5481339755 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/3151#pullrequestreview-5481339755). Originally posted by [Dextroid17](https://github.com/Dextroid17) on 2026-10-11T00:29:32Z. **Review:** COMMENTED --- Both of these are the same kind of fix — a libkernel socket call that a *valid* guest argument can drive into undefined or wrong behaviour — and both are settled by the same authority: POSIX behaviour, for `select` in the first case and for positioned I/O on a descriptor that is not seekable in the second. **In #3152 the saturation is done the right way round.** The check is a *division* before the multiply (`seconds > remaining / MicrosecondsPerSecond`), so the comparison itself cannot overflow, and the second guard subtracts an already-bounded `requested` from `remaining`, which is what keeps the residual check safe as well. Saturating at `time_point::max()` rather than clamping to some large constant is what makes an oversized `timeval` behave as an effectively infinite wait rather than a long one — and covering normal durations, `int64_t` maximum and the clock boundary is the set that would catch a regression in either direction. **In #3151 the two error forms are exactly the POSIX ones.** `ESPIPE` for an open guest socket and `EBADF` for a closed descriptor, with the POSIX exports setting `errno` and returning −1 while the `sceKernel` forms return the translated kernel error, is the split a caller compiled against either interface expects. Answering from the guest descriptor before the host sees it is the part that matters, and the old behaviour shows why: the host call was made with the guest's own number, which is not a host descriptor at all, so it came back `EBADF` from a table that does not hold that object. `ESPIPE` for an open guest socket is the answer the guest is owed, and it can only come from the guest's own descriptor table. **The descriptor split is fixed for the process lifetime, which is what makes the guard sound.** `GuestSockets::FirstDescriptor` is a `constexpr`, the table starts its counter at that value and only ever increments, and a host descriptor below the boundary is never entered into it — so a number cannot cross between the ranges, and the number alone decides which table owns the descriptor. That is worth one line in the entry, because otherwise a reader has to reconstruct it from the allocation code. Written with AI assistance.
MarcZX10 commented 2026-10-11 03:25:19 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-10-11T03:25:19Z.


I clarified the descriptor split in TechnicalDebt.md: guest socket handles start at 0x10000000 and are allocated upward, which is why the descriptor range selects the guest socket table or host descriptor table. I rebased the PR onto current main and reran the Release build plus guest_socket_read_write (1/1 passed); conventions and git diff --check pass. The separate preadv/pwritev behavior remains scoped in this PR as tested.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6105002091 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/3151#issuecomment-6105002091). Originally posted by [MarcZX10](https://github.com/MarcZX10) on 2026-10-11T03:25:19Z. --- I clarified the descriptor split in `TechnicalDebt.md`: guest socket handles start at `0x10000000` and are allocated upward, which is why the descriptor range selects the guest socket table or host descriptor table. I rebased the PR onto current `main` and reran the Release build plus `guest_socket_read_write` (1/1 passed); conventions and `git diff --check` pass. The separate `preadv`/`pwritev` behavior remains scoped in this PR as tested.
MarcZX10 commented 2026-10-11 04:45:08 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-10-11T04:45:08Z.


Thanks for the Windows baseline and current-head results, and for flagging the overlap with #3150. These PRs cover separate socket behaviors but edit adjacent paths in the same file. Neither depends on the other; when one merges, I’ll rebase the remaining PR onto main, resolve the overlap, and rerun its focused checks.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6105546078 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/3151#issuecomment-6105546078). Originally posted by [MarcZX10](https://github.com/MarcZX10) on 2026-10-11T04:45:08Z. --- Thanks for the Windows baseline and current-head results, and for flagging the overlap with #3150. These PRs cover separate socket behaviors but edit adjacent paths in the same file. Neither depends on the other; when one merges, I’ll rebase the remaining PR onto `main`, resolve the overlap, and rerun its focused checks.
adriadam10 commented 2026-10-11 07:40:21 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by adriadam10 on 2026-10-11T07:40:21Z.


Rebased onto current main (conflicts with #3150 in GuestSocketReadWrite.cpp and the TechnicalDebt.md read/write line; kept both tests and merged the line with the ESPIPE/EBADF wording and the descriptor-range note). Build and guest_socket_read_write pass on the rebased tree; merging.

Minor, not blocking: with a negative offset on a socket these return EINVAL, where FreeBSD checks seekability first and returns ESPIPE.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6106768582 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/3151#issuecomment-6106768582). Originally posted by [adriadam10](https://github.com/adriadam10) on 2026-10-11T07:40:21Z. --- Rebased onto current `main` (conflicts with #3150 in `GuestSocketReadWrite.cpp` and the `TechnicalDebt.md` read/write line; kept both tests and merged the line with the `ESPIPE`/`EBADF` wording and the descriptor-range note). Build and `guest_socket_read_write` pass on the rebased tree; merging. Minor, not blocking: with a negative offset on a socket these return `EINVAL`, where FreeBSD checks seekability first and returns `ESPIPE`.
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!3151
No description provided.