]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - contrib/compiler-rt/lib/asan/asan_new_delete.cc
MFV r329753: 8809 libzpool should leverage work done in libfakekernel
[FreeBSD/FreeBSD.git] / contrib / compiler-rt / lib / asan / asan_new_delete.cc
1 //===-- asan_interceptors.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 AddressSanitizer, an address sanity checker.
11 //
12 // Interceptors for operators new and delete.
13 //===----------------------------------------------------------------------===//
14
15 #include "asan_allocator.h"
16 #include "asan_internal.h"
17 #include "asan_stack.h"
18
19 #include "interception/interception.h"
20
21 #include <stddef.h>
22
23 // C++ operators can't have dllexport attributes on Windows. We export them
24 // anyway by passing extra -export flags to the linker, which is exactly that
25 // dllexport would normally do. We need to export them in order to make the
26 // VS2015 dynamic CRT (MD) work.
27 #if SANITIZER_WINDOWS
28 #define CXX_OPERATOR_ATTRIBUTE
29 #define COMMENT_EXPORT(sym) __pragma(comment(linker, "/export:" sym))
30 #ifdef _WIN64
31 COMMENT_EXPORT("??2@YAPEAX_K@Z")                     // operator new
32 COMMENT_EXPORT("??2@YAPEAX_KAEBUnothrow_t@std@@@Z")  // operator new nothrow
33 COMMENT_EXPORT("??3@YAXPEAX@Z")                      // operator delete
34 COMMENT_EXPORT("??3@YAXPEAX_K@Z")                    // sized operator delete
35 COMMENT_EXPORT("??_U@YAPEAX_K@Z")                    // operator new[]
36 COMMENT_EXPORT("??_V@YAXPEAX@Z")                     // operator delete[]
37 #else
38 COMMENT_EXPORT("??2@YAPAXI@Z")                    // operator new
39 COMMENT_EXPORT("??2@YAPAXIABUnothrow_t@std@@@Z")  // operator new nothrow
40 COMMENT_EXPORT("??3@YAXPAX@Z")                    // operator delete
41 COMMENT_EXPORT("??3@YAXPAXI@Z")                   // sized operator delete
42 COMMENT_EXPORT("??_U@YAPAXI@Z")                   // operator new[]
43 COMMENT_EXPORT("??_V@YAXPAX@Z")                   // operator delete[]
44 #endif
45 #undef COMMENT_EXPORT
46 #else
47 #define CXX_OPERATOR_ATTRIBUTE INTERCEPTOR_ATTRIBUTE
48 #endif
49
50 using namespace __asan;  // NOLINT
51
52 // FreeBSD prior v9.2 have wrong definition of 'size_t'.
53 // http://svnweb.freebsd.org/base?view=revision&revision=232261
54 #if SANITIZER_FREEBSD && SANITIZER_WORDSIZE == 32
55 #include <sys/param.h>
56 #if __FreeBSD_version <= 902001  // v9.2
57 #define size_t unsigned
58 #endif  // __FreeBSD_version
59 #endif  // SANITIZER_FREEBSD && SANITIZER_WORDSIZE == 32
60
61 // This code has issues on OSX.
62 // See https://github.com/google/sanitizers/issues/131.
63
64 // Fake std::nothrow_t and std::align_val_t to avoid including <new>.
65 namespace std {
66 struct nothrow_t {};
67 enum class align_val_t: size_t {};
68 }  // namespace std
69
70 // TODO(alekseys): throw std::bad_alloc instead of dying on OOM.
71 #define OPERATOR_NEW_BODY(type, nothrow) \
72   GET_STACK_TRACE_MALLOC;\
73   void *res = asan_memalign(0, size, &stack, type);\
74   if (!nothrow && UNLIKELY(!res)) DieOnFailure::OnOOM();\
75   return res;
76 #define OPERATOR_NEW_BODY_ALIGN(type, nothrow) \
77   GET_STACK_TRACE_MALLOC;\
78   void *res = asan_memalign((uptr)align, size, &stack, type);\
79   if (!nothrow && UNLIKELY(!res)) DieOnFailure::OnOOM();\
80   return res;
81
82 // On OS X it's not enough to just provide our own 'operator new' and
83 // 'operator delete' implementations, because they're going to be in the
84 // runtime dylib, and the main executable will depend on both the runtime
85 // dylib and libstdc++, each of those'll have its implementation of new and
86 // delete.
87 // To make sure that C++ allocation/deallocation operators are overridden on
88 // OS X we need to intercept them using their mangled names.
89 #if !SANITIZER_MAC
90 CXX_OPERATOR_ATTRIBUTE
91 void *operator new(size_t size)
92 { OPERATOR_NEW_BODY(FROM_NEW, false /*nothrow*/); }
93 CXX_OPERATOR_ATTRIBUTE
94 void *operator new[](size_t size)
95 { OPERATOR_NEW_BODY(FROM_NEW_BR, false /*nothrow*/); }
96 CXX_OPERATOR_ATTRIBUTE
97 void *operator new(size_t size, std::nothrow_t const&)
98 { OPERATOR_NEW_BODY(FROM_NEW, true /*nothrow*/); }
99 CXX_OPERATOR_ATTRIBUTE
100 void *operator new[](size_t size, std::nothrow_t const&)
101 { OPERATOR_NEW_BODY(FROM_NEW_BR, true /*nothrow*/); }
102 CXX_OPERATOR_ATTRIBUTE
103 void *operator new(size_t size, std::align_val_t align)
104 { OPERATOR_NEW_BODY_ALIGN(FROM_NEW, false /*nothrow*/); }
105 CXX_OPERATOR_ATTRIBUTE
106 void *operator new[](size_t size, std::align_val_t align)
107 { OPERATOR_NEW_BODY_ALIGN(FROM_NEW_BR, false /*nothrow*/); }
108 CXX_OPERATOR_ATTRIBUTE
109 void *operator new(size_t size, std::align_val_t align, std::nothrow_t const&)
110 { OPERATOR_NEW_BODY_ALIGN(FROM_NEW, true /*nothrow*/); }
111 CXX_OPERATOR_ATTRIBUTE
112 void *operator new[](size_t size, std::align_val_t align, std::nothrow_t const&)
113 { OPERATOR_NEW_BODY_ALIGN(FROM_NEW_BR, true /*nothrow*/); }
114
115 #else  // SANITIZER_MAC
116 INTERCEPTOR(void *, _Znwm, size_t size) {
117   OPERATOR_NEW_BODY(FROM_NEW, false /*nothrow*/);
118 }
119 INTERCEPTOR(void *, _Znam, size_t size) {
120   OPERATOR_NEW_BODY(FROM_NEW_BR, false /*nothrow*/);
121 }
122 INTERCEPTOR(void *, _ZnwmRKSt9nothrow_t, size_t size, std::nothrow_t const&) {
123   OPERATOR_NEW_BODY(FROM_NEW, true /*nothrow*/);
124 }
125 INTERCEPTOR(void *, _ZnamRKSt9nothrow_t, size_t size, std::nothrow_t const&) {
126   OPERATOR_NEW_BODY(FROM_NEW_BR, true /*nothrow*/);
127 }
128 #endif  // !SANITIZER_MAC
129
130 #define OPERATOR_DELETE_BODY(type) \
131   GET_STACK_TRACE_FREE;\
132   asan_delete(ptr, 0, 0, &stack, type);
133
134 #define OPERATOR_DELETE_BODY_SIZE(type) \
135   GET_STACK_TRACE_FREE;\
136   asan_delete(ptr, size, 0, &stack, type);
137
138 #define OPERATOR_DELETE_BODY_ALIGN(type) \
139   GET_STACK_TRACE_FREE;\
140   asan_delete(ptr, 0, static_cast<uptr>(align), &stack, type);
141
142 #define OPERATOR_DELETE_BODY_SIZE_ALIGN(type) \
143   GET_STACK_TRACE_FREE;\
144   asan_delete(ptr, size, static_cast<uptr>(align), &stack, type);
145
146 #if !SANITIZER_MAC
147 CXX_OPERATOR_ATTRIBUTE
148 void operator delete(void *ptr) NOEXCEPT
149 { OPERATOR_DELETE_BODY(FROM_NEW); }
150 CXX_OPERATOR_ATTRIBUTE
151 void operator delete[](void *ptr) NOEXCEPT
152 { OPERATOR_DELETE_BODY(FROM_NEW_BR); }
153 CXX_OPERATOR_ATTRIBUTE
154 void operator delete(void *ptr, std::nothrow_t const&)
155 { OPERATOR_DELETE_BODY(FROM_NEW); }
156 CXX_OPERATOR_ATTRIBUTE
157 void operator delete[](void *ptr, std::nothrow_t const&)
158 { OPERATOR_DELETE_BODY(FROM_NEW_BR); }
159 CXX_OPERATOR_ATTRIBUTE
160 void operator delete(void *ptr, size_t size) NOEXCEPT
161 { OPERATOR_DELETE_BODY_SIZE(FROM_NEW); }
162 CXX_OPERATOR_ATTRIBUTE
163 void operator delete[](void *ptr, size_t size) NOEXCEPT
164 { OPERATOR_DELETE_BODY_SIZE(FROM_NEW_BR); }
165 CXX_OPERATOR_ATTRIBUTE
166 void operator delete(void *ptr, std::align_val_t align) NOEXCEPT
167 { OPERATOR_DELETE_BODY_ALIGN(FROM_NEW); }
168 CXX_OPERATOR_ATTRIBUTE
169 void operator delete[](void *ptr, std::align_val_t align) NOEXCEPT
170 { OPERATOR_DELETE_BODY_ALIGN(FROM_NEW_BR); }
171 CXX_OPERATOR_ATTRIBUTE
172 void operator delete(void *ptr, std::align_val_t align, std::nothrow_t const&)
173 { OPERATOR_DELETE_BODY_ALIGN(FROM_NEW); }
174 CXX_OPERATOR_ATTRIBUTE
175 void operator delete[](void *ptr, std::align_val_t align, std::nothrow_t const&)
176 { OPERATOR_DELETE_BODY_ALIGN(FROM_NEW_BR); }
177 CXX_OPERATOR_ATTRIBUTE
178 void operator delete(void *ptr, size_t size, std::align_val_t align) NOEXCEPT
179 { OPERATOR_DELETE_BODY_SIZE_ALIGN(FROM_NEW); }
180 CXX_OPERATOR_ATTRIBUTE
181 void operator delete[](void *ptr, size_t size, std::align_val_t align) NOEXCEPT
182 { OPERATOR_DELETE_BODY_SIZE_ALIGN(FROM_NEW_BR); }
183
184 #else  // SANITIZER_MAC
185 INTERCEPTOR(void, _ZdlPv, void *ptr)
186 { OPERATOR_DELETE_BODY(FROM_NEW); }
187 INTERCEPTOR(void, _ZdaPv, void *ptr)
188 { OPERATOR_DELETE_BODY(FROM_NEW_BR); }
189 INTERCEPTOR(void, _ZdlPvRKSt9nothrow_t, void *ptr, std::nothrow_t const&)
190 { OPERATOR_DELETE_BODY(FROM_NEW); }
191 INTERCEPTOR(void, _ZdaPvRKSt9nothrow_t, void *ptr, std::nothrow_t const&)
192 { OPERATOR_DELETE_BODY(FROM_NEW_BR); }
193 #endif  // !SANITIZER_MAC