fix(libc): fpurge keeps the logical read position and the pushed-back bytes as the console does #22143
Loading…
Reference in a new issue
No description provided.
Delete branch "upstream-pr/22143"
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 xXPBSXx on 2026-11-17T06:26:43Z.
What
On the console,
fpurgedoes not move a stream's read position and does not drop a pushed-back byte. A PS5 payload process on firmware 11.60 (libSceLibcInternal.sprx) read one byte of a 100-byte file andftellreturned 1; afterfpurgeit still returned 1 and the next byte read was the second one (case a, line 3 of the raw output). Afterungetc('Z')andfpurge, the nextfgetcreturnedZ(case c, line 4). End-of-file status was kept andferrorstayed 0 (case d, line 5), and output written withfputsand not flushed was discarded: the file had 0 bytes afterfclose(case b, line 6). Thefpurgeof this library followed FreeBSD 11.4 and discarded buffered input and the pushback without writing or seeking: after the purge, the next read started at the host's position behind the read-ahead data, and the pushed-back byte was gone. Thefpurgeentry indocs/dev/debt/libc.mdsaid that the console behavior is unverified.StreamPurge.cppnow notes the logical position of a regular file (ftell) before the purge and, when a pushed-back byte is still recorded, its position and the byte; after the purge it moves the descriptor back to that position (_lseeki64on Windows,fseekoelsewhere) and pushes the recorded byte back.ungetcinStandardStreams.cpprecords the byte.FileStream.hppgets three private fields (the recorded bytes, whether they are still valid and a cached "regular file" answer) and the accessors; any other access that goes through the stream status (SyncStatus,SyncUnlockedStatus) marks the recorded pushback invalid. Pipes, terminals and other streams that cannot be repositioned are purged as before. The debt fragment states the console result and what is still open.Limits of this change, all stated in the pull request and not hidden: (1) a pushed-back byte survives
fpurgeonly if no other stream call,feofandferrorincluded, happens between theungetcand thefpurge; (2) bytes pushed back bystd::ungetccalls outsideStandardStreams.cppare not recorded (Formatting.cpplines 303 and 314, the scanf pushback), while the wide scanner'sungetc_nid_postfixcall (ScanningWide.cppline 185) is; (3) on Linux the repositioning usesfseeko, after which glibc reads the buffer again, so the descriptor's physical position differs from the logical one (the test therefore checksftell); (4) macOS was not built. Not measured on the console: more than one pushed-back byte, a pipe or terminal, and text mode.Tested
Measured on a PS5 with firmware 11.60 (
kernel_get_fw_version0x11600005), from a homebrew payload process (ps5-payload-sdk v0.45, elfldr) that loadslibSceLibcInternal.sprxand takesfopen,fclose,fgetc,fputs,ftell,ungetc,feof,ferror,fwriteandfpurgewithdlsym(line 2 shows that all ten were found), on 2026-11-16 at 23:37 UTC, on a temporary file in the probe's own directory on the data partition that was removed at the end. A title process may differ. The cases are 0 to b, lines 2 to 6 of the raw output in the first comment.fpurge:ftell1 and the next byte is the secondftell1, nextB(line 3)guest_stream_purge, passesungetc,fpurge: the pushed-back byte comes backZreturned (line 4)guest_stream_purge, passesfreadread it)guest_stream_purge, passesfpurge, no errorfeof1 before and after,ferror0 (line 5)On
main, with the test commit alone, one test,guest_stream_purge, fails; it contains the first three checks and does not fail row by row.Windows 10 Pro 10.0.19045 and Linux (WSL), one build per commit, on base
upstream/main140d6a4546b, on 2026-11-17 between 03:51 and 03:56 UTC. Windows: the test commit alone built withninjareturning 0 andctestreturned 8, a failing test (the Linux run shows it isguest_stream_purge); a head with the same code as the head of this pull request (the documentation commit has changed only the text of the debt fragment since) built withninjareturning 0 andctestreturned 0. Linux: on commits whose trees equal the three commits of this pull request,ninjareturned 0 for all three, the test commit alone abortedguest_stream_purge(1 of 9 tests failed), and the fix commit and the documentation commit passed all 9 tests (guest_stream_purge,file_streams,file_position,guest_binary_wordand the fiveguest_standard_streams*tests) in three runs each;tools/check_conventions.pypassed for each commit.Cost of
ungetc: a local benchmark by the author (not in the commits) of 2,000,000 pairs offgetcandungetcon a 26-byte file that fits the buffer, three runs per state. Windows: 205.2, 221.3 and 214.7 ns per pair onmain, 255.6, 230.9 and 229.9 ns with this change (means 213.7 and 238.8 ns, about +12 %). Linux: 34.8, 35.2 and 34.3 ns onmain, 51.8, 42.9 and 43.8 ns with this change (means 34.8 and 46.2 ns, about +11 ns). An earlier version of the change that read the position at everyungetccost about 40 times as much on Windows and was dropped; the position is now read atfpurge, andungetconly records the byte and, once per stream, whether the file is a regular file.tools/check_conventions.py --base upstream/mainpasses with no error and no warning (the changed entry is 888 characters, counted without the leading '- ', up from 520). The change is 5 files, +122/-6, with no new comments in code. Not tested: macOS, the CI runner, a title process, other firmware, the cases listed above as not measured, and the pushback ofstd::ungetccalls outsideStandardStreams.cpp.Overlap: the other open pull requests that change one of the four files of this branch (
core/libs/prx/libc/include/FileStream.hpp,core/libs/prx/libc/src/StandardStreams.cpp,core/libs/prx/libc/src/StreamPurge.cpp,core/libs/tests/GuestStreamPurge.cpp) or the debt fragment, found by reading the changed files of all 1650 open pull requests at about 05:06 UTC, by reading what they change indocs/dev/debt/libc.mdand in the fragments underdocs/dev/debt/changes/forfpurgeandungetc, and by searching their titles and branch names for the same names (no hit), were test-merged on 2026-11-17 at about 05:12 UTC againstmaina72186d3cfdtogether with this head; the branch is based on140d6a4546b, an ancestor of it, merges intomainwithout a conflict, andtools/debt_fold.py --checkapplies its fragment without an error (checked without the fragmentlibressl3-20171-inventory.md, which already conflicts withfeat-libressl3-evp-pkey-new-mac-key-v2.mdonmainand is not part of this change). Eleven pull requests list one of the files. Nine of them are unaffected by this change: #12965, #12700, #12688, #12682, #12315, #8150 and #7447 already conflict withmainand conflict in the same files with this head merged first, and #22047 and #21721 merge cleanly withmainand with this head. Two conflict only with this change: #17459 (fix(libc): grow the pushback room of Windows streams for repeated ungetc) changes 79 lines ofFileStream.hppandStandardStreams.cpparound the same pushback code, and #11831 (feat(libc): implement fcloseall with tracked stream lifetimes) changesFileStream.hpp; both merge cleanly withmainand conflict withmainplus this head inFileStream.hpp(#17459 also inStandardStreams.cpp). Whichever lands second needs a rebase. #17459 matters beyond the text: it keeps several pushed-back bytes on Windows, while this change remembers only the last pushed-back byte forfpurge, so after both the behaviour offpurgewith more than one pushed-back byte on Windows is not covered by either. The merged result was not built or run. No open pull request changes thefpurgedebt entry or a line that namesfpurge.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)Imported from boykopovar/AnyPS5. Originally posted by PrivacyOnion on 2026-11-17T06:38:22Z.
Review: APPROVED
Verified on Linux (Ubuntu 24.04, GCC 13.3, Ninja, Release) against
06172539a6:tools/check_conventions.pypassed.guest_stream_purge_testsbuilt cleanly.guest_stream_purge):core/libs/prx/libc/include/FileStream.hpp,core/libs/prx/libc/src/StandardStreams.cpp,core/libs/prx/libc/src/StreamPurge.cppreverted tomainand the new tests kept, the suite fails: Restoring the change makes it pass again.Looks Good To Me
Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-11-20T20:34:15Z.
Review: COMMENTED
Reviewed — no blocking findings
Nothing blocks this change; the points below are optional.
What I ran
Reviewed head
06172539a649fc4798ea4e6c49b60a899ff1dc34using the prepared diff andrepo_read.pyfor changed files and targeted context. No tests or builds were run during this review.Results
Findings
Blocking
None.
Non-blocking
fpurge_nid_postfixignores errors fromSeekDescriptorand subsequentungetccalls, then reports success. A failed reposition can leave the stream at the wrong position while returning success. Consider propagating those failures and preserving appropriate stream status. (core/libs/prx/libc/src/StreamPurge.cpp:70-72)Not verified
FileStream.hpp,StandardStreams.cpp,StreamPurge.cpp,GuestStreamPurge.cpp, andfix-libc-fpurge-keep-position.md.files.jsonwas unavailable through the reader, so no additional excluded-file inventory was verified.To get this to a good state
Nothing is required. Consider handling reposition and pushback failures in
fpurge_nid_postfix.Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-11-25T03:07:59Z.
Review: APPROVED
Reviewed — looks good
Nothing blocks this change; the reviewed code supports merging, with the documented scope limits for pushback tracking and unmeasured platforms and stream types.
What I ran
Reviewed head
ee656502bf9d5e71b3c91e9f15b5bf799e7c3fa2usingrepo_read.pyfor the prepared diff and targeted source. No commands, builds, or tests were run in this session; test results below are author-reported.Results
Findings
Blocking
None.
Non-blocking
ungetcwrapper and to the case where no intervening stream operation clears the live marker. The description documents these limits; multiple pushed-back bytes, text mode, and console behavior beyond the reported cases remain unmeasured.Not verified
files.jsonwas unavailable. No other files were read.To get this to a good state
Nothing is required. Optional: extend coverage for multiple pushbacks and additional console stream types when measurements are available.