Project

General

Profile

Actions

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/LSAN

AddressSanitizer 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.

  1. Add the PPA and update the package cache:
    $ sudo add-apt-repository ppa:ubuntu-toolchain-r/test
    $ sudo apt-get update
    
  2. Install new toolchain (GCC 5.x is sufficient, others will work too):
    $ sudo apt-get install g++-5
    

Rebuild with ASAN Support

Applications must be recompiled with ASAN support.
  1. Clear existing build directory:
    $ cd some/path/rhfs/build/
    $ rm -rf
    
  2. Set environment variables pointing to the appropriate compiler:
    $ export CXX=`which g++-5`
    $ export CC=`which gcc-5`
    
  3. Add/enable ASAN compiler flags: `-fsanitize=address -fno-omit-frame-pointer -fuse-ld=gold`.
  4. Run CMake.
    $ cmake ..
    

    Verify that the CMake output lists the intended compiler:
    ...
    -- The C compiler identification is GNU 5.4.1
    -- The CXX compiler identification is GNU 5.4.1
    ...
    
  5. Build then install:
    $ make -j44
    $ sudo make install
    
  6. 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:

$ sudo stop ccc_mgr
$ sudo stop rhfsd
$ sudo su
# ASAN_OPTIONS=detect_leaks=1 /usr/sbin/rhfsd 

Then in another terminal window:

$ sudo su
# ASAN_OPTIONS=detect_leaks=1 /usr/bin/ccc_mgr

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