feat(libkernel): pass descriptors over socketpairs on Windows #25791

Merged
DannyOppenheimer merged 1 commit from upstream-pr/25791 into main 2026-11-26 09:34:08 +00:00
DannyOppenheimer commented 2026-11-22 17:48:49 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by DannyOppenheimer on 2026-11-22T17:48:49Z.


What

On Windows, sendmsg with control data threw (sendmsg: control data is not supported on Windows), so a guest could not pass descriptors in SCM_RIGHTS messages. recvmsg also refused MSG_CMSG_CLOEXEC with EOPNOTSUPP, because only a receive that can take descriptors accepts that flag. Parallax Browser (PPSA99207, #23335) hits the second case: every IPC receive of its UI process fails with Error receiving message: Operation not supported. Linux already passes descriptors through the host's SCM_RIGHTS (#7090). Windows socket pairs are loopback TCP and UDP sockets, which cannot carry descriptors, so this PR passes them in the guest layer for SOCK_STREAM and SOCK_SEQPACKET socketpair ends.

  • sendmsg holds a guest dup of each passed descriptor and queues it on the peer end, keyed to the stream byte (stream) or record (seqpacket) where its data starts. Sends on an end are serialized, as FreeBSD's sblock on so_snd does, so the key is exact. The queue entry is made before the host send and withdrawn if the send fails.
  • Receives on those ends check readability and read under the end's queue lock, so a sender cannot queue descriptors in the middle of a read. Every receive path goes through this: recv, recvfrom, recvmsg, read and readv. Sends from send, sendto, sendmsg, write, writev and sendfile advance the sender's position.
  • Semantics follow FreeBSD 11.4:
    • A stream read stops where a message with descriptors starts. Data sent after that message joins its record. See uipc_send (sbappendcontrol_locked makes a record) and soreceive_generic.
    • MSG_WAITALL returns short at that point (L1856).
    • A seqpacket record carries its own descriptors, also when its data is truncated.
    • A zero-length recvmsg on a stream delivers the descriptors once their data has arrived and leaves the data.
    • Each SCM_RIGHTS message of one sendmsg arrives as its own control message.
    • A control buffer too small for all of them gets the whole messages that fit plus MSG_CTRUNC, and the rest are closed (kern_recvit copies whole control mbufs and disposes the rest).
    • MSG_CMSG_CLOEXEC sets close-on-exec (unp_externalize).
    • If the descriptor table is full, the receive fails with EMSGSIZE and the descriptors are freed (L1825).
    • A receive without a control buffer (recv, read, ...) closes the descriptors, as do shutdown(SHUT_RD) (sorflush) and closing (or dup2 over) the receiving end's last descriptor.
    • A received descriptor gets the lowest free number. A descriptor in flight holds a number of its own; the receiver keeps whichever of that number and a new duplicate is lower.
  • SOCK_DGRAM pairs still throw: Windows drops a datagram sent to a full receiver, which would put the queue out of step. Seqpacket pairs do not drop with one sender per end: #23341's flow control admits a record only while the receiver holds less than the sender's SO_SNDBUF (at most 512 KiB), and a record is at most 64 KiB, against a 1 MiB host buffer. Each sender checks for room before it takes the send lock, so nine or more threads sending 64 KiB records on one end at the same moment could still overflow the buffer. That was already possible with #23341, where it loses data; here it would also misplace descriptors. Moving the room check under the send lock is a follow-up.
  • Linux behaviour is unchanged: the record writer is shared and PassedDescriptors also reports each message's count.
  • Debt: the sendmsg and recvmsg entry now says what Windows does. It also lists the two remaining differences: a descriptor in flight occupies a number, and a socket passed into its own queue is never freed (FreeBSD's unp_gc).

Tested

Windows 11 Pro 10.0.26200, Intel Core i7-11370H, WinLibs GCC 15.2.0 r7, Release, rebased on main 788b1afac9.

  • New guest_socket_pair_rights (Windows only): passed pipes and stream, seqpacket and pair sockets; stream boundaries and merging; MSG_WAITALL stopping at a boundary; a blocked receiver; a zero-length receive; peek; truncation and several messages; disposal on a plain read, on close, on dup2 and on SHUT_RD; refused sends to a closed peer and after SHUT_WR (the second withdraws the queued entry); seqpacket records with truncation; a refused datagram pair; the lowest free number. 40 runs in a row pass (--repeat until-fail:40).
  • Red checks, each making the new test abort: without the stream boundary (CheckStreamRecords), without the lower-number choice (CheckLowestNumber), without disposal on close (CheckClosedReceiver) and without the withdrawal of a failed send (CheckRefusedSend).
  • All 162 socket-related ctests built here pass (socket, pair, poll, select, kqueue, equeue, sendfile, TCP, UDP, net, rudp, resolver).
  • Parallax Browser (PPSA99207, 01.002.002), same build, 180 s from start, with only libkernel.prx switched between main and this PR:
    • main: 9 lines IPC: error receiving on socket N ...: Error receiving message: Operation not supported. The web process ends after the first relaunch, and the title shows its error page twice.
    • This PR: no IPC receive errors. The web process ends once, after the network process spent its launch retries on the ELF loader.
    • The title still needs the jailbreak ELF loader (127.0.0.1:9021) for its WebProcess and NetworkProcess, so no page loads yet.
  • Linux: g++ -std=gnu++20 -fsyntax-only -Wall -Wextra of Udp.cpp with GCC 13 (WSL Ubuntu 24.04) is clean. The Linux rights test was not run here.
  • tools/progress.py --module libkernel: 723/769 on main and on this branch.
  • tools/check_conventions.py --base origin/main: passes.
  • tools/pr_overlap.py: no open PR implements descriptor passing on Windows.
    • #17113 (sendfile: the SendIovecs signature) and #25017 (macOS seqpacket pairs: Close and Forget) change lines next to this PR's; #18197 shares only the file.
    • #8605, #11896, #12682, #12688, #12700, #12965 and #22048 conflict with main, so the tool could not compare them. I compared them by hand: none passes descriptors. #22048 and #24944 edit recvmsg and leave the passing path to it.
    • I will rebase onto whichever lands first.

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 and the licence notice of code adapted from upstream (see AGENTS.md)
  • 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: PPSA99207 #23335
  • GPUs tested: n/a
  • AI-assisted: yes (Claude Code)

🤖 Generated with Claude Code

<!-- greyfinch-sync:boykopovar/AnyPS5:issue:5441645931 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/25791). Originally posted by [DannyOppenheimer](https://github.com/DannyOppenheimer) on 2026-11-22T17:48:49Z. --- ### What On Windows, `sendmsg` with control data threw (`sendmsg: control data is not supported on Windows`), so a guest could not pass descriptors in `SCM_RIGHTS` messages. `recvmsg` also refused `MSG_CMSG_CLOEXEC` with `EOPNOTSUPP`, because only a receive that can take descriptors accepts that flag. Parallax Browser (PPSA99207, #23335) hits the second case: every IPC receive of its UI process fails with `Error receiving message: Operation not supported`. Linux already passes descriptors through the host's `SCM_RIGHTS` (#7090). Windows socket pairs are loopback TCP and UDP sockets, which cannot carry descriptors, so this PR passes them in the guest layer for `SOCK_STREAM` and `SOCK_SEQPACKET` socketpair ends. - `sendmsg` holds a guest `dup` of each passed descriptor and queues it on the peer end, keyed to the stream byte (stream) or record (seqpacket) where its data starts. Sends on an end are serialized, as FreeBSD's `sblock` on `so_snd` does, so the key is exact. The queue entry is made before the host send and withdrawn if the send fails. - Receives on those ends check readability and read under the end's queue lock, so a sender cannot queue descriptors in the middle of a read. Every receive path goes through this: `recv`, `recvfrom`, `recvmsg`, `read` and `readv`. Sends from `send`, `sendto`, `sendmsg`, `write`, `writev` and `sendfile` advance the sender's position. - Semantics follow FreeBSD 11.4: - A stream read stops where a message with descriptors starts. Data sent after that message joins its record. See [`uipc_send`](https://github.com/freebsd/freebsd-src/blob/release/11.4.0/sys/kern/uipc_usrreq.c#L997) (`sbappendcontrol_locked` makes a record) and [`soreceive_generic`](https://github.com/freebsd/freebsd-src/blob/release/11.4.0/sys/kern/uipc_socket.c#L1732). - `MSG_WAITALL` returns short at that point ([L1856](https://github.com/freebsd/freebsd-src/blob/release/11.4.0/sys/kern/uipc_socket.c#L1856)). - A seqpacket record carries its own descriptors, also when its data is truncated. - A zero-length `recvmsg` on a stream delivers the descriptors once their data has arrived and leaves the data. - Each `SCM_RIGHTS` message of one `sendmsg` arrives as its own control message. - A control buffer too small for all of them gets the whole messages that fit plus `MSG_CTRUNC`, and the rest are closed ([`kern_recvit`](https://github.com/freebsd/freebsd-src/blob/release/11.4.0/sys/kern/uipc_syscalls.c#L1143-L1155) copies whole control mbufs and disposes the rest). - `MSG_CMSG_CLOEXEC` sets close-on-exec ([`unp_externalize`](https://github.com/freebsd/freebsd-src/blob/release/11.4.0/sys/kern/uipc_usrreq.c#L1833)). - If the descriptor table is full, the receive fails with `EMSGSIZE` and the descriptors are freed ([L1825](https://github.com/freebsd/freebsd-src/blob/release/11.4.0/sys/kern/uipc_usrreq.c#L1825)). - A receive without a control buffer (`recv`, `read`, ...) closes the descriptors, as do `shutdown(SHUT_RD)` ([`sorflush`](https://github.com/freebsd/freebsd-src/blob/release/11.4.0/sys/kern/uipc_socket.c#L2429)) and closing (or `dup2` over) the receiving end's last descriptor. - A received descriptor gets the lowest free number. A descriptor in flight holds a number of its own; the receiver keeps whichever of that number and a new duplicate is lower. - `SOCK_DGRAM` pairs still throw: Windows drops a datagram sent to a full receiver, which would put the queue out of step. Seqpacket pairs do not drop with one sender per end: #23341's flow control admits a record only while the receiver holds less than the sender's `SO_SNDBUF` (at most 512 KiB), and a record is at most 64 KiB, against a 1 MiB host buffer. Each sender checks for room before it takes the send lock, so nine or more threads sending 64 KiB records on one end at the same moment could still overflow the buffer. That was already possible with #23341, where it loses data; here it would also misplace descriptors. Moving the room check under the send lock is a follow-up. - Linux behaviour is unchanged: the record writer is shared and `PassedDescriptors` also reports each message's count. - Debt: the `sendmsg and recvmsg` entry now says what Windows does. It also lists the two remaining differences: a descriptor in flight occupies a number, and a socket passed into its own queue is never freed (FreeBSD's `unp_gc`). ### Tested <!-- OS, title or homebrew, what was checked. "Not tested" is a valid answer. A performance change: the summary lines of main and of this PR from the same machine and scene, with the traces attached, captured as the Performance report issue form describes. --> Windows 11 Pro 10.0.26200, Intel Core i7-11370H, WinLibs GCC 15.2.0 r7, Release, rebased on `main` `788b1afac9`. - New `guest_socket_pair_rights` (Windows only): passed pipes and stream, seqpacket and pair sockets; stream boundaries and merging; `MSG_WAITALL` stopping at a boundary; a blocked receiver; a zero-length receive; peek; truncation and several messages; disposal on a plain read, on close, on `dup2` and on `SHUT_RD`; refused sends to a closed peer and after `SHUT_WR` (the second withdraws the queued entry); seqpacket records with truncation; a refused datagram pair; the lowest free number. 40 runs in a row pass (`--repeat until-fail:40`). - Red checks, each making the new test abort: without the stream boundary (`CheckStreamRecords`), without the lower-number choice (`CheckLowestNumber`), without disposal on close (`CheckClosedReceiver`) and without the withdrawal of a failed send (`CheckRefusedSend`). - All 162 socket-related ctests built here pass (socket, pair, poll, select, kqueue, equeue, sendfile, TCP, UDP, net, rudp, resolver). - Parallax Browser (PPSA99207, 01.002.002), same build, 180 s from start, with only `libkernel.prx` switched between `main` and this PR: - `main`: 9 lines `IPC: error receiving on socket N ...: Error receiving message: Operation not supported`. The web process ends after the first relaunch, and the title shows its error page twice. - This PR: no IPC receive errors. The web process ends once, after the network process spent its launch retries on the ELF loader. - The title still needs the jailbreak ELF loader (`127.0.0.1:9021`) for its WebProcess and NetworkProcess, so no page loads yet. - Linux: `g++ -std=gnu++20 -fsyntax-only -Wall -Wextra` of `Udp.cpp` with GCC 13 (WSL Ubuntu 24.04) is clean. The Linux rights test was not run here. - `tools/progress.py --module libkernel`: 723/769 on `main` and on this branch. - `tools/check_conventions.py --base origin/main`: passes. - `tools/pr_overlap.py`: no open PR implements descriptor passing on Windows. - #17113 (sendfile: the `SendIovecs` signature) and #25017 (macOS seqpacket pairs: `Close` and `Forget`) change lines next to this PR's; #18197 shares only the file. - #8605, #11896, #12682, #12688, #12700, #12965 and #22048 conflict with `main`, so the tool could not compare them. I compared them by hand: none passes descriptors. #22048 and #24944 edit `recvmsg` and leave the passing path to it. - I will rebase onto whichever lands first. ### Checklist <!-- Rules behind each item: https://github.com/boykopovar/AnyPS5/blob/main/CONTRIBUTING.md --> - [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) and the licence notice of code adapted from upstream (see AGENTS.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 <!-- #PR, or none --> - [x] Fixes: none <!-- issue it closes as Fixes #N, or none --> - [x] Unblocks: PPSA99207 #23335 <!-- title ID and Game bring-up discussion, e.g. PPSA01325 #3232, or none --> - [x] GPUs tested: n/a <!-- for AGC, shader recompiler or VideoOut changes: vendor, model, driver and OS of each GPU the GPU tests ran on; otherwise n/a --> - [x] AI-assisted: yes (Claude Code) <!-- yes / no --> 🤖 Generated with [Claude Code](https://claude.com/claude-code)
MarcZX10 commented 2026-11-22 17:51:19 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-11-22T17:51:19Z.

Review: APPROVED


Reviewed — looks good

The code changes and added Windows test are consistent with the stated descriptor-passing behavior; the test results in the description were author-reported and were not run in this review.

What I ran

Reviewed head 536766a4744b25148a87603f5181ec3504ff026c using repo_read.py. No build or test commands were run. The PR description reports Windows tests and Linux syntax checking; those results were not independently confirmed.

Results

Check Result Note
Diff and changed code passed Four changed files read
Build and tests not run Author-reported results only

Findings

Blocking

None.

Non-blocking

None.

Not verified

The full 162-test run, 40-repeat test run, Linux syntax check, and Parallax Browser run were not performed here. The Windows test target and its test source were read, but not executed. All changed files listed in the review manifest were read; no files were excluded.

To get this to a good state

Nothing is required.

<!-- greyfinch-sync:boykopovar/AnyPS5:review:6013595816 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/25791#pullrequestreview-6013595816). Originally posted by [MarcZX10](https://github.com/MarcZX10) on 2026-11-22T17:51:19Z. **Review:** APPROVED --- # Reviewed — looks good The code changes and added Windows test are consistent with the stated descriptor-passing behavior; the test results in the description were author-reported and were not run in this review. ## What I ran Reviewed head `536766a4744b25148a87603f5181ec3504ff026c` using `repo_read.py`. No build or test commands were run. The PR description reports Windows tests and Linux syntax checking; those results were not independently confirmed. ## Results | Check | Result | Note | |---|---|---| | Diff and changed code | passed | Four changed files read | | Build and tests | not run | Author-reported results only | ## Findings ### Blocking None. ### Non-blocking None. ## Not verified The full 162-test run, 40-repeat test run, Linux syntax check, and Parallax Browser run were not performed here. The Windows test target and its test source were read, but not executed. All changed files listed in the review manifest were read; no files were excluded. ## To get this to a good state Nothing is required.
malekaifa commented 2026-11-23 03:12:31 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by malekaifa on 2026-11-23T03:12:31Z.

Review: APPROVED


Reviewed current head 1250520f2a6f5d1962fbb5770ece1e4390e00f14 against MarcZX10's previously approved head 536766a4744b25148a87603f5181ec3504ff026c.

Three of the four PR files are byte-identical. The only implementation delta is in Linux SendBeforeConnect: the zero-time poll(POLLOUT) now retries EINTR, throws on another host poll error, and preserves the prior ready == 0 still-pending path. I found no regression; it prevents a negative poll result from being treated as a completed connect.

Current exact-head checks are fully terminal: 9 success, 6 intentional workflow-design skips, 0 failures, 0 pending, including a successful Windows job for the descriptor-passing implementation.

No blocking findings on the current head. AI-assisted: yes

<!-- greyfinch-sync:boykopovar/AnyPS5:review:6016157038 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/25791#pullrequestreview-6016157038). Originally posted by [malekaifa](https://github.com/malekaifa) on 2026-11-23T03:12:31Z. **Review:** APPROVED --- Reviewed current head `1250520f2a6f5d1962fbb5770ece1e4390e00f14` against MarcZX10's previously approved head `536766a4744b25148a87603f5181ec3504ff026c`. Three of the four PR files are byte-identical. The only implementation delta is in Linux `SendBeforeConnect`: the zero-time `poll(POLLOUT)` now retries `EINTR`, throws on another host poll error, and preserves the prior `ready == 0` still-pending path. I found no regression; it prevents a negative poll result from being treated as a completed connect. Current exact-head checks are fully terminal: 9 success, 6 intentional workflow-design skips, 0 failures, 0 pending, including a successful Windows job for the descriptor-passing implementation. No blocking findings on the current 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!25791
No description provided.