msan_new_delete.cpp 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. //===-- msan_new_delete.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 MemorySanitizer.
  10. //
  11. // Interceptors for operators new and delete.
  12. //===----------------------------------------------------------------------===//
  13. #include "msan.h"
  14. #include "interception/interception.h"
  15. #include "sanitizer_common/sanitizer_allocator.h"
  16. #include "sanitizer_common/sanitizer_allocator_report.h"
  17. #if MSAN_REPLACE_OPERATORS_NEW_AND_DELETE
  18. #include <stddef.h>
  19. using namespace __msan;
  20. // Fake std::nothrow_t and std::align_val_t to avoid including <new>.
  21. namespace std {
  22. struct nothrow_t {};
  23. enum class align_val_t: size_t {};
  24. } // namespace std
  25. // TODO(alekseys): throw std::bad_alloc instead of dying on OOM.
  26. # define OPERATOR_NEW_BODY(nothrow) \
  27. GET_MALLOC_STACK_TRACE; \
  28. void *res = msan_malloc(size, &stack); \
  29. if (!nothrow && UNLIKELY(!res)) { \
  30. GET_FATAL_STACK_TRACE_IF_EMPTY(&stack); \
  31. ReportOutOfMemory(size, &stack); \
  32. } \
  33. return res
  34. # define OPERATOR_NEW_BODY_ALIGN(nothrow) \
  35. GET_MALLOC_STACK_TRACE; \
  36. void *res = msan_memalign((uptr)align, size, &stack); \
  37. if (!nothrow && UNLIKELY(!res)) { \
  38. GET_FATAL_STACK_TRACE_IF_EMPTY(&stack); \
  39. ReportOutOfMemory(size, &stack); \
  40. } \
  41. return res;
  42. INTERCEPTOR_ATTRIBUTE
  43. void *operator new(size_t size) { OPERATOR_NEW_BODY(false /*nothrow*/); }
  44. INTERCEPTOR_ATTRIBUTE
  45. void *operator new[](size_t size) { OPERATOR_NEW_BODY(false /*nothrow*/); }
  46. INTERCEPTOR_ATTRIBUTE
  47. void *operator new(size_t size, std::nothrow_t const&) {
  48. OPERATOR_NEW_BODY(true /*nothrow*/);
  49. }
  50. INTERCEPTOR_ATTRIBUTE
  51. void *operator new[](size_t size, std::nothrow_t const&) {
  52. OPERATOR_NEW_BODY(true /*nothrow*/);
  53. }
  54. INTERCEPTOR_ATTRIBUTE
  55. void *operator new(size_t size, std::align_val_t align)
  56. { OPERATOR_NEW_BODY_ALIGN(false /*nothrow*/); }
  57. INTERCEPTOR_ATTRIBUTE
  58. void *operator new[](size_t size, std::align_val_t align)
  59. { OPERATOR_NEW_BODY_ALIGN(false /*nothrow*/); }
  60. INTERCEPTOR_ATTRIBUTE
  61. void *operator new(size_t size, std::align_val_t align, std::nothrow_t const&)
  62. { OPERATOR_NEW_BODY_ALIGN(true /*nothrow*/); }
  63. INTERCEPTOR_ATTRIBUTE
  64. void *operator new[](size_t size, std::align_val_t align, std::nothrow_t const&)
  65. { OPERATOR_NEW_BODY_ALIGN(true /*nothrow*/); }
  66. #define OPERATOR_DELETE_BODY \
  67. GET_MALLOC_STACK_TRACE; \
  68. if (ptr) MsanDeallocate(&stack, ptr)
  69. INTERCEPTOR_ATTRIBUTE
  70. void operator delete(void *ptr) NOEXCEPT { OPERATOR_DELETE_BODY; }
  71. INTERCEPTOR_ATTRIBUTE
  72. void operator delete[](void *ptr) NOEXCEPT { OPERATOR_DELETE_BODY; }
  73. INTERCEPTOR_ATTRIBUTE
  74. void operator delete(void *ptr, std::nothrow_t const&) { OPERATOR_DELETE_BODY; }
  75. INTERCEPTOR_ATTRIBUTE
  76. void operator delete[](void *ptr, std::nothrow_t const&) {
  77. OPERATOR_DELETE_BODY;
  78. }
  79. INTERCEPTOR_ATTRIBUTE
  80. void operator delete(void *ptr, size_t size) NOEXCEPT { OPERATOR_DELETE_BODY; }
  81. INTERCEPTOR_ATTRIBUTE
  82. void operator delete[](void *ptr, size_t size) NOEXCEPT
  83. { OPERATOR_DELETE_BODY; }
  84. INTERCEPTOR_ATTRIBUTE
  85. void operator delete(void *ptr, std::align_val_t align) NOEXCEPT
  86. { OPERATOR_DELETE_BODY; }
  87. INTERCEPTOR_ATTRIBUTE
  88. void operator delete[](void *ptr, std::align_val_t align) NOEXCEPT
  89. { OPERATOR_DELETE_BODY; }
  90. INTERCEPTOR_ATTRIBUTE
  91. void operator delete(void *ptr, std::align_val_t align, std::nothrow_t const&)
  92. { OPERATOR_DELETE_BODY; }
  93. INTERCEPTOR_ATTRIBUTE
  94. void operator delete[](void *ptr, std::align_val_t align, std::nothrow_t const&)
  95. { OPERATOR_DELETE_BODY; }
  96. INTERCEPTOR_ATTRIBUTE
  97. void operator delete(void *ptr, size_t size, std::align_val_t align) NOEXCEPT
  98. { OPERATOR_DELETE_BODY; }
  99. INTERCEPTOR_ATTRIBUTE
  100. void operator delete[](void *ptr, size_t size, std::align_val_t align) NOEXCEPT
  101. { OPERATOR_DELETE_BODY; }
  102. #endif // MSAN_REPLACE_OPERATORS_NEW_AND_DELETE