From 621a487bdf364dca90c473c4a8fb2fd64c4e6388 Mon Sep 17 00:00:00 2001 From: Hussein Kadhim Date: Sun, 11 Oct 2026 22:39:55 +0300 Subject: [PATCH 1/7] fix(libc): print the FreeBSD printf extensions and stray percent signs on Windows The Windows printf family threw on the q length modifier, the D, O and U conversions, an unknown conversion character and a format ending in a percent sign, where FreeBSD's vfprintf formats the first three and prints the unknown character itself or stops at the end of the format. (cherry picked from commit 0e3b2896d228c601cefc992b2895723188ebf0fa) --- .../prx/libc/include/WindowsFormatting.hpp | 13 ++++++++--- core/libs/tests/Formatting.cpp | 23 +++++++++++++++++++ docs/dev/debt/libc.md | 1 + 3 files changed, 34 insertions(+), 3 deletions(-) diff --git a/core/libs/prx/libc/include/WindowsFormatting.hpp b/core/libs/prx/libc/include/WindowsFormatting.hpp index 0c8dd2861f26..75acb0202f1e 100644 --- a/core/libs/prx/libc/include/WindowsFormatting.hpp +++ b/core/libs/prx/libc/include/WindowsFormatting.hpp @@ -311,14 +311,19 @@ inline int FormatWindows(char* buffer, size_t size, const char* format, const vo } } std::string length; - if (*format && std::strchr("hljztL", *format)) { + if (*format && std::strchr("hljztLq", *format)) { length += *format++; if ((length == "h" && *format == 'h') || (length == "l" && *format == 'l')) length += *format++; } - const char conversion = *format; - if (!conversion) throw std::invalid_argument("Incomplete format conversion"); + if (length == "q") length = "ll"; + char conversion = *format; + if (!conversion) break; ++format; + if (conversion == 'D' || conversion == 'O' || conversion == 'U') { + if (length != "j" && length != "z" && length != "t" && length != "ll") length = "l"; + conversion = static_cast(conversion + 'a' - 'A'); + } const bool integerLength = length.empty() || length == "h" || length == "hh" || length == "l" || length == "ll" || length == "j" || length == "z" || length == "t"; if (conversion == 'd' || conversion == 'i') { @@ -404,6 +409,8 @@ inline int FormatWindows(char* buffer, size_t size, const char* format, const vo else if (length == "h") *static_cast(pointer) = static_cast(count); else if (length.empty()) *static_cast(pointer) = count; else *static_cast(pointer) = count; + } else if (!std::strchr("diouxXaAeEfFgGcCsSpn", conversion)) { + output.Value(spec + 'c', static_cast(static_cast(conversion))); } else { throw std::invalid_argument("Unsupported format conversion '" + spec + length + conversion + "' in \"" + std::string(formatStart).substr(0, 160) + "\""); } diff --git a/core/libs/tests/Formatting.cpp b/core/libs/tests/Formatting.cpp index 8d80547b6a85..eef060afc074 100644 --- a/core/libs/tests/Formatting.cpp +++ b/core/libs/tests/Formatting.cpp @@ -255,6 +255,7 @@ __attribute__((noinline)) static void APS5_VABI RunChecks() { Require(std::strcmp(buffer, expected) == 0); char pointers[128]; const auto address = [](std::uintptr_t value) { return reinterpret_cast(value); }; +<<<<<<< HEAD Require(snprintf_nid_postfix(pointers, sizeof(pointers), "%p|%p|%p", address(0), address(0x1234), address(0xdeadbeef12)) == 17 && std::strcmp(pointers, "0|1234|deadbeef12") == 0); Require(snprintf_nid_postfix(pointers, sizeof(pointers), "[%p][%5p][%-5p][%05p][%.8p][%10.6p][%.0p]", @@ -271,6 +272,28 @@ __attribute__((noinline)) static void APS5_VABI RunChecks() { Require(snprintf_nid_postfix(pointers, 6, "%p", address(0xabcdef)) == 6 && std::strcmp(pointers, "abcde") == 0); Require(snprintf_nid_postfix(pointers, sizeof(pointers), "%p", address(0xffffffffffffffffull)) == 16 && std::strcmp(pointers, "ffffffffffffffff") == 0); +======= + Require(snprintf_nid_postfix(pointers, sizeof(pointers), "%p|%p|%p", address(0), address(0x1234), address(0xdeadbeef12)) == 23 && + std::strcmp(pointers, "0x0|0x1234|0xdeadbeef12") == 0); + Require(snprintf_nid_postfix(pointers, sizeof(pointers), "[%8p][%-8p][%08p][%.6p][%10.4p][%.0p]", + address(0x1a), address(0x1a), address(0x1a), address(0x1a), address(0x1a), address(0)) == 56 && + std::strcmp(pointers, "[ 0x1a][0x1a ][0x00001a][0x00001a][ 0x001a][0x]") == 0); + Require(snprintf_nid_postfix(pointers, sizeof(pointers), "[%+p][% p][%#p][%*p][%-*p]", address(0x1a), address(0x1a), + address(0x1a), 6, address(0xf), -6, address(0xf)) == 34 && + std::strcmp(pointers, "[0x1a][0x1a][0x1a][ 0xf][0xf ]") == 0); + Require(snprintf_nid_postfix(pointers, 6, "%p", address(0xabcdef)) == 8 && std::strcmp(pointers, "0xabc") == 0); + char freebsd[64]; + Require(snprintf_nid_postfix(freebsd, sizeof(freebsd), "%qd|%qu|%qx|%qo", -5000000000LL, 5000000000ULL, 255ULL, 8ULL) == 28 && + std::strcmp(freebsd, "-5000000000|5000000000|ff|10") == 0); + Require(snprintf_nid_postfix(freebsd, sizeof(freebsd), "%D|%hD|%O|%U|%jD", -5000000000L, 5000000000L, 8L, 5000000000UL, -6000000000LL) == 48 && + std::strcmp(freebsd, "-5000000000|5000000000|10|5000000000|-6000000000") == 0); + Require(snprintf_nid_postfix(freebsd, sizeof(freebsd), "[%5%][%-5%][%y][%5y][%-3y]") == 29 && + std::strcmp(freebsd, "[ %][% ][y][ y][y ]") == 0); + Require(snprintf_nid_postfix(freebsd, sizeof(freebsd), "100%") == 3 && std::strcmp(freebsd, "100") == 0); + Require(snprintf_nid_postfix(freebsd, sizeof(freebsd), "%d done 5% ", 7) == 8 && std::strcmp(freebsd, "7 done 5") == 0); + Require(snprintf_nid_postfix(freebsd, 4, "ab%dcd%", 12) == 6 && std::strcmp(freebsd, "ab1") == 0); +#endif +>>>>>>> 5a3fd9f2ca (fix(libc): print the FreeBSD printf extensions and stray percent signs on Windows) snprintf_nid_postfix(buffer, sizeof(buffer), "%.*s", -1, "unlimited"); Require(std::strcmp(buffer, "unlimited") == 0); buffer[5] = '!'; diff --git a/docs/dev/debt/libc.md b/docs/dev/debt/libc.md index 72a5f70f4bc6..d4f154acfb5d 100644 --- a/docs/dev/debt/libc.md +++ b/docs/dev/debt/libc.md @@ -156,3 +156,4 @@ - [ios_base standard streams](../../../core/libs/prx/libc/src/LocaleSupport.cpp) (libc) `_Addstd` queues the stream in the standard stream table of Dinkumware [ios.cpp](https://github.com/ganboing/crt.vc12/blob/master/src/ios.cpp) (`stdstr`, `stdopens`, `_Addstd`, `_Ios_base_dtor`): it takes the first of slots 1 to 7 that is free or already holds the stream, and slot 8 (`NSTDSTR`) when none is, so slot 8 is shared by every stream past the first seven and registration never fails. It stores the 1-based slot index in the guest `IosBase::standardStream` (Dinkumware's `_Stdstr`, where 0 means not a standard stream) and counts the stream objects registered per slot; registering the same stream again reuses its slot and raises its count. Measured on a PS5 (firmware 13.60) from a homebrew payload calling the system `libSceLibcInternal` export: in a process whose own standard streams already hold slots 1 to 7, twelve distinct streams and a repeated one all get 8, written as a 64-bit value at offset 0x8 with no other byte of the object touched; the assignment of slots 1 to 7 to fresh streams is not observable there and follows VC12. The counts are `int`, not VC12's `char`, and the registration and the destructor lock the table where VC12 locks only `_Addstd`; a slot index past the table throws in the destructor instead of indexing out of bounds. The destructor decrements its slot's count and suppresses the rest of the destruction while the count stays positive, as `_Ios_base_dtor` does, and the slots of destroyed streams keep their pointer and stay reserved for that stream, as `stdstr` does. The streams themselves are destroyed by the title's own code as it registers them through `__cxa_atexit`; when the last reference closes, the unsupported stream storage and callback cleanup is still the throw it was and the locale is released as for a non-standard stream. - [std::_Pad, _Thrd_join, _Thrd_detach, _Thrd_id](../../../core/libs/prx/libc/src/Threads.cpp) (libc) follow Dinkumware V6.00 `thr/xthread` and `thr/threadcall.cpp` ([crt.vc12 `f1d47126`](https://github.com/ganboing/crt.vc12/tree/f1d47126/src/thr)), without console verification. `_Thrd_id` throws because whether the console returns `pthread_t` or a kernel thread ID and at what width is unknown; `_Launch` omits `_Cnd_do_broadcast_at_thread_exit` and calls `pthread_create` registered by libkernel with default attributes. The 32-byte pad layout (`_Go()` vtable at 0x0, `_Cnd_t` at 0x8, `_Mtx_t` at 0x10, `bool _Started` at 0x18) is assumed from VC12, unverified. Failing primitives or thread start throw the guest `std::system_error`; null arguments throw. `_Thrd_join` returns `_Thrd_error` (4) on `pthread_join` failure and truncates the exit code to `int`; `_Thrd_detach` returns `_Thrd_error` (4) on `pthread_detach` failure and throws when none is registered - [num_get::id, time_get::id and the num_get vtable](../../../core/libs/prx/libc/src/LocaleSupport.cpp) (libc) are data objects, as the public SDK's libSceLibcInternal table declares them and as the other facet ids and the `num_put` and `codecvt` vtables are kept: the ids start at zero and the vtable is 17 zeroed slots, the 136-byte size of `KfcTPbeaOqg` in the console's libSceLibcInternal, whose relocations fill the offset-to-top and typeinfo entries, the two destructors, `locale::facet::_Incref` and `_Decref`, and the eleven `do_get` of Dinkumware's crt.vc12 `xlocnum`, so a guest resolves the symbols and reads data, and a facet call through the vtable faults. libSceAmpr exported the three as throwing functions, which a data consumer reads as code; its qualified imports fall back to libc. +- [printf family conversions](../../../core/libs/prx/libc/include/WindowsFormatting.hpp) (libc) - on Windows the `q` length modifier, the `%D`, `%O` and `%U` conversions, an unknown conversion character (printed as itself with the width and `-` flag, as `%c` would) and a `%` that ends the format (output stops there) follow FreeBSD 11.4 [vfprintf.c](https://github.com/freebsd/freebsd-src/blob/release/11.4.0/lib/libc/stdio/vfprintf.c). This records the host implementation; the console's Dinkumware `_Printf` behavior for these cases is unverified and may differ. The `0` flag on an unknown conversion is handled by the host `%c`, which pads with blanks where FreeBSD pads with zeroes. Linux formats these through glibc, which prints `%D`, `%O`, `%U` and unknown conversions as written From b331e3a86d279690847ea79c31077c0d68b33c98 Mon Sep 17 00:00:00 2001 From: Hussein Kadhim Date: Sun, 11 Oct 2026 22:59:52 +0300 Subject: [PATCH 2/7] fix(libc): keep throwing for printf conversions FreeBSD supports but Windows does not The unknown-conversion branch printed %m, positional arguments, the grouping flag and modifiers in an unusual order as literal characters. Only characters FreeBSD's vfprintf prints as themselves are now printed. (cherry picked from commit 0106858d3d39f83eb2234a5f581d5bf5260521a0) --- core/libs/prx/libc/include/WindowsFormatting.hpp | 2 +- core/libs/tests/Formatting.cpp | 5 +++++ 2 files changed, 6 insertions(+), 1 deletion(-) diff --git a/core/libs/prx/libc/include/WindowsFormatting.hpp b/core/libs/prx/libc/include/WindowsFormatting.hpp index 75acb0202f1e..1afc24195b4b 100644 --- a/core/libs/prx/libc/include/WindowsFormatting.hpp +++ b/core/libs/prx/libc/include/WindowsFormatting.hpp @@ -409,7 +409,7 @@ inline int FormatWindows(char* buffer, size_t size, const char* format, const vo else if (length == "h") *static_cast(pointer) = static_cast(count); else if (length.empty()) *static_cast(pointer) = count; else *static_cast(pointer) = count; - } else if (!std::strchr("diouxXaAeEfFgGcCsSpn", conversion)) { + } else if (!std::strchr(" #*-+'.0123456789$LhjlqtzCcDdiaAeEfFgGmnOopSsUuXx", conversion)) { output.Value(spec + 'c', static_cast(static_cast(conversion))); } else { throw std::invalid_argument("Unsupported format conversion '" + spec + length + conversion + "' in \"" + std::string(formatStart).substr(0, 160) + "\""); diff --git a/core/libs/tests/Formatting.cpp b/core/libs/tests/Formatting.cpp index eef060afc074..af7bd315e94b 100644 --- a/core/libs/tests/Formatting.cpp +++ b/core/libs/tests/Formatting.cpp @@ -289,6 +289,11 @@ __attribute__((noinline)) static void APS5_VABI RunChecks() { std::strcmp(freebsd, "-5000000000|5000000000|10|5000000000|-6000000000") == 0); Require(snprintf_nid_postfix(freebsd, sizeof(freebsd), "[%5%][%-5%][%y][%5y][%-3y]") == 29 && std::strcmp(freebsd, "[ %][% ][y][ y][y ]") == 0); + for (const char* unsupported : {"%m", "%1$d", "%'d", "%l5d", "%lqd", "%qc", "%lhd", "%l*d"}) { + bool threw = false; + try { snprintf_nid_postfix(freebsd, sizeof(freebsd), unsupported, 1); } catch (const std::invalid_argument&) { threw = true; } + Require(threw); + } Require(snprintf_nid_postfix(freebsd, sizeof(freebsd), "100%") == 3 && std::strcmp(freebsd, "100") == 0); Require(snprintf_nid_postfix(freebsd, sizeof(freebsd), "%d done 5% ", 7) == 8 && std::strcmp(freebsd, "7 done 5") == 0); Require(snprintf_nid_postfix(freebsd, 4, "ab%dcd%", 12) == 6 && std::strcmp(freebsd, "ab1") == 0); From 45bfa20e4365dbaf7093b474ca24bd7bcf2786e0 Mon Sep 17 00:00:00 2001 From: MarcZX100 Date: Sun, 11 Oct 2026 20:39:33 +0000 Subject: [PATCH 3/7] test(libc): separate Windows printf conversion cases --- core/libs/tests/Formatting.cpp | 32 +++++++++++++++++--------------- 1 file changed, 17 insertions(+), 15 deletions(-) diff --git a/core/libs/tests/Formatting.cpp b/core/libs/tests/Formatting.cpp index af7bd315e94b..544221b7131e 100644 --- a/core/libs/tests/Formatting.cpp +++ b/core/libs/tests/Formatting.cpp @@ -282,21 +282,6 @@ __attribute__((noinline)) static void APS5_VABI RunChecks() { address(0x1a), 6, address(0xf), -6, address(0xf)) == 34 && std::strcmp(pointers, "[0x1a][0x1a][0x1a][ 0xf][0xf ]") == 0); Require(snprintf_nid_postfix(pointers, 6, "%p", address(0xabcdef)) == 8 && std::strcmp(pointers, "0xabc") == 0); - char freebsd[64]; - Require(snprintf_nid_postfix(freebsd, sizeof(freebsd), "%qd|%qu|%qx|%qo", -5000000000LL, 5000000000ULL, 255ULL, 8ULL) == 28 && - std::strcmp(freebsd, "-5000000000|5000000000|ff|10") == 0); - Require(snprintf_nid_postfix(freebsd, sizeof(freebsd), "%D|%hD|%O|%U|%jD", -5000000000L, 5000000000L, 8L, 5000000000UL, -6000000000LL) == 48 && - std::strcmp(freebsd, "-5000000000|5000000000|10|5000000000|-6000000000") == 0); - Require(snprintf_nid_postfix(freebsd, sizeof(freebsd), "[%5%][%-5%][%y][%5y][%-3y]") == 29 && - std::strcmp(freebsd, "[ %][% ][y][ y][y ]") == 0); - for (const char* unsupported : {"%m", "%1$d", "%'d", "%l5d", "%lqd", "%qc", "%lhd", "%l*d"}) { - bool threw = false; - try { snprintf_nid_postfix(freebsd, sizeof(freebsd), unsupported, 1); } catch (const std::invalid_argument&) { threw = true; } - Require(threw); - } - Require(snprintf_nid_postfix(freebsd, sizeof(freebsd), "100%") == 3 && std::strcmp(freebsd, "100") == 0); - Require(snprintf_nid_postfix(freebsd, sizeof(freebsd), "%d done 5% ", 7) == 8 && std::strcmp(freebsd, "7 done 5") == 0); - Require(snprintf_nid_postfix(freebsd, 4, "ab%dcd%", 12) == 6 && std::strcmp(freebsd, "ab1") == 0); #endif >>>>>>> 5a3fd9f2ca (fix(libc): print the FreeBSD printf extensions and stray percent signs on Windows) snprintf_nid_postfix(buffer, sizeof(buffer), "%.*s", -1, "unlimited"); @@ -392,6 +377,23 @@ __attribute__((noinline)) static void APS5_VABI RunChecks() { } char narrow[8] = {}; Require(sscanf_nid_postfix("ab%", "%7[^%ls]", narrow) == 1 && std::strcmp(narrow, "ab") == 0); +#endif +#ifdef _WIN32 + char freebsd[64]; + Require(snprintf_nid_postfix(freebsd, sizeof(freebsd), "%qd|%qu|%qx|%qo", -5000000000LL, 5000000000ULL, 255ULL, 8ULL) == 28 && + std::strcmp(freebsd, "-5000000000|5000000000|ff|10") == 0); + Require(snprintf_nid_postfix(freebsd, sizeof(freebsd), "%D|%hD|%O|%U|%jD", -5000000000L, 5000000000L, 8L, 5000000000UL, -6000000000LL) == 48 && + std::strcmp(freebsd, "-5000000000|5000000000|10|5000000000|-6000000000") == 0); + Require(snprintf_nid_postfix(freebsd, sizeof(freebsd), "[%5%][%-5%][%y][%5y][%-3y]") == 29 && + std::strcmp(freebsd, "[ %][% ][y][ y][y ]") == 0); + for (const char* unsupported : {"%m", "%1$d", "%'d", "%l5d", "%lqd", "%qc", "%lhd", "%l*d"}) { + bool threw = false; + try { snprintf_nid_postfix(freebsd, sizeof(freebsd), unsupported, 1); } catch (const std::invalid_argument&) { threw = true; } + Require(threw); + } + Require(snprintf_nid_postfix(freebsd, sizeof(freebsd), "100%") == 3 && std::strcmp(freebsd, "100") == 0); + Require(snprintf_nid_postfix(freebsd, sizeof(freebsd), "%d done 5% ", 7) == 8 && std::strcmp(freebsd, "7 done 5") == 0); + Require(snprintf_nid_postfix(freebsd, 4, "ab%dcd%", 12) == 6 && std::strcmp(freebsd, "ab1") == 0); #endif long long count = -1; int smallCount = -1; From 87cf00de0da144a889eac62b40e65bc65fbf6549 Mon Sep 17 00:00:00 2001 From: MarcZX100 Date: Tue, 13 Oct 2026 03:41:28 +0000 Subject: [PATCH 4/7] test(libc): drop printf formats now supported on main --- core/libs/tests/Formatting.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/core/libs/tests/Formatting.cpp b/core/libs/tests/Formatting.cpp index 544221b7131e..f46009ea5d02 100644 --- a/core/libs/tests/Formatting.cpp +++ b/core/libs/tests/Formatting.cpp @@ -386,7 +386,7 @@ __attribute__((noinline)) static void APS5_VABI RunChecks() { std::strcmp(freebsd, "-5000000000|5000000000|10|5000000000|-6000000000") == 0); Require(snprintf_nid_postfix(freebsd, sizeof(freebsd), "[%5%][%-5%][%y][%5y][%-3y]") == 29 && std::strcmp(freebsd, "[ %][% ][y][ y][y ]") == 0); - for (const char* unsupported : {"%m", "%1$d", "%'d", "%l5d", "%lqd", "%qc", "%lhd", "%l*d"}) { + for (const char* unsupported : {"%m", "%l5d", "%lqd", "%qc", "%lhd", "%l*d"}) { bool threw = false; try { snprintf_nid_postfix(freebsd, sizeof(freebsd), unsupported, 1); } catch (const std::invalid_argument&) { threw = true; } Require(threw); From 11fc4b3e1a399c046b4b12b81f7ea13b4099f8e9 Mon Sep 17 00:00:00 2001 From: MarcZX10 <67475268+MarcZX10@users.noreply.github.com> Date: Mon, 19 Oct 2026 15:08:57 +0000 Subject: [PATCH 5/7] fix(libc): resolve printf rebase conflicts --- core/libs/tests/Formatting.cpp | 13 ------------- 1 file changed, 13 deletions(-) diff --git a/core/libs/tests/Formatting.cpp b/core/libs/tests/Formatting.cpp index f46009ea5d02..fd9540998f90 100644 --- a/core/libs/tests/Formatting.cpp +++ b/core/libs/tests/Formatting.cpp @@ -255,7 +255,6 @@ __attribute__((noinline)) static void APS5_VABI RunChecks() { Require(std::strcmp(buffer, expected) == 0); char pointers[128]; const auto address = [](std::uintptr_t value) { return reinterpret_cast(value); }; -<<<<<<< HEAD Require(snprintf_nid_postfix(pointers, sizeof(pointers), "%p|%p|%p", address(0), address(0x1234), address(0xdeadbeef12)) == 17 && std::strcmp(pointers, "0|1234|deadbeef12") == 0); Require(snprintf_nid_postfix(pointers, sizeof(pointers), "[%p][%5p][%-5p][%05p][%.8p][%10.6p][%.0p]", @@ -272,18 +271,6 @@ __attribute__((noinline)) static void APS5_VABI RunChecks() { Require(snprintf_nid_postfix(pointers, 6, "%p", address(0xabcdef)) == 6 && std::strcmp(pointers, "abcde") == 0); Require(snprintf_nid_postfix(pointers, sizeof(pointers), "%p", address(0xffffffffffffffffull)) == 16 && std::strcmp(pointers, "ffffffffffffffff") == 0); -======= - Require(snprintf_nid_postfix(pointers, sizeof(pointers), "%p|%p|%p", address(0), address(0x1234), address(0xdeadbeef12)) == 23 && - std::strcmp(pointers, "0x0|0x1234|0xdeadbeef12") == 0); - Require(snprintf_nid_postfix(pointers, sizeof(pointers), "[%8p][%-8p][%08p][%.6p][%10.4p][%.0p]", - address(0x1a), address(0x1a), address(0x1a), address(0x1a), address(0x1a), address(0)) == 56 && - std::strcmp(pointers, "[ 0x1a][0x1a ][0x00001a][0x00001a][ 0x001a][0x]") == 0); - Require(snprintf_nid_postfix(pointers, sizeof(pointers), "[%+p][% p][%#p][%*p][%-*p]", address(0x1a), address(0x1a), - address(0x1a), 6, address(0xf), -6, address(0xf)) == 34 && - std::strcmp(pointers, "[0x1a][0x1a][0x1a][ 0xf][0xf ]") == 0); - Require(snprintf_nid_postfix(pointers, 6, "%p", address(0xabcdef)) == 8 && std::strcmp(pointers, "0xabc") == 0); -#endif ->>>>>>> 5a3fd9f2ca (fix(libc): print the FreeBSD printf extensions and stray percent signs on Windows) snprintf_nid_postfix(buffer, sizeof(buffer), "%.*s", -1, "unlimited"); Require(std::strcmp(buffer, "unlimited") == 0); buffer[5] = '!'; From a3e41499bec974562b5b1cb75205156843593119 Mon Sep 17 00:00:00 2001 From: MarcZX10 <67475268+MarcZX10@users.noreply.github.com> Date: Sat, 24 Oct 2026 00:46:50 +0000 Subject: [PATCH 6/7] fix(libc): match title printf conversions on Windows --- .../prx/libc/include/WindowsFormatting.hpp | 21 ++++++++++--------- core/libs/tests/Formatting.cpp | 21 ++++++++++--------- docs/dev/debt/libc.md | 2 +- 3 files changed, 23 insertions(+), 21 deletions(-) diff --git a/core/libs/prx/libc/include/WindowsFormatting.hpp b/core/libs/prx/libc/include/WindowsFormatting.hpp index 1afc24195b4b..a00cc1eb815d 100644 --- a/core/libs/prx/libc/include/WindowsFormatting.hpp +++ b/core/libs/prx/libc/include/WindowsFormatting.hpp @@ -279,8 +279,11 @@ inline int FormatWindows(char* buffer, size_t size, const char* format, const vo } if (const size_t position = ArgumentPosition(format)) args.Seek(position); std::string spec = "%"; - for (; *format && std::strchr("-+ #0'", *format); ++format) + bool unsupportedUnknownFlag = false; + for (; *format && std::strchr("-+ #0'", *format); ++format) { if (*format != '\'') spec += *format; + if (*format != '-') unsupportedUnknownFlag = true; + } if (*format == '*') { ++format; const int width = star(); @@ -311,19 +314,14 @@ inline int FormatWindows(char* buffer, size_t size, const char* format, const vo } } std::string length; - if (*format && std::strchr("hljztLq", *format)) { + if (*format && std::strchr("hljztL", *format)) { length += *format++; if ((length == "h" && *format == 'h') || (length == "l" && *format == 'l')) length += *format++; } - if (length == "q") length = "ll"; char conversion = *format; - if (!conversion) break; + if (!conversion) throw std::invalid_argument("Incomplete format conversion"); ++format; - if (conversion == 'D' || conversion == 'O' || conversion == 'U') { - if (length != "j" && length != "z" && length != "t" && length != "ll") length = "l"; - conversion = static_cast(conversion + 'a' - 'A'); - } const bool integerLength = length.empty() || length == "h" || length == "hh" || length == "l" || length == "ll" || length == "j" || length == "z" || length == "t"; if (conversion == 'd' || conversion == 'i') { @@ -409,8 +407,11 @@ inline int FormatWindows(char* buffer, size_t size, const char* format, const vo else if (length == "h") *static_cast(pointer) = static_cast(count); else if (length.empty()) *static_cast(pointer) = count; else *static_cast(pointer) = count; - } else if (!std::strchr(" #*-+'.0123456789$LhjlqtzCcDdiaAeEfFgGmnOopSsUuXx", conversion)) { - output.Value(spec + 'c', static_cast(static_cast(conversion))); + } else if (!std::strchr("diouxXaAeEfFgGcsSpnmCS", conversion)) { + if (secure || unsupportedUnknownFlag || spec.find_first_not_of("%-0123456789") != std::string::npos) + throw std::invalid_argument("Unsupported format conversion '" + spec + length + conversion + "' in \"" + std::string(formatStart).substr(0, 160) + "\""); + const std::string text = conversion == '%' ? "%" : std::string("%") + conversion; + output.Value(spec + 's', text.c_str()); } else { throw std::invalid_argument("Unsupported format conversion '" + spec + length + conversion + "' in \"" + std::string(formatStart).substr(0, 160) + "\""); } diff --git a/core/libs/tests/Formatting.cpp b/core/libs/tests/Formatting.cpp index fd9540998f90..2eafd73d684c 100644 --- a/core/libs/tests/Formatting.cpp +++ b/core/libs/tests/Formatting.cpp @@ -367,20 +367,21 @@ __attribute__((noinline)) static void APS5_VABI RunChecks() { #endif #ifdef _WIN32 char freebsd[64]; - Require(snprintf_nid_postfix(freebsd, sizeof(freebsd), "%qd|%qu|%qx|%qo", -5000000000LL, 5000000000ULL, 255ULL, 8ULL) == 28 && - std::strcmp(freebsd, "-5000000000|5000000000|ff|10") == 0); - Require(snprintf_nid_postfix(freebsd, sizeof(freebsd), "%D|%hD|%O|%U|%jD", -5000000000L, 5000000000L, 8L, 5000000000UL, -6000000000LL) == 48 && - std::strcmp(freebsd, "-5000000000|5000000000|10|5000000000|-6000000000") == 0); - Require(snprintf_nid_postfix(freebsd, sizeof(freebsd), "[%5%][%-5%][%y][%5y][%-3y]") == 29 && - std::strcmp(freebsd, "[ %][% ][y][ y][y ]") == 0); - for (const char* unsupported : {"%m", "%l5d", "%lqd", "%qc", "%lhd", "%l*d"}) { + Require(snprintf_nid_postfix(freebsd, sizeof(freebsd), "%qd|%qu|%qx|%qo") == 15 && + std::strcmp(freebsd, "%qd|%qu|%qx|%qo") == 0); + Require(snprintf_nid_postfix(freebsd, sizeof(freebsd), "%qd|%d", 7) == 5 && std::strcmp(freebsd, "%qd|7") == 0); + Require(snprintf_nid_postfix(freebsd, sizeof(freebsd), "%D|%hD|%O|%U|%jD") == 19 && + std::strcmp(freebsd, "%D|%D|%O|%U|%D") == 0); + Require(snprintf_nid_postfix(freebsd, sizeof(freebsd), "[%5%][%-5%][%y][%5y][%-3y][%hhz]") == 34 && + std::strcmp(freebsd, "[ %][% ][%y][ %y][%y ][%z]") == 0); + for (const char* unsupported : {"%+y", "%#y", "%05y", "%.2y"}) { bool threw = false; try { snprintf_nid_postfix(freebsd, sizeof(freebsd), unsupported, 1); } catch (const std::invalid_argument&) { threw = true; } Require(threw); } - Require(snprintf_nid_postfix(freebsd, sizeof(freebsd), "100%") == 3 && std::strcmp(freebsd, "100") == 0); - Require(snprintf_nid_postfix(freebsd, sizeof(freebsd), "%d done 5% ", 7) == 8 && std::strcmp(freebsd, "7 done 5") == 0); - Require(snprintf_nid_postfix(freebsd, 4, "ab%dcd%", 12) == 6 && std::strcmp(freebsd, "ab1") == 0); + bool incomplete = false; + try { snprintf_nid_postfix(freebsd, sizeof(freebsd), "100%"); } catch (const std::invalid_argument&) { incomplete = true; } + Require(incomplete); #endif long long count = -1; int smallCount = -1; diff --git a/docs/dev/debt/libc.md b/docs/dev/debt/libc.md index d4f154acfb5d..3d8aa5ff2ff0 100644 --- a/docs/dev/debt/libc.md +++ b/docs/dev/debt/libc.md @@ -156,4 +156,4 @@ - [ios_base standard streams](../../../core/libs/prx/libc/src/LocaleSupport.cpp) (libc) `_Addstd` queues the stream in the standard stream table of Dinkumware [ios.cpp](https://github.com/ganboing/crt.vc12/blob/master/src/ios.cpp) (`stdstr`, `stdopens`, `_Addstd`, `_Ios_base_dtor`): it takes the first of slots 1 to 7 that is free or already holds the stream, and slot 8 (`NSTDSTR`) when none is, so slot 8 is shared by every stream past the first seven and registration never fails. It stores the 1-based slot index in the guest `IosBase::standardStream` (Dinkumware's `_Stdstr`, where 0 means not a standard stream) and counts the stream objects registered per slot; registering the same stream again reuses its slot and raises its count. Measured on a PS5 (firmware 13.60) from a homebrew payload calling the system `libSceLibcInternal` export: in a process whose own standard streams already hold slots 1 to 7, twelve distinct streams and a repeated one all get 8, written as a 64-bit value at offset 0x8 with no other byte of the object touched; the assignment of slots 1 to 7 to fresh streams is not observable there and follows VC12. The counts are `int`, not VC12's `char`, and the registration and the destructor lock the table where VC12 locks only `_Addstd`; a slot index past the table throws in the destructor instead of indexing out of bounds. The destructor decrements its slot's count and suppresses the rest of the destruction while the count stays positive, as `_Ios_base_dtor` does, and the slots of destroyed streams keep their pointer and stay reserved for that stream, as `stdstr` does. The streams themselves are destroyed by the title's own code as it registers them through `__cxa_atexit`; when the last reference closes, the unsupported stream storage and callback cleanup is still the throw it was and the locale is released as for a non-standard stream. - [std::_Pad, _Thrd_join, _Thrd_detach, _Thrd_id](../../../core/libs/prx/libc/src/Threads.cpp) (libc) follow Dinkumware V6.00 `thr/xthread` and `thr/threadcall.cpp` ([crt.vc12 `f1d47126`](https://github.com/ganboing/crt.vc12/tree/f1d47126/src/thr)), without console verification. `_Thrd_id` throws because whether the console returns `pthread_t` or a kernel thread ID and at what width is unknown; `_Launch` omits `_Cnd_do_broadcast_at_thread_exit` and calls `pthread_create` registered by libkernel with default attributes. The 32-byte pad layout (`_Go()` vtable at 0x0, `_Cnd_t` at 0x8, `_Mtx_t` at 0x10, `bool _Started` at 0x18) is assumed from VC12, unverified. Failing primitives or thread start throw the guest `std::system_error`; null arguments throw. `_Thrd_join` returns `_Thrd_error` (4) on `pthread_join` failure and truncates the exit code to `int`; `_Thrd_detach` returns `_Thrd_error` (4) on `pthread_detach` failure and throws when none is registered - [num_get::id, time_get::id and the num_get vtable](../../../core/libs/prx/libc/src/LocaleSupport.cpp) (libc) are data objects, as the public SDK's libSceLibcInternal table declares them and as the other facet ids and the `num_put` and `codecvt` vtables are kept: the ids start at zero and the vtable is 17 zeroed slots, the 136-byte size of `KfcTPbeaOqg` in the console's libSceLibcInternal, whose relocations fill the offset-to-top and typeinfo entries, the two destructors, `locale::facet::_Incref` and `_Decref`, and the eleven `do_get` of Dinkumware's crt.vc12 `xlocnum`, so a guest resolves the symbols and reads data, and a facet call through the vtable faults. libSceAmpr exported the three as throwing functions, which a data consumer reads as code; its qualified imports fall back to libc. -- [printf family conversions](../../../core/libs/prx/libc/include/WindowsFormatting.hpp) (libc) - on Windows the `q` length modifier, the `%D`, `%O` and `%U` conversions, an unknown conversion character (printed as itself with the width and `-` flag, as `%c` would) and a `%` that ends the format (output stops there) follow FreeBSD 11.4 [vfprintf.c](https://github.com/freebsd/freebsd-src/blob/release/11.4.0/lib/libc/stdio/vfprintf.c). This records the host implementation; the console's Dinkumware `_Printf` behavior for these cases is unverified and may differ. The `0` flag on an unknown conversion is handled by the host `%c`, which pads with blanks where FreeBSD pads with zeroes. Linux formats these through glibc, which prints `%D`, `%O`, `%U` and unknown conversions as written +- [printf family conversions](../../../core/libs/prx/libc/include/WindowsFormatting.hpp) (libc) - Windows `FormatWindows` follows the title `libc.prx` `_Printf` measured from PPSA13934, PPSA07923, PPSA05916 and PPSA04158: `%q`, `%D`, `%O` and `%U` are unknown conversions; an unknown character is printed with its leading `%`, and width, `-` and discarded length modifiers behave as measured (`%5y` is ` %y`, `%hhz` is `%z`). `%%` takes a width. A trailing `%` reads past the format's NUL and is not reproduced; it throws. Other flags and precision on unknown conversions are unverified and throw. Linux formats these through glibc, which prints `%D`, `%O`, `%U` and unknown conversions as written From a5e60928eceb10e49d2436f67745d7f9c31b04b2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Adri=C3=A0=20Franch?= Date: Sat, 24 Oct 2026 10:22:18 +0200 Subject: [PATCH 7/7] test(libc): fix expected length of the printf unknown conversion case --- core/libs/tests/Formatting.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/core/libs/tests/Formatting.cpp b/core/libs/tests/Formatting.cpp index 2eafd73d684c..5d1a9f5a80be 100644 --- a/core/libs/tests/Formatting.cpp +++ b/core/libs/tests/Formatting.cpp @@ -370,7 +370,7 @@ __attribute__((noinline)) static void APS5_VABI RunChecks() { Require(snprintf_nid_postfix(freebsd, sizeof(freebsd), "%qd|%qu|%qx|%qo") == 15 && std::strcmp(freebsd, "%qd|%qu|%qx|%qo") == 0); Require(snprintf_nid_postfix(freebsd, sizeof(freebsd), "%qd|%d", 7) == 5 && std::strcmp(freebsd, "%qd|7") == 0); - Require(snprintf_nid_postfix(freebsd, sizeof(freebsd), "%D|%hD|%O|%U|%jD") == 19 && + Require(snprintf_nid_postfix(freebsd, sizeof(freebsd), "%D|%hD|%O|%U|%jD") == 14 && std::strcmp(freebsd, "%D|%D|%O|%U|%D") == 0); Require(snprintf_nid_postfix(freebsd, sizeof(freebsd), "[%5%][%-5%][%y][%5y][%-3y][%hhz]") == 34 && std::strcmp(freebsd, "[ %][% ][%y][ %y][%y ][%z]") == 0);