dfsan_new_delete.cpp 4.0 KB

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