feat(libc): track stream orientation and implement fwide #10324
Loading…
Reference in a new issue
No description provided.
Delete branch "upstream-pr/10324"
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 MarrasFederico on 2026-10-23T17:51:49Z.
What
fwide(NIDw6Aq68dFoP4) is exported by the PS5'slibSceLibcInternal(3.20 inventory) but AnyPS5's libc did not provide it, so a title importing it could not resolve the symbol.It now follows FreeBSD 11.4
fwide.c. Each stream keeps an orientation: 0 (none), -1 (byte) or 1 (wide).fwide(stream, 0)returns it, and a nonzero mode orients a stream that has none yet, wide for a positive mode and byte for a negative one, and never changes an established orientation.The I/O calls set it the way FreeBSD's
ORIENT(fp, o)does:ORIENTdoes;fgetsorients before it checks its size, as in FreeBSD. That coversfgetc/getc,getc_unlocked,ungetc,fgets,fread,fputc/putc,putc_unlocked,fputs,puts,fwrite,vfprintf, theprintffamily,fscanf/vfscanfandperroron stderr. The wrappers that forward to them (getchar,putchar,__srget,__swbuf,getw,putw,fprintf,scanf) inherit it.ungetwcandfgetwsalso before their argument checks:fgetwc,ungetwc,fgetws,fputwc,fputws,vfwprintf,wprintfandvfwscanf/vfwscanf_s, plus their wrappers.freadorfwrite, which return beforeORIENTin FreeBSD, or from metadata calls such asfseek,fflushandfileno.freopenclears the orientation, as FreeBSD'sfreopen.cdoes, and so does closing a stream.The
printfandwprintffamilies,perrorandputworient the stream as C11 7.21.2p4 requires. FreeBSD 11.4 differs here: itsvfprintf.c,vfwprintf.c,perror.candputw.chave noORIENT, andvfwprintfcan even orient a fresh stream to bytes through__swbuf. As in FreeBSD, the orientation only answersfwide: a byte call on a wide stream still runs. The debt entry records the C11 choice and that nothing was measured on a console.Tested
Ubuntu 26.04.1, kernel 7.0.0-38, GCC 15.2.0, Release, full build with
BUILD_TESTING=ON. Windows was not compiled here; wachi35676's Windows run is below. No title was run.guest_fwidetest, which loads the built libc and covers:fseek,fflush,filenoand zero-lengthfread/fwriteleave it so;fwide(+1)orients it wide and a laterfwide(-1)keeps it wide;fputcandputc_unlockedorient a stream to bytes, and a byte read keeps it so;fputwcandfgetwcorient a stream wide,fgetwceven when it hits end of file;freopenclears the orientation;ungetwc(WEOF),fgetswith size 0 andfgetwswith size 0 still orient the stream;vfscanfandvfscanf_sorient the stream to bytes at end of file,vfprintf_sorients it to bytes andvfwprintf_sto wide.main, wherefwide_nid_postfixdoes not exist.fputc,putc_unlocked,ungetwc,fgetsorfgetws, keeping it acrossfreopen, lettingfwidereplace an established orientation, and orienting before the zero-length check infread. They were run before the latest rebase.main(rebased,a12f8fc4d3), 1794 of the 1799 tests outside the AGC, shader, VideoOut and Vulkan groups pass (ctest -E 'agc|shader|video_out|vulkan'), includingpublic_headersand the stream testsguest_wide_formatting,guest_wide_conversion,guest_wide_stream_scan,guest_standard_streams*,file_streamsandguest_formatting. The other five are network tests that need an IPv6 loopback this host lacks; they pass in a network namespace with one. Removing the orientation call from the newvfprintf_s,vfscanf_sorvfwprintf_spaths failsguest_fwide.5e32fdc2a5merged ontomain, WinLibs GCC 15.2.0, plus WSL2 Ubuntu 24.04 with GCC 13.3):guest_fwidebuilds with MinGW and passes 3 of 3 on both hosts, as do eight neighbouring stream tests. With the touched libc files frommain,guest_fwidefails on both and the other eight still pass.tools/check_conventions.py --base origin/mainpasses.tools/pr_overlap.pyreports two conflicts. #10139 adds itsfpurgeentry at the same place indocs/dev/debt/libc.md, so whichever lands second keeps both entries. #10405 changes the samefgetwcandFileStreamlines; its author proposed that this PR lands first and #10405 rebases onto it. #8982 is merged, sovfscanfnow orients before scanning, asfscanfdoes. The orientation calls are placed so that they do not touch the lines #4627, #7447 and #8982 change. The 19 PRs it could not compare (#3965, #5021, #5303, #6044, #7081, #7201, #7648, #7715, #8150, #8306, #8965, #9133, #9408, #9447, #9526, #9527, #9552, #9669, #9929) were checked by hand: none addsfwideor stream orientation.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 harisibrar1 on 2026-10-24T01:12:39Z.
Review: COMMENTED
Checked against main
11d6efa06cand FreeBSD 11.4lib/libc/stdio(fwide.c, local.h, vfprintf.c, vfwprintf.c, wbuf.c, perror.c, putw.c, ungetwc.c, fgets.c, fgetws.c, freopen.c):Orientation()matchesfwide.c's "only set if 0" rule, and the reset inReopenmatchesfreopen.cL188.ORIENT", but 11.4'svfprintf.candvfwprintf.chave noORIENT,perror.cwrites with_writevand doesn't touch stderr's orientation, andputw.cgoes straight to__sfvwrite.vfwprintfwrites through__sputc, whose__swbufrunsORIENT(fp, -1)(wbuf.c L69). So, reading the source,fprintf(fp, "x"); fwide(fp, 0)would give 0 there, andfwprintfnever gives 1. I couldn't run this. Following C11 7.21.2p4 seems the better choice, but could the entry say that the formatted functions,perrorandputwfollow C11 rather than FreeBSD?ungetwc(L80),fgets(L60) andfgetws(L57) orient before their argument checks. Hereungetwc(WEOF, fp),fgets(buf, 0, fp)andfgetws(buf, 0, fp)return early and leave the stream unoriented:fwide(fp, 0)gives 0 where FreeBSD gives 1, -1 and 1. Moving the call up would match.vfscanf_nid_postfixorients after the scan, whilefscanf_nid_postfixorients before it (FreeBSDvfscanf.cL482 orients before).Reviewed with AI assistance (Claude Code); the checks above were run locally on my machine.
Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-10-24T21:07:30Z.
Review: CHANGES_REQUESTED
Please rebase this branch onto the current
main, as required by CONTRIBUTING.md. The current main is 6,761 commits beyond the branch base, and the full test run in the PR description was run against the older base; the hosted product-test jobs are skipped for this change.One remaining behavior question from the earlier review:
vfscanf_nid_postfixrecords byte orientation only after scanning returns, whilefscanf_nid_postfixdoes it before scanning. An unoriented stream should acquire byte orientation when the byte I/O function is applied, including when scanning exits through an unsupported-conversion exception. Could you move the call before the scan and add a focused regression test? If #8982 is the only reason the hunk cannot move cleanly, please note the dependency and preserve the orientation behavior when rebasing.Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-10-29T23:07:04Z.
Review: CHANGES_REQUESTED
Re-checked — new head, changes requested
The requested
fwidebehavior and Windows test evidence are now present on head5e32fdc2a5ccd4050a6a2e7f46f94e31966f8cca, but this head conflicts with currentmainin six files.What I ran
Reviewed head
5e32fdc2a5ccd4050a6a2e7f46f94e31966f8ccaagainst PR baseacbfd7c7fe3fd5a10039719051e1d3919954d65e; currentmainisdc38224141e0b6096007b876b4c42fc9144d11b8. The merge preview is conflicted. Local checks ran in Linux; no compiler or build was invoked.Results
FileStream.hpp,FileAndHeap.cpp,Formatting.cpp,FormattingWide.cpp,StandardStreams.cppandlibc.mdguest_fwideandpublic_headerspass, and the eight neighboring stream tests pass 3/3 on Windows and Linux; not independently run hereUnedited local check output
Findings
Blocking
main, so the orientation changes cannot yet be checked in the current integration context. Please resolve the six conflicts, preserving both the current stream changes andfwidebehavior, then rerun the focusedguest_fwide,public_headers, and neighboring stream tests on the rebased head.Non-blocking
The earlier review concerns about orienting before the
vfscanfscan and removing the invalid%lsassertion appear addressed in the current source and tests. The reported test runs are author-provided, not local runs.Sources
vfscanforientation change at this headfwidetests at this headNot verified
I did not build or run tests locally, or test console behavior. Hosted product Linux and Windows jobs skipped; full test results are author-reported.
To get this to a good state
Please resolve the six conflicts against
dc38224141e0b6096007b876b4c42fc9144d11b8and rerun the named focused tests on the resulting head. I will rerun the merge preview, diff check and conventions on that exact update.