Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 21 additions & 1 deletion core/libs/prx/libc/src/LocaleSupport.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,11 @@ std::mutex g_localeInitMutex;
bool g_localeInitialized = false;
std::vector<GuestLocale::Facet*> 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");
}
Expand Down Expand Up @@ -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;
}
Expand All @@ -324,6 +334,16 @@ 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;
}
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<std::mutex> lock(g_localeInitMutex);
Expand Down
4 changes: 0 additions & 4 deletions core/libs/prx/libc/src/RuntimeSupport.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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__);
}

}
43 changes: 43 additions & 0 deletions core/libs/tests/GuestLocale.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down Expand Up @@ -176,6 +177,47 @@ 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 < 7; ++index) {
_ZNSt8ios_base7_AddstdEPS__nid_postfix(&streams[index].stream);
Require(streams[index].stream.standardStream == index + 1);
}
_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);
}
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();
Expand Down Expand Up @@ -205,6 +247,7 @@ int main() {
CheckCharacterConversions();
CheckStreamDestruction();
CheckStreamWidth();
CheckStandardStreams();
CheckWideCaseTranslation();
_Locksyslock_nid_postfix();
_Locksyslock_nid_postfix();
Expand Down
1 change: 1 addition & 0 deletions docs/dev/debt/libc.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 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.

Check warning on line 116 in docs/dev/debt/libc.md

View workflow job for this annotation

GitHub Actions / conventions

debt-length

Technical debt entry over 1000 characters: state the gap first (what differs from the console, what is unverified, what throws) and keep only the behaviour it needs: 1882 characters
Loading