feat(libc): add the wide stream scanf family #8479
Loading…
Reference in a new issue
No description provided.
Delete branch "upstream-pr/8479"
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 hypermoist on 2026-10-20T13:25:43Z.
What
Adds the eight wide stream
scanfexports, which upstream does not have yet:fwscanfa6CYO8YOzfwfwscanf_sBo5wtXSj4kcvfwscanfWF4fBmip+38vfwscanf_sWvm90I-TGl0wscanfD8JBAR3RiZQwscanf_sRV7X3FrWfTIvwscanfQNwdOK7HfJUvwscanf_sYgZ6qvFH3QIIn the console's
libSceLibcInternal.sprx, each of them locks the stream and calls the same scanner asswscanf, passing a callback that reads withfgetwcand pushes back withungetwc. The_sforms set the secure flag and returnEOFfor a null stream or format. Thewscanfforms usestdin.So
ScanningWide.cppnow takes its input from a smallWideInputsource, which reads either the existing buffer or a guestFileStream:%ncounts the characters consumed.ungetcis used becauseungetwcupstream only takes single bytes.vfwscanfandvfwscanf_s(and the forms that call them) hold the stream lock withflockfilefor the whole call, as the console does, so another thread's reads cannot interleave between fields. A closed stream is not locked.The buffer path behaves as before: consuming a field that then fails has no visible effect there. A new entry under Functional in
libc.mdrecords the push-back limit on Windows and the null checks.Tested
Linux (CachyOS, GCC, Release).
é, newlines) across 26 formats againstvfwscanfon a temporary file, checking the return value, stored bytes,%nand the text left in the stream. What a field consumes and what goes back to the stream matched in every case.swscanfalready shares upstream, and the buffer path has the same ones. The console converts the mantissa of an incomplete exponent (1e+), stores a short%cfield, and returns 0 rather thanEOFwhen a literal meets the end of input. Those fixes touch theswscanfdebt line that #8306 is changing, so they will follow in a separate PR.guest_wide_stream_scancovers consumption and push-back (including a non-ASCII lookahead),%ln, the sized forms, null streams andstdin. It passes, along withguest_wide_conversion,guest_stdio_variadic,guest_wide_stringsandguest_scan_count.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
https://claude.ai/code/session_01RbLgECLQ2bpDM7oe2urGZK
Imported from boykopovar/AnyPS5. Originally posted by github-actions[bot] on 2026-10-20T13:26:25Z.
Progress report (
c8301bb→ad7d2a1)📈 System libraries: 62.87% (+0.04%, +8 implemented functions)
✅ 8 implemented
fwscanf_nid_postfixfwscanf_s_nid_postfixvfwscanf_nid_postfixvfwscanf_s_nid_postfixvwscanf_nid_postfixvwscanf_s_nid_postfixwscanf_nid_postfixwscanf_s_nid_postfixImported from boykopovar/AnyPS5. Originally posted by github-actions[bot] on 2026-10-20T13:47:06Z.
Some comparisons could not be completed because a pull request conflicts with the base branch. Review these shared files manually before concluding that there is no overlap.
core/libs/prx/libc/CMakeLists.txtdocs/dev/debt/libc.mdcore/libs/prx/libc/CMakeLists.txtcore/libs/prx/libc/CMakeLists.txtdocs/dev/debt/libc.mdcore/libs/prx/libc/CMakeLists.txtdocs/dev/debt/libc.mdcore/libs/prx/libc/CMakeLists.txtdocs/dev/debt/libc.mdcore/libs/prx/libc/src/ScanningWide.cppdocs/dev/debt/libc.mdcore/libs/prx/libc/CMakeLists.txtdocs/dev/debt/libc.mdcore/libs/prx/libc/CMakeLists.txtdocs/dev/debt/libc.mdcore/libs/prx/libc/CMakeLists.txtdocs/dev/debt/libc.mdcore/libs/prx/libc/CMakeLists.txtdocs/dev/debt/libc.mdcore/libs/prx/libc/CMakeLists.txtdocs/dev/debt/libc.mddocs/dev/debt/libc.mddocs/dev/debt/libc.mdImported from boykopovar/AnyPS5. Originally posted by nikowolf123 on 2026-10-20T19:04:47Z.
The wide stream scan does not hold the stream lock
vfwscanf_nid_postfixandvfwscanf_s_nid_postfixrunScan()andRelease()without the stream lock. EveryPeek()is a separatefgetwc_nid_postfixcall (L149), and the lookahead goes back through separateungetccalls (L185). Each of these host calls takes and releases theFILElock by itself. Guest threads are host threads, so another thread can read the same stream in the middle of a scan.For example, the stream holds
12 34, thread A callsfwscanf(f, L"%d", &x)and thread B callsfgetwc(f):1and2, B gets the space and A's field goes on with3and4. A returns 1 withx == 1234, a number that is not in the input.ungetcinRelease(), B gets3and the space is then pushed back in front of4, so the stream is reordered.With the lock held for the whole call, the only possible results are those of the two calls running one after the other: either B gets
1and A gets 2, or A gets 12 and B gets the space. Single-threaded use is not affected.The PR description says that on the console each of these functions "locks the stream and calls the same scanner as
swscanf". FreeBSD'svfwscanf_ldoes the same, wrapping__vfwscanfinFLOCKFILE_CANCELSAFE/FUNLOCKFILE_CANCELSAFE(vfwscanf.c#L431-L434, the revisionlibc.mdalready cites). In this repo,putwalready wraps its multi-byte write in a guard built onflockfile_nid_postfix(BinaryWordIo.cpp#L19-L25).I would add the same kind of guard to both entry points, after the null checks:
The
IsOpen()check keeps the current result for a closed stream (errno9 andEOF), becauseflockfile_nid_postfixgoes throughGetStreamHandle, which throws on a closed stream. The destructor also releases the lock whenFail()throws. The host stream locks are recursive, so thefgetcandungetccalls inside still work. If the lock is left out on purpose, please add a sentence to the newlibc.mdentry saying that the scan is not atomic against other threads using the same stream, while the console's is.Imported from boykopovar/AnyPS5. Originally posted by hypermoist on 2026-10-20T22:47:17Z.
Thanks, that's right: the console locks the stream around the whole scan, and this didn't. Fixed in the latest push:
vfwscanfandvfwscanf_s(and sofwscanf,wscanfand the_sforms) now hold aLockedStreamguard after the null checks. It skips a closed stream, so that case still givesEOFwitherrno9, and the destructor unlocks whenFail()throws.CheckLockinginGuestWideStreamScan.cppholdsflockfileon the stream, starts a thread that callsfwscanf, checks the thread is still blocked after 50 ms, then unlocks and checks it reads12. Without the guard the scan finishes while the lock is held, and the test aborts.guest_wide_stream_scanpassed 5 of 5 runs;guest_wide_conversionandguest_stdio_variadicalso pass.Imported from boykopovar/AnyPS5. Originally posted by hypermoist on 2026-10-20T22:47:55Z.
Correction to my last comment: the
CheckLockingtest did not prove anything. I ran it with the guard removed and it still passed, because each innerfgetctakes the hostFILElock itself, so the scanning thread blocks on the held lock either way. I've removed the test in the latest push rather than keep a check that can't fail. The guard itself stays. The interleavings you describe need a second thread to read between two of the scan's own calls, and I don't see a way to force that deterministically in a unit test.