1 //===-- hwasan_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 HWAddressSanitizer.
12 // Interceptors for operators new and delete.
13 //===----------------------------------------------------------------------===//
16 #include "interception/interception.h"
17 #include "sanitizer_common/sanitizer_allocator.h"
19 #if HWASAN_REPLACE_OPERATORS_NEW_AND_DELETE
23 using namespace __hwasan; // NOLINT
25 // Fake std::nothrow_t to avoid including <new>.
31 // TODO(alekseys): throw std::bad_alloc instead of dying on OOM.
32 #define OPERATOR_NEW_BODY(nothrow) \
33 GET_MALLOC_STACK_TRACE; \
34 void *res = hwasan_malloc(size, &stack);\
35 if (!nothrow && UNLIKELY(!res)) DieOnFailure::OnOOM();\
39 void *operator new(size_t size) { OPERATOR_NEW_BODY(false /*nothrow*/); }
41 void *operator new[](size_t size) { OPERATOR_NEW_BODY(false /*nothrow*/); }
43 void *operator new(size_t size, std::nothrow_t const&) {
44 OPERATOR_NEW_BODY(true /*nothrow*/);
47 void *operator new[](size_t size, std::nothrow_t const&) {
48 OPERATOR_NEW_BODY(true /*nothrow*/);
51 #define OPERATOR_DELETE_BODY \
52 GET_MALLOC_STACK_TRACE; \
53 if (ptr) HwasanDeallocate(&stack, ptr)
56 void operator delete(void *ptr) NOEXCEPT { OPERATOR_DELETE_BODY; }
58 void operator delete[](void *ptr) NOEXCEPT { OPERATOR_DELETE_BODY; }
60 void operator delete(void *ptr, std::nothrow_t const&) { OPERATOR_DELETE_BODY; }
62 void operator delete[](void *ptr, std::nothrow_t const&) {
66 #endif // HWASAN_REPLACE_OPERATORS_NEW_AND_DELETE