Home | History | Annotate | Download | only in msan
      1 //===-- msan_new_delete.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 MemorySanitizer.
     11 //
     12 // Interceptors for operators new and delete.
     13 //===----------------------------------------------------------------------===//
     14 
     15 #include "msan.h"
     16 
     17 #if MSAN_REPLACE_OPERATORS_NEW_AND_DELETE
     18 
     19 #include <stddef.h>
     20 
     21 namespace __msan {
     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 MSan
     24 // run-time library at link-time.
     25 void ReplaceOperatorsNewAndDelete() { }
     26 }
     27 
     28 using namespace __msan;  // NOLINT
     29 
     30 // Fake std::nothrow_t to avoid including <new>.
     31 namespace std {
     32   struct nothrow_t {};
     33 }  // namespace std
     34 
     35 
     36 #define OPERATOR_NEW_BODY \
     37   GET_MALLOC_STACK_TRACE; \
     38   return MsanReallocate(&stack, 0, size, sizeof(u64), false)
     39 
     40 void *operator new(size_t size) { OPERATOR_NEW_BODY; }
     41 void *operator new[](size_t size) { OPERATOR_NEW_BODY; }
     42 void *operator new(size_t size, std::nothrow_t const&) { OPERATOR_NEW_BODY; }
     43 void *operator new[](size_t size, std::nothrow_t const&) { OPERATOR_NEW_BODY; }
     44 
     45 #define OPERATOR_DELETE_BODY \
     46   if (ptr) MsanDeallocate(ptr)
     47 
     48 void operator delete(void *ptr) { OPERATOR_DELETE_BODY; }
     49 void operator delete[](void *ptr) { OPERATOR_DELETE_BODY; }
     50 void operator delete(void *ptr, std::nothrow_t const&) { OPERATOR_DELETE_BODY; }
     51 void operator delete[](void *ptr, std::nothrow_t const&) {
     52   OPERATOR_DELETE_BODY;
     53 }
     54 
     55 #endif // MSAN_REPLACE_OPERATORS_NEW_AND_DELETE
     56