fix(libSceNet): read setsockopt timeouts as struct timeval #4371

Merged
milescoviello merged 1 commit from upstream-pr/4371 into main 2026-10-12 18:21:56 +00:00
milescoviello commented 2026-10-12 07:58:36 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by milescoviello on 2026-10-12T07:58:36Z.


What

Since #4111, libkernel's setsockopt() hands options for a libSceNet socket to sceNetSetsockopt(). That function's SO_RCVTIMEO and SO_SNDTIMEO take an int of microseconds, but the POSIX call takes a struct timeval, so main reads the low half of tv_sec as microseconds:

  • {1, 250000} (1.25 s) sets a 1 µs receive timeout, so every receive fails with EAGAIN almost at once;
  • {0, 500000} (0.5 s) sets 0, which disables the timeout, so a receive that should give up after half a second blocks forever;
  • an out-of-range tv_usec is accepted.

FreeBSD reads a struct timeval for these options, sys/kern/uipc_socket.c, sosetopt():

case SO_SNDTIMEO:
case SO_RCVTIMEO:
	...
		error = sooptcopyin(sopt, &tv, sizeof tv,
		    sizeof tv);
	if (error)
		goto bad;
	if (tv.tv_sec < 0 || tv.tv_usec < 0 ||
	    tv.tv_usec >= 1000000) {
		error = EDOM;
		goto bad;
	}

and sooptcopyin() rejects a shorter value:

if ((valsize = sopt->sopt_valsize) < minlen)
	return EINVAL;

setsockopt() on a libSceNet socket now converts the timeval for these two options (EINVAL for a value shorter than a timeval, EDOM for a negative or out-of-range one, EFAULT for a null value with a length, as kern_setsockopt() does) and applies it to the host socket. sceNetSetsockopt() keeps taking microseconds; both paths now share the code that sets the host timeout, and sceNetGetsockopt() reports the timeout either way. No title is claimed.

Tested

Ubuntu 24.04, GCC 13.3 (CI's compiler), Release with CI's configure flags.

  • guest_sce_net sets SO_RCVTIMEO {1, 250000} and SO_SNDTIMEO {0, 500000} on a libSceNet socket through setsockopt() and reads them back with sceNetGetsockopt(); checks EDOM for {0, 1000000} and {-1, 0} and EINVAL for a 4-byte value, both leaving the timeouts unchanged; checks that sceNetSetsockopt() still takes an int of microseconds; and, on Linux, that a receive on a bound socket with a {0, 50000} timeout fails with EAGAIN after at least 40 ms.
  • With only the test applied to main's library sources it fails:
    setsockopt(0x1006) with a struct timeval on a libSceNet socket set 1 microseconds, expected 1250000
    
  • With the fix it passes, as do guest_udp, guest_tcp, guest_socket_pair, guest_socket_read_write and guest_socket_events, and all 297 guest_* tests pass (-E '^guest_video_out_').

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
  • Unblocks: none
  • AI-assisted: yes

🤖 Generated with Claude Code

<!-- greyfinch-sync:boykopovar/AnyPS5:issue:4826548997 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/4371). Originally posted by [milescoviello](https://github.com/milescoviello) on 2026-10-12T07:58:36Z. --- ### What Since #4111, libkernel's `setsockopt()` hands options for a libSceNet socket to `sceNetSetsockopt()`. That function's `SO_RCVTIMEO` and `SO_SNDTIMEO` take an `int` of microseconds, but the POSIX call takes a `struct timeval`, so main reads the low half of `tv_sec` as microseconds: - `{1, 250000}` (1.25 s) sets a 1 µs receive timeout, so every receive fails with `EAGAIN` almost at once; - `{0, 500000}` (0.5 s) sets 0, which disables the timeout, so a receive that should give up after half a second blocks forever; - an out-of-range `tv_usec` is accepted. FreeBSD reads a `struct timeval` for these options, [`sys/kern/uipc_socket.c`](https://github.com/freebsd/freebsd-src/blob/releng/11.4/sys/kern/uipc_socket.c#L2657-L2677), `sosetopt()`: ```c case SO_SNDTIMEO: case SO_RCVTIMEO: ... error = sooptcopyin(sopt, &tv, sizeof tv, sizeof tv); if (error) goto bad; if (tv.tv_sec < 0 || tv.tv_usec < 0 || tv.tv_usec >= 1000000) { error = EDOM; goto bad; } ``` and [`sooptcopyin()`](https://github.com/freebsd/freebsd-src/blob/releng/11.4/sys/kern/uipc_socket.c#L2474-L2475) rejects a shorter value: ```c if ((valsize = sopt->sopt_valsize) < minlen) return EINVAL; ``` `setsockopt()` on a libSceNet socket now converts the `timeval` for these two options (`EINVAL` for a value shorter than a `timeval`, `EDOM` for a negative or out-of-range one, `EFAULT` for a null value with a length, as [`kern_setsockopt()`](https://github.com/freebsd/freebsd-src/blob/releng/11.4/sys/kern/uipc_syscalls.c#L1414-L1415) does) and applies it to the host socket. `sceNetSetsockopt()` keeps taking microseconds; both paths now share the code that sets the host timeout, and `sceNetGetsockopt()` reports the timeout either way. No title is claimed. ### Tested Ubuntu 24.04, GCC 13.3 (CI's compiler), Release with CI's configure flags. - `guest_sce_net` sets `SO_RCVTIMEO` `{1, 250000}` and `SO_SNDTIMEO` `{0, 500000}` on a libSceNet socket through `setsockopt()` and reads them back with `sceNetGetsockopt()`; checks `EDOM` for `{0, 1000000}` and `{-1, 0}` and `EINVAL` for a 4-byte value, both leaving the timeouts unchanged; checks that `sceNetSetsockopt()` still takes an `int` of microseconds; and, on Linux, that a receive on a bound socket with a `{0, 50000}` timeout fails with `EAGAIN` after at least 40 ms. - With only the test applied to main's library sources it fails: ```text setsockopt(0x1006) with a struct timeval on a libSceNet socket set 1 microseconds, expected 1250000 ``` - With the fix it passes, as do `guest_udp`, `guest_tcp`, `guest_socket_pair`, `guest_socket_read_write` and `guest_socket_events`, and all 297 `guest_*` tests pass (`-E '^guest_video_out_'`). ### 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] Unblocks: none - [x] AI-assisted: yes 🤖 Generated with [Claude Code](https://claude.com/claude-code)
xXPBSXx commented 2026-10-12 12:20:21 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by xXPBSXx on 2026-10-12T12:20:21Z.


Windows test: head e9c5ae26b merged onto current main ffa198407 (clean merge, no conflicts). Windows 10, MinGW GCC (WinLibs), Ninja Release, -j4: full build 0 errors; ctest with the filter guest_sce_net: guest_sce_net passed (1 test(s), 0 failed); python tools/check_conventions.py --base upstream/main passed. I did not run the new tests against main without the change.

AI-assisted (Claude Code).

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6124145467 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/4371#issuecomment-6124145467). Originally posted by [xXPBSXx](https://github.com/xXPBSXx) on 2026-10-12T12:20:21Z. --- Windows test: head `e9c5ae26b` merged onto current `main` `ffa198407` (clean merge, no conflicts). Windows 10, MinGW GCC (WinLibs), Ninja Release, `-j4`: full build 0 errors; ctest with the filter `guest_sce_net`: guest_sce_net passed (1 test(s), 0 failed); `python tools/check_conventions.py --base upstream/main` passed. I did not run the new tests against `main` without the change. AI-assisted (Claude Code).
github-actions[bot] commented 2026-10-12 17:49:28 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by github-actions[bot] on 2026-10-12T17:49:28Z.


Other open pull requests touch the same code. Merging one will break the other, or both implement the same thing. If this one builds on them, list them in Depends on.

Pull request Conflicting files (lines on main) Same exports Same new files
#3728 core/libs/prx/libSceNet/Export.cpp L323-324
<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6129585437 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/4371#issuecomment-6129585437). Originally posted by [github-actions[bot]](https://github.com/apps/github-actions) on 2026-10-12T17:49:28Z. --- <!-- pr-overlap --> Other open pull requests touch the same code. Merging one will break the other, or both implement the same thing. If this one builds on them, list them in **Depends on**. | Pull request | Conflicting files (lines on `main`) | Same exports | Same new files | |---|---|---|---| | #3728 | `core/libs/prx/libSceNet/Export.cpp` [L323-324](https://github.com/boykopovar/AnyPS5/blob/bde332a59ce98ce8aa776b26a4ed04e53417946d/core/libs/prx/libSceNet/Export.cpp#L323-L324) | | |
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!4371
No description provided.