From 9d1e14b4e3fd2ab428a73b24fc9e82f6024bf33f Mon Sep 17 00:00:00 2001 From: Kraken <10617703+UnhealthyKraken@users.noreply.github.com> Date: Thu, 22 Oct 2026 20:30:41 +0100 Subject: [PATCH 1/6] fix(libc): retain short wide scanf character fields --- core/libs/prx/libc/src/ScanningWide.cpp | 3 +- core/libs/tests/GuestWideConversion.cpp | 47 +++++++++++++++++++++++-- core/libs/tests/GuestWideStreamScan.cpp | 34 ++++++++++++++++++ docs/dev/debt/libc.md | 2 +- 4 files changed, 82 insertions(+), 4 deletions(-) diff --git a/core/libs/prx/libc/src/ScanningWide.cpp b/core/libs/prx/libc/src/ScanningWide.cpp index 759e475c908a..62cdb1c3f9b2 100644 --- a/core/libs/prx/libc/src/ScanningWide.cpp +++ b/core/libs/prx/libc/src/ScanningWide.cpp @@ -428,7 +428,8 @@ class WideScanner { item.push_back(static_cast(input.Peek())); input.Advance(); } - if (item.size() < count) return Outcome::Matching; + if (item.size() < count && sized && target != nullptr && capacity < count) + Fail("short character field with capacity below width"); } else { const std::size_t limit = directive.width == 0 ? std::numeric_limits::max() : directive.width; while (item.size() < limit && input.Peek() >= 0) { diff --git a/core/libs/tests/GuestWideConversion.cpp b/core/libs/tests/GuestWideConversion.cpp index 1ade68c23f1f..c30588616083 100644 --- a/core/libs/tests/GuestWideConversion.cpp +++ b/core/libs/tests/GuestWideConversion.cpp @@ -131,7 +131,7 @@ static void CheckScan() { char16_t pair[3] = {u'?', u'?', u'?'}; Require(swscanf_s_nid_postfix(u"xyz", u"%2lc", pair, std::size_t{2}) == 1 && pair[0] == u'x' && pair[1] == u'y' && pair[2] == u'?', "%2lc"); Require(swscanf_s_nid_postfix(u"xyz", u"%2lc", pair, std::size_t{1}) == 0, "%2lc too small"); - Require(swscanf_s_nid_postfix(u"x", u"%2lc", pair, std::size_t{2}) == 0, "%2lc short input"); + Require(swscanf_s_nid_postfix(u"x", u"%2lc", pair, std::size_t{2}) == 1 && pair[0] == u'x', "%2lc short input"); Require(swscanf_s_nid_postfix(u"cab12", u"%l[abc]%d", text, std::size_t{6}, &first) == 2 && std::u16string(text) == u"cab" && first == 12, "%l["); Require(swscanf_s_nid_postfix(u"été!", u"%l[^!]", text, std::size_t{6}) == 1 && std::u16string(text) == u"été", "%l[^"); std::int64_t count = -1; @@ -190,7 +190,7 @@ static void CheckUnsizedScan() { Require(swscanf_nid_postfix(u"abc def", u"%s%d", narrow, &first) == 1 && std::strcmp(narrow, "abc") == 0, "swscanf %s"); char16_t pair[3] = {u'?', u'?', u'?'}; Require(swscanf_nid_postfix(u"xyz", u"%2lc", pair) == 1 && pair[0] == u'x' && pair[1] == u'y' && pair[2] == u'?', "swscanf %2lc"); - Require(swscanf_nid_postfix(u"x", u"%2lc", pair) == 0, "swscanf %2lc short input"); + Require(swscanf_nid_postfix(u"x", u"%2lc", pair) == 1 && pair[0] == u'x', "swscanf %2lc short input"); char letter = 0; Require(swscanf_nid_postfix(u" q7", u"%c%d", &letter, &first) == 1 && letter == ' ', "swscanf %c does not skip white space"); Require(swscanf_nid_postfix(u"cab12", u"%l[abc]%d", text, &first) == 2 && std::u16string(text) == u"cab" && first == 12, "swscanf %l["); @@ -227,7 +227,50 @@ static void CheckUnsizedScan() { Require(Throws([&] { swscanf_nid_postfix(u"Ā", u"%s", narrow); }), "swscanf unconvertible narrow %s throws"); } +static void CheckShortCharacterFields() { + const char16_t* inputs[]{u"", u"a", u"ab", u"abc", u"abcd"}; + for (const char16_t* input : inputs) { + const std::size_t length = std::u16string(input).size(); + const std::size_t consumed = length < 3 ? length : 3; + for (bool sized : {false, true}) { + char16_t output[]{0xa55a, 0xa55a, 0xa55a, 0xa55a, 0xa55a}; + int count = -1; + const int result = sized ? swscanf_s_nid_postfix(input, u"%3lc%n", output + 1, 3u, &count) + : swscanf_nid_postfix(input, u"%3lc%n", output + 1, &count); + Require(result == (length == 0 ? EOF : 1), "short wide character field assignment count"); + Require(count == (length == 0 ? -1 : static_cast(consumed)), "short wide character field consumed count"); + for (std::size_t index = 0; index < 5; ++index) { + const char16_t expected = index > 0 && index <= consumed ? input[index - 1] : 0xa55a; + Require(output[index] == expected, "short wide character field exact write extent without terminator"); + } + count = -1; + const int suppressed = sized ? swscanf_s_nid_postfix(input, u"%*3lc%n", &count) + : swscanf_nid_postfix(input, u"%*3lc%n", &count); + Require(suppressed == (length == 0 ? EOF : 0), "suppressed short character field assignment count"); + Require(count == (length == 0 ? -1 : static_cast(consumed)), "suppressed short character field continues to count"); + } + } + char16_t output[]{0xa55a, 0xa55a, 0xa55a}; + int value = -1, count = -1; + Require(swscanf_nid_postfix(u"5 a", u"%d %3lc%n", &value, output, &count) == 2 && value == 5 && count == 3, + "short character field after another assignment"); + Require(output[0] == u'a' && output[1] == 0xa55a && output[2] == 0xa55a, "short field after assignment does not terminate"); + for (unsigned int capacity : {0u, 1u, 2u}) { + char16_t guarded[]{0xa55a, 0xa55a, 0xa55a}; + Require(Throws([&] { swscanf_s_nid_postfix(u"a", u"%3lc", guarded + 1, capacity); }), + "short secure field with capacity below width is explicitly unsupported"); + Require(guarded[0] == 0xa55a && guarded[1] == 0xa55a && guarded[2] == 0xa55a, + "unsupported short secure field leaves output untouched"); + } + char bytes[]{'?', '?', '?', '?'}; + count = -1; + Require(swscanf_nid_postfix(u"ab", u"%3c%n", bytes + 1, &count) == 1 && count == 2, + "short narrow destination follows FreeBSD character conversion"); + Require(bytes[0] == '?' && bytes[1] == 'a' && bytes[2] == 'b' && bytes[3] == '?', "short narrow destination bounds"); +} + int main() { + CheckShortCharacterFields(); CheckStoul(); CheckClassification(); CheckScan(); diff --git a/core/libs/tests/GuestWideStreamScan.cpp b/core/libs/tests/GuestWideStreamScan.cpp index 632a98975934..ac6830ea5005 100644 --- a/core/libs/tests/GuestWideStreamScan.cpp +++ b/core/libs/tests/GuestWideStreamScan.cpp @@ -168,7 +168,41 @@ static void CheckStandardInput() { Close(stream); } +static void CheckShortCharacterFields() { + const char* inputs[]{"", "a", "ab", "abc", "abcd"}; + for (const char* input : inputs) { + const std::size_t length = std::string(input).size(); + const std::size_t consumed = length < 3 ? length : 3; + for (int mode = 0; mode < 4; ++mode) { + FileStream* stream = Open(input); + char16_t output[]{0xa55a, 0xa55a, 0xa55a, 0xa55a, 0xa55a}; + int count = -1; + const int result = mode == 0 ? fwscanf_nid_postfix(stream, u"%3lc%n", output + 1, &count) + : mode == 1 ? ScanStream(stream, u"%3lc%n", output + 1, &count) + : mode == 2 ? fwscanf_s_nid_postfix(stream, u"%3lc%n", output + 1, 3u, &count) + : ScanStreamSized(stream, u"%3lc%n", output + 1, 3u, &count); + Require(result == (length == 0 ? EOF : 1), "short stream character assignment count"); + Require(count == (length == 0 ? -1 : static_cast(consumed)), "short stream character consumed count"); + for (std::size_t index = 0; index < 5; ++index) { + const char16_t expected = index > 0 && index <= consumed ? static_cast(input[index - 1]) : 0xa55a; + Require(output[index] == expected, "short stream character exact write extent without terminator"); + } + Require(fgetwc_nid_postfix(stream) == (length > 3 ? 'd' : -1), "short stream character lookahead"); + Close(stream); + } + } + for (bool sized : {false, true}) { + FileStream* stream = Open("ab"); + int count = -1; + const int result = sized ? ScanStreamSized(stream, u"%*3lc%n", &count) : ScanStream(stream, u"%*3lc%n", &count); + Require(result == 0 && count == 2, "suppressed short stream field continues to count"); + Require(fgetwc_nid_postfix(stream) == -1, "suppressed short stream field consumes input"); + Close(stream); + } +} + int main() { + CheckShortCharacterFields(); CheckConsumption(); CheckWideText(); CheckStandardInput(); diff --git a/docs/dev/debt/libc.md b/docs/dev/debt/libc.md index ce70a5a9588c..7e044c09805c 100644 --- a/docs/dev/debt/libc.md +++ b/docs/dev/debt/libc.md @@ -128,7 +128,7 @@ - [syslog](../../../core/libs/prx/libc/src/Syslog.cpp) (libc) writes each message to `stderr` as `[syslog:] `, with a newline added when missing, instead of sending it to the console's system log. Priority and facility handling follow FreeBSD's `vsyslog`: `%m` expands to the guest errno's message, unknown bits are reported (as priority 0x23, since FreeBSD passes `LOG_ERR|LOG_CONS|LOG_PERROR|LOG_PID` as the priority) and masked, a missing facility becomes `LOG_USER` or the facility set by `openlog`, and errno is preserved. `openlog` applies its identity and `LOG_PID`; a null identity retains the previous one, and a valid nonzero facility replaces the default. `closelog` clears the identity but retains the options and facility, as in FreeBSD 11; when no identity is set, FreeBSD falls back to the program name, which this backend does not emit. `LOG_PERROR` has no extra effect because stderr is the emulated log sink; `LOG_NDELAY`/`LOG_ODELAY`, `LOG_NOWAIT` and `LOG_CONS` have no daemon, child or console backend to control. `setlogmask` follows [FreeBSD 11](https://github.com/freebsd/freebsd-src/blob/8d2708de5d6112a492744d367cfdfb8fd219227a/lib/libc/gen/syslog.c): the process-wide mask defaults to 0xff, zero queries it, and nonzero values replace it; masked messages are skipped before formatting, including the unknown-priority diagnostic. The console's mask behaviour is unverified - [fwscanf, vfwscanf, wscanf, vwscanf and their `_s` forms](../../../core/libs/prx/libc/src/ScanningWide.cpp) (libc) run the `swscanf` scanner on characters read with `fgetwc`, so everything listed for `swscanf` in this file applies to them. As in the console's `libc.prx`, a field takes characters while they can still begin a valid item, so a failed field such as `0x` for `%x` stays consumed, and only the character that ended the scan goes back to the stream. That character is pushed back as its UTF-8 bytes with `ungetc`, because `ungetwc` takes single bytes only; the Windows C runtime guarantees one byte of push-back, so a non-ASCII lookahead can be lost there. A null stream or format returns `EOF`; the console checks them only in the `_s` forms. - [daemon](../../../core/libs/prx/libc/src/Process.cpp) (libc) throws: forking into the background is not supported. PPSA85300 imports it and fails to load without it. VLC 3.0 calls it only when its `daemon` option is set ([libvlc.c](https://github.com/videolan/vlc/blob/6de05adcbaf2e8b85fe86aad4169393098628119/src/libvlc.c#L190-L197)), and the title starts libVLC with `--ignore-config` and without `--daemon` ([player.cc](https://github.com/ZiZc3/VLC/blob/f3253020a64368153233ede29183014c0b6c8979/src/player.cc#L960-L981)) -- [_WStoul](../../../core/libs/prx/libc/src/Strings.cpp), [_Iswctype](../../../core/libs/prx/libc/src/RuneLocale.cpp), [swscanf_s and swscanf](../../../core/libs/prx/libc/src/ScanningWide.cpp) (libc) - `_WStoul` is `wcstoul` and throws for a base other than 0 or 2 to 36. `_Iswctype` takes the class numbers of Dinkumware's [xwcc.h](https://github.com/superna9999/ti-cgt6x/blob/cgt6x_7_3_4/include/xwcc.h) (1 `alnum` to 11 `xdigit`, 12 `blank`) and throws for any other value, including the FreeBSD masks `wctype` returns; the numbering is not confirmed on the console's `libc.prx`. `swscanf_s` follows C11 7.29.2.2 and K.3.9.1.5, but reads the size argument as `unsigned int` like `sscanf_s` where K.3.9.1.5 specifies `rsize_t`; the console's width is unverified. A runtime-constraint violation (null buffer, format or receiving pointer) only returns `EOF`, and a too small array gets a null first element as in `sscanf_s`. A literal at end of input returns the number of assignments, including zero, rather than `EOF` as FreeBSD does, as reported from the console scanner in #8479; `%%` at end of input still returns `EOF`. A literal mismatch is a matching failure, where FreeBSD's `vfwscanf` reports an input failure. An incomplete floating exponent such as `1e+` converts its mantissa, as reported from the console scanner in #8479; other incomplete exponent forms are not separately verified. A short `%c` field is currently a matching failure without storing its available prefix, while #9321 reports that the console stores a short `%3lc` field; its return count, partial-output extent and precedence are unverified. An integer field that is only a prefix, such as `0x`, does not convert; FreeBSD's `vfwscanf` pushes back the `x` and converts the `0`. Integers out of range are truncated from `strtoll`/`strtoull`. `%p` follows FreeBSD 11's [vfwscanf.c](https://github.com/freebsd/freebsd-src/blob/4ab2e064d7950be84256d671a7ae93f87cc6aa36/lib/libc/stdio/vfwscanf.c#L610-L615): a hexadecimal integer with an optional `0x`, converted as unsigned and stored as a pointer; a length modifier on it throws, where FreeBSD ignores it. It throws for `%S`, `%C`, unknown length modifiers, scanset ranges, `%n` with suppression or width, and narrow `%s`, `%c` or `%[` input that the C-locale `wcrtomb` cannot convert. `swscanf` runs the same scanner without the size arguments of `%s`, `%c` and `%[`, as FreeBSD's [swscanf.c](https://github.com/freebsd/freebsd-src/blob/d106002967eff712894ab8b5337bbbff106ce5e0/lib/libc/stdio/swscanf.c) passes its arguments to `vswscanf`, so everything above except the sizes applies to it; it also keeps the null buffer, format and receiving pointer checks returning `EOF`, where C leaves those undefined. Neither variant is confirmed on a PS5 title +- [_WStoul](../../../core/libs/prx/libc/src/Strings.cpp), [_Iswctype](../../../core/libs/prx/libc/src/RuneLocale.cpp), [swscanf_s and swscanf](../../../core/libs/prx/libc/src/ScanningWide.cpp) (libc) - `_WStoul` is `wcstoul` and throws for a base other than 0 or 2 to 36. `_Iswctype` takes the class numbers of Dinkumware's [xwcc.h](https://github.com/superna9999/ti-cgt6x/blob/cgt6x_7_3_4/include/xwcc.h) (1 `alnum` to 11 `xdigit`, 12 `blank`) and throws for any other value, including the FreeBSD masks `wctype` returns; the numbering is not confirmed on the console's `libc.prx`. `swscanf_s` follows C11 7.29.2.2 and K.3.9.1.5, but reads the size argument as `unsigned int` like `sscanf_s` where K.3.9.1.5 specifies `rsize_t`; the console's width is unverified. A runtime-constraint violation (null buffer, format or receiving pointer) only returns `EOF`, and a too small array gets a null first element as in `sscanf_s`. A literal at end of input returns the number of assignments, including zero, rather than `EOF` as FreeBSD does, as reported from the console scanner in #8479; `%%` at end of input still returns `EOF`. A literal mismatch is a matching failure, where FreeBSD's `vfwscanf` reports an input failure. An incomplete floating exponent such as `1e+` converts its mantissa, as reported from the console scanner in #8479; other incomplete exponent forms are not separately verified. Short `%c` fields follow FreeBSD 11.4 [vfwscanf.c](https://github.com/freebsd/freebsd-src/blob/d106002967eff712894ab8b5337bbbff106ce5e0/lib/libc/stdio/vfwscanf.c): a nonempty prefix counts as a conversion, without a terminator. The short `%lc` assignment, suppression and `%n` results were reported from native calls into one bundled `libc.prx` build in [#9321](https://github.com/boykopovar/AnyPS5/issues/9321#issuecomment-6342217421), not measured on a console here; other versions and narrow destinations are unverified. A short sized `%c` field with a nonnull destination capacity below its requested width throws after consuming the prefix and before writing, because that capacity precedence is unverified. An integer field that is only a prefix, such as `0x`, does not convert; FreeBSD's `vfwscanf` pushes back the `x` and converts the `0`. Integers out of range are truncated from `strtoll`/`strtoull`. `%p` follows FreeBSD 11's [vfwscanf.c](https://github.com/freebsd/freebsd-src/blob/4ab2e064d7950be84256d671a7ae93f87cc6aa36/lib/libc/stdio/vfwscanf.c#L610-L615): a hexadecimal integer with an optional `0x`, converted as unsigned and stored as a pointer; a length modifier on it throws, where FreeBSD ignores it. It throws for `%S`, `%C`, unknown length modifiers, scanset ranges, `%n` with suppression or width, and narrow `%s`, `%c` or `%[` input that the C-locale `wcrtomb` cannot convert. `swscanf` runs the same scanner without the size arguments of `%s`, `%c` and `%[`, as FreeBSD's [swscanf.c](https://github.com/freebsd/freebsd-src/blob/d106002967eff712894ab8b5337bbbff106ce5e0/lib/libc/stdio/swscanf.c) passes its arguments to `vswscanf`, so everything above except the sizes applies to it; it also keeps the null buffer, format and receiving pointer checks returning `EOF`, where C leaves those undefined. Neither variant is confirmed on a PS5 title - [flock](../../../core/libs/prx/libc/src/FileLock.cpp) (libkernel) on Windows shares one lock state between duplicated descriptors, following FreeBSD 11.4's [flock(2)](https://github.com/freebsd/freebsd-src/blob/releng/11.4/lib/libc/sys/flock.2). Independent opens have separate states; closing a descriptor or reopening a stream drops only its reference. Lock transitions on one open file description are serialized. The lock is a host [LockFileEx](https://learn.microsoft.com/en-us/windows/win32/api/fileapi/nf-fileapi-lockfileex) lock on one byte at offset 2^63 - 2, which no file reaches, so it excludes other `flock` calls and leaves reads, writes and mapped views alone, like FreeBSD's advisory locks. Console behavior, signal interruption and concurrent closure of a descriptor while its lock call is pending are unverified. - [std::_Throw_Cpp_error](../../../core/libs/prx/libc/src/exception/StandardExceptions.cpp) (libc) follows Dinkumware `thread0.cpp` ([VC12](https://github.com/ganboing/crt.vc12/blob/f1d47126/src/thread0.cpp), [STL](https://github.com/microsoft/STL/blob/da52dc0f/stl/src/thread0.cpp)), without console verification. Codes outside 0..6 throw `std::out_of_range`, the deleting destructor throws, and STREAMS errnos absent from FreeBSD 11.4 [`errno.h`](https://github.com/freebsd/freebsd-src/blob/releng/11.4/sys/sys/errno.h) (`ENODATA`, `ENOSR`, `ENOSTR`, `ETIME`) are omitted. Codes 0..6 throw a 32-byte [VC12 `system_error`](https://github.com/ganboing/crt.vc12/blob/f1d47126/src/system_error) with FreeBSD errnos (16, 22, 3, 12, 1, 11, 35), a static `generic_category` (not `_Generic_object`), and unverified 40-byte [GuestLocale::String](../../../core/libs/prx/libc/include/GuestLocale.hpp) messages from [`syserror.cpp`](https://github.com/ganboing/crt.vc12/blob/f1d47126/src/syserror.cpp) - [Call tracing](../../../core/libs/prx/libc/src/specifics/x86_64/CallTrace.cpp) (`--trace-imports`, `APS5_TRACE_CALLS`) is missing on macOS and records only the entry of a call, not its arguments or result. One process-wide ring keeps the last 65536 calls, so a busy thread pushes the others out; the reporting thread's own calls are listed only while they are still in that ring. Calls are counted per traced target; an import bound to a target already traced under another name is counted under the first name. The thunk saves the integer argument registers and `xmm0` to `xmm7`; wider vector arguments survive only while the recording code avoids AVX, which is untested. Linux has no crash report, so it prints the trace only on `SIGABRT` (guest or host `abort()`), not on `SIGSEGV`; a caller in a module loaded after the last traced import prints without a module name, and a thread aborting while another writes the report waits for it at most 2 seconds From 26db0f2a6942b5aecca5111c42a5188073c93960 Mon Sep 17 00:00:00 2001 From: Kraken <10617703+UnhealthyKraken@users.noreply.github.com> Date: Thu, 22 Oct 2026 20:32:55 +0100 Subject: [PATCH 2/6] docs(libc): keep short-field evidence limits concise --- docs/dev/debt/libc.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/dev/debt/libc.md b/docs/dev/debt/libc.md index 7e044c09805c..c5460ee9311c 100644 --- a/docs/dev/debt/libc.md +++ b/docs/dev/debt/libc.md @@ -128,7 +128,7 @@ - [syslog](../../../core/libs/prx/libc/src/Syslog.cpp) (libc) writes each message to `stderr` as `[syslog:] `, with a newline added when missing, instead of sending it to the console's system log. Priority and facility handling follow FreeBSD's `vsyslog`: `%m` expands to the guest errno's message, unknown bits are reported (as priority 0x23, since FreeBSD passes `LOG_ERR|LOG_CONS|LOG_PERROR|LOG_PID` as the priority) and masked, a missing facility becomes `LOG_USER` or the facility set by `openlog`, and errno is preserved. `openlog` applies its identity and `LOG_PID`; a null identity retains the previous one, and a valid nonzero facility replaces the default. `closelog` clears the identity but retains the options and facility, as in FreeBSD 11; when no identity is set, FreeBSD falls back to the program name, which this backend does not emit. `LOG_PERROR` has no extra effect because stderr is the emulated log sink; `LOG_NDELAY`/`LOG_ODELAY`, `LOG_NOWAIT` and `LOG_CONS` have no daemon, child or console backend to control. `setlogmask` follows [FreeBSD 11](https://github.com/freebsd/freebsd-src/blob/8d2708de5d6112a492744d367cfdfb8fd219227a/lib/libc/gen/syslog.c): the process-wide mask defaults to 0xff, zero queries it, and nonzero values replace it; masked messages are skipped before formatting, including the unknown-priority diagnostic. The console's mask behaviour is unverified - [fwscanf, vfwscanf, wscanf, vwscanf and their `_s` forms](../../../core/libs/prx/libc/src/ScanningWide.cpp) (libc) run the `swscanf` scanner on characters read with `fgetwc`, so everything listed for `swscanf` in this file applies to them. As in the console's `libc.prx`, a field takes characters while they can still begin a valid item, so a failed field such as `0x` for `%x` stays consumed, and only the character that ended the scan goes back to the stream. That character is pushed back as its UTF-8 bytes with `ungetc`, because `ungetwc` takes single bytes only; the Windows C runtime guarantees one byte of push-back, so a non-ASCII lookahead can be lost there. A null stream or format returns `EOF`; the console checks them only in the `_s` forms. - [daemon](../../../core/libs/prx/libc/src/Process.cpp) (libc) throws: forking into the background is not supported. PPSA85300 imports it and fails to load without it. VLC 3.0 calls it only when its `daemon` option is set ([libvlc.c](https://github.com/videolan/vlc/blob/6de05adcbaf2e8b85fe86aad4169393098628119/src/libvlc.c#L190-L197)), and the title starts libVLC with `--ignore-config` and without `--daemon` ([player.cc](https://github.com/ZiZc3/VLC/blob/f3253020a64368153233ede29183014c0b6c8979/src/player.cc#L960-L981)) -- [_WStoul](../../../core/libs/prx/libc/src/Strings.cpp), [_Iswctype](../../../core/libs/prx/libc/src/RuneLocale.cpp), [swscanf_s and swscanf](../../../core/libs/prx/libc/src/ScanningWide.cpp) (libc) - `_WStoul` is `wcstoul` and throws for a base other than 0 or 2 to 36. `_Iswctype` takes the class numbers of Dinkumware's [xwcc.h](https://github.com/superna9999/ti-cgt6x/blob/cgt6x_7_3_4/include/xwcc.h) (1 `alnum` to 11 `xdigit`, 12 `blank`) and throws for any other value, including the FreeBSD masks `wctype` returns; the numbering is not confirmed on the console's `libc.prx`. `swscanf_s` follows C11 7.29.2.2 and K.3.9.1.5, but reads the size argument as `unsigned int` like `sscanf_s` where K.3.9.1.5 specifies `rsize_t`; the console's width is unverified. A runtime-constraint violation (null buffer, format or receiving pointer) only returns `EOF`, and a too small array gets a null first element as in `sscanf_s`. A literal at end of input returns the number of assignments, including zero, rather than `EOF` as FreeBSD does, as reported from the console scanner in #8479; `%%` at end of input still returns `EOF`. A literal mismatch is a matching failure, where FreeBSD's `vfwscanf` reports an input failure. An incomplete floating exponent such as `1e+` converts its mantissa, as reported from the console scanner in #8479; other incomplete exponent forms are not separately verified. Short `%c` fields follow FreeBSD 11.4 [vfwscanf.c](https://github.com/freebsd/freebsd-src/blob/d106002967eff712894ab8b5337bbbff106ce5e0/lib/libc/stdio/vfwscanf.c): a nonempty prefix counts as a conversion, without a terminator. The short `%lc` assignment, suppression and `%n` results were reported from native calls into one bundled `libc.prx` build in [#9321](https://github.com/boykopovar/AnyPS5/issues/9321#issuecomment-6342217421), not measured on a console here; other versions and narrow destinations are unverified. A short sized `%c` field with a nonnull destination capacity below its requested width throws after consuming the prefix and before writing, because that capacity precedence is unverified. An integer field that is only a prefix, such as `0x`, does not convert; FreeBSD's `vfwscanf` pushes back the `x` and converts the `0`. Integers out of range are truncated from `strtoll`/`strtoull`. `%p` follows FreeBSD 11's [vfwscanf.c](https://github.com/freebsd/freebsd-src/blob/4ab2e064d7950be84256d671a7ae93f87cc6aa36/lib/libc/stdio/vfwscanf.c#L610-L615): a hexadecimal integer with an optional `0x`, converted as unsigned and stored as a pointer; a length modifier on it throws, where FreeBSD ignores it. It throws for `%S`, `%C`, unknown length modifiers, scanset ranges, `%n` with suppression or width, and narrow `%s`, `%c` or `%[` input that the C-locale `wcrtomb` cannot convert. `swscanf` runs the same scanner without the size arguments of `%s`, `%c` and `%[`, as FreeBSD's [swscanf.c](https://github.com/freebsd/freebsd-src/blob/d106002967eff712894ab8b5337bbbff106ce5e0/lib/libc/stdio/swscanf.c) passes its arguments to `vswscanf`, so everything above except the sizes applies to it; it also keeps the null buffer, format and receiving pointer checks returning `EOF`, where C leaves those undefined. Neither variant is confirmed on a PS5 title +- [_WStoul](../../../core/libs/prx/libc/src/Strings.cpp), [_Iswctype](../../../core/libs/prx/libc/src/RuneLocale.cpp), [swscanf_s and swscanf](../../../core/libs/prx/libc/src/ScanningWide.cpp) (libc) - `_WStoul` is `wcstoul` and throws for a base other than 0 or 2 to 36. `_Iswctype` takes the class numbers of Dinkumware's [xwcc.h](https://github.com/superna9999/ti-cgt6x/blob/cgt6x_7_3_4/include/xwcc.h) (1 `alnum` to 11 `xdigit`, 12 `blank`) and throws for any other value, including the FreeBSD masks `wctype` returns; the numbering is not confirmed on the console's `libc.prx`. `swscanf_s` follows C11 7.29.2.2 and K.3.9.1.5, but reads the size argument as `unsigned int` like `sscanf_s` where K.3.9.1.5 specifies `rsize_t`; the console's width is unverified. A runtime-constraint violation (null buffer, format or receiving pointer) only returns `EOF`, and a too small array gets a null first element as in `sscanf_s`. A literal at end of input returns the number of assignments, including zero, rather than `EOF` as FreeBSD does, as reported from the console scanner in #8479; `%%` at end of input still returns `EOF`. A literal mismatch is a matching failure, where FreeBSD's `vfwscanf` reports an input failure. An incomplete floating exponent such as `1e+` converts its mantissa, as reported from the console scanner in #8479; other incomplete exponent forms are not separately verified. Short `%c` assignments follow FreeBSD 11.4; only `%lc` buffer results have contributor-reported native measurements of one bundled build in [#9321](https://github.com/boykopovar/AnyPS5/issues/9321#issuecomment-6342217421), not console or other-version verification. Short sized fields with a nonnull destination capacity below the requested width throw after consuming input but before writing; that precedence is unverified. An integer field that is only a prefix, such as `0x`, does not convert; FreeBSD's `vfwscanf` pushes back the `x` and converts the `0`. Integers out of range are truncated from `strtoll`/`strtoull`. `%p` follows FreeBSD 11's [vfwscanf.c](https://github.com/freebsd/freebsd-src/blob/4ab2e064d7950be84256d671a7ae93f87cc6aa36/lib/libc/stdio/vfwscanf.c#L610-L615): a hexadecimal integer with an optional `0x`, converted as unsigned and stored as a pointer; a length modifier on it throws, where FreeBSD ignores it. It throws for `%S`, `%C`, unknown length modifiers, scanset ranges, `%n` with suppression or width, and narrow `%s`, `%c` or `%[` input that the C-locale `wcrtomb` cannot convert. `swscanf` runs the same scanner without the size arguments of `%s`, `%c` and `%[`, as FreeBSD's [swscanf.c](https://github.com/freebsd/freebsd-src/blob/d106002967eff712894ab8b5337bbbff106ce5e0/lib/libc/stdio/swscanf.c) passes its arguments to `vswscanf`, so everything above except the sizes applies to it; it also keeps the null buffer, format and receiving pointer checks returning `EOF`, where C leaves those undefined. Neither variant is confirmed on a PS5 title - [flock](../../../core/libs/prx/libc/src/FileLock.cpp) (libkernel) on Windows shares one lock state between duplicated descriptors, following FreeBSD 11.4's [flock(2)](https://github.com/freebsd/freebsd-src/blob/releng/11.4/lib/libc/sys/flock.2). Independent opens have separate states; closing a descriptor or reopening a stream drops only its reference. Lock transitions on one open file description are serialized. The lock is a host [LockFileEx](https://learn.microsoft.com/en-us/windows/win32/api/fileapi/nf-fileapi-lockfileex) lock on one byte at offset 2^63 - 2, which no file reaches, so it excludes other `flock` calls and leaves reads, writes and mapped views alone, like FreeBSD's advisory locks. Console behavior, signal interruption and concurrent closure of a descriptor while its lock call is pending are unverified. - [std::_Throw_Cpp_error](../../../core/libs/prx/libc/src/exception/StandardExceptions.cpp) (libc) follows Dinkumware `thread0.cpp` ([VC12](https://github.com/ganboing/crt.vc12/blob/f1d47126/src/thread0.cpp), [STL](https://github.com/microsoft/STL/blob/da52dc0f/stl/src/thread0.cpp)), without console verification. Codes outside 0..6 throw `std::out_of_range`, the deleting destructor throws, and STREAMS errnos absent from FreeBSD 11.4 [`errno.h`](https://github.com/freebsd/freebsd-src/blob/releng/11.4/sys/sys/errno.h) (`ENODATA`, `ENOSR`, `ENOSTR`, `ETIME`) are omitted. Codes 0..6 throw a 32-byte [VC12 `system_error`](https://github.com/ganboing/crt.vc12/blob/f1d47126/src/system_error) with FreeBSD errnos (16, 22, 3, 12, 1, 11, 35), a static `generic_category` (not `_Generic_object`), and unverified 40-byte [GuestLocale::String](../../../core/libs/prx/libc/include/GuestLocale.hpp) messages from [`syserror.cpp`](https://github.com/ganboing/crt.vc12/blob/f1d47126/src/syserror.cpp) - [Call tracing](../../../core/libs/prx/libc/src/specifics/x86_64/CallTrace.cpp) (`--trace-imports`, `APS5_TRACE_CALLS`) is missing on macOS and records only the entry of a call, not its arguments or result. One process-wide ring keeps the last 65536 calls, so a busy thread pushes the others out; the reporting thread's own calls are listed only while they are still in that ring. Calls are counted per traced target; an import bound to a target already traced under another name is counted under the first name. The thunk saves the integer argument registers and `xmm0` to `xmm7`; wider vector arguments survive only while the recording code avoids AVX, which is untested. Linux has no crash report, so it prints the trace only on `SIGABRT` (guest or host `abort()`), not on `SIGSEGV`; a caller in a module loaded after the last traced import prints without a module name, and a thread aborting while another writes the report waits for it at most 2 seconds From c3eb4c2af84944d6101ff6ea1cdb796d8f8e637f Mon Sep 17 00:00:00 2001 From: Blazing Cook <324818824+cookie42073-cmd@users.noreply.github.com> Date: Fri, 23 Oct 2026 14:24:00 +0100 Subject: [PATCH 3/6] test(libc): cover positive secure wide character capacities --- core/libs/tests/GuestWideConversion.cpp | 51 ++++++++++++++++++++++++- core/libs/tests/GuestWideStreamScan.cpp | 43 +++++++++++++++++++++ 2 files changed, 92 insertions(+), 2 deletions(-) diff --git a/core/libs/tests/GuestWideConversion.cpp b/core/libs/tests/GuestWideConversion.cpp index c7a0689b9f99..18af27dc469d 100644 --- a/core/libs/tests/GuestWideConversion.cpp +++ b/core/libs/tests/GuestWideConversion.cpp @@ -11,6 +11,7 @@ extern "C" { std::uint64_t APS5_VABI _WStoul_nid_postfix(const char16_t*, char16_t**, int); int APS5_VABI _Iswctype_nid_postfix(std::uint32_t, std::uint64_t); +int* APS5_VABI __error_nid_postfix(); int APS5_VABI swscanf_nid_postfix(const char16_t*, const char16_t*, ...); int APS5_VABI swscanf_s_nid_postfix(const char16_t*, const char16_t*, ...); int APS5_VABI iswalnum_nid_postfix(std::uint32_t); @@ -130,7 +131,7 @@ static void CheckScan() { Require(swscanf_s_nid_postfix(u"abc def", u"%s", narrow, std::size_t{4}) == 1 && std::strcmp(narrow, "abc") == 0, "%s"); char16_t pair[3] = {u'?', u'?', u'?'}; Require(swscanf_s_nid_postfix(u"xyz", u"%2lc", pair, std::size_t{2}) == 1 && pair[0] == u'x' && pair[1] == u'y' && pair[2] == u'?', "%2lc"); - Require(swscanf_s_nid_postfix(u"xyz", u"%2lc", pair, std::size_t{1}) == 0, "%2lc too small"); + Require(swscanf_s_nid_postfix(u"xyz", u"%2lc", pair, std::size_t{1}) == 1 && pair[0] == u'x', "%2lc limited by capacity"); Require(swscanf_s_nid_postfix(u"x", u"%2lc", pair, std::size_t{2}) == 1 && pair[0] == u'x', "%2lc short input"); Require(swscanf_s_nid_postfix(u"cab12", u"%l[abc]%d", text, std::size_t{6}, &first) == 2 && std::u16string(text) == u"cab" && first == 12, "%l["); Require(swscanf_s_nid_postfix(u"été!", u"%l[^!]", text, std::size_t{6}) == 1 && std::u16string(text) == u"été", "%l[^"); @@ -255,7 +256,7 @@ static void CheckShortCharacterFields() { Require(swscanf_nid_postfix(u"5 a", u"%d %3lc%n", &value, output, &count) == 2 && value == 5 && count == 3, "short character field after another assignment"); Require(output[0] == u'a' && output[1] == 0xa55a && output[2] == 0xa55a, "short field after assignment does not terminate"); - for (unsigned int capacity : {0u, 1u, 2u}) { + for (unsigned int capacity : {0u}) { char16_t guarded[]{0xa55a, 0xa55a, 0xa55a}; Require(Throws([&] { swscanf_s_nid_postfix(u"a", u"%3lc", guarded + 1, capacity); }), "short secure field with capacity below width is explicitly unsupported"); @@ -269,7 +270,53 @@ static void CheckShortCharacterFields() { Require(bytes[0] == '?' && bytes[1] == 'a' && bytes[2] == 'b' && bytes[3] == '?', "short narrow destination bounds"); } +static void CheckSecureCharacterCapacities() { + const char16_t* inputs[]{u"", u"a", u"ab", u"abc", u"abcd"}; + for (const char16_t* input : inputs) { + const std::size_t length = std::u16string(input).size(); + for (unsigned int capacity : {1u, 2u, 3u, 4u}) { + const std::size_t limit = capacity < 3 ? capacity : 3; + const std::size_t consumed = length < limit ? length : limit; + char16_t output[]{0xa55a, 0xa55a, 0xa55a, 0xa55a, 0xa55a, 0xa55a}; + char16_t next = 0xa55a; + int count = -1; + int result = 0; + *__error_nid_postfix() = 0; + if (Throws([&] { result = swscanf_s_nid_postfix(input, u"%3lc%n%lc", output + 1, capacity, &count, &next, 1u); })) { + Require(false, "positive secure character capacity does not throw"); + continue; + } + const int expected = length == 0 ? EOF : length > consumed ? 2 : 1; + Require(result == expected, "positive secure character capacity assignment count"); + Require(count == (length == 0 ? -1 : static_cast(consumed)), "positive secure character capacity consumed count"); + Require(next == (length > consumed ? input[consumed] : 0xa55a), "the next conversion sees the capacity-limited remainder"); + Require(*__error_nid_postfix() == 0, "positive secure character capacity preserves errno"); + for (std::size_t index = 0; index < 6; ++index) { + const char16_t expectedUnit = index > 0 && index <= consumed ? input[index - 1] : 0xa55a; + Require(output[index] == expectedUnit, "positive secure character capacity exact writes without terminator"); + } + } + } + char16_t output[]{0xa55a, 0xa55a, 0xa55a, 0xa55a}; + char16_t next = 0xa55a; + int value = -1, count = -1; + Require(swscanf_s_nid_postfix(u"7 abcZ", u"%d %3lc%n%lc", &value, output + 1, 2u, &count, &next, 1u) == 3 + && value == 7 && count == 4 && next == u'c', "positive secure character capacity after another assignment"); + Require(output[0] == 0xa55a && output[1] == u'a' && output[2] == u'b' && output[3] == 0xa55a, + "positive secure character capacity after another assignment preserves bounds"); + count = -1; + next = 0xa55a; + Require(swscanf_s_nid_postfix(u"abcd", u"%*3lc%n%lc", &count, &next, 1u) == 1 && count == 3 && next == u'd', + "suppressed character fields keep width and consume no capacity argument"); + char bytes[]{'?', '?', '?', '?', '?'}; + Require(Throws([&] { swscanf_s_nid_postfix(u"a", u"%3c", bytes + 1, 2u); }), + "undersized narrow character capacity remains explicitly unsupported"); + Require(bytes[0] == '?' && bytes[1] == '?' && bytes[2] == '?' && bytes[3] == '?' && bytes[4] == '?', + "unsupported narrow character capacity leaves the destination unchanged"); +} + int main() { + CheckSecureCharacterCapacities(); CheckShortCharacterFields(); CheckStoul(); CheckClassification(); diff --git a/core/libs/tests/GuestWideStreamScan.cpp b/core/libs/tests/GuestWideStreamScan.cpp index ac6830ea5005..071fe3b57474 100644 --- a/core/libs/tests/GuestWideStreamScan.cpp +++ b/core/libs/tests/GuestWideStreamScan.cpp @@ -7,6 +7,7 @@ #include extern "C" { +int* APS5_VABI __error_nid_postfix(); extern FileStream* __stdinp_nid_postfix; int APS5_VABI vfwscanf_nid_postfix(FileStream*, const char16_t*, void*); int APS5_VABI vfwscanf_s_nid_postfix(FileStream*, const char16_t*, void*); @@ -201,7 +202,49 @@ static void CheckShortCharacterFields() { } } +static void CheckSecureCharacterCapacities() { + const char* inputs[]{"", "a", "ab", "abc", "abcd"}; + for (const char* input : inputs) { + const std::size_t length = std::string(input).size(); + for (unsigned int capacity : {1u, 2u, 3u, 4u}) { + const std::size_t limit = capacity < 3 ? capacity : 3; + const std::size_t consumed = length < limit ? length : limit; + for (bool indirect : {false, true}) { + FileStream* stream = Open(input); + char16_t output[]{0xa55a, 0xa55a, 0xa55a, 0xa55a, 0xa55a, 0xa55a}; + int count = -1; + *__error_nid_postfix() = 0; + const int result = indirect ? ScanStreamSized(stream, u"%3lc%n", output + 1, capacity, &count) + : fwscanf_s_nid_postfix(stream, u"%3lc%n", output + 1, capacity, &count); + Require(result == (length == 0 ? EOF : 1), "positive secure stream character capacity assignment count"); + Require(count == (length == 0 ? -1 : static_cast(consumed)), "positive secure stream character capacity consumed count"); + Require(*__error_nid_postfix() == 0, "positive secure stream character capacity preserves errno"); + for (std::size_t index = 0; index < 6; ++index) { + const char16_t expectedUnit = index > 0 && index <= consumed ? static_cast(input[index - 1]) : 0xa55a; + Require(output[index] == expectedUnit, "positive secure stream character capacity exact writes without terminator"); + } + Require(fgetwc_nid_postfix(stream) == (length > consumed ? input[consumed] : -1), + "positive secure stream character capacity leaves lookahead"); + Close(stream); + } + } + } + for (bool indirect : {false, true}) { + FileStream* stream = Open("7 abcZ"); + char16_t output[]{0xa55a, 0xa55a, 0xa55a, 0xa55a}; + int value = -1, count = -1; + const int result = indirect ? ScanStreamSized(stream, u"%d %3lc%n", &value, output + 1, 2u, &count) + : fwscanf_s_nid_postfix(stream, u"%d %3lc%n", &value, output + 1, 2u, &count); + Require(result == 2 && value == 7 && count == 4, "positive secure stream capacity after another assignment"); + Require(output[0] == 0xa55a && output[1] == u'a' && output[2] == u'b' && output[3] == 0xa55a, + "positive secure stream capacity after another assignment preserves bounds"); + Require(fgetwc_nid_postfix(stream) == u'c', "positive secure stream capacity after another assignment leaves lookahead"); + Close(stream); + } +} + int main() { + CheckSecureCharacterCapacities(); CheckShortCharacterFields(); CheckConsumption(); CheckWideText(); From 12446569274e168eb8e14d2bc86205b7e2d02492 Mon Sep 17 00:00:00 2001 From: Blazing Cook <324818824+cookie42073-cmd@users.noreply.github.com> Date: Fri, 23 Oct 2026 14:29:54 +0100 Subject: [PATCH 4/6] fix(libc): bound secure wide character fields by positive capacity --- core/libs/prx/libc/src/ScanningWide.cpp | 3 ++- docs/dev/debt/libc.md | 2 +- 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/core/libs/prx/libc/src/ScanningWide.cpp b/core/libs/prx/libc/src/ScanningWide.cpp index 62cdb1c3f9b2..be7a3795ced5 100644 --- a/core/libs/prx/libc/src/ScanningWide.cpp +++ b/core/libs/prx/libc/src/ScanningWide.cpp @@ -423,7 +423,8 @@ class WideScanner { if (input.Peek() < 0) return Outcome::Input; std::u16string item; if (directive.conversion == u'c') { - const std::size_t count = directive.width == 0 ? 1 : directive.width; + std::size_t count = directive.width == 0 ? 1 : directive.width; + if (sized && target != nullptr && directive.length == "l" && capacity != 0 && capacity < count) count = capacity; while (item.size() < count && input.Peek() >= 0) { item.push_back(static_cast(input.Peek())); input.Advance(); diff --git a/docs/dev/debt/libc.md b/docs/dev/debt/libc.md index 4b5f2e0e0910..d6f9129d2d22 100644 --- a/docs/dev/debt/libc.md +++ b/docs/dev/debt/libc.md @@ -130,7 +130,7 @@ - [syslog](../../../core/libs/prx/libc/src/Syslog.cpp) (libc) writes each message to `stderr` as `[syslog:] `, with a newline added when missing, instead of sending it to the console's system log. Priority and facility handling follow FreeBSD's `vsyslog`: `%m` expands to the guest errno's message, unknown bits are reported (as priority 0x23, since FreeBSD passes `LOG_ERR|LOG_CONS|LOG_PERROR|LOG_PID` as the priority) and masked, a missing facility becomes `LOG_USER` or the facility set by `openlog`, and errno is preserved. `openlog` applies its identity and `LOG_PID`; a null identity retains the previous one, and a valid nonzero facility replaces the default. `closelog` clears the identity but retains the options and facility, as in FreeBSD 11; when no identity is set, FreeBSD falls back to the program name, which this backend does not emit. `LOG_PERROR` has no extra effect because stderr is the emulated log sink; `LOG_NDELAY`/`LOG_ODELAY`, `LOG_NOWAIT` and `LOG_CONS` have no daemon, child or console backend to control. `setlogmask` follows [FreeBSD 11](https://github.com/freebsd/freebsd-src/blob/8d2708de5d6112a492744d367cfdfb8fd219227a/lib/libc/gen/syslog.c): the process-wide mask defaults to 0xff, zero queries it, and nonzero values replace it; masked messages are skipped before formatting, including the unknown-priority diagnostic. The console's mask behaviour is unverified - [fwscanf, vfwscanf, wscanf, vwscanf and their `_s` forms](../../../core/libs/prx/libc/src/ScanningWide.cpp) (libc) run the `swscanf` scanner on characters read with `fgetwc`, so everything listed for `swscanf` in this file applies to them. As in the console's `libc.prx`, a field takes characters while they can still begin a valid item, so a failed field such as `0x` for `%x` stays consumed, and only the character that ended the scan goes back to the stream. That character is pushed back as its UTF-8 bytes with `ungetc`, because `ungetwc` takes single bytes only; the Windows C runtime guarantees one byte of push-back, so a non-ASCII lookahead can be lost there. A null stream or format returns `EOF`; the console checks them only in the `_s` forms. - [daemon](../../../core/libs/prx/libc/src/Process.cpp) (libc) throws: forking into the background is not supported. PPSA85300 imports it and fails to load without it. VLC 3.0 calls it only when its `daemon` option is set ([libvlc.c](https://github.com/videolan/vlc/blob/6de05adcbaf2e8b85fe86aad4169393098628119/src/libvlc.c#L190-L197)), and the title starts libVLC with `--ignore-config` and without `--daemon` ([player.cc](https://github.com/ZiZc3/VLC/blob/f3253020a64368153233ede29183014c0b6c8979/src/player.cc#L960-L981)) -- [_WStoul](../../../core/libs/prx/libc/src/Strings.cpp), [_Iswctype](../../../core/libs/prx/libc/src/RuneLocale.cpp), [swscanf_s and swscanf](../../../core/libs/prx/libc/src/ScanningWide.cpp) (libc) - `_WStoul` is `wcstoul` and throws for a base other than 0 or 2 to 36. `_Iswctype` takes the class numbers of Dinkumware's [xwcc.h](https://github.com/superna9999/ti-cgt6x/blob/cgt6x_7_3_4/include/xwcc.h) (1 `alnum` to 11 `xdigit`, 12 `blank`) and throws for any other value, including the FreeBSD masks `wctype` returns; the numbering is not confirmed on the console's `libc.prx`. `swscanf_s` follows C11 7.29.2.2 and K.3.9.1.5, but reads the size argument as `unsigned int` like `sscanf_s` where K.3.9.1.5 specifies `rsize_t`; the console's width is unverified. A runtime-constraint violation (null buffer, format or receiving pointer) only returns `EOF`, and a too small array gets a null first element as in `sscanf_s`. A literal at end of input returns the number of assignments, including zero, rather than `EOF` as FreeBSD does, as reported from the console scanner in #8479; `%%` at end of input still returns `EOF`. A literal mismatch is a matching failure, where FreeBSD's `vfwscanf` reports an input failure. An incomplete floating exponent such as `1e+` converts its mantissa, as reported from the console scanner in #8479; other incomplete exponent forms are not separately verified. Short `%c` assignments follow FreeBSD 11.4; only `%lc` buffer results have contributor-reported native measurements of one bundled build in [#9321](https://github.com/boykopovar/AnyPS5/issues/9321#issuecomment-6342217421), not console or other-version verification. Short sized fields with a nonnull destination capacity below the requested width throw after consuming input but before writing; that precedence is unverified. An integer field that is only a prefix, such as `0x`, does not convert; FreeBSD's `vfwscanf` pushes back the `x` and converts the `0`. Integers out of range are truncated from `strtoll`/`strtoull`. `%p` follows FreeBSD 11's [vfwscanf.c](https://github.com/freebsd/freebsd-src/blob/4ab2e064d7950be84256d671a7ae93f87cc6aa36/lib/libc/stdio/vfwscanf.c#L610-L615): a hexadecimal integer with an optional `0x`, converted as unsigned and stored as a pointer; a length modifier on it throws, where FreeBSD ignores it. It throws for `%S`, `%C`, unknown length modifiers, scanset ranges, `%n` with suppression or width, and narrow `%s`, `%c` or `%[` input that the C-locale `wcrtomb` cannot convert. `swscanf` runs the same scanner without the size arguments of `%s`, `%c` and `%[`, as FreeBSD's [swscanf.c](https://github.com/freebsd/freebsd-src/blob/d106002967eff712894ab8b5337bbbff106ce5e0/lib/libc/stdio/swscanf.c) passes its arguments to `vswscanf`, so everything above except the sizes applies to it; it also keeps the null buffer, format and receiving pointer checks returning `EOF`, where C leaves those undefined. Neither variant is confirmed on a PS5 title +- [_WStoul](../../../core/libs/prx/libc/src/Strings.cpp), [_Iswctype](../../../core/libs/prx/libc/src/RuneLocale.cpp), [swscanf_s and swscanf](../../../core/libs/prx/libc/src/ScanningWide.cpp) (libc) - `_WStoul` is `wcstoul` and throws for a base other than 0 or 2 to 36. `_Iswctype` takes the class numbers of Dinkumware's [xwcc.h](https://github.com/superna9999/ti-cgt6x/blob/cgt6x_7_3_4/include/xwcc.h) (1 `alnum` to 11 `xdigit`, 12 `blank`) and throws for any other value, including the FreeBSD masks `wctype` returns; the numbering is not confirmed on the console's `libc.prx`. `swscanf_s` follows C11 7.29.2.2 and K.3.9.1.5, but reads the size argument as `unsigned int` like `sscanf_s` where K.3.9.1.5 specifies `rsize_t`; the console's width is unverified. A runtime-constraint violation (null buffer, format or receiving pointer) only returns `EOF`, and a too small string or scanset array gets a null first element as in `sscanf_s`. A literal at end of input returns the number of assignments, including zero, rather than `EOF` as FreeBSD does, as reported from the console scanner in #8479; `%%` at end of input still returns `EOF`. A literal mismatch is a matching failure, where FreeBSD's `vfwscanf` reports an input failure. An incomplete floating exponent such as `1e+` converts its mantissa, as reported from the console scanner in #8479; other incomplete exponent forms are not separately verified. Short `%c` assignments follow FreeBSD 11.4; only `%lc` buffer results have contributor-reported native measurements of one bundled build in [#9321](https://github.com/boykopovar/AnyPS5/issues/9321#issuecomment-6342217421), not console or other-version verification. Zero-capacity short character fields and short narrow-destination fields whose capacity is below the width still throw after consuming input but before writing; zero-capacity consumption and general narrow byte-capacity rules are unmeasured. Positive wide-destination limits use [contributor-reported native calls to three bundled builds](https://github.com/boykopovar/AnyPS5/pull/9863#issuecomment-6357861510), not console measurements; stream parity remains unverified. An integer field that is only a prefix, such as `0x`, does not convert; FreeBSD's `vfwscanf` pushes back the `x` and converts the `0`. Integers out of range are truncated from `strtoll`/`strtoull`. `%p` follows FreeBSD 11's [vfwscanf.c](https://github.com/freebsd/freebsd-src/blob/4ab2e064d7950be84256d671a7ae93f87cc6aa36/lib/libc/stdio/vfwscanf.c#L610-L615): a hexadecimal integer with an optional `0x`, converted as unsigned and stored as a pointer; a length modifier on it throws, where FreeBSD ignores it. It throws for `%S`, `%C`, unknown length modifiers, scanset ranges, `%n` with suppression or width, and a failed guest `wcrtomb` conversion. Narrow destinations use the guest UTF-8 conversion; encoding-error precedence remains unverified. `swscanf` runs the same scanner without the size arguments of `%s`, `%c` and `%[`, as FreeBSD's [swscanf.c](https://github.com/freebsd/freebsd-src/blob/d106002967eff712894ab8b5337bbbff106ce5e0/lib/libc/stdio/swscanf.c) passes its arguments to `vswscanf`, so everything above except the sizes applies to it; it also keeps the null buffer, format and receiving pointer checks returning `EOF`, where C leaves those undefined. Neither variant is confirmed on a PS5 title - [flock](../../../core/libs/prx/libc/src/FileLock.cpp) (libkernel) on Windows shares one lock state between duplicated descriptors, following FreeBSD 11.4's [flock(2)](https://github.com/freebsd/freebsd-src/blob/releng/11.4/lib/libc/sys/flock.2). Independent opens have separate states; closing a descriptor or reopening a stream drops only its reference. Lock transitions on one open file description are serialized. The lock is a host [LockFileEx](https://learn.microsoft.com/en-us/windows/win32/api/fileapi/nf-fileapi-lockfileex) lock on one byte at offset 2^63 - 2, which no file reaches, so it excludes other `flock` calls and leaves reads, writes and mapped views alone, like FreeBSD's advisory locks. Console behavior, signal interruption and concurrent closure of a descriptor while its lock call is pending are unverified. - [std::_Throw_Cpp_error](../../../core/libs/prx/libc/src/exception/StandardExceptions.cpp) (libc) follows Dinkumware `thread0.cpp` ([VC12](https://github.com/ganboing/crt.vc12/blob/f1d47126/src/thread0.cpp), [STL](https://github.com/microsoft/STL/blob/da52dc0f/stl/src/thread0.cpp)), without console verification. Codes outside 0..6 throw `std::out_of_range`, the deleting destructor throws, and STREAMS errnos absent from FreeBSD 11.4 [`errno.h`](https://github.com/freebsd/freebsd-src/blob/releng/11.4/sys/sys/errno.h) (`ENODATA`, `ENOSR`, `ENOSTR`, `ETIME`) are omitted. Codes 0..6 throw a 32-byte [VC12 `system_error`](https://github.com/ganboing/crt.vc12/blob/f1d47126/src/system_error) with FreeBSD errnos (16, 22, 3, 12, 1, 11, 35), a static `generic_category` (not `_Generic_object`), and unverified 40-byte [GuestLocale::String](../../../core/libs/prx/libc/include/GuestLocale.hpp) messages from [`syserror.cpp`](https://github.com/ganboing/crt.vc12/blob/f1d47126/src/syserror.cpp) - [Call tracing](../../../core/libs/prx/libc/src/specifics/x86_64/CallTrace.cpp) (`--trace-imports`, `APS5_TRACE_CALLS`) is missing on macOS and records only the entry of a call, not its arguments or result. One process-wide ring keeps the last 65536 calls, so a busy thread pushes the others out; the reporting thread's own calls are listed only while they are still in that ring. Calls are counted per traced target; an import bound to a target already traced under another name is counted under the first name. The thunk saves the integer argument registers and `xmm0` to `xmm7`; wider vector arguments survive only while the recording code avoids AVX, which is untested. Linux has no crash report, so it prints the trace only on `SIGABRT` (guest or host `abort()`), not on `SIGSEGV`; a caller in a module loaded after the last traced import prints without a module name, and a thread aborting while another writes the report waits for it at most 2 seconds From 664b4af88440104953ca756c930a25154cea7d74 Mon Sep 17 00:00:00 2001 From: Kraken <10617703+UnhealthyKraken@users.noreply.github.com> Date: Fri, 23 Oct 2026 15:24:36 +0100 Subject: [PATCH 5/6] test(libc): cover zero-capacity wide character results --- core/libs/tests/GuestWideConversion.cpp | 29 +++++++++++++++++++------ core/libs/tests/GuestWideStreamScan.cpp | 18 +++++++++++++++ 2 files changed, 40 insertions(+), 7 deletions(-) diff --git a/core/libs/tests/GuestWideConversion.cpp b/core/libs/tests/GuestWideConversion.cpp index 18af27dc469d..1e641cad5c80 100644 --- a/core/libs/tests/GuestWideConversion.cpp +++ b/core/libs/tests/GuestWideConversion.cpp @@ -256,13 +256,6 @@ static void CheckShortCharacterFields() { Require(swscanf_nid_postfix(u"5 a", u"%d %3lc%n", &value, output, &count) == 2 && value == 5 && count == 3, "short character field after another assignment"); Require(output[0] == u'a' && output[1] == 0xa55a && output[2] == 0xa55a, "short field after assignment does not terminate"); - for (unsigned int capacity : {0u}) { - char16_t guarded[]{0xa55a, 0xa55a, 0xa55a}; - Require(Throws([&] { swscanf_s_nid_postfix(u"a", u"%3lc", guarded + 1, capacity); }), - "short secure field with capacity below width is explicitly unsupported"); - Require(guarded[0] == 0xa55a && guarded[1] == 0xa55a && guarded[2] == 0xa55a, - "unsupported short secure field leaves output untouched"); - } char bytes[]{'?', '?', '?', '?'}; count = -1; Require(swscanf_nid_postfix(u"ab", u"%3c%n", bytes + 1, &count) == 1 && count == 2, @@ -270,6 +263,27 @@ static void CheckShortCharacterFields() { Require(bytes[0] == '?' && bytes[1] == 'a' && bytes[2] == 'b' && bytes[3] == '?', "short narrow destination bounds"); } +static void CheckZeroCapacityCharacterFields() { + for (const char16_t* input : {u"a", u"ab", u"abc", u"abcd"}) { + char16_t output[]{0xa55a, 0xa55a, 0xa55a}; + int count = -1, result = -1; + *__error_nid_postfix() = 0; + Require(!Throws([&] { result = swscanf_s_nid_postfix(input, u"%3lc%n", output + 1, 0u, &count); }), + "zero-capacity wide character field does not throw"); + const int error = *__error_nid_postfix(); + Require(result == 0, "zero-capacity wide character field returns no assignment"); + Require(count == -1, "zero-capacity wide character field does not reach percent n"); + Require(error == 0, "zero-capacity wide character field preserves errno"); + for (const char16_t unit : output) Require(unit == 0xa55a, "zero-capacity wide character field leaves output untouched"); + } + char16_t output[]{0xa55a, 0xa55a, 0xa55a}; + int value = -1, count = -1, result = -1; + Require(!Throws([&] { result = swscanf_s_nid_postfix(u"7 a", u"%d %3lc%n", &value, output + 1, 0u, &count); }), + "zero-capacity field after an assignment does not throw"); + Require(result == 1 && value == 7 && count == -1, "zero-capacity field retains only prior assignments"); + for (const char16_t unit : output) Require(unit == 0xa55a, "zero-capacity field after an assignment leaves output untouched"); +} + static void CheckSecureCharacterCapacities() { const char16_t* inputs[]{u"", u"a", u"ab", u"abc", u"abcd"}; for (const char16_t* input : inputs) { @@ -316,6 +330,7 @@ static void CheckSecureCharacterCapacities() { } int main() { + CheckZeroCapacityCharacterFields(); CheckSecureCharacterCapacities(); CheckShortCharacterFields(); CheckStoul(); diff --git a/core/libs/tests/GuestWideStreamScan.cpp b/core/libs/tests/GuestWideStreamScan.cpp index 071fe3b57474..f22eeaa58446 100644 --- a/core/libs/tests/GuestWideStreamScan.cpp +++ b/core/libs/tests/GuestWideStreamScan.cpp @@ -202,6 +202,23 @@ static void CheckShortCharacterFields() { } } +static void CheckZeroCapacityCharacterFields() { + for (bool indirect : {false, true}) { + FileStream* stream = Open("a"); + char16_t output[]{0xa55a, 0xa55a, 0xa55a}; + int count = -1; + *__error_nid_postfix() = 0; + const int result = indirect ? ScanStreamSized(stream, u"%3lc%n", output + 1, 0u, &count) + : fwscanf_s_nid_postfix(stream, u"%3lc%n", output + 1, 0u, &count); + const int error = *__error_nid_postfix(); + Require(result == 0, "zero-capacity stream character field returns no assignment"); + Require(count == -1, "zero-capacity stream character field does not reach percent n"); + Require(error == 0, "zero-capacity stream character field preserves errno"); + for (const char16_t unit : output) Require(unit == 0xa55a, "zero-capacity stream character field leaves output untouched"); + Close(stream); + } +} + static void CheckSecureCharacterCapacities() { const char* inputs[]{"", "a", "ab", "abc", "abcd"}; for (const char* input : inputs) { @@ -244,6 +261,7 @@ static void CheckSecureCharacterCapacities() { } int main() { + CheckZeroCapacityCharacterFields(); CheckSecureCharacterCapacities(); CheckShortCharacterFields(); CheckConsumption(); From 925d839d4794202dfdf3fadaa466e9038f322528 Mon Sep 17 00:00:00 2001 From: Kraken <10617703+UnhealthyKraken@users.noreply.github.com> Date: Fri, 23 Oct 2026 15:28:58 +0100 Subject: [PATCH 6/6] fix(libc): return no assignment for zero-capacity wide fields --- core/libs/prx/libc/src/ScanningWide.cpp | 1 + docs/dev/debt/libc.md | 2 +- 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/core/libs/prx/libc/src/ScanningWide.cpp b/core/libs/prx/libc/src/ScanningWide.cpp index be7a3795ced5..5d0a0622eb6a 100644 --- a/core/libs/prx/libc/src/ScanningWide.cpp +++ b/core/libs/prx/libc/src/ScanningWide.cpp @@ -429,6 +429,7 @@ class WideScanner { item.push_back(static_cast(input.Peek())); input.Advance(); } + if (sized && target != nullptr && directive.length == "l" && capacity == 0) return Outcome::Matching; if (item.size() < count && sized && target != nullptr && capacity < count) Fail("short character field with capacity below width"); } else { diff --git a/docs/dev/debt/libc.md b/docs/dev/debt/libc.md index d6f9129d2d22..2be1fe667acd 100644 --- a/docs/dev/debt/libc.md +++ b/docs/dev/debt/libc.md @@ -130,7 +130,7 @@ - [syslog](../../../core/libs/prx/libc/src/Syslog.cpp) (libc) writes each message to `stderr` as `[syslog:] `, with a newline added when missing, instead of sending it to the console's system log. Priority and facility handling follow FreeBSD's `vsyslog`: `%m` expands to the guest errno's message, unknown bits are reported (as priority 0x23, since FreeBSD passes `LOG_ERR|LOG_CONS|LOG_PERROR|LOG_PID` as the priority) and masked, a missing facility becomes `LOG_USER` or the facility set by `openlog`, and errno is preserved. `openlog` applies its identity and `LOG_PID`; a null identity retains the previous one, and a valid nonzero facility replaces the default. `closelog` clears the identity but retains the options and facility, as in FreeBSD 11; when no identity is set, FreeBSD falls back to the program name, which this backend does not emit. `LOG_PERROR` has no extra effect because stderr is the emulated log sink; `LOG_NDELAY`/`LOG_ODELAY`, `LOG_NOWAIT` and `LOG_CONS` have no daemon, child or console backend to control. `setlogmask` follows [FreeBSD 11](https://github.com/freebsd/freebsd-src/blob/8d2708de5d6112a492744d367cfdfb8fd219227a/lib/libc/gen/syslog.c): the process-wide mask defaults to 0xff, zero queries it, and nonzero values replace it; masked messages are skipped before formatting, including the unknown-priority diagnostic. The console's mask behaviour is unverified - [fwscanf, vfwscanf, wscanf, vwscanf and their `_s` forms](../../../core/libs/prx/libc/src/ScanningWide.cpp) (libc) run the `swscanf` scanner on characters read with `fgetwc`, so everything listed for `swscanf` in this file applies to them. As in the console's `libc.prx`, a field takes characters while they can still begin a valid item, so a failed field such as `0x` for `%x` stays consumed, and only the character that ended the scan goes back to the stream. That character is pushed back as its UTF-8 bytes with `ungetc`, because `ungetwc` takes single bytes only; the Windows C runtime guarantees one byte of push-back, so a non-ASCII lookahead can be lost there. A null stream or format returns `EOF`; the console checks them only in the `_s` forms. - [daemon](../../../core/libs/prx/libc/src/Process.cpp) (libc) throws: forking into the background is not supported. PPSA85300 imports it and fails to load without it. VLC 3.0 calls it only when its `daemon` option is set ([libvlc.c](https://github.com/videolan/vlc/blob/6de05adcbaf2e8b85fe86aad4169393098628119/src/libvlc.c#L190-L197)), and the title starts libVLC with `--ignore-config` and without `--daemon` ([player.cc](https://github.com/ZiZc3/VLC/blob/f3253020a64368153233ede29183014c0b6c8979/src/player.cc#L960-L981)) -- [_WStoul](../../../core/libs/prx/libc/src/Strings.cpp), [_Iswctype](../../../core/libs/prx/libc/src/RuneLocale.cpp), [swscanf_s and swscanf](../../../core/libs/prx/libc/src/ScanningWide.cpp) (libc) - `_WStoul` is `wcstoul` and throws for a base other than 0 or 2 to 36. `_Iswctype` takes the class numbers of Dinkumware's [xwcc.h](https://github.com/superna9999/ti-cgt6x/blob/cgt6x_7_3_4/include/xwcc.h) (1 `alnum` to 11 `xdigit`, 12 `blank`) and throws for any other value, including the FreeBSD masks `wctype` returns; the numbering is not confirmed on the console's `libc.prx`. `swscanf_s` follows C11 7.29.2.2 and K.3.9.1.5, but reads the size argument as `unsigned int` like `sscanf_s` where K.3.9.1.5 specifies `rsize_t`; the console's width is unverified. A runtime-constraint violation (null buffer, format or receiving pointer) only returns `EOF`, and a too small string or scanset array gets a null first element as in `sscanf_s`. A literal at end of input returns the number of assignments, including zero, rather than `EOF` as FreeBSD does, as reported from the console scanner in #8479; `%%` at end of input still returns `EOF`. A literal mismatch is a matching failure, where FreeBSD's `vfwscanf` reports an input failure. An incomplete floating exponent such as `1e+` converts its mantissa, as reported from the console scanner in #8479; other incomplete exponent forms are not separately verified. Short `%c` assignments follow FreeBSD 11.4; only `%lc` buffer results have contributor-reported native measurements of one bundled build in [#9321](https://github.com/boykopovar/AnyPS5/issues/9321#issuecomment-6342217421), not console or other-version verification. Zero-capacity short character fields and short narrow-destination fields whose capacity is below the width still throw after consuming input but before writing; zero-capacity consumption and general narrow byte-capacity rules are unmeasured. Positive wide-destination limits use [contributor-reported native calls to three bundled builds](https://github.com/boykopovar/AnyPS5/pull/9863#issuecomment-6357861510), not console measurements; stream parity remains unverified. An integer field that is only a prefix, such as `0x`, does not convert; FreeBSD's `vfwscanf` pushes back the `x` and converts the `0`. Integers out of range are truncated from `strtoll`/`strtoull`. `%p` follows FreeBSD 11's [vfwscanf.c](https://github.com/freebsd/freebsd-src/blob/4ab2e064d7950be84256d671a7ae93f87cc6aa36/lib/libc/stdio/vfwscanf.c#L610-L615): a hexadecimal integer with an optional `0x`, converted as unsigned and stored as a pointer; a length modifier on it throws, where FreeBSD ignores it. It throws for `%S`, `%C`, unknown length modifiers, scanset ranges, `%n` with suppression or width, and a failed guest `wcrtomb` conversion. Narrow destinations use the guest UTF-8 conversion; encoding-error precedence remains unverified. `swscanf` runs the same scanner without the size arguments of `%s`, `%c` and `%[`, as FreeBSD's [swscanf.c](https://github.com/freebsd/freebsd-src/blob/d106002967eff712894ab8b5337bbbff106ce5e0/lib/libc/stdio/swscanf.c) passes its arguments to `vswscanf`, so everything above except the sizes applies to it; it also keeps the null buffer, format and receiving pointer checks returning `EOF`, where C leaves those undefined. Neither variant is confirmed on a PS5 title +- [_WStoul](../../../core/libs/prx/libc/src/Strings.cpp), [_Iswctype](../../../core/libs/prx/libc/src/RuneLocale.cpp), [swscanf_s and swscanf](../../../core/libs/prx/libc/src/ScanningWide.cpp) (libc) - `_WStoul` is `wcstoul` and throws for a base other than 0 or 2 to 36. `_Iswctype` takes the class numbers of Dinkumware's [xwcc.h](https://github.com/superna9999/ti-cgt6x/blob/cgt6x_7_3_4/include/xwcc.h) (1 `alnum` to 11 `xdigit`, 12 `blank`) and throws for any other value, including the FreeBSD masks `wctype` returns; the numbering is not confirmed on the console's `libc.prx`. `swscanf_s` follows C11 7.29.2.2 and K.3.9.1.5, but reads the size argument as `unsigned int` like `sscanf_s` where K.3.9.1.5 specifies `rsize_t`; the console's width is unverified. A runtime-constraint violation (null buffer, format or receiving pointer) only returns `EOF`, and a too small string or scanset array gets a null first element as in `sscanf_s`. A literal at end of input returns the number of assignments, including zero, rather than `EOF` as FreeBSD does, as reported from the console scanner in #8479; `%%` at end of input still returns `EOF`. A literal mismatch is a matching failure, where FreeBSD's `vfwscanf` reports an input failure. An incomplete floating exponent such as `1e+` converts its mantissa, as reported from the console scanner in #8479; other incomplete exponent forms are not separately verified. Short `%c` assignments follow FreeBSD 11.4; only `%lc` buffer results have contributor-reported native measurements of one bundled build in [#9321](https://github.com/boykopovar/AnyPS5/issues/9321#issuecomment-6342217421), not console or other-version verification. Zero-capacity wide-field input consumption, empty-input precedence and general narrow byte-capacity rules remain unverified. Short narrow-destination fields whose capacity is below the width still throw after consuming input but before writing, unlike the reported undersized ASCII case. Wide-destination limits and zero-capacity return/output use [contributor-reported native calls to three bundled builds](https://github.com/boykopovar/AnyPS5/pull/9863#issuecomment-6357861510), not console measurements; stream parity remains unverified. An integer field that is only a prefix, such as `0x`, does not convert; FreeBSD's `vfwscanf` pushes back the `x` and converts the `0`. Integers out of range are truncated from `strtoll`/`strtoull`. `%p` follows FreeBSD 11's [vfwscanf.c](https://github.com/freebsd/freebsd-src/blob/4ab2e064d7950be84256d671a7ae93f87cc6aa36/lib/libc/stdio/vfwscanf.c#L610-L615): a hexadecimal integer with an optional `0x`, converted as unsigned and stored as a pointer; a length modifier on it throws, where FreeBSD ignores it. It throws for `%S`, `%C`, unknown length modifiers, scanset ranges, `%n` with suppression or width, and a failed guest `wcrtomb` conversion. Narrow destinations use the guest UTF-8 conversion; encoding-error precedence remains unverified. `swscanf` runs the same scanner without the size arguments of `%s`, `%c` and `%[`, as FreeBSD's [swscanf.c](https://github.com/freebsd/freebsd-src/blob/d106002967eff712894ab8b5337bbbff106ce5e0/lib/libc/stdio/swscanf.c) passes its arguments to `vswscanf`, so everything above except the sizes applies to it; it also keeps the null buffer, format and receiving pointer checks returning `EOF`, where C leaves those undefined. Neither variant is confirmed on a PS5 title - [flock](../../../core/libs/prx/libc/src/FileLock.cpp) (libkernel) on Windows shares one lock state between duplicated descriptors, following FreeBSD 11.4's [flock(2)](https://github.com/freebsd/freebsd-src/blob/releng/11.4/lib/libc/sys/flock.2). Independent opens have separate states; closing a descriptor or reopening a stream drops only its reference. Lock transitions on one open file description are serialized. The lock is a host [LockFileEx](https://learn.microsoft.com/en-us/windows/win32/api/fileapi/nf-fileapi-lockfileex) lock on one byte at offset 2^63 - 2, which no file reaches, so it excludes other `flock` calls and leaves reads, writes and mapped views alone, like FreeBSD's advisory locks. Console behavior, signal interruption and concurrent closure of a descriptor while its lock call is pending are unverified. - [std::_Throw_Cpp_error](../../../core/libs/prx/libc/src/exception/StandardExceptions.cpp) (libc) follows Dinkumware `thread0.cpp` ([VC12](https://github.com/ganboing/crt.vc12/blob/f1d47126/src/thread0.cpp), [STL](https://github.com/microsoft/STL/blob/da52dc0f/stl/src/thread0.cpp)), without console verification. Codes outside 0..6 throw `std::out_of_range`, the deleting destructor throws, and STREAMS errnos absent from FreeBSD 11.4 [`errno.h`](https://github.com/freebsd/freebsd-src/blob/releng/11.4/sys/sys/errno.h) (`ENODATA`, `ENOSR`, `ENOSTR`, `ETIME`) are omitted. Codes 0..6 throw a 32-byte [VC12 `system_error`](https://github.com/ganboing/crt.vc12/blob/f1d47126/src/system_error) with FreeBSD errnos (16, 22, 3, 12, 1, 11, 35), a static `generic_category` (not `_Generic_object`), and unverified 40-byte [GuestLocale::String](../../../core/libs/prx/libc/include/GuestLocale.hpp) messages from [`syserror.cpp`](https://github.com/ganboing/crt.vc12/blob/f1d47126/src/syserror.cpp) - [Call tracing](../../../core/libs/prx/libc/src/specifics/x86_64/CallTrace.cpp) (`--trace-imports`, `APS5_TRACE_CALLS`) is missing on macOS and records only the entry of a call, not its arguments or result. One process-wide ring keeps the last 65536 calls, so a busy thread pushes the others out; the reporting thread's own calls are listed only while they are still in that ring. Calls are counted per traced target; an import bound to a target already traced under another name is counted under the first name. The thunk saves the integer argument registers and `xmm0` to `xmm7`; wider vector arguments survive only while the recording code avoids AVX, which is untested. Linux has no crash report, so it prints the trace only on `SIGABRT` (guest or host `abort()`), not on `SIGSEGV`; a caller in a module loaded after the last traced import prints without a module name, and a thread aborting while another writes the report waits for it at most 2 seconds