fix(libc): keep the host standard stream when a guest freopen of it fails #5668
Loading…
Reference in a new issue
No description provided.
Delete branch "upstream-pr/5668"
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 babyhuey on 2026-10-14T05:37:28Z.
What
A guest
freopenofstdin,stdoutorstderrreopens the host's own standard stream, since the guestFileStreamwraps it. When thatfreopenfails, the hostfreopenhas already closed the host stream. From then on, every host-side print to it fails: on Linux silently withEBADF, on Windows through the UCRT invalid-parameter handler, which reportsFATAL: UCRT invalid parameterand dumps all threads.Now, for a host standard stream:
FileStream::Reopenduplicates the stream's descriptor (DuplicateHostDescriptor_nid_no_patch) and holds the stream lock across thefreopen. If thefreopenfails, it reopens the host stream on the null device, moves the duplicate back onto its descriptor withdup2(_dup2on Windows) and closes the duplicate. The guestFileStreamstays closed, so the guest sees a closed stream as on FreeBSD (#5632). The target is opened once, by the realfreopen, so"wx","w"and"a"behave as onmain.freopen_nid_postfixfails path resolution, it detaches the guestFileStreaminstead of callingClose(), so the host stream is never closed.stderralready moves the host onto its kept copy (KeepHostStandardError_nid_no_patch), so onlystdinandstdoutneed the restore there.Streams that aren't standard behave as before.
Tested
On the PR head
2d8d3b9d(rebased ontomain5bf2e139e), Linux (Ubuntu 24.04, GCC 13) and Windows 11 (MinGW-w64 GCC 15.2 from WinLibs), Release:guest_standard_streams,guest_standard_streams_closed_stdout, the newguest_standard_streams_exclusive_stdoutandfile_streamseach pass 5/5 (--repeat until-fail:5) on both.--closed-stdoutnow also writes and flushes the hoststdoutafter the failed guestfreopen, then does the same for a failed"a"reopen ofstderr; it fails againstmain's library (on Windows with0xC0000409).--exclusive-stdoutreopensstdoutwith"wx"on a fresh file, writes through it and checks the bytes; it fails against the previous head's probe (the reportedEEXIST).PS5LM (PPSA99581 Plus 0.8.3 and PPSA99575 0.9.0) calls
freopen("/data/PS5LM/app.log", "w", stdout)and then reopensstderrwith"a"at start-up, and/data/PS5LMdoesn't exist:Linux, headless (SDL offscreen), 3 runs each (library code identical to the PR head; only the test file changed since): both stop at the same
DisplayWindowVulkan check on both builds. Onmainthe 12 host log lines after thefreopen(Vulkan device setup) are missing from stdout; with this PR they are there.Windows 11 (RTX 3080), 60 s, 2 runs each, PS5LM Plus 0.8.3 and PS5LM 0.9.0:
FATAL: UCRT invalid parameterreportsmain5bf2e139eTransferring control to ELF entry point)With this PR the PS5LM Plus dashboard and the PS5LM 0.9.0 "Choose a model" page draw, and host lines keep reaching
stderrafter the failed reopens ([HUI] pad open,[HUI] audio open, theno modelsnotification). Onmain,stderrholds only the UCRT reports and their backtraces.PS5LM Plus 0.8.4, now in the catalog, creates
/data/PS5LMitself (it opens/data/PS5LM/.probewithO_CREAT), so itsfreopensucceeds and it no longer reaches this path; PS5LM 0.9.0 still does.No new warnings in the files this PR touches on either OS.
check_conventions.pypasses.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)Reopen; whichever lands second needs a small merge.Imported from boykopovar/AnyPS5. Originally posted by github-actions[bot] on 2026-10-14T19:24:45Z.
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)core/libs/prx/libc/include/FileStream.hppL82Some 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/libc/CMakeLists.txtcore/libs/prx/libc/include/FileStream.hppcore/libs/prx/libc/src/General.cppcore/libs/prx/libc/CMakeLists.txtcore/libs/tests/GuestStandardStreams.cppcore/libs/prx/libc/CMakeLists.txtcore/libs/prx/libc/include/FileStream.hppcore/libs/prx/libc/src/General.cppcore/libs/prx/libc/src/General.cppcore/libs/prx/libc/CMakeLists.txtcore/libs/prx/libc/src/General.cppcore/libs/prx/libc/CMakeLists.txtcore/libs/prx/libc/CMakeLists.txtcore/libs/prx/libc/include/FileStream.hppcore/libs/prx/libc/src/FileAndHeap.cppcore/libs/prx/libc/src/General.cppcore/libs/prx/libc/CMakeLists.txtcore/libs/prx/libc/include/FileStream.hppcore/libs/prx/libc/src/FileAndHeap.cppcore/libs/prx/libc/src/General.cppcore/libs/prx/libc/CMakeLists.txtcore/libs/prx/libc/include/FileStream.hppcore/libs/prx/libc/src/FileAndHeap.cppcore/libs/prx/libc/src/General.cppImported from boykopovar/AnyPS5. Originally posted by UmillD on 2026-10-15T08:15:44Z.
I found a regression in FileStream::Reopen on head 881a1090bf837090e4aba729e64ed42d4b437639: successful exclusive creation of a standard stream now fails.
For a fresh filename, FileStream(stdout).Reopen(path, "wx") succeeds on base
d8888df9b2. On this PR, _wfopen creates the file during the probe; the following _wfreopen of the same path with "wx" fails with EEXIST (errno 17). The guest gets a failed reopen even though its call created the file.I compiled the exact base and candidate FileStream.hpp headers independently on Windows using WinLibs GCC 15.2, C++20, O2. All ten paired runs gave base=true/errno=0 versus candidate=false/errno=17/file_exists=true. The Windows handle-preservation and file-lock integration callbacks were no-ops in this focused harness; this is method-level evidence, not a full runtime or PS5LM run.
The existing missing-directory test does not cover this success path. Could the fix avoid opening the target twice with its destructive/exclusive mode, and add a regression for successful "wx" creation through a standard stream? The original missing-directory/host-output regression should remain covered as well.
AI-assisted: yes, investigated with Codex. No replacement implementation is proposed in this report.
Update: I also reproduced this through the real guest libc exports, with no handle/lock callback stubs. The test calls freopen_nid_postfix(path, "wx", __stdoutp_nid_postfix), then fputs_nid_postfix/fflush_nid_postfix and checks the resulting payload; all calls use the guest sysv_abi convention and errno is read through __error_nid_postfix.
This was a controlled local integration: validation commit 4879dc913 plus this PR's four-file patch applied cleanly, rather than a build of the isolated PR head. After rebuilding libc, all ten candidate processes failed with nullptr/errno 17 and an empty created file. After reversing the patch and rebuilding, all ten control processes successfully created the file and verified the payload. Meanwhile guest_standard_streams, guest_standard_streams_failed_reopen and file_streams each passed five runs on the candidate (15/15). No title execution is claimed.
Imported from boykopovar/AnyPS5. Originally posted by adriadam10 on 2026-10-15T11:51:26Z.
Review: CHANGES_REQUESTED
Thanks for the fix, and for the PS5LM numbers. Checked the diff against the new head; one regression blocks it, the one @UmillD reported:
core/libs/prx/libc/include/FileStream.hpp:96-106: the probe opens the target with the guest's own mode before the real reopen. With"wx"the probe creates the file, so the followingfreopenof the same path fails withEEXIST. A validfreopen(fresh, "wx", stdout)that succeeds onmainnow fails and leaves the stream detached. The same double open also truncates/creates twice for"w"/"a".core/libs/tests/GuestStandardStreams.cpp:497-501: only the failing missing-directory case is covered. Please add a regression for a successful"wx"reopen of a standard stream (write through it and check the file contents), and keep the existing one.Direction: avoid opening the target twice. For example, keep a
dupof the host stream's descriptor, run the realfreopen, and on failure restore the descriptor withdup2(or_dup/_dup2on Windows) and detach the guest stream; that way no destructive probe is needed. Your call on the exact design.Imported from boykopovar/AnyPS5. Originally posted by DavidSamir on 2026-10-16T18:01:42Z.
Windows run on a second PS5LM entry: PS5LM 0.9.0 (PPSA99575, catalog release; the PR text tested PPSA99581 Plus). Its eboot contains
/data/PS5LM/app.log,/data/PS5LM/app-args.txtand/data/PS5LM/cache, and/data/PS5LMdoes not exist in a new run folder.Windows 11 Pro build 26300, Intel i7-11700F, NVIDIA GeForce RTX 3070, WinLibs GCC 15.2, Release, relinked with
--windows, 60 s runs, fixed controller sequence, a new output folder each time:FATAL: UCRT invalid parameterreports in the process's stderrmain82ddae75b6Transferring control to ELF entry point)main82ddae75b6+ this PR's library change (FileStream.hpp,FileAndHeap.cpp)With gdb on the unfixed build, the first
_invalid_parameter_noinfois reached fromucrtbase!fwrite, called byAgcDriver::AsyncLog::Run()in libSceAgcDriver, so the host log thread is the one writing to the closed host stream.Note for the rebase: on current
mainthis PR merges with conflicts incore/libs/prx/libc/CMakeLists.txtandcore/libs/tests/GuestStandardStreams.cpp. Both sides add a test next toguest_standard_streams(mainhasguest_standard_streams_closed_stdoutand aCheckClosedStreamWrites/CheckClosedStandardOutputpair; this PR addsguest_standard_streams_failed_reopen). For this run I keptmain's side of both files and took only the library change, so this PR's own test was not run here. I did not measure frame rate.AI-assisted: yes
Imported from boykopovar/AnyPS5. Originally posted by babyhuey on 2026-10-18T13:48:37Z.
@adriadam10 Reworked as suggested, and rebased onto
main5bf2e139e. The probe is gone, so the target is opened once, by the realfreopen:Reopenduplicates its descriptor first and holds the stream lock across thefreopen. If thefreopenfails, it reopens the host stream on the null device and moves the duplicate back onto its descriptor withdup2(_dup2on Windows), then drops the duplicate. The null-device reopen is needed because a failedfreopenleaves the FILE itself closed, so restoring only the descriptor would still leave the host's nextfwriteon a closed stream. The guestFileStreamstays closed, as #5632 expects. On a successfulfreopenthe duplicate is just closed.freopen(fresh, "wx", stdout)now creates the file and writes through it. The newguest_standard_streams_exclusive_stdouttest covers it: it fails against the previous head (abort on thefreopen), and passes on this one. The missing-directory host-write check moved intomain's existing--closed-stdouttest, which fails againstmain's library and passes here.Thanks @UmillD for the
"wx"reproduction. Results for both OSes are in the description.