[ASan] Move error reports away from ASan allocator. Add new source file to CMakeLists as well

git-svn-id: https://llvm.org/svn/llvm-project/compiler-rt/trunk@161569 91177308-0d34-0410-b5e6-96231b3b80d8
diff --git a/lib/asan/CMakeLists.txt b/lib/asan/CMakeLists.txt
index 753755d..111f80e 100644
--- a/lib/asan/CMakeLists.txt
+++ b/lib/asan/CMakeLists.txt
@@ -13,6 +13,7 @@
   asan_poisoning.cc
   asan_posix.cc
   asan_printf.cc
+  asan_report.cc
   asan_rtl.cc
   asan_stack.cc
   asan_stats.cc
diff --git a/lib/asan/asan_allocator.cc b/lib/asan/asan_allocator.cc
index b9c1942..f5c3e5f 100644
--- a/lib/asan/asan_allocator.cc
+++ b/lib/asan/asan_allocator.cc
@@ -32,6 +32,7 @@
 #include "asan_lock.h"
 #include "asan_mapping.h"
 #include "asan_stats.h"
+#include "asan_report.h"
 #include "asan_thread.h"
 #include "asan_thread_registry.h"
 #include "sanitizer_common/sanitizer_atomic.h"
@@ -585,7 +586,7 @@
   malloc_info.SwallowThreadLocalMallocStorage(this, true);
 }
 
-static void Describe(uptr addr, uptr access_size) {
+void DescribeHeapAddress(uptr addr, uptr access_size) {
   AsanChunk *m = malloc_info.FindMallocedOrFreed(addr, access_size);
   if (!m) return;
   m->DescribeAddress(addr, access_size);
@@ -727,15 +728,9 @@
                                        memory_order_acq_rel);
 
   if (old_chunk_state == CHUNK_QUARANTINE) {
-    AsanReport("ERROR: AddressSanitizer attempting double-free on %p:\n", ptr);
-    stack->PrintStack();
-    Describe((uptr)ptr, 1);
-    ShowStatsAndAbort();
+    ReportDoubleFree((uptr)ptr, stack);
   } else if (old_chunk_state != CHUNK_ALLOCATED) {
-    AsanReport("ERROR: AddressSanitizer attempting free on address "
-               "which was not malloc()-ed: %p\n", ptr);
-    stack->PrintStack();
-    ShowStatsAndAbort();
+    ReportFreeNotMalloced((uptr)ptr, stack);
   }
   CHECK(old_chunk_state == CHUNK_ALLOCATED);
   // With REDZONE==16 m->next is in the user area, otherwise it should be 0.
@@ -884,12 +879,7 @@
   if (ptr == 0) return 0;
   uptr usable_size = malloc_info.AllocationSize((uptr)ptr);
   if (flags()->check_malloc_usable_size && (usable_size == 0)) {
-    AsanReport("ERROR: AddressSanitizer attempting to call "
-               "malloc_usable_size() for pointer which is "
-               "not owned: %p\n", ptr);
-    stack->PrintStack();
-    Describe((uptr)ptr, 1);
-    ShowStatsAndAbort();
+    ReportMallocUsableSizeNotOwned((uptr)ptr, stack);
   }
   return usable_size;
 }
@@ -898,10 +888,6 @@
   return malloc_info.AllocationSize((uptr)ptr);
 }
 
-void DescribeHeapAddress(uptr addr, uptr access_size) {
-  Describe(addr, access_size);
-}
-
 void asan_mz_force_lock() {
   malloc_info.ForceLock();
 }
@@ -1090,12 +1076,8 @@
   uptr allocated_size = malloc_info.AllocationSize((uptr)p);
   // Die if p is not malloced or if it is already freed.
   if (allocated_size == 0) {
-    AsanReport("ERROR: AddressSanitizer attempting to call "
-               "__asan_get_allocated_size() for pointer which is "
-               "not owned: %p\n", p);
-    PRINT_CURRENT_STACK();
-    Describe((uptr)p, 1);
-    ShowStatsAndAbort();
+    GET_STACK_TRACE_HERE(kStackTraceMax);
+    ReportAsanGetAllocatedSizeNotOwned((uptr)p, &stack);
   }
   return allocated_size;
 }
diff --git a/lib/asan/asan_report.cc b/lib/asan/asan_report.cc
index 8b70f32..05d9386 100644
--- a/lib/asan/asan_report.cc
+++ b/lib/asan/asan_report.cc
@@ -11,6 +11,7 @@
 //
 // This file contains error reporting code.
 //===----------------------------------------------------------------------===//
+#include "asan_allocator.h"
 #include "asan_internal.h"
 #include "asan_report.h"
 #include "asan_stack.h"
@@ -29,4 +30,37 @@
   ShowStatsAndAbort();
 }
 
+void ReportDoubleFree(uptr addr, AsanStackTrace *stack) {
+  AsanReport("ERROR: AddressSanitizer attempting double-free on %p:\n", addr);
+  stack->PrintStack();
+  DescribeHeapAddress(addr, 1);
+  ShowStatsAndAbort();
+}
+
+void ReportFreeNotMalloced(uptr addr, AsanStackTrace *stack) {
+  AsanReport("ERROR: AddressSanitizer attempting free on address "
+             "which was not malloc()-ed: %p\n", addr);
+  stack->PrintStack();
+  ShowStatsAndAbort();
+}
+
+void ReportMallocUsableSizeNotOwned(uptr addr, AsanStackTrace *stack) {
+  AsanReport("ERROR: AddressSanitizer attempting to call "
+             "malloc_usable_size() for pointer which is "
+             "not owned: %p\n", addr);
+  stack->PrintStack();
+  DescribeHeapAddress(addr, 1);
+  ShowStatsAndAbort();
+}
+
+void ReportAsanGetAllocatedSizeNotOwned(uptr addr, AsanStackTrace *stack) {
+  AsanReport("ERROR: AddressSanitizer attempting to call "
+             "__asan_get_allocated_size() for pointer which is "
+             "not owned: %p\n", addr);
+  stack->PrintStack();
+  DescribeHeapAddress(addr, 1);
+  ShowStatsAndAbort();
+}
+
+
 }  // namespace __asan
diff --git a/lib/asan/asan_report.h b/lib/asan/asan_report.h
index 36e622c..952a4ba 100644
--- a/lib/asan/asan_report.h
+++ b/lib/asan/asan_report.h
@@ -16,6 +16,13 @@
 
 namespace __asan {
 
-void ReportSIGSEGV(uptr pc, uptr sp, uptr bp, uptr addr);
+void NORETURN ReportSIGSEGV(uptr pc, uptr sp, uptr bp, uptr addr);
+
+void NORETURN ReportDoubleFree(uptr addr, AsanStackTrace *stack);
+void NORETURN ReportFreeNotMalloced(uptr addr, AsanStackTrace *stack);
+void NORETURN ReportMallocUsableSizeNotOwned(uptr addr,
+                                             AsanStackTrace *stack);
+void NORETURN ReportAsanGetAllocatedSizeNotOwned(uptr addr,
+                                                 AsanStackTrace *stack);
 
 }  // namespace __asan