1 //===-- asan_malloc_win.cc ------------------------------------------------===//
3 // This file is distributed under the University of Illinois Open Source
4 // License. See LICENSE.TXT for details.
6 //===----------------------------------------------------------------------===//
8 // This file is a part of AddressSanitizer, an address sanity checker.
10 // Windows-specific malloc interception.
11 //===----------------------------------------------------------------------===//
13 #include "sanitizer_common/sanitizer_platform.h"
16 #include "asan_allocator.h"
17 #include "asan_interceptors.h"
18 #include "asan_internal.h"
19 #include "asan_stack.h"
20 #include "sanitizer_common/sanitizer_interception.h"
24 // ---------------------- Replacement functions ---------------- {{{1
25 using namespace __asan
; // NOLINT
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.
33 SANITIZER_INTERFACE_ATTRIBUTE
34 void free(void *ptr
) {
36 return asan_free(ptr
, &stack
, FROM_MALLOC
);
39 SANITIZER_INTERFACE_ATTRIBUTE
40 void _free_dbg(void* ptr
, int) {
44 void cfree(void *ptr
) {
45 CHECK(!"cfree() should not be used on Windows?");
48 SANITIZER_INTERFACE_ATTRIBUTE
49 void *malloc(size_t size
) {
50 GET_STACK_TRACE_MALLOC
;
51 return asan_malloc(size
, &stack
);
54 SANITIZER_INTERFACE_ATTRIBUTE
55 void* _malloc_dbg(size_t size
, int , const char*, int) {
59 SANITIZER_INTERFACE_ATTRIBUTE
60 void *calloc(size_t nmemb
, size_t size
) {
61 GET_STACK_TRACE_MALLOC
;
62 return asan_calloc(nmemb
, size
, &stack
);
65 SANITIZER_INTERFACE_ATTRIBUTE
66 void* _calloc_dbg(size_t n
, size_t size
, int, const char*, int) {
67 return calloc(n
, size
);
70 SANITIZER_INTERFACE_ATTRIBUTE
71 void *_calloc_impl(size_t nmemb
, size_t size
, int *errno_tmp
) {
72 return calloc(nmemb
, size
);
75 SANITIZER_INTERFACE_ATTRIBUTE
76 void *realloc(void *ptr
, size_t size
) {
77 GET_STACK_TRACE_MALLOC
;
78 return asan_realloc(ptr
, size
, &stack
);
81 SANITIZER_INTERFACE_ATTRIBUTE
82 void *_realloc_dbg(void *ptr
, size_t size
, int) {
83 CHECK(!"_realloc_dbg should not exist!");
87 SANITIZER_INTERFACE_ATTRIBUTE
88 void* _recalloc(void* p
, size_t n
, size_t elem_size
) {
90 return calloc(n
, elem_size
);
91 const size_t size
= n
* elem_size
;
92 if (elem_size
!= 0 && size
/ elem_size
!= n
)
94 return realloc(p
, size
);
97 SANITIZER_INTERFACE_ATTRIBUTE
98 size_t _msize(void *ptr
) {
101 return asan_malloc_usable_size(ptr
, pc
, bp
);
104 SANITIZER_INTERFACE_ATTRIBUTE
105 void *_expand(void *memblock
, size_t size
) {
106 // _expand is used in realloc-like functions to resize the buffer if possible.
107 // We don't want memory to stand still while resizing buffers, so return 0.
111 SANITIZER_INTERFACE_ATTRIBUTE
112 void *_expand_dbg(void *memblock
, size_t size
) {
116 // TODO(timurrrr): Might want to add support for _aligned_* allocation
117 // functions to detect a bit more bugs. Those functions seem to wrap malloc().
119 int _CrtDbgReport(int, const char*, int,
120 const char*, const char*, ...) {
124 int _CrtDbgReportW(int reportType
, const wchar_t*, int,
125 const wchar_t*, const wchar_t*, ...) {
129 int _CrtSetReportMode(int, int) {
134 using __interception::GetRealFunctionAddress
;
136 // We don't want to include "windows.h" in this file to avoid extra attributes
137 // set on malloc/free etc (e.g. dllimport), so declare a few things manually:
138 extern "C" int __stdcall
VirtualProtect(void* addr
, size_t size
,
139 DWORD prot
, DWORD
*old_prot
);
140 const int PAGE_EXECUTE_READWRITE
= 0x40;
143 void ReplaceSystemMalloc() {
146 # error ReplaceSystemMalloc was not tested on x64
149 if (GetRealFunctionAddress("malloc", (void**)&crt_malloc
)) {
150 // Replace malloc in the CRT dll with a jump to our malloc.
151 DWORD old_prot
, unused
;
152 CHECK(VirtualProtect(crt_malloc
, 16, PAGE_EXECUTE_READWRITE
, &old_prot
));
153 REAL(memset
)(crt_malloc
, 0xCC /* int 3 */, 16); // just in case.
155 ptrdiff_t jmp_offset
= (char*)malloc
- (char*)crt_malloc
- 5;
156 crt_malloc
[0] = 0xE9; // jmp, should be followed by an offset.
157 REAL(memcpy
)(crt_malloc
+ 1, &jmp_offset
, sizeof(jmp_offset
));
159 CHECK(VirtualProtect(crt_malloc
, 16, old_prot
, &unused
));
161 // FYI: FlushInstructionCache is needed on Itanium etc but not on x86/x64.
164 // FIXME: investigate whether anything else is needed.
167 } // namespace __asan