fix(libkernel): keep host stderr off a guest dup2 onto descriptor 2 #7807
Loading…
Reference in a new issue
No description provided.
Delete branch "upstream-pr/7807"
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 mazen320 on 2026-10-19T08:21:15Z.
What
Host logging and the guest stdio streams no longer share one
FILEafter a guestdup2onto descriptor 1 or 2. On Windows the saved descriptor is opened with_fdopen, and hoststdoutandstderruse thatFILE. The guestFILEstays on the descriptordup2replaced, so guestprintf,putsandfprintfstill reach the pipe. On Linux,wprintfwrites through the guest stdout stream, which already kept the originalFILE.Tested
Windows 11, WinLibs GCC 15.2.0 UCRT, Release.
guest_host_standard_error_windowspassed: an 8192-byte hostfputsstays on the pipe the descriptor had before the guestdup2, a 5-bytewriteon that descriptor stays on the guest pipe, guestprintfandputsafterdup2onto 1 land on the guest pipe, and guestfprintfon_Stderr_nid_postfixafterdup2onto 2 lands on the guest pipe. The Linuxguest_host_standard_erroradditions were not run here. Eternal Darkness was not run.Follow-up at
18da29b89: Windows host-stream redirection now also applies to the static shader recompiler. A new test starts the real disk-cache writer after guest stderr is redirected; it fails on the previous head because the cache diagnostic enters the guest pipe, and passes with the fix because the diagnostic reaches host stderr and the guest pipe stays empty. The standard-stream test also checks output buffered beforedup2. All 10 selected cache/stdio/error tests passed, and the two routing tests passed five consecutive runs each. Conventions and whitespace checks passed. The missing PPSA99314 pad-open message has not been reproduced here; no title-level claim is made for it.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)Imported from boykopovar/AnyPS5. Originally posted by github-actions[bot] on 2026-10-19T08:31:31Z.
Other open pull requests touch the same code. Merging one will break the other, or both implement the same thing. If this one builds on them, list them in Depends on.
main)docs/dev/debt/libkernel.mdL161-162core/libs/prx/libSceAgcDriver/tests/ShaderDiskCache.cppL61Some comparisons could not be completed because a pull request conflicts with the base branch. Review these shared files manually before concluding that there is no overlap.
core/libs/prx/libSceAgcDriver/CMakeLists.txtcore/libs/prx/libSceAgcDriver/CMakeLists.txtcore/libs/prx/libSceAgcDriver/CMakeLists.txtdocs/dev/debt/libkernel.mdcore/libs/prx/libkernel/CMakeLists.txtcore/libs/CMakeLists.txtcore/libs/prx/libSceAgcDriver/CMakeLists.txtcore/libs/prx/libc/include/WindowsFormatting.hppcore/libs/prx/libc/src/General.cppcore/libs/prx/libc/src/RuntimeSupport.cppcore/libs/prx/libkernel/CMakeLists.txtcore/libs/prx/libkernel/File/src/Stdio.cppdocs/dev/debt/libkernel.mdcore/libs/prx/libSceAgcDriver/CMakeLists.txtcore/libs/prx/libSceAgcDriver/CMakeLists.txtcore/libs/prx/libSceAgcDriver/CMakeLists.txtcore/libs/prx/libSceAgcDriver/CMakeLists.txtcore/libs/prx/libSceAgcDriver/tests/ShaderDiskCache.cppcore/libs/prx/libSceAgcDriver/CMakeLists.txtcore/shader/recompiler/CMakeLists.txtcore/libs/CMakeLists.txtcore/libs/prx/libSceAgcDriver/CMakeLists.txtcore/libs/prx/libkernel/CMakeLists.txtcore/libs/tests/GuestHostStandardError.cppcore/libs/prx/libSceAgcDriver/CMakeLists.txtdocs/dev/debt/libkernel.mdcore/libs/prx/libSceAgcDriver/tests/ShaderDiskCache.cppcore/libs/prx/libSceAgcDriver/CMakeLists.txtcore/libs/prx/libSceAgcDriver/CMakeLists.txtcore/libs/prx/libSceAgcDriver/CMakeLists.txtcore/libs/prx/libSceAgcDriver/CMakeLists.txtcore/libs/prx/libSceAgcDriver/CMakeLists.txtcore/libs/prx/libSceAgcDriver/CMakeLists.txtcore/libs/prx/libc/src/General.cppcore/libs/prx/libkernel/CMakeLists.txtcore/libs/prx/libSceAgcDriver/tests/ShaderDiskCache.cppcore/libs/prx/libSceAgcDriver/CMakeLists.txtcore/libs/prx/libSceAgcDriver/tests/ShaderDiskCache.cppcore/libs/prx/libSceAgcDriver/CMakeLists.txtcore/libs/prx/libSceAgcDriver/CMakeLists.txtcore/libs/prx/libSceAgcDriver/tests/ShaderDiskCache.cppcore/libs/prx/libkernel/CMakeLists.txtdocs/dev/debt/libkernel.mdcore/libs/prx/libSceAgcDriver/CMakeLists.txtcore/libs/prx/libSceAgcDriver/tests/ShaderDiskCache.cppcore/libs/prx/libSceAgcDriver/CMakeLists.txtdocs/dev/debt/libkernel.mdcore/libs/prx/libSceAgcDriver/tests/ShaderDiskCache.cppcore/libs/prx/libSceAgcDriver/CMakeLists.txtcore/libs/prx/libkernel/CMakeLists.txtdocs/dev/debt/libkernel.mdcore/libs/prx/libSceAgcDriver/CMakeLists.txtcore/libs/prx/libSceAgcDriver/CMakeLists.txtdocs/dev/debt/libkernel.mdcore/libs/prx/libSceAgcDriver/CMakeLists.txtcore/libs/prx/libSceAgcDriver/CMakeLists.txtcore/libs/prx/libSceAgcDriver/CMakeLists.txtcore/libs/prx/libSceAgcDriver/CMakeLists.txtcore/libs/prx/libc/include/WindowsFormatting.hppcore/libs/prx/libc/src/General.cppcore/libs/prx/libkernel/File/src/Stdio.cppcore/shader/recompiler/CMakeLists.txtcore/libs/prx/libkernel/CMakeLists.txtcore/libs/prx/libkernel/File/src/Stdio.cppdocs/dev/debt/libkernel.mdcore/libs/prx/libSceAgcDriver/CMakeLists.txtcore/libs/prx/libSceAgcDriver/CMakeLists.txtcore/libs/prx/libSceAgcDriver/tests/ShaderDiskCache.cppcore/shader/recompiler/CMakeLists.txtcore/libs/prx/libSceAgcDriver/CMakeLists.txtcore/libs/prx/libSceAgcDriver/tests/ShaderDiskCache.cppcore/shader/recompiler/CMakeLists.txtcore/libs/prx/libSceAgcDriver/tests/ShaderDiskCache.cppcore/libs/prx/libkernel/CMakeLists.txtcore/libs/prx/libkernel/File/src/Stdio.cppcore/shader/recompiler/CMakeLists.txtdocs/dev/debt/libkernel.mdcore/libs/prx/libSceAgcDriver/CMakeLists.txtcore/libs/prx/libSceAgcDriver/CMakeLists.txtdocs/dev/debt/libkernel.mdcore/libs/prx/libSceAgcDriver/CMakeLists.txtcore/libs/prx/libSceAgcDriver/tests/ShaderDiskCache.cppdocs/dev/debt/libkernel.mddocs/dev/debt/libkernel.mdcore/libs/CMakeLists.txtcore/libs/prx/libSceAgcDriver/CMakeLists.txtcore/libs/prx/libc/src/General.cppcore/libs/prx/libc/src/RuntimeSupport.cppcore/libs/prx/libkernel/CMakeLists.txtcore/libs/prx/libkernel/File/src/Stdio.cppcore/shader/recompiler/CMakeLists.txtcore/libs/prx/libSceAgcDriver/CMakeLists.txtcore/libs/prx/libSceAgcDriver/tests/ShaderDiskCache.cppcore/libs/CMakeLists.txtcore/libs/prx/libSceAgcDriver/CMakeLists.txtcore/libs/prx/libc/src/General.cppcore/libs/prx/libc/src/RuntimeSupport.cppcore/libs/prx/libkernel/CMakeLists.txtcore/libs/prx/libkernel/File/src/Stdio.cppcore/shader/recompiler/CMakeLists.txtdocs/dev/debt/libkernel.mdcore/libs/prx/libSceAgcDriver/CMakeLists.txtcore/libs/prx/libSceAgcDriver/tests/ShaderDiskCache.cppcore/libs/prx/libSceAgcDriver/CMakeLists.txtcore/libs/prx/libSceAgcDriver/CMakeLists.txtcore/libs/prx/libSceAgcDriver/CMakeLists.txtcore/libs/prx/libkernel/CMakeLists.txtdocs/dev/debt/libkernel.mdcore/libs/prx/libSceAgcDriver/tests/ShaderDiskCache.cppcore/libs/prx/libSceAgcDriver/CMakeLists.txtdocs/dev/debt/libkernel.mdcore/libs/prx/libSceAgcDriver/CMakeLists.txtcore/libs/prx/libSceAgcDriver/tests/ShaderDiskCache.cppcore/libs/prx/libSceAgcDriver/CMakeLists.txtcore/libs/prx/libSceAgcDriver/CMakeLists.txtcore/libs/prx/libSceAgcDriver/tests/ShaderDiskCache.cppcore/shader/recompiler/CMakeLists.txtcore/libs/prx/libSceAgcDriver/CMakeLists.txtcore/libs/prx/libSceAgcDriver/CMakeLists.txtcore/libs/prx/libSceAgcDriver/tests/ShaderDiskCache.cppcore/libs/prx/libSceAgcDriver/CMakeLists.txtcore/shader/recompiler/CMakeLists.txtcore/libs/prx/libSceAgcDriver/CMakeLists.txtcore/shader/recompiler/CMakeLists.txtcore/libs/prx/libSceAgcDriver/CMakeLists.txtcore/libs/prx/libSceAgcDriver/CMakeLists.txtcore/libs/prx/libSceAgcDriver/CMakeLists.txtdocs/dev/debt/libkernel.mdImported from boykopovar/AnyPS5. Originally posted by jotitadev on 2026-10-19T10:28:22Z.
Review: COMMENTED
Linux run of
36dc34485, since the description says the Linuxguest_host_standard_errorstdout case was not run.Machine: Linux x86_64, i7-8700, Ubuntu 26.04, GCC 15.2, Release,
BUILD_TESTING=ON, full build. Head36dc34485merged ontomain8816fd772(clean merge).guest_host_standard_errorpasses, also 30 of 30 with--repeat until-fail:30, and the non-GPU suite is 775/775, same asmain.GuestHostStandardError.cpponmain(library unchanged) aborts, so the new stdout case fails without the LinuxKeepHostStandardOutputchange and passes with it.Written with AI assistance (Claude Code). I ran the builds and tests above on my machine.
Imported from boykopovar/AnyPS5. Originally posted by babyhuey on 2026-10-19T10:36:31Z.
Linux and Windows runs of this head (36dc34485), each merged onto
main64e2a810a(clean merge).Linux (Ubuntu 24.04 container, GCC 13, Release):
guest_host_standard_error, including the new standard-output case, and the threeguest_standard_streams*tests pass 5 of 5. No new warnings.Windows 11 (RTX 3080, WinLibs GCC 15.2, Release):
guest_host_standard_error_windowspasses 5 of 5. Eternal Darkness (PPSA99314, #7791), 30 s runs:main, 4 of 10 runs froze (about 160 frames, against about 415 in a healthy run). On64a9caa62it was 5 of 28.[gpu], 69[display], 30[pipelines]and 30 shader disk cache reports. Onmainthey mostly vanish into the title's pipe. Stdout is unchanged.One difference from Linux, for the title's own output. On
main, the title's stderr lines (starts (standard error to klog: yes),splash dismissed,pad: player 0 opened ...) come back through its pipe and klog as[debug:0] [Eternal Darkness] .... With this PR they arrive bare on the host stderr, and none take the klog path. The guest's_Stderr_nid_postfixwraps the same UCRTFILE(FileStream _Stderr_nid_postfix{stderr}inRuntimeSupport.cpp), so rewriting thatFILE's descriptor moves the guest'sfprintf(stderr, ...)off its owndup2target as well. Only a rawwrite(2, ...)stays on the pipe. On Linux,KeepHostStandardError_nid_no_patchassigns the hoststderra newFILE, and the guest stream keeps the original one on descriptor 2. Is that difference intended? A title that reads back what it wrote to stderr would get nothing on Windows.Imported from boykopovar/AnyPS5. Originally posted by oneandonlydean on 2026-10-19T12:22:33Z.
Thanks, keeping host logging off a guest
dup2makes sense. One problem with the Windows half, though:The guest's own stdio is built on the same host
FILEobjects:FileStream _Stdout_nid_postfix{stdout};and_Stderr_nid_postfix{stderr};(core/libs/prx/libc/src/RuntimeSupport.cpp:56-57), and guestprintf/puts/fwrite(stdout)go through_Stdout_nid_postfix(Formatting.cpp:314 onward).RetargetHostStream(Stdio.cpp:675-680 here) rewrites the descriptor inside the one UCRTstdout/stderrobject, so after a guestdup2(pipe, 1)the guest's ownprintfnow goes to the saved host descriptor instead of the pipe. On main it reaches the pipe, the way FreeBSD does. The same applies to guestfprintf(stderr, …)afterdup2(x, 2).On Linux this doesn't happen for the narrow-char path, because
stdout = stream(General.cpp:431) swaps the global pointer and_Stdout_nid_postfixkeeps the originalFILE. But FormattingWide.cpp:328 still writes guest wide output to the globalstdout, so after this PR guestwprintfalso skips the guest'sdup2on Linux.Could you:
FILE(an_fdopenof the saved descriptor) instead of rewriting the shared one, and point FormattingWide at_Stdout_nid_postfixrather than the globalstdout;guest_host_standard_error(_windows): guestpipe+dup2_nid_postfix(guest[1], 1), then a guestprintf_nid_postfix/puts_nid_postfix, and require the text to land on the guest pipe (and the same for descriptor 2 with guestfprintfon_Stderr_nid_postfix).The offset-24 write also relies on UCRT's private
FILElayout. The check that the word holds 1 or 2 stops a wrong write, but on any other layout every guestdup2onto 1 or 2 then throws. A separate hostFILEwould avoid that too.Imported from boykopovar/AnyPS5. Originally posted by mazen320 on 2026-10-19T12:45:31Z.
Addressed in e86a647c3.
The descriptor write inside the shared UCRT
FILEis gone. Afterdup2onto 1 or 2, host logging uses an_fdopenof the saved descriptor._Stdout_nid_postfixand_Stderr_nid_postfixstay on the descriptor that was replaced, and guestprintf,putsandfprintfwrite through those streams.wprintfdoes too, including on Linux, where it was still using the globalstdout.guest_host_standard_error_windowson this machine (Windows 11, WinLibs GCC 15.2.0, UCRT) now requires the guest text on the guest pipe:printfandputsafterdup2onto 1, andfprintfon_Stderr_nid_postfixafterdup2onto 2. The hostfputsstill stays on the saved descriptor. The same guest checks are inguest_host_standard_error; that Linux run was not repeated here.Imported from boykopovar/AnyPS5. Originally posted by oneandonlydean on 2026-10-20T00:52:27Z.
Thanks, e86a647c3 does what we asked: host logging has its own
_fdopenFILE, the offset-24 write is gone, and guestprintf/puts/vprintf/printf_s/wprintfnow write through the guest streams. Every implicit-stdoutcall in libc and libkernel is converted.One guest path is still left on the host stream:
perror_nid_postfix(core/libs/prx/libc/src/Errors.cpp:79-80) writes withstd::fprintf(stderr, …). On Windows, libc is now compiled with-include HostStandardStreams.hpp, so thatstderrisHostStandardErrorFile_nid_no_patch(). After a guestdup2(x, 2), guestperrortext goes to the saved host descriptor instead ofx. On main it reachesx, as on FreeBSD, whereperrorwrites to the process'sstderr(descriptor 2).On Linux this is already the case on main, because
KeepHostStandardError_nid_no_patchreplaces the globalstderr(General.cpp:401). Pointingperrorat the guest stream fixes both, e.g.GetNativeStream(&_Stderr_nid_postfix)(or__stderrp_nid_postfix, aswprintfnow uses__stdoutp_nid_postfix).Could you make that change and add a
perror_nid_postfixline to the descriptor-2 case inguest_host_standard_errorandguest_host_standard_error_windows, requiring its text on the guest pipe?Imported from boykopovar/AnyPS5. Originally posted by mazen320 on 2026-10-20T08:38:02Z.
perror_nid_postfixnow writes throughGetNativeStream(&_Stderr_nid_postfix).guest_host_standard_errorandguest_host_standard_error_windowsboth require the perror text on the guest pipe after adup2onto descriptor 2.Local Windows:
guest_host_standard_error_windows,guest_errors, andguest_safe_errorspassed. Conventions passed against the previous branch tip.Imported from boykopovar/AnyPS5. Originally posted by oneandonlydean on 2026-10-20T09:35:20Z.
Thanks for the
perrorchange. The full CI run onbf77da6fe5(merged withmain) is green on Linux, but on Windows three existing tests abort within about 0.1 s:guest_wide_formatting,guest_formattingandguest_standard_streams(exit 0xc0000409). They pass onmainon the same runner. The first two stop at:guest_wide_formatting:swprintf positional long double and the next argument(GuestWideFormatting.cpp:108,swprintf(u"%1$Lg %2$d %d", 0.5L, 3, 9))guest_formatting:Formatting check failedBoth are
long doubleformatting, with no stdout involved, so the likely cause is the new-include HostStandardStreams.hpprather than the routing. That header includes the C<stdio.h>, and the option makes it the first thing in every libc and prx translation unit on Windows. With MinGW-w64 and UCRT, the printf family only uses MinGW's C99 implementation (80-bitlong double, positional arguments) when__USE_MINGW_ANSI_STDIOis 1 at the first<stdio.h>. Nothing in the repository sets it, so it normally comes from libstdc++'sos_defines.hvia the first C++ standard header. With<stdio.h>forced first, stdio would bind to UCRT's printf, which readslong doubleas a 64-bit double. That would explain the "and the next argument" failure.A quick way to confirm it:
x86_64-w64-mingw32-g++ -std=gnu++20 -dM -E -include core/libs/prx/libc/include/HostStandardStreams.hpp core/libs/prx/libc/src/Formatting.cpp | grep USE_MINGW_ANSI_STDIO, with and without the-include.If that is it, including
<cstdio>instead of<stdio.h>inHostStandardStreams.hppwhen compiling C++ (so libstdc++'s configuration comes first), or defining__USE_MINGW_ANSI_STDIO=1on those targets, should restore the previous stdio.guest_standard_streamsaborts without a message, so please check it as well after the change.Imported from boykopovar/AnyPS5. Originally posted by mazen320 on 2026-10-20T09:39:19Z.
Good catch — confirmed locally.
-include HostStandardStreams.hppwas pulling in<stdio.h>before libstdc++ set__USE_MINGW_ANSI_STDIO, so MinGW bound the printf family to UCRT andlong doublewas read as a 64-bit double (matches theswprintf/ formatting failures).Head now defines
__USE_MINGW_ANSI_STDIObefore the include and uses<cstdio>for C++. On this machine after the change:guest_wide_formatting,guest_formatting,guest_standard_streams(+ closed/exclusive stdout) passguest_errors,guest_safe_errorspasspython tools/check_conventions.py --base upstream/mainpassesRebased onto current
main.Imported from boykopovar/AnyPS5. Originally posted by babyhuey on 2026-10-20T11:53:06Z.
Windows run of this head (
e1095000c), merged ontomain1d4164552(clean merge). Windows 11, RTX 3080, WinLibs GCC 15.2, Release.guest_host_standard_error_windows,guest_standard_streams,guest_standard_streams_closed_stdout,guest_standard_streams_exclusive_stdout,guest_formattingandguest_wide_formattingall pass 5 of 5.guest_standard_streams, which aborted with0xC0000409one86a647c3, passes now.main(877 to 931 frames). The emulator's[gpu],[log]andpipeline-cachelines reach the captured stderr, and the title'sstarts (standard error to klog: yes)andsplash dismissedcome back through its klog pipe as onmain.Two lines that
mainprints once per run never appear with this PR, neither through klog nor bare:pad: player 0 opened(klog-forwarded onmain);[shader-disk-cache] ...line (also klog-forwarded onmain, since it is written tostderr).Is something written through a path this PR doesn't cover? Possibly a host
stderrwrite made before the guest'sdup2, or a guest write that reaches the old descriptor after the swap.