fix(libc): convert multibyte text as UTF-8 like the console's C locale #8855
Loading…
Reference in a new issue
No description provided.
Delete branch "upstream-pr/8855"
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-21T03:25:34Z.
What
The console's C locale converts multibyte text as UTF-8, and this libc used a single-byte C locale. This PR switches the multibyte conversions to UTF-8. The evidence is the decrypted
libSceLibcInternal.sprx(PS5 12.00): its exports were loaded and called directly, and their results compared with this build's. It also agrees with the payload measurement already inlibc.md(U+263A →E2 98 BA).Rules taken from the firmware:
_Getmbcurmaxreturns 3. Four-byte leads (≥ 0xF0) are invalid; overlong forms and encoded surrogates decode.__mb_cur_maxstays 1, like the console's variable.mbsinitreads that count.Per function:
mbrtowc(0x836a0) converts a null source as""with a null destination and the caller's count:n == 0it returns -2 and leaves the state as it was;EILSEQfrom a pending state, which it keeps.mbrlen(0x83660) with a null source returns 0 and resets the state, whatever the count.mbtowc(0x6f7b0) keeps its state per thread and clampsnto 3.mblen(0x6f680) uses a fresh state on each call.mbsrtowcs(0x83720) writesdst[0] = 0on entry.wcstombs(0x89260) fills the capacity even in the middle of a character.wctob(0x89810) truncates its argument to 16 bits.btowc(0x7da90) returnsWEOFfor 0x80 and above. It setsEILSEQwhen the low byte is 0x80–0xBF or 0xF0–0xFF, which cannot start a character, and leaveserrnoalone for lead bytes 0xC0–0xEF andEOF. Found by @JJenkx against titles'libc.prxand confirmed on the firmware export.wcstombs_s(0x893e0) ends without error when a character does not fit a limit below the capacity, as C11 K.3.6.5.2 allows.wcsrtombs_s(0x85fd0) keeps reportingERANGEthere. Both paths were unreachable with a single-byte locale.wcsftimesizes its narrow buffer with_Getmbcurmaxand checks the length before it writes. Characters above 0xFF now pass through: the console copies them verbatim.Tests changed: the
wcstombs_scases inGuestWcsrtombsS.cppandGuestAmprThreads.cppthat expectedEILSEQfor U+0100 now expect61 C4 80, the firmware's result.GuestMultibyteLocale.cppis new.Debt entries: the "Wide-character conversions",
c16rtomb/mbrtoc16andwcsftimeentries inlibc.mdnow describe the UTF-8 locale and the remaining gaps. Forwcsftimethose are:SIZE_MAXcount the console formats as usual, while here it returns 0 withEINVAL.Two entries are left for a follow-up because #7841 inserts a line next to them: the
wcsrtombs_ssentence aboutMB_CUR_MAX1, and #7841's Annex K locale line.Tested
Linux (CachyOS, GCC, Release): full build. All 539
guest_*and Ampr tests pass.Firmware differential (the harness loads the module and calls the addresses above):
mbrtowc: every 1–4-byte input from a fresh state (16.8M cases), 0 mismatches.wcstombswith a null destination: there the console reads past the terminator when the capacity equals the running count, as recorded in the debt entry.wcstombs_sandwcsrtombs_s: 400k random cases, 0 mismatches.tools/check_conventions.pyreports no errors; it warns that thec16rtombentry I edited is over 1000 characters.pr_overlap: no overlap with open pull requests.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 MarcZX10 on 2026-10-21T03:44:10Z.
Code review location:
core/libs/prx/libc/src/Multibyte.cpp; line 144The implementation and summary disagree on this edge case.
mbrtowcturns a nullsourceinto""and then passes the caller’scounttoLocaleToWide; whencount == 0, that helper returnsIncompletebefore reading the empty string or changing state. The PR summary says a null source resets state and returns 0.CheckStatetests a pending state with null source atcount == 1(expectingInvalid) and testsmbrlen(nullptr, 1, state), but notmbrtowc(nullptr, 0, state). Could you reconcile the reported firmware behavior forn == 0and add the exact null-source/zero-count case, including whether pending state is cleared?Imported from boykopovar/AnyPS5. Originally posted by hypermoist on 2026-10-21T03:47:39Z.
Code review location:
core/libs/prx/libc/src/Multibyte.cpp; line 144Thanks, you're right: the summary was wrong and the code was right. I ran the firmware
mbrtowc(0x836a0) andmbrlen(0x83660) with a null source at counts 0, 1 and 5, from the initial state and from states pending after one and after two bytes of a three-byte character. All 18 results (return value, errno and all 16 state bytes) match this branch:mbrtowc(&w, NULL, n, st)mbrtowc(&w, NULL, n, st)*wnot writtenmbrtowc(&w, NULL, n, st)EILSEQ(86)*wnot writtenmbrlen(NULL, n, st)So a null source in
mbrtowcdoes not reset the state; it converts""with the caller's count, as the code does. Onlymbrlenresets.In
1c96d7ca1d,CheckStatenow covers the exact cases:mbrtowc(&value, nullptr, 0, state)returns -2 and leaves a pending state byte for byte;EILSEQ, an untouched*valueand the kept state;mbrlen(nullptr, 0, state)resets;As a negative control I made
mbrtowcreset the state on a null source, as the old summary described, and the test fails on the count-0 pending case. The PR description is corrected. All 539guest_*/Ampr tests pass.Imported from boykopovar/AnyPS5. Originally posted by MaxLikesCode on 2026-10-21T17:38:36Z.
Windows run of head
1c96d7ca1merged ontomaind34da5d69: it builds, andguest_bounds_checkingfails 3 of 3:That test's checks came in with #7841 after this PR's base (
e0e57a327). Lines 132 and 147 ofcore/libs/tests/GuestBoundsChecking.cppexpectwctomb_sandwcrtomb_sto refuse U+263A with 86 (EILSEQ) in the C locale. With this PR both convert it, which is what this PR and the payload measurement inlibc.mdsay the console does:With those two checks replaced by the prints above, the rest of
guest_bounds_checkingpasses, so only those two expectations need to change. The other 946 non-GPU tests thattools/select_tests.pyselects for this PR pass.mainalone passesguest_bounds_checking.Windows 11 Pro 26200, WinLibs GCC 15.2.0 r7, Release,
BUILD_TESTING=ON.AI-assisted: yes (Claude Code)
Imported from boykopovar/AnyPS5. Originally posted by oneandonlydean on 2026-10-21T23:55:40Z.
@hypermoist, after #7841 merged, this PR's checks fail
guest_bounds_checkingon both Linux and Windows:The test is from #7841 and was written against the single-byte locale.
core/libs/tests/GuestBoundsChecking.cpp:133expectswctomb_s(..., 0x263a)to returnEILSEQwith status -1, and:147expects the same fromwcrtomb_s. With this PR's UTF-8 conversions, U+263A converts toE2 98 BA, and no 16-bit value is unrepresentable any more. So both assertions need the firmware's result: return 0, status/result 3, bytesE2 98 BA. Those are the only two locale-dependent cases in that file. The rest use ASCII or check arguments.There is also a real bug where the two PRs meet.
wctomb_sandwcrtomb_sincore/libs/prx/libc/src/BoundsChecking.cpp(lines 130 and 157) stage through a scratch buffer only when___mb_cur_max()is larger than the capacity. This PR keeps__mb_cur_maxat 1, as the console does, while characters are now up to 3 bytes. So any capacity of 1 or more skips the scratch buffer, andwctomb_s(&status, buffer, 2, 0x263a)writes 3 bytes into a 2-byte buffer. Comparing against_Getmbcurmax()(3) in those two places restores the staging path and theERANGEcheck. Could you add a case with capacity 2 and U+263A that checks forERANGEand that nothing past the buffer changes? If the firmware answers that case differently, its answer wins, but it must not write past the capacity.wcstombs_sandwcsrtombs_sdon't have this issue. They're the ones you already adjusted here.Imported from boykopovar/AnyPS5. Originally posted by hypermoist on 2026-10-22T01:42:36Z.
@MaxLikesCode @oneandonlydean thanks, both fixed. I merged
main(clean), then ind06dfb1caf:GuestBoundsChecking.cpp:wctomb_sandwcrtomb_sof U+263A now expect the console's result: 0, status/result 3, bytesE2 98 BA.wctomb_sandwcrtomb_snow stage through the scratch buffer when_Getmbcurmax()is larger than the capacity, as the firmware does. InlibSceLibcInternal.sprxboth call_Getmbcurmax(0x3c90) for that comparison, not___mb_cur_max(read from disassembly, not measured on a console). With a capacity of 2, U+263A now returnsERANGEthrough the constraint handler.wctomb_sleaves the status and all four bytes untouched.wcrtomb_ssets the result to -1 and writes 0 only to the first byte, which is what the firmware's too-small path stores. The new cases check that nothing past the buffer changes.The next commit shortens the char16/char32 debt entry to under 1000 characters. Locally, all 526 non-GPU
guest_*tests pass on Linux andcheck_conventionspasses.Imported from boykopovar/AnyPS5. Originally posted by JJenkx on 2026-10-22T07:28:54Z.
Supporting evidence from the
libc.prxthat titles bundle, which is the module this libc replaces (read from disassembly of the decrypted copies of PPSA13934, PPSA07923 and PPSA05916; not run, not measured on a console). It agrees with this PR's rules fromlibSceLibcInternal.sprx._Getmbcurmax(U52BlHBvYvE; 0x3a40, 0x38d0 and 0x3810 in the three copies) ismovsbqof one byte of.datathat is 3 in the file in all three.mbrtowc(qVHpv0PxouI, 91 bytes) uses an internal state whenpsis null. A nullsbecomes""with a nullpwcand the caller'sn. It then calls one converter (0x3f990 in PPSA13934), passing a pointer to an eight-entry table of state tables (0x113bf8). That table is in.bss, so it is null until a locale installs one. While it is null, the converter takes a hard-coded UTF-8 path:(b & 0xE0) == 0xC0expects 1 more byte, keeping 5 bits;(b & 0xF0) == 0xE0expects 2 more bytes, keeping 4 bits;errnoto 0x56 (86,EILSEQ) and returns -1 without writing the state back;(b & 0xC0) == 0x80, with no overlong or surrogate check;and 0xffffffe0 / cmp 0xc0,and 0xfffffff0 / cmp 0xe0,movl $0x56) and the same null-table check are in all three copies (PPSA07923 0x40e70, PPSA05916 0x46fb0).So a title calling its bundled
mbrtowc,fgetwc(which goes through the same converter) or_Getmbcurmaxat startup gets UTF-8 with characters of up to 3 bytes. That is what this PR implements, and it resolves the "Titles bundle their own libc.prx, whose locale was not measured" sentence inlibc.md. One limit: I did not follow whatsetlocaleinstalls in that table. The code that fills it is reached through a getter (0x7a50) with 43 callers, so a title that callssetlocalewith another locale may get a table-driven conversion instead.Imported from boykopovar/AnyPS5. Originally posted by DavidSamir on 2026-10-22T08:49:58Z.
Hi @hypermoist, a merge-order note for #9552 (
fix(libc): accept printf argument positions above 99). It edits the samedocs/dev/debt/libc.mdparagraph as this PR, so they conflict in that file only; the code doesn't meet. Proposal: neither depends on the other, and whichever merges second keeps both sentence changes. Does that work for you? I'll record it in #9552's checklist.AI-assisted: yes
Imported from boykopovar/AnyPS5. Originally posted by hypermoist on 2026-10-22T09:08:09Z.
@DavidSamir that works for me. The two PRs only meet in the "narrow printf family" paragraph of
docs/dev/debt/libc.md, and neither depends on the other. Whichever merges second keeps both sentence changes. If #9552 lands first, I'll mergemaininto this branch and combine the paragraph that way.Imported from boykopovar/AnyPS5. Originally posted by hypermoist on 2026-10-22T09:09:47Z.
@JJenkx thanks, that settles the open point. The
libc.mdentry used to say the locale of a title's ownlibc.prxwas not measured. Ina37f8c630ait says a title's bundledlibc.prxalso converts UTF-8 by default, untilsetlocaleinstalls a table, and links your comment. The rules in the code didn't need to change, because your three copies agree with what this PR takes fromlibSceLibcInternal.sprx.