2012-06-07 16:52:56 +08:00
|
|
|
//===-- sanitizer_allocator.cc --------------------------------------------===//
|
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// This file is shared between AddressSanitizer and ThreadSanitizer
|
|
|
|
// run-time libraries.
|
2013-05-29 17:15:39 +08:00
|
|
|
// This allocator is used inside run-times.
|
2012-06-07 16:52:56 +08:00
|
|
|
//===----------------------------------------------------------------------===//
|
2015-10-01 07:52:54 +08:00
|
|
|
|
2013-05-29 17:15:39 +08:00
|
|
|
#include "sanitizer_allocator.h"
|
2016-09-30 07:00:54 +08:00
|
|
|
|
2017-07-19 03:11:04 +08:00
|
|
|
#include "sanitizer_allocator_checks.h"
|
2013-05-29 17:15:39 +08:00
|
|
|
#include "sanitizer_allocator_internal.h"
|
2016-09-30 07:00:54 +08:00
|
|
|
#include "sanitizer_atomic.h"
|
2012-06-07 16:52:56 +08:00
|
|
|
#include "sanitizer_common.h"
|
|
|
|
|
2013-05-29 17:15:39 +08:00
|
|
|
namespace __sanitizer {
|
|
|
|
|
|
|
|
// ThreadSanitizer for Go uses libc malloc/free.
|
2016-10-29 04:14:18 +08:00
|
|
|
#if SANITIZER_GO || defined(SANITIZER_USE_MALLOC)
|
2013-05-29 17:15:39 +08:00
|
|
|
# if SANITIZER_LINUX && !SANITIZER_ANDROID
|
|
|
|
extern "C" void *__libc_malloc(uptr size);
|
2016-10-29 04:14:18 +08:00
|
|
|
# if !SANITIZER_GO
|
2016-06-07 02:18:47 +08:00
|
|
|
extern "C" void *__libc_memalign(uptr alignment, uptr size);
|
2016-07-20 19:43:52 +08:00
|
|
|
# endif
|
2016-06-07 02:18:47 +08:00
|
|
|
extern "C" void *__libc_realloc(void *ptr, uptr size);
|
2012-06-09 17:21:44 +08:00
|
|
|
extern "C" void __libc_free(void *ptr);
|
2013-05-29 17:15:39 +08:00
|
|
|
# else
|
|
|
|
# include <stdlib.h>
|
2016-06-07 02:18:47 +08:00
|
|
|
# define __libc_malloc malloc
|
2016-10-29 04:14:18 +08:00
|
|
|
# if !SANITIZER_GO
|
2016-06-07 02:18:47 +08:00
|
|
|
static void *__libc_memalign(uptr alignment, uptr size) {
|
|
|
|
void *p;
|
|
|
|
uptr error = posix_memalign(&p, alignment, size);
|
|
|
|
if (error) return nullptr;
|
|
|
|
return p;
|
|
|
|
}
|
2016-07-20 19:43:52 +08:00
|
|
|
# endif
|
2016-06-07 02:18:47 +08:00
|
|
|
# define __libc_realloc realloc
|
|
|
|
# define __libc_free free
|
2013-05-29 17:15:39 +08:00
|
|
|
# endif
|
2012-06-07 16:52:56 +08:00
|
|
|
|
2016-06-07 02:18:47 +08:00
|
|
|
static void *RawInternalAlloc(uptr size, InternalAllocatorCache *cache,
|
|
|
|
uptr alignment) {
|
|
|
|
(void)cache;
|
2016-10-29 04:14:18 +08:00
|
|
|
#if !SANITIZER_GO
|
2016-06-07 02:18:47 +08:00
|
|
|
if (alignment == 0)
|
|
|
|
return __libc_malloc(size);
|
|
|
|
else
|
|
|
|
return __libc_memalign(alignment, size);
|
2016-07-20 19:43:52 +08:00
|
|
|
#else
|
|
|
|
// Windows does not provide __libc_memalign/posix_memalign. It provides
|
|
|
|
// __aligned_malloc, but the allocated blocks can't be passed to free,
|
|
|
|
// they need to be passed to __aligned_free. InternalAlloc interface does
|
|
|
|
// not account for such requirement. Alignemnt does not seem to be used
|
|
|
|
// anywhere in runtime, so just call __libc_malloc for now.
|
|
|
|
DCHECK_EQ(alignment, 0);
|
|
|
|
return __libc_malloc(size);
|
|
|
|
#endif
|
2016-06-07 02:18:47 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
static void *RawInternalRealloc(void *ptr, uptr size,
|
|
|
|
InternalAllocatorCache *cache) {
|
2013-05-29 17:15:39 +08:00
|
|
|
(void)cache;
|
2016-06-07 02:18:47 +08:00
|
|
|
return __libc_realloc(ptr, size);
|
2013-05-29 17:15:39 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
static void RawInternalFree(void *ptr, InternalAllocatorCache *cache) {
|
|
|
|
(void)cache;
|
2016-06-07 02:18:47 +08:00
|
|
|
__libc_free(ptr);
|
2013-05-29 17:15:39 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
InternalAllocator *internal_allocator() {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2016-10-29 04:14:18 +08:00
|
|
|
#else // SANITIZER_GO || defined(SANITIZER_USE_MALLOC)
|
2013-05-29 17:15:39 +08:00
|
|
|
|
2013-05-29 18:41:53 +08:00
|
|
|
static ALIGNED(64) char internal_alloc_placeholder[sizeof(InternalAllocator)];
|
2013-05-29 17:15:39 +08:00
|
|
|
static atomic_uint8_t internal_allocator_initialized;
|
|
|
|
static StaticSpinMutex internal_alloc_init_mu;
|
|
|
|
|
|
|
|
static InternalAllocatorCache internal_allocator_cache;
|
|
|
|
static StaticSpinMutex internal_allocator_cache_mu;
|
|
|
|
|
|
|
|
InternalAllocator *internal_allocator() {
|
|
|
|
InternalAllocator *internal_allocator_instance =
|
|
|
|
reinterpret_cast<InternalAllocator *>(&internal_alloc_placeholder);
|
|
|
|
if (atomic_load(&internal_allocator_initialized, memory_order_acquire) == 0) {
|
|
|
|
SpinMutexLock l(&internal_alloc_init_mu);
|
|
|
|
if (atomic_load(&internal_allocator_initialized, memory_order_relaxed) ==
|
|
|
|
0) {
|
2017-06-21 05:23:02 +08:00
|
|
|
internal_allocator_instance->Init(kReleaseToOSIntervalNever);
|
2013-05-29 17:15:39 +08:00
|
|
|
atomic_store(&internal_allocator_initialized, 1, memory_order_release);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return internal_allocator_instance;
|
|
|
|
}
|
|
|
|
|
2016-06-07 02:18:47 +08:00
|
|
|
static void *RawInternalAlloc(uptr size, InternalAllocatorCache *cache,
|
|
|
|
uptr alignment) {
|
|
|
|
if (alignment == 0) alignment = 8;
|
2013-05-29 17:15:39 +08:00
|
|
|
if (cache == 0) {
|
|
|
|
SpinMutexLock l(&internal_allocator_cache_mu);
|
2016-06-07 02:18:47 +08:00
|
|
|
return internal_allocator()->Allocate(&internal_allocator_cache, size,
|
2017-06-17 05:00:03 +08:00
|
|
|
alignment);
|
2013-05-29 17:15:39 +08:00
|
|
|
}
|
2017-06-17 05:00:03 +08:00
|
|
|
return internal_allocator()->Allocate(cache, size, alignment);
|
2016-06-07 02:18:47 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
static void *RawInternalRealloc(void *ptr, uptr size,
|
|
|
|
InternalAllocatorCache *cache) {
|
|
|
|
uptr alignment = 8;
|
|
|
|
if (cache == 0) {
|
|
|
|
SpinMutexLock l(&internal_allocator_cache_mu);
|
|
|
|
return internal_allocator()->Reallocate(&internal_allocator_cache, ptr,
|
|
|
|
size, alignment);
|
|
|
|
}
|
|
|
|
return internal_allocator()->Reallocate(cache, ptr, size, alignment);
|
2013-05-29 17:15:39 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
static void RawInternalFree(void *ptr, InternalAllocatorCache *cache) {
|
2015-10-01 07:52:54 +08:00
|
|
|
if (!cache) {
|
2013-05-29 17:15:39 +08:00
|
|
|
SpinMutexLock l(&internal_allocator_cache_mu);
|
|
|
|
return internal_allocator()->Deallocate(&internal_allocator_cache, ptr);
|
|
|
|
}
|
|
|
|
internal_allocator()->Deallocate(cache, ptr);
|
|
|
|
}
|
|
|
|
|
2016-10-29 04:14:18 +08:00
|
|
|
#endif // SANITIZER_GO || defined(SANITIZER_USE_MALLOC)
|
2012-06-07 16:52:56 +08:00
|
|
|
|
2012-06-25 23:09:24 +08:00
|
|
|
const u64 kBlockMagic = 0x6A6CB03ABCEBC041ull;
|
2012-06-07 16:52:56 +08:00
|
|
|
|
2016-06-07 02:18:47 +08:00
|
|
|
void *InternalAlloc(uptr size, InternalAllocatorCache *cache, uptr alignment) {
|
2012-07-06 00:18:28 +08:00
|
|
|
if (size + sizeof(u64) < size)
|
2015-10-01 07:52:54 +08:00
|
|
|
return nullptr;
|
2016-06-07 02:18:47 +08:00
|
|
|
void *p = RawInternalAlloc(size + sizeof(u64), cache, alignment);
|
2018-01-18 07:20:36 +08:00
|
|
|
if (UNLIKELY(!p))
|
|
|
|
return DieOnFailure::OnOOM();
|
2012-06-25 23:09:24 +08:00
|
|
|
((u64*)p)[0] = kBlockMagic;
|
2012-06-07 16:52:56 +08:00
|
|
|
return (char*)p + sizeof(u64);
|
|
|
|
}
|
|
|
|
|
2016-06-07 02:18:47 +08:00
|
|
|
void *InternalRealloc(void *addr, uptr size, InternalAllocatorCache *cache) {
|
|
|
|
if (!addr)
|
|
|
|
return InternalAlloc(size, cache);
|
|
|
|
if (size + sizeof(u64) < size)
|
|
|
|
return nullptr;
|
|
|
|
addr = (char*)addr - sizeof(u64);
|
|
|
|
size = size + sizeof(u64);
|
|
|
|
CHECK_EQ(kBlockMagic, ((u64*)addr)[0]);
|
|
|
|
void *p = RawInternalRealloc(addr, size, cache);
|
2018-01-18 07:20:36 +08:00
|
|
|
if (UNLIKELY(!p))
|
|
|
|
return DieOnFailure::OnOOM();
|
2016-06-07 02:18:47 +08:00
|
|
|
return (char*)p + sizeof(u64);
|
|
|
|
}
|
|
|
|
|
|
|
|
void *InternalCalloc(uptr count, uptr size, InternalAllocatorCache *cache) {
|
2017-07-19 03:11:04 +08:00
|
|
|
if (UNLIKELY(CheckForCallocOverflow(count, size)))
|
2018-01-18 07:20:36 +08:00
|
|
|
return DieOnFailure::OnBadRequest();
|
2016-06-07 02:18:47 +08:00
|
|
|
void *p = InternalAlloc(count * size, cache);
|
2018-01-18 07:20:36 +08:00
|
|
|
if (LIKELY(p))
|
|
|
|
internal_memset(p, 0, count * size);
|
2016-06-07 02:18:47 +08:00
|
|
|
return p;
|
|
|
|
}
|
|
|
|
|
2013-05-29 17:15:39 +08:00
|
|
|
void InternalFree(void *addr, InternalAllocatorCache *cache) {
|
2015-10-01 07:52:54 +08:00
|
|
|
if (!addr)
|
2012-07-06 00:18:28 +08:00
|
|
|
return;
|
2012-06-07 16:52:56 +08:00
|
|
|
addr = (char*)addr - sizeof(u64);
|
2013-05-29 17:15:39 +08:00
|
|
|
CHECK_EQ(kBlockMagic, ((u64*)addr)[0]);
|
2012-06-07 16:52:56 +08:00
|
|
|
((u64*)addr)[0] = 0;
|
2013-05-29 17:15:39 +08:00
|
|
|
RawInternalFree(addr, cache);
|
2012-06-07 16:52:56 +08:00
|
|
|
}
|
|
|
|
|
2012-08-27 17:30:58 +08:00
|
|
|
// LowLevelAllocator
|
2017-11-21 09:01:32 +08:00
|
|
|
constexpr uptr kLowLevelAllocatorDefaultAlignment = 8;
|
|
|
|
static uptr low_level_alloc_min_alignment = kLowLevelAllocatorDefaultAlignment;
|
2012-08-27 17:30:58 +08:00
|
|
|
static LowLevelAllocateCallback low_level_alloc_callback;
|
|
|
|
|
|
|
|
void *LowLevelAllocator::Allocate(uptr size) {
|
2012-08-27 22:51:36 +08:00
|
|
|
// Align allocation size.
|
2017-11-21 09:01:32 +08:00
|
|
|
size = RoundUpTo(size, low_level_alloc_min_alignment);
|
2012-08-27 17:30:58 +08:00
|
|
|
if (allocated_end_ - allocated_current_ < (sptr)size) {
|
2012-11-24 13:03:11 +08:00
|
|
|
uptr size_to_allocate = Max(size, GetPageSizeCached());
|
2012-08-27 17:30:58 +08:00
|
|
|
allocated_current_ =
|
2014-02-27 04:33:22 +08:00
|
|
|
(char*)MmapOrDie(size_to_allocate, __func__);
|
2012-08-27 17:30:58 +08:00
|
|
|
allocated_end_ = allocated_current_ + size_to_allocate;
|
|
|
|
if (low_level_alloc_callback) {
|
|
|
|
low_level_alloc_callback((uptr)allocated_current_,
|
|
|
|
size_to_allocate);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
CHECK(allocated_end_ - allocated_current_ >= (sptr)size);
|
|
|
|
void *res = allocated_current_;
|
|
|
|
allocated_current_ += size;
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
2017-11-21 09:01:32 +08:00
|
|
|
void SetLowLevelAllocateMinAlignment(uptr alignment) {
|
|
|
|
CHECK(IsPowerOfTwo(alignment));
|
|
|
|
low_level_alloc_min_alignment = Max(alignment, low_level_alloc_min_alignment);
|
|
|
|
}
|
|
|
|
|
2012-08-27 17:30:58 +08:00
|
|
|
void SetLowLevelAllocateCallback(LowLevelAllocateCallback callback) {
|
|
|
|
low_level_alloc_callback = callback;
|
|
|
|
}
|
|
|
|
|
2017-06-21 05:23:02 +08:00
|
|
|
static atomic_uint8_t allocator_out_of_memory = {0};
|
|
|
|
static atomic_uint8_t allocator_may_return_null = {0};
|
2016-09-30 07:00:54 +08:00
|
|
|
|
2017-06-21 05:23:02 +08:00
|
|
|
bool IsAllocatorOutOfMemory() {
|
|
|
|
return atomic_load_relaxed(&allocator_out_of_memory);
|
|
|
|
}
|
2016-09-30 07:00:54 +08:00
|
|
|
|
2017-06-21 05:23:02 +08:00
|
|
|
// Prints error message and kills the program.
|
|
|
|
void NORETURN ReportAllocatorCannotReturnNull() {
|
2013-09-06 17:25:11 +08:00
|
|
|
Report("%s's allocator is terminating the process instead of returning 0\n",
|
|
|
|
SanitizerToolName);
|
|
|
|
Report("If you don't like this behavior set allocator_may_return_null=1\n");
|
|
|
|
CHECK(0);
|
2014-12-13 04:07:35 +08:00
|
|
|
Die();
|
2013-09-06 17:25:11 +08:00
|
|
|
}
|
|
|
|
|
2017-06-21 05:23:02 +08:00
|
|
|
bool AllocatorMayReturnNull() {
|
|
|
|
return atomic_load(&allocator_may_return_null, memory_order_relaxed);
|
|
|
|
}
|
|
|
|
|
|
|
|
void SetAllocatorMayReturnNull(bool may_return_null) {
|
|
|
|
atomic_store(&allocator_may_return_null, may_return_null,
|
|
|
|
memory_order_relaxed);
|
|
|
|
}
|
|
|
|
|
|
|
|
void *ReturnNullOrDieOnFailure::OnBadRequest() {
|
|
|
|
if (AllocatorMayReturnNull())
|
|
|
|
return nullptr;
|
|
|
|
ReportAllocatorCannotReturnNull();
|
|
|
|
}
|
|
|
|
|
|
|
|
void *ReturnNullOrDieOnFailure::OnOOM() {
|
|
|
|
atomic_store_relaxed(&allocator_out_of_memory, 1);
|
|
|
|
if (AllocatorMayReturnNull())
|
|
|
|
return nullptr;
|
|
|
|
ReportAllocatorCannotReturnNull();
|
|
|
|
}
|
|
|
|
|
2017-06-29 05:58:57 +08:00
|
|
|
void NORETURN *DieOnFailure::OnBadRequest() {
|
2017-06-21 05:23:02 +08:00
|
|
|
ReportAllocatorCannotReturnNull();
|
|
|
|
}
|
|
|
|
|
2017-06-29 05:58:57 +08:00
|
|
|
void NORETURN *DieOnFailure::OnOOM() {
|
2017-06-21 05:23:02 +08:00
|
|
|
atomic_store_relaxed(&allocator_out_of_memory, 1);
|
|
|
|
ReportAllocatorCannotReturnNull();
|
|
|
|
}
|
|
|
|
|
2015-10-01 07:52:54 +08:00
|
|
|
} // namespace __sanitizer
|