From b4b316fc5b8594e52ea7c9e30f0438eae8c61b67 Mon Sep 17 00:00:00 2001 From: Alexey Samsonov Date: Thu, 9 Aug 2012 08:15:46 +0000 Subject: [PATCH] [ASan] Move error reports away from ASan allocator. Add new source file to CMakeLists as well llvm-svn: 161569 --- compiler-rt/lib/asan/CMakeLists.txt | 1 + compiler-rt/lib/asan/asan_allocator.cc | 32 ++++++------------------ compiler-rt/lib/asan/asan_report.cc | 34 ++++++++++++++++++++++++++ compiler-rt/lib/asan/asan_report.h | 9 ++++++- 4 files changed, 50 insertions(+), 26 deletions(-) diff --git a/compiler-rt/lib/asan/CMakeLists.txt b/compiler-rt/lib/asan/CMakeLists.txt index 753755da1668..111f80ea0dd1 100644 --- a/compiler-rt/lib/asan/CMakeLists.txt +++ b/compiler-rt/lib/asan/CMakeLists.txt @@ -13,6 +13,7 @@ set(ASAN_SOURCES asan_poisoning.cc asan_posix.cc asan_printf.cc + asan_report.cc asan_rtl.cc asan_stack.cc asan_stats.cc diff --git a/compiler-rt/lib/asan/asan_allocator.cc b/compiler-rt/lib/asan/asan_allocator.cc index b9c194233cfe..f5c3e5fbc702 100644 --- a/compiler-rt/lib/asan/asan_allocator.cc +++ b/compiler-rt/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 @@ void AsanThreadLocalMallocStorage::CommitBack() { 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 @@ static void Deallocate(u8 *ptr, AsanStackTrace *stack) { 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 @@ uptr asan_malloc_usable_size(void *ptr, AsanStackTrace *stack) { 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 @@ uptr asan_mz_size(const void *ptr) { 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 __asan_get_allocated_size(const void *p) { 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/compiler-rt/lib/asan/asan_report.cc b/compiler-rt/lib/asan/asan_report.cc index 8b70f320e983..05d938680d39 100644 --- a/compiler-rt/lib/asan/asan_report.cc +++ b/compiler-rt/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 @@ void ReportSIGSEGV(uptr pc, uptr sp, uptr bp, uptr addr) { 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/compiler-rt/lib/asan/asan_report.h b/compiler-rt/lib/asan/asan_report.h index 36e622cf5e28..952a4ba6034e 100644 --- a/compiler-rt/lib/asan/asan_report.h +++ b/compiler-rt/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