Was the program compiled with -fsanitize=thread?

101 views
Skip to first unread message

Simone Atzeni

unread,
Jul 21, 2017, 10:58:15 AM7/21/17
to thread-sanitizer
Hi,

is there a way to know at runtime if the program was compiled with Tsan?

Thanks!
Simone

Francis Ricci

unread,
Jul 21, 2017, 11:03:10 AM7/21/17
to thread-s...@googlegroups.com, simo...@gmail.com
__has_feature(thread_sanitizer) should work

Refer to https://clang.llvm.org/docs/ThreadSanitizer.html for more info
> --
> You received this message because you are subscribed to the Google Groups
> "thread-sanitizer" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to thread-sanitiz...@googlegroups.com.
> For more options, visit https://groups.google.com/d/optout.

Simone Atzeni

unread,
Jul 21, 2017, 11:07:56 AM7/21/17
to thread-sanitizer, simo...@gmail.com
Hi Francis,

thanks for your suggestion.
That would work only at compile time, for example to enabled some code only when tsan flag is enabled, right?

I was hoping to be able to call a function from a shared library that returns true if the program (that is calling the shared library) was compiled with tsan.

Simone

Francis Ricci

unread,
Jul 21, 2017, 11:25:14 AM7/21/17
to thread-s...@googlegroups.com, simo...@gmail.com
You could use __has_feature to write a function that returns true if
the function was compiled with tsan. You could also theoretically
check for the presence of weak hooks (interface functions in
tsan_interface.h), but that's a bit messier.

Kuba Mracek

unread,
Jul 21, 2017, 11:26:31 AM7/21/17
to thread-sanitizer, Simone Atzeni
I think that "was the program compiled with TSan" is a weird question, because you can have a program that only has some parts/sources/modules/libraries built with the instrumentation, and some that are not.  So instead I usually ask the question "is the process running with the TSan runtime", which can be answered in many ways, for example with dlsym("__tsan_read8"), i.e. looking up a well-known exported function.  Other option is to iterate over loaded modules and look for "libclang_rt.tsan...".

But note that the TSan runtime can be present even if you don't have a single instrumented file:  The interceptors can sometimes catch problems even without any instrumentation.

Kuba
Reply all
Reply to author
Forward
0 new messages