[ASan] Move error reports away from ASan allocator. Add new source file to CMakeLists as well
llvm-svn: 161569
This commit is contained in:
		
							parent
							
								
									aa05110a17
								
							
						
					
					
						commit
						b4b316fc5b
					
				| 
						 | 
					@ -13,6 +13,7 @@ set(ASAN_SOURCES
 | 
				
			||||||
  asan_poisoning.cc
 | 
					  asan_poisoning.cc
 | 
				
			||||||
  asan_posix.cc
 | 
					  asan_posix.cc
 | 
				
			||||||
  asan_printf.cc
 | 
					  asan_printf.cc
 | 
				
			||||||
 | 
					  asan_report.cc
 | 
				
			||||||
  asan_rtl.cc
 | 
					  asan_rtl.cc
 | 
				
			||||||
  asan_stack.cc
 | 
					  asan_stack.cc
 | 
				
			||||||
  asan_stats.cc
 | 
					  asan_stats.cc
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -32,6 +32,7 @@
 | 
				
			||||||
#include "asan_lock.h"
 | 
					#include "asan_lock.h"
 | 
				
			||||||
#include "asan_mapping.h"
 | 
					#include "asan_mapping.h"
 | 
				
			||||||
#include "asan_stats.h"
 | 
					#include "asan_stats.h"
 | 
				
			||||||
 | 
					#include "asan_report.h"
 | 
				
			||||||
#include "asan_thread.h"
 | 
					#include "asan_thread.h"
 | 
				
			||||||
#include "asan_thread_registry.h"
 | 
					#include "asan_thread_registry.h"
 | 
				
			||||||
#include "sanitizer_common/sanitizer_atomic.h"
 | 
					#include "sanitizer_common/sanitizer_atomic.h"
 | 
				
			||||||
| 
						 | 
					@ -585,7 +586,7 @@ void AsanThreadLocalMallocStorage::CommitBack() {
 | 
				
			||||||
  malloc_info.SwallowThreadLocalMallocStorage(this, true);
 | 
					  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);
 | 
					  AsanChunk *m = malloc_info.FindMallocedOrFreed(addr, access_size);
 | 
				
			||||||
  if (!m) return;
 | 
					  if (!m) return;
 | 
				
			||||||
  m->DescribeAddress(addr, access_size);
 | 
					  m->DescribeAddress(addr, access_size);
 | 
				
			||||||
| 
						 | 
					@ -727,15 +728,9 @@ static void Deallocate(u8 *ptr, AsanStackTrace *stack) {
 | 
				
			||||||
                                       memory_order_acq_rel);
 | 
					                                       memory_order_acq_rel);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
  if (old_chunk_state == CHUNK_QUARANTINE) {
 | 
					  if (old_chunk_state == CHUNK_QUARANTINE) {
 | 
				
			||||||
    AsanReport("ERROR: AddressSanitizer attempting double-free on %p:\n", ptr);
 | 
					    ReportDoubleFree((uptr)ptr, stack);
 | 
				
			||||||
    stack->PrintStack();
 | 
					 | 
				
			||||||
    Describe((uptr)ptr, 1);
 | 
					 | 
				
			||||||
    ShowStatsAndAbort();
 | 
					 | 
				
			||||||
  } else if (old_chunk_state != CHUNK_ALLOCATED) {
 | 
					  } else if (old_chunk_state != CHUNK_ALLOCATED) {
 | 
				
			||||||
    AsanReport("ERROR: AddressSanitizer attempting free on address "
 | 
					    ReportFreeNotMalloced((uptr)ptr, stack);
 | 
				
			||||||
               "which was not malloc()-ed: %p\n", ptr);
 | 
					 | 
				
			||||||
    stack->PrintStack();
 | 
					 | 
				
			||||||
    ShowStatsAndAbort();
 | 
					 | 
				
			||||||
  }
 | 
					  }
 | 
				
			||||||
  CHECK(old_chunk_state == CHUNK_ALLOCATED);
 | 
					  CHECK(old_chunk_state == CHUNK_ALLOCATED);
 | 
				
			||||||
  // With REDZONE==16 m->next is in the user area, otherwise it should be 0.
 | 
					  // 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;
 | 
					  if (ptr == 0) return 0;
 | 
				
			||||||
  uptr usable_size = malloc_info.AllocationSize((uptr)ptr);
 | 
					  uptr usable_size = malloc_info.AllocationSize((uptr)ptr);
 | 
				
			||||||
  if (flags()->check_malloc_usable_size && (usable_size == 0)) {
 | 
					  if (flags()->check_malloc_usable_size && (usable_size == 0)) {
 | 
				
			||||||
    AsanReport("ERROR: AddressSanitizer attempting to call "
 | 
					    ReportMallocUsableSizeNotOwned((uptr)ptr, stack);
 | 
				
			||||||
               "malloc_usable_size() for pointer which is "
 | 
					 | 
				
			||||||
               "not owned: %p\n", ptr);
 | 
					 | 
				
			||||||
    stack->PrintStack();
 | 
					 | 
				
			||||||
    Describe((uptr)ptr, 1);
 | 
					 | 
				
			||||||
    ShowStatsAndAbort();
 | 
					 | 
				
			||||||
  }
 | 
					  }
 | 
				
			||||||
  return usable_size;
 | 
					  return usable_size;
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
| 
						 | 
					@ -898,10 +888,6 @@ uptr asan_mz_size(const void *ptr) {
 | 
				
			||||||
  return malloc_info.AllocationSize((uptr)ptr);
 | 
					  return malloc_info.AllocationSize((uptr)ptr);
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
void DescribeHeapAddress(uptr addr, uptr access_size) {
 | 
					 | 
				
			||||||
  Describe(addr, access_size);
 | 
					 | 
				
			||||||
}
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
void asan_mz_force_lock() {
 | 
					void asan_mz_force_lock() {
 | 
				
			||||||
  malloc_info.ForceLock();
 | 
					  malloc_info.ForceLock();
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
| 
						 | 
					@ -1090,12 +1076,8 @@ uptr __asan_get_allocated_size(const void *p) {
 | 
				
			||||||
  uptr allocated_size = malloc_info.AllocationSize((uptr)p);
 | 
					  uptr allocated_size = malloc_info.AllocationSize((uptr)p);
 | 
				
			||||||
  // Die if p is not malloced or if it is already freed.
 | 
					  // Die if p is not malloced or if it is already freed.
 | 
				
			||||||
  if (allocated_size == 0) {
 | 
					  if (allocated_size == 0) {
 | 
				
			||||||
    AsanReport("ERROR: AddressSanitizer attempting to call "
 | 
					    GET_STACK_TRACE_HERE(kStackTraceMax);
 | 
				
			||||||
               "__asan_get_allocated_size() for pointer which is "
 | 
					    ReportAsanGetAllocatedSizeNotOwned((uptr)p, &stack);
 | 
				
			||||||
               "not owned: %p\n", p);
 | 
					 | 
				
			||||||
    PRINT_CURRENT_STACK();
 | 
					 | 
				
			||||||
    Describe((uptr)p, 1);
 | 
					 | 
				
			||||||
    ShowStatsAndAbort();
 | 
					 | 
				
			||||||
  }
 | 
					  }
 | 
				
			||||||
  return allocated_size;
 | 
					  return allocated_size;
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -11,6 +11,7 @@
 | 
				
			||||||
//
 | 
					//
 | 
				
			||||||
// This file contains error reporting code.
 | 
					// This file contains error reporting code.
 | 
				
			||||||
//===----------------------------------------------------------------------===//
 | 
					//===----------------------------------------------------------------------===//
 | 
				
			||||||
 | 
					#include "asan_allocator.h"
 | 
				
			||||||
#include "asan_internal.h"
 | 
					#include "asan_internal.h"
 | 
				
			||||||
#include "asan_report.h"
 | 
					#include "asan_report.h"
 | 
				
			||||||
#include "asan_stack.h"
 | 
					#include "asan_stack.h"
 | 
				
			||||||
| 
						 | 
					@ -29,4 +30,37 @@ void ReportSIGSEGV(uptr pc, uptr sp, uptr bp, uptr addr) {
 | 
				
			||||||
  ShowStatsAndAbort();
 | 
					  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
 | 
					}  // namespace __asan
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -16,6 +16,13 @@
 | 
				
			||||||
 | 
					
 | 
				
			||||||
namespace __asan {
 | 
					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
 | 
					}  // namespace __asan
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
		Loading…
	
		Reference in New Issue