Skip to content

feat(libc): scan 16-bit wide conversions on Linux - #7386

Closed
peeldealer wants to merge 8 commits into
boykopovar:mainfrom
peeldealer:feat/libc-linux-wide-scan
Closed

peeldealer wants to merge 8 commits into
boykopovar:mainfrom
peeldealer:feat/libc-linux-wide-scan

Conversation

@peeldealer

Copy link
Copy Markdown

What

On Linux, fscanf, sscanf, vsscanf, sscanf_s and vsscanf_s threw through NotImplemented_nid_no_patch for the 16-bit wide conversions %ls, %lc, %l[, %S and %C, because glibc's wchar_t is 32 bits. A format with any of these conversions now goes through the guest scanner, the way the printf family already sends wide formats to the guest formatter. The scanner stores each input byte as one 16-bit unit of the same value, as the guest's C-locale mbrtowc converts it. A format without these conversions still goes to glibc unchanged.

  • ScanGuest now reads from a buffer (BufferInput) or a FILE* (StreamInput), so fscanf takes the same path as sscanf. %n counts characters across both kinds of input.
  • The wide conversions follow C17 7.21.6.2:
    • %ls skips leading white space and reads non-white-space bytes up to the width.
    • %lc reads the width (default 1) without skipping or terminating.
    • %l[ reads the scanset.
    • Zero bytes read is an input failure at end of input, and a matching failure otherwise.
  • sscanf_s and vsscanf_s apply their size argument to wide fields in 16-bit elements, with the same overflow handling as the narrow fields. This path is shared, so it also applies on Windows.
  • A conversion outside C17's set that reaches the guest scanner (%2$C, %'ls) throws std::runtime_error, as before.
  • The fscanf and vsscanf exports no longer reach NotImplemented_nid_no_patch, which moves the libraries count from 3432 to 3434.
  • Updated the scanf entry in docs/dev/debt/libc.md. Bytes above 0x7F and locales other than C are not measured on the console.

Tested

Ubuntu 24.04 (WSL2), GCC 13.3.0, Release, no title or homebrew.

  • guest_formatting and file_streams cover:
    • sscanf, vsscanf, sscanf_s and vsscanf_s with %ls, %lc, %l[, %S and %C, plus width, suppression, %n, a byte above 0x7F, EOF and matching failure;
    • secure-capacity overflow and truncation;
    • fscanf on a file stream, including %n and the stream position afterwards.
  • Both tests fail on main and pass with this change.
  • All test suites whose sources use scanf pass, 15 in total:
    • guest_scan_count, guest_wide_formatting, guest_wide_conversion
    • guest_standard_streams, guest_standard_streams_closed_stdout
    • file_streams, guest_formatting, guest_strings
    • guest_allocator_failures, guest_sysmodule, guest_sysmodule_maps_scan
    • guest_kernel_stack_protect, guest_memory, guest_mmap_fixed_anonymous, guest_append_writes
  • Windows was not built locally. The new wide-conversion checks also run on Windows, through the host scanner for sscanf, vsscanf and fscanf and through the guest scanner for the _s forms. The byte-above-0x7F check is Linux-only.

Checklist

  • Based on current main; 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
  • Fixes: none
  • Unblocks: none
  • GPUs tested: n/a
  • AI-assisted: yes

@oneandonlydean

Copy link
Copy Markdown
Collaborator

Thanks. Linux is green, but on Windows guest_formatting and file_streams abort with vsscanf format conversion not implemented (CI run 39096164461). On Windows, sscanf/vsscanf/fscanf go through ScanWindowsArguments_nid_no_patch (core/libs/prx/libc/include/WindowsScanning.hpp:35), which accepts only diouxXaAeEfFgGcspn[. %ls, %lc and %l[ pass, but %S and %C reach NotImplemented_nid_no_patch, so "%S %C" in Formatting.cpp and "%*ls %C" / "%3S" in FileStreams.cpp throw there. Either let the shim accept S/C as ls/lc (UCRT's wchar_t is 16 bits, which the %ls path already relies on; this also matches the _s forms, which now accept them on Windows) and update the debt sentence, or guard those cases with #ifndef _WIN32 and check that they still throw on Windows. The first keeps both platforms consistent.

@github-actions

github-actions Bot commented Oct 18, 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 3965 docs/dev/debt/libc.md
PR 5021 core/libs/tests/Formatting.cpp
docs/dev/debt/libc.md
PR 5303 docs/dev/debt/libc.md
PR 7447 docs/dev/debt/libc.md
PR 7544 docs/dev/debt/libc.md
PR 7715 docs/dev/debt/libc.md
PR 8150 core/libs/tests/FileStreams.cpp
docs/dev/debt/libc.md
PR 8417 docs/dev/debt/libc.md
PR 8965 docs/dev/debt/libc.md
PR 9340 core/libs/prx/libc/src/Formatting.cpp
core/libs/tests/Formatting.cpp
docs/dev/debt/libc.md
PR 9371 docs/dev/debt/libc.md
PR 9447 docs/dev/debt/libc.md
PR 9466 docs/dev/debt/libc.md
PR 9526 docs/dev/debt/libc.md
PR 9677 docs/dev/debt/libc.md
PR 10007 docs/dev/debt/libc.md
PR 10113 docs/dev/debt/libc.md
PR 10226 docs/dev/debt/libc.md
PR 10274 docs/dev/debt/libc.md
PR 10321 docs/dev/debt/libc.md
PR 10397 docs/dev/debt/libc.md
PR 10409 docs/dev/debt/libc.md
PR 10569 docs/dev/debt/libc.md
PR 10780 docs/dev/debt/libc.md
PR 11109 docs/dev/debt/libc.md
PR 11172 docs/dev/debt/libc.md
PR 11262 docs/dev/debt/libc.md
PR 11266 docs/dev/debt/libc.md
PR 11625 docs/dev/debt/libc.md
PR 11634 docs/dev/debt/libc.md
PR 11661 docs/dev/debt/libc.md
PR 11689 docs/dev/debt/libc.md
PR 11698 docs/dev/debt/libc.md
PR 11726 core/libs/prx/libc/src/Formatting.cpp
core/libs/tests/Formatting.cpp
docs/dev/debt/libc.md
PR 11831 core/libs/tests/FileStreams.cpp
docs/dev/debt/libc.md
PR 11848 docs/dev/debt/libc.md
PR 12053 docs/dev/debt/libc.md
PR 12184 docs/dev/debt/libc.md
PR 12518 docs/dev/debt/libc.md
PR 12574 docs/dev/debt/libc.md
PR 12594 docs/dev/debt/libc.md
PR 12598 docs/dev/debt/libc.md
PR 12627 docs/dev/debt/libc.md
PR 12682 docs/dev/debt/libc.md
PR 12688 docs/dev/debt/libc.md
PR 12700 docs/dev/debt/libc.md
PR 12704 docs/dev/debt/libc.md
PR 12849 docs/dev/debt/libc.md
PR 12965 docs/dev/debt/libc.md
PR 13449 docs/dev/debt/libc.md
PR 13794 docs/dev/debt/libc.md
PR 13804 docs/dev/debt/libc.md
PR 14212 docs/dev/debt/libc.md
PR 14298 docs/dev/debt/libc.md
PR 14559 core/libs/tests/FileStreams.cpp
PR 14560 core/libs/tests/FileStreams.cpp
PR 14588 docs/dev/debt/libc.md
PR 14595 docs/dev/debt/libc.md
PR 14915 docs/dev/debt/libc.md
PR 15095 docs/dev/debt/libc.md
PR 15336 docs/dev/debt/libc.md
PR 15561 docs/dev/debt/libc.md
PR 15879 docs/dev/debt/libc.md
PR 15973 docs/dev/debt/libc.md
PR 16170 core/libs/tests/FileStreams.cpp
docs/dev/debt/libc.md
PR 16178 docs/dev/debt/libc.md
PR 16214 docs/dev/debt/libc.md
PR 16516 docs/dev/debt/libc.md
PR 16548 docs/dev/debt/libc.md
PR 16560 docs/dev/debt/libc.md
PR 16613 docs/dev/debt/libc.md
PR 16740 docs/dev/debt/libc.md
PR 16747 docs/dev/debt/libc.md
PR 16757 docs/dev/debt/libc.md
PR 16791 docs/dev/debt/libc.md
PR 16847 docs/dev/debt/libc.md
PR 16861 docs/dev/debt/libc.md
PR 16862 docs/dev/debt/libc.md
PR 16866 docs/dev/debt/libc.md
PR 16912 core/libs/prx/libc/src/Formatting.cpp
core/libs/tests/Formatting.cpp
docs/dev/debt/libc.md
PR 16913 core/libs/tests/FileStreams.cpp
PR 16919 docs/dev/debt/libc.md
PR 16924 docs/dev/debt/libc.md
PR 16933 docs/dev/debt/libc.md
PR 16940 docs/dev/debt/libc.md
PR 16944 docs/dev/debt/libc.md
PR 16949 docs/dev/debt/libc.md
PR 16975 docs/dev/debt/libc.md
PR 17008 docs/dev/debt/libc.md
PR 17010 docs/dev/debt/libc.md
PR 17019 docs/dev/debt/libc.md
PR 17026 docs/dev/debt/libc.md
PR 17062 docs/dev/debt/libc.md
PR 17218 core/libs/prx/libc/src/Formatting.cpp
core/libs/tests/FileStreams.cpp
docs/dev/debt/libc.md
PR 17259 docs/dev/debt/libc.md
PR 17261 docs/dev/debt/libc.md
PR 17277 docs/dev/debt/libc.md
PR 17296 docs/dev/debt/libc.md
PR 17331 docs/dev/debt/libc.md
PR 17432 docs/dev/debt/libc.md
PR 17465 docs/dev/debt/libc.md
PR 17539 docs/dev/debt/libc.md
PR 17555 docs/dev/debt/libc.md
PR 17556 docs/dev/debt/libc.md
PR 17557 docs/dev/debt/libc.md
PR 17558 docs/dev/debt/libc.md
PR 17568 docs/dev/debt/libc.md
PR 17578 docs/dev/debt/libc.md
PR 17607 docs/dev/debt/libc.md
PR 17610 docs/dev/debt/libc.md
PR 17664 docs/dev/debt/libc.md
PR 17724 docs/dev/debt/libc.md

@peeldealer

Copy link
Copy Markdown
Author

Thanks, I took the first option. ScanWindowsArguments_nid_no_patch now passes %S and %C to the host as %ls and %lc, so both platforms accept the same conversions. A length modifier on them, such as %lS, still reaches NotImplemented_nid_no_patch. The debt sentence in libc.md now covers this.

I couldn't run the Windows tests here. What I checked:

  • WindowsScanning.hpp compiles with MinGW-w64 (-fsyntax-only).
  • A Linux harness drove the translation through a recording scanner:
    • %S %C → %ls %lc;
    • %*ls %C → %*ls%n %lc;
    • %3S → %3ls;
    • %ls, %l[a-z], %d %s and %ld translate as before;
    • %lS throws.
  • guest_formatting and file_streams still pass on Linux.

Could you re-run the Windows build?

@peeldealer

Copy link
Copy Markdown
Author

I ran the repository's Build workflow on my fork for this head, 06e6d3e99, merged into main, so the %S/%C fix has a Windows result. The run used windows-latest, the WinLibs GCC 15.2.0 toolchain and the full configure, with the Linux job off because it needs the self-hosted runner.

Result: 1472 of 1472 tests passed in the Windows job, including guest_formatting and file_streams, which aborted in CI run 39096164461.

Run: https://github.com/peeldealer/AnyPS5/actions/runs/39113627972

The same run's relinker, register-context and write-watch jobs passed too, on Linux, macOS and ASan.

@github-actions github-actions Bot added the needs rebase Conflicts with main, rebase needed label Oct 19, 2026
@adriadam10 adriadam10 self-assigned this Oct 19, 2026
@github-actions

github-actions Bot commented Oct 19, 2026 •

Copy link
Copy Markdown

Progress report (38d463a → bf73523)

📈 System libraries: 48.8% (+0.01%, +2 implemented functions)

✅ 2 implemented
Library Name
libc fscanf_nid_postfix
libc vfscanf_nid_postfix

Changed libraries (implemented exports)

Library Before After Change
libc 1338/1348 (99.26%) 1340/1348 (99.41%) +0.15 pp

@adriadam10
adriadam10 force-pushed the feat/libc-linux-wide-scan branch from 06e6d3e to 1e4be7b Compare October 19, 2026 14:00
@github-actions github-actions Bot removed the needs rebase Conflicts with main, rebase needed label Oct 19, 2026
@github-actions github-actions Bot added the needs rebase Conflicts with main, rebase needed label Oct 19, 2026
fscanf, sscanf, vsscanf, sscanf_s and vsscanf_s on Linux threw for
%ls, %lc, %l[, %S and %C because glibc stores a 32-bit wchar_t. Formats
with these conversions now go through the guest scanner, which reads
from a buffer or a FILE stream and stores each byte as one 16-bit unit,
as the guest's C-locale mbrtowc does.
The Windows sscanf, vsscanf and fscanf shim accepted %ls and %lc but
threw for %S and %C, which the new wide-conversion tests use. They are
now passed to the host as %ls and %lc, whose wchar_t is 16 bits, so
both platforms accept the same conversions.
@nikowolf123

Copy link
Copy Markdown
Contributor

Heads-up before this is merged: GitHub shows no conflict, but the merge with current main no longer builds on Linux.

#8292 (merged today as 4b527ad) added vfscanf_nid_postfix to core/libs/prx/libc/src/Formatting.cpp, and its Linux branch calls RejectGuestWideScan(format). This PR renames that helper to HasGuestWideScan, so in the merge of 2b64ed4286 into main 6830c91 (git merge-tree --write-tree origin/main 2b64ed4286 → tree 423212e5ef), line 679 of Formatting.cpp calls a function that no longer exists. git grep RejectGuestWideScan on that tree finds only this call:

int APS5_VABI vfscanf_nid_postfix(FileStream* stream, const char* format, VaList* args) {
    ...
#else
    RejectGuestWideScan(format);

The earlier CI runs on this PR predate #8292, so they could not show it. Rebasing onto current main and sending the Linux path of vfscanf_nid_postfix through the stream path that fscanf_nid_postfix uses here would fix the build. It would also give vfscanf, vscanf and scanf the same %ls/%S handling on Linux as fscanf.

@github-actions github-actions Bot added the needs rebase Conflicts with main, rebase needed label Oct 21, 2026
@github-actions github-actions Bot removed the needs rebase Conflicts with main, rebase needed label Oct 21, 2026
@oneandonlydean oneandonlydean self-assigned this Oct 30, 2026
@oneandonlydean

Copy link
Copy Markdown
Collaborator

Thanks for the Windows fix: mapping bare %S/%C to %ls/%lc in WindowsScanning.hpp resolves what we reported on the earlier head.

Since then main has moved a lot under this branch (it is about 8,500 commits behind): #7525 added 16-bit wide conversions to the sscanf family, and the stream scanning paths in core/libs/prx/libc/src/Formatting.cpp were reworked. Stream scans on main still throw for wide conversions (RejectGuestWideScan), which this PR implements. The branch now conflicts in Formatting.cpp, core/libs/tests/GuestStdioVariadic.cpp and the libc debt file. Could you merge current main (or rebase), keep what #7525 already provides, and rerun guest_formatting, file_streams and the stdio variadic tests on Linux and Windows? We'll review the rebased head.

@oneandonlydean

Copy link
Copy Markdown
Collaborator

Hi @peeldealer, @adriadam10,

Thanks for the work on the 16-bit wide scan conversions. We're closing this as superseded: main has since reworked the libc formatting and scanning paths this touches, and the PR now conflicts with main in Formatting.cpp, WindowsScanning.hpp, GuestStdioVariadic.cpp and the libc debt file, with no update since mid-October.

If any of %ls, %lc, %l[, %S or %C still throws on current main for a title, a fresh PR against current main with that case would be very welcome.

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.

6 participants