2013.05.25 - address sanitizer

when it comes to debugging memory issues two tools have a warm place in my heart: duma and valgrind. both are nice, but there is always some “but”. duma is fast, but is only able to detect issues related to a heap. valgrind is a bit more robust (in fact – not limited to out-of-range r/w detection), but is terribly slow. recently i got aware of a new kid in town – address sanitizer plugin for clang, starting with version 3.1. it is incredibly easy to use: it is enough to pass -g -fsanitize=address -fno-omit-frame-pointer flags to compilation and linking, and it is done – code is instrumented using Asan!

the main idea behind this tool is to instrument code, in places that “might go wrong”, instead of relying on page allocations (heap-only) or full emulation (slow). most of the memory issues can be detected this way, while having minimal overhead on the performance (~2x, typically) and extra memory usage (both heap and stack).

having sample program:

#include <iostream>
using namespace std;
char const* mkStr(string const& in)
{
  return in.c_str();
}
int main(void)
{
  auto str = mkStr("abc");
  cout << str << endl;
  return 0;
}

and running binary compiled with llvm/clang-trunk (i.e. pre-3.3 release), produces the following output:

asan error report: heap

similarly out-of-bound reads on stack can be detected. happy debugging! :)

blog/2013/05/25/asan.txt · Last modified: 2013/05/25 11:34 by basz
Back to top
Valid CSS Driven by DokuWiki Recent changes RSS feed Valid XHTML 1.0