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"
18 #include "sanitizer_common/sanitizer_allocator_report.h"
20 #if MSAN_REPLACE_OPERATORS_NEW_AND_DELETE
24 using namespace __msan; // NOLINT
26 // Fake std::nothrow_t and std::align_val_t to avoid including <new>.
29 enum class align_val_t: size_t {};
33 // TODO(alekseys): throw std::bad_alloc instead of dying on OOM.
34 #define OPERATOR_NEW_BODY(nothrow) \
35 GET_MALLOC_STACK_TRACE; \
36 void *res = msan_malloc(size, &stack);\
37 if (!nothrow && UNLIKELY(!res)) ReportOutOfMemory(size, &stack);\
39 #define OPERATOR_NEW_BODY_ALIGN(nothrow) \
40 GET_MALLOC_STACK_TRACE;\
41 void *res = msan_memalign((uptr)align, size, &stack);\
42 if (!nothrow && UNLIKELY(!res)) ReportOutOfMemory(size, &stack);\
46 void *operator new(size_t size) { OPERATOR_NEW_BODY(false /*nothrow*/); }
48 void *operator new[](size_t size) { OPERATOR_NEW_BODY(false /*nothrow*/); }
50 void *operator new(size_t size, std::nothrow_t const&) {
51 OPERATOR_NEW_BODY(true /*nothrow*/);
54 void *operator new[](size_t size, std::nothrow_t const&) {
55 OPERATOR_NEW_BODY(true /*nothrow*/);
58 void *operator new(size_t size, std::align_val_t align)
59 { OPERATOR_NEW_BODY_ALIGN(false /*nothrow*/); }
61 void *operator new[](size_t size, std::align_val_t align)
62 { OPERATOR_NEW_BODY_ALIGN(false /*nothrow*/); }
64 void *operator new(size_t size, std::align_val_t align, std::nothrow_t const&)
65 { OPERATOR_NEW_BODY_ALIGN(true /*nothrow*/); }
67 void *operator new[](size_t size, std::align_val_t align, std::nothrow_t const&)
68 { OPERATOR_NEW_BODY_ALIGN(true /*nothrow*/); }
70 #define OPERATOR_DELETE_BODY \
71 GET_MALLOC_STACK_TRACE; \
72 if (ptr) MsanDeallocate(&stack, ptr)
75 void operator delete(void *ptr) NOEXCEPT { OPERATOR_DELETE_BODY; }
77 void operator delete[](void *ptr) NOEXCEPT { OPERATOR_DELETE_BODY; }
79 void operator delete(void *ptr, std::nothrow_t const&) { OPERATOR_DELETE_BODY; }
81 void operator delete[](void *ptr, std::nothrow_t const&) {
85 void operator delete(void *ptr, size_t size) NOEXCEPT { OPERATOR_DELETE_BODY; }
87 void operator delete[](void *ptr, size_t size) NOEXCEPT
88 { OPERATOR_DELETE_BODY; }
90 void operator delete(void *ptr, std::align_val_t align) NOEXCEPT
91 { OPERATOR_DELETE_BODY; }
93 void operator delete[](void *ptr, std::align_val_t align) NOEXCEPT
94 { OPERATOR_DELETE_BODY; }
96 void operator delete(void *ptr, std::align_val_t align, std::nothrow_t const&)
97 { OPERATOR_DELETE_BODY; }
99 void operator delete[](void *ptr, std::align_val_t align, std::nothrow_t const&)
100 { OPERATOR_DELETE_BODY; }
101 INTERCEPTOR_ATTRIBUTE
102 void operator delete(void *ptr, size_t size, std::align_val_t align) NOEXCEPT
103 { OPERATOR_DELETE_BODY; }
104 INTERCEPTOR_ATTRIBUTE
105 void operator delete[](void *ptr, size_t size, std::align_val_t align) NOEXCEPT
106 { OPERATOR_DELETE_BODY; }
109 #endif // MSAN_REPLACE_OPERATORS_NEW_AND_DELETE