From 4bf4fc9864bc9189356f69f716119bef2d051d45 Mon Sep 17 00:00:00 2001 From: tecmo Date: Wed, 14 Oct 2026 19:29:56 -0500 Subject: [PATCH 1/2] feat(libc): implement ios_base::_Addstd Queue the stream in the table of the 8 standard stream slots of Dinkumware's ios.cpp: the first free slot or the one that already holds the stream, with a reference count per slot, and the destructor suppresses the destruction of a standard stream while its slot still has open references. --- core/libs/prx/libc/src/LocaleSupport.cpp | 26 ++++++++++++++- core/libs/prx/libc/src/RuntimeSupport.cpp | 4 --- core/libs/tests/GuestLocale.cpp | 39 +++++++++++++++++++++++ docs/dev/debt/libc.md | 1 + 4 files changed, 65 insertions(+), 5 deletions(-) diff --git a/core/libs/prx/libc/src/LocaleSupport.cpp b/core/libs/prx/libc/src/LocaleSupport.cpp index e2e687de7fa3..656e5676bc4d 100644 --- a/core/libs/prx/libc/src/LocaleSupport.cpp +++ b/core/libs/prx/libc/src/LocaleSupport.cpp @@ -35,6 +35,11 @@ std::mutex g_localeInitMutex; bool g_localeInitialized = false; std::vector g_registeredFacets; +constexpr std::int64_t StandardStreamSlots = 8; +std::mutex g_standardStreamMutex; +GuestLocale::IosBase* g_standardStreams[StandardStreamSlots + 1] = {}; +int g_standardStreamOpens[StandardStreamSlots + 1] = {}; + void APS5_VABI DestroyClassicLocale(GuestLocale::Facet*) { throw std::runtime_error("Cannot destroy the classic locale"); } @@ -302,7 +307,12 @@ GuestLocale::Implementation* _ZSt21_sceLibcClassicLocale_nid_postfix = &g_classi void APS5_VABI _ZNSt8ios_baseD2Ev_nid_postfix(GuestLocale::IosBase* self) { if (self == nullptr) throw std::invalid_argument("ios_base destructor: null object"); - if (self->standardStream != 0 || self->storage != nullptr || self->callbacks != nullptr) throw std::runtime_error("ios_base destructor: unsupported stream storage or callbacks"); + std::lock_guard lock(g_standardStreamMutex); + if (self->standardStream > 0) { + if (self->standardStream > StandardStreamSlots) throw std::runtime_error("ios_base destructor: unknown standard stream slot"); + if (0 < --g_standardStreamOpens[self->standardStream]) return; + } + if (self->storage != nullptr || self->callbacks != nullptr) throw std::runtime_error("ios_base destructor: unsupported stream storage or callbacks"); if (self->locale != &_ZSt21_sceLibcClassicLocale_nid_postfix) throw std::runtime_error("ios_base destructor: unsupported locale ownership"); self->locale = nullptr; } @@ -324,6 +334,20 @@ StreamManipulator APS5_VABI _ZSt4setwi_nid_postfix(std::int32_t width) { return {&SetStreamWidth, width}; } +void APS5_VABI _ZNSt8ios_base7_AddstdEPS__nid_postfix(GuestLocale::IosBase* self) { + if (self == nullptr) throw std::invalid_argument("ios_base _Addstd: null stream"); + std::lock_guard lock(g_standardStreamMutex); + for (self->standardStream = 0; ++self->standardStream <= StandardStreamSlots; ) { + if (g_standardStreams[self->standardStream] == nullptr || g_standardStreams[self->standardStream] == self) break; + } + if (self->standardStream > StandardStreamSlots) { + self->standardStream = 0; + throw std::runtime_error("ios_base _Addstd: every standard stream slot is taken"); + } + g_standardStreams[self->standardStream] = self; + ++g_standardStreamOpens[self->standardStream]; +} + // Dinkumware's static locale::_Init(bool) returns the global _Locimp, which the caller copies. GuestLocale::Implementation* APS5_VABI _ZNSt6locale5_InitEv_nid_postfix() { std::lock_guard lock(g_localeInitMutex); diff --git a/core/libs/prx/libc/src/RuntimeSupport.cpp b/core/libs/prx/libc/src/RuntimeSupport.cpp index 97995bf9d21e..8fc522804af8 100644 --- a/core/libs/prx/libc/src/RuntimeSupport.cpp +++ b/core/libs/prx/libc/src/RuntimeSupport.cpp @@ -196,8 +196,4 @@ __int128 APS5_VABI __modti3_nid_postfix(__int128 dividend, __int128 divisor) { return SonyModti3(dividend, divisor); } -void APS5_VABI _ZNSt8ios_base7_AddstdEPS__nid_postfix(void*) { - NotImplemented_nid_no_patch(__func__); -} - } diff --git a/core/libs/tests/GuestLocale.cpp b/core/libs/tests/GuestLocale.cpp index a72216251bc0..679f2b01f1bb 100644 --- a/core/libs/tests/GuestLocale.cpp +++ b/core/libs/tests/GuestLocale.cpp @@ -24,6 +24,7 @@ struct StreamManipulator { std::int32_t argument; }; StreamManipulator APS5_VABI _ZSt4setwi_nid_postfix(std::int32_t width); +void APS5_VABI _ZNSt8ios_base7_AddstdEPS__nid_postfix(GuestLocale::IosBase* self); const short* APS5_VABI _Getpctype_nid_postfix(); const short* APS5_VABI _Getptolower_nid_postfix(); const short* APS5_VABI _Getptoupper_nid_postfix(); @@ -176,6 +177,43 @@ static void CheckStreamWidth() { RequireException([&] { manipulator.function(nullptr, manipulator.argument); }); } +static void CheckStandardStreams() { + struct GuardedStream { + std::uint64_t before = 0x12345678; + GuestLocale::IosBase stream{}; + std::uint64_t after = 0x87654321; + } streams[9]; + auto* classic = &_ZSt21_sceLibcClassicLocale_nid_postfix; + for (auto& object : streams) object.stream.locale = classic; + RequireException([&] { _ZNSt8ios_base7_AddstdEPS__nid_postfix(nullptr); }); + _ZNSt8ios_base7_AddstdEPS__nid_postfix(&streams[0].stream); + _ZNSt8ios_base7_AddstdEPS__nid_postfix(&streams[1].stream); + Require(streams[0].stream.standardStream == 1 && streams[1].stream.standardStream == 2); + _ZNSt8ios_base7_AddstdEPS__nid_postfix(&streams[0].stream); + Require(streams[0].stream.standardStream == 1); + _ZNSt8ios_baseD2Ev_nid_postfix(&streams[0].stream); + Require(streams[0].stream.locale == classic && streams[0].stream.standardStream == 1); + _ZNSt8ios_baseD2Ev_nid_postfix(&streams[0].stream); + Require(streams[0].stream.locale == nullptr && streams[0].stream.standardStream == 1); + streams[0].stream.locale = classic; + _ZNSt8ios_base7_AddstdEPS__nid_postfix(&streams[0].stream); + Require(streams[0].stream.standardStream == 1); + _ZNSt8ios_baseD2Ev_nid_postfix(&streams[1].stream); + Require(streams[1].stream.locale == nullptr && streams[1].stream.standardStream == 2); + for (int index = 2; index < 8; ++index) { + _ZNSt8ios_base7_AddstdEPS__nid_postfix(&streams[index].stream); + Require(streams[index].stream.standardStream == index + 1); + } + Require(streams[8].stream.standardStream == 0); + RequireException([&] { _ZNSt8ios_base7_AddstdEPS__nid_postfix(&streams[8].stream); }); + Require(streams[8].stream.standardStream == 0); + for (int index = 2; index < 8; ++index) { + _ZNSt8ios_baseD2Ev_nid_postfix(&streams[index].stream); + Require(streams[index].stream.locale == nullptr && streams[index].stream.standardStream == index + 1); + } + for (auto& object : streams) Require(object.before == 0x12345678 && object.after == 0x87654321); +} + static void CheckWideCaseTranslation() { const auto* lower = _Getptolower_nid_postfix(); const auto* upper = _Getptoupper_nid_postfix(); @@ -205,6 +243,7 @@ int main() { CheckCharacterConversions(); CheckStreamDestruction(); CheckStreamWidth(); + CheckStandardStreams(); CheckWideCaseTranslation(); _Locksyslock_nid_postfix(); _Locksyslock_nid_postfix(); diff --git a/docs/dev/debt/libc.md b/docs/dev/debt/libc.md index ff94841f6719..e0c2b7587fda 100644 --- a/docs/dev/debt/libc.md +++ b/docs/dev/debt/libc.md @@ -113,3 +113,4 @@ - [_Dtest](../../../core/libs/prx/libc/src/Math.cpp) (libc) follows Dinkumware double classification matching _FDtest: finite values return -1, subnormals -2, zeros 0, infinities 1, and NaNs 2. - [app0.zar](../../../core/libs/prx/libc/src/AppArchive.cpp) (libc) - a [ZArchive](https://github.com/Exzap/ZArchive) beside `app0/` is a read-only layer under it: a path missing from `app0/` is looked up in the archive, case-insensitively. It is not a console format; titles ship their files in a PFS image instead. Resolving an archive-only path through any call other than the archive-aware libkernel ones throws, so `fopen`, `stat`, `access`, `opendir` and the rest do not read the archive yet, and `opendir` of a directory that exists in both places, `app0/` itself included, and `fdopen` of an archived descriptor throw instead of missing its entries or reading the null device. A damaged compressed block throws instead of returning `EIO` - [fopen_s](../../../core/libs/prx/libc/src/FileAndHeap.cpp) (libc) - returns the errno of the failed open, as C11 K.3.5.2.1 leaves the nonzero value open and Microsoft's CRT returns errno; the console's Dinkumware `fopen_s` is not verified +- [ios_base standard streams](../../../core/libs/prx/libc/src/LocaleSupport.cpp) (libc) `_Addstd` queues the stream in the table of the 8 standard stream slots 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 free slot or the one that already holds the stream, 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. Where a ninth stream makes VC12 write past `stdstr[NSTDSTR]` into the arrays' slack, a full table resets the refused stream's slot to 0 and throws, as unexpected states strictly throw. 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. The console's exact standard stream table is unverified. \ No newline at end of file From 6fdb88fd48af62c30bec2903b2b3a2a9b7f83fb6 Mon Sep 17 00:00:00 2001 From: T3cm0 Date: Fri, 16 Oct 2026 18:49:37 -0500 Subject: [PATCH 2/2] fix(libc): share slot 8 in ios_base::_Addstd as the console does --- core/libs/prx/libc/src/LocaleSupport.cpp | 6 +----- core/libs/tests/GuestLocale.cpp | 14 +++++++++----- docs/dev/debt/libc.md | 2 +- 3 files changed, 11 insertions(+), 11 deletions(-) diff --git a/core/libs/prx/libc/src/LocaleSupport.cpp b/core/libs/prx/libc/src/LocaleSupport.cpp index 656e5676bc4d..a4388c8dfe5d 100644 --- a/core/libs/prx/libc/src/LocaleSupport.cpp +++ b/core/libs/prx/libc/src/LocaleSupport.cpp @@ -337,13 +337,9 @@ StreamManipulator APS5_VABI _ZSt4setwi_nid_postfix(std::int32_t width) { void APS5_VABI _ZNSt8ios_base7_AddstdEPS__nid_postfix(GuestLocale::IosBase* self) { if (self == nullptr) throw std::invalid_argument("ios_base _Addstd: null stream"); std::lock_guard lock(g_standardStreamMutex); - for (self->standardStream = 0; ++self->standardStream <= StandardStreamSlots; ) { + for (self->standardStream = 0; ++self->standardStream < StandardStreamSlots; ) { if (g_standardStreams[self->standardStream] == nullptr || g_standardStreams[self->standardStream] == self) break; } - if (self->standardStream > StandardStreamSlots) { - self->standardStream = 0; - throw std::runtime_error("ios_base _Addstd: every standard stream slot is taken"); - } g_standardStreams[self->standardStream] = self; ++g_standardStreamOpens[self->standardStream]; } diff --git a/core/libs/tests/GuestLocale.cpp b/core/libs/tests/GuestLocale.cpp index 679f2b01f1bb..71b71a16690a 100644 --- a/core/libs/tests/GuestLocale.cpp +++ b/core/libs/tests/GuestLocale.cpp @@ -200,14 +200,18 @@ static void CheckStandardStreams() { Require(streams[0].stream.standardStream == 1); _ZNSt8ios_baseD2Ev_nid_postfix(&streams[1].stream); Require(streams[1].stream.locale == nullptr && streams[1].stream.standardStream == 2); - for (int index = 2; index < 8; ++index) { + for (int index = 2; index < 7; ++index) { _ZNSt8ios_base7_AddstdEPS__nid_postfix(&streams[index].stream); Require(streams[index].stream.standardStream == index + 1); } - Require(streams[8].stream.standardStream == 0); - RequireException([&] { _ZNSt8ios_base7_AddstdEPS__nid_postfix(&streams[8].stream); }); - Require(streams[8].stream.standardStream == 0); - for (int index = 2; index < 8; ++index) { + _ZNSt8ios_base7_AddstdEPS__nid_postfix(&streams[7].stream); + _ZNSt8ios_base7_AddstdEPS__nid_postfix(&streams[8].stream); + Require(streams[7].stream.standardStream == 8 && streams[8].stream.standardStream == 8); + _ZNSt8ios_baseD2Ev_nid_postfix(&streams[7].stream); + Require(streams[7].stream.locale == classic); + _ZNSt8ios_baseD2Ev_nid_postfix(&streams[8].stream); + Require(streams[8].stream.locale == nullptr); + for (int index = 2; index < 7; ++index) { _ZNSt8ios_baseD2Ev_nid_postfix(&streams[index].stream); Require(streams[index].stream.locale == nullptr && streams[index].stream.standardStream == index + 1); } diff --git a/docs/dev/debt/libc.md b/docs/dev/debt/libc.md index e0c2b7587fda..d065604851ad 100644 --- a/docs/dev/debt/libc.md +++ b/docs/dev/debt/libc.md @@ -113,4 +113,4 @@ - [_Dtest](../../../core/libs/prx/libc/src/Math.cpp) (libc) follows Dinkumware double classification matching _FDtest: finite values return -1, subnormals -2, zeros 0, infinities 1, and NaNs 2. - [app0.zar](../../../core/libs/prx/libc/src/AppArchive.cpp) (libc) - a [ZArchive](https://github.com/Exzap/ZArchive) beside `app0/` is a read-only layer under it: a path missing from `app0/` is looked up in the archive, case-insensitively. It is not a console format; titles ship their files in a PFS image instead. Resolving an archive-only path through any call other than the archive-aware libkernel ones throws, so `fopen`, `stat`, `access`, `opendir` and the rest do not read the archive yet, and `opendir` of a directory that exists in both places, `app0/` itself included, and `fdopen` of an archived descriptor throw instead of missing its entries or reading the null device. A damaged compressed block throws instead of returning `EIO` - [fopen_s](../../../core/libs/prx/libc/src/FileAndHeap.cpp) (libc) - returns the errno of the failed open, as C11 K.3.5.2.1 leaves the nonzero value open and Microsoft's CRT returns errno; the console's Dinkumware `fopen_s` is not verified -- [ios_base standard streams](../../../core/libs/prx/libc/src/LocaleSupport.cpp) (libc) `_Addstd` queues the stream in the table of the 8 standard stream slots 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 free slot or the one that already holds the stream, 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. Where a ninth stream makes VC12 write past `stdstr[NSTDSTR]` into the arrays' slack, a full table resets the refused stream's slot to 0 and throws, as unexpected states strictly throw. 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. The console's exact standard stream table is unverified. \ No newline at end of file +- [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.