sanitizer_allocator.cpp 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  1. //===-- sanitizer_allocator.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 shared between AddressSanitizer and ThreadSanitizer
  10. // run-time libraries.
  11. // This allocator is used inside run-times.
  12. //===----------------------------------------------------------------------===//
  13. #include "sanitizer_allocator.h"
  14. #include "sanitizer_allocator_checks.h"
  15. #include "sanitizer_allocator_internal.h"
  16. #include "sanitizer_atomic.h"
  17. #include "sanitizer_common.h"
  18. #include "sanitizer_platform.h"
  19. namespace __sanitizer {
  20. // Default allocator names.
  21. const char *PrimaryAllocatorName = "SizeClassAllocator";
  22. const char *SecondaryAllocatorName = "LargeMmapAllocator";
  23. static ALIGNED(64) char internal_alloc_placeholder[sizeof(InternalAllocator)];
  24. static atomic_uint8_t internal_allocator_initialized;
  25. static StaticSpinMutex internal_alloc_init_mu;
  26. static InternalAllocatorCache internal_allocator_cache;
  27. static StaticSpinMutex internal_allocator_cache_mu;
  28. InternalAllocator *internal_allocator() {
  29. InternalAllocator *internal_allocator_instance =
  30. reinterpret_cast<InternalAllocator *>(&internal_alloc_placeholder);
  31. if (atomic_load(&internal_allocator_initialized, memory_order_acquire) == 0) {
  32. SpinMutexLock l(&internal_alloc_init_mu);
  33. if (atomic_load(&internal_allocator_initialized, memory_order_relaxed) ==
  34. 0) {
  35. internal_allocator_instance->Init(kReleaseToOSIntervalNever);
  36. atomic_store(&internal_allocator_initialized, 1, memory_order_release);
  37. }
  38. }
  39. return internal_allocator_instance;
  40. }
  41. static void *RawInternalAlloc(uptr size, InternalAllocatorCache *cache,
  42. uptr alignment) {
  43. if (alignment == 0) alignment = 8;
  44. if (cache == 0) {
  45. SpinMutexLock l(&internal_allocator_cache_mu);
  46. return internal_allocator()->Allocate(&internal_allocator_cache, size,
  47. alignment);
  48. }
  49. return internal_allocator()->Allocate(cache, size, alignment);
  50. }
  51. static void *RawInternalRealloc(void *ptr, uptr size,
  52. InternalAllocatorCache *cache) {
  53. uptr alignment = 8;
  54. if (cache == 0) {
  55. SpinMutexLock l(&internal_allocator_cache_mu);
  56. return internal_allocator()->Reallocate(&internal_allocator_cache, ptr,
  57. size, alignment);
  58. }
  59. return internal_allocator()->Reallocate(cache, ptr, size, alignment);
  60. }
  61. static void RawInternalFree(void *ptr, InternalAllocatorCache *cache) {
  62. if (!cache) {
  63. SpinMutexLock l(&internal_allocator_cache_mu);
  64. return internal_allocator()->Deallocate(&internal_allocator_cache, ptr);
  65. }
  66. internal_allocator()->Deallocate(cache, ptr);
  67. }
  68. static void NORETURN ReportInternalAllocatorOutOfMemory(uptr requested_size) {
  69. SetAllocatorOutOfMemory();
  70. Report("FATAL: %s: internal allocator is out of memory trying to allocate "
  71. "0x%zx bytes\n", SanitizerToolName, requested_size);
  72. Die();
  73. }
  74. void *InternalAlloc(uptr size, InternalAllocatorCache *cache, uptr alignment) {
  75. void *p = RawInternalAlloc(size, cache, alignment);
  76. if (UNLIKELY(!p))
  77. ReportInternalAllocatorOutOfMemory(size);
  78. return p;
  79. }
  80. void *InternalRealloc(void *addr, uptr size, InternalAllocatorCache *cache) {
  81. void *p = RawInternalRealloc(addr, size, cache);
  82. if (UNLIKELY(!p))
  83. ReportInternalAllocatorOutOfMemory(size);
  84. return p;
  85. }
  86. void *InternalReallocArray(void *addr, uptr count, uptr size,
  87. InternalAllocatorCache *cache) {
  88. if (UNLIKELY(CheckForCallocOverflow(count, size))) {
  89. Report(
  90. "FATAL: %s: reallocarray parameters overflow: count * size (%zd * %zd) "
  91. "cannot be represented in type size_t\n",
  92. SanitizerToolName, count, size);
  93. Die();
  94. }
  95. return InternalRealloc(addr, count * size, cache);
  96. }
  97. void *InternalCalloc(uptr count, uptr size, InternalAllocatorCache *cache) {
  98. if (UNLIKELY(CheckForCallocOverflow(count, size))) {
  99. Report("FATAL: %s: calloc parameters overflow: count * size (%zd * %zd) "
  100. "cannot be represented in type size_t\n", SanitizerToolName, count,
  101. size);
  102. Die();
  103. }
  104. void *p = InternalAlloc(count * size, cache);
  105. if (LIKELY(p))
  106. internal_memset(p, 0, count * size);
  107. return p;
  108. }
  109. void InternalFree(void *addr, InternalAllocatorCache *cache) {
  110. RawInternalFree(addr, cache);
  111. }
  112. void InternalAllocatorLock() SANITIZER_NO_THREAD_SAFETY_ANALYSIS {
  113. internal_allocator_cache_mu.Lock();
  114. internal_allocator()->ForceLock();
  115. }
  116. void InternalAllocatorUnlock() SANITIZER_NO_THREAD_SAFETY_ANALYSIS {
  117. internal_allocator()->ForceUnlock();
  118. internal_allocator_cache_mu.Unlock();
  119. }
  120. // LowLevelAllocator
  121. constexpr uptr kLowLevelAllocatorDefaultAlignment = 8;
  122. constexpr uptr kMinNumPagesRounded = 16;
  123. constexpr uptr kMinRoundedSize = 65536;
  124. static uptr low_level_alloc_min_alignment = kLowLevelAllocatorDefaultAlignment;
  125. static LowLevelAllocateCallback low_level_alloc_callback;
  126. static LowLevelAllocator Alloc;
  127. LowLevelAllocator &GetGlobalLowLevelAllocator() { return Alloc; }
  128. void *LowLevelAllocator::Allocate(uptr size) {
  129. // Align allocation size.
  130. size = RoundUpTo(size, low_level_alloc_min_alignment);
  131. if (allocated_end_ - allocated_current_ < (sptr)size) {
  132. uptr size_to_allocate = RoundUpTo(
  133. size, Min(GetPageSizeCached() * kMinNumPagesRounded, kMinRoundedSize));
  134. allocated_current_ = (char *)MmapOrDie(size_to_allocate, __func__);
  135. allocated_end_ = allocated_current_ + size_to_allocate;
  136. if (low_level_alloc_callback) {
  137. low_level_alloc_callback((uptr)allocated_current_, size_to_allocate);
  138. }
  139. }
  140. CHECK(allocated_end_ - allocated_current_ >= (sptr)size);
  141. void *res = allocated_current_;
  142. allocated_current_ += size;
  143. return res;
  144. }
  145. void SetLowLevelAllocateMinAlignment(uptr alignment) {
  146. CHECK(IsPowerOfTwo(alignment));
  147. low_level_alloc_min_alignment = Max(alignment, low_level_alloc_min_alignment);
  148. }
  149. void SetLowLevelAllocateCallback(LowLevelAllocateCallback callback) {
  150. low_level_alloc_callback = callback;
  151. }
  152. // Allocator's OOM and other errors handling support.
  153. static atomic_uint8_t allocator_out_of_memory = {0};
  154. static atomic_uint8_t allocator_may_return_null = {0};
  155. bool IsAllocatorOutOfMemory() {
  156. return atomic_load_relaxed(&allocator_out_of_memory);
  157. }
  158. void SetAllocatorOutOfMemory() {
  159. atomic_store_relaxed(&allocator_out_of_memory, 1);
  160. }
  161. bool AllocatorMayReturnNull() {
  162. return atomic_load(&allocator_may_return_null, memory_order_relaxed);
  163. }
  164. void SetAllocatorMayReturnNull(bool may_return_null) {
  165. atomic_store(&allocator_may_return_null, may_return_null,
  166. memory_order_relaxed);
  167. }
  168. void PrintHintAllocatorCannotReturnNull() {
  169. Report("HINT: if you don't care about these errors you may set "
  170. "allocator_may_return_null=1\n");
  171. }
  172. static atomic_uint8_t rss_limit_exceeded;
  173. bool IsRssLimitExceeded() {
  174. return atomic_load(&rss_limit_exceeded, memory_order_relaxed);
  175. }
  176. void SetRssLimitExceeded(bool limit_exceeded) {
  177. atomic_store(&rss_limit_exceeded, limit_exceeded, memory_order_relaxed);
  178. }
  179. } // namespace __sanitizer