blob: 73e4c825092c6ab27343c1dd9f8e8472a145af0a [file] [log] [blame]
Alexey Samsonove5f58952012-06-04 13:50:10 +00001//===-- asan_malloc_win.cc ------------------------------------------------===//
Kostya Serebryany938b1052012-02-06 17:56:38 +00002//
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//===----------------------------------------------------------------------===//
Evgeniy Stepanov24e13722013-03-19 14:33:38 +000014
15#include "sanitizer_common/sanitizer_platform.h"
Evgeniy Stepanov30e110e2013-03-19 14:54:17 +000016#if SANITIZER_WINDOWS
Kostya Serebryany938b1052012-02-06 17:56:38 +000017
18#include "asan_allocator.h"
19#include "asan_interceptors.h"
20#include "asan_internal.h"
21#include "asan_stack.h"
Timur Iskhodzhanovd9a88cc2012-02-29 11:43:03 +000022#include "interception/interception.h"
Kostya Serebryany938b1052012-02-06 17:56:38 +000023
Alexey Samsonovb46941a2012-09-24 11:43:40 +000024#include <stddef.h>
25
Kostya Serebryany938b1052012-02-06 17:56:38 +000026// ---------------------- Replacement functions ---------------- {{{1
27using namespace __asan; // NOLINT
28
29// FIXME: Simply defining functions with the same signature in *.obj
30// files overrides the standard functions in *.lib
31// This works well for simple helloworld-like tests but might need to be
32// revisited in the future.
33
34extern "C" {
Timur Iskhodzhanove2748412013-08-13 16:48:18 +000035SANITIZER_INTERFACE_ATTRIBUTE
Kostya Serebryany938b1052012-02-06 17:56:38 +000036void free(void *ptr) {
Kostya Serebryanya30c8f92012-12-13 09:34:23 +000037 GET_STACK_TRACE_FREE;
Kostya Serebryanyfe6d9162012-12-21 08:53:59 +000038 return asan_free(ptr, &stack, FROM_MALLOC);
Kostya Serebryany938b1052012-02-06 17:56:38 +000039}
40
Timur Iskhodzhanove2748412013-08-13 16:48:18 +000041SANITIZER_INTERFACE_ATTRIBUTE
Timur Iskhodzhanov1bbe2562012-03-23 11:33:02 +000042void _free_dbg(void* ptr, int) {
43 free(ptr);
44}
45
46void cfree(void *ptr) {
47 CHECK(!"cfree() should not be used on Windows?");
48}
49
Timur Iskhodzhanove2748412013-08-13 16:48:18 +000050SANITIZER_INTERFACE_ATTRIBUTE
Kostya Serebryany938b1052012-02-06 17:56:38 +000051void *malloc(size_t size) {
Kostya Serebryanya30c8f92012-12-13 09:34:23 +000052 GET_STACK_TRACE_MALLOC;
Kostya Serebryany938b1052012-02-06 17:56:38 +000053 return asan_malloc(size, &stack);
54}
55
Timur Iskhodzhanove2748412013-08-13 16:48:18 +000056SANITIZER_INTERFACE_ATTRIBUTE
Timur Iskhodzhanov1bbe2562012-03-23 11:33:02 +000057void* _malloc_dbg(size_t size, int , const char*, int) {
58 return malloc(size);
59}
60
Timur Iskhodzhanove2748412013-08-13 16:48:18 +000061SANITIZER_INTERFACE_ATTRIBUTE
Kostya Serebryany938b1052012-02-06 17:56:38 +000062void *calloc(size_t nmemb, size_t size) {
Kostya Serebryanya30c8f92012-12-13 09:34:23 +000063 GET_STACK_TRACE_MALLOC;
Kostya Serebryany938b1052012-02-06 17:56:38 +000064 return asan_calloc(nmemb, size, &stack);
65}
66
Timur Iskhodzhanove2748412013-08-13 16:48:18 +000067SANITIZER_INTERFACE_ATTRIBUTE
Timur Iskhodzhanov1bbe2562012-03-23 11:33:02 +000068void* _calloc_dbg(size_t n, size_t size, int, const char*, int) {
69 return calloc(n, size);
70}
71
Timur Iskhodzhanove2748412013-08-13 16:48:18 +000072SANITIZER_INTERFACE_ATTRIBUTE
Timur Iskhodzhanov2716a612012-03-12 11:45:09 +000073void *_calloc_impl(size_t nmemb, size_t size, int *errno_tmp) {
Timur Iskhodzhanov1bbe2562012-03-23 11:33:02 +000074 return calloc(nmemb, size);
Timur Iskhodzhanov2716a612012-03-12 11:45:09 +000075}
76
Timur Iskhodzhanove2748412013-08-13 16:48:18 +000077SANITIZER_INTERFACE_ATTRIBUTE
Kostya Serebryany938b1052012-02-06 17:56:38 +000078void *realloc(void *ptr, size_t size) {
Kostya Serebryanya30c8f92012-12-13 09:34:23 +000079 GET_STACK_TRACE_MALLOC;
Kostya Serebryany938b1052012-02-06 17:56:38 +000080 return asan_realloc(ptr, size, &stack);
81}
Timur Iskhodzhanovcf13eb22012-03-07 11:19:26 +000082
Timur Iskhodzhanove2748412013-08-13 16:48:18 +000083SANITIZER_INTERFACE_ATTRIBUTE
Timur Iskhodzhanov1bbe2562012-03-23 11:33:02 +000084void *_realloc_dbg(void *ptr, size_t size, int) {
85 CHECK(!"_realloc_dbg should not exist!");
Kostya Serebryany3f4c3872012-05-31 14:35:53 +000086 return 0;
Timur Iskhodzhanov1bbe2562012-03-23 11:33:02 +000087}
88
Timur Iskhodzhanove2748412013-08-13 16:48:18 +000089SANITIZER_INTERFACE_ATTRIBUTE
Timur Iskhodzhanov1bbe2562012-03-23 11:33:02 +000090void* _recalloc(void* p, size_t n, size_t elem_size) {
91 if (!p)
92 return calloc(n, elem_size);
93 const size_t size = n * elem_size;
94 if (elem_size != 0 && size / elem_size != n)
Kostya Serebryany3f4c3872012-05-31 14:35:53 +000095 return 0;
Timur Iskhodzhanov1bbe2562012-03-23 11:33:02 +000096 return realloc(p, size);
97}
98
Timur Iskhodzhanove2748412013-08-13 16:48:18 +000099SANITIZER_INTERFACE_ATTRIBUTE
Timur Iskhodzhanovcf13eb22012-03-07 11:19:26 +0000100size_t _msize(void *ptr) {
Alexey Samsonov1b17f5b2013-11-13 14:46:58 +0000101 GET_CURRENT_PC_BP_SP;
102 (void)sp;
103 return asan_malloc_usable_size(ptr, pc, bp);
Timur Iskhodzhanovcf13eb22012-03-07 11:19:26 +0000104}
Timur Iskhodzhanov1bbe2562012-03-23 11:33:02 +0000105
106int _CrtDbgReport(int, const char*, int,
107 const char*, const char*, ...) {
108 ShowStatsAndAbort();
109}
110
111int _CrtDbgReportW(int reportType, const wchar_t*, int,
112 const wchar_t*, const wchar_t*, ...) {
113 ShowStatsAndAbort();
114}
115
116int _CrtSetReportMode(int, int) {
117 return 0;
118}
Kostya Serebryany938b1052012-02-06 17:56:38 +0000119} // extern "C"
120
Timur Iskhodzhanovd9a88cc2012-02-29 11:43:03 +0000121using __interception::GetRealFunctionAddress;
122
123// We don't want to include "windows.h" in this file to avoid extra attributes
124// set on malloc/free etc (e.g. dllimport), so declare a few things manually:
125extern "C" int __stdcall VirtualProtect(void* addr, size_t size,
126 DWORD prot, DWORD *old_prot);
127const int PAGE_EXECUTE_READWRITE = 0x40;
128
129namespace __asan {
130void ReplaceSystemMalloc() {
Timur Iskhodzhanov2716a612012-03-12 11:45:09 +0000131#if defined(_DLL)
132# ifdef _WIN64
133# error ReplaceSystemMalloc was not tested on x64
134# endif
Timur Iskhodzhanovd9a88cc2012-02-29 11:43:03 +0000135 char *crt_malloc;
136 if (GetRealFunctionAddress("malloc", (void**)&crt_malloc)) {
137 // Replace malloc in the CRT dll with a jump to our malloc.
138 DWORD old_prot, unused;
139 CHECK(VirtualProtect(crt_malloc, 16, PAGE_EXECUTE_READWRITE, &old_prot));
140 REAL(memset)(crt_malloc, 0xCC /* int 3 */, 16); // just in case.
141
Timur Iskhodzhanov2716a612012-03-12 11:45:09 +0000142 ptrdiff_t jmp_offset = (char*)malloc - (char*)crt_malloc - 5;
Timur Iskhodzhanovd9a88cc2012-02-29 11:43:03 +0000143 crt_malloc[0] = 0xE9; // jmp, should be followed by an offset.
144 REAL(memcpy)(crt_malloc + 1, &jmp_offset, sizeof(jmp_offset));
145
146 CHECK(VirtualProtect(crt_malloc, 16, old_prot, &unused));
147
148 // FYI: FlushInstructionCache is needed on Itanium etc but not on x86/x64.
149 }
150
151 // FIXME: investigate whether anything else is needed.
Timur Iskhodzhanov2716a612012-03-12 11:45:09 +0000152#endif
Timur Iskhodzhanovd9a88cc2012-02-29 11:43:03 +0000153}
154} // namespace __asan
155
Kostya Serebryany938b1052012-02-06 17:56:38 +0000156#endif // _WIN32