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