1 //===-- asan_malloc_win.cc ------------------------------------------------===//
2 //
3 // This file is distributed under the University of Illinois Open Source
4 // License. See LICENSE.TXT for details.
5 //
6 //===----------------------------------------------------------------------===//
7 //
8 // This file is a part of AddressSanitizer, an address sanity checker.
9 //
10 // Windows-specific malloc interception.
11 //===----------------------------------------------------------------------===//
12 
13 #include "sanitizer_common/sanitizer_platform.h"
14 #if SANITIZER_WINDOWS
15 
16 #include "asan_allocator.h"
17 #include "asan_interceptors.h"
18 #include "asan_internal.h"
19 #include "asan_stack.h"
20 #include "interception/interception.h"
21 
22 #include <stddef.h>
23 
24 // ---------------------- Replacement functions ---------------- {{{1
25 using namespace __asan;  // NOLINT
26 
27 // FIXME: Simply defining functions with the same signature in *.obj
28 // files overrides the standard functions in *.lib
29 // This works well for simple helloworld-like tests but might need to be
30 // revisited in the future.
31 
32 extern "C" {
33 SANITIZER_INTERFACE_ATTRIBUTE
free(void * ptr)34 void free(void *ptr) {
35   GET_STACK_TRACE_FREE;
36   return asan_free(ptr, &stack, FROM_MALLOC);
37 }
38 
39 SANITIZER_INTERFACE_ATTRIBUTE
_free_dbg(void * ptr,int)40 void _free_dbg(void* ptr, int) {
41   free(ptr);
42 }
43 
cfree(void * ptr)44 void cfree(void *ptr) {
45   CHECK(!"cfree() should not be used on Windows?");
46 }
47 
48 SANITIZER_INTERFACE_ATTRIBUTE
malloc(size_t size)49 void *malloc(size_t size) {
50   GET_STACK_TRACE_MALLOC;
51   return asan_malloc(size, &stack);
52 }
53 
54 SANITIZER_INTERFACE_ATTRIBUTE
_malloc_dbg(size_t size,int,const char *,int)55 void* _malloc_dbg(size_t size, int , const char*, int) {
56   return malloc(size);
57 }
58 
59 SANITIZER_INTERFACE_ATTRIBUTE
calloc(size_t nmemb,size_t size)60 void *calloc(size_t nmemb, size_t size) {
61   GET_STACK_TRACE_MALLOC;
62   return asan_calloc(nmemb, size, &stack);
63 }
64 
65 SANITIZER_INTERFACE_ATTRIBUTE
_calloc_dbg(size_t n,size_t size,int,const char *,int)66 void* _calloc_dbg(size_t n, size_t size, int, const char*, int) {
67   return calloc(n, size);
68 }
69 
70 SANITIZER_INTERFACE_ATTRIBUTE
_calloc_impl(size_t nmemb,size_t size,int * errno_tmp)71 void *_calloc_impl(size_t nmemb, size_t size, int *errno_tmp) {
72   return calloc(nmemb, size);
73 }
74 
75 SANITIZER_INTERFACE_ATTRIBUTE
realloc(void * ptr,size_t size)76 void *realloc(void *ptr, size_t size) {
77   GET_STACK_TRACE_MALLOC;
78   return asan_realloc(ptr, size, &stack);
79 }
80 
81 SANITIZER_INTERFACE_ATTRIBUTE
_realloc_dbg(void * ptr,size_t size,int)82 void *_realloc_dbg(void *ptr, size_t size, int) {
83   CHECK(!"_realloc_dbg should not exist!");
84   return 0;
85 }
86 
87 SANITIZER_INTERFACE_ATTRIBUTE
_recalloc(void * p,size_t n,size_t elem_size)88 void* _recalloc(void* p, size_t n, size_t elem_size) {
89   if (!p)
90     return calloc(n, elem_size);
91   const size_t size = n * elem_size;
92   if (elem_size != 0 && size / elem_size != n)
93     return 0;
94   return realloc(p, size);
95 }
96 
97 SANITIZER_INTERFACE_ATTRIBUTE
_msize(void * ptr)98 size_t _msize(void *ptr) {
99   GET_CURRENT_PC_BP_SP;
100   (void)sp;
101   return asan_malloc_usable_size(ptr, pc, bp);
102 }
103 
_CrtDbgReport(int,const char *,int,const char *,const char *,...)104 int _CrtDbgReport(int, const char*, int,
105                   const char*, const char*, ...) {
106   ShowStatsAndAbort();
107 }
108 
_CrtDbgReportW(int reportType,const wchar_t *,int,const wchar_t *,const wchar_t *,...)109 int _CrtDbgReportW(int reportType, const wchar_t*, int,
110                    const wchar_t*, const wchar_t*, ...) {
111   ShowStatsAndAbort();
112 }
113 
_CrtSetReportMode(int,int)114 int _CrtSetReportMode(int, int) {
115   return 0;
116 }
117 }  // extern "C"
118 
119 using __interception::GetRealFunctionAddress;
120 
121 // We don't want to include "windows.h" in this file to avoid extra attributes
122 // set on malloc/free etc (e.g. dllimport), so declare a few things manually:
123 extern "C" int __stdcall VirtualProtect(void* addr, size_t size,
124                                         DWORD prot, DWORD *old_prot);
125 const int PAGE_EXECUTE_READWRITE = 0x40;
126 
127 namespace __asan {
ReplaceSystemMalloc()128 void ReplaceSystemMalloc() {
129 #if defined(_DLL)
130 # ifdef _WIN64
131 #  error ReplaceSystemMalloc was not tested on x64
132 # endif
133   char *crt_malloc;
134   if (GetRealFunctionAddress("malloc", (void**)&crt_malloc)) {
135     // Replace malloc in the CRT dll with a jump to our malloc.
136     DWORD old_prot, unused;
137     CHECK(VirtualProtect(crt_malloc, 16, PAGE_EXECUTE_READWRITE, &old_prot));
138     REAL(memset)(crt_malloc, 0xCC /* int 3 */, 16);  // just in case.
139 
140     ptrdiff_t jmp_offset = (char*)malloc - (char*)crt_malloc - 5;
141     crt_malloc[0] = 0xE9;  // jmp, should be followed by an offset.
142     REAL(memcpy)(crt_malloc + 1, &jmp_offset, sizeof(jmp_offset));
143 
144     CHECK(VirtualProtect(crt_malloc, 16, old_prot, &unused));
145 
146     // FYI: FlushInstructionCache is needed on Itanium etc but not on x86/x64.
147   }
148 
149   // FIXME: investigate whether anything else is needed.
150 #endif
151 }
152 }  // namespace __asan
153 
154 #endif  // _WIN32
155