]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - contrib/compiler-rt/lib/asan/asan_malloc_win.cc
Merge compiler-rt r291274.
[FreeBSD/FreeBSD.git] / contrib / compiler-rt / lib / asan / asan_malloc_win.cc
1 //===-- asan_malloc_win.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 // Windows-specific malloc interception.
13 //===----------------------------------------------------------------------===//
14
15 #include "sanitizer_common/sanitizer_platform.h"
16 #if SANITIZER_WINDOWS
17 #define WIN32_LEAN_AND_MEAN
18 #include <windows.h>
19
20 #include "asan_allocator.h"
21 #include "asan_interceptors.h"
22 #include "asan_internal.h"
23 #include "asan_stack.h"
24 #include "interception/interception.h"
25
26 #include <stddef.h>
27
28 using namespace __asan;  // NOLINT
29
30 // MT: Simply defining functions with the same signature in *.obj
31 // files overrides the standard functions in the CRT.
32 // MD: Memory allocation functions are defined in the CRT .dll,
33 // so we have to intercept them before they are called for the first time.
34
35 #if ASAN_DYNAMIC
36 # define ALLOCATION_FUNCTION_ATTRIBUTE
37 #else
38 # define ALLOCATION_FUNCTION_ATTRIBUTE SANITIZER_INTERFACE_ATTRIBUTE
39 #endif
40
41 extern "C" {
42 ALLOCATION_FUNCTION_ATTRIBUTE
43 void free(void *ptr) {
44   GET_STACK_TRACE_FREE;
45   return asan_free(ptr, &stack, FROM_MALLOC);
46 }
47
48 ALLOCATION_FUNCTION_ATTRIBUTE
49 void _free_dbg(void *ptr, int) {
50   free(ptr);
51 }
52
53 ALLOCATION_FUNCTION_ATTRIBUTE
54 void _free_base(void *ptr) {
55   free(ptr);
56 }
57
58 ALLOCATION_FUNCTION_ATTRIBUTE
59 void cfree(void *ptr) {
60   CHECK(!"cfree() should not be used on Windows");
61 }
62
63 ALLOCATION_FUNCTION_ATTRIBUTE
64 void *malloc(size_t size) {
65   GET_STACK_TRACE_MALLOC;
66   return asan_malloc(size, &stack);
67 }
68
69 ALLOCATION_FUNCTION_ATTRIBUTE
70 void *_malloc_base(size_t size) {
71   return malloc(size);
72 }
73
74 ALLOCATION_FUNCTION_ATTRIBUTE
75 void *_malloc_dbg(size_t size, int, const char *, int) {
76   return malloc(size);
77 }
78
79 ALLOCATION_FUNCTION_ATTRIBUTE
80 void *calloc(size_t nmemb, size_t size) {
81   GET_STACK_TRACE_MALLOC;
82   return asan_calloc(nmemb, size, &stack);
83 }
84
85 ALLOCATION_FUNCTION_ATTRIBUTE
86 void *_calloc_base(size_t nmemb, size_t size) {
87   return calloc(nmemb, size);
88 }
89
90 ALLOCATION_FUNCTION_ATTRIBUTE
91 void *_calloc_dbg(size_t nmemb, size_t size, int, const char *, int) {
92   return calloc(nmemb, size);
93 }
94
95 ALLOCATION_FUNCTION_ATTRIBUTE
96 void *_calloc_impl(size_t nmemb, size_t size, int *errno_tmp) {
97   return calloc(nmemb, size);
98 }
99
100 ALLOCATION_FUNCTION_ATTRIBUTE
101 void *realloc(void *ptr, size_t size) {
102   GET_STACK_TRACE_MALLOC;
103   return asan_realloc(ptr, size, &stack);
104 }
105
106 ALLOCATION_FUNCTION_ATTRIBUTE
107 void *_realloc_dbg(void *ptr, size_t size, int) {
108   CHECK(!"_realloc_dbg should not exist!");
109   return 0;
110 }
111
112 ALLOCATION_FUNCTION_ATTRIBUTE
113 void *_realloc_base(void *ptr, size_t size) {
114   return realloc(ptr, size);
115 }
116
117 ALLOCATION_FUNCTION_ATTRIBUTE
118 void *_recalloc(void *p, size_t n, size_t elem_size) {
119   if (!p)
120     return calloc(n, elem_size);
121   const size_t size = n * elem_size;
122   if (elem_size != 0 && size / elem_size != n)
123     return 0;
124   return realloc(p, size);
125 }
126
127 ALLOCATION_FUNCTION_ATTRIBUTE
128 void *_recalloc_base(void *p, size_t n, size_t elem_size) {
129   return _recalloc(p, n, elem_size);
130 }
131
132 ALLOCATION_FUNCTION_ATTRIBUTE
133 size_t _msize(const void *ptr) {
134   GET_CURRENT_PC_BP_SP;
135   (void)sp;
136   return asan_malloc_usable_size(ptr, pc, bp);
137 }
138
139 ALLOCATION_FUNCTION_ATTRIBUTE
140 void *_expand(void *memblock, size_t size) {
141   // _expand is used in realloc-like functions to resize the buffer if possible.
142   // We don't want memory to stand still while resizing buffers, so return 0.
143   return 0;
144 }
145
146 ALLOCATION_FUNCTION_ATTRIBUTE
147 void *_expand_dbg(void *memblock, size_t size) {
148   return _expand(memblock, size);
149 }
150
151 // TODO(timurrrr): Might want to add support for _aligned_* allocation
152 // functions to detect a bit more bugs.  Those functions seem to wrap malloc().
153
154 int _CrtDbgReport(int, const char*, int,
155                   const char*, const char*, ...) {
156   ShowStatsAndAbort();
157 }
158
159 int _CrtDbgReportW(int reportType, const wchar_t*, int,
160                    const wchar_t*, const wchar_t*, ...) {
161   ShowStatsAndAbort();
162 }
163
164 int _CrtSetReportMode(int, int) {
165   return 0;
166 }
167 }  // extern "C"
168
169 INTERCEPTOR_WINAPI(LPVOID, HeapAlloc, HANDLE hHeap, DWORD dwFlags,
170                    SIZE_T dwBytes) {
171   GET_STACK_TRACE_MALLOC;
172   void *p = asan_malloc(dwBytes, &stack);
173   // Reading MSDN suggests that the *entire* usable allocation is zeroed out.
174   // Otherwise it is difficult to HeapReAlloc with HEAP_ZERO_MEMORY.
175   // https://blogs.msdn.microsoft.com/oldnewthing/20120316-00/?p=8083
176   if (dwFlags == HEAP_ZERO_MEMORY)
177     internal_memset(p, 0, asan_mz_size(p));
178   else
179     CHECK(dwFlags == 0 && "unsupported heap flags");
180   return p;
181 }
182
183 INTERCEPTOR_WINAPI(BOOL, HeapFree, HANDLE hHeap, DWORD dwFlags, LPVOID lpMem) {
184   CHECK(dwFlags == 0 && "unsupported heap flags");
185   GET_STACK_TRACE_FREE;
186   asan_free(lpMem, &stack, FROM_MALLOC);
187   return true;
188 }
189
190 INTERCEPTOR_WINAPI(LPVOID, HeapReAlloc, HANDLE hHeap, DWORD dwFlags,
191                    LPVOID lpMem, SIZE_T dwBytes) {
192   GET_STACK_TRACE_MALLOC;
193   // Realloc should never reallocate in place.
194   if (dwFlags & HEAP_REALLOC_IN_PLACE_ONLY)
195     return nullptr;
196   CHECK(dwFlags == 0 && "unsupported heap flags");
197   return asan_realloc(lpMem, dwBytes, &stack);
198 }
199
200 INTERCEPTOR_WINAPI(SIZE_T, HeapSize, HANDLE hHeap, DWORD dwFlags,
201                    LPCVOID lpMem) {
202   CHECK(dwFlags == 0 && "unsupported heap flags");
203   GET_CURRENT_PC_BP_SP;
204   (void)sp;
205   return asan_malloc_usable_size(lpMem, pc, bp);
206 }
207
208 namespace __asan {
209
210 static void TryToOverrideFunction(const char *fname, uptr new_func) {
211   // Failure here is not fatal. The CRT may not be present, and different CRT
212   // versions use different symbols.
213   if (!__interception::OverrideFunction(fname, new_func))
214     VPrintf(2, "Failed to override function %s\n", fname);
215 }
216
217 void ReplaceSystemMalloc() {
218 #if defined(ASAN_DYNAMIC)
219   TryToOverrideFunction("free", (uptr)free);
220   TryToOverrideFunction("_free_base", (uptr)free);
221   TryToOverrideFunction("malloc", (uptr)malloc);
222   TryToOverrideFunction("_malloc_base", (uptr)malloc);
223   TryToOverrideFunction("_malloc_crt", (uptr)malloc);
224   TryToOverrideFunction("calloc", (uptr)calloc);
225   TryToOverrideFunction("_calloc_base", (uptr)calloc);
226   TryToOverrideFunction("_calloc_crt", (uptr)calloc);
227   TryToOverrideFunction("realloc", (uptr)realloc);
228   TryToOverrideFunction("_realloc_base", (uptr)realloc);
229   TryToOverrideFunction("_realloc_crt", (uptr)realloc);
230   TryToOverrideFunction("_recalloc", (uptr)_recalloc);
231   TryToOverrideFunction("_recalloc_base", (uptr)_recalloc);
232   TryToOverrideFunction("_recalloc_crt", (uptr)_recalloc);
233   TryToOverrideFunction("_msize", (uptr)_msize);
234   TryToOverrideFunction("_expand", (uptr)_expand);
235   TryToOverrideFunction("_expand_base", (uptr)_expand);
236
237   // Recent versions of ucrtbase.dll appear to be built with PGO and LTCG, which
238   // enable cross-module inlining. This means our _malloc_base hook won't catch
239   // all CRT allocations. This code here patches the import table of
240   // ucrtbase.dll so that all attempts to use the lower-level win32 heap
241   // allocation API will be directed to ASan's heap. We don't currently
242   // intercept all calls to HeapAlloc. If we did, we would have to check on
243   // HeapFree whether the pointer came from ASan of from the system.
244 #define INTERCEPT_UCRT_FUNCTION(func)                                         \
245   if (!INTERCEPT_FUNCTION_DLLIMPORT("ucrtbase.dll",                           \
246                                     "api-ms-win-core-heap-l1-1-0.dll", func)) \
247     VPrintf(2, "Failed to intercept ucrtbase.dll import %s\n", #func);
248   INTERCEPT_UCRT_FUNCTION(HeapAlloc);
249   INTERCEPT_UCRT_FUNCTION(HeapFree);
250   INTERCEPT_UCRT_FUNCTION(HeapReAlloc);
251   INTERCEPT_UCRT_FUNCTION(HeapSize);
252 #undef INTERCEPT_UCRT_FUNCTION
253 #endif
254 }
255 }  // namespace __asan
256
257 #endif  // _WIN32