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 //===----------------------------------------------------------------------===//
14 #include "sanitizer_allocator.h"
15 #include "sanitizer_allocator_internal.h"
16 #include "sanitizer_common.h"
18 namespace __sanitizer {
20 // ThreadSanitizer for Go uses libc malloc/free.
21 #if defined(SANITIZER_GO) || defined(SANITIZER_USE_MALLOC)
22 # if SANITIZER_LINUX && !SANITIZER_ANDROID
23 extern "C" void *__libc_malloc(uptr size);
24 extern "C" void __libc_free(void *ptr);
25 # define LIBC_MALLOC __libc_malloc
26 # define LIBC_FREE __libc_free
29 # define LIBC_MALLOC malloc
30 # define LIBC_FREE free
33 static void *RawInternalAlloc(uptr size, InternalAllocatorCache *cache) {
35 return LIBC_MALLOC(size);
38 static void RawInternalFree(void *ptr, InternalAllocatorCache *cache) {
43 InternalAllocator *internal_allocator() {
49 static ALIGNED(64) char internal_alloc_placeholder[sizeof(InternalAllocator)];
50 static atomic_uint8_t internal_allocator_initialized;
51 static StaticSpinMutex internal_alloc_init_mu;
53 static InternalAllocatorCache internal_allocator_cache;
54 static StaticSpinMutex internal_allocator_cache_mu;
56 InternalAllocator *internal_allocator() {
57 InternalAllocator *internal_allocator_instance =
58 reinterpret_cast<InternalAllocator *>(&internal_alloc_placeholder);
59 if (atomic_load(&internal_allocator_initialized, memory_order_acquire) == 0) {
60 SpinMutexLock l(&internal_alloc_init_mu);
61 if (atomic_load(&internal_allocator_initialized, memory_order_relaxed) ==
63 internal_allocator_instance->Init(/* may_return_null*/ false);
64 atomic_store(&internal_allocator_initialized, 1, memory_order_release);
67 return internal_allocator_instance;
70 static void *RawInternalAlloc(uptr size, InternalAllocatorCache *cache) {
72 SpinMutexLock l(&internal_allocator_cache_mu);
73 return internal_allocator()->Allocate(&internal_allocator_cache, size, 8,
76 return internal_allocator()->Allocate(cache, size, 8, false);
79 static void RawInternalFree(void *ptr, InternalAllocatorCache *cache) {
81 SpinMutexLock l(&internal_allocator_cache_mu);
82 return internal_allocator()->Deallocate(&internal_allocator_cache, ptr);
84 internal_allocator()->Deallocate(cache, ptr);
87 #endif // SANITIZER_GO
89 const u64 kBlockMagic = 0x6A6CB03ABCEBC041ull;
91 void *InternalAlloc(uptr size, InternalAllocatorCache *cache) {
92 if (size + sizeof(u64) < size)
94 void *p = RawInternalAlloc(size + sizeof(u64), cache);
97 ((u64*)p)[0] = kBlockMagic;
98 return (char*)p + sizeof(u64);
101 void InternalFree(void *addr, InternalAllocatorCache *cache) {
104 addr = (char*)addr - sizeof(u64);
105 CHECK_EQ(kBlockMagic, ((u64*)addr)[0]);
107 RawInternalFree(addr, cache);
111 static LowLevelAllocateCallback low_level_alloc_callback;
113 void *LowLevelAllocator::Allocate(uptr size) {
114 // Align allocation size.
115 size = RoundUpTo(size, 8);
116 if (allocated_end_ - allocated_current_ < (sptr)size) {
117 uptr size_to_allocate = Max(size, GetPageSizeCached());
119 (char*)MmapOrDie(size_to_allocate, __func__);
120 allocated_end_ = allocated_current_ + size_to_allocate;
121 if (low_level_alloc_callback) {
122 low_level_alloc_callback((uptr)allocated_current_,
126 CHECK(allocated_end_ - allocated_current_ >= (sptr)size);
127 void *res = allocated_current_;
128 allocated_current_ += size;
132 void SetLowLevelAllocateCallback(LowLevelAllocateCallback callback) {
133 low_level_alloc_callback = callback;
136 bool CallocShouldReturnNullDueToOverflow(uptr size, uptr n) {
137 if (!size) return false;
138 uptr max = (uptr)-1L;
139 return (max / size) < n;
142 void NORETURN ReportAllocatorCannotReturnNull() {
143 Report("%s's allocator is terminating the process instead of returning 0\n",
145 Report("If you don't like this behavior set allocator_may_return_null=1\n");
150 } // namespace __sanitizer