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"
18 #include "sanitizer_common/sanitizer_allocator_report.h"
20 #if HWASAN_REPLACE_OPERATORS_NEW_AND_DELETE
24 using namespace __hwasan; // NOLINT
26 // Fake std::nothrow_t to avoid including <new>.
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 = hwasan_malloc(size, &stack);\
36 if (!nothrow && UNLIKELY(!res)) ReportOutOfMemory(size, &stack);\
40 void *operator new(size_t size) { OPERATOR_NEW_BODY(false /*nothrow*/); }
42 void *operator new[](size_t size) { OPERATOR_NEW_BODY(false /*nothrow*/); }
44 void *operator new(size_t size, std::nothrow_t const&) {
45 OPERATOR_NEW_BODY(true /*nothrow*/);
48 void *operator new[](size_t size, std::nothrow_t const&) {
49 OPERATOR_NEW_BODY(true /*nothrow*/);
52 #define OPERATOR_DELETE_BODY \
53 GET_MALLOC_STACK_TRACE; \
54 if (ptr) HwasanDeallocate(&stack, ptr)
57 void operator delete(void *ptr) NOEXCEPT { OPERATOR_DELETE_BODY; }
59 void operator delete[](void *ptr) NOEXCEPT { OPERATOR_DELETE_BODY; }
61 void operator delete(void *ptr, std::nothrow_t const&) { OPERATOR_DELETE_BODY; }
63 void operator delete[](void *ptr, std::nothrow_t const&) {
67 #endif // HWASAN_REPLACE_OPERATORS_NEW_AND_DELETE