From 1079cb91e8e1ea4202a5aa990859fc49fc2338e1 Mon Sep 17 00:00:00 2001 From: Kostya Serebryany Date: Tue, 22 Apr 2014 13:56:56 +0000 Subject: [PATCH] [asan] enable LeakSanitizer (LSan) by default in asan. This only affects Linux x86_64. LSan has been used in various projects for more than half a year and we now consider it quite stable to be on by default. git-svn-id: https://llvm.org/svn/llvm-project/compiler-rt/trunk@206896 91177308-0d34-0410-b5e6-96231b3b80d8 --- lib/asan/asan_rtl.cc | 5 ++--- lib/sanitizer_common/sanitizer_flags.cc | 2 +- test/asan/TestCases/Linux/leak.cc | 16 ++++++++++++++++ test/asan/TestCases/allocator_returns_null.cc | 2 ++ test/ubsan/TestCases/TypeCheck/vptr.cpp | 3 ++- 5 files changed, 23 insertions(+), 5 deletions(-) create mode 100644 test/asan/TestCases/Linux/leak.cc diff --git a/lib/asan/asan_rtl.cc b/lib/asan/asan_rtl.cc index 4b11f2a51..b9e375b88 100644 --- a/lib/asan/asan_rtl.cc +++ b/lib/asan/asan_rtl.cc @@ -230,6 +230,7 @@ static void ParseFlagsFromString(Flags *f, const char *str) { void InitializeFlags(Flags *f, const char *env) { CommonFlags *cf = common_flags(); SetCommonFlagsDefaults(cf); + cf->detect_leaks = CAN_SANITIZE_LEAKS; cf->external_symbolizer_path = GetEnv("ASAN_SYMBOLIZER_PATH"); cf->malloc_context_size = kDefaultMallocContextSize; cf->intercept_tls_get_addr = true; @@ -288,13 +289,11 @@ void InitializeFlags(Flags *f, const char *env) { PrintFlagDescriptions(); } -#if !CAN_SANITIZE_LEAKS - if (cf->detect_leaks) { + if (!CAN_SANITIZE_LEAKS && cf->detect_leaks) { Report("%s: detect_leaks is not supported on this platform.\n", SanitizerToolName); cf->detect_leaks = false; } -#endif // Make "strict_init_order" imply "check_initialization_order". // TODO(samsonov): Use a single runtime flag for an init-order checker. diff --git a/lib/sanitizer_common/sanitizer_flags.cc b/lib/sanitizer_common/sanitizer_flags.cc index e3e9aedce..b722b4931 100644 --- a/lib/sanitizer_common/sanitizer_flags.cc +++ b/lib/sanitizer_common/sanitizer_flags.cc @@ -40,7 +40,7 @@ void SetCommonFlagsDefaults(CommonFlags *f) { f->malloc_context_size = 1; f->log_path = "stderr"; f->verbosity = 0; - f->detect_leaks = false; + f->detect_leaks = true; f->leak_check_at_exit = true; f->allocator_may_return_null = false; f->print_summary = true; diff --git a/test/asan/TestCases/Linux/leak.cc b/test/asan/TestCases/Linux/leak.cc new file mode 100644 index 000000000..42ee84b9c --- /dev/null +++ b/test/asan/TestCases/Linux/leak.cc @@ -0,0 +1,16 @@ +// Minimal test for LeakSanitizer+AddressSanitizer. +// REQUIRES: asan-64-bits +// +// RUN: %clangxx_asan %s -o %t +// RUN: ASAN_OPTIONS=detect_leaks=1 not %t 2>&1 | FileCheck %s +// RUN: not %t 2>&1 | FileCheck %s +// RUN: ASAN_OPTIONS=detect_leaks=0 %t +#include +int *t; + +int main(int argc, char **argv) { + t = new int[argc - 1]; + printf("t: %p\n", t); + t = 0; +} +// CHECK: LeakSanitizer: detected memory leaks diff --git a/test/asan/TestCases/allocator_returns_null.cc b/test/asan/TestCases/allocator_returns_null.cc index 595c9e252..fe98eaea9 100644 --- a/test/asan/TestCases/allocator_returns_null.cc +++ b/test/asan/TestCases/allocator_returns_null.cc @@ -52,10 +52,12 @@ int main(int argc, char **argv) { *t = 42; x = (char*)realloc(t, size); assert(*t == 42); + free(t); } // The NULL pointer is printed differently on different systems, while (long)0 // is always the same. fprintf(stderr, "x: %lx\n", (long)x); + free(x); return x != 0; } // CHECK-mCRASH: malloc: diff --git a/test/ubsan/TestCases/TypeCheck/vptr.cpp b/test/ubsan/TestCases/TypeCheck/vptr.cpp index 9095f7279..9d23b3fc8 100644 --- a/test/ubsan/TestCases/TypeCheck/vptr.cpp +++ b/test/ubsan/TestCases/TypeCheck/vptr.cpp @@ -31,6 +31,8 @@ struct T : S { struct U : S, T { virtual int v() { return 2; } }; +T *p = 0; // Make p global so that lsan does not complain. + int main(int, char **argv) { T t; (void)t.a; @@ -49,7 +51,6 @@ int main(int, char **argv) { (void)u.T::v(); (void)((T&)u).S::v(); - T *p = 0; char Buffer[sizeof(U)] = {}; switch (argv[1][1]) { case '0': -- 2.11.4.GIT