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