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

Closed
peeldealer wants to merge 8 commits from upstream-pr/7386 into main
peeldealer commented 2026-10-18 04:00:59 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by peeldealer on 2026-10-18T04:00:59Z.


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
<!-- greyfinch-sync:boykopovar/AnyPS5:issue:4906221421 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/7386). Originally posted by [peeldealer](https://github.com/peeldealer) on 2026-10-18T04:00:59Z. --- ### 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 <!-- Rules behind each item: https://github.com/boykopovar/AnyPS5/blob/main/CONTRIBUTING.md --> - [x] Based on current `main`; no other open PR implements the same functions - [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 - [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
oneandonlydean commented 2026-10-18 04:23:43 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by oneandonlydean on 2026-10-18T04:23:43Z.


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.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6245616730 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/7386#issuecomment-6245616730). Originally posted by [oneandonlydean](https://github.com/oneandonlydean) on 2026-10-18T04:23:43Z. --- 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.
peeldealer commented 2026-10-18 05:52:59 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by peeldealer on 2026-10-18T05:52:59Z.


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?

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6246284908 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/7386#issuecomment-6246284908). Originally posted by [peeldealer](https://github.com/peeldealer) on 2026-10-18T05:52:59Z. --- 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 commented 2026-10-18 09:41:01 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by peeldealer on 2026-10-18T09:41:01Z.


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.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6248351867 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/7386#issuecomment-6248351867). Originally posted by [peeldealer](https://github.com/peeldealer) on 2026-10-18T09:41:01Z. --- 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.
nikowolf123 commented 2026-10-20 21:52:59 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by nikowolf123 on 2026-10-20T21:52:59Z.


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 4b527adf05) 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 6830c91641 (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.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6302492358 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/7386#issuecomment-6302492358). Originally posted by [nikowolf123](https://github.com/nikowolf123) on 2026-10-20T21:52:59Z. --- 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 4b527adf05) 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` 6830c91641 (`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: ```cpp 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`.
MarcZX10 commented 2026-10-23 22:12:27 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-10-23T22:12:27Z.

Review: CHANGES_REQUESTED


The stream lookahead can require more pushback than the portable stdio contract guarantees. PeekWideScan reads up to four bytes, then restores them by calling StreamInput::Back repeatedly; each call uses std::ungetc, and this path is enabled for every non-Windows host, including macOS. POSIX guarantees one byte of pushback and allows later ungetc calls to fail without an intervening read or positioning operation (POSIX ungetc). This can make valid multibyte wide input fail or leave the stream partially restored on a supported host. Please keep lookahead bytes in an internal buffer (or use another mechanism that does not depend on multiple consecutive ungetc calls), and add/ run the multibyte stream case on the supported non-Windows platforms.

<!-- greyfinch-sync:boykopovar/AnyPS5:review:5634988192 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/7386#pullrequestreview-5634988192). Originally posted by [MarcZX10](https://github.com/MarcZX10) on 2026-10-23T22:12:27Z. **Review:** CHANGES_REQUESTED --- The stream lookahead can require more pushback than the portable stdio contract guarantees. `PeekWideScan` reads up to four bytes, then restores them by calling `StreamInput::Back` repeatedly; each call uses `std::ungetc`, and this path is enabled for every non-Windows host, including macOS. POSIX guarantees one byte of pushback and allows later `ungetc` calls to fail without an intervening read or positioning operation ([POSIX `ungetc`](https://www.man7.org/linux/man-pages/man3/ungetc.3p.html)). This can make valid multibyte wide input fail or leave the stream partially restored on a supported host. Please keep lookahead bytes in an internal buffer (or use another mechanism that does not depend on multiple consecutive `ungetc` calls), and add/ run the multibyte stream case on the supported non-Windows platforms.

Pull request closed

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!7386
No description provided.