Skip to content

feat(libkernel): pass descriptors in SCM_RIGHTS messages on Unix sockets - #7090

Merged
oneandonlydean merged 16 commits into
boykopovar:mainfrom
milescoviello:feat/scm-rights
Nov 1, 2026
Merged

oneandonlydean merged 16 commits into
boykopovar:mainfrom
milescoviello:feat/scm-rights

Conversation

@milescoviello

@milescoviello milescoviello commented Oct 17, 2026 •

Copy link
Copy Markdown
Contributor

What

sendmsg threw control data is not supported for any control buffer. Wine passes descriptors to and from its wineserver this way, so Forever Launcher's in-process Wine (PPSA99030) stops on it. On a guest Unix-domain socket, SCM_RIGHTS control messages now pass descriptors, as FreeBSD 11's unp_internalize and unp_externalize do.

  • sendmsg: reads FreeBSD's layout: a 12-byte cmsghdr with the descriptors at offset 16, and records CMSG_SPACE bytes apart. It sends the descriptors with the host's SCM_RIGHTS, because a guest descriptor here is a host descriptor (a guest socket's number is a duplicate of its host socket).
    • It returns EBADF for a descriptor that is not open, and EINVAL for a level other than SOL_SOCKET or a record longer than the buffer.
  • recvmsg with a control buffer: receives with the host's SCM_RIGHTS. A received socket is installed as a guest socket (family from getsockname(), type from SO_TYPE); any other descriptor is used as it is.
    • It writes FreeBSD's record, CMSG_LEN in cmsg_len and CMSG_SPACE bytes, when the whole record fits the caller's buffer. Otherwise it copies nothing, sets controlLength 0 and MSG_CTRUNC, and closes the passed descriptors. That is what 11.4's kern_recvit does: it copies only control mbufs that fit whole and disposes the rest with m_dispose_extcontrolm. A fault writing the record, the data or the address also closes them and returns EFAULT, as kern_recvit's out: path disposes of the control descriptors whenever it returns an error.
    • MSG_CMSG_CLOEXEC marks the received descriptors close-on-exec. Where the host has no MSG_CMSG_CLOEXEC (macOS), each received descriptor gets fcntl(F_SETFD, FD_CLOEXEC) before it is installed.
  • Ownership: received descriptors belong to the receive until the whole result, final msghdr write included, is written back. Any earlier return closes each of them exactly once, including a failure part-way through installing them.
  • MSG_PEEK with control space reads only the data, as on main, and leaves any passed descriptors queued.
  • Refused descriptors (b9e987b82e, from XairRrR's review): a guest kqueue returns EOPNOTSUPP, as in FreeBSD's unp_internalize. A random device, a directory or a JIT shared memory descriptor, whose state is libkernel's, throws, as do archived files. So does a zero-byte stream send that carries descriptors, since Linux sends no record without data.
  • Still throws: Windows (its socket pairs are loopback TCP), control messages other than SCM_RIGHTS, and control data on IP sockets. The debt entry says so.

Overlap: #10767 and #11223 (Ragbie) change the same receive condition in recvmsg (#10767 adds the receive-shut check, this PR adds the SCM_RIGHTS setup above it) and add neighbouring entries to docs/dev/debt/libkernel.md. Agreed order: whichever lands second resolves the one-line conflict and keeps both changes.

Tested

  • This head (1e19b327f7, XairRrR's second review): main 52c09d567d merged in, with the kqueue check renamed to IsKqueueDescriptor_nid_no_patch (fix(libkernel_sys): fail fpathconf on a kqueue or shm descriptor with EOPNOTSUPP #11296). A JIT shared memory descriptor (IsJitSharedMemory) now throws too. The 19 tests matching ^guest_socket|^guest_recvmsg|^guest_udp|^guest_tcp|^guest_jit pass 3 of 3.

  • This head (b9e987b82e, XairRrR's review): the 18 tests matching ^guest_socket|^guest_recvmsg|^guest_udp|^guest_tcp pass 3 of 3. The new kqueue case in guest_socket_rights requires -1/errno 45 and that the next plain send arrives with no control data. With the kqueue check taken out (6966f73e71), the test aborts on it. Windows 11 VM (GT 1030, WinLibs GCC 15.2): of the 31 tests matching the same regex, 28 pass 3 of 3 on the merge base f759e165d9 and on this head. guest_socket_options, guest_socket_options_multicast and guest_socket_stream_options fail the same way on both (0xc0000409).

  • Review rework (1db8d77c9a, main ef90ce0452 merged in), after oneandonlydean's review: ReceivedRights owns the received descriptors until the final header write. The old rollback could close a descriptor twice: after a failed Install the Socket had already closed it, and the loop closed it again. Plain MSG_PEEK works again. The test includes prx/libkernel/include/Api.hpp and prx/libc/include/Api.hpp (the public_headers audit passes) and counts host descriptors with fcntl(F_GETFD) instead of /proc. New cases:

    • a read-only msghdr whose final header write faults after the data and control copies succeeded: EFAULT with the host descriptor count unchanged;
    • a Unix socket and an AF_ROUTE socket passed together in both orders, where installing the route socket fails: the host descriptor count is unchanged;
    • a plain-data MSG_PEEK with control space, which returns the data, and then a read of the same bytes.

    On Linux (Ubuntu 26.04.1, GCC 15.2, Release), guest_socket_rights, guest_socket_pair, guest_recvmsg_memory, guest_udp and guest_tcp pass 3 of 3. With the previous Udp.cpp under the new test, guest_socket_rights aborts. On the Windows 11 VM (WinLibs GCC 15.2, Release), the four of them that build on Windows pass 3 of 3 at this head and at main f759e165d9.

  • Merged main 1e740a2 into the branch (04eeeb9d11), then added 484bc8c2c3. main had changed recvmsg in fix(libkernel): answer zero-length datagram receives and recvmsg flags as measured on a PS5 #10308: the host receive runs only for data, a stream socket or a readable one, and msg_flags starts from the call's flags as measured on a PS5. The descriptor-passing receive now sits inside that condition, and the two-socket case of guest_socket_rights now expects MSG_CMSG_CLOEXEC back in msg_flags (it expected 0, and failed 3 of 3 against fix(libkernel): answer zero-length datagram receives and recvmsg flags as measured on a PS5 #10308). On Linux (Ubuntu 26.04.1, GCC 15.2), the 14 guest_socket* tests pass 3 of 3 on 484bc8c2c3, and the 649 tests matching ^guest_|libc passed 3 of 3 on 04eeeb9d11 apart from that case.

  • Title run on 484bc8c2c3 (by @babyhuey, Linux CI image with lavapipe, Ubuntu 24.04, GCC 13, Release): the 14 guest_socket* tests pass, and Forever Launcher (PPSA99030) is still running at the 60 s timeout in 2 of 2 runs, with no sendmsg: control data is not supported, what() or FATAL (main f3456a4 stops at that sendmsg line).
    Ubuntu 24.04, GCC 13.3, Release. The results below are from main 673f740, except the rebase line.

  • On current main (run by @babyhuey, Linux CI image with lavapipe, Ubuntu 24.04, GCC 13, Release): with the other PRs below merged, main f3456a4 still stops Forever Launcher with sendmsg: control data is not supported. With this head (1142d3a6bc, clean merge) the 14 guest_socket* tests pass and the launcher runs the full 60 s in 2 of 2 runs, ending on the same first-start Wine prefix result described here.

  • Data and address faults (516263f), also found by @wachi35676: a fault writing the data buffer or the address after the descriptors were installed left them open. They are now closed on those returns too. New guest_socket_rights case: two pipe ends are passed and received into a read-only data buffer, and the receive must return EFAULT with the count in /proc/self/fd unchanged. It aborts with the previous Udp.cpp and passes with the fix, and the 12 guest_socket_* tests pass 3 of 3. The debt entry also says now that several SCM_RIGHTS messages in one sendmsg arrive as one record. @wachi35676 re-checked 516263f8f6 merged onto main a020d02: the 14 guest_socket_* tests pass 3 of 3, reverting only the Udp.cpp hunks makes the faulted-receive case abort, and their probe now leaves no descriptor open after a fault on the data buffer. The Windows build of Udp.cpp and libkernel succeeds on the same merge.

  • Truncation fixed (223a7c1), after @wachi35676 found that a short control buffer leaked the passed descriptors. With the old record copy and the new test, guest_socket_rights aborts. With the fix, the 12 guest_socket_* tests pass 3 of 3, and ctest -R '^guest_|libc' passes 475 of 476. guest_module_directories (a relinker test) fails the same way on main in that tree.

  • Rebase onto main bf2c63f (5f492d3). recvmsg on main now reads the guest's msghdr and iovecs into a copy and writes its results back with ReadGuest/WriteGuest. The control record is written the same way: into the copied control pointer, with controlLength and MSG_CTRUNC in the copied header that is written back. guest_socket_* (12 tests, guest_socket_rights included) pass 3 of 3. ctest -R '^guest_|libc': 474 of 476 pass. guest_bad_optional_access was not built in that tree, and guest_module_directories (a relinker test) fails the same way with main's sources.

  • New guest_socket_rights test (Linux), over a stream socket pair:

    • It passes a pipe's write end, writes through the received descriptor and reads it from the pipe. The received descriptor is not close-on-exec.
    • It passes the ends of a stream and a datagram socket pair in one record with MSG_CMSG_CLOEXEC, and checks that both received sockets are close-on-exec and send and receive.
    • It passes another pipe end with MSG_CMSG_CLOEXEC and checks that the received host descriptor is close-on-exec. A pipe end goes through unchanged, so this reads the host flag, while the sockets' check reads the guest's close-on-exec set.
    • A control buffer of 16 bytes, too small for the 24-byte record, gets controlLength 0 and MSG_CTRUNC. So does a record of two descriptors with room for one (20 bytes), and in both cases the count in /proc/self/fd is unchanged, so no descriptor leaks. A receive without a control buffer gets the data only.
    • A closed descriptor gives EBADF, and another level or an overlong record gives EINVAL.
    • With main's Udp.cpp it aborts on sendmsg: control data is not supported. It passes 3 of 3, with guest_socket_pair too.
  • ctest -R '^guest_|libc': 402 of 402 pass.

  • Forever Launcher (PPSA99030, 01.000.012), in gdb: Wine's first such call is sendmsg on a socket-pair end with MSG_NOSIGNAL | MSG_DONTWAIT, one byte of data and a 24-byte control buffer holding one SOL_SOCKET/SCM_RIGHTS record (cmsg_len 20) that passes the end of another socket pair.

  • @p3n1cillin checked current main with two catalog titles that run Wine with an in-process wineserver: Forever Launcher (PPSA99030, 01.000.021) and RuneScape Launcher (PPSA99225, 01.000.021). Setup: Ubuntu on WSL2, GCC 15.2.0, Release, llvmpipe on Xvfb, 120 s without input.

    • On main 7fea87746 both abort on sendmsg: control data is not supported.
    • With this PR merged onto main 319621ba2e (4fa9ea8a0f, clean), neither stops there. Both start their wineserver, and Wine runs and shuts down in order.
    • Both then stop on their own first-start prefix setup ("Setting up the Windows files did not finish"), as above.
    • The five scm|rights|unix|sendmsg|recvmsg ctests pass on the merge.
  • Windows build: @wachi35676 found that Udp.cpp did not compile there: GuestControlCloseOnExec was declared inside the non-Windows block. 17d9e0a moves the declaration out of it. A MinGW-w64 -fsyntax-only compile of Udp.cpp stops on that line before and passes after, and the 11 socket tests and 411 ^guest_ tests pass again on Linux. @screamm then built head 17d9e0a516 on Windows (Windows 11, WinLibs GCC 15.2.0, full build and libs). Moving the declaration back reproduces the compile error, and the 35 Windows socket, poll, select, rudp, equeue and kqueue tests pass 3 of 3. On Windows, sendmsg with an SCM_RIGHTS buffer still stops with control data is not supported on Windows, as this PR intends. None of their local titles sends or receives such a message.

  • Windows VM (Windows 11, WinLibs GCC 15.2.0, Release), head 1142d3a and its merge base eaff876: the 13 guest_socket_* tests that build on Windows (address length, connect to a listener, descriptor flags, descriptors, errors, events, non-socket descriptors, options, pairs, port 0, read/write, stream options, timeouts) pass 3 of 3 on both. SCM_RIGHTS still throws on Windows, as described above, so this checks that the change leaves the rest of the Windows socket layer as it was.

  • macOS: @jotitadev found that SO_DOMAIN and MSG_CMSG_CLOEXEC don't exist in XNU's headers, so Udp.cpp didn't compile on top of feat: macOS on Apple Silicon host support (Rosetta 2 and MoltenVK) #7332. aff8c2a takes the received socket's family from getsockname(). Where MSG_CMSG_CLOEXEC doesn't exist, it marks each received descriptor with fcntl(F_SETFD, FD_CLOEXEC) when the guest asked for it. Their local equivalent compiled with -Wall -Wextra on arm64 and x86_64, and guest_socket_rights passed 5 of 5 under Rosetta. They also found that the test didn't check the host flag, so a3b3801 adds the pipe-end case above. On Linux the head passes 3 of 3, and so does a build with the receive flag forced to 0, through the fcntl path. With the flag forced to 0 and the fcntl loop removed, the test fails. ^guest_|libc passed 411 of 411 on aff8c2a. @jotitadev then checked head a3b3801aa6 on feat: macOS on Apple Silicon host support (Rosetta 2 and MoltenVK) #7332 (57bccfb, M2 Max, macOS 27.2, Apple clang 21.0.0): Udp.cpp compiles with -Wall -Wextra on arm64 and x86_64 with no warnings of its own, through the fcntl fallback. Under Rosetta guest_socket_rights passes 5 of 5, and with the fcntl loop removed it fails 3 of 3 on the new pipe check. The 20 socket, UDP, RUDP, poll and select tests pass, except an intermittent ECONNRESET in guest_socket_read_write that also happens on feat: macOS on Apple Silicon host support (Rosetta 2 and MoltenVK) #7332 alone.

Checklist

🤖 Generated with Claude Code

@github-actions

github-actions Bot commented Oct 17, 2026 •

Copy link
Copy Markdown

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
PR 8463 core/libs/prx/libkernel/Socket/src/Udp.cpp L1756, L1760, L1768-1772
PR 11223 docs/dev/debt/libkernel.md L266

Some comparisons could not be completed because a pull request conflicts with the base branch. Review these shared files manually before concluding that there is no overlap.

Pull request Shared files not compared
PR 4563 core/libs/prx/libkernel/CMakeLists.txt
PR 4630 docs/dev/debt/libkernel.md
PR 5021 core/libs/prx/libkernel/CMakeLists.txt
core/libs/prx/libkernel/DirectMemory/Jit.cpp
core/libs/prx/libkernel/Socket/src/Udp.cpp
docs/dev/debt/libkernel.md
PR 6936 core/libs/prx/libkernel/CMakeLists.txt
docs/dev/debt/libkernel.md
PR 7088 core/libs/prx/libkernel/Socket/src/Udp.cpp
docs/dev/debt/libkernel.md
PR 7105 core/libs/prx/libkernel/DirectMemory/DirectMemory.hpp
docs/dev/debt/libkernel.md
PR 7121 docs/dev/debt/libkernel.md
PR 7135 docs/dev/debt/libkernel.md
PR 7149 core/libs/prx/libkernel/DirectMemory/DirectMemory.hpp
docs/dev/debt/libkernel.md
PR 7192 core/libs/prx/libkernel/CMakeLists.txt
docs/dev/debt/libkernel.md
PR 7447 docs/dev/debt/libkernel.md
PR 7464 docs/dev/debt/libkernel.md
PR 7507 core/libs/prx/libkernel/Socket/src/Udp.cpp
PR 7509 docs/dev/debt/libkernel.md
PR 7804 docs/dev/debt/libkernel.md
PR 8477 core/libs/prx/libkernel/CMakeLists.txt
PR 9156 core/libs/prx/libkernel/CMakeLists.txt
docs/dev/debt/libkernel.md
PR 9199 docs/dev/debt/libkernel.md
PR 9293 core/libs/prx/libkernel/CMakeLists.txt
docs/dev/debt/libkernel.md
PR 9297 core/libs/prx/libkernel/CMakeLists.txt
docs/dev/debt/libkernel.md
PR 9302 core/libs/prx/libkernel/CMakeLists.txt
docs/dev/debt/libkernel.md
PR 9326 core/libs/prx/libkernel/CMakeLists.txt
docs/dev/debt/libkernel.md
PR 9383 core/libs/prx/libkernel/CMakeLists.txt
PR 9389 core/libs/prx/libkernel/CMakeLists.txt
docs/dev/debt/libkernel.md
PR 9557 core/libs/prx/libkernel/CMakeLists.txt
docs/dev/debt/libkernel.md
PR 9566 core/libs/prx/libkernel/CMakeLists.txt
docs/dev/debt/libkernel.md
PR 9843 docs/dev/debt/libkernel.md
PR 9938 core/libs/prx/libkernel/CMakeLists.txt
docs/dev/debt/libkernel.md
PR 10025 core/libs/prx/libkernel/DirectMemory/DirectMemory.hpp
docs/dev/debt/libkernel.md
PR 10319 docs/dev/debt/libkernel.md
PR 10442 docs/dev/debt/libkernel.md
PR 11624 docs/dev/debt/libkernel.md
PR 11628 docs/dev/debt/libkernel.md
PR 11661 docs/dev/debt/libkernel.md
PR 11689 docs/dev/debt/libkernel.md
PR 11698 docs/dev/debt/libkernel.md
PR 11831 core/libs/prx/libkernel/CMakeLists.txt
PR 11896 core/libs/prx/libkernel/CMakeLists.txt
core/libs/prx/libkernel/Socket/src/Udp.cpp
docs/dev/debt/libkernel.md
PR 11939 core/libs/prx/libkernel/CMakeLists.txt
docs/dev/debt/libkernel.md
PR 12544 core/libs/prx/libkernel/CMakeLists.txt
PR 12560 docs/dev/debt/libkernel.md
PR 12682 core/libs/prx/libkernel/CMakeLists.txt
core/libs/prx/libkernel/Socket/src/Udp.cpp
docs/dev/debt/libkernel.md
PR 12688 core/libs/prx/libkernel/CMakeLists.txt
core/libs/prx/libkernel/Socket/src/Udp.cpp
docs/dev/debt/libkernel.md
PR 12700 core/libs/prx/libkernel/CMakeLists.txt
core/libs/prx/libkernel/Socket/src/Udp.cpp
docs/dev/debt/libkernel.md
PR 12826 core/libs/prx/libkernel/CMakeLists.txt
docs/dev/debt/libkernel.md
PR 12965 core/libs/prx/libkernel/CMakeLists.txt
core/libs/prx/libkernel/Socket/src/Udp.cpp
docs/dev/debt/libkernel.md
PR 14108 docs/dev/debt/libkernel.md
PR 14111 core/libs/prx/libkernel/CMakeLists.txt
docs/dev/debt/libkernel.md
PR 14128 docs/dev/debt/libkernel.md
PR 14212 docs/dev/debt/libkernel.md

@wachi35676

Copy link
Copy Markdown
Contributor

This doesn't compile on Windows (head 4cd70d4ae; WinLibs GCC 15.2.0, Ninja, Release), alone or merged onto main 11acfdd84:

core/libs/prx/libkernel/Socket/src/Udp.cpp:1371:51: error: 'GuestControlCloseOnExec' was not declared in this scope

GuestControlCloseOnExec is declared at line 1244, inside the #ifndef _WIN32 block that runs from line 1240 to 1328. The flag check at line 1371 in recvmsg_nid_postfix, (passing ? GuestControlCloseOnExec : 0), is outside any guard. The CI windows job was skipped for this PR, so it didn't show. Declaring the constant above the #ifndef _WIN32 fixes the build here.

AI-assisted: yes (Claude Code); results above were run on this machine.

@milescoviello

Copy link
Copy Markdown
Contributor Author

Thanks @wachi35676, that's right. 17d9e0a declares GuestControlCloseOnExec above the #ifndef _WIN32 block, as you suggested; recvmsg masks the flag on every host.

Checked here with MinGW-w64 (x86_64-w64-mingw32-g++-posix): a -fsyntax-only compile of Udp.cpp stops on exactly your line at 4cd70d4, and passes with 17d9e0a under -Wall -Wextra with no new warnings. On Linux (GCC 13.3, Release) the 11 socket tests pass 3 of 3, and the 411 ^guest_ tests pass. I haven't run a full Windows build.

AI-assisted: yes (Claude Code)

@screamm

screamm commented Oct 18, 2026

Copy link
Copy Markdown
Contributor

Windows build of head 17d9e0a51, merged onto main 0d078f75d together with #7492 (fd2eb3521) and #7509 (c170bba42); all three merge cleanly.

  • Host: Windows 11 Pro 26220, Ryzen 7 5700X, RTX 4070 (driver 596.21), WinLibs MinGW-w64 GCC 15.2.0 r7, Ninja, Release, BUILD_TESTING=ON.
  • A real full build and libs both pass (not a syntax-only compile). Udp.cpp builds with no warning, and g++ -Wall -Wextra -fsyntax-only of it with the project's flags is clean.
  • Control for the fix: moving GuestControlCloseOnExec back inside #ifndef _WIN32 reproduces error: 'GuestControlCloseOnExec' was not declared in this scope at the recvmsg flag check (1391:51 in my copy, the same expression @wachi35676 reported at 1371:51); with the declaration above the block it compiles.
  • guest_socket_rights is registered only if(NOT WIN32), so on Windows it neither runs nor skips; ctest does not list it. The 35 Windows socket, poll, select, rudp, equeue and kqueue tests pass 3x on a quiet host (105/105).
  • Windows behaviour with a scratch probe (not part of the PR): on a socketpair, sendmsg without control and recvmsg with a control buffer behave as before (data, controlLength 0, flags 0), recvmsg with MSG_CMSG_CLOEXEC still fails with 45, and sendmsg with a one-descriptor SCM_RIGHTS buffer ends in terminate with sendmsg: control data is not supported on Windows. With Udp.cpp from main the probe differs only in the message text, so the 32 socket tests cannot tell the two apart on Windows.
  • Non-GPU suite (ctest -j8 --timeout 120 -E '^agc_driver_|^video_out_flip'): 936 tests, 929 pass, 2 skipped, 5 load timeouts that pass serially. tools/check_conventions.py --base origin/main and git diff origin/main --check pass.

Titles: PPSA99030 is not available here. recvmsg is imported by 13 and sendmsg by 12 local titles. I ran PPSA74126 (Stremio Plus) and PPSA99082 (OpenNOW) for 45 s each, and PPSA99169 (RetroArch, app.exe only; its picker child's output is not captured), with a local diagnostic libkernel (reverted) that logged every sendmsg and recvmsg call with a control buffer. It logged none, so I have no local title that sends or receives an SCM_RIGHTS message.

Not checked: the descriptor passing itself (Linux only, guest_socket_rights not run), Linux, a console, PPSA99030.

Written with Claude Code assistance (validation by a subagent, reviewed in this session).

@screamm screamm mentioned this pull request Oct 18, 2026
7 tasks done
@milescoviello

Copy link
Copy Markdown
Contributor Author

Thanks @screamm for the full Windows build, the control and the probe. I added it to the description.

AI-assisted: yes (Claude Code)

@github-actions github-actions Bot added the unblocks title Names a title ID on the Unblocks line label Oct 18, 2026

@jotitadev jotitadev left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

macOS build note on head 17d9e0a51.

The new code sits under #ifndef _WIN32, so it is also compiled on macOS hosts, but two of its identifiers do not exist in XNU's headers:

  • core/libs/prx/libkernel/Socket/src/Udp.cpp:1315: getsockopt(native, SOL_SOCKET, SO_DOMAIN, ...). macOS has no SO_DOMAIN.
  • core/libs/prx/libkernel/Socket/src/Udp.cpp:1396: MSG_CMSG_CLOEXEC. macOS has no such recv flag.

Repro on an M2 Max (macOS 27.2, Apple clang 21.0.0, arm64): merging this head onto #7332 (b182750ca, the PR that builds libkernel on macOS) is conflict-free. A -fsyntax-only compile of the merged Udp.cpp then fails with exactly these two errors:

Udp.cpp:1421:42: error: use of undeclared identifier 'SO_DOMAIN'
Udp.cpp:1502:122: error: use of undeclared identifier 'MSG_CMSG_CLOEXEC'

The same file from #7332 alone compiles cleanly. The line numbers above are in the merged file; the lines in this PR are 1315 and 1396. guest_socket_rights is registered under if(NOT WIN32), so it would be built on macOS too.

Suggestion that keeps one path for both Unix hosts:

  • Take the family from getsockname() (sockaddr_storage::ss_family) instead of SO_DOMAIN. On macOS this returns AF_UNIX for a socketpair end and AF_INET6 for an unbound IPv6 socket, and SO_TYPE works as it is.
  • Drop MSG_CMSG_CLOEXEC and set FD_CLOEXEC with fcntl on each received descriptor when the guest asked for it, before InstallPassed. Alternatively, guard the flag with #ifdef MSG_CMSG_CLOEXEC and use the fcntl path otherwise.

Not checked: running guest_socket_rights on macOS. The full libs build on macOS needs #7332 and an x86-64 build, which I could not run here.

AI-assisted: yes (Claude Code)

@milescoviello

Copy link
Copy Markdown
Contributor Author

@jotitadev thanks, both are right. I took your first suggestion and the #ifdef variant of the second, in aff8c2a:

  • The received socket's family comes from getsockname() (ss_family) instead of SO_DOMAIN. SO_TYPE is unchanged.
  • Where MSG_CMSG_CLOEXEC exists, recvmsg still sets close-on-exec atomically, so Linux behaves exactly as tested. Elsewhere each received descriptor gets fcntl(F_SETFD, FD_CLOEXEC) right after recvmsg when the guest asked for it, before InstallPassed. I kept the atomic flag where it exists because the fcntl path leaves a window in which another thread's spawn could inherit the descriptor.

I'm running guest_socket_rights on Linux, plus once with the fallback forced so the fcntl path runs too, and the ^guest_|libc set. I'll push when those pass. I can't build for macOS here, so a -fsyntax-only check of the pushed head on top of #7332 would be welcome if you have time.

AI-assisted: yes (Claude Code)

@jotitadev

Copy link
Copy Markdown
Contributor

@milescoviello thanks. feat/scm-rights is still at 17d9e0a51, and aff8c2a isn't on GitHub yet, so I checked the current head plus a local equivalent of what you described. I'll re-run on the pushed head.

M2 Max, macOS 27.2, Apple clang 21.0.0. Base: #7332 at 4a1c11a91 (the PR merges onto it without conflicts).

One gap in the test: it never checks close-on-exec on the received host descriptor. The only ControlCloseOnExec case receives two sockets. For a guest socket, F_GETFD reads closeOnExecDescriptors, which InstallPassed fills, and not the host fd. So the check passes when nothing sets the host flag:

  • macOS, local equivalent with the fcntl loop removed: guest_socket_rights passes 3 of 3.
  • Linux (Ubuntu 26.04, GCC 15.2, Release), 17d9e0a51 with MSG_CMSG_CLOEXEC removed from the recvmsg call: it also passes 3 of 3.

So your forced-fallback run on Linux will pass whether or not the fcntl path runs. One way to cover both paths is to receive a passed pipe end with ControlCloseOnExec and require fcntl_nid_postfix(writer, GetDescriptorFlags) == 1. A pipe goes through InstallPassed unchanged, so the guest sees the host flag. With that check, both mutations fail (passed pipe without FD_CLOEXEC), and the unmodified code passes on macOS (with the fallback) and on Linux.

Not checked: your actual aff8c2a, arm64 runtime (the libs build is x86-64 only on macOS).

Written with AI assistance (Claude Code). I ran the builds and tests above on my machine.

@milescoviello

Copy link
Copy Markdown
Contributor Author

@jotitadev thanks, and sorry the commit wasn't up yet when you looked. Both are pushed now:

  • aff8c2a is the fix as described: getsockname() for the family, MSG_CMSG_CLOEXEC where it exists, otherwise fcntl(F_SETFD, FD_CLOEXEC) per received descriptor.
  • a3b3801 is the check you suggested. CheckPassedPipe now also passes a pipe end with ControlCloseOnExec and requires F_GETFD == 1 on it.

You're right that my forced-fallback run proved nothing before that check. With it, on Linux (Ubuntu 24.04, GCC 13.3):

  • The head passes 3 of 3.
  • With the receive flag forced to 0, it passes through the fcntl path.
  • With the flag forced to 0 and the fcntl loop removed, it fails.
  • The ^guest_|libc set passed 411 of 411 on aff8c2a.

A run of -fsyntax-only on a3b3801 over #7332 would confirm the macOS side whenever you get to it.

AI-assisted: yes (Claude Code)

@jotitadev

Copy link
Copy Markdown
Contributor

@milescoviello re-ran on the pushed head a3b3801aa. The macOS side is fine.

M2 Max, macOS 27.2, Apple clang 21.0.0. Base: #7332 at 57bccfbfc. This PR merges onto it with no conflicts.

  • Udp.cpp -fsyntax-only -Wall -Wextra, arm64 and x86_64: 0 errors, and no warnings in Udp.cpp. The only warnings come from existing headers (-Wreturn-type-c-linkage in Equeue.hpp and General.hpp). On macOS, MSG_CMSG_CLOEXEC is undefined, so the fcntl fallback is the path that gets compiled.
  • Real x86_64 build, run under Rosetta: guest_socket_rights passes 5 of 5.
  • The 20 guest_socket_*, guest_udp, guest_rudp_*, guest_poll and guest_select* tests passed in 2 of 3 runs. In the third, guest_socket_read_write aborted at line 154 with errno 54 (ECONNRESET) and passed on re-run. Run alone, it fails about once in 10 to 30 tries. It was already intermittent on feat: macOS on Apple Silicon host support (Rosetta 2 and MoltenVK) #7332 without this PR in my previous run.
  • Mutation: I removed the fcntl(F_SETFD, FD_CLOEXEC) loop. guest_socket_rights now fails 3 of 3. It aborts on the new pipe check: with a temporary print, F_GETFD on the received pipe end reads 0 with the mutation and 1 without it. So the test now covers the macOS path.

Not checked: arm64 runtime, because the libs build is x86-64 only on macOS.

Written with AI assistance (Claude Code). I ran the builds and tests above on my machine.

@milescoviello

Copy link
Copy Markdown
Contributor Author

@jotitadev thanks, that settles the macOS side. The mutation result is the useful part: it shows the new pipe check actually exercises the fcntl path. I've added your results to the PR body.

About the guest_socket_read_write abort at line 154: that loop keeps writing to the server end after the client closed. The first write succeeds, the client's stack answers with an RST, and a later write fails. Linux and FreeBSD report a reset that arrives in CLOSE_WAIT as EPIPE, which is what the test expects. I believe XNU reports it as ECONNRESET in that state, and which of the two a write sees depends on whether the RST is processed before or during that write. That would explain 1 failure in 10 to 30 runs and no failures on Linux. If that's right, it belongs with #7332 (accept 54 alongside 32 on Apple at that line), not in this PR. I haven't verified it on a Mac.

AI-assisted: yes (Claude Code)

@adriadam10 adriadam10 self-assigned this Oct 19, 2026
@github-actions github-actions Bot added the needs rebase Conflicts with main, rebase needed label Oct 19, 2026
@milescoviello

Copy link
Copy Markdown
Contributor Author

Rebased onto main bf2c63f (5f492d3). recvmsg on main now works on a copy of the guest's msghdr and writes its results back with WriteGuest. The SCM_RIGHTS record now goes through the same path: it is written to the copied control pointer, and controlLength and MSG_CTRUNC go into the header copy that is written back. guest_socket_* pass 3 of 3 (12 tests); details are in the description.

AI-assisted: yes (Claude Code)

@github-actions github-actions Bot removed the needs rebase Conflicts with main, rebase needed label Oct 19, 2026
@wachi35676

Copy link
Copy Markdown
Contributor

Re-run of head 5f492d3 merged onto current main 0929be5 (clean).

Linux: WSL Ubuntu 24.04, GCC 13.3, Release, BUILD_TESTING=ON.

  • guest_socket_* (12 tests, guest_socket_rights included) pass 3/3.
  • Negative control (Udp.cpp from main, tests kept): guest_socket_rights aborts, and the other 11 pass.

Windows: Windows 11 Home 26300, WinLibs GCC 15.2.0, Release.

  • It builds.
  • guest_socket_* (25 tests; guest_socket_rights is not registered on Windows) plus guest_kqueue and guest_select pass 3/3.
  • With Udp.cpp from main the 25 also pass, as expected, since the Windows path only throws.

Truncation. A probe sends one SCM_RIGHTS record with two pipe write ends (record 24 bytes). It receives with different control capacities and counts /proc/self/fd before and after the receive:

2 fds sent (record 24 bytes), control capacity 16: bytes=1 controlLength=16 MSG_CTRUNC=1 cmsg_len=24, process fds after-before=2
2 fds sent (record 24 bytes), control capacity 20: bytes=1 controlLength=20 MSG_CTRUNC=1 cmsg_len=24, process fds after-before=2
2 fds sent (record 24 bytes), control capacity 24: bytes=1 controlLength=24 MSG_CTRUNC=0 cmsg_len=24, process fds after-before=2
2 fds sent (record 24 bytes), control capacity 32: bytes=1 controlLength=24 MSG_CTRUNC=0 cmsg_len=24, process fds after-before=2
  • MSG_CTRUNC is set correctly through the copied header.
  • With a short buffer, both received descriptors stay open, but the guest is told about none of them (capacity 16) or one (capacity 20). Two descriptors leak at capacity 16 and one at capacity 20.
  • FreeBSD 11.4 handles this differently in kern_recvit:
    • it copies only control mbufs that fit whole (len >= m->m_len);
    • it sets MSG_CTRUNC and calls m_dispose_extcontrolm(m) on the rest, which closes the descriptors that didn't fit.
    • So for this record it returns controlLength 0, MSG_CTRUNC, and no new descriptors open. That is not "truncated to the caller's buffer" with the header copied, which is what the description and the 16-byte test case expect.
  • A fix could:
    • close (or not install) the descriptors of a record that doesn't fit whole;
    • copy only whole records;
    • update the 16-byte test case to expect controlLength == 0.

AI-assisted: yes (Claude Code); results above were run on this machine.

@milescoviello

Copy link
Copy Markdown
Contributor Author

@wachi35676 thanks, you're right, and the probe that counts /proc/self/fd shows the leak directly. I checked 11.4's kern_recvit (uipc_syscalls.c L1143-L1155). The loop copies only while a whole control mbuf fits (len >= m->m_len), and msg_controllen adds up only what it copied. Whatever is left gets MSG_CTRUNC and m_dispose_extcontrolm, which closes those descriptors. My description and the debt entry claimed the partial copy was what kern_recvit does, and that was wrong.

223a7c1 (not pushed yet) does it FreeBSD's way:

  • A record that doesn't fit is not copied. controlLength is 0, MSG_CTRUNC is set, and the descriptors it carried are closed: sockets through GuestSockets::Close, other descriptors with close.
  • A fault writing the record also closes them and returns EFAULT, as kern_recvit's error path does.
  • guest_socket_rights now expects controlLength 0 for the 16-byte buffer, and adds your case of two descriptors with room for one. Both check that the count in /proc/self/fd is unchanged.
  • The debt entry's sentence about the partial copy is gone.

Red/green runs and the wider ^guest_ set are queued behind the current batch. I'll push and update the description when they're green.

AI-assisted: yes (Claude Code)

# Conflicts:
#	core/libs/prx/libkernel/Socket/src/Udp.cpp
#	docs/dev/debt/libkernel.md
@milescoviello

Copy link
Copy Markdown
Contributor Author

Merged main d093ea9 into the branch (now 1aa584d11e), since #11141 landed with its SCM_BINTIME/SCM_TIMESTAMP change in recvmsg, as discussed above. Two conflicts, both resolved by keeping both sides:

  • Udp.cpp: main's new const bool shut = ReceiveShut(*socket); sits after this PR's passed-descriptor state, and the host receive (including the SCM_RIGHTS path) still runs only under main's !shut && (...) condition. fix(libkernel): report SCM_BINTIME and SCM_TIMESTAMP from recvmsg as measured on a PS5 #11141's timestamp block merged in without a conflict.
  • libkernel.md: main replaced the old "recv and recvfrom after shutdown(SHUT_RD)" entry with its new shutdown(SHUT_RD) one. I kept main's entry and this PR's sendmsg and recvmsg entry after it.

The 15 guest_socket* tests pass 3 of 3 on Linux at the merge.

AI-assisted: yes (Claude Code)

@github-actions github-actions Bot removed the needs rebase Conflicts with main, rebase needed label Oct 27, 2026
@Felipebmaitan

Copy link
Copy Markdown
Contributor

@milescoviello I am coordinating #11872 (POSIX read/receive cancellation, head bcf5df1) with this PR under CONTRIBUTING.md.

Proposed order: #11872 first, then #7090. Your recvmsg control-data branch and #11872 modify the same native receive. With #11872 first, the later SCM_RIGHTS integration should retain the POSIX Io/descriptor admission boundary, route the blocking recvmsg through the native interruption machinery, and keep ownership of received descriptors through failure/cancellation and guest-output copy. Combined checks should cover blocked cancellation and close/reuse as well as rights truncation/copy failure; I have not run the combined branch.

Would you agree to that order and record #11872 in your Depends on line for the ordering? If your PR needs to go first, please say so; I can handle the later conflict resolution on #11872 instead. This is a proposal awaiting your confirmation, not an existing agreement.

AI-assisted coordination.

@milescoviello

milescoviello commented Oct 27, 2026 •

Copy link
Copy Markdown
Contributor Author

@Felipebmaitan thanks for checking first. I'd like #7090 to go first, and I'll take your offer to handle the later conflict on #11872.

My reasons:

So I'll leave Depends on: none here.

When you rebase #11872 onto it, the code to keep is in Udp.cpp's recvmsg. A blocked receive owns no descriptors yet. Ownership starts only after the native recvmsg returns:

  • each received descriptor goes through InstallPassed;
  • if installing one fails, the rest are closed;
  • ClosePassed closes all of them when the copy to guest memory faults, or when the record doesn't fit the guest's control buffer (which also sets MSG_CTRUNC).

So a cancellation while blocked has nothing to release. Your interruption path can wrap the native call without touching the rights handling after it.

If it helps, I can run the combined branch with guest_socket_rights and guest_socket_pair, plus a Forever Launcher run, once it's up.

AI-assisted: yes (Claude Code)

@oneandonlydean

Copy link
Copy Markdown
Collaborator

@milescoviello thanks for this, the SCM_RIGHTS passing itself reads right. A few things before it can go in:

  1. Received descriptors can leak on one supported path. In Udp.cpp:1962-1963 the final copy of controlLength/flags back to the guest returns Fail(14) instead of going through the faulted cleanup. With a read-only msghdr (name = nullptr) but writable iov and control buffers, recvmsg consumes the SCM_RIGHTS, InstallPassed succeeds and the data/control copies succeed, then the final header write fails and every passed descriptor stays open behind an EFAULT. The existing CheckFaultedReceive case only covers a read-only iov buffer, which takes the earlier cleanup path. Could you keep ownership of the received descriptors until the whole result is committed, roll back exactly once (the truncation path already closes some, so watch for a double close), and add a final-header EFAULT test that checks the host fd count is unchanged?
  2. Installation can throw part-way (Udp.cpp:1660-1679), which leaves descriptors already installed and the rest still owned by the host. Please make that rollback safe too, with an injected installation-failure test.
  3. A plain-data MSG_PEEK on a Unix socket now throws when the caller supplies control space; on main it works. Please keep that path working.
  4. The new test declares 11 exports and types locally. Please include the canonical public headers instead (it fails public_headers otherwise), and make its fd counter portable: it's registered for every non-Windows platform but reads /proc.

Please keep the existing truncation/data/address cases and the explicit unsupported behaviour and debt as they are, without tests that pin unsupported forms to throw. Connected-socket state and composition with #11872 can stay as follow-ups.

@milescoviello

Copy link
Copy Markdown
Contributor Author

@oneandonlydean thanks for the careful read. I'll work through all four:

  1. Received descriptors stay owned by the receive until the whole result, final header included, is written back. They're closed exactly once on any later fault, and the truncation path's closes are folded into that one rollback. I'll add a final-header EFAULT test that checks the host fd count is unchanged.
  2. Installation becomes all-or-nothing, with a test that injects an installation failure part-way.
  3. A plain-data MSG_PEEK with control space on a Unix socket works again, as on main.
  4. The test will include the canonical public headers, and its fd counter will be portable instead of reading /proc.

The truncation, data and address cases and the unsupported behaviour and debt stay as they are. Connected-socket state and #11872 stay as follow-ups.

AI-assisted: yes (Claude Code)

milescoviello and others added 2 commits October 27, 2026 22:59
…s whole result

A fault on the final message header write, or an installation that fails
part-way, now closes every received descriptor exactly once. A plain-data
MSG_PEEK with control space on a Unix socket reads the data again, as on
main, and the test includes the public headers and counts host descriptors
portably.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@milescoviello

Copy link
Copy Markdown
Contributor Author

@oneandonlydean all four are in 1db8d77c9a, on main ef90ce0452, merged into the branch:

  1. Ownership until commit. recvmsg keeps every received descriptor in a ReceivedRights owner, as host descriptors before installation and guest numbers after it. They are released only after the final msghdr write succeeds. Any earlier return closes each one exactly once: a data, address, control or final-header fault, and an EFAULT or table-full error during installation. The truncation path closes them through the same owner, so there's no second close. New case: a read-only msghdr with name null and writable iov and control buffers. The data and control copies succeed, the final header write fails, and the receive returns EFAULT with the host descriptor count unchanged.
  2. Installation rollback. PassedSocket only queries the descriptor and builds the Socket, which then owns it. The old rollback could close a descriptor twice: after Install failed, the Socket had already closed it, and the loop closed it again. New case: a Unix socket and an AF_ROUTE socket are passed together in both orders, and libkernel refuses the route socket while installing. The test doesn't pin how the call fails, only that the host descriptor count is unchanged afterwards (or, if a future version installs both, that both close cleanly).
  3. MSG_PEEK with control space on a Unix socket takes the plain-data path again, as on main: it returns the data and no control data, and the next read gets the same bytes. The debt entry says so now instead of listing it as throwing.
  4. Headers. The test includes prx/libkernel/include/Api.hpp and prx/libc/include/Api.hpp and uses GuestMsghdr and KernelIovec. test_public_headers.py --audit-only passes. The descriptor count walks descriptors with fcntl(F_GETFD) instead of reading /proc.

Tested on Linux (Ubuntu 26.04.1, GCC 15.2, Release, BUILD_TESTING=ON): guest_socket_rights, guest_socket_pair, guest_recvmsg_memory, guest_udp and guest_tcp pass 3 of 3. With the previous Udp.cpp under the new test, guest_socket_rights aborts. On the Windows 11 VM (WinLibs GCC 15.2, Release), the four socket tests that build there pass 3 of 3 at this head and at main f759e165d9. The description has the details.

The truncation, data and address cases and the unsupported behaviour are as they were. Connected-socket state and #11872 stay follow-ups.

AI-assisted: yes (Claude Code)

@p3n1cillin

Copy link
Copy Markdown
Contributor

Two homebrew catalog titles that need this on Linux: bizkut's Wine-based Forever Launcher (PPSA99030, 01.000.021) and RuneScape Launcher (PPSA99225, 01.000.021). Both run Wine with an in-process wineserver, which passes descriptors to its clients with SCM_RIGHTS. On a Windows host neither gets that far: Wine needs 0x7ffe0000, which the host already owns.

Setup:

  • Ubuntu under WSL2, GCC 15.2.0, Release.
  • Rendering through llvmpipe on Xvfb, no audio device.
  • Each title converted from its catalog zip and run for 120 s with no input.
Build Forever Launcher RuneScape Launcher
main 7fea87746 aborts: sendmsg: control data is not supported aborts: sendmsg: control data is not supported
main 319621ba2e + this PR 1db8d77c9a no sendmsg stop no sendmsg stop

With this PR (merge 4fa9ea8a0f, clean):

  • Both start their private wineserver, and Wine runs and shuts down in order: private wineserver joined once; result=0, title clients quiesced; loader callbacks closed; initial thread joined.
  • Both then stop on their own first-start prefix setup, not on this PR: forever: Setting up the Windows files did not finish.
    • Forever Launcher's Wine exits with -1073741515 (0xC0000135, a DLL not found).
    • RuneScape's exits with 0.
    • I haven't looked into that next stop.
  • The five scm|rights|unix|sendmsg|recvmsg ctests pass on the merge.

Not tested: Windows (the titles can't start there), and a console.

AI-assisted: yes (Claude Code).

@milescoviello

Copy link
Copy Markdown
Contributor Author

@p3n1cillin thanks for running both launchers on current main. I've added your table to Tested and RuneScape Launcher (PPSA99225) to Unblocks. Forever Launcher's 0xC0000135 after the prefix setup matches what I saw on 98c251f: Wine can't find wineboot.exe, which the launcher's own setup should have put there.

AI-assisted: yes (Claude Code)

@XairRrR XairRrR left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Reviewed — changes requested

The SCM_RIGHTS transport, the receive-side ownership and the truncation and fault paths work in my runs, but sendmsg reports success for two inputs whose descriptors do not arrive intact: libkernel's emulated descriptors (kqueue, /dev/urandom) and a zero-byte send on a stream socket.

What I ran

  • PR head: 1db8d77c9afda0f719e2b4467cdc131843c95edd
  • main: b433c40f2f46aa2e59e19806010a839ac8572d64
  • Merged with git merge --no-ff in a clean worktree (no conflicts). Arch Linux 7.2.11-zen, GCC 16.2.1, Release, Ninja, Intel Core i9-14900KF.
git diff --check origin/main...refs/review/7090
python3 tools/check_conventions.py --base origin/main
cmake --build build -j10 --target libs guest_socket_rights_tests guest_socket_pair_tests guest_udp_tests guest_tcp_tests \
    guest_socket_read_write_tests guest_socket_descriptors_tests guest_socket_on_non_socket_tests guest_socket_descriptor_flags_tests
ctest --test-dir build -j4 --output-on-failure --repeat until-fail:3 \
    -R '^(guest_socket_rights|guest_socket_pair|guest_recvmsg_memory|guest_udp|guest_tcp|guest_socket_read_write|guest_socket_descriptors|guest_socket_on_non_socket|guest_socket_descriptor_flags)$'
# negative control
git checkout origin/main -- core/libs/prx/libkernel/Socket/src/Udp.cpp
cmake --build build -j10 --target guest_socket_rights_tests
ctest --test-dir build --output-on-failure -R '^guest_socket_rights$'
# probe: a temporary main() in GuestSocketRights.cpp (reverted afterwards), see Findings

Results

Check Result
Merge onto main clean
git diff --check clean
check_conventions.py All conventions checks passed.
Build (libs and the 8 socket test targets) passes
9 socket tests, 3 repeats 9 of 9 pass each time; guest_socket_rights takes 0.01 s
Negative control guest_socket_rights aborts
Probe: emulated descriptors and a zero-byte stream send the 3 cases below go wrong
Probe output on the merged tree
PROBE urandom: fd=9 direct read=8 sent=3 received=3 fd=10 read=0
PROBE kqueue: fd=11 kevent=0 sent=2 errno=0 received=2 controlLength=24 fd=12 kevent=-1 errno=9
PROBE zero-byte stream: sent=0 plain=1 received=1 controlLength=0 flags=0x0

Negative control

I restored only core/libs/prx/libkernel/Socket/src/Udp.cpp from main and kept the new test. guest_socket_rights aborts with terminate called after throwing an instance of 'std::runtime_error' / what(): sendmsg: control data is not supported. With the PR's Udp.cpp back, it passes.

Findings

Line numbers in Udp.cpp are at the PR head. The other libkernel files are linked at main.

Blocking

  1. sendmsg passes libkernel's placeholder descriptors as if they were the real ones. The description says "a guest descriptor here is a host descriptor", but that doesn't hold for every descriptor. A guest kqueue is a /dev/null placeholder plus an entry in g_kqueues (Kqueue.cpp L86-L92, L561-L572). A guest /dev/urandom is a /dev/null placeholder that libkernel fills on read (RandomDevice.cpp L28-L38). PassedDescriptors checks only that the host descriptor is open and that it isn't an archived file, so the placeholder is sent. The probe shows the result:

    • kqueue: sendmsg returns 2. The received descriptor fails kevent with EBADF, though the original returns 0. FreeBSD 11.4 refuses the send instead: unp_internalize returns EOPNOTSUPP for a file without DFLAG_PASSABLE (uipc_usrreq.c L2004-L2011), and kqueueops sets no fo_flags (kern_event.c L122-L135).
    • /dev/urandom (opened with sceKernelOpen): reading the original gives 8 bytes, but reading the received descriptor gives 0, an end of file.

    Both calls succeed and hand the receiver something else. CONTRIBUTING.md asks that "every function either does exactly what it is supposed to or throws". Suggested fix: in PassedDescriptors, return 45 (EOPNOTSUPP) for a kqueue descriptor, as FreeBSD does. Every other descriptor whose behaviour lives in a libkernel table (random device, and any other placeholder) should throw through NotImplemented_nid_no_patch, as RefuseArchivedFile already does for archived files. Name them in the debt entry, and add a guest_socket_rights case for the kqueue EOPNOTSUPP.

  2. A zero-byte sendmsg with SCM_RIGHTS on a stream socket returns 0 and drops the descriptors. SendPassing hands an empty iovec to the host sendmsg. Linux doesn't carry ancillary data on a SOCK_STREAM Unix socket without at least one data byte. In the probe the call returns 0, and the receive after a later one-byte plain send gets controlLength 0 and no MSG_CTRUNC, so the descriptor is gone and neither side is told. FreeBSD 11.4 delivers control-only sends: sosend_generic builds "a single empty mbuf" when "resid is 0, which can happen only if we have control to send" and calls pru_send with it (uipc_socket.c L1325-L1331). Suggested fix: when the socket is a stream, descriptors is not empty and data is empty, throw through NotImplemented_nid_no_patch and add the case to the debt entry, until there is a way to carry the record. Datagram and seqpacket sockets aren't affected.

Non-blocking

  1. @lgqdaxia reported that guest getpeername returns ENOTCONN on a received connected Unix socket. The debt entry at docs/dev/debt/libkernel.md:239 (PR head) mentions only default emulated socket options. I did not reproduce this. If it holds, one more sentence in that entry would cover it.

Not verified

  • macOS and Windows builds; I relied on the earlier reports in this thread for those.
  • The full test suite, and the other ^guest_|libc tests outside the 9 listed.
  • Any title: no game or homebrew was launched.
  • Whether RefuseArchivedFile also catches the /dev/null placeholder that DirectoryDescriptor.cpp opens for an archived directory.

To get this to a good state

  • Refuse emulated descriptors in PassedDescriptors: EOPNOTSUPP for kqueue, NotImplemented_nid_no_patch for the others. Add a test and a debt line.
  • Throw on a zero-byte stream sendmsg that carries descriptors, and list it in the debt entry.

…irectory in SCM_RIGHTS

A guest kqueue and a guest /dev/urandom are /dev/null placeholders with
their behaviour in libkernel's tables, and a guest directory descriptor
keeps its reading state there too. sendmsg sent the placeholder, and the
receiver got a descriptor that failed kevent or read as empty. FreeBSD's
unp_internalize returns EOPNOTSUPP for a kqueue (no DFLAG_PASSABLE), so a
kqueue now gets that; a random device or a directory throws.

A zero-byte sendmsg carrying descriptors on a stream socket returned 0
and lost them, as Linux sends no record without data. It throws now.
FreeBSD sends the control data alone there.

guest_socket_rights checks the kqueue refusal and that the next send on
the pair arrives without control data.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@milescoviello

Copy link
Copy Markdown
Contributor Author

@XairRrR thanks for the probe. Both are fixed in b9e987b82e:

  1. Placeholder descriptors. PassedDescriptors now checks what the guest descriptor is before it sends the host descriptor:
    • A guest kqueue returns EOPNOTSUPP (45), as FreeBSD's unp_internalize does for a file without DFLAG_PASSABLE.
    • A guest random device or a directory descriptor throws through NotImplemented_nid_no_patch. A directory keeps its reading state in libkernel's table, as you suspected for the archived-directory placeholder, so it's covered there too.
    • Archived files keep their existing RefuseArchivedFile throw.
    • libkernel's other placeholders (Kqueue.cpp, RandomDevice.cpp, ArchivedFile.cpp, DirectoryDescriptor.cpp) are those four kinds.
  2. Zero-byte stream send. A sendmsg with descriptors and no data on a stream socket now throws, until the record can be carried. Datagram and seqpacket sockets aren't affected.

The debt entry lists both. To fit the 1000-character limit, I tightened the rest of its wording without dropping anything.

guest_socket_rights has a new kqueue case. The send returns -1 with errno 45, and the next plain send on the pair arrives with no control data and no flags.

  • The 18 tests matching ^guest_socket|^guest_recvmsg|^guest_udp|^guest_tcp pass 3 of 3 at this head.
  • With the kqueue check taken out (6966f73e71), guest_socket_rights aborts on the new case (Subprocess aborted), one run.

On getpeername after receiving a connected Unix socket: I haven't reproduced it either, so I left the entry as it is.

AI-assisted: yes (Claude Code)

@github-actions

github-actions Bot commented Oct 29, 2026 •

Copy link
Copy Markdown

Progress report (2960e9a → 1e19b32)

📉 System libraries: 45.9% (-0.01%, -1 reverted functions)

⚠️ 1 went back to stubs
Library Name
libkernel sendmsg_nid_postfix

Changed libraries (implemented exports)

Library Before After Change
libkernel 693/737 (94.03%) 692/737 (93.89%) -0.14 pp

@XairRrR XairRrR left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Re-checked — changes still requested

b9e987b82e fixes both earlier blockers: a kqueue now gets EOPNOTSUPP, and a random device, a directory or a zero-byte stream send now throws. But the head no longer compiles on current main, and a JIT shared-memory descriptor is still passed as if it were complete.

What changed since my last review

  • One commit, b9e987b82e. It adds the kqueue, random-device and directory checks to PassedDescriptors and the zero-byte stream check to SendPassing, adds a CheckRefusedKqueue case to guest_socket_rights, and rewrites the debt entry.
  • The author replied in the thread, and the description now has a "Refused descriptors" item and a new top Tested line.
  • The progress bot now counts sendmsg_nid_postfix as a stub. That is how tools/progress.py works: sendmsg reaches a helper that calls NotImplemented_nid_no_patch, so it counts. It is expected and not a problem.

What I ran

  • PR head: b9e987b82e177fbd8d4da475806e0a0d56199f2c
  • main: 52c09d567d2fa453494dd720fcf53a98149468e4
  • I merged with git merge --no-ff in a clean worktree. Git reported no textual conflict. Machine: Arch Linux 7.2.11-zen, GCC 16.2.1, Release, Ninja, Intel Core i9-14900KF.
git diff --check origin/main...refs/review/7090
python3 tools/check_conventions.py --base origin/main
cmake --build build -j10 --target libs guest_socket_rights_tests guest_socket_pair_tests guest_udp_tests guest_tcp_tests \
    guest_socket_read_write_tests guest_socket_descriptors_tests guest_socket_on_non_socket_tests guest_socket_descriptor_flags_tests
# fails; then, locally only, renamed the call at Udp.cpp:1627 (merged tree) to IsKqueueDescriptor_nid_no_patch and rebuilt
ctest --test-dir build -j4 --output-on-failure --repeat until-fail:3 \
    -R '^(guest_socket_rights|guest_socket_pair|guest_recvmsg_memory|guest_udp|guest_tcp|guest_socket_read_write|guest_socket_descriptors|guest_socket_on_non_socket|guest_socket_descriptor_flags)$'
# negative control: deleted only the `IsKqueueDescriptor_nid_no_patch(passed)) return 45;` line
cmake --build build -j10 --target guest_socket_rights_tests
ctest --test-dir build --output-on-failure -R '^guest_socket_rights$'
# probe: a temporary main() in GuestSocketRights.cpp (reverted afterwards)
Check Result
Textual merge onto main clean
git diff --check clean
check_conventions.py All conventions checks passed.
Build of the merged tree fails: Udp.cpp:1627: error: 'IsKqueueDescriptor' was not declared in this scope (GCC's message, translated from the host locale)
9 socket tests, 3 repeats (with the local rename) 9 of 9 pass each time
Negative control (kqueue check removed) guest_socket_rights aborts (Subprocess aborted)
Probe see below
Probe output on the merged tree (with the local rename)
PROBE jit: create=0 fd=10 map(original)=0 sent=1 received=1 controlLength=24 fd=11 map(received)=0x80020009
PROBE urandom: threw: sendmsg: passing a random device descriptor, whose state is libkernel's, fd=12 not implemented
PROBE zero-byte stream: threw: sendmsg: SCM_RIGHTS without data on a stream socket not implemented
PROBE zero-byte datagram: sent=0 received=0 controlLength=24

Previous findings

  1. Blocking 1 (placeholder descriptors): resolved for the descriptors it named. The new test and my negative control cover the kqueue EOPNOTSUPP. In the probe, /dev/urandom opened with sceKernelOpen now throws. The archived-directory /dev/null placeholder is in g_directories (OpenDirectoryDescriptor registers the descriptor that OpenPlaceholder returns), so IsDirectoryDescriptor covers it too. That closes the item I listed under "Not verified" last time. The JIT table is the same kind of problem, under New findings.
  2. Blocking 2 (zero-byte stream send): resolved. In the probe it throws. A zero-byte datagram send still delivers its 24-byte record.
  3. Non-blocking (getpeername on a received socket): still open, unverified. Neither of us reproduced it, so leaving the entry as it is seems fine.

New findings

Blocking

  1. The head does not compile on current main. #11296, merged on 2026-10-28, renamed IsKqueueDescriptor to IsKqueueDescriptor_nid_no_patch (commit f87b6289ac). The new check at Udp.cpp L1614 still uses the old name. Git merges the files without a textual conflict, but libkernel then fails to build. The branch's merge base f759e165d9 is older than the rename, which is why it built for the author. Fix: merge or rebase onto current main and call IsKqueueDescriptor_nid_no_patch(passed). With that one-line rename, everything above passes.

  2. A JIT shared-memory descriptor is still sent as if it were complete. sceKernelJitCreateSharedMemory returns a real host memfd, but its size object and maximum protection live in libkernel's g_jitDescriptors. sceKernelJitMapSharedMemory returns EBADF for any descriptor that is not in that table. dup copies the entry (Stdio.cpp L862), but the receive side of SCM_RIGHTS does not. In the probe, sendmsg returns 1 and the record arrives. Mapping the original descriptor returns 0, but mapping the received one returns 0x80020009 (SCE_KERNEL_ERROR_EBADF). Handing JIT memory to another party is the main reason to send this descriptor over a socket, and the call succeeds while giving the receiver a descriptor it cannot map. Under CONTRIBUTING.md's "does exactly what it is supposed to or throws", that should throw. Suggested fix: export a predicate from Jit.cpp (as IsRandomDevice is exported), throw for it in PassedDescriptors next to the random-device and directory check, and add "JIT shared memory" to the debt entry at docs/dev/debt/libkernel.md:239. The alternative is to register the received descriptor under the sender's entry.

Non-blocking

None.

Not verified

  • macOS and Windows builds of this head.
  • Tests outside the 9 socket tests listed. I ran no title.
  • Whether other libkernel-only state is lost on a received descriptor (for example, emulated-only status flags in DescriptorControl.cpp). I found nothing else in a table that sendmsg hands over on Linux. The SharedMemory.cpp access table is Windows-only, and SCM_RIGHTS throws on Windows.

To get this to a good state

  • Bring the branch up to current main and rename the call to IsKqueueDescriptor_nid_no_patch.
  • Refuse JIT shared-memory descriptors in PassedDescriptors, or register them on receive, and list the case in the debt entry.

milescoviello and others added 2 commits October 29, 2026 05:47
…and use the renamed kqueue check

sceKernelJitCreateSharedMemory's size object and maximum protection live
in libkernel's g_jitDescriptors, which the receive side of SCM_RIGHTS
doesn't fill, so a received JIT descriptor failed
sceKernelJitMapSharedMemory with EBADF (XairRrR's review). Passing one
now throws, with the random device and directory cases; IsJitSharedMemory
answers it. The merge of main brought boykopovar#11296's rename of
IsKqueueDescriptor to IsKqueueDescriptor_nid_no_patch, which the kqueue
check now calls.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@milescoviello

Copy link
Copy Markdown
Contributor Author

@XairRrR thanks, both fixed. The head is 1e19b327f7:

  1. Build on current main. I merged main 52c09d567d into the branch (9409deaa87, no textual conflicts), and the kqueue check now calls IsKqueueDescriptor_nid_no_patch, after fix(libkernel_sys): fail fpathconf on a kqueue or shm descriptor with EOPNOTSUPP #11296's rename.
  2. JIT shared memory. Jit.cpp now exports IsJitSharedMemory(int), which checks g_jitDescriptors under g_jitLock. PassedDescriptors throws for it next to the random-device and directory cases ("a JIT shared memory descriptor, whose state is libkernel's"). The debt entry lists it.

At this head, the 19 tests matching ^guest_socket|^guest_recvmsg|^guest_udp|^guest_tcp|^guest_jit pass 3 of 3 (Ubuntu 26.04.1, GCC 15.2).

AI-assisted: yes (Claude Code)

@XairRrR XairRrR left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Re-checked — approved

1e19b327f7 merges current main, renames the kqueue call to IsKqueueDescriptor_nid_no_patch, and refuses JIT shared-memory descriptors in sendmsg. Both of my earlier blockers are fixed, and the merged tree builds and passes the socket and JIT tests.

What changed since my last review

  • 9409deaa87: merge of main 52c09d567d into the branch. Git reported no conflicts, and it brings in #11296's rename.
  • 1e19b327f7: adds IsJitSharedMemory (a lookup in g_jitDescriptors under g_jitLock), throws for it in PassedDescriptors next to the random-device and directory cases, calls IsKqueueDescriptor_nid_no_patch, and adds JIT shared memory to the debt entry at docs/dev/debt/libkernel.md:242.
  • The author replied in the thread, and the description has a new top Tested line.

What I ran

  • PR head: 1e19b327f7f0262d5e3c4df150ec3f04b9adad2d
  • main: 2960e9a6e7e1fc99c4ed180c71495b5eb83464c7
  • I merged with git merge --no-ff in a clean worktree. Machine: Arch Linux 7.2.11-zen, GCC 16.2.1, Release, Ninja, Intel Core i9-14900KF.
git diff --check origin/main...refs/review/7090
python3 tools/check_conventions.py --base origin/main
cmake --build build -j10 --target libs <the 17 test executables behind the 19 tests below>
ctest --test-dir build -j4 --output-on-failure --repeat until-fail:3 \
    -R '^guest_socket|^guest_recvmsg|^guest_udp|^guest_tcp|^guest_jit'
# probe: a temporary main() in GuestSocketRights.cpp (reverted afterwards)
# guest_socket_options_multicast on origin/main, same worktree
cmake --build build -j10 --target guest_socket_options_tests
ctest --test-dir build --output-on-failure -R '^guest_socket_options_multicast$'
Check Result
Merge onto main clean
git diff --check clean
check_conventions.py All conventions checks passed.
Build of libs and the tests succeeds
19 tests, ^guest_socket|^guest_recvmsg|^guest_udp|^guest_tcp|^guest_jit 18 pass; guest_socket_options_multicast aborts (joining 224.0.0.251 through INADDR_ANY failed (errno 5)) and fails the same way on main. My test environment has no network, so this is not caused by the PR
Probe see below
Probe output on the merged tree
PROBE jit: threw: sendmsg: passing a JIT shared memory descriptor, whose state is libkernel's, fd=9 not implemented
PROBE jit: next received=4 controlLength=0
PROBE alias: threw: sendmsg: passing a JIT shared memory descriptor, whose state is libkernel's, fd=10 not implemented
PROBE alias: next received=4 controlLength=0
PROBE dup: threw: sendmsg: passing a JIT shared memory descriptor, whose state is libkernel's, fd=11 not implemented
PROBE dup: next received=4 controlLength=0
PROBE pipe-after-jit-closed: sent=1
PROBE pipe-after-jit-closed: received=1 controlLength=24
PROBE pipe-after-jit-closed: next received=4 controlLength=0

jit comes from sceKernelJitCreateSharedMemory, alias from sceKernelJitCreateAliasOfSharedMemory and dup from dup. After each refusal, nothing is queued on the pair: the next plain send arrives without control data. A pipe end sent after the JIT descriptor is closed still goes through.

Negative control

None. This push adds no test and changes none. The probe above covers the new refusal instead.

Previous findings

  1. Blocking 1 (does not compile on current main): resolved. The head calls IsKqueueDescriptor_nid_no_patch (Udp.cpp L1628). On main 2960e9a6e7, the merged tree builds libs and the tests without my local rename.
  2. Blocking 2 (JIT shared-memory descriptor passed as complete): resolved. The probe shows that a created, aliased or duplicated JIT descriptor now throws before anything is sent. Aliases and dup copies have their own entries in g_jitDescriptors, so the lookup covers them. Closing the descriptor removes its entry (ForgetJitSharedMemory in Stdio.cpp and Open.cpp), so a later descriptor that reuses the number is not refused by mistake. The debt entry lists the case.
  3. Non-blocking (getpeername on a received socket): no change. Neither of us reproduced it, and the author left the entry as it is. That is fine.

New findings

Blocking

None.

Non-blocking

  1. The JIT refusal has no regression test. NotImplemented_nid_no_patch throws an exception that a test can catch. My probe shows this, and the random-device and directory refusals have no test either. A CheckRefusedJit case in guest_socket_rights would keep the check from disappearing quietly. It could catch the exception for a sceKernelJitCreateSharedMemory descriptor, then confirm that the next plain send arrives without control data. This is optional.
  2. A stray line in the description's Tested section. "Ubuntu 24.04, GCC 13.3, Release. The results below are from main 673f740, except the rebase line." sits in the middle of the list, after the title-run item. It looks like a leftover from an earlier version of the description and no longer fits the items around it. Consider removing it.

Not verified

  • macOS and Windows builds of this head. IsJitSharedMemory is defined outside the platform blocks in Jit.cpp, so it should build everywhere, but I did not compile it there.
  • Tests outside the 19 listed. I ran no title.
  • guest_socket_options_multicast with a network. Its failure here matches main.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

build docs libkernel unblocks title Names a title ID on the Unblocks line

Projects

None yet

Development

Successfully merging this pull request may close these issues.