Tools/Debugging/ASAN Address Sanitizer

= Address Sanitizer =

GCC and Clang support address sanitizer, which will use of uninitialized memory, accessing freed memory, accessing outside memory bounds, and so on. It also supports leak checking when Blender exits.

Enable with the WITH_COMPILER_ASAN option in the CMake configuration. It is also enabled when using make developer to enable development options.

Debugging
When the output is not enough to go by, run inside a debugger and break on the error reporting function

break __asan_on_error@plt

Example stack trace:

Quiet LeakSanitizer
While this tool is really useful to detect memory leaks, it has some annoying drawback in Blender currently - usage of Python leads to a fair amount of (valid!) leak reports, about which we cannot do much currently (see also Python documentation).

To silence those warnings (which can “hide” a real new one in the flow), you can use a suppression file. For that you can set an environment variable like this, possible in .bashrc or similar to make it permanent.

The contents of blender-lsan-suppressions.txt can be as follows:

Most warnings will come from Python and graphics drivers, the common ones are included here. More can be added as needed, using library and function names, including wildcards.

Note that this will remove any leak reports from code which backtrace contains a reference to libpython - this may be too generic, in which case just filtering out leaks coming from our bpy init code can be an option:

leak:BPY_python_start

Comparison with Valgrind
Address sanitizer can help find bugs that Valgrind would, but there are some important differences.

Pros
 * Fast enough for general use.
 * Easy to use a breakpoint to investigate the point when and error happens. (Valgrind can use GDB too but its a bit more involved).

Cons
 * Only deals with heap memory (not stack as valgrind).
 * Can't be used with valgrind (to use valgrind you'll have to remove the compiler flags and rebuild)