feat(libc): implement buffered stream purge #10139

Merged
dotaku22 merged 2 commits from upstream-pr/10139 into main 2026-10-26 21:53:28 +00:00
dotaku22 commented 2026-10-23 10:37:34 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by dotaku22 on 2026-10-23T10:37:34Z.


What

Add the missing fpurge export to libc. It discards buffered input, ungetc pushback and pending output without writing or repositioning the descriptor, preserves EOF/error indicators, and returns -1 with EBADF for a closed stream. It holds the existing stream lock and republishes guest stream status.

The contract follows FreeBSD 11.4 fpurge.c. Linux uses __fpurge, macOS uses native fpurge, and Windows clears the buffer through the UCRT stream representation already used by FileStream. The implementation is original host integration code; no third-party source is copied. Console behavior and host-dependent buffering remain documented in libc technical debt.

Tested

Windows 11, WinLibs MinGW-w64 GCC 15.2.0, Release, based on upstream 513ca7e558 (head ad9dc21d9f). No game or console-hardware validation; Linux/macOS are not tested locally.

  • Built the production NID-patched libc library and the focused stream test executables.
  • ctest --test-dir build -R '^(guest_stream_purge|guest_binary_word|file_streams|guest_standard_streams.*)$' -V: 6/6 passed, aggregate 1.12 seconds.
  • New behavior checks cover discarded output, subsequent writes, input read-ahead and pushback, unchanged descriptor position, preserved errno/EOF/error, unbuffered pushback, a non-seekable pipe, repeated empty purges, and invalid streams.
  • tools/check_conventions.py --base origin/main passes; tools/check_test_registration.py finds no newly unregistered executable; git diff --check passes.
  • tools/pr_overlap.py --branch feat/libc-fpurge reports no directly conflicting PR. It flags 21 already-conflicting branches that share build/debt files; scoped inspection found no fpurge implementation or new test file in those branches. They concern other functions/platforms, so no dependency is required. The branch was subsequently rebased onto the same upstream base used by the overlap scan.

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:4981464808 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/10139). Originally posted by [dotaku22](https://github.com/dotaku22) on 2026-10-23T10:37:34Z. --- ### What Add the missing `fpurge` export to libc. It discards buffered input, `ungetc` pushback and pending output without writing or repositioning the descriptor, preserves EOF/error indicators, and returns -1 with EBADF for a closed stream. It holds the existing stream lock and republishes guest stream status. The contract follows FreeBSD 11.4 [fpurge.c](https://github.com/freebsd/freebsd-src/blob/releng/11.4/lib/libc/stdio/fpurge.c). Linux uses `__fpurge`, macOS uses native `fpurge`, and Windows clears the buffer through the UCRT stream representation already used by `FileStream`. The implementation is original host integration code; no third-party source is copied. Console behavior and host-dependent buffering remain documented in libc technical debt. ### Tested Windows 11, WinLibs MinGW-w64 GCC 15.2.0, Release, based on upstream 513ca7e558 (head ad9dc21d9f). No game or console-hardware validation; Linux/macOS are not tested locally. - Built the production NID-patched libc library and the focused stream test executables. - `ctest --test-dir build -R '^(guest_stream_purge|guest_binary_word|file_streams|guest_standard_streams.*)$' -V`: 6/6 passed, aggregate 1.12 seconds. - New behavior checks cover discarded output, subsequent writes, input read-ahead and pushback, unchanged descriptor position, preserved errno/EOF/error, unbuffered pushback, a non-seekable pipe, repeated empty purges, and invalid streams. - `tools/check_conventions.py --base origin/main` passes; `tools/check_test_registration.py` finds no newly unregistered executable; `git diff --check` passes. - `tools/pr_overlap.py --branch feat/libc-fpurge` reports no directly conflicting PR. It flags 21 already-conflicting branches that share build/debt files; scoped inspection found no `fpurge` implementation or new test file in those branches. They concern other functions/platforms, so no dependency is required. The branch was subsequently rebased onto the same upstream base used by the overlap scan. <!-- OS, title or homebrew, what was checked. "Not tested" is a valid answer. A performance change: the summary lines of main and of this PR from the same machine and scene, with the traces attached, captured as the Performance report issue form describes. --> ### 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 <!-- #PR, or none --> - [x] Fixes: none <!-- issue it closes as Fixes #N, or none --> - [x] Unblocks: none <!-- title ID and Game bring-up discussion, e.g. PPSA01325 #3232, or none --> - [x] GPUs tested: n/a <!-- for AGC, shader recompiler or VideoOut changes: vendor, model, driver and OS of each GPU the GPU tests ran on; otherwise n/a --> - [x] AI-assisted: yes <!-- yes / no -->
MarcZX10 commented 2026-10-23 10:43:54 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-10-23T10:43:54Z.

Review: APPROVED


I reviewed the wrapper and the new cases for output discard, input read-ahead/pushback, unbuffered input, non-seekable pipes, repeated empty purges, status preservation, and closed/null streams. The Linux path uses __fpurge; the Windows path resets the UCRT buffer pointers/count without seeking, and the macOS path uses fpurge. The existing stream lock surrounds the purge and status synchronization. Locally, GCC 12.2 syntax-checked StreamPurge.cpp and GuestStreamPurge.cpp; a small executable linked to the actual wrapper passed buffered-output discard/no-write/no-seek and EOF-preservation checks. The full repository test target could not be built in this workspace because the configured project lacks SDL2/XRandR dependencies. The author reports Windows 11/MinGW, with the new test and five related CTest entries passing (6/6). Current-main convention, diff-check, and sync checks pass (1 ahead/4 behind; merge clean). Hosted checks currently show conventions, labels, and report success; no product build result is present, so I am not claiming one.

<!-- greyfinch-sync:boykopovar/AnyPS5:review:5627149201 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/10139#pullrequestreview-5627149201). Originally posted by [MarcZX10](https://github.com/MarcZX10) on 2026-10-23T10:43:54Z. **Review:** APPROVED --- I reviewed the wrapper and the new cases for output discard, input read-ahead/pushback, unbuffered input, non-seekable pipes, repeated empty purges, status preservation, and closed/null streams. The Linux path uses `__fpurge`; the Windows path resets the UCRT buffer pointers/count without seeking, and the macOS path uses `fpurge`. The existing stream lock surrounds the purge and status synchronization. Locally, GCC 12.2 syntax-checked `StreamPurge.cpp` and `GuestStreamPurge.cpp`; a small executable linked to the actual wrapper passed buffered-output discard/no-write/no-seek and EOF-preservation checks. The full repository test target could not be built in this workspace because the configured project lacks SDL2/XRandR dependencies. The author reports Windows 11/MinGW, with the new test and five related CTest entries passing (6/6). Current-main convention, diff-check, and sync checks pass (1 ahead/4 behind; merge clean). Hosted checks currently show conventions, labels, and report success; no product build result is present, so I am not claiming one.
MarcZX10 commented 2026-10-23 11:11:28 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-10-23T11:11:28Z.

Review: APPROVED


Reviewed the exact head 171419e9588a301cede51f9a9578f335383401e4. The implementation locks the stream, purges host input/output buffering without seeking, resets the guest buffer cursor, and republishes EOF/error status; platform selection is limited to UCRT, glibc and macOS paths. The regression covers pending output, read-ahead plus pushback, non-seekable pipes, unbuffered input, repeated empty purges, status preservation and closed/null streams. Exact-head conventions and diff checks pass; hosted register-context, relinker platform and ASan checks pass, while full Linux/Windows product jobs were skipped. The PR reports its Windows libc tests passing; I did not independently run those tests. No blocking issue found.

<!-- greyfinch-sync:boykopovar/AnyPS5:review:5627425457 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/10139#pullrequestreview-5627425457). Originally posted by [MarcZX10](https://github.com/MarcZX10) on 2026-10-23T11:11:28Z. **Review:** APPROVED --- Reviewed the exact head `171419e9588a301cede51f9a9578f335383401e4`. The implementation locks the stream, purges host input/output buffering without seeking, resets the guest buffer cursor, and republishes EOF/error status; platform selection is limited to UCRT, glibc and macOS paths. The regression covers pending output, read-ahead plus pushback, non-seekable pipes, unbuffered input, repeated empty purges, status preservation and closed/null streams. Exact-head conventions and diff checks pass; hosted register-context, relinker platform and ASan checks pass, while full Linux/Windows product jobs were skipped. The PR reports its Windows libc tests passing; I did not independently run those tests. No blocking issue found.
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!10139
No description provided.