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