feat(libc): add the wide stream scanf family #8479

Merged
hypermoist merged 3 commits from upstream-pr/8479 into main 2026-10-21 07:58:25 +00:00
hypermoist commented 2026-10-20 13:25:43 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by hypermoist on 2026-10-20T13:25:43Z.


What

Adds the eight wide stream scanf exports, which upstream does not have yet:

Export NID
fwscanf a6CYO8YOzfw
fwscanf_s Bo5wtXSj4kc
vfwscanf WF4fBmip+38
vfwscanf_s Wvm90I-TGl0
wscanf D8JBAR3RiZQ
wscanf_s RV7X3FrWfTI
vwscanf QNwdOK7HfJU
vwscanf_s YgZ6qvFH3QI

In the console's libSceLibcInternal.sprx, each of them locks the stream and calls the same scanner as swscanf, passing a callback that reads with fgetwc and pushes back with ungetwc. The _s forms set the secure flag and return EOF for a null stream or format. The wscanf forms use stdin.

So ScanningWide.cpp now takes its input from a small WideInput source, which reads either the existing buffer or a guest FileStream:

  • A field consumes characters as it reads them, as stream scanning must.
  • %n counts the characters consumed.
  • When scanning ends, the one lookahead character is pushed back to the stream as its UTF-8 bytes. ungetc is used because ungetwc upstream only takes single bytes.
  • vfwscanf and vfwscanf_s (and the forms that call them) hold the stream lock with flockfile for the whole call, as the console does, so another thread's reads cannot interleave between fields. A closed stream is not locked.

The buffer path behaves as before: consuming a field that then fails has no visible effect there. A new entry under Functional in libc.md records the push-back limit on Windows and the null checks.

Tested

Linux (CachyOS, GCC, Release).

  • Firmware differential. A harness called the console's scanner with a recording stream callback. It compared 100 000 random inputs (ASCII, é, newlines) across 26 formats against vfwscanf on a temporary file, checking the return value, stored bytes, %n and the text left in the stream. What a field consumes and what goes back to the stream matched in every case.
  • Remaining differences. Every mismatch is in the scanner swscanf already shares upstream, and the buffer path has the same ones. The console converts the mantissa of an incomplete exponent (1e+), stores a short %c field, and returns 0 rather than EOF when a literal meets the end of input. Those fixes touch the swscanf debt line that #8306 is changing, so they will follow in a separate PR.
  • Repo tests. New test guest_wide_stream_scan covers consumption and push-back (including a non-ASCII lookahead), %ln, the sized forms, null streams and stdin. It passes, along with guest_wide_conversion, guest_stdio_variadic, guest_wide_strings and guest_scan_count.

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 verified
  • GPUs tested: none (CPU-only change)
  • AI-assisted: yes (Claude Code), reviewed by the contributor

🤖 Generated with Claude Code

https://claude.ai/code/session_01RbLgECLQ2bpDM7oe2urGZK

<!-- greyfinch-sync:boykopovar/AnyPS5:issue:4936429398 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/8479). Originally posted by [hypermoist](https://github.com/hypermoist) on 2026-10-20T13:25:43Z. --- ### What Adds the eight wide stream `scanf` exports, which upstream does not have yet: | Export | NID | |---|---| | `fwscanf` | `a6CYO8YOzfw` | | `fwscanf_s` | `Bo5wtXSj4kc` | | `vfwscanf` | `WF4fBmip+38` | | `vfwscanf_s` | `Wvm90I-TGl0` | | `wscanf` | `D8JBAR3RiZQ` | | `wscanf_s` | `RV7X3FrWfTI` | | `vwscanf` | `QNwdOK7HfJU` | | `vwscanf_s` | `YgZ6qvFH3QI` | In the console's `libSceLibcInternal.sprx`, each of them locks the stream and calls the same scanner as `swscanf`, passing a callback that reads with `fgetwc` and pushes back with `ungetwc`. The `_s` forms set the secure flag and return `EOF` for a null stream or format. The `wscanf` forms use `stdin`. So `ScanningWide.cpp` now takes its input from a small `WideInput` source, which reads either the existing buffer or a guest `FileStream`: - A field consumes characters as it reads them, as stream scanning must. - `%n` counts the characters consumed. - When scanning ends, the one lookahead character is pushed back to the stream as its UTF-8 bytes. `ungetc` is used because `ungetwc` upstream only takes single bytes. - `vfwscanf` and `vfwscanf_s` (and the forms that call them) hold the stream lock with `flockfile` for the whole call, as the console does, so another thread's reads cannot interleave between fields. A closed stream is not locked. The buffer path behaves as before: consuming a field that then fails has no visible effect there. A new entry under *Functional* in `libc.md` records the push-back limit on Windows and the null checks. ### Tested Linux (CachyOS, GCC, Release). - **Firmware differential.** A harness called the console's scanner with a recording stream callback. It compared 100 000 random inputs (ASCII, `é`, newlines) across 26 formats against `vfwscanf` on a temporary file, checking the return value, stored bytes, `%n` and the text left in the stream. What a field consumes and what goes back to the stream matched in every case. - **Remaining differences.** Every mismatch is in the scanner `swscanf` already shares upstream, and the buffer path has the same ones. The console converts the mantissa of an incomplete exponent (`1e+`), stores a short `%c` field, and returns 0 rather than `EOF` when a literal meets the end of input. Those fixes touch the `swscanf` debt line that #8306 is changing, so they will follow in a separate PR. - **Repo tests.** New test `guest_wide_stream_scan` covers consumption and push-back (including a non-ASCII lookahead), `%ln`, the sized forms, null streams and `stdin`. It passes, along with `guest_wide_conversion`, `guest_stdio_variadic`, `guest_wide_strings` and `guest_scan_count`. ### Checklist - [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 verified - [x] GPUs tested: none (CPU-only change) - [x] AI-assisted: yes (Claude Code), reviewed by the contributor 🤖 Generated with [Claude Code](https://claude.com/claude-code) https://claude.ai/code/session_01RbLgECLQ2bpDM7oe2urGZK
github-actions[bot] commented 2026-10-20 13:26:25 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by github-actions[bot] on 2026-10-20T13:26:25Z.


Progress report (c8301bb → ad7d2a1)

📈 System libraries: 62.87% (+0.04%, +8 implemented functions)

✅ 8 implemented
Library Name
libc fwscanf_nid_postfix
libc fwscanf_s_nid_postfix
libc vfwscanf_nid_postfix
libc vfwscanf_s_nid_postfix
libc vwscanf_nid_postfix
libc vwscanf_s_nid_postfix
libc wscanf_nid_postfix
libc wscanf_s_nid_postfix
<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6291943851 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/8479#issuecomment-6291943851). Originally posted by [github-actions[bot]](https://github.com/apps/github-actions) on 2026-10-20T13:26:25Z. --- <!-- progress-report --> ### Progress report (c8301bb → ad7d2a1) 📈 **System libraries**: 62.87% (+0.04%, +8 implemented functions) <details> <summary>✅ 8 implemented</summary> | Library | Name | | - | - | | libc | `fwscanf_nid_postfix` | | libc | `fwscanf_s_nid_postfix` | | libc | `vfwscanf_nid_postfix` | | libc | `vfwscanf_s_nid_postfix` | | libc | `vwscanf_nid_postfix` | | libc | `vwscanf_s_nid_postfix` | | libc | `wscanf_nid_postfix` | | libc | `wscanf_s_nid_postfix` | </details>
github-actions[bot] commented 2026-10-20 13:47:06 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by github-actions[bot] on 2026-10-20T13:47:06Z.


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 core/libs/prx/libc/CMakeLists.txt
docs/dev/debt/libc.md
PR 4627 core/libs/prx/libc/CMakeLists.txt
PR 5021 core/libs/prx/libc/CMakeLists.txt
docs/dev/debt/libc.md
PR 6044 core/libs/prx/libc/CMakeLists.txt
PR 6469 docs/dev/debt/libc.md
PR 6819 core/libs/prx/libc/CMakeLists.txt
docs/dev/debt/libc.md
PR 6879 core/libs/prx/libc/src/ScanningWide.cpp
docs/dev/debt/libc.md
PR 7081 core/libs/prx/libc/CMakeLists.txt
docs/dev/debt/libc.md
PR 7201 core/libs/prx/libc/CMakeLists.txt
PR 7648 docs/dev/debt/libc.md
PR 7715 core/libs/prx/libc/CMakeLists.txt
docs/dev/debt/libc.md
PR 8150 core/libs/prx/libc/CMakeLists.txt
docs/dev/debt/libc.md
PR 8520 core/libs/prx/libc/CMakeLists.txt
docs/dev/debt/libc.md
PR 8546 docs/dev/debt/libc.md
PR 8550 docs/dev/debt/libc.md
<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6292366776 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/8479#issuecomment-6292366776). Originally posted by [github-actions[bot]](https://github.com/apps/github-actions) on 2026-10-20T13:47:06Z. --- <!-- pr-overlap --> 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](https://www.github.com/boykopovar/AnyPS5/pull/3965) | `core/libs/prx/libc/CMakeLists.txt`<br>`docs/dev/debt/libc.md` | | [PR 4627](https://www.github.com/boykopovar/AnyPS5/pull/4627) | `core/libs/prx/libc/CMakeLists.txt` | | [PR 5021](https://www.github.com/boykopovar/AnyPS5/pull/5021) | `core/libs/prx/libc/CMakeLists.txt`<br>`docs/dev/debt/libc.md` | | [PR 6044](https://www.github.com/boykopovar/AnyPS5/pull/6044) | `core/libs/prx/libc/CMakeLists.txt` | | [PR 6469](https://www.github.com/boykopovar/AnyPS5/pull/6469) | `docs/dev/debt/libc.md` | | [PR 6819](https://www.github.com/boykopovar/AnyPS5/pull/6819) | `core/libs/prx/libc/CMakeLists.txt`<br>`docs/dev/debt/libc.md` | | [PR 6879](https://www.github.com/boykopovar/AnyPS5/pull/6879) | `core/libs/prx/libc/src/ScanningWide.cpp`<br>`docs/dev/debt/libc.md` | | [PR 7081](https://www.github.com/boykopovar/AnyPS5/pull/7081) | `core/libs/prx/libc/CMakeLists.txt`<br>`docs/dev/debt/libc.md` | | [PR 7201](https://www.github.com/boykopovar/AnyPS5/pull/7201) | `core/libs/prx/libc/CMakeLists.txt` | | [PR 7648](https://www.github.com/boykopovar/AnyPS5/pull/7648) | `docs/dev/debt/libc.md` | | [PR 7715](https://www.github.com/boykopovar/AnyPS5/pull/7715) | `core/libs/prx/libc/CMakeLists.txt`<br>`docs/dev/debt/libc.md` | | [PR 8150](https://www.github.com/boykopovar/AnyPS5/pull/8150) | `core/libs/prx/libc/CMakeLists.txt`<br>`docs/dev/debt/libc.md` | | [PR 8520](https://www.github.com/boykopovar/AnyPS5/pull/8520) | `core/libs/prx/libc/CMakeLists.txt`<br>`docs/dev/debt/libc.md` | | [PR 8546](https://www.github.com/boykopovar/AnyPS5/pull/8546) | `docs/dev/debt/libc.md` | | [PR 8550](https://www.github.com/boykopovar/AnyPS5/pull/8550) | `docs/dev/debt/libc.md` |
nikowolf123 commented 2026-10-20 19:04:47 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by nikowolf123 on 2026-10-20T19:04:47Z.


The wide stream scan does not hold the stream lock

vfwscanf_nid_postfix and vfwscanf_s_nid_postfix run Scan() and Release() without the stream lock. Every Peek() is a separate fgetwc_nid_postfix call (L149), and the lookahead goes back through separate ungetc calls (L185). Each of these host calls takes and releases the FILE lock by itself. Guest threads are host threads, so another thread can read the same stream in the middle of a scan.

For example, the stream holds 12 34, thread A calls fwscanf(f, L"%d", &x) and thread B calls fgetwc(f):

  • If B reads after A has taken 1 and 2, B gets the space and A's field goes on with 3 and 4. A returns 1 with x == 1234, a number that is not in the input.
  • If B reads between A's lookahead read of the space and the ungetc in Release(), B gets 3 and the space is then pushed back in front of 4, so the stream is reordered.

With the lock held for the whole call, the only possible results are those of the two calls running one after the other: either B gets 1 and A gets 2, or A gets 12 and B gets the space. Single-threaded use is not affected.

The PR description says that on the console each of these functions "locks the stream and calls the same scanner as swscanf". FreeBSD's vfwscanf_l does the same, wrapping __vfwscanf in FLOCKFILE_CANCELSAFE/FUNLOCKFILE_CANCELSAFE (vfwscanf.c#L431-L434, the revision libc.md already cites). In this repo, putw already wraps its multi-byte write in a guard built on flockfile_nid_postfix (BinaryWordIo.cpp#L19-L25).

I would add the same kind of guard to both entry points, after the null checks:

extern "C" void APS5_VABI flockfile_nid_postfix(FileStream* stream);
extern "C" void APS5_VABI funlockfile_nid_postfix(FileStream* stream);

class StreamLock {
    FileStream* const stream;

public:
    explicit StreamLock(FileStream* value) : stream(value->IsOpen() ? value : nullptr) {
        if (stream != nullptr) flockfile_nid_postfix(stream);
    }
    ~StreamLock() {
        if (stream != nullptr) funlockfile_nid_postfix(stream);
    }
};

int APS5_VABI vfwscanf_nid_postfix(FileStream* stream, const char16_t* format, VaList* args) {
    if (stream == nullptr || format == nullptr) return EOF;
    const StreamLock lock(stream);
    return WideScanner("vfwscanf", false, WideInput(stream), format, args).Run();
}

The IsOpen() check keeps the current result for a closed stream (errno 9 and EOF), because flockfile_nid_postfix goes through GetStreamHandle, which throws on a closed stream. The destructor also releases the lock when Fail() throws. The host stream locks are recursive, so the fgetc and ungetc calls inside still work. If the lock is left out on purpose, please add a sentence to the new libc.md entry saying that the scan is not atomic against other threads using the same stream, while the console's is.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6298908409 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/8479#issuecomment-6298908409). Originally posted by [nikowolf123](https://github.com/nikowolf123) on 2026-10-20T19:04:47Z. --- **The wide stream scan does not hold the stream lock** [`vfwscanf_nid_postfix`](https://github.com/boykopovar/AnyPS5/blob/b5e988263068d380456013268c3fb758901de68b/core/libs/prx/libc/src/ScanningWide.cpp#L505) and [`vfwscanf_s_nid_postfix`](https://github.com/boykopovar/AnyPS5/blob/b5e988263068d380456013268c3fb758901de68b/core/libs/prx/libc/src/ScanningWide.cpp#L510) run `Scan()` and `Release()` without the stream lock. Every `Peek()` is a separate `fgetwc_nid_postfix` call ([L149](https://github.com/boykopovar/AnyPS5/blob/b5e988263068d380456013268c3fb758901de68b/core/libs/prx/libc/src/ScanningWide.cpp#L149)), and the lookahead goes back through separate `ungetc` calls ([L185](https://github.com/boykopovar/AnyPS5/blob/b5e988263068d380456013268c3fb758901de68b/core/libs/prx/libc/src/ScanningWide.cpp#L185)). Each of these host calls takes and releases the `FILE` lock by itself. Guest threads are host threads, so another thread can read the same stream in the middle of a scan. For example, the stream holds `12 34`, thread A calls `fwscanf(f, L"%d", &x)` and thread B calls `fgetwc(f)`: - If B reads after A has taken `1` and `2`, B gets the space and A's field goes on with `3` and `4`. A returns 1 with `x == 1234`, a number that is not in the input. - If B reads between A's lookahead read of the space and the `ungetc` in `Release()`, B gets `3` and the space is then pushed back in front of `4`, so the stream is reordered. With the lock held for the whole call, the only possible results are those of the two calls running one after the other: either B gets `1` and A gets 2, or A gets 12 and B gets the space. Single-threaded use is not affected. The PR description says that on the console each of these functions "locks the stream and calls the same scanner as `swscanf`". FreeBSD's `vfwscanf_l` does the same, wrapping `__vfwscanf` in `FLOCKFILE_CANCELSAFE`/`FUNLOCKFILE_CANCELSAFE` ([vfwscanf.c#L431-L434](https://github.com/freebsd/freebsd-src/blob/4ab2e064d7950be84256d671a7ae93f87cc6aa36/lib/libc/stdio/vfwscanf.c#L431-L434), the revision `libc.md` already cites). In this repo, `putw` already wraps its multi-byte write in a guard built on `flockfile_nid_postfix` ([BinaryWordIo.cpp#L19-L25](https://github.com/boykopovar/AnyPS5/blob/b5e988263068d380456013268c3fb758901de68b/core/libs/prx/libc/src/BinaryWordIo.cpp#L19-L25)). I would add the same kind of guard to both entry points, after the null checks: ```cpp extern "C" void APS5_VABI flockfile_nid_postfix(FileStream* stream); extern "C" void APS5_VABI funlockfile_nid_postfix(FileStream* stream); class StreamLock { FileStream* const stream; public: explicit StreamLock(FileStream* value) : stream(value->IsOpen() ? value : nullptr) { if (stream != nullptr) flockfile_nid_postfix(stream); } ~StreamLock() { if (stream != nullptr) funlockfile_nid_postfix(stream); } }; int APS5_VABI vfwscanf_nid_postfix(FileStream* stream, const char16_t* format, VaList* args) { if (stream == nullptr || format == nullptr) return EOF; const StreamLock lock(stream); return WideScanner("vfwscanf", false, WideInput(stream), format, args).Run(); } ``` The `IsOpen()` check keeps the current result for a closed stream (`errno` 9 and `EOF`), because `flockfile_nid_postfix` goes through `GetStreamHandle`, which throws on a closed stream. The destructor also releases the lock when `Fail()` throws. The host stream locks are recursive, so the `fgetc` and `ungetc` calls inside still work. If the lock is left out on purpose, please add a sentence to the new `libc.md` entry saying that the scan is not atomic against other threads using the same stream, while the console's is.
hypermoist commented 2026-10-20 22:47:17 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by hypermoist on 2026-10-20T22:47:17Z.


Thanks, that's right: the console locks the stream around the whole scan, and this didn't. Fixed in the latest push:

  • vfwscanf and vfwscanf_s (and so fwscanf, wscanf and the _s forms) now hold a LockedStream guard after the null checks. It skips a closed stream, so that case still gives EOF with errno 9, and the destructor unlocks when Fail() throws.
  • CheckLocking in GuestWideStreamScan.cpp holds flockfile on the stream, starts a thread that calls fwscanf, checks the thread is still blocked after 50 ms, then unlocks and checks it reads 12. Without the guard the scan finishes while the lock is held, and the test aborts.

guest_wide_stream_scan passed 5 of 5 runs; guest_wide_conversion and guest_stdio_variadic also pass.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6303490789 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/8479#issuecomment-6303490789). Originally posted by [hypermoist](https://github.com/hypermoist) on 2026-10-20T22:47:17Z. --- Thanks, that's right: the console locks the stream around the whole scan, and this didn't. Fixed in the latest push: - `vfwscanf` and `vfwscanf_s` (and so `fwscanf`, `wscanf` and the `_s` forms) now hold a `LockedStream` guard after the null checks. It skips a closed stream, so that case still gives `EOF` with `errno` 9, and the destructor unlocks when `Fail()` throws. - `CheckLocking` in `GuestWideStreamScan.cpp` holds `flockfile` on the stream, starts a thread that calls `fwscanf`, checks the thread is still blocked after 50 ms, then unlocks and checks it reads `12`. Without the guard the scan finishes while the lock is held, and the test aborts. `guest_wide_stream_scan` passed 5 of 5 runs; `guest_wide_conversion` and `guest_stdio_variadic` also pass.
hypermoist commented 2026-10-20 22:47:55 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by hypermoist on 2026-10-20T22:47:55Z.


Correction to my last comment: the CheckLocking test did not prove anything. I ran it with the guard removed and it still passed, because each inner fgetc takes the host FILE lock itself, so the scanning thread blocks on the held lock either way. I've removed the test in the latest push rather than keep a check that can't fail. The guard itself stays. The interleavings you describe need a second thread to read between two of the scan's own calls, and I don't see a way to force that deterministically in a unit test.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6303501061 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/8479#issuecomment-6303501061). Originally posted by [hypermoist](https://github.com/hypermoist) on 2026-10-20T22:47:55Z. --- Correction to my last comment: the `CheckLocking` test did not prove anything. I ran it with the guard removed and it still passed, because each inner `fgetc` takes the host `FILE` lock itself, so the scanning thread blocks on the held lock either way. I've removed the test in the latest push rather than keep a check that can't fail. The guard itself stays. The interleavings you describe need a second thread to read between two of the scan's own calls, and I don't see a way to force that deterministically in a unit test.
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!8479
No description provided.