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

Conversation

armcknight
Copy link
Member

We didn't completely fix the problem reported in #3354 with #3364. This changes the callsites to check for NULL before dereferencing the pointer.

I checked the sources and it does appear that NULL is a possible return value from pthread_mach_thread_np : https://github.com/apple/darwin-libpthread/blob/2b46cbcc56ba33791296cd9714b2c90dae185ec7/src/pthread.c#L931-L937 and for _pthread_is_valid , https://opensource.apple.com/source/libpthread/libpthread-416.11.1/src/internal.h.

Comment on lines +432 to +438
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
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.

Comment on lines +48 to +50
if (thread == (mach_port_t)NULL) {
return nullptr;
}
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)

Copy link

codecov bot commented Nov 23, 2023

Codecov Report

Merging #3443 (72da8d3) into main (cb6ab62) will increase coverage by 0.049%.
The diff coverage is 76.190%.

Additional details and impacted files

Impacted file tree graph

@@              Coverage Diff              @@
##              main     #3443       +/-   ##
=============================================
+ Coverage   88.967%   89.016%   +0.049%     
=============================================
  Files          525       525               
  Lines        56577     56630       +53     
  Branches     20357     20390       +33     
=============================================
+ Hits         50335     50410       +75     
+ Misses        5317      5300       -17     
+ Partials       925       920        -5     
Files Coverage Δ
Sources/Sentry/PrivateSentrySDKOnly.mm 87.272% <100.000%> (+0.116%) ⬆️
...SentryProfilerTests/SentryProfilerSwiftTests.swift 97.769% <100.000%> (ø)
Sources/Sentry/SentryProfiler.mm 80.979% <66.666%> (-0.180%) ⬇️
Sources/Sentry/SentryThreadHandle.cpp 71.891% <33.333%> (-0.636%) ⬇️
Sources/Sentry/SentryTransactionContext.mm 97.590% <77.777%> (-2.410%) ⬇️

... and 26 files with indirect coverage changes


Continue to review full report in Codecov by Sentry.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update cb6ab62...72da8d3. Read the comment docs.

Copy link

Performance metrics 🚀

  Plain With Sentry Diff
Startup time 1226.71 ms 1246.04 ms 19.33 ms
Size 22.85 KiB 413.47 KiB 390.62 KiB

Baseline results on branch: main

Startup times

Revision Plain With Sentry Diff
2af280d 1246.22 ms 1253.10 ms 6.88 ms
bb5dc7d 1240.44 ms 1266.45 ms 26.01 ms
2b4a663 1220.55 ms 1249.98 ms 29.43 ms
407ff99 1250.10 ms 1269.78 ms 19.67 ms
e2abb0d 1235.08 ms 1257.00 ms 21.92 ms
230ba67 1230.18 ms 1252.32 ms 22.13 ms
45d3ca5 1248.27 ms 1255.48 ms 7.21 ms
904d7fa 1225.73 ms 1249.22 ms 23.49 ms
881a955 1222.16 ms 1237.22 ms 15.06 ms
39b1c35 1235.90 ms 1257.37 ms 21.47 ms

App size

Revision Plain With Sentry Diff
2af280d 20.76 KiB 435.22 KiB 414.46 KiB
bb5dc7d 22.85 KiB 412.98 KiB 390.13 KiB
2b4a663 20.76 KiB 436.65 KiB 415.89 KiB
407ff99 20.76 KiB 427.87 KiB 407.10 KiB
e2abb0d 20.76 KiB 434.72 KiB 413.96 KiB
230ba67 20.76 KiB 427.35 KiB 406.59 KiB
45d3ca5 20.76 KiB 427.54 KiB 406.78 KiB
904d7fa 20.76 KiB 432.87 KiB 412.11 KiB
881a955 22.85 KiB 407.63 KiB 384.78 KiB
39b1c35 22.85 KiB 408.88 KiB 386.03 KiB

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.

What's missing to finish this PR apart from tests, @armcknight?

Comment on lines +432 to +438
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
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?

@armcknight
Copy link
Member Author

What's missing to finish this PR apart from tests, @armcknight?

The customer reported that this branch doesn't resolve the crash.

@armcknight
Copy link
Member Author

#3520 builds on this by adding more stringent and additional checks in other spots, so I'll close this in favor of moving that one forward.

@armcknight armcknight closed this Jan 9, 2024
@armcknight armcknight deleted the armcknight/fix/3354-nilcheck branch February 16, 2024 12:34
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.

2 participants