fix(libc): keep setvbuf stream buffers in host memory #12469

Merged
BunnyFl merged 4 commits from upstream-pr/12469 into main 2026-10-29 14:00:41 +00:00
BunnyFl commented 2026-10-28 15:06:55 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by BunnyFl on 2026-10-28T15:06:55Z.


What

setvbuf and setbuf pass the guest's buffer straight to the host C library's setvbuf. Every later refill of the stream then writes into that guest buffer through the kernel: the test below shows it for fgetc, and fgets, fscanf and the buffered part of fread go through the same refill. On Windows the refill goes through _read and ReadFile. None of these writes holds a GuestArena::HostWrite, which CONVENTIONS requires. A bracket can't cover them either, because they happen in later calls, not inside setvbuf.

On Windows, when the buffer is in a shared view whose pages the write watch left read-only, the refill fails. fgetc then returns EOF with the error indicator set, although the file still has data.

On Windows this PR stops passing the guest's buffer to the host library: UCRT allocates the stream's buffer itself, with the requested mode and size (per Microsoft's setvbuf documentation). No guest-visible FILE field holds the buffer pointer, so streams behave the same apart from one difference, recorded in debt/libc.md: the guest's buffer no longer receives the stream data, which FreeBSD's __srefill reads into it. Linux and macOS are unchanged. On those hosts the guest's buffer is still handed to the host library; the debt entry records that macOS refills into write-watched pages are expected to fail (not tested).

Overlap: #12057 changes the Windows part of the same function, to record a one-byte buffer for _IONBF. The two changes don't depend on each other; whichever lands second rebases.

Tested

Windows 11, WinLibs GCC 15.2.0 (Release):

  • New guest_stream_buffer_host_write (Windows only, modelled on guest_socket_host_write):
    • Setup: direct memory mapped twice and used as a 0x5000-byte setvbuf buffer, write watch armed, a 0xa000-byte file.
    • The test reads the whole file with fgetc and a small fread, re-arming the watch in between, and requires every byte, then EOF with no error and the guest buffer left untouched.
    • With this PR: passes.
    • With StandardStreams.cpp from main (same test): fails 3/3 at the first fgetc, which returns -1 with ferror set.
  • ctest -R "guest_standard_stream|guest_filesystem|guest_host_standard_error_windows|guest_stream_purge|^file_streams$|guest_stream_buffer_host_write": 10/10 pass. This covers the existing setvbuf callers in tests, including the stdout/stderr buffer test.
  • python tools/check_conventions.py --base upstream/main: passed.
  • python tools/pr_overlap.py --branch fix/libc-setvbuf-host-write --base main: #12057 only (StandardStreams.cpp L284).
  • Not tested on Linux 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 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: #12057
  • Fixes: none
  • Unblocks: none
  • GPUs tested: n/a
  • AI-assisted: yes (Claude Code)

🤖 Generated with Claude Code

<!-- greyfinch-sync:boykopovar/AnyPS5:issue:5052791682 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/12469). Originally posted by [BunnyFl](https://github.com/BunnyFl) on 2026-10-28T15:06:55Z. --- ### What `setvbuf` and `setbuf` pass the guest's buffer straight to the host C library's `setvbuf`. Every later refill of the stream then writes into that guest buffer through the kernel: the test below shows it for `fgetc`, and `fgets`, `fscanf` and the buffered part of `fread` go through the same refill. On Windows the refill goes through `_read` and `ReadFile`. None of these writes holds a `GuestArena::HostWrite`, which [CONVENTIONS](https://github.com/boykopovar/AnyPS5/blob/main/docs/dev/CONVENTIONS.md#host-writes-into-guest-memory) requires. A bracket can't cover them either, because they happen in later calls, not inside `setvbuf`. On Windows, when the buffer is in a shared view whose pages the write watch left read-only, the refill fails. `fgetc` then returns `EOF` with the error indicator set, although the file still has data. On Windows this PR stops passing the guest's buffer to the host library: UCRT allocates the stream's buffer itself, with the requested mode and size (per Microsoft's `setvbuf` documentation). No guest-visible `FILE` field holds the buffer pointer, so streams behave the same apart from one difference, recorded in [debt/libc.md](https://github.com/boykopovar/AnyPS5/blob/main/docs/dev/debt/libc.md): the guest's buffer no longer receives the stream data, which FreeBSD's `__srefill` reads into it. Linux and macOS are unchanged. On those hosts the guest's buffer is still handed to the host library; the debt entry records that macOS refills into write-watched pages are expected to fail (not tested). Overlap: #12057 changes the Windows part of the same function, to record a one-byte buffer for `_IONBF`. The two changes don't depend on each other; whichever lands second rebases. ### Tested Windows 11, WinLibs GCC 15.2.0 (Release): - New `guest_stream_buffer_host_write` (Windows only, modelled on `guest_socket_host_write`): - Setup: direct memory mapped twice and used as a 0x5000-byte `setvbuf` buffer, write watch armed, a 0xa000-byte file. - The test reads the whole file with `fgetc` and a small `fread`, re-arming the watch in between, and requires every byte, then `EOF` with no error and the guest buffer left untouched. - With this PR: passes. - With `StandardStreams.cpp` from `main` (same test): fails 3/3 at the first `fgetc`, which returns `-1` with `ferror` set. - `ctest -R "guest_standard_stream|guest_filesystem|guest_host_standard_error_windows|guest_stream_purge|^file_streams$|guest_stream_buffer_host_write"`: 10/10 pass. This covers the existing `setvbuf` callers in tests, including the stdout/stderr buffer test. - `python tools/check_conventions.py --base upstream/main`: passed. - `python tools/pr_overlap.py --branch fix/libc-setvbuf-host-write --base main`: #12057 only (`StandardStreams.cpp` L284). - Not tested on Linux 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 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: #12057 - [x] Fixes: none - [x] Unblocks: none - [x] GPUs tested: n/a - [x] AI-assisted: yes (Claude Code) 🤖 Generated with [Claude Code](https://claude.com/claude-code)
MarcZX10 commented 2026-10-28 16:15:24 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-10-28T16:15:24Z.

Review: COMMENTED


I reviewed exact head df28f3f54be8. The change is Windows-only and passes nullptr to UCRT setvbuf, retaining the requested buffering mode and size while keeping subsequent host ReadFile refills out of watched guest pages. The new regression creates a directly mapped buffer, arms write tracking, exercises both fgetc and buffered fread, re-arms tracking for a later refill, and checks data integrity, EOF/error state, page protection, and that the supplied guest buffer remains unchanged. The debt entry clearly records the guest-visible buffer difference and that Linux/macOS remain unchanged, with macOS risk marked untested. I found no blocker in this scoped review.

Hosted Windows, conventions, eligibility, labels, register-context Debug/Release on Ubuntu, relinker Ubuntu/Windows and ASan pass. macOS lanes are pending; Linux guest tests are skipped. The author reports the focused Windows coverage and red control. I did not rerun the Windows-specific test locally, so this is a scoped comment rather than an approval.

<!-- greyfinch-sync:boykopovar/AnyPS5:review:5685941216 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/12469#pullrequestreview-5685941216). Originally posted by [MarcZX10](https://github.com/MarcZX10) on 2026-10-28T16:15:24Z. **Review:** COMMENTED --- I reviewed exact head `df28f3f54be8`. The change is Windows-only and passes `nullptr` to UCRT `setvbuf`, retaining the requested buffering mode and size while keeping subsequent host `ReadFile` refills out of watched guest pages. The new regression creates a directly mapped buffer, arms write tracking, exercises both `fgetc` and buffered `fread`, re-arms tracking for a later refill, and checks data integrity, EOF/error state, page protection, and that the supplied guest buffer remains unchanged. The debt entry clearly records the guest-visible buffer difference and that Linux/macOS remain unchanged, with macOS risk marked untested. I found no blocker in this scoped review. Hosted Windows, conventions, eligibility, labels, register-context Debug/Release on Ubuntu, relinker Ubuntu/Windows and ASan pass. macOS lanes are pending; Linux guest tests are skipped. The author reports the focused Windows coverage and red control. I did not rerun the Windows-specific test locally, so this is a scoped comment rather than an approval.
MarcZX10 commented 2026-10-29 08:29:11 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-10-29T08:29:11Z.

Review: APPROVED


Re-reviewed exact head df28f3f54be811af90a64f0628037e2501d28795. The Windows-only _WIN32 guard now leaves Linux/macOS host-buffer behavior unchanged, and the Windows regression checks the guest buffer remains untouched after fgetc. The author reports the focused test passing 3/3 on the PR head and failing 3/3 with the old source; the related stream tests also pass. The hosted Windows job and current relinker/specialized jobs pass, while Linux product tests are skipped by path policy. Against current upstream/main 4c7d275bef89d309313d16bc2bc05d37a1b50be2, the merge preview is clean; git diff --check and python3 tools/check_conventions.py --base upstream/main pass. I found no remaining blocker.

<!-- greyfinch-sync:boykopovar/AnyPS5:review:5694789988 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/12469#pullrequestreview-5694789988). Originally posted by [MarcZX10](https://github.com/MarcZX10) on 2026-10-29T08:29:11Z. **Review:** APPROVED --- Re-reviewed exact head `df28f3f54be811af90a64f0628037e2501d28795`. The Windows-only `_WIN32` guard now leaves Linux/macOS host-buffer behavior unchanged, and the Windows regression checks the guest buffer remains untouched after `fgetc`. The author reports the focused test passing 3/3 on the PR head and failing 3/3 with the old source; the related stream tests also pass. The hosted Windows job and current relinker/specialized jobs pass, while Linux product tests are skipped by path policy. Against current `upstream/main` `4c7d275bef89d309313d16bc2bc05d37a1b50be2`, the merge preview is clean; `git diff --check` and `python3 tools/check_conventions.py --base upstream/main` pass. I found no remaining blocker.
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!12469
No description provided.