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

Check MACH_PORT_VALID on return from pthread_mach_thread_np #3520

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 4 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
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
# Changelog

## Unreleased

### Fixes

- Check MACH_PORT_VALID on return from pthread_mach_thread_np (#3520)

### Features

Expand Down
7 changes: 3 additions & 4 deletions Sources/Sentry/PrivateSentrySDKOnly.mm
Original file line number Diff line number Diff line change
Expand Up @@ -140,11 +140,10 @@ + (uint64_t)startProfilerForTrace:(SentryId *)traceId;
onHub:[SentrySDK currentHub]];

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

return payload;
Expand Down
7 changes: 7 additions & 0 deletions Sources/Sentry/SentryThreadHandle.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,9 @@ namespace profiling {
ThreadHandle::current() noexcept
{
const auto thread = pthread_mach_thread_np(pthread_self());
if (!MACH_PORT_VALID(thread)) {
return nullptr;
}
return std::make_unique<ThreadHandle>(thread);
}

Expand Down Expand Up @@ -74,6 +77,10 @@ namespace profiling {
mach_msg_type_number_t count;
thread_act_array_t list;
auto current = ThreadHandle::current();
if (current == nullptr) {
return std::make_pair(threads, nullptr);
philipphofmann marked this conversation as resolved.
Show resolved Hide resolved
}

if (SENTRY_PROF_LOG_KERN_RETURN(task_threads(mach_task_self(), &list, &count))
== KERN_SUCCESS) {
for (decltype(count) i = 0; i < count; i++) {
Expand Down
5 changes: 3 additions & 2 deletions Sources/Sentry/SentryTransactionContext.mm
Original file line number Diff line number Diff line change
Expand Up @@ -124,8 +124,9 @@ - (instancetype)initWithName:(NSString *)name
- (void)getThreadInfo
{
#if SENTRY_TARGET_PROFILING_SUPPORTED
const auto threadID = sentry::profiling::ThreadHandle::current()->tid();
self.threadInfo = [[SentryThread alloc] initWithThreadId:@(threadID)];
const auto thread = sentry::profiling::ThreadHandle::current();
self.threadInfo =
[[SentryThread alloc] initWithThreadId:(thread == nullptr) ? @0 : @(thread->tid())];
#endif
}

Expand Down
4 changes: 4 additions & 0 deletions Sources/Sentry/include/SentryThreadHandle.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,8 @@ namespace profiling {
/**
* @return A handle to the currently executing thread, which is acquired
* in a non async-signal-safe manner.
*
* Returns nullptr if the current thread could not be retrieved.
*/
static std::unique_ptr<ThreadHandle> current() noexcept;

Expand All @@ -69,6 +71,8 @@ namespace profiling {
* the threads in the current process, excluding the current thread (the
* thread that this function is being called on), and the second element
* is a handle to the current thread.
*
* Returns ({}, nullptr) if the current thread could not be retrieved.
*/
static std::pair<std::vector<std::unique_ptr<ThreadHandle>>, std::unique_ptr<ThreadHandle>>
armcknight marked this conversation as resolved.
Show resolved Hide resolved
allExcludingCurrent() noexcept;
Expand Down
Loading