1 //===-- msan_new_delete.cc ------------------------------------------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file is a part of MemorySanitizer.
12 // Interceptors for operators new and delete.
13 //===----------------------------------------------------------------------===//
16 #include "interception/interception.h"
17 #include "sanitizer_common/sanitizer_allocator.h"
19 #if MSAN_REPLACE_OPERATORS_NEW_AND_DELETE
23 using namespace __msan; // NOLINT
25 // Fake std::nothrow_t and std::align_val_t to avoid including <new>.
28 enum class align_val_t: size_t {};
32 // TODO(alekseys): throw std::bad_alloc instead of dying on OOM.
33 #define OPERATOR_NEW_BODY(nothrow) \
34 GET_MALLOC_STACK_TRACE; \
35 void *res = msan_malloc(size, &stack);\
36 if (!nothrow && UNLIKELY(!res)) DieOnFailure::OnOOM();\
38 #define OPERATOR_NEW_BODY_ALIGN(nothrow) \
39 GET_MALLOC_STACK_TRACE;\
40 void *res = msan_memalign((uptr)align, size, &stack);\
41 if (!nothrow && UNLIKELY(!res)) DieOnFailure::OnOOM();\
45 void *operator new(size_t size) { OPERATOR_NEW_BODY(false /*nothrow*/); }
47 void *operator new[](size_t size) { OPERATOR_NEW_BODY(false /*nothrow*/); }
49 void *operator new(size_t size, std::nothrow_t const&) {
50 OPERATOR_NEW_BODY(true /*nothrow*/);
53 void *operator new[](size_t size, std::nothrow_t const&) {
54 OPERATOR_NEW_BODY(true /*nothrow*/);
57 void *operator new(size_t size, std::align_val_t align)
58 { OPERATOR_NEW_BODY_ALIGN(false /*nothrow*/); }
60 void *operator new[](size_t size, std::align_val_t align)
61 { OPERATOR_NEW_BODY_ALIGN(false /*nothrow*/); }
63 void *operator new(size_t size, std::align_val_t align, std::nothrow_t const&)
64 { OPERATOR_NEW_BODY_ALIGN(true /*nothrow*/); }
66 void *operator new[](size_t size, std::align_val_t align, std::nothrow_t const&)
67 { OPERATOR_NEW_BODY_ALIGN(true /*nothrow*/); }
69 #define OPERATOR_DELETE_BODY \
70 GET_MALLOC_STACK_TRACE; \
71 if (ptr) MsanDeallocate(&stack, ptr)
74 void operator delete(void *ptr) NOEXCEPT { OPERATOR_DELETE_BODY; }
76 void operator delete[](void *ptr) NOEXCEPT { OPERATOR_DELETE_BODY; }
78 void operator delete(void *ptr, std::nothrow_t const&) { OPERATOR_DELETE_BODY; }
80 void operator delete[](void *ptr, std::nothrow_t const&) {
84 void operator delete(void *ptr, size_t size) NOEXCEPT { OPERATOR_DELETE_BODY; }
86 void operator delete[](void *ptr, size_t size) NOEXCEPT
87 { OPERATOR_DELETE_BODY; }
89 void operator delete(void *ptr, std::align_val_t align) NOEXCEPT
90 { OPERATOR_DELETE_BODY; }
92 void operator delete[](void *ptr, std::align_val_t align) NOEXCEPT
93 { OPERATOR_DELETE_BODY; }
95 void operator delete(void *ptr, std::align_val_t align, std::nothrow_t const&)
96 { OPERATOR_DELETE_BODY; }
98 void operator delete[](void *ptr, std::align_val_t align, std::nothrow_t const&)
99 { OPERATOR_DELETE_BODY; }
100 INTERCEPTOR_ATTRIBUTE
101 void operator delete(void *ptr, size_t size, std::align_val_t align) NOEXCEPT
102 { OPERATOR_DELETE_BODY; }
103 INTERCEPTOR_ATTRIBUTE
104 void operator delete[](void *ptr, size_t size, std::align_val_t align) NOEXCEPT
105 { OPERATOR_DELETE_BODY; }
108 #endif // MSAN_REPLACE_OPERATORS_NEW_AND_DELETE