[CMake] Respect CMAKE_CXX_FLAGS in custom clang_compile commands
[blocksruntime.git] / lib / asan / asan_malloc_win.cc
blob62cd16361fa56f3fb3441eccfae633d96a791ce6
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.
12 // Windows-specific malloc interception.
13 //===----------------------------------------------------------------------===//
15 #include "sanitizer_common/sanitizer_platform.h"
16 #if SANITIZER_WINDOWS
18 #include "asan_allocator.h"
19 #include "asan_interceptors.h"
20 #include "asan_internal.h"
21 #include "asan_stack.h"
22 #include "sanitizer_common/sanitizer_interception.h"
24 #include <stddef.h>
26 // ---------------------- Replacement functions ---------------- {{{1
27 using namespace __asan; // NOLINT
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.
34 extern "C" {
35 SANITIZER_INTERFACE_ATTRIBUTE
36 void free(void *ptr) {
37 GET_STACK_TRACE_FREE;
38 return asan_free(ptr, &stack, FROM_MALLOC);
41 SANITIZER_INTERFACE_ATTRIBUTE
42 void _free_dbg(void* ptr, int) {
43 free(ptr);
46 void cfree(void *ptr) {
47 CHECK(!"cfree() should not be used on Windows?");
50 SANITIZER_INTERFACE_ATTRIBUTE
51 void *malloc(size_t size) {
52 GET_STACK_TRACE_MALLOC;
53 return asan_malloc(size, &stack);
56 SANITIZER_INTERFACE_ATTRIBUTE
57 void* _malloc_dbg(size_t size, int , const char*, int) {
58 return malloc(size);
61 SANITIZER_INTERFACE_ATTRIBUTE
62 void *calloc(size_t nmemb, size_t size) {
63 GET_STACK_TRACE_MALLOC;
64 return asan_calloc(nmemb, size, &stack);
67 SANITIZER_INTERFACE_ATTRIBUTE
68 void* _calloc_dbg(size_t n, size_t size, int, const char*, int) {
69 return calloc(n, size);
72 SANITIZER_INTERFACE_ATTRIBUTE
73 void *_calloc_impl(size_t nmemb, size_t size, int *errno_tmp) {
74 return calloc(nmemb, size);
77 SANITIZER_INTERFACE_ATTRIBUTE
78 void *realloc(void *ptr, size_t size) {
79 GET_STACK_TRACE_MALLOC;
80 return asan_realloc(ptr, size, &stack);
83 SANITIZER_INTERFACE_ATTRIBUTE
84 void *_realloc_dbg(void *ptr, size_t size, int) {
85 CHECK(!"_realloc_dbg should not exist!");
86 return 0;
89 SANITIZER_INTERFACE_ATTRIBUTE
90 void* _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)
95 return 0;
96 return realloc(p, size);
99 SANITIZER_INTERFACE_ATTRIBUTE
100 size_t _msize(void *ptr) {
101 GET_CURRENT_PC_BP_SP;
102 (void)sp;
103 return asan_malloc_usable_size(ptr, pc, bp);
106 int _CrtDbgReport(int, const char*, int,
107 const char*, const char*, ...) {
108 ShowStatsAndAbort();
111 int _CrtDbgReportW(int reportType, const wchar_t*, int,
112 const wchar_t*, const wchar_t*, ...) {
113 ShowStatsAndAbort();
116 int _CrtSetReportMode(int, int) {
117 return 0;
119 } // extern "C"
121 using __interception::GetRealFunctionAddress;
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:
125 extern "C" int __stdcall VirtualProtect(void* addr, size_t size,
126 DWORD prot, DWORD *old_prot);
127 const int PAGE_EXECUTE_READWRITE = 0x40;
129 namespace __asan {
130 void ReplaceSystemMalloc() {
131 #if defined(_DLL)
132 # ifdef _WIN64
133 # error ReplaceSystemMalloc was not tested on x64
134 # endif
135 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.
142 ptrdiff_t jmp_offset = (char*)malloc - (char*)crt_malloc - 5;
143 crt_malloc[0] = 0xE9; // jmp, should be followed by an offset.
144 REAL(memcpy)(crt_malloc + 1, &jmp_offset, sizeof(jmp_offset));
146 CHECK(VirtualProtect(crt_malloc, 16, old_prot, &unused));
148 // FYI: FlushInstructionCache is needed on Itanium etc but not on x86/x64.
151 // FIXME: investigate whether anything else is needed.
152 #endif
154 } // namespace __asan
156 #endif // _WIN32