LeakSanitizer

LeakSanitizer (LSan) is a heap leak detector similar to HeapLeakChecker or to the leak checker in Valgrind/Memcheck. LSan can be run on top of AddressSanitizer with no additional slowdown compared to plain ASan.

LeakSanitizer on Chromium buildbots

LeakSanitizer is enabled on the ASan bots on chromium.memory (both Linux and ChromeOS). Memory leaks are treated as tree-closing bugs. You can use the linux_asan trybot to verify that your CL is clean.

Note that we don't detect leaks in browser_tests, content_browsertests and interactive_ui_tests just yet.

Ping earthdok@ if you encounter any issues.

My CL has benign (test-only, intentional etc) leaks, how do I suppress them?

You should fix the CL instead. The leaks may be harmless, but letting them accumulate over time has a negative impact on the stability of our bots. If you leak memory intentionally, use an in-code annotation instead (see base/debug/leak_annotations.h).
Ideally, you should only land suppressions for leaks in third-party code that you have no control over. If that is the case, take a look at tools/lsan/suppressions.txt. The syntax is as follows:

leak:<wildcard>

The wildcard will be matched against the function/module/source file name of each frame in the stack trace. Supported special characters are *, ^ (beginning of string) and $ (end of string).
Please be careful not to make the wildcard too generic. The TSan v2 page has some examples of good and bad suppressions.

Using LeakSanitizer

If you don't want to run LeakSanitizer locally, you can skip this section.

Building Chromium with ASan + LeakSanitizer

LeakSanitizer is supported on x86_64 Linux only. To build, follow the ASan build instructions and add lsan=1 to GYP_DEFINES. E.g.:

GYP_DEFINES='asan=1 lsan=1' gclient runhooks
ninja -C out/Release base_unittests

Note: Leak detection must not be enabled at this time, or LSan will find leaks during the build process and fail. Make sure ASAN_OPTIONS is not set when you build.

Using LeakSanitizer on top of ASan

At run-time, simply add detect_leaks=1 to ASAN_OPTIONS:

export ASAN_SYMBOLIZER_PATH=$CHROMIUM/third_party/llvm-build/Release+Asserts/bin/llvm-symbolizer
ASAN_OPTIONS="detect_leaks=1 symbolize=1" out/Release/base_unittests

You should see leak reports:

=================================================================
==18109==ERROR: LeakSanitizer: detected memory leaks

Direct leak of 1024 byte(s) in 1 object(s) allocated from:
    #0 0x430205 in _Znam _asan_rtl_
    #1 0xfc0edd in TestBody base/tools_sanity_unittest.cc:83
    #2 0x10eeef7 in Run testing/gtest/src/gtest.cc:2067
    #3 0x10f04e1 in Run testing/gtest/src/gtest.cc:2244
    #4 0x10f1357 in Run testing/gtest/src/gtest.cc:2351
    #5 0x10fe7b2 in RunAllTests testing/gtest/src/gtest.cc:4177
    #6 0x10fdcbc in impl testing/gtest/src/gtest.cc:2051
    #7 0x12b5d16 in Run base/test/test_suite.cc:167
    #8 0x128eaf6 in main base/test/run_all_unittests.cc:8
    #9 0x7f18a1dc576c in __libc_start_main /build/buildd/eglibc-2.15/csu/libc-start.c:226
    #10 0x444a6c in _start ??:0

SUMMARY: LeakSanitizer: 1024 byte(s) leaked in 1 allocation(s).

You should also point LSan to the Chromium-specific suppressions file, which resides under tools/lsan/suppressions.txt. Do as follows:

ASAN_OPTIONS="detect_leaks=1 symbolize=1" LSAN_OPTIONS="suppressions=tools/lsan/suppressions.txt" out/Release/base_unittests

Note that LeakSanitizer is incompatible with the sandbox. You must pass --no-sandbox if you want to run browser_tests or the full Chromium with LSan.

Using debug versions of shared libraries

Be aware that ASan's fast stack unwinder depends on frame pointers, which are often missing in release versions of shared libraries. If you want to use the fast unwinder (enabled by default), you should at least install a debug version of libstdc++. This worked for us on Ubuntu:

sudo apt-get install libstdc++6-4.6-dbg
LD_LIBRARY_PATH="/usr/lib/x86_64-linux-gnu/debug" ASAN_OPTIONS="detect_leaks=1 strict_memcmp=0" out/Release/base_unittests

If you still see incomplete stack traces, you can disable the fast unwinder by adding fast_unwind_on_malloc=0 to ASAN_OPTIONS

GLib

GLib may not play well with leak detection tools in the default mode. We recommend to pass G_SLICE=always-malloc, especially if you're trying to reproduce a GLib-related leak.

Using LeakSanitizer without ASan

LSan can also be used without ASan instrumentation. In GYP_GENERATORS above, omit asan=1 and specify only lsan=1. At runtime, use LSAN_SYMBOLIZER_PATH and LSAN_OPTIONS instead of ASAN_SYMBOLIZER_PATH and ASAN_OPTIONS, respectively. Disabling ASan instrumentation can bring a performance advantage, but be aware that this mode is not as well tested as the LSan+ASan mode.

In this mode leak detection is enabled by default. When building, you must explicitly set LSAN_OPTIONS="detect_leaks=0" to prevent build errors (see above).

Comments