fix(libc): open closed standard descriptors on /dev/null when libc loads #6107
Loading…
Reference in a new issue
No description provided.
Delete branch "upstream-pr/6107"
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 YossiYad on 2026-10-14T22:19:53Z.
What
A converted title started with its standard input closed (a launcher or a shell that closes it,
./app.elf <&-) gets one of AnyPS5's own descriptors as its standard input. The first descriptor the libraries open takes the lowest free number, and on Linux that is the write watch'suserfaultfd:/proc/<pid>/fd/0readsanon_inode:[userfaultfd]about 100 ms in. Yamagi Quake II (PPSA99007) callsfcntlon descriptor 0 at start-up and stops onThe same happens to descriptors 1 and 2: a guest write to a closed stdout would go to whatever AnyPS5 opened in its place.
StandardStreams.cppopens/dev/nullon each of descriptors 0, 1 and 2 that is closed when libc loads, read-only for 0 and write-only for 1 and 2, the way glibc'scheck_fds.cdoes for set-user-ID programs. libc is loaded and initialized before the libraries that depend on it, so this runs before any of them opens a descriptor. Descriptors that are open are not touched. Linux and macOS only.guest_closed_standard_descriptors: the test runs itself again with stdin, stdout, or all three closed, and the child checks that each closed descriptor is/dev/nullwith the expected access mode by the timemainruns, and that the guestfcntlon descriptor 0 succeeds. If a libc initializer opened a descriptor before this one, it would take descriptor 0 and the check would fail.Title. Yamagi Quake II (PPSA99007, catalog release) with
./app.elf <&-: onmainit aborts on thefcntlthrow above in each of 3 runs. With this PR it no longer aborts and runs the same as when started with</dev/null(in both cases it then leaves on its own after start-up and tears down cleanly with status 0).Tested
Linux (Bazzite 44, GCC 16.2.1, Ninja, Release,
-DBUILD_TESTING=ON), RX 7700 XT (RADV), based onmain922abe6a4:ctest: 1187 of 1211 pass and 21 are skipped (display tests).agc_driver_tessellationandapplication_heap_lazysegfault, andagc_host_import_sparsefails, on unchangedmainhere too (#5759 fixes the last one).guest_closed_standard_descriptorspasses 5 of 5 (--repeat until-fail:5). With the previousStandardStreams.cppit fails on its first case (child exited with 74: descriptor 0 is not/dev/null).main4808d9c20, hidden display, no controller): as above.tools/check_conventions.py --base upstream/mainpasses.tools/pr_overlap.pyreports #5816 indocs/dev/debt/libc.mdonly (a neighbouring line). Windows and macOS were not built here.Checklist
main; no other open PR implements the same functions.mdfiles or images added to the repositoryImported from boykopovar/AnyPS5. Originally posted by github-actions[bot] on 2026-10-14T22:25:09Z.
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.
core/libs/prx/libkernel/CMakeLists.txtcore/libs/prx/libc/src/StandardStreams.cppcore/libs/prx/libkernel/CMakeLists.txtdocs/dev/debt/libc.mddocs/dev/debt/libc.mdcore/libs/prx/libkernel/CMakeLists.txtcore/libs/prx/libc/src/StandardStreams.cppcore/libs/prx/libkernel/CMakeLists.txtdocs/dev/debt/libc.mdcore/libs/prx/libkernel/CMakeLists.txtdocs/dev/debt/libc.mdcore/libs/prx/libkernel/CMakeLists.txtdocs/dev/debt/libc.mddocs/dev/debt/libc.mddocs/dev/debt/libc.mdcore/libs/prx/libkernel/CMakeLists.txtcore/libs/prx/libkernel/CMakeLists.txtcore/libs/prx/libkernel/CMakeLists.txtcore/libs/prx/libkernel/CMakeLists.txtcore/libs/prx/libkernel/CMakeLists.txtcore/libs/prx/libc/src/StandardStreams.cppcore/libs/prx/libkernel/CMakeLists.txtcore/libs/prx/libkernel/CMakeLists.txtdocs/dev/debt/libc.mdcore/libs/prx/libkernel/CMakeLists.txtImported from boykopovar/AnyPS5. Originally posted by YossiYad on 2026-10-15T16:22:34Z.
Rebased onto current main (head
a7ef31e10). The only conflict was in docs/dev/debt/libc.md, where main had expanded the neighbouring*_unlockedandfgetwslines; I kept those as they are on main and added the "Closed standard descriptors" line after them. The fix and test commits are unchanged.Checks are green and it merges cleanly. Could you take a look when you have a moment, and merge if it looks right?
Imported from boykopovar/AnyPS5. Originally posted by screamm on 2026-10-15T22:24:18Z.
Windows check of head
a7ef31e10on main44df21632(Windows 11 Pro 26220, RTX 4070 596.21, MinGW GCC 15.2). This PR is Linux and macOS code, so this is a no-regression run only.guest_closed_standard_descriptorsdoes not exist on Windows (ctest -Nlists no such test; it is registered underif(UNIX)and usesfork/execv), so no 3x run or negative control is possible. The initializer inStandardStreams.cppis under#ifndef _WIN32, so the Windows code path is not covered; the debt line already says that../app.exe 0<&-:main13934 frames, with this PR 13941 frames.cmd /c "app.exe < NUL":main13562 frames, with this PR 13450 frames.mainon Windows does not hit thefcntl: descriptor 0 ...throw from the description with stdin closed, so there is nothing for the PR to fix there.Written with Claude Code assistance (validation by a subagent, reviewed in this session).
Imported from boykopovar/AnyPS5. Originally posted by adriadam10 on 2026-10-16T23:16:35Z.
Checked the initializer in
StandardStreams.cpp, the newguest_closed_standard_descriptors(passes; it fails without the fix) and the debt entry. Build and the libc/libkernel ctest scope are green on the PR merged onto main; merging.Imported from boykopovar/AnyPS5. Originally posted by YossiYad on 2026-10-17T11:44:41Z.
@adriadam10 thanks for checking it. It's still open; it merges cleanly onto current
mainand all checks are green, so it's ready whenever you have a moment to merge.