Repository navigation
fix(libkernel): fail readv and writev on a kqueue descriptor with EOPNOTSUPP - #17587
Conversation
…NOTSUPP FreeBSD 11.4's kqueueops route fo_read and fo_write to invfo_rdwr, which returns EOPNOTSUPP, and dofileread returns 0 for a zero-length read before calling fo_read. boykopovar#10014 applied this to read and write. readv and writev still reached the host /dev/null descriptor behind a kqueue: readv returned 0 and writev EBADF.
|
Windows check of head Setup: Windows 11 Enterprise 22621, WinLibs GCC 15.2 UCRT, Release. The hosted Windows check does not run these tests on pull requests.
AI-assisted: yes. |
|
Thanks @JJenkx, added to Tested. AI-assisted: yes (Claude Code) |
|
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.
|
d56c963 to
4375a03
Compare
milescoviello
left a comment
There was a problem hiding this comment.
Native Linux run, plus a check with #10016, since both PRs add rows to KqueueDescriptorOperations.
Setup: Ubuntu 26.04.1, GCC 15.2, Release. I tested d56c963b38, whose tree (cd7c4f2dcb) is the same as the current head 4375a03aa1. ctest -R '^guest_kqueue', 3 runs each:
| build | guest_kqueue |
guest_kqueue_cleared_race |
|---|---|---|
main c57db08819 |
3 of 3 pass | 3 of 3 pass |
main + this PR (0093d84f95) |
3 of 3 pass | 3 of 3 pass |
main + this PR + #10016 c1716688cb (4443c4184c) |
3 of 3 pass | 3 of 3 pass |
Both merges are clean, so the two PRs can land in either order.
From reading, against FreeBSD 11.4:
TransferIovecs: the kqueue return comes afterCopyIovecsand afterPositionedArgumentError. Sopreadv/pwritevkeepESPIPE, askern_preadvchecksDFLAG_SEEKABLEfirst, and a nullbasewith a nonzero length never faults, becauseinvfo_rdwrfails beforeuiomove.sceKernelReadv/sceKernelWritev: the check sits beforeOpenIovecsand the socket path, so no destination is opened for a kqueue.iovcnt == 0:readvreturns 0 andwritevreturnsEOPNOTSUPP, matchingdofileread's zero-resid shortcut and its absence indofilewrite.
One small thing, not a blocker: KqueueIovecsResult sums the lengths into a std::size_t, so lengths that wrap to exactly 0 make readv return 0. FreeBSD's copyinuio would return EINVAL there. std::any_of(iov, iov + iovcnt, [](const KernelIovec& v) { return v.length != 0; }) gives the same answer for every non-wrapping input and can't wrap.
AI-assisted: yes (Claude Code)
… them milescoviello's review: lengths that wrap a size_t sum to exactly 0 made readv on a kqueue return 0. Checking whether any vector has a nonzero length gives the same answer otherwise and cannot wrap.
|
Thanks @milescoviello for the run with #10016 and the read against FreeBSD. I took the AI-assisted: yes (Claude Code) |
|
Rerun after the review fix. This is the Windows and WSL2 run of
AI-assisted: yes (Claude Code) |
There was a problem hiding this comment.
Re-checked 15184495e1: the only change since 4375a03aa1 is KqueueIovecsResult, which now tests each length with std::none_of (<algorithm> is already included at line 3). An empty list (iovcnt == 0, empty vectors) is still an empty range, so a zero-length readv returns 0 and writev still returns EOPNOTSUPP. It still merges cleanly into current main. Approval carried over to this head.
Windows check (11-10), for @gabichuuuuu's question about line 1027. Windows 11 VM, GeForce GT 1030, WinLibs GCC 15.2 UCRT, Release. guest_kqueue_tests was rebuilt on each side and run 3 times with ctest.
| build | guest_kqueue |
guest_kqueue_cleared_race |
|---|---|---|
merge base 38abe83721 |
3/3 pass | 3/3 pass |
this head 15184495e1 on its own base |
0/3: kqueue check failed at core/libs/tests/GuestKqueue.cpp:134 |
3/3 pass |
current main 5fec2fe6e5 |
3/3 pass | 3/3 pass |
main 5fec2fe6e5 + this head (133eaa5adb, merged on the VM) |
3/3 pass | 3/3 pass |
EdgeTriggeredSocketRead(line 1027) passes on this VM on currentmainand with this PR merged, so I could not reproduce that failure here.- The head on its own base fails at line 134, the same place as gabichuuuuu's control with the
Stdio.cppchange reverted. Merged onto currentmain, the new checks pass. So on Windows the change depends on something that reachedmainafter38abe83721; I have not found which commit. Mergingmaininto the branch would make the head itself pass on Windows.
AI-assisted: yes (Claude Code)
gabichuuuuu
left a comment
There was a problem hiding this comment.
Read 15184495e1 against main.
Correctness. Stdio.cpp:KqueueIovecsResult returns 0 for a readv whose vectors are all zero-length (including iovcnt == 0) and EOPNOTSUPP otherwise and for every writev. That matches FreeBSD 11.4: dofileread returns early on zero length and dofilewrite does not, before invfo_rdwr. All three call sites run it after CopyIovecs, so a bad iovcnt, an unreadable iovec array or a total above INT64_MAX still fails first with EINVAL/EFAULT, as copyinuio does. The check also runs before the null-base EFAULT loop and before OpenIovecs, so no buffer is touched, as in FreeBSD. On Windows, TransferIovecs limits it to offset == nullptr; preadv/pwritev keep the earlier ESPIPE from PositionedArgumentError. On other hosts it sits in sceKernelReadv/sceKernelWritev before the socket and native paths. readv_nid_postfix/writev_nid_postfix turn the SCE code into errno 45.
Test. On main, readv on a kqueue reaches the read-only /dev/null backing and returns 0, and writev fails with EBADF. The two one-byte rows added to KqueueDescriptorOperations therefore fail there. The zero-length rows pin the read/write asymmetry.
Not run here; this is from reading the code.
AI-assisted: yes.
|
Thanks @gabichuuuuu for reading it against FreeBSD and the call sites. AI-assisted: yes (Claude Code) |
|
Windows check of head Setup: Windows 11 x64, WinLibs GCC 15.2.0 (r7), Ninja, Release,
AI-assisted: yes. |
|
Thanks @gabichuuuuu, and for running the control. On the abort at Your build was on AI-assisted: yes (Claude Code) |
|
@zaryabzip that run is already in my review above (#17587 (review)), from 11-10 on the Windows GT 1030 VM. One thing that may be worth checking on your side: on its own base AI-assisted: yes (Claude Code) |
|
Tested
AI-assisted: yes (Claude Code); the runs are automated, this summary was written from their reports. |
|
Thanks @milescoviello. That settles it: AI-assisted: yes (Claude Code) |
What
readvandwritev(sceKernelReadv,sceKernelWritevand the POSIX forms) on a kqueue descriptor now fail withEOPNOTSUPP. Areadvwhose vectors add up to zero bytes returns 0. This is the same answer #10014 gavereadandwrite.Source. The same FreeBSD 11.4 code #10014 used:
sys/kern/kern_event.c,kqueueopssetsfo_readandfo_writetoinvfo_rdwr, which returnsEOPNOTSUPP.sys/kern/sys_generic.c,dofilereadreturns 0 for a zero-length read before it callsfo_read.dofilewritehas no such shortcut.sys_readvandsys_writevreach the samedofilereadanddofilewriteasreadandwrite, throughkern_readvandkern_writev. #10014 said thatreadv,writev,preadandpwritewere not changed there.mainhas since answeredpread,pwriteand their vector forms withESPIPE(PositionedArgumentError), butreadvandwritevstill reached the host descriptor that backs a kqueue, which is/dev/nullopened read-only. Soreadvreturned 0 (end of file) andwritevfailed withEBADF.Change (
Stdio.cpp). After the iovec array is copied and checked, a kqueue descriptor returnsEOPNOTSUPP, or 0 for a zero-lengthreadv, before any buffer is touched. This is the same place FreeBSD fails:copyinuiochecks the array, andinvfo_rdwrfails beforeuiomove. It applies to both the WindowsTransferIovecspath and the non-WindowssceKernelReadv/sceKernelWritev.Test (
GuestKqueue.cpp, ctestguest_kqueue).KqueueDescriptorOperations, which #10014 added, gains four rows:readvof one byte fails withEOPNOTSUPP;writevof one byte fails withEOPNOTSUPP;readvof zero bytes returns 0;writevof zero bytes fails withEOPNOTSUPP.Related. xXPBSXx's #10016 covers
fstat,fchmod,fchownandioctlon a kqueue descriptor from the samekqueueops, so this PR keeps toreadvandwritev. The two PRs merge cleanly with each other.Tested
Windows and WSL2, by me at
4375a03aa1(treecd7c4f2dcb), onmain38abe83721. Windows runs theTransferIovecspath and WSL2 the other one.ctest -R "guest_kqueue|guest_append_writes|guest_socket_read_write": all 4 matching tests pass 3 of 3 on both hosts. They areguest_kqueue,guest_kqueue_cleared_race,guest_append_writesandguest_socket_read_write.main'sStdio.cpp,guest_kqueueaborts on both hosts at the first new row, the one-bytereadv, which doesn't return -1 onmain.Stdio.cpporGuestKqueue.cpp.Windows, by JJenkx at
4375a03aa1merged ontomain972b77046f, a clean merge (comment). Windows 11 Enterprise 22621, WinLibs GCC 15.2 UCRT, Release:guest_kqueueandguest_kqueue_cleared_racepass 3 of 3, none skipped.Windows and WSL2 rerun, by me at
15184495e1(tree233472df9e), onmain38abe83721, afterKqueueIovecsResultswitched tostd::none_of(comment):ctest -R "guest_kqueue|guest_append_writes|guest_socket_read_write", run once, Release, on the same toolchains as above: all 4 tests pass on both hosts.Stdio.cpporGuestKqueue.cpp.Windows, by gabichuuuuu at
15184495e1merged ontomain94d70921fawith other open PRs that touch other files (comment). Windows 11 x64, WinLibs GCC 15.2.0 r7, Ninja, Release.guest_kqueue_cleared_racepasses 3 of 3.guest_kqueuepasses this PR's rows, then aborts 3 of 3 atGuestKqueue.cpp:1027. That line is inEdgeTriggeredSocketRead, asocketpairread withEV_CLEAR, which this PR doesn't reach.main'sStdio.cpp,guest_kqueuestops 2 of 2 at the first new row, before that test.mainalone, and on this head merged with it, is pending.Checked so far, on macOS (arm64, where no full build is possible):
clang++ -fsyntax-only.tools/check_conventions.py --base mainpasses.mainor any open PR.main(feat(aot): consolidate native ARM64 compiler, runtime and consumers #5021, feat(libkernel): support SEEK_DATA and SEEK_HOLE in lseek #6936, feat(libkernel): support kevent read and write events on regular files #7088, feat(libkernel): report open, read and close notes on watched files #7121, feat(libc): support archive-backed stdio reads #7447, fix(libkernel): wake every thread waiting on an equeue or kqueue when an event fires #7492, feat(libkernel_sys): implement statfs symlinkat and futimesat #9161, feat(libkernel): implement the named ksem semaphores #9293, feat(libkernel_sys): implement POSIX message queues #9297, feat(libkernel_sys): implement kmq_notify registration #9302, feat(libkernel): cancel blocked POSIX reads with owned operations #12682, fix(libkernel): preserve blocking sockets over nonblocking native transport #12688, feat(libkernel): cancel blocked POSIX writes with partial progress #12700, feat(libkernel): support deferred cancellation in POSIX accept #12965), so I checked each withgit merge-treeagainst this head. Each has the same number of conflict hunks inStdio.cppandGuestKqueue.cppwith this PR as withmain. Only feat(aot): consolidate native ARM64 compiler, runtime and consumers #5021, the ARM64 AOT consolidation, already conflicts withmainacross the region ofStdio.cppwhere this PR adds its check. This PR's lines fall inside that existing hunk and add no new one.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)