fix(libkernel): fail EVFILT_READ and EVFILT_WRITE on a ksem id with EINVAL, as FreeBSD 11.4 does #24490

Open
brajankw wants to merge 2 commits from upstream-pr/24490 into main
brajankw commented 2026-11-20 17:29:05 +00:00 (Migrated from github.com)

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


What

kevent threw for read and write events on a ksem id, a host null device descriptor here. FreeBSD 11.4 attaches these filters through the file's fo_kqfilter, and ksem_ops uses invfo_kqfilter, which fails with EINVAL.

  • EVFILT_READ and EVFILT_WRITE fail with EINVAL; kqueue_register clears fflags before the attach, so the error event has fflags 0 and EINVAL in data.
  • sceKernelAddReadEvent and sceKernelAddWriteEvent still throw on a ksem id, and EVFILT_VNODE and sceKernelAddFileEvent watch the host null device; the new debt entry records both.
  • IsKsemDescriptor, which this uses, is replaced by DuplicateKsemDescriptor in open #22191. The two merge without a textual conflict, but whichever lands second needs a rebase that keeps a ksem predicate, or the build breaks. I will rebase this as soon as #22191 lands.
  • The check runs first, so a ksem id that got descriptor 0 after standard input was closed answers the same.
  • Merges without overlap with #24482, which does the random device.
  • Tests: KsemReadWrite in GuestKqueue.cpp, including a ksem id on descriptor 0.
  • Debt: a new entry.

Tested

Linux (Debian 13, GCC 14.2.0, Release). Head 4386105ec3, 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 4386105ec3, ctest over guest_kernel_sys_ksem, guest_kqueue, guest_kernel_sys, guest_filesystem, guest_file_vector, guest_kernel_sys_aio, guest_equeue_events, guest_kernel_fcntl, guest_kernel_ioctl_fionread and guest_kernel_ioctl_device_type: all 10 pass.
  • Full ctest (6393 tests, -j 12) on this branch before review, which then moved the check ahead of the standard-input shortcut and added the descriptor-0 case, 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-ksem-einval: it flagged #7088 for an earlier placement of the check; with the check first in AddDescriptorKnote, git merge-tree with #7088's head is clean. It could not compare #7121, which conflicts with main and does not touch AddDescriptorKnote.
  • 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

Summary by CodeRabbit

  • Bug Fixes
    • Read and write event filters now reject kernel semaphore identifiers with an invalid-argument error instead of treating them as valid targets.
  • Documentation
    • Clarified the behavior of read and write filters for semaphore identifiers.
<!-- greyfinch-sync:boykopovar/AnyPS5:issue:5417571133 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/24490). Originally posted by [brajankw](https://github.com/brajankw) on 2026-11-20T17:29:05Z. --- ### What `kevent` threw for read and write events on a ksem id, a host null device descriptor here. FreeBSD 11.4 attaches these filters through the file's `fo_kqfilter`, and `ksem_ops` uses [`invfo_kqfilter`](https://github.com/freebsd/freebsd-src/blob/release/11.4.0/sys/kern/kern_descrip.c#L4060-L4065), which fails with `EINVAL`. - `EVFILT_READ` and `EVFILT_WRITE` fail with `EINVAL`; `kqueue_register` clears `fflags` before the attach, so the error event has `fflags` 0 and `EINVAL` in `data`. - `sceKernelAddReadEvent` and `sceKernelAddWriteEvent` still throw on a ksem id, and `EVFILT_VNODE` and `sceKernelAddFileEvent` watch the host null device; the new debt entry records both. - `IsKsemDescriptor`, which this uses, is replaced by `DuplicateKsemDescriptor` in open #22191. The two merge without a textual conflict, but whichever lands second needs a rebase that keeps a ksem predicate, or the build breaks. I will rebase this as soon as #22191 lands. - The check runs first, so a ksem id that got descriptor 0 after standard input was closed answers the same. - Merges without overlap with #24482, which does the random device. - Tests: `KsemReadWrite` in `GuestKqueue.cpp`, including a ksem id on descriptor 0. - Debt: a new entry. ### 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 `4386105ec3`, 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 `4386105ec3`, `ctest` over `guest_kernel_sys_ksem`, `guest_kqueue`, `guest_kernel_sys`, `guest_filesystem`, `guest_file_vector`, `guest_kernel_sys_aio`, `guest_equeue_events`, `guest_kernel_fcntl`, `guest_kernel_ioctl_fionread` and `guest_kernel_ioctl_device_type`: all 10 pass. - Full `ctest` (6393 tests, `-j 12`) on this branch before review, which then moved the check ahead of the standard-input shortcut and added the descriptor-0 case, 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-ksem-einval`: it flagged #7088 for an earlier placement of the check; with the check first in `AddDescriptorKnote`, `git merge-tree` with #7088's head is clean. It could not compare #7121, which conflicts with `main` and does not touch `AddDescriptorKnote`. - 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 <!-- This is an auto-generated comment: release notes by coderabbit.ai --> <!-- coderabbit-ai-generated:v1 --> ## Summary by CodeRabbit * **Bug Fixes** * Read and write event filters now reject kernel semaphore identifiers with an invalid-argument error instead of treating them as valid targets. * **Documentation** * Clarified the behavior of read and write filters for semaphore identifiers. <!-- end of auto-generated comment: release notes by coderabbit.ai -->
PrivacyOnion commented 2026-11-20 17:47:56 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by PrivacyOnion on 2026-11-20T17:47:56Z.

Review: APPROVED


Verified on Linux (Ubuntu 24.04, GCC 13.3, Ninja, Release) against 4386105ec3:

  • 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    6.19 sec
    2/2 Test #5878: guest_kqueue_cleared_race ........   Passed   18.59 sec
    100% tests passed, 0 tests failed out of 2
    
  • Negative control: with 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:   3.87 sec
    2/2 Test #5878: guest_kqueue_cleared_race ........   Passed   23.39 sec
    50% tests passed, 1 tests failed out of 2
    
    Restoring the change makes it pass again.
  • Code reading:
    • core/libs/prx/libkernel/Equeue/src/Kqueue.cpp: Verified AddDescriptorKnote checks IsKsemDescriptor(descriptor) immediately upon entry, returning GUEST_EINVAL and clearing fflags and data to 0 as expected by FreeBSD 11.4 invfo_kqfilter behavior.
    • docs/dev/debt/changes/fix-libkernel-kevent-ksem-einval.md: Verified debt fragment adheres to format, notes sceKernelAddReadEvent/sceKernelAddWriteEvent and EVFILT_VNODE behavior, and satisfies AGENTS.md rules.
    • core/libs/tests/GuestKqueue.cpp: Verified KsemReadWrite test covers both EvfiltRead and EvfiltWrite, EV_ADD error reporting, EV_DELETE returning ENOENT, fflags and data zeroing, descriptor 0 re-use after close(0), and clean teardown/restoration of stdio.
    • Checked for AGENTS.md compliance, missing comments, LP64/LLP64 portability, and verified no TOCTOU, memory safety, or resource leak issues are introduced.

Looks Good To Me

<!-- greyfinch-sync:boykopovar/AnyPS5:review:5997043052 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/24490#pullrequestreview-5997043052). Originally posted by [PrivacyOnion](https://github.com/PrivacyOnion) on 2026-11-20T17:47:56Z. **Review:** APPROVED --- Verified on Linux (Ubuntu 24.04, GCC 13.3, Ninja, Release) against `4386105ec3`: - **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 6.19 sec 2/2 Test #5878: guest_kqueue_cleared_race ........ Passed 18.59 sec 100% tests passed, 0 tests failed out of 2 ``` - **Negative control**: with `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: 3.87 sec 2/2 Test #5878: guest_kqueue_cleared_race ........ Passed 23.39 sec 50% tests passed, 1 tests failed out of 2 ``` Restoring the change makes it pass again. - **Code reading**: - core/libs/prx/libkernel/Equeue/src/Kqueue.cpp: Verified AddDescriptorKnote checks IsKsemDescriptor(descriptor) immediately upon entry, returning GUEST_EINVAL and clearing fflags and data to 0 as expected by FreeBSD 11.4 invfo_kqfilter behavior. - docs/dev/debt/changes/fix-libkernel-kevent-ksem-einval.md: Verified debt fragment adheres to format, notes sceKernelAddReadEvent/sceKernelAddWriteEvent and EVFILT_VNODE behavior, and satisfies AGENTS.md rules. - core/libs/tests/GuestKqueue.cpp: Verified KsemReadWrite test covers both EvfiltRead and EvfiltWrite, EV_ADD error reporting, EV_DELETE returning ENOENT, fflags and data zeroing, descriptor 0 re-use after close(0), and clean teardown/restoration of stdio. - Checked for AGENTS.md compliance, missing comments, LP64/LLP64 portability, and verified no TOCTOU, memory safety, or resource leak issues are introduced. Looks Good To Me
MarcZX10 commented 2026-11-21 05:53:51 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-11-21T05:53:51Z.

Review: APPROVED


Reviewed — looks good

The change at head 4386105ec3f8ae5c55fbc35052ece935bc51c9dd rejects ksem descriptors before the standard-input shortcut and reports the expected EINVAL event; no blocking defects were found in the reviewed files.

What I ran

No commands were run. I read the prepared diff and the changed files at the pinned head.

Results

Check Result Note
Changed code review passed Three changed files read
Tests not run Only author-reported results available

Findings

Blocking

None.

Non-blocking

None.

Not verified

I did not run tests or build the change. The reported test and platform results in the PR description are author-supplied. The reader listed all three changed files as included and reviewed; no changed files were excluded.

To get this to a good state

Nothing is required.

<!-- greyfinch-sync:boykopovar/AnyPS5:review:6003330614 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/24490#pullrequestreview-6003330614). Originally posted by [MarcZX10](https://github.com/MarcZX10) on 2026-11-21T05:53:51Z. **Review:** APPROVED --- # Reviewed — looks good The change at head `4386105ec3f8ae5c55fbc35052ece935bc51c9dd` rejects ksem descriptors before the standard-input shortcut and reports the expected `EINVAL` event; no blocking defects were found in the reviewed files. ## What I ran No commands were run. I read the prepared diff and the changed files at the pinned head. ## Results | Check | Result | Note | |---|---|---| | Changed code review | passed | Three changed files read | | Tests | not run | Only author-reported results available | ## Findings ### Blocking None. ### Non-blocking None. ## Not verified I did not run tests or build the change. The reported test and platform results in the PR description are author-supplied. The reader listed all three changed files as included and reviewed; no changed files were excluded. ## To get this to a good state Nothing is required.
MarcZX10 commented 2026-11-28 13:33:43 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-11-28T13:33:43Z.

Review: APPROVED


Reviewed — looks good

Nothing blocks this; the points below are optional.

What I ran

Reviewed head aef78456354d3a7b23e4aef392546c0fd3e760b8 using the prepared diff and repo_read.py at that head. No tests or build commands were run during this review; test results in the PR description are author-reported.

Results

Check Result Note
Changed code review passed All three changed files read
Tests in this review not run Author reports targeted and full test results
Changed-file coverage passed 3 of 3 reviewed files read

Findings

Blocking

None.

Non-blocking

None.

Not verified

No test/build rerun, Windows or macOS run, physical-console behavior, or FreeBSD source behavior was independently verified. Read: core/libs/prx/libkernel/Equeue/src/Kqueue.cpp, core/libs/tests/GuestKqueue.cpp, and docs/dev/debt/changes/fix-libkernel-kevent-ksem-einval.md. Excluded changed files: none. The referenced FreeBSD source and other PR interactions were not inspected.

To get this to a good state

Nothing is required.

<!-- greyfinch-sync:boykopovar/AnyPS5:review:6098048601 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/24490#pullrequestreview-6098048601). Originally posted by [MarcZX10](https://github.com/MarcZX10) on 2026-11-28T13:33:43Z. **Review:** APPROVED --- # Reviewed — looks good Nothing blocks this; the points below are optional. ## What I ran Reviewed head `aef78456354d3a7b23e4aef392546c0fd3e760b8` using the prepared diff and `repo_read.py` at that head. No tests or build commands were run during this review; test results in the PR description are author-reported. ## Results | Check | Result | Note | |---|---|---| | Changed code review | passed | All three changed files read | | Tests in this review | not run | Author reports targeted and full test results | | Changed-file coverage | passed | 3 of 3 reviewed files read | ## Findings ### Blocking None. ### Non-blocking None. ## Not verified No test/build rerun, Windows or macOS run, physical-console behavior, or FreeBSD source behavior was independently verified. Read: `core/libs/prx/libkernel/Equeue/src/Kqueue.cpp`, `core/libs/tests/GuestKqueue.cpp`, and `docs/dev/debt/changes/fix-libkernel-kevent-ksem-einval.md`. Excluded changed files: none. The referenced FreeBSD source and other PR interactions were not inspected. ## To get this to a good state Nothing is required.
Plyxen commented 2026-12-03 14:08:57 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by Plyxen on 2026-12-03T14:08:57Z.

Review: APPROVED


I read the diff at aef7845635. Windows 11 with GCC 15.2 and Linux (Ubuntu 24.04 in WSL2, GCC 13.3), merged into main at 6c6b29320fe together with other open PRs (so not an isolated build).

  • guest_kqueue and guest_kqueue_cleared_race: pass on both.
  • Control (the same tree with only this PR's source change taken out): on Windows guest_kqueue stops with "kevent: EVFILT_READ on a descriptor that is neither a socket nor the read end of a pipe not implemented". On Linux, with the changed sources of the tested CPU PRs put back to main's, guest_kqueue fails too.

Not checked: a console.

AI-assisted: yes (Claude Code)

<!-- greyfinch-sync:boykopovar/AnyPS5:review:6171389633 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/24490#pullrequestreview-6171389633). Originally posted by [Plyxen](https://github.com/Plyxen) on 2026-12-03T14:08:57Z. **Review:** APPROVED --- I read the diff at `aef7845635`. Windows 11 with GCC 15.2 and Linux (Ubuntu 24.04 in WSL2, GCC 13.3), merged into `main` at `6c6b29320fe` together with other open PRs (so not an isolated build). - `guest_kqueue` and `guest_kqueue_cleared_race`: pass on both. - Control (the same tree with only this PR's source change taken out): on Windows `guest_kqueue` stops with "kevent: EVFILT_READ on a descriptor that is neither a socket nor the read end of a pipe not implemented". On Linux, with the changed sources of the tested CPU PRs put back to `main`'s, `guest_kqueue` fails too. Not checked: a console. AI-assisted: yes (Claude Code)
ligmaSec commented 2026-12-03 14:18:17 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by ligmaSec on 2026-12-03T14:18:17Z.

Review: COMMENTED


Built the changed Kqueue unit with the branch's GuestKqueue fixture against the existing Linux runtime. The fixture passed, including EINVAL for read and write filters on a semaphore. I checked that the semaphore test still runs when the semaphore occupies descriptor zero.

AI-assisted: yes.

<!-- greyfinch-sync:boykopovar/AnyPS5:review:6171555261 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/24490#pullrequestreview-6171555261). Originally posted by [ligmaSec](https://github.com/ligmaSec) on 2026-12-03T14:18:17Z. **Review:** COMMENTED --- Built the changed Kqueue unit with the branch's `GuestKqueue` fixture against the existing Linux runtime. The fixture passed, including `EINVAL` for read and write filters on a semaphore. I checked that the semaphore test still runs when the semaphore occupies descriptor zero. AI-assisted: yes.
This pull request cannot be merged automatically due to conflicts.
Merge manually to resolve the conflicts.
View command line instructions

Checkout

From your project repository, check out a new branch and test the changes.
git fetch -u origin 'upstream-pr/24490:upstream-pr/24490'
git switch 'upstream-pr/24490'

Merge

Merge the changes and update on Forgejo.

Warning: The "Autodetect manual merge" setting is not enabled for this repository, you will have to mark this pull request as manually merged afterwards.

git switch 'main'
git merge --no-ff 'upstream-pr/24490'
git switch 'upstream-pr/24490'
git rebase 'main'
git switch 'main'
git merge --ff-only 'upstream-pr/24490'
git switch 'upstream-pr/24490'
git rebase 'main'
git switch 'main'
git merge --no-ff 'upstream-pr/24490'
git switch 'main'
git merge --squash 'upstream-pr/24490'
git switch 'main'
git merge --ff-only 'upstream-pr/24490'
git switch 'main'
git merge 'upstream-pr/24490'
git push origin 'main'
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!24490
No description provided.