1 //===-- sanitizer_allocator.cc --------------------------------------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file is shared between AddressSanitizer and ThreadSanitizer
11 // run-time libraries.
12 // This allocator is used inside run-times.
13 //===----------------------------------------------------------------------===//
15 #include "sanitizer_allocator.h"
17 #include "sanitizer_allocator_checks.h"
18 #include "sanitizer_allocator_internal.h"
19 #include "sanitizer_atomic.h"
20 #include "sanitizer_common.h"
22 namespace __sanitizer {
24 // Default allocator names.
25 const char *PrimaryAllocatorName = "SizeClassAllocator";
26 const char *SecondaryAllocatorName = "LargeMmapAllocator";
28 // ThreadSanitizer for Go uses libc malloc/free.
29 #if SANITIZER_GO || defined(SANITIZER_USE_MALLOC)
30 # if SANITIZER_LINUX && !SANITIZER_ANDROID
31 extern "C" void *__libc_malloc(uptr size);
33 extern "C" void *__libc_memalign(uptr alignment, uptr size);
35 extern "C" void *__libc_realloc(void *ptr, uptr size);
36 extern "C" void __libc_free(void *ptr);
39 # define __libc_malloc malloc
41 static void *__libc_memalign(uptr alignment, uptr size) {
43 uptr error = posix_memalign(&p, alignment, size);
44 if (error) return nullptr;
48 # define __libc_realloc realloc
49 # define __libc_free free
52 static void *RawInternalAlloc(uptr size, InternalAllocatorCache *cache,
57 return __libc_malloc(size);
59 return __libc_memalign(alignment, size);
61 // Windows does not provide __libc_memalign/posix_memalign. It provides
62 // __aligned_malloc, but the allocated blocks can't be passed to free,
63 // they need to be passed to __aligned_free. InternalAlloc interface does
64 // not account for such requirement. Alignemnt does not seem to be used
65 // anywhere in runtime, so just call __libc_malloc for now.
66 DCHECK_EQ(alignment, 0);
67 return __libc_malloc(size);
71 static void *RawInternalRealloc(void *ptr, uptr size,
72 InternalAllocatorCache *cache) {
74 return __libc_realloc(ptr, size);
77 static void RawInternalFree(void *ptr, InternalAllocatorCache *cache) {
82 InternalAllocator *internal_allocator() {
86 #else // SANITIZER_GO || defined(SANITIZER_USE_MALLOC)
88 static ALIGNED(64) char internal_alloc_placeholder[sizeof(InternalAllocator)];
89 static atomic_uint8_t internal_allocator_initialized;
90 static StaticSpinMutex internal_alloc_init_mu;
92 static InternalAllocatorCache internal_allocator_cache;
93 static StaticSpinMutex internal_allocator_cache_mu;
95 InternalAllocator *internal_allocator() {
96 InternalAllocator *internal_allocator_instance =
97 reinterpret_cast<InternalAllocator *>(&internal_alloc_placeholder);
98 if (atomic_load(&internal_allocator_initialized, memory_order_acquire) == 0) {
99 SpinMutexLock l(&internal_alloc_init_mu);
100 if (atomic_load(&internal_allocator_initialized, memory_order_relaxed) ==
102 internal_allocator_instance->Init(kReleaseToOSIntervalNever);
103 atomic_store(&internal_allocator_initialized, 1, memory_order_release);
106 return internal_allocator_instance;
109 static void *RawInternalAlloc(uptr size, InternalAllocatorCache *cache,
111 if (alignment == 0) alignment = 8;
113 SpinMutexLock l(&internal_allocator_cache_mu);
114 return internal_allocator()->Allocate(&internal_allocator_cache, size,
117 return internal_allocator()->Allocate(cache, size, alignment);
120 static void *RawInternalRealloc(void *ptr, uptr size,
121 InternalAllocatorCache *cache) {
124 SpinMutexLock l(&internal_allocator_cache_mu);
125 return internal_allocator()->Reallocate(&internal_allocator_cache, ptr,
128 return internal_allocator()->Reallocate(cache, ptr, size, alignment);
131 static void RawInternalFree(void *ptr, InternalAllocatorCache *cache) {
133 SpinMutexLock l(&internal_allocator_cache_mu);
134 return internal_allocator()->Deallocate(&internal_allocator_cache, ptr);
136 internal_allocator()->Deallocate(cache, ptr);
139 #endif // SANITIZER_GO || defined(SANITIZER_USE_MALLOC)
141 const u64 kBlockMagic = 0x6A6CB03ABCEBC041ull;
143 static void NORETURN ReportInternalAllocatorOutOfMemory(uptr requested_size) {
144 SetAllocatorOutOfMemory();
145 Report("FATAL: %s: internal allocator is out of memory trying to allocate "
146 "0x%zx bytes\n", SanitizerToolName, requested_size);
150 void *InternalAlloc(uptr size, InternalAllocatorCache *cache, uptr alignment) {
151 if (size + sizeof(u64) < size)
153 void *p = RawInternalAlloc(size + sizeof(u64), cache, alignment);
155 ReportInternalAllocatorOutOfMemory(size + sizeof(u64));
156 ((u64*)p)[0] = kBlockMagic;
157 return (char*)p + sizeof(u64);
160 void *InternalRealloc(void *addr, uptr size, InternalAllocatorCache *cache) {
162 return InternalAlloc(size, cache);
163 if (size + sizeof(u64) < size)
165 addr = (char*)addr - sizeof(u64);
166 size = size + sizeof(u64);
167 CHECK_EQ(kBlockMagic, ((u64*)addr)[0]);
168 void *p = RawInternalRealloc(addr, size, cache);
170 ReportInternalAllocatorOutOfMemory(size);
171 return (char*)p + sizeof(u64);
174 void *InternalCalloc(uptr count, uptr size, InternalAllocatorCache *cache) {
175 if (UNLIKELY(CheckForCallocOverflow(count, size))) {
176 Report("FATAL: %s: calloc parameters overflow: count * size (%zd * %zd) "
177 "cannot be represented in type size_t\n", SanitizerToolName, count,
181 void *p = InternalAlloc(count * size, cache);
183 internal_memset(p, 0, count * size);
187 void InternalFree(void *addr, InternalAllocatorCache *cache) {
190 addr = (char*)addr - sizeof(u64);
191 CHECK_EQ(kBlockMagic, ((u64*)addr)[0]);
193 RawInternalFree(addr, cache);
197 constexpr uptr kLowLevelAllocatorDefaultAlignment = 8;
198 static uptr low_level_alloc_min_alignment = kLowLevelAllocatorDefaultAlignment;
199 static LowLevelAllocateCallback low_level_alloc_callback;
201 void *LowLevelAllocator::Allocate(uptr size) {
202 // Align allocation size.
203 size = RoundUpTo(size, low_level_alloc_min_alignment);
204 if (allocated_end_ - allocated_current_ < (sptr)size) {
205 uptr size_to_allocate = Max(size, GetPageSizeCached());
207 (char*)MmapOrDie(size_to_allocate, __func__);
208 allocated_end_ = allocated_current_ + size_to_allocate;
209 if (low_level_alloc_callback) {
210 low_level_alloc_callback((uptr)allocated_current_,
214 CHECK(allocated_end_ - allocated_current_ >= (sptr)size);
215 void *res = allocated_current_;
216 allocated_current_ += size;
220 void SetLowLevelAllocateMinAlignment(uptr alignment) {
221 CHECK(IsPowerOfTwo(alignment));
222 low_level_alloc_min_alignment = Max(alignment, low_level_alloc_min_alignment);
225 void SetLowLevelAllocateCallback(LowLevelAllocateCallback callback) {
226 low_level_alloc_callback = callback;
229 // Allocator's OOM and other errors handling support.
231 static atomic_uint8_t allocator_out_of_memory = {0};
232 static atomic_uint8_t allocator_may_return_null = {0};
234 bool IsAllocatorOutOfMemory() {
235 return atomic_load_relaxed(&allocator_out_of_memory);
238 void SetAllocatorOutOfMemory() {
239 atomic_store_relaxed(&allocator_out_of_memory, 1);
242 bool AllocatorMayReturnNull() {
243 return atomic_load(&allocator_may_return_null, memory_order_relaxed);
246 void SetAllocatorMayReturnNull(bool may_return_null) {
247 atomic_store(&allocator_may_return_null, may_return_null,
248 memory_order_relaxed);
251 void PrintHintAllocatorCannotReturnNull() {
252 Report("HINT: if you don't care about these errors you may set "
253 "allocator_may_return_null=1\n");
256 } // namespace __sanitizer