1 //===-- sanitizer_allocator.cpp -------------------------------------------===//
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
7 //===----------------------------------------------------------------------===//
9 // This file is shared between AddressSanitizer and ThreadSanitizer
10 // run-time libraries.
11 // This allocator is used inside run-times.
12 //===----------------------------------------------------------------------===//
14 #include "sanitizer_allocator.h"
16 #include "sanitizer_allocator_checks.h"
17 #include "sanitizer_allocator_internal.h"
18 #include "sanitizer_atomic.h"
19 #include "sanitizer_common.h"
20 #include "sanitizer_platform.h"
22 namespace __sanitizer
{
24 // Default allocator names.
25 const char *PrimaryAllocatorName
= "SizeClassAllocator";
26 const char *SecondaryAllocatorName
= "LargeMmapAllocator";
28 static ALIGNED(64) char internal_alloc_placeholder
[sizeof(InternalAllocator
)];
29 static atomic_uint8_t internal_allocator_initialized
;
30 static StaticSpinMutex internal_alloc_init_mu
;
32 static InternalAllocatorCache internal_allocator_cache
;
33 static StaticSpinMutex internal_allocator_cache_mu
;
35 InternalAllocator
*internal_allocator() {
36 InternalAllocator
*internal_allocator_instance
=
37 reinterpret_cast<InternalAllocator
*>(&internal_alloc_placeholder
);
38 if (atomic_load(&internal_allocator_initialized
, memory_order_acquire
) == 0) {
39 SpinMutexLock
l(&internal_alloc_init_mu
);
40 if (atomic_load(&internal_allocator_initialized
, memory_order_relaxed
) ==
42 internal_allocator_instance
->Init(kReleaseToOSIntervalNever
);
43 atomic_store(&internal_allocator_initialized
, 1, memory_order_release
);
46 return internal_allocator_instance
;
49 static void *RawInternalAlloc(uptr size
, InternalAllocatorCache
*cache
,
51 if (alignment
== 0) alignment
= 8;
53 SpinMutexLock
l(&internal_allocator_cache_mu
);
54 return internal_allocator()->Allocate(&internal_allocator_cache
, size
,
57 return internal_allocator()->Allocate(cache
, size
, alignment
);
60 static void *RawInternalRealloc(void *ptr
, uptr size
,
61 InternalAllocatorCache
*cache
) {
64 SpinMutexLock
l(&internal_allocator_cache_mu
);
65 return internal_allocator()->Reallocate(&internal_allocator_cache
, ptr
,
68 return internal_allocator()->Reallocate(cache
, ptr
, size
, alignment
);
71 static void RawInternalFree(void *ptr
, InternalAllocatorCache
*cache
) {
73 SpinMutexLock
l(&internal_allocator_cache_mu
);
74 return internal_allocator()->Deallocate(&internal_allocator_cache
, ptr
);
76 internal_allocator()->Deallocate(cache
, ptr
);
79 static void NORETURN
ReportInternalAllocatorOutOfMemory(uptr requested_size
) {
80 SetAllocatorOutOfMemory();
81 Report("FATAL: %s: internal allocator is out of memory trying to allocate "
82 "0x%zx bytes\n", SanitizerToolName
, requested_size
);
86 void *InternalAlloc(uptr size
, InternalAllocatorCache
*cache
, uptr alignment
) {
87 void *p
= RawInternalAlloc(size
, cache
, alignment
);
89 ReportInternalAllocatorOutOfMemory(size
);
93 void *InternalRealloc(void *addr
, uptr size
, InternalAllocatorCache
*cache
) {
94 void *p
= RawInternalRealloc(addr
, size
, cache
);
96 ReportInternalAllocatorOutOfMemory(size
);
100 void *InternalReallocArray(void *addr
, uptr count
, uptr size
,
101 InternalAllocatorCache
*cache
) {
102 if (UNLIKELY(CheckForCallocOverflow(count
, size
))) {
104 "FATAL: %s: reallocarray parameters overflow: count * size (%zd * %zd) "
105 "cannot be represented in type size_t\n",
106 SanitizerToolName
, count
, size
);
109 return InternalRealloc(addr
, count
* size
, cache
);
112 void *InternalCalloc(uptr count
, uptr size
, InternalAllocatorCache
*cache
) {
113 if (UNLIKELY(CheckForCallocOverflow(count
, size
))) {
114 Report("FATAL: %s: calloc parameters overflow: count * size (%zd * %zd) "
115 "cannot be represented in type size_t\n", SanitizerToolName
, count
,
119 void *p
= InternalAlloc(count
* size
, cache
);
121 internal_memset(p
, 0, count
* size
);
125 void InternalFree(void *addr
, InternalAllocatorCache
*cache
) {
126 RawInternalFree(addr
, cache
);
129 void InternalAllocatorLock() SANITIZER_NO_THREAD_SAFETY_ANALYSIS
{
130 internal_allocator_cache_mu
.Lock();
131 internal_allocator()->ForceLock();
134 void InternalAllocatorUnlock() SANITIZER_NO_THREAD_SAFETY_ANALYSIS
{
135 internal_allocator()->ForceUnlock();
136 internal_allocator_cache_mu
.Unlock();
140 constexpr uptr kLowLevelAllocatorDefaultAlignment
= 8;
141 static uptr low_level_alloc_min_alignment
= kLowLevelAllocatorDefaultAlignment
;
142 static LowLevelAllocateCallback low_level_alloc_callback
;
144 void *LowLevelAllocator::Allocate(uptr size
) {
145 // Align allocation size.
146 size
= RoundUpTo(size
, low_level_alloc_min_alignment
);
147 if (allocated_end_
- allocated_current_
< (sptr
)size
) {
148 uptr size_to_allocate
= RoundUpTo(size
, GetPageSizeCached());
149 allocated_current_
= (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_
, size_to_allocate
);
155 CHECK(allocated_end_
- allocated_current_
>= (sptr
)size
);
156 void *res
= allocated_current_
;
157 allocated_current_
+= size
;
161 void SetLowLevelAllocateMinAlignment(uptr alignment
) {
162 CHECK(IsPowerOfTwo(alignment
));
163 low_level_alloc_min_alignment
= Max(alignment
, low_level_alloc_min_alignment
);
166 void SetLowLevelAllocateCallback(LowLevelAllocateCallback callback
) {
167 low_level_alloc_callback
= callback
;
170 // Allocator's OOM and other errors handling support.
172 static atomic_uint8_t allocator_out_of_memory
= {0};
173 static atomic_uint8_t allocator_may_return_null
= {0};
175 bool IsAllocatorOutOfMemory() {
176 return atomic_load_relaxed(&allocator_out_of_memory
);
179 void SetAllocatorOutOfMemory() {
180 atomic_store_relaxed(&allocator_out_of_memory
, 1);
183 bool AllocatorMayReturnNull() {
184 return atomic_load(&allocator_may_return_null
, memory_order_relaxed
);
187 void SetAllocatorMayReturnNull(bool may_return_null
) {
188 atomic_store(&allocator_may_return_null
, may_return_null
,
189 memory_order_relaxed
);
192 void PrintHintAllocatorCannotReturnNull() {
193 Report("HINT: if you don't care about these errors you may set "
194 "allocator_may_return_null=1\n");
197 static atomic_uint8_t rss_limit_exceeded
;
199 bool IsRssLimitExceeded() {
200 return atomic_load(&rss_limit_exceeded
, memory_order_relaxed
);
203 void SetRssLimitExceeded(bool limit_exceeded
) {
204 atomic_store(&rss_limit_exceeded
, limit_exceeded
, memory_order_relaxed
);
207 } // namespace __sanitizer