From 728804f93e9f74d591108dd8a8d2df61a645c02b Mon Sep 17 00:00:00 2001 From: Andrew McKnight Date: Mon, 30 Oct 2023 11:25:37 -0800 Subject: [PATCH] fix: avoid call to possibly crashing `mach_thread_deallocate` (#3364) --- CHANGELOG.md | 3 ++- Sources/Sentry/SentryBacktrace.cpp | 3 +++ Sources/Sentry/SentryThreadHandle.cpp | 5 ++--- 3 files changed, 7 insertions(+), 4 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 2cfd7cc9c16..6d8182c455e 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -6,9 +6,10 @@ - Add thread id and name to span data (#3359) -### Improvements +### Fixes - Stop sending empty thread names (#3361) +- Work around edge case with a thread info kernel call sometimes returning invalid data, leading to a crash (#3364) ## 8.14.2 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>