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
|
|
|
//===----------------------------------------------------------------------===//
|
2013-05-29 17:15:39 +08:00
|
|
|
#include "sanitizer_allocator.h"
|
|
|
|
#include "sanitizer_allocator_internal.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.
|
|
|
|
#if defined(SANITIZER_GO)
|
|
|
|
# if SANITIZER_LINUX && !SANITIZER_ANDROID
|
|
|
|
extern "C" void *__libc_malloc(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
|
|
|
# define LIBC_MALLOC __libc_malloc
|
|
|
|
# define LIBC_FREE __libc_free
|
|
|
|
# else
|
|
|
|
# include <stdlib.h>
|
|
|
|
# define LIBC_MALLOC malloc
|
|
|
|
# define LIBC_FREE free
|
|
|
|
# endif
|
2012-06-07 16:52:56 +08:00
|
|
|
|
2013-05-29 17:15:39 +08:00
|
|
|
static void *RawInternalAlloc(uptr size, InternalAllocatorCache *cache) {
|
|
|
|
(void)cache;
|
|
|
|
return LIBC_MALLOC(size);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void RawInternalFree(void *ptr, InternalAllocatorCache *cache) {
|
|
|
|
(void)cache;
|
|
|
|
LIBC_FREE(ptr);
|
|
|
|
}
|
|
|
|
|
|
|
|
InternalAllocator *internal_allocator() {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
#else // SANITIZER_GO
|
|
|
|
|
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) {
|
|
|
|
internal_allocator_instance->Init();
|
|
|
|
atomic_store(&internal_allocator_initialized, 1, memory_order_release);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return internal_allocator_instance;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void *RawInternalAlloc(uptr size, InternalAllocatorCache *cache) {
|
|
|
|
if (cache == 0) {
|
|
|
|
SpinMutexLock l(&internal_allocator_cache_mu);
|
|
|
|
return internal_allocator()->Allocate(&internal_allocator_cache, size, 8,
|
|
|
|
false);
|
|
|
|
}
|
|
|
|
return internal_allocator()->Allocate(cache, size, 8, false);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void RawInternalFree(void *ptr, InternalAllocatorCache *cache) {
|
|
|
|
if (cache == 0) {
|
|
|
|
SpinMutexLock l(&internal_allocator_cache_mu);
|
|
|
|
return internal_allocator()->Deallocate(&internal_allocator_cache, ptr);
|
|
|
|
}
|
|
|
|
internal_allocator()->Deallocate(cache, ptr);
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif // SANITIZER_GO
|
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
|
|
|
|
2013-05-29 17:15:39 +08:00
|
|
|
void *InternalAlloc(uptr size, InternalAllocatorCache *cache) {
|
2012-07-06 00:18:28 +08:00
|
|
|
if (size + sizeof(u64) < size)
|
|
|
|
return 0;
|
2013-05-29 17:15:39 +08:00
|
|
|
void *p = RawInternalAlloc(size + sizeof(u64), cache);
|
2012-07-06 00:18:28 +08:00
|
|
|
if (p == 0)
|
|
|
|
return 0;
|
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);
|
|
|
|
}
|
|
|
|
|
2013-05-29 17:15:39 +08:00
|
|
|
void InternalFree(void *addr, InternalAllocatorCache *cache) {
|
2012-07-06 00:18:28 +08:00
|
|
|
if (addr == 0)
|
|
|
|
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
|
|
|
|
static LowLevelAllocateCallback low_level_alloc_callback;
|
|
|
|
|
|
|
|
void *LowLevelAllocator::Allocate(uptr size) {
|
2012-08-27 22:51:36 +08:00
|
|
|
// Align allocation size.
|
|
|
|
size = RoundUpTo(size, 8);
|
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_ =
|
|
|
|
(char*)MmapOrDie(size_to_allocate, __FUNCTION__);
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
void SetLowLevelAllocateCallback(LowLevelAllocateCallback callback) {
|
|
|
|
low_level_alloc_callback = callback;
|
|
|
|
}
|
|
|
|
|
2013-01-25 19:46:22 +08:00
|
|
|
bool CallocShouldReturnNullDueToOverflow(uptr size, uptr n) {
|
2013-01-25 20:22:21 +08:00
|
|
|
if (!size) return false;
|
|
|
|
uptr max = (uptr)-1L;
|
|
|
|
return (max / size) < n;
|
2013-01-25 19:46:22 +08:00
|
|
|
}
|
|
|
|
|
2012-06-07 16:52:56 +08:00
|
|
|
} // namespace __sanitizer
|