Skip to content

Commit

Permalink
fix: avoid call to possibly crashing mach_thread_deallocate (#3364)
Browse files Browse the repository at this point in the history
  • Loading branch information
armcknight authored Oct 30, 2023
1 parent f15a6bb commit 728804f
Show file tree
Hide file tree
Showing 3 changed files with 7 additions and 4 deletions.
3 changes: 2 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
3 changes: 3 additions & 0 deletions Sources/Sentry/SentryBacktrace.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -76,6 +77,7 @@ namespace profiling {
if (UNLIKELY(!isValidFrame(current, bounds))) {
return 0;
}

bool reachedEndOfStack = false;
while (depth < maxDepth) {
const auto frame = reinterpret_cast<StackFrame *>(current);
Expand All @@ -92,6 +94,7 @@ namespace profiling {
break;
}
}

if (LIKELY(reachedEndOfStackPtr != nullptr)) {
*reachedEndOfStackPtr = reachedEndOfStack;
}
Expand Down
5 changes: 2 additions & 3 deletions Sources/Sentry/SentryThreadHandle.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -44,9 +44,8 @@ namespace profiling {
std::unique_ptr<ThreadHandle>
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<ThreadHandle>(port);
const auto thread = pthread_mach_thread_np(pthread_self());
return std::make_unique<ThreadHandle>(thread);
}

std::vector<std::unique_ptr<ThreadHandle>>
Expand Down

0 comments on commit 728804f

Please sign in to comment.