Home | History | Annotate | Download | only in asan
      1 //===-- asan_interceptors.cc ----------------------------------------------===//
      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 <stddef.h>
     20 #include <new>
     21 
     22 namespace __asan {
     23 // This function is a no-op. We need it to make sure that object file
     24 // with our replacements will actually be loaded from static ASan
     25 // run-time library at link-time.
     26 void ReplaceOperatorsNewAndDelete() { }
     27 }
     28 
     29 using namespace __asan;  // NOLINT
     30 
     31 #define OPERATOR_NEW_BODY \
     32   GET_STACK_TRACE_HERE_FOR_MALLOC;\
     33   return asan_memalign(0, size, &stack);
     34 
     35 #if ASAN_ANDROID
     36 void *operator new(size_t size) { OPERATOR_NEW_BODY; }
     37 void *operator new[](size_t size) { OPERATOR_NEW_BODY; }
     38 #else
     39 void *operator new(size_t size) throw(std::bad_alloc) { OPERATOR_NEW_BODY; }
     40 void *operator new[](size_t size) throw(std::bad_alloc) { OPERATOR_NEW_BODY; }
     41 void *operator new(size_t size, std::nothrow_t const&) throw()
     42 { OPERATOR_NEW_BODY; }
     43 void *operator new[](size_t size, std::nothrow_t const&) throw()
     44 { OPERATOR_NEW_BODY; }
     45 #endif
     46 
     47 #define OPERATOR_DELETE_BODY \
     48   GET_STACK_TRACE_HERE_FOR_FREE(ptr);\
     49   asan_free(ptr, &stack);
     50 
     51 void operator delete(void *ptr) throw() { OPERATOR_DELETE_BODY; }
     52 void operator delete[](void *ptr) throw() { OPERATOR_DELETE_BODY; }
     53 void operator delete(void *ptr, std::nothrow_t const&) throw()
     54 { OPERATOR_DELETE_BODY; }
     55 void operator delete[](void *ptr, std::nothrow_t const&) throw()
     56 { OPERATOR_DELETE_BODY; }
     57