fix(libc): preserve host standard handles on Windows redirects #4640

Merged
MarcZX10 merged 1 commit from upstream-pr/4640 into main 2026-10-13 04:49:03 +00:00
MarcZX10 commented 2026-10-12 17:24:52 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-10-12T17:24:52Z.


What

Carries David's offered fix, preserving his original commit author. On Windows, before guest dup2 or _wfreopen replaces descriptor 0, 1 or 2, the code preserves the corresponding Win32 standard handle if it aliases that CRT descriptor, then restores the standard handle after the redirect. Guest writes still go to the redirected descriptor; host writes through GetStdHandle continue to reach the original host stream. This prevents crash and termination reports from being lost or redirected into title files.

This is complementary to open PR #4536: that change keeps sceKernelDebugOutText on host stderr; this change protects Win32 GetStdHandle consumers and covers freopen. The changes touch adjacent dup2 logic and GuestFilesystem.cpp, but neither depends on the other; a small integration rebase may be needed whichever lands second.

Tested

  • David reports Windows 11 10.0.26300, WinLibs GCC 15.2.0 r7, Release: guest_filesystem and guest_dup pass three runs; a full CTest rerun passed 971/971 after removing PYTHONHOME. A transient guest_audio_in_capture failure passed six follow-up runs. The four reproduced titles now emit their stop reports instead of losing them; further catalog results are in issue #4631.
  • David reports WSL2 Ubuntu 24.04, GCC 13.3: guest_filesystem and guest_standard_streams pass 3/3.
  • The offered source commit was tested on main d592d54e; the rebase onto current main 2a5eca11 is patch-identical. I did not rerun builds or tests after rebasing.
  • Not tested on a console, with --windows-gui, or with a commercial title.

Checklist

  • Based on current main; no other open PR implements the same behavior
  • One topic per PR; follow-ups go in a new PR
  • No comments in code except technical debt
  • Unimplemented paths throw or use NotImplemented_nid_no_patch
  • No notes, investigation files or images added to the repository
  • No new third-party code
  • Generic behavior
  • Depends on: none
  • AI-assisted: yes
<!-- greyfinch-sync:boykopovar/AnyPS5:issue:4832794705 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/4640). Originally posted by [MarcZX10](https://github.com/MarcZX10) on 2026-10-12T17:24:52Z. --- ### What Carries David's offered fix, preserving his original commit author. On Windows, before guest `dup2` or `_wfreopen` replaces descriptor 0, 1 or 2, the code preserves the corresponding Win32 standard handle if it aliases that CRT descriptor, then restores the standard handle after the redirect. Guest writes still go to the redirected descriptor; host writes through `GetStdHandle` continue to reach the original host stream. This prevents crash and termination reports from being lost or redirected into title files. This is complementary to open PR #4536: that change keeps `sceKernelDebugOutText` on host stderr; this change protects Win32 `GetStdHandle` consumers and covers `freopen`. The changes touch adjacent `dup2` logic and `GuestFilesystem.cpp`, but neither depends on the other; a small integration rebase may be needed whichever lands second. ### Tested - David reports Windows 11 10.0.26300, WinLibs GCC 15.2.0 r7, Release: `guest_filesystem` and `guest_dup` pass three runs; a full CTest rerun passed 971/971 after removing `PYTHONHOME`. A transient `guest_audio_in_capture` failure passed six follow-up runs. The four reproduced titles now emit their stop reports instead of losing them; further catalog results are in [issue #4631](https://github.com/boykopovar/AnyPS5/issues/4631). - David reports WSL2 Ubuntu 24.04, GCC 13.3: `guest_filesystem` and `guest_standard_streams` pass 3/3. - The offered source commit was tested on main `d592d54e`; the rebase onto current main `2a5eca11` is patch-identical. I did not rerun builds or tests after rebasing. - Not tested on a console, with `--windows-gui`, or with a commercial title. ### Checklist - [x] Based on current `main`; no other open PR implements the same behavior - [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 or use `NotImplemented_nid_no_patch` - [x] No notes, investigation files or images added to the repository - [x] No new third-party code - [x] Generic behavior - [x] Depends on: none - [x] AI-assisted: yes
MarcZX10 commented 2026-10-12 19:41:45 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-10-12T19:41:45Z.


The Windows results in the description are from the offered commit on d592d54e; the rebased PR head was not rebuilt, and the current PR checks skip the Windows job. Could you run guest_filesystem (including the new dup2/freopen cases) on this head and add the result? That would verify the Windows-only path against the current main before merge.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6131367010 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/4640#issuecomment-6131367010). Originally posted by [MarcZX10](https://github.com/MarcZX10) on 2026-10-12T19:41:45Z. --- The Windows results in the description are from the offered commit on `d592d54e`; the rebased PR head was not rebuilt, and the current PR checks skip the Windows job. Could you run `guest_filesystem` (including the new `dup2`/`freopen` cases) on this head and add the result? That would verify the Windows-only path against the current `main` before merge.
DavidSamir commented 2026-10-12 20:48:42 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by DavidSamir on 2026-10-12T20:48:42Z.


Ran it on Windows, and it was worth asking: the head as it is fails.

Head 44a5ebfc4 merged onto main 0428328b7 (clean merge), Windows 11 10.0.26300, WinLibs GCC 15.2.0 r7, Release, full build: guest_filesystem fails, Filesystem check failed at line 279. That line is the [debug:1] kept check that #4536 added, not one of this PR's.

Cause: a test-order clash, not the fix. #4536 keeps the host's standard error once per process, at the first dup2_nid_postfix(..., 2) (PreserveHostStandardError). After the rebase this PR's block runs before that check and makes the first such call while descriptor 2 is on its own temporary file, so the later sceKernelDebugOutText goes to that file instead of the one #4536's check reads.

Fixed commit: DavidSamir:fix/libkernel-keep-host-stderr-handle-windows is now a7fea811e, your 44a5ebfc4 cherry-picked onto main 0428328b7 with one change: the Windows block in GuestFilesystem.cpp is moved below the [debug:1] kept check. The changes to the four source files are the same as in 44a5ebfc4.

With a7fea811e on main 0428328b7, same machine, full build each time:

  • guest_filesystem passes 5/5 (--repeat until-fail:5).
  • guest_standard_streams, file_streams, guest_dup, guest_fiopen, guest_poll, windows_exceptions and guest_kernel_fcntl pass 3/3.
  • With FileStream.hpp, General.hpp, General.cpp and Stdio.cpp as on main and the test kept, guest_filesystem fails at this PR's check (Filesystem check failed at line 267, the captured && redirected && separate && written && restored line); restored, it passes again.

Not rerun for this comment: the title runs and the full ctest from #4631, which were on d592d54e1.

AI-assisted: yes

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6132356648 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/4640#issuecomment-6132356648). Originally posted by [DavidSamir](https://github.com/DavidSamir) on 2026-10-12T20:48:42Z. --- Ran it on Windows, and it was worth asking: the head as it is fails. **Head 44a5ebfc4 merged onto `main` 0428328b7** (clean merge), Windows 11 10.0.26300, WinLibs GCC 15.2.0 r7, Release, full build: `guest_filesystem` fails, `Filesystem check failed at line 279`. That line is the `[debug:1] kept` check that #4536 added, not one of this PR's. Cause: a test-order clash, not the fix. #4536 keeps the host's standard error once per process, at the first `dup2_nid_postfix(..., 2)` (`PreserveHostStandardError`). After the rebase this PR's block runs before that check and makes the first such call while descriptor 2 is on its own temporary file, so the later `sceKernelDebugOutText` goes to that file instead of the one #4536's check reads. **Fixed commit:** [`DavidSamir:fix/libkernel-keep-host-stderr-handle-windows`](https://github.com/boykopovar/AnyPS5/compare/main...DavidSamir:AnyPS5:fix/libkernel-keep-host-stderr-handle-windows) is now a7fea811e, your 44a5ebfc4 cherry-picked onto `main` 0428328b7 with one change: the Windows block in `GuestFilesystem.cpp` is moved below the `[debug:1] kept` check. The changes to the four source files are the same as in 44a5ebfc4. With a7fea811e on `main` 0428328b7, same machine, full build each time: - `guest_filesystem` passes 5/5 (`--repeat until-fail:5`). - `guest_standard_streams`, `file_streams`, `guest_dup`, `guest_fiopen`, `guest_poll`, `windows_exceptions` and `guest_kernel_fcntl` pass 3/3. - With `FileStream.hpp`, `General.hpp`, `General.cpp` and `Stdio.cpp` as on `main` and the test kept, `guest_filesystem` fails at this PR's check (`Filesystem check failed at line 267`, the `captured && redirected && separate && written && restored` line); restored, it passes again. Not rerun for this comment: the title runs and the full `ctest` from #4631, which were on d592d54e1. AI-assisted: yes
MarcZX10 commented 2026-10-12 21:00:45 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-10-12T21:00:45Z.


Thanks for running the rebased Windows head and isolating the ordering issue. The failure is from #4536 capturing stderr before its check, because this PR reaches dup2(..., 2) first; your a7fea811e commit moves the block after that check and the repeated guest_filesystem runs support the fix. I’m going to carry that corrected source onto the current main and validate the Linux path, then update this PR.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6132528132 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/4640#issuecomment-6132528132). Originally posted by [MarcZX10](https://github.com/MarcZX10) on 2026-10-12T21:00:45Z. --- Thanks for running the rebased Windows head and isolating the ordering issue. The failure is from #4536 capturing stderr before its check, because this PR reaches `dup2(..., 2)` first; your a7fea811e commit moves the block after that check and the repeated `guest_filesystem` runs support the fix. I’m going to carry that corrected source onto the current main and validate the Linux path, then update this PR.
MarcZX10 commented 2026-10-12 21:23:12 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-10-12T21:23:12Z.


Update: after the first rebase, upstream advanced again, so I am rebasing once more and rerunning the focused Linux tests. GitHub conventions pass. The eligibility check failed because the workflow hit GitHub API rate limiting (403 fetching PR metadata); this was not a code/build failure. I will update with the final head and test result.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6132860538 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/4640#issuecomment-6132860538). Originally posted by [MarcZX10](https://github.com/MarcZX10) on 2026-10-12T21:23:12Z. --- Update: after the first rebase, upstream advanced again, so I am rebasing once more and rerunning the focused Linux tests. GitHub conventions pass. The eligibility check failed because the workflow hit GitHub API rate limiting (403 fetching PR metadata); this was not a code/build failure. I will update with the final head and test result.
MarcZX10 commented 2026-10-12 21:26:23 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-10-12T21:26:23Z.


Final rebase update: head c22654102b5c7d981602fd1a1b22d25722ca807f is based on current upstream main 584cc4c5716b02210ca031593d1a6b0b4ce049cb. After this rebase, Linux Release guest_filesystem and guest_dup passed (2/2), conventions passed, and git diff --check passed. The earlier eligibility workflow run remains failed due GitHub API rate limiting (403), not a source check.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6132910730 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/4640#issuecomment-6132910730). Originally posted by [MarcZX10](https://github.com/MarcZX10) on 2026-10-12T21:26:23Z. --- Final rebase update: head `c22654102b5c7d981602fd1a1b22d25722ca807f` is based on current upstream main `584cc4c5716b02210ca031593d1a6b0b4ce049cb`. After this rebase, Linux Release `guest_filesystem` and `guest_dup` passed (2/2), conventions passed, and `git diff --check` passed. The earlier eligibility workflow run remains failed due GitHub API rate limiting (403), not a source check.
MarcZX10 commented 2026-10-12 21:40:12 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-10-12T21:40:12Z.


The refreshed hosted checks for head c22654102 have completed: conventions, eligibility, report, relinker on Ubuntu/macOS/Windows, register-context on Ubuntu/macOS, and write-watch on macOS passed. Linux, Windows, and some platform-specific jobs were skipped by the PR path filters. The PR is currently reported mergeable/clean.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6133111850 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/4640#issuecomment-6133111850). Originally posted by [MarcZX10](https://github.com/MarcZX10) on 2026-10-12T21:40:12Z. --- The refreshed hosted checks for head `c22654102` have completed: conventions, eligibility, report, relinker on Ubuntu/macOS/Windows, register-context on Ubuntu/macOS, and write-watch on macOS passed. Linux, Windows, and some platform-specific jobs were skipped by the PR path filters. The PR is currently reported mergeable/clean.
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!4640
No description provided.