Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix tail call optimisation in C++ backtrace capture #468

Merged
merged 2 commits into from
May 23, 2024
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
Original file line number Diff line number Diff line change
Expand Up @@ -77,12 +77,13 @@ static KSStackCursor g_stackCursor;
#pragma mark - Callbacks -
// ============================================================================

static void captureStackTrace(void*, std::type_info*, void (*)(void*))
static void captureStackTrace(void*, std::type_info*, void (*)(void*)) __attribute__((disable_tail_calls))
{
if(g_captureNextStackTrace)
{
kssc_initSelfThread(&g_stackCursor, 2);
}
__asm__ __volatile__(""); // thwart tail-call optimization
}

typedef void (*cxa_throw_type)(void*, std::type_info*, void (*)(void*));
Expand All @@ -91,7 +92,7 @@ extern "C"
{
void __cxa_throw(void* thrown_exception, std::type_info* tinfo, void (*dest)(void*)) __attribute__ ((weak));

void __cxa_throw(void* thrown_exception, std::type_info* tinfo, void (*dest)(void*))
void __cxa_throw(void* thrown_exception, std::type_info* tinfo, void (*dest)(void*)) __attribute__((disable_tail_calls))
{
static cxa_throw_type orig_cxa_throw = NULL;
if (g_cxaSwapEnabled == false)
Expand All @@ -103,6 +104,7 @@ extern "C"
orig_cxa_throw = (cxa_throw_type) dlsym(RTLD_NEXT, "__cxa_throw");
}
orig_cxa_throw(thrown_exception, tinfo, dest);
__asm__ __volatile__(""); // thwart tail-call optimization
__builtin_unreachable();
}
}
Expand Down
3 changes: 2 additions & 1 deletion Sources/KSCrashRecordingCore/KSStackCursor_SelfThread.c
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,10 @@ typedef struct
uintptr_t backtrace[0];
} SelfThreadContext;

void kssc_initSelfThread(KSStackCursor *cursor, int skipEntries)
void kssc_initSelfThread(KSStackCursor *cursor, int skipEntries) __attribute__((disable_tail_calls))
{
SelfThreadContext* context = (SelfThreadContext*)cursor->context;
int backtraceLength = backtrace((void**)context->backtrace, MAX_BACKTRACE_LENGTH);
kssc_initWithBacktrace(cursor, context->backtrace, backtraceLength, skipEntries + 1);
__asm__ __volatile__(""); // thwart tail-call optimization
}
Loading