7

We use the following code to determine if -fsanitize=address has been specified at compile time for clang and gcc. How do we determine if -fsanitize=undefined has been specified?

    bool isSanitized = false;
#if defined(__has_feature)
#if __has_feature(address_sanitizer)
    isSanitized = true;
#endif
#elif defined(__SANITIZE_ADDRESS__)
    isSanitized = true;
#endif
yugr
  • 13,457
  • 3
  • 37
  • 71
  • You might check this http://stackoverflow.com/questions/38719560/is-there-a-way-to-store-clang-compile-time-flags-in-the-output-binary – Jans Sep 09 '16 at 22:53
  • That doesn't work the same way as ASAN detection. – Jonathan Abrahams Sep 12 '16 at 14:33
  • A request for this was opened at https://github.com/google/sanitizers/issues/765 and subsequently closed for lack of a convincing use case. – ecatmur Sep 28 '18 at 19:29

3 Answers3

2

I suggest you file this as a bug to ASan github (or GCC bugzilla) about this (we already have defined for ASan and TSan so it makes sense to cook one for UBSan as well). For now it seems your only option is to pass a custom define together with -fsanitize-undefined in a Makefile.

yugr
  • 13,457
  • 3
  • 37
  • 71
1

With Clang, you can now check for UBSan very similarly to how you can check for ASan:

#if defined(__has_feature)
#  if __has_feature(undefined_behavior_sanitizer)
#    define HAS_UBSAN 1
#  endif
#endif

I don't know if it's possible to check under GCC yet though.

aij
  • 4,278
  • 28
  • 38
0

A request for this was opened at https://github.com/google/sanitizers/issues/765 and subsequently closed for lack of a convincing use case.

A hack to detect presence of ubsan at run time is to look for one of its symbols, typically a symbol associated with the undefined behavior you are interested in. For example, to detect whether ubsan is sanitizing __builtin_unreachable you can check for the presence of __ubsan_handle_builtin_unreachable:

extern "C" void __attribute__((weak)) __ubsan_handle_builtin_unreachable();

if (&__ubsan_handle_builtin_unreachable)
    ; // ubsan is present
else
    ; // ubsan is not present
ecatmur
  • 137,771
  • 23
  • 263
  • 343