hwasan_new_delete.cpp 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. //===-- hwasan_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 HWAddressSanitizer.
  10. //
  11. // Interceptors for operators new and delete.
  12. //===----------------------------------------------------------------------===//
  13. #include "hwasan.h"
  14. #include "interception/interception.h"
  15. #include "sanitizer_common/sanitizer_allocator.h"
  16. #include "sanitizer_common/sanitizer_allocator_report.h"
  17. #include <stddef.h>
  18. #include <stdlib.h>
  19. #if HWASAN_REPLACE_OPERATORS_NEW_AND_DELETE
  20. // TODO(alekseys): throw std::bad_alloc instead of dying on OOM.
  21. #define OPERATOR_NEW_BODY(nothrow) \
  22. GET_MALLOC_STACK_TRACE; \
  23. void *res = hwasan_malloc(size, &stack);\
  24. if (!nothrow && UNLIKELY(!res)) ReportOutOfMemory(size, &stack);\
  25. return res
  26. #define OPERATOR_NEW_ALIGN_BODY(nothrow) \
  27. GET_MALLOC_STACK_TRACE; \
  28. void *res = hwasan_aligned_alloc(static_cast<uptr>(align), size, &stack); \
  29. if (!nothrow && UNLIKELY(!res)) \
  30. ReportOutOfMemory(size, &stack); \
  31. return res
  32. #define OPERATOR_DELETE_BODY \
  33. GET_MALLOC_STACK_TRACE; \
  34. if (ptr) hwasan_free(ptr, &stack)
  35. #elif defined(__ANDROID__)
  36. // We don't actually want to intercept operator new and delete on Android, but
  37. // since we previously released a runtime that intercepted these functions,
  38. // removing the interceptors would break ABI. Therefore we simply forward to
  39. // malloc and free.
  40. #define OPERATOR_NEW_BODY(nothrow) return malloc(size)
  41. #define OPERATOR_DELETE_BODY free(ptr)
  42. #endif
  43. #ifdef OPERATOR_NEW_BODY
  44. using namespace __hwasan;
  45. // Fake std::nothrow_t to avoid including <new>.
  46. namespace std {
  47. struct nothrow_t {};
  48. } // namespace std
  49. INTERCEPTOR_ATTRIBUTE SANITIZER_WEAK_ATTRIBUTE
  50. void *operator new(size_t size) { OPERATOR_NEW_BODY(false /*nothrow*/); }
  51. INTERCEPTOR_ATTRIBUTE SANITIZER_WEAK_ATTRIBUTE
  52. void *operator new[](size_t size) { OPERATOR_NEW_BODY(false /*nothrow*/); }
  53. INTERCEPTOR_ATTRIBUTE SANITIZER_WEAK_ATTRIBUTE
  54. void *operator new(size_t size, std::nothrow_t const&) {
  55. OPERATOR_NEW_BODY(true /*nothrow*/);
  56. }
  57. INTERCEPTOR_ATTRIBUTE SANITIZER_WEAK_ATTRIBUTE
  58. void *operator new[](size_t size, std::nothrow_t const&) {
  59. OPERATOR_NEW_BODY(true /*nothrow*/);
  60. }
  61. INTERCEPTOR_ATTRIBUTE SANITIZER_WEAK_ATTRIBUTE void operator delete(void *ptr)
  62. NOEXCEPT {
  63. OPERATOR_DELETE_BODY;
  64. }
  65. INTERCEPTOR_ATTRIBUTE SANITIZER_WEAK_ATTRIBUTE void operator delete[](
  66. void *ptr) NOEXCEPT {
  67. OPERATOR_DELETE_BODY;
  68. }
  69. INTERCEPTOR_ATTRIBUTE SANITIZER_WEAK_ATTRIBUTE void operator delete(
  70. void *ptr, std::nothrow_t const &) {
  71. OPERATOR_DELETE_BODY;
  72. }
  73. INTERCEPTOR_ATTRIBUTE SANITIZER_WEAK_ATTRIBUTE void operator delete[](
  74. void *ptr, std::nothrow_t const &) {
  75. OPERATOR_DELETE_BODY;
  76. }
  77. #endif // OPERATOR_NEW_BODY
  78. #ifdef OPERATOR_NEW_ALIGN_BODY
  79. namespace std {
  80. enum class align_val_t : size_t {};
  81. } // namespace std
  82. INTERCEPTOR_ATTRIBUTE SANITIZER_WEAK_ATTRIBUTE void *operator new(
  83. size_t size, std::align_val_t align) {
  84. OPERATOR_NEW_ALIGN_BODY(false /*nothrow*/);
  85. }
  86. INTERCEPTOR_ATTRIBUTE SANITIZER_WEAK_ATTRIBUTE void *operator new[](
  87. size_t size, std::align_val_t align) {
  88. OPERATOR_NEW_ALIGN_BODY(false /*nothrow*/);
  89. }
  90. INTERCEPTOR_ATTRIBUTE SANITIZER_WEAK_ATTRIBUTE void *operator new(
  91. size_t size, std::align_val_t align, std::nothrow_t const &) {
  92. OPERATOR_NEW_ALIGN_BODY(true /*nothrow*/);
  93. }
  94. INTERCEPTOR_ATTRIBUTE SANITIZER_WEAK_ATTRIBUTE void *operator new[](
  95. size_t size, std::align_val_t align, std::nothrow_t const &) {
  96. OPERATOR_NEW_ALIGN_BODY(true /*nothrow*/);
  97. }
  98. INTERCEPTOR_ATTRIBUTE SANITIZER_WEAK_ATTRIBUTE void operator delete(
  99. void *ptr, std::align_val_t align) NOEXCEPT {
  100. OPERATOR_DELETE_BODY;
  101. }
  102. INTERCEPTOR_ATTRIBUTE SANITIZER_WEAK_ATTRIBUTE void operator delete[](
  103. void *ptr, std::align_val_t) NOEXCEPT {
  104. OPERATOR_DELETE_BODY;
  105. }
  106. INTERCEPTOR_ATTRIBUTE SANITIZER_WEAK_ATTRIBUTE void operator delete(
  107. void *ptr, std::align_val_t, std::nothrow_t const &) NOEXCEPT {
  108. OPERATOR_DELETE_BODY;
  109. }
  110. INTERCEPTOR_ATTRIBUTE SANITIZER_WEAK_ATTRIBUTE void operator delete[](
  111. void *ptr, std::align_val_t, std::nothrow_t const &) NOEXCEPT {
  112. OPERATOR_DELETE_BODY;
  113. }
  114. #endif // OPERATOR_NEW_ALIGN_BODY