[compiler-rt] r184303 - [lsan] Move symbolization and reporting out of StopTheWorld callback.

Sergey Matveev earthdok at google.com
Wed Jun 19 07:04:12 PDT 2013


Author: smatveev
Date: Wed Jun 19 09:04:11 2013
New Revision: 184303

URL: http://llvm.org/viewvc/llvm-project?rev=184303&view=rev
Log:
[lsan] Move symbolization and reporting out of StopTheWorld callback.

Modified:
    compiler-rt/trunk/lib/lsan/lit_tests/TestCases/large_allocation_leak.cc
    compiler-rt/trunk/lib/lsan/lit_tests/TestCases/stale_stack_leak.cc
    compiler-rt/trunk/lib/lsan/lit_tests/TestCases/use_globals_initialized.cc
    compiler-rt/trunk/lib/lsan/lit_tests/TestCases/use_globals_uninitialized.cc
    compiler-rt/trunk/lib/lsan/lit_tests/TestCases/use_registers.cc
    compiler-rt/trunk/lib/lsan/lit_tests/TestCases/use_stacks.cc
    compiler-rt/trunk/lib/lsan/lit_tests/TestCases/use_stacks_threaded.cc
    compiler-rt/trunk/lib/lsan/lit_tests/TestCases/use_tls_dynamic.cc
    compiler-rt/trunk/lib/lsan/lit_tests/TestCases/use_tls_pthread_specific_dynamic.cc
    compiler-rt/trunk/lib/lsan/lit_tests/TestCases/use_tls_pthread_specific_static.cc
    compiler-rt/trunk/lib/lsan/lit_tests/TestCases/use_tls_static.cc
    compiler-rt/trunk/lib/lsan/lit_tests/TestCases/use_unaligned.cc
    compiler-rt/trunk/lib/lsan/lsan_common.cc
    compiler-rt/trunk/lib/sanitizer_common/sanitizer_stoptheworld_linux_libcdep.cc

Modified: compiler-rt/trunk/lib/lsan/lit_tests/TestCases/large_allocation_leak.cc
URL: http://llvm.org/viewvc/llvm-project/compiler-rt/trunk/lib/lsan/lit_tests/TestCases/large_allocation_leak.cc?rev=184303&r1=184302&r2=184303&view=diff
==============================================================================
--- compiler-rt/trunk/lib/lsan/lit_tests/TestCases/large_allocation_leak.cc (original)
+++ compiler-rt/trunk/lib/lsan/lit_tests/TestCases/large_allocation_leak.cc Wed Jun 19 09:04:11 2013
@@ -13,6 +13,6 @@ int main() {
   return 0;
 }
 // CHECK: Test alloc: [[ADDR:.*]].
-// CHECK: LeakSanitizer: detected memory leaks
 // CHECK: Directly leaked 33554432 byte object at [[ADDR]]
+// CHECK: LeakSanitizer: detected memory leaks
 // CHECK: SUMMARY: LeakSanitizer:

Modified: compiler-rt/trunk/lib/lsan/lit_tests/TestCases/stale_stack_leak.cc
URL: http://llvm.org/viewvc/llvm-project/compiler-rt/trunk/lib/lsan/lit_tests/TestCases/stale_stack_leak.cc?rev=184303&r1=184302&r2=184303&view=diff
==============================================================================
--- compiler-rt/trunk/lib/lsan/lit_tests/TestCases/stale_stack_leak.cc (original)
+++ compiler-rt/trunk/lib/lsan/lit_tests/TestCases/stale_stack_leak.cc Wed Jun 19 09:04:11 2013
@@ -36,7 +36,7 @@ void ConfirmPointerHasSurvived() {
 }
 // CHECK: Test alloc: [[ADDR:.*]].
 // CHECK-sanity: Test alloc: [[ADDR:.*]].
-// CHECK: LeakSanitizer: detected memory leaks
 // CHECK: Directly leaked 1337 byte object at [[ADDR]]
+// CHECK: LeakSanitizer: detected memory leaks
 // CHECK: SUMMARY: LeakSanitizer:
 // CHECK-sanity: Value after LSan: [[ADDR]].

Modified: compiler-rt/trunk/lib/lsan/lit_tests/TestCases/use_globals_initialized.cc
URL: http://llvm.org/viewvc/llvm-project/compiler-rt/trunk/lib/lsan/lit_tests/TestCases/use_globals_initialized.cc?rev=184303&r1=184302&r2=184303&view=diff
==============================================================================
--- compiler-rt/trunk/lib/lsan/lit_tests/TestCases/use_globals_initialized.cc (original)
+++ compiler-rt/trunk/lib/lsan/lit_tests/TestCases/use_globals_initialized.cc Wed Jun 19 09:04:11 2013
@@ -16,6 +16,6 @@ int main() {
   return 0;
 }
 // CHECK: Test alloc: [[ADDR:.*]].
-// CHECK: LeakSanitizer: detected memory leaks
 // CHECK: Directly leaked 1337 byte object at [[ADDR]]
+// CHECK: LeakSanitizer: detected memory leaks
 // CHECK: SUMMARY: LeakSanitizer:

Modified: compiler-rt/trunk/lib/lsan/lit_tests/TestCases/use_globals_uninitialized.cc
URL: http://llvm.org/viewvc/llvm-project/compiler-rt/trunk/lib/lsan/lit_tests/TestCases/use_globals_uninitialized.cc?rev=184303&r1=184302&r2=184303&view=diff
==============================================================================
--- compiler-rt/trunk/lib/lsan/lit_tests/TestCases/use_globals_uninitialized.cc (original)
+++ compiler-rt/trunk/lib/lsan/lit_tests/TestCases/use_globals_uninitialized.cc Wed Jun 19 09:04:11 2013
@@ -16,6 +16,6 @@ int main() {
   return 0;
 }
 // CHECK: Test alloc: [[ADDR:.*]].
-// CHECK: LeakSanitizer: detected memory leaks
 // CHECK: Directly leaked 1337 byte object at [[ADDR]]
+// CHECK: LeakSanitizer: detected memory leaks
 // CHECK: SUMMARY: LeakSanitizer:

Modified: compiler-rt/trunk/lib/lsan/lit_tests/TestCases/use_registers.cc
URL: http://llvm.org/viewvc/llvm-project/compiler-rt/trunk/lib/lsan/lit_tests/TestCases/use_registers.cc?rev=184303&r1=184302&r2=184303&view=diff
==============================================================================
--- compiler-rt/trunk/lib/lsan/lit_tests/TestCases/use_registers.cc (original)
+++ compiler-rt/trunk/lib/lsan/lit_tests/TestCases/use_registers.cc Wed Jun 19 09:04:11 2013
@@ -46,6 +46,6 @@ int main() {
   return 0;
 }
 // CHECK: Test alloc: [[ADDR:.*]].
-// CHECK: LeakSanitizer: detected memory leaks
 // CHECK: Directly leaked 1337 byte object at [[ADDR]]
+// CHECK: LeakSanitizer: detected memory leaks
 // CHECK: SUMMARY: LeakSanitizer:

Modified: compiler-rt/trunk/lib/lsan/lit_tests/TestCases/use_stacks.cc
URL: http://llvm.org/viewvc/llvm-project/compiler-rt/trunk/lib/lsan/lit_tests/TestCases/use_stacks.cc?rev=184303&r1=184302&r2=184303&view=diff
==============================================================================
--- compiler-rt/trunk/lib/lsan/lit_tests/TestCases/use_stacks.cc (original)
+++ compiler-rt/trunk/lib/lsan/lit_tests/TestCases/use_stacks.cc Wed Jun 19 09:04:11 2013
@@ -15,6 +15,6 @@ int main() {
   exit(0);
 }
 // CHECK: Test alloc: [[ADDR:.*]].
-// CHECK: LeakSanitizer: detected memory leaks
 // CHECK: Directly leaked 1337 byte object at [[ADDR]]
+// CHECK: LeakSanitizer: detected memory leaks
 // CHECK: SUMMARY: LeakSanitizer:

Modified: compiler-rt/trunk/lib/lsan/lit_tests/TestCases/use_stacks_threaded.cc
URL: http://llvm.org/viewvc/llvm-project/compiler-rt/trunk/lib/lsan/lit_tests/TestCases/use_stacks_threaded.cc?rev=184303&r1=184302&r2=184303&view=diff
==============================================================================
--- compiler-rt/trunk/lib/lsan/lit_tests/TestCases/use_stacks_threaded.cc (original)
+++ compiler-rt/trunk/lib/lsan/lit_tests/TestCases/use_stacks_threaded.cc Wed Jun 19 09:04:11 2013
@@ -31,6 +31,6 @@ int main() {
   return 0;
 }
 // CHECK: Test alloc: [[ADDR:.*]].
-// CHECK: LeakSanitizer: detected memory leaks
 // CHECK: Directly leaked 1337 byte object at [[ADDR]]
+// CHECK: LeakSanitizer: detected memory leaks
 // CHECK: SUMMARY: LeakSanitizer:

Modified: compiler-rt/trunk/lib/lsan/lit_tests/TestCases/use_tls_dynamic.cc
URL: http://llvm.org/viewvc/llvm-project/compiler-rt/trunk/lib/lsan/lit_tests/TestCases/use_tls_dynamic.cc?rev=184303&r1=184302&r2=184303&view=diff
==============================================================================
--- compiler-rt/trunk/lib/lsan/lit_tests/TestCases/use_tls_dynamic.cc (original)
+++ compiler-rt/trunk/lib/lsan/lit_tests/TestCases/use_tls_dynamic.cc Wed Jun 19 09:04:11 2013
@@ -28,6 +28,6 @@ int main(int argc, char *argv[]) {
   return 0;
 }
 // CHECK: Test alloc: [[ADDR:.*]].
-// CHECK: LeakSanitizer: detected memory leaks
 // CHECK: leaked 1337 byte object at [[ADDR]]
+// CHECK: LeakSanitizer: detected memory leaks
 // CHECK: SUMMARY: LeakSanitizer:

Modified: compiler-rt/trunk/lib/lsan/lit_tests/TestCases/use_tls_pthread_specific_dynamic.cc
URL: http://llvm.org/viewvc/llvm-project/compiler-rt/trunk/lib/lsan/lit_tests/TestCases/use_tls_pthread_specific_dynamic.cc?rev=184303&r1=184302&r2=184303&view=diff
==============================================================================
--- compiler-rt/trunk/lib/lsan/lit_tests/TestCases/use_tls_pthread_specific_dynamic.cc (original)
+++ compiler-rt/trunk/lib/lsan/lit_tests/TestCases/use_tls_pthread_specific_dynamic.cc Wed Jun 19 09:04:11 2013
@@ -32,6 +32,6 @@ int main() {
   return 0;
 }
 // CHECK: Test alloc: [[ADDR:.*]].
-// CHECK: LeakSanitizer: detected memory leaks
 // CHECK: leaked 1337 byte object at [[ADDR]]
+// CHECK: LeakSanitizer: detected memory leaks
 // CHECK: SUMMARY: LeakSanitizer:

Modified: compiler-rt/trunk/lib/lsan/lit_tests/TestCases/use_tls_pthread_specific_static.cc
URL: http://llvm.org/viewvc/llvm-project/compiler-rt/trunk/lib/lsan/lit_tests/TestCases/use_tls_pthread_specific_static.cc?rev=184303&r1=184302&r2=184303&view=diff
==============================================================================
--- compiler-rt/trunk/lib/lsan/lit_tests/TestCases/use_tls_pthread_specific_static.cc (original)
+++ compiler-rt/trunk/lib/lsan/lit_tests/TestCases/use_tls_pthread_specific_static.cc Wed Jun 19 09:04:11 2013
@@ -26,6 +26,6 @@ int main() {
   return 0;
 }
 // CHECK: Test alloc: [[ADDR:.*]].
-// CHECK: LeakSanitizer: detected memory leaks
 // CHECK: Directly leaked 1337 byte object at [[ADDR]]
+// CHECK: LeakSanitizer: detected memory leaks
 // CHECK: SUMMARY: LeakSanitizer:

Modified: compiler-rt/trunk/lib/lsan/lit_tests/TestCases/use_tls_static.cc
URL: http://llvm.org/viewvc/llvm-project/compiler-rt/trunk/lib/lsan/lit_tests/TestCases/use_tls_static.cc?rev=184303&r1=184302&r2=184303&view=diff
==============================================================================
--- compiler-rt/trunk/lib/lsan/lit_tests/TestCases/use_tls_static.cc (original)
+++ compiler-rt/trunk/lib/lsan/lit_tests/TestCases/use_tls_static.cc Wed Jun 19 09:04:11 2013
@@ -16,6 +16,6 @@ int main() {
   return 0;
 }
 // CHECK: Test alloc: [[ADDR:.*]].
-// CHECK: LeakSanitizer: detected memory leaks
 // CHECK: Directly leaked 1337 byte object at [[ADDR]]
+// CHECK: LeakSanitizer: detected memory leaks
 // CHECK: SUMMARY: LeakSanitizer:

Modified: compiler-rt/trunk/lib/lsan/lit_tests/TestCases/use_unaligned.cc
URL: http://llvm.org/viewvc/llvm-project/compiler-rt/trunk/lib/lsan/lit_tests/TestCases/use_unaligned.cc?rev=184303&r1=184302&r2=184303&view=diff
==============================================================================
--- compiler-rt/trunk/lib/lsan/lit_tests/TestCases/use_unaligned.cc (original)
+++ compiler-rt/trunk/lib/lsan/lit_tests/TestCases/use_unaligned.cc Wed Jun 19 09:04:11 2013
@@ -18,6 +18,6 @@ int main() {
   return 0;
 }
 // CHECK: Test alloc: [[ADDR:.*]].
-// CHECK: LeakSanitizer: detected memory leaks
 // CHECK: Directly leaked 1337 byte object at [[ADDR]]
+// CHECK: LeakSanitizer: detected memory leaks
 // CHECK: SUMMARY: LeakSanitizer:

Modified: compiler-rt/trunk/lib/lsan/lsan_common.cc
URL: http://llvm.org/viewvc/llvm-project/compiler-rt/trunk/lib/lsan/lsan_common.cc?rev=184303&r1=184302&r2=184303&view=diff
==============================================================================
--- compiler-rt/trunk/lib/lsan/lsan_common.cc (original)
+++ compiler-rt/trunk/lib/lsan/lsan_common.cc Wed Jun 19 09:04:11 2013
@@ -275,45 +275,34 @@ void PrintLeakedCb::operator()(void *p)
   LsanMetadata m(p);
   if (!m.allocated()) return;
   if (m.tag() == kDirectlyLeaked || m.tag() == kIndirectlyLeaked) {
-    Printf("%s leaked %llu byte object at %p\n",
+    Printf("%s leaked %llu byte object at %p.\n",
            m.tag() == kDirectlyLeaked ? "Directly" : "Indirectly",
            m.requested_size(), p);
   }
 }
 
 static void PrintLeaked() {
+  Printf("\n");
   Printf("Reporting individual objects:\n");
-  Printf("============================\n");
   ForEachChunk(PrintLeakedCb());
-  Printf("\n");
 }
 
-enum LeakCheckResult {
-  kFatalError,
-  kLeaksFound,
-  kNoLeaks
+struct DoLeakCheckParam {
+  bool success;
+  LeakReport leak_report;
 };
 
 static void DoLeakCheckCallback(const SuspendedThreadsList &suspended_threads,
                                 void *arg) {
-  LeakCheckResult *result = reinterpret_cast<LeakCheckResult *>(arg);
-  CHECK_EQ(*result, kFatalError);
+  DoLeakCheckParam *param = reinterpret_cast<DoLeakCheckParam *>(arg);
+  CHECK(param);
+  CHECK(!param->success);
+  CHECK(param->leak_report.IsEmpty());
   ClassifyAllChunks(suspended_threads);
-  LeakReport leak_report;
-  CollectLeaks(&leak_report);
-  if (leak_report.IsEmpty()) {
-    *result = kNoLeaks;
-    return;
-  }
-  Printf("\n");
-  Printf("=================================================================\n");
-  Report("ERROR: LeakSanitizer: detected memory leaks\n");
-  leak_report.PrintLargest(flags()->max_leaks);
-  if (flags()->report_objects)
+  CollectLeaks(&param->leak_report);
+  if (!param->leak_report.IsEmpty() && flags()->report_objects)
     PrintLeaked();
-  leak_report.PrintSummary();
-  Printf("\n");
-  *result = kLeaksFound;
+  param->success = true;
 }
 
 void DoLeakCheck() {
@@ -321,16 +310,25 @@ void DoLeakCheck() {
   static bool already_done;
   CHECK(!already_done);
   already_done = true;
-  LeakCheckResult result = kFatalError;
+
+  DoLeakCheckParam param;
+  param.success = false;
   LockThreadRegistry();
   LockAllocator();
-  StopTheWorld(DoLeakCheckCallback, &result);
+  StopTheWorld(DoLeakCheckCallback, &param);
   UnlockAllocator();
   UnlockThreadRegistry();
-  if (result == kFatalError) {
+
+  if (!param.success) {
     Report("LeakSanitizer has encountered a fatal error.\n");
     Die();
-  } else if (result == kLeaksFound) {
+  }
+  if (!param.leak_report.IsEmpty()) {
+    Printf("\n================================================================="
+           "\n");
+    Report("ERROR: LeakSanitizer: detected memory leaks\n");
+    param.leak_report.PrintLargest(flags()->max_leaks);
+    param.leak_report.PrintSummary();
     if (flags()->exitcode)
       internal__exit(flags()->exitcode);
   }
@@ -396,7 +394,7 @@ void LeakReport::PrintSummary() {
       bytes += leaks_[i].total_size;
       allocations += leaks_[i].hit_count;
   }
-  Printf("SUMMARY: LeakSanitizer: %llu byte(s) leaked in %llu allocation(s).\n",
+  Printf("SUMMARY: LeakSanitizer: %llu byte(s) leaked in %llu allocation(s).\n\n",
          bytes, allocations);
 }
 

Modified: compiler-rt/trunk/lib/sanitizer_common/sanitizer_stoptheworld_linux_libcdep.cc
URL: http://llvm.org/viewvc/llvm-project/compiler-rt/trunk/lib/sanitizer_common/sanitizer_stoptheworld_linux_libcdep.cc?rev=184303&r1=184302&r2=184303&view=diff
==============================================================================
--- compiler-rt/trunk/lib/sanitizer_common/sanitizer_stoptheworld_linux_libcdep.cc (original)
+++ compiler-rt/trunk/lib/sanitizer_common/sanitizer_stoptheworld_linux_libcdep.cc Wed Jun 19 09:04:11 2013
@@ -278,11 +278,20 @@ class ScopedStackSpaceWithGuard {
   uptr guard_start_;
 };
 
+static void WipeStack() {
+  char arr[256];
+  internal_memset(arr, 0, sizeof(arr));
+}
+
 static sigset_t blocked_sigset;
 static sigset_t old_sigset;
 static struct sigaction old_sigactions[ARRAY_SIZE(kUnblockedSignals)];
 
 void StopTheWorld(StopTheWorldCallback callback, void *argument) {
+  // Glibc's sigaction() has a side-effect where it copies garbage stack values
+  // into oldact, which can cause false negatives in LSan. As a quick workaround
+  // we zero some stack space here.
+  WipeStack();
   // Block all signals that can be blocked safely, and install default handlers
   // for the remaining signals.
   // We cannot allow user-defined handlers to run while the ThreadSuspender





More information about the llvm-commits mailing list