Alexey Samsonov | e5f5895 | 2012-06-04 13:50:10 +0000 | [diff] [blame] | 1 | //===-- asan_malloc_win.cc ------------------------------------------------===// |
Kostya Serebryany | 938b105 | 2012-02-06 17:56:38 +0000 | [diff] [blame] | 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 | //===----------------------------------------------------------------------===// |
Evgeniy Stepanov | 24e1372 | 2013-03-19 14:33:38 +0000 | [diff] [blame] | 14 | |
| 15 | #include "sanitizer_common/sanitizer_platform.h" |
Evgeniy Stepanov | 30e110e | 2013-03-19 14:54:17 +0000 | [diff] [blame] | 16 | #if SANITIZER_WINDOWS |
Kostya Serebryany | 938b105 | 2012-02-06 17:56:38 +0000 | [diff] [blame] | 17 | |
| 18 | #include "asan_allocator.h" |
| 19 | #include "asan_interceptors.h" |
| 20 | #include "asan_internal.h" |
| 21 | #include "asan_stack.h" |
Timur Iskhodzhanov | d9a88cc | 2012-02-29 11:43:03 +0000 | [diff] [blame] | 22 | #include "interception/interception.h" |
Kostya Serebryany | 938b105 | 2012-02-06 17:56:38 +0000 | [diff] [blame] | 23 | |
Alexey Samsonov | b46941a | 2012-09-24 11:43:40 +0000 | [diff] [blame] | 24 | #include <stddef.h> |
| 25 | |
Kostya Serebryany | 938b105 | 2012-02-06 17:56:38 +0000 | [diff] [blame] | 26 | // ---------------------- Replacement functions ---------------- {{{1 |
| 27 | using 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 | |
| 34 | extern "C" { |
Timur Iskhodzhanov | e274841 | 2013-08-13 16:48:18 +0000 | [diff] [blame] | 35 | SANITIZER_INTERFACE_ATTRIBUTE |
Kostya Serebryany | 938b105 | 2012-02-06 17:56:38 +0000 | [diff] [blame] | 36 | void free(void *ptr) { |
Kostya Serebryany | a30c8f9 | 2012-12-13 09:34:23 +0000 | [diff] [blame] | 37 | GET_STACK_TRACE_FREE; |
Kostya Serebryany | fe6d916 | 2012-12-21 08:53:59 +0000 | [diff] [blame] | 38 | return asan_free(ptr, &stack, FROM_MALLOC); |
Kostya Serebryany | 938b105 | 2012-02-06 17:56:38 +0000 | [diff] [blame] | 39 | } |
| 40 | |
Timur Iskhodzhanov | e274841 | 2013-08-13 16:48:18 +0000 | [diff] [blame] | 41 | SANITIZER_INTERFACE_ATTRIBUTE |
Timur Iskhodzhanov | 1bbe256 | 2012-03-23 11:33:02 +0000 | [diff] [blame] | 42 | void _free_dbg(void* ptr, int) { |
| 43 | free(ptr); |
| 44 | } |
| 45 | |
| 46 | void cfree(void *ptr) { |
| 47 | CHECK(!"cfree() should not be used on Windows?"); |
| 48 | } |
| 49 | |
Timur Iskhodzhanov | e274841 | 2013-08-13 16:48:18 +0000 | [diff] [blame] | 50 | SANITIZER_INTERFACE_ATTRIBUTE |
Kostya Serebryany | 938b105 | 2012-02-06 17:56:38 +0000 | [diff] [blame] | 51 | void *malloc(size_t size) { |
Kostya Serebryany | a30c8f9 | 2012-12-13 09:34:23 +0000 | [diff] [blame] | 52 | GET_STACK_TRACE_MALLOC; |
Kostya Serebryany | 938b105 | 2012-02-06 17:56:38 +0000 | [diff] [blame] | 53 | return asan_malloc(size, &stack); |
| 54 | } |
| 55 | |
Timur Iskhodzhanov | e274841 | 2013-08-13 16:48:18 +0000 | [diff] [blame] | 56 | SANITIZER_INTERFACE_ATTRIBUTE |
Timur Iskhodzhanov | 1bbe256 | 2012-03-23 11:33:02 +0000 | [diff] [blame] | 57 | void* _malloc_dbg(size_t size, int , const char*, int) { |
| 58 | return malloc(size); |
| 59 | } |
| 60 | |
Timur Iskhodzhanov | e274841 | 2013-08-13 16:48:18 +0000 | [diff] [blame] | 61 | SANITIZER_INTERFACE_ATTRIBUTE |
Kostya Serebryany | 938b105 | 2012-02-06 17:56:38 +0000 | [diff] [blame] | 62 | void *calloc(size_t nmemb, size_t size) { |
Kostya Serebryany | a30c8f9 | 2012-12-13 09:34:23 +0000 | [diff] [blame] | 63 | GET_STACK_TRACE_MALLOC; |
Kostya Serebryany | 938b105 | 2012-02-06 17:56:38 +0000 | [diff] [blame] | 64 | return asan_calloc(nmemb, size, &stack); |
| 65 | } |
| 66 | |
Timur Iskhodzhanov | e274841 | 2013-08-13 16:48:18 +0000 | [diff] [blame] | 67 | SANITIZER_INTERFACE_ATTRIBUTE |
Timur Iskhodzhanov | 1bbe256 | 2012-03-23 11:33:02 +0000 | [diff] [blame] | 68 | void* _calloc_dbg(size_t n, size_t size, int, const char*, int) { |
| 69 | return calloc(n, size); |
| 70 | } |
| 71 | |
Timur Iskhodzhanov | e274841 | 2013-08-13 16:48:18 +0000 | [diff] [blame] | 72 | SANITIZER_INTERFACE_ATTRIBUTE |
Timur Iskhodzhanov | 2716a61 | 2012-03-12 11:45:09 +0000 | [diff] [blame] | 73 | void *_calloc_impl(size_t nmemb, size_t size, int *errno_tmp) { |
Timur Iskhodzhanov | 1bbe256 | 2012-03-23 11:33:02 +0000 | [diff] [blame] | 74 | return calloc(nmemb, size); |
Timur Iskhodzhanov | 2716a61 | 2012-03-12 11:45:09 +0000 | [diff] [blame] | 75 | } |
| 76 | |
Timur Iskhodzhanov | e274841 | 2013-08-13 16:48:18 +0000 | [diff] [blame] | 77 | SANITIZER_INTERFACE_ATTRIBUTE |
Kostya Serebryany | 938b105 | 2012-02-06 17:56:38 +0000 | [diff] [blame] | 78 | void *realloc(void *ptr, size_t size) { |
Kostya Serebryany | a30c8f9 | 2012-12-13 09:34:23 +0000 | [diff] [blame] | 79 | GET_STACK_TRACE_MALLOC; |
Kostya Serebryany | 938b105 | 2012-02-06 17:56:38 +0000 | [diff] [blame] | 80 | return asan_realloc(ptr, size, &stack); |
| 81 | } |
Timur Iskhodzhanov | cf13eb2 | 2012-03-07 11:19:26 +0000 | [diff] [blame] | 82 | |
Timur Iskhodzhanov | e274841 | 2013-08-13 16:48:18 +0000 | [diff] [blame] | 83 | SANITIZER_INTERFACE_ATTRIBUTE |
Timur Iskhodzhanov | 1bbe256 | 2012-03-23 11:33:02 +0000 | [diff] [blame] | 84 | void *_realloc_dbg(void *ptr, size_t size, int) { |
| 85 | CHECK(!"_realloc_dbg should not exist!"); |
Kostya Serebryany | 3f4c387 | 2012-05-31 14:35:53 +0000 | [diff] [blame] | 86 | return 0; |
Timur Iskhodzhanov | 1bbe256 | 2012-03-23 11:33:02 +0000 | [diff] [blame] | 87 | } |
| 88 | |
Timur Iskhodzhanov | e274841 | 2013-08-13 16:48:18 +0000 | [diff] [blame] | 89 | SANITIZER_INTERFACE_ATTRIBUTE |
Timur Iskhodzhanov | 1bbe256 | 2012-03-23 11:33:02 +0000 | [diff] [blame] | 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) |
Kostya Serebryany | 3f4c387 | 2012-05-31 14:35:53 +0000 | [diff] [blame] | 95 | return 0; |
Timur Iskhodzhanov | 1bbe256 | 2012-03-23 11:33:02 +0000 | [diff] [blame] | 96 | return realloc(p, size); |
| 97 | } |
| 98 | |
Timur Iskhodzhanov | e274841 | 2013-08-13 16:48:18 +0000 | [diff] [blame] | 99 | SANITIZER_INTERFACE_ATTRIBUTE |
Timur Iskhodzhanov | cf13eb2 | 2012-03-07 11:19:26 +0000 | [diff] [blame] | 100 | size_t _msize(void *ptr) { |
Alexey Samsonov | 1b17f5b | 2013-11-13 14:46:58 +0000 | [diff] [blame] | 101 | GET_CURRENT_PC_BP_SP; |
| 102 | (void)sp; |
| 103 | return asan_malloc_usable_size(ptr, pc, bp); |
Timur Iskhodzhanov | cf13eb2 | 2012-03-07 11:19:26 +0000 | [diff] [blame] | 104 | } |
Timur Iskhodzhanov | 1bbe256 | 2012-03-23 11:33:02 +0000 | [diff] [blame] | 105 | |
| 106 | int _CrtDbgReport(int, const char*, int, |
| 107 | const char*, const char*, ...) { |
| 108 | ShowStatsAndAbort(); |
| 109 | } |
| 110 | |
| 111 | int _CrtDbgReportW(int reportType, const wchar_t*, int, |
| 112 | const wchar_t*, const wchar_t*, ...) { |
| 113 | ShowStatsAndAbort(); |
| 114 | } |
| 115 | |
| 116 | int _CrtSetReportMode(int, int) { |
| 117 | return 0; |
| 118 | } |
Kostya Serebryany | 938b105 | 2012-02-06 17:56:38 +0000 | [diff] [blame] | 119 | } // extern "C" |
| 120 | |
Timur Iskhodzhanov | d9a88cc | 2012-02-29 11:43:03 +0000 | [diff] [blame] | 121 | using __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: |
| 125 | extern "C" int __stdcall VirtualProtect(void* addr, size_t size, |
| 126 | DWORD prot, DWORD *old_prot); |
| 127 | const int PAGE_EXECUTE_READWRITE = 0x40; |
| 128 | |
| 129 | namespace __asan { |
| 130 | void ReplaceSystemMalloc() { |
Timur Iskhodzhanov | 2716a61 | 2012-03-12 11:45:09 +0000 | [diff] [blame] | 131 | #if defined(_DLL) |
| 132 | # ifdef _WIN64 |
| 133 | # error ReplaceSystemMalloc was not tested on x64 |
| 134 | # endif |
Timur Iskhodzhanov | d9a88cc | 2012-02-29 11:43:03 +0000 | [diff] [blame] | 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. |
| 141 | |
Timur Iskhodzhanov | 2716a61 | 2012-03-12 11:45:09 +0000 | [diff] [blame] | 142 | ptrdiff_t jmp_offset = (char*)malloc - (char*)crt_malloc - 5; |
Timur Iskhodzhanov | d9a88cc | 2012-02-29 11:43:03 +0000 | [diff] [blame] | 143 | 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 Iskhodzhanov | 2716a61 | 2012-03-12 11:45:09 +0000 | [diff] [blame] | 152 | #endif |
Timur Iskhodzhanov | d9a88cc | 2012-02-29 11:43:03 +0000 | [diff] [blame] | 153 | } |
| 154 | } // namespace __asan |
| 155 | |
Kostya Serebryany | 938b105 | 2012-02-06 17:56:38 +0000 | [diff] [blame] | 156 | #endif // _WIN32 |