fix(libc): leave write mode when an unbuffered update stream switches to reading on Windows #12036

Merged
Ragbie merged 2 commits from upstream-pr/12036 into main 2026-11-09 10:46:40 +00:00
Ragbie commented 2026-10-27 17:19:03 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by Ragbie on 2026-10-27T17:19:03Z.


What

On Windows a read that follows a write on an unbuffered update stream fails. With 0123456789 opened r+b and setvbuf(_IONBF), fputc('A') and fputc('B') then fgetc return EOF and set the error indicator, where FreeBSD returns '2'. fread, getc_unlocked, ungetc followed by fgetc, and an fflush before the read fail the same way. Writes that reach the UCRT through fputc, putc_unlocked or fputwc leave the stream in write mode; fwrite and fputs of more than one byte bypass it and already work.

FreeBSD 11.4's __srefill flushes a stream that is writing, clears __SWR and sets __SRD before it reads, and __ungetc does the same. FileStream::SwitchToReadingUnlocked (from #8226) flushes and adds _IOREAD, but leaves clearing _IOWRITE to the UCRT's _fflush_nolock. That only happens when the flush writes buffered bytes: a stream without a buffer returns 0 at once with _IOWRITE still set, and _filbuf then refuses to read.

The change clears _IOWRITE after a successful flush, before setting _IOREAD. A failed flush leaves the flags as they were, as before. On a buffered stream that the flush wrote, the UCRT has already cleared the flag, so nothing changes there. Linux and macOS are not touched. The libc debt entry already says reads directly after writes follow __srefill, which is now also true for unbuffered streams.

Tested

Windows 11 Pro, Intel Core i5-14600KF, WinLibs GCC 15.2.0, full Release build:

  • file_streams gets CheckUnbufferedStreamReadsAfterWrites. On an unbuffered r+b stream over 0123456789, each of fwrite, fputs, fputc, putc_unlocked, fprintf, fputwc and fputc with fflush writes AB. Then fgetc, getc_unlocked, a one-byte fread, or ungetc('Z') with two fgetc must read '2' without the error indicator, ftell must give 3, and the file must hold AB23456789.
  • Without the fix (main's sources): file_streams fails with fgetc after a write on an unbuffered r+ stream. The fwrite and fputs rounds before it pass.
  • With the fix:
    • file_streams and the guest_standard_streams tests pass with --repeat until-fail:5.
    • The 97 tests matching stream|stdio|printf|wide|format|fopen|freopen|fclose|fwrite|fread|putw|getw|ungetc|gets|scanf|puts|libc|random|fwide|setvbuf pass with --repeat until-fail:3, except guest_wide_formatting. That test fails on main here too: %Z gives the host's non-ASCII zone name, which #10987 addresses.
    • Full ctest on main c547deaac: 99% tests passed, 3 tests failed out of 2669.
      • guest_wide_formatting, as above.
      • agc_driver_image_plane_address_on_line also fails on main on this machine.
      • agc_driver_mesh_barycentric_emulation fails every time on this machine's NVIDIA GPU with the pixel shader did not emulate barycentrics. #9193 changed that test today. It does not use libc streams.
    • check_skips.py reports 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.

macOS: not tested.

Overlaps:

  • #8150 also adds a call to main of core/libs/tests/FileStreams.cpp, so the two conflict there; the tested behaviours are separate. #8150 already conflicts with main in StandardStreams.cpp, FileStreams.cpp and docs/dev/debt/libc.md. Proposed order, asked of #8150's author in this PR's conversation: this PR first; #8150 then keeps both calls when it rebases and lists #12036 under Depends on. If #8150 goes first instead, this PR rebases after it and Depends on becomes #8150.

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
  • Fixes: none
  • Unblocks: none
  • GPUs tested: n/a
  • AI-assisted: yes
<!-- greyfinch-sync:boykopovar/AnyPS5:issue:5038185736 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/12036). Originally posted by [Ragbie](https://github.com/Ragbie) on 2026-10-27T17:19:03Z. --- ### What On Windows a read that follows a write on an unbuffered update stream fails. With `0123456789` opened `r+b` and `setvbuf(_IONBF)`, `fputc('A')` and `fputc('B')` then `fgetc` return `EOF` and set the error indicator, where FreeBSD returns `'2'`. `fread`, `getc_unlocked`, `ungetc` followed by `fgetc`, and an `fflush` before the read fail the same way. Writes that reach the UCRT through `fputc`, `putc_unlocked` or `fputwc` leave the stream in write mode; `fwrite` and `fputs` of more than one byte bypass it and already work. FreeBSD 11.4's [`__srefill`](https://github.com/freebsd/freebsd-src/blob/release/11.4.0/lib/libc/stdio/refill.c#L84-L98) flushes a stream that is writing, clears `__SWR` and sets `__SRD` before it reads, and [`__ungetc`](https://github.com/freebsd/freebsd-src/blob/release/11.4.0/lib/libc/stdio/ungetc.c#L113-L128) does the same. `FileStream::SwitchToReadingUnlocked` (from #8226) flushes and adds `_IOREAD`, but leaves clearing `_IOWRITE` to the UCRT's `_fflush_nolock`. That only happens when the flush writes buffered bytes: a stream without a buffer returns 0 at once with `_IOWRITE` still set, and `_filbuf` then refuses to read. The change clears `_IOWRITE` after a successful flush, before setting `_IOREAD`. A failed flush leaves the flags as they were, as before. On a buffered stream that the flush wrote, the UCRT has already cleared the flag, so nothing changes there. Linux and macOS are not touched. The libc debt entry already says reads directly after writes follow `__srefill`, which is now also true for unbuffered streams. ### Tested Windows 11 Pro, Intel Core i5-14600KF, WinLibs GCC 15.2.0, full Release build: - `file_streams` gets `CheckUnbufferedStreamReadsAfterWrites`. On an unbuffered `r+b` stream over `0123456789`, each of `fwrite`, `fputs`, `fputc`, `putc_unlocked`, `fprintf`, `fputwc` and `fputc` with `fflush` writes `AB`. Then `fgetc`, `getc_unlocked`, a one-byte `fread`, or `ungetc('Z')` with two `fgetc` must read `'2'` without the error indicator, `ftell` must give 3, and the file must hold `AB23456789`. - **Without the fix (`main`'s sources):** `file_streams` fails with `fgetc after a write on an unbuffered r+ stream`. The `fwrite` and `fputs` rounds before it pass. - **With the fix:** - `file_streams` and the `guest_standard_streams` tests pass with `--repeat until-fail:5`. - The 97 tests matching `stream|stdio|printf|wide|format|fopen|freopen|fclose|fwrite|fread|putw|getw|ungetc|gets|scanf|puts|libc|random|fwide|setvbuf` pass with `--repeat until-fail:3`, except `guest_wide_formatting`. That test fails on `main` here too: `%Z` gives the host's non-ASCII zone name, which #10987 addresses. - Full `ctest` on `main` `c547deaac`: `99% tests passed, 3 tests failed out of 2669`. - `guest_wide_formatting`, as above. - `agc_driver_image_plane_address_on_line` also fails on `main` on this machine. - `agc_driver_mesh_barycentric_emulation` fails every time on this machine's NVIDIA GPU with `the pixel shader did not emulate barycentrics`. #9193 changed that test today. It does not use libc streams. - `check_skips.py` reports 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. macOS: not tested. **Overlaps:** - #8150 also adds a call to `main` of `core/libs/tests/FileStreams.cpp`, so the two conflict there; the tested behaviours are separate. #8150 already conflicts with `main` in `StandardStreams.cpp`, `FileStreams.cpp` and `docs/dev/debt/libc.md`. Proposed order, asked of #8150's author in this PR's conversation: this PR first; #8150 then keeps both calls when it rebases and lists #12036 under `Depends on`. If #8150 goes first instead, this PR rebases after it and `Depends on` becomes #8150. ### 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 - [x] Fixes: none - [x] Unblocks: none - [x] GPUs tested: n/a - [x] AI-assisted: yes
MarcZX10 commented 2026-10-27 19:45:39 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-10-27T19:45:39Z.

Review: DISMISSED


Reviewed exact head 9f16485c4188efc23cae31f13be4f6012fd4ce59. The Windows-specific flag update is narrowly placed after a successful flush, and the regression matrix exercises seven write APIs crossed with four read paths. The author reports a main control failing before the change and passing after it, plus repeated stream tests; hosted Windows and the Linux/macOS relinker and supporting checks pass. I did not independently reproduce the Windows runtime test.

One repository overlap item remains: the exact pairwise merge preview with open #8150 reports a content conflict in core/libs/tests/FileStreams.cpp. Both PRs add calls in that test executable's main, so this is an integration-order issue even though the test behaviors are distinct. CONTRIBUTING.md requires agreement with the overlapping PR author and a Depends on entry for this case. Please agree with #8150's author which test change lands first and record the order in this checklist; it currently says Depends on: none. The other shared-path hunks I checked (#5021, #7386, #7447 and #10324) do not conflict in the changed SwitchToReadingUnlocked behavior.

I am requesting changes for that coordination requirement, not for a defect in the stream fix.

<!-- greyfinch-sync:boykopovar/AnyPS5:review:5673287192 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/12036#pullrequestreview-5673287192). Originally posted by [MarcZX10](https://github.com/MarcZX10) on 2026-10-27T19:45:39Z. **Review:** DISMISSED --- Reviewed exact head `9f16485c4188efc23cae31f13be4f6012fd4ce59`. The Windows-specific flag update is narrowly placed after a successful flush, and the regression matrix exercises seven write APIs crossed with four read paths. The author reports a main control failing before the change and passing after it, plus repeated stream tests; hosted Windows and the Linux/macOS relinker and supporting checks pass. I did not independently reproduce the Windows runtime test. One repository overlap item remains: the exact pairwise merge preview with open #8150 reports a content conflict in `core/libs/tests/FileStreams.cpp`. Both PRs add calls in that test executable's `main`, so this is an integration-order issue even though the test behaviors are distinct. `CONTRIBUTING.md` requires agreement with the overlapping PR author and a `Depends on` entry for this case. Please agree with #8150's author which test change lands first and record the order in this checklist; it currently says `Depends on: none`. The other shared-path hunks I checked (#5021, #7386, #7447 and #10324) do not conflict in the changed `SwitchToReadingUnlocked` behavior. I am requesting changes for that coordination requirement, not for a defect in the stream fix.
adriadam10 commented 2026-10-28 09:59:26 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by adriadam10 on 2026-10-28T09:59:26Z.

Review: DISMISSED


The fix and the test look right: clearing UcrtWriting after a successful flush in SwitchToReadingUnlocked (FileStream.hpp:285) matches FreeBSD's __srefill, and the 7 writes x 4 reads matrix pins the position and file contents.

One thing left, the one @MarcZX10 requested: #8150 also adds a call in main of core/libs/tests/FileStreams.cpp and conflicts with this PR there (it already conflicts with main). Please agree with its author which goes first and record it under Depends on (this PR looks like the natural first one, Depends on: none stays if so and #8150 lists #12036). Once that is in the description this can go to the merge queue.

<!-- greyfinch-sync:boykopovar/AnyPS5:review:5680814201 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/12036#pullrequestreview-5680814201). Originally posted by [adriadam10](https://github.com/adriadam10) on 2026-10-28T09:59:26Z. **Review:** DISMISSED --- The fix and the test look right: clearing `UcrtWriting` after a successful flush in `SwitchToReadingUnlocked` (`FileStream.hpp:285`) matches FreeBSD's `__srefill`, and the 7 writes x 4 reads matrix pins the position and file contents. One thing left, the one @MarcZX10 requested: #8150 also adds a call in `main` of `core/libs/tests/FileStreams.cpp` and conflicts with this PR there (it already conflicts with `main`). Please agree with its author which goes first and record it under `Depends on` (this PR looks like the natural first one, `Depends on: none` stays if so and #8150 lists #12036). Once that is in the description this can go to the merge queue.
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!12036
No description provided.