]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - contrib/compiler-rt/lib/hwasan/hwasan_new_delete.cc
Merge compiler-rt trunk r321017 to contrib/compiler-rt.
[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
19 #if HWASAN_REPLACE_OPERATORS_NEW_AND_DELETE
20
21 #include <stddef.h>
22
23 using namespace __hwasan;  // NOLINT
24
25 // Fake std::nothrow_t to avoid including <new>.
26 namespace std {
27   struct nothrow_t {};
28 }  // namespace std
29
30
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();\
36   return res
37
38 INTERCEPTOR_ATTRIBUTE
39 void *operator new(size_t size) { OPERATOR_NEW_BODY(false /*nothrow*/); }
40 INTERCEPTOR_ATTRIBUTE
41 void *operator new[](size_t size) { OPERATOR_NEW_BODY(false /*nothrow*/); }
42 INTERCEPTOR_ATTRIBUTE
43 void *operator new(size_t size, std::nothrow_t const&) {
44   OPERATOR_NEW_BODY(true /*nothrow*/);
45 }
46 INTERCEPTOR_ATTRIBUTE
47 void *operator new[](size_t size, std::nothrow_t const&) {
48   OPERATOR_NEW_BODY(true /*nothrow*/);
49 }
50
51 #define OPERATOR_DELETE_BODY \
52   GET_MALLOC_STACK_TRACE; \
53   if (ptr) HwasanDeallocate(&stack, ptr)
54
55 INTERCEPTOR_ATTRIBUTE
56 void operator delete(void *ptr) NOEXCEPT { OPERATOR_DELETE_BODY; }
57 INTERCEPTOR_ATTRIBUTE
58 void operator delete[](void *ptr) NOEXCEPT { OPERATOR_DELETE_BODY; }
59 INTERCEPTOR_ATTRIBUTE
60 void operator delete(void *ptr, std::nothrow_t const&) { OPERATOR_DELETE_BODY; }
61 INTERCEPTOR_ATTRIBUTE
62 void operator delete[](void *ptr, std::nothrow_t const&) {
63   OPERATOR_DELETE_BODY;
64 }
65
66 #endif // HWASAN_REPLACE_OPERATORS_NEW_AND_DELETE