asan_malloc_win.cc 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. //===-- asan_malloc_win.cc ------------------------------------------------===//
  2. //
  3. // This file is distributed under the University of Illinois Open Source
  4. // License. See LICENSE.TXT for details.
  5. //
  6. //===----------------------------------------------------------------------===//
  7. //
  8. // This file is a part of AddressSanitizer, an address sanity checker.
  9. //
  10. // Windows-specific malloc interception.
  11. //===----------------------------------------------------------------------===//
  12. #include "sanitizer_common/sanitizer_platform.h"
  13. #if SANITIZER_WINDOWS
  14. #include "asan_allocator.h"
  15. #include "asan_interceptors.h"
  16. #include "asan_internal.h"
  17. #include "asan_stack.h"
  18. #include "sanitizer_common/sanitizer_interception.h"
  19. #include <stddef.h>
  20. using namespace __asan; // NOLINT
  21. // MT: Simply defining functions with the same signature in *.obj
  22. // files overrides the standard functions in the CRT.
  23. // MD: Memory allocation functions are defined in the CRT .dll,
  24. // so we have to intercept them before they are called for the first time.
  25. #if ASAN_DYNAMIC
  26. # define ALLOCATION_FUNCTION_ATTRIBUTE
  27. #else
  28. # define ALLOCATION_FUNCTION_ATTRIBUTE SANITIZER_INTERFACE_ATTRIBUTE
  29. #endif
  30. extern "C" {
  31. ALLOCATION_FUNCTION_ATTRIBUTE
  32. void free(void *ptr) {
  33. GET_STACK_TRACE_FREE;
  34. return asan_free(ptr, &stack, FROM_MALLOC);
  35. }
  36. ALLOCATION_FUNCTION_ATTRIBUTE
  37. void _free_dbg(void *ptr, int) {
  38. free(ptr);
  39. }
  40. ALLOCATION_FUNCTION_ATTRIBUTE
  41. void cfree(void *ptr) {
  42. CHECK(!"cfree() should not be used on Windows");
  43. }
  44. ALLOCATION_FUNCTION_ATTRIBUTE
  45. void *malloc(size_t size) {
  46. GET_STACK_TRACE_MALLOC;
  47. return asan_malloc(size, &stack);
  48. }
  49. ALLOCATION_FUNCTION_ATTRIBUTE
  50. void *_malloc_dbg(size_t size, int, const char *, int) {
  51. return malloc(size);
  52. }
  53. ALLOCATION_FUNCTION_ATTRIBUTE
  54. void *calloc(size_t nmemb, size_t size) {
  55. GET_STACK_TRACE_MALLOC;
  56. return asan_calloc(nmemb, size, &stack);
  57. }
  58. ALLOCATION_FUNCTION_ATTRIBUTE
  59. void *_calloc_dbg(size_t nmemb, size_t size, int, const char *, int) {
  60. return calloc(nmemb, size);
  61. }
  62. ALLOCATION_FUNCTION_ATTRIBUTE
  63. void *_calloc_impl(size_t nmemb, size_t size, int *errno_tmp) {
  64. return calloc(nmemb, size);
  65. }
  66. ALLOCATION_FUNCTION_ATTRIBUTE
  67. void *realloc(void *ptr, size_t size) {
  68. GET_STACK_TRACE_MALLOC;
  69. return asan_realloc(ptr, size, &stack);
  70. }
  71. ALLOCATION_FUNCTION_ATTRIBUTE
  72. void *_realloc_dbg(void *ptr, size_t size, int) {
  73. CHECK(!"_realloc_dbg should not exist!");
  74. return 0;
  75. }
  76. ALLOCATION_FUNCTION_ATTRIBUTE
  77. void *_recalloc(void *p, size_t n, size_t elem_size) {
  78. if (!p)
  79. return calloc(n, elem_size);
  80. const size_t size = n * elem_size;
  81. if (elem_size != 0 && size / elem_size != n)
  82. return 0;
  83. return realloc(p, size);
  84. }
  85. ALLOCATION_FUNCTION_ATTRIBUTE
  86. size_t _msize(void *ptr) {
  87. GET_CURRENT_PC_BP_SP;
  88. (void)sp;
  89. return asan_malloc_usable_size(ptr, pc, bp);
  90. }
  91. ALLOCATION_FUNCTION_ATTRIBUTE
  92. void *_expand(void *memblock, size_t size) {
  93. // _expand is used in realloc-like functions to resize the buffer if possible.
  94. // We don't want memory to stand still while resizing buffers, so return 0.
  95. return 0;
  96. }
  97. ALLOCATION_FUNCTION_ATTRIBUTE
  98. void *_expand_dbg(void *memblock, size_t size) {
  99. return _expand(memblock, size);
  100. }
  101. // TODO(timurrrr): Might want to add support for _aligned_* allocation
  102. // functions to detect a bit more bugs. Those functions seem to wrap malloc().
  103. int _CrtDbgReport(int, const char*, int,
  104. const char*, const char*, ...) {
  105. ShowStatsAndAbort();
  106. }
  107. int _CrtDbgReportW(int reportType, const wchar_t*, int,
  108. const wchar_t*, const wchar_t*, ...) {
  109. ShowStatsAndAbort();
  110. }
  111. int _CrtSetReportMode(int, int) {
  112. return 0;
  113. }
  114. } // extern "C"
  115. namespace __asan {
  116. void ReplaceSystemMalloc() {
  117. #if defined(ASAN_DYNAMIC)
  118. // We don't check the result because CRT might not be used in the process.
  119. __interception::OverrideFunction("free", (uptr)free);
  120. __interception::OverrideFunction("malloc", (uptr)malloc);
  121. __interception::OverrideFunction("_malloc_crt", (uptr)malloc);
  122. __interception::OverrideFunction("calloc", (uptr)calloc);
  123. __interception::OverrideFunction("_calloc_crt", (uptr)calloc);
  124. __interception::OverrideFunction("realloc", (uptr)realloc);
  125. __interception::OverrideFunction("_realloc_crt", (uptr)realloc);
  126. __interception::OverrideFunction("_recalloc", (uptr)_recalloc);
  127. __interception::OverrideFunction("_recalloc_crt", (uptr)_recalloc);
  128. __interception::OverrideFunction("_msize", (uptr)_msize);
  129. __interception::OverrideFunction("_expand", (uptr)_expand);
  130. // Override different versions of 'operator new' and 'operator delete'.
  131. // No need to override the nothrow versions as they just wrap the throw
  132. // versions.
  133. // FIXME: Unfortunately, MSVC miscompiles the statements that take the
  134. // addresses of the array versions of these operators,
  135. // see https://connect.microsoft.com/VisualStudio/feedbackdetail/view/946992
  136. // We might want to try to work around this by [inline] assembly or compiling
  137. // parts of the RTL with Clang.
  138. void *(*op_new)(size_t sz) = operator new;
  139. void (*op_delete)(void *p) = operator delete;
  140. void *(*op_array_new)(size_t sz) = operator new[];
  141. void (*op_array_delete)(void *p) = operator delete[];
  142. __interception::OverrideFunction("??2@YAPAXI@Z", (uptr)op_new);
  143. __interception::OverrideFunction("??3@YAXPAX@Z", (uptr)op_delete);
  144. __interception::OverrideFunction("??_U@YAPAXI@Z", (uptr)op_array_new);
  145. __interception::OverrideFunction("??_V@YAXPAX@Z", (uptr)op_array_delete);
  146. #endif
  147. }
  148. } // namespace __asan
  149. #endif // _WIN32