1 | //===-- asan_interceptors.cpp ---------------------------------------------===// |
2 | // |
3 | // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
4 | // See https://llvm.org/LICENSE.txt for license information. |
5 | // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
6 | // |
7 | //===----------------------------------------------------------------------===// |
8 | // |
9 | // This file is a part of AddressSanitizer, an address sanity checker. |
10 | // |
11 | // Interceptors for operators new and delete. |
12 | //===----------------------------------------------------------------------===// |
13 | |
14 | #include <stddef.h> |
15 | |
16 | #include "asan_allocator.h" |
17 | #include "asan_internal.h" |
18 | #include "asan_report.h" |
19 | #include "asan_stack.h" |
20 | #include "interception/interception.h" |
21 | |
22 | // C++ operators can't have dllexport attributes on Windows. We export them |
23 | // anyway by passing extra -export flags to the linker, which is exactly that |
24 | // dllexport would normally do. We need to export them in order to make the |
25 | // VS2015 dynamic CRT (MD) work. |
26 | #if SANITIZER_WINDOWS && defined(_MSC_VER) |
27 | #define CXX_OPERATOR_ATTRIBUTE |
28 | #define COMMENT_EXPORT(sym) __pragma(comment(linker, "/export:" sym)) |
29 | #ifdef _WIN64 |
30 | COMMENT_EXPORT("??2@YAPEAX_K@Z" ) // operator new |
31 | COMMENT_EXPORT("??2@YAPEAX_KAEBUnothrow_t@std@@@Z" ) // operator new nothrow |
32 | COMMENT_EXPORT("??3@YAXPEAX@Z" ) // operator delete |
33 | COMMENT_EXPORT("??3@YAXPEAX_K@Z" ) // sized operator delete |
34 | COMMENT_EXPORT("??_U@YAPEAX_K@Z" ) // operator new[] |
35 | COMMENT_EXPORT("??_V@YAXPEAX@Z" ) // operator delete[] |
36 | #else |
37 | COMMENT_EXPORT("??2@YAPAXI@Z" ) // operator new |
38 | COMMENT_EXPORT("??2@YAPAXIABUnothrow_t@std@@@Z" ) // operator new nothrow |
39 | COMMENT_EXPORT("??3@YAXPAX@Z" ) // operator delete |
40 | COMMENT_EXPORT("??3@YAXPAXI@Z" ) // sized operator delete |
41 | COMMENT_EXPORT("??_U@YAPAXI@Z" ) // operator new[] |
42 | COMMENT_EXPORT("??_V@YAXPAX@Z" ) // operator delete[] |
43 | #endif |
44 | #undef COMMENT_EXPORT |
45 | #else |
46 | #define CXX_OPERATOR_ATTRIBUTE INTERCEPTOR_ATTRIBUTE SANITIZER_WEAK_ATTRIBUTE |
47 | #endif |
48 | |
49 | using namespace __asan; |
50 | |
51 | // This code has issues on OSX. |
52 | // See https://github.com/google/sanitizers/issues/131. |
53 | |
54 | // Fake std::nothrow_t and std::align_val_t to avoid including <new>. |
55 | namespace std { |
56 | struct nothrow_t {}; |
57 | enum class align_val_t: size_t {}; |
58 | } // namespace std |
59 | |
60 | // TODO(alekseyshl): throw std::bad_alloc instead of dying on OOM. |
61 | // For local pool allocation, align to SHADOW_GRANULARITY to match asan |
62 | // allocator behavior. |
63 | #define OPERATOR_NEW_BODY \ |
64 | GET_STACK_TRACE_MALLOC; \ |
65 | void *res = asan_memalign(0, size, &stack, FROM_NEW); \ |
66 | if (UNLIKELY(!res)) \ |
67 | ReportOutOfMemory(size, &stack); \ |
68 | return res |
69 | #define OPERATOR_NEW_BODY_NOTHROW \ |
70 | GET_STACK_TRACE_MALLOC; \ |
71 | return asan_memalign(0, size, &stack, FROM_NEW) |
72 | #define OPERATOR_NEW_BODY_ARRAY \ |
73 | GET_STACK_TRACE_MALLOC; \ |
74 | void *res = asan_memalign(0, size, &stack, FROM_NEW_BR); \ |
75 | if (UNLIKELY(!res)) \ |
76 | ReportOutOfMemory(size, &stack); \ |
77 | return res |
78 | #define OPERATOR_NEW_BODY_ARRAY_NOTHROW \ |
79 | GET_STACK_TRACE_MALLOC; \ |
80 | return asan_memalign(0, size, &stack, FROM_NEW_BR) |
81 | #define OPERATOR_NEW_BODY_ALIGN \ |
82 | GET_STACK_TRACE_MALLOC; \ |
83 | void *res = asan_memalign((uptr)align, size, &stack, FROM_NEW); \ |
84 | if (UNLIKELY(!res)) \ |
85 | ReportOutOfMemory(size, &stack); \ |
86 | return res |
87 | #define OPERATOR_NEW_BODY_ALIGN_NOTHROW \ |
88 | GET_STACK_TRACE_MALLOC; \ |
89 | return asan_memalign((uptr)align, size, &stack, FROM_NEW) |
90 | #define OPERATOR_NEW_BODY_ALIGN_ARRAY \ |
91 | GET_STACK_TRACE_MALLOC; \ |
92 | void *res = asan_memalign((uptr)align, size, &stack, FROM_NEW_BR); \ |
93 | if (UNLIKELY(!res)) \ |
94 | ReportOutOfMemory(size, &stack); \ |
95 | return res |
96 | #define OPERATOR_NEW_BODY_ALIGN_ARRAY_NOTHROW \ |
97 | GET_STACK_TRACE_MALLOC; \ |
98 | return asan_memalign((uptr)align, size, &stack, FROM_NEW_BR) |
99 | |
100 | // On OS X it's not enough to just provide our own 'operator new' and |
101 | // 'operator delete' implementations, because they're going to be in the |
102 | // runtime dylib, and the main executable will depend on both the runtime |
103 | // dylib and libstdc++, each of those'll have its implementation of new and |
104 | // delete. |
105 | // To make sure that C++ allocation/deallocation operators are overridden on |
106 | // OS X we need to intercept them using their mangled names. |
107 | #if !SANITIZER_APPLE |
108 | CXX_OPERATOR_ATTRIBUTE |
109 | void *operator new(size_t size) { OPERATOR_NEW_BODY; } |
110 | CXX_OPERATOR_ATTRIBUTE |
111 | void *operator new[](size_t size) { OPERATOR_NEW_BODY_ARRAY; } |
112 | CXX_OPERATOR_ATTRIBUTE |
113 | void *operator new(size_t size, std::nothrow_t const &) { |
114 | OPERATOR_NEW_BODY_NOTHROW; |
115 | } |
116 | CXX_OPERATOR_ATTRIBUTE |
117 | void *operator new[](size_t size, std::nothrow_t const &) { |
118 | OPERATOR_NEW_BODY_ARRAY_NOTHROW; |
119 | } |
120 | CXX_OPERATOR_ATTRIBUTE |
121 | void *operator new(size_t size, std::align_val_t align) { |
122 | OPERATOR_NEW_BODY_ALIGN; |
123 | } |
124 | CXX_OPERATOR_ATTRIBUTE |
125 | void *operator new[](size_t size, std::align_val_t align) { |
126 | OPERATOR_NEW_BODY_ALIGN_ARRAY; |
127 | } |
128 | CXX_OPERATOR_ATTRIBUTE |
129 | void *operator new(size_t size, std::align_val_t align, |
130 | std::nothrow_t const &) { |
131 | OPERATOR_NEW_BODY_ALIGN_NOTHROW; |
132 | } |
133 | CXX_OPERATOR_ATTRIBUTE |
134 | void *operator new[](size_t size, std::align_val_t align, |
135 | std::nothrow_t const &) { |
136 | OPERATOR_NEW_BODY_ALIGN_ARRAY_NOTHROW; |
137 | } |
138 | |
139 | #else // SANITIZER_APPLE |
140 | INTERCEPTOR(void *, _Znwm, size_t size) { OPERATOR_NEW_BODY; } |
141 | INTERCEPTOR(void *, _Znam, size_t size) { OPERATOR_NEW_BODY_ARRAY; } |
142 | INTERCEPTOR(void *, _ZnwmRKSt9nothrow_t, size_t size, std::nothrow_t const&) { |
143 | OPERATOR_NEW_BODY_NOTHROW; |
144 | } |
145 | INTERCEPTOR(void *, _ZnamRKSt9nothrow_t, size_t size, std::nothrow_t const&) { |
146 | OPERATOR_NEW_BODY_ARRAY_NOTHROW; |
147 | } |
148 | #endif // !SANITIZER_APPLE |
149 | |
150 | #define OPERATOR_DELETE_BODY \ |
151 | GET_STACK_TRACE_FREE; \ |
152 | asan_delete(ptr, 0, 0, &stack, FROM_NEW) |
153 | #define OPERATOR_DELETE_BODY_ARRAY \ |
154 | GET_STACK_TRACE_FREE; \ |
155 | asan_delete(ptr, 0, 0, &stack, FROM_NEW_BR) |
156 | #define OPERATOR_DELETE_BODY_ALIGN \ |
157 | GET_STACK_TRACE_FREE; \ |
158 | asan_delete(ptr, 0, static_cast<uptr>(align), &stack, FROM_NEW) |
159 | #define OPERATOR_DELETE_BODY_ALIGN_ARRAY \ |
160 | GET_STACK_TRACE_FREE; \ |
161 | asan_delete(ptr, 0, static_cast<uptr>(align), &stack, FROM_NEW_BR) |
162 | #define OPERATOR_DELETE_BODY_SIZE \ |
163 | GET_STACK_TRACE_FREE; \ |
164 | asan_delete(ptr, size, 0, &stack, FROM_NEW) |
165 | #define OPERATOR_DELETE_BODY_SIZE_ARRAY \ |
166 | GET_STACK_TRACE_FREE; \ |
167 | asan_delete(ptr, size, 0, &stack, FROM_NEW_BR) |
168 | #define OPERATOR_DELETE_BODY_SIZE_ALIGN \ |
169 | GET_STACK_TRACE_FREE; \ |
170 | asan_delete(ptr, size, static_cast<uptr>(align), &stack, FROM_NEW) |
171 | #define OPERATOR_DELETE_BODY_SIZE_ALIGN_ARRAY \ |
172 | GET_STACK_TRACE_FREE; \ |
173 | asan_delete(ptr, size, static_cast<uptr>(align), &stack, FROM_NEW_BR) |
174 | |
175 | #if !SANITIZER_APPLE |
176 | CXX_OPERATOR_ATTRIBUTE |
177 | void operator delete(void *ptr) NOEXCEPT { OPERATOR_DELETE_BODY; } |
178 | CXX_OPERATOR_ATTRIBUTE |
179 | void operator delete[](void *ptr) NOEXCEPT { OPERATOR_DELETE_BODY_ARRAY; } |
180 | CXX_OPERATOR_ATTRIBUTE |
181 | void operator delete(void *ptr, std::nothrow_t const &) { |
182 | OPERATOR_DELETE_BODY; |
183 | } |
184 | CXX_OPERATOR_ATTRIBUTE |
185 | void operator delete[](void *ptr, std::nothrow_t const &) { |
186 | OPERATOR_DELETE_BODY_ARRAY; |
187 | } |
188 | CXX_OPERATOR_ATTRIBUTE |
189 | void operator delete(void *ptr, size_t size) NOEXCEPT { |
190 | OPERATOR_DELETE_BODY_SIZE; |
191 | } |
192 | CXX_OPERATOR_ATTRIBUTE |
193 | void operator delete[](void *ptr, size_t size) NOEXCEPT { |
194 | OPERATOR_DELETE_BODY_SIZE_ARRAY; |
195 | } |
196 | CXX_OPERATOR_ATTRIBUTE |
197 | void operator delete(void *ptr, std::align_val_t align) NOEXCEPT { |
198 | OPERATOR_DELETE_BODY_ALIGN; |
199 | } |
200 | CXX_OPERATOR_ATTRIBUTE |
201 | void operator delete[](void *ptr, std::align_val_t align) NOEXCEPT { |
202 | OPERATOR_DELETE_BODY_ALIGN_ARRAY; |
203 | } |
204 | CXX_OPERATOR_ATTRIBUTE |
205 | void operator delete(void *ptr, std::align_val_t align, |
206 | std::nothrow_t const &) { |
207 | OPERATOR_DELETE_BODY_ALIGN; |
208 | } |
209 | CXX_OPERATOR_ATTRIBUTE |
210 | void operator delete[](void *ptr, std::align_val_t align, |
211 | std::nothrow_t const &) { |
212 | OPERATOR_DELETE_BODY_ALIGN_ARRAY; |
213 | } |
214 | CXX_OPERATOR_ATTRIBUTE |
215 | void operator delete(void *ptr, size_t size, std::align_val_t align) NOEXCEPT { |
216 | OPERATOR_DELETE_BODY_SIZE_ALIGN; |
217 | } |
218 | CXX_OPERATOR_ATTRIBUTE |
219 | void operator delete[](void *ptr, size_t size, |
220 | std::align_val_t align) NOEXCEPT { |
221 | OPERATOR_DELETE_BODY_SIZE_ALIGN_ARRAY; |
222 | } |
223 | |
224 | #else // SANITIZER_APPLE |
225 | INTERCEPTOR(void, _ZdlPv, void *ptr) { OPERATOR_DELETE_BODY; } |
226 | INTERCEPTOR(void, _ZdaPv, void *ptr) { OPERATOR_DELETE_BODY_ARRAY; } |
227 | INTERCEPTOR(void, _ZdlPvRKSt9nothrow_t, void *ptr, std::nothrow_t const &) { |
228 | OPERATOR_DELETE_BODY; |
229 | } |
230 | INTERCEPTOR(void, _ZdaPvRKSt9nothrow_t, void *ptr, std::nothrow_t const &) { |
231 | OPERATOR_DELETE_BODY_ARRAY; |
232 | } |
233 | #endif // !SANITIZER_APPLE |
234 | |