From b855bb6866c343daa24ff5d1ea4f043e06814305 Mon Sep 17 00:00:00 2001 From: Andrew McKnight Date: Fri, 27 Oct 2023 12:55:11 -0800 Subject: [PATCH 1/3] fix: use pthread api that doesn't require calling mach_port_deallocate --- Sources/Sentry/SentryBacktrace.cpp | 3 +++ Sources/Sentry/SentryThreadHandle.cpp | 5 ++--- 2 files changed, 5 insertions(+), 3 deletions(-) diff --git a/Sources/Sentry/SentryBacktrace.cpp b/Sources/Sentry/SentryBacktrace.cpp index dc0c557f58a..ad7d65a1d22 100644 --- a/Sources/Sentry/SentryBacktrace.cpp +++ b/Sources/Sentry/SentryBacktrace.cpp @@ -68,6 +68,7 @@ namespace profiling { } else { current = getFrameAddress(&machineContext); } + // Even if this bounds check passes, the frame pointer address could still be invalid if the // thread was suspended in an inconsistent state. The best we can do is to detect these // situations at symbolication time on the server and filter them out -- there's not an easy @@ -76,6 +77,7 @@ namespace profiling { if (UNLIKELY(!isValidFrame(current, bounds))) { return 0; } + bool reachedEndOfStack = false; while (depth < maxDepth) { const auto frame = reinterpret_cast(current); @@ -92,6 +94,7 @@ namespace profiling { break; } } + if (LIKELY(reachedEndOfStackPtr != nullptr)) { *reachedEndOfStackPtr = reachedEndOfStack; } diff --git a/Sources/Sentry/SentryThreadHandle.cpp b/Sources/Sentry/SentryThreadHandle.cpp index f4e6b0864ab..2aa44ac6530 100644 --- a/Sources/Sentry/SentryThreadHandle.cpp +++ b/Sources/Sentry/SentryThreadHandle.cpp @@ -44,9 +44,8 @@ namespace profiling { std::unique_ptr ThreadHandle::current() noexcept { - const auto port = mach_thread_self(); - SENTRY_PROF_LOG_KERN_RETURN(mach_port_deallocate(mach_task_self(), port)); - return std::make_unique(port); + const auto thread = pthread_mach_thread_np(pthread_self()); + return std::make_unique(thread); } std::vector> From 18412229f5d359033191270b5c8d9cb3d3455e0e Mon Sep 17 00:00:00 2001 From: Andrew McKnight Date: Mon, 30 Oct 2023 11:22:23 -0800 Subject: [PATCH 2/3] changelog --- CHANGELOG.md | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index e9fac15162d..5bbed075b90 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -6,6 +6,10 @@ - Add thread id and name to span data (#3359) +### Fixes + +- Work around edge case with a thread info syscall sometimes returning an invalid pointer, leading to a crash (#3364) + ## 8.14.2 ### Features From 6ef8a517b12cf313856090e1e48b5acaaf2e8fbe Mon Sep 17 00:00:00 2001 From: Andrew McKnight Date: Mon, 30 Oct 2023 11:25:20 -0800 Subject: [PATCH 3/3] Update CHANGELOG.md --- CHANGELOG.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index fabb5555904..6d8182c455e 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -9,7 +9,7 @@ ### Fixes - Stop sending empty thread names (#3361) -- Work around edge case with a thread info syscall sometimes returning an invalid pointer, leading to a crash (#3364) +- Work around edge case with a thread info kernel call sometimes returning invalid data, leading to a crash (#3364) ## 8.14.2