Skip to content

fix(libkernel): fail EVFILT_READ and EVFILT_WRITE on the random device with ENODEV, as FreeBSD 11.4 does - #24482

Merged
oneandonlydean merged 1 commit into
boykopovar:mainfrom
brajankw:fix/libkernel-kevent-random-device-enodev
Nov 21, 2026
Merged

oneandonlydean merged 1 commit into
boykopovar:mainfrom
brajankw:fix/libkernel-kevent-random-device-enodev

Conversation

@brajankw

Copy link
Copy Markdown
Contributor

What

kevent, sceKernelAddReadEvent and sceKernelAddWriteEvent threw for read and write events on /dev/random and /dev/urandom. FreeBSD 11.4 attaches these filters through the file's fo_kqfilter: devfs passes it to the device's d_kqfilter, which random_cdevsw leaves unset and prep_cdevsw fills with enodev.

Tested

Linux (Debian 13, GCC 14.2.0, Release). Head 0c7dd7b9cb, the single commit on main c8f72e905c.

  • Negative control: with only the test change, on main 84e16538d4, guest_kqueue aborts with kevent: EVFILT_READ on a descriptor that is neither a socket nor the read end of a pipe not implemented.
  • On 0c7dd7b9cb, ctest over guest_kqueue, guest_equeue_events, guest_random and guest_kernel_sys_ksem: all pass.
  • Full ctest (6393 tests, -j 12) on this branch and my other eleven branches of this round, merged onto main 84e16538d4: 6378 passed. The 15 failures also fail on main in this environment:
    • nine need a .git directory, network access or a passwd entry for the user: conventions_git_errors, test_script_registration, web_library_assets, title_run, title_run_identity, guest_kernel_sys_login, guest_net_ctl, guest_getlogin and guest_getlogin_no_session;
    • five agc_driver GPU cases fail the same way on main: prepared_shaders with its indirect, uncached and debug forms, and storage_file_write;
    • agc_driver_image_table_packed_guards is unstable on main as well (2 of 10 runs passed on its own on main 3f4693be43).
  • python3 tools/check_conventions.py --base origin/main: passed.
  • python3 tools/pr_overlap.py --branch fix/libkernel-kevent-random-device-enodev: could not compare feat(libkernel): report open, read and close notes on watched files #7121 (Kqueue.cpp, GuestKqueue.cpp) and feat(libkernel): implement the named ksem semaphores #9293, feat(libkernel_sys): implement POSIX message queues #9297 and feat(libkernel_sys): implement kmq_notify registration #9302 (Equeue.cpp), which conflict with main; none of their diffs touches AddDescriptorKnote or the two equeue wrappers. git merge-tree with feat(libkernel): add pipe write events and socket low watermarks to sceKernelAdd*Event #10645's head is clean.
  • Not tested on Windows or macOS. No title.

Checklist

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

…e with ENODEV, as FreeBSD 11.4 does

kevent, sceKernelAddReadEvent and sceKernelAddWriteEvent threw for read
and write events on /dev/random and /dev/urandom. FreeBSD 11.4 attaches
these filters through the file's fo_kqfilter; devfs passes it to the
device's d_kqfilter, which random_cdevsw leaves unset and prep_cdevsw
fills with enodev. kqueue_register clears fflags before the attach, so
the error event has fflags 0 and ENODEV in data. The check now comes
before the standard input shortcut, so a random device duplicated onto
descriptor 0 answers the same, and the two equeue calls check a bad
queue first, as kern_kevent fetches the kqueue before the attach.

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

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

Verified on Linux (Ubuntu 24.04, GCC 13.3, Ninja, Release) against 0c7dd7b9cb:

  • Conventions: tools/check_conventions.py passed.
  • Build: guest_kqueue_tests built cleanly.
  • CTest (guest_kqueue, guest_kqueue_cleared_race):
    1/2 Test #5877: guest_kqueue .....................   Passed    5.57 sec
    2/2 Test #5878: guest_kqueue_cleared_race ........   Passed   20.80 sec
    100% tests passed, 0 tests failed out of 2
    
  • Negative control: with core/libs/prx/libkernel/Equeue/Equeue.cpp, core/libs/prx/libkernel/Equeue/src/Kqueue.cpp reverted to main and the new tests kept, the suite fails:
    1/2 Test #5877: guest_kqueue .....................Subprocess aborted***Exception:   2.68 sec
    2/2 Test #5878: guest_kqueue_cleared_race ........   Passed   26.92 sec
    50% tests passed, 1 tests failed out of 2
    
    Restoring the change makes it pass again.
  • Code reading:
    • Verified sceKernelAddReadEvent and sceKernelAddWriteEvent validate EqueuePin_nid_postfix(eq) before returning SCE_KERNEL_ERROR_ENODEV or SCE_KERNEL_ERROR_EBADF, matching libkernel queue validation semantics.
    • Verified AddDescriptorKnote in Kqueue.cpp checks File::IsRandomDevice(descriptor) and returns GUEST_ENODEV while clearing change.fflags and change.data to 0 according to FreeBSD 11.4 kqueue_register behavior.
    • Verified error handling in kevent when EV_ERROR is returned in change/eventlist matches the expected struct fields without uninitialized data or improper state mutation.
    • Verified guest_kqueue tests cover /dev/random, /dev/urandom, EVFILT_READ, EVFILT_WRITE, EV_DELETE (returning GuestEnoent), bad queue handle checks, and descriptor 0 redirection.
    • Verified technical debt documentation fragment in docs/dev/debt/changes/fix-libkernel-kevent-random-device-enodev.md conforms to AGENTS.md requirements without forbidden comments in code.

Looks Good To Me

@oneandonlydean
oneandonlydean merged commit 5f477cb into boykopovar:main Nov 21, 2026
14 checks passed
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.

3 participants