10

Trying to use the AddressSanitizer tool (ASan) on my C++ project, I get a very verbose output full of undefined reference to '__asan_report_store8' and undefined reference to '__asan_report_load8', and others like __asan_stack_malloc_2. My project is built with CMake, and I've added the following line to my CMakeLists.txt:

add_definitions(-fsanitize=address -g3 -fno-omit-frame-pointer)

following these ASan examples:

Running the above makefile example I get the expected pretty output (which can also be seen in the video link). But for my project I'm getting a messy stack trace of those errors.

Notes:

  • ubuntu 16.04
  • I've set the environment variables ASAN_OPTIONS=symbolize=1 and ASAN_SYMBOLIZER_PATH=/usr/lib/llvm-5.0/bin/llvm-symbolizer in order to print the line numbers in the ASan output
BoltzmannBrain
  • 5,082
  • 11
  • 46
  • 79

1 Answers1

20

When compiling code to be run with one of the LLVM Sanitizers, you have to pass the -fsanitize=... flag to both the compiler and the linker. With CMake, you can do this by calling target_link_libraries:

target_link_libraries(MyTarget
  -fsanitize=address
)

Alternatively, if you aren't using modern CMake, you can do the same with the link_libraries command

Justin
  • 24,288
  • 12
  • 92
  • 142
  • Yes that does it! Now I can see my memory leaks :) – BoltzmannBrain May 03 '18 at 21:32
  • Any advice on getting the line numbers to print in the output? The config I specified in above doesn't appear to work. And when I set `ASAN_SYMBOLIZER_PATH=/usr/lib/llvm-5.0/bin/llvm-symbolizer` as an `add_definitions` argument, cmake gives me an error that the path does not exist (when indeed it does, found with `which llvm-symbolizer`). – BoltzmannBrain May 03 '18 at 23:42
  • @BoltzmannBrain Have you set `ASAN_OPTIONS=symbolize=1` in environment? – yugr May 04 '18 at 08:18
  • @yugr yes, see the original post above. I set those options from the command line as `export ASAN_OPTIONS=symbolize=1` and then run the CMake steps. – BoltzmannBrain May 04 '18 at 16:42
  • 4
    CMake 3.13+ provides [target_link_options](https://cmake.org/cmake/help/v3.13/command/target_link_options.html#target-link-options) as well. – Kevin W Matthews Dec 16 '18 at 20:05