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鈥檒l 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 4 commits into
base: main
Choose a base branch
from

Conversation

indragiek
Copy link
Member

馃摐 Description

pthread_mach_thread_np can return invalid values so check for those and return nullptr from ThreadHandle. That means changing the call sites for ThreadHandle::current() to check for a null pointer value.

馃挕 Motivation and Context

We don't have a solid repro for #3354 but this is one of the potential causes. In any case it doesn't hurt to add the check.

馃挌 How did you test it?

Run tests

馃摑 Checklist

You have to check all boxes before merging:

  • I reviewed the submitted code.
  • I added tests to verify the changes.
  • No new PII added or SDK only sends newly added PII if sendDefaultPII is enabled.
  • I updated the docs if needed.
  • Review from the native team if needed.
  • No breaking change or entry added to the changelog.
  • No breaking change for hybrid SDKs or communicated to hybrid SDKs.

馃敭 Next steps

pthread_mach_thread_np can return invalid values so check for those and return nullptr from ThreadHandle
@indragiek indragiek force-pushed the indragiek/pthread_mach_thread_np-null branch from 6990048 to b656bde Compare December 27, 2023 21:16
Copy link
Member

@philipphofmann philipphofmann left a comment

Choose a reason for hiding this comment

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

@indragiek, good catch 馃憤 . CI is broken, and it would be great to add or change some tests for these changes if possible.

Sources/Sentry/SentryThreadHandle.cpp Show resolved Hide resolved
@@ -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>>
Copy link
Member

Choose a reason for hiding this comment

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

h: If I'm not mistaken, we have to check for a potential nullptr of this method here

const auto depth = backtrace(*thread, *pair.second, addresses, stackBounds,
&reachedEndOfStack, kMaxBacktraceDepth, 0);
or does backtrace handle nullptr correctly?

Copy link
Member

Choose a reason for hiding this comment

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

Good catch, doesn't look like there's a check for nullptr before this dereference.

Copy link
Member

@armcknight armcknight left a comment

Choose a reason for hiding this comment

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

Reading the logic again in SentryBacktrace.cpp which contains the callsite for the function we've modified is a bit unclear to me, given the name allExcludingCurrent. Reading it starting at the loop, it looks like we're going to do work on all the other threads except the current one, which is true, but we do still have to retain the current thread in order to know what thread we're running on, and the callsite tells me we threw out that info when we actually didn't.

TL;DR I would propose we rename allExcludingCurrent to currentAndOtherThreads.

Sources/Sentry/SentryThreadHandle.cpp Show resolved Hide resolved
@@ -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>>
Copy link
Member

Choose a reason for hiding this comment

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

Good catch, doesn't look like there's a check for nullptr before this dereference.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants