#include "prx/libc/include/exceptions/Unwind.hpp" #include "prx/libc/include/Api.hpp" #include "prx/libc/include/exceptions/Runtime.hpp" #include "prx/libc/include/RunReport.hpp" #include #include #include #include #include #include #include #ifdef _WIN32 #include #else #include #include #endif #ifdef __APPLE__ namespace { [[maybe_unused]] const bool hostRefstringPrepared = [] { const int saved = errno; { const std::runtime_error prepared(""); } errno = saved; return true; }(); } #endif namespace LibcException { // Writes straight to the stderr handle: terminate paths must not wait on the CRT stream lock, // which another (possibly suspended) thread may hold. static void RawWrite(const char* data, std::size_t size) { #ifdef _WIN32 const HANDLE handle = GetStdHandle(STD_ERROR_HANDLE); while (size != 0) { DWORD written = 0; const auto chunk = static_cast(std::min(size, std::numeric_limits::max())); if (!WriteFile(handle, data, chunk, &written, nullptr) || written == 0) return; data += written; size -= written; } #else const int kept = HostStandardError_nid_no_patch(); const int descriptor = kept >= 0 ? kept : STDERR_FILENO; while (size != 0) { const auto written = ::write(descriptor, data, size); if (written < 0 && errno == EINTR) continue; if (written <= 0) return; data += written; size -= static_cast(written); } #endif } static void RawLog(const char* format, ...) { char buffer[1024]; va_list args; va_start(args, format); int length = std::vsnprintf(buffer, sizeof(buffer), format, args); va_end(args); if (length <= 0) return; if (length > static_cast(sizeof(buffer)) - 1) length = sizeof(buffer) - 1; RawWrite(buffer, static_cast(length)); } const char* CaughtWhat(const char** typeName) { if (!globals.caught || !Native(globals.caught->unwind.exception_class)) return nullptr; Header* primary = Primary(globals.caught); if (typeName != nullptr) *typeName = primary->type ? primary->type->name() : nullptr; void* exception = primary->adjusted; if (!exception || !primary->type || !Match(&typeid(std::exception), primary->type, exception)) return nullptr; struct VtableLayout { std::ptrdiff_t offset; const void* type; void (*destroy)(void*); void (*del)(void*); const char* (*whatFn)(const void*); }; const void* vtable = *static_cast(exception); auto* layout = reinterpret_cast(static_cast(vtable) - offsetof(VtableLayout, destroy)); #ifdef _WIN32 if (!primary->_pad) { using GuestWhat = const char* (APS5_VABI *)(const void*); return reinterpret_cast(layout->whatFn)(exception); } #endif return layout->whatFn(exception); } [[noreturn]] static void DefaultTerminate() { if (globals.caught && Native(globals.caught->unwind.exception_class)) { const char* typeName = nullptr; const char* what = CaughtWhat(&typeName); int status = 0; char* demangled = abi::__cxa_demangle(typeName, nullptr, nullptr, &status); const char* displayName = (status == 0 && demangled) ? demangled : typeName; if (what) { RawLog("terminate called after throwing an instance of '%s'\n what(): ", displayName); RawWrite(what, std::strlen(what)); RawWrite("\n", 1); } else RawLog("terminate called after throwing an instance of '%s'\n", displayName); std::free(demangled); } else { RawLog("terminate called without an active exception\n"); } std::abort(); } inline std::atomic terminateHandler {DefaultTerminate}; [[noreturn]] void InvokeTerminate(void (*handler)()) { const bool runtimeHandler = handler == nullptr || handler == DefaultTerminate || handler == std::abort; if (runtimeHandler) LibcRunReportTerminate_nid_no_patch(CaughtWhat(nullptr)); try { if (handler) handler(); } catch (...) {} if (!runtimeHandler) LibcRunReportTerminate_nid_no_patch(CaughtWhat(nullptr)); std::abort(); } [[noreturn]] void Terminate() { InvokeTerminate(terminateHandler.load(std::memory_order_acquire)); } _Unwind_Exception* UnwindOf(Header* header) { return Native(header->unwind.exception_class) ? &header->unwind : reinterpret_cast<_Unwind_Exception*>(header->landing); } void DeleteForeignHeader(Header* header) { header->~Header(); std::free(header); } void Release(void* object) { if (!object) return; auto* header = FromObject(object); auto* allocation = AllocationOf(header); if (allocation->references.fetch_sub(1, std::memory_order_acq_rel) == 1) { try { if (header->destructor) { #ifdef _WIN32 if (!header->_pad) { using GuestDestructor = void (__attribute__((sysv_abi)) *)(void*); reinterpret_cast(header->destructor)(object); } else #endif header->destructor(object); } } catch (...) { Terminate(); } allocation->~Allocation(); std::free(allocation); } } static void APS5_VABI Cleanup(_Unwind_Reason_Code reason, _Unwind_Exception* exception) { if (reason != _URC_FOREIGN_EXCEPTION_CAUGHT) Terminate(); auto* header = FromUnwind(exception); if (exception->exception_class == DependentClass) { Release(reinterpret_cast(header->type)); header->~Header(); std::free(header); } else Release(header + 1); } bool Equal(const std::type_info* a, const std::type_info* b) { return a == b || (a && b && std::strcmp(a->name(), b->name()) == 0); } static const std::type_info* TypeOfTypeInfo(const std::type_info* type) { auto table = *reinterpret_cast(type); return table[-1]; } static const std::type_info* BaseAtSameAddress(const std::type_info* type) { const char* kind = TypeOfTypeInfo(type)->name(); if (std::strcmp(kind, "N10__cxxabiv120__si_class_type_infoE") == 0) return static_cast(type)->__base_type; if (std::strcmp(kind, "N10__cxxabiv121__vmi_class_type_infoE") != 0) return nullptr; auto* vmi = static_cast(type); if (vmi->__base_count != 1 || (vmi->__base_info[0].__offset_flags & ~2L) != 0) return nullptr; return vmi->__base_info[0].__base_type; } const char* Kind(const std::type_info* type) { const std::type_info* kind = TypeOfTypeInfo(type); for (unsigned depth = 0; depth < 16 && std::strncmp(kind->name(), "N10__cxxabiv1", 13) != 0; ++depth) { const std::type_info* base = BaseAtSameAddress(kind); if (!base) break; kind = base; } return kind->name(); } struct Search { const std::type_info* wanted; void* found {}; unsigned count {}; }; struct BaseLocation { void* object; std::uintptr_t identity; }; template void VisitBases(const std::type_info* type, BaseLocation location, bool publicPath, Visitor& visitor, unsigned depth = 0) { if (!type) return; if (depth > 128) Terminate(); visitor(type, location, publicPath); const char* kind = Kind(type); if (std::strcmp(kind, "N10__cxxabiv120__si_class_type_infoE") == 0) { auto* si = static_cast(type); VisitBases(si->__base_type, location, publicPath, visitor, depth + 1); } else if (std::strcmp(kind, "N10__cxxabiv121__vmi_class_type_infoE") == 0) { auto* vmi = static_cast(type); for (unsigned i = 0; i < vmi->__base_count; ++i) { const auto& base = vmi->__base_info[i]; auto offset = base.__offset_flags >> 8; BaseLocation next = location; if (base.__offset_flags & 1) { if (location.object) { auto* vtable = *static_cast(location.object); std::memcpy(&offset, vtable + offset, sizeof(offset)); } else { next.identity = reinterpret_cast(base.__base_type); offset = 0; } } if (location.object) next.object = static_cast(location.object) + offset; else next.identity += offset; VisitBases(base.__base_type, next, publicPath && (base.__offset_flags & 2), visitor, depth + 1); } } } void Bases(const std::type_info* type, void* object, Search& search) { std::uintptr_t identity = 0; bool reachedPublicly = false; auto visitor = [&](const std::type_info* candidate, BaseLocation location, bool publicPath) { if (!Equal(candidate, search.wanted)) return; if (!search.count) { search.found = location.object; identity = location.identity; search.count = 1; reachedPublicly = publicPath; } else if (search.found != location.object || identity != location.identity) search.count = 2; else if (publicPath) reachedPublicly = true; }; VisitBases(type, {object, 0}, true, visitor); if (search.count == 1 && !reachedPublicly) search.count = 0; } bool PointerMatch(const __cxxabiv1::__pbase_type_info* caught, const __cxxabiv1::__pbase_type_info* thrown, void*& object, unsigned depth, bool constPath) { if (depth > 64) return false; unsigned added = (caught->__flags & 7) & ~(thrown->__flags & 7); if (((thrown->__flags & 7) & ~(caught->__flags & 7)) || (depth && added && !constPath)) return false; if ((caught->__flags & 0x60) & ~(thrown->__flags & 0x60)) return false; if (Equal(caught->__pointee, thrown->__pointee)) return true; if (std::strcmp(Kind(caught->__pointee), "N10__cxxabiv119__pointer_type_infoE") == 0 && std::strcmp(Kind(thrown->__pointee), "N10__cxxabiv119__pointer_type_infoE") == 0) return PointerMatch(static_cast(caught->__pointee), static_cast(thrown->__pointee), object, depth + 1, constPath && (caught->__flags & 1)); if (depth) return false; if (std::strcmp(caught->__pointee->name(), "v") == 0 && std::strcmp(Kind(thrown->__pointee), "N10__cxxabiv120__function_type_infoE") != 0) return true; Search search {caught->__pointee}; Bases(thrown->__pointee, object, search); if (search.count != 1) return false; object = search.found; return true; } bool Match(const std::type_info* caught, const std::type_info* thrown, void*& object) { if (!caught || Equal(caught, thrown)) return true; if (!thrown) return false; const char* caughtKind = Kind(caught); const char* thrownKind = Kind(thrown); if (std::strcmp(caughtKind, "N10__cxxabiv119__pointer_type_infoE") == 0 && std::strcmp(thrownKind, "N10__cxxabiv119__pointer_type_infoE") == 0) { return PointerMatch(static_cast(caught), static_cast(thrown), object, 0, true); } if (std::strcmp(caughtKind, "N10__cxxabiv119__pointer_type_infoE") == 0 && std::strcmp(thrown->name(), "Dn") == 0) { object = nullptr; return true; } if (std::strcmp(caughtKind, "N10__cxxabiv129__pointer_to_member_type_infoE") == 0 && std::strcmp(thrownKind, "N10__cxxabiv129__pointer_to_member_type_infoE") == 0) { auto* c = static_cast(caught); auto* t = static_cast(thrown); return Equal(c->__context, t->__context) && PointerMatch(c, t, object, 0, true); } Search search {caught}; Bases(thrown, object, search); if (search.count == 1) { object = search.found; return true; } return false; } } extern "C" { void* APS5_VABI __cxa_allocate_exception_nid_postfix(std::size_t size) { using namespace LibcException; if (size > std::numeric_limits::max() - sizeof(Allocation)) Terminate(); void* storage = std::malloc(sizeof(Allocation) + size); if (!storage) Terminate(); static_assert(offsetof(Allocation, header) + sizeof(Header) == sizeof(Allocation)); auto* allocation = new (storage) Allocation; return &allocation->header + 1; } void APS5_VABI __cxa_free_exception_nid_postfix(void* object) { if (!object) return; auto* allocation = LibcException::AllocationOf(LibcException::FromObject(object)); allocation->~Allocation(); std::free(allocation); } [[noreturn]] static void ThrowFromSite(void* object, std::type_info* type, void (*destructor)(void*), void* throwSite) { using namespace LibcException; auto* header = FromObject(object); header->type = type; header->destructor = destructor; header->terminate = terminateHandler.load(std::memory_order_acquire); header->adjusted = object; header->unwind.exception_class = PrimaryClass; header->unwind.exception_cleanup = Cleanup; ++globals.uncaught; _Unwind_RaiseException_nid_postfix(&header->unwind); RawLog("[libc] unhandled exception '%s' thrown from %p\n", type ? type->name() : "?", throwSite); __cxa_begin_catch_nid_postfix(&header->unwind); InvokeTerminate(header->terminate); } [[noreturn]] void APS5_VABI __cxa_throw_nid_postfix(void* object, std::type_info* type, void (*destructor)(void*)) { ThrowFromSite(object, type, destructor, __builtin_return_address(0)); } LibcException::Header* APS5_VABI __cxa_init_primary_exception_nid_postfix(void* object, std::type_info* type, void (*destructor)(void*)) { using namespace LibcException; auto* header = FromObject(object); AllocationOf(header)->references.store(0, std::memory_order_relaxed); header->type = type; header->destructor = destructor; header->terminate = terminateHandler.load(std::memory_order_acquire); header->adjusted = object; header->unwind.exception_class = PrimaryClass; header->unwind.exception_cleanup = Cleanup; return header; } void* APS5_VABI __cxa_begin_catch_nid_postfix(void* exception) { using namespace LibcException; auto* unwind = static_cast<_Unwind_Exception*>(exception); if (!unwind) Terminate(); Header* header; if (Native(unwind->exception_class)) { header = FromUnwind(unwind); if (globals.uncaught) --globals.uncaught; } else if (globals.caught && UnwindOf(globals.caught) == unwind) { header = globals.caught; } else { void* storage = std::malloc(sizeof(Header)); if (!storage) Terminate(); header = new (storage) Header; header->unwind.exception_class = unwind->exception_class; header->landing = reinterpret_cast(unwind); header->adjusted = unwind + 1; } header->handlers = header->handlers < 0 ? -header->handlers + 1 : header->handlers + 1; if (globals.caught != header) { header->next = globals.caught; globals.caught = header; } return header->adjusted; } void APS5_VABI __cxa_end_catch_nid_postfix() { using namespace LibcException; auto* header = globals.caught; if (!header) return; if (header->handlers < 0) { if (++header->handlers == 0) { globals.caught = header->next; if (!Native(header->unwind.exception_class)) DeleteForeignHeader(header); } } else if (--header->handlers == 0) { globals.caught = header->next; bool foreign = !Native(header->unwind.exception_class); _Unwind_DeleteException_nid_postfix(UnwindOf(header)); if (foreign) DeleteForeignHeader(header); } } [[noreturn]] void APS5_VABI __cxa_rethrow_nid_postfix() { using namespace LibcException; auto* header = globals.caught; if (!header) Terminate(); header->handlers = -header->handlers; if (Native(header->unwind.exception_class)) ++globals.uncaught; _Unwind_RaiseException_nid_postfix(UnwindOf(header)); __cxa_begin_catch_nid_postfix(UnwindOf(header)); InvokeTerminate(header->terminate ? header->terminate : terminateHandler.load()); } void* APS5_VABI __cxa_get_exception_ptr_nid_postfix(void* exception) { auto* unwind = static_cast<_Unwind_Exception*>(exception); return LibcException::Native(unwind->exception_class) ? LibcException::FromUnwind(unwind)->adjusted : unwind + 1; } std::type_info* APS5_VABI __cxa_current_exception_type_nid_postfix() { using namespace LibcException; return globals.caught && Native(globals.caught->unwind.exception_class) ? Primary(globals.caught)->type : nullptr; } void* APS5_VABI __cxa_get_globals_nid_postfix() { return &LibcException::globals; } void* APS5_VABI __cxa_get_globals_fast_nid_postfix() { return &LibcException::globals; } bool APS5_VABI __cxa_uncaught_exception_nid_postfix() { return LibcException::globals.uncaught != 0; } unsigned APS5_VABI __cxa_uncaught_exceptions_nid_postfix() { return LibcException::globals.uncaught; } bool APS5_VABI _ZSt18uncaught_exceptionv_nid_postfix() { return __cxa_uncaught_exception_nid_postfix(); } int APS5_VABI _ZSt19uncaught_exceptionsv_nid_postfix() { return static_cast(__cxa_uncaught_exceptions_nid_postfix()); } [[noreturn]] void APS5_VABI _ZSt9terminatev_nid_postfix() { LibcException::Terminate(); } LibcTerminateHandler APS5_VABI _ZSt13set_terminatePFvvE_nid_postfix(LibcTerminateHandler handler) { #ifdef __APPLE__ std::set_terminate(handler ? handler : std::abort); #endif return LibcException::terminateHandler.exchange(handler ? handler : std::abort); } LibcTerminateHandler APS5_VABI _ZSt13get_terminatev_nid_postfix() { return LibcException::terminateHandler.load(); } void APS5_VABI __cxa_increment_exception_refcount_nid_postfix(void* object) { if (object) LibcException::AllocationOf(LibcException::FromObject(object))->references.fetch_add(1, std::memory_order_relaxed); } void APS5_VABI __cxa_decrement_exception_refcount_nid_postfix(void* object) { LibcException::Release(object); } void* APS5_VABI __cxa_current_primary_exception_nid_postfix() { using namespace LibcException; if (!globals.caught || !Native(globals.caught->unwind.exception_class)) return nullptr; void* object = Primary(globals.caught) + 1; __cxa_increment_exception_refcount_nid_postfix(object); return object; } void* APS5_VABI __cxa_allocate_dependent_exception_nid_postfix() { void* storage = std::malloc(sizeof(LibcException::Header)); if (!storage) LibcException::Terminate(); return new (storage) LibcException::Header; } void APS5_VABI __cxa_free_dependent_exception_nid_postfix(void* storage) { if (!storage) return; static_cast(storage)->~Header(); std::free(storage); } void APS5_VABI __cxa_rethrow_primary_exception_nid_postfix(void* object) { using namespace LibcException; if (!object) return; auto* header = static_cast(__cxa_allocate_dependent_exception_nid_postfix()); header->type = reinterpret_cast(object); header->adjusted = object; header->terminate = terminateHandler.load(); header->unwind.exception_class = DependentClass; header->unwind.exception_cleanup = Cleanup; __cxa_increment_exception_refcount_nid_postfix(object); ++globals.uncaught; _Unwind_RaiseException_nid_postfix(&header->unwind); __cxa_begin_catch_nid_postfix(&header->unwind); InvokeTerminate(header->terminate); } void* APS5_VABI __dynamic_cast_nid_postfix(const void* source, const __cxxabiv1::__class_type_info* sourceType, const __cxxabiv1::__class_type_info* destinationType, std::ptrdiff_t) { using namespace LibcException; if (!source) return nullptr; auto* vtable = *static_cast(source); auto* complete = const_cast(static_cast(source)) + static_cast(vtable[-2]); auto* dynamicType = reinterpret_cast(vtable[-1]); Search downcastTarget {destinationType}; auto visitor = [&](const std::type_info* candidate, BaseLocation location, bool) { if (!Equal(candidate, destinationType)) return; bool containsSource = false; auto sourceVisitor = [&](const std::type_info* base, BaseLocation baseLocation, bool publicPath) { if (publicPath && Equal(base, sourceType) && baseLocation.object == source) containsSource = true; }; VisitBases(candidate, location, true, sourceVisitor); if (!containsSource) return; if (!downcastTarget.count) { downcastTarget.found = location.object; downcastTarget.count = 1; } else if (downcastTarget.found != location.object) downcastTarget.count = 2; }; VisitBases(dynamicType, {complete, 0}, true, visitor); if (downcastTarget.count == 1) return downcastTarget.found; bool sourceIsPublic = false; auto sourceVisitor = [&](const std::type_info* candidate, BaseLocation location, bool publicPath) { if (publicPath && Equal(candidate, sourceType) && location.object == source) sourceIsPublic = true; }; VisitBases(dynamicType, {complete, 0}, true, sourceVisitor); if (!sourceIsPublic) return nullptr; Search publicTarget {destinationType}; Bases(dynamicType, complete, publicTarget); return publicTarget.count == 1 ? publicTarget.found : nullptr; } [[noreturn]] void APS5_VABI __cxa_call_terminate_nid_postfix(void* exception) { __cxa_begin_catch_nid_postfix(exception); LibcException::Terminate(); } [[noreturn]] void APS5_VABI __cxa_pure_virtual_nid_postfix() { LibcException::Terminate(); } [[noreturn]] void APS5_VABI __cxa_deleted_virtual_nid_postfix() { LibcException::Terminate(); } }