Remove a layer of indirection from hash_table
[gcc.git] / libsanitizer / asan / asan_malloc_win.cc
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 "sanitizer_common/sanitizer_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
34 void free(void *ptr) {
35 GET_STACK_TRACE_FREE;
36 return asan_free(ptr, &stack, FROM_MALLOC);
37 }
38
39 SANITIZER_INTERFACE_ATTRIBUTE
40 void _free_dbg(void* ptr, int) {
41 free(ptr);
42 }
43
44 void cfree(void *ptr) {
45 CHECK(!"cfree() should not be used on Windows?");
46 }
47
48 SANITIZER_INTERFACE_ATTRIBUTE
49 void *malloc(size_t size) {
50 GET_STACK_TRACE_MALLOC;
51 return asan_malloc(size, &stack);
52 }
53
54 SANITIZER_INTERFACE_ATTRIBUTE
55 void* _malloc_dbg(size_t size, int , const char*, int) {
56 return malloc(size);
57 }
58
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);
63 }
64
65 SANITIZER_INTERFACE_ATTRIBUTE
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
71 void *_calloc_impl(size_t nmemb, size_t size, int *errno_tmp) {
72 return calloc(nmemb, size);
73 }
74
75 SANITIZER_INTERFACE_ATTRIBUTE
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
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
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
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
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.
108 return 0;
109 }
110
111 SANITIZER_INTERFACE_ATTRIBUTE
112 void *_expand_dbg(void *memblock, size_t size) {
113 return 0;
114 }
115
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().
118
119 int _CrtDbgReport(int, const char*, int,
120 const char*, const char*, ...) {
121 ShowStatsAndAbort();
122 }
123
124 int _CrtDbgReportW(int reportType, const wchar_t*, int,
125 const wchar_t*, const wchar_t*, ...) {
126 ShowStatsAndAbort();
127 }
128
129 int _CrtSetReportMode(int, int) {
130 return 0;
131 }
132 } // extern "C"
133
134 using __interception::GetRealFunctionAddress;
135
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;
141
142 namespace __asan {
143 void ReplaceSystemMalloc() {
144 #if defined(_DLL)
145 # ifdef _WIN64
146 # error ReplaceSystemMalloc was not tested on x64
147 # endif
148 char *crt_malloc;
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.
154
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));
158
159 CHECK(VirtualProtect(crt_malloc, 16, old_prot, &unused));
160
161 // FYI: FlushInstructionCache is needed on Itanium etc but not on x86/x64.
162 }
163
164 // FIXME: investigate whether anything else is needed.
165 #endif
166 }
167 } // namespace __asan
168
169 #endif // _WIN32