From 939c3d98f7ea59eef7b7252e2a8533ee723a8912 Mon Sep 17 00:00:00 2001 From: mugurc Date: Tue, 6 Oct 2026 20:19:20 +0300 Subject: [PATCH] feat(libc): exception runtime on macOS hosts On macOS the host keeps libc++abi: libc++ binds __cxa_throw two-level, so moving only the personality into this runtime would leave the two halves disagreeing on the exception header. The host personalities stay libc++abi's and libunwind's, and libc.prx serves guest code: - the guest's libstdc++ std::exception_ptr is modelled directly, since libc++'s differs, and the Itanium type_info layouts that libc++abi keeps private are declared for the runtime; - guest frames are found through their PT_GNU_EH_FRAME, using a dl_iterate_phdr emulation over the ELF program headers that relinked images keep in __APS5DATA,__meta (written by relinker --macos, which comes in a later pull request); - host frames are found by scanning __TEXT,__eh_frame; - sceKernelGetModuleInfoForUnwind uses the same emulation and falls back to dladdr for host images. The libc++ paths compile only with libc++/libc++abi and the rest only on __APPLE__, so Linux and Windows builds are unchanged. exception_c_cleanup throws from host code, which this runtime does not serve on macOS, so it is not built there. --- core/libs/prx/libc/CMakeLists.txt | 21 ++++---- .../libc/include/specifics/itanium/CxxAbi.hpp | 47 +++++++++++++++++ .../libc/include/specifics/linux/ElfTypes.hpp | 4 ++ core/libs/prx/libc/src/CxxAbiSupport.cpp | 2 +- .../libc/src/exception/ExceptionPointer.cpp | 51 +++++++++++++++++++ .../prx/libc/src/exception/Personality.cpp | 16 +++--- core/libs/prx/libc/src/exception/Unwind.cpp | 37 ++++++++++++-- .../src/specifics/linux/DlIteratePhdr.cpp | 2 +- .../libc/src/specifics/macos/GuestImages.cpp | 42 +++++++++++++++ core/libs/prx/libkernel/Module/src/Module.cpp | 49 ++++++++++++++++++ 10 files changed, 248 insertions(+), 23 deletions(-) create mode 100644 core/libs/prx/libc/src/specifics/macos/GuestImages.cpp diff --git a/core/libs/prx/libc/CMakeLists.txt b/core/libs/prx/libc/CMakeLists.txt index 5af0ac767..946c1bd77 100644 --- a/core/libs/prx/libc/CMakeLists.txt +++ b/core/libs/prx/libc/CMakeLists.txt @@ -4,6 +4,7 @@ target_sources(libc PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}/src/specifics/gcc/StackGuard.cpp ${CMAKE_CURRENT_SOURCE_DIR}/src/specifics/windows/CrashReport.cpp ${CMAKE_CURRENT_SOURCE_DIR}/src/specifics/linux/DlIteratePhdr.cpp + ${CMAKE_CURRENT_SOURCE_DIR}/src/specifics/macos/GuestImages.cpp ${CMAKE_CURRENT_SOURCE_DIR}/src/Strings.cpp ${CMAKE_CURRENT_SOURCE_DIR}/src/AllocatingStrings.cpp ${CMAKE_CURRENT_SOURCE_DIR}/src/WideConversion.cpp @@ -263,15 +264,17 @@ if(BUILD_TESTING) target_include_directories(exception_runtime_tests PRIVATE ${LIBS_INCLUDE_DIR}) target_link_libraries(exception_runtime_tests PRIVATE libc) add_test(NAME exception_runtime COMMAND exception_runtime_tests) - enable_language(C) - add_test_executable(exception_c_cleanup_tests ${LIBS_INCLUDE_DIR}/tests/ExceptionCCleanup.cpp ${LIBS_INCLUDE_DIR}/tests/ExceptionCCleanup.c) - set_source_files_properties(${LIBS_INCLUDE_DIR}/tests/ExceptionCCleanup.c PROPERTIES COMPILE_OPTIONS -fexceptions) - target_link_libraries(exception_c_cleanup_tests PRIVATE libc) - add_test(NAME exception_c_cleanup COMMAND exception_c_cleanup_tests) - add_test_executable(guest_image_anonymous_tests ${LIBS_INCLUDE_DIR}/tests/GuestImageAnonymous.cpp) - target_include_directories(guest_image_anonymous_tests PRIVATE ${LIBS_INCLUDE_DIR}) - target_link_libraries(guest_image_anonymous_tests PRIVATE libc) - add_test(NAME guest_image_anonymous COMMAND guest_image_anonymous_tests) + if(NOT APPLE) + enable_language(C) + add_test_executable(exception_c_cleanup_tests ${LIBS_INCLUDE_DIR}/tests/ExceptionCCleanup.cpp ${LIBS_INCLUDE_DIR}/tests/ExceptionCCleanup.c) + set_source_files_properties(${LIBS_INCLUDE_DIR}/tests/ExceptionCCleanup.c PROPERTIES COMPILE_OPTIONS -fexceptions) + target_link_libraries(exception_c_cleanup_tests PRIVATE libc) + add_test(NAME exception_c_cleanup COMMAND exception_c_cleanup_tests) + add_test_executable(guest_image_anonymous_tests ${LIBS_INCLUDE_DIR}/tests/GuestImageAnonymous.cpp) + target_include_directories(guest_image_anonymous_tests PRIVATE ${LIBS_INCLUDE_DIR}) + target_link_libraries(guest_image_anonymous_tests PRIVATE libc) + add_test(NAME guest_image_anonymous COMMAND guest_image_anonymous_tests) + endif() endif() endif() diff --git a/core/libs/prx/libc/include/specifics/itanium/CxxAbi.hpp b/core/libs/prx/libc/include/specifics/itanium/CxxAbi.hpp index 7c68c9e16..53d45d895 100644 --- a/core/libs/prx/libc/include/specifics/itanium/CxxAbi.hpp +++ b/core/libs/prx/libc/include/specifics/itanium/CxxAbi.hpp @@ -3,4 +3,51 @@ #include +#ifdef _LIBCPPABI_VERSION +#include + +namespace __cxxabiv1 { +class __class_type_info : public std::type_info { +public: + enum __sub_kind { __unknown = 0 }; + struct __upcast_result; + struct __dyncast_result; +}; + +class __si_class_type_info : public __class_type_info { +public: + const __class_type_info* __base_type; +}; + +struct __base_class_type_info { + const __class_type_info* __base_type; + long __offset_flags; +}; + +class __vmi_class_type_info : public __class_type_info { +public: + unsigned int __flags; + unsigned int __base_count; + __base_class_type_info __base_info[1]; +}; + +class __pbase_type_info : public std::type_info { +public: + unsigned int __flags; + const std::type_info* __pointee; +}; + +class __pointer_type_info : public __pbase_type_info {}; + +class __pointer_to_member_type_info : public __pbase_type_info { +public: + const __class_type_info* __context; +}; + +#ifdef __APPLE__ +extern "C" int __cxa_thread_atexit(void (*)(void*), void*, void*) noexcept; +#endif +} +#endif + #endif diff --git a/core/libs/prx/libc/include/specifics/linux/ElfTypes.hpp b/core/libs/prx/libc/include/specifics/linux/ElfTypes.hpp index 5263b92c4..89dd7d3bc 100644 --- a/core/libs/prx/libc/include/specifics/linux/ElfTypes.hpp +++ b/core/libs/prx/libc/include/specifics/linux/ElfTypes.hpp @@ -27,6 +27,10 @@ static constexpr std::uint32_t PT_GNU_EH_FRAME = 0x6474e550; static constexpr std::uint32_t PF_X = 1; static constexpr std::uint32_t PF_W = 2; +#ifdef __APPLE__ +extern "C" int dl_iterate_phdr(int (*callback)(dl_phdr_info*, std::size_t, void*), void* data) __asm__("_dl_iterate_phdr_nid_no_patch"); +#else extern "C" int dl_iterate_phdr(int (*callback)(dl_phdr_info*, std::size_t, void*), void* data); +#endif #endif diff --git a/core/libs/prx/libc/src/CxxAbiSupport.cpp b/core/libs/prx/libc/src/CxxAbiSupport.cpp index 008cc422e..12600bab1 100644 --- a/core/libs/prx/libc/src/CxxAbiSupport.cpp +++ b/core/libs/prx/libc/src/CxxAbiSupport.cpp @@ -4,7 +4,7 @@ #define _UNWIND_H #endif -#include +#include "prx/libc/include/specifics/itanium/CxxAbi.hpp" #include #include #include diff --git a/core/libs/prx/libc/src/exception/ExceptionPointer.cpp b/core/libs/prx/libc/src/exception/ExceptionPointer.cpp index 50bfcdffa..eba668749 100644 --- a/core/libs/prx/libc/src/exception/ExceptionPointer.cpp +++ b/core/libs/prx/libc/src/exception/ExceptionPointer.cpp @@ -1,5 +1,6 @@ #include +#ifndef _LIBCPP_VERSION extern "C" { void ExceptionPointerAddref(std::exception_ptr* self) noexcept asm("_ZNSt15__exception_ptr13exception_ptr9_M_addrefEv"); void ExceptionPointerRelease(std::exception_ptr* self) noexcept asm("_ZNSt15__exception_ptr13exception_ptr10_M_releaseEv"); @@ -79,3 +80,53 @@ const type_info* exception_ptr::__cxa_exception_type() const noexcept { } } + +#else + +namespace { +struct GuestExceptionPointer { + void* object {}; + explicit GuestExceptionPointer(void* primary) noexcept : object(primary) {} + GuestExceptionPointer(const GuestExceptionPointer&) = delete; + ~GuestExceptionPointer() {} +}; +} + +extern "C" { +void APS5_VABI _ZNSt15__exception_ptr13exception_ptr9_M_addrefEv_nid_postfix(GuestExceptionPointer* self) noexcept { + __cxa_increment_exception_refcount_nid_postfix(self->object); +} + +void APS5_VABI _ZNSt15__exception_ptr13exception_ptr10_M_releaseEv_nid_postfix(GuestExceptionPointer* self) noexcept { + __cxa_decrement_exception_refcount_nid_postfix(self->object); + self->object = nullptr; +} + +void* APS5_VABI _ZNKSt15__exception_ptr13exception_ptr6_M_getEv_nid_postfix(const GuestExceptionPointer* self) noexcept { + return self->object; +} + +void APS5_VABI _ZNSt15__exception_ptr13exception_ptrC1EPv_nid_postfix(GuestExceptionPointer* self, void* exception) noexcept { + self->object = exception; + __cxa_increment_exception_refcount_nid_postfix(exception); +} + +void APS5_VABI _ZNSt15__exception_ptr13exception_ptrC2EPv_nid_postfix(GuestExceptionPointer* self, void* exception) noexcept { + _ZNSt15__exception_ptr13exception_ptrC1EPv_nid_postfix(self, exception); +} + +const std::type_info* APS5_VABI _ZNKSt15__exception_ptr13exception_ptr20__cxa_exception_typeEv_nid_postfix(const GuestExceptionPointer* self) noexcept { + return LibcException::FromObject(self->object)->type; +} + +GuestExceptionPointer APS5_VABI _ZSt17current_exceptionv_nid_postfix() noexcept { + return GuestExceptionPointer(__cxa_current_primary_exception_nid_postfix()); +} + +[[noreturn]] void APS5_VABI _ZSt17rethrow_exceptionNSt15__exception_ptr13exception_ptrE_nid_postfix(GuestExceptionPointer& exception) { + __cxa_rethrow_primary_exception_nid_postfix(exception.object); + LibcException::Terminate(); +} +} + +#endif diff --git a/core/libs/prx/libc/src/exception/Personality.cpp b/core/libs/prx/libc/src/exception/Personality.cpp index 4090d8f15..87826a2dc 100644 --- a/core/libs/prx/libc/src/exception/Personality.cpp +++ b/core/libs/prx/libc/src/exception/Personality.cpp @@ -71,13 +71,6 @@ extern "C" _Unwind_Reason_Code APS5_VABI __gxx_personality_v0_nid_postfix( return _URC_CONTINUE_UNWIND; } -extern "C" _Unwind_Reason_Code __gxx_personality_v0( - int version, _Unwind_Action actions, std::uint64_t exceptionClass, - _Unwind_Exception* exception, _Unwind_Context* context -) { - return __gxx_personality_v0_nid_postfix(version, actions, exceptionClass, exception, context); -} - extern "C" _Unwind_Reason_Code APS5_VABI __gcc_personality_v0_nid_postfix( int version, _Unwind_Action actions, std::uint64_t, _Unwind_Exception* exception, _Unwind_Context* context @@ -108,9 +101,18 @@ extern "C" _Unwind_Reason_Code APS5_VABI __gcc_personality_v0_nid_postfix( return _URC_CONTINUE_UNWIND; } +#ifndef __APPLE__ +extern "C" _Unwind_Reason_Code __gxx_personality_v0( + int version, _Unwind_Action actions, std::uint64_t exceptionClass, + _Unwind_Exception* exception, _Unwind_Context* context +) { + return __gxx_personality_v0_nid_postfix(version, actions, exceptionClass, exception, context); +} + extern "C" _Unwind_Reason_Code __gcc_personality_v0( int version, _Unwind_Action actions, std::uint64_t exceptionClass, _Unwind_Exception* exception, _Unwind_Context* context ) { return __gcc_personality_v0_nid_postfix(version, actions, exceptionClass, exception, context); } +#endif diff --git a/core/libs/prx/libc/src/exception/Unwind.cpp b/core/libs/prx/libc/src/exception/Unwind.cpp index 14775faf2..9da514f5b 100644 --- a/core/libs/prx/libc/src/exception/Unwind.cpp +++ b/core/libs/prx/libc/src/exception/Unwind.cpp @@ -5,13 +5,16 @@ #ifdef _WIN32 #include #endif +#ifdef __APPLE__ +#include +#include +#include +#endif -#if defined(__linux__) || defined(_WIN32) +#if defined(__linux__) || defined(_WIN32) || defined(__APPLE__) -#if defined(_WIN32) || defined(__linux__) extern "C" _Unwind_Reason_Code __gxx_personality_v0(int, _Unwind_Action, std::uint64_t, _Unwind_Exception*, _Unwind_Context*); extern "C" _Unwind_Reason_Code __gcc_personality_v0(int, _Unwind_Action, std::uint64_t, _Unwind_Exception*, _Unwind_Context*); -#endif namespace LibcUnwind { _Unwind_Reason_Code CallPersonality(Word personality, _Unwind_Action actions, _Unwind_Exception* exception, _Unwind_Context* context) { @@ -31,7 +34,7 @@ _Unwind_Reason_Code CallPersonality(Word personality, _Unwind_Action actions, _U } struct Lookup { Word pc; const Byte* fde {}; Word text {}; Word data {}; }; -#ifdef __linux__ +#if defined(__linux__) || defined(__APPLE__) int FindFrame(dl_phdr_info* info, std::size_t, void* argument) { auto& query = *static_cast(argument); const Byte* header = nullptr; @@ -141,8 +144,32 @@ bool DecodeCandidate(_Unwind_Context& context, Frame& frame, const Lookup& query bool DecodeFrame(_Unwind_Context& context, Frame& frame) { Lookup query {context.registers[16] - !context.signalFrame}; -#ifdef __linux__ +#if defined(__linux__) || defined(__APPLE__) dl_iterate_phdr(FindFrame, &query); +#ifdef __APPLE__ + if (!query.fde) { + Dl_info image {}; + if (!dladdr(reinterpret_cast(query.pc), &image) || !image.dli_fbase) return false; + unsigned long size = 0; + const Byte* p = getsectiondata(static_cast(image.dli_fbase), "__TEXT", "__eh_frame", &size); + if (!p) return false; + const Byte* end = p + size; + while (end - p >= 8) { + const Byte* record = p; + const auto length = Read(p); + if (length == 0) return false; + if (length == 0xffffffff || length < 4 || Word(end - p) < length) return false; + const Byte* next = p + length; + if (Read(p)) { + query.fde = record; + frame = {}; + if (DecodeCandidate(context, frame, query)) return true; + } + p = next; + } + return false; + } +#endif return DecodeCandidate(context, frame, query); #else MEMORY_BASIC_INFORMATION memory{}; diff --git a/core/libs/prx/libc/src/specifics/linux/DlIteratePhdr.cpp b/core/libs/prx/libc/src/specifics/linux/DlIteratePhdr.cpp index 5556e14d3..cc2ff7b9e 100644 --- a/core/libs/prx/libc/src/specifics/linux/DlIteratePhdr.cpp +++ b/core/libs/prx/libc/src/specifics/linux/DlIteratePhdr.cpp @@ -7,7 +7,7 @@ extern "C" { int APS5_VABI dl_iterate_phdr_nid_postfix(int (*callback)(dl_phdr_info*, std::size_t, void*), void* data) { -#if defined(__linux__) +#if defined(__linux__) || defined(__APPLE__) return dl_iterate_phdr(callback, data); #else (void)callback; diff --git a/core/libs/prx/libc/src/specifics/macos/GuestImages.cpp b/core/libs/prx/libc/src/specifics/macos/GuestImages.cpp new file mode 100644 index 000000000..57a60699f --- /dev/null +++ b/core/libs/prx/libc/src/specifics/macos/GuestImages.cpp @@ -0,0 +1,42 @@ +#ifdef __APPLE__ + +#include "prx/libc/include/specifics/linux/ElfTypes.hpp" +#include +#include +#include +#include + +namespace { + +constexpr std::uint64_t MetaVersion = 2; +constexpr std::size_t LoadBiasOffset = 0x08; +constexpr std::size_t HeaderCountOffset = 0x58; +constexpr std::size_t HeadersOffset = 0x60; + +} + +extern "C" int dl_iterate_phdr(int (*callback)(dl_phdr_info*, std::size_t, void*), void* data) { + for (std::uint32_t index = 0; index < _dyld_image_count(); ++index) { + const auto* header = reinterpret_cast(_dyld_get_image_header(index)); + if (header == nullptr || header->magic != MH_MAGIC_64 || (header->flags & MH_DYLIB_IN_CACHE) != 0) continue; + unsigned long size = 0; + const auto* meta = getsectiondata(header, "__APS5DATA", "__meta", &size); + if (meta == nullptr || size < HeadersOffset) continue; + std::uint64_t version; + std::uint64_t loadBias; + std::uint64_t count; + std::memcpy(&version, meta, sizeof(version)); + std::memcpy(&loadBias, meta + LoadBiasOffset, sizeof(loadBias)); + std::memcpy(&count, meta + HeaderCountOffset, sizeof(count)); + if (version != MetaVersion || count > 0xffff || count > (size - HeadersOffset) / sizeof(Elf64_Phdr)) continue; + dl_phdr_info info {}; + info.dlpi_addr = static_cast(loadBias); + info.dlpi_name = header->filetype == MH_EXECUTE ? "" : _dyld_get_image_name(index); + info.dlpi_phdr = reinterpret_cast(meta + HeadersOffset); + info.dlpi_phnum = static_cast(count); + if (const int result = callback(&info, sizeof(info), data); result != 0) return result; + } + return 0; +} + +#endif diff --git a/core/libs/prx/libkernel/Module/src/Module.cpp b/core/libs/prx/libkernel/Module/src/Module.cpp index 3bc8244d8..466329d8c 100644 --- a/core/libs/prx/libkernel/Module/src/Module.cpp +++ b/core/libs/prx/libkernel/Module/src/Module.cpp @@ -11,6 +11,10 @@ #ifdef _WIN32 #include #include +#elif defined(__APPLE__) +#include +#include +#include #else #include #endif @@ -107,6 +111,51 @@ int APS5_VABI sceKernelGetModuleInfoForUnwind(uint64_t addr, int flags, ModuleIn std::strncpy(info->name, path, sizeof(info->name) - 1); info->name[sizeof(info->name) - 1] = '\0'; return 0; +#elif defined(__APPLE__) + struct Search { + std::uint64_t address; + ModuleInfoForUnwind* info; + bool found; + } search {addr, info, false}; + dl_iterate_phdr([](dl_phdr_info* image, std::size_t, void* data) { + auto& search = *static_cast(data); + const Elf64_Phdr* first = nullptr; + const Elf64_Phdr* frames = nullptr; + bool contains = false; + for (std::uint16_t index = 0; index < image->dlpi_phnum; ++index) { + const auto& header = image->dlpi_phdr[index]; + const auto start = image->dlpi_addr + header.p_vaddr; + if (header.p_type == PT_LOAD && first == nullptr) first = &header; + if (header.p_type == PT_LOAD && search.address >= start && search.address - start < header.p_memsz) contains = true; + if (header.p_type == PT_GNU_EH_FRAME) frames = &header; + } + if (!contains) return 0; + auto* info = search.info; + info->st_size = sizeof(ModuleInfoForUnwind); + std::strncpy(info->name, image->dlpi_name != nullptr ? image->dlpi_name : "", sizeof(info->name) - 1); + info->name[sizeof(info->name) - 1] = '\0'; + info->eh_frame_hdr_addr = frames != nullptr ? image->dlpi_addr + frames->p_vaddr : 0; + info->eh_frame_addr = 0; + info->eh_frame_size = 0; + info->seg0_addr = image->dlpi_addr + first->p_vaddr; + info->seg0_size = first->p_memsz; + search.found = true; + return 1; + }, &search); + if (search.found) return 0; + Dl_info image {}; + if (!dladdr(reinterpret_cast(addr), &image) || image.dli_fbase == nullptr) return SCE_KERNEL_ERROR_ESRCH; + unsigned long textSize = 0; + getsegmentdata(static_cast(image.dli_fbase), "__TEXT", &textSize); + info->st_size = sizeof(ModuleInfoForUnwind); + std::strncpy(info->name, image.dli_fname != nullptr ? image.dli_fname : "", sizeof(info->name) - 1); + info->name[sizeof(info->name) - 1] = '\0'; + info->eh_frame_hdr_addr = 0; + info->eh_frame_addr = 0; + info->eh_frame_size = 0; + info->seg0_addr = reinterpret_cast(image.dli_fbase); + info->seg0_size = textSize; + return 0; #else std::ifstream maps("/proc/self/maps"); if (!maps) throw std::runtime_error("sceKernelGetModuleInfoForUnwind: failed to open /proc/self/maps");