Curious
Curious

Reputation: 21540

How to detect thread sanitizer for gcc 5

How can I detect whether thread sanitizer has been turned on for a build using gcc 5? Neither one of the two between __has_feature(thread_sanitizer) nor __SANITIZE_THREAD__ work

#include <iostream>

using std::cout;
using std::endl;

int main() {
    cout << __has_feature(thread_sanitizer) << endl;
    cout << __SANITIZE_THREAD__ << endl;
}

https://wandbox.org/permlink/t5qYme4Whyj54aYV. This compiles on the versions of clang that have thread sanitizer; but not for some gcc versions (5 in particular)


Both the feature check and the __SANITIZE_THREAD__ macro are useful in detecting when the thread sanitizer has been turned on so tests can suppress false-negatives (eg. when thread sanitizer catches a bug that's not actually a data race) See this for more

Upvotes: 7

Views: 2040

Answers (1)

Wyck
Wyck

Reputation: 11760

I don't know, but as a last resort, the following command line will find a #define if it exists:

diff <(gcc -dM -E -x c /dev/null) <(gcc -fsanitize=thread -dM -E -x c /dev/null)

On my gcc 7.4.0 it outputs:

> #define __SANITIZE_THREAD__ 1

...which means that __SANITIZE_THREAD__ is defined to be 1 if you are using -fsanitize=thread, but is not defined if you don't. So you should guard your code behind an #ifdef __SANITIZE_THREAD__ rather than just using the symbol directly.

Additional Info:

I checked the gcc source and the __SANITIZE_THREAD__ macro was not introduced until version 7.1.0.

Upvotes: 9

Related Questions