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 "interception/interception.h"
17 
18 #if MSAN_REPLACE_OPERATORS_NEW_AND_DELETE
19 
20 #include <stddef.h>
21 
22 using namespace __msan;  // NOLINT
23 
24 // Fake std::nothrow_t to avoid including <new>.
25 namespace std {
26   struct nothrow_t {};
27 }  // namespace std
28 
29 
30 #define OPERATOR_NEW_BODY \
31   GET_MALLOC_STACK_TRACE; \
32   return MsanReallocate(&stack, 0, size, sizeof(u64), false)
33 
34 INTERCEPTOR_ATTRIBUTE
operator new(size_t size)35 void *operator new(size_t size) { OPERATOR_NEW_BODY; }
36 INTERCEPTOR_ATTRIBUTE
operator new[](size_t size)37 void *operator new[](size_t size) { OPERATOR_NEW_BODY; }
38 INTERCEPTOR_ATTRIBUTE
operator new(size_t size,std::nothrow_t const &)39 void *operator new(size_t size, std::nothrow_t const&) { OPERATOR_NEW_BODY; }
40 INTERCEPTOR_ATTRIBUTE
operator new[](size_t size,std::nothrow_t const &)41 void *operator new[](size_t size, std::nothrow_t const&) { OPERATOR_NEW_BODY; }
42 
43 #define OPERATOR_DELETE_BODY \
44   GET_MALLOC_STACK_TRACE; \
45   if (ptr) MsanDeallocate(&stack, ptr)
46 
47 INTERCEPTOR_ATTRIBUTE
operator delete(void * ptr)48 void operator delete(void *ptr) NOEXCEPT { OPERATOR_DELETE_BODY; }
49 INTERCEPTOR_ATTRIBUTE
operator delete[](void * ptr)50 void operator delete[](void *ptr) NOEXCEPT { OPERATOR_DELETE_BODY; }
51 INTERCEPTOR_ATTRIBUTE
operator delete(void * ptr,std::nothrow_t const &)52 void operator delete(void *ptr, std::nothrow_t const&) { OPERATOR_DELETE_BODY; }
53 INTERCEPTOR_ATTRIBUTE
operator delete[](void * ptr,std::nothrow_t const &)54 void operator delete[](void *ptr, std::nothrow_t const&) {
55   OPERATOR_DELETE_BODY;
56 }
57 
58 #endif // MSAN_REPLACE_OPERATORS_NEW_AND_DELETE
59