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: check for null value returned from pthread_mach_thread_np #3443

Closed
wants to merge 2 commits into from
Closed
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
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
# Changelog

## Unreleased

### Fixes

- Crash when dereferencing an invalid thread pointer (#3443)

## 8.16.1

### Fixes
Expand Down
9 changes: 5 additions & 4 deletions Sources/Sentry/PrivateSentrySDKOnly.mm
Original file line number Diff line number Diff line change
Expand Up @@ -141,10 +141,11 @@ + (uint64_t)startProfilerForTrace:(SentryId *)traceId;

if (payload != nil) {
payload[@"platform"] = SentryPlatformName;
payload[@"transaction"] = @{
@"active_thread_id" :
[NSNumber numberWithLongLong:sentry::profiling::ThreadHandle::current()->tid()]
};
const auto thread = sentry::profiling::ThreadHandle::current();
if (thread != nullptr) {
payload[@"transaction"] =
@{ @"active_thread_id" : [NSNumber numberWithLongLong:thread->tid()] };
}
}

return payload;
Expand Down
6 changes: 5 additions & 1 deletion Sources/Sentry/SentryProfiler.mm
Original file line number Diff line number Diff line change
Expand Up @@ -428,12 +428,16 @@ + (void)updateProfilePayload:(NSMutableDictionary<NSString *, id> *)payload
forTransaction:(SentryTransaction *)transaction;
{
payload[@"platform"] = transaction.platform;

const auto activeThreadID =
[transaction.trace.transactionContext sentry_threadInfo].threadId ?: @(-1);
payload[@"transaction"] = @{
@"id" : transaction.eventId.sentryIdString,
@"trace_id" : transaction.trace.traceId.sentryIdString,
@"name" : transaction.transaction,
@"active_thread_id" : [transaction.trace.transactionContext sentry_threadInfo].threadId
@"active_thread_id" : activeThreadID
Comment on lines +432 to +438
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure what the right thing to put here would be.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do you mean if the thread ID is NULL? If yes, couldn't we just not set the active_thread_id?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

maybe; i'm not sure what ramifications that would have in the backend

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@phacops Please let us know how we would proceed in this case. Maybe we just choose not to send the profile at all.

};

const auto timestamp = transaction.trace.originalStartTimestamp;
if (UNLIKELY(timestamp == nil)) {
SENTRY_LOG_WARN(@"There was no start timestamp on the provided transaction. Falling back "
Expand Down
3 changes: 3 additions & 0 deletions Sources/Sentry/SentryThreadHandle.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,9 @@
ThreadHandle::current() noexcept
{
const auto thread = pthread_mach_thread_np(pthread_self());
if (thread == (mach_port_t)NULL) {
return nullptr;

Check warning on line 49 in Sources/Sentry/SentryThreadHandle.cpp

View check run for this annotation

Codecov / codecov/patch

Sources/Sentry/SentryThreadHandle.cpp#L49

Added line #L49 was not covered by tests
}
Comment on lines +48 to +50
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think we actually need this, the crash was actually happening when current() is dereferenced (see the callsites in this diff)

return std::make_unique<ThreadHandle>(thread);
}

Expand Down
18 changes: 13 additions & 5 deletions Sources/Sentry/SentryTransactionContext.mm
Original file line number Diff line number Diff line change
Expand Up @@ -116,25 +116,31 @@
_name = [NSString stringWithString:name];
_nameSource = source;
self.parentSampled = parentSampled;
#if SENTRY_TARGET_PROFILING_SUPPORTED
[self getThreadInfo];
#endif // SENTRY_TARGET_PROFILING_SUPPORTED
}
return self;
}

#if SENTRY_TARGET_PROFILING_SUPPORTED
- (void)getThreadInfo
{
#if SENTRY_TARGET_PROFILING_SUPPORTED
const auto threadID = sentry::profiling::ThreadHandle::current()->tid();
const auto thread = sentry::profiling::ThreadHandle::current();
if (thread == nullptr) {
return;

Check warning on line 131 in Sources/Sentry/SentryTransactionContext.mm

View check run for this annotation

Codecov / codecov/patch

Sources/Sentry/SentryTransactionContext.mm#L131

Added line #L131 was not covered by tests
}
const auto threadID = thread->tid();
self.threadInfo = [[SentryThread alloc] initWithThreadId:@(threadID)];
#endif
}
#endif // SENTRY_TARGET_PROFILING_SUPPORTED

#if SENTRY_TARGET_PROFILING_SUPPORTED
- (SentryThread *)sentry_threadInfo
- (nullable SentryThread *)sentry_threadInfo
{
return self.threadInfo;
}
#endif
#endif // SENTRY_TARGET_PROFILING_SUPPORTED

- (void)commonInitWithName:(NSString *)name
source:(SentryTransactionNameSource)source
Expand All @@ -143,7 +149,9 @@
_name = [NSString stringWithString:name];
_nameSource = source;
self.parentSampled = parentSampled;
#if SENTRY_TARGET_PROFILING_SUPPORTED
[self getThreadInfo];
#endif // SENTRY_TARGET_PROFILING_SUPPORTED
SENTRY_LOG_DEBUG(@"Created transaction context with name %@", name);
}

Expand Down
11 changes: 7 additions & 4 deletions Sources/Sentry/include/SentryTransactionContext+Private.h
Original file line number Diff line number Diff line change
Expand Up @@ -37,12 +37,15 @@ SentryTransactionContext ()
parentSampled:(SentrySampleDecision)parentSampled;

#if SENTRY_TARGET_PROFILING_SUPPORTED

// This is currently only exposed for testing purposes, see -[SentryProfilerTests
// testProfilerMutationDuringSerialization]
@property (nonatomic, strong) SentryThread *threadInfo;
// testProfilerMutationDuringSerialization]. Can be null if there was a problem
// getting the current thread info from the underlying API.
@property (nonatomic, strong, nullable) SentryThread *threadInfo;

- (nullable SentryThread *)sentry_threadInfo;

- (SentryThread *)sentry_threadInfo;
#endif
#endif // SENTRY_TARGET_PROFILING_SUPPORTED

@end

Expand Down
2 changes: 1 addition & 1 deletion Tests/SentryProfilerTests/SentryProfilerSwiftTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -826,7 +826,7 @@ private extension SentryProfilerSwiftTests {
XCTAssertNotEqual(SentryId.empty, linkedTransactionTraceId)

let activeThreadId = try XCTUnwrap(linkedTransactionInfo["active_thread_id"] as? NSNumber)
XCTAssertEqual(activeThreadId, latestTransaction.trace.transactionContext.sentry_threadInfo().threadId)
XCTAssertEqual(activeThreadId, try XCTUnwrap(latestTransaction.trace.transactionContext.sentry_threadInfo()).threadId)

for sample in samples {
let timestamp = try XCTUnwrap(sample["elapsed_since_start_ns"] as? NSString)
Expand Down
Loading