Page MenuHomePhabricator

[TSan] Register threads created via pthread_create_from_mach_thread
AcceptedPublic

Authored by yln on Jan 10 2020, 6:04 PM.

Details

Summary

Previously, TSan was crashing on the first call into the runtime from
any thread created via pthread_create_from_mach_thread SPI. These
threads were uninitialized, because TSan could not observe their
creation. This meant that TSan crashed for Xcode's SwiftUI preview
(which uses remote code injection).

Threads created via pthread_create_from_mach_thread bypass the
pthread_create interceptor / __tsan_thread_start_func thread
function wrapper that usually ensures that threads are properly
registered. In addition, we don't receive THREAD_CREATE/DESTROY events
in our pthread introspection hooks. The reason for this is that those
would run in the context of the parent mach thread which makes them not
very useful.

Adding an interceptor for this SPI is not sufficient; it is typically
looked up in the parent process and then marshalled into the
instrumented process.

This change uses the following strategy to register these threads. For
the THREAD_CREATE event, we attach enough information to the thread that
is being created so that in THREAD_START we can identify how the thread
that we are on was actually created. For regular threads and GCD
workers there will be identifying information, for "special" threads
(threads for which we are unable to observe creation, e.g., threads
created via pthread_create_from_mach_thread from a Mach thread) the
absence of this information lets us infer that they are special.

rdar://57269889

Event Timeline

yln created this revision.Jan 10 2020, 6:04 PM
Herald added projects: Restricted Project, Restricted Project. · View Herald TranscriptJan 10 2020, 6:04 PM
Herald added subscribers: llvm-commits, Restricted Project, jfb. · View Herald Transcript
MadCoder accepted this revision.Jan 11 2020, 10:48 AM
MadCoder added inline comments.
compiler-rt/lib/tsan/rtl/tsan_platform_mac.cpp
208

nit: it should be void * not const void * ;) but it really doesn't matter for you

This revision is now accepted and ready to land.Jan 11 2020, 10:48 AM
yln marked 2 inline comments as done.Jan 13 2020, 9:55 AM
yln added inline comments.
compiler-rt/lib/tsan/rtl/tsan_platform_mac.cpp
208

Thanks! Fixed.

yln updated this revision to Diff 237719.Jan 13 2020, 10:03 AM
yln marked an inline comment as done.

Fixed signature of pthread_setspecific_introspection_np and added
CHECK_EQ(res, 0); to ensure the call to it succeeds.