fix(libc): open NUL on standard descriptors without a stream on Windows #9945
Loading…
Reference in a new issue
No description provided.
Delete branch "upstream-pr/9945"
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 p3n1cillin on 2026-10-22T23:08:25Z.
What
A Windows process started without a standard handle has descriptors 0 to 2 whose
_get_osfhandleis -2. The UCRT documents that value for a standard descriptor with no stream. It happens to a detached process and to a--windows-guiexecutable started from Explorer.DescriptorIsOpencounts such a descriptor as open, butGetFileTypefails on it, sofcntlthrows:Halo: Combat Evolved (PPSA99500, catalog build-113) stops there at start-up. Its PS5 host checks descriptors 0 to 2 with
fcntl(fd, F_GETFD)before opening anything, because, as its comment says, a title starts without them.On Linux and macOS, libc's static initializer already opens
/dev/nullon a standard descriptor that is closed when libc loads. The libc debt entry said the Windows case was not handled. This PR adds the Windows side,PrepareStandardStreams_nid_no_patch, whichFileStreamcalls before it captures a descriptor:NUL, read-only for 0 and write-only for 1 and 2._wfreopen, because_dup2alone leaves the stream's_filenoat -2. A stream bound to the raw descriptor closes itself through_wfreopen, so the descriptor is not closed twice.The stream attachment and the follow-up cases come from @Queuse's branch, cherry-picked with authorship kept, after @harisibrar1 found the stream gap. The debt entry describes both hosts and links the title's check. What the console gives a title as its standard descriptors stays unmeasured. As on Linux, a title here finds the descriptor open where the Halo port expects it closed, and it leaves that descriptor alone.
Tested
Windows 11 Pro (26300), WinLibs GCC 15.2.0, Release, full build on
main2ace103a9cplus these commits:guest_closed_standard_descriptors_windowsstarts itself as a detached child without stdin, without stdout, without stderr, and without all three. For each missing descriptor the child checks a character-device handle, the guestfcntlanswers forF_GETFDandF_GETFL, and that the native and guest streams use that descriptor. It also checks that writes and flushes on stdout and stderr succeed, and that stdin reads EOF without an error. Onmainthe child exits on the -2 handle. A non-detached child doesn't show the problem, because Windows gives a new console's process console handles in place of null ones.guest_closed_standard_streams_load_windowsloads libc into a process whose standard descriptors were closed, and into one whose stdout is already valid on a separate descriptor. It checks that no invalid-parameter callback runs, that the valid stdout is kept, and that native and guest writes succeed.core/libs/prx/libcandcore/libs/prx/libkernelregister pass.mainit aborts at 0 s with thefcntlerror above. With this PR it runs its start-up, with SDL 2.30.12, RADV'svkCreateInstanceand glslang. It then quits on its own check for the game files:the game needs /data/halo, with the game's disc image in it. I don't have the disc image, so nothing past that point was run.WSL Ubuntu 26.04.1, GCC 15.2.0:
libcbuilds, andguest_closed_standard_descriptorsand the threeguest_standard_streamstests pass. The non-Windows branch of the initializer is unchanged.tools/check_conventions.py --base origin/mainpasses.tools/pr_overlap.py --branchreports only #9596, which edits thefgetwsdebt line next to this entry's line. #8150 and #7201 also touchStandardStreams.cpp, in other functions.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)🤖 Generated with Claude Code
Imported from boykopovar/AnyPS5. Originally posted by harisibrar1 on 2026-10-23T03:24:19Z.
Review: COMMENTED
Checked against main
3875f8c8ffand the UCRT_get_osfhandledocs, plus a standalone probe I compiled with WinLibs GCC 15.2.0 (UCRT):StandardStreams.cppon main only has the#ifndef _WIN32branch), and it still merges cleanly. Thanks for the detached-child test.DETACHED_PROCESSwith null std handles. After the_dup2,_get_osfhandle(0..2)are real handles and_write(1)succeeds. But_fileno(stdin/stdout/stderr)stays -2:fputs(stderr)returns -1 with errno 9,fflush(stdout)returns -1 with errno 9, andfgetc(stdin)returns -1 withferrorset.FileStreamtakes its guest descriptor from_fileno(FileStream.hpp:78-82), so_Stdout/_Stderr/inputwould keep descriptor -1, and guest stdio on them would still give EBADF, where on Linux the same streams write to/dev/null. Could the debt entry say this, or could the streams be re-attached? A plainfreopen("NUL", ...)landed on fds 3/4/5 in my probe, so it doesn't solve it as-is.fputstostdoutsucceeds would cover this.Reviewed with AI assistance (Claude Code); the checks above were run locally on my machine.