feat(libkernel): keep the SIGIO owner of a pipe for F_GETOWN and F_SETOWN #9557

Closed
DavidSamir wants to merge 3 commits from upstream-pr/9557 into main
DavidSamir commented 2026-10-22 02:12:30 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by DavidSamir on 2026-10-22T02:12:30Z.


What

fcntl threw for F_GETOWN and F_SETOWN on every descriptor, and ioctl threw for FIOGETOWN and FIOSETOWN. They now follow FreeBSD 11:

  • What FreeBSD does: kern_fcntl turns F_GETOWN and F_SETOWN into fo_ioctl(FIOGETOWN/FIOSETOWN) (sys/kern/kern_descrip.c:579-598).
  • On a pipe: pipe_ioctl keeps the end's SIGIO owner through fsetown and fgetown (sys/kern/sys_pipe.c:1392-1399, kern_descrip.c:1043 and :1148). Here:
    • 0 clears the owner;
    • the guest's pid, or its negation (the guest leads its own process group), is stored;
    • any other value gives ESRCH, as pfind/pgfind find no such process or group;
    • the owner belongs to the open file, so dup, dup2 and F_DUPFD share it.
  • On a file, a directory or a file read from app0.zar: all four fail with ENOTTY, as vn_ioctl hands them to VOP_IOCTL (sys/kern/vfs_vnops.c:1488).
  • Still throwing: character devices and the random device, which aren't modelled.

No SIGIO is ever sent. O_ASYNC still throws, so a stored owner never implies a signal. The owner is kept in a weak_ptr map keyed on the descriptor's shared status object, which leaves the descriptor table's Entry and ShareDescriptor unchanged. tcgetpgrp/tcsetpgrp on a pipe (TIOCGPGRP/TIOCSPGRP) will follow in a separate PR.

Tested

Windows 11 Pro 26200, WinLibs GCC 15.2.0 r7, Release. This is CPU-side, so no GPU is involved.

New test guest_kernel_fcntl_owner (core/libs/tests/GuestKernelFcntlOwner.cpp), which covers:

  • Pipes: an owner of 0 at first; set, get and clear; ESRCH for pid + 1 and -(pid + 1) with the owner kept; sharing through dup; the other end staying independent; the FIOSETOWN/FIOGETOWN forms, including EFAULT for a NULL FIOGETOWN argument.
  • A file and a directory: ENOTTY from all four requests.
  • A closed descriptor: EBADF.

Results:

  • It passes, 3 of 3 runs. guest_kernel_fcntl, guest_dup, guest_kqueue and guest_terminal pass in 3 runs of the set. In one earlier run of the set, right after a partial rebuild, guest_kqueue aborted. It didn't reproduce in 5 solo runs and 3 more runs of the set.
  • Negative controls:
    • with the owner kept per descriptor number instead of per open file, the test fails at line 57 (dup sharing);
    • without the ESRCH check, it fails at line 60.
  • tools/check_conventions.py: 0 errors. Its one warning is the length of the fcntl debt entry, which was already 4476 characters on main.
  • Overlap: tools/pr_overlap.py --branch plus git merge-tree. #9131 and #7121 merge cleanly. #9256, #8544 and #9109 rewrite the same long fcntl debt line in docs/dev/debt/libkernel.md, so this PR conflicts with them on that line only. The code itself doesn't conflict, and whichever merges second keeps both changes.

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
<!-- greyfinch-sync:boykopovar/AnyPS5:issue:4961058249 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/9557). Originally posted by [DavidSamir](https://github.com/DavidSamir) on 2026-10-22T02:12:30Z. --- ### What `fcntl` threw for `F_GETOWN` and `F_SETOWN` on every descriptor, and `ioctl` threw for `FIOGETOWN` and `FIOSETOWN`. They now follow FreeBSD 11: - **What FreeBSD does:** `kern_fcntl` turns `F_GETOWN` and `F_SETOWN` into `fo_ioctl(FIOGETOWN/FIOSETOWN)` (`sys/kern/kern_descrip.c:579-598`). - **On a pipe:** `pipe_ioctl` keeps the end's SIGIO owner through `fsetown` and `fgetown` (`sys/kern/sys_pipe.c:1392-1399`, `kern_descrip.c:1043` and `:1148`). Here: - 0 clears the owner; - the guest's pid, or its negation (the guest leads its own process group), is stored; - any other value gives `ESRCH`, as `pfind`/`pgfind` find no such process or group; - the owner belongs to the open file, so `dup`, `dup2` and `F_DUPFD` share it. - **On a file, a directory or a file read from `app0.zar`:** all four fail with `ENOTTY`, as `vn_ioctl` hands them to `VOP_IOCTL` (`sys/kern/vfs_vnops.c:1488`). - **Still throwing:** character devices and the random device, which aren't modelled. No `SIGIO` is ever sent. `O_ASYNC` still throws, so a stored owner never implies a signal. The owner is kept in a `weak_ptr` map keyed on the descriptor's shared status object, which leaves the descriptor table's `Entry` and `ShareDescriptor` unchanged. `tcgetpgrp`/`tcsetpgrp` on a pipe (`TIOCGPGRP`/`TIOCSPGRP`) will follow in a separate PR. ### Tested Windows 11 Pro 26200, WinLibs GCC 15.2.0 r7, Release. This is CPU-side, so no GPU is involved. New test `guest_kernel_fcntl_owner` (`core/libs/tests/GuestKernelFcntlOwner.cpp`), which covers: - **Pipes:** an owner of 0 at first; set, get and clear; `ESRCH` for `pid + 1` and `-(pid + 1)` with the owner kept; sharing through `dup`; the other end staying independent; the `FIOSETOWN`/`FIOGETOWN` forms, including `EFAULT` for a NULL `FIOGETOWN` argument. - **A file and a directory:** `ENOTTY` from all four requests. - **A closed descriptor:** `EBADF`. Results: - It passes, 3 of 3 runs. `guest_kernel_fcntl`, `guest_dup`, `guest_kqueue` and `guest_terminal` pass in 3 runs of the set. In one earlier run of the set, right after a partial rebuild, `guest_kqueue` aborted. It didn't reproduce in 5 solo runs and 3 more runs of the set. - Negative controls: - with the owner kept per descriptor number instead of per open file, the test fails at line 57 (`dup` sharing); - without the `ESRCH` check, it fails at line 60. - `tools/check_conventions.py`: 0 errors. Its one warning is the length of the `fcntl` debt entry, which was already 4476 characters on `main`. - Overlap: `tools/pr_overlap.py --branch` plus `git merge-tree`. #9131 and #7121 merge cleanly. #9256, #8544 and #9109 rewrite the same long `fcntl` debt line in `docs/dev/debt/libkernel.md`, so this PR conflicts with them on that line only. The code itself doesn't conflict, and whichever merges second keeps both changes. ### 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) - [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
MarcZX10 commented 2026-10-22 04:14:36 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-10-22T04:14:36Z.

Review: CHANGES_REQUESTED


The owner state and focused pipe tests are coherent on inspection, but there is a concrete invalid-pointer gap in the ioctl path: FIOSETOWN directly dereferences argument (*static_cast<const int*>(argument)) before validating it. FIOGETOWN explicitly returns EFAULT for a null output pointer, while FIOSETOWN with null can fault the host instead of returning guest EFAULT. Please add checked copy-in for the four-byte input (including an unreadable non-null pointer) and a regression test, matching the checked output handling. Also, the overlap report flags live shared debt edits in #9256, #8544 and #9109; those authors must agree the ordering and preserve each debt change, with a Depends on entry if sequencing is required. Finally this branch is based on 8c26eac3, 23 commits behind current main dce41a89478876a8e3ea9c346b1dac00bb31c8cd; please update and rerun relevant tests before approval. Hosted policy checks pass, product build jobs are skipped; Windows test evidence in the body is author-reported and I did not run it locally.

<!-- greyfinch-sync:boykopovar/AnyPS5:review:5609182615 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/9557#pullrequestreview-5609182615). Originally posted by [MarcZX10](https://github.com/MarcZX10) on 2026-10-22T04:14:36Z. **Review:** CHANGES_REQUESTED --- The owner state and focused pipe tests are coherent on inspection, but there is a concrete invalid-pointer gap in the ioctl path: `FIOSETOWN` directly dereferences `argument` (`*static_cast<const int*>(argument)`) before validating it. `FIOGETOWN` explicitly returns `EFAULT` for a null output pointer, while `FIOSETOWN` with null can fault the host instead of returning guest `EFAULT`. Please add checked copy-in for the four-byte input (including an unreadable non-null pointer) and a regression test, matching the checked output handling. Also, the overlap report flags live shared debt edits in #9256, #8544 and #9109; those authors must agree the ordering and preserve each debt change, with a `Depends on` entry if sequencing is required. Finally this branch is based on `8c26eac3`, 23 commits behind current `main` `dce41a89478876a8e3ea9c346b1dac00bb31c8cd`; please update and rerun relevant tests before approval. Hosted policy checks pass, product build jobs are skipped; Windows test evidence in the body is author-reported and I did not run it locally.
harisibrar1 commented 2026-10-22 06:01:29 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by harisibrar1 on 2026-10-22T06:01:29Z.

Review: COMMENTED


Checked against main 4a17c7175 and FreeBSD releng/11.4 (sys/kern/sys_pipe.c:1392-1410, sys/kern/kern_descrip.c:1043-1148, sys/fs/fifofs/fifo_vnops.c):

  • The owner rules match fsetown/fgetown: 0 clears, ESRCH keeps the old owner, and the owner is per open file and shared by dup. The weak_ptr map is reset correctly by pipe() and close().
  • A small correction on the FIOSETOWN NULL point: Ioctl already returns EFAULT for a NULL IOC_IN argument at DescriptorControl.cpp:559, before the dereference at :581. Only a non-NULL unreadable pointer gets through, and FIONBIO on main does the same. A checked copy-in would be nice for both, plus a test that NULL FIOSETOWN gives EFAULT, which isn't covered yet.
  • On Linux/macOS, a host socket that is not a guest socket is classified as Kind::Socket, so F_GETOWN/F_SETOWN on it throw. The debt sentence only names the character device and random device. Could you add that case?
  • On FreeBSD all opens of a FIFO share one pipe_sigio (fifo_vnops.c passes fip->fi_pipe to every open). Here two open()s of the same host FIFO keep separate owners. Worth a short debt note.

Reviewed with AI assistance (Claude Code); the checks above were run locally on my machine.

<!-- greyfinch-sync:boykopovar/AnyPS5:review:5609873210 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/9557#pullrequestreview-5609873210). Originally posted by [harisibrar1](https://github.com/harisibrar1) on 2026-10-22T06:01:29Z. **Review:** COMMENTED --- Checked against main 4a17c7175 and FreeBSD releng/11.4 (`sys/kern/sys_pipe.c:1392-1410`, `sys/kern/kern_descrip.c:1043-1148`, `sys/fs/fifofs/fifo_vnops.c`): - The owner rules match `fsetown`/`fgetown`: 0 clears, `ESRCH` keeps the old owner, and the owner is per open file and shared by `dup`. The weak_ptr map is reset correctly by `pipe()` and `close()`. - A small correction on the `FIOSETOWN` NULL point: `Ioctl` already returns `EFAULT` for a NULL `IOC_IN` argument at `DescriptorControl.cpp:559`, before the dereference at `:581`. Only a non-NULL unreadable pointer gets through, and `FIONBIO` on main does the same. A checked copy-in would be nice for both, plus a test that NULL `FIOSETOWN` gives `EFAULT`, which isn't covered yet. - On Linux/macOS, a host socket that is not a guest socket is classified as `Kind::Socket`, so `F_GETOWN`/`F_SETOWN` on it throw. The debt sentence only names the character device and random device. Could you add that case? - On FreeBSD all opens of a FIFO share one `pipe_sigio` (`fifo_vnops.c` passes `fip->fi_pipe` to every open). Here two `open()`s of the same host FIFO keep separate owners. Worth a short debt note. Reviewed with AI assistance (Claude Code); the checks above were run locally on my machine.
DavidSamir commented 2026-10-22 07:58:56 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by DavidSamir on 2026-10-22T07:58:56Z.


Thanks @MarcZX10 and @harisibrar1. New commits, no force-push:

  • 4aa8b79b25: FIOSETOWN and FIONBIO now copy their int argument in with a checked read: ReadProcessMemory on Windows, process_vm_readv on Linux, mach_vm_read_overwrite on macOS, the same as Stdio.cpp's CopyIovecs. An unreadable non-null pointer fails with EFAULT instead of faulting the host. As @harisibrar1 noted, a null argument already got EFAULT at the IOC_IN check, but no test covered it.
    • guest_kernel_fcntl_owner now checks FIOSETOWN with nullptr and with an unreadable pointer (0x10), each giving EFAULT with the owner unchanged, and FIONBIO with the unreadable pointer.
    • Negative control: with the direct dereference put back, the test dies with 0xc0000005.
    • The debt line now also says that F_GETOWN/F_SETOWN on a host socket that is not a guest socket still throw (Linux and macOS), and that two opens of one FIFO keep separate owners, where FreeBSD shares pipe_sigio.
  • f8698a0b66: merges current main (d6eafa54d0).

Re-run on the merge (Windows 11, WinLibs GCC 15.2): guest_kernel_fcntl, guest_kernel_fcntl_owner, guest_terminal and guest_append_writes pass, and check_conventions.py reports 0 errors. Its one warning is the fcntl debt entry, already over the limit on main.

For the debt-row overlaps (#9256, #8544, #9109), I'm asking those authors for an order and will record it in the checklist.

AI-assisted: yes

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6335781139 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/9557#issuecomment-6335781139). Originally posted by [DavidSamir](https://github.com/DavidSamir) on 2026-10-22T07:58:56Z. --- Thanks @MarcZX10 and @harisibrar1. New commits, no force-push: - **`4aa8b79b25`:** `FIOSETOWN` and `FIONBIO` now copy their int argument in with a checked read: `ReadProcessMemory` on Windows, `process_vm_readv` on Linux, `mach_vm_read_overwrite` on macOS, the same as `Stdio.cpp`'s `CopyIovecs`. An unreadable non-null pointer fails with `EFAULT` instead of faulting the host. As @harisibrar1 noted, a null argument already got `EFAULT` at the `IOC_IN` check, but no test covered it. - `guest_kernel_fcntl_owner` now checks `FIOSETOWN` with `nullptr` and with an unreadable pointer (`0x10`), each giving `EFAULT` with the owner unchanged, and `FIONBIO` with the unreadable pointer. - Negative control: with the direct dereference put back, the test dies with `0xc0000005`. - The debt line now also says that `F_GETOWN`/`F_SETOWN` on a host socket that is not a guest socket still throw (Linux and macOS), and that two opens of one FIFO keep separate owners, where FreeBSD shares `pipe_sigio`. - **`f8698a0b66`:** merges current `main` (`d6eafa54d0`). Re-run on the merge (Windows 11, WinLibs GCC 15.2): `guest_kernel_fcntl`, `guest_kernel_fcntl_owner`, `guest_terminal` and `guest_append_writes` pass, and `check_conventions.py` reports 0 errors. Its one warning is the `fcntl` debt entry, already over the limit on `main`. For the debt-row overlaps (#9256, #8544, #9109), I'm asking those authors for an order and will record it in the checklist. AI-assisted: yes
MarcZX10 commented 2026-10-22 11:22:51 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-10-22T11:22:51Z.


The checked copy-in for FIOSETOWN/FIONBIO and the added debt notes address the concrete feedback from the earlier review; the author-reported Windows focused tests pass. I checked this head against current upstream/main: the merge is clean and no intervening main commit changes DescriptorControl.cpp, the new owner test, or the debt file, but it remains 3 ahead/11 behind (merge base d6eafa54d). Please update to current main and rerun the focused fcntl/owner tests and conventions. The debt-line length is still reported as a warning by conventions (0 errors). The shared debt edit also needs recorded agreement with #9256, #8544, #9109 and the adjacent #9561 update; please add Depends on where an order is required. I found the order proposal to #9256, but no confirmation from the affected author yet. Product and relinker jobs are skipped; I did not run tests locally because the filesystem is full.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6339526990 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/9557#issuecomment-6339526990). Originally posted by [MarcZX10](https://github.com/MarcZX10) on 2026-10-22T11:22:51Z. --- The checked copy-in for FIOSETOWN/FIONBIO and the added debt notes address the concrete feedback from the earlier review; the author-reported Windows focused tests pass. I checked this head against current upstream/main: the merge is clean and no intervening main commit changes DescriptorControl.cpp, the new owner test, or the debt file, but it remains 3 ahead/11 behind (merge base d6eafa54d). Please update to current main and rerun the focused fcntl/owner tests and conventions. The debt-line length is still reported as a warning by conventions (0 errors). The shared debt edit also needs recorded agreement with #9256, #8544, #9109 and the adjacent #9561 update; please add Depends on where an order is required. I found the order proposal to #9256, but no confirmation from the affected author yet. Product and relinker jobs are skipped; I did not run tests locally because the filesystem is full.
MarcZX10 commented 2026-10-22 11:22:52 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-10-22T11:22:52Z.

Review: CHANGES_REQUESTED


The invalid-pointer safety issue is addressed, and the author reports the focused Windows tests passing. The remaining blockers are repository integration requirements: this branch is 3 ahead/11 behind current upstream/main 4777caca34 (merge base d6eafa54d), so update and rerun relevant tests; and record merge-order agreement for the shared libkernel debt edit with #9256, #8544, #9109 and #9561, declaring Depends on if required. Conventions reports 0 errors with the inherited long debt-row warning. Hosted product/relinker tests are skipped. I did not run tests locally because the filesystem is full.

<!-- greyfinch-sync:boykopovar/AnyPS5:review:5613378562 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/9557#pullrequestreview-5613378562). Originally posted by [MarcZX10](https://github.com/MarcZX10) on 2026-10-22T11:22:52Z. **Review:** CHANGES_REQUESTED --- The invalid-pointer safety issue is addressed, and the author reports the focused Windows tests passing. The remaining blockers are repository integration requirements: this branch is 3 ahead/11 behind current upstream/main 4777caca34 (merge base d6eafa54d), so update and rerun relevant tests; and record merge-order agreement for the shared libkernel debt edit with #9256, #8544, #9109 and #9561, declaring Depends on if required. Conventions reports 0 errors with the inherited long debt-row warning. Hosted product/relinker tests are skipped. I did not run tests locally because the filesystem is full.
harisibrar1 commented 2026-10-22 12:54:55 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by harisibrar1 on 2026-10-22T12:54:55Z.

Review: APPROVED


Rechecked at f8698a0b6:

  • FIOSETOWN and FIONBIO now copy their argument in through ReadGuestInt (DescriptorControl.cpp:554-574). It follows main's CopyIovecs checks (null and wrap, then ReadProcessMemory / process_vm_readv / mach_vm_read_overwrite with a full-length check), so an unreadable pointer gives EFAULT and the owner is left unchanged. The new nullptr and 0x10 cases in GuestKernelFcntlOwner cover both.
  • The fcntl debt entry now covers the host socket on Linux/macOS and the per-open FIFO owner. All my points are addressed, thanks.

Reviewed with AI assistance (Claude Code); the checks above were run locally on my machine.

<!-- greyfinch-sync:boykopovar/AnyPS5:review:5614478044 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/9557#pullrequestreview-5614478044). Originally posted by [harisibrar1](https://github.com/harisibrar1) on 2026-10-22T12:54:55Z. **Review:** APPROVED --- Rechecked at f8698a0b6: - `FIOSETOWN` and `FIONBIO` now copy their argument in through `ReadGuestInt` (DescriptorControl.cpp:554-574). It follows main's `CopyIovecs` checks (null and wrap, then `ReadProcessMemory` / `process_vm_readv` / `mach_vm_read_overwrite` with a full-length check), so an unreadable pointer gives `EFAULT` and the owner is left unchanged. The new `nullptr` and `0x10` cases in `GuestKernelFcntlOwner` cover both. - The fcntl debt entry now covers the host socket on Linux/macOS and the per-open FIFO owner. All my points are addressed, thanks. Reviewed with AI assistance (Claude Code); the checks above were run locally on my machine.

Pull request closed

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!9557
No description provided.