Debugging with AddressSanitizer and LeakSanitizer¶
While Valgrind is a very useful tool is has a few issues, namely that it is very slow and doesn't always work (there are issues running RHFS with it, for example). Fortunately some clever folks at Google have created several sanitizers that implement much of the same functionality but work reliably (at least in my experience) and run much faster.
Of particular interest are AddressSanitizer (aka ASAN) for finding various memory errors and LeakSanitizer (aka LSAN) for finding memory leaks.
Using ASAN/LSANAddressSanitizer is a part of LLVM/Clang starting with version 3.1 and a part of GCC starting with version 4.8, however LeakSanitizer wasn't added until GCC version 4.9. Unfortunately Ubuntu 14.04 ships with GCC 4.8 so to use LeakSanitizer you will need to upgrade your toolchain.
Upgrade Toolchain¶
As mentioned above Ubuntu 14.04 ships with GCC 4.8 so to use LeakSanitizer you will need to upgrade your toolchain to something more modern. The Ubuntu toolchain PPA provides backports of more recent toolchains. The new toolchain will be installed alongside the default one making it possible to install several if desired.
- Add the PPA and update the package cache:
- Install new toolchain (GCC 5.x is sufficient, others will work too):
Rebuild with ASAN Support¶
Applications must be recompiled with ASAN support.- Clear existing build directory:
- Set environment variables pointing to the appropriate compiler:
- Add/enable ASAN compiler flags: `-fsanitize=address -fno-omit-frame-pointer -fuse-ld=gold`.
- Run CMake.
Verify that the CMake output lists the intended compiler: - Build then install:
- When done debugging remember to rebuild with the default toolchain.
Debugging¶
ASAN and LSAN output to stderr, so programs must be run in the foreground to see output.
For example to run RHFS:
Then in another terminal window:
Finally run searches/tests in a third terminal window.
Notes¶
- LeakSanitizer does not run until/unless the program terminates cleanly. Verify your program is terminating cleanly or you will get no leak check output.
- By default ASAN terminates a running program when it finds a memory error. To enable continue-after-error, compile with `-fsanitize-recover=address` and then run your code with `ASAN_OPTIONS=halt_on_error=0`.
Updated by almost 5 years ago · 1 revisions