]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - contrib/compiler-rt/lib/hwasan/hwasan_new_delete.cc
MFC r355070:
[FreeBSD/FreeBSD.git] / contrib / compiler-rt / lib / hwasan / hwasan_new_delete.cc
1 //===-- hwasan_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 HWAddressSanitizer.
11 //
12 // Interceptors for operators new and delete.
13 //===----------------------------------------------------------------------===//
14
15 #include "hwasan.h"
16 #include "interception/interception.h"
17 #include "sanitizer_common/sanitizer_allocator.h"
18 #include "sanitizer_common/sanitizer_allocator_report.h"
19
20 #if HWASAN_REPLACE_OPERATORS_NEW_AND_DELETE
21
22 #include <stddef.h>
23
24 using namespace __hwasan;  // NOLINT
25
26 // Fake std::nothrow_t to avoid including <new>.
27 namespace std {
28   struct nothrow_t {};
29 }  // namespace std
30
31
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);\
37   return res
38
39 INTERCEPTOR_ATTRIBUTE
40 void *operator new(size_t size) { OPERATOR_NEW_BODY(false /*nothrow*/); }
41 INTERCEPTOR_ATTRIBUTE
42 void *operator new[](size_t size) { OPERATOR_NEW_BODY(false /*nothrow*/); }
43 INTERCEPTOR_ATTRIBUTE
44 void *operator new(size_t size, std::nothrow_t const&) {
45   OPERATOR_NEW_BODY(true /*nothrow*/);
46 }
47 INTERCEPTOR_ATTRIBUTE
48 void *operator new[](size_t size, std::nothrow_t const&) {
49   OPERATOR_NEW_BODY(true /*nothrow*/);
50 }
51
52 #define OPERATOR_DELETE_BODY \
53   GET_MALLOC_STACK_TRACE; \
54   if (ptr) hwasan_free(ptr, &stack)
55
56 INTERCEPTOR_ATTRIBUTE
57 void operator delete(void *ptr) NOEXCEPT { OPERATOR_DELETE_BODY; }
58 INTERCEPTOR_ATTRIBUTE
59 void operator delete[](void *ptr) NOEXCEPT { OPERATOR_DELETE_BODY; }
60 INTERCEPTOR_ATTRIBUTE
61 void operator delete(void *ptr, std::nothrow_t const&) { OPERATOR_DELETE_BODY; }
62 INTERCEPTOR_ATTRIBUTE
63 void operator delete[](void *ptr, std::nothrow_t const&) {
64   OPERATOR_DELETE_BODY;
65 }
66
67 #endif // HWASAN_REPLACE_OPERATORS_NEW_AND_DELETE