fix(libc): give an unbuffered stream FreeBSD's one-byte buffer on Windows #12057
Loading…
Reference in a new issue
No description provided.
Delete branch "upstream-pr/12057"
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 Ragbie on 2026-10-27T17:53:13Z.
What
On Windows a write that follows a read on an unbuffered update stream lands one byte too far. With
0123456789openedr+bandsetvbuf(_IONBF),fgetcthenfputs("XY")gives01XY456789, where FreeBSD and Linux glibc give0XY3456789. Afreadof three bytes thenfputs("XY")gives0123XY6789instead of012XY56789.The UCRT's
setvbuf(_IONBF)gives the stream its two-byte_charbufas the buffer, so a refill reads two bytes. Since #11560, a write after a read follows FreeBSD 11.4's__swsetup: it drops the read buffer and writes at the descriptor's offset. FreeBSD'ssetvbufgives an unbuffered stream its one-byte_nbuf(_bf._size = 1, L111-L117), so its descriptor offset stays at the stream position. The UCRT's sits one byte past it.The change: on Windows,
setvbufwith_IONBFsets the UCRT stream's buffer size to 1 after a successful call, under the stream lock. A refill then reads one byte, as on FreeBSD, andfreadreads exactly the requested count, which FreeBSD's__freadreaches through one-byte refills.UcrtStreamStategets the three fields up to_bufsizin the UCRT's__crt_stdio_stream_datalayout. Nothing seeks, so this also works on a stream that cannot seek. Linux and macOS are not touched.The UCRT's own wide reads need two bytes in the buffer, but no guest stream function reaches them:
fgetwc,fgetwsand the wide scanners decode through the guest's narrowfgetc, andungetwccallsungetc. The libc debt entry for writes after reads no longer says unbuffered streams read two bytes ahead.Tested
Rebased onto
main73f4f7e06. The only conflict was thesetvbufblock shared with #12469, resolved as described under Overlaps. On this head:file_streamsand theguest_standard_streams*tests (six in all) pass with--repeat until-fail:5, and the 121 tests matching the pattern below pass with--repeat until-fail:3exceptguest_wide_formatting(see below). Withmain'sFileStream.hppandStandardStreams.cppand this head's tests,file_streamsfails withWrite after read wrote other bytes.--repeat until-fail:5, and the 93 tests matching the pattern with--repeat until-fail:3.The results below are from the earlier base
3ae8209b6.Windows 11 Pro, Intel Core i5-14600KF, WinLibs GCC 15.2.0, full Release build:
file_streamsgets two new cases inCheckStreamWritesAfterReads:fwrite,fputs,fputc,putc_unlocked,fprintf,fprintf_s,fputwc,fputws,fwprintf) writesXYafterfgetcon an unbufferedr+bstream, and the file must hold0XY3456789.fputs("XY")after a three-bytefreadon an unbufferedr+bstream must give012XY56789.main's sources):file_streamsfails withWrite after read wrote other bytes.file_streamsand theguest_standard_streamstests pass with--repeat until-fail:5.stream|stdio|printf|wide|format|fopen|freopen|fclose|fwrite|fread|putw|getw|ungetc|gets|scanf|puts|libc|random|fwide|setvbuf|purgepass with--repeat until-fail:3, exceptguest_wide_formatting. That test fails onmainhere too:%Zgives the host's non-ASCII zone name, which #10987 addresses.ctestonmain3ae8209b6:99% tests passed, 3 tests failed out of 2673.guest_wide_formatting, as above.agc_driver_image_plane_address_on_linealso fails onmainon this machine.agc_driver_mesh_barycentric_emulationfails every time on this machine's NVIDIA GPU withthe pixel shader did not emulate barycentrics. #9193 changed that test on 2026-10-27, and it does not use libc streams.check_skips.pyreports one skip,guest_fopen_errors. It skips itself on this machine because creating symbolic links needs Developer Mode.Linux (Ubuntu 24.04 container, GCC 13.3.0), where the change does nothing: the 70 tests matching the same pattern pass with
--repeat until-fail:3, including the new cases. Their expectations hold on glibc as on FreeBSD.macOS: not tested.
Overlaps:
SwitchToReadingUnlockedin the same header and adds a test function to the same file. The two merge cleanly and neither needs the other./dev/randomentry indocs/dev/debt/libc.md. Order agreed with its author: this PR first; #10620 listsDepends on: #12057and keeps this PR'sWrites on an update streamentry and its own/dev/randomentry on its rebase.buffer = nullptr;to the same_WIN32block ofsetvbuf_nid_postfix. The behaviours are independent: with_IONBFUCRT ignores the buffer argument and uses_charbuf, which is what this PR shrinks to one byte. The rebase keeps its line after the twoKeepHostStandard*checks and before_lock_file, the shape agreed with its author.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 MarcZX10 on 2026-10-27T18:13:18Z.
Review: CHANGES_REQUESTED
The Windows-specific implementation and test changes look coherent on inspection: the added
UcrtStreamStatefields match the UCRT layout, the stream is locked aroundsetvbufand the_bufsizadjustment, and the new cases cover bothfgetcandfreadfollowed by writes. I also verified that the pairwise merge of #12036 and this PR is clean, and the current-main merge preview for this PR is clean.There is one repository-rule blocker to resolve before approval. #10620 is still open and edits the same
Writes on an update streamentry indocs/dev/debt/libc.md. I confirmed that merging the current #10620 and #12057 heads conflicts in that file. Your description notes the overlap but does not say which PR goes first or record an agreed order; the checklist still saysDepends on: none.CONTRIBUTING.mdrequires agreeing the order with the overlapping author and declaring it on the checklist. Please coordinate the order with #10620’s author and update the dependency/checklist plus the combined debt wording accordingly.Local validation:
file_streams_testsbuilt on Linux with the added tests, but its QEMU run stopped in guest-path setup because QEMU returnedENOSYSforprocess_vm_readv; this is an emulator limitation, not a test assertion result. Your Windows runtime results are therefore the available runtime evidence here; hosted Windows passes and the other completed CI jobs are green. macOS jobs are still queued.Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-10-29T00:12:18Z.
Review: CHANGES_REQUESTED
Reviewed — changes requested
The merge order with #10620 and #12469 is now recorded, but #12057 still needs rebase and focused retesting because current
mainchanged bothFileStream.hppand the shared debt file.What I ran
Reviewed exact head
b03facfbd4ab0e7dee657cd827d6806cf99d19a0; its merge base with currentmainis3ae8209b643be054ef09542f9e9e5e951b1ed73b. The individual merge preview onto currentmainis clean. The pairwise merge with #10620 still conflicts only indocs/dev/debt/libc.md, consistent with the now-recorded order of #12057 first and #10620 after it. Environment: Linux x86-64, Git and GitHub CLI. I did not rerun the build or runtime tests in this re-check.Results
core/libs/prx/libc/include/FileStream.hppanddocs/dev/debt/libc.mdDepends on: #12057Command output
Negative control
Findings
Blocking
mainis3ae8209b643b, and currentmainhas since changedFileStream.hppanddocs/dev/debt/libc.md, both touched by this PR. The merge preview is clean, but the integrated code and test evidence need to be refreshed on the current branch.Non-blocking
Sources
Not verified
file_streamson this head, and I did not run a native PS5 test. The earlier Linux/QEMU attempt stopped in setup because QEMU returnedENOSYSforprocess_vm_readv; that is not a test assertion result.To get this to a good state
Rebase #12057 onto current
main, retain #12057-first wording in both PR descriptions and preserve the current-main debt text. Rerunfile_streamsandguest_standard_streamson the new head and record exact commands/results. I will re-check the changed code and repeat the locally available focused validation.Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-10-31T16:47:28Z.
Review: APPROVED
Re-checked — looks good
The rebased head addresses the earlier freshness and focused-test follow-up; the current-main preview is clean and the changed Windows stream test passes in CI.
What I ran
Reviewed head
a745546f030f310044b3004cf5354498a30c997fagainst current base474030e58424fefdb65e5df9f3820810fbb4553c. The merge preview is clean. The five commits added tomainafter the author’s stated rebase touched no paths changed by this PR. Environment: Linux x86-64, Python 3.11.2, Git 2.39.5; I did not build locally.Results
file_streamstarget passed; the changed-test step reports 2,523/2,523 selected tests passed.guest_wide_formatting), and the wider Linux set passed.write_watch_read-faultwas registered twice; this PR changes no write-watch files.Depends on: none. The direct merge preview above is clean.Selected hosted CI output
Negative control
FileStream.hppandStandardStreams.cppfrommainwhile retaining this head’s testsfile_streamsreportedly failed withWrite after read wrote other bytes; I did not independently rerun the control.Findings
Blocking
None found on the reviewed head.
Non-blocking
The macOS write-watch job needs its duplicate CMake test registration resolved in the workflow/test setup; it does not involve files changed by this PR.
Sources
Not verified
I did not build or run Linux/macOS tests locally, repeat the author’s reported Linux runs, or independently reproduce the negative control. The macOS write-watch job failed at CMake configure time as shown above.