This is an archive of the discontinued LLVM Phabricator instance.

[tsan] Add interceptors for objc_sync_enter and objc_sync_exit
ClosedPublic

Authored by kubamracek on Apr 10 2018, 7:35 AM.

Details

Summary

Objective-C's @synchronize synchronization primitive uses calls to objc_sync_enter and objc_sync_exit runtime functions. In most cases, they end up just calling pthread_mutex_lock/pthread_mutex_unlock, but there are some cases where the synchronization from pthread_mutex_lock/pthread_mutex_unlock interceptors isn't enough. Let's add explicit interceptors for objc_sync_enter and objc_sync_exit to handle all cases.

Diff Detail

Repository
rL LLVM

Event Timeline

kubamracek created this revision.Apr 10 2018, 7:35 AM
Herald added subscribers: Restricted Project, mgorny. · View Herald TranscriptApr 10 2018, 7:35 AM

Adding test.

Would it be hard to add another test where those interceptors do produce a warning?

delcypher added inline comments.Apr 10 2018, 10:56 AM
lib/tsan/CMakeLists.txt
121 ↗(On Diff #141862)

Does this mean that clang_rt.tsan will now always link against libobjc? Is that really necessary. Won't that mean it gets linked in for sanitized C programs that have absolutely no business doing anything with the ObjectiveC runtime?

kubamracek added inline comments.Apr 10 2018, 11:03 AM
lib/tsan/CMakeLists.txt
121 ↗(On Diff #141862)

You're right, this always links libobjc, even into program that are not using Objective-C. I don't know how to avoid this dependency, because the dyld interposition requires us to link against the symbols that we intercept.

On the bright side, libobjc gets loaded into every process on macOS anyway. So I believe this is actually safe and shouldn't break anything.

Would it be hard to add another test where those interceptors do produce a warning?

Whaat kind of test are you looking for? As this adds more synchronization edges, this strictly removes reports (false positives in this case). I can add a test that incorrectly uses @synchronized that still triggers a TSan report, if you want.

this strictly removes reports

Ah sorry, didn't realize that.
I thought it would remove some reports and add some as well (deadlocks? but now I see those are not fully supported)
LGTM in that case.

This revision is now accepted and ready to land.Apr 10 2018, 11:08 AM
delcypher added inline comments.Apr 11 2018, 9:59 AM
lib/tsan/CMakeLists.txt
121 ↗(On Diff #141862)

Okay. Presumably, TSan will still initialise properly (i.e. the fact that TSan links libobjc won't break initialisation order)?

delcypher accepted this revision.Apr 11 2018, 9:59 AM
kubamracek added inline comments.Apr 11 2018, 10:19 AM
lib/tsan/CMakeLists.txt
121 ↗(On Diff #141862)

Although libobjc isn't part of libSystem, it's actually loaded by libSystem unconditionally to all processes, and libobjc initializer (_objc_init) is called early in process startup. So explicitly linking against libobjc shouldn't really change any initialization order.

This revision was automatically updated to reflect the committed changes.