Difference between revisions of "Address Sanitizer"

From PHASTA Wiki
Jump to: navigation, search
(Created page with "== Introduction == Address sanitizer is a compiler extension and runtime library used for debugging memory errors. It works by introducing extra "canary" regions around memory al...")
(No difference)

Revision as of 12:59, 15 July 2013

Introduction

Address sanitizer is a compiler extension and runtime library used for debugging memory errors. It works by introducing extra "canary" regions around memory allocated by your program, and checks for accesses to those regions. This allows it to detect when your program accesses memory that has not been properly allocated. You can read more about how address sanitizer works [here]

Use

Address sanitizer is available in newer versions of GCC (>= 4.8.0) and Clang (>=3.0?). On the Viz nodes, you'll want to use GCC.

First, load GCC 4.8

 soft add +gcc-4.8.1

Then compile your code using appropriate flags. If you're using CMake, you'll want to set the CMAKE_C_FLAGS, CMAKE_CXX_FLAGS and CMAKE_Fortran_FLAGS variables (these are "advanced" - you'll need to press the "t" key in ccmake to see them)

 gcc -fsanitize=address -O0 -fno-omit-frame-pointer -g test.c

Next run your program normally. If it runs to completion, congratulations, it's likely you don't have any bad accesses. Otherwise, your program will crash and print a couple pages of information. You can use this output to find the problem (the "addr2line" command may be helpful). An easier way is to use gdb:

In serial:

 gdb my_program
 break __asan_report_error
 run

At which point, your program should stop and return you to a gdb prompt right after the bad access occurs. you can use the

 bt

command to see where the code stopped and debug as you normally would with gdb.

In parallel, the procedure is basically the same, but you'll need to use multiple copies of gdb (or a parallel debugger if you're using a system on which one is available). See the Debugging page for more information on using gdb in parallel.