fix(libkernel): report a host-only descriptor as not open to fcntl and ioctl #12474
Loading…
Reference in a new issue
No description provided.
Delete branch "upstream-pr/12474"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
Imported from boykopovar/AnyPS5. Originally posted by milescoviello on 2026-10-28T15:12:55Z.
What
Dusklight (PPSA99305 1.0.0, the catalog's Twilight Princess port) aborts 8 s in, after the first menu press:
Just before, the title tries to open
/proc/self/fd, which fails, and then callsfcntlon descriptor numbers it never opened. Guest descriptors are host descriptor numbers here, so one of those numbers is a descriptor the emulator's own process holds, of a kind a guest can't create: an anonymous inode such as an epoll or an eventfd (libkernel's kevent helpers create both, and host libraries hold others). I didn't identify which component held descriptor 87.ClassifyinFile/src/DescriptorControl.cppthrows for any kind it doesn't recognise, sofcntlends the title.How
On Linux,
Classifynow reports a descriptor of any other kind as not open, as it already does for a closed one, sofcntlandioctlreturnEBADF. A guest can't create such a descriptor there: its files, directories, pipes, sockets and character devices (guest shared memory is a regular file, and guest kqueues are backed by/dev/nulldescriptors) are the kindsClassifykeeps, so the rest belong to the host. On the console the guest's descriptor table holds only what the title opened, and FreeBSD'sfcntlandioctlreturnEBADFfor any other number. The host descriptor itself is left alone.Other hosts keep the throw. On macOS a guest named
shm_opendescriptor also reportsS_IFMT0, so the same rule would hide a descriptor the guest owns there (rooklz's table). On Windows, guest descriptors are CRT descriptors, and the emulator's own objects are handles outside that table.Tested
Linux (Ubuntu 26.04.1, GCC 15.2).
main52c09d567d(MarcZX10's review): headf83a259550.git range-diffshows both commits unchanged.mainhad changedDescriptorControl.cppandGuestKernelFcntl.cpp, and the rebase merged cleanly.^(guest_kernel_fcntl|guest_kernel_fcntl_sync|guest_closed_standard_descriptors|guest_socket_descriptor_flags|guest_socket_descriptors|guest_dup|guest_shm_open|guest_named_shm_.*|guest_stdio_variadic)$pass 3 of 3.main'sDescriptorControl.cppand this PR's test (35868b94bf),guest_kernel_fcntlaborts:fcntl: descriptor 6 is neither a file, a directory, a pipe, a socket nor a character device.^guest_tests pass at this head.Earlier, on
main04fefebe90:guest_kernel_fcntl(Linux only): for a host epoll descriptor and a host eventfd,fcntlF_GETFD,F_GETFLandF_SETFDandioctlFIONREADreturn -1 withEBADF, and the host descriptor keeps itsFD_CLOEXEC. A guest namedshm_opendescriptor still answers:F_GETFLgivesO_RDWR, andF_SETFDthenF_GETFDgive backFD_CLOEXEC. It passes 3 of 3 at this head (e821de8aca). The first version of the test alone onmain(cfa15a24ab) fails, asfcntlthrows.^guest_tests pass at this head.5396a73723, whose Linux code is this head's, 120 s with pad input: it runs the full 120 s past thefcntlcall. Its launcher menu and settings tabs follow the input, and it reports "No disc image found", as there is none here. Onmain446bd8776cthe same run aborts at 8 s as above.guest_kernel_fcntlpasses 3 of 3 at this head and onmain04fefebe90(the new case is Linux only).Not tested on macOS: as rooklz found,
SharedMemory.cppdoesn't build there onmain.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)fcntlabort)🤖 Generated with Claude Code
Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-10-28T15:27:21Z.
Review: APPROVED
Reviewed exact head
5396a7372354d247c689d201e08886917b4a7610. On non-Windows hosts,Classifynow maps anfstatresult with no supported guest descriptor kind to “not open”; actualEBADFhandling remains unchanged, and recognized file, directory, pipe, socket and character-device paths are preserved. The Linux-only regression checks epoll/eventfd behavior throughfcntlandioctland confirms host descriptor flags remain intact. The author reports the focused test and all 948 guest tests on Linux plus the focused Windows run; my exact-head syntax, diff-check and merge-tree checks pass. Hosted Windows and policy checks are green; platform jobs marked skipped are not treated as executed. I found no blocker.Imported from boykopovar/AnyPS5. Originally posted by rooklz on 2026-10-28T16:11:48Z.
Review: COMMENTED
Reviewed head
5396a73723on macOS 26.2, host side only. It merges cleanly ontomainf8f5b24b77andcheck_conventionspasses. The Linux reasoning holds. I have one scope question about macOS.What
fstatreports for each descriptor kind (small C probes; Linux was agcc:latestcontainer):S_IFMTS_IFMTClassifyat this headmemfd_create//dev/shm(guestshm_openon Linux)01000000EBADF)O_PATH|O_NOFOLLOWon a symlink0120000kqueue()010000shm_openobject (arm64 and x86_64)0/dev/null020000020000On Linux every descriptor the guest can create still lands in a kept kind, because guest shm is
S_IFREG. So the premise "the rest belong to the host" is true there.On macOS the only new outcome this PR produces is for
S_IFMT == 0, and that is exactly what a guest namedshm_openreturns there.OpenNamedFile's non-Windows branch (SharedMemory.cppL599-637) calls the host::shm_open. Before this PR,fcntl(F_GETFD/F_GETFL/F_SETFD)orioctlon such a descriptor threw, which is a loud and recorded gap. At this head it quietly returnsEBADFfor a descriptor the guest opened, while FreeBSDkern_fcntlanswersF_GETFD/F_GETFLon a shm descriptor.IsHostSharedMemoryDescriptorreturnsfalseon macOS (#elsearm), so nothing else catches it first. The host side agrees:fcntl(F_GETFD)=1andF_GETFL=2on that descriptor.Two caveats. I can't run this end to end on macOS: on
main, libkernel'sCreateAnonymousFileusesmemfd_create/MFD_CLOEXECunguarded, soSharedMemory.cppdoesn't compile there (-fsyntax-only:use of undeclared identifier 'MFD_CLOEXEC'), and CI's macOS lanes are relinker-only. Also, the Dusklight evidence is Linux only.Suggestion: limit the new
return std::nullopt;to Linux, where anonymous inodes are the only kinds it reaches, and keep the throw elsewhere:You could also keep it as is and say in the body/debt that on macOS a guest named shm descriptor now reads as not open. Either is fine with me, but the current body says "On Linux and macOS ... A guest can't create such a descriptor", which isn't true on macOS.
Non-blocking, and present before this PR: on macOS a host
kqueue()reportsS_IFIFO, so host-only kqueues (FileEvent.cppL449) are classified asPiperather than not open. TheEBADFreasoning in this PR wouldn't reach them there.AI-assisted: yes
Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-10-28T16:14:36Z.
Review: CHANGES_REQUESTED
After my earlier review, I checked the macOS descriptor case raised in the latest review. This head changes every unrecognized
fstattype tonullopt, andOpenNamedFileuses hostshm_openon macOS.IsHostSharedMemoryDescriptoronly recognizes the Linux and Windows implementations, so a guest-created named shared-memory descriptor on macOS reaches the newEBADFpath infcntl/ioctlinstead of remaining a valid guest descriptor. The PR description currently claims the change is safe on macOS, but that case contradicts the claim.Please either scope this fallback to Linux, where this PR's epoll/eventfd regression is demonstrated, or explicitly recognize and preserve guest-created shared-memory descriptors on macOS before classifying unknown host descriptors as closed. Please add a regression for the chosen behavior and correct the platform claim. Until that is addressed I am changing my review to request changes; the Linux behavior remains supported by the reported tests.
Imported from boykopovar/AnyPS5. Originally posted by XairRrR on 2026-10-29T03:50:44Z.
Review: CHANGES_REQUESTED
Reviewed — changes requested
The Linux-only change to
Classifyis correct and the new regression fails without it, but theUnblocksline names no Game bring-up discussion, which CONTRIBUTING requires.What I ran
e821de8aca108cd1ab22688e6f19285c81ec2c9bmain:b433c40f2f46aa2e59e19806010a839ac8572d64mainwith--no-ffin a clean worktree (no conflicts). Release, GCC 16.2.1, Ninja, on Arch Linux 7.2.11-zen, Intel Core i9-14900KF.Results
maingit diff --checkcheck_conventions.pyAll conventions checks passed.libs, full treeguest_kernel_fcntl0.00 s^guest_(983 tests)guest_net_ctl), 2 fail:guest_freebsd_clocksandguest_socket_options_multicast. Both fail the same way withmain'sDescriptorControl.cpprestored, so this change does not cause them. The multicast one fails because the sandbox has no network.guest_kernel_fcntlaborts without the production changeArea run
guest_socket_options_multicast:joining 224.0.0.251 through INADDR_ANY failed (errno 5).Negative control
I restored only
core/libs/prx/libkernel/File/src/DescriptorControl.cppfrommainand kept the new test.guest_kernel_fcntlthen aborts on the first host-only descriptor:Output
When I put the merged file back, the test passes again.
Findings
Blocking
Unblocksline has no Game bring-up discussion. CONTRIBUTING.md L64 says a PR that fixes a stop seen on a title names "the title ID and its Game bring-up discussion (Unblocks: PPSA01325 #3232)". The body hasUnblocks: Dusklight (PPSA99305) past its fcntl abort, with no discussion number. I found no Game bring-up discussion for PPSA99305 or Dusklight. Its only mention is in the import list of #7971, which is in Ideas. Fix: open a Game bring-up discussion for PPSA99305 (with thefcntlabort log) and change the line toUnblocks: PPSA99305 #<number>. This is a change to the PR body only; the code needs nothing.Non-blocking
fcntlhandles. It says nothing about a descriptor of another kind. With this PR, such a descriptor answersEBADFon Linux and still throws on Windows and macOS (DescriptorControl.cpp L304-L308). The rule also only hides host descriptors whose kind no guest call creates. The runtime's own epoll, eventfd, inotify and userfaultfd descriptors (EdgeWatch.cpp,FileEvent.cpp,GuestWriteWatch.cpp) now read as closed. Its own regular files, pipes, sockets and character devices (GPU device nodes, for example) still answerfcntlas open guest descriptors, where a console would returnEBADF. Dusklight importsexecv,execvpandposix_spawnp, so its loop over descriptors after/proc/self/fdfails looks like a pre-spawn cleanup. If it closes whatfcntlaccepts, it could still reach those. Adding a sentence to that entry would record this gap. L247 already notes that the host table holds the runtime's own files.The earlier macOS concern (rooklz and MarcZX10, on
5396a73723) is resolved at this head. The fallback is#ifdef __linux__, other hosts keep the throw, and the test now checks that a guest namedshm_opendescriptor still answersF_GETFL,F_SETFDandF_GETFD. On Linux I found nothing a guest can open thatfstatreports as anything but a regular file, directory, FIFO, character device or socket. The onlyO_PATHand anonymous-inode descriptors undercore/are the runtime's own.Not verified
guest_freebsd_clocks: I did not find out why it fails on this machine. It fails the same way withmain'sDescriptorControl.cpp.To get this to a good state
Unblocksline.docs/dev/debt/libkernel.mdabout descriptors of other kinds (EBADFon Linux, a throw elsewhere) and about the runtime's own files, pipes, sockets and devices still answering as open.Imported from boykopovar/AnyPS5. Originally posted by XairRrR on 2026-10-29T04:03:57Z.
Review: APPROVED
Re-checked — approved
The PR body's
Unblocksline now names the Game bring-up discussion #12708 for PPSA99305, which resolves my only blocking finding; the code andmainare the same commits I built and tested last time.What changed since my last review
Unblocks: PPSA99305 #12708 (Dusklight, past its fcntl abort).fcntlabort log frommain, the setup, and a link to this PR.e821de8aca.main: it has not moved since my last review (b433c40f2f), so last time's merge, build, test and negative-control results apply unchanged.What I ran
e821de8aca108cd1ab22688e6f19285c81ec2c9bmain:b433c40f2f46aa2e59e19806010a839ac8572d64I did not rebuild or rerun the tests. No code, test or build file changed, and both SHAs are the ones I tested in my last review.
Previous findings
Unblocksline has no Game bring-up discussion. Resolved. The line follows the form in CONTRIBUTING.md L64 (Unblocks: PPSA01325 #3232). #12708 is a Game bring-up discussion titled "Game bring-up: Dusklight (PPSA99305): …", and it lists this PR under "Pull requests from this title".EBADFon Linux and still throws elsewhere. It also does not say that the runtime's own files, pipes, sockets and devices still answer as open. This does not block the merge; a follow-up could add it.New findings
Blocking
None.
Non-blocking
None.
Not verified
Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-10-29T05:19:15Z.
Review: CHANGES_REQUESTED
Re-checked — changes requested
The author corrected the macOS descriptor classification and added the title bring-up discussion requested by review; the remaining blocker is that current
mainchanged the same implementation and test files after this branch’s base, so the integrated behavior and regression test need a fresh run.What I ran
e821de8aca108cd1ab22688e6f19285c81ec2c9bwith currentmain52c09d567d2fa453494dd720fcf53a98149468e4.git merge-tree --write-treeand inspected the same-path changes on currentmain.Unblocks: PPSA99305 #12708, the prior Linux/macOS findings, and hosted checks.Results
ee3f1a0826...04fefebe901d9ff4b29acc7d6c4c28d66643d86f; current main has 332 later commitscore/libs/prx/libkernel/File/src/DescriptorControl.cppandcore/libs/tests/GuestKernelFcntl.cppUnblocksline was fixedRaw output
Negative control
mainchanged these files.Findings
Blocking
mainchanges bothDescriptorControl.cppandGuestKernelFcntl.cppafter this branch’s base. The preview merges cleanly, but the previous focused build/test did not include those current changes. Please refresh the branch and rerun the focused descriptor/fcntl test on the integrated head before this review can be cleared.Non-blocking
Unblockschecklist now points to the PPSA99305 Game bring-up discussion #12708. The merge preview is clean.Sources
Not verified
To get this to a good state
Rebase onto current
main, rerun the focused descriptor/fcntl test, and update the Tested section with the exact head and base. The previous functional and checklist findings are otherwise resolved.Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-10-29T05:37:25Z.
Review: APPROVED
Re-checked — approved
The current-main rebase resolves my integration blocker. The Linux-only fallback, preservation of guest
shm_opendescriptors, title context, and regression coverage now agree with the reported behavior.What I ran
f83a259550f2892184ff476913a0f95b3aa81dfdagainst currentmain52c09d567d2fa453494dd720fcf53a98149468e4.git merge-tree --write-tree,git diff --check, andpython3 tools/check_conventions.py --base refs/review/main-20261029c --head refs/review/pr-12474-latest.Results
git diff --checkguest_kernel_fcntlregression aborts withmain’s implementation^guest_suiteThe focused regression and clean integration are sufficient for approval. The full suite and Windows job remain pending; I am not counting either as completed.