• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
21 namespace __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.
ReplaceOperatorsNewAndDelete()25 void ReplaceOperatorsNewAndDelete() { }
26 }
27 
28 using 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
operator new(size_t size)35 void *operator new(size_t size) { OPERATOR_NEW_BODY; }
operator new[](size_t size)36 void *operator new[](size_t size) { OPERATOR_NEW_BODY; }
37 #else
operator new(size_t size)38 void *operator new(size_t size) throw(std::bad_alloc) { OPERATOR_NEW_BODY; }
operator new[](size_t size)39 void *operator new[](size_t size) throw(std::bad_alloc) { OPERATOR_NEW_BODY; }
operator new(size_t size,std::nothrow_t const &)40 void *operator new(size_t size, std::nothrow_t const&) throw()
41 { OPERATOR_NEW_BODY; }
operator new[](size_t size,std::nothrow_t const &)42 void *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 
operator delete(void * ptr)50 void operator delete(void *ptr) throw() { OPERATOR_DELETE_BODY; }
operator delete[](void * ptr)51 void operator delete[](void *ptr) throw() { OPERATOR_DELETE_BODY; }
operator delete(void * ptr,std::nothrow_t const &)52 void operator delete(void *ptr, std::nothrow_t const&) throw()
53 { OPERATOR_DELETE_BODY; }
operator delete[](void * ptr,std::nothrow_t const &)54 void operator delete[](void *ptr, std::nothrow_t const&) throw()
55 { OPERATOR_DELETE_BODY; }
56