fix(libkernel): saturate oversized select timeouts #3152

Merged
MarcZX10 merged 2 commits from upstream-pr/3152 into main 2026-10-11 07:49:59 +00:00
MarcZX10 commented 2026-10-10 23:22:18 +00:00 (Migrated from github.com)

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


What

A valid large timeval can overflow when select converts its seconds directly into the clock duration, producing a deadline in the past and an immediate timeout. Compute the available range before converting and saturate oversized deadlines at steady_clock::time_point::max(). Round remaining microseconds to milliseconds without an overflowing addition.

The regression checks normal durations, very large and maximum int64_t seconds, and values at the clock deadline boundary. Oversized valid timeouts can wait effectively indefinitely after saturation; the console behavior for values outside the host clock's range is unknown and is documented in Technical Debt.

Tested

Linux, GCC 12.2, Release build on rebased head 71b2a153:

  • cmake --build /tmp/AnyPS5-build-3152-current-release --target guest_sce_net_tests -j2
  • ctest --test-dir /tmp/AnyPS5-build-3152-current-release -R "^guest_sce_net$" --output-on-failure (1/1 passed)
  • python3 tools/check_conventions.py --base upstream/main (passed)
  • git diff --check (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:4815156007 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/3152). Originally posted by [MarcZX10](https://github.com/MarcZX10) on 2026-10-10T23:22:18Z. --- ### What A valid large `timeval` can overflow when `select` converts its seconds directly into the clock duration, producing a deadline in the past and an immediate timeout. Compute the available range before converting and saturate oversized deadlines at `steady_clock::time_point::max()`. Round remaining microseconds to milliseconds without an overflowing addition. The regression checks normal durations, very large and maximum `int64_t` seconds, and values at the clock deadline boundary. Oversized valid timeouts can wait effectively indefinitely after saturation; the console behavior for values outside the host clock's range is unknown and is documented in Technical Debt. ### Tested Linux, GCC 12.2, Release build on rebased head `71b2a153`: - `cmake --build /tmp/AnyPS5-build-3152-current-release --target guest_sce_net_tests -j2` - `ctest --test-dir /tmp/AnyPS5-build-3152-current-release -R "^guest_sce_net$" --output-on-failure` (1/1 passed) - `python3 tools/check_conventions.py --base upstream/main` (passed) - `git diff --check` (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:51 +00:00 (Migrated from github.com)

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


Windows check (Windows 11, WinLibs GCC 15.2.0 r7, Release), this head (268b8f4a) merged onto main 181420f8: guest_sce_net passes 3/3 with the PR.

On Windows, the new check also passes on main with only the test change applied, so on this host it doesn't distinguish the fix. Presumably the oversized timeout doesn't overflow in the Windows select path, or the test value doesn't reach it there. Linux is where it discriminates.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6103527340 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/3152#issuecomment-6103527340). Originally posted by [reyani-dev](https://github.com/reyani-dev) on 2026-10-10T23:57:51Z. --- Windows check (Windows 11, WinLibs GCC 15.2.0 r7, Release), this head (268b8f4a) merged onto `main` 181420f8: `guest_sce_net` passes 3/3 with the PR. On Windows, the new check also passes on `main` with only the test change applied, so on this host it doesn't distinguish the fix. Presumably the oversized timeout doesn't overflow in the Windows `select` path, or the test value doesn't reach it there. Linux is where it discriminates.
Dextroid17 commented 2026-10-11 00:29:30 +00:00 (Migrated from github.com)

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

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:5481339683 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/3152#pullrequestreview-5481339683). Originally posted by [Dextroid17](https://github.com/Dextroid17) on 2026-10-11T00:29:30Z. **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:36:16 +00:00 (Migrated from github.com)

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


Thanks for the careful review. I added a Technical Debt entry documenting that valid timeouts beyond the remaining host steady_clock range saturate at time_point::max() and can therefore wait effectively indefinitely. The console behavior for a valid timeout outside the host clock's representable range is unknown, so I have not claimed that this matches console behavior; the saturation prevents host-side overflow and the immediate timeout it caused.

I rebased onto current main, added the documentation, and reran the focused Release test on Linux (GCC 12.2): guest_sce_net passes (1/1). The updated head is 71b2a153.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6105077175 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/3152#issuecomment-6105077175). Originally posted by [MarcZX10](https://github.com/MarcZX10) on 2026-10-11T03:36:16Z. --- Thanks for the careful review. I added a Technical Debt entry documenting that valid timeouts beyond the remaining host `steady_clock` range saturate at `time_point::max()` and can therefore wait effectively indefinitely. The console behavior for a valid timeout outside the host clock's representable range is unknown, so I have not claimed that this matches console behavior; the saturation prevents host-side overflow and the immediate timeout it caused. I rebased onto current `main`, added the documentation, and reran the focused Release test on Linux (GCC 12.2): `guest_sce_net` passes (1/1). The updated head is `71b2a153`.
MarcZX10 commented 2026-10-11 04:42:40 +00:00 (Migrated from github.com)

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


Thanks for checking this on Windows. Agreed: because the new test also passes on unmodified main there, that run does not demonstrate the fix. The PR's Tested section only claims the Linux run, where the overflow occurs; I will treat the Windows result as a platform-scope observation, not regression evidence.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6105529618 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/3152#issuecomment-6105529618). Originally posted by [MarcZX10](https://github.com/MarcZX10) on 2026-10-11T04:42:40Z. --- Thanks for checking this on Windows. Agreed: because the new test also passes on unmodified `main` there, that run does not demonstrate the fix. The PR's Tested section only claims the Linux run, where the overflow occurs; I will treat the Windows result as a platform-scope observation, not regression evidence.
adriadam10 commented 2026-10-11 07:44:18 +00:00 (Migrated from github.com)

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


Rebased onto current main (only a TechnicalDebt adjacency conflict with the new pthread_kill line; both lines kept). Rebuilt and guest_sce_net passes on Linux. Earlier checks: each saturation guard in DeadlineAfter is covered by the test, and FreeBSD's tvtosbt_sat saturates the same way. Windows run confirmed by @AchrafReyani. Merging once CI is green.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6106798877 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/3152#issuecomment-6106798877). Originally posted by [adriadam10](https://github.com/adriadam10) on 2026-10-11T07:44:18Z. --- Rebased onto current `main` (only a TechnicalDebt adjacency conflict with the new `pthread_kill` line; both lines kept). Rebuilt and `guest_sce_net` passes on Linux. Earlier checks: each saturation guard in `DeadlineAfter` is covered by the test, and FreeBSD's `tvtosbt_sat` saturates the same way. Windows run confirmed by @AchrafReyani. Merging once CI is green.
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!3152
No description provided.