libc: align fgetwc conversion/state with mbrtowc and verify fgetws edge cases #8897
Loading…
Reference in a new issue
No description provided.
Delete branch "%!s()"
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 UnhealthyKraken on 2026-10-21T05:00:26Z.
Observed behavior
In a standalone Windows x86-64 Release probe,
fgetwcreturns0x1F600for the UTF-8 bytesF0 9F 98 80, whilefgetwson the same bytes stores a single 16-bit0xF600followed by NUL. The source directly narrows the 32-bit result tochar16_tat FormattingWide.cpp.This reports the current implementation's conversion loss, not a measured PS5 expected result. We need to establish the console's
fgetwc/fgetwscontract for supplementary characters before choosing a fix: a UTF-16 surrogate pair, rejection, or another console-specific result must not be guessed.Environment and validation
Windows x86-64, MinGW-w64 GCC15.2, Release. Library built in an isolated checkout on
aa408677b0e794cf59f314ebcf9a716ede638295;StandardStreams.cpp,FormattingWide.cppandFileStream.hppare unchanged on inspected upstream mainc68a27cc4. Only own temporary streams used; no game, firmware binary or proprietary capture.The existing
guest_standard_streamstest passes. The six-case probe below records:The byte sequences are named descriptively relative to Unicode scalar-value UTF-8. Acceptance alone is not proof that they differ from PS5: the current debt explicitly records legacy conversion behavior in other multibyte APIs. Their behavior needs measurement separately. The important reproducible observation here is the 32-to-16-bit loss between these two stream functions.
Source-only reproducer
Compile from the checkout root with its MinGW toolchain,
-std=c++20 -O2 -Icore/libs, and link the built unpatchedlibc.prx. Put the unpatched library directory and toolchain runtime directory on PATH. For example, save aswork/wide-stream-probe.cppand use:Scope / overlap
Merged #5816 introduced
fgetws; open #8479 adds stream scanning usingfgetwcand related push-back. This report does not duplicate those exports or propose changing their branch. Current all-state searches forfgetwc,fgetws+surrogate and wide+supplementary found no focused report for this case. No code change proposed until the console contract is established. No Linux/macOS execution yet.AI-assisted investigation and reproducer; I ran the Windows probe and existing stream test locally.
Evidence update (2026-10-22)
Contributor disassembly of three bundled builds and the separately attributed system-module measurements are summarized in the follow-up reply. The low-16-bit fgetws store is observed in those bundled libraries; the remaining discrepancy is the host fgetwc conversion/state path, plus size<2 and partial-read-error behavior. The bundled converter tables and malformed/four-byte behavior remain unverified. Open #8855 tracks the separate multibyte implementation work. The original report above is retained as reproduction history, not as proof that the narrowing itself differs from firmware.
Imported from boykopovar/AnyPS5. Originally posted by shravanveeravali-lab on 2026-10-21T14:44:58Z.
I traced the implementation in
FormattingWide.cpp.fgetwc_nid_postfix()returns a 32-bit decoded code point, whilefgetws_nid_postfix()casts it directly tochar16_t, which truncates supplementary characters such as U+1F600.Before proposing a fix, could someone confirm the expected console behavior for supplementary characters in
fgetws? Should the output use a UTF-16 surrogate pair, or does the console follow a different contract? I also noticed the decoder may accept malformed UTF-8 sequences, so I'd like to confirm the intended error behavior before adding regression tests.Imported from boykopovar/AnyPS5. Originally posted by UnhealthyKraken on 2026-10-22T03:16:26Z.
I do not have a measured console contract for supplementary characters or malformed sequences. The reproducible fact in this issue is the current Windows conversion loss:
fgetwcyields0x1f600, thenfgetwsstores0xf600. It does not establish that the console emits a surrogate pair, rejects the character, or follows the same narrowing behavior.The malformed-sequence results are also current-model observations, not confirmed PS5 errors. A fix needs attributable results for output units/bounds, return value, errno and stream consumption, including a buffer boundary within a supplementary character. Until that evidence is available I would keep the encoding question open rather than prescribe a surrogate-pair or strict-decoder change. No new console run is claimed here.
Imported from boykopovar/AnyPS5. Originally posted by JJenkx on 2026-10-22T03:48:51Z.
Data point for the console's
fgetwscontract, read from disassembly (not measured on a console). The source is thelibc.prxthat three titles ship insce_module/: PPSA13934, PPSA07923 and PPSA05916. All three have the same 133-bytefgetws(F81hKe2k2tg). The addresses below are from PPSA13934's copy, wherefgetwsis at 0x6cab0 andfgetwc(bzbQ5zQ2Y3g) is at 0x6c7a0.The 16-bit narrowing is the console's own behaviour.
fgetwscallsfgetwcand stores only the low 16 bits of its result:There is no surrogate pair and no rejection, so on this console
libc.prxa supplementary character thatfgetwcreturns as0x1F600is stored as0xF600, as in this issue's probe. The'\n'test compares all 32 bits of thefgetwcresult.Two differences from
fgetws_nid_postfixon currentmain:fgetwsmainsize < 2cmp $0x2,%esi; jge, elsexor %r14d,%r14d).size == 1, stores a NUL and returns the buffer. Forsize <= 0, also sets the stream error anderrno22.fgetwcreturns -1 after some characters were storedferror: null is returned only when nothing was stored.ferroris set.Not settled here:
fgetwcdecodes fromF0 9F 98 80or from the malformed sequences. I didn't read the 498-bytefgetwc.0xcof the stream's second flag byte are set (testb $0xc,0x1(%r15), then a call with 0).libc.prxbuilds, not a system module.Imported from boykopovar/AnyPS5. Originally posted by JJenkx on 2026-10-22T03:58:19Z.
Follow-up on
fgetwc, from the same three bundledlibc.prxcopies (addresses from PPSA13934's:fgetwc0x6c7a0, 498 bytes; read from disassembly, not measured).The console's
fgetwcreturns one 16-bit unit, zero-extended, and decodes with the same routine asmbrtowc.r8and calls a table-driven converter.mbrtowc(qVHpv0PxouI) calls the same 0x3bbe0, whilembrtoc16takes a different path.cmp $0xfffffffd,%eax; cmovne), so one input can come out as several 16-bit units over several calls, with the state kept in the stream.cmp $0x5,%r14; jafails). With 6 or more, or when the refill gets nothing, it sets the stream's error bit (orb $0x2,0x1(%r13)) and returns -1.movzwl (%rax)).What this means for #8897:
fgetwcnever returns0x1F600. Its result is whatever thembrtowcconversion of the title's locale produces, one 16-bit unit per call.fgetwsloses nothing on the console.main,fgetwcdecodes UTF-8 to a 32-bit value. Per the libc debt entry, this libc'smbrtowckeeps the single-byte C locale, so the two use different conversions.fgetwcwould decode through the same conversion as this libc'smbrtowc, with the state in the stream.Not established:
libc.prx's default locale is single-byte or UTF-8. So the console's exact output forF0 9F 98 80and for the malformed sequences is still open.0xcof the stream's second flag byte are set.Imported from boykopovar/AnyPS5. Originally posted by JJenkx on 2026-10-22T04:25:40Z.
One more data point on the locale question, from the same bundled
libc.prx(PPSA13934). Read from disassembly, not measured.Exported
_Getmbcurmax(U52BlHBvYvE, 9 bytes at 0x3a40) returns a signed byte from initialized data, and that byte is 3 in the file:I found no instruction that writes that byte, and none that takes the address of anything around it; the only reference is this read. As far as a static reading shows, this
libc.prxreportsMB_CUR_MAX3. That is a multibyte default consistent with UTF-8 limited to 16-bit units, not the single-byte C locale.This fits the debt entry's console measurement that the system
libSceLibcInternal's default locale converts UTF-8 (wctombof U+263A givesE2 98 BA).On
main,_Getmbcurmaxreturns___mb_cur_max, and the libc debt entry describes this libc'swctomb/wcrtomb/mbrtowcas single-byte withMB_CUR_MAX1.Still not established:
F0 9F 98 80(refused, or split with -3), overlong forms, and surrogates.MB_CUR_MAXbyte through a computed pointer.Imported from boykopovar/AnyPS5. Originally posted by JJenkx on 2026-10-22T04:28:41Z.
#8855 (hypermoist) settles part of what I left open above, for the firmware's
libSceLibcInternal.sprx(12.00), by calling its exports directly._Getmbcurmaxreturns 3, as in the bundledlibc.prxhere. Four-byte leads (≥ 0xF0) are invalid, and overlong forms and encoded surrogates decode. If the bundledlibc.prx's converter follows the same rules, the console'sfgetwcwould fail onF0 9F 98 80rather than return0x1F600or a surrogate pair. I haven't verified that for the bundled module, whose tables are filled at run time.Imported from boykopovar/AnyPS5. Originally posted by UnhealthyKraken on 2026-10-22T14:20:27Z.
These follow-ups correct the interpretation of my original symptom: the low-16-bit store in
fgetwsis not itself evidence of a firmware bug. Your reading of the three bundled builds places the discrepancy earlier, infgetwcusing a different conversion path from the sharedmbrtowcroutine. I have recorded the source/build and disassembly-only limits, and linked the system-module measurements in open #8855 separately; they do not by themselves establish the bundled module's dynamically initialized tables. I am keeping this issue open for the conversion/state alignment and the independently reported size<2 / partial-read-error differences. Exact handling of four-byte sequences and malformed inputs in those bundled builds remains unverified; no widening or surrogate behavior has been invented.Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-10-22T14:58:18Z.
I opened #9786 for the two separately evidenced
fgetwscases: sizes below two return null without touching the buffer or stream error state, and a read error after partial output returns the null-terminated partial string while preserving the error indicator. The Linux focused standard-stream tests pass 3/3. Thefgetwcconversion/state discrepancy remains open and is not claimed as fixed.