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