fix(libkernel): answer read, write, lseek, fsync and flock on a kqueue descriptor as FreeBSD does #10014

Merged
xXPBSXx merged 6 commits from upstream-pr/10014 into main 2026-11-02 17:26:54 +00:00
xXPBSXx commented 2026-10-23 04:03:27 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by xXPBSXx on 2026-10-23T04:03:27Z.


What

sceKernelRead, sceKernelWrite, sceKernelLseek, sceKernelFsync, sceKernelFdatasync and flock now fail on a kqueue descriptor with EOPNOTSUPP, EOPNOTSUPP, ESPIPE, EINVAL, EINVAL and EOPNOTSUPP, using the existing IsKqueueDescriptor. guest_kqueue gets the cases. readv, writev, pread and pwrite on a kqueue descriptor are not changed here.

FreeBSD 11.4 kqueueops, invfo_rdwr, kern_fsync, sys_flock; found by comparing the descriptor operations against that source, not from a title. A second commit, 8deb85d957, finishes a zero-length read on a kqueue descriptor with 0 instead of EOPNOTSUPP: FreeBSD 11.4's dofileread returns 0 for uio_resid == 0 before it calls fo_read (sys_generic.c:367, "Finish zero length reads right here"), so only a non-empty read reaches the kqueue's EOPNOTSUPP. dofilewrite has no such shortcut, so a zero-length write stays EOPNOTSUPP. guest_kqueue gets both cases.

Fixes: #10013

Tested

Head 5ba5164c72, rebased onto main bd8e13428a; git merge-tree against current main e7e3e9c198 is clean. The diff is 3 files, +22/-4: Open.cpp +3/-1, Stdio.cpp +3/-3 and GuestKqueue.cpp +16. The rebase dropped exactly one hunk of the earlier head b01e5ae181: the local declarations of lseek_nid_postfix, fsync_nid_postfix, fdatasync_nid_postfix and flock_nid_postfix in GuestKqueue.cpp, which main now declares in the library headers. The other hunks are unchanged.

  • Windows 10 Pro 19045, MinGW-w64 GCC 15.2, Release, on main bd8e13428a with this head (ninja exit code 0): guest_kqueue passes 3 of 3 runs.
  • Without the changes in Open.cpp and Stdio.cpp (both taken from main, same build directory, test kept; ninja exit code 0): guest_kqueue fails 3 of 3 runs with exit code 0xc0000409.
  • The family file|posix|shm|kqueue|equeue|pipe|truncate|lseek|flock|fsync|vector|socket_read|dup|fcntl|descriptor|stdio|aio|kernel_sys has 153 tests on main now (the earlier description named 112); with this head 151 pass and 2 are skipped (the named shared memory tests guest_named_shm_windows_short_temp and guest_named_shm_windows_long_short_temp, which skip in this configuration); ctest reports 100% tests passed, 0 tests failed out of 153, counting the skips as passed.
  • check_conventions --base upstream/main passes.
  • Not run by me: a title, a console, and macOS at the current head. Measured by wachi35676 on Windows 11 and WSL2 Ubuntu 24.04 (GCC 13.3): at head 5ba5164c72 merged onto main 9c435c2812, guest_kqueue, guest_kernel_fcntl and guest_filesystem pass 3 of 3 on both hosts, and guest_kqueue fails with Open.cpp and Stdio.cpp from main; at the current head 8deb85d957 merged onto main 504ea3b53b, guest_kqueue passes 3 of 3 on both hosts and still fails with main's Open.cpp/Stdio.cpp, and a probe gives 0 for sceKernelRead(kq, buf, 0) and read(kq, buf, 0) and 0x8002002d for sceKernelWrite(kq, buf, 0). Measured by jotitadev on macOS 27.2 (Apple M2 Max, x86_64 under Rosetta 2, on #8658) at the earlier head b01e5ae181 (5ba5164c72 only drops four local declarations from GuestKqueue.cpp; the zero-length-read commit 8deb85d957 came after and was not run on macOS): the new test aborts 3 of 3 without the fix, and guest_kqueue and guest_kernel_sys pass 5 of 5 with it. During development one run of guest_kqueue right after a rebuild failed 3/3; the cause was not identified. It did not occur in the three runs with this head above; that is all I can say about it.

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 #10013
  • Unblocks: none
  • GPUs tested: n/a
  • AI-assisted: yes

AI-assisted (Claude Code).

🤖 Generated with Claude Code

<!-- greyfinch-sync:boykopovar/AnyPS5:issue:4977623054 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/10014). Originally posted by [xXPBSXx](https://github.com/xXPBSXx) on 2026-10-23T04:03:27Z. --- ### What `sceKernelRead`, `sceKernelWrite`, `sceKernelLseek`, `sceKernelFsync`, `sceKernelFdatasync` and `flock` now fail on a kqueue descriptor with `EOPNOTSUPP`, `EOPNOTSUPP`, `ESPIPE`, `EINVAL`, `EINVAL` and `EOPNOTSUPP`, using the existing `IsKqueueDescriptor`. `guest_kqueue` gets the cases. `readv`, `writev`, `pread` and `pwrite` on a kqueue descriptor are not changed here. FreeBSD 11.4 `kqueueops`, `invfo_rdwr`, `kern_fsync`, `sys_flock`; found by comparing the descriptor operations against that source, not from a title. A second commit, `8deb85d957`, finishes a zero-length read on a kqueue descriptor with 0 instead of `EOPNOTSUPP`: FreeBSD 11.4's `dofileread` returns 0 for `uio_resid == 0` before it calls `fo_read` (`sys_generic.c:367`, "Finish zero length reads right here"), so only a non-empty read reaches the kqueue's `EOPNOTSUPP`. `dofilewrite` has no such shortcut, so a zero-length write stays `EOPNOTSUPP`. `guest_kqueue` gets both cases. Fixes: #10013 ### Tested Head `5ba5164c72`, rebased onto `main` `bd8e13428a`; `git merge-tree` against current `main` `e7e3e9c198` is clean. The diff is 3 files, +22/-4: `Open.cpp` +3/-1, `Stdio.cpp` +3/-3 and `GuestKqueue.cpp` +16. The rebase dropped exactly one hunk of the earlier head `b01e5ae181`: the local declarations of `lseek_nid_postfix`, `fsync_nid_postfix`, `fdatasync_nid_postfix` and `flock_nid_postfix` in `GuestKqueue.cpp`, which `main` now declares in the library headers. The other hunks are unchanged. - Windows 10 Pro 19045, MinGW-w64 GCC 15.2, Release, on `main` `bd8e13428a` with this head (ninja exit code 0): `guest_kqueue` passes 3 of 3 runs. - Without the changes in `Open.cpp` and `Stdio.cpp` (both taken from `main`, same build directory, test kept; ninja exit code 0): `guest_kqueue` fails 3 of 3 runs with exit code 0xc0000409. - The family `file|posix|shm|kqueue|equeue|pipe|truncate|lseek|flock|fsync|vector|socket_read|dup|fcntl|descriptor|stdio|aio|kernel_sys` has 153 tests on `main` now (the earlier description named 112); with this head 151 pass and 2 are skipped (the named shared memory tests `guest_named_shm_windows_short_temp` and `guest_named_shm_windows_long_short_temp`, which skip in this configuration); ctest reports `100% tests passed, 0 tests failed out of 153`, counting the skips as passed. - `check_conventions --base upstream/main` passes. - Not run by me: a title, a console, and macOS at the current head. Measured by wachi35676 on Windows 11 and WSL2 Ubuntu 24.04 (GCC 13.3): at head `5ba5164c72` merged onto `main` `9c435c2812`, `guest_kqueue`, `guest_kernel_fcntl` and `guest_filesystem` pass 3 of 3 on both hosts, and `guest_kqueue` fails with `Open.cpp` and `Stdio.cpp` from `main`; at the current head `8deb85d957` merged onto `main` `504ea3b53b`, `guest_kqueue` passes 3 of 3 on both hosts and still fails with `main`'s `Open.cpp`/`Stdio.cpp`, and a probe gives 0 for `sceKernelRead(kq, buf, 0)` and `read(kq, buf, 0)` and `0x8002002d` for `sceKernelWrite(kq, buf, 0)`. Measured by jotitadev on macOS 27.2 (Apple M2 Max, x86_64 under Rosetta 2, on #8658) at the earlier head `b01e5ae181` (`5ba5164c72` only drops four local declarations from `GuestKqueue.cpp`; the zero-length-read commit `8deb85d957` came after and was not run on macOS): the new test aborts 3 of 3 without the fix, and `guest_kqueue` and `guest_kernel_sys` pass 5 of 5 with it. During development one run of `guest_kqueue` right after a rebuild failed 3/3; the cause was not identified. It did not occur in the three runs with this head above; that is all I can say about it. ### Checklist - [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) - [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 #10013 - [x] Unblocks: none - [x] GPUs tested: n/a - [x] AI-assisted: yes AI-assisted (Claude Code). 🤖 Generated with [Claude Code](https://claude.com/claude-code)
MarcZX10 commented 2026-10-23 04:08:47 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-10-23T04:08:47Z.

Review: APPROVED


Reviewed exact head b01e5ae1815e92e26dda5f3297520a7efc79caad against current main 9d6adeb550. The targeted guards consistently route kqueue descriptors through the requested FreeBSD-compatible errors for read/write, seek, fsync/fdatasync, and flock; the new guest_kqueue case covers each path and closes its descriptor. The code reuses IsKqueueDescriptor and existing error mappings, and the PR explicitly scopes out readv/writev/pread/pwrite. I ran git diff --check and the repository convention check against current main; both pass. I could not run the native guest_kqueue binary in this environment. The reported Windows run and 112-test kernel/file family are author-reported, including the disclosed unexplained initial failure; the hosted product build jobs are skipped by repository policy. I found no blocking issue in the reviewed diff.

<!-- greyfinch-sync:boykopovar/AnyPS5:review:5623772891 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/10014#pullrequestreview-5623772891). Originally posted by [MarcZX10](https://github.com/MarcZX10) on 2026-10-23T04:08:47Z. **Review:** APPROVED --- Reviewed exact head b01e5ae1815e92e26dda5f3297520a7efc79caad against current main 9d6adeb5500c6b65e6591bc8e0a3c71b38c4b3e9. The targeted guards consistently route kqueue descriptors through the requested FreeBSD-compatible errors for read/write, seek, fsync/fdatasync, and flock; the new guest_kqueue case covers each path and closes its descriptor. The code reuses IsKqueueDescriptor and existing error mappings, and the PR explicitly scopes out readv/writev/pread/pwrite. I ran git diff --check and the repository convention check against current main; both pass. I could not run the native guest_kqueue binary in this environment. The reported Windows run and 112-test kernel/file family are author-reported, including the disclosed unexplained initial failure; the hosted product build jobs are skipped by repository policy. I found no blocking issue in the reviewed diff.
jotitadev commented 2026-10-23 05:59:06 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by jotitadev on 2026-10-23T05:59:06Z.

Review: COMMENTED


macOS test: Apple M2 Max, macOS 27.2

Apple M2 Max, macOS 27.2, Apple clang (Xcode 27), x86-64 Release build under Rosetta 2, BUILD_TESTING=ON. Head b01e5ae181. main does not build on macOS, so the platform is #8658 (feat/macos-port) @ fa063d8b07 with this PR's diff applied (clean apply).

On macOS the kqueue placeholder is also /dev/null, so the same calls reach the host device without the change.

guest_kqueue guest_kernel_sys
#8658 alone pass pass
#8658 + this PR's test only 3/3 abort (SIGABRT) –
#8658 + this PR 5/5 pass 5/5 pass

So the new KqueueDescriptorOperations case catches the bug on macOS as well, and the fix holds there.

Written with AI assistance (Claude Code). I ran the builds and tests above on my machine.

<!-- greyfinch-sync:boykopovar/AnyPS5:review:5624502260 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/10014#pullrequestreview-5624502260). Originally posted by [jotitadev](https://github.com/jotitadev) on 2026-10-23T05:59:06Z. **Review:** COMMENTED --- ### macOS test: Apple M2 Max, macOS 27.2 Apple M2 Max, macOS 27.2, Apple clang (Xcode 27), x86-64 Release build under Rosetta 2, `BUILD_TESTING=ON`. Head `b01e5ae181`. `main` does not build on macOS, so the platform is #8658 (`feat/macos-port`) @ `fa063d8b07` with this PR's diff applied (clean apply). On macOS the kqueue placeholder is also `/dev/null`, so the same calls reach the host device without the change. | | `guest_kqueue` | `guest_kernel_sys` | |---|---|---| | #8658 alone | pass | pass | | #8658 + this PR's test only | 3/3 abort (SIGABRT) | – | | #8658 + this PR | 5/5 pass | 5/5 pass | So the new `KqueueDescriptorOperations` case catches the bug on macOS as well, and the fix holds there. Written with AI assistance (Claude Code). I ran the builds and tests above on my machine.
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!10014
No description provided.