168d75effSDimitry Andric //===-- sanitizer_allocator.cpp -------------------------------------------===//
268d75effSDimitry Andric //
368d75effSDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
468d75effSDimitry Andric // See https://llvm.org/LICENSE.txt for license information.
568d75effSDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
668d75effSDimitry Andric //
768d75effSDimitry Andric //===----------------------------------------------------------------------===//
868d75effSDimitry Andric //
968d75effSDimitry Andric // This file is shared between AddressSanitizer and ThreadSanitizer
1068d75effSDimitry Andric // run-time libraries.
1168d75effSDimitry Andric // This allocator is used inside run-times.
1268d75effSDimitry Andric //===----------------------------------------------------------------------===//
1368d75effSDimitry Andric
1468d75effSDimitry Andric #include "sanitizer_allocator.h"
1568d75effSDimitry Andric
1668d75effSDimitry Andric #include "sanitizer_allocator_checks.h"
1768d75effSDimitry Andric #include "sanitizer_allocator_internal.h"
1868d75effSDimitry Andric #include "sanitizer_atomic.h"
1968d75effSDimitry Andric #include "sanitizer_common.h"
200eae32dcSDimitry Andric #include "sanitizer_platform.h"
2168d75effSDimitry Andric
2268d75effSDimitry Andric namespace __sanitizer {
2368d75effSDimitry Andric
2468d75effSDimitry Andric // Default allocator names.
2568d75effSDimitry Andric const char *PrimaryAllocatorName = "SizeClassAllocator";
2668d75effSDimitry Andric const char *SecondaryAllocatorName = "LargeMmapAllocator";
2768d75effSDimitry Andric
28*0fca6ea1SDimitry Andric alignas(64) static char internal_alloc_placeholder[sizeof(InternalAllocator)];
2968d75effSDimitry Andric static atomic_uint8_t internal_allocator_initialized;
3068d75effSDimitry Andric static StaticSpinMutex internal_alloc_init_mu;
3168d75effSDimitry Andric
3268d75effSDimitry Andric static InternalAllocatorCache internal_allocator_cache;
3368d75effSDimitry Andric static StaticSpinMutex internal_allocator_cache_mu;
3468d75effSDimitry Andric
internal_allocator()3568d75effSDimitry Andric InternalAllocator *internal_allocator() {
3668d75effSDimitry Andric InternalAllocator *internal_allocator_instance =
3768d75effSDimitry Andric reinterpret_cast<InternalAllocator *>(&internal_alloc_placeholder);
3868d75effSDimitry Andric if (atomic_load(&internal_allocator_initialized, memory_order_acquire) == 0) {
3968d75effSDimitry Andric SpinMutexLock l(&internal_alloc_init_mu);
4068d75effSDimitry Andric if (atomic_load(&internal_allocator_initialized, memory_order_relaxed) ==
4168d75effSDimitry Andric 0) {
4268d75effSDimitry Andric internal_allocator_instance->Init(kReleaseToOSIntervalNever);
4368d75effSDimitry Andric atomic_store(&internal_allocator_initialized, 1, memory_order_release);
4468d75effSDimitry Andric }
4568d75effSDimitry Andric }
4668d75effSDimitry Andric return internal_allocator_instance;
4768d75effSDimitry Andric }
4868d75effSDimitry Andric
RawInternalAlloc(uptr size,InternalAllocatorCache * cache,uptr alignment)4968d75effSDimitry Andric static void *RawInternalAlloc(uptr size, InternalAllocatorCache *cache,
5068d75effSDimitry Andric uptr alignment) {
5168d75effSDimitry Andric if (alignment == 0) alignment = 8;
5268d75effSDimitry Andric if (cache == 0) {
5368d75effSDimitry Andric SpinMutexLock l(&internal_allocator_cache_mu);
5468d75effSDimitry Andric return internal_allocator()->Allocate(&internal_allocator_cache, size,
5568d75effSDimitry Andric alignment);
5668d75effSDimitry Andric }
5768d75effSDimitry Andric return internal_allocator()->Allocate(cache, size, alignment);
5868d75effSDimitry Andric }
5968d75effSDimitry Andric
RawInternalRealloc(void * ptr,uptr size,InternalAllocatorCache * cache)6068d75effSDimitry Andric static void *RawInternalRealloc(void *ptr, uptr size,
6168d75effSDimitry Andric InternalAllocatorCache *cache) {
6268d75effSDimitry Andric uptr alignment = 8;
6368d75effSDimitry Andric if (cache == 0) {
6468d75effSDimitry Andric SpinMutexLock l(&internal_allocator_cache_mu);
6568d75effSDimitry Andric return internal_allocator()->Reallocate(&internal_allocator_cache, ptr,
6668d75effSDimitry Andric size, alignment);
6768d75effSDimitry Andric }
6868d75effSDimitry Andric return internal_allocator()->Reallocate(cache, ptr, size, alignment);
6968d75effSDimitry Andric }
7068d75effSDimitry Andric
RawInternalFree(void * ptr,InternalAllocatorCache * cache)7168d75effSDimitry Andric static void RawInternalFree(void *ptr, InternalAllocatorCache *cache) {
7268d75effSDimitry Andric if (!cache) {
7368d75effSDimitry Andric SpinMutexLock l(&internal_allocator_cache_mu);
7468d75effSDimitry Andric return internal_allocator()->Deallocate(&internal_allocator_cache, ptr);
7568d75effSDimitry Andric }
7668d75effSDimitry Andric internal_allocator()->Deallocate(cache, ptr);
7768d75effSDimitry Andric }
7868d75effSDimitry Andric
ReportInternalAllocatorOutOfMemory(uptr requested_size)7968d75effSDimitry Andric static void NORETURN ReportInternalAllocatorOutOfMemory(uptr requested_size) {
8068d75effSDimitry Andric SetAllocatorOutOfMemory();
8168d75effSDimitry Andric Report("FATAL: %s: internal allocator is out of memory trying to allocate "
8268d75effSDimitry Andric "0x%zx bytes\n", SanitizerToolName, requested_size);
8368d75effSDimitry Andric Die();
8468d75effSDimitry Andric }
8568d75effSDimitry Andric
InternalAlloc(uptr size,InternalAllocatorCache * cache,uptr alignment)8668d75effSDimitry Andric void *InternalAlloc(uptr size, InternalAllocatorCache *cache, uptr alignment) {
87fe6060f1SDimitry Andric void *p = RawInternalAlloc(size, cache, alignment);
8868d75effSDimitry Andric if (UNLIKELY(!p))
89fe6060f1SDimitry Andric ReportInternalAllocatorOutOfMemory(size);
90fe6060f1SDimitry Andric return p;
9168d75effSDimitry Andric }
9268d75effSDimitry Andric
InternalRealloc(void * addr,uptr size,InternalAllocatorCache * cache)9368d75effSDimitry Andric void *InternalRealloc(void *addr, uptr size, InternalAllocatorCache *cache) {
94fe6060f1SDimitry Andric void *p = RawInternalRealloc(addr, size, cache);
9568d75effSDimitry Andric if (UNLIKELY(!p))
96fe6060f1SDimitry Andric ReportInternalAllocatorOutOfMemory(size);
97fe6060f1SDimitry Andric return p;
9868d75effSDimitry Andric }
9968d75effSDimitry Andric
InternalReallocArray(void * addr,uptr count,uptr size,InternalAllocatorCache * cache)10068d75effSDimitry Andric void *InternalReallocArray(void *addr, uptr count, uptr size,
10168d75effSDimitry Andric InternalAllocatorCache *cache) {
10268d75effSDimitry Andric if (UNLIKELY(CheckForCallocOverflow(count, size))) {
10368d75effSDimitry Andric Report(
10468d75effSDimitry Andric "FATAL: %s: reallocarray parameters overflow: count * size (%zd * %zd) "
10568d75effSDimitry Andric "cannot be represented in type size_t\n",
10668d75effSDimitry Andric SanitizerToolName, count, size);
10768d75effSDimitry Andric Die();
10868d75effSDimitry Andric }
10968d75effSDimitry Andric return InternalRealloc(addr, count * size, cache);
11068d75effSDimitry Andric }
11168d75effSDimitry Andric
InternalCalloc(uptr count,uptr size,InternalAllocatorCache * cache)11268d75effSDimitry Andric void *InternalCalloc(uptr count, uptr size, InternalAllocatorCache *cache) {
11368d75effSDimitry Andric if (UNLIKELY(CheckForCallocOverflow(count, size))) {
11468d75effSDimitry Andric Report("FATAL: %s: calloc parameters overflow: count * size (%zd * %zd) "
11568d75effSDimitry Andric "cannot be represented in type size_t\n", SanitizerToolName, count,
11668d75effSDimitry Andric size);
11768d75effSDimitry Andric Die();
11868d75effSDimitry Andric }
11968d75effSDimitry Andric void *p = InternalAlloc(count * size, cache);
12068d75effSDimitry Andric if (LIKELY(p))
12168d75effSDimitry Andric internal_memset(p, 0, count * size);
12268d75effSDimitry Andric return p;
12368d75effSDimitry Andric }
12468d75effSDimitry Andric
InternalFree(void * addr,InternalAllocatorCache * cache)12568d75effSDimitry Andric void InternalFree(void *addr, InternalAllocatorCache *cache) {
126fe6060f1SDimitry Andric RawInternalFree(addr, cache);
12768d75effSDimitry Andric }
12868d75effSDimitry Andric
InternalAllocatorLock()12904eeddc0SDimitry Andric void InternalAllocatorLock() SANITIZER_NO_THREAD_SAFETY_ANALYSIS {
1304824e7fdSDimitry Andric internal_allocator_cache_mu.Lock();
1314824e7fdSDimitry Andric internal_allocator()->ForceLock();
1324824e7fdSDimitry Andric }
1334824e7fdSDimitry Andric
InternalAllocatorUnlock()13404eeddc0SDimitry Andric void InternalAllocatorUnlock() SANITIZER_NO_THREAD_SAFETY_ANALYSIS {
1354824e7fdSDimitry Andric internal_allocator()->ForceUnlock();
1364824e7fdSDimitry Andric internal_allocator_cache_mu.Unlock();
1374824e7fdSDimitry Andric }
1384824e7fdSDimitry Andric
13968d75effSDimitry Andric // LowLevelAllocator
14068d75effSDimitry Andric constexpr uptr kLowLevelAllocatorDefaultAlignment = 8;
1415f757f3fSDimitry Andric constexpr uptr kMinNumPagesRounded = 16;
1425f757f3fSDimitry Andric constexpr uptr kMinRoundedSize = 65536;
14368d75effSDimitry Andric static uptr low_level_alloc_min_alignment = kLowLevelAllocatorDefaultAlignment;
14468d75effSDimitry Andric static LowLevelAllocateCallback low_level_alloc_callback;
14568d75effSDimitry Andric
1465f757f3fSDimitry Andric static LowLevelAllocator Alloc;
GetGlobalLowLevelAllocator()1475f757f3fSDimitry Andric LowLevelAllocator &GetGlobalLowLevelAllocator() { return Alloc; }
1485f757f3fSDimitry Andric
Allocate(uptr size)14968d75effSDimitry Andric void *LowLevelAllocator::Allocate(uptr size) {
15068d75effSDimitry Andric // Align allocation size.
15168d75effSDimitry Andric size = RoundUpTo(size, low_level_alloc_min_alignment);
15268d75effSDimitry Andric if (allocated_end_ - allocated_current_ < (sptr)size) {
1535f757f3fSDimitry Andric uptr size_to_allocate = RoundUpTo(
1545f757f3fSDimitry Andric size, Min(GetPageSizeCached() * kMinNumPagesRounded, kMinRoundedSize));
15506c3fb27SDimitry Andric allocated_current_ = (char *)MmapOrDie(size_to_allocate, __func__);
15668d75effSDimitry Andric allocated_end_ = allocated_current_ + size_to_allocate;
15768d75effSDimitry Andric if (low_level_alloc_callback) {
15806c3fb27SDimitry Andric low_level_alloc_callback((uptr)allocated_current_, size_to_allocate);
15968d75effSDimitry Andric }
16068d75effSDimitry Andric }
16168d75effSDimitry Andric CHECK(allocated_end_ - allocated_current_ >= (sptr)size);
16268d75effSDimitry Andric void *res = allocated_current_;
16368d75effSDimitry Andric allocated_current_ += size;
16468d75effSDimitry Andric return res;
16568d75effSDimitry Andric }
16668d75effSDimitry Andric
SetLowLevelAllocateMinAlignment(uptr alignment)16768d75effSDimitry Andric void SetLowLevelAllocateMinAlignment(uptr alignment) {
16868d75effSDimitry Andric CHECK(IsPowerOfTwo(alignment));
16968d75effSDimitry Andric low_level_alloc_min_alignment = Max(alignment, low_level_alloc_min_alignment);
17068d75effSDimitry Andric }
17168d75effSDimitry Andric
SetLowLevelAllocateCallback(LowLevelAllocateCallback callback)17268d75effSDimitry Andric void SetLowLevelAllocateCallback(LowLevelAllocateCallback callback) {
17368d75effSDimitry Andric low_level_alloc_callback = callback;
17468d75effSDimitry Andric }
17568d75effSDimitry Andric
17668d75effSDimitry Andric // Allocator's OOM and other errors handling support.
17768d75effSDimitry Andric
17868d75effSDimitry Andric static atomic_uint8_t allocator_out_of_memory = {0};
17968d75effSDimitry Andric static atomic_uint8_t allocator_may_return_null = {0};
18068d75effSDimitry Andric
IsAllocatorOutOfMemory()18168d75effSDimitry Andric bool IsAllocatorOutOfMemory() {
18268d75effSDimitry Andric return atomic_load_relaxed(&allocator_out_of_memory);
18368d75effSDimitry Andric }
18468d75effSDimitry Andric
SetAllocatorOutOfMemory()18568d75effSDimitry Andric void SetAllocatorOutOfMemory() {
18668d75effSDimitry Andric atomic_store_relaxed(&allocator_out_of_memory, 1);
18768d75effSDimitry Andric }
18868d75effSDimitry Andric
AllocatorMayReturnNull()18968d75effSDimitry Andric bool AllocatorMayReturnNull() {
19068d75effSDimitry Andric return atomic_load(&allocator_may_return_null, memory_order_relaxed);
19168d75effSDimitry Andric }
19268d75effSDimitry Andric
SetAllocatorMayReturnNull(bool may_return_null)19368d75effSDimitry Andric void SetAllocatorMayReturnNull(bool may_return_null) {
19468d75effSDimitry Andric atomic_store(&allocator_may_return_null, may_return_null,
19568d75effSDimitry Andric memory_order_relaxed);
19668d75effSDimitry Andric }
19768d75effSDimitry Andric
PrintHintAllocatorCannotReturnNull()19868d75effSDimitry Andric void PrintHintAllocatorCannotReturnNull() {
19968d75effSDimitry Andric Report("HINT: if you don't care about these errors you may set "
20068d75effSDimitry Andric "allocator_may_return_null=1\n");
20168d75effSDimitry Andric }
20268d75effSDimitry Andric
2030eae32dcSDimitry Andric static atomic_uint8_t rss_limit_exceeded;
2040eae32dcSDimitry Andric
IsRssLimitExceeded()2050eae32dcSDimitry Andric bool IsRssLimitExceeded() {
2060eae32dcSDimitry Andric return atomic_load(&rss_limit_exceeded, memory_order_relaxed);
2070eae32dcSDimitry Andric }
2080eae32dcSDimitry Andric
SetRssLimitExceeded(bool limit_exceeded)2090eae32dcSDimitry Andric void SetRssLimitExceeded(bool limit_exceeded) {
2100eae32dcSDimitry Andric atomic_store(&rss_limit_exceeded, limit_exceeded, memory_order_relaxed);
2110eae32dcSDimitry Andric }
2120eae32dcSDimitry Andric
21368d75effSDimitry Andric } // namespace __sanitizer
214