• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 #include "sanitizer_common/sanitizer_interception.h"
17 
18 #if MSAN_REPLACE_OPERATORS_NEW_AND_DELETE
19 
20 #include <stddef.h>
21 
22 namespace __msan {
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 MSan
25 // run-time library at link-time.
ReplaceOperatorsNewAndDelete()26 void ReplaceOperatorsNewAndDelete() { }
27 }
28 
29 using namespace __msan;  // NOLINT
30 
31 // Fake std::nothrow_t to avoid including <new>.
32 namespace std {
33   struct nothrow_t {};
34 }  // namespace std
35 
36 
37 #define OPERATOR_NEW_BODY \
38   GET_MALLOC_STACK_TRACE; \
39   return MsanReallocate(&stack, 0, size, sizeof(u64), false)
40 
41 INTERCEPTOR_ATTRIBUTE
operator new(size_t size)42 void *operator new(size_t size) { OPERATOR_NEW_BODY; }
43 INTERCEPTOR_ATTRIBUTE
operator new[](size_t size)44 void *operator new[](size_t size) { OPERATOR_NEW_BODY; }
45 INTERCEPTOR_ATTRIBUTE
operator new(size_t size,std::nothrow_t const &)46 void *operator new(size_t size, std::nothrow_t const&) { OPERATOR_NEW_BODY; }
47 INTERCEPTOR_ATTRIBUTE
operator new[](size_t size,std::nothrow_t const &)48 void *operator new[](size_t size, std::nothrow_t const&) { OPERATOR_NEW_BODY; }
49 
50 #define OPERATOR_DELETE_BODY \
51   GET_MALLOC_STACK_TRACE; \
52   if (ptr) MsanDeallocate(&stack, ptr)
53 
54 INTERCEPTOR_ATTRIBUTE
operator delete(void * ptr)55 void operator delete(void *ptr) throw() { OPERATOR_DELETE_BODY; }
56 INTERCEPTOR_ATTRIBUTE
operator delete[](void * ptr)57 void operator delete[](void *ptr) throw() { OPERATOR_DELETE_BODY; }
58 INTERCEPTOR_ATTRIBUTE
operator delete(void * ptr,std::nothrow_t const &)59 void operator delete(void *ptr, std::nothrow_t const&) { OPERATOR_DELETE_BODY; }
60 INTERCEPTOR_ATTRIBUTE
operator delete[](void * ptr,std::nothrow_t const &)61 void operator delete[](void *ptr, std::nothrow_t const&) {
62   OPERATOR_DELETE_BODY;
63 }
64 
65 #endif // MSAN_REPLACE_OPERATORS_NEW_AND_DELETE
66