fix(libc): open NUL on standard descriptors without a stream on Windows #9945

Merged
p3n1cillin merged 8 commits from upstream-pr/9945 into main 2026-10-24 10:40:28 +00:00
p3n1cillin commented 2026-10-22 23:08:25 +00:00 (Migrated from github.com)

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_osfhandle is -2. The UCRT documents that value for a standard descriptor with no stream. It happens to a detached process and to a --windows-gui executable started from Explorer. DescriptorIsOpen counts such a descriptor as open, but GetFileType fails on it, so fcntl throws:

terminate called after throwing an instance of 'std::runtime_error'
  what():  fcntl: descriptor 0 has an unknown Windows file type, error=6

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/null on 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, which FileStream calls before it captures a descriptor:

  • A descriptor 0 to 2 whose handle is -2 or -1 is opened on NUL, read-only for 0 and write-only for 1 and 2.
  • The UCRT stream is then attached to it with _wfreopen, because _dup2 alone leaves the stream's _fileno at -2. A stream bound to the raw descriptor closes itself through _wfreopen, so the descriptor is not closed twice.
  • A stream that is already valid on another descriptor is kept, and the raw descriptor is restored from it rather than reopened.

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 main 2ace103a9c plus these commits:

  • guest_closed_standard_descriptors_windows starts 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 guest fcntl answers for F_GETFD and F_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. On main the 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_windows loads 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.
  • All 360 tests that core/libs/prx/libc and core/libs/prx/libkernel register pass.
  • Halo: Combat Evolved (PPSA99500), started on a separate desktop with a null stdin handle and stdout and stderr redirected to a file. On main it aborts at 0 s with the fcntl error above. With this PR it runs its start-up, with SDL 2.30.12, RADV's vkCreateInstance and 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: libc builds, and guest_closed_standard_descriptors and the three guest_standard_streams tests pass. The non-Windows branch of the initializer is unchanged.

tools/check_conventions.py --base origin/main passes. tools/pr_overlap.py --branch reports only #9596, which edits the fgetws debt line next to this entry's line. #8150 and #7201 also touch StandardStreams.cpp, in other functions.

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: PPSA99500 (Halo: Combat Evolved) when started without a standard input handle
  • GPUs tested: n/a
  • AI-assisted: yes (Claude Code)

🤖 Generated with Claude Code

<!-- greyfinch-sync:boykopovar/AnyPS5:issue:4975348336 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/9945). Originally posted by [p3n1cillin](https://github.com/p3n1cillin) on 2026-10-22T23:08:25Z. --- ### What A Windows process started without a standard handle has descriptors 0 to 2 whose `_get_osfhandle` is -2. [The UCRT documents](https://learn.microsoft.com/en-us/cpp/c-runtime-library/reference/get-osfhandle) that value for a standard descriptor with no stream. It happens to a detached process and to a `--windows-gui` executable started from Explorer. `DescriptorIsOpen` counts such a descriptor as open, but `GetFileType` fails on it, so `fcntl` throws: ``` terminate called after throwing an instance of 'std::runtime_error' what(): fcntl: descriptor 0 has an unknown Windows file type, error=6 ``` 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)`](https://github.com/thelinkin3000/halo-ce-universal/blob/201e0f18011a30ea6d098ad249bf17eba005d653/port/ps5/host/host_main.c#L407-L429) before opening anything, because, as its comment says, a title starts without them. On Linux and macOS, libc's static initializer already opens `/dev/null` on 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`, which `FileStream` calls before it captures a descriptor: - A descriptor 0 to 2 whose handle is -2 or -1 is opened on `NUL`, read-only for 0 and write-only for 1 and 2. - The UCRT stream is then attached to it with `_wfreopen`, because `_dup2` alone leaves the stream's `_fileno` at -2. A stream bound to the raw descriptor closes itself through `_wfreopen`, so the descriptor is not closed twice. - A stream that is already valid on another descriptor is kept, and the raw descriptor is restored from it rather than reopened. 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 `main` 2ace103a9c plus these commits: - `guest_closed_standard_descriptors_windows` starts 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 guest `fcntl` answers for `F_GETFD` and `F_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. On `main` the 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_windows` loads 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. - All 360 tests that `core/libs/prx/libc` and `core/libs/prx/libkernel` register pass. - Halo: Combat Evolved (PPSA99500), started on a separate desktop with a null stdin handle and stdout and stderr redirected to a file. On `main` it aborts at 0 s with the `fcntl` error above. With this PR it runs its start-up, with SDL 2.30.12, RADV's `vkCreateInstance` and 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: `libc` builds, and `guest_closed_standard_descriptors` and the three `guest_standard_streams` tests pass. The non-Windows branch of the initializer is unchanged. `tools/check_conventions.py --base origin/main` passes. `tools/pr_overlap.py --branch` reports only #9596, which edits the `fgetws` debt line next to this entry's line. #8150 and #7201 also touch `StandardStreams.cpp`, in other functions. ### 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: PPSA99500 (Halo: Combat Evolved) when started without a standard input handle - [x] GPUs tested: n/a - [x] AI-assisted: yes (Claude Code) 🤖 Generated with [Claude Code](https://claude.com/claude-code)
harisibrar1 commented 2026-10-23 03:24:19 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by harisibrar1 on 2026-10-23T03:24:19Z.

Review: COMMENTED


Checked against main 3875f8c8ff and the UCRT _get_osfhandle docs, plus a standalone probe I compiled with WinLibs GCC 15.2.0 (UCRT):

  • The approach matches main's Linux/macOS initializer (StandardStreams.cpp on main only has the #ifndef _WIN32 branch), and it still merges cleanly. Thanks for the detached-child test.
  • I ran a standalone exe (not the libc DLL) that runs your initializer in a static initializer and starts itself DETACHED_PROCESS with 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, and fgetc(stdin) returns -1 with ferror set.
  • Main's FileStream takes its guest descriptor from _fileno (FileStream.hpp:78-82), so _Stdout/_Stderr/input would 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 plain freopen("NUL", ...) landed on fds 3/4/5 in my probe, so it doesn't solve it as-is.
  • Optional: a check in the child that fputs to stdout succeeds would cover this.

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

<!-- greyfinch-sync:boykopovar/AnyPS5:review:5623529199 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/9945#pullrequestreview-5623529199). Originally posted by [harisibrar1](https://github.com/harisibrar1) on 2026-10-23T03:24:19Z. **Review:** COMMENTED --- Checked against main 3875f8c8ff and the UCRT `_get_osfhandle` docs, plus a standalone probe I compiled with WinLibs GCC 15.2.0 (UCRT): - The approach matches main's Linux/macOS initializer (`StandardStreams.cpp` on main only has the `#ifndef _WIN32` branch), and it still merges cleanly. Thanks for the detached-child test. - I ran a standalone exe (not the libc DLL) that runs your initializer in a static initializer and starts itself `DETACHED_PROCESS` with 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, and `fgetc(stdin)` returns -1 with `ferror` set. - Main's `FileStream` takes its guest descriptor from `_fileno` (`FileStream.hpp:78-82`), so `_Stdout`/`_Stderr`/`input` would 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 plain `freopen("NUL", ...)` landed on fds 3/4/5 in my probe, so it doesn't solve it as-is. - Optional: a check in the child that `fputs` to `stdout` succeeds would cover this. Reviewed with AI assistance (Claude Code); the checks above were run locally on my machine.
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!9945
No description provided.