Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to determine when -fsanitize=memory is in use?

I want to clear a false positive on FD_ZERO and FD_SET when the memory sanitizer is in use. Clearing it is somewhat easy:

#include <sanitizer/msan_interface.h>
...

__msan_unpoison(&readfds, sizeof(readfds));
__msan_unpoison(&writefds, sizeof(writefds));

However, I don't know how to detect when the memory sanitizer is in use. That is, detect when -fsanitize=memory was specified on the command line. The preprocessor does not seem to be helping:

$ clang -dM -E -fsanitize=memory - </dev/null | egrep -i 'memory|sanitize|msan'
$

How can I determine when -fsanitize=memory is in use?

like image 825
jww Avatar asked Oct 30 '22 08:10

jww


1 Answers

According to Konstantin Serebryany on the Memory Sanitizer mailing list, there is no preprocessor macro. The __has_feature(memory_sanitizer) should be used:

#if defined(__has_feature)
#  if __has_feature(memory_sanitizer)
#    define MEMORY_SANITIZER 1
#  endif
#endif
...

#ifdef MEMORY_SANITIZER
#  include <sanitizer/msan_interface.h>
#endif
...

#ifdef MEMORY_SANITIZER
__msan_unpoison(&readfds, sizeof(readfds));
__msan_unpoison(&writefds, sizeof(writefds));
#endif
...
like image 62
jww Avatar answered Nov 14 '22 09:11

jww