fix(libc): discard pushback bookkeeping when reopening a stream #27494

Open
JJenkx wants to merge 2 commits from upstream-pr/27494 into main
JJenkx commented 2026-11-26 14:41:28 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by JJenkx on 2026-11-26T14:41:28Z.


What

After ungetc on a stream, freopen replaces the native handle but leaves the wrapper's narrow pushback bookkeeping live. A subsequent fpurge can restore a byte from the old file into the reopened stream. Clear that bookkeeping in FileStream::Adopt, alongside the other per-handle resets. The regression reopens a file containing new and requires fpurge followed by fread to return those three bytes.

This follows the reset of the old pushback buffer in FreeBSD 11.4 freopen; no reference source code is copied. This fixes main independently of the close-all/alias work in #11831.

Tested

2026-11-26, Linux 7.3.5-arch1-1 x86-64, GCC 16.2.1, Release. Base af6a58c4d30bbc8901b5855081002cd965254b7c; head 2b1eec1ddd570840d07439af97d489703e0391a0. Fresh independent source/build with 49 verified submodules and newly built raw libc/kernel providers.

  • 14/14 focused CPU CTests pass, zero skips: stream purge, file streams/status, five standard-stream modes, fwide, wide-stream scanning, formatting, host-standard reopen and both registered host-log reopen cases.
  • The same final regression executable (SHA-256 51455729e9b009f4e22968fcdc6a10b0928480fd48f219d0cad145dae4d68d54) passes with the fixed provider and aborts at reopen discards the previous stream's pushback with the preserved main provider. Main production source is unchanged in that control; only the identical fixture was added. Actual provider initialization paths are checked with LD_DEBUG; ldd -r is clean.
  • Conventions, debt-fold and whitespace checks pass. No Windows/macOS, patched SDK, title/GPU, console, performance or full-suite result is claimed.

Patch, fixture, build/test logs, JUnit, loader records and hashes, SHA-256 7e86831b25bee09b61673d8ddd1566a84c0614cd4d30024ccd29a7a36fa75a75; anonymous download and all 33 payload hashes verified.

Before implementation, the export lookup found #11831 plus two library-split PRs for freopen, and #11831 for fpurge. The full branch scan examined 2,407 open PRs but failed closed on seven conflicting branches. I manually compared their shared FileStream.hpp patches and Adopt bodies at the pinned heads in the archive: #7447, #11831, #12682, #12688, #12700, #12965 and #24406. None contains this newer narrow-pushback bookkeeping or its reset; their archive, lifetime, descriptor-locking and bad-descriptor changes are separate. This is a manual source disposition, not clean full-branch composition or runtime qualification of those PRs.

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 and the licence notice of code adapted from upstream (see AGENTS.md)
  • 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 (libc CPU change)
  • AI-assisted: yes

Summary by CodeRabbit

  • Bug Fixes
    • Fixed an issue where reading from a reopened input stream could return a previously buffered character instead of the new file’s contents. Reopened streams now correctly reflect the replacement file after being purged, preventing stale input from appearing in subsequent reads.
<!-- greyfinch-sync:boykopovar/AnyPS5:issue:5512042759 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/27494). Originally posted by [JJenkx](https://github.com/JJenkx) on 2026-11-26T14:41:28Z. --- ### What After `ungetc` on a stream, `freopen` replaces the native handle but leaves the wrapper's narrow pushback bookkeeping live. A subsequent `fpurge` can restore a byte from the old file into the reopened stream. Clear that bookkeeping in `FileStream::Adopt`, alongside the other per-handle resets. The regression reopens a file containing `new` and requires `fpurge` followed by `fread` to return those three bytes. This follows the reset of the old pushback buffer in [FreeBSD 11.4 freopen](https://github.com/freebsd/freebsd-src/blob/8c639945a44588f6cfdddfb057bb5aad88735da0/lib/libc/stdio/freopen.c#L173-L175); no reference source code is copied. This fixes main independently of the close-all/alias work in #11831. ### Tested 2026-11-26, Linux 7.3.5-arch1-1 x86-64, GCC 16.2.1, Release. Base `af6a58c4d30bbc8901b5855081002cd965254b7c`; head `2b1eec1ddd570840d07439af97d489703e0391a0`. Fresh independent source/build with 49 verified submodules and newly built raw libc/kernel providers. - **14/14 focused CPU CTests pass**, zero skips: stream purge, file streams/status, five standard-stream modes, fwide, wide-stream scanning, formatting, host-standard reopen and both registered host-log reopen cases. - The **same final regression executable** (SHA-256 `51455729e9b009f4e22968fcdc6a10b0928480fd48f219d0cad145dae4d68d54`) passes with the fixed provider and aborts at `reopen discards the previous stream's pushback` with the preserved main provider. Main production source is unchanged in that control; only the identical fixture was added. Actual provider initialization paths are checked with `LD_DEBUG`; `ldd -r` is clean. - Conventions, debt-fold and whitespace checks pass. No Windows/macOS, patched SDK, title/GPU, console, performance or full-suite result is claimed. [Patch, fixture, build/test logs, JUnit, loader records and hashes](https://github.com/JJenkx/AnyPS5/releases/download/linux-validation-20261122/libc-reopen-pushback-2b1eec1-linux-20261126.tar.gz), SHA-256 `7e86831b25bee09b61673d8ddd1566a84c0614cd4d30024ccd29a7a36fa75a75`; anonymous download and all **33 payload hashes** verified. Before implementation, the export lookup found #11831 plus two library-split PRs for `freopen`, and #11831 for `fpurge`. The full branch scan examined 2,407 open PRs but failed closed on seven conflicting branches. I manually compared their shared `FileStream.hpp` patches and `Adopt` bodies at the pinned heads in the archive: #7447, #11831, #12682, #12688, #12700, #12965 and #24406. None contains this newer narrow-pushback bookkeeping or its reset; their archive, lifetime, descriptor-locking and bad-descriptor changes are separate. This is a manual source disposition, not clean full-branch composition or runtime qualification of those PRs. <!-- 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) and the licence notice of code adapted from upstream (see AGENTS.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: <!-- #PR, or none --> none - [x] Fixes: <!-- issue it closes as Fixes #N, or none --> none - [x] Unblocks: <!-- title ID and Game bring-up discussion, e.g. PPSA01325 #3232, or none --> none - [x] GPUs tested: <!-- for AGC, shader recompiler or VideoOut changes: vendor, model, driver and OS of each GPU the GPU tests ran on; otherwise n/a --> n/a (libc CPU change) - [x] AI-assisted: <!-- yes / no --> yes <!-- This is an auto-generated comment: release notes by coderabbit.ai --> <!-- coderabbit-ai-generated:v1 --> ## Summary by CodeRabbit * **Bug Fixes** * Fixed an issue where reading from a reopened input stream could return a previously buffered character instead of the new file’s contents. Reopened streams now correctly reflect the replacement file after being purged, preventing stale input from appearing in subsequent reads. <!-- end of auto-generated comment: release notes by coderabbit.ai -->
MarcZX10 commented 2026-11-26 14:52:45 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-11-26T14:52:45Z.

Review: APPROVED


Reviewed — looks good

The reviewed change resets wrapper pushback bookkeeping when a stream adopts a replacement handle, and the focused regression covers reopening, purging, and reading the replacement contents.

What I ran

Read the prepared diff and both changed files at head 2b1eec1ddd570840d07439af97d489703e0391a0 using repo_read.py. No build or tests were run during this review; test results in the PR description are author reported.

Results

Check Result Note
Diff and changed source review passed Both changed files read
Tests during review not run Focused results are author reported

Findings

Blocking

None.

Non-blocking

None.

Not verified

No tests were run during this review. The PR description reports focused Linux CTest and regression-control results, but those were not independently verified here. No other changed files were excluded; both files listed in the manifest as reviewed were read.

To get this to a good state

Nothing is required.

<!-- greyfinch-sync:boykopovar/AnyPS5:review:6076216534 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/27494#pullrequestreview-6076216534). Originally posted by [MarcZX10](https://github.com/MarcZX10) on 2026-11-26T14:52:45Z. **Review:** APPROVED --- # Reviewed — looks good The reviewed change resets wrapper pushback bookkeeping when a stream adopts a replacement handle, and the focused regression covers reopening, purging, and reading the replacement contents. ## What I ran Read the prepared diff and both changed files at head `2b1eec1ddd570840d07439af97d489703e0391a0` using `repo_read.py`. No build or tests were run during this review; test results in the PR description are author reported. ## Results | Check | Result | Note | |---|---|---| | Diff and changed source review | passed | Both changed files read | | Tests during review | not run | Focused results are author reported | ## Findings ### Blocking None. ### Non-blocking None. ## Not verified No tests were run during this review. The PR description reports focused Linux CTest and regression-control results, but those were not independently verified here. No other changed files were excluded; both files listed in the manifest as reviewed were read. ## To get this to a good state Nothing is required.
ligmaSec commented 2026-12-03 14:12:26 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by ligmaSec on 2026-12-03T14:12:26Z.

Review: COMMENTED


Built and ran guest_stream_purge on Linux with this head integrated. The suite passed. The added test pushes a byte back on the old stream, reopens it on a second file, and checks that purging leaves only the second file's bytes. FileStream::Adopt now clears the pushback state alongside its other per-handle resets.

AI-assisted: yes.

<!-- greyfinch-sync:boykopovar/AnyPS5:review:6171447356 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/27494#pullrequestreview-6171447356). Originally posted by [ligmaSec](https://github.com/ligmaSec) on 2026-12-03T14:12:26Z. **Review:** COMMENTED --- Built and ran `guest_stream_purge` on Linux with this head integrated. The suite passed. The added test pushes a byte back on the old stream, reopens it on a second file, and checks that purging leaves only the second file's bytes. `FileStream::Adopt` now clears the pushback state alongside its other per-handle resets. AI-assisted: yes.
This pull request cannot be merged automatically due to conflicts.
Merge manually to resolve the conflicts.
View command line instructions

Checkout

From your project repository, check out a new branch and test the changes.
git fetch -u origin 'upstream-pr/27494:upstream-pr/27494'
git switch 'upstream-pr/27494'

Merge

Merge the changes and update on Forgejo.

Warning: The "Autodetect manual merge" setting is not enabled for this repository, you will have to mark this pull request as manually merged afterwards.

git switch 'main'
git merge --no-ff 'upstream-pr/27494'
git switch 'upstream-pr/27494'
git rebase 'main'
git switch 'main'
git merge --ff-only 'upstream-pr/27494'
git switch 'upstream-pr/27494'
git rebase 'main'
git switch 'main'
git merge --no-ff 'upstream-pr/27494'
git switch 'main'
git merge --squash 'upstream-pr/27494'
git switch 'main'
git merge --ff-only 'upstream-pr/27494'
git switch 'main'
git merge 'upstream-pr/27494'
git push origin 'main'
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!27494
No description provided.