fix(libc): keep host logging on the console when a guest reopens stdout or stderr #13390

Merged
medazizktata merged 5 commits from upstream-pr/13390 into main 2026-11-09 05:44:04 +00:00
medazizktata commented 2026-10-30 13:40:10 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by medazizktata on 2026-10-30T13:40:10Z.


What

After a guest freopen of stdout or stderr, AnyPS5's own log lines followed the guest stream into the title's file (#13328). On Windows both streams were affected, on Linux only stdout. A guest dup2 onto descriptor 1 or 2 already keeps the host streams first (#7807), and so does setvbuf (#10020); freopen did not, except for Linux stderr (#4792).

Changes, all in core/libs/prx/libc/include/FileStream.hpp:

  • FileStream::Reopen keeps the host stream before the guest's file replaces descriptor 1 or 2:
    • Windows: KeepHostStandardOutput_nid_no_patch() or KeepHostStandardError_nid_no_patch() when the stream is the C runtime's stdout or stderr;
    • Linux: KeepHostStandardOutput_nid_no_patch() for descriptor 1, next to the existing KeepHostStandardError_nid_no_patch() for descriptor 2.
  • On Windows, IsHostStandard() compares against __acrt_iob_func(0/1/2) instead of stdin/stdout/stderr. Inside libc those macros resolve, through the forced HostStandardStreams.hpp, to the kept host stream once a keep has run. After the first keep, a guest standard stream therefore no longer counted as standard, and a second freopen of it took the fclose path and closed the C runtime's stream. guest_standard_streams catches that.
  • Both Keep functions are declared on every platform. Before, KeepHostStandardError_nid_no_patch was declared outside Windows only.
  • Keeping the host stream is best effort on Linux: if a keep throws std::system_error (no free descriptor, no memory), Reopen restores errno and the guest's freopen goes ahead; only AnyPS5's own output then follows the guest's file. This covers the existing stderr keep too. On Windows a failed keep already returns -1 and is ignored.
  • The new Linux stdout keep is skipped under Emscripten, where KeepHostStandardOutput_nid_no_patch throws (General.cpp). A WebAssembly build keeps today's behaviour, and the WebAssembly entry in docs/dev/debt/libc.md now says that host stdout follows a guest freopen there.

docs/dev/GAME_TESTING.md: the line added by #13024 said run.log ends at the entry point for these titles. It now says that the title's own output, including its exit reason, is not in run.log; the pointer to app.log stays.

Test: new ctest guest_freopen_host_log (core/libs/tests/GuestFreopenHostLog.cpp, registered in core/libs/prx/libc/CMakeLists.txt, Windows and Linux).

  • It points descriptors 1 and 2 at a file standing in for the console, has the guest freopen its stdout and stderr to two files and write a line to each, then writes one host line to each stream.
  • It checks that each guest line is in its file, that no host line is in a guest file, and that both host lines reach the console file.

Second test, Linux only: guest_freopen_host_log_keep_failure, a mode of the same executable. In a fresh process it fills descriptors 3 and up under a lowered RLIMIT_NOFILE and closes descriptor 0, so the first stdout keep fails with EMFILE while the guest's file can still be opened. It checks that no exception leaves freopen, that freopen succeeds with the guest's line in its file, and that the host's stdout was not replaced.

Tested

All on main a5939f10a unless stated. The branch is now on f91137142e. Between the two, this code only gained the Emscripten guard, so on Linux I built libc.prx with and without it there: both builds are byte-identical, libc was recompiled for the second, and the test-only build differs from both. On f91137142e the test still fails 3/3 on the stdout checks without the fix, and the 9 related tests pass 5/5 with it.

After review, with the keep-failure catch, on f91137142e: guest_freopen_host_log_keep_failure fails 3/3 without the catch and passes 5/5 with it; Linux ctest -E agc 1872/1872, Windows 1954/1954.

test only (no fix) with this change
Windows 11 Pro 10.0.26200, i5-12600KF, WinLibs GCC 15.2.0 r7, Release: guest_freopen_host_log fails 3/3, all four host checks passes 5/5
Windows: 10 related tests (guest_freopen_host_log, guest_freopen_host_standard, the five guest_standard_streams*, guest_host_standard_error_windows, guest_host_standard_buffering_windows, guest_stdio_variadic) the other 9 pass pass 5/5
Windows: ctest -E agc — 1942/1942
Pop!_OS 24.04 LTS, GCC 13.3.0, Release: guest_freopen_host_log fails 3/3, stdout checks only passes 5/5, with the 8 other related tests that build on Linux
Pop!_OS: ctest -E agc — 1859/1859 (27 video-out tests skipped: no display)
  • Deliberate breaks, Windows:

    • removing the two Windows keep lines: guest_freopen_host_log fails 3/3 on all four host checks;
    • putting back the old IsHostStandard(): guest_standard_streams aborts 3/3 (0xc0000409).
  • Deliberate break, Linux: removing the new stdout keep makes guest_freopen_host_log fail 3/3 on the stdout checks.

  • Restoring the file byte-for-byte passes again on both. Build logs show libc relinked in every step.

  • Real titles, Windows, RTX 4060: Trophy Manager (PPSA99842) and PSCord (PPSA99190), each converted once and run in two copies that differ only in the libraries (test only / this change), with APS5_PROFILE_DRAW=1. All runs were still running when stopped.

    10 minutes, main 66b8e907e:

    console lines [present] on console AnyPS5 lines in the title's app.log
    Trophy Manager, test only 3 0 59 [present], 45 [src/…], 2 [pipeline-cache], plus [recipe], [draw-cache]…
    Trophy Manager, this change 1833 59 none
    PSCord, test only 3 0 58 [present], 202 [src/…], 2 [pipeline-cache]…
    PSCord, this change 2279 58 none

    With the change, the console and app.log share no line tag; app.log holds only the title's own lines ([TROPHY], [HUI], [ps5-gallium], [dc]…).

    120 s with ANYPS5_LOG_FILE (#12715), main a5939f10a:

    console lines copy file lines AnyPS5 lines in app.log
    Trophy Manager, test only 3 4 11 [present], 45 [src/…]…
    Trophy Manager, this change 413 414 none
    PSCord, test only 3 4 11 [present], 142 [src/…]…
    PSCord, this change 571 572 none

    The extra line in each copy is its [log] ANYPS5_LOG_FILE: appending… header; every tag on the console is in the copy. This is also a first run of ANYPS5_LOG_FILE on Windows, which docs/user/USAGE.md lists as compiled but not run.

  • tools/check_conventions.py --base origin/main: passes.

Not tested:

  • a real title on Linux, and ANYPS5_LOG_FILE on Linux;
  • macOS (not built);
  • a WebAssembly build (not built; the guard only removes the new line there);
  • a failing fdopen in the Linux keep (ENOMEM), and a failing stderr keep; both take the same catch as the tested stdout case;
  • on Windows, a failed freopen of stdout or stderr after an earlier keep now detaches the C runtime's stream instead of closing it; the existing closed-stream tests pass, but there is no test for this case.

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 (#10755, #11831, #12682, #12688 and #12700 edit nearby lines of FileStream.hpp, so whichever lands second has a small text conflict)
  • Fixes: Fixes #13328
  • Unblocks: none
  • GPUs tested: NVIDIA GeForce RTX 4060 (real-title runs)
  • AI-assisted: yes (Claude Code)
<!-- greyfinch-sync:boykopovar/AnyPS5:issue:5085265409 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/13390). Originally posted by [medazizktata](https://github.com/medazizktata) on 2026-10-30T13:40:10Z. --- ### What After a guest `freopen` of `stdout` or `stderr`, AnyPS5's own log lines followed the guest stream into the title's file (#13328). On Windows both streams were affected, on Linux only `stdout`. A guest `dup2` onto descriptor 1 or 2 already keeps the host streams first (#7807), and so does `setvbuf` (#10020); `freopen` did not, except for Linux `stderr` (#4792). Changes, all in `core/libs/prx/libc/include/FileStream.hpp`: - `FileStream::Reopen` keeps the host stream before the guest's file replaces descriptor 1 or 2: - Windows: `KeepHostStandardOutput_nid_no_patch()` or `KeepHostStandardError_nid_no_patch()` when the stream is the C runtime's stdout or stderr; - Linux: `KeepHostStandardOutput_nid_no_patch()` for descriptor 1, next to the existing `KeepHostStandardError_nid_no_patch()` for descriptor 2. - On Windows, `IsHostStandard()` compares against `__acrt_iob_func(0/1/2)` instead of `stdin`/`stdout`/`stderr`. Inside libc those macros resolve, through the forced `HostStandardStreams.hpp`, to the kept host stream once a keep has run. After the first keep, a guest standard stream therefore no longer counted as standard, and a second `freopen` of it took the `fclose` path and closed the C runtime's stream. `guest_standard_streams` catches that. - Both Keep functions are declared on every platform. Before, `KeepHostStandardError_nid_no_patch` was declared outside Windows only. - Keeping the host stream is best effort on Linux: if a keep throws `std::system_error` (no free descriptor, no memory), `Reopen` restores `errno` and the guest's `freopen` goes ahead; only AnyPS5's own output then follows the guest's file. This covers the existing stderr keep too. On Windows a failed keep already returns -1 and is ignored. - The new Linux stdout keep is skipped under Emscripten, where `KeepHostStandardOutput_nid_no_patch` throws (`General.cpp`). A WebAssembly build keeps today's behaviour, and the WebAssembly entry in `docs/dev/debt/libc.md` now says that host stdout follows a guest `freopen` there. `docs/dev/GAME_TESTING.md`: the line added by #13024 said `run.log` ends at the entry point for these titles. It now says that the title's own output, including its exit reason, is not in `run.log`; the pointer to `app.log` stays. Test: new ctest `guest_freopen_host_log` (`core/libs/tests/GuestFreopenHostLog.cpp`, registered in `core/libs/prx/libc/CMakeLists.txt`, Windows and Linux). - It points descriptors 1 and 2 at a file standing in for the console, has the guest `freopen` its `stdout` and `stderr` to two files and write a line to each, then writes one host line to each stream. - It checks that each guest line is in its file, that no host line is in a guest file, and that both host lines reach the console file. Second test, Linux only: `guest_freopen_host_log_keep_failure`, a mode of the same executable. In a fresh process it fills descriptors 3 and up under a lowered `RLIMIT_NOFILE` and closes descriptor 0, so the first stdout keep fails with `EMFILE` while the guest's file can still be opened. It checks that no exception leaves `freopen`, that `freopen` succeeds with the guest's line in its file, and that the host's `stdout` was not replaced. ### Tested All on `main` `a5939f10a` unless stated. The branch is now on `f91137142e`. Between the two, this code only gained the Emscripten guard, so on Linux I built `libc.prx` with and without it there: both builds are byte-identical, libc was recompiled for the second, and the test-only build differs from both. On `f91137142e` the test still fails 3/3 on the stdout checks without the fix, and the 9 related tests pass 5/5 with it. After review, with the keep-failure `catch`, on `f91137142e`: `guest_freopen_host_log_keep_failure` fails 3/3 without the `catch` and passes 5/5 with it; Linux `ctest -E agc` 1872/1872, Windows 1954/1954. | | test only (no fix) | with this change | |---|---|---| | Windows 11 Pro 10.0.26200, i5-12600KF, WinLibs GCC 15.2.0 r7, Release: `guest_freopen_host_log` | fails 3/3, all four host checks | passes 5/5 | | Windows: 10 related tests (`guest_freopen_host_log`, `guest_freopen_host_standard`, the five `guest_standard_streams*`, `guest_host_standard_error_windows`, `guest_host_standard_buffering_windows`, `guest_stdio_variadic`) | the other 9 pass | pass 5/5 | | Windows: `ctest -E agc` | — | 1942/1942 | | Pop!_OS 24.04 LTS, GCC 13.3.0, Release: `guest_freopen_host_log` | fails 3/3, stdout checks only | passes 5/5, with the 8 other related tests that build on Linux | | Pop!_OS: `ctest -E agc` | — | 1859/1859 (27 video-out tests skipped: no display) | - **Deliberate breaks, Windows:** - removing the two Windows keep lines: `guest_freopen_host_log` fails 3/3 on all four host checks; - putting back the old `IsHostStandard()`: `guest_standard_streams` aborts 3/3 (`0xc0000409`). - **Deliberate break, Linux:** removing the new stdout keep makes `guest_freopen_host_log` fail 3/3 on the stdout checks. - **Restoring the file byte-for-byte** passes again on both. Build logs show libc relinked in every step. - **Real titles, Windows, RTX 4060:** Trophy Manager (PPSA99842) and PSCord (PPSA99190), each converted once and run in two copies that differ only in the libraries (test only / this change), with `APS5_PROFILE_DRAW=1`. All runs were still running when stopped. 10 minutes, `main` `66b8e907e`: | | console lines | `[present]` on console | AnyPS5 lines in the title's `app.log` | |---|---|---|---| | Trophy Manager, test only | 3 | 0 | 59 `[present]`, 45 `[src/…]`, 2 `[pipeline-cache]`, plus `[recipe]`, `[draw-cache]`… | | Trophy Manager, this change | 1833 | 59 | none | | PSCord, test only | 3 | 0 | 58 `[present]`, 202 `[src/…]`, 2 `[pipeline-cache]`… | | PSCord, this change | 2279 | 58 | none | With the change, the console and `app.log` share no line tag; `app.log` holds only the title's own lines (`[TROPHY]`, `[HUI]`, `[ps5-gallium]`, `[dc]`…). 120 s with `ANYPS5_LOG_FILE` (#12715), `main` `a5939f10a`: | | console lines | copy file lines | AnyPS5 lines in `app.log` | |---|---|---|---| | Trophy Manager, test only | 3 | 4 | 11 `[present]`, 45 `[src/…]`… | | Trophy Manager, this change | 413 | 414 | none | | PSCord, test only | 3 | 4 | 11 `[present]`, 142 `[src/…]`… | | PSCord, this change | 571 | 572 | none | The extra line in each copy is its `[log] ANYPS5_LOG_FILE: appending…` header; every tag on the console is in the copy. This is also a first run of `ANYPS5_LOG_FILE` on Windows, which `docs/user/USAGE.md` lists as compiled but not run. - `tools/check_conventions.py --base origin/main`: passes. **Not tested:** - a real title on Linux, and `ANYPS5_LOG_FILE` on Linux; - macOS (not built); - a WebAssembly build (not built; the guard only removes the new line there); - a failing `fdopen` in the Linux keep (`ENOMEM`), and a failing stderr keep; both take the same `catch` as the tested stdout case; - on Windows, a failed `freopen` of stdout or stderr after an earlier keep now detaches the C runtime's stream instead of closing it; the existing closed-stream tests pass, but there is no test for this case. ### 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 (#10755, #11831, #12682, #12688 and #12700 edit nearby lines of `FileStream.hpp`, so whichever lands second has a small text conflict) - [x] Fixes: Fixes #13328 - [x] Unblocks: none - [x] GPUs tested: NVIDIA GeForce RTX 4060 (real-title runs) - [x] AI-assisted: yes (Claude Code)
adriadam10 commented 2026-10-30 15:15:07 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by adriadam10 on 2026-10-30T15:15:07Z.

Review: DISMISSED


Thanks, the approach and the shared-console test look right. One blocker, already raised by @oneandonlydean and not yet addressed:

  • FileStream.hpp:197-198 and :242 call KeepHostStandardOutput_nid_no_patch() / KeepHostStandardError_nid_no_patch() from Reopen. On Linux KeepHostStandardOutput_nid_no_patch (General.cpp:623-632) throws std::system_error on fcntl/fdopen failure (EMFILE, ENOMEM), it does not return -1. freopen_nid_postfix only catches bad_alloc and filesystem_error, so a failed keep escapes the guest C ABI. The "Not tested: a failing Keep returns -1" note in the body does not match the code.
    Please choose an explicit policy (for example catch the keep failure in Reopen and have the freopen fail with the errno, or continue without the keep) so no new exception can leave freopen, and state it in the PR.
  • Add a bounded regression for the failing first stdout keep that checks the guest outcome (return value, errno) and the host stream state afterwards. There is no need to exhaust the descriptor table; a hook or a closed descriptor 1 is enough.

Keep the existing guest-file/host-console separation test. Happy to re-review once pushed.

<!-- greyfinch-sync:boykopovar/AnyPS5:review:5714400239 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/13390#pullrequestreview-5714400239). Originally posted by [adriadam10](https://github.com/adriadam10) on 2026-10-30T15:15:07Z. **Review:** DISMISSED --- Thanks, the approach and the shared-console test look right. One blocker, already raised by @oneandonlydean and not yet addressed: - `FileStream.hpp:197-198` and `:242` call `KeepHostStandardOutput_nid_no_patch()` / `KeepHostStandardError_nid_no_patch()` from `Reopen`. On Linux `KeepHostStandardOutput_nid_no_patch` (`General.cpp:623-632`) throws `std::system_error` on `fcntl`/`fdopen` failure (EMFILE, ENOMEM), it does not return -1. `freopen_nid_postfix` only catches `bad_alloc` and `filesystem_error`, so a failed keep escapes the guest C ABI. The "Not tested: a failing Keep returns -1" note in the body does not match the code. Please choose an explicit policy (for example catch the keep failure in `Reopen` and have the `freopen` fail with the errno, or continue without the keep) so no new exception can leave `freopen`, and state it in the PR. - Add a bounded regression for the failing first stdout keep that checks the guest outcome (return value, errno) and the host stream state afterwards. There is no need to exhaust the descriptor table; a hook or a closed descriptor 1 is enough. Keep the existing guest-file/host-console separation test. Happy to re-review once pushed.
malekaifa commented 2026-10-30 19:33:25 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by malekaifa on 2026-10-30T19:33:25Z.

Review: APPROVED


Reviewed exact head 4cf81e1d1d4aebae35b813dd8dc89a118d72beba.

The earlier keep-failure blocker is addressed in the current source: on non-Windows, Reopen catches std::system_error from preserving stdout/stderr, restores the prior errno, and continues with the documented best-effort policy instead of allowing an exception to escape the guest freopen ABI. The Linux-only regression exercises that path. On Windows, using the raw UCRT standard-stream identity for IsHostStandard() is consistent with keeping host logging on the preserved duplicate while allowing repeated guest reopens; the existing standard-stream suite covers successful redirect, failed reopen, closed/reopened stdout, and update mode.

Independent Windows 11 / WinLibs GCC 15.2.0 Release validation on this exact head:

  • built guest_freopen_host_log_tests cleanly;
  • guest_freopen_host_log: 5/5 consecutive passes;
  • built guest_standard_stream_tests and guest_freopen_host_standard_tests cleanly;
  • all five guest_standard_streams* cases: 3 repetitions each, 15/15 passes;
  • guest_freopen_host_standard: 3/3 passes;
  • tools/check_conventions.py --base origin/main: pass;
  • git diff --check origin/main...HEAD: pass;
  • git merge-tree --write-tree HEAD origin/main against current main 16345cad6a: clean.

Hosted checks on the exact head currently show Windows, conventions, library portability, report, and relinker jobs green; macOS write-watch jobs are still pending. I did not independently execute the Linux keep-failure test, so that part of this review is source-level plus the submitted regression rather than a local Linux run.

No remaining blocker found in this change.

AI-assisted: yes (OpenAI ChatGPT).

<!-- greyfinch-sync:boykopovar/AnyPS5:review:5717724339 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/13390#pullrequestreview-5717724339). Originally posted by [malekaifa](https://github.com/malekaifa) on 2026-10-30T19:33:25Z. **Review:** APPROVED --- Reviewed exact head `4cf81e1d1d4aebae35b813dd8dc89a118d72beba`. The earlier keep-failure blocker is addressed in the current source: on non-Windows, `Reopen` catches `std::system_error` from preserving stdout/stderr, restores the prior errno, and continues with the documented best-effort policy instead of allowing an exception to escape the guest `freopen` ABI. The Linux-only regression exercises that path. On Windows, using the raw UCRT standard-stream identity for `IsHostStandard()` is consistent with keeping host logging on the preserved duplicate while allowing repeated guest reopens; the existing standard-stream suite covers successful redirect, failed reopen, closed/reopened stdout, and update mode. Independent Windows 11 / WinLibs GCC 15.2.0 Release validation on this exact head: - built `guest_freopen_host_log_tests` cleanly; - `guest_freopen_host_log`: 5/5 consecutive passes; - built `guest_standard_stream_tests` and `guest_freopen_host_standard_tests` cleanly; - all five `guest_standard_streams*` cases: 3 repetitions each, 15/15 passes; - `guest_freopen_host_standard`: 3/3 passes; - `tools/check_conventions.py --base origin/main`: pass; - `git diff --check origin/main...HEAD`: pass; - `git merge-tree --write-tree HEAD origin/main` against current `main` `16345cad6a`: clean. Hosted checks on the exact head currently show Windows, conventions, library portability, report, and relinker jobs green; macOS write-watch jobs are still pending. I did not independently execute the Linux keep-failure test, so that part of this review is source-level plus the submitted regression rather than a local Linux run. No remaining blocker found in this change. AI-assisted: yes (OpenAI ChatGPT).
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!13390
No description provided.