Skip to content

fix(libkernel): fail readv and writev on a kqueue descriptor with EOPNOTSUPP - #17587

Merged
oneandonlydean merged 2 commits into
boykopovar:mainfrom
zaryabzip:fix/kqueue-readv-writev
Nov 12, 2026
Merged

oneandonlydean merged 2 commits into
boykopovar:mainfrom
zaryabzip:fix/kqueue-readv-writev

Conversation

@zaryabzip

@zaryabzip zaryabzip commented Nov 9, 2026 •

Copy link
Copy Markdown
Contributor

What

readv and writev (sceKernelReadv, sceKernelWritev and the POSIX forms) on a kqueue descriptor now fail with EOPNOTSUPP. A readv whose vectors add up to zero bytes returns 0. This is the same answer #10014 gave read and write.

Source. The same FreeBSD 11.4 code #10014 used:

  • In sys/kern/kern_event.c, kqueueops sets fo_read and fo_write to invfo_rdwr, which returns EOPNOTSUPP.
  • In sys/kern/sys_generic.c, dofileread returns 0 for a zero-length read before it calls fo_read. dofilewrite has no such shortcut.

sys_readv and sys_writev reach the same dofileread and dofilewrite as read and write, through kern_readv and kern_writev. #10014 said that readv, writev, pread and pwrite were not changed there. main has since answered pread, pwrite and their vector forms with ESPIPE (PositionedArgumentError), but readv and writev still reached the host descriptor that backs a kqueue, which is /dev/null opened read-only. So readv returned 0 (end of file) and writev failed with EBADF.

Change (Stdio.cpp). After the iovec array is copied and checked, a kqueue descriptor returns EOPNOTSUPP, or 0 for a zero-length readv, before any buffer is touched. This is the same place FreeBSD fails: copyinuio checks the array, and invfo_rdwr fails before uiomove. It applies to both the Windows TransferIovecs path and the non-Windows sceKernelReadv/sceKernelWritev.

Test (GuestKqueue.cpp, ctest guest_kqueue). KqueueDescriptorOperations, which #10014 added, gains four rows:

  • readv of one byte fails with EOPNOTSUPP;
  • writev of one byte fails with EOPNOTSUPP;
  • readv of zero bytes returns 0;
  • writev of zero bytes fails with EOPNOTSUPP.

Related. xXPBSXx's #10016 covers fstat, fchmod, fchown and ioctl on a kqueue descriptor from the same kqueueops, so this PR keeps to readv and writev. The two PRs merge cleanly with each other.

Tested

  • Windows and WSL2, by me at 4375a03aa1 (tree cd7c4f2dcb), on main 38abe83721. Windows runs the TransferIovecs path and WSL2 the other one.

    • Windows: WinLibs GCC 15.2.0 r7, Release. WSL2: Ubuntu 24.04.5, GCC 13.3.0, Release.
    • ctest -R "guest_kqueue|guest_append_writes|guest_socket_read_write": all 4 matching tests pass 3 of 3 on both hosts. They are guest_kqueue, guest_kqueue_cleared_race, guest_append_writes and guest_socket_read_write.
    • Control: with main's Stdio.cpp, guest_kqueue aborts on both hosts at the first new row, the one-byte readv, which doesn't return -1 on main.
    • No warnings in Stdio.cpp or GuestKqueue.cpp.
  • Windows, by JJenkx at 4375a03aa1 merged onto main 972b77046f, a clean merge (comment). Windows 11 Enterprise 22621, WinLibs GCC 15.2 UCRT, Release: guest_kqueue and guest_kqueue_cleared_race pass 3 of 3, none skipped.

  • Windows and WSL2 rerun, by me at 15184495e1 (tree 233472df9e), on main 38abe83721, after KqueueIovecsResult switched to std::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.
    • No warnings in Stdio.cpp or GuestKqueue.cpp.
  • Windows, by gabichuuuuu at 15184495e1 merged onto main 94d70921fa with other open PRs that touch other files (comment). Windows 11 x64, WinLibs GCC 15.2.0 r7, Ninja, Release.

    • guest_kqueue_cleared_race passes 3 of 3.
    • guest_kqueue passes this PR's rows, then aborts 3 of 3 at GuestKqueue.cpp:1027. That line is in EdgeTriggeredSocketRead, a socketpair read with EV_CLEAR, which this PR doesn't reach.
    • With main's Stdio.cpp, guest_kqueue stops 2 of 2 at the first new row, before that test.
    • A run of that test on current main alone, and on this head merged with it, is pending.

Checked so far, on macOS (arm64, where no full build is possible):

Checklist

  • Based on current main; no other open PR implements the same functions
  • One topic per PR; follow-ups go in a new PR
  • No comments in code except technical debt
  • Unimplemented paths throw (NotImplemented_nid_no_patch); silent stubs are listed under Silent stubs in their library's technical debt file
  • No notes, investigation .md files or images added to the repository (attach them to this PR)
  • New third-party code is a submodule built from source
  • Generic behaviour, not specific to one title
  • Depends on: none
  • Fixes: none
  • Unblocks: none
  • GPUs tested: n/a
  • AI-assisted: yes (Claude Code)

…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.
@JJenkx

JJenkx commented Nov 9, 2026

Copy link
Copy Markdown
Contributor

Windows check of head 4375a03aa1, merged onto main 972b77046f (4a4c247fb5, clean merge).

Setup: Windows 11 Enterprise 22621, WinLibs GCC 15.2 UCRT, Release. The hosted Windows check does not run these tests on pull requests.

  • guest_kqueue, guest_kqueue_cleared_race pass in 3 of 3 runs, none skipped.

AI-assisted: yes.

@zaryabzip

Copy link
Copy Markdown
Contributor Author

Thanks @JJenkx, added to Tested.

AI-assisted: yes (Claude Code)

@github-actions

github-actions Bot commented Nov 9, 2026 •

Copy link
Copy Markdown

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 5021 core/libs/prx/libkernel/File/src/Stdio.cpp
PR 6936 core/libs/prx/libkernel/File/src/Stdio.cpp
PR 7088 core/libs/tests/GuestKqueue.cpp
PR 7121 core/libs/prx/libkernel/File/src/Stdio.cpp
core/libs/tests/GuestKqueue.cpp
PR 7447 core/libs/prx/libkernel/File/src/Stdio.cpp
PR 7492 core/libs/tests/GuestKqueue.cpp
PR 9293 core/libs/prx/libkernel/File/src/Stdio.cpp
PR 9297 core/libs/prx/libkernel/File/src/Stdio.cpp
PR 9302 core/libs/prx/libkernel/File/src/Stdio.cpp
PR 11625 core/libs/prx/libkernel/File/src/Stdio.cpp
PR 12682 core/libs/prx/libkernel/File/src/Stdio.cpp
PR 12688 core/libs/prx/libkernel/File/src/Stdio.cpp
PR 12700 core/libs/prx/libkernel/File/src/Stdio.cpp
PR 12965 core/libs/prx/libkernel/File/src/Stdio.cpp
PR 15037 core/libs/prx/libkernel/File/src/Stdio.cpp
PR 18462 core/libs/tests/GuestKqueue.cpp

@zaryabzip
zaryabzip force-pushed the fix/kqueue-readv-writev branch from d56c963 to 4375a03 Compare November 9, 2026 09:23

@milescoviello milescoviello 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.

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 after CopyIovecs and after PositionedArgumentError. So preadv/pwritev keep ESPIPE, as kern_preadv checks DFLAG_SEEKABLE first, and a null base with a nonzero length never faults, because invfo_rdwr fails before uiomove.
  • sceKernelReadv/sceKernelWritev: the check sits before OpenIovecs and the socket path, so no destination is opened for a kqueue.
  • iovcnt == 0: readv returns 0 and writev returns EOPNOTSUPP, matching dofileread's zero-resid shortcut and its absence in dofilewrite.

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.
@zaryabzip

Copy link
Copy Markdown
Contributor Author

Thanks @milescoviello for the run with #10016 and the read against FreeBSD. I took the none_of suggestion in 15184495e1, so wrapping lengths can no longer make readv return 0. A Windows and WSL2 rerun is queued.

AI-assisted: yes (Claude Code)

@zaryabzip

Copy link
Copy Markdown
Contributor Author

Rerun after the review fix. This is the Windows and WSL2 run of 15184495e1 (tree 233472df9e), on main 38abe83721. Since the tested 4375a03aa1, the only change is in KqueueIovecsResult, from milescoviello's review: it now checks each vector's length with std::none_of instead of summing them. Both systems built the head as is.

ctest -R "guest_kqueue|guest_append_writes|guest_socket_read_write" --output-on-failure, run once on each system. It matches 4 tests: guest_kqueue, guest_kqueue_cleared_race, guest_append_writes and guest_socket_read_write.

  • Windows 11 Home 25H2 (26200.9711), WinLibs GCC 15.2.0 r7 UCRT, CMake 4.3.1, Ninja 1.13.2, Release: 4 of 4 pass. This is the TransferIovecs path.
  • WSL2, Ubuntu 24.04.5 (kernel 6.18.54.1), GCC 13.3.0, CMake 3.28.3, Ninja 1.11.1, Release: 4 of 4 pass. This is the non-Windows path.
  • Warnings: neither build has warnings in Stdio.cpp or GuestKqueue.cpp. The only warning is in libkernel's Signals.cpp, which this PR doesn't touch.
  • No control this time. The earlier one, with main's Stdio.cpp, still applies.

main has since moved to 0dd2fffe35. None of its new commits touch Stdio.cpp or the three test files, and this PR still merges with it without conflicts.

AI-assisted: yes (Claude Code)

@milescoviello milescoviello 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 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 current main and 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.cpp change reverted. Merged onto current main, the new checks pass. So on Windows the change depends on something that reached main after 38abe83721; I have not found which commit. Merging main into the branch would make the head itself pass on Windows.

AI-assisted: yes (Claude Code)

@gabichuuuuu gabichuuuuu 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.

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.

@zaryabzip

Copy link
Copy Markdown
Contributor Author

Thanks @gabichuuuuu for reading it against FreeBSD and the call sites.

AI-assisted: yes (Claude Code)

@gabichuuuuu

Copy link
Copy Markdown
Contributor

Windows check of head 15184495e1, merged onto main 94d70921fa (clean merge, built together with other open PRs that touch different files).

Setup: Windows 11 x64, WinLibs GCC 15.2.0 (r7), Ninja, Release, BUILD_TESTING=ON, Python 3.15.

  • Tests built from this PR's test files: guest_kqueue, guest_kqueue_cleared_race.
  • guest_kqueue_cleared_race passes in 3 of 3 runs.
  • guest_kqueue exits with 0xc0000409 in 3 of 3 runs, at kqueue check failed at core/libs/tests/GuestKqueue.cpp:1027. That check is in EdgeTriggeredSocketRead (a socketpair read with EV_CLEAR), which this PR does not change. main() calls it after KqueueDescriptorOperations, where this PR adds its checks, so the new checks pass before the crash. I did not run main alone in this batch, so I cannot say whether that failure also happens on main on this machine.
  • Negative control: with this PR's Stdio.cpp change reverted to main and rebuilt, guest_kqueue stops earlier, at GuestKqueue.cpp:134 in the RequireFailure helper, before EdgeTriggeredSocketRead runs, in 2 of 2 runs. So the new checks detect the change, and the later failure at line 1027 is a separate problem.

AI-assisted: yes.

@zaryabzip

Copy link
Copy Markdown
Contributor Author

Thanks @gabichuuuuu, and for running the control. On the abort at GuestKqueue.cpp:1027: this PR's change runs only on kqueue descriptors. All three call sites are guarded by IsKqueueDescriptor_nid_no_patch: TransferIovecs (Stdio.cpp:1690), and sceKernelReadv and sceKernelWritev (1810, 1831). The read in EdgeTriggeredSocketRead is a socketpair read with EV_CLEAR and never reaches it. On my runs at this head on main 38abe83721, guest_kqueue passed 3 of 3 on Windows and WSL2, and so did JJenkx's.

Your build was on main 94d70921fa with other open PRs, so either newer main or one of those PRs is the likelier cause. I've asked for a run of guest_kqueue on current main alone and on this head merged with it, and I'll post what it shows. I added your run to Tested with that note.

AI-assisted: yes (Claude Code)

@milescoviello

Copy link
Copy Markdown
Contributor

@zaryabzip that run is already in my review above (#17587 (review)), from 11-10 on the Windows GT 1030 VM. guest_kqueue and guest_kqueue_cleared_race both pass 3 of 3 on current main 5fec2fe6e5 alone, and on that main with this head merged (133eaa5adb). So line 1027 didn't fail on either, and I couldn't reproduce gabichuuuuu's abort, which fits your reading that it came from their batch.

One thing that may be worth checking on your side: on its own base 38abe83721, the head failed guest_kqueue at GuestKqueue.cpp:134 in 3 of 3 runs on that VM, while the base passed. Merged onto current main it passes.

AI-assisted: yes (Claude Code)

@ycbuzi

ycbuzi commented Nov 11, 2026

Copy link
Copy Markdown
Contributor

Tested 15184495e1 merged into main, with the whole ctest suite on each machine below. Nothing fails that does not also fail on main on the same machine.

  • Windows Server 2022 (GitHub-hosted runner), full build, WinLibs GCC 15.2.0: 99% tests passed, 41 tests failed out of 4921; 99% tests passed, 41 tests failed out of 4921. 41 of the failing tests fail on main too. run
  • Windows Server 2025 (GitHub-hosted runner), full build, WinLibs GCC 15.2.0: 99% tests passed, 15 tests failed out of 4921; 99% tests passed, 15 tests failed out of 4921. 15 of the failing tests fail on main too. run

AI-assisted: yes (Claude Code); the runs are automated, this summary was written from their reports.

@oneandonlydean
oneandonlydean merged commit 09044bb into boykopovar:main Nov 12, 2026
17 checks passed
@zaryabzip

Copy link
Copy Markdown
Contributor Author

Thanks @milescoviello. That settles it: guest_kqueue passed on main alone and with this head merged, so the line-1027 abort came from the batch. I've dropped my own repeat of that run.

AI-assisted: yes (Claude Code)

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

Projects

None yet

Development

Successfully merging this pull request may close these issues.

6 participants