llvm-project
187 строк · 7.6 Кб
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
30COMMENT_EXPORT("??2@YAPEAX_K@Z") // operator new
31COMMENT_EXPORT("??2@YAPEAX_KAEBUnothrow_t@std@@@Z") // operator new nothrow
32COMMENT_EXPORT("??3@YAXPEAX@Z") // operator delete
33COMMENT_EXPORT("??3@YAXPEAX_K@Z") // sized operator delete
34COMMENT_EXPORT("??_U@YAPEAX_K@Z") // operator new[]
35COMMENT_EXPORT("??_V@YAXPEAX@Z") // operator delete[]
36#else
37COMMENT_EXPORT("??2@YAPAXI@Z") // operator new
38COMMENT_EXPORT("??2@YAPAXIABUnothrow_t@std@@@Z") // operator new nothrow
39COMMENT_EXPORT("??3@YAXPAX@Z") // operator delete
40COMMENT_EXPORT("??3@YAXPAXI@Z") // sized operator delete
41COMMENT_EXPORT("??_U@YAPAXI@Z") // operator new[]
42COMMENT_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
49using 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>.
55namespace std {
56struct nothrow_t {};
57enum 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(type, nothrow) \
64GET_STACK_TRACE_MALLOC; \
65void *res = asan_memalign(0, size, &stack, type); \
66if (!nothrow && UNLIKELY(!res)) \
67ReportOutOfMemory(size, &stack); \
68return res;
69#define OPERATOR_NEW_BODY_ALIGN(type, nothrow) \
70GET_STACK_TRACE_MALLOC; \
71void *res = asan_memalign((uptr)align, size, &stack, type); \
72if (!nothrow && UNLIKELY(!res)) \
73ReportOutOfMemory(size, &stack); \
74return res;
75
76// On OS X it's not enough to just provide our own 'operator new' and
77// 'operator delete' implementations, because they're going to be in the
78// runtime dylib, and the main executable will depend on both the runtime
79// dylib and libstdc++, each of those'll have its implementation of new and
80// delete.
81// To make sure that C++ allocation/deallocation operators are overridden on
82// OS X we need to intercept them using their mangled names.
83#if !SANITIZER_APPLE
84CXX_OPERATOR_ATTRIBUTE
85void *operator new(size_t size)
86{ OPERATOR_NEW_BODY(FROM_NEW, false /*nothrow*/); }
87CXX_OPERATOR_ATTRIBUTE
88void *operator new[](size_t size)
89{ OPERATOR_NEW_BODY(FROM_NEW_BR, false /*nothrow*/); }
90CXX_OPERATOR_ATTRIBUTE
91void *operator new(size_t size, std::nothrow_t const&)
92{ OPERATOR_NEW_BODY(FROM_NEW, true /*nothrow*/); }
93CXX_OPERATOR_ATTRIBUTE
94void *operator new[](size_t size, std::nothrow_t const&)
95{ OPERATOR_NEW_BODY(FROM_NEW_BR, true /*nothrow*/); }
96CXX_OPERATOR_ATTRIBUTE
97void *operator new(size_t size, std::align_val_t align)
98{ OPERATOR_NEW_BODY_ALIGN(FROM_NEW, false /*nothrow*/); }
99CXX_OPERATOR_ATTRIBUTE
100void *operator new[](size_t size, std::align_val_t align)
101{ OPERATOR_NEW_BODY_ALIGN(FROM_NEW_BR, false /*nothrow*/); }
102CXX_OPERATOR_ATTRIBUTE
103void *operator new(size_t size, std::align_val_t align, std::nothrow_t const&)
104{ OPERATOR_NEW_BODY_ALIGN(FROM_NEW, true /*nothrow*/); }
105CXX_OPERATOR_ATTRIBUTE
106void *operator new[](size_t size, std::align_val_t align, std::nothrow_t const&)
107{ OPERATOR_NEW_BODY_ALIGN(FROM_NEW_BR, true /*nothrow*/); }
108
109#else // SANITIZER_APPLE
110INTERCEPTOR(void *, _Znwm, size_t size) {
111OPERATOR_NEW_BODY(FROM_NEW, false /*nothrow*/);
112}
113INTERCEPTOR(void *, _Znam, size_t size) {
114OPERATOR_NEW_BODY(FROM_NEW_BR, false /*nothrow*/);
115}
116INTERCEPTOR(void *, _ZnwmRKSt9nothrow_t, size_t size, std::nothrow_t const&) {
117OPERATOR_NEW_BODY(FROM_NEW, true /*nothrow*/);
118}
119INTERCEPTOR(void *, _ZnamRKSt9nothrow_t, size_t size, std::nothrow_t const&) {
120OPERATOR_NEW_BODY(FROM_NEW_BR, true /*nothrow*/);
121}
122#endif // !SANITIZER_APPLE
123
124#define OPERATOR_DELETE_BODY(type) \
125GET_STACK_TRACE_FREE; \
126asan_delete(ptr, 0, 0, &stack, type);
127
128#define OPERATOR_DELETE_BODY_SIZE(type) \
129GET_STACK_TRACE_FREE; \
130asan_delete(ptr, size, 0, &stack, type);
131
132#define OPERATOR_DELETE_BODY_ALIGN(type) \
133GET_STACK_TRACE_FREE; \
134asan_delete(ptr, 0, static_cast<uptr>(align), &stack, type);
135
136#define OPERATOR_DELETE_BODY_SIZE_ALIGN(type) \
137GET_STACK_TRACE_FREE; \
138asan_delete(ptr, size, static_cast<uptr>(align), &stack, type);
139
140#if !SANITIZER_APPLE
141CXX_OPERATOR_ATTRIBUTE
142void operator delete(void *ptr) NOEXCEPT
143{ OPERATOR_DELETE_BODY(FROM_NEW); }
144CXX_OPERATOR_ATTRIBUTE
145void operator delete[](void *ptr) NOEXCEPT
146{ OPERATOR_DELETE_BODY(FROM_NEW_BR); }
147CXX_OPERATOR_ATTRIBUTE
148void operator delete(void *ptr, std::nothrow_t const&)
149{ OPERATOR_DELETE_BODY(FROM_NEW); }
150CXX_OPERATOR_ATTRIBUTE
151void operator delete[](void *ptr, std::nothrow_t const&)
152{ OPERATOR_DELETE_BODY(FROM_NEW_BR); }
153CXX_OPERATOR_ATTRIBUTE
154void operator delete(void *ptr, size_t size) NOEXCEPT
155{ OPERATOR_DELETE_BODY_SIZE(FROM_NEW); }
156CXX_OPERATOR_ATTRIBUTE
157void operator delete[](void *ptr, size_t size) NOEXCEPT
158{ OPERATOR_DELETE_BODY_SIZE(FROM_NEW_BR); }
159CXX_OPERATOR_ATTRIBUTE
160void operator delete(void *ptr, std::align_val_t align) NOEXCEPT
161{ OPERATOR_DELETE_BODY_ALIGN(FROM_NEW); }
162CXX_OPERATOR_ATTRIBUTE
163void operator delete[](void *ptr, std::align_val_t align) NOEXCEPT
164{ OPERATOR_DELETE_BODY_ALIGN(FROM_NEW_BR); }
165CXX_OPERATOR_ATTRIBUTE
166void operator delete(void *ptr, std::align_val_t align, std::nothrow_t const&)
167{ OPERATOR_DELETE_BODY_ALIGN(FROM_NEW); }
168CXX_OPERATOR_ATTRIBUTE
169void operator delete[](void *ptr, std::align_val_t align, std::nothrow_t const&)
170{ OPERATOR_DELETE_BODY_ALIGN(FROM_NEW_BR); }
171CXX_OPERATOR_ATTRIBUTE
172void operator delete(void *ptr, size_t size, std::align_val_t align) NOEXCEPT
173{ OPERATOR_DELETE_BODY_SIZE_ALIGN(FROM_NEW); }
174CXX_OPERATOR_ATTRIBUTE
175void operator delete[](void *ptr, size_t size, std::align_val_t align) NOEXCEPT
176{ OPERATOR_DELETE_BODY_SIZE_ALIGN(FROM_NEW_BR); }
177
178#else // SANITIZER_APPLE
179INTERCEPTOR(void, _ZdlPv, void *ptr)
180{ OPERATOR_DELETE_BODY(FROM_NEW); }
181INTERCEPTOR(void, _ZdaPv, void *ptr)
182{ OPERATOR_DELETE_BODY(FROM_NEW_BR); }
183INTERCEPTOR(void, _ZdlPvRKSt9nothrow_t, void *ptr, std::nothrow_t const&)
184{ OPERATOR_DELETE_BODY(FROM_NEW); }
185INTERCEPTOR(void, _ZdaPvRKSt9nothrow_t, void *ptr, std::nothrow_t const&)
186{ OPERATOR_DELETE_BODY(FROM_NEW_BR); }
187#endif // !SANITIZER_APPLE
188