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

Merged
brajankw merged 1 commit from upstream-pr/24482 into main 2026-11-21 03:35:24 +00:00
brajankw commented 2026-11-20 17:22:59 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by brajankw on 2026-11-20T17:22:59Z.


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.

  • EVFILT_READ and EVFILT_WRITE fail with ENODEV. kqueue_register clears fflags before the attach, so the error event has fflags 0 and ENODEV in data.
  • The check runs before the standard input shortcut, so a random device duplicated onto descriptor 0 answers the same.
  • sceKernelAddReadEvent and sceKernelAddWriteEvent answer ENODEV in their export wrappers, after EBADF for a bad queue, as kern_kevent fetches the kqueue before the attach. AddDescriptorEvent, which #10645 rewrites, is not touched; this merges with #10645 without overlap.
  • EVFILT_PROCDESC there still throws, as #7653 limited it to measured kinds, and EVFILT_VNODE and sceKernelAddFileEvent still watch the host device, where FreeBSD fails with ENODEV as well.
  • Tests: RandomDeviceReadWrite in GuestKqueue.cpp, for both devices, through kevent with and without an event list, EV_DELETE afterwards, the two equeue calls, and a random device on descriptor 0.
  • Debt: a new entry, as the console's random device was not measured.

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 #7121 (Kqueue.cpp, GuestKqueue.cpp) and #9293, #9297 and #9302 (Equeue.cpp), which conflict with main; none of their diffs touches AddDescriptorKnote or the two equeue wrappers. git merge-tree with #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
<!-- greyfinch-sync:boykopovar/AnyPS5:issue:5417487410 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/24482). Originally posted by [brajankw](https://github.com/brajankw) on 2026-11-20T17:22:59Z. --- ### 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`](https://github.com/freebsd/freebsd-src/blob/release/11.4.0/sys/dev/random/randomdev.c#L76-L83) leaves unset and [`prep_cdevsw`](https://github.com/freebsd/freebsd-src/blob/release/11.4.0/sys/kern/kern_conf.c#L689) fills with `enodev`. - `EVFILT_READ` and `EVFILT_WRITE` fail with `ENODEV`. [`kqueue_register`](https://github.com/freebsd/freebsd-src/blob/release/11.4.0/sys/kern/kern_event.c#L1392-L1410) clears `fflags` before the attach, so the error event has `fflags` 0 and `ENODEV` in `data`. - The check runs before the standard input shortcut, so a random device duplicated onto descriptor 0 answers the same. - `sceKernelAddReadEvent` and `sceKernelAddWriteEvent` answer `ENODEV` in their export wrappers, after `EBADF` for a bad queue, as `kern_kevent` fetches the kqueue before the attach. `AddDescriptorEvent`, which #10645 rewrites, is not touched; this merges with #10645 without overlap. - `EVFILT_PROCDESC` there still throws, as #7653 limited it to measured kinds, and `EVFILT_VNODE` and `sceKernelAddFileEvent` still watch the host device, where FreeBSD fails with `ENODEV` as well. - Tests: `RandomDeviceReadWrite` in `GuestKqueue.cpp`, for both devices, through `kevent` with and without an event list, `EV_DELETE` afterwards, the two equeue calls, and a random device on descriptor 0. - Debt: a new entry, as the console's random device was not measured. ### Tested <!-- OS, title or homebrew, what was checked. "Not tested" is a valid answer. A performance change: the summary lines of main and of this PR from the same machine and scene, with the traces attached, captured as the Performance report issue form describes. --> 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 #7121 (`Kqueue.cpp`, `GuestKqueue.cpp`) and #9293, #9297 and #9302 (`Equeue.cpp`), which conflict with `main`; none of their diffs touches `AddDescriptorKnote` or the two equeue wrappers. `git merge-tree` with #10645's head is clean. - Not tested on Windows or macOS. No title. ### Checklist <!-- Rules behind each item: https://github.com/boykopovar/AnyPS5/blob/main/CONTRIBUTING.md --> - [x] Based on current `main`; no other open PR implements the same functions - [x] One topic per PR; follow-ups go in a new PR - [x] No comments in code except [technical debt](https://github.com/boykopovar/AnyPS5/blob/main/docs/dev/CONVENTIONS.md) and the licence notice of code adapted from upstream (see AGENTS.md) - [x] Unimplemented paths throw (`NotImplemented_nid_no_patch`); silent stubs are listed under Silent stubs in their library's [technical debt](https://github.com/boykopovar/AnyPS5/tree/main/docs/dev/debt) file - [x] No notes, investigation `.md` files or images added to the repository (attach them to this PR) - [x] New third-party code is a submodule built from source - [x] Generic behaviour, not specific to one title - [x] Depends on: none - [x] Fixes: none - [x] Unblocks: none - [x] GPUs tested: n/a - [x] AI-assisted: yes
PrivacyOnion commented 2026-11-20 18:30:38 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by PrivacyOnion on 2026-11-20T18:30:38Z.

Review: APPROVED


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

<!-- greyfinch-sync:boykopovar/AnyPS5:review:5997563770 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/24482#pullrequestreview-5997563770). Originally posted by [PrivacyOnion](https://github.com/PrivacyOnion) on 2026-11-20T18:30:38Z. **Review:** APPROVED --- 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
Sign in to join this conversation.
No reviewers
No labels
No milestone
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Reference
boykopovar/AnyPS5!24482
No description provided.