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
9 changes: 6 additions & 3 deletions core/libs/prx/libSceAmpr/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ if(BUILD_TESTING)
"PATH=path_list_prepend:$<TARGET_FILE_DIR:libSceAmpr>")
endif()

if(MINGW)
if(MINGW OR CMAKE_SYSTEM_NAME STREQUAL "Linux")
enable_language(ASM)
add_test_executable(guest_ampr_cpp_error_tests
${LIBS_INCLUDE_DIR}/tests/GuestAmprCppError.cpp
Expand All @@ -43,7 +43,10 @@ if(BUILD_TESTING)
target_link_libraries(guest_ampr_cpp_error_tests PRIVATE libSceAmpr libkernel libc)
configure_windows_unwind(guest_ampr_cpp_error_tests)
add_test(NAME guest_ampr_cpp_error COMMAND guest_ampr_cpp_error_tests)
set_tests_properties(guest_ampr_cpp_error PROPERTIES TIMEOUT 10 ENVIRONMENT_MODIFICATION
"PATH=path_list_prepend:$<TARGET_FILE_DIR:libSceAmpr>")
set_tests_properties(guest_ampr_cpp_error PROPERTIES TIMEOUT 10)
if(WIN32)
set_tests_properties(guest_ampr_cpp_error PROPERTIES ENVIRONMENT_MODIFICATION
"PATH=path_list_prepend:$<TARGET_FILE_DIR:libSceAmpr>")
endif()
endif()
endif()
10 changes: 10 additions & 0 deletions core/libs/prx/libc/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -442,6 +442,16 @@ if(BUILD_TESTING)
target_include_directories(guest_new_handler_tests PRIVATE ${LIBS_INCLUDE_DIR})
target_link_libraries(guest_new_handler_tests PRIVATE libc)
configure_windows_unwind(guest_new_handler_tests)
add_test_executable(guest_generic_category_tests ${LIBS_INCLUDE_DIR}/tests/GuestGenericCategory.cpp)
target_include_directories(guest_generic_category_tests PRIVATE ${LIBS_INCLUDE_DIR})
target_link_libraries(guest_generic_category_tests PRIVATE libc)
configure_windows_unwind(guest_generic_category_tests)
add_test(NAME guest_generic_category COMMAND guest_generic_category_tests)
set_tests_properties(guest_generic_category PROPERTIES TIMEOUT 10)
if(WIN32)
set_tests_properties(guest_generic_category PROPERTIES ENVIRONMENT_MODIFICATION
"PATH=path_list_prepend:$<TARGET_FILE_DIR:libc>")
endif()
add_test(NAME guest_new_handler COMMAND guest_new_handler_tests)
if(WIN32)
set_tests_properties(guest_new_handler PROPERTIES ENVIRONMENT_MODIFICATION
Expand Down
109 changes: 106 additions & 3 deletions core/libs/prx/libc/src/exception/StandardExceptions.cpp
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
#include "prx/libc/include/exceptions/Runtime.hpp"
#include "prx/libc/include/GuestLocale.hpp"
#include <cstdio>
#include <iterator>
#include <limits>
#include <regex>
#include <stdexcept>
#include <string>

extern "C" void* APS5_VABI _Znwm_nid_postfix(std::size_t size);

namespace LibcException {
struct TypeRecord { const void* const* vtable; const char* name; const TypeRecord* base; };
struct ExceptionObject { const void* vtable; const char* message; };
Expand Down Expand Up @@ -129,15 +132,105 @@ struct SystemErrorObject { ExceptionObject base; ErrorCode code; };
static_assert(sizeof(ErrorCode) == 16 && sizeof(SystemErrorObject) == 32 && offsetof(SystemErrorObject, code) == 16);
struct CppError { int value; const char* message; };

static const char* GenericErrorMessage(int value) {
static constexpr CppError errors[] {
{1, "operation not permitted"},
{2, "no such file or directory"},
{3, "no such process"},
{4, "interrupted"},
{5, "io error"},
{6, "no such device or address"},
{7, "argument list too long"},
{8, "executable format error"},
{9, "bad file descriptor"},
{10, "no child process"},
{11, "resource deadlock would occur"},
{12, "not enough memory"},
{13, "permission denied"},
{14, "bad address"},
{16, "device or resource busy"},
{17, "file exists"},
{18, "cross device link"},
{19, "no such device"},
{20, "not a directory"},
{21, "is a directory"},
{22, "invalid argument"},
{23, "too many files open in system"},
{24, "too many files open"},
{25, "inappropriate io control operation"},
{26, "text file busy"},
{27, "file too large"},
{28, "no space on device"},
{29, "invalid seek"},
{30, "read only file system"},
{31, "too many links"},
{32, "broken pipe"},
{33, "argument out of domain"},
{34, "result out of range"},
{35, "operation would block"},
{36, "operation in progress"},
{37, "connection already in progress"},
{38, "not a socket"},
{39, "destination address required"},
{40, "message size"},
{41, "wrong protocol type"},
{42, "no protocol option"},
{43, "protocol not supported"},
{45, "not supported"},
{47, "address family not supported"},
{48, "address in use"},
{49, "address not available"},
{50, "network down"},
{51, "network unreachable"},
{52, "network reset"},
{53, "connection aborted"},
{54, "connection reset"},
{55, "no buffer space"},
{56, "already connected"},
{57, "not connected"},
{60, "timed out"},
{61, "connection refused"},
{62, "too many symbolic link levels"},
{63, "filename too long"},
{65, "host unreachable"},
{66, "directory not empty"},
{77, "no lock available"},
{78, "function not supported"},
{82, "identifier removed"},
{83, "no message"},
{84, "value too large"},
{85, "operation canceled"},
{86, "illegal byte sequence"},
{89, "bad message"},
{91, "no link"},
{92, "protocol error"},
{95, "state not recoverable"},
{96, "owner dead"}
};
for (const auto& error : errors)
if (error.value == value) return error.message;
return nullptr;
}

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");
}
static const char* APS5_VABI GenericCategoryName_nid_no_patch(const CategoryObject*) { return "generic"; }
static void* APS5_VABI GenericCategoryMessage_nid_no_patch(void* result, const CategoryObject*, int) {
NotImplemented_nid_no_patch("std::_Generic_error_category::message");
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");
const char* message = GenericErrorMessage(value);
if (!message) message = "unknown error";
const auto length = std::strlen(message);
const auto capacity = length | std::size_t{15};
char* storage = length <= 15 ? result->buffer : static_cast<char*>(_Znwm_nid_postfix(capacity + 1));
std::memcpy(storage, message, length + 1);
result->reserved = 0;
if (length > 15) result->pointer = storage;
result->size = length;
result->capacity = capacity;
return result;
}
static ErrorCode APS5_VABI GenericCategoryCondition_nid_no_patch(const CategoryObject* self, int value) { return {value, self}; }
Expand All @@ -159,6 +252,16 @@ const CategoryObject GenericCategory {GenericCategoryVtable + 2};
}

extern "C" {
const char* APS5_VABI _ZSt13_Syserror_mapi_nid_postfix(int value) {
return LibcException::GenericErrorMessage(value);
}
const LibcException::CategoryObject* APS5_VABI _ZSt16generic_categoryv_nid_postfix() {
return &LibcException::GenericCategory;
}
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);
}
LibcException::TypeRecord _ZTISt8ios_base_nid_postfix {LibcException::ClassTypeVtable + 2, "St8ios_base", nullptr};
LibcException::TypeRecord _ZTISt9exception_nid_postfix {LibcException::ClassTypeVtable + 2, "St9exception", nullptr};
LibcException::ExceptionVtable _ZTVSt9exception_nid_postfix {0, &_ZTISt9exception_nid_postfix, LibcException::GuestDestroyPlain_nid_no_patch, LibcException::GuestDeletePlain_nid_no_patch, LibcException::GuestPlainWhat_nid_no_patch};
Expand Down Expand Up @@ -382,7 +485,7 @@ const char* APS5_VABI _ZNKSt8ios_base7failure4whatEv_nid_postfix(const LibcExcep
throw std::out_of_range("_Throw_Cpp_error: " + std::to_string(code) + " is not a _Cpp_error code");
const auto& error = errors[code];
char message[128];
std::snprintf(message, sizeof(message), "%s: %s", error.message, error.message);
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};
Expand Down
7 changes: 7 additions & 0 deletions core/libs/tests/GuestAmprCppError.S
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,11 @@ GuestCatch:
call FixtureFail
ud2
.cfi_endproc
#ifdef _WIN32
.section .rdata,"dr"
#else
.section .rodata
#endif
.Lcatch_lsda:
.byte 0xff, 0x80
.uleb128 .Lcatch_types_end-.Lcatch_types_offset
Expand All @@ -48,3 +52,6 @@ GuestCatch:
.quad FixtureCatchType
.quad FixtureOtherType
.Lcatch_types_end:
#ifndef _WIN32
.section .note.GNU-stack,"",@progbits
#endif
21 changes: 12 additions & 9 deletions core/libs/tests/GuestAmprCppError.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
#include "prx/libc/include/general/VabiMacros.hpp"
#include "prx/libc/include/GuestLocale.hpp"
#include "prx/libc/include/ApplicationHeap.hpp"
#include <array>
#include <cstddef>
#include <cstdio>
#include <cstdlib>
Expand Down Expand Up @@ -29,7 +32,7 @@ static constexpr Expected Errors[] {
{12, "not enough memory: not enough memory"},
{1, "operation not permitted: operation not permitted"},
{11, "resource deadlock would occur: resource deadlock would occur"},
{35, "resource unavailable try again: resource unavailable try again"}
{35, "resource unavailable try again: operation would block"}
};

extern "C" {
Expand All @@ -41,6 +44,7 @@ extern const unsigned char _ZTISt12system_error_nid_postfix[];
void APS5_VABI _ZNSt12system_errorC1ERKS__nid_postfix(SystemError* self, const SystemError* source);
SystemError* APS5_VABI _ZNSt12system_erroraSERKS__nid_postfix(SystemError* self, const SystemError* source);
void APS5_VABI _ZNSt12system_errorD1Ev_nid_postfix(SystemError* self);
void APS5_VABI _ZdlPv_nid_postfix(void* pointer);
void APS5_VABI GuestCatch(int code);

const void* FixtureCatchType = nullptr;
Expand Down Expand Up @@ -86,14 +90,11 @@ static void CheckCategory(const SystemError& error) {
Require(equivalentCode(category, &error.code, error.code.value), 28);
Require(!equivalentCode(category, &error.code, error.code.value + 1), 29);
Require(!equivalentCode(category, &foreign, error.code.value), 30);
bool messageThrew = false;
try {
unsigned char result[32] {};
Slot<CategoryMessage>(category->vtable, 3)(result, category, error.code.value);
} catch (const std::runtime_error&) {
messageThrew = true;
}
Require(messageThrew, 31);
GuestLocale::String result {};
Require(Slot<CategoryMessage>(category->vtable, 3)(&result, category, error.code.value) == &result, 33);
const char* data = result.capacity <= 15 ? result.buffer : result.pointer;
Require(std::strcmp(data, std::strstr(FixtureExpected->what, ": ") + 2) == 0, 34);
if (result.capacity > 15) _ZdlPv_nid_postfix(result.pointer);
bool deleteThrew = false;
try {
Slot<CategoryDelete>(category->vtable, 1)(category);
Expand Down Expand Up @@ -148,6 +149,8 @@ static void CheckOutOfRange(int code) {
}

int main() {
const std::array<void*, 10> api {};
ApplicationHeapRegister_nid_no_patch(api.data());
FixtureOtherType = _ZTISt11logic_error_nid_postfix;
for (int code = 0; code < static_cast<int>(std::size(Errors)); ++code) Catch(_ZTISt12system_error_nid_postfix, code);
Catch(_ZTISt13runtime_error_nid_postfix, 5);
Expand Down
99 changes: 99 additions & 0 deletions core/libs/tests/GuestGenericCategory.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
#include "prx/libc/include/GuestLocale.hpp"
#include "prx/libc/include/ApplicationHeap.hpp"
#include <array>
#include <cstdint>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <limits>
#include <stdexcept>

struct Category { const void* const* vtable; };
struct ErrorCode { 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);

extern "C" {
const char* APS5_VABI _ZSt13_Syserror_mapi_nid_postfix(int value);
const Category* APS5_VABI _ZSt16generic_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();
void APS5_VABI _ZdlPv_nid_postfix(void* pointer);
}

static void Require(bool value, int line) {
if (value) return;
std::fprintf(stderr, "generic category check failed at line %d\n", line);
std::exit(1);
}
#define REQUIRE(value) Require((value), __LINE__)

static void CheckMessage(Message message, const Category* category, int value, const char* expected) {
constexpr std::uint64_t marker = 0xA55AA55AA55AA55Aull;
struct Guarded { std::uint64_t before; GuestLocale::String result; std::uint64_t after; };
Guarded guarded {marker, {}, marker};
std::memset(&guarded.result, 0xcd, sizeof(guarded.result));
*__error_nid_postfix() = 61;
REQUIRE(message(&guarded.result, category, value) == &guarded.result);
REQUIRE(*__error_nid_postfix() == 61);
REQUIRE(guarded.before == marker && guarded.after == marker);
const auto length = std::strlen(expected);
REQUIRE(guarded.result.reserved == 0 && guarded.result.size == length);
REQUIRE(guarded.result.capacity >= length && (guarded.result.capacity & 15) == 15);
const char* text = guarded.result.capacity <= 15 ? guarded.result.buffer : guarded.result.pointer;
REQUIRE(std::memcmp(text, expected, length + 1) == 0);
REQUIRE((guarded.result.capacity <= 15) == (length <= 15));
if (length > 15) _ZdlPv_nid_postfix(guarded.result.pointer);
}

int main() {
const std::array<void*, 10> api {};
ApplicationHeapRegister_nid_no_patch(api.data());
const Category* category = _ZSt16generic_categoryv_nid_postfix();
REQUIRE(category && category == _ZSt16generic_categoryv_nid_postfix());
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);

struct Expected { int value; const char* text; };
const Expected expected[] {
{3, "no such process"}, {14, "bad address"}, {13, "permission denied"},
{25, "inappropriate io control operation"}, {35, "operation would block"},
{45, "not supported"}, {47, "address family not supported"}, {60, "timed out"},
{78, "function not supported"}, {95, "state not recoverable"}, {96, "owner dead"}
};
for (const auto& error : expected) {
*__error_nid_postfix() = 61;
const char* mapped = _ZSt13_Syserror_mapi_nid_postfix(error.value);
REQUIRE(mapped && std::strcmp(mapped, error.text) == 0 && *__error_nid_postfix() == 61);
CheckMessage(message, category, error.value, error.text);
CheckMessage(_ZNKSt23_Generic_error_category7messageEi_nid_postfix, category, error.value, error.text);
}

unsigned known = 0;
for (int value = 1; value <= 96; ++value) {
const char* mapped = _ZSt13_Syserror_mapi_nid_postfix(value);
if (mapped) ++known;
CheckMessage(message, category, value, mapped ? mapped : "unknown error");
}
REQUIRE(known == 72);
for (int value : {std::numeric_limits<int>::min(), -1, 0, 15, 44, 97, std::numeric_limits<int>::max()}) {
*__error_nid_postfix() = 61;
REQUIRE(_ZSt13_Syserror_mapi_nid_postfix(value) == nullptr && *__error_nid_postfix() == 61);
CheckMessage(message, category, value, "unknown error");
}
GuestLocale::String first {}, second {};
REQUIRE(message(&first, category, 25) == &first && message(&second, category, 11) == &second);
REQUIRE(first.pointer != second.pointer);
_ZdlPv_nid_postfix(second.pointer);
REQUIRE(std::strcmp(first.pointer, "inappropriate io control operation") == 0);
_ZdlPv_nid_postfix(first.pointer);
bool rejected = false;
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");
}
Loading
Loading