mirror of
https://github.com/boykopovar/AnyPS5.git
synced 2026-10-10 01:28:44 +02:00
Merge pull request #478 from oneandonlydean/fix/libc-unwind-c-personality
fix(libc): run the cleanups of frames that use the C personality
This commit is contained in:
@@ -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()
|
||||
|
||||
|
||||
@@ -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<const Byte*>(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<Word>(exception);
|
||||
context->registers[1] = 0;
|
||||
context->registers[16] = landingBase + landing;
|
||||
return _URC_INSTALL_CONTEXT;
|
||||
}
|
||||
return _URC_CONTINUE_UNWIND;
|
||||
}
|
||||
|
||||
@@ -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<const Byte*>(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<Word>(__gxx_personality_v0_nid_postfix)) return true;
|
||||
#if defined(_WIN32) || defined(__linux__)
|
||||
if (personality == reinterpret_cast<Word>(__gxx_personality_v0)) return true;
|
||||
#endif
|
||||
return false;
|
||||
if (personality == reinterpret_cast<Word>(__gxx_personality_v0_nid_postfix) || personality == reinterpret_cast<Word>(__gxx_personality_v0))
|
||||
return __gxx_personality_v0_nid_postfix(1, actions, exception->exception_class, exception, context);
|
||||
if (personality == reinterpret_cast<Word>(__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);
|
||||
|
||||
@@ -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();
|
||||
}
|
||||
@@ -0,0 +1,49 @@
|
||||
#include <cstdio>
|
||||
#include <sys/wait.h>
|
||||
#include <unistd.h>
|
||||
|
||||
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;
|
||||
}
|
||||
Reference in New Issue
Block a user