[compiler-rt] r185066 - [LSan] Add the way to disable LSan at link time

Alexey Samsonov samsonov at google.com
Thu Jun 27 02:35:50 PDT 2013


Author: samsonov
Date: Thu Jun 27 04:35:50 2013
New Revision: 185066

URL: http://llvm.org/viewvc/llvm-project?rev=185066&view=rev
Log:
[LSan] Add the way to disable LSan at link time

Added:
    compiler-rt/trunk/lib/lsan/lit_tests/TestCases/link_turned_off.cc
Modified:
    compiler-rt/trunk/include/sanitizer/lsan_interface.h
    compiler-rt/trunk/lib/lsan/lsan_common.cc
    compiler-rt/trunk/lib/lsan/lsan_common.h

Modified: compiler-rt/trunk/include/sanitizer/lsan_interface.h
URL: http://llvm.org/viewvc/llvm-project/compiler-rt/trunk/include/sanitizer/lsan_interface.h?rev=185066&r1=185065&r2=185066&view=diff
==============================================================================
--- compiler-rt/trunk/include/sanitizer/lsan_interface.h (original)
+++ compiler-rt/trunk/include/sanitizer/lsan_interface.h Thu Jun 27 04:35:50 2013
@@ -25,6 +25,10 @@ extern "C" {
   void __lsan_enable();
   // The heap object into which p points will be treated as a non-leak.
   void __lsan_ignore_object(const void *p);
+  // The user may optionally provide this function to disallow leak checking
+  // for the program it is linked into. Note: this function may be called late,
+  // after all the global destructors.
+  int __lsan_is_turned_off();
 #ifdef __cplusplus
 }  // extern "C"
 

Added: compiler-rt/trunk/lib/lsan/lit_tests/TestCases/link_turned_off.cc
URL: http://llvm.org/viewvc/llvm-project/compiler-rt/trunk/lib/lsan/lit_tests/TestCases/link_turned_off.cc?rev=185066&view=auto
==============================================================================
--- compiler-rt/trunk/lib/lsan/lit_tests/TestCases/link_turned_off.cc (added)
+++ compiler-rt/trunk/lib/lsan/lit_tests/TestCases/link_turned_off.cc Thu Jun 27 04:35:50 2013
@@ -0,0 +1,23 @@
+// Test for disabling LSan at link-time.
+// RUN: %clangxx_lsan %s -o %t
+// RUN: %t
+// RUN: %t foo 2>&1 | FileCheck %s
+
+#include <sanitizer/lsan_interface.h>
+
+int argc_copy;
+
+extern "C" {
+int __lsan_is_turned_off() {
+  return (argc_copy == 1);
+}
+}
+
+int main(int argc, char *argv[]) {
+  volatile int *x = new int;
+  *x = 42;
+  argc_copy = argc;
+  return 0;
+}
+
+// CHECK: SUMMARY: LeakSanitizer: 4 byte(s) leaked in 1 allocation(s)

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=185066&r1=185065&r2=185066&view=diff
==============================================================================
--- compiler-rt/trunk/lib/lsan/lsan_common.cc (original)
+++ compiler-rt/trunk/lib/lsan/lsan_common.cc Thu Jun 27 04:35:50 2013
@@ -314,6 +314,8 @@ void DoLeakCheck() {
   static bool already_done;
   CHECK(!already_done);
   already_done = true;
+  if (&__lsan_is_turned_off && __lsan_is_turned_off())
+    return;
 
   DoLeakCheckParam param;
   param.success = false;
@@ -442,4 +444,11 @@ void __lsan_enable() {
   __lsan::disable_counter--;
 #endif
 }
+
+#if !SANITIZER_SUPPORTS_WEAK_HOOKS
+SANITIZER_WEAK_ATTRIBUTE SANITIZER_INTERFACE_ATTRIBUTE
+int __lsan_is_turned_off() {
+  return 0;
+}
+#endif
 }  // extern "C"

Modified: compiler-rt/trunk/lib/lsan/lsan_common.h
URL: http://llvm.org/viewvc/llvm-project/compiler-rt/trunk/lib/lsan/lsan_common.h?rev=185066&r1=185065&r2=185066&view=diff
==============================================================================
--- compiler-rt/trunk/lib/lsan/lsan_common.h (original)
+++ compiler-rt/trunk/lib/lsan/lsan_common.h Thu Jun 27 04:35:50 2013
@@ -154,4 +154,9 @@ class LsanMetadata {
 
 }  // namespace __lsan
 
+extern "C" {
+int __lsan_is_turned_off() SANITIZER_WEAK_ATTRIBUTE
+    SANITIZER_INTERFACE_ATTRIBUTE;
+}  // extern "C"
+
 #endif  // LSAN_COMMON_H





More information about the llvm-commits mailing list