[asan] add a flag poison_heap to allow better allocator benchmarking, implemenet malloc_stats() on Linux

git-svn-id: https://llvm.org/svn/llvm-project/compiler-rt/trunk@170685 91177308-0d34-0410-b5e6-96231b3b80d8
diff --git a/lib/asan/asan_allocator2.cc b/lib/asan/asan_allocator2.cc
index 4527e3b..1dff110 100644
--- a/lib/asan/asan_allocator2.cc
+++ b/lib/asan/asan_allocator2.cc
@@ -307,7 +307,7 @@
   if (size_rounded_down_to_granularity)
     PoisonShadow(user_beg, size_rounded_down_to_granularity, 0);
   // Deal with the end of the region if size is not aligned to granularity.
-  if (size != size_rounded_down_to_granularity) {
+  if (size != size_rounded_down_to_granularity && flags()->poison_heap) {
     u8 *shadow = (u8*)MemToShadow(user_beg + size_rounded_down_to_granularity);
     *shadow = size & (SHADOW_GRANULARITY - 1);
   }
diff --git a/lib/asan/asan_flags.h b/lib/asan/asan_flags.h
index ded2b24..8306170 100644
--- a/lib/asan/asan_flags.h
+++ b/lib/asan/asan_flags.h
@@ -99,6 +99,9 @@
   bool fast_unwind_on_fatal;
   // Use fast (frame-pointer-based) unwinder on malloc/free (if available).
   bool fast_unwind_on_malloc;
+  // Poison (or not) the heap memory on [de]allocation. Zero value is useful
+  // for benchmarking the allocator or instrumentator.
+  bool poison_heap;
 };
 
 Flags *flags();
diff --git a/lib/asan/asan_malloc_linux.cc b/lib/asan/asan_malloc_linux.cc
index 1bf9051..df94513 100644
--- a/lib/asan/asan_malloc_linux.cc
+++ b/lib/asan/asan_malloc_linux.cc
@@ -19,6 +19,7 @@
 #include "asan_interceptors.h"
 #include "asan_internal.h"
 #include "asan_stack.h"
+#include "asan_thread_registry.h"
 
 #if ASAN_ANDROID
 DECLARE_REAL_AND_INTERCEPTOR(void*, malloc, uptr size)
@@ -141,4 +142,10 @@
   return asan_pvalloc(size, &stack);
 }
 
+INTERCEPTOR(void, malloc_stats, void) {
+  Printf("AddressSanitizer malloc_stats()\n");
+  Printf("  total mmapped: %zdM\n",
+         asanThreadRegistry().GetHeapSize() >> 20);
+}
+
 #endif  // __linux__
diff --git a/lib/asan/asan_poisoning.cc b/lib/asan/asan_poisoning.cc
index 3da9671..11aa87a 100644
--- a/lib/asan/asan_poisoning.cc
+++ b/lib/asan/asan_poisoning.cc
@@ -20,6 +20,7 @@
 namespace __asan {
 
 void PoisonShadow(uptr addr, uptr size, u8 value) {
+  if (!flags()->poison_heap) return;
   CHECK(AddrIsAlignedByGranularity(addr));
   CHECK(AddrIsAlignedByGranularity(addr + size));
   uptr shadow_beg = MemToShadow(addr);
@@ -32,6 +33,7 @@
                                      uptr size,
                                      uptr redzone_size,
                                      u8 value) {
+  if (!flags()->poison_heap) return;
   CHECK(AddrIsAlignedByGranularity(addr));
   u8 *shadow = (u8*)MemToShadow(addr);
   for (uptr i = 0; i < redzone_size;
diff --git a/lib/asan/asan_rtl.cc b/lib/asan/asan_rtl.cc
index 94b17c8..b4f0d1a 100644
--- a/lib/asan/asan_rtl.cc
+++ b/lib/asan/asan_rtl.cc
@@ -106,6 +106,7 @@
   ParseFlag(str, &f->log_path, "log_path");
   ParseFlag(str, &f->fast_unwind_on_fatal, "fast_unwind_on_fatal");
   ParseFlag(str, &f->fast_unwind_on_malloc, "fast_unwind_on_malloc");
+  ParseFlag(str, &f->poison_heap, "poison_heap");
 }
 
 void InitializeFlags(Flags *f, const char *env) {
@@ -141,6 +142,7 @@
   f->log_path = 0;
   f->fast_unwind_on_fatal = true;
   f->fast_unwind_on_malloc = true;
+  f->poison_heap = true;
 
   // Override from user-specified string.
   ParseFlagsFromString(f, MaybeCallAsanDefaultOptions());