2019-08-01 21:43:28 +08:00
|
|
|
//===-- asan_allocator.cpp ------------------------------------------------===//
|
2012-12-10 21:52:55 +08:00
|
|
|
//
|
2019-01-19 16:50:56 +08:00
|
|
|
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
|
|
|
|
// See https://llvm.org/LICENSE.txt for license information.
|
|
|
|
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
|
2012-12-10 21:52:55 +08:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// 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.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2015-10-01 08:22:21 +08:00
|
|
|
#include "asan_allocator.h"
|
2020-09-01 19:45:11 +08:00
|
|
|
|
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"
|
2014-03-04 21:12:25 +08:00
|
|
|
#include "asan_stack.h"
|
2012-12-11 22:41:31 +08:00
|
|
|
#include "asan_thread.h"
|
2020-09-01 19:45:11 +08:00
|
|
|
#include "lsan/lsan_common.h"
|
2017-07-19 03:11:04 +08:00
|
|
|
#include "sanitizer_common/sanitizer_allocator_checks.h"
|
2014-07-08 01:39:31 +08:00
|
|
|
#include "sanitizer_common/sanitizer_allocator_interface.h"
|
2017-07-15 06:23:47 +08:00
|
|
|
#include "sanitizer_common/sanitizer_errno.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"
|
2013-01-11 16:07:43 +08:00
|
|
|
#include "sanitizer_common/sanitizer_quarantine.h"
|
2020-09-01 19:45:11 +08:00
|
|
|
#include "sanitizer_common/sanitizer_stackdepot.h"
|
2012-12-10 21:52:55 +08:00
|
|
|
|
|
|
|
namespace __asan {
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2019-08-21 13:06:21 +08:00
|
|
|
static AsanAllocator &get_allocator();
|
|
|
|
|
2020-09-11 10:59:31 +08:00
|
|
|
static void AtomicContextStore(volatile atomic_uint64_t *atomic_context,
|
|
|
|
u32 tid, u32 stack) {
|
|
|
|
u64 context = tid;
|
|
|
|
context <<= 32;
|
|
|
|
context += stack;
|
|
|
|
atomic_store(atomic_context, context, memory_order_relaxed);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void AtomicContextLoad(const volatile atomic_uint64_t *atomic_context,
|
|
|
|
u32 &tid, u32 &stack) {
|
|
|
|
u64 context = atomic_load(atomic_context, memory_order_relaxed);
|
|
|
|
stack = context;
|
|
|
|
context >>= 32;
|
|
|
|
tid = context;
|
|
|
|
}
|
|
|
|
|
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.
|
|
|
|
|
2013-06-10 18:46:27 +08:00
|
|
|
// If the left redzone is greater than the ChunkHeader size we store a magic
|
2012-12-20 22:35:06 +08:00
|
|
|
// value in the first uptr word of the memory block and store the address of
|
|
|
|
// ChunkBase in the next uptr.
|
2013-06-10 18:46:27 +08:00
|
|
|
// M B L L L L L L L L L H H U U U U U U
|
|
|
|
// | ^
|
|
|
|
// ---------------------|
|
|
|
|
// M -- magic value kAllocBegMagic
|
2012-12-20 22:35:06 +08:00
|
|
|
// B -- address of ChunkHeader pointing to the first 'H'
|
|
|
|
|
2020-09-11 10:59:31 +08:00
|
|
|
class ChunkHeader {
|
|
|
|
public:
|
2020-09-06 15:07:14 +08:00
|
|
|
atomic_uint8_t chunk_state;
|
|
|
|
u8 alloc_type : 2;
|
|
|
|
u8 lsan_tag : 2;
|
2020-09-11 10:59:31 +08:00
|
|
|
|
2017-10-26 01:21:37 +08:00
|
|
|
// align < 8 -> 0
|
|
|
|
// else -> log2(min(align, 512)) - 2
|
2020-09-15 15:07:56 +08:00
|
|
|
u8 user_requested_alignment_log : 3;
|
2020-09-11 10:59:31 +08:00
|
|
|
|
|
|
|
private:
|
2020-09-15 15:07:56 +08:00
|
|
|
u16 user_requested_size_hi;
|
2020-09-15 14:53:58 +08:00
|
|
|
u32 user_requested_size_lo;
|
2020-09-11 10:59:31 +08:00
|
|
|
atomic_uint64_t alloc_context_id;
|
|
|
|
|
|
|
|
public:
|
2020-09-15 14:53:58 +08:00
|
|
|
uptr UsedSize() const {
|
2021-11-13 05:00:54 +08:00
|
|
|
static_assert(sizeof(user_requested_size_lo) == 4,
|
|
|
|
"Expression below requires this");
|
|
|
|
return FIRST_32_SECOND_64(0, ((uptr)user_requested_size_hi << 32)) +
|
|
|
|
user_requested_size_lo;
|
2020-09-15 14:53:58 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void SetUsedSize(uptr size) {
|
|
|
|
user_requested_size_lo = size;
|
2021-11-13 05:00:54 +08:00
|
|
|
static_assert(sizeof(user_requested_size_lo) == 4,
|
|
|
|
"Expression below requires this");
|
|
|
|
user_requested_size_hi = FIRST_32_SECOND_64(0, size >> 32);
|
|
|
|
CHECK_EQ(UsedSize(), size);
|
2020-09-15 14:53:58 +08:00
|
|
|
}
|
|
|
|
|
2020-09-11 10:59:31 +08:00
|
|
|
void SetAllocContext(u32 tid, u32 stack) {
|
|
|
|
AtomicContextStore(&alloc_context_id, tid, stack);
|
|
|
|
}
|
|
|
|
|
|
|
|
void GetAllocContext(u32 &tid, u32 &stack) const {
|
|
|
|
AtomicContextLoad(&alloc_context_id, tid, stack);
|
|
|
|
}
|
2012-12-11 22:41:31 +08:00
|
|
|
};
|
|
|
|
|
2020-09-11 10:59:31 +08:00
|
|
|
class ChunkBase : public ChunkHeader {
|
|
|
|
atomic_uint64_t free_context_id;
|
|
|
|
|
|
|
|
public:
|
|
|
|
void SetFreeContext(u32 tid, u32 stack) {
|
|
|
|
AtomicContextStore(&free_context_id, tid, stack);
|
|
|
|
}
|
|
|
|
|
|
|
|
void GetFreeContext(u32 &tid, u32 &stack) const {
|
|
|
|
AtomicContextLoad(&free_context_id, tid, stack);
|
|
|
|
}
|
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
|
|
|
|
2014-12-17 09:55:03 +08:00
|
|
|
enum {
|
2020-09-04 09:54:52 +08:00
|
|
|
// Either just allocated by underlying allocator, but AsanChunk is not yet
|
|
|
|
// ready, or almost returned to undelying allocator and AsanChunk is already
|
|
|
|
// meaningless.
|
|
|
|
CHUNK_INVALID = 0,
|
2020-09-04 15:17:34 +08:00
|
|
|
// The chunk is allocated and not yet freed.
|
2020-09-04 09:54:52 +08:00
|
|
|
CHUNK_ALLOCATED = 2,
|
2020-09-04 15:17:34 +08:00
|
|
|
// The chunk was freed and put into quarantine zone.
|
2020-09-04 16:17:18 +08:00
|
|
|
CHUNK_QUARANTINE = 3,
|
2014-12-17 09:55:03 +08:00
|
|
|
};
|
|
|
|
|
2020-09-11 10:59:31 +08:00
|
|
|
class AsanChunk : public ChunkBase {
|
|
|
|
public:
|
2012-12-11 22:41:31 +08:00
|
|
|
uptr Beg() { return reinterpret_cast<uptr>(this) + kChunkHeaderSize; }
|
2020-09-16 15:03:07 +08:00
|
|
|
bool AddrIsInside(uptr addr) {
|
|
|
|
return (addr >= Beg()) && (addr < Beg() + UsedSize());
|
|
|
|
}
|
2012-12-11 22:41:31 +08:00
|
|
|
};
|
|
|
|
|
2020-09-15 15:12:02 +08:00
|
|
|
class LargeChunkHeader {
|
2020-09-15 15:16:55 +08:00
|
|
|
static constexpr uptr kAllocBegMagic =
|
|
|
|
FIRST_32_SECOND_64(0xCC6E96B9, 0xCC6E96B9CC6E96B9ULL);
|
|
|
|
atomic_uintptr_t magic;
|
2020-09-15 15:12:02 +08:00
|
|
|
AsanChunk *chunk_header;
|
|
|
|
|
|
|
|
public:
|
2020-09-15 15:22:10 +08:00
|
|
|
AsanChunk *Get() const {
|
2020-09-15 15:12:02 +08:00
|
|
|
return atomic_load(&magic, memory_order_acquire) == kAllocBegMagic
|
|
|
|
? chunk_header
|
2020-09-15 15:22:10 +08:00
|
|
|
: nullptr;
|
2020-09-15 15:12:02 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void Set(AsanChunk *p) {
|
|
|
|
if (p) {
|
|
|
|
chunk_header = p;
|
|
|
|
atomic_store(&magic, kAllocBegMagic, memory_order_release);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2020-09-15 15:16:55 +08:00
|
|
|
uptr old = kAllocBegMagic;
|
2020-09-15 15:12:02 +08:00
|
|
|
if (!atomic_compare_exchange_strong(&magic, &old, 0,
|
|
|
|
memory_order_release)) {
|
|
|
|
CHECK_EQ(old, kAllocBegMagic);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2013-01-11 16:07:43 +08:00
|
|
|
struct QuarantineCallback {
|
2018-03-29 02:22:40 +08:00
|
|
|
QuarantineCallback(AllocatorCache *cache, BufferedStackTrace *stack)
|
|
|
|
: cache_(cache),
|
|
|
|
stack_(stack) {
|
2012-12-17 17:06:25 +08:00
|
|
|
}
|
|
|
|
|
2013-01-11 16:07:43 +08:00
|
|
|
void Recycle(AsanChunk *m) {
|
2020-09-15 15:12:02 +08:00
|
|
|
void *p = get_allocator().GetBlockBegin(m);
|
|
|
|
if (p != m) {
|
|
|
|
// Clear the magic value, as allocator internals may overwrite the
|
|
|
|
// contents of deallocated chunk, confusing GetAsanChunk lookup.
|
|
|
|
reinterpret_cast<LargeChunkHeader *>(p)->Set(nullptr);
|
|
|
|
}
|
|
|
|
|
2020-09-06 15:07:14 +08:00
|
|
|
u8 old_chunk_state = CHUNK_QUARANTINE;
|
|
|
|
if (!atomic_compare_exchange_strong(&m->chunk_state, &old_chunk_state,
|
|
|
|
CHUNK_INVALID, memory_order_acquire)) {
|
|
|
|
CHECK_EQ(old_chunk_state, CHUNK_QUARANTINE);
|
|
|
|
}
|
|
|
|
|
2021-12-08 07:18:04 +08:00
|
|
|
PoisonShadow(m->Beg(), RoundUpTo(m->UsedSize(), ASAN_SHADOW_GRANULARITY),
|
2012-12-17 17:06:25 +08:00
|
|
|
kAsanHeapLeftRedzoneMagic);
|
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();
|
|
|
|
|
2014-12-17 09:55:03 +08:00
|
|
|
get_allocator().Deallocate(cache_, p);
|
2012-12-17 17:06:25 +08:00
|
|
|
}
|
|
|
|
|
2013-01-11 16:07:43 +08:00
|
|
|
void *Allocate(uptr size) {
|
2017-06-29 05:58:57 +08:00
|
|
|
void *res = get_allocator().Allocate(cache_, size, 1);
|
|
|
|
// TODO(alekseys): Consider making quarantine OOM-friendly.
|
|
|
|
if (UNLIKELY(!res))
|
2018-03-29 02:22:40 +08:00
|
|
|
ReportOutOfMemory(size, stack_);
|
2017-06-29 05:58:57 +08:00
|
|
|
return res;
|
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) {
|
2014-12-17 09:55:03 +08:00
|
|
|
get_allocator().Deallocate(cache_, p);
|
2013-01-11 16:07:43 +08:00
|
|
|
}
|
2012-12-17 17:06:25 +08:00
|
|
|
|
2018-03-29 02:22:40 +08:00
|
|
|
private:
|
|
|
|
AllocatorCache* const cache_;
|
|
|
|
BufferedStackTrace* const stack_;
|
2013-01-11 16:07:43 +08:00
|
|
|
};
|
2012-12-17 17:06:25 +08:00
|
|
|
|
2014-12-17 09:55:03 +08:00
|
|
|
typedef Quarantine<QuarantineCallback, AsanChunk> AsanQuarantine;
|
|
|
|
typedef AsanQuarantine::Cache QuarantineCache;
|
|
|
|
|
|
|
|
void AsanMapUnmapCallback::OnMap(uptr p, uptr size) const {
|
|
|
|
PoisonShadow(p, size, kAsanHeapLeftRedzoneMagic);
|
|
|
|
// Statistics.
|
|
|
|
AsanStats &thread_stats = GetCurrentThreadStats();
|
|
|
|
thread_stats.mmaps++;
|
|
|
|
thread_stats.mmaped += size;
|
|
|
|
}
|
|
|
|
void AsanMapUnmapCallback::OnUnmap(uptr p, uptr size) const {
|
|
|
|
PoisonShadow(p, size, 0);
|
|
|
|
// We are about to unmap a chunk of user memory.
|
|
|
|
// Mark the corresponding shadow memory as not needed.
|
|
|
|
FlushUnneededASanShadowMemory(p, size);
|
|
|
|
// Statistics.
|
|
|
|
AsanStats &thread_stats = GetCurrentThreadStats();
|
|
|
|
thread_stats.munmaps++;
|
|
|
|
thread_stats.munmaped += size;
|
2012-12-17 17:06:25 +08:00
|
|
|
}
|
2012-12-11 22:41:31 +08:00
|
|
|
|
2014-12-17 09:55:03 +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);
|
|
|
|
return &ms->allocator_cache;
|
|
|
|
}
|
|
|
|
|
|
|
|
QuarantineCache *GetQuarantineCache(AsanThreadLocalMallocStorage *ms) {
|
|
|
|
CHECK(ms);
|
|
|
|
CHECK_LE(sizeof(QuarantineCache), sizeof(ms->quarantine_cache));
|
|
|
|
return reinterpret_cast<QuarantineCache *>(ms->quarantine_cache);
|
|
|
|
}
|
|
|
|
|
2014-12-20 03:35:11 +08:00
|
|
|
void AllocatorOptions::SetFrom(const Flags *f, const CommonFlags *cf) {
|
2015-01-07 10:37:52 +08:00
|
|
|
quarantine_size_mb = f->quarantine_size_mb;
|
2016-12-23 05:43:22 +08:00
|
|
|
thread_local_quarantine_size_kb = f->thread_local_quarantine_size_kb;
|
2014-12-20 03:35:11 +08:00
|
|
|
min_redzone = f->redzone;
|
|
|
|
max_redzone = f->max_redzone;
|
|
|
|
may_return_null = cf->allocator_may_return_null;
|
|
|
|
alloc_dealloc_mismatch = f->alloc_dealloc_mismatch;
|
2016-11-29 08:22:50 +08:00
|
|
|
release_to_os_interval_ms = cf->allocator_release_to_os_interval_ms;
|
2014-12-20 03:35:11 +08:00
|
|
|
}
|
|
|
|
|
2014-12-20 04:35:53 +08:00
|
|
|
void AllocatorOptions::CopyTo(Flags *f, CommonFlags *cf) {
|
2015-01-07 10:37:52 +08:00
|
|
|
f->quarantine_size_mb = quarantine_size_mb;
|
2016-12-23 05:43:22 +08:00
|
|
|
f->thread_local_quarantine_size_kb = thread_local_quarantine_size_kb;
|
2014-12-20 04:35:53 +08:00
|
|
|
f->redzone = min_redzone;
|
|
|
|
f->max_redzone = max_redzone;
|
|
|
|
cf->allocator_may_return_null = may_return_null;
|
|
|
|
f->alloc_dealloc_mismatch = alloc_dealloc_mismatch;
|
2016-11-29 08:22:50 +08:00
|
|
|
cf->allocator_release_to_os_interval_ms = release_to_os_interval_ms;
|
2014-12-20 04:35:53 +08:00
|
|
|
}
|
|
|
|
|
2014-12-17 09:55:03 +08:00
|
|
|
struct Allocator {
|
|
|
|
static const uptr kMaxAllowedMallocSize =
|
2016-06-15 02:05:45 +08:00
|
|
|
FIRST_32_SECOND_64(3UL << 30, 1ULL << 40);
|
2014-12-17 09:55:03 +08:00
|
|
|
|
|
|
|
AsanAllocator allocator;
|
|
|
|
AsanQuarantine quarantine;
|
|
|
|
StaticSpinMutex fallback_mutex;
|
|
|
|
AllocatorCache fallback_allocator_cache;
|
|
|
|
QuarantineCache fallback_quarantine_cache;
|
|
|
|
|
2019-10-31 02:17:56 +08:00
|
|
|
uptr max_user_defined_malloc_size;
|
2017-06-14 07:57:24 +08:00
|
|
|
|
2014-12-20 03:35:11 +08:00
|
|
|
// ------------------- Options --------------------------
|
|
|
|
atomic_uint16_t min_redzone;
|
|
|
|
atomic_uint16_t max_redzone;
|
|
|
|
atomic_uint8_t alloc_dealloc_mismatch;
|
|
|
|
|
|
|
|
// ------------------- Initialization ------------------------
|
2014-12-17 09:55:03 +08:00
|
|
|
explicit Allocator(LinkerInitialized)
|
|
|
|
: quarantine(LINKER_INITIALIZED),
|
|
|
|
fallback_quarantine_cache(LINKER_INITIALIZED) {}
|
|
|
|
|
2014-12-20 03:35:11 +08:00
|
|
|
void CheckOptions(const AllocatorOptions &options) const {
|
|
|
|
CHECK_GE(options.min_redzone, 16);
|
|
|
|
CHECK_GE(options.max_redzone, options.min_redzone);
|
|
|
|
CHECK_LE(options.max_redzone, 2048);
|
|
|
|
CHECK(IsPowerOfTwo(options.min_redzone));
|
|
|
|
CHECK(IsPowerOfTwo(options.max_redzone));
|
2012-12-26 12:52:07 +08:00
|
|
|
}
|
2012-12-26 14:30:02 +08:00
|
|
|
|
2014-12-20 03:35:11 +08:00
|
|
|
void SharedInitCode(const AllocatorOptions &options) {
|
|
|
|
CheckOptions(options);
|
|
|
|
quarantine.Init((uptr)options.quarantine_size_mb << 20,
|
2016-12-23 05:43:22 +08:00
|
|
|
(uptr)options.thread_local_quarantine_size_kb << 10);
|
2014-12-20 03:35:11 +08:00
|
|
|
atomic_store(&alloc_dealloc_mismatch, options.alloc_dealloc_mismatch,
|
|
|
|
memory_order_release);
|
|
|
|
atomic_store(&min_redzone, options.min_redzone, memory_order_release);
|
|
|
|
atomic_store(&max_redzone, options.max_redzone, memory_order_release);
|
|
|
|
}
|
|
|
|
|
2017-12-14 08:07:15 +08:00
|
|
|
void InitLinkerInitialized(const AllocatorOptions &options) {
|
2017-06-21 05:23:02 +08:00
|
|
|
SetAllocatorMayReturnNull(options.may_return_null);
|
2017-12-14 08:07:15 +08:00
|
|
|
allocator.InitLinkerInitialized(options.release_to_os_interval_ms);
|
2014-12-20 03:35:11 +08:00
|
|
|
SharedInitCode(options);
|
2019-10-31 02:17:56 +08:00
|
|
|
max_user_defined_malloc_size = common_flags()->max_allocation_size_mb
|
|
|
|
? common_flags()->max_allocation_size_mb
|
|
|
|
<< 20
|
|
|
|
: kMaxAllowedMallocSize;
|
2014-12-20 03:35:11 +08:00
|
|
|
}
|
|
|
|
|
2016-09-14 02:38:40 +08:00
|
|
|
void RePoisonChunk(uptr chunk) {
|
2017-01-12 06:10:35 +08:00
|
|
|
// This could be a user-facing chunk (with redzones), or some internal
|
2016-09-14 02:38:40 +08:00
|
|
|
// housekeeping chunk, like TransferBatch. Start by assuming the former.
|
|
|
|
AsanChunk *ac = GetAsanChunk((void *)chunk);
|
2020-09-10 07:17:37 +08:00
|
|
|
uptr allocated_size = allocator.GetActuallyAllocatedSize((void *)chunk);
|
|
|
|
if (ac && atomic_load(&ac->chunk_state, memory_order_acquire) ==
|
|
|
|
CHUNK_ALLOCATED) {
|
2020-09-06 15:07:14 +08:00
|
|
|
uptr beg = ac->Beg();
|
2020-09-15 14:53:58 +08:00
|
|
|
uptr end = ac->Beg() + ac->UsedSize();
|
2020-09-06 15:07:14 +08:00
|
|
|
uptr chunk_end = chunk + allocated_size;
|
|
|
|
if (chunk < beg && beg < end && end <= chunk_end) {
|
|
|
|
// Looks like a valid AsanChunk in use, poison redzones only.
|
|
|
|
PoisonShadow(chunk, beg - chunk, kAsanHeapLeftRedzoneMagic);
|
2021-12-08 07:18:04 +08:00
|
|
|
uptr end_aligned_down = RoundDownTo(end, ASAN_SHADOW_GRANULARITY);
|
2020-09-06 15:07:14 +08:00
|
|
|
FastPoisonShadowPartialRightRedzone(
|
|
|
|
end_aligned_down, end - end_aligned_down,
|
|
|
|
chunk_end - end_aligned_down, kAsanHeapLeftRedzoneMagic);
|
|
|
|
return;
|
|
|
|
}
|
2016-09-14 02:38:40 +08:00
|
|
|
}
|
2020-09-06 15:07:14 +08:00
|
|
|
|
|
|
|
// This is either not an AsanChunk or freed or quarantined AsanChunk.
|
|
|
|
// In either case, poison everything.
|
|
|
|
PoisonShadow(chunk, allocated_size, kAsanHeapLeftRedzoneMagic);
|
2016-09-14 02:38:40 +08:00
|
|
|
}
|
|
|
|
|
2014-12-20 03:35:11 +08:00
|
|
|
void ReInitialize(const AllocatorOptions &options) {
|
2017-06-21 05:23:02 +08:00
|
|
|
SetAllocatorMayReturnNull(options.may_return_null);
|
2016-11-29 08:22:50 +08:00
|
|
|
allocator.SetReleaseToOSIntervalMs(options.release_to_os_interval_ms);
|
2014-12-20 03:35:11 +08:00
|
|
|
SharedInitCode(options);
|
2016-09-14 02:38:40 +08:00
|
|
|
|
|
|
|
// Poison all existing allocation's redzones.
|
|
|
|
if (CanPoisonMemory()) {
|
|
|
|
allocator.ForceLock();
|
|
|
|
allocator.ForEachChunk(
|
|
|
|
[](uptr chunk, void *alloc) {
|
|
|
|
((Allocator *)alloc)->RePoisonChunk(chunk);
|
|
|
|
},
|
|
|
|
this);
|
|
|
|
allocator.ForceUnlock();
|
|
|
|
}
|
2014-12-17 09:55:03 +08:00
|
|
|
}
|
2012-12-11 22:41:31 +08:00
|
|
|
|
2014-12-20 04:35:53 +08:00
|
|
|
void GetOptions(AllocatorOptions *options) const {
|
|
|
|
options->quarantine_size_mb = quarantine.GetSize() >> 20;
|
2016-12-23 05:43:22 +08:00
|
|
|
options->thread_local_quarantine_size_kb = quarantine.GetCacheSize() >> 10;
|
2014-12-20 04:35:53 +08:00
|
|
|
options->min_redzone = atomic_load(&min_redzone, memory_order_acquire);
|
|
|
|
options->max_redzone = atomic_load(&max_redzone, memory_order_acquire);
|
2017-06-21 05:23:02 +08:00
|
|
|
options->may_return_null = AllocatorMayReturnNull();
|
2014-12-20 04:35:53 +08:00
|
|
|
options->alloc_dealloc_mismatch =
|
|
|
|
atomic_load(&alloc_dealloc_mismatch, memory_order_acquire);
|
2016-11-29 08:22:50 +08:00
|
|
|
options->release_to_os_interval_ms = allocator.ReleaseToOSIntervalMs();
|
2014-12-20 04:35:53 +08:00
|
|
|
}
|
|
|
|
|
2014-12-17 09:55:03 +08:00
|
|
|
// -------------------- Helper methods. -------------------------
|
|
|
|
uptr ComputeRZLog(uptr user_requested_size) {
|
2020-09-09 18:06:46 +08:00
|
|
|
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;
|
|
|
|
u32 hdr_log = RZSize2Log(RoundUpToPowerOfTwo(sizeof(ChunkHeader)));
|
|
|
|
u32 min_log = RZSize2Log(atomic_load(&min_redzone, memory_order_acquire));
|
|
|
|
u32 max_log = RZSize2Log(atomic_load(&max_redzone, memory_order_acquire));
|
|
|
|
return Min(Max(rz_log, Max(min_log, hdr_log)), Max(max_log, hdr_log));
|
2012-12-14 20:15:09 +08:00
|
|
|
}
|
|
|
|
|
2017-10-26 01:21:37 +08:00
|
|
|
static uptr ComputeUserRequestedAlignmentLog(uptr user_requested_alignment) {
|
|
|
|
if (user_requested_alignment < 8)
|
|
|
|
return 0;
|
|
|
|
if (user_requested_alignment > 512)
|
|
|
|
user_requested_alignment = 512;
|
|
|
|
return Log2(user_requested_alignment) - 2;
|
|
|
|
}
|
|
|
|
|
|
|
|
static uptr ComputeUserAlignment(uptr user_requested_alignment_log) {
|
|
|
|
if (user_requested_alignment_log == 0)
|
|
|
|
return 0;
|
|
|
|
return 1LL << (user_requested_alignment_log + 2);
|
|
|
|
}
|
|
|
|
|
2014-12-17 09:55:03 +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) {
|
2020-09-10 07:17:37 +08:00
|
|
|
if (!left_chunk)
|
|
|
|
return right_chunk;
|
|
|
|
if (!right_chunk)
|
|
|
|
return left_chunk;
|
2014-12-17 09:55:03 +08:00
|
|
|
// Prefer an allocated chunk over freed chunk and freed chunk
|
|
|
|
// over available chunk.
|
2020-09-06 15:07:14 +08:00
|
|
|
u8 left_state = atomic_load(&left_chunk->chunk_state, memory_order_relaxed);
|
|
|
|
u8 right_state =
|
|
|
|
atomic_load(&right_chunk->chunk_state, memory_order_relaxed);
|
|
|
|
if (left_state != right_state) {
|
|
|
|
if (left_state == CHUNK_ALLOCATED)
|
2014-12-17 09:55:03 +08:00
|
|
|
return left_chunk;
|
2020-09-06 15:07:14 +08:00
|
|
|
if (right_state == CHUNK_ALLOCATED)
|
2014-12-17 09:55:03 +08:00
|
|
|
return right_chunk;
|
2020-09-06 15:07:14 +08:00
|
|
|
if (left_state == CHUNK_QUARANTINE)
|
2014-12-17 09:55:03 +08:00
|
|
|
return left_chunk;
|
2020-09-06 15:07:14 +08:00
|
|
|
if (right_state == CHUNK_QUARANTINE)
|
2014-12-17 09:55:03 +08:00
|
|
|
return right_chunk;
|
|
|
|
}
|
|
|
|
// Same chunk_state: choose based on offset.
|
|
|
|
sptr l_offset = 0, r_offset = 0;
|
|
|
|
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;
|
2013-04-04 19:17:14 +08:00
|
|
|
}
|
2014-12-17 09:55:03 +08:00
|
|
|
|
2019-10-19 08:17:48 +08:00
|
|
|
bool UpdateAllocationStack(uptr addr, BufferedStackTrace *stack) {
|
|
|
|
AsanChunk *m = GetAsanChunkByAddr(addr);
|
|
|
|
if (!m) return false;
|
2020-09-06 15:07:14 +08:00
|
|
|
if (atomic_load(&m->chunk_state, memory_order_acquire) != CHUNK_ALLOCATED)
|
|
|
|
return false;
|
2019-10-19 08:17:48 +08:00
|
|
|
if (m->Beg() != addr) return false;
|
2020-09-11 10:59:31 +08:00
|
|
|
AsanThread *t = GetCurrentThread();
|
2021-04-28 15:19:37 +08:00
|
|
|
m->SetAllocContext(t ? t->tid() : kMainTid, StackDepotPut(*stack));
|
2019-10-19 08:17:48 +08:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2014-12-17 09:55:03 +08:00
|
|
|
// -------------------- Allocation/Deallocation routines ---------------
|
|
|
|
void *Allocate(uptr size, uptr alignment, BufferedStackTrace *stack,
|
|
|
|
AllocType alloc_type, bool can_fill) {
|
|
|
|
if (UNLIKELY(!asan_inited))
|
|
|
|
AsanInitFromRtl();
|
2021-12-03 04:47:47 +08:00
|
|
|
if (UNLIKELY(IsRssLimitExceeded())) {
|
2018-03-29 02:22:40 +08:00
|
|
|
if (AllocatorMayReturnNull())
|
|
|
|
return nullptr;
|
|
|
|
ReportRssLimitExceeded(stack);
|
|
|
|
}
|
2014-12-17 09:55:03 +08:00
|
|
|
Flags &fl = *flags();
|
|
|
|
CHECK(stack);
|
2021-12-08 07:18:04 +08:00
|
|
|
const uptr min_alignment = ASAN_SHADOW_GRANULARITY;
|
2017-10-26 01:21:37 +08:00
|
|
|
const uptr user_requested_alignment_log =
|
|
|
|
ComputeUserRequestedAlignmentLog(alignment);
|
2014-12-17 09:55:03 +08:00
|
|
|
if (alignment < min_alignment)
|
|
|
|
alignment = min_alignment;
|
|
|
|
if (size == 0) {
|
|
|
|
// 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;
|
|
|
|
}
|
|
|
|
CHECK(IsPowerOfTwo(alignment));
|
|
|
|
uptr rz_log = ComputeRZLog(size);
|
|
|
|
uptr rz_size = RZLog2Size(rz_log);
|
|
|
|
uptr rounded_size = RoundUpTo(Max(size, kChunkHeader2Size), alignment);
|
|
|
|
uptr needed_size = rounded_size + rz_size;
|
|
|
|
if (alignment > min_alignment)
|
|
|
|
needed_size += alignment;
|
|
|
|
// If we are allocating from the secondary allocator, there will be no
|
|
|
|
// automatic right redzone, so add the right redzone manually.
|
2020-09-15 15:22:10 +08:00
|
|
|
if (!PrimaryAllocator::CanAllocate(needed_size, alignment))
|
2014-12-17 09:55:03 +08:00
|
|
|
needed_size += rz_size;
|
|
|
|
CHECK(IsAligned(needed_size, min_alignment));
|
2019-10-31 02:17:56 +08:00
|
|
|
if (size > kMaxAllowedMallocSize || needed_size > kMaxAllowedMallocSize ||
|
|
|
|
size > max_user_defined_malloc_size) {
|
2018-03-29 02:22:40 +08:00
|
|
|
if (AllocatorMayReturnNull()) {
|
|
|
|
Report("WARNING: AddressSanitizer failed to allocate 0x%zx bytes\n",
|
2021-11-03 20:49:21 +08:00
|
|
|
size);
|
2018-03-29 02:22:40 +08:00
|
|
|
return nullptr;
|
|
|
|
}
|
2019-10-31 02:17:56 +08:00
|
|
|
uptr malloc_limit =
|
|
|
|
Min(kMaxAllowedMallocSize, max_user_defined_malloc_size);
|
|
|
|
ReportAllocationSizeTooBig(size, needed_size, malloc_limit, stack);
|
2014-12-17 09:55:03 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
AsanThread *t = GetCurrentThread();
|
|
|
|
void *allocated;
|
|
|
|
if (t) {
|
|
|
|
AllocatorCache *cache = GetAllocatorCache(&t->malloc_storage());
|
2017-06-17 05:00:03 +08:00
|
|
|
allocated = allocator.Allocate(cache, needed_size, 8);
|
2014-12-17 09:55:03 +08:00
|
|
|
} else {
|
|
|
|
SpinMutexLock l(&fallback_mutex);
|
|
|
|
AllocatorCache *cache = &fallback_allocator_cache;
|
2017-06-17 05:00:03 +08:00
|
|
|
allocated = allocator.Allocate(cache, needed_size, 8);
|
2014-12-17 09:55:03 +08:00
|
|
|
}
|
2018-03-29 02:22:40 +08:00
|
|
|
if (UNLIKELY(!allocated)) {
|
|
|
|
SetAllocatorOutOfMemory();
|
|
|
|
if (AllocatorMayReturnNull())
|
|
|
|
return nullptr;
|
|
|
|
ReportOutOfMemory(size, stack);
|
|
|
|
}
|
2015-01-07 07:53:32 +08:00
|
|
|
|
2014-12-17 09:55:03 +08:00
|
|
|
if (*(u8 *)MEM_TO_SHADOW((uptr)allocated) == 0 && CanPoisonMemory()) {
|
|
|
|
// Heap poisoning is enabled, but the allocator provides an unpoisoned
|
|
|
|
// chunk. This is possible if CanPoisonMemory() was false for some
|
|
|
|
// time, for example, due to flags()->start_disabled.
|
|
|
|
// Anyway, poison the block before using it for anything else.
|
|
|
|
uptr allocated_size = allocator.GetActuallyAllocatedSize(allocated);
|
|
|
|
PoisonShadow((uptr)allocated, allocated_size, kAsanHeapLeftRedzoneMagic);
|
|
|
|
}
|
|
|
|
|
|
|
|
uptr alloc_beg = reinterpret_cast<uptr>(allocated);
|
|
|
|
uptr alloc_end = alloc_beg + needed_size;
|
2020-09-15 14:54:48 +08:00
|
|
|
uptr user_beg = alloc_beg + rz_size;
|
2014-12-17 09:55:03 +08:00
|
|
|
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);
|
|
|
|
m->alloc_type = alloc_type;
|
2020-09-15 14:53:58 +08:00
|
|
|
CHECK(size);
|
|
|
|
m->SetUsedSize(size);
|
2017-10-26 01:21:37 +08:00
|
|
|
m->user_requested_alignment_log = user_requested_alignment_log;
|
2014-12-17 09:55:03 +08:00
|
|
|
|
2021-04-28 15:19:37 +08:00
|
|
|
m->SetAllocContext(t ? t->tid() : kMainTid, StackDepotPut(*stack));
|
2014-12-17 09:55:03 +08:00
|
|
|
|
|
|
|
uptr size_rounded_down_to_granularity =
|
2021-12-08 07:18:04 +08:00
|
|
|
RoundDownTo(size, ASAN_SHADOW_GRANULARITY);
|
2014-12-17 09:55:03 +08:00
|
|
|
// 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.
|
|
|
|
if (size != size_rounded_down_to_granularity && CanPoisonMemory()) {
|
|
|
|
u8 *shadow =
|
|
|
|
(u8 *)MemToShadow(user_beg + size_rounded_down_to_granularity);
|
2021-12-08 07:18:04 +08:00
|
|
|
*shadow = fl.poison_partial ? (size & (ASAN_SHADOW_GRANULARITY - 1)) : 0;
|
2014-12-17 09:55:03 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
AsanStats &thread_stats = GetCurrentThreadStats();
|
|
|
|
thread_stats.mallocs++;
|
|
|
|
thread_stats.malloced += size;
|
|
|
|
thread_stats.malloced_redzones += needed_size - size;
|
2019-08-21 13:06:21 +08:00
|
|
|
if (needed_size > SizeClassMap::kMaxSize)
|
2014-12-17 09:55:03 +08:00
|
|
|
thread_stats.malloc_large++;
|
2015-06-27 03:18:02 +08:00
|
|
|
else
|
2019-08-21 13:06:21 +08:00
|
|
|
thread_stats.malloced_by_size[SizeClassMap::ClassID(needed_size)]++;
|
2014-12-17 09:55:03 +08:00
|
|
|
|
|
|
|
void *res = reinterpret_cast<void *>(user_beg);
|
|
|
|
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-21 23:50:49 +08:00
|
|
|
#if CAN_SANITIZE_LEAKS
|
2014-12-17 09:55:03 +08:00
|
|
|
m->lsan_tag = __lsan::DisabledInThisThread() ? __lsan::kIgnored
|
|
|
|
: __lsan::kDirectlyLeaked;
|
2013-06-21 23:50:49 +08:00
|
|
|
#endif
|
2014-12-17 09:55:03 +08:00
|
|
|
// Must be the last mutation of metadata in this function.
|
2020-09-06 15:07:14 +08:00
|
|
|
atomic_store(&m->chunk_state, CHUNK_ALLOCATED, memory_order_release);
|
2020-09-15 15:12:02 +08:00
|
|
|
if (alloc_beg != chunk_beg) {
|
|
|
|
CHECK_LE(alloc_beg + sizeof(LargeChunkHeader), chunk_beg);
|
|
|
|
reinterpret_cast<LargeChunkHeader *>(alloc_beg)->Set(m);
|
|
|
|
}
|
2022-04-13 10:19:44 +08:00
|
|
|
RunMallocHooks(res, size);
|
2014-12-17 09:55:03 +08:00
|
|
|
return res;
|
|
|
|
}
|
2012-12-11 22:41:31 +08:00
|
|
|
|
2016-02-02 15:32:24 +08:00
|
|
|
// Set quarantine flag if chunk is allocated, issue ASan error report on
|
|
|
|
// available and quarantined chunks. Return true on success, false otherwise.
|
|
|
|
bool AtomicallySetQuarantineFlagIfAllocated(AsanChunk *m, void *ptr,
|
2020-09-06 15:07:14 +08:00
|
|
|
BufferedStackTrace *stack) {
|
2020-09-06 14:41:25 +08:00
|
|
|
u8 old_chunk_state = CHUNK_ALLOCATED;
|
2014-12-17 09:55:03 +08:00
|
|
|
// Flip the chunk_state atomically to avoid race on double-free.
|
2020-09-06 15:07:14 +08:00
|
|
|
if (!atomic_compare_exchange_strong(&m->chunk_state, &old_chunk_state,
|
2016-02-02 15:32:24 +08:00
|
|
|
CHUNK_QUARANTINE,
|
|
|
|
memory_order_acquire)) {
|
2014-12-17 09:55:03 +08:00
|
|
|
ReportInvalidFree(ptr, old_chunk_state, stack);
|
2016-02-02 15:32:24 +08:00
|
|
|
// It's not safe to push a chunk in quarantine on invalid free.
|
|
|
|
return false;
|
|
|
|
}
|
2014-12-17 09:55:03 +08:00
|
|
|
CHECK_EQ(CHUNK_ALLOCATED, old_chunk_state);
|
2020-09-06 07:52:48 +08:00
|
|
|
// It was a user data.
|
2020-09-11 10:59:31 +08:00
|
|
|
m->SetFreeContext(kInvalidTid, 0);
|
2016-02-02 15:32:24 +08:00
|
|
|
return true;
|
2014-12-17 09:55:03 +08:00
|
|
|
}
|
2012-12-17 17:06:25 +08:00
|
|
|
|
2014-12-17 09:55:03 +08:00
|
|
|
// Expects the chunk to already be marked as quarantined by using
|
2016-02-02 15:32:24 +08:00
|
|
|
// AtomicallySetQuarantineFlagIfAllocated.
|
2017-06-29 05:58:57 +08:00
|
|
|
void QuarantineChunk(AsanChunk *m, void *ptr, BufferedStackTrace *stack) {
|
2020-09-06 15:07:14 +08:00
|
|
|
CHECK_EQ(atomic_load(&m->chunk_state, memory_order_relaxed),
|
|
|
|
CHUNK_QUARANTINE);
|
2014-12-17 09:55:03 +08:00
|
|
|
AsanThread *t = GetCurrentThread();
|
2020-09-11 10:59:31 +08:00
|
|
|
m->SetFreeContext(t ? t->tid() : 0, StackDepotPut(*stack));
|
2017-03-30 23:44:57 +08:00
|
|
|
|
|
|
|
Flags &fl = *flags();
|
|
|
|
if (fl.max_free_fill_size > 0) {
|
|
|
|
// We have to skip the chunk header, it contains free_context_id.
|
|
|
|
uptr scribble_start = (uptr)m + kChunkHeaderSize + kChunkHeader2Size;
|
|
|
|
if (m->UsedSize() >= kChunkHeader2Size) { // Skip Header2 in user area.
|
|
|
|
uptr size_to_fill = m->UsedSize() - kChunkHeader2Size;
|
|
|
|
size_to_fill = Min(size_to_fill, (uptr)fl.max_free_fill_size);
|
|
|
|
REAL(memset)((void *)scribble_start, fl.free_fill_byte, size_to_fill);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-12-17 09:55:03 +08:00
|
|
|
// Poison the region.
|
2021-12-08 07:18:04 +08:00
|
|
|
PoisonShadow(m->Beg(), RoundUpTo(m->UsedSize(), ASAN_SHADOW_GRANULARITY),
|
2014-12-17 09:55:03 +08:00
|
|
|
kAsanHeapFreeMagic);
|
|
|
|
|
|
|
|
AsanStats &thread_stats = GetCurrentThreadStats();
|
|
|
|
thread_stats.frees++;
|
|
|
|
thread_stats.freed += m->UsedSize();
|
|
|
|
|
|
|
|
// Push into quarantine.
|
|
|
|
if (t) {
|
|
|
|
AsanThreadLocalMallocStorage *ms = &t->malloc_storage();
|
|
|
|
AllocatorCache *ac = GetAllocatorCache(ms);
|
2018-03-29 02:22:40 +08:00
|
|
|
quarantine.Put(GetQuarantineCache(ms), QuarantineCallback(ac, stack), m,
|
|
|
|
m->UsedSize());
|
2014-12-17 09:55:03 +08:00
|
|
|
} else {
|
|
|
|
SpinMutexLock l(&fallback_mutex);
|
|
|
|
AllocatorCache *ac = &fallback_allocator_cache;
|
2018-03-29 02:22:40 +08:00
|
|
|
quarantine.Put(&fallback_quarantine_cache, QuarantineCallback(ac, stack),
|
|
|
|
m, m->UsedSize());
|
2014-12-17 09:55:03 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-10-26 01:21:37 +08:00
|
|
|
void Deallocate(void *ptr, uptr delete_size, uptr delete_alignment,
|
|
|
|
BufferedStackTrace *stack, AllocType alloc_type) {
|
2014-12-17 09:55:03 +08:00
|
|
|
uptr p = reinterpret_cast<uptr>(ptr);
|
|
|
|
if (p == 0) return;
|
|
|
|
|
|
|
|
uptr chunk_beg = p - kChunkHeaderSize;
|
|
|
|
AsanChunk *m = reinterpret_cast<AsanChunk *>(chunk_beg);
|
2016-04-14 05:04:27 +08:00
|
|
|
|
2017-02-22 00:09:38 +08:00
|
|
|
// On Windows, uninstrumented DLLs may allocate memory before ASan hooks
|
|
|
|
// malloc. Don't report an invalid free in this case.
|
|
|
|
if (SANITIZER_WINDOWS &&
|
|
|
|
!get_allocator().PointerIsMine(ptr)) {
|
|
|
|
if (!IsSystemHeapAddress(p))
|
|
|
|
ReportFreeNotMalloced(p, stack);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2022-04-13 10:19:44 +08:00
|
|
|
RunFreeHooks(ptr);
|
2017-02-22 00:09:38 +08:00
|
|
|
|
2014-12-17 09:55:03 +08:00
|
|
|
// Must mark the chunk as quarantined before any changes to its metadata.
|
2016-02-02 15:32:24 +08:00
|
|
|
// Do not quarantine given chunk if we failed to set CHUNK_QUARANTINE flag.
|
|
|
|
if (!AtomicallySetQuarantineFlagIfAllocated(m, ptr, stack)) return;
|
2016-04-14 05:04:27 +08:00
|
|
|
|
|
|
|
if (m->alloc_type != alloc_type) {
|
|
|
|
if (atomic_load(&alloc_dealloc_mismatch, memory_order_acquire)) {
|
|
|
|
ReportAllocTypeMismatch((uptr)ptr, stack, (AllocType)m->alloc_type,
|
|
|
|
(AllocType)alloc_type);
|
|
|
|
}
|
2017-10-26 01:21:37 +08:00
|
|
|
} else {
|
|
|
|
if (flags()->new_delete_type_mismatch &&
|
|
|
|
(alloc_type == FROM_NEW || alloc_type == FROM_NEW_BR) &&
|
|
|
|
((delete_size && delete_size != m->UsedSize()) ||
|
|
|
|
ComputeUserRequestedAlignmentLog(delete_alignment) !=
|
|
|
|
m->user_requested_alignment_log)) {
|
|
|
|
ReportNewDeleteTypeMismatch(p, delete_size, delete_alignment, stack);
|
|
|
|
}
|
2016-04-14 05:04:27 +08:00
|
|
|
}
|
|
|
|
|
2017-06-29 05:58:57 +08:00
|
|
|
QuarantineChunk(m, ptr, stack);
|
2014-12-17 09:55:03 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void *Reallocate(void *old_ptr, uptr new_size, BufferedStackTrace *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);
|
|
|
|
|
|
|
|
AsanStats &thread_stats = GetCurrentThreadStats();
|
|
|
|
thread_stats.reallocs++;
|
|
|
|
thread_stats.realloced += new_size;
|
|
|
|
|
|
|
|
void *new_ptr = Allocate(new_size, 8, stack, FROM_MALLOC, true);
|
|
|
|
if (new_ptr) {
|
2020-09-06 15:07:14 +08:00
|
|
|
u8 chunk_state = atomic_load(&m->chunk_state, memory_order_acquire);
|
2014-12-17 09:55:03 +08:00
|
|
|
if (chunk_state != CHUNK_ALLOCATED)
|
|
|
|
ReportInvalidFree(old_ptr, chunk_state, stack);
|
2015-10-01 08:22:21 +08:00
|
|
|
CHECK_NE(REAL(memcpy), nullptr);
|
2014-12-17 09:55:03 +08:00
|
|
|
uptr memcpy_size = Min(new_size, m->UsedSize());
|
|
|
|
// If realloc() races with free(), we may start copying freed memory.
|
|
|
|
// However, we will report racy double-free later anyway.
|
|
|
|
REAL(memcpy)(new_ptr, old_ptr, memcpy_size);
|
2017-10-26 01:21:37 +08:00
|
|
|
Deallocate(old_ptr, 0, 0, stack, FROM_MALLOC);
|
2014-12-17 09:55:03 +08:00
|
|
|
}
|
|
|
|
return new_ptr;
|
|
|
|
}
|
2012-12-20 16:53:41 +08:00
|
|
|
|
2014-12-17 09:55:03 +08:00
|
|
|
void *Calloc(uptr nmemb, uptr size, BufferedStackTrace *stack) {
|
2018-03-29 02:22:40 +08:00
|
|
|
if (UNLIKELY(CheckForCallocOverflow(size, nmemb))) {
|
|
|
|
if (AllocatorMayReturnNull())
|
|
|
|
return nullptr;
|
|
|
|
ReportCallocOverflow(nmemb, size, stack);
|
|
|
|
}
|
2014-12-17 09:55:03 +08:00
|
|
|
void *ptr = Allocate(nmemb * size, 8, stack, FROM_MALLOC, false);
|
|
|
|
// If the memory comes from the secondary allocator no need to clear it
|
|
|
|
// as it comes directly from mmap.
|
|
|
|
if (ptr && allocator.FromPrimary(ptr))
|
|
|
|
REAL(memset)(ptr, 0, nmemb * size);
|
|
|
|
return ptr;
|
|
|
|
}
|
|
|
|
|
2020-09-06 14:41:25 +08:00
|
|
|
void ReportInvalidFree(void *ptr, u8 chunk_state, BufferedStackTrace *stack) {
|
2014-12-17 09:55:03 +08:00
|
|
|
if (chunk_state == CHUNK_QUARANTINE)
|
|
|
|
ReportDoubleFree((uptr)ptr, stack);
|
|
|
|
else
|
|
|
|
ReportFreeNotMalloced((uptr)ptr, stack);
|
|
|
|
}
|
|
|
|
|
2018-03-29 02:22:40 +08:00
|
|
|
void CommitBack(AsanThreadLocalMallocStorage *ms, BufferedStackTrace *stack) {
|
2013-01-11 16:07:43 +08:00
|
|
|
AllocatorCache *ac = GetAllocatorCache(ms);
|
2018-03-29 02:22:40 +08:00
|
|
|
quarantine.Drain(GetQuarantineCache(ms), QuarantineCallback(ac, stack));
|
2014-12-17 09:55:03 +08:00
|
|
|
allocator.SwallowCache(ac);
|
2012-12-17 17:06:25 +08:00
|
|
|
}
|
2012-12-11 17:02:36 +08:00
|
|
|
|
2014-12-17 09:55:03 +08:00
|
|
|
// -------------------------- Chunk lookup ----------------------
|
2013-05-20 21:05:58 +08:00
|
|
|
|
2014-12-17 09:55:03 +08:00
|
|
|
// Assumes alloc_beg == allocator.GetBlockBegin(alloc_beg).
|
2020-09-01 19:49:49 +08:00
|
|
|
// Returns nullptr if AsanChunk is not yet initialized just after
|
|
|
|
// get_allocator().Allocate(), or is being destroyed just before
|
|
|
|
// get_allocator().Deallocate().
|
2014-12-17 09:55:03 +08:00
|
|
|
AsanChunk *GetAsanChunk(void *alloc_beg) {
|
2020-09-04 16:17:18 +08:00
|
|
|
if (!alloc_beg)
|
|
|
|
return nullptr;
|
2020-09-15 15:22:10 +08:00
|
|
|
AsanChunk *p = reinterpret_cast<LargeChunkHeader *>(alloc_beg)->Get();
|
|
|
|
if (!p) {
|
|
|
|
if (!allocator.FromPrimary(alloc_beg))
|
|
|
|
return nullptr;
|
|
|
|
p = reinterpret_cast<AsanChunk *>(alloc_beg);
|
2014-12-17 09:55:03 +08:00
|
|
|
}
|
2020-09-10 07:17:37 +08:00
|
|
|
u8 state = atomic_load(&p->chunk_state, memory_order_relaxed);
|
|
|
|
// It does not guaranty that Chunk is initialized, but it's
|
|
|
|
// definitely not for any other value.
|
|
|
|
if (state == CHUNK_ALLOCATED || state == CHUNK_QUARANTINE)
|
|
|
|
return p;
|
|
|
|
return nullptr;
|
2014-07-30 17:48:23 +08:00
|
|
|
}
|
2013-05-20 21:05:58 +08:00
|
|
|
|
2014-12-17 09:55:03 +08:00
|
|
|
AsanChunk *GetAsanChunkByAddr(uptr p) {
|
|
|
|
void *alloc_beg = allocator.GetBlockBegin(reinterpret_cast<void *>(p));
|
|
|
|
return GetAsanChunk(alloc_beg);
|
|
|
|
}
|
2012-12-17 17:06:25 +08:00
|
|
|
|
2014-12-17 09:55:03 +08:00
|
|
|
// Allocator must be locked when this function is called.
|
|
|
|
AsanChunk *GetAsanChunkByAddrFastLocked(uptr p) {
|
|
|
|
void *alloc_beg =
|
|
|
|
allocator.GetBlockBeginFastLocked(reinterpret_cast<void *>(p));
|
|
|
|
return GetAsanChunk(alloc_beg);
|
|
|
|
}
|
2012-12-21 20:26:31 +08:00
|
|
|
|
2014-12-17 09:55:03 +08:00
|
|
|
uptr AllocationSize(uptr p) {
|
|
|
|
AsanChunk *m = GetAsanChunkByAddr(p);
|
|
|
|
if (!m) return 0;
|
2020-09-06 15:07:14 +08:00
|
|
|
if (atomic_load(&m->chunk_state, memory_order_acquire) != CHUNK_ALLOCATED)
|
|
|
|
return 0;
|
2014-12-17 09:55:03 +08:00
|
|
|
if (m->Beg() != p) return 0;
|
|
|
|
return m->UsedSize();
|
2012-12-17 17:06:25 +08:00
|
|
|
}
|
|
|
|
|
2014-12-17 09:55:03 +08:00
|
|
|
AsanChunkView FindHeapChunkByAddress(uptr addr) {
|
|
|
|
AsanChunk *m1 = GetAsanChunkByAddr(addr);
|
|
|
|
sptr offset = 0;
|
2020-09-10 07:17:37 +08:00
|
|
|
if (!m1 || AsanChunkView(m1).AddrIsAtLeft(addr, 1, &offset)) {
|
2014-12-17 09:55:03 +08:00
|
|
|
// 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.
|
2015-10-01 08:22:21 +08:00
|
|
|
AsanChunk *m2 = nullptr;
|
2014-12-17 09:55:03 +08:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2018-03-29 02:22:40 +08:00
|
|
|
void Purge(BufferedStackTrace *stack) {
|
2017-10-24 01:12:07 +08:00
|
|
|
AsanThread *t = GetCurrentThread();
|
|
|
|
if (t) {
|
|
|
|
AsanThreadLocalMallocStorage *ms = &t->malloc_storage();
|
|
|
|
quarantine.DrainAndRecycle(GetQuarantineCache(ms),
|
2018-03-29 02:22:40 +08:00
|
|
|
QuarantineCallback(GetAllocatorCache(ms),
|
|
|
|
stack));
|
2017-10-24 01:12:07 +08:00
|
|
|
}
|
|
|
|
{
|
|
|
|
SpinMutexLock l(&fallback_mutex);
|
|
|
|
quarantine.DrainAndRecycle(&fallback_quarantine_cache,
|
2018-03-29 02:22:40 +08:00
|
|
|
QuarantineCallback(&fallback_allocator_cache,
|
|
|
|
stack));
|
2017-10-24 01:12:07 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
allocator.ForceReleaseToOS();
|
|
|
|
}
|
|
|
|
|
2014-12-17 09:55:03 +08:00
|
|
|
void PrintStats() {
|
|
|
|
allocator.PrintStats();
|
2017-01-06 06:17:53 +08:00
|
|
|
quarantine.PrintStats();
|
2014-12-17 09:55:03 +08:00
|
|
|
}
|
|
|
|
|
2022-01-05 20:35:13 +08:00
|
|
|
void ForceLock() SANITIZER_ACQUIRE(fallback_mutex) {
|
2014-12-17 09:55:03 +08:00
|
|
|
allocator.ForceLock();
|
|
|
|
fallback_mutex.Lock();
|
|
|
|
}
|
|
|
|
|
2022-01-05 20:35:13 +08:00
|
|
|
void ForceUnlock() SANITIZER_RELEASE(fallback_mutex) {
|
2014-12-17 09:55:03 +08:00
|
|
|
fallback_mutex.Unlock();
|
|
|
|
allocator.ForceUnlock();
|
2012-12-20 22:35:06 +08:00
|
|
|
}
|
2014-12-17 09:55:03 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
static Allocator instance(LINKER_INITIALIZED);
|
|
|
|
|
2019-08-21 13:06:21 +08:00
|
|
|
static AsanAllocator &get_allocator() {
|
2014-12-17 09:55:03 +08:00
|
|
|
return instance.allocator;
|
|
|
|
}
|
|
|
|
|
2017-01-06 06:17:53 +08:00
|
|
|
bool AsanChunkView::IsValid() const {
|
2020-09-06 15:07:14 +08:00
|
|
|
return chunk_ && atomic_load(&chunk_->chunk_state, memory_order_relaxed) !=
|
|
|
|
CHUNK_INVALID;
|
2012-12-17 22:57:25 +08:00
|
|
|
}
|
2017-01-06 06:17:53 +08:00
|
|
|
bool AsanChunkView::IsAllocated() const {
|
2020-09-06 15:07:14 +08:00
|
|
|
return chunk_ && atomic_load(&chunk_->chunk_state, memory_order_relaxed) ==
|
|
|
|
CHUNK_ALLOCATED;
|
[asan] Assert in __sanitizer_ptr_{sub,cmp} if one of the pointers was freed.
Summary:
This (partially) implements the check mentioned at
http://kristerw.blogspot.co.uk/2016/04/dangling-pointers-and-undefined-behavior.html
(via John Regehr)
Quoting:
"That the behavior is undefined follows from C11 6.2.4 "Storage
durations of objects"
The lifetime of an object is the portion of program execution during
which storage is guaranteed to be reserved for it. An object exists, has
a constant address, and retains its last-stored value throughout its
lifetime. If an object is referred to outside of its lifetime, the
behavior is undefined. The value of a pointer becomes indeterminate when
the object it points to (or just past) reaches the end of its lifetime.
and 7.22.3 "Memory management functions" that says that free ends the
lifetime of objects
The lifetime of an allocated object extends from the allocation until
the deallocation.
"
We can probably implement this for stack variables too, but I think this
is a good start to see if there's interest in this check.
We can also hide this behind a flag, too.
Reviewers: samsonov, kcc, rsmith, regehr
Subscribers: kubabrecka, llvm-commits
Differential Revision: http://reviews.llvm.org/D19691
llvm-svn: 268097
2016-04-30 04:37:34 +08:00
|
|
|
}
|
2017-01-06 06:17:53 +08:00
|
|
|
bool AsanChunkView::IsQuarantined() const {
|
2020-09-06 15:07:14 +08:00
|
|
|
return chunk_ && atomic_load(&chunk_->chunk_state, memory_order_relaxed) ==
|
|
|
|
CHUNK_QUARANTINE;
|
2017-01-06 06:17:53 +08:00
|
|
|
}
|
|
|
|
uptr AsanChunkView::Beg() const { return chunk_->Beg(); }
|
|
|
|
uptr AsanChunkView::End() const { return Beg() + UsedSize(); }
|
|
|
|
uptr AsanChunkView::UsedSize() const { return chunk_->UsedSize(); }
|
2017-10-26 01:21:37 +08:00
|
|
|
u32 AsanChunkView::UserRequestedAlignment() const {
|
|
|
|
return Allocator::ComputeUserAlignment(chunk_->user_requested_alignment_log);
|
|
|
|
}
|
2020-09-11 10:59:31 +08:00
|
|
|
|
|
|
|
uptr AsanChunkView::AllocTid() const {
|
|
|
|
u32 tid = 0;
|
|
|
|
u32 stack = 0;
|
|
|
|
chunk_->GetAllocContext(tid, stack);
|
|
|
|
return tid;
|
|
|
|
}
|
|
|
|
|
2020-09-06 07:52:48 +08:00
|
|
|
uptr AsanChunkView::FreeTid() const {
|
2020-09-11 10:59:31 +08:00
|
|
|
if (!IsQuarantined())
|
|
|
|
return kInvalidTid;
|
|
|
|
u32 tid = 0;
|
|
|
|
u32 stack = 0;
|
|
|
|
chunk_->GetFreeContext(tid, stack);
|
|
|
|
return tid;
|
2020-09-06 07:52:48 +08:00
|
|
|
}
|
2020-09-11 10:59:31 +08:00
|
|
|
|
2017-01-06 06:17:53 +08:00
|
|
|
AllocType AsanChunkView::GetAllocType() const {
|
2016-08-17 17:24:33 +08:00
|
|
|
return (AllocType)chunk_->alloc_type;
|
2016-08-17 17:16:08 +08:00
|
|
|
}
|
2012-12-17 17:06:25 +08:00
|
|
|
|
2020-09-06 15:07:14 +08:00
|
|
|
u32 AsanChunkView::GetAllocStackId() const {
|
2020-09-11 10:59:31 +08:00
|
|
|
u32 tid = 0;
|
|
|
|
u32 stack = 0;
|
|
|
|
chunk_->GetAllocContext(tid, stack);
|
|
|
|
return stack;
|
2020-09-06 15:07:14 +08:00
|
|
|
}
|
2020-09-11 10:59:31 +08:00
|
|
|
|
2020-09-06 07:52:48 +08:00
|
|
|
u32 AsanChunkView::GetFreeStackId() const {
|
2020-09-11 10:59:31 +08:00
|
|
|
if (!IsQuarantined())
|
|
|
|
return 0;
|
|
|
|
u32 tid = 0;
|
|
|
|
u32 stack = 0;
|
|
|
|
chunk_->GetFreeContext(tid, stack);
|
|
|
|
return stack;
|
2020-09-06 07:52:48 +08:00
|
|
|
}
|
2016-06-02 09:21:52 +08:00
|
|
|
|
2014-12-20 03:35:11 +08:00
|
|
|
void InitializeAllocator(const AllocatorOptions &options) {
|
2017-12-14 08:07:15 +08:00
|
|
|
instance.InitLinkerInitialized(options);
|
2014-12-17 09:55:03 +08:00
|
|
|
}
|
|
|
|
|
2014-12-20 03:35:11 +08:00
|
|
|
void ReInitializeAllocator(const AllocatorOptions &options) {
|
|
|
|
instance.ReInitialize(options);
|
2012-12-19 16:32:50 +08:00
|
|
|
}
|
|
|
|
|
2014-12-20 04:35:53 +08:00
|
|
|
void GetAllocatorOptions(AllocatorOptions *options) {
|
|
|
|
instance.GetOptions(options);
|
|
|
|
}
|
|
|
|
|
2012-12-19 16:32:50 +08:00
|
|
|
AsanChunkView FindHeapChunkByAddress(uptr addr) {
|
2014-12-17 09:55:03 +08:00
|
|
|
return instance.FindHeapChunkByAddress(addr);
|
2012-12-11 17:02:36 +08:00
|
|
|
}
|
2016-08-24 02:13:51 +08:00
|
|
|
AsanChunkView FindHeapChunkByAllocBeg(uptr addr) {
|
|
|
|
return AsanChunkView(instance.GetAsanChunk(reinterpret_cast<void*>(addr)));
|
|
|
|
}
|
2012-12-11 17:02:36 +08:00
|
|
|
|
|
|
|
void AsanThreadLocalMallocStorage::CommitBack() {
|
2018-03-29 02:22:40 +08:00
|
|
|
GET_STACK_TRACE_MALLOC;
|
|
|
|
instance.CommitBack(this, &stack);
|
2012-12-11 17:02:36 +08:00
|
|
|
}
|
|
|
|
|
2012-12-27 22:09:19 +08:00
|
|
|
void PrintInternalAllocatorStats() {
|
2014-12-17 09:55:03 +08:00
|
|
|
instance.PrintStats();
|
2012-12-27 22:09:19 +08:00
|
|
|
}
|
|
|
|
|
2014-10-26 11:35:14 +08:00
|
|
|
void asan_free(void *ptr, BufferedStackTrace *stack, AllocType alloc_type) {
|
2017-10-26 01:21:37 +08:00
|
|
|
instance.Deallocate(ptr, 0, 0, stack, alloc_type);
|
2014-07-30 17:48:23 +08:00
|
|
|
}
|
|
|
|
|
2017-10-26 01:21:37 +08:00
|
|
|
void asan_delete(void *ptr, uptr size, uptr alignment,
|
|
|
|
BufferedStackTrace *stack, AllocType alloc_type) {
|
|
|
|
instance.Deallocate(ptr, size, alignment, stack, alloc_type);
|
2012-12-11 17:02:36 +08:00
|
|
|
}
|
|
|
|
|
2014-10-26 11:35:14 +08:00
|
|
|
void *asan_malloc(uptr size, BufferedStackTrace *stack) {
|
2017-07-19 03:11:04 +08:00
|
|
|
return SetErrnoOnNull(instance.Allocate(size, 8, stack, FROM_MALLOC, true));
|
2012-12-11 17:02:36 +08:00
|
|
|
}
|
|
|
|
|
2014-10-26 11:35:14 +08:00
|
|
|
void *asan_calloc(uptr nmemb, uptr size, BufferedStackTrace *stack) {
|
2017-07-19 03:11:04 +08:00
|
|
|
return SetErrnoOnNull(instance.Calloc(nmemb, size, stack));
|
2012-12-11 17:02:36 +08:00
|
|
|
}
|
|
|
|
|
2019-05-02 01:33:01 +08:00
|
|
|
void *asan_reallocarray(void *p, uptr nmemb, uptr size,
|
|
|
|
BufferedStackTrace *stack) {
|
|
|
|
if (UNLIKELY(CheckForCallocOverflow(size, nmemb))) {
|
|
|
|
errno = errno_ENOMEM;
|
|
|
|
if (AllocatorMayReturnNull())
|
|
|
|
return nullptr;
|
|
|
|
ReportReallocArrayOverflow(nmemb, size, stack);
|
|
|
|
}
|
|
|
|
return asan_realloc(p, nmemb * size, stack);
|
|
|
|
}
|
|
|
|
|
2014-10-26 11:35:14 +08:00
|
|
|
void *asan_realloc(void *p, uptr size, BufferedStackTrace *stack) {
|
2015-10-01 08:22:21 +08:00
|
|
|
if (!p)
|
2017-07-19 03:11:04 +08:00
|
|
|
return SetErrnoOnNull(instance.Allocate(size, 8, stack, FROM_MALLOC, true));
|
2012-12-14 21:16:19 +08:00
|
|
|
if (size == 0) {
|
2017-03-30 02:17:22 +08:00
|
|
|
if (flags()->allocator_frees_and_returns_null_on_realloc_zero) {
|
2017-10-26 01:21:37 +08:00
|
|
|
instance.Deallocate(p, 0, 0, stack, FROM_MALLOC);
|
2017-03-30 02:17:22 +08:00
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
// Allocate a size of 1 if we shouldn't free() on Realloc to 0
|
|
|
|
size = 1;
|
2012-12-14 21:16:19 +08:00
|
|
|
}
|
2017-07-19 03:11:04 +08:00
|
|
|
return SetErrnoOnNull(instance.Reallocate(p, size, stack));
|
2012-12-11 17:02:36 +08:00
|
|
|
}
|
|
|
|
|
2014-10-26 11:35:14 +08:00
|
|
|
void *asan_valloc(uptr size, BufferedStackTrace *stack) {
|
2017-07-19 03:11:04 +08:00
|
|
|
return SetErrnoOnNull(
|
2017-07-15 06:23:47 +08:00
|
|
|
instance.Allocate(size, GetPageSizeCached(), stack, FROM_MALLOC, true));
|
2012-12-11 17:02:36 +08:00
|
|
|
}
|
|
|
|
|
2014-10-26 11:35:14 +08:00
|
|
|
void *asan_pvalloc(uptr size, BufferedStackTrace *stack) {
|
2012-12-14 21:16:19 +08:00
|
|
|
uptr PageSize = GetPageSizeCached();
|
2017-08-05 04:28:59 +08:00
|
|
|
if (UNLIKELY(CheckForPvallocOverflow(size, PageSize))) {
|
|
|
|
errno = errno_ENOMEM;
|
2018-03-29 02:22:40 +08:00
|
|
|
if (AllocatorMayReturnNull())
|
|
|
|
return nullptr;
|
|
|
|
ReportPvallocOverflow(size, stack);
|
2017-08-05 04:28:59 +08:00
|
|
|
}
|
2017-07-15 06:23:47 +08:00
|
|
|
// pvalloc(0) should allocate one page.
|
|
|
|
size = size ? RoundUpTo(size, PageSize) : PageSize;
|
2017-07-19 03:11:04 +08:00
|
|
|
return SetErrnoOnNull(
|
|
|
|
instance.Allocate(size, PageSize, stack, FROM_MALLOC, true));
|
2017-07-15 06:23:47 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void *asan_memalign(uptr alignment, uptr size, BufferedStackTrace *stack,
|
|
|
|
AllocType alloc_type) {
|
|
|
|
if (UNLIKELY(!IsPowerOfTwo(alignment))) {
|
|
|
|
errno = errno_EINVAL;
|
2018-03-29 02:22:40 +08:00
|
|
|
if (AllocatorMayReturnNull())
|
|
|
|
return nullptr;
|
|
|
|
ReportInvalidAllocationAlignment(alignment, stack);
|
2012-12-14 21:16:19 +08:00
|
|
|
}
|
2017-07-19 03:11:04 +08:00
|
|
|
return SetErrnoOnNull(
|
2017-07-15 06:23:47 +08:00
|
|
|
instance.Allocate(size, alignment, stack, alloc_type, true));
|
2012-12-11 17:02:36 +08:00
|
|
|
}
|
|
|
|
|
2018-06-09 04:40:35 +08:00
|
|
|
void *asan_aligned_alloc(uptr alignment, uptr size, BufferedStackTrace *stack) {
|
|
|
|
if (UNLIKELY(!CheckAlignedAllocAlignmentAndSize(alignment, size))) {
|
|
|
|
errno = errno_EINVAL;
|
|
|
|
if (AllocatorMayReturnNull())
|
|
|
|
return nullptr;
|
|
|
|
ReportInvalidAlignedAllocAlignment(size, alignment, stack);
|
|
|
|
}
|
|
|
|
return SetErrnoOnNull(
|
|
|
|
instance.Allocate(size, alignment, stack, FROM_MALLOC, true));
|
|
|
|
}
|
|
|
|
|
2012-12-11 17:02:36 +08:00
|
|
|
int asan_posix_memalign(void **memptr, uptr alignment, uptr size,
|
2014-10-26 11:35:14 +08:00
|
|
|
BufferedStackTrace *stack) {
|
2017-07-19 03:11:04 +08:00
|
|
|
if (UNLIKELY(!CheckPosixMemalignAlignment(alignment))) {
|
2018-03-29 02:22:40 +08:00
|
|
|
if (AllocatorMayReturnNull())
|
|
|
|
return errno_EINVAL;
|
|
|
|
ReportInvalidPosixMemalignAlignment(alignment, stack);
|
2017-07-15 06:23:47 +08:00
|
|
|
}
|
2014-12-17 09:55:03 +08:00
|
|
|
void *ptr = instance.Allocate(size, alignment, stack, FROM_MALLOC, true);
|
2017-07-19 03:11:04 +08:00
|
|
|
if (UNLIKELY(!ptr))
|
2018-03-29 02:22:40 +08:00
|
|
|
// OOM error is already taken care of by Allocate.
|
2017-07-15 06:23:47 +08:00
|
|
|
return errno_ENOMEM;
|
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;
|
|
|
|
}
|
|
|
|
|
2016-03-25 04:19:48 +08:00
|
|
|
uptr asan_malloc_usable_size(const void *ptr, uptr pc, uptr bp) {
|
2015-10-01 08:22:21 +08:00
|
|
|
if (!ptr) return 0;
|
2014-12-17 09:55:03 +08:00
|
|
|
uptr usable_size = instance.AllocationSize(reinterpret_cast<uptr>(ptr));
|
2013-11-13 22:46:58 +08:00
|
|
|
if (flags()->check_malloc_usable_size && (usable_size == 0)) {
|
|
|
|
GET_STACK_TRACE_FATAL(pc, bp);
|
|
|
|
ReportMallocUsableSizeNotOwned((uptr)ptr, &stack);
|
|
|
|
}
|
2012-12-17 22:57:25 +08:00
|
|
|
return usable_size;
|
2012-12-11 17:02:36 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
uptr asan_mz_size(const void *ptr) {
|
2014-12-17 09:55:03 +08:00
|
|
|
return instance.AllocationSize(reinterpret_cast<uptr>(ptr));
|
2012-12-11 17:02:36 +08:00
|
|
|
}
|
|
|
|
|
2022-01-05 20:35:13 +08:00
|
|
|
void asan_mz_force_lock() SANITIZER_NO_THREAD_SAFETY_ANALYSIS {
|
|
|
|
instance.ForceLock();
|
|
|
|
}
|
2012-12-11 17:02:36 +08:00
|
|
|
|
2022-01-05 20:35:13 +08:00
|
|
|
void asan_mz_force_unlock() SANITIZER_NO_THREAD_SAFETY_ANALYSIS {
|
2014-12-17 09:55:03 +08:00
|
|
|
instance.ForceUnlock();
|
2012-12-11 17:02:36 +08:00
|
|
|
}
|
|
|
|
|
2020-09-04 16:17:18 +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() {
|
2014-12-17 09:55:03 +08:00
|
|
|
__asan::get_allocator().ForceLock();
|
2013-05-21 21:46:41 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void UnlockAllocator() {
|
2014-12-17 09:55:03 +08:00
|
|
|
__asan::get_allocator().ForceUnlock();
|
2013-05-21 21:46:41 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void GetAllocatorGlobalRange(uptr *begin, uptr *end) {
|
2014-12-17 09:55:03 +08:00
|
|
|
*begin = (uptr)&__asan::get_allocator();
|
|
|
|
*end = *begin + sizeof(__asan::get_allocator());
|
2013-05-21 21:46:41 +08:00
|
|
|
}
|
|
|
|
|
2020-09-15 04:32:14 +08:00
|
|
|
uptr PointsIntoChunk(void *p) {
|
2013-05-21 21:46:41 +08:00
|
|
|
uptr addr = reinterpret_cast<uptr>(p);
|
2014-12-17 09:55:03 +08:00
|
|
|
__asan::AsanChunk *m = __asan::instance.GetAsanChunkByAddrFastLocked(addr);
|
2020-09-06 15:07:14 +08:00
|
|
|
if (!m || atomic_load(&m->chunk_state, memory_order_acquire) !=
|
|
|
|
__asan::CHUNK_ALLOCATED)
|
2020-09-06 14:41:25 +08:00
|
|
|
return 0;
|
2020-09-16 15:03:07 +08:00
|
|
|
uptr chunk = m->Beg();
|
|
|
|
if (m->AddrIsInside(addr))
|
|
|
|
return chunk;
|
|
|
|
if (IsSpecialCaseOfOperatorNew0(chunk, m->UsedSize(), addr))
|
|
|
|
return chunk;
|
|
|
|
return 0;
|
2013-05-21 21:46:41 +08:00
|
|
|
}
|
|
|
|
|
2013-06-24 16:34:50 +08:00
|
|
|
uptr GetUserBegin(uptr chunk) {
|
2014-12-17 09:55:03 +08:00
|
|
|
__asan::AsanChunk *m = __asan::instance.GetAsanChunkByAddrFastLocked(chunk);
|
2020-09-01 19:49:49 +08:00
|
|
|
return m ? m->Beg() : 0;
|
2013-05-21 21:46:41 +08:00
|
|
|
}
|
|
|
|
|
2013-06-24 16:34:50 +08:00
|
|
|
LsanMetadata::LsanMetadata(uptr chunk) {
|
2020-09-01 19:49:49 +08:00
|
|
|
metadata_ = chunk ? reinterpret_cast<void *>(chunk - __asan::kChunkHeaderSize)
|
|
|
|
: nullptr;
|
2013-05-21 21:46:41 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
bool LsanMetadata::allocated() const {
|
2020-09-01 19:49:49 +08:00
|
|
|
if (!metadata_)
|
|
|
|
return false;
|
2013-05-21 21:46:41 +08:00
|
|
|
__asan::AsanChunk *m = reinterpret_cast<__asan::AsanChunk *>(metadata_);
|
2020-09-06 15:07:14 +08:00
|
|
|
return atomic_load(&m->chunk_state, memory_order_relaxed) ==
|
|
|
|
__asan::CHUNK_ALLOCATED;
|
2013-05-21 21:46:41 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
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_);
|
2020-09-15 14:53:58 +08:00
|
|
|
return m->UsedSize();
|
2013-05-21 21:46:41 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
u32 LsanMetadata::stack_trace_id() const {
|
|
|
|
__asan::AsanChunk *m = reinterpret_cast<__asan::AsanChunk *>(metadata_);
|
2020-09-11 10:59:31 +08:00
|
|
|
u32 tid = 0;
|
|
|
|
u32 stack = 0;
|
|
|
|
m->GetAllocContext(tid, stack);
|
|
|
|
return stack;
|
2013-05-21 21:46:41 +08:00
|
|
|
}
|
|
|
|
|
2013-06-24 16:34:50 +08:00
|
|
|
void ForEachChunk(ForEachChunkCallback callback, void *arg) {
|
2014-12-17 09:55:03 +08:00
|
|
|
__asan::get_allocator().ForEachChunk(callback, arg);
|
2013-05-21 21:46:41 +08:00
|
|
|
}
|
2013-06-06 22:17:56 +08:00
|
|
|
|
|
|
|
IgnoreObjectResult IgnoreObjectLocked(const void *p) {
|
|
|
|
uptr addr = reinterpret_cast<uptr>(p);
|
2014-12-17 09:55:03 +08:00
|
|
|
__asan::AsanChunk *m = __asan::instance.GetAsanChunkByAddr(addr);
|
2020-09-16 15:27:13 +08:00
|
|
|
if (!m ||
|
|
|
|
(atomic_load(&m->chunk_state, memory_order_acquire) !=
|
|
|
|
__asan::CHUNK_ALLOCATED) ||
|
|
|
|
!m->AddrIsInside(addr)) {
|
2020-09-15 07:32:25 +08:00
|
|
|
return kIgnoreObjectInvalid;
|
2013-06-06 22:17:56 +08:00
|
|
|
}
|
2020-09-15 07:32:25 +08:00
|
|
|
if (m->lsan_tag == kIgnored)
|
|
|
|
return kIgnoreObjectAlreadyIgnored;
|
|
|
|
m->lsan_tag = __lsan::kIgnored;
|
|
|
|
return kIgnoreObjectSuccess;
|
2013-06-06 22:17:56 +08:00
|
|
|
}
|
2021-01-22 07:48:41 +08:00
|
|
|
|
|
|
|
void GetAdditionalThreadContextPtrs(ThreadContextBase *tctx, void *ptrs) {
|
2021-01-07 09:41:46 +08:00
|
|
|
// Look for the arg pointer of threads that have been created or are running.
|
|
|
|
// This is necessary to prevent false positive leaks due to the AsanThread
|
|
|
|
// holding the only live reference to a heap object. This can happen because
|
|
|
|
// the `pthread_create()` interceptor doesn't wait for the child thread to
|
|
|
|
// start before returning and thus loosing the the only live reference to the
|
|
|
|
// heap object on the stack.
|
|
|
|
|
|
|
|
__asan::AsanThreadContext *atctx =
|
|
|
|
reinterpret_cast<__asan::AsanThreadContext *>(tctx);
|
|
|
|
__asan::AsanThread *asan_thread = atctx->thread;
|
|
|
|
|
|
|
|
// Note ThreadStatusRunning is required because there is a small window where
|
|
|
|
// the thread status switches to `ThreadStatusRunning` but the `arg` pointer
|
|
|
|
// still isn't on the stack yet.
|
|
|
|
if (atctx->status != ThreadStatusCreated &&
|
|
|
|
atctx->status != ThreadStatusRunning)
|
|
|
|
return;
|
|
|
|
|
|
|
|
uptr thread_arg = reinterpret_cast<uptr>(asan_thread->get_arg());
|
|
|
|
if (!thread_arg)
|
|
|
|
return;
|
|
|
|
|
|
|
|
auto ptrsVec = reinterpret_cast<InternalMmapVector<uptr> *>(ptrs);
|
|
|
|
ptrsVec->push_back(thread_arg);
|
2021-01-22 07:48:41 +08:00
|
|
|
}
|
|
|
|
|
2013-05-21 21:46:41 +08:00
|
|
|
} // namespace __lsan
|
|
|
|
|
2012-12-11 17:02:36 +08:00
|
|
|
// ---------------------- Interface ---------------- {{{1
|
2019-09-12 07:19:48 +08:00
|
|
|
using namespace __asan;
|
2012-12-11 17:02:36 +08:00
|
|
|
|
|
|
|
// 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.
|
2014-07-08 01:39:31 +08:00
|
|
|
uptr __sanitizer_get_estimated_allocated_size(uptr size) {
|
2012-12-20 16:53:41 +08:00
|
|
|
return size;
|
2012-12-11 17:02:36 +08:00
|
|
|
}
|
|
|
|
|
2014-07-08 01:39:31 +08:00
|
|
|
int __sanitizer_get_ownership(const void *p) {
|
2013-01-17 21:25:17 +08:00
|
|
|
uptr ptr = reinterpret_cast<uptr>(p);
|
2014-12-17 09:55:03 +08:00
|
|
|
return instance.AllocationSize(ptr) > 0;
|
2012-12-11 17:02:36 +08:00
|
|
|
}
|
|
|
|
|
2014-07-08 01:39:31 +08:00
|
|
|
uptr __sanitizer_get_allocated_size(const void *p) {
|
2015-10-01 08:22:21 +08:00
|
|
|
if (!p) return 0;
|
2013-01-17 21:25:17 +08:00
|
|
|
uptr ptr = reinterpret_cast<uptr>(p);
|
2014-12-17 09:55:03 +08:00
|
|
|
uptr allocated_size = instance.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;
|
2014-07-08 01:39:31 +08:00
|
|
|
ReportSanitizerGetAllocatedSizeNotOwned(ptr, &stack);
|
2012-12-20 16:53:41 +08:00
|
|
|
}
|
|
|
|
return allocated_size;
|
2012-12-11 17:02:36 +08:00
|
|
|
}
|
|
|
|
|
2017-10-24 01:12:07 +08:00
|
|
|
void __sanitizer_purge_allocator() {
|
2018-03-29 02:22:40 +08:00
|
|
|
GET_STACK_TRACE_MALLOC;
|
|
|
|
instance.Purge(&stack);
|
2017-10-24 01:12:07 +08:00
|
|
|
}
|
|
|
|
|
2019-10-19 08:17:48 +08:00
|
|
|
int __asan_update_allocation_context(void* addr) {
|
|
|
|
GET_STACK_TRACE_MALLOC;
|
|
|
|
return instance.UpdateAllocationStack((uptr)addr, &stack);
|
|
|
|
}
|