From 6670c5257131c5295a95522d43f601c6ce965370 Mon Sep 17 00:00:00 2001 From: Dean Galvin Date: Sun, 4 Oct 2026 05:32:55 +1300 Subject: [PATCH] fix(libc): run the cleanups of frames that use the C personality GCC gives C code built with -fexceptions, and cleanup-only functions after link-time optimization, the C personality __gcc_personality_v0. The unwinder accepted only its own C++ personality, so an exception thrown through such a frame was never caught. libc now provides the C personality, which never catches and installs the frame's cleanup landing pad in the cleanup phase, and the unwinder calls it for those frames. --- core/libs/prx/libc/CMakeLists.txt | 5 ++ .../prx/libc/src/exception/Personality.cpp | 30 ++++++++++++ core/libs/prx/libc/src/exception/Unwind.cpp | 19 ++++--- core/libs/tests/ExceptionCCleanup.c | 14 ++++++ core/libs/tests/ExceptionCCleanup.cpp | 49 +++++++++++++++++++ 5 files changed, 107 insertions(+), 10 deletions(-) create mode 100644 core/libs/tests/ExceptionCCleanup.c create mode 100644 core/libs/tests/ExceptionCCleanup.cpp diff --git a/core/libs/prx/libc/CMakeLists.txt b/core/libs/prx/libc/CMakeLists.txt index 718e49f6f..641a45cd2 100644 --- a/core/libs/prx/libc/CMakeLists.txt +++ b/core/libs/prx/libc/CMakeLists.txt @@ -163,6 +163,11 @@ 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) endif() endif() diff --git a/core/libs/prx/libc/src/exception/Personality.cpp b/core/libs/prx/libc/src/exception/Personality.cpp index 883a5357a..26377ad0c 100644 --- a/core/libs/prx/libc/src/exception/Personality.cpp +++ b/core/libs/prx/libc/src/exception/Personality.cpp @@ -77,3 +77,33 @@ extern "C" _Unwind_Reason_Code __gxx_personality_v0( ) { 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, + _Unwind_Exception* exception, _Unwind_Context* context +) { + using namespace LibcUnwind; + if (version != 1) return _URC_FATAL_PHASE1_ERROR; + if (!(actions & _UA_CLEANUP_PHASE) || !context->lsda) return _URC_CONTINUE_UNWIND; + const Byte* p = reinterpret_cast(context->lsda); + Byte landingEncoding = *p++; + Word landingBase = landingEncoding == 255 ? context->region : Encoded(p, landingEncoding, context->dataBase, context->region, context->textBase); + if (*p++ != 255) Uleb(p); + Byte callEncoding = *p++; + Word length = Uleb(p); + const Byte* callEnd = p + length; + Word ip = context->registers[16] - !context->signalFrame; + while (p < callEnd) { + Word start = Encoded(p, callEncoding); + Word size = Encoded(p, callEncoding); + Word landing = Encoded(p, callEncoding); + Uleb(p); + if (ip < context->region + start || ip - context->region - start >= size) continue; + if (!landing) return _URC_CONTINUE_UNWIND; + context->registers[0] = reinterpret_cast(exception); + context->registers[1] = 0; + context->registers[16] = landingBase + landing; + return _URC_INSTALL_CONTEXT; + } + return _URC_CONTINUE_UNWIND; +} diff --git a/core/libs/prx/libc/src/exception/Unwind.cpp b/core/libs/prx/libc/src/exception/Unwind.cpp index 95690e1f1..53b741735 100644 --- a/core/libs/prx/libc/src/exception/Unwind.cpp +++ b/core/libs/prx/libc/src/exception/Unwind.cpp @@ -10,10 +10,11 @@ #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 { -bool OwnPersonality(Word personality) { +_Unwind_Reason_Code CallPersonality(Word personality, _Unwind_Action actions, _Unwind_Exception* exception, _Unwind_Context* context) { #ifdef _WIN32 const auto* code = reinterpret_cast(personality); if (code[0] == 0xff && code[1] == 0x25) { @@ -22,11 +23,11 @@ bool OwnPersonality(Word personality) { std::memcpy(&personality, code + 6 + displacement, sizeof(personality)); } #endif - if (personality == reinterpret_cast(__gxx_personality_v0_nid_postfix)) return true; -#if defined(_WIN32) || defined(__linux__) - if (personality == reinterpret_cast(__gxx_personality_v0)) return true; -#endif - return false; + if (personality == reinterpret_cast(__gxx_personality_v0_nid_postfix) || personality == reinterpret_cast(__gxx_personality_v0)) + return __gxx_personality_v0_nid_postfix(1, actions, exception->exception_class, exception, context); + if (personality == reinterpret_cast(__gcc_personality_v0)) + return __gcc_personality_v0(1, actions, exception->exception_class, exception, context); + return (actions & _UA_SEARCH_PHASE) ? _URC_FATAL_PHASE1_ERROR : _URC_FATAL_PHASE2_ERROR; } struct Lookup { Word pc; const Byte* fde {}; Word text {}; Word data {}; }; @@ -393,8 +394,7 @@ _Unwind_Reason_Code PhaseTwo(_Unwind_Context context, _Unwind_Exception* excepti if (result != _URC_NO_REASON) return result; } else if (context.cfa == exception->private_2) actions = _Unwind_Action(actions | _UA_HANDLER_FRAME); if (frame.personality) { - if (!OwnPersonality(frame.personality)) return _URC_FATAL_PHASE2_ERROR; - auto result = __gxx_personality_v0_nid_postfix(1, actions, exception->exception_class, exception, &context); + auto result = CallPersonality(frame.personality, actions, exception, &context); if (result == _URC_INSTALL_CONTEXT) LibcRestoreRegisters(context.registers); if (result != _URC_CONTINUE_UNWIND) return _URC_FATAL_PHASE2_ERROR; } @@ -422,8 +422,7 @@ _Unwind_Reason_Code APS5_VABI _Unwind_RaiseException_nid_postfix(_Unwind_Excepti LibcUnwind::Frame frame; LibcUnwind::Rules rules; if (!LibcUnwind::GetRules(context, frame, rules)) return _URC_END_OF_STACK; if (frame.personality) { - if (!LibcUnwind::OwnPersonality(frame.personality)) return _URC_FATAL_PHASE1_ERROR; - auto result = __gxx_personality_v0_nid_postfix(1, _UA_SEARCH_PHASE, exception->exception_class, exception, &context); + auto result = LibcUnwind::CallPersonality(frame.personality, _UA_SEARCH_PHASE, exception, &context); if (result == _URC_HANDLER_FOUND) { exception->private_2 = context.cfa; return LibcUnwind::PhaseTwo(start, exception); diff --git a/core/libs/tests/ExceptionCCleanup.c b/core/libs/tests/ExceptionCCleanup.c new file mode 100644 index 000000000..83d508534 --- /dev/null +++ b/core/libs/tests/ExceptionCCleanup.c @@ -0,0 +1,14 @@ +struct CleanupCounter { int* count; }; + +static void Leave(struct CleanupCounter* counter) { ++*counter->count; } + +void CallWithCleanup(void (*function)(void), int* count) { + struct CleanupCounter counter __attribute__((cleanup(Leave))) = {count}; + function(); +} + +void CallAroundCleanup(void (*before)(void), void (*within)(void), int* count) { + before(); + struct CleanupCounter counter __attribute__((cleanup(Leave))) = {count}; + within(); +} diff --git a/core/libs/tests/ExceptionCCleanup.cpp b/core/libs/tests/ExceptionCCleanup.cpp new file mode 100644 index 000000000..cca7677ed --- /dev/null +++ b/core/libs/tests/ExceptionCCleanup.cpp @@ -0,0 +1,49 @@ +#include +#include +#include + +extern "C" void CallWithCleanup(void (*function)(), int* count); +extern "C" void CallAroundCleanup(void (*before)(), void (*within)(), int* count); +extern "C" void (*_ZSt13set_terminatePFvvE_nid_postfix(void (*)()))(); + +int cleanups; + +struct Counted { ~Counted() { ++cleanups; } }; + +[[gnu::noinline]] void Throw() { throw 42; } +[[gnu::noinline]] void Nothing() {} +[[gnu::noinline]] void ThrowThroughCleanup() { CallWithCleanup(Throw, &cleanups); } + +bool Caught(void (*function)(), int expectedCleanups) { + cleanups = 0; + try { + function(); + } catch (int value) { + return value == 42 && cleanups == expectedCleanups; + } + return false; +} + +bool TerminatesWithoutCleanup() { + pid_t child = fork(); + if (child < 0) return false; + if (child == 0) { + cleanups = 0; + _ZSt13set_terminatePFvvE_nid_postfix([] { _exit(cleanups == 0 ? 61 : 62); }); + CallWithCleanup(Throw, &cleanups); + _exit(63); + } + int status; + return waitpid(child, &status, 0) == child && WIFEXITED(status) && WEXITSTATUS(status) == 61; +} + +int main() { + if (!Caught([] { CallWithCleanup(Throw, &cleanups); }, 1)) return 1; + if (!Caught([] { CallWithCleanup(ThrowThroughCleanup, &cleanups); }, 2)) return 2; + if (!Caught([] { CallAroundCleanup(Throw, Nothing, &cleanups); }, 0)) return 3; + if (!Caught([] { CallAroundCleanup(Nothing, Throw, &cleanups); }, 1)) return 4; + if (!Caught([] { Counted counted; CallWithCleanup(Throw, &cleanups); }, 2)) return 5; + if (!TerminatesWithoutCleanup()) return 6; + std::puts("exception C cleanup tests passed"); + return 0; +}