feat(libkernel): pass descriptors in SCM_RIGHTS messages on Unix sockets #7090
Loading…
Reference in a new issue
No description provided.
Delete branch "upstream-pr/7090"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
Imported from boykopovar/AnyPS5. Originally posted by milescoviello on 2026-10-17T11:01:40Z.
What
sendmsgthrewcontrol data is not supportedfor 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_RIGHTScontrol messages now pass descriptors, as FreeBSD 11'sunp_internalizeandunp_externalizedo.sendmsg: reads FreeBSD's layout: a 12-bytecmsghdrwith the descriptors at offset 16, and recordsCMSG_SPACEbytes apart. It sends the descriptors with the host'sSCM_RIGHTS, because a guest descriptor here is a host descriptor (a guest socket's number is a duplicate of its host socket).EBADFfor a descriptor that is not open, andEINVALfor a level other thanSOL_SOCKETor a record longer than the buffer.recvmsgwith a control buffer: receives with the host'sSCM_RIGHTS. A received socket is installed as a guest socket (family fromgetsockname(), type fromSO_TYPE); any other descriptor is used as it is.CMSG_LENincmsg_lenandCMSG_SPACEbytes, when the whole record fits the caller's buffer. Otherwise it copies nothing, setscontrolLength0 andMSG_CTRUNC, and closes the passed descriptors. That is what 11.4'skern_recvitdoes: it copies only control mbufs that fit whole and disposes the rest withm_dispose_extcontrolm. A fault writing the record, the data or the address also closes them and returnsEFAULT, askern_recvit'sout:path disposes of the control descriptors whenever it returns an error.MSG_CMSG_CLOEXECmarks the received descriptors close-on-exec. Where the host has noMSG_CMSG_CLOEXEC(macOS), each received descriptor getsfcntl(F_SETFD, FD_CLOEXEC)before it is installed.msghdrwrite included, is written back. Any earlier return closes each of them exactly once, including a failure part-way through installing them.MSG_PEEKwith control space reads only the data, as onmain, and leaves any passed descriptors queued.b9e987b82e, from XairRrR's review): a guest kqueue returnsEOPNOTSUPP, as in FreeBSD'sunp_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.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 theSCM_RIGHTSsetup above it) and add neighbouring entries todocs/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):main52c09d567dmerged in, with the kqueue check renamed toIsKqueueDescriptor_nid_no_patch(#11296). A JIT shared memory descriptor (IsJitSharedMemory) now throws too. The 19 tests matching^guest_socket|^guest_recvmsg|^guest_udp|^guest_tcp|^guest_jitpass 3 of 3.This head (
b9e987b82e, XairRrR's review): the 18 tests matching^guest_socket|^guest_recvmsg|^guest_udp|^guest_tcppass 3 of 3. The new kqueue case inguest_socket_rightsrequires-1/errno45 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 basef759e165d9and on this head.guest_socket_options,guest_socket_options_multicastandguest_socket_stream_optionsfail the same way on both (0xc0000409).Review rework (
1db8d77c9a,mainef90ce0452merged in), after oneandonlydean's review:ReceivedRightsowns the received descriptors until the final header write. The old rollback could close a descriptor twice: after a failedInstalltheSockethad already closed it, and the loop closed it again. PlainMSG_PEEKworks again. The test includesprx/libkernel/include/Api.hppandprx/libc/include/Api.hpp(thepublic_headersaudit passes) and counts host descriptors withfcntl(F_GETFD)instead of/proc. New cases:msghdrwhose final header write faults after the data and control copies succeeded:EFAULTwith the host descriptor count unchanged;AF_ROUTEsocket passed together in both orders, where installing the route socket fails: the host descriptor count is unchanged;MSG_PEEKwith 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_udpandguest_tcppass 3 of 3. With the previousUdp.cppunder the new test,guest_socket_rightsaborts. 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 atmainf759e165d9.Merged
main1e740a23a3into the branch (04eeeb9d11), then added484bc8c2c3.mainhad changedrecvmsgin #10308: the host receive runs only for data, a stream socket or a readable one, andmsg_flagsstarts from the call's flags as measured on a PS5. The descriptor-passing receive now sits inside that condition, and the two-socket case ofguest_socket_rightsnow expectsMSG_CMSG_CLOEXECback inmsg_flags(it expected 0, and failed 3 of 3 against #10308). On Linux (Ubuntu 26.04.1, GCC 15.2), the 14guest_socket*tests pass 3 of 3 on484bc8c2c3, and the 649 tests matching^guest_|libcpassed 3 of 3 on04eeeb9d11apart from that case.Title run on
484bc8c2c3(by @babyhuey, Linux CI image with lavapipe, Ubuntu 24.04, GCC 13, Release): the 14guest_socket*tests pass, and Forever Launcher (PPSA99030) is still running at the 60 s timeout in 2 of 2 runs, with nosendmsg: control data is not supported,what()orFATAL(mainf3456a4152stops at thatsendmsgline).Ubuntu 24.04, GCC 13.3, Release. The results below are from
main673f7406c5, 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,mainf3456a4152still stops Forever Launcher withsendmsg: control data is not supported. With this head (1142d3a6bc, clean merge) the 14guest_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 (516263f8f6), 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_rightscase: two pipe ends are passed and received into a read-only data buffer, and the receive must returnEFAULTwith the count in/proc/self/fdunchanged. It aborts with the previousUdp.cppand passes with the fix, and the 12guest_socket_*tests pass 3 of 3. The debt entry also says now that severalSCM_RIGHTSmessages in onesendmsgarrive as one record. @wachi35676 re-checked 516263f8f6 merged ontomaina020d0256: the 14guest_socket_*tests pass 3 of 3, reverting only theUdp.cpphunks makes the faulted-receive case abort, and their probe now leaves no descriptor open after a fault on the data buffer. The Windows build ofUdp.cppandlibkernelsucceeds on the same merge.Truncation fixed (223a7c147f), after @wachi35676 found that a short control buffer leaked the passed descriptors. With the old record copy and the new test,
guest_socket_rightsaborts. With the fix, the 12guest_socket_*tests pass 3 of 3, andctest -R '^guest_|libc'passes 475 of 476.guest_module_directories(a relinker test) fails the same way onmainin that tree.Rebase onto
mainbf2c63f853(5f492d3e94).recvmsgonmainnow reads the guest'smsghdrand iovecs into a copy and writes its results back withReadGuest/WriteGuest. The control record is written the same way: into the copiedcontrolpointer, withcontrolLengthandMSG_CTRUNCin the copied header that is written back.guest_socket_*(12 tests,guest_socket_rightsincluded) pass 3 of 3.ctest -R '^guest_|libc': 474 of 476 pass.guest_bad_optional_accesswas not built in that tree, andguest_module_directories(a relinker test) fails the same way withmain's sources.New
guest_socket_rightstest (Linux), over a stream socket pair:MSG_CMSG_CLOEXEC, and checks that both received sockets are close-on-exec and send and receive.MSG_CMSG_CLOEXECand 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.controlLength0 andMSG_CTRUNC. So does a record of two descriptors with room for one (20 bytes), and in both cases the count in/proc/self/fdis unchanged, so no descriptor leaks. A receive without a control buffer gets the data only.EBADF, and another level or an overlong record givesEINVAL.main'sUdp.cppit aborts onsendmsg: control data is not supported. It passes 3 of 3, withguest_socket_pairtoo.ctest -R '^guest_|libc': 402 of 402 pass.Forever Launcher (PPSA99030, 01.000.012), in gdb: Wine's first such call is
sendmsgon a socket-pair end withMSG_NOSIGNAL | MSG_DONTWAIT, one byte of data and a 24-byte control buffer holding oneSOL_SOCKET/SCM_RIGHTSrecord (cmsg_len20) that passes the end of another socket pair.mainwith the PRs for its earlier stops merged locally (#6915, #6028, #6939, #6787, #7070, #6469), it stops at 0 s onsendmsg: control data is not supported, 2 of 2.kevent: NOTE_LOWAT(wineserver's kqueue registersEVFILT_READandEVFILT_WRITEwithNOTE_LOWAT1). That is a separate PR.NOTE_LOWAT) merged, that next stop is gone. Onmain98c251fc62with #6915, #6939, #6787, #7070, #6469 and this PR (17d9e0a516), Forever Launcher runs the whole 60 s in 2 of 2 runs with no unimplemented call. Its first-start prefix setup then reportstitle wineboot skipped; prefix must be initialized by payload Wineandfailed to open "C:\\windows\\system32\\wineboot.exe", and Wine exits with 0xC0000135, the launcher's "Setting up the Windows files did not finish". I read that as the launcher's own setup, not a missing emulator call, but I haven't looked further.@p3n1cillin checked current
mainwith 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.main7fea87746both abort onsendmsg: control data is not supported.main319621ba2e(4fa9ea8a0f, clean), neither stops there. Both start their wineserver, and Wine runs and shuts down in order.scm|rights|unix|sendmsg|recvmsgctests pass on the merge.Windows build: @wachi35676 found that
Udp.cppdid not compile there:GuestControlCloseOnExecwas declared inside the non-Windows block. 17d9e0a516 moves the declaration out of it. A MinGW-w64-fsyntax-onlycompile ofUdp.cppstops 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 andlibs). 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,sendmsgwith anSCM_RIGHTSbuffer still stops withcontrol 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
1142d3a6bcand its merge baseeaff87674c: the 13guest_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_RIGHTSstill 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_DOMAINandMSG_CMSG_CLOEXECdon't exist in XNU's headers, soUdp.cppdidn't compile on top of #7332. aff8c2af51 takes the received socket's family fromgetsockname(). WhereMSG_CMSG_CLOEXECdoesn't exist, it marks each received descriptor withfcntl(F_SETFD, FD_CLOEXEC)when the guest asked for it. Their local equivalent compiled with-Wall -Wextraon arm64 and x86_64, andguest_socket_rightspassed 5 of 5 under Rosetta. They also found that the test didn't check the host flag, so a3b3801aa6 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 thefcntlpath. With the flag forced to 0 and thefcntlloop removed, the test fails.^guest_|libcpassed 411 of 411 on aff8c2af51. @jotitadev then checked head a3b3801aa6 on #7332 (57bccfbfc, M2 Max, macOS 27.2, Apple clang 21.0.0):Udp.cppcompiles with-Wall -Wextraon arm64 and x86_64 with no warnings of its own, through thefcntlfallback. Under Rosettaguest_socket_rightspasses 5 of 5, and with thefcntlloop removed it fails 3 of 3 on the new pipe check. The 20 socket, UDP, RUDP, poll and select tests pass, except an intermittentECONNRESETinguest_socket_read_writethat also happens on #7332 alone.Checklist
main; no other open PR implements the same functionsNotImplemented_nid_no_patch); silent stubs are listed under Silent stubs in their library's technical debt file.mdfiles or images added to the repository (attach them to this PR)sendmsgabort (p3n1cillin's run); PPSA99030 #12712 (Forever Launcher): #6915, #6028, #6939, #6787, #7070 and #6469 have landed, and this is its last stop onmain(babyhuey's run)🤖 Generated with Claude Code
Imported from boykopovar/AnyPS5. Originally posted by wachi35676 on 2026-10-17T22:36:28Z.
This doesn't compile on Windows (head
4cd70d4ae; WinLibs GCC 15.2.0, Ninja, Release), alone or merged ontomain11acfdd84:GuestControlCloseOnExecis declared at line 1244, inside the#ifndef _WIN32block that runs from line 1240 to 1328. The flag check at line 1371 inrecvmsg_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 _WIN32fixes the build here.AI-assisted: yes (Claude Code); results above were run on this machine.
Imported from boykopovar/AnyPS5. Originally posted by milescoviello on 2026-10-17T23:24:38Z.
Thanks @wachi35676, that's right. 17d9e0a516 declares
GuestControlCloseOnExecabove the#ifndef _WIN32block, as you suggested;recvmsgmasks the flag on every host.Checked here with MinGW-w64 (
x86_64-w64-mingw32-g++-posix): a-fsyntax-onlycompile ofUdp.cppstops on exactly your line at 4cd70d4ae, and passes with 17d9e0a516 under-Wall -Wextrawith 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)
Imported from boykopovar/AnyPS5. Originally posted by screamm on 2026-10-18T13:53:27Z.
Windows build of head
17d9e0a51, merged ontomain0d078f75dtogether with #7492 (fd2eb3521) and #7509 (c170bba42); all three merge cleanly.BUILD_TESTING=ON.libsboth pass (not a syntax-only compile).Udp.cppbuilds with no warning, andg++ -Wall -Wextra -fsyntax-onlyof it with the project's flags is clean.GuestControlCloseOnExecback inside#ifndef _WIN32reproduceserror: 'GuestControlCloseOnExec' was not declared in this scopeat therecvmsgflag check (1391:51in my copy, the same expression @wachi35676 reported at1371:51); with the declaration above the block it compiles.guest_socket_rightsis registered onlyif(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).socketpair,sendmsgwithout control andrecvmsgwith a control buffer behave as before (data,controlLength0, flags 0),recvmsgwithMSG_CMSG_CLOEXECstill fails with 45, andsendmsgwith a one-descriptorSCM_RIGHTSbuffer ends interminatewithsendmsg: control data is not supported on Windows. WithUdp.cppfrommainthe probe differs only in the message text, so the 32 socket tests cannot tell the two apart on Windows.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/mainandgit diff origin/main --checkpass.Titles: PPSA99030 is not available here.
recvmsgis imported by 13 andsendmsgby 12 local titles. I ran PPSA74126 (Stremio Plus) and PPSA99082 (OpenNOW) for 45 s each, and PPSA99169 (RetroArch,app.exeonly; its picker child's output is not captured), with a local diagnosticlibkernel(reverted) that logged everysendmsgandrecvmsgcall with a control buffer. It logged none, so I have no local title that sends or receives anSCM_RIGHTSmessage.Not checked: the descriptor passing itself (Linux only,
guest_socket_rightsnot run), Linux, a console, PPSA99030.Written with Claude Code assistance (validation by a subagent, reviewed in this session).
Imported from boykopovar/AnyPS5. Originally posted by milescoviello on 2026-10-18T18:45:40Z.
Thanks @screamm for the full Windows build, the control and the probe. I added it to the description.
AI-assisted: yes (Claude Code)
Imported from boykopovar/AnyPS5. Originally posted by jotitadev on 2026-10-18T21:04:09Z.
Review: COMMENTED
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 noSO_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-onlycompile of the mergedUdp.cppthen fails with exactly these two errors: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_rightsis registered underif(NOT WIN32), so it would be built on macOS too.Suggestion that keeps one path for both Unix hosts:
getsockname()(sockaddr_storage::ss_family) instead ofSO_DOMAIN. On macOS this returnsAF_UNIXfor asocketpairend andAF_INET6for an unbound IPv6 socket, andSO_TYPEworks as it is.MSG_CMSG_CLOEXECand setFD_CLOEXECwithfcntlon each received descriptor when the guest asked for it, beforeInstallPassed. Alternatively, guard the flag with#ifdef MSG_CMSG_CLOEXECand use thefcntlpath otherwise.Not checked: running
guest_socket_rightson 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)
Imported from boykopovar/AnyPS5. Originally posted by milescoviello on 2026-10-18T21:06:55Z.
@jotitadev thanks, both are right. I took your first suggestion and the
#ifdefvariant of the second, in aff8c2af51:getsockname()(ss_family) instead ofSO_DOMAIN.SO_TYPEis unchanged.MSG_CMSG_CLOEXECexists,recvmsgstill sets close-on-exec atomically, so Linux behaves exactly as tested. Elsewhere each received descriptor getsfcntl(F_SETFD, FD_CLOEXEC)right afterrecvmsgwhen the guest asked for it, beforeInstallPassed. I kept the atomic flag where it exists because thefcntlpath leaves a window in which another thread's spawn could inherit the descriptor.I'm running
guest_socket_rightson Linux, plus once with the fallback forced so thefcntlpath runs too, and the^guest_|libcset. I'll push when those pass. I can't build for macOS here, so a-fsyntax-onlycheck of the pushed head on top of #7332 would be welcome if you have time.AI-assisted: yes (Claude Code)
Imported from boykopovar/AnyPS5. Originally posted by jotitadev on 2026-10-19T00:13:18Z.
@milescoviello thanks.
feat/scm-rightsis still at17d9e0a51, and aff8c2af51 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).17d9e0a51on #7332:Udp.cppstill fails-fsyntax-onlyfor arm64 and x86_64, with the same two errors (SO_DOMAIN,MSG_CMSG_CLOEXEC). #7332 alone compiles.getsockname()ss_family,MSG_CMSG_CLOEXECunder#ifdef, and otherwisefcntl(F_SETFD, FD_CLOEXEC)on each received descriptor beforeInstallPassed.Udp.cppcompiles with-Wall -Wextraand no warnings on both arches. In a real x86_64 build run under Rosetta,guest_socket_rightspasses 5 of 5. The 20guest_socket_*,guest_udp,guest_rudp_*,guest_pollandguest_select*tests pass. In 3 repeats,guest_socket_read_writefailed once and passed on re-run; it is already intermittent on #7332 alone.One gap in the test: it never checks close-on-exec on the received host descriptor. The only
ControlCloseOnExeccase receives two sockets. For a guest socket,F_GETFDreadscloseOnExecDescriptors, whichInstallPassedfills, and not the host fd. So the check passes when nothing sets the host flag:fcntlloop removed:guest_socket_rightspasses 3 of 3.17d9e0a51withMSG_CMSG_CLOEXECremoved from therecvmsgcall: it also passes 3 of 3.So your forced-fallback run on Linux will pass whether or not the
fcntlpath runs. One way to cover both paths is to receive a passed pipe end withControlCloseOnExecand requirefcntl_nid_postfix(writer, GetDescriptorFlags) == 1. A pipe goes throughInstallPassedunchanged, 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 aff8c2af51, 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.
Imported from boykopovar/AnyPS5. Originally posted by milescoviello on 2026-10-19T01:04:30Z.
@jotitadev thanks, and sorry the commit wasn't up yet when you looked. Both are pushed now:
getsockname()for the family,MSG_CMSG_CLOEXECwhere it exists, otherwisefcntl(F_SETFD, FD_CLOEXEC)per received descriptor.CheckPassedPipenow also passes a pipe end withControlCloseOnExecand requiresF_GETFD == 1on it.You're right that my forced-fallback run proved nothing before that check. With it, on Linux (Ubuntu 24.04, GCC 13.3):
fcntlpath.fcntlloop removed, it fails.^guest_|libcset passed 411 of 411 on aff8c2af51.A run of
-fsyntax-onlyon a3b3801aa6 over #7332 would confirm the macOS side whenever you get to it.AI-assisted: yes (Claude Code)
Imported from boykopovar/AnyPS5. Originally posted by jotitadev on 2026-10-19T02:16:14Z.
@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 inUdp.cpp. The only warnings come from existing headers (-Wreturn-type-c-linkageinEqueue.hppandGeneral.hpp). On macOS,MSG_CMSG_CLOEXECis undefined, so thefcntlfallback is the path that gets compiled.guest_socket_rightspasses 5 of 5.guest_socket_*,guest_udp,guest_rudp_*,guest_pollandguest_select*tests passed in 2 of 3 runs. In the third,guest_socket_read_writeaborted 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 #7332 without this PR in my previous run.fcntl(F_SETFD, FD_CLOEXEC)loop.guest_socket_rightsnow fails 3 of 3. It aborts on the new pipe check: with a temporary print,F_GETFDon 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.
Imported from boykopovar/AnyPS5. Originally posted by milescoviello on 2026-10-19T02:18:32Z.
@jotitadev thanks, that settles the macOS side. The mutation result is the useful part: it shows the new pipe check actually exercises the
fcntlpath. I've added your results to the PR body.About the
guest_socket_read_writeabort 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 inCLOSE_WAITasEPIPE, which is what the test expects. I believe XNU reports it asECONNRESETin 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)
Imported from boykopovar/AnyPS5. Originally posted by milescoviello on 2026-10-19T21:43:26Z.
Rebased onto
mainbf2c63f853(5f492d3e94).recvmsgonmainnow works on a copy of the guest'smsghdrand writes its results back withWriteGuest. The SCM_RIGHTS record now goes through the same path: it is written to the copiedcontrolpointer, andcontrolLengthandMSG_CTRUNCgo 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)
Imported from boykopovar/AnyPS5. Originally posted by wachi35676 on 2026-10-19T22:30:19Z.
Re-run of head 5f492d3e9 merged onto current
main0929be5e8(clean).Linux: WSL Ubuntu 24.04, GCC 13.3, Release,
BUILD_TESTING=ON.guest_socket_*(12 tests,guest_socket_rightsincluded) pass 3/3.Udp.cppfrommain, tests kept):guest_socket_rightsaborts, and the other 11 pass.Windows: Windows 11 Home 26300, WinLibs GCC 15.2.0, Release.
guest_socket_*(25 tests;guest_socket_rightsis not registered on Windows) plusguest_kqueueandguest_selectpass 3/3.Udp.cppfrommainthe 25 also pass, as expected, since the Windows path only throws.Truncation. A probe sends one
SCM_RIGHTSrecord with two pipe write ends (record 24 bytes). It receives with different control capacities and counts/proc/self/fdbefore and after the receive:MSG_CTRUNCis set correctly through the copied header.kern_recvit:len >= m->m_len);MSG_CTRUNCand callsm_dispose_extcontrolm(m)on the rest, which closes the descriptors that didn't fit.controlLength0,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.controlLength == 0.AI-assisted: yes (Claude Code); results above were run on this machine.
Imported from boykopovar/AnyPS5. Originally posted by milescoviello on 2026-10-19T22:35:38Z.
@wachi35676 thanks, you're right, and the probe that counts
/proc/self/fdshows the leak directly. I checked 11.4'skern_recvit(uipc_syscalls.cL1143-L1155). The loop copies only while a whole control mbuf fits (len >= m->m_len), andmsg_controllenadds up only what it copied. Whatever is left getsMSG_CTRUNCandm_dispose_extcontrolm, which closes those descriptors. My description and the debt entry claimed the partial copy was whatkern_recvitdoes, and that was wrong.223a7c147f (not pushed yet) does it FreeBSD's way:
controlLengthis 0,MSG_CTRUNCis set, and the descriptors it carried are closed: sockets throughGuestSockets::Close, other descriptors withclose.EFAULT, askern_recvit's error path does.guest_socket_rightsnow expectscontrolLength0 for the 16-byte buffer, and adds your case of two descriptors with room for one. Both check that the count in/proc/self/fdis unchanged.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)
Imported from boykopovar/AnyPS5. Originally posted by milescoviello on 2026-10-19T23:20:47Z.
@wachi35676 pushed 223a7c147f, and the description now says it the FreeBSD way. A record that doesn't fit whole gets
controlLength0 andMSG_CTRUNC, and its descriptors are closed.guest_socket_rightsnow covers your two cases (16 bytes, and two descriptors with room for one), and checks that the count in/proc/self/fdis unchanged after each. With the old record copy it aborts. With the fix, the 12guest_socket_*tests pass 3 of 3 and^guest_|libcpasses 475 of 476 (the one failure, the relinker testguest_module_directories, fails the same way onmainhere). Your probe on a short buffer should now showprocess fds after-before=0.AI-assisted: yes (Claude Code)
Imported from boykopovar/AnyPS5. Originally posted by wachi35676 on 2026-10-20T00:14:46Z.
Re-run of head 223a7c147 merged onto current
maineb7500680(clean).Linux: WSL Ubuntu 24.04, GCC 13.3, Release,
BUILD_TESTING=ON.guest_socket_*(12 tests) pass 3/3.Udp.cppfrom the previous head 5f492d3e9,guest_socket_rightsaborts and the other 11 pass. WithUdp.cppfrommainit aborts too (sendmsg: control data is not supported).Windows: Windows 11 Home 26300, WinLibs GCC 15.2.0, Release.
guest_socket_*(25 tests) andguest_kqueuepass 3/3.guest_selectaborts (0xc0000409) 3/3 here. It aborts the same way withUdp.cppfrommain, so this PR doesn't cause it.Short control buffer. I ran the same probe as last time. It counts
/proc/self/fdaround the receive and closes whatever the record reports. "Two records" means onesendmsgwith two separateSCM_RIGHTScmsgs of one descriptor each.controlLength0,MSG_CTRUNCand no new descriptors. 24 and 32 give the whole 24-byte record. A fault writing the record returnsEFAULTand closes both descriptors.recvmsgreturnsEFAULTbut leaves both received descriptors open, and the guest never learns their numbers.return Fail(14)afterWriteGuest(vector.base, ...)(and themsg_namewrite after it) runs oncepassedis installed, but before the new dispose branch. In FreeBSD 11.4 a data-copy error fromsoreceivereacheskern_recvit'sout:label, which callsm_dispose_extcontrolm(control)whenevererror != 0(L1157-L1170). Closingpassedon those two early returns as well (the same loop) would match it.sendmsgflattens everySCM_RIGHTScmsg into a single host cmsg, so the receiver sees one 2-descriptor record. Nothing leaks, but the layout differs from FreeBSD: there,unp_internalizekeeps one control mbuf per cmsg (uipc_usrreq.c L2096), so the receiver would get two 24-byte records. At capacity 24 or 32 it would get the first record,MSG_CTRUNC, and the second descriptor closed. At capacity 48 it would get both records. So the "only the first record fits" case can't happen here. This could just be a line in the debt entry.AI-assisted: yes (Claude Code); results above were run on this machine.
Imported from boykopovar/AnyPS5. Originally posted by milescoviello on 2026-10-20T02:47:42Z.
@wachi35676 thanks again, both points were right. 516263f8f6 (pushed) closes the passed descriptors on the data and address
EFAULTreturns as well, through the same helper as the short-buffer path, which matcheskern_recvit'sout:disposal on any error. A newguest_socket_rightscase passes two pipe ends and receives into a read-only data buffer. It requiresEFAULTand an unchanged/proc/self/fdcount, aborts with the previousUdp.cpp, and passes with the fix; the 12guest_socket_*tests pass 3 of 3.On the two cmsgs: Linux merges them into one
SCM_RIGHTSrecord on the receiving side, so keeping FreeBSD's one record per message would need bookkeeping of our own. As you suggested, the debt entry now records it: severalSCM_RIGHTSmessages in onesendmsgarrive as one record, so a buffer with room for only the first record can't receive it alone.AI-assisted: yes (Claude Code)
Imported from boykopovar/AnyPS5. Originally posted by wachi35676 on 2026-10-20T02:55:55Z.
Re-checked head 516263f8f merged onto current
maina020d0256(clean merge).Linux (WSL Ubuntu 24.04, GCC 13.3, Release,
BUILD_TESTING=ON):guest_socket_*(14 tests) pass 3/3.Udp.cpphunks of 516263f8f reverted,guest_socket_rightsaborts (the new faulted-receive case) and the other 13 pass. Restored, 14/14 pass.Probe (same as last time; it counts
/proc/self/fdaround the receive):EFAULT, and no descriptors are left open (it was +2 on 223a7c147). The short-buffer and read-only-control rows are the same as before.SCM_RIGHTSmessages arrive as one record, so a buffer with room for only the first record can't receive it alone. That matches what the probe measures.check_conventions.py --base origin/mainpasses.Windows (WinLibs GCC 15.2.0, Release):
Udp.cppandlibkernelbuild on the same merge.guest_socket_rightsis Linux only, so it doesn't run here.Nothing further from me on this PR.
AI-assisted: yes (Claude Code); results above were run on this machine.
Imported from boykopovar/AnyPS5. Originally posted by milescoviello on 2026-10-20T03:01:47Z.
@wachi35676 thanks for the re-check and for the probe across all the buffer cases. I added your run to Tested.
AI-assisted: yes (Claude Code)
Imported from boykopovar/AnyPS5. Originally posted by milescoviello on 2026-10-20T03:56:59Z.
Rebased onto
main525e299fc0(now 766b697384). The only conflict was indocs/dev/debt/libkernel.md, next tomain's newMSG_DONTWAITentry, which on Windows now polls withWSAPollfirst. I kept that text asmainhas it, and theSCM_RIGHTSentry is unchanged.Udp.cppmerged on its own withmain's changes. The 14guest_socket_*tests pass 3 of 3, andctest -R '^guest_|libc'passes 482 of 483. The one failure,guest_module_directories(a relinker test), fails the same way onmainhere.AI-assisted: yes (Claude Code)
Imported from boykopovar/AnyPS5. Originally posted by milescoviello on 2026-10-21T18:04:47Z.
Rebased onto
main606297f34f(now3e9a4ad967). #8474 had changedNativeReceiveFlagsto take the socket type forMSG_WAITALL, and added a debt entry next to this PR's. Bothrecvmsgpaths here now passsocket->type, and both debt entries are kept. On that base the 26guest_socket_*,net_*andguest_sce_net*tests pass 3 of 3, except the threenet_dontwait_blocking_*tests, which fail onmainon this host as well (#8917, #8918).AI-assisted: yes (Claude Code)
Imported from boykopovar/AnyPS5. Originally posted by milescoviello on 2026-10-21T18:45:54Z.
Rebased again onto
maineaff87674c(now1142d3a6bc), past #8492 and #8663. Only the debt file conflicted: #8492 added its zero-length receive entry next to this one, and both are kept. The 26 socket and net tests give the same result as before, 3 of 3: all pass except the threenet_dontwait_blocking_*(#8917, #8918). This time the tree was built with GCC 15.2.AI-assisted: yes (Claude Code)
Imported from boykopovar/AnyPS5. Originally posted by XairRrR on 2026-10-29T04:12:04Z.
Review: CHANGES_REQUESTED
Reviewed — changes requested
The
SCM_RIGHTStransport, the receive-side ownership and the truncation and fault paths work in my runs, butsendmsgreports 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
1db8d77c9afda0f719e2b4467cdc131843c95eddmain:b433c40f2f46aa2e59e19806010a839ac8572d64git merge --no-ffin a clean worktree (no conflicts). Arch Linux 7.2.11-zen, GCC 16.2.1, Release, Ninja, Intel Core i9-14900KF.Results
maingit diff --checkcheck_conventions.pyAll conventions checks passed.libsand the 8 socket test targets)guest_socket_rightstakes 0.01 sguest_socket_rightsabortsProbe output on the merged tree
Negative control
I restored only
core/libs/prx/libkernel/Socket/src/Udp.cppfrommainand kept the new test.guest_socket_rightsaborts withterminate called after throwing an instance of 'std::runtime_error'/what(): sendmsg: control data is not supported. With the PR'sUdp.cppback, it passes.Findings
Line numbers in
Udp.cppare at the PR head. The other libkernel files are linked atmain.Blocking
sendmsgpasses 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/nullplaceholder plus an entry ing_kqueues(Kqueue.cppL86-L92, L561-L572). A guest/dev/urandomis a/dev/nullplaceholder that libkernel fills on read (RandomDevice.cppL28-L38).PassedDescriptorschecks 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:sendmsgreturns 2. The received descriptor failskeventwithEBADF, though the original returns 0. FreeBSD 11.4 refuses the send instead:unp_internalizereturnsEOPNOTSUPPfor a file withoutDFLAG_PASSABLE(uipc_usrreq.cL2004-L2011), andkqueueopssets nofo_flags(kern_event.cL122-L135)./dev/urandom(opened withsceKernelOpen): 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 throughNotImplemented_nid_no_patch, asRefuseArchivedFilealready does for archived files. Name them in the debt entry, and add aguest_socket_rightscase for the kqueueEOPNOTSUPP.A zero-byte
sendmsgwithSCM_RIGHTSon a stream socket returns 0 and drops the descriptors.SendPassinghands an empty iovec to the hostsendmsg. Linux doesn't carry ancillary data on aSOCK_STREAMUnix socket without at least one data byte. In the probe the call returns 0, and the receive after a later one-byte plain send getscontrolLength0 and noMSG_CTRUNC, so the descriptor is gone and neither side is told. FreeBSD 11.4 delivers control-only sends:sosend_genericbuilds "a single empty mbuf" when "resid is 0, which can happen only if we have control to send" and callspru_sendwith it (uipc_socket.cL1325-L1331). Suggested fix: when the socket is a stream,descriptorsis not empty anddatais empty, throw throughNotImplemented_nid_no_patchand 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
getpeernamereturnsENOTCONNon a received connected Unix socket. The debt entry atdocs/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
^guest_|libctests outside the 9 listed.RefuseArchivedFilealso catches the/dev/nullplaceholder thatDirectoryDescriptor.cppopens for an archived directory.To get this to a good state
PassedDescriptors:EOPNOTSUPPfor kqueue,NotImplemented_nid_no_patchfor the others. Add a test and a debt line.sendmsgthat carries descriptors, and list it in the debt entry.Imported from boykopovar/AnyPS5. Originally posted by XairRrR on 2026-10-29T05:23:45Z.
Review: CHANGES_REQUESTED
Re-checked — changes still requested
b9e987b82efixes both earlier blockers: a kqueue now getsEOPNOTSUPP, and a random device, a directory or a zero-byte stream send now throws. But the head no longer compiles on currentmain, and a JIT shared-memory descriptor is still passed as if it were complete.What changed since my last review
b9e987b82e. It adds the kqueue, random-device and directory checks toPassedDescriptorsand the zero-byte stream check toSendPassing, adds aCheckRefusedKqueuecase toguest_socket_rights, and rewrites the debt entry.Testedline.sendmsg_nid_postfixas a stub. That is howtools/progress.pyworks:sendmsgreaches a helper that callsNotImplemented_nid_no_patch, so it counts. It is expected and not a problem.What I ran
b9e987b82e177fbd8d4da475806e0a0d56199f2cmain:52c09d567d2fa453494dd720fcf53a98149468e4git merge --no-ffin 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.maingit diff --checkcheck_conventions.pyAll conventions checks passed.Udp.cpp:1627: error: 'IsKqueueDescriptor' was not declared in this scope(GCC's message, translated from the host locale)guest_socket_rightsaborts (Subprocess aborted)Probe output on the merged tree (with the local rename)
Previous findings
EOPNOTSUPP. In the probe,/dev/urandomopened withsceKernelOpennow throws. The archived-directory/dev/nullplaceholder is ing_directories(OpenDirectoryDescriptorregisters the descriptor thatOpenPlaceholderreturns), soIsDirectoryDescriptorcovers 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.getpeernameon a received socket): still open, unverified. Neither of us reproduced it, so leaving the entry as it is seems fine.New findings
Blocking
The head does not compile on current
main. #11296, merged on 2026-10-28, renamedIsKqueueDescriptortoIsKqueueDescriptor_nid_no_patch(commitf87b6289ac). The new check atUdp.cppL1614 still uses the old name. Git merges the files without a textual conflict, butlibkernelthen fails to build. The branch's merge basef759e165d9is older than the rename, which is why it built for the author. Fix: merge or rebase onto currentmainand callIsKqueueDescriptor_nid_no_patch(passed). With that one-line rename, everything above passes.A JIT shared-memory descriptor is still sent as if it were complete.
sceKernelJitCreateSharedMemoryreturns a real host memfd, but its size object and maximum protection live in libkernel'sg_jitDescriptors.sceKernelJitMapSharedMemoryreturnsEBADFfor any descriptor that is not in that table.dupcopies the entry (Stdio.cppL862), but the receive side ofSCM_RIGHTSdoes not. In the probe,sendmsgreturns 1 and the record arrives. Mapping the original descriptor returns 0, but mapping the received one returns0x80020009(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 fromJit.cpp(asIsRandomDeviceis exported), throw for it inPassedDescriptorsnext to the random-device and directory check, and add "JIT shared memory" to the debt entry atdocs/dev/debt/libkernel.md:239. The alternative is to register the received descriptor under the sender's entry.Non-blocking
None.
Not verified
DescriptorControl.cpp). I found nothing else in a table thatsendmsghands over on Linux. TheSharedMemory.cppaccess table is Windows-only, andSCM_RIGHTSthrows on Windows.To get this to a good state
mainand rename the call toIsKqueueDescriptor_nid_no_patch.PassedDescriptors, or register them on receive, and list the case in the debt entry.Imported from boykopovar/AnyPS5. Originally posted by XairRrR on 2026-10-29T06:12:28Z.
Review: APPROVED
Re-checked — approved
1e19b327f7merges currentmain, renames the kqueue call toIsKqueueDescriptor_nid_no_patch, and refuses JIT shared-memory descriptors insendmsg. 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 ofmain52c09d567dinto the branch. Git reported no conflicts, and it brings in #11296's rename.1e19b327f7: addsIsJitSharedMemory(a lookup ing_jitDescriptorsunderg_jitLock), throws for it inPassedDescriptorsnext to the random-device and directory cases, callsIsKqueueDescriptor_nid_no_patch, and adds JIT shared memory to the debt entry atdocs/dev/debt/libkernel.md:242.Testedline.What I ran
1e19b327f7f0262d5e3c4df150ec3f04b9adad2dmain:2960e9a6e7e1fc99c4ed180c71495b5eb83464c7git merge --no-ffin a clean worktree. Machine: Arch Linux 7.2.11-zen, GCC 16.2.1, Release, Ninja, Intel Core i9-14900KF.maingit diff --checkcheck_conventions.pyAll conventions checks passed.libsand the tests^guest_socket|^guest_recvmsg|^guest_udp|^guest_tcp|^guest_jitguest_socket_options_multicastaborts (joining 224.0.0.251 through INADDR_ANY failed (errno 5)) and fails the same way onmain. My test environment has no network, so this is not caused by the PRProbe output on the merged tree
jitcomes fromsceKernelJitCreateSharedMemory,aliasfromsceKernelJitCreateAliasOfSharedMemoryanddupfromdup. 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
main): resolved. The head callsIsKqueueDescriptor_nid_no_patch(Udp.cppL1628). Onmain2960e9a6e7, the merged tree buildslibsand the tests without my local rename.dupcopies have their own entries ing_jitDescriptors, so the lookup covers them. Closing the descriptor removes its entry (ForgetJitSharedMemoryinStdio.cppandOpen.cpp), so a later descriptor that reuses the number is not refused by mistake. The debt entry lists the case.getpeernameon 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
NotImplemented_nid_no_patchthrows an exception that a test can catch. My probe shows this, and the random-device and directory refusals have no test either. ACheckRefusedJitcase inguest_socket_rightswould keep the check from disappearing quietly. It could catch the exception for asceKernelJitCreateSharedMemorydescriptor, then confirm that the next plain send arrives without control data. This is optional.Testedsection. "Ubuntu 24.04, GCC 13.3, Release. The results below are frommain673f7406c5, 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
IsJitSharedMemoryis defined outside the platform blocks inJit.cpp, so it should build everywhere, but I did not compile it there.guest_socket_options_multicastwith a network. Its failure here matchesmain.