2012-12-10 21:52:55 +08:00
|
|
|
//===-- asan_allocator2.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 a part of AddressSanitizer, an address sanity checker.
|
|
|
|
//
|
|
|
|
// Implementation of ASan's memory allocator, 2-nd version.
|
|
|
|
// This variant uses the allocator from sanitizer_common, i.e. the one shared
|
|
|
|
// with ThreadSanitizer and MemorySanitizer.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
#include "asan_allocator.h"
|
|
|
|
|
2012-12-14 20:15:09 +08:00
|
|
|
#include "asan_mapping.h"
|
2013-03-28 23:42:43 +08:00
|
|
|
#include "asan_poisoning.h"
|
2012-12-17 17:06:25 +08:00
|
|
|
#include "asan_report.h"
|
2012-12-11 22:41:31 +08:00
|
|
|
#include "asan_thread.h"
|
2012-12-10 21:52:55 +08:00
|
|
|
#include "sanitizer_common/sanitizer_allocator.h"
|
2013-05-06 19:27:58 +08:00
|
|
|
#include "sanitizer_common/sanitizer_flags.h"
|
2012-12-11 17:02:36 +08:00
|
|
|
#include "sanitizer_common/sanitizer_internal_defs.h"
|
2012-12-17 17:06:25 +08:00
|
|
|
#include "sanitizer_common/sanitizer_list.h"
|
2012-12-26 14:30:02 +08:00
|
|
|
#include "sanitizer_common/sanitizer_stackdepot.h"
|
2013-01-11 16:07:43 +08:00
|
|
|
#include "sanitizer_common/sanitizer_quarantine.h"
|
2013-05-21 21:46:41 +08:00
|
|
|
#include "lsan/lsan_common.h"
|
2012-12-10 21:52:55 +08:00
|
|
|
|
|
|
|
namespace __asan {
|
|
|
|
|
2012-12-14 20:15:09 +08:00
|
|
|
struct AsanMapUnmapCallback {
|
|
|
|
void OnMap(uptr p, uptr size) const {
|
|
|
|
PoisonShadow(p, size, kAsanHeapLeftRedzoneMagic);
|
2012-12-19 22:56:38 +08:00
|
|
|
// Statistics.
|
2013-03-20 18:11:24 +08:00
|
|
|
AsanStats &thread_stats = GetCurrentThreadStats();
|
2012-12-19 22:56:38 +08:00
|
|
|
thread_stats.mmaps++;
|
|
|
|
thread_stats.mmaped += size;
|
2012-12-14 20:15:09 +08:00
|
|
|
}
|
|
|
|
void OnUnmap(uptr p, uptr size) const {
|
|
|
|
PoisonShadow(p, size, 0);
|
2012-12-27 15:37:24 +08:00
|
|
|
// We are about to unmap a chunk of user memory.
|
|
|
|
// Mark the corresponding shadow memory as not needed.
|
|
|
|
// Since asan's mapping is compacting, the shadow chunk may be
|
|
|
|
// not page-aligned, so we only flush the page-aligned portion.
|
|
|
|
uptr page_size = GetPageSizeCached();
|
|
|
|
uptr shadow_beg = RoundUpTo(MemToShadow(p), page_size);
|
|
|
|
uptr shadow_end = RoundDownTo(MemToShadow(p + size), page_size);
|
|
|
|
FlushUnneededShadowMemory(shadow_beg, shadow_end - shadow_beg);
|
2012-12-19 22:56:38 +08:00
|
|
|
// Statistics.
|
2013-03-20 18:11:24 +08:00
|
|
|
AsanStats &thread_stats = GetCurrentThreadStats();
|
2012-12-19 22:56:38 +08:00
|
|
|
thread_stats.munmaps++;
|
|
|
|
thread_stats.munmaped += size;
|
2012-12-14 20:15:09 +08:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2012-12-10 21:52:55 +08:00
|
|
|
#if SANITIZER_WORDSIZE == 64
|
2013-01-23 21:27:43 +08:00
|
|
|
#if defined(__powerpc64__)
|
|
|
|
const uptr kAllocatorSpace = 0xa0000000000ULL;
|
2013-05-15 20:36:29 +08:00
|
|
|
const uptr kAllocatorSize = 0x20000000000ULL; // 2T.
|
2013-01-23 21:27:43 +08:00
|
|
|
#else
|
2012-12-10 21:52:55 +08:00
|
|
|
const uptr kAllocatorSpace = 0x600000000000ULL;
|
2013-03-12 17:04:49 +08:00
|
|
|
const uptr kAllocatorSize = 0x40000000000ULL; // 4T.
|
2013-05-15 20:36:29 +08:00
|
|
|
#endif
|
2012-12-21 20:26:31 +08:00
|
|
|
typedef DefaultSizeClassMap SizeClassMap;
|
2012-12-10 21:52:55 +08:00
|
|
|
typedef SizeClassAllocator64<kAllocatorSpace, kAllocatorSize, 0 /*metadata*/,
|
2012-12-21 20:26:31 +08:00
|
|
|
SizeClassMap, AsanMapUnmapCallback> PrimaryAllocator;
|
2012-12-10 21:52:55 +08:00
|
|
|
#elif SANITIZER_WORDSIZE == 32
|
|
|
|
static const u64 kAddressSpaceSize = 1ULL << 32;
|
2012-12-21 20:26:31 +08:00
|
|
|
typedef CompactSizeClassMap SizeClassMap;
|
2013-05-20 15:29:21 +08:00
|
|
|
static const uptr kRegionSizeLog = 20;
|
|
|
|
static const uptr kFlatByteMapSize = kAddressSpaceSize >> kRegionSizeLog;
|
2012-12-14 20:15:09 +08:00
|
|
|
typedef SizeClassAllocator32<0, kAddressSpaceSize, 16,
|
2013-05-20 15:29:21 +08:00
|
|
|
SizeClassMap, kRegionSizeLog,
|
|
|
|
FlatByteMap<kFlatByteMapSize>,
|
|
|
|
AsanMapUnmapCallback> PrimaryAllocator;
|
2012-12-10 21:52:55 +08:00
|
|
|
#endif
|
|
|
|
|
|
|
|
typedef SizeClassAllocatorLocalCache<PrimaryAllocator> AllocatorCache;
|
2012-12-14 20:15:09 +08:00
|
|
|
typedef LargeMmapAllocator<AsanMapUnmapCallback> SecondaryAllocator;
|
2012-12-10 21:52:55 +08:00
|
|
|
typedef CombinedAllocator<PrimaryAllocator, AllocatorCache,
|
|
|
|
SecondaryAllocator> Allocator;
|
|
|
|
|
2012-12-17 21:43:47 +08:00
|
|
|
// We can not use THREADLOCAL because it is not supported on some of the
|
|
|
|
// platforms we care about (OSX 10.6, Android).
|
|
|
|
// static THREADLOCAL AllocatorCache cache;
|
|
|
|
AllocatorCache *GetAllocatorCache(AsanThreadLocalMallocStorage *ms) {
|
|
|
|
CHECK(ms);
|
|
|
|
CHECK_LE(sizeof(AllocatorCache), sizeof(ms->allocator2_cache));
|
|
|
|
return reinterpret_cast<AllocatorCache *>(ms->allocator2_cache);
|
|
|
|
}
|
|
|
|
|
2012-12-11 22:41:31 +08:00
|
|
|
static Allocator allocator;
|
2012-12-10 21:52:55 +08:00
|
|
|
|
2012-12-11 22:41:31 +08:00
|
|
|
static const uptr kMaxAllowedMallocSize =
|
2012-12-17 17:06:25 +08:00
|
|
|
FIRST_32_SECOND_64(3UL << 30, 8UL << 30);
|
|
|
|
|
|
|
|
static const uptr kMaxThreadLocalQuarantine =
|
2012-12-17 22:57:25 +08:00
|
|
|
FIRST_32_SECOND_64(1 << 18, 1 << 20);
|
2012-12-17 17:06:25 +08:00
|
|
|
|
2012-12-11 22:41:31 +08:00
|
|
|
// Every chunk of memory allocated by this allocator can be in one of 3 states:
|
|
|
|
// CHUNK_AVAILABLE: the chunk is in the free list and ready to be allocated.
|
|
|
|
// CHUNK_ALLOCATED: the chunk is allocated and not yet freed.
|
|
|
|
// CHUNK_QUARANTINE: the chunk was freed and put into quarantine zone.
|
|
|
|
enum {
|
2012-12-26 18:41:24 +08:00
|
|
|
CHUNK_AVAILABLE = 0, // 0 is the default value even if we didn't set it.
|
2012-12-11 22:41:31 +08:00
|
|
|
CHUNK_ALLOCATED = 2,
|
|
|
|
CHUNK_QUARANTINE = 3
|
|
|
|
};
|
|
|
|
|
2012-12-26 18:41:24 +08:00
|
|
|
// Valid redzone sizes are 16, 32, 64, ... 2048, so we encode them in 3 bits.
|
|
|
|
// We use adaptive redzones: for larger allocation larger redzones are used.
|
|
|
|
static u32 RZLog2Size(u32 rz_log) {
|
|
|
|
CHECK_LT(rz_log, 8);
|
|
|
|
return 16 << rz_log;
|
|
|
|
}
|
|
|
|
|
|
|
|
static u32 RZSize2Log(u32 rz_size) {
|
|
|
|
CHECK_GE(rz_size, 16);
|
|
|
|
CHECK_LE(rz_size, 2048);
|
|
|
|
CHECK(IsPowerOfTwo(rz_size));
|
2013-02-08 20:02:00 +08:00
|
|
|
u32 res = Log2(rz_size) - 4;
|
2012-12-26 18:41:24 +08:00
|
|
|
CHECK_EQ(rz_size, RZLog2Size(res));
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
|
|
|
static uptr ComputeRZLog(uptr user_requested_size) {
|
|
|
|
u32 rz_log =
|
|
|
|
user_requested_size <= 64 - 16 ? 0 :
|
|
|
|
user_requested_size <= 128 - 32 ? 1 :
|
|
|
|
user_requested_size <= 512 - 64 ? 2 :
|
|
|
|
user_requested_size <= 4096 - 128 ? 3 :
|
|
|
|
user_requested_size <= (1 << 14) - 256 ? 4 :
|
|
|
|
user_requested_size <= (1 << 15) - 512 ? 5 :
|
|
|
|
user_requested_size <= (1 << 16) - 1024 ? 6 : 7;
|
|
|
|
return Max(rz_log, RZSize2Log(flags()->redzone));
|
|
|
|
}
|
|
|
|
|
2012-12-11 22:41:31 +08:00
|
|
|
// The memory chunk allocated from the underlying allocator looks like this:
|
|
|
|
// L L L L L L H H U U U U U U R R
|
|
|
|
// L -- left redzone words (0 or more bytes)
|
2012-12-20 22:35:06 +08:00
|
|
|
// H -- ChunkHeader (16 bytes), which is also a part of the left redzone.
|
2012-12-11 22:41:31 +08:00
|
|
|
// U -- user memory.
|
|
|
|
// R -- right redzone (0 or more bytes)
|
|
|
|
// ChunkBase consists of ChunkHeader and other bytes that overlap with user
|
|
|
|
// memory.
|
|
|
|
|
2012-12-20 22:35:06 +08:00
|
|
|
// If a memory chunk is allocated by memalign and we had to increase the
|
|
|
|
// allocation size to achieve the proper alignment, then we store this magic
|
|
|
|
// value in the first uptr word of the memory block and store the address of
|
|
|
|
// ChunkBase in the next uptr.
|
|
|
|
// M B ? ? ? L L L L L L H H U U U U U U
|
|
|
|
// M -- magic value kMemalignMagic
|
|
|
|
// B -- address of ChunkHeader pointing to the first 'H'
|
|
|
|
static const uptr kMemalignMagic = 0xCC6E96B9;
|
|
|
|
|
2012-12-26 12:52:07 +08:00
|
|
|
struct ChunkHeader {
|
2012-12-11 22:41:31 +08:00
|
|
|
// 1-st 8 bytes.
|
2012-12-26 12:52:07 +08:00
|
|
|
u32 chunk_state : 8; // Must be first.
|
|
|
|
u32 alloc_tid : 24;
|
2012-12-21 16:53:59 +08:00
|
|
|
|
2012-12-26 12:52:07 +08:00
|
|
|
u32 free_tid : 24;
|
|
|
|
u32 from_memalign : 1;
|
|
|
|
u32 alloc_type : 2;
|
2012-12-26 18:41:24 +08:00
|
|
|
u32 rz_log : 3;
|
2013-05-21 21:46:41 +08:00
|
|
|
u32 lsan_tag : 2;
|
2012-12-11 22:41:31 +08:00
|
|
|
// 2-nd 8 bytes
|
2012-12-26 12:52:07 +08:00
|
|
|
// This field is used for small sizes. For large sizes it is equal to
|
|
|
|
// SizeClassMap::kMaxSize and the actual size is stored in the
|
|
|
|
// SecondaryAllocator's metadata.
|
|
|
|
u32 user_requested_size;
|
|
|
|
u32 alloc_context_id;
|
2012-12-11 22:41:31 +08:00
|
|
|
};
|
|
|
|
|
2012-12-26 12:52:07 +08:00
|
|
|
struct ChunkBase : ChunkHeader {
|
|
|
|
// Header2, intersects with user memory.
|
|
|
|
u32 free_context_id;
|
2012-12-11 22:41:31 +08:00
|
|
|
};
|
|
|
|
|
2012-12-26 12:52:07 +08:00
|
|
|
static const uptr kChunkHeaderSize = sizeof(ChunkHeader);
|
|
|
|
static const uptr kChunkHeader2Size = sizeof(ChunkBase) - kChunkHeaderSize;
|
|
|
|
COMPILER_CHECK(kChunkHeaderSize == 16);
|
|
|
|
COMPILER_CHECK(kChunkHeader2Size <= 16);
|
2012-12-11 22:41:31 +08:00
|
|
|
|
|
|
|
struct AsanChunk: ChunkBase {
|
|
|
|
uptr Beg() { return reinterpret_cast<uptr>(this) + kChunkHeaderSize; }
|
2012-12-26 12:52:07 +08:00
|
|
|
uptr UsedSize() {
|
|
|
|
if (user_requested_size != SizeClassMap::kMaxSize)
|
|
|
|
return user_requested_size;
|
|
|
|
return *reinterpret_cast<uptr *>(allocator.GetMetaData(AllocBeg()));
|
|
|
|
}
|
|
|
|
void *AllocBeg() {
|
|
|
|
if (from_memalign)
|
2012-12-26 14:30:02 +08:00
|
|
|
return allocator.GetBlockBegin(reinterpret_cast<void *>(this));
|
2012-12-26 18:41:24 +08:00
|
|
|
return reinterpret_cast<void*>(Beg() - RZLog2Size(rz_log));
|
2012-12-26 12:52:07 +08:00
|
|
|
}
|
2013-05-20 16:20:17 +08:00
|
|
|
// If we don't use stack depot, we store the alloc/free stack traces
|
|
|
|
// in the chunk itself.
|
2012-12-17 14:31:53 +08:00
|
|
|
u32 *AllocStackBeg() {
|
2012-12-26 18:41:24 +08:00
|
|
|
return (u32*)(Beg() - RZLog2Size(rz_log));
|
2012-12-14 21:16:19 +08:00
|
|
|
}
|
|
|
|
uptr AllocStackSize() {
|
2012-12-26 18:41:24 +08:00
|
|
|
CHECK_LE(RZLog2Size(rz_log), kChunkHeaderSize);
|
|
|
|
return (RZLog2Size(rz_log) - kChunkHeaderSize) / sizeof(u32);
|
2012-12-17 14:31:53 +08:00
|
|
|
}
|
|
|
|
u32 *FreeStackBeg() {
|
|
|
|
return (u32*)(Beg() + kChunkHeader2Size);
|
|
|
|
}
|
|
|
|
uptr FreeStackSize() {
|
2012-12-26 20:20:35 +08:00
|
|
|
if (user_requested_size < kChunkHeader2Size) return 0;
|
|
|
|
uptr available = RoundUpTo(user_requested_size, SHADOW_GRANULARITY);
|
2012-12-17 14:31:53 +08:00
|
|
|
return (available - kChunkHeader2Size) / sizeof(u32);
|
2012-12-14 21:16:19 +08:00
|
|
|
}
|
2013-05-21 21:46:41 +08:00
|
|
|
bool AddrIsInside(uptr addr) {
|
|
|
|
return (addr >= Beg()) && (addr < Beg() + UsedSize());
|
|
|
|
}
|
2012-12-11 22:41:31 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
uptr AsanChunkView::Beg() { return chunk_->Beg(); }
|
2012-12-11 17:02:36 +08:00
|
|
|
uptr AsanChunkView::End() { return Beg() + UsedSize(); }
|
2012-12-11 22:41:31 +08:00
|
|
|
uptr AsanChunkView::UsedSize() { return chunk_->UsedSize(); }
|
|
|
|
uptr AsanChunkView::AllocTid() { return chunk_->alloc_tid; }
|
|
|
|
uptr AsanChunkView::FreeTid() { return chunk_->free_tid; }
|
|
|
|
|
2012-12-26 14:30:02 +08:00
|
|
|
static void GetStackTraceFromId(u32 id, StackTrace *stack) {
|
|
|
|
CHECK(id);
|
|
|
|
uptr size = 0;
|
|
|
|
const uptr *trace = StackDepotGet(id, &size);
|
|
|
|
CHECK_LT(size, kStackTraceMax);
|
|
|
|
internal_memcpy(stack->trace, trace, sizeof(uptr) * size);
|
|
|
|
stack->size = size;
|
|
|
|
}
|
|
|
|
|
2012-12-11 22:41:31 +08:00
|
|
|
void AsanChunkView::GetAllocStack(StackTrace *stack) {
|
2012-12-26 14:30:02 +08:00
|
|
|
if (flags()->use_stack_depot)
|
|
|
|
GetStackTraceFromId(chunk_->alloc_context_id, stack);
|
|
|
|
else
|
|
|
|
StackTrace::UncompressStack(stack, chunk_->AllocStackBeg(),
|
|
|
|
chunk_->AllocStackSize());
|
2012-12-11 22:41:31 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void AsanChunkView::GetFreeStack(StackTrace *stack) {
|
2012-12-26 14:30:02 +08:00
|
|
|
if (flags()->use_stack_depot)
|
|
|
|
GetStackTraceFromId(chunk_->free_context_id, stack);
|
|
|
|
else
|
|
|
|
StackTrace::UncompressStack(stack, chunk_->FreeStackBeg(),
|
|
|
|
chunk_->FreeStackSize());
|
2012-12-11 22:41:31 +08:00
|
|
|
}
|
|
|
|
|
2013-01-11 16:07:43 +08:00
|
|
|
struct QuarantineCallback;
|
|
|
|
typedef Quarantine<QuarantineCallback, AsanChunk> AsanQuarantine;
|
|
|
|
typedef AsanQuarantine::Cache QuarantineCache;
|
|
|
|
static AsanQuarantine quarantine(LINKER_INITIALIZED);
|
|
|
|
static QuarantineCache fallback_quarantine_cache(LINKER_INITIALIZED);
|
|
|
|
static AllocatorCache fallback_allocator_cache;
|
|
|
|
static SpinMutex fallback_mutex;
|
|
|
|
|
|
|
|
QuarantineCache *GetQuarantineCache(AsanThreadLocalMallocStorage *ms) {
|
2013-01-11 19:03:35 +08:00
|
|
|
CHECK(ms);
|
|
|
|
CHECK_LE(sizeof(QuarantineCache), sizeof(ms->quarantine_cache));
|
2013-01-11 16:07:43 +08:00
|
|
|
return reinterpret_cast<QuarantineCache *>(ms->quarantine_cache);
|
|
|
|
}
|
2012-12-20 16:53:41 +08:00
|
|
|
|
2013-01-11 16:07:43 +08:00
|
|
|
struct QuarantineCallback {
|
|
|
|
explicit QuarantineCallback(AllocatorCache *cache)
|
|
|
|
: cache_(cache) {
|
2012-12-17 17:06:25 +08:00
|
|
|
}
|
|
|
|
|
2013-01-11 16:07:43 +08:00
|
|
|
void Recycle(AsanChunk *m) {
|
2013-05-19 02:27:33 +08:00
|
|
|
CHECK_EQ(m->chunk_state, CHUNK_QUARANTINE);
|
2013-05-20 19:25:18 +08:00
|
|
|
atomic_store((atomic_uint8_t*)m, CHUNK_AVAILABLE, memory_order_relaxed);
|
2012-12-17 17:06:25 +08:00
|
|
|
CHECK_NE(m->alloc_tid, kInvalidTid);
|
|
|
|
CHECK_NE(m->free_tid, kInvalidTid);
|
|
|
|
PoisonShadow(m->Beg(),
|
2012-12-26 12:52:07 +08:00
|
|
|
RoundUpTo(m->UsedSize(), SHADOW_GRANULARITY),
|
2012-12-17 17:06:25 +08:00
|
|
|
kAsanHeapLeftRedzoneMagic);
|
2012-12-26 18:41:24 +08:00
|
|
|
void *p = reinterpret_cast<void *>(m->AllocBeg());
|
2012-12-20 22:35:06 +08:00
|
|
|
if (m->from_memalign) {
|
|
|
|
uptr *memalign_magic = reinterpret_cast<uptr *>(p);
|
|
|
|
CHECK_EQ(memalign_magic[0], kMemalignMagic);
|
|
|
|
CHECK_EQ(memalign_magic[1], reinterpret_cast<uptr>(m));
|
|
|
|
}
|
2012-12-20 16:53:41 +08:00
|
|
|
|
|
|
|
// Statistics.
|
2013-03-20 18:11:24 +08:00
|
|
|
AsanStats &thread_stats = GetCurrentThreadStats();
|
2012-12-20 16:53:41 +08:00
|
|
|
thread_stats.real_frees++;
|
|
|
|
thread_stats.really_freed += m->UsedSize();
|
|
|
|
|
2013-01-11 16:07:43 +08:00
|
|
|
allocator.Deallocate(cache_, p);
|
2012-12-17 17:06:25 +08:00
|
|
|
}
|
|
|
|
|
2013-01-11 16:07:43 +08:00
|
|
|
void *Allocate(uptr size) {
|
2013-01-11 19:03:35 +08:00
|
|
|
return allocator.Allocate(cache_, size, 1, false);
|
2013-01-11 16:07:43 +08:00
|
|
|
}
|
2012-12-17 17:06:25 +08:00
|
|
|
|
2013-01-11 16:07:43 +08:00
|
|
|
void Deallocate(void *p) {
|
|
|
|
allocator.Deallocate(cache_, p);
|
|
|
|
}
|
2012-12-17 17:06:25 +08:00
|
|
|
|
2013-01-11 16:07:43 +08:00
|
|
|
AllocatorCache *cache_;
|
|
|
|
};
|
2012-12-17 17:06:25 +08:00
|
|
|
|
2013-01-28 16:05:47 +08:00
|
|
|
void InitializeAllocator() {
|
2013-01-11 16:07:43 +08:00
|
|
|
allocator.Init();
|
|
|
|
quarantine.Init((uptr)flags()->quarantine_size, kMaxThreadLocalQuarantine);
|
2012-12-17 17:06:25 +08:00
|
|
|
}
|
2012-12-11 22:41:31 +08:00
|
|
|
|
2012-12-21 16:53:59 +08:00
|
|
|
static void *Allocate(uptr size, uptr alignment, StackTrace *stack,
|
2013-04-04 19:17:14 +08:00
|
|
|
AllocType alloc_type, bool can_fill) {
|
2013-01-28 16:05:47 +08:00
|
|
|
if (!asan_inited)
|
|
|
|
__asan_init();
|
2013-04-04 19:17:14 +08:00
|
|
|
Flags &fl = *flags();
|
2012-12-11 22:41:31 +08:00
|
|
|
CHECK(stack);
|
2012-12-26 20:20:35 +08:00
|
|
|
const uptr min_alignment = SHADOW_GRANULARITY;
|
|
|
|
if (alignment < min_alignment)
|
|
|
|
alignment = min_alignment;
|
2012-12-21 22:54:46 +08:00
|
|
|
if (size == 0) {
|
2013-01-29 15:51:34 +08:00
|
|
|
// We'd be happy to avoid allocating memory for zero-size requests, but
|
|
|
|
// some programs/tests depend on this behavior and assume that malloc would
|
|
|
|
// not return NULL even for zero-size allocations. Moreover, it looks like
|
|
|
|
// operator new should never return NULL, and results of consecutive "new"
|
|
|
|
// calls must be different even if the allocated size is zero.
|
|
|
|
size = 1;
|
2012-12-21 22:54:46 +08:00
|
|
|
}
|
2012-12-11 22:41:31 +08:00
|
|
|
CHECK(IsPowerOfTwo(alignment));
|
2012-12-26 18:41:24 +08:00
|
|
|
uptr rz_log = ComputeRZLog(size);
|
|
|
|
uptr rz_size = RZLog2Size(rz_log);
|
2013-05-20 16:20:17 +08:00
|
|
|
uptr rounded_size = RoundUpTo(Max(size, kChunkHeader2Size), alignment);
|
2012-12-11 22:41:31 +08:00
|
|
|
uptr needed_size = rounded_size + rz_size;
|
2012-12-26 20:20:35 +08:00
|
|
|
if (alignment > min_alignment)
|
2012-12-11 22:41:31 +08:00
|
|
|
needed_size += alignment;
|
2012-12-26 12:52:07 +08:00
|
|
|
bool using_primary_allocator = true;
|
2012-12-25 17:40:20 +08:00
|
|
|
// If we are allocating from the secondary allocator, there will be no
|
|
|
|
// automatic right redzone, so add the right redzone manually.
|
2012-12-26 12:52:07 +08:00
|
|
|
if (!PrimaryAllocator::CanAllocate(needed_size, alignment)) {
|
2012-12-25 17:40:20 +08:00
|
|
|
needed_size += rz_size;
|
2012-12-26 12:52:07 +08:00
|
|
|
using_primary_allocator = false;
|
|
|
|
}
|
2012-12-26 20:20:35 +08:00
|
|
|
CHECK(IsAligned(needed_size, min_alignment));
|
2012-12-11 22:41:31 +08:00
|
|
|
if (size > kMaxAllowedMallocSize || needed_size > kMaxAllowedMallocSize) {
|
|
|
|
Report("WARNING: AddressSanitizer failed to allocate %p bytes\n",
|
|
|
|
(void*)size);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2013-03-20 17:23:28 +08:00
|
|
|
AsanThread *t = GetCurrentThread();
|
2013-01-11 19:15:48 +08:00
|
|
|
void *allocated;
|
|
|
|
if (t) {
|
|
|
|
AllocatorCache *cache = GetAllocatorCache(&t->malloc_storage());
|
|
|
|
allocated = allocator.Allocate(cache, needed_size, 8, false);
|
|
|
|
} else {
|
|
|
|
SpinMutexLock l(&fallback_mutex);
|
2013-01-11 19:27:42 +08:00
|
|
|
AllocatorCache *cache = &fallback_allocator_cache;
|
2013-01-11 19:15:48 +08:00
|
|
|
allocated = allocator.Allocate(cache, needed_size, 8, false);
|
|
|
|
}
|
2012-12-11 22:41:31 +08:00
|
|
|
uptr alloc_beg = reinterpret_cast<uptr>(allocated);
|
2013-01-16 17:12:14 +08:00
|
|
|
// Clear the first allocated word (an old kMemalignMagic may still be there).
|
|
|
|
reinterpret_cast<uptr *>(alloc_beg)[0] = 0;
|
2012-12-11 22:41:31 +08:00
|
|
|
uptr alloc_end = alloc_beg + needed_size;
|
|
|
|
uptr beg_plus_redzone = alloc_beg + rz_size;
|
|
|
|
uptr user_beg = beg_plus_redzone;
|
|
|
|
if (!IsAligned(user_beg, alignment))
|
|
|
|
user_beg = RoundUpTo(user_beg, alignment);
|
|
|
|
uptr user_end = user_beg + size;
|
|
|
|
CHECK_LE(user_end, alloc_end);
|
|
|
|
uptr chunk_beg = user_beg - kChunkHeaderSize;
|
|
|
|
AsanChunk *m = reinterpret_cast<AsanChunk *>(chunk_beg);
|
2012-12-21 16:53:59 +08:00
|
|
|
m->alloc_type = alloc_type;
|
2012-12-26 18:41:24 +08:00
|
|
|
m->rz_log = rz_log;
|
2012-12-11 22:41:31 +08:00
|
|
|
u32 alloc_tid = t ? t->tid() : 0;
|
|
|
|
m->alloc_tid = alloc_tid;
|
|
|
|
CHECK_EQ(alloc_tid, m->alloc_tid); // Does alloc_tid fit into the bitfield?
|
2012-12-14 21:16:19 +08:00
|
|
|
m->free_tid = kInvalidTid;
|
2012-12-11 22:41:31 +08:00
|
|
|
m->from_memalign = user_beg != beg_plus_redzone;
|
2012-12-20 22:35:06 +08:00
|
|
|
if (m->from_memalign) {
|
|
|
|
CHECK_LE(beg_plus_redzone + 2 * sizeof(uptr), user_beg);
|
|
|
|
uptr *memalign_magic = reinterpret_cast<uptr *>(alloc_beg);
|
|
|
|
memalign_magic[0] = kMemalignMagic;
|
|
|
|
memalign_magic[1] = chunk_beg;
|
|
|
|
}
|
2012-12-26 12:52:07 +08:00
|
|
|
if (using_primary_allocator) {
|
|
|
|
CHECK(size);
|
|
|
|
m->user_requested_size = size;
|
|
|
|
CHECK(allocator.FromPrimary(allocated));
|
|
|
|
} else {
|
|
|
|
CHECK(!allocator.FromPrimary(allocated));
|
|
|
|
m->user_requested_size = SizeClassMap::kMaxSize;
|
2012-12-26 18:41:24 +08:00
|
|
|
uptr *meta = reinterpret_cast<uptr *>(allocator.GetMetaData(allocated));
|
|
|
|
meta[0] = size;
|
|
|
|
meta[1] = chunk_beg;
|
2012-12-26 12:52:07 +08:00
|
|
|
}
|
2012-12-26 14:30:02 +08:00
|
|
|
|
2013-04-04 19:17:14 +08:00
|
|
|
if (fl.use_stack_depot) {
|
2012-12-26 14:30:02 +08:00
|
|
|
m->alloc_context_id = StackDepotPut(stack->trace, stack->size);
|
|
|
|
} else {
|
|
|
|
m->alloc_context_id = 0;
|
|
|
|
StackTrace::CompressStack(stack, m->AllocStackBeg(), m->AllocStackSize());
|
|
|
|
}
|
2012-12-11 22:41:31 +08:00
|
|
|
|
2012-12-14 20:15:09 +08:00
|
|
|
uptr size_rounded_down_to_granularity = RoundDownTo(size, SHADOW_GRANULARITY);
|
|
|
|
// Unpoison the bulk of the memory region.
|
|
|
|
if (size_rounded_down_to_granularity)
|
|
|
|
PoisonShadow(user_beg, size_rounded_down_to_granularity, 0);
|
|
|
|
// Deal with the end of the region if size is not aligned to granularity.
|
2013-04-04 19:17:14 +08:00
|
|
|
if (size != size_rounded_down_to_granularity && fl.poison_heap) {
|
2012-12-14 20:15:09 +08:00
|
|
|
u8 *shadow = (u8*)MemToShadow(user_beg + size_rounded_down_to_granularity);
|
|
|
|
*shadow = size & (SHADOW_GRANULARITY - 1);
|
|
|
|
}
|
|
|
|
|
2013-03-20 18:11:24 +08:00
|
|
|
AsanStats &thread_stats = GetCurrentThreadStats();
|
2012-12-20 16:53:41 +08:00
|
|
|
thread_stats.mallocs++;
|
|
|
|
thread_stats.malloced += size;
|
2012-12-21 20:26:31 +08:00
|
|
|
thread_stats.malloced_redzones += needed_size - size;
|
|
|
|
uptr class_id = Min(kNumberOfSizeClasses, SizeClassMap::ClassID(needed_size));
|
|
|
|
thread_stats.malloced_by_size[class_id]++;
|
|
|
|
if (needed_size > SizeClassMap::kMaxSize)
|
|
|
|
thread_stats.malloc_large++;
|
2012-12-20 16:53:41 +08:00
|
|
|
|
2012-12-11 22:41:31 +08:00
|
|
|
void *res = reinterpret_cast<void *>(user_beg);
|
2013-04-04 19:17:14 +08:00
|
|
|
if (can_fill && fl.max_malloc_fill_size) {
|
|
|
|
uptr fill_size = Min(size, (uptr)fl.max_malloc_fill_size);
|
|
|
|
REAL(memset)(res, fl.malloc_fill_byte, fill_size);
|
|
|
|
}
|
2013-06-03 19:21:34 +08:00
|
|
|
m->lsan_tag =
|
|
|
|
t->lsan_disabled() ? __lsan::kSuppressed : __lsan::kDirectlyLeaked;
|
2013-05-20 19:25:18 +08:00
|
|
|
// Must be the last mutation of metadata in this function.
|
|
|
|
atomic_store((atomic_uint8_t *)m, CHUNK_ALLOCATED, memory_order_release);
|
2012-12-11 22:41:31 +08:00
|
|
|
ASAN_MALLOC_HOOK(res, size);
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
2013-05-20 21:05:58 +08:00
|
|
|
static void AtomicallySetQuarantineFlag(AsanChunk *m,
|
|
|
|
void *ptr, StackTrace *stack) {
|
2013-03-22 15:40:34 +08:00
|
|
|
u8 old_chunk_state = CHUNK_ALLOCATED;
|
2012-12-17 17:06:25 +08:00
|
|
|
// Flip the chunk_state atomically to avoid race on double-free.
|
2013-03-23 02:16:57 +08:00
|
|
|
if (!atomic_compare_exchange_strong((atomic_uint8_t*)m, &old_chunk_state,
|
2013-05-20 19:25:18 +08:00
|
|
|
CHUNK_QUARANTINE, memory_order_acquire)) {
|
2013-03-22 15:40:34 +08:00
|
|
|
if (old_chunk_state == CHUNK_QUARANTINE)
|
|
|
|
ReportDoubleFree((uptr)ptr, stack);
|
|
|
|
else
|
|
|
|
ReportFreeNotMalloced((uptr)ptr, stack);
|
|
|
|
}
|
|
|
|
CHECK_EQ(CHUNK_ALLOCATED, old_chunk_state);
|
2013-05-20 21:05:58 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// Expects the chunk to already be marked as quarantined by using
|
|
|
|
// AtomicallySetQuarantineFlag.
|
|
|
|
static void QuarantineChunk(AsanChunk *m, void *ptr,
|
|
|
|
StackTrace *stack, AllocType alloc_type) {
|
|
|
|
CHECK_EQ(m->chunk_state, CHUNK_QUARANTINE);
|
|
|
|
|
|
|
|
// FIXME: if the free hook produces an ASan report (e.g. due to a bug),
|
|
|
|
// printing the report may crash as the AsanChunk free-related fields have not
|
|
|
|
// been updated yet. We might need to introduce yet another chunk state to
|
|
|
|
// handle this correctly, but don't want to yet.
|
|
|
|
ASAN_FREE_HOOK(ptr);
|
2013-03-22 15:40:34 +08:00
|
|
|
|
2012-12-21 16:53:59 +08:00
|
|
|
if (m->alloc_type != alloc_type && flags()->alloc_dealloc_mismatch)
|
|
|
|
ReportAllocTypeMismatch((uptr)ptr, stack,
|
|
|
|
(AllocType)m->alloc_type, (AllocType)alloc_type);
|
2012-12-17 17:06:25 +08:00
|
|
|
|
2012-12-17 14:31:53 +08:00
|
|
|
CHECK_GE(m->alloc_tid, 0);
|
2012-12-17 22:57:25 +08:00
|
|
|
if (SANITIZER_WORDSIZE == 64) // On 32-bits this resides in user area.
|
|
|
|
CHECK_EQ(m->free_tid, kInvalidTid);
|
2013-03-20 17:23:28 +08:00
|
|
|
AsanThread *t = GetCurrentThread();
|
2012-12-17 14:31:53 +08:00
|
|
|
m->free_tid = t ? t->tid() : 0;
|
2012-12-26 14:30:02 +08:00
|
|
|
if (flags()->use_stack_depot) {
|
|
|
|
m->free_context_id = StackDepotPut(stack->trace, stack->size);
|
|
|
|
} else {
|
|
|
|
m->free_context_id = 0;
|
|
|
|
StackTrace::CompressStack(stack, m->FreeStackBeg(), m->FreeStackSize());
|
|
|
|
}
|
2012-12-14 20:15:09 +08:00
|
|
|
// Poison the region.
|
2012-12-17 17:06:25 +08:00
|
|
|
PoisonShadow(m->Beg(),
|
2012-12-26 12:52:07 +08:00
|
|
|
RoundUpTo(m->UsedSize(), SHADOW_GRANULARITY),
|
2012-12-14 20:15:09 +08:00
|
|
|
kAsanHeapFreeMagic);
|
2012-12-17 17:06:25 +08:00
|
|
|
|
2013-03-20 18:11:24 +08:00
|
|
|
AsanStats &thread_stats = GetCurrentThreadStats();
|
2012-12-20 16:53:41 +08:00
|
|
|
thread_stats.frees++;
|
|
|
|
thread_stats.freed += m->UsedSize();
|
|
|
|
|
2012-12-17 17:06:25 +08:00
|
|
|
// Push into quarantine.
|
|
|
|
if (t) {
|
2013-01-11 16:07:43 +08:00
|
|
|
AsanThreadLocalMallocStorage *ms = &t->malloc_storage();
|
|
|
|
AllocatorCache *ac = GetAllocatorCache(ms);
|
2013-01-11 19:03:35 +08:00
|
|
|
quarantine.Put(GetQuarantineCache(ms), QuarantineCallback(ac),
|
|
|
|
m, m->UsedSize());
|
2012-12-17 17:06:25 +08:00
|
|
|
} else {
|
2013-01-11 16:07:43 +08:00
|
|
|
SpinMutexLock l(&fallback_mutex);
|
|
|
|
AllocatorCache *ac = &fallback_allocator_cache;
|
2013-01-11 19:03:35 +08:00
|
|
|
quarantine.Put(&fallback_quarantine_cache, QuarantineCallback(ac),
|
|
|
|
m, m->UsedSize());
|
2012-12-17 17:06:25 +08:00
|
|
|
}
|
2012-12-11 22:41:31 +08:00
|
|
|
}
|
2012-12-11 17:02:36 +08:00
|
|
|
|
2013-05-20 21:05:58 +08:00
|
|
|
static void Deallocate(void *ptr, StackTrace *stack, AllocType alloc_type) {
|
|
|
|
uptr p = reinterpret_cast<uptr>(ptr);
|
|
|
|
if (p == 0) return;
|
|
|
|
|
|
|
|
uptr chunk_beg = p - kChunkHeaderSize;
|
|
|
|
AsanChunk *m = reinterpret_cast<AsanChunk *>(chunk_beg);
|
|
|
|
// Must mark the chunk as quarantined before any changes to its metadata.
|
|
|
|
AtomicallySetQuarantineFlag(m, ptr, stack);
|
|
|
|
QuarantineChunk(m, ptr, stack, alloc_type);
|
|
|
|
}
|
|
|
|
|
2012-12-17 17:06:25 +08:00
|
|
|
static void *Reallocate(void *old_ptr, uptr new_size, StackTrace *stack) {
|
|
|
|
CHECK(old_ptr && new_size);
|
|
|
|
uptr p = reinterpret_cast<uptr>(old_ptr);
|
|
|
|
uptr chunk_beg = p - kChunkHeaderSize;
|
|
|
|
AsanChunk *m = reinterpret_cast<AsanChunk *>(chunk_beg);
|
|
|
|
|
2013-03-20 18:11:24 +08:00
|
|
|
AsanStats &thread_stats = GetCurrentThreadStats();
|
2012-12-21 20:26:31 +08:00
|
|
|
thread_stats.reallocs++;
|
|
|
|
thread_stats.realloced += new_size;
|
|
|
|
|
2013-05-20 21:05:58 +08:00
|
|
|
// Must mark the chunk as quarantined before any changes to its metadata.
|
|
|
|
// This also ensures that other threads can't deallocate it in the meantime.
|
|
|
|
AtomicallySetQuarantineFlag(m, old_ptr, stack);
|
|
|
|
|
2012-12-17 17:06:25 +08:00
|
|
|
uptr old_size = m->UsedSize();
|
|
|
|
uptr memcpy_size = Min(new_size, old_size);
|
2013-04-04 19:17:14 +08:00
|
|
|
void *new_ptr = Allocate(new_size, 8, stack, FROM_MALLOC, true);
|
2012-12-17 17:06:25 +08:00
|
|
|
if (new_ptr) {
|
2013-02-26 20:59:06 +08:00
|
|
|
CHECK_NE(REAL(memcpy), (void*)0);
|
2012-12-17 17:06:25 +08:00
|
|
|
REAL(memcpy)(new_ptr, old_ptr, memcpy_size);
|
2013-05-20 21:05:58 +08:00
|
|
|
QuarantineChunk(m, old_ptr, stack, FROM_MALLOC);
|
2012-12-17 17:06:25 +08:00
|
|
|
}
|
|
|
|
return new_ptr;
|
|
|
|
}
|
|
|
|
|
2013-05-31 19:13:45 +08:00
|
|
|
// Assumes alloc_beg == allocator.GetBlockBegin(alloc_beg).
|
|
|
|
static AsanChunk *GetAsanChunk(void *alloc_beg) {
|
2012-12-17 22:57:25 +08:00
|
|
|
if (!alloc_beg) return 0;
|
2012-12-20 22:35:06 +08:00
|
|
|
uptr *memalign_magic = reinterpret_cast<uptr *>(alloc_beg);
|
|
|
|
if (memalign_magic[0] == kMemalignMagic) {
|
2012-12-26 18:41:24 +08:00
|
|
|
AsanChunk *m = reinterpret_cast<AsanChunk *>(memalign_magic[1]);
|
|
|
|
CHECK(m->from_memalign);
|
|
|
|
return m;
|
|
|
|
}
|
2013-05-31 19:13:45 +08:00
|
|
|
if (!allocator.FromPrimary(alloc_beg)) {
|
|
|
|
uptr *meta = reinterpret_cast<uptr *>(allocator.GetMetaData(alloc_beg));
|
2012-12-26 18:41:24 +08:00
|
|
|
AsanChunk *m = reinterpret_cast<AsanChunk *>(meta[1]);
|
|
|
|
return m;
|
2012-12-20 22:35:06 +08:00
|
|
|
}
|
2013-05-31 19:13:45 +08:00
|
|
|
uptr actual_size =
|
|
|
|
allocator.GetActuallyAllocatedSize(alloc_beg);
|
2012-12-26 18:41:24 +08:00
|
|
|
CHECK_LE(actual_size, SizeClassMap::kMaxSize);
|
|
|
|
// We know the actually allocted size, but we don't know the redzone size.
|
|
|
|
// Just try all possible redzone sizes.
|
|
|
|
for (u32 rz_log = 0; rz_log < 8; rz_log++) {
|
|
|
|
u32 rz_size = RZLog2Size(rz_log);
|
|
|
|
uptr max_possible_size = actual_size - rz_size;
|
|
|
|
if (ComputeRZLog(max_possible_size) != rz_log)
|
|
|
|
continue;
|
|
|
|
return reinterpret_cast<AsanChunk *>(
|
2013-05-31 19:13:45 +08:00
|
|
|
reinterpret_cast<uptr>(alloc_beg) + rz_size - kChunkHeaderSize);
|
2012-12-26 18:41:24 +08:00
|
|
|
}
|
|
|
|
return 0;
|
2012-12-17 22:57:25 +08:00
|
|
|
}
|
2012-12-17 17:06:25 +08:00
|
|
|
|
2013-05-31 19:13:45 +08:00
|
|
|
static AsanChunk *GetAsanChunkByAddr(uptr p) {
|
|
|
|
void *alloc_beg = allocator.GetBlockBegin(reinterpret_cast<void *>(p));
|
|
|
|
return GetAsanChunk(alloc_beg);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Allocator must be locked when this function is called.
|
|
|
|
static AsanChunk *GetAsanChunkByAddrFastLocked(uptr p) {
|
|
|
|
void *alloc_beg =
|
|
|
|
allocator.GetBlockBeginFastLocked(reinterpret_cast<void *>(p));
|
|
|
|
return GetAsanChunk(alloc_beg);
|
|
|
|
}
|
|
|
|
|
2012-12-17 22:57:25 +08:00
|
|
|
static uptr AllocationSize(uptr p) {
|
|
|
|
AsanChunk *m = GetAsanChunkByAddr(p);
|
|
|
|
if (!m) return 0;
|
|
|
|
if (m->chunk_state != CHUNK_ALLOCATED) return 0;
|
|
|
|
if (m->Beg() != p) return 0;
|
|
|
|
return m->UsedSize();
|
|
|
|
}
|
2012-12-17 17:06:25 +08:00
|
|
|
|
2012-12-19 16:32:50 +08:00
|
|
|
// We have an address between two chunks, and we want to report just one.
|
|
|
|
AsanChunk *ChooseChunk(uptr addr,
|
|
|
|
AsanChunk *left_chunk, AsanChunk *right_chunk) {
|
2012-12-26 18:41:24 +08:00
|
|
|
// Prefer an allocated chunk over freed chunk and freed chunk
|
|
|
|
// over available chunk.
|
|
|
|
if (left_chunk->chunk_state != right_chunk->chunk_state) {
|
|
|
|
if (left_chunk->chunk_state == CHUNK_ALLOCATED)
|
|
|
|
return left_chunk;
|
|
|
|
if (right_chunk->chunk_state == CHUNK_ALLOCATED)
|
|
|
|
return right_chunk;
|
|
|
|
if (left_chunk->chunk_state == CHUNK_QUARANTINE)
|
|
|
|
return left_chunk;
|
|
|
|
if (right_chunk->chunk_state == CHUNK_QUARANTINE)
|
|
|
|
return right_chunk;
|
|
|
|
}
|
|
|
|
// Same chunk_state: choose based on offset.
|
2013-02-05 22:32:03 +08:00
|
|
|
sptr l_offset = 0, r_offset = 0;
|
2012-12-19 16:32:50 +08:00
|
|
|
CHECK(AsanChunkView(left_chunk).AddrIsAtRight(addr, 1, &l_offset));
|
|
|
|
CHECK(AsanChunkView(right_chunk).AddrIsAtLeft(addr, 1, &r_offset));
|
|
|
|
if (l_offset < r_offset)
|
|
|
|
return left_chunk;
|
|
|
|
return right_chunk;
|
|
|
|
}
|
|
|
|
|
|
|
|
AsanChunkView FindHeapChunkByAddress(uptr addr) {
|
|
|
|
AsanChunk *m1 = GetAsanChunkByAddr(addr);
|
|
|
|
if (!m1) return AsanChunkView(m1);
|
2013-02-05 22:32:03 +08:00
|
|
|
sptr offset = 0;
|
2012-12-19 16:32:50 +08:00
|
|
|
if (AsanChunkView(m1).AddrIsAtLeft(addr, 1, &offset)) {
|
|
|
|
// The address is in the chunk's left redzone, so maybe it is actually
|
|
|
|
// a right buffer overflow from the other chunk to the left.
|
|
|
|
// Search a bit to the left to see if there is another chunk.
|
|
|
|
AsanChunk *m2 = 0;
|
|
|
|
for (uptr l = 1; l < GetPageSizeCached(); l++) {
|
|
|
|
m2 = GetAsanChunkByAddr(addr - l);
|
|
|
|
if (m2 == m1) continue; // Still the same chunk.
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (m2 && AsanChunkView(m2).AddrIsAtRight(addr, 1, &offset))
|
|
|
|
m1 = ChooseChunk(addr, m2, m1);
|
|
|
|
}
|
|
|
|
return AsanChunkView(m1);
|
2012-12-11 17:02:36 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void AsanThreadLocalMallocStorage::CommitBack() {
|
2013-01-11 16:07:43 +08:00
|
|
|
AllocatorCache *ac = GetAllocatorCache(this);
|
|
|
|
quarantine.Drain(GetQuarantineCache(this), QuarantineCallback(ac));
|
2012-12-20 16:53:41 +08:00
|
|
|
allocator.SwallowCache(GetAllocatorCache(this));
|
2012-12-11 17:02:36 +08:00
|
|
|
}
|
|
|
|
|
2012-12-27 22:09:19 +08:00
|
|
|
void PrintInternalAllocatorStats() {
|
|
|
|
allocator.PrintStats();
|
|
|
|
}
|
|
|
|
|
2012-12-11 17:02:36 +08:00
|
|
|
SANITIZER_INTERFACE_ATTRIBUTE
|
2012-12-21 16:53:59 +08:00
|
|
|
void *asan_memalign(uptr alignment, uptr size, StackTrace *stack,
|
|
|
|
AllocType alloc_type) {
|
2013-04-04 19:17:14 +08:00
|
|
|
return Allocate(size, alignment, stack, alloc_type, true);
|
2012-12-11 17:02:36 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
SANITIZER_INTERFACE_ATTRIBUTE
|
2012-12-21 16:53:59 +08:00
|
|
|
void asan_free(void *ptr, StackTrace *stack, AllocType alloc_type) {
|
|
|
|
Deallocate(ptr, stack, alloc_type);
|
2012-12-11 17:02:36 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
SANITIZER_INTERFACE_ATTRIBUTE
|
|
|
|
void *asan_malloc(uptr size, StackTrace *stack) {
|
2013-04-04 19:17:14 +08:00
|
|
|
return Allocate(size, 8, stack, FROM_MALLOC, true);
|
2012-12-11 17:02:36 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void *asan_calloc(uptr nmemb, uptr size, StackTrace *stack) {
|
2013-01-25 19:46:22 +08:00
|
|
|
if (CallocShouldReturnNullDueToOverflow(size, nmemb)) return 0;
|
2013-04-04 19:17:14 +08:00
|
|
|
void *ptr = Allocate(nmemb * size, 8, stack, FROM_MALLOC, false);
|
2013-02-27 21:38:19 +08:00
|
|
|
// If the memory comes from the secondary allocator no need to clear it
|
|
|
|
// as it comes directly from mmap.
|
|
|
|
if (ptr && allocator.FromPrimary(ptr))
|
2012-12-14 21:16:19 +08:00
|
|
|
REAL(memset)(ptr, 0, nmemb * size);
|
2012-12-17 21:43:47 +08:00
|
|
|
return ptr;
|
2012-12-11 17:02:36 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void *asan_realloc(void *p, uptr size, StackTrace *stack) {
|
2013-01-29 15:51:34 +08:00
|
|
|
if (p == 0)
|
2013-04-04 19:17:14 +08:00
|
|
|
return Allocate(size, 8, stack, FROM_MALLOC, true);
|
2012-12-14 21:16:19 +08:00
|
|
|
if (size == 0) {
|
2012-12-21 16:53:59 +08:00
|
|
|
Deallocate(p, stack, FROM_MALLOC);
|
2012-12-14 21:16:19 +08:00
|
|
|
return 0;
|
|
|
|
}
|
2012-12-17 17:06:25 +08:00
|
|
|
return Reallocate(p, size, stack);
|
2012-12-11 17:02:36 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void *asan_valloc(uptr size, StackTrace *stack) {
|
2013-04-04 19:17:14 +08:00
|
|
|
return Allocate(size, GetPageSizeCached(), stack, FROM_MALLOC, true);
|
2012-12-11 17:02:36 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void *asan_pvalloc(uptr size, StackTrace *stack) {
|
2012-12-14 21:16:19 +08:00
|
|
|
uptr PageSize = GetPageSizeCached();
|
|
|
|
size = RoundUpTo(size, PageSize);
|
|
|
|
if (size == 0) {
|
|
|
|
// pvalloc(0) should allocate one page.
|
|
|
|
size = PageSize;
|
|
|
|
}
|
2013-04-04 19:17:14 +08:00
|
|
|
return Allocate(size, PageSize, stack, FROM_MALLOC, true);
|
2012-12-11 17:02:36 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
int asan_posix_memalign(void **memptr, uptr alignment, uptr size,
|
2012-12-14 21:16:19 +08:00
|
|
|
StackTrace *stack) {
|
2013-04-04 19:17:14 +08:00
|
|
|
void *ptr = Allocate(size, alignment, stack, FROM_MALLOC, true);
|
2012-12-14 21:16:19 +08:00
|
|
|
CHECK(IsAligned((uptr)ptr, alignment));
|
|
|
|
*memptr = ptr;
|
2012-12-11 17:02:36 +08:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
uptr asan_malloc_usable_size(void *ptr, StackTrace *stack) {
|
2012-12-17 22:57:25 +08:00
|
|
|
CHECK(stack);
|
|
|
|
if (ptr == 0) return 0;
|
|
|
|
uptr usable_size = AllocationSize(reinterpret_cast<uptr>(ptr));
|
|
|
|
if (flags()->check_malloc_usable_size && (usable_size == 0))
|
|
|
|
ReportMallocUsableSizeNotOwned((uptr)ptr, stack);
|
|
|
|
return usable_size;
|
2012-12-11 17:02:36 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
uptr asan_mz_size(const void *ptr) {
|
2013-02-07 19:40:03 +08:00
|
|
|
return AllocationSize(reinterpret_cast<uptr>(ptr));
|
2012-12-11 17:02:36 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void asan_mz_force_lock() {
|
2013-02-07 19:40:03 +08:00
|
|
|
allocator.ForceLock();
|
|
|
|
fallback_mutex.Lock();
|
2012-12-11 17:02:36 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void asan_mz_force_unlock() {
|
2013-02-07 19:40:03 +08:00
|
|
|
fallback_mutex.Unlock();
|
|
|
|
allocator.ForceUnlock();
|
2012-12-11 17:02:36 +08:00
|
|
|
}
|
|
|
|
|
2012-12-10 21:52:55 +08:00
|
|
|
} // namespace __asan
|
2012-12-11 17:02:36 +08:00
|
|
|
|
2013-05-21 21:46:41 +08:00
|
|
|
// --- Implementation of LSan-specific functions --- {{{1
|
|
|
|
namespace __lsan {
|
|
|
|
void LockAllocator() {
|
|
|
|
__asan::allocator.ForceLock();
|
|
|
|
}
|
|
|
|
|
|
|
|
void UnlockAllocator() {
|
|
|
|
__asan::allocator.ForceUnlock();
|
|
|
|
}
|
|
|
|
|
|
|
|
void GetAllocatorGlobalRange(uptr *begin, uptr *end) {
|
|
|
|
*begin = (uptr)&__asan::allocator;
|
|
|
|
*end = *begin + sizeof(__asan::allocator);
|
|
|
|
}
|
|
|
|
|
|
|
|
void *PointsIntoChunk(void* p) {
|
|
|
|
uptr addr = reinterpret_cast<uptr>(p);
|
2013-05-31 19:13:45 +08:00
|
|
|
__asan::AsanChunk *m = __asan::GetAsanChunkByAddrFastLocked(addr);
|
2013-05-21 21:46:41 +08:00
|
|
|
if (!m) return 0;
|
|
|
|
uptr chunk = m->Beg();
|
|
|
|
if ((m->chunk_state == __asan::CHUNK_ALLOCATED) && m->AddrIsInside(addr))
|
|
|
|
return reinterpret_cast<void *>(chunk);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
void *GetUserBegin(void *p) {
|
2013-05-31 19:13:45 +08:00
|
|
|
__asan::AsanChunk *m =
|
|
|
|
__asan::GetAsanChunkByAddrFastLocked(reinterpret_cast<uptr>(p));
|
2013-05-21 21:46:41 +08:00
|
|
|
CHECK(m);
|
|
|
|
return reinterpret_cast<void *>(m->Beg());
|
|
|
|
}
|
|
|
|
|
|
|
|
LsanMetadata::LsanMetadata(void *chunk) {
|
|
|
|
uptr addr = reinterpret_cast<uptr>(chunk);
|
|
|
|
metadata_ = reinterpret_cast<void *>(addr - __asan::kChunkHeaderSize);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool LsanMetadata::allocated() const {
|
|
|
|
__asan::AsanChunk *m = reinterpret_cast<__asan::AsanChunk *>(metadata_);
|
|
|
|
return m->chunk_state == __asan::CHUNK_ALLOCATED;
|
|
|
|
}
|
|
|
|
|
|
|
|
ChunkTag LsanMetadata::tag() const {
|
|
|
|
__asan::AsanChunk *m = reinterpret_cast<__asan::AsanChunk *>(metadata_);
|
|
|
|
return static_cast<ChunkTag>(m->lsan_tag);
|
|
|
|
}
|
|
|
|
|
|
|
|
void LsanMetadata::set_tag(ChunkTag value) {
|
|
|
|
__asan::AsanChunk *m = reinterpret_cast<__asan::AsanChunk *>(metadata_);
|
|
|
|
m->lsan_tag = value;
|
|
|
|
}
|
|
|
|
|
|
|
|
uptr LsanMetadata::requested_size() const {
|
|
|
|
__asan::AsanChunk *m = reinterpret_cast<__asan::AsanChunk *>(metadata_);
|
|
|
|
return m->UsedSize();
|
|
|
|
}
|
|
|
|
|
|
|
|
u32 LsanMetadata::stack_trace_id() const {
|
|
|
|
__asan::AsanChunk *m = reinterpret_cast<__asan::AsanChunk *>(metadata_);
|
|
|
|
return m->alloc_context_id;
|
|
|
|
}
|
|
|
|
|
|
|
|
template <typename Callable> void ForEachChunk(Callable const &callback) {
|
|
|
|
__asan::allocator.ForEachChunk(callback);
|
|
|
|
}
|
2013-05-22 21:20:37 +08:00
|
|
|
#if CAN_SANITIZE_LEAKS
|
2013-05-21 21:46:41 +08:00
|
|
|
template void ForEachChunk<ProcessPlatformSpecificAllocationsCb>(
|
|
|
|
ProcessPlatformSpecificAllocationsCb const &callback);
|
|
|
|
template void ForEachChunk<PrintLeakedCb>(PrintLeakedCb const &callback);
|
|
|
|
template void ForEachChunk<CollectLeaksCb>(CollectLeaksCb const &callback);
|
|
|
|
template void ForEachChunk<MarkIndirectlyLeakedCb>(
|
|
|
|
MarkIndirectlyLeakedCb const &callback);
|
2013-06-03 19:21:34 +08:00
|
|
|
template void ForEachChunk<CollectSuppressedCb>(
|
|
|
|
CollectSuppressedCb const &callback);
|
2013-05-22 20:44:57 +08:00
|
|
|
#endif // CAN_SANITIZE_LEAKS
|
2013-05-21 21:46:41 +08:00
|
|
|
} // namespace __lsan
|
|
|
|
|
2013-06-03 19:21:34 +08:00
|
|
|
extern "C" {
|
|
|
|
void __lsan_disable() {
|
|
|
|
__asan_init();
|
|
|
|
__asan::AsanThread *t = __asan::GetCurrentThread();
|
|
|
|
CHECK(t);
|
|
|
|
t->disable_lsan();
|
|
|
|
}
|
|
|
|
|
|
|
|
void __lsan_enable() {
|
|
|
|
__asan_init();
|
|
|
|
__asan::AsanThread *t = __asan::GetCurrentThread();
|
|
|
|
CHECK(t);
|
|
|
|
t->enable_lsan();
|
|
|
|
}
|
|
|
|
} // extern "C"
|
|
|
|
|
2012-12-11 17:02:36 +08:00
|
|
|
// ---------------------- Interface ---------------- {{{1
|
|
|
|
using namespace __asan; // NOLINT
|
|
|
|
|
|
|
|
// ASan allocator doesn't reserve extra bytes, so normally we would
|
2012-12-20 16:53:41 +08:00
|
|
|
// just return "size". We don't want to expose our redzone sizes, etc here.
|
2012-12-11 17:02:36 +08:00
|
|
|
uptr __asan_get_estimated_allocated_size(uptr size) {
|
2012-12-20 16:53:41 +08:00
|
|
|
return size;
|
2012-12-11 17:02:36 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
bool __asan_get_ownership(const void *p) {
|
2013-01-17 21:25:17 +08:00
|
|
|
uptr ptr = reinterpret_cast<uptr>(p);
|
2013-01-29 15:51:34 +08:00
|
|
|
return (AllocationSize(ptr) > 0);
|
2012-12-11 17:02:36 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
uptr __asan_get_allocated_size(const void *p) {
|
2012-12-20 16:53:41 +08:00
|
|
|
if (p == 0) return 0;
|
2013-01-17 21:25:17 +08:00
|
|
|
uptr ptr = reinterpret_cast<uptr>(p);
|
|
|
|
uptr allocated_size = AllocationSize(ptr);
|
2012-12-20 16:53:41 +08:00
|
|
|
// Die if p is not malloced or if it is already freed.
|
2013-01-29 15:51:34 +08:00
|
|
|
if (allocated_size == 0) {
|
2012-12-20 16:53:41 +08:00
|
|
|
GET_STACK_TRACE_FATAL_HERE;
|
2013-01-17 21:25:17 +08:00
|
|
|
ReportAsanGetAllocatedSizeNotOwned(ptr, &stack);
|
2012-12-20 16:53:41 +08:00
|
|
|
}
|
|
|
|
return allocated_size;
|
2012-12-11 17:02:36 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
#if !SANITIZER_SUPPORTS_WEAK_HOOKS
|
|
|
|
// Provide default (no-op) implementation of malloc hooks.
|
|
|
|
extern "C" {
|
|
|
|
SANITIZER_WEAK_ATTRIBUTE SANITIZER_INTERFACE_ATTRIBUTE
|
|
|
|
void __asan_malloc_hook(void *ptr, uptr size) {
|
|
|
|
(void)ptr;
|
|
|
|
(void)size;
|
|
|
|
}
|
|
|
|
SANITIZER_WEAK_ATTRIBUTE SANITIZER_INTERFACE_ATTRIBUTE
|
|
|
|
void __asan_free_hook(void *ptr) {
|
|
|
|
(void)ptr;
|
|
|
|
}
|
|
|
|
} // extern "C"
|
|
|
|
#endif
|