fix(libc): keep setvbuf stream buffers in host memory #12469
Loading…
Reference in a new issue
No description provided.
Delete branch "upstream-pr/12469"
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 BunnyFl on 2026-10-28T15:06:55Z.
What
setvbufandsetbufpass the guest's buffer straight to the host C library'ssetvbuf. Every later refill of the stream then writes into that guest buffer through the kernel: the test below shows it forfgetc, andfgets,fscanfand the buffered part offreadgo through the same refill. On Windows the refill goes through_readandReadFile. None of these writes holds aGuestArena::HostWrite, which CONVENTIONS requires. A bracket can't cover them either, because they happen in later calls, not insidesetvbuf.On Windows, when the buffer is in a shared view whose pages the write watch left read-only, the refill fails.
fgetcthen returnsEOFwith 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
setvbufdocumentation). No guest-visibleFILEfield 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__srefillreads 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):
guest_stream_buffer_host_write(Windows only, modelled onguest_socket_host_write):setvbufbuffer, write watch armed, a 0xa000-byte file.fgetcand a smallfread, re-arming the watch in between, and requires every byte, thenEOFwith no error and the guest buffer left untouched.StandardStreams.cppfrommain(same test): fails 3/3 at the firstfgetc, which returns-1withferrorset.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 existingsetvbufcallers 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.cppL284).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)🤖 Generated with Claude Code
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 passesnullptrto UCRTsetvbuf, retaining the requested buffering mode and size while keeping subsequent hostReadFilerefills out of watched guest pages. The new regression creates a directly mapped buffer, arms write tracking, exercises bothfgetcand bufferedfread, 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.
Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-10-29T08:29:11Z.
Review: APPROVED
Re-reviewed exact head
df28f3f54be811af90a64f0628037e2501d28795. The Windows-only_WIN32guard now leaves Linux/macOS host-buffer behavior unchanged, and the Windows regression checks the guest buffer remains untouched afterfgetc. 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 currentupstream/main4c7d275bef89d309313d16bc2bc05d37a1b50be2, the merge preview is clean;git diff --checkandpython3 tools/check_conventions.py --base upstream/mainpass. I found no remaining blocker.