2012-10-29 Wei Mi <wmi@google.com>
[official-gcc.git] / libasan / asan_malloc_win.cc
blob7389a2572487e50570f1aebedd1bbc370c38ed84
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
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"
20 #include <stddef.h>
22 // ---------------------- Replacement functions ---------------- {{{1
23 using namespace __asan; // NOLINT
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.
30 extern "C" {
31 void free(void *ptr) {
32 GET_STACK_TRACE_HERE_FOR_FREE(ptr);
33 return asan_free(ptr, &stack);
36 void _free_dbg(void* ptr, int) {
37 free(ptr);
40 void cfree(void *ptr) {
41 CHECK(!"cfree() should not be used on Windows?");
44 void *malloc(size_t size) {
45 GET_STACK_TRACE_HERE_FOR_MALLOC;
46 return asan_malloc(size, &stack);
49 void* _malloc_dbg(size_t size, int , const char*, int) {
50 return malloc(size);
53 void *calloc(size_t nmemb, size_t size) {
54 GET_STACK_TRACE_HERE_FOR_MALLOC;
55 return asan_calloc(nmemb, size, &stack);
58 void* _calloc_dbg(size_t n, size_t size, int, const char*, int) {
59 return calloc(n, size);
62 void *_calloc_impl(size_t nmemb, size_t size, int *errno_tmp) {
63 return calloc(nmemb, size);
66 void *realloc(void *ptr, size_t size) {
67 GET_STACK_TRACE_HERE_FOR_MALLOC;
68 return asan_realloc(ptr, size, &stack);
71 void *_realloc_dbg(void *ptr, size_t size, int) {
72 CHECK(!"_realloc_dbg should not exist!");
73 return 0;
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);
85 size_t _msize(void *ptr) {
86 GET_STACK_TRACE_HERE_FOR_MALLOC;
87 return asan_malloc_usable_size(ptr, &stack);
90 int _CrtDbgReport(int, const char*, int,
91 const char*, const char*, ...) {
92 ShowStatsAndAbort();
95 int _CrtDbgReportW(int reportType, const wchar_t*, int,
96 const wchar_t*, const wchar_t*, ...) {
97 ShowStatsAndAbort();
100 int _CrtSetReportMode(int, int) {
101 return 0;
103 } // extern "C"
105 using __interception::GetRealFunctionAddress;
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;
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.
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));
130 CHECK(VirtualProtect(crt_malloc, 16, old_prot, &unused));
132 // FYI: FlushInstructionCache is needed on Itanium etc but not on x86/x64.
135 // FIXME: investigate whether anything else is needed.
136 #endif
138 } // namespace __asan
140 #endif // _WIN32