[lsan] Make __lsan_do_leak_check() honor the detect_leaks flag.

Also move detect_leaks to common flags.

git-svn-id: https://llvm.org/svn/llvm-project/compiler-rt/trunk@186821 91177308-0d34-0410-b5e6-96231b3b80d8
diff --git a/lib/asan/asan_flags.h b/lib/asan/asan_flags.h
index d0f821a..b83a147 100644
--- a/lib/asan/asan_flags.h
+++ b/lib/asan/asan_flags.h
@@ -106,8 +106,6 @@
   // If true, assume that dynamic initializers can never access globals from
   // other modules, even if the latter are already initialized.
   bool strict_init_order;
-  // Invoke LeakSanitizer at process exit.
-  bool detect_leaks;
 };
 
 extern Flags asan_flags_dont_use_directly;
diff --git a/lib/asan/asan_rtl.cc b/lib/asan/asan_rtl.cc
index 19ed2b7..1315c2f 100644
--- a/lib/asan/asan_rtl.cc
+++ b/lib/asan/asan_rtl.cc
@@ -124,7 +124,6 @@
   ParseFlag(str, &f->use_stack_depot, "use_stack_depot");
   ParseFlag(str, &f->strict_memcmp, "strict_memcmp");
   ParseFlag(str, &f->strict_init_order, "strict_init_order");
-  ParseFlag(str, &f->detect_leaks, "detect_leaks");
 }
 
 void InitializeFlags(Flags *f, const char *env) {
@@ -137,6 +136,7 @@
   cf->strip_path_prefix = "";
   cf->handle_ioctl = false;
   cf->log_path = 0;
+  cf->detect_leaks = false;
 
   internal_memset(f, 0, sizeof(*f));
   f->quarantine_size = (ASAN_LOW_MEMORY) ? 1UL << 26 : 1UL << 28;
@@ -173,7 +173,6 @@
   f->use_stack_depot = true;
   f->strict_memcmp = true;
   f->strict_init_order = false;
-  f->detect_leaks = false;
 
   // Override from compile definition.
   ParseFlagsFromString(f, MaybeUseAsanDefaultOptionsCompileDefiniton());
@@ -189,17 +188,17 @@
   ParseFlagsFromString(f, env);
 
 #if !CAN_SANITIZE_LEAKS
-  if (f->detect_leaks) {
+  if (cf->detect_leaks) {
     Report("%s: detect_leaks is not supported on this platform.\n",
            SanitizerToolName);
-    f->detect_leaks = false;
+    cf->detect_leaks = false;
   }
 #endif
 
-  if (f->detect_leaks && !f->use_stack_depot) {
+  if (cf->detect_leaks && !f->use_stack_depot) {
     Report("%s: detect_leaks is ignored (requires use_stack_depot).\n",
            SanitizerToolName);
-    f->detect_leaks = false;
+    cf->detect_leaks = false;
   }
 }
 
@@ -557,7 +556,7 @@
 
 #if CAN_SANITIZE_LEAKS
   __lsan::InitCommonLsan();
-  if (flags()->detect_leaks) {
+  if (common_flags()->detect_leaks) {
     Atexit(__lsan::DoLeakCheck);
   }
 #endif  // CAN_SANITIZE_LEAKS
diff --git a/lib/lsan/lsan.cc b/lib/lsan/lsan.cc
index c917bcc..79e593f 100644
--- a/lib/lsan/lsan.cc
+++ b/lib/lsan/lsan.cc
@@ -30,6 +30,7 @@
   cf->strip_path_prefix = "";
   cf->fast_unwind_on_malloc = true;
   cf->malloc_context_size = 30;
+  cf->detect_leaks = true;
 
   ParseCommonFlagsFromString(GetEnv("LSAN_OPTIONS"));
 }
diff --git a/lib/lsan/lsan_common.cc b/lib/lsan/lsan_common.cc
index 8f57ae0..265b1ce 100644
--- a/lib/lsan/lsan_common.cc
+++ b/lib/lsan/lsan_common.cc
@@ -550,8 +550,9 @@
 SANITIZER_INTERFACE_ATTRIBUTE
 void __lsan_do_leak_check() {
 #if CAN_SANITIZE_LEAKS
-  __lsan::DoLeakCheck();
-#endif
+  if (common_flags()->detect_leaks)
+    __lsan::DoLeakCheck();
+#endif  // CAN_SANITIZE_LEAKS
 }
 
 #if !SANITIZER_SUPPORTS_WEAK_HOOKS
diff --git a/lib/sanitizer_common/sanitizer_flags.cc b/lib/sanitizer_common/sanitizer_flags.cc
index b8b1c3b..ed7336a 100644
--- a/lib/sanitizer_common/sanitizer_flags.cc
+++ b/lib/sanitizer_common/sanitizer_flags.cc
@@ -29,6 +29,7 @@
   ParseFlag(str, &f->symbolize, "symbolize");
   ParseFlag(str, &f->handle_ioctl, "handle_ioctl");
   ParseFlag(str, &f->log_path, "log_path");
+  ParseFlag(str, &f->detect_leaks, "detect_leaks");
 }
 
 static bool GetFlagValue(const char *env, const char *name,
diff --git a/lib/sanitizer_common/sanitizer_flags.h b/lib/sanitizer_common/sanitizer_flags.h
index 16cbc64..f3f4a26 100644
--- a/lib/sanitizer_common/sanitizer_flags.h
+++ b/lib/sanitizer_common/sanitizer_flags.h
@@ -39,6 +39,8 @@
   int malloc_context_size;
   // Write logs to "log_path.pid" instead of stderr.
   const char *log_path;
+  // Enable memory leak detection.
+  bool detect_leaks;
 };
 
 extern CommonFlags common_flags_dont_use_directly;