wrappers_cpp.cpp 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. //===-- wrappers_cpp.cpp ----------------------------------------*- C++ -*-===//
  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. #include "platform.h"
  9. // Skip this compilation unit if compiled as part of Bionic.
  10. #if !SCUDO_ANDROID || !_BIONIC
  11. #include "allocator_config.h"
  12. #include "internal_defs.h"
  13. #include "platform.h"
  14. #include "scudo/interface.h"
  15. #include "wrappers_c.h"
  16. #include <stdint.h>
  17. namespace std {
  18. struct nothrow_t {};
  19. enum class align_val_t : size_t {};
  20. } // namespace std
  21. static void reportAllocation(void *ptr, size_t size) {
  22. if (SCUDO_ENABLE_HOOKS)
  23. if (__scudo_allocate_hook && ptr)
  24. __scudo_allocate_hook(ptr, size);
  25. }
  26. static void reportDeallocation(void *ptr) {
  27. if (SCUDO_ENABLE_HOOKS)
  28. if (__scudo_deallocate_hook)
  29. __scudo_deallocate_hook(ptr);
  30. }
  31. INTERFACE WEAK void *operator new(size_t size) {
  32. void *Ptr = Allocator.allocate(size, scudo::Chunk::Origin::New);
  33. reportAllocation(Ptr, size);
  34. return Ptr;
  35. }
  36. INTERFACE WEAK void *operator new[](size_t size) {
  37. void *Ptr = Allocator.allocate(size, scudo::Chunk::Origin::NewArray);
  38. reportAllocation(Ptr, size);
  39. return Ptr;
  40. }
  41. INTERFACE WEAK void *operator new(size_t size,
  42. std::nothrow_t const &) NOEXCEPT {
  43. void *Ptr = Allocator.allocate(size, scudo::Chunk::Origin::New);
  44. reportAllocation(Ptr, size);
  45. return Ptr;
  46. }
  47. INTERFACE WEAK void *operator new[](size_t size,
  48. std::nothrow_t const &) NOEXCEPT {
  49. void *Ptr = Allocator.allocate(size, scudo::Chunk::Origin::NewArray);
  50. reportAllocation(Ptr, size);
  51. return Ptr;
  52. }
  53. INTERFACE WEAK void *operator new(size_t size, std::align_val_t align) {
  54. void *Ptr = Allocator.allocate(size, scudo::Chunk::Origin::New,
  55. static_cast<scudo::uptr>(align));
  56. reportAllocation(Ptr, size);
  57. return Ptr;
  58. }
  59. INTERFACE WEAK void *operator new[](size_t size, std::align_val_t align) {
  60. void *Ptr = Allocator.allocate(size, scudo::Chunk::Origin::NewArray,
  61. static_cast<scudo::uptr>(align));
  62. reportAllocation(Ptr, size);
  63. return Ptr;
  64. }
  65. INTERFACE WEAK void *operator new(size_t size, std::align_val_t align,
  66. std::nothrow_t const &) NOEXCEPT {
  67. void *Ptr = Allocator.allocate(size, scudo::Chunk::Origin::New,
  68. static_cast<scudo::uptr>(align));
  69. reportAllocation(Ptr, size);
  70. return Ptr;
  71. }
  72. INTERFACE WEAK void *operator new[](size_t size, std::align_val_t align,
  73. std::nothrow_t const &) NOEXCEPT {
  74. void *Ptr = Allocator.allocate(size, scudo::Chunk::Origin::NewArray,
  75. static_cast<scudo::uptr>(align));
  76. reportAllocation(Ptr, size);
  77. return Ptr;
  78. }
  79. INTERFACE WEAK void operator delete(void *ptr) NOEXCEPT {
  80. reportDeallocation(ptr);
  81. Allocator.deallocate(ptr, scudo::Chunk::Origin::New);
  82. }
  83. INTERFACE WEAK void operator delete[](void *ptr) NOEXCEPT {
  84. reportDeallocation(ptr);
  85. Allocator.deallocate(ptr, scudo::Chunk::Origin::NewArray);
  86. }
  87. INTERFACE WEAK void operator delete(void *ptr,
  88. std::nothrow_t const &) NOEXCEPT {
  89. reportDeallocation(ptr);
  90. Allocator.deallocate(ptr, scudo::Chunk::Origin::New);
  91. }
  92. INTERFACE WEAK void operator delete[](void *ptr,
  93. std::nothrow_t const &) NOEXCEPT {
  94. reportDeallocation(ptr);
  95. Allocator.deallocate(ptr, scudo::Chunk::Origin::NewArray);
  96. }
  97. INTERFACE WEAK void operator delete(void *ptr, size_t size) NOEXCEPT {
  98. reportDeallocation(ptr);
  99. Allocator.deallocate(ptr, scudo::Chunk::Origin::New, size);
  100. }
  101. INTERFACE WEAK void operator delete[](void *ptr, size_t size) NOEXCEPT {
  102. reportDeallocation(ptr);
  103. Allocator.deallocate(ptr, scudo::Chunk::Origin::NewArray, size);
  104. }
  105. INTERFACE WEAK void operator delete(void *ptr,
  106. std::align_val_t align) NOEXCEPT {
  107. reportDeallocation(ptr);
  108. Allocator.deallocate(ptr, scudo::Chunk::Origin::New, 0,
  109. static_cast<scudo::uptr>(align));
  110. }
  111. INTERFACE WEAK void operator delete[](void *ptr,
  112. std::align_val_t align) NOEXCEPT {
  113. reportDeallocation(ptr);
  114. Allocator.deallocate(ptr, scudo::Chunk::Origin::NewArray, 0,
  115. static_cast<scudo::uptr>(align));
  116. }
  117. INTERFACE WEAK void operator delete(void *ptr, std::align_val_t align,
  118. std::nothrow_t const &) NOEXCEPT {
  119. reportDeallocation(ptr);
  120. Allocator.deallocate(ptr, scudo::Chunk::Origin::New, 0,
  121. static_cast<scudo::uptr>(align));
  122. }
  123. INTERFACE WEAK void operator delete[](void *ptr, std::align_val_t align,
  124. std::nothrow_t const &) NOEXCEPT {
  125. reportDeallocation(ptr);
  126. Allocator.deallocate(ptr, scudo::Chunk::Origin::NewArray, 0,
  127. static_cast<scudo::uptr>(align));
  128. }
  129. INTERFACE WEAK void operator delete(void *ptr, size_t size,
  130. std::align_val_t align) NOEXCEPT {
  131. reportDeallocation(ptr);
  132. Allocator.deallocate(ptr, scudo::Chunk::Origin::New, size,
  133. static_cast<scudo::uptr>(align));
  134. }
  135. INTERFACE WEAK void operator delete[](void *ptr, size_t size,
  136. std::align_val_t align) NOEXCEPT {
  137. reportDeallocation(ptr);
  138. Allocator.deallocate(ptr, scudo::Chunk::Origin::NewArray, size,
  139. static_cast<scudo::uptr>(align));
  140. }
  141. #endif // !SCUDO_ANDROID || !_BIONIC