====== 2013.05.25 - address sanitizer ====== when it comes to debugging memory issues two tools have a warm place in my heart: [[wp>Duma (software)|duma]] and [[wp>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 -- [[https://code.google.com/p/address-sanitizer|address sanitizer]] plugin for [[wp>clang]], starting with version 3.1. it is [[http://clang.llvm.org/docs/AddressSanitizer.html|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 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: {{:blog:2013:05:25:asan_error_report.png|asan error report: heap}} similarly out-of-bound reads on stack can be detected. happy debugging! :)