[PATCH] [lsan] Added __lsan_will_detect_leaks().

Sergey Matveev earthdok at google.com
Fri Aug 16 04:48:20 PDT 2013


Hi kcc,

A new interface function for those rare cases when user code needs to know whether the leak detection phase is going to happen.

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

Files:
  include/sanitizer/lsan_interface.h
  lib/lsan/lit_tests/TestCases/will_detect_leaks.cc
  lib/lsan/lsan_common.cc

Index: include/sanitizer/lsan_interface.h
===================================================================
--- include/sanitizer/lsan_interface.h
+++ include/sanitizer/lsan_interface.h
@@ -25,18 +25,22 @@
   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 (if the return value is non-zero). This
-  // function must be defined as returning a constant value; any behavior beyond
-  // that is unsupported.
-  int __lsan_is_turned_off();
+  // Returns a non-zero value iff leak detection is enabled for this process
+  // (i.e. the detect_leaks flag is set and __lsan_is_turned_off() returns 0).
+  int __lsan_will_detect_leaks();
   // Calling this function makes LSan enter the leak checking phase immediately.
   // Use this if normal end-of-process leak checking happens too late (e.g. if
   // you have intentional memory leaks in your shutdown code). Calling this
   // function overrides end-of-process leak checking; it must be called at
   // most once per process. This function will terminate the process if there
   // are memory leaks and the exit_code flag is non-zero.
   void __lsan_do_leak_check();
+
+  // The user may optionally provide this function to disallow leak checking
+  // for the program it is linked into (if the return value is non-zero). This
+  // function must be defined as returning a constant value; any behavior beyond
+  // that is unsupported.
+  int __lsan_is_turned_off();
 #ifdef __cplusplus
 }  // extern "C"
 
Index: lib/lsan/lit_tests/TestCases/will_detect_leaks.cc
===================================================================
--- /dev/null
+++ lib/lsan/lit_tests/TestCases/will_detect_leaks.cc
@@ -0,0 +1,32 @@
+// Test for disabling LSan at link-time.
+// RUN: %clangxx_lsan %s -o %t
+// RUN: LSAN_OPTIONS="detect_leaks=0" ASAN_OPTIONS="detect_leaks=0" %t | FileCheck %s --check-prefix=CHECK-no
+// RUN: LSAN_OPTIONS="detect_leaks=0" ASAN_OPTIONS="detect_leaks=0" %t foo | FileCheck %s --check-prefix=CHECK-no
+// RUN: %t | FileCheck %s --check-prefix=CHECK-no
+// RUN: %t foo | FileCheck %s --check-prefix=CHECK-yes
+
+#include <stdio.h>
+#include <stdlib.h>
+#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[]) {
+  // Make sure standalone LSan is inited.
+  // FIXME: remove this once early initialization for standalone LSan is in
+  void *x = malloc(1);
+  free(x);
+
+  argc_copy = argc;
+  printf("%s\n", __lsan_will_detect_leaks() ? "YEAH" : "HELL NO");
+  return 0;
+}
+
+// CHECK-yes: YEAH
+// CHECK-no: HELL NO
Index: lib/lsan/lsan_common.cc
===================================================================
--- lib/lsan/lsan_common.cc
+++ lib/lsan/lsan_common.cc
@@ -555,6 +555,13 @@
 #endif  // CAN_SANITIZE_LEAKS
 }
 
+int __lsan_will_detect_leaks() {
+#if CAN_SANITIZE_LEAKS
+  return !__lsan_is_turned_off() && common_flags()->detect_leaks;
+#endif
+  return 0;
+}
+
 #if !SANITIZER_SUPPORTS_WEAK_HOOKS
 SANITIZER_WEAK_ATTRIBUTE SANITIZER_INTERFACE_ATTRIBUTE
 int __lsan_is_turned_off() {
-------------- next part --------------
A non-text attachment was scrubbed...
Name: D1421.1.patch
Type: text/x-patch
Size: 3294 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20130816/ea51673d/attachment.bin>


More information about the llvm-commits mailing list