Skip to content

feat(libc): scan 16-bit wide conversions in sscanf and vsscanf on Linux - #6017

Closed
KernelPanicNT wants to merge 1 commit into
boykopovar:mainfrom
KernelPanicNT:feat/libc-sscanf-guest-wide-conversions
Closed

KernelPanicNT wants to merge 1 commit into
boykopovar:mainfrom
KernelPanicNT:feat/libc-sscanf-guest-wide-conversions

Conversation

@KernelPanicNT

@KernelPanicNT KernelPanicNT commented Oct 14, 2026 •

Copy link
Copy Markdown
Contributor

What

On Linux, sscanf, vsscanf, sscanf_s and vsscanf_s throw for the 16-bit wide conversions %ls, %lc, %l[, %S and %C (RejectGuestWideScan, added in #3984): glibc would store a 32-bit wchar_t per character into the guest's 16-bit buffer. This PR scans them instead.

Source of the behaviour. No console measurement is involved. The model is the FreeBSD 11.4 libc (release/11.4.0, commit 8c63994) in its C locale, which this library's multibyte functions already follow:

  • vfscanf.c L594-L612: %S and %C are %s and %c with the l flag, which they set whatever length modifier precedes them, so %lS and %hC are wide too. L683-L728: with l, %c, %[ and %s call convert_wchar, convert_wccl and convert_wstring (L161-L308). Each reads one character at a time through the locale's mbrtowc (__fgetwc_mbs). %ls stops at iswspace and is terminated. %l[ tests ccltab[wctob(wi)], and an empty match leaves the buffer untouched. %lc reads width characters (default 1) with no terminator.
  • none.c L99-L113: in the C locale mbrtowc consumes one byte and stores its value. So a wide directive reads exactly the bytes the narrow directive reads, and iswspace/wctob agree with isspace and the narrow scanset.
  • This library's mbrtowc (fix(runtime): run converted SDK samples through clean C++ shutdown #98) does the same, and the scanner calls it for every byte.
  • vfscanf.c L804-L805: an input failure returns EOF only when no conversion, suppressed or not, has completed.

That the console's libc.prx scans this way is assumed, not checked: the console's locale and scanner were not read.

Changes:

  • sscanf and vsscanf on Linux send a format with a 16-bit wide conversion to ScanGuest, the per-directive scanner that sscanf_s and vsscanf_s already use (feat: boot PPSA04263 with its own sce_module, into the story mode load (supersedes #144) #153 introduced it, feat(libc): implement vsnprintf_s and vsscanf_s #3046 made vsscanf_s use it). Formats without one still go to glibc unchanged.
  • In ScanGuest, a wide directive is scanned by glibc as its narrow form (%S → %s, %C → %c, %l[ → %[) into a temporary byte buffer. Each byte is then widened through mbrtowc into the guest's buffer. %ls and %l[ are terminated, %lc is not. In the _s forms the capacity counts 16-bit units, and a string that does not fit stores an empty string, as %s already does. The temporary byte buffer is bounded by the remaining input, so a large guest width (%2000000000ls) does not size it.
  • The other directives of such a format follow ScanGuest rather than glibc. The one visible difference: when input fails after only suppressed conversions, the call returns 0, as FreeBSD (L804-L805) and C17 do, where glibc returns EOF. The debt entry records this.
  • The format pre-scan now skips length modifiers before the conversion, so it finds %S and %C after one (%lS, %hC). It also skips the contents of a %l[ scanset, which matters now that the pre-scan continues past the first wide conversion instead of throwing there. ScanGuest scans %S and %C as wide whatever length modifier precedes them, as FreeBSD does (L594-L596, L607-L609). Before, a format whose only wide directive was %lS or %hC went to glibc, which stored a 32-bit wchar_t per character. The pre-scan and ScanGuest also accept the q length modifier, as FreeBSD does (L529-L531, q is long long), so %qS and %qC are wide and %qd or %qn in a format scanned by ScanGuest stores a long long.
  • ScanGuest hands a narrow s, c or [ to the host without any length modifier other than l, so %lls, %qs or %Lc there is narrow, as in FreeBSD, where q, ll, L, j, z and t only change integer and floating conversions. Before, sscanf_s and vsscanf_s passed ll, L, j, z and t on these conversions to glibc, which scans them as wide and wrote a 32-bit wchar_t per character into a buffer whose capacity the guest gave in bytes.

Still throwing, and why:

  • A wide conversion together with a positional argument (%1$ls) or the ' flag throws std::runtime_error, because ScanGuest implements neither.
  • fscanf keeps throwing for wide conversions. It reads a host stream, so it needs a separate source; that is the follow-up, feat(libc): scan 16-bit wide conversions in fscanf on Linux #6018, which is stacked on this PR and should be merged after it.
  • A format with no 16-bit wide conversion still goes to glibc unchanged from sscanf and vsscanf, so there %lls, %Ls, %qs, %js, %zs and %ts (and these modifiers with c or [) are still scanned as wide with a 32-bit wchar_t per character, as on the base. These formats are undefined behaviour in C; the debt entry records them.
  • On Windows %ls, %lc and %l[ still go to the host, whose wchar_t is 16 bits. The only Windows change is in ScanGuest, which sscanf_s and vsscanf_s use there: q is handed to the host as ll for integer conversions, and s, c and [ lose any length modifier other than l. %S and %C still throw in sscanf, vsscanf and fscanf there, in the format translation.

Tests: on Linux, guest_formatting no longer expects these formats to throw. It now checks %ls, %S, %3lc after a suppressed %*s, %C, %l[a-z], %lS, %hC, a width, bytes above 0x7f (\xe9, \xff → U+00E9, U+00FF), a mix with %d, a suppressed %*ls, vsscanf, empty and blank input (EOF), an empty %l[ match (0, buffer untouched), and the _s capacity (fit, %lc with capacity 1, overflow → empty string). It also checks %qS, %qC, a width of 2000000000 on short input for %ls and %lc, %ls %qd%qn (two conversions, a long long and a long long count), the variadic sscanf_s with %ls %C, and %qs %ll[a-z] %Lc through sscanf_s, which must store narrow bytes. Guard units after every result must stay untouched. %2$C, %'ls and %1$ls still throw without writing, and %1$ls is checked through sscanf, vsscanf, vsscanf_s and sscanf_s.

Progress (tools/progress.py on the branch base e27ff7c and on the branch head): system libraries 3269/3679 → 3270/3679 (+vsscanf_nid_postfix). The tool counted vsscanf as unimplemented because its single definition reached NotImplemented_nid_no_patch on Linux. sscanf, sscanf_s and vsscanf_s already counted as implemented through their Windows definitions. vsscanf now counts as implemented because the one case still unimplemented on Linux, a wide conversion with a positional argument or the ' flag, throws std::runtime_error rather than calling NotImplemented_nid_no_patch.

Tested

Head f4fa4a38fbffa8f19a3cdb6ec4b593c3da919e10, base e27ff7c545cb5ac669612567d853956ffa02ebe6.

  • Linux (Ubuntu 24.04, GCC 14.2.0, Ninja, Release, BUILD_TESTING=ON, AMD EPYC 7763 (GitHub Codespaces), tests run natively): guest_formatting_tests, guest_scan_count_tests and guest_string_tests each pass in 5 of 5 runs, the test executable being run directly five times.
  • Negative control: keep the tests and replace only core/libs/prx/libc/src/Formatting.cpp with the base version: guest_formatting_tests fails with scanf 16-bit wide conversion on Linux not implemented; guest_scan_count_tests, guest_string_tests still pass there, as expected for regression coverage. Restoring the PR source and rebuilding, guest_formatting_tests, guest_scan_count_tests and guest_string_tests each pass in 5 of 5 runs again.
  • Windows, full Build workflow on my fork (run 38736530145, attempt 2; attempt 1 stopped at configure when the runner could not download FFmpeg), this head built together with the updated heads of feat(libc): scan 16-bit wide conversions in sscanf and vsscanf on Linux #6017, feat(libc): scan 16-bit wide conversions in fscanf on Linux #6018 and feat(libSceAmpr): implement __cxa_call_unexpected #6024 and merged into the fork's main at b08db6a093f440af697d71ea907cec5f675fcdc5, WinLibs MinGW-w64 GCC 15.2.0, Release, BUILD_TESTING=ON: 1278/1279 tests pass, including guest_formatting, guest_scan_count and guest_strings. The one failure, prx_thread_local_destructors, reports a thread_local destructor in libSceAgcDriver (traceDrawKeyLookup), which main gained in de2ddda the same day; this PR does not touch that library. The relinker (Linux, Windows, macOS), register-context and write-watch jobs of the same run pass.
  • Conventions checker and git diff --check pass. The Linux job of the Build workflow now targets the project's self-hosted runner, so it was not run on the fork; no title was run.

Duplicate and overlap check

Checklist

  • Based on e27ff7c; no other open PR implements the same functions
  • 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
  • Generic behaviour, not specific to one title
  • Depends on: none
  • Unblocks: none
  • GPUs tested: n/a
  • AI-assisted: yes (Claude)

🤖 Generated with Claude Code

@github-actions

github-actions Bot commented Oct 14, 2026 •

Copy link
Copy Markdown

Progress report (2e5dd5a → f4fa4a3)

📈 System libraries: 88.72% (+0.02%, +1 implemented functions)

✅ 1 implemented
Library Name
libc vsscanf_nid_postfix

@github-actions

github-actions Bot commented Oct 14, 2026 •

Copy link
Copy Markdown

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.

Pull request Shared files not compared
PR 3891 core/libs/tests/Formatting.cpp
PR 3965 docs/dev/debt/libc.md
PR 3987 core/libs/tests/Formatting.cpp
docs/dev/debt/libc.md
PR 4156 core/libs/tests/Formatting.cpp
PR 4396 core/libs/tests/Formatting.cpp
PR 4397 core/libs/tests/Formatting.cpp
PR 4422 core/libs/tests/Formatting.cpp
PR 4423 core/libs/tests/Formatting.cpp
PR 4424 core/libs/tests/Formatting.cpp
PR 4520 core/libs/tests/Formatting.cpp
PR 4521 core/libs/tests/Formatting.cpp
PR 4522 core/libs/tests/Formatting.cpp
PR 4523 core/libs/tests/Formatting.cpp
PR 4566 core/libs/tests/Formatting.cpp
PR 4599 core/libs/tests/Formatting.cpp
PR 4627 core/libs/tests/Formatting.cpp
PR 4718 core/libs/tests/Formatting.cpp
PR 4740 core/libs/tests/Formatting.cpp
PR 4749 core/libs/tests/Formatting.cpp
PR 4760 core/libs/tests/Formatting.cpp
PR 4813 core/libs/tests/Formatting.cpp
PR 5021 core/libs/tests/Formatting.cpp
PR 5092 docs/dev/debt/libc.md
PR 5303 docs/dev/debt/libc.md
PR 5772 docs/dev/debt/libc.md
PR 5774 core/libs/tests/Formatting.cpp
PR 5835 docs/dev/debt/libc.md
PR 6044 core/libs/tests/Formatting.cpp
PR 6061 docs/dev/debt/libc.md
PR 6107 docs/dev/debt/libc.md
PR 6124 docs/dev/debt/libc.md
PR 6133 docs/dev/debt/libc.md
PR 6144 docs/dev/debt/libc.md
PR 6217 docs/dev/debt/libc.md
PR 6222 core/libs/tests/Formatting.cpp
docs/dev/debt/libc.md
PR 6226 docs/dev/debt/libc.md
PR 6315 core/libs/prx/libc/src/Formatting.cpp
core/libs/tests/Formatting.cpp
docs/dev/debt/libc.md
PR 6318 docs/dev/debt/libc.md
PR 6319 docs/dev/debt/libc.md
PR 6322 docs/dev/debt/libc.md
PR 6330 docs/dev/debt/libc.md
PR 6331 docs/dev/debt/libc.md
PR 6334 docs/dev/debt/libc.md
PR 6339 docs/dev/debt/libc.md
PR 6345 docs/dev/debt/libc.md

@github-actions github-actions Bot mentioned this pull request Oct 15, 2026
11 tasks
On Linux, sscanf, vsscanf, sscanf_s and vsscanf_s threw for %ls, %lc,
%l[, %S and %C because glibc stores a 32-bit wchar_t per character into
the guest's 16-bit buffer. A format with one of these conversions is now
scanned directive by directive in ScanGuest: each wide directive reads
its bytes as the narrow directive does and widens every byte through
this library's mbrtowc, which is what FreeBSD 11.4's vfscanf.c reads in
the single-byte C locale of none.c. The byte buffer for a wide directive
is bounded by the remaining input, not only by the guest's width.

As in FreeBSD, %S and %C are wide whatever length modifier precedes
them. The format pre-scan and ScanGuest recognise the q length modifier
as FreeBSD does, so %lS, %hC and %qS are found too instead of reaching
glibc, and %qd or %qn in a format scanned by ScanGuest stores a long
long. On Windows, ScanGuest hands q to the host as ll for integer
conversions. A narrow s, c or [ is handed to the host without any length
modifier other than l, so %lls, %qs or %Lc in a format scanned by
ScanGuest are narrow, as in FreeBSD, instead of reaching glibc as wide.

A wide conversion combined with a positional argument or the ' flag
still throws. fscanf keeps throwing for wide conversions.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This was referenced Oct 15, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

docs libc needs rebase Conflicts with main, rebase needed

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant