From 1860f903040b49cf0e227e500faaa3d28aea72b1 Mon Sep 17 00:00:00 2001 From: hypermist Date: Wed, 21 Oct 2026 22:47:12 +1300 Subject: [PATCH] feat(libc): add std::system_category and store error_code values at offset 4 The console error_code and error_condition keep the value in the second dword: the shared error_category::equivalent reads it at +4, the default_error_condition overrides return it shifted into the high half, and title code built against the SDK headers stores 0 at +0 and reads value() at +4. The generic category returned and compared the value at +0. system_category returns a static _System_error_category named "system" that shares the generic message table and maps a value with a message to the generic category in default_error_condition. The equivalent overloads are shared by both categories and go through the object's own default_error_condition, as the console's base implementation does. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01RbLgECLQ2bpDM7oe2urGZK --- .../libc/src/exception/StandardExceptions.cpp | 56 +++++++++++++------ core/libs/tests/GuestAmprCppError.cpp | 8 +-- core/libs/tests/GuestAmprThreads.cpp | 1 + core/libs/tests/GuestGenericCategory.cpp | 44 ++++++++++++++- 4 files changed, 84 insertions(+), 25 deletions(-) diff --git a/core/libs/prx/libc/src/exception/StandardExceptions.cpp b/core/libs/prx/libc/src/exception/StandardExceptions.cpp index 31feeb3d8ed2..bf6b539f8201 100644 --- a/core/libs/prx/libc/src/exception/StandardExceptions.cpp +++ b/core/libs/prx/libc/src/exception/StandardExceptions.cpp @@ -129,9 +129,10 @@ ExceptionObject* Assign(ExceptionObject* object, const ExceptionObject* source) } struct CategoryObject { const void* vtable; }; -struct ErrorCode { int value; const CategoryObject* category; }; +struct ErrorCode { int reserved; int value; const CategoryObject* category; }; struct SystemErrorObject { ExceptionObject base; ErrorCode code; }; -static_assert(sizeof(ErrorCode) == 16 && sizeof(SystemErrorObject) == 32 && offsetof(SystemErrorObject, code) == 16); +static_assert(sizeof(ErrorCode) == 16 && offsetof(ErrorCode, value) == 4 && offsetof(ErrorCode, category) == 8); +static_assert(sizeof(SystemErrorObject) == 32 && offsetof(SystemErrorObject, code) == 16); struct CppError { int value; const char* message; }; static const char* GenericErrorMessage(int value) { @@ -216,13 +217,15 @@ static const char* GenericErrorMessage(int value) { const TypeRecord ErrorCategoryType {ClassTypeVtable + 2, "St14error_category", nullptr}; const TypeRecord GenericCategoryType {SingleTypeVtable + 2, "St23_Generic_error_category", &ErrorCategoryType}; -static void APS5_VABI GenericCategoryDestroy_nid_no_patch(CategoryObject*) {} -static void APS5_VABI GenericCategoryDelete_nid_no_patch(CategoryObject*) { - throw std::invalid_argument("std::_Generic_error_category: the generic category object is not heap allocated"); +const TypeRecord SystemCategoryType {SingleTypeVtable + 2, "St22_System_error_category", &ErrorCategoryType}; +static void APS5_VABI CategoryDestroy_nid_no_patch(CategoryObject*) {} +static void APS5_VABI CategoryDelete_nid_no_patch(CategoryObject*) { + throw std::invalid_argument("std::error_category: the generic and system category objects are not heap allocated"); } static const char* APS5_VABI GenericCategoryName_nid_no_patch(const CategoryObject*) { return "generic"; } -static GuestLocale::String* APS5_VABI GenericCategoryMessage_nid_no_patch(GuestLocale::String* result, const CategoryObject*, int value) { - if (!result) throw std::invalid_argument("std::_Generic_error_category::message: null result"); +static const char* APS5_VABI SystemCategoryName_nid_no_patch(const CategoryObject*) { return "system"; } +static GuestLocale::String* APS5_VABI CategoryMessage_nid_no_patch(GuestLocale::String* result, const CategoryObject*, int value) { + if (!result) throw std::invalid_argument("std::error_category::message: null result"); const char* message = GenericErrorMessage(value); if (!message) message = "unknown error"; const auto length = std::strlen(message); @@ -235,22 +238,36 @@ static GuestLocale::String* APS5_VABI GenericCategoryMessage_nid_no_patch(GuestL result->capacity = capacity; return result; } -static ErrorCode APS5_VABI GenericCategoryCondition_nid_no_patch(const CategoryObject* self, int value) { return {value, self}; } -static bool APS5_VABI GenericCategoryEquivalentCondition_nid_no_patch(const CategoryObject* self, int value, const ErrorCode* condition) { - return condition->category == self && condition->value == value; +static ErrorCode APS5_VABI CategoryCondition_nid_no_patch(const CategoryObject* self, int value) { return {0, value, self}; } +static bool APS5_VABI CategoryEquivalentCondition_nid_no_patch(const CategoryObject* self, int value, const ErrorCode* condition) { + using Condition = ErrorCode (APS5_VABI *)(const CategoryObject*, int); + const auto mapped = reinterpret_cast(static_cast(self->vtable)[4])(self, value); + return condition->category == mapped.category && condition->value == mapped.value; } -static bool APS5_VABI GenericCategoryEquivalentCode_nid_no_patch(const CategoryObject* self, const ErrorCode* code, int value) { +static bool APS5_VABI CategoryEquivalentCode_nid_no_patch(const CategoryObject* self, const ErrorCode* code, int value) { return code->category == self && code->value == value; } const void* const GenericCategoryVtable[] { nullptr, &GenericCategoryType, - reinterpret_cast(GenericCategoryDestroy_nid_no_patch), reinterpret_cast(GenericCategoryDelete_nid_no_patch), - reinterpret_cast(GenericCategoryName_nid_no_patch), reinterpret_cast(GenericCategoryMessage_nid_no_patch), - reinterpret_cast(GenericCategoryCondition_nid_no_patch), - reinterpret_cast(GenericCategoryEquivalentCondition_nid_no_patch), - reinterpret_cast(GenericCategoryEquivalentCode_nid_no_patch) + reinterpret_cast(CategoryDestroy_nid_no_patch), reinterpret_cast(CategoryDelete_nid_no_patch), + reinterpret_cast(GenericCategoryName_nid_no_patch), reinterpret_cast(CategoryMessage_nid_no_patch), + reinterpret_cast(CategoryCondition_nid_no_patch), + reinterpret_cast(CategoryEquivalentCondition_nid_no_patch), + reinterpret_cast(CategoryEquivalentCode_nid_no_patch) }; const CategoryObject GenericCategory {GenericCategoryVtable + 2}; +static ErrorCode APS5_VABI SystemCategoryCondition_nid_no_patch(const CategoryObject* self, int value) { + return {0, value, GenericErrorMessage(value) ? &GenericCategory : self}; +} +const void* const SystemCategoryVtable[] { + nullptr, &SystemCategoryType, + reinterpret_cast(CategoryDestroy_nid_no_patch), reinterpret_cast(CategoryDelete_nid_no_patch), + reinterpret_cast(SystemCategoryName_nid_no_patch), reinterpret_cast(CategoryMessage_nid_no_patch), + reinterpret_cast(SystemCategoryCondition_nid_no_patch), + reinterpret_cast(CategoryEquivalentCondition_nid_no_patch), + reinterpret_cast(CategoryEquivalentCode_nid_no_patch) +}; +const CategoryObject SystemCategory {SystemCategoryVtable + 2}; } extern "C" { @@ -260,9 +277,12 @@ const char* APS5_VABI _ZSt13_Syserror_mapi_nid_postfix(int value) { const LibcException::CategoryObject* APS5_VABI _ZSt16generic_categoryv_nid_postfix() { return &LibcException::GenericCategory; } +const LibcException::CategoryObject* APS5_VABI _ZSt15system_categoryv_nid_postfix() { + return &LibcException::SystemCategory; +} GuestLocale::String* APS5_VABI _ZNKSt23_Generic_error_category7messageEi_nid_postfix( GuestLocale::String* result, const LibcException::CategoryObject* self, int value) { - return LibcException::GenericCategoryMessage_nid_no_patch(result, self, value); + return LibcException::CategoryMessage_nid_no_patch(result, self, value); } LibcException::TypeRecord _ZTISt8ios_base_nid_postfix {LibcException::ClassTypeVtable + 2, "St8ios_base", nullptr}; LibcException::TypeRecord _ZTISt9exception_nid_postfix {LibcException::ClassTypeVtable + 2, "St9exception", nullptr}; @@ -509,7 +529,7 @@ const char* APS5_VABI _ZNKSt8ios_base7failure4whatEv_nid_postfix(const LibcExcep std::snprintf(message, sizeof(message), "%s: %s", error.message, LibcException::GenericErrorMessage(error.value)); auto* object = static_cast(__cxa_allocate_exception_nid_postfix(sizeof(LibcException::SystemErrorObject))); LibcException::Construct(&object->base, _ZTVSt12system_error_nid_postfix, message); - object->code = {error.value, &LibcException::GenericCategory}; + object->code = {0, error.value, &LibcException::GenericCategory}; __cxa_throw_nid_postfix(object, reinterpret_cast(&_ZTISt12system_error_nid_postfix), reinterpret_cast(LibcException::DestroyGuestMessage_nid_no_patch)); } diff --git a/core/libs/tests/GuestAmprCppError.cpp b/core/libs/tests/GuestAmprCppError.cpp index 8e2407c199c7..2cd8eceea5ea 100644 --- a/core/libs/tests/GuestAmprCppError.cpp +++ b/core/libs/tests/GuestAmprCppError.cpp @@ -12,7 +12,7 @@ struct TypeRecord { const void* vtable; const char* name; const TypeRecord* base; }; struct Category { const void* const* vtable; }; -struct ErrorCode { int value; const Category* category; }; +struct ErrorCode { int reserved; int value; const Category* category; }; struct SystemError { const void* const* vtable; const char* message; ErrorCode code; }; static_assert(sizeof(SystemError) == 32 && offsetof(SystemError, code) == 16); @@ -80,8 +80,8 @@ static void CheckCategory(const SystemError& error) { Require(std::strcmp(Slot(category->vtable, 2)(category), "generic") == 0, 23); const ErrorCode condition = Slot(category->vtable, 4)(category, error.code.value); Require(condition.value == error.code.value && condition.category == category, 24); - const ErrorCode other {error.code.value + 1, category}; - const ErrorCode foreign {error.code.value, nullptr}; + const ErrorCode other {0, error.code.value + 1, category}; + const ErrorCode foreign {0, error.code.value, nullptr}; auto equivalentCondition = Slot(category->vtable, 5); Require(equivalentCondition(category, error.code.value, &condition), 25); Require(!equivalentCondition(category, error.code.value, &other), 26); @@ -111,7 +111,7 @@ static void CheckCopies(const SystemError& error) { Require(copy.code.value == error.code.value && copy.code.category == error.code.category, 41); SystemError assigned {}; _ZNSt12system_errorC1ERKS__nid_postfix(&assigned, &error); - assigned.code = {0, nullptr}; + assigned.code = {0, 0, nullptr}; Require(_ZNSt12system_erroraSERKS__nid_postfix(&assigned, &error) == &assigned, 42); Require(assigned.code.value == error.code.value && assigned.code.category == error.code.category, 43); _ZNSt12system_errorD1Ev_nid_postfix(&assigned); diff --git a/core/libs/tests/GuestAmprThreads.cpp b/core/libs/tests/GuestAmprThreads.cpp index 403c445304c7..5467abcebd6f 100644 --- a/core/libs/tests/GuestAmprThreads.cpp +++ b/core/libs/tests/GuestAmprThreads.cpp @@ -38,6 +38,7 @@ struct GuestTypeInfo { struct GuestSystemError { const void* vtable; const char* message; + int reserved; int code; const void* category; }; diff --git a/core/libs/tests/GuestGenericCategory.cpp b/core/libs/tests/GuestGenericCategory.cpp index b4f3f9868789..bc279a1a567a 100644 --- a/core/libs/tests/GuestGenericCategory.cpp +++ b/core/libs/tests/GuestGenericCategory.cpp @@ -9,14 +9,19 @@ #include struct Category { void* const* vtable; }; -struct ErrorCode { int value; const Category* category; }; +struct ErrorCode { int reserved; int value; const Category* category; }; using Message = GuestLocale::String* (APS5_VABI *)(GuestLocale::String*, const Category*, int); using Name = const char* (APS5_VABI *)(const Category*); using Condition = ErrorCode (APS5_VABI *)(const Category*, int); +using EquivalentCondition = bool (APS5_VABI *)(const Category*, int, const ErrorCode*); +using EquivalentCode = bool (APS5_VABI *)(const Category*, const ErrorCode*, int); +using Delete = void (APS5_VABI *)(const Category*); +struct TypeRecord { const void* vtable; const char* name; const TypeRecord* base; }; extern "C" { const char* APS5_VABI _ZSt13_Syserror_mapi_nid_postfix(int value); const Category* APS5_VABI _ZSt16generic_categoryv_nid_postfix(); +const Category* APS5_VABI _ZSt15system_categoryv_nid_postfix(); GuestLocale::String* APS5_VABI _ZNKSt23_Generic_error_category7messageEi_nid_postfix( GuestLocale::String* result, const Category* self, int value); int* APS5_VABI __error_nid_postfix(); @@ -48,6 +53,36 @@ static void CheckMessage(Message message, const Category* category, int value, c if (length > 15) _ZdlPv_nid_postfix(guarded.result.pointer); } +static void CheckSystemCategory(const Category* generic) { + const Category* system = _ZSt15system_categoryv_nid_postfix(); + REQUIRE(system && system != generic && system == _ZSt15system_categoryv_nid_postfix()); + const auto* type = static_cast(system->vtable[-1]); + REQUIRE(std::strcmp(type->name, "St22_System_error_category") == 0); + REQUIRE(type->base && std::strcmp(type->base->name, "St14error_category") == 0); + REQUIRE(std::strcmp(reinterpret_cast(system->vtable[2])(system), "system") == 0); + const auto condition = reinterpret_cast(system->vtable[4]); + const ErrorCode known = condition(system, 2); + std::uint64_t head = 0; + std::memcpy(&head, &known, sizeof(head)); + REQUIRE(head == std::uint64_t{2} << 32 && known.value == 2 && known.category == generic); + const ErrorCode unknown = condition(system, 44); + REQUIRE(unknown.reserved == 0 && unknown.value == 44 && unknown.category == system); + const auto equivalentCondition = reinterpret_cast(system->vtable[5]); + REQUIRE(equivalentCondition(system, 2, &known)); + const ErrorCode sameCategory {0, 2, system}; + REQUIRE(!equivalentCondition(system, 2, &sameCategory)); + REQUIRE(equivalentCondition(system, 44, &unknown)); + const auto equivalentCode = reinterpret_cast(system->vtable[6]); + REQUIRE(equivalentCode(system, &sameCategory, 2) && !equivalentCode(system, &known, 2)); + const auto message = reinterpret_cast(system->vtable[3]); + CheckMessage(message, system, 2, "no such file or directory"); + CheckMessage(message, system, 44, "unknown error"); + bool rejected = false; + try { reinterpret_cast(system->vtable[1])(system); } + catch (const std::invalid_argument&) { rejected = true; } + REQUIRE(rejected); +} + int main() { const std::array api {}; ApplicationHeapRegister_nid_no_patch(api.data()); @@ -56,7 +91,10 @@ int main() { REQUIRE(std::strcmp(reinterpret_cast(category->vtable[2])(category), "generic") == 0); const auto message = reinterpret_cast(category->vtable[3]); const auto condition = reinterpret_cast(category->vtable[4])(category, 35); - REQUIRE(condition.value == 35 && condition.category == category); + REQUIRE(condition.reserved == 0 && condition.value == 35 && condition.category == category); + const auto equivalent = reinterpret_cast(category->vtable[5]); + REQUIRE(equivalent(category, 35, &condition)); + CheckSystemCategory(category); struct Expected { int value; const char* text; }; const Expected expected[] { @@ -95,5 +133,5 @@ int main() { try { message(nullptr, category, 22); } catch (const std::invalid_argument&) { rejected = true; } REQUIRE(rejected); - std::puts("guest generic category, FreeBSD error map and message ownership: PASS"); + std::puts("guest generic and system categories, FreeBSD error map and message ownership: PASS"); }