fix(libc): decode a wide printf %s as titles' libc.prx does, failing on invalid UTF-8 #9953

Merged
JJenkx merged 1 commit from upstream-pr/9953 into main 2026-11-02 07:59:39 +00:00
JJenkx commented 2026-10-22 23:43:16 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by JJenkx on 2026-10-22T23:43:16Z.


What

The wide printf family (swprintf, vswprintf, wprintf, fwprintf, …) decodes a narrow %s argument into UTF-16. Up to now it accepted UTF-8 of up to 4 bytes and turned any byte that didn't form a character into U+0000–U+00FF. The libc.prx that titles ship decodes differently. It accepts at most 3 bytes per character, the same limit #8855 measures in the firmware's multibyte functions. On anything else it fails the whole call. Console vs main:

Call Console main
[%s] of F0 9F 98 80 -1, errno 86 (EILSEQ), buffer [ 4: [, U+D83D U+DE00, ]
x%sy of 80, C3, E2 82, FF or C3 A9 C3 -1, EILSEQ, buffer x 3 or 4, with the bytes as U+0080…U+00FF
[%s] of C0 80 then A 2: [] 3: [, U+0000, ]
[%s] of C1 81 or E0 81 81 [A] [, U+00C1, U+0081, ]
[%3s] of "", or [%3.0s] of AB [] [ ]
[%.0s] of 80 -1, EILSEQ []

Rules followed, all read from that libc.prx:

  • Decoding. Bytes 0x00–0x7F are one character. 0xC0–0xDF need one continuation byte and 0xE0–0xEF need two. Overlong forms and encoded surrogates decode: ED A0 BD ED B8 80 is U+D83D U+DE00, the form #9939 writes for %ls.
  • Invalid input. These fail the call: a continuation byte as a lead (0x80–0xBF), any lead of 0xF0 or more, and a missing or wrong continuation byte. swprintf and vswprintf then return -1 with errno EILSEQ. The buffer keeps the text produced before that conversion.
  • NUL. A decoded NUL, such as an overlong C0 80, ends the string.
  • Precision. A precision stops after that many characters but always decodes the first. So %.1s of A\x80 is A, while %.2s of it fails and so does %.0s of \x80.
  • Width. A field that decodes to no character is not padded to its width. The narrow %ls/%S of the wide family still pads an empty string, as the console does.

Implementation. AppendUtf16 applies these rules and reports a failure. FormatWide then throws WideEncodingError with the text so far. vswprintf writes that text and sets EILSEQ. wprintf and vfwprintf set EILSEQ instead of EINVAL. The secure forms fail as before. The debt file records the stream and secure behaviour as unverified.

Tested

Main conflict refresh, 2026-10-27 10:18 UTC pass: exact head d777f1973b80b3056827557c713bef216d675227 on tested main 3fea2a937ba9fbab2f06c51691fe0c5763659aa3. The topic production/test edits and current main's includes and checked-pointer behavior are preserved.

Linux Release focused rebuild: Narrow/wide formatting, stdio variadic and public headers each pass three times: 12 passes, zero skips. Conventions, whitespace and merge checks pass. Every refreshed head's submodule pointers match the pinned main; the private dependency checkouts used for the gates are aligned. No new negative-control, Windows, console, title, GPU, full-suite or performance run is claimed. Earlier measurements below retain their original exact pins. Fulfilled merge-order dependencies have been cleared from the checklist; open prerequisites remain.

Exact source/dependency pins, patch-preservation and gitlink proofs, builds, verbose tests, JUnit and lease-push receipts. SHA-256: 702c4110ee9dde421c2dd5e057ed53a2df05e0cf7b01f67e8fb9cb779e332f7c; downloaded copy verified.

Linux follow-up (2026-10-23 UTC): Fresh Release configuration of exact head 8bffaa164af60bb9be39d93b58b5c821bd4e1a01 on Arch Linux 7.2.9 / glibc 2.44 / GCC 16.2.1. guest_wide_formatting passes 1/1 in three runs, with no skips, exercising the new decoder, guest errno == 86, partial-buffer handling and width/precision cases. Negative control: retain the PR tests but substitute parent 24c6cc33f090296ab90dc154abdfb9ef548fc923's FormattingWide.cpp; it builds and the same test aborts at swprintf rejects a 4-byte sequence and keeps the output before it. Source restored clean afterward; the private build's last executable is the negative control. Full build/test logs, source pins, validator and review (SHA-256 b5a63de38830d7486e72610e0b0843eaa194d24f42e678fd0405e37984bfb751). Exact-head focused Linux evidence; no latest-main, full-product, native-console or title claim.

Windows 11 Enterprise 10.0.22621, Ryzen 9 5950X, WinLibs GCC 15.2.0 UCRT r7, Ninja, Release, BUILD_TESTING=ON, main 24c6cc33f0, head 8bffaa164a.

  • Differential run. A scratch loader runs PPSA07923's decrypted libc.prx natively. It compares its swprintf (nJz16JE1txM) with this build's swprintf_nid_postfix, checking the result, the units written and errno.

    • Formats: [%s], [%.1s], [%3s] and [%hs] on 13 strings: A é, €, a 4-byte U+1F600, its 3-byte surrogate form, 80, C3, C3 x, E2 82, FF, C0 80, E0 80 80, EF BF BF and F4 8F BF BF.
    • Results: main differs in 36 of the 52 cases; this build in none.
    • Separate probes of the precision and empty-field rules match as listed above.
  • guest_wide_formatting. Updated tests:

    • Three Check rows and the swprintf surrogate test used a 4-byte U+1F600, which the console rejects. They now use its 3-byte surrogate form, with the console's results.
    • The %n test now counts that form's two units.
    • New checks cover the 4-byte and invalid cases with EILSEQ and the partial buffer, overlong NUL and A, precision before and at an invalid byte, precision zero, and empty and non-empty fields with a width.

    The libc formatting, scanning, stream, wide-character and string tests (34) pass in three runs out of three. With main's FormattingWide.cpp and these tests, guest_wide_formatting aborts.

  • python tools/check_conventions.py --base origin/main and git diff --check pass.

  • Overlap. Merges cleanly with #9927 and #9596, which also edit FormattingWide.cpp. #9527 and #5303 have a neighbouring-line conflict in docs/dev/debt/libc.md.

  • Not tested. A native console; the output of wprintf/fwprintf on such a string.

  • Linux (reviewer run): MarcZX10 ran a focused Linux check in an existing configured Release build with GCC 12: guest_wide_formatting passed 1/1 with this PR's FormattingWide.cpp and GuestWideFormatting.cpp. That reused an older configured tree with the PR's sources overlaid, so it is a focused Linux run, not a clean CI build.

  • Linux (reviewer run, exact head): Dom3005 ran head 8bffaa164a merged onto main 83462cc57 in an Ubuntu 26.04 container (GCC 15.2.0, glibc 2.43, Release, BUILD_TESTING=ON): guest_wide_formatting passed 3/3, including the EILSEQ assertions. The negative control was not run there.

Checklist

  • Based on current main; no other open PR changes how the wide family decodes %s
  • One topic per PR; follow-ups go in a new PR
  • No comments in code except technical debt
  • Unimplemented paths throw (NotImplemented_nid_no_patch); silent stubs are listed under Silent stubs in their library's technical debt file
  • No notes, investigation .md files or images added to the repository (attach them to this PR)
  • New third-party code is a submodule built from source: none
  • Generic behaviour, not specific to one title
  • Depends on: none
  • Fixes: none
  • Unblocks: none
  • GPUs tested: n/a
  • AI-assisted: yes

AI-assisted (Claude Code).

Conflict refresh, 2026-10-27 UTC: exact head f40626ee7bb854b9b3ced6a9eec54d4ea667dc02 on tested main 74acfad65b3f0fd4eab653f3a3989d298fba1157. Preserved the wide UTF-8 conversion topic and current main's added test includes and debt.

Linux Release focused libraries/executables rebuilt. Three groups (wide formatting, narrow formatting, public headers) each pass three times: 9 initial and 9 restored passes. Main-production-only FormattingWide control fails the intended wide-formatting group; restored libc is byte-identical. Zero skips; conventions and whitespace pass. This is focused Linux evidence, without new Windows, console, title, GPU or full-suite validation. Earlier measurements retain their original exact pins.

Source/dependency pins, production preservation, verbose build/test/control/restoration logs, JUnit, hashes and lease-push receipts. SHA-256: 594bf1dd3a57728c23dd0752926f501f34a8be8e31c8d924e6bec3723d029a82; downloaded copy verified.

<!-- greyfinch-sync:boykopovar/AnyPS5:issue:4975633292 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/9953). Originally posted by [JJenkx](https://github.com/JJenkx) on 2026-10-22T23:43:16Z. --- ### What The wide printf family (`swprintf`, `vswprintf`, `wprintf`, `fwprintf`, …) decodes a narrow `%s` argument into UTF-16. Up to now it accepted UTF-8 of up to 4 bytes and turned any byte that didn't form a character into U+0000–U+00FF. The `libc.prx` that titles ship decodes differently. It accepts at most 3 bytes per character, the same limit #8855 measures in the firmware's multibyte functions. On anything else it fails the whole call. Console vs `main`: | Call | Console | `main` | |---|---|---| | `[%s]` of `F0 9F 98 80` | -1, `errno` 86 (`EILSEQ`), buffer `[` | 4: `[`, U+D83D U+DE00, `]` | | `x%sy` of `80`, `C3`, `E2 82`, `FF` or `C3 A9 C3` | -1, `EILSEQ`, buffer `x` | 3 or 4, with the bytes as U+0080…U+00FF | | `[%s]` of `C0 80` then `A` | 2: `[]` | 3: `[`, U+0000, `]` | | `[%s]` of `C1 81` or `E0 81 81` | `[A]` | `[`, U+00C1, U+0081, `]` | | `[%3s]` of `""`, or `[%3.0s]` of `AB` | `[]` | `[ ]` | | `[%.0s]` of `80` | -1, `EILSEQ` | `[]` | **Rules followed, all read from that `libc.prx`:** - **Decoding.** Bytes 0x00–0x7F are one character. 0xC0–0xDF need one continuation byte and 0xE0–0xEF need two. Overlong forms and encoded surrogates decode: `ED A0 BD ED B8 80` is U+D83D U+DE00, the form #9939 writes for `%ls`. - **Invalid input.** These fail the call: a continuation byte as a lead (0x80–0xBF), any lead of 0xF0 or more, and a missing or wrong continuation byte. `swprintf` and `vswprintf` then return -1 with `errno` `EILSEQ`. The buffer keeps the text produced before that conversion. - **NUL.** A decoded NUL, such as an overlong `C0 80`, ends the string. - **Precision.** A precision stops after that many characters but always decodes the first. So `%.1s` of `A\x80` is `A`, while `%.2s` of it fails and so does `%.0s` of `\x80`. - **Width.** A field that decodes to no character is not padded to its width. The narrow `%ls`/`%S` of the wide family still pads an empty string, as the console does. **Implementation.** `AppendUtf16` applies these rules and reports a failure. `FormatWide` then throws `WideEncodingError` with the text so far. `vswprintf` writes that text and sets `EILSEQ`. `wprintf` and `vfwprintf` set `EILSEQ` instead of `EINVAL`. The secure forms fail as before. The debt file records the stream and secure behaviour as unverified. ### Tested <!-- codex-main-refresh-20261027-1018-pr9953 --> **Main conflict refresh, 2026-10-27 10:18 UTC pass:** exact head `d777f1973b80b3056827557c713bef216d675227` on tested main `3fea2a937ba9fbab2f06c51691fe0c5763659aa3`. The topic production/test edits and current main's includes and checked-pointer behavior are preserved. Linux Release focused rebuild: Narrow/wide formatting, stdio variadic and public headers each pass three times: 12 passes, zero skips. Conventions, whitespace and merge checks pass. Every refreshed head's submodule pointers match the pinned main; the private dependency checkouts used for the gates are aligned. No new negative-control, Windows, console, title, GPU, full-suite or performance run is claimed. Earlier measurements below retain their original exact pins. Fulfilled merge-order dependencies have been cleared from the checklist; open prerequisites remain. [Exact source/dependency pins, patch-preservation and gitlink proofs, builds, verbose tests, JUnit and lease-push receipts](https://github.com/JJenkx/AnyPS5/releases/download/linux-validation-20261023/owned-cpu11-main3fea-linux-20261027-1018.tar.gz). SHA-256: `702c4110ee9dde421c2dd5e057ed53a2df05e0cf7b01f67e8fb9cb779e332f7c`; downloaded copy verified. **Linux follow-up (2026-10-23 UTC):** Fresh Release configuration of exact head `8bffaa164af60bb9be39d93b58b5c821bd4e1a01` on Arch Linux 7.2.9 / glibc 2.44 / GCC 16.2.1. `guest_wide_formatting` passes **1/1 in three runs**, with no skips, exercising the new decoder, guest `errno == 86`, partial-buffer handling and width/precision cases. Negative control: retain the PR tests but substitute parent `24c6cc33f090296ab90dc154abdfb9ef548fc923`'s `FormattingWide.cpp`; it builds and the same test aborts at `swprintf rejects a 4-byte sequence and keeps the output before it`. Source restored clean afterward; the private build's last executable is the negative control. [Full build/test logs, source pins, validator and review](https://github.com/JJenkx/AnyPS5/releases/download/game-library-checks-20261016/20261023-pr9953-linux-wide-formatting-validation.tar.gz) (SHA-256 `b5a63de38830d7486e72610e0b0843eaa194d24f42e678fd0405e37984bfb751`). Exact-head focused Linux evidence; no latest-main, full-product, native-console or title claim. Windows 11 Enterprise 10.0.22621, Ryzen 9 5950X, WinLibs GCC 15.2.0 UCRT r7, Ninja, Release, `BUILD_TESTING=ON`, `main` 24c6cc33f0, head 8bffaa164a. - **Differential run.** A scratch loader runs PPSA07923's decrypted `libc.prx` natively. It compares its `swprintf` (`nJz16JE1txM`) with this build's `swprintf_nid_postfix`, checking the result, the units written and `errno`. - Formats: `[%s]`, `[%.1s]`, `[%3s]` and `[%hs]` on 13 strings: `A é`, `€`, a 4-byte U+1F600, its 3-byte surrogate form, `80`, `C3`, `C3 x`, `E2 82`, `FF`, `C0 80`, `E0 80 80`, `EF BF BF` and `F4 8F BF BF`. - Results: `main` differs in 36 of the 52 cases; this build in none. - Separate probes of the precision and empty-field rules match as listed above. - **`guest_wide_formatting`.** Updated tests: - Three `Check` rows and the `swprintf` surrogate test used a 4-byte U+1F600, which the console rejects. They now use its 3-byte surrogate form, with the console's results. - The `%n` test now counts that form's two units. - New checks cover the 4-byte and invalid cases with `EILSEQ` and the partial buffer, overlong NUL and `A`, precision before and at an invalid byte, precision zero, and empty and non-empty fields with a width. The libc formatting, scanning, stream, wide-character and string tests (34) pass in three runs out of three. With `main`'s `FormattingWide.cpp` and these tests, `guest_wide_formatting` aborts. - `python tools/check_conventions.py --base origin/main` and `git diff --check` pass. - **Overlap.** Merges cleanly with #9927 and #9596, which also edit `FormattingWide.cpp`. #9527 and #5303 have a neighbouring-line conflict in `docs/dev/debt/libc.md`. - **Not tested.** A native console; the output of `wprintf`/`fwprintf` on such a string. - **Linux (reviewer run):** [MarcZX10 ran a focused Linux check](https://github.com/boykopovar/AnyPS5/pull/9953#issuecomment-6354432845) in an existing configured Release build with GCC 12: `guest_wide_formatting` passed 1/1 with this PR's `FormattingWide.cpp` and `GuestWideFormatting.cpp`. That reused an older configured tree with the PR's sources overlaid, so it is a focused Linux run, not a clean CI build. - **Linux (reviewer run, exact head):** [Dom3005 ran head 8bffaa164a merged onto `main` 83462cc57](https://github.com/boykopovar/AnyPS5/pull/9953#issuecomment-6354717140) in an Ubuntu 26.04 container (GCC 15.2.0, glibc 2.43, Release, `BUILD_TESTING=ON`): `guest_wide_formatting` passed 3/3, including the `EILSEQ` assertions. The negative control was not run there. ### Checklist - [x] Based on current `main`; no other open PR changes how the wide family decodes `%s` - [x] One topic per PR; follow-ups go in a new PR - [x] No comments in code except [technical debt](https://github.com/boykopovar/AnyPS5/blob/main/docs/dev/CONVENTIONS.md) - [x] Unimplemented paths throw (`NotImplemented_nid_no_patch`); silent stubs are listed under Silent stubs in their library's [technical debt](https://github.com/boykopovar/AnyPS5/tree/main/docs/dev/debt) file - [x] No notes, investigation `.md` files or images added to the repository (attach them to this PR) - [x] New third-party code is a submodule built from source: none - [x] Generic behaviour, not specific to one title - [x] Depends on: none - [x] Fixes: none - [x] Unblocks: none - [x] GPUs tested: n/a - [x] AI-assisted: yes AI-assisted (Claude Code). <!-- codex-main-refresh-20261027-pr9953 --> **Conflict refresh, 2026-10-27 UTC:** exact head `f40626ee7bb854b9b3ced6a9eec54d4ea667dc02` on tested main `74acfad65b3f0fd4eab653f3a3989d298fba1157`. Preserved the wide UTF-8 conversion topic and current main's added test includes and debt. Linux Release focused libraries/executables rebuilt. Three groups (wide formatting, narrow formatting, public headers) each pass three times: 9 initial and 9 restored passes. Main-production-only FormattingWide control fails the intended wide-formatting group; restored libc is byte-identical. Zero skips; conventions and whitespace pass. This is focused Linux evidence, without new Windows, console, title, GPU or full-suite validation. Earlier measurements retain their original exact pins. [Source/dependency pins, production preservation, verbose build/test/control/restoration logs, JUnit, hashes and lease-push receipts](https://github.com/JJenkx/AnyPS5/releases/download/linux-validation-20261023/owned-formatting-clock-rand-main74ac-linux-20261027.tar.gz). SHA-256: `594bf1dd3a57728c23dd0752926f501f34a8be8e31c8d924e6bec3723d029a82`; downloaded copy verified.
MarcZX10 commented 2026-10-23 00:30:14 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-10-23T00:30:14Z.

Review: APPROVED


Reviewed the decoder, failure path, width/precision behavior, partial-buffer handling, and regression cases. The test includes a valid prefix followed by invalid input and asserts that output from the current conversion is not committed, matching the reported console behavior. I ran the focused Linux guest_wide_formatting test; it passed 1/1 using the PR head’s changed implementation and test sources in the existing configured build. The author’s Windows differential evidence is extensive, and required hosted checks pass; full Linux/Windows product CI remains skipped by policy. Approved at head 8bffaa164af60bb9be39d93b58b5c821bd4e1a01.

<!-- greyfinch-sync:boykopovar/AnyPS5:review:5622554828 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/9953#pullrequestreview-5622554828). Originally posted by [MarcZX10](https://github.com/MarcZX10) on 2026-10-23T00:30:14Z. **Review:** APPROVED --- Reviewed the decoder, failure path, width/precision behavior, partial-buffer handling, and regression cases. The test includes a valid prefix followed by invalid input and asserts that output from the current conversion is not committed, matching the reported console behavior. I ran the focused Linux `guest_wide_formatting` test; it passed 1/1 using the PR head’s changed implementation and test sources in the existing configured build. The author’s Windows differential evidence is extensive, and required hosted checks pass; full Linux/Windows product CI remains skipped by policy. Approved at head 8bffaa164af60bb9be39d93b58b5c821bd4e1a01.
Sign in to join this conversation.
No reviewers
No labels
No milestone
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Reference
boykopovar/AnyPS5!9953
No description provided.