fix(libkernel): bind an unbound stream socket on listen on Windows #16585

Merged
JJenkx merged 2 commits from upstream-pr/16585 into main 2026-11-10 00:55:37 +00:00
JJenkx commented 2026-11-07 07:02:12 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by JJenkx on 2026-11-07T07:02:12Z.


What

On Windows, listen on a stream socket that was never bound failed with EINVAL. FreeBSD's tcp_usr_listen binds a socket without a local port to an ephemeral port on the wildcard address (in_pcbbind / in6_pcbbind) before listening, and Linux does the same. Winsock instead rejects listen on an unbound socket with WSAEINVAL:

unbound listen(0) -> -1 wsa=10022 (WSAEINVAL=10022)

(a standalone Winsock probe on this machine; listens on a bound socket succeed for every backlog, including 0, negatives, INT_MIN and INT_MAX.)

listen_nid_postfix now handles this on Windows only. When ::listen fails with EINVAL, and getsockname shows the AF_INET or AF_INET6 stream socket is unbound, it binds the socket to the wildcard address with port 0 and retries listen. A bound socket, other families and every other error take the existing path unchanged, and nothing changes off Windows.

Found while checking #16577 on Windows, whose new guest_socket_backlog test calls _listen on an unbound socket and fails here with errno 22. This PR fixes the library behaviour that test depends on; it doesn't change #16577.

Tested

Windows 11 Enterprise 22621, AMD Ryzen 9 5950X, WinLibs GCC 15.2 UCRT, Release, on main 9b0fdbfb24.

  • guest_socket_port_zero gets a new CheckUnboundListen for AF_INET and AF_INET6. It calls listen with no bind, checks that getsockname reports a nonzero port, then connects to that port on loopback and accepts. Passes in 3 of 3 runs.
  • Negative control: with only the Udp.cpp change reverted (rebuilt, Udp.cpp recompiled), the same test fails at listen on an unbound stream socket binds it to an ephemeral port (errno 22).
  • All 34 tests matching guest_socket_*, guest_refused_connect, guest_poll*, guest_net_kernel_forwards*, guest_sce_net* and guest_kernel_socket* pass.
  • check_conventions.py passes.
  • Not tested on Linux; the change is inside #ifdef _WIN32. The hosted Windows check does not link these guest tests on pull requests.

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 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:5199330938 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/16585). Originally posted by [JJenkx](https://github.com/JJenkx) on 2026-11-07T07:02:12Z. --- ### What On Windows, `listen` on a stream socket that was never bound failed with `EINVAL`. FreeBSD's `tcp_usr_listen` binds a socket without a local port to an ephemeral port on the wildcard address (`in_pcbbind` / `in6_pcbbind`) before listening, and Linux does the same. Winsock instead rejects `listen` on an unbound socket with `WSAEINVAL`: ``` unbound listen(0) -> -1 wsa=10022 (WSAEINVAL=10022) ``` (a standalone Winsock probe on this machine; listens on a bound socket succeed for every backlog, including `0`, negatives, `INT_MIN` and `INT_MAX`.) `listen_nid_postfix` now handles this on Windows only. When `::listen` fails with `EINVAL`, and `getsockname` shows the `AF_INET` or `AF_INET6` stream socket is unbound, it binds the socket to the wildcard address with port 0 and retries `listen`. A bound socket, other families and every other error take the existing path unchanged, and nothing changes off Windows. Found while checking #16577 on Windows, whose new `guest_socket_backlog` test calls `_listen` on an unbound socket and fails here with `errno 22`. This PR fixes the library behaviour that test depends on; it doesn't change #16577. ### Tested Windows 11 Enterprise 22621, AMD Ryzen 9 5950X, WinLibs GCC 15.2 UCRT, Release, on `main` `9b0fdbfb24`. - `guest_socket_port_zero` gets a new `CheckUnboundListen` for `AF_INET` and `AF_INET6`. It calls `listen` with no `bind`, checks that `getsockname` reports a nonzero port, then connects to that port on loopback and accepts. Passes in 3 of 3 runs. - Negative control: with only the `Udp.cpp` change reverted (rebuilt, `Udp.cpp` recompiled), the same test fails at `listen on an unbound stream socket binds it to an ephemeral port (errno 22)`. - All 34 tests matching `guest_socket_*`, `guest_refused_connect`, `guest_poll*`, `guest_net_kernel_forwards*`, `guest_sce_net*` and `guest_kernel_socket*` pass. - `check_conventions.py` passes. - Not tested on Linux; the change is inside `#ifdef _WIN32`. The hosted Windows check does not link these guest tests on pull requests. ### 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 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
gabichuuuuu commented 2026-11-07 15:01:08 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by gabichuuuuu on 2026-11-07T15:01:08Z.

Review: COMMENTED


An independent Windows check of head 49fb156488.

Setup: Windows 11 x64, WinLibs GCC 15.2.0 r7 UCRT, Release, full build. Head merged onto main 4e44ed59b2 (clean merge), with main 4e44ed59b2 from the same build directory as the control. Each switch rebuilt Udp.cpp and the changed test before running.

ctest -R '^guest_socket_', alternating main and this PR for 5 rounds:

all 29 pass other results
main 3 of 5 1 round with guest_socket_options_multicast, 1 round with two host_write_sceNetRecv* timeouts
this PR 4 of 5 1 round with guest_socket_options_multicast
  • guest_socket_port_zero, with the new CheckUnboundListen for IPv4 and IPv6, passes in every run with this PR, usually in about 0.03 s (5 s once, in a run where the machine stalled).
  • The failures are not from this change. guest_socket_options_multicast fails on both sides with joining 224.0.0.251 through INADDR_ANY failed (errno 65), which depends on this host's network, and the 20 s timeouts showed up on both sides in tests that pass on main, where the new listen path does not exist. Earlier non-alternating runs had more timeouts on the PR side only because they ran later, while the machine was stalling. Alternating the two removed that difference.

AI-assisted: yes.

<!-- greyfinch-sync:boykopovar/AnyPS5:review:5813056702 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/16585#pullrequestreview-5813056702). Originally posted by [gabichuuuuu](https://github.com/gabichuuuuu) on 2026-11-07T15:01:08Z. **Review:** COMMENTED --- An independent Windows check of head `49fb156488`. **Setup:** Windows 11 x64, WinLibs GCC 15.2.0 r7 UCRT, Release, full build. Head merged onto `main` `4e44ed59b2` (clean merge), with `main` `4e44ed59b2` from the same build directory as the control. Each switch rebuilt `Udp.cpp` and the changed test before running. **`ctest -R '^guest_socket_'`, alternating `main` and this PR for 5 rounds:** | | all 29 pass | other results | |---|---|---| | `main` | 3 of 5 | 1 round with `guest_socket_options_multicast`, 1 round with two `host_write_sceNetRecv*` timeouts | | this PR | 4 of 5 | 1 round with `guest_socket_options_multicast` | - `guest_socket_port_zero`, with the new `CheckUnboundListen` for IPv4 and IPv6, passes in every run with this PR, usually in about 0.03 s (5 s once, in a run where the machine stalled). - The failures are not from this change. `guest_socket_options_multicast` fails on both sides with `joining 224.0.0.251 through INADDR_ANY failed (errno 65)`, which depends on this host's network, and the 20 s timeouts showed up on both sides in tests that pass on `main`, where the new `listen` path does not exist. Earlier non-alternating runs had more timeouts on the PR side only because they ran later, while the machine was stalling. Alternating the two removed that difference. AI-assisted: yes.
Superseabas commented 2026-11-07 17:49:14 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by Superseabas on 2026-11-07T17:49:14Z.

Review: APPROVED


Windows check of head 49fb156488, merged onto main 090f57166a (clean merge).

Setup: Windows 11 Home 10.0.26200, Ryzen 7 7700X, WinLibs GCC 16.2.0 (x86_64-ucrt-posix-seh, r3), Release, BUILD_TESTING=ON. Run from PowerShell.

Socket and Net tests (ctest -R '(^|_)(net|sce_net|socket|sock|reuse|ipv6only|setsockopt|getsockopt|linger|poll|refused)' -E '^agc' -j 8, 132 tests):

runs result
main 2 132 / 132
this PR 3 132 / 132 each time

Red check: with main's Udp.cpp and the PR's test, guest_socket_port_zero fails 3 of 3 at listen on an unbound stream socket binds it to an ephemeral port (errno 22).

Reading the code: skipping bind_nid_postfix for the implicit bind loses nothing for a stream socket. There it's a plain ::bind (localAddressCleared is only reset for datagram sockets), so binding the wildcard directly matches what an explicit bind to it would do.

A gap outside this PR's scope: libSceNet's sceNetListen calls Winsock's ::listen directly (Export.cpp), so it doesn't get this fix. I built a probe on this PR's head (local only) that creates an unbound stream socket through each library and calls listen with a backlog of 0:

probe sceNetListen family 2: result 0x80410116 errno 22, getsockname 0 port 0
probe listen (libkernel) family 2: result 0 errno 0
probe sceNetListen family 28: result 0x80410116 errno 22, getsockname 0 port 0
probe listen (libkernel) family 28: result 0 errno 0

So with this PR, libkernel listen binds and listens, and sceNetListen on the same kind of socket still fails with EINVAL on Windows. That doesn't block this PR, whose description scopes it to listen_nid_postfix. Titles that listen through libSceNet without binding first would need the same change in sceNetListen, as a follow-up.

tools/check_conventions.py --base main passes.

<!-- greyfinch-sync:boykopovar/AnyPS5:review:5814093256 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/16585#pullrequestreview-5814093256). Originally posted by [Superseabas](https://github.com/Superseabas) on 2026-11-07T17:49:14Z. **Review:** APPROVED --- Windows check of head `49fb156488`, merged onto `main` `090f57166a` (clean merge). **Setup:** Windows 11 Home 10.0.26200, Ryzen 7 7700X, WinLibs GCC 16.2.0 (`x86_64-ucrt-posix-seh`, r3), Release, `BUILD_TESTING=ON`. Run from PowerShell. **Socket and Net tests** (`ctest -R '(^|_)(net|sce_net|socket|sock|reuse|ipv6only|setsockopt|getsockopt|linger|poll|refused)' -E '^agc' -j 8`, 132 tests): | | runs | result | |---|---|---| | `main` | 2 | 132 / 132 | | this PR | 3 | 132 / 132 each time | **Red check:** with `main`'s `Udp.cpp` and the PR's test, `guest_socket_port_zero` fails 3 of 3 at `listen on an unbound stream socket binds it to an ephemeral port (errno 22)`. **Reading the code:** skipping `bind_nid_postfix` for the implicit bind loses nothing for a stream socket. There it's a plain `::bind` (`localAddressCleared` is only reset for datagram sockets), so binding the wildcard directly matches what an explicit `bind` to it would do. **A gap outside this PR's scope:** libSceNet's `sceNetListen` calls Winsock's `::listen` directly (`Export.cpp`), so it doesn't get this fix. I built a probe on this PR's head (local only) that creates an unbound stream socket through each library and calls `listen` with a backlog of 0: ``` probe sceNetListen family 2: result 0x80410116 errno 22, getsockname 0 port 0 probe listen (libkernel) family 2: result 0 errno 0 probe sceNetListen family 28: result 0x80410116 errno 22, getsockname 0 port 0 probe listen (libkernel) family 28: result 0 errno 0 ``` So with this PR, libkernel `listen` binds and listens, and `sceNetListen` on the same kind of socket still fails with `EINVAL` on Windows. That doesn't block this PR, whose description scopes it to `listen_nid_postfix`. Titles that listen through libSceNet without binding first would need the same change in `sceNetListen`, as a follow-up. `tools/check_conventions.py --base main` passes.
Superseabas commented 2026-11-08 17:10:09 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by Superseabas on 2026-11-08T17:10:09Z.

Review: APPROVED


Re-approving after the rebase. The lines this PR changes at 5baa8d229e are identical to 49fb156488, which I approved after testing it on Windows. I compared the diffs and did not re-run the tests on this head.

AI-assisted: yes.

<!-- greyfinch-sync:boykopovar/AnyPS5:review:5818798662 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/16585#pullrequestreview-5818798662). Originally posted by [Superseabas](https://github.com/Superseabas) on 2026-11-08T17:10:09Z. **Review:** APPROVED --- Re-approving after the rebase. The lines this PR changes at `5baa8d229e` are identical to `49fb156488`, which I approved after testing it on Windows. I compared the diffs and did not re-run the tests on this head. AI-assisted: yes.
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!16585
No description provided.