1 //===-- asan_malloc_win.cc ------------------------------------------------===//
3 // This file is distributed under the University of Illinois Open Source
4 // License. See LICENSE.TXT for details.
6 //===----------------------------------------------------------------------===//
8 // This file is a part of AddressSanitizer, an address sanity checker.
10 // Windows-specific malloc interception.
11 //===----------------------------------------------------------------------===//
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"
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.
31 void free(void *ptr
) {
33 return asan_free(ptr
, &stack
, FROM_MALLOC
);
36 void _free_dbg(void* ptr
, int) {
40 void cfree(void *ptr
) {
41 CHECK(!"cfree() should not be used on Windows?");
44 void *malloc(size_t size
) {
45 GET_STACK_TRACE_MALLOC
;
46 return asan_malloc(size
, &stack
);
49 void* _malloc_dbg(size_t size
, int , const char*, int) {
53 void *calloc(size_t nmemb
, size_t size
) {
54 GET_STACK_TRACE_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_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!");
76 void* _recalloc(void* p
, size_t n
, size_t elem_size
) {
78 return calloc(n
, elem_size
);
79 const size_t size
= n
* elem_size
;
80 if (elem_size
!= 0 && size
/ elem_size
!= n
)
82 return realloc(p
, size
);
85 size_t _msize(void *ptr
) {
86 GET_STACK_TRACE_MALLOC
;
87 return asan_malloc_usable_size(ptr
, &stack
);
90 int _CrtDbgReport(int, const char*, int,
91 const char*, const char*, ...) {
95 int _CrtDbgReportW(int reportType
, const wchar_t*, int,
96 const wchar_t*, const wchar_t*, ...) {
100 int _CrtSetReportMode(int, int) {
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;
114 void ReplaceSystemMalloc() {
117 # error ReplaceSystemMalloc was not tested on x64
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.
138 } // namespace __asan