asan_new_delete.cc revision 4d5f98df886051afeece1698d4bc8f154391c22d
1//===-- asan_interceptors.cc ------------------------------------*- C++ -*-===// 2// 3// The LLVM Compiler Infrastructure 4// 5// This file is distributed under the University of Illinois Open Source 6// License. See LICENSE.TXT for details. 7// 8//===----------------------------------------------------------------------===// 9// 10// This file is a part of AddressSanitizer, an address sanity checker. 11// 12// Interceptors for operators new and delete. 13//===----------------------------------------------------------------------===// 14 15#include "asan_allocator.h" 16#include "asan_internal.h" 17#include "asan_stack.h" 18 19#include <new> 20 21namespace __asan { 22// This function is a no-op. We need it to make sure that object file 23// with our replacements will actually be loaded from static ASan 24// run-time library at link-time. 25void ReplaceOperatorsNewAndDelete() { } 26} 27 28using namespace __asan; // NOLINT 29 30#define OPERATOR_NEW_BODY \ 31 GET_STACK_TRACE_HERE_FOR_MALLOC;\ 32 return asan_memalign(0, size, &stack); 33 34#ifdef ANDROID 35void *operator new(size_t size) { OPERATOR_NEW_BODY; } 36void *operator new[](size_t size) { OPERATOR_NEW_BODY; } 37#else 38void *operator new(size_t size) throw(std::bad_alloc) { OPERATOR_NEW_BODY; } 39void *operator new[](size_t size) throw(std::bad_alloc) { OPERATOR_NEW_BODY; } 40void *operator new(size_t size, std::nothrow_t const&) throw() 41{ OPERATOR_NEW_BODY; } 42void *operator new[](size_t size, std::nothrow_t const&) throw() 43{ OPERATOR_NEW_BODY; } 44#endif 45 46#define OPERATOR_DELETE_BODY \ 47 GET_STACK_TRACE_HERE_FOR_FREE(ptr);\ 48 asan_free(ptr, &stack); 49 50void operator delete(void *ptr) throw() { OPERATOR_DELETE_BODY; } 51void operator delete[](void *ptr) throw() { OPERATOR_DELETE_BODY; } 52void operator delete(void *ptr, std::nothrow_t const&) throw() 53{ OPERATOR_DELETE_BODY; } 54void operator delete[](void *ptr, std::nothrow_t const&) throw() 55{ OPERATOR_DELETE_BODY; } 56