msan_new_delete.cpp 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  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)) ReportOutOfMemory(size, &stack);\
  30. return res
  31. #define OPERATOR_NEW_BODY_ALIGN(nothrow) \
  32. GET_MALLOC_STACK_TRACE;\
  33. void *res = msan_memalign((uptr)align, size, &stack);\
  34. if (!nothrow && UNLIKELY(!res)) ReportOutOfMemory(size, &stack);\
  35. return res;
  36. INTERCEPTOR_ATTRIBUTE
  37. void *operator new(size_t size) { OPERATOR_NEW_BODY(false /*nothrow*/); }
  38. INTERCEPTOR_ATTRIBUTE
  39. void *operator new[](size_t size) { OPERATOR_NEW_BODY(false /*nothrow*/); }
  40. INTERCEPTOR_ATTRIBUTE
  41. void *operator new(size_t size, std::nothrow_t const&) {
  42. OPERATOR_NEW_BODY(true /*nothrow*/);
  43. }
  44. INTERCEPTOR_ATTRIBUTE
  45. void *operator new[](size_t size, std::nothrow_t const&) {
  46. OPERATOR_NEW_BODY(true /*nothrow*/);
  47. }
  48. INTERCEPTOR_ATTRIBUTE
  49. void *operator new(size_t size, std::align_val_t align)
  50. { OPERATOR_NEW_BODY_ALIGN(false /*nothrow*/); }
  51. INTERCEPTOR_ATTRIBUTE
  52. void *operator new[](size_t size, std::align_val_t align)
  53. { OPERATOR_NEW_BODY_ALIGN(false /*nothrow*/); }
  54. INTERCEPTOR_ATTRIBUTE
  55. void *operator new(size_t size, std::align_val_t align, std::nothrow_t const&)
  56. { OPERATOR_NEW_BODY_ALIGN(true /*nothrow*/); }
  57. INTERCEPTOR_ATTRIBUTE
  58. void *operator new[](size_t size, std::align_val_t align, std::nothrow_t const&)
  59. { OPERATOR_NEW_BODY_ALIGN(true /*nothrow*/); }
  60. #define OPERATOR_DELETE_BODY \
  61. GET_MALLOC_STACK_TRACE; \
  62. if (ptr) MsanDeallocate(&stack, ptr)
  63. INTERCEPTOR_ATTRIBUTE
  64. void operator delete(void *ptr) NOEXCEPT { OPERATOR_DELETE_BODY; }
  65. INTERCEPTOR_ATTRIBUTE
  66. void operator delete[](void *ptr) NOEXCEPT { OPERATOR_DELETE_BODY; }
  67. INTERCEPTOR_ATTRIBUTE
  68. void operator delete(void *ptr, std::nothrow_t const&) { OPERATOR_DELETE_BODY; }
  69. INTERCEPTOR_ATTRIBUTE
  70. void operator delete[](void *ptr, std::nothrow_t const&) {
  71. OPERATOR_DELETE_BODY;
  72. }
  73. INTERCEPTOR_ATTRIBUTE
  74. void operator delete(void *ptr, size_t size) NOEXCEPT { OPERATOR_DELETE_BODY; }
  75. INTERCEPTOR_ATTRIBUTE
  76. void operator delete[](void *ptr, size_t size) NOEXCEPT
  77. { OPERATOR_DELETE_BODY; }
  78. INTERCEPTOR_ATTRIBUTE
  79. void operator delete(void *ptr, std::align_val_t align) NOEXCEPT
  80. { OPERATOR_DELETE_BODY; }
  81. INTERCEPTOR_ATTRIBUTE
  82. void operator delete[](void *ptr, std::align_val_t align) NOEXCEPT
  83. { OPERATOR_DELETE_BODY; }
  84. INTERCEPTOR_ATTRIBUTE
  85. void operator delete(void *ptr, std::align_val_t align, std::nothrow_t const&)
  86. { OPERATOR_DELETE_BODY; }
  87. INTERCEPTOR_ATTRIBUTE
  88. void operator delete[](void *ptr, std::align_val_t align, std::nothrow_t const&)
  89. { OPERATOR_DELETE_BODY; }
  90. INTERCEPTOR_ATTRIBUTE
  91. void operator delete(void *ptr, size_t size, std::align_val_t align) NOEXCEPT
  92. { OPERATOR_DELETE_BODY; }
  93. INTERCEPTOR_ATTRIBUTE
  94. void operator delete[](void *ptr, size_t size, std::align_val_t align) NOEXCEPT
  95. { OPERATOR_DELETE_BODY; }
  96. #endif // MSAN_REPLACE_OPERATORS_NEW_AND_DELETE