Repository navigation
feat(libkernel): answer tcgetpgrp and tcsetpgrp on a pipe with its SIGIO owner - #9566
DavidSamir wants to merge 9 commits into
Conversation
…GIO owner FreeBSD's tcgetpgrp and tcsetpgrp are the TIOCGPGRP and TIOCSPGRP ioctls, which pipe_ioctl answers through fgetown and fsetown on the pipe's SIGIO owner. Builds on the owner kept for F_GETOWN/F_SETOWN.
|
Some comparisons could not be completed because a pull request conflicts with the base branch. Review these shared files manually before concluding that there is no overlap.
|
jotitadev
left a comment
There was a problem hiding this comment.
First run on Linux (the PR is tested on Windows only). Ubuntu 26.04, x86_64, Intel i7-8700, GCC 15.2.0, Release, BUILD_TESTING=ON, head e846772 merged --no-commit onto current origin/main (6ef5a1c).
- Build: ok.
ctest -R "guest_kernel_fcntl_owner|guest_terminal"with the PR: both pass.- Negative control (revert
DescriptorControl.cpp/.hppandTerminal.cpptoorigin/main, keep the tests): both abort with the real assert
terminate called after throwing an instance of 'std::runtime_error'
what(): fcntl: F_GETOWN on a pipe is not implemented, fd=6
- Restoring the PR implementation: both pass again.
So the pipe SIGIO-owner path (F_GETOWN/F_SETOWN, FIO[GS]ETOWN, TIOC[GS]PGRP) is exercised on Linux and the tests genuinely fail without it.
Written with AI assistance (Claude Code). I ran the builds and tests above on my machine.
MarcZX10
left a comment
There was a problem hiding this comment.
The pipe SIGIO owner path is covered by focused owner/terminal tests, including the group/owner sign reversal, and the reported Linux negative-control run exercises the implementation. The declared #9557 prerequisite is correct. Hosted conventions, eligibility, intake, labels and report checks pass; product and relinker jobs are skipped. I did not run tests locally.
I cannot complete the overlap requirement yet. The PR description identifies a direct GuestTerminal.cpp overlap with #9242 and says whichever lands second should rebase, but it does not record agreement with #9242’s author or an agreed order. Please coordinate with that author; if either change must land first, state the order and add the corresponding Depends on entry. Preserve both sets of tests through the merge. The other overlap rows you list should likewise have an explicit agreed integration order wherever the actual changed lines conflict.
|
The PR description identifies the |
harisibrar1
left a comment
There was a problem hiding this comment.
Checked against main 4a17c71, #9557's head c375560 (this PR's parent commit), and FreeBSD releng/11.4 (sys/kern/sys_pipe.c:1401-1410, lib/libc/gen/termios.c):
- The sign reversal between owner and group matches
pipe_ioctl, andtcgetpgrp/tcsetpgrpreuse the #9557 rules. Nothing here duplicates #9557; it just needs to land after it. - The
ioctl, _ioctldebt entry (docs/dev/debt/libkernel.md:220) still says every group-ttermios request fails withENOTTY. With this PR,TIOCSPGRPandTIOCGPGRPon a pipe above fd 2 now succeed (DescriptorControl.cpp:601-609). Could that entry mention the exception? - Nit:
-groupinSetPipeProcessGroupand-*static_cast<const int*>(argument)(:602) overflow forINT_MIN. The result happens to beESRCH, as on FreeBSD, but it depends on undefined behaviour. An explicitINT_MIN→ESRCHcheck would make it well-defined.
Reviewed with AI assistance (Claude Code); the checks above were run locally on my machine.
|
@DavidSamir a merge-order note: #9491 adds a |
…cked read FIOSETOWN and FIONBIO read their int argument by dereferencing the guest pointer, so an unreadable non-null pointer faulted the host instead of failing with EFAULT. Both now copy the int in with ReadProcessMemory, process_vm_readv or mach_vm_read_overwrite. The owner test covers a null and an unreadable argument. The debt line notes that a host socket that is not a guest socket still throws, and that two opens of one FIFO keep separate owners.
…group The process-group setters negated the group to store it as a SIGIO owner, which is undefined for INT_MIN. INT_MIN now fails with ESRCH before the negation, and TIOCSPGRP copies its argument in with the checked read, so an unreadable pointer gives EFAULT. The ioctl debt line names the TIOCSPGRP and TIOCGPGRP exception on a pipe.
|
Thanks @MarcZX10 and @harisibrar1. New commits, no force-push:
Re-run on the merge (Windows 11, WinLibs GCC 15.2): On the overlaps: #9242 is ours as well (the same fork), so we'll order the two AI-assisted: yes |
jotitadev
left a comment
There was a problem hiding this comment.
Re-ran on Linux at ecd4a97 (follow-up to my earlier run at e846772). The new ReadGuestInt uses the process_vm_readv branch here, which the Windows run doesn't exercise.
Ubuntu 26.04, x86_64, i7-8700, GCC 15.2.0, Release, head merged --no-commit onto origin/main 7e2ba73.
ctest -R "guest_kernel_fcntl|guest_terminal": 3/3 pass.- Control:
DescriptorControl.cppfrom e846772, tests from ecd4a97:guest_terminalandguest_kernel_fcntl_ownerboth die with SIGSEGV (the new0x10argument cases dereferenced directly). With the head's file both pass, soprocess_vm_readvreturnsEFAULTcleanly for the unreadable pointer. - ASan+UBSan build (
-fsanitize=address,undefined -fno-sanitize-recover=undefined,-O1): with the head, both tests pass with no reports. With the e846772DescriptorControl.cpp,guest_terminalstops at the newINT_MINcase with:
DescriptorControl.cpp:464:25: runtime error: negation of -2147483648 cannot be represented in type 'int'
So the INT_MIN check in SetProcessGroup removes a real UB that UBSan flags on this path.
Written with AI assistance (Claude Code). I ran the builds and tests above on my machine.
|
@MarcZX10 the integration order is now recorded in the description:
The prerequisite stays #9557. AI-assisted: yes |
|
@jotitadev thank you, especially for the UBSan run. It shows the AI-assisted: yes |
|
Rechecked head |
harisibrar1
left a comment
There was a problem hiding this comment.
Rechecked at ecd4a97:
SetProcessGroupnow returnsESRCHforINT_MINbefore negating (DescriptorControl.cpp:470-473), and the stored owner can only be 0 or ±pid, so the remaining-GetSigioOwnernegations are safe too. The result still matches FreeBSD'sfsetown.TIOCSPGRPnow goes through the checkedReadGuestInt(:641-644), and theioctl, _ioctldebt entry names theTIOCSPGRP/TIOCGPGRPexception on a pipe above fd 2. Both of my points are addressed; it still needs to land after #9557.
Reviewed with AI assistance (Claude Code); the checks above were run locally on my machine.
MarcZX10
left a comment
There was a problem hiding this comment.
The earlier technical findings are addressed in this head: the INT_MIN case is rejected before negation, TIOCSPGRP uses checked guest copy-in, and the debt text records the pipe ioctl exception. I verified the current #9557 head f8698a0b663a95a51f59839c42e245bf0f5598e0 is an ancestor of this PR, so the declared prerequisite is present. #9242 and #9491 remain open at 028ccac1 and d1604d79; the stated no-wait agreements cover their test/debt overlaps.
Current upstream/main is 329efe1bfdaff116c57cdf62718026ee4f5521ae. This PR is 6 ahead / 56 behind, merge-tree is clean, and current-base git diff --check passes. The current conventions run has zero errors and only the reported inherited debt-length warnings. Main has updated docs/dev/debt/libkernel.md since the PR base, while the changed C++ and test files have no intervening main edits. Please refresh from current main and let conventions run again before merge; then rerun the focused terminal/owner tests if the rebase changes the tested tree. The Windows and Linux/ASan/UBSan runs in the discussion are reporter-run on the current code head; I did not run a local project build here.
|
I’ve noticed that asking you to hold this PR was unnecessary. It’s fine to keep submitting PRs; maintainers will handle them in due course. |
|
Console measurement of the pipe cases, in case it is useful for the tests. PS5, firmware 13.590.001. A payload with pid 101 in process group 91 created a pipe and called
Both ends of the pipe behave the same. For comparison, the same two calls on a Unix socket give |
MarcZX10
left a comment
There was a problem hiding this comment.
Reviewed — no blocking findings
Nothing blocks this; the points below are optional.
What I ran
Head 0bc531a0a29be507638f5edaa51645a8169aed4f, base main. Static review only: no build and no test run.
python3 repo_read.py search g_mutex core/libs/prx/libkernel/File
python3 repo_read.py search GuestCopyIn_nid_no_patch core/libs/prx/libkernel
python3 repo_read.py search HostWrite core/libs/prx/libkernel
python3 repo_read.py search GuestArenaPrepareHostRead core/libs/prx/libkernel
Files read: all 7 in files.json under reviewed, none excluded. Terminal.cpp was read in full. DescriptorControl.cpp was read through its changed hunks, plus its head lines 120–159 and 380–439. CMakeLists.txt, GuestKernelFcntlOwner.cpp, GuestTerminal.cpp and libkernel.md were read through their changed hunks only.
Results
| Check | Result | Note |
|---|---|---|
| Build | not run | No builds in this review |
| Tests | not run | Author reports a Windows run only |
| Changed code read | passed | 7 of 7 files, see above |
| Lock order and helpers | passed | No nested lock; helpers exist |
| Conventions script | not run | Author reports 0 errors |
Findings
Blocking
None.
Non-blocking
core/libs/prx/libkernel/File/src/DescriptorControl.cpp:621: on Linux,ReadGuestIntthrows whenprocess_vm_readvfails with an errno other thanEFAULT. TheFIONBIOpath now reads its argument through this helper (core/libs/prx/libkernel/File/src/DescriptorControl.cpp:654), where it usedGuestCopyIn_nid_no_patchbefore. If the host refuses the call,FIONBIOthrows instead of returning a guest error. I did not read the Linux implementation ofGuestCopyIn_nid_no_patch, so I cannot say whether the old path could fail the same way.
Sources
Not verified
- Nothing was compiled or run here. The Linux branch (
process_vm_readv) and the macOS branch (mach_vm_read_overwrite) ofReadGuestInthave not been compiled by anyone in this review. - The author's Windows run of
guest_terminalandguest_kernel_fcntl_owneris author-reported and was not repeated here. - The
lib/libc/gen/termios.cside oftcgetpgrpandtcsetpgrp, cited in the description, was not read. - The FreeBSD behaviour cited in the description and the debt entries (
sys_pipe.c) was not checked. - The overlap check and the
check_conventions.pyresult are author-reported and were not run.
To get this to a good state
Nothing is required.
Optional: on Linux, return a guest fault for any process_vm_readv failure, or document the throw in the libkernel debt file, so that a restricted host does not turn FIONBIO into an exception.
MarcZX10
left a comment
There was a problem hiding this comment.
Reviewed — changes requested
Changes are required before merge: the debt register now holds duplicate entries that contradict each other on the behaviour this PR changes.
What I ran
- Head SHA: fd2ec30 (base main).
- Read the full
diff.patch,description.md,commits.mdandmanifest.jsonfrom the review folder. - Read the house rules sections "Readable reviews", "Accuracy and evidence" and "Standard PR response format".
- Repository reads at head through the reader (
repo_read.py):searchfor the debt entries, the mutex andMaterializeinDescriptorControl.cpp, thefcntl_nid_postfixdeclarations;catofDescriptorControl.cpplines 136–150 and 396–406. - No build and no test run in this review.
Results
| Check | Result | Note |
|---|---|---|
| Full diff read | passed | 428 lines, all 8 changed files |
| Debt register at head | failed | Duplicate, contradictory entries |
Lock order in GetSigioOwner / Owner |
passed | Materialize takes no lock, so no self-deadlock |
| Build and CTest | not run | Not built here |
| Author's test report | not run | Author-reported only, not reproduced |
Findings
Blocking
docs/dev/debt/libkernel.md:189(added at diff lines 408–410): the PR adds a newtcsendbreak, tcdrain, tcflush, tcflow, tcgetpgrp, tcsetpgrpentry, but the old entry stays atdocs/dev/debt/libkernel.md:194. The new entry saystcgetpgrpandtcsetpgrpuse the pipe's SIGIO owner; the old one says they throw on a host pipe. The same goes forsem_setname(187 and 191) andkevent measurements(188 and 192): the copies disagree on what a null name returns (EINVALat 191, a throw at 187).- Failure scenario: a reader of the debt register gets two opposite answers for
tcgetpgrpon a pipe. The code now implements the pipe-owner answer, so the entry at 194 is stale. - Fix: keep one entry per export. Drop the old
tcsendbreakentry at 194 and the old copies at 191–192, and checkPthread/Posix/Sem.cppandEqueue/src/Kqueue.cppat head to decide whichsem_setnameandkeventtext is right. Those two are unrelated to this PR, so they belong in a separate change or should be removed from this one.
- Failure scenario: a reader of the debt register gets two opposite answers for
Non-blocking
description.mdsays the terminal entry sits atdocs/dev/debt/libkernel.md:128and that its throw clause is replaced. The diff touches lines 187–189 and 355–361 only, and the old entry is at 194. Point the description at the real lines once the duplicates are gone.
Not verified
- Build, CTest, and the new
guest_kernel_fcntl_ownertest under CMake on any host. - The author's test results (
guest_terminalandguest_kernel_fcntl_owner, 3 of 3 runs, and the negative controls at lines 120 and 121). Author-reported only. - The author's character counts for the terminal debt entry (1047 against 1131).
- Host behaviour of the calls the code relies on:
process_vm_readvon Linux,mach_vm_read_overwriteon macOS,ReadProcessMemoryon Windows. std::erase_ifonstd::mapand the<map>and<memory>includes; theENVIRONMENT_MODIFICATIONblock inCMakeLists.txtfor Windows.- The PS5 claims in the debt entries. These cannot be checked from the repository.
- The overlap claims against #9557, #9242, #9491, #9131, #7121, #8520, #9019 and #9561.
- Files read: the four review-folder files and the diff in full, plus the head text of
DescriptorControl.cppat 136–150 and 396–406. Not read: thefiles/full texts,files.json, the head text ofTerminal.cpp,CMakeLists.txt, the two test files, andApi.hppbeyond the grep. Lockfiles, minified files,dist/,build/and.svgare excluded from the diff by rule.
To get this to a good state
- Remove the duplicate debt entries so each export has one entry with one answer (see the blocking finding).
- Fix the description's pointer to the debt entry.
- On the new head, rerun
guest_terminalandguest_kernel_fcntl_ownerand attach the output.
|
This passed review on fd2ec30, but it now conflicts with main in DescriptorControl.cpp. Please merge main into the branch and push; it goes back to the merge queue on the new head. |
|
@DavidSamir the code is fine; one docs fix. This PR adds ten lines straight to |
ligmaSec
left a comment
There was a problem hiding this comment.
Compiled the changed descriptor and terminal sources with GuestKernelFcntlOwner and GuestTerminal against the existing Linux PRXs. Both fixtures passed, including process-group handling on the pipe endpoints.
AI-assisted: yes.
What
Builds on #9557. Until #9557 merges, this PR's diff also shows its commit. This PR's own change is the last commit.
tcgetpgrpandtcsetpgrpthrew on a host pipe or FIFO above descriptor 2, because the pipe's SIGIO owner was not modelled. #9557 now keeps that owner, so they follow FreeBSD 11:tcsetpgrpis_ioctl(fd, TIOCSPGRP, &pgrp)andtcgetpgrpis_ioctl(fd, TIOCGPGRP, &s)(lib/libc/gen/termios.c:82-99).pipe_ioctlanswersTIOCSPGRPwithfsetown(-pgrp)andTIOCGPGRPwith-fgetown()(sys/kern/sys_pipe.c:1402-1410). So these use the same owner asF_SETOWN/FIOSETOWN, with the sign reversed: afterF_SETOWN(pid),tcgetpgrpreturns-pid.How:
Terminal.cpp: on a host pipe or FIFO at descriptor 3 or above,tcgetpgrpandtcsetpgrpcallFile::PipeProcessGroupandFile::SetPipeProcessGroupinstead of throwing. The samefsetownrules apply: 0 clears, our own process group is stored, and any other group givesESRCH. Descriptors 0 to 2 keep their documentedENOTTY, since they're taken as non-terminals that aren't pipes.DescriptorControl.cpp: the two helpers. The rawTIOCSPGRP(0x80047476) andTIOCGPGRP(0x40047477) ioctls now give the same answer on such a pipe, ahead of the terminal-groupENOTTY.docs/dev/debt/libkernel.md:128) replaces its "throw on a host pipe" clause.Tested
Windows 11 Pro 26200, WinLibs GCC 15.2.0 r7, Release. This is CPU-side, so no GPU is involved.
guest_terminal(core/libs/tests/GuestTerminal.cpp): in the pipe loop, the two checks that expected a throw now check:ESRCHfor another group, with the owner kept;F_SETOWN(pid)makingtcgetpgrpreturn-pid, and the reverse;TIOCSPGRP/TIOCGPGRPioctls;Results:
guest_terminalandguest_kernel_fcntl_ownerpass, 3 of 3 runs.guest_kernel_fcntlandguest_duppass.tools/check_conventions.py: 0 errors. It warns that the terminal debt entry (now 1047 characters, from 1131 onmain) and thefcntlentry are long; both were already over 1000 onmain.tools/pr_overlap.py --branchplusgit merge-tree. The code merges cleanly with fix(libkernel_sys): fail fchdir on a pipe or socket with EINVAL #9131, feat(libkernel): report open, read and close notes on watched files #7121, feat(libkernel): enumerate archived app directories #8520, feat(libkernel_sys): implement pwrite through the shared POSIX provider #9019 and feat(libkernel): sync each write on a file whose O_FSYNC flag is set #9561. Textual conflicts:tcgetsid, also ours) addstcgetsidchecks right after the twoGuestTerminal.cpplines this PR has to change, and edits the same debt entry. Agreed with its author on the board: no dependency either way; whichever lands second keeps both blocks of checks and both debt changes.libkernel.md:127-129). fix(libkernel): hand signalled semaphore tokens to queued waiters #9491's author (EagleBearerYS) agreed on this PR that neither waits: whichever merges second keeps both entries.fcntldebt line (:218) comes from feat(libkernel): keep the SIGIO owner of a pipe for F_GETOWN and F_SETOWN #9557 and is shared with fix(libkernel): report O_RDWR from F_GETFL on both ends of a pipe #8544, fix(libkernel): change O_APPEND on a Windows file through fcntl #9256, fix(libc): fail with ENOTDIR for a file named with a trailing slash on Windows #9109 and feat(libkernel): sync each write on a file whose O_FSYNC flag is set #9561.Checklist
main; no other open PR implements the same functionsNotImplemented_nid_no_patch); silent stubs are listed under Silent stubs in their library's technical debt file.mdfiles or images added to the repository (attach them to this PR)