[PATCH] [lsan] Add exitcode flag. Kill the process if leaks are found.

Sergey Matveev earthdok at google.com
Fri May 24 06:05:01 PDT 2013


Hi kcc, glider,

http://llvm-reviews.chandlerc.com/D858

Files:
  lib/lsan/lsan_common.cc
  lib/lsan/lsan_common.h

Index: lib/lsan/lsan_common.cc
===================================================================
--- lib/lsan/lsan_common.cc
+++ lib/lsan/lsan_common.cc
@@ -32,6 +32,7 @@
   f->report_blocks = false;
   f->resolution = 0;
   f->max_leaks = 0;
+  f->exitcode = 23;
   f->log_pointers = false;
   f->log_threads = false;
 
@@ -47,6 +48,7 @@
     CHECK_GE(&f->max_leaks, 0);
     ParseFlag(options, &f->log_pointers, "log_pointers");
     ParseFlag(options, &f->log_threads, "log_threads");
+    ParseFlag(options, &f->exitcode, "exitcode");
   }
 }
 
@@ -269,28 +271,42 @@
   ForEachChunk(PrintLeakedCb());
 }
 
+enum LeakCheckResult {
+  kFatalError,
+  kLeaksFound,
+  kNoLeaks
+};
+
 static void DoLeakCheckCallback(const SuspendedThreadsList &suspended_threads,
                                 void *arg) {
+  LeakCheckResult *result = reinterpret_cast<LeakCheckResult *>(arg);
+  CHECK_EQ(*result, kFatalError);
   // Allocator must not be locked when we call GetRegionBegin().
   UnlockAllocator();
-  bool *success = reinterpret_cast<bool *>(arg);
   ClassifyAllChunks(suspended_threads);
   LeakReport leak_report;
   CollectLeaks(&leak_report);
-  if (!leak_report.IsEmpty()) {
-    leak_report.PrintLargest(flags()->max_leaks);
-    if (flags()->report_blocks)
-      PrintLeaked();
+  if (leak_report.IsEmpty()) {
+    *result = kNoLeaks;
+    return;
   }
+  leak_report.PrintLargest(flags()->max_leaks);
+  if (flags()->report_blocks)
+    PrintLeaked();
   ForEachChunk(ClearTagCb());
-  *success = true;
+  *result = kLeaksFound;
 }
 
 void DoLeakCheck() {
-  bool success = false;
-  LockAndSuspendThreads(DoLeakCheckCallback, &success);
-  if (!success)
-    Report("Leak check failed!\n");
+  LeakCheckResult result = kFatalError;
+  LockAndSuspendThreads(DoLeakCheckCallback, &result);
+  if (result == kFatalError) {
+    Report("LeakSanitizer has encountered a fatal error.\n");
+    Die();
+  } else if (result == kLeaksFound) {
+    if (flags()->exitcode)
+      internal__exit(flags()->exitcode);
+  }
 }
 
 ///// Reporting of leaked blocks' addresses (for testing). /////
Index: lib/lsan/lsan_common.h
===================================================================
--- lib/lsan/lsan_common.h
+++ lib/lsan/lsan_common.h
@@ -68,6 +68,8 @@
   int resolution;
   // The number of leaks reported.
   int max_leaks;
+  // If nonzero kill the process with this exit code upon finding leaks.
+  int exitcode;
 
   // Debug logging.
   bool log_pointers;
-------------- next part --------------
A non-text attachment was scrubbed...
Name: D858.1.patch
Type: text/x-patch
Size: 2477 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20130524/d25a7e10/attachment.bin>


More information about the llvm-commits mailing list