asan_new_delete.cpp 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  1. //===-- asan_interceptors.cpp ---------------------------------------------===//
  2. //
  3. // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
  4. // See https://llvm.org/LICENSE.txt for license information.
  5. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  6. //
  7. //===----------------------------------------------------------------------===//
  8. //
  9. // This file is a part of AddressSanitizer, an address sanity checker.
  10. //
  11. // Interceptors for operators new and delete.
  12. //===----------------------------------------------------------------------===//
  13. #include <stddef.h>
  14. #include "asan_allocator.h"
  15. #include "asan_internal.h"
  16. #include "asan_report.h"
  17. #include "asan_stack.h"
  18. #include "interception/interception.h"
  19. // C++ operators can't have dllexport attributes on Windows. We export them
  20. // anyway by passing extra -export flags to the linker, which is exactly that
  21. // dllexport would normally do. We need to export them in order to make the
  22. // VS2015 dynamic CRT (MD) work.
  23. #if SANITIZER_WINDOWS && defined(_MSC_VER)
  24. #define CXX_OPERATOR_ATTRIBUTE
  25. #define COMMENT_EXPORT(sym) __pragma(comment(linker, "/export:" sym))
  26. #ifdef _WIN64
  27. COMMENT_EXPORT("??2@YAPEAX_K@Z") // operator new
  28. COMMENT_EXPORT("??2@YAPEAX_KAEBUnothrow_t@std@@@Z") // operator new nothrow
  29. COMMENT_EXPORT("??3@YAXPEAX@Z") // operator delete
  30. COMMENT_EXPORT("??3@YAXPEAX_K@Z") // sized operator delete
  31. COMMENT_EXPORT("??_U@YAPEAX_K@Z") // operator new[]
  32. COMMENT_EXPORT("??_V@YAXPEAX@Z") // operator delete[]
  33. #else
  34. COMMENT_EXPORT("??2@YAPAXI@Z") // operator new
  35. COMMENT_EXPORT("??2@YAPAXIABUnothrow_t@std@@@Z") // operator new nothrow
  36. COMMENT_EXPORT("??3@YAXPAX@Z") // operator delete
  37. COMMENT_EXPORT("??3@YAXPAXI@Z") // sized operator delete
  38. COMMENT_EXPORT("??_U@YAPAXI@Z") // operator new[]
  39. COMMENT_EXPORT("??_V@YAXPAX@Z") // operator delete[]
  40. #endif
  41. #undef COMMENT_EXPORT
  42. #else
  43. #define CXX_OPERATOR_ATTRIBUTE INTERCEPTOR_ATTRIBUTE SANITIZER_WEAK_ATTRIBUTE
  44. #endif
  45. using namespace __asan;
  46. // FreeBSD prior v9.2 have wrong definition of 'size_t'.
  47. // http://svnweb.freebsd.org/base?view=revision&revision=232261
  48. #if SANITIZER_FREEBSD && SANITIZER_WORDSIZE == 32
  49. #include <sys/param.h>
  50. #if __FreeBSD_version <= 902001 // v9.2
  51. #define size_t unsigned
  52. #endif // __FreeBSD_version
  53. #endif // SANITIZER_FREEBSD && SANITIZER_WORDSIZE == 32
  54. // This code has issues on OSX.
  55. // See https://github.com/google/sanitizers/issues/131.
  56. // Fake std::nothrow_t and std::align_val_t to avoid including <new>.
  57. namespace std {
  58. struct nothrow_t {};
  59. enum class align_val_t: size_t {};
  60. } // namespace std
  61. // TODO(alekseyshl): throw std::bad_alloc instead of dying on OOM.
  62. // For local pool allocation, align to SHADOW_GRANULARITY to match asan
  63. // allocator behavior.
  64. #define OPERATOR_NEW_BODY(type, nothrow) \
  65. GET_STACK_TRACE_MALLOC; \
  66. void *res = asan_memalign(0, size, &stack, type); \
  67. if (!nothrow && UNLIKELY(!res)) \
  68. ReportOutOfMemory(size, &stack); \
  69. return res;
  70. #define OPERATOR_NEW_BODY_ALIGN(type, nothrow) \
  71. GET_STACK_TRACE_MALLOC; \
  72. void *res = asan_memalign((uptr)align, size, &stack, type); \
  73. if (!nothrow && UNLIKELY(!res)) \
  74. ReportOutOfMemory(size, &stack); \
  75. return res;
  76. // On OS X it's not enough to just provide our own 'operator new' and
  77. // 'operator delete' implementations, because they're going to be in the
  78. // runtime dylib, and the main executable will depend on both the runtime
  79. // dylib and libstdc++, each of those'll have its implementation of new and
  80. // delete.
  81. // To make sure that C++ allocation/deallocation operators are overridden on
  82. // OS X we need to intercept them using their mangled names.
  83. #if !SANITIZER_MAC
  84. CXX_OPERATOR_ATTRIBUTE
  85. void *operator new(size_t size)
  86. { OPERATOR_NEW_BODY(FROM_NEW, false /*nothrow*/); }
  87. CXX_OPERATOR_ATTRIBUTE
  88. void *operator new[](size_t size)
  89. { OPERATOR_NEW_BODY(FROM_NEW_BR, false /*nothrow*/); }
  90. CXX_OPERATOR_ATTRIBUTE
  91. void *operator new(size_t size, std::nothrow_t const&)
  92. { OPERATOR_NEW_BODY(FROM_NEW, true /*nothrow*/); }
  93. CXX_OPERATOR_ATTRIBUTE
  94. void *operator new[](size_t size, std::nothrow_t const&)
  95. { OPERATOR_NEW_BODY(FROM_NEW_BR, true /*nothrow*/); }
  96. CXX_OPERATOR_ATTRIBUTE
  97. void *operator new(size_t size, std::align_val_t align)
  98. { OPERATOR_NEW_BODY_ALIGN(FROM_NEW, false /*nothrow*/); }
  99. CXX_OPERATOR_ATTRIBUTE
  100. void *operator new[](size_t size, std::align_val_t align)
  101. { OPERATOR_NEW_BODY_ALIGN(FROM_NEW_BR, false /*nothrow*/); }
  102. CXX_OPERATOR_ATTRIBUTE
  103. void *operator new(size_t size, std::align_val_t align, std::nothrow_t const&)
  104. { OPERATOR_NEW_BODY_ALIGN(FROM_NEW, true /*nothrow*/); }
  105. CXX_OPERATOR_ATTRIBUTE
  106. void *operator new[](size_t size, std::align_val_t align, std::nothrow_t const&)
  107. { OPERATOR_NEW_BODY_ALIGN(FROM_NEW_BR, true /*nothrow*/); }
  108. #else // SANITIZER_MAC
  109. INTERCEPTOR(void *, _Znwm, size_t size) {
  110. OPERATOR_NEW_BODY(FROM_NEW, false /*nothrow*/);
  111. }
  112. INTERCEPTOR(void *, _Znam, size_t size) {
  113. OPERATOR_NEW_BODY(FROM_NEW_BR, false /*nothrow*/);
  114. }
  115. INTERCEPTOR(void *, _ZnwmRKSt9nothrow_t, size_t size, std::nothrow_t const&) {
  116. OPERATOR_NEW_BODY(FROM_NEW, true /*nothrow*/);
  117. }
  118. INTERCEPTOR(void *, _ZnamRKSt9nothrow_t, size_t size, std::nothrow_t const&) {
  119. OPERATOR_NEW_BODY(FROM_NEW_BR, true /*nothrow*/);
  120. }
  121. #endif // !SANITIZER_MAC
  122. #define OPERATOR_DELETE_BODY(type) \
  123. GET_STACK_TRACE_FREE; \
  124. asan_delete(ptr, 0, 0, &stack, type);
  125. #define OPERATOR_DELETE_BODY_SIZE(type) \
  126. GET_STACK_TRACE_FREE; \
  127. asan_delete(ptr, size, 0, &stack, type);
  128. #define OPERATOR_DELETE_BODY_ALIGN(type) \
  129. GET_STACK_TRACE_FREE; \
  130. asan_delete(ptr, 0, static_cast<uptr>(align), &stack, type);
  131. #define OPERATOR_DELETE_BODY_SIZE_ALIGN(type) \
  132. GET_STACK_TRACE_FREE; \
  133. asan_delete(ptr, size, static_cast<uptr>(align), &stack, type);
  134. #if !SANITIZER_MAC
  135. CXX_OPERATOR_ATTRIBUTE
  136. void operator delete(void *ptr) NOEXCEPT
  137. { OPERATOR_DELETE_BODY(FROM_NEW); }
  138. CXX_OPERATOR_ATTRIBUTE
  139. void operator delete[](void *ptr) NOEXCEPT
  140. { OPERATOR_DELETE_BODY(FROM_NEW_BR); }
  141. CXX_OPERATOR_ATTRIBUTE
  142. void operator delete(void *ptr, std::nothrow_t const&)
  143. { OPERATOR_DELETE_BODY(FROM_NEW); }
  144. CXX_OPERATOR_ATTRIBUTE
  145. void operator delete[](void *ptr, std::nothrow_t const&)
  146. { OPERATOR_DELETE_BODY(FROM_NEW_BR); }
  147. CXX_OPERATOR_ATTRIBUTE
  148. void operator delete(void *ptr, size_t size) NOEXCEPT
  149. { OPERATOR_DELETE_BODY_SIZE(FROM_NEW); }
  150. CXX_OPERATOR_ATTRIBUTE
  151. void operator delete[](void *ptr, size_t size) NOEXCEPT
  152. { OPERATOR_DELETE_BODY_SIZE(FROM_NEW_BR); }
  153. CXX_OPERATOR_ATTRIBUTE
  154. void operator delete(void *ptr, std::align_val_t align) NOEXCEPT
  155. { OPERATOR_DELETE_BODY_ALIGN(FROM_NEW); }
  156. CXX_OPERATOR_ATTRIBUTE
  157. void operator delete[](void *ptr, std::align_val_t align) NOEXCEPT
  158. { OPERATOR_DELETE_BODY_ALIGN(FROM_NEW_BR); }
  159. CXX_OPERATOR_ATTRIBUTE
  160. void operator delete(void *ptr, std::align_val_t align, std::nothrow_t const&)
  161. { OPERATOR_DELETE_BODY_ALIGN(FROM_NEW); }
  162. CXX_OPERATOR_ATTRIBUTE
  163. void operator delete[](void *ptr, std::align_val_t align, std::nothrow_t const&)
  164. { OPERATOR_DELETE_BODY_ALIGN(FROM_NEW_BR); }
  165. CXX_OPERATOR_ATTRIBUTE
  166. void operator delete(void *ptr, size_t size, std::align_val_t align) NOEXCEPT
  167. { OPERATOR_DELETE_BODY_SIZE_ALIGN(FROM_NEW); }
  168. CXX_OPERATOR_ATTRIBUTE
  169. void operator delete[](void *ptr, size_t size, std::align_val_t align) NOEXCEPT
  170. { OPERATOR_DELETE_BODY_SIZE_ALIGN(FROM_NEW_BR); }
  171. #else // SANITIZER_MAC
  172. INTERCEPTOR(void, _ZdlPv, void *ptr)
  173. { OPERATOR_DELETE_BODY(FROM_NEW); }
  174. INTERCEPTOR(void, _ZdaPv, void *ptr)
  175. { OPERATOR_DELETE_BODY(FROM_NEW_BR); }
  176. INTERCEPTOR(void, _ZdlPvRKSt9nothrow_t, void *ptr, std::nothrow_t const&)
  177. { OPERATOR_DELETE_BODY(FROM_NEW); }
  178. INTERCEPTOR(void, _ZdaPvRKSt9nothrow_t, void *ptr, std::nothrow_t const&)
  179. { OPERATOR_DELETE_BODY(FROM_NEW_BR); }
  180. #endif // !SANITIZER_MAC