[compiler-rt] r187578 - [lsan] Add leak_check_at_exit flag.

Sergey Matveev earthdok at google.com
Thu Aug 1 07:57:07 PDT 2013


Author: smatveev
Date: Thu Aug  1 09:57:07 2013
New Revision: 187578

URL: http://llvm.org/viewvc/llvm-project?rev=187578&view=rev
Log:
[lsan] Add leak_check_at_exit flag.

We needed a way to tell LSan to invoke leak checking only if __do_leak_check()
is called explicitly. This can now be achieved by setting
leak_check_at_exit=false.

Added:
    compiler-rt/trunk/lib/lsan/lit_tests/TestCases/leak_check_at_exit.cc
Modified:
    compiler-rt/trunk/lib/asan/asan_rtl.cc
    compiler-rt/trunk/lib/lsan/lsan.cc
    compiler-rt/trunk/lib/sanitizer_common/sanitizer_flags.cc
    compiler-rt/trunk/lib/sanitizer_common/sanitizer_flags.h

Modified: compiler-rt/trunk/lib/asan/asan_rtl.cc
URL: http://llvm.org/viewvc/llvm-project/compiler-rt/trunk/lib/asan/asan_rtl.cc?rev=187578&r1=187577&r2=187578&view=diff
==============================================================================
--- compiler-rt/trunk/lib/asan/asan_rtl.cc (original)
+++ compiler-rt/trunk/lib/asan/asan_rtl.cc Thu Aug  1 09:57:07 2013
@@ -137,6 +137,7 @@ void InitializeFlags(Flags *f, const cha
   cf->handle_ioctl = false;
   cf->log_path = 0;
   cf->detect_leaks = false;
+  cf->leak_check_at_exit = true;
 
   internal_memset(f, 0, sizeof(*f));
   f->quarantine_size = (ASAN_LOW_MEMORY) ? 1UL << 26 : 1UL << 28;
@@ -556,7 +557,7 @@ void __asan_init() {
 
 #if CAN_SANITIZE_LEAKS
   __lsan::InitCommonLsan();
-  if (common_flags()->detect_leaks) {
+  if (common_flags()->detect_leaks && common_flags()->leak_check_at_exit) {
     Atexit(__lsan::DoLeakCheck);
   }
 #endif  // CAN_SANITIZE_LEAKS

Added: compiler-rt/trunk/lib/lsan/lit_tests/TestCases/leak_check_at_exit.cc
URL: http://llvm.org/viewvc/llvm-project/compiler-rt/trunk/lib/lsan/lit_tests/TestCases/leak_check_at_exit.cc?rev=187578&view=auto
==============================================================================
--- compiler-rt/trunk/lib/lsan/lit_tests/TestCases/leak_check_at_exit.cc (added)
+++ compiler-rt/trunk/lib/lsan/lit_tests/TestCases/leak_check_at_exit.cc Thu Aug  1 09:57:07 2013
@@ -0,0 +1,19 @@
+// Test for the leak_check_at_exit flag.
+// RUN: %clangxx_lsan %s -o %t
+// RUN: LSAN_OPTIONS="verbosity=1" %t foo 2>&1 | FileCheck %s --check-prefix=CHECK-do
+// RUN: LSAN_OPTIONS="verbosity=1" %t 2>&1 | FileCheck %s --check-prefix=CHECK-do
+// RUN: LSAN_OPTIONS="verbosity=1:leak_check_at_exit=0" ASAN_OPTIONS="$ASAN_OPTIONS:leak_check_at_exit=0" %t foo 2>&1 | FileCheck %s --check-prefix=CHECK-do
+// RUN: LSAN_OPTIONS="verbosity=1:leak_check_at_exit=0" ASAN_OPTIONS="$ASAN_OPTIONS:leak_check_at_exit=0" %t 2>&1 | FileCheck %s --check-prefix=CHECK-dont
+
+#include <stdio.h>
+#include <sanitizer/lsan_interface.h>
+
+int main(int argc, char *argv[]) {
+  printf("printf to break optimization\n");
+  if (argc > 1)
+    __lsan_do_leak_check();
+  return 0;
+}
+
+// CHECK-do: SUMMARY: LeakSanitizer:
+// CHECK-dont-NOT: SUMMARY: LeakSanitizer:

Modified: compiler-rt/trunk/lib/lsan/lsan.cc
URL: http://llvm.org/viewvc/llvm-project/compiler-rt/trunk/lib/lsan/lsan.cc?rev=187578&r1=187577&r2=187578&view=diff
==============================================================================
--- compiler-rt/trunk/lib/lsan/lsan.cc (original)
+++ compiler-rt/trunk/lib/lsan/lsan.cc Thu Aug  1 09:57:07 2013
@@ -31,6 +31,7 @@ static void InitializeCommonFlags() {
   cf->fast_unwind_on_malloc = true;
   cf->malloc_context_size = 30;
   cf->detect_leaks = true;
+  cf->leak_check_at_exit = true;
 
   ParseCommonFlagsFromString(GetEnv("LSAN_OPTIONS"));
 }
@@ -59,7 +60,8 @@ void Init() {
   }
 
   InitCommonLsan();
-  Atexit(DoLeakCheck);
+  if (common_flags()->detect_leaks && common_flags()->leak_check_at_exit)
+    Atexit(DoLeakCheck);
 }
 
 }  // namespace __lsan

Modified: compiler-rt/trunk/lib/sanitizer_common/sanitizer_flags.cc
URL: http://llvm.org/viewvc/llvm-project/compiler-rt/trunk/lib/sanitizer_common/sanitizer_flags.cc?rev=187578&r1=187577&r2=187578&view=diff
==============================================================================
--- compiler-rt/trunk/lib/sanitizer_common/sanitizer_flags.cc (original)
+++ compiler-rt/trunk/lib/sanitizer_common/sanitizer_flags.cc Thu Aug  1 09:57:07 2013
@@ -30,6 +30,7 @@ void ParseCommonFlagsFromString(const ch
   ParseFlag(str, &f->handle_ioctl, "handle_ioctl");
   ParseFlag(str, &f->log_path, "log_path");
   ParseFlag(str, &f->detect_leaks, "detect_leaks");
+  ParseFlag(str, &f->leak_check_at_exit, "leak_check_at_exit");
 }
 
 static bool GetFlagValue(const char *env, const char *name,

Modified: compiler-rt/trunk/lib/sanitizer_common/sanitizer_flags.h
URL: http://llvm.org/viewvc/llvm-project/compiler-rt/trunk/lib/sanitizer_common/sanitizer_flags.h?rev=187578&r1=187577&r2=187578&view=diff
==============================================================================
--- compiler-rt/trunk/lib/sanitizer_common/sanitizer_flags.h (original)
+++ compiler-rt/trunk/lib/sanitizer_common/sanitizer_flags.h Thu Aug  1 09:57:07 2013
@@ -41,6 +41,10 @@ struct CommonFlags {
   const char *log_path;
   // Enable memory leak detection.
   bool detect_leaks;
+  // Invoke leak checking in an atexit handler. Has no effect if
+  // detect_leaks=false, or if __lsan_do_leak_check() is called before the
+  // handler has a chance to run.
+  bool leak_check_at_exit;
 };
 
 extern CommonFlags common_flags_dont_use_directly;





More information about the llvm-commits mailing list