2014-09-15 Andreas Krebbel <Andreas.Krebbel@de.ibm.com>
[official-gcc.git] / libsanitizer / asan / asan_malloc_win.cc
blob8463d5ef2e94ef8a40d21da6d54cbdd3881194a7
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 //===----------------------------------------------------------------------===//
13 #include "sanitizer_common/sanitizer_platform.h"
14 #if SANITIZER_WINDOWS
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"
22 #include <stddef.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.
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);
39 SANITIZER_INTERFACE_ATTRIBUTE
40 void _free_dbg(void* ptr, int) {
41 free(ptr);
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) {
56 return malloc(size);
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!");
84 return 0;
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);
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);
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;
111 SANITIZER_INTERFACE_ATTRIBUTE
112 void *_expand_dbg(void *memblock, size_t size) {
113 return 0;
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*, ...) {
121 ShowStatsAndAbort();
124 int _CrtDbgReportW(int reportType, const wchar_t*, int,
125 const wchar_t*, const wchar_t*, ...) {
126 ShowStatsAndAbort();
129 int _CrtSetReportMode(int, int) {
130 return 0;
132 } // extern "C"
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;
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.
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.
165 #endif
167 } // namespace __asan
169 #endif // _WIN32