How to detect if building with address sanitizer when building with gcc 4.8?
From the GCC 4.8.0 manual: __SANITIZE_ADDRESS__ This macro is defined, with value 1, when -fsanitize=address is in use.
From the GCC 4.8.0 manual: __SANITIZE_ADDRESS__ This macro is defined, with value 1, when -fsanitize=address is in use.
You can build your project with -fsanitize=address -fsanitize-recover=address flags, and run with environment variable ASAN_OPTIONS=halt_on_error=0. Source: https://github.com/google/sanitizers/wiki/AddressSanitizer
This is what is working for me: Make sure you have installed llvm (including llvm-symbolizer). Export the following variable export ASAN_SYMBOLIZER_PATH=/usr/bin/llvm-symbolizer (replace with your correct path to the llvm-symbolizer command). Now run your executable (a.out for now) as ASAN_OPTIONS=symbolize=1 a.out
You need to add -fsanitize=address to compiler flags (both CFLAGS and CXXFLAGS) and linker flags (LDFLAGS). You’ve probably added it to your compiler flags only. Note that using explicit -lasan option has been widely discouraged by ASan developers (e.g. here) as it misses some other important linker flags. The only recommended way to link is … Read more
While breaking on the detection functions (as described by @Mark Plotnick and @Iwillnotexist Idonotexist) is one option, a better approach is breaking on the functions that report these issues after detection. This approach is also used for ASAN where one would break on __asan_report_error. Summary: You can stop on an ubsan report via a breakpoint … Read more
I think you’ll find this wiki useful. TLDR main advantages of sanitizers are much smaller CPU overheads (Lsan is practically free, UBsan/Isan is 1.25x, Asan and Msan are 2-4x for computationally intensive tasks and 1.05-1.1x for GUIs, Tsan is 5-15x) wider class of detected errors (stack and global overflows, use-after-return/scope) full support of multi-threaded apps … Read more
Since the top-voted answer is the wrong way to do it these days and I did not get the proper cmake solution for this reading this thread, I thought I would mention the correct way at the time of writing this so that the next reader does not need to spend much time with this. … Read more
You need to add -fsanitize=address to compiler flags (both CFLAGS and CXXFLAGS) and linker flags (LDFLAGS). You’ve probably added it to your compiler flags only. Note that using explicit -lasan option has been widely discouraged by ASan developers (e.g. here) as it misses some other important linker flags. The only recommended way to link is … Read more