fix(libc): keep host diagnostics on stderr when a guest redirects it on Linux #4792

Merged
marshmll merged 1 commit from upstream-pr/4792 into main 2026-10-13 02:36:52 +00:00
marshmll commented 2026-10-12 22:05:16 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by marshmll on 2026-10-12T22:05:16Z.


What

The Linux side of #4631. On Linux a guest stderr redirect also moves the host's: guest descriptors are host descriptors, and the guest stderr stream wraps the host stderr FILE (_Stderr_nid_postfix{stderr}, and FileStream::Reopen runs std::freopen on it). After a guest dup2(fd, 2), or freopen(path, mode, stderr) followed by setvbuf with full buffering, the libc terminate report (RawWrite uses fwrite(..., stderr) on Linux), the host fprintf(stderr, ...) diagnostics and libstdc++'s terminate message go into the title's pipe or file, and abort() drops what is still buffered.

RetroArch (PPSA99169) does exactly this: its trace says stderr is the trace file (buffered, flushed every 250 ms). On main it aborts with nothing on the console and nothing in app0/trace.txt; the stop was only found with gdb. With this change the console shows:

[libc] unhandled exception 'St13runtime_error' thrown from 0x...
terminate called after throwing an instance of 'std::runtime_error'
  what():  sceSystemServiceLoadExec: executable replacement not implemented

The same happened with ps5-opengl (PPSA99005): with write watch unavailable, its FATAL: an address-based build copies 328 MiB ... landed in its own download0/ps5-opengl.log instead of the console.

  • libc (General.cpp, Linux only): KeepHostStandardError_nid_no_patch() keeps a close-on-exec duplicate of host descriptor 2 once, before the first guest redirect, and points the host stderr at an unbuffered stream on it. The guest stream keeps its own FILE and follows its redirect as on FreeBSD. If descriptor 2 is closed there is nothing to keep.
  • libc (FileStream::Reopen, Linux branch): keeps it before reopening a stream on descriptor 2.
  • libkernel (dup2, Linux branch): PreserveHostStandardError takes the same kept descriptor instead of its own dup(2), so sceKernelDebugOutText (#4536) and the rest of the host output share it.
  • libc (RawWrite, Linux branch): writes the terminate report to the kept descriptor with write(), without the stream lock or buffering, as its comment already asks.
  • New guest_host_standard_error test (Unix): in forked children, a guest dup2 and a guest freopen + full buffering, each with a host fputs(stderr) and with an uncaught exception. The host output and the what(): line must reach the original stderr, and the guest output its redirect.

Windows code is unchanged; #4640 covers it there. The two touch neighbouring lines in FileStream.hpp, General.hpp, General.cpp and Stdio.cpp but no hunk overlaps; whichever lands second may need a trivial rebase.

Tested

Fedora 44, GCC 16.2.1, Release with BUILD_TESTING=ON and ANYPS5_ENABLE_SPIRV_TOOLS=ON, AMD RX 9060 XT (RADV) and lavapipe, on main ee4962190.

  • guest_host_standard_error fails without the library changes (the host fputs after a guest dup2 never reaches the original stderr) and passes with them.
  • guest_host_standard_error, guest_filesystem, guest_dup, exception_runtime, guest_standard_streams, file_streams, guest_fiopen, guest_syslog, guest_formatting pass.
  • Full ctest on lavapipe before the rebase: 994/995. agc_compute_pipeline_replay fails on Mesa 26.2.4 lavapipe (replay did not export a pipeline cache header) and passes on RADV; it does not use stderr. guest_video_out_high_frame_rate skips because the local display has no 120 Hz mode.
  • RetroArch (PPSA99169, catalog release, sha256 checked) relinked and run in a rootless Podman container without network: the stop message above now reaches the console.
  • python3 tools/check_conventions.py --base origin/main passes.
  • Not tested on Windows (unchanged) or macOS.

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 in TechnicalDebt
  • 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
  • AI-assisted: yes
<!-- greyfinch-sync:boykopovar/AnyPS5:issue:4835527499 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/4792). Originally posted by [marshmll](https://github.com/marshmll) on 2026-10-12T22:05:16Z. --- ### What The Linux side of #4631. On Linux a guest stderr redirect also moves the host's: guest descriptors are host descriptors, and the guest `stderr` stream wraps the host `stderr` `FILE` (`_Stderr_nid_postfix{stderr}`, and `FileStream::Reopen` runs `std::freopen` on it). After a guest `dup2(fd, 2)`, or `freopen(path, mode, stderr)` followed by `setvbuf` with full buffering, the libc terminate report (`RawWrite` uses `fwrite(..., stderr)` on Linux), the host `fprintf(stderr, ...)` diagnostics and libstdc++'s terminate message go into the title's pipe or file, and `abort()` drops what is still buffered. RetroArch (PPSA99169) does exactly this: its trace says `stderr is the trace file (buffered, flushed every 250 ms)`. On `main` it aborts with nothing on the console and nothing in `app0/trace.txt`; the stop was only found with gdb. With this change the console shows: ``` [libc] unhandled exception 'St13runtime_error' thrown from 0x... terminate called after throwing an instance of 'std::runtime_error' what(): sceSystemServiceLoadExec: executable replacement not implemented ``` The same happened with ps5-opengl (PPSA99005): with write watch unavailable, its `FATAL: an address-based build copies 328 MiB ...` landed in its own `download0/ps5-opengl.log` instead of the console. - `libc` (`General.cpp`, Linux only): `KeepHostStandardError_nid_no_patch()` keeps a close-on-exec duplicate of host descriptor 2 once, before the first guest redirect, and points the host `stderr` at an unbuffered stream on it. The guest stream keeps its own `FILE` and follows its redirect as on FreeBSD. If descriptor 2 is closed there is nothing to keep. - `libc` (`FileStream::Reopen`, Linux branch): keeps it before reopening a stream on descriptor 2. - `libkernel` (`dup2`, Linux branch): `PreserveHostStandardError` takes the same kept descriptor instead of its own `dup(2)`, so `sceKernelDebugOutText` (#4536) and the rest of the host output share it. - `libc` (`RawWrite`, Linux branch): writes the terminate report to the kept descriptor with `write()`, without the stream lock or buffering, as its comment already asks. - New `guest_host_standard_error` test (Unix): in forked children, a guest `dup2` and a guest `freopen` + full buffering, each with a host `fputs(stderr)` and with an uncaught exception. The host output and the `what():` line must reach the original stderr, and the guest output its redirect. Windows code is unchanged; #4640 covers it there. The two touch neighbouring lines in `FileStream.hpp`, `General.hpp`, `General.cpp` and `Stdio.cpp` but no hunk overlaps; whichever lands second may need a trivial rebase. ### Tested Fedora 44, GCC 16.2.1, Release with `BUILD_TESTING=ON` and `ANYPS5_ENABLE_SPIRV_TOOLS=ON`, AMD RX 9060 XT (RADV) and lavapipe, on `main` ee4962190. - `guest_host_standard_error` fails without the library changes (the host `fputs` after a guest `dup2` never reaches the original stderr) and passes with them. - `guest_host_standard_error`, `guest_filesystem`, `guest_dup`, `exception_runtime`, `guest_standard_streams`, `file_streams`, `guest_fiopen`, `guest_syslog`, `guest_formatting` pass. - Full `ctest` on lavapipe before the rebase: 994/995. `agc_compute_pipeline_replay` fails on Mesa 26.2.4 lavapipe (`replay did not export a pipeline cache header`) and passes on RADV; it does not use stderr. `guest_video_out_high_frame_rate` skips because the local display has no 120 Hz mode. - RetroArch (PPSA99169, catalog release, sha256 checked) relinked and run in a rootless Podman container without network: the stop message above now reaches the console. - `python3 tools/check_conventions.py --base origin/main` passes. - Not tested on Windows (unchanged) or macOS. ### Checklist - [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 in [TechnicalDebt](https://github.com/boykopovar/AnyPS5/blob/main/docs/dev/TechnicalDebt.md#silent-stubs) - [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] AI-assisted: yes
nicolasfalesy commented 2026-10-13 02:09:41 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by nicolasfalesy on 2026-10-13T02:09:41Z.


Tested on Linux (Arch, kernel 7.2.5, GCC 16.2.1, Release), this PR (438d41082) merged onto main 37dd48308, with ProsperoLight (PPSA99002, catalog release, relinked with --to-intel):

  • main: the title aborts (exit 134) and the log has no what() line; the throw is only visible in gdb (File::Fcntl → Classify).
  • With this PR: the same abort now ends with
    [libc] unhandled exception 'St13runtime_error' thrown from 0x742fd1a5421d
    terminate called after throwing an instance of 'std::runtime_error'
      what():  fcntl: descriptor 35 is neither a file, a directory, a pipe nor a character device
    
    in the title's stdout/stderr log, so the stop is readable without a debugger.

Only this title was checked; no change in where it stops.

Tested with Claude Code assistance.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6136654406 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/4792#issuecomment-6136654406). Originally posted by [nicolasfalesy](https://github.com/nicolasfalesy) on 2026-10-13T02:09:41Z. --- Tested on Linux (Arch, kernel 7.2.5, GCC 16.2.1, Release), this PR (438d41082) merged onto `main` 37dd48308, with ProsperoLight (PPSA99002, catalog release, relinked with `--to-intel`): - `main`: the title aborts (exit 134) and the log has no `what()` line; the throw is only visible in `gdb` (`File::Fcntl` → `Classify`). - With this PR: the same abort now ends with ``` [libc] unhandled exception 'St13runtime_error' thrown from 0x742fd1a5421d terminate called after throwing an instance of 'std::runtime_error' what(): fcntl: descriptor 35 is neither a file, a directory, a pipe nor a character device ``` in the title's stdout/stderr log, so the stop is readable without a debugger. Only this title was checked; no change in where it stops. Tested with Claude Code assistance.
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!4792
No description provided.