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
56 changes: 38 additions & 18 deletions core/libs/prx/libc/src/exception/StandardExceptions.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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);
Expand All @@ -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<Condition>(static_cast<const void* const*>(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<const void*>(GenericCategoryDestroy_nid_no_patch), reinterpret_cast<const void*>(GenericCategoryDelete_nid_no_patch),
reinterpret_cast<const void*>(GenericCategoryName_nid_no_patch), reinterpret_cast<const void*>(GenericCategoryMessage_nid_no_patch),
reinterpret_cast<const void*>(GenericCategoryCondition_nid_no_patch),
reinterpret_cast<const void*>(GenericCategoryEquivalentCondition_nid_no_patch),
reinterpret_cast<const void*>(GenericCategoryEquivalentCode_nid_no_patch)
reinterpret_cast<const void*>(CategoryDestroy_nid_no_patch), reinterpret_cast<const void*>(CategoryDelete_nid_no_patch),
reinterpret_cast<const void*>(GenericCategoryName_nid_no_patch), reinterpret_cast<const void*>(CategoryMessage_nid_no_patch),
reinterpret_cast<const void*>(CategoryCondition_nid_no_patch),
reinterpret_cast<const void*>(CategoryEquivalentCondition_nid_no_patch),
reinterpret_cast<const void*>(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<const void*>(CategoryDestroy_nid_no_patch), reinterpret_cast<const void*>(CategoryDelete_nid_no_patch),
reinterpret_cast<const void*>(SystemCategoryName_nid_no_patch), reinterpret_cast<const void*>(CategoryMessage_nid_no_patch),
reinterpret_cast<const void*>(SystemCategoryCondition_nid_no_patch),
reinterpret_cast<const void*>(CategoryEquivalentCondition_nid_no_patch),
reinterpret_cast<const void*>(CategoryEquivalentCode_nid_no_patch)
};
const CategoryObject SystemCategory {SystemCategoryVtable + 2};
}

extern "C" {
Expand All @@ -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};
Expand Down Expand Up @@ -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<LibcException::SystemErrorObject*>(__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<std::type_info*>(&_ZTISt12system_error_nid_postfix),
reinterpret_cast<decltype(LibcException::Header::destructor)>(LibcException::DestroyGuestMessage_nid_no_patch));
}
Expand Down
8 changes: 4 additions & 4 deletions core/libs/tests/GuestAmprCppError.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down Expand Up @@ -80,8 +80,8 @@ static void CheckCategory(const SystemError& error) {
Require(std::strcmp(Slot<CategoryName>(category->vtable, 2)(category), "generic") == 0, 23);
const ErrorCode condition = Slot<CategoryCondition>(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<CategoryEquivalentCondition>(category->vtable, 5);
Require(equivalentCondition(category, error.code.value, &condition), 25);
Require(!equivalentCondition(category, error.code.value, &other), 26);
Expand Down Expand Up @@ -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);
Expand Down
1 change: 1 addition & 0 deletions core/libs/tests/GuestAmprThreads.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ struct GuestTypeInfo {
struct GuestSystemError {
const void* vtable;
const char* message;
int reserved;
int code;
const void* category;
};
Expand Down
44 changes: 41 additions & 3 deletions core/libs/tests/GuestGenericCategory.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,19 @@
#include <stdexcept>

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();
Expand Down Expand Up @@ -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<const TypeRecord*>(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<Name>(system->vtable[2])(system), "system") == 0);
const auto condition = reinterpret_cast<Condition>(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<EquivalentCondition>(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<EquivalentCode>(system->vtable[6]);
REQUIRE(equivalentCode(system, &sameCategory, 2) && !equivalentCode(system, &known, 2));
const auto message = reinterpret_cast<Message>(system->vtable[3]);
CheckMessage(message, system, 2, "no such file or directory");
CheckMessage(message, system, 44, "unknown error");
bool rejected = false;
try { reinterpret_cast<Delete>(system->vtable[1])(system); }
catch (const std::invalid_argument&) { rejected = true; }
REQUIRE(rejected);
}

int main() {
const std::array<void*, 10> api {};
ApplicationHeapRegister_nid_no_patch(api.data());
Expand All @@ -56,7 +91,10 @@ int main() {
REQUIRE(std::strcmp(reinterpret_cast<Name>(category->vtable[2])(category), "generic") == 0);
const auto message = reinterpret_cast<Message>(category->vtable[3]);
const auto condition = reinterpret_cast<Condition>(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<EquivalentCondition>(category->vtable[5]);
REQUIRE(equivalent(category, 35, &condition));
CheckSystemCategory(category);

struct Expected { int value; const char* text; };
const Expected expected[] {
Expand Down Expand Up @@ -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");
}
Loading