1 //===-- asan_malloc_win.cc ------------------------------------------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file is a part of AddressSanitizer, an address sanity checker.
12 // Windows-specific malloc interception.
13 //===----------------------------------------------------------------------===//
15 #include "sanitizer_common/sanitizer_platform.h"
17 #define WIN32_LEAN_AND_MEAN
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"
28 using namespace __asan; // NOLINT
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.
36 # define ALLOCATION_FUNCTION_ATTRIBUTE
38 # define ALLOCATION_FUNCTION_ATTRIBUTE SANITIZER_INTERFACE_ATTRIBUTE
42 ALLOCATION_FUNCTION_ATTRIBUTE
43 void free(void *ptr) {
45 return asan_free(ptr, &stack, FROM_MALLOC);
48 ALLOCATION_FUNCTION_ATTRIBUTE
49 void _free_dbg(void *ptr, int) {
53 ALLOCATION_FUNCTION_ATTRIBUTE
54 void _free_base(void *ptr) {
58 ALLOCATION_FUNCTION_ATTRIBUTE
59 void cfree(void *ptr) {
60 CHECK(!"cfree() should not be used on Windows");
63 ALLOCATION_FUNCTION_ATTRIBUTE
64 void *malloc(size_t size) {
65 GET_STACK_TRACE_MALLOC;
66 return asan_malloc(size, &stack);
69 ALLOCATION_FUNCTION_ATTRIBUTE
70 void *_malloc_base(size_t size) {
74 ALLOCATION_FUNCTION_ATTRIBUTE
75 void *_malloc_dbg(size_t size, int, const char *, int) {
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);
85 ALLOCATION_FUNCTION_ATTRIBUTE
86 void *_calloc_base(size_t nmemb, size_t size) {
87 return calloc(nmemb, size);
90 ALLOCATION_FUNCTION_ATTRIBUTE
91 void *_calloc_dbg(size_t nmemb, size_t size, int, const char *, int) {
92 return calloc(nmemb, size);
95 ALLOCATION_FUNCTION_ATTRIBUTE
96 void *_calloc_impl(size_t nmemb, size_t size, int *errno_tmp) {
97 return calloc(nmemb, size);
100 ALLOCATION_FUNCTION_ATTRIBUTE
101 void *realloc(void *ptr, size_t size) {
102 GET_STACK_TRACE_MALLOC;
103 return asan_realloc(ptr, size, &stack);
106 ALLOCATION_FUNCTION_ATTRIBUTE
107 void *_realloc_dbg(void *ptr, size_t size, int) {
108 CHECK(!"_realloc_dbg should not exist!");
112 ALLOCATION_FUNCTION_ATTRIBUTE
113 void *_realloc_base(void *ptr, size_t size) {
114 return realloc(ptr, size);
117 ALLOCATION_FUNCTION_ATTRIBUTE
118 void *_recalloc(void *p, size_t n, size_t elem_size) {
120 return calloc(n, elem_size);
121 const size_t size = n * elem_size;
122 if (elem_size != 0 && size / elem_size != n)
124 return realloc(p, size);
127 ALLOCATION_FUNCTION_ATTRIBUTE
128 void *_recalloc_base(void *p, size_t n, size_t elem_size) {
129 return _recalloc(p, n, elem_size);
132 ALLOCATION_FUNCTION_ATTRIBUTE
133 size_t _msize(const void *ptr) {
134 GET_CURRENT_PC_BP_SP;
136 return asan_malloc_usable_size(ptr, pc, bp);
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.
146 ALLOCATION_FUNCTION_ATTRIBUTE
147 void *_expand_dbg(void *memblock, size_t size) {
148 return _expand(memblock, size);
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().
154 int _CrtDbgReport(int, const char*, int,
155 const char*, const char*, ...) {
159 int _CrtDbgReportW(int reportType, const wchar_t*, int,
160 const wchar_t*, const wchar_t*, ...) {
164 int _CrtSetReportMode(int, int) {
169 INTERCEPTOR_WINAPI(LPVOID, HeapAlloc, HANDLE hHeap, DWORD dwFlags,
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));
179 CHECK(dwFlags == 0 && "unsupported heap flags");
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);
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)
196 CHECK(dwFlags == 0 && "unsupported heap flags");
197 return asan_realloc(lpMem, dwBytes, &stack);
200 INTERCEPTOR_WINAPI(SIZE_T, HeapSize, HANDLE hHeap, DWORD dwFlags,
202 CHECK(dwFlags == 0 && "unsupported heap flags");
203 GET_CURRENT_PC_BP_SP;
205 return asan_malloc_usable_size(lpMem, pc, bp);
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);
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);
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
255 } // namespace __asan