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