Make lsan complain loudly when running under ptrace
authorKostya Serebryany <kcc@google.com>
Thu, 13 Oct 2016 22:34:13 +0000 (22:34 +0000)
committerKostya Serebryany <kcc@google.com>
Thu, 13 Oct 2016 22:34:13 +0000 (22:34 +0000)
Summary:
LeakSanitizer does not work with ptrace but currently it
will print warnings (only under verbosity=1) and then proceed
to print tons of false reports.
This patch makes lsan fail hard under ptrace with a verbose message.

https://github.com/google/sanitizers/issues/728

Reviewers: eugenis, vitalybuka, aizatsky

Subscribers: kubabrecka, llvm-commits

Differential Revision: https://reviews.llvm.org/D25538

llvm-svn: 284171

compiler-rt/lib/lsan/lsan_common.cc
compiler-rt/lib/sanitizer_common/sanitizer_stoptheworld_linux_libcdep.cc
compiler-rt/test/lsan/TestCases/strace_test.cc [new file with mode: 0644]

index 888a25b..f1ab2e6 100644 (file)
@@ -449,6 +449,8 @@ static bool CheckForLeaks() {
     Report(
         "HINT: For debugging, try setting environment variable "
         "LSAN_OPTIONS=verbosity=1:log_threads=1\n");
+    Report(
+        "HINT: LeakSanitizer does not work under ptrace (strace, gdb, etc)\n");
     Die();
   }
   param.leak_report.ApplySuppressions();
index 1ce232b..eb4c403 100644 (file)
@@ -190,6 +190,7 @@ void ThreadSuspender::KillAllThreads() {
 bool ThreadSuspender::SuspendAllThreads() {
   ThreadLister thread_lister(pid_);
   bool added_threads;
+  bool first_iteration = true;
   do {
     // Run through the directory entries once.
     added_threads = false;
@@ -199,12 +200,13 @@ bool ThreadSuspender::SuspendAllThreads() {
         added_threads = true;
       tid = thread_lister.GetNextTID();
     }
-    if (thread_lister.error()) {
+    if (thread_lister.error() || (first_iteration && !added_threads)) {
       // Detach threads and fail.
       ResumeAllThreads();
       return false;
     }
     thread_lister.Reset();
+    first_iteration = false;
   } while (added_threads);
   return true;
 }
diff --git a/compiler-rt/test/lsan/TestCases/strace_test.cc b/compiler-rt/test/lsan/TestCases/strace_test.cc
new file mode 100644 (file)
index 0000000..b3568d0
--- /dev/null
@@ -0,0 +1,14 @@
+// Test that lsan reports a proper error when running under strace.
+// RUN: %clangxx_lsan %s -o %t
+// RUN: not strace -o /dev/null %run %t 2>&1 | FileCheck %s
+
+#include <stdio.h>
+#include <stdlib.h>
+
+static volatile void *sink;
+
+int main() {
+  sink = malloc(42);
+}
+// CHECK: LeakSanitizer has encountered a fatal error
+// CHECK: HINT: LeakSanitizer does not work under ptrace (strace, gdb, etc)