Path: blob/main/contrib/llvm-project/compiler-rt/lib/asan/asan_allocator.cpp
35233 views
//===-- asan_allocator.cpp ------------------------------------------------===//1//2// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.3// See https://llvm.org/LICENSE.txt for license information.4// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception5//6//===----------------------------------------------------------------------===//7//8// This file is a part of AddressSanitizer, an address sanity checker.9//10// Implementation of ASan's memory allocator, 2-nd version.11// This variant uses the allocator from sanitizer_common, i.e. the one shared12// with ThreadSanitizer and MemorySanitizer.13//14//===----------------------------------------------------------------------===//1516#include "asan_allocator.h"1718#include "asan_internal.h"19#include "asan_mapping.h"20#include "asan_poisoning.h"21#include "asan_report.h"22#include "asan_stack.h"23#include "asan_thread.h"24#include "lsan/lsan_common.h"25#include "sanitizer_common/sanitizer_allocator_checks.h"26#include "sanitizer_common/sanitizer_allocator_interface.h"27#include "sanitizer_common/sanitizer_common.h"28#include "sanitizer_common/sanitizer_errno.h"29#include "sanitizer_common/sanitizer_flags.h"30#include "sanitizer_common/sanitizer_internal_defs.h"31#include "sanitizer_common/sanitizer_list.h"32#include "sanitizer_common/sanitizer_quarantine.h"33#include "sanitizer_common/sanitizer_stackdepot.h"3435namespace __asan {3637// Valid redzone sizes are 16, 32, 64, ... 2048, so we encode them in 3 bits.38// We use adaptive redzones: for larger allocation larger redzones are used.39static u32 RZLog2Size(u32 rz_log) {40CHECK_LT(rz_log, 8);41return 16 << rz_log;42}4344static u32 RZSize2Log(u32 rz_size) {45CHECK_GE(rz_size, 16);46CHECK_LE(rz_size, 2048);47CHECK(IsPowerOfTwo(rz_size));48u32 res = Log2(rz_size) - 4;49CHECK_EQ(rz_size, RZLog2Size(res));50return res;51}5253static AsanAllocator &get_allocator();5455static void AtomicContextStore(volatile atomic_uint64_t *atomic_context,56u32 tid, u32 stack) {57u64 context = tid;58context <<= 32;59context += stack;60atomic_store(atomic_context, context, memory_order_relaxed);61}6263static void AtomicContextLoad(const volatile atomic_uint64_t *atomic_context,64u32 &tid, u32 &stack) {65u64 context = atomic_load(atomic_context, memory_order_relaxed);66stack = context;67context >>= 32;68tid = context;69}7071// The memory chunk allocated from the underlying allocator looks like this:72// L L L L L L H H U U U U U U R R73// L -- left redzone words (0 or more bytes)74// H -- ChunkHeader (16 bytes), which is also a part of the left redzone.75// U -- user memory.76// R -- right redzone (0 or more bytes)77// ChunkBase consists of ChunkHeader and other bytes that overlap with user78// memory.7980// If the left redzone is greater than the ChunkHeader size we store a magic81// value in the first uptr word of the memory block and store the address of82// ChunkBase in the next uptr.83// M B L L L L L L L L L H H U U U U U U84// | ^85// ---------------------|86// M -- magic value kAllocBegMagic87// B -- address of ChunkHeader pointing to the first 'H'8889class ChunkHeader {90public:91atomic_uint8_t chunk_state;92u8 alloc_type : 2;93u8 lsan_tag : 2;9495// align < 8 -> 096// else -> log2(min(align, 512)) - 297u8 user_requested_alignment_log : 3;9899private:100u16 user_requested_size_hi;101u32 user_requested_size_lo;102atomic_uint64_t alloc_context_id;103104public:105uptr UsedSize() const {106static_assert(sizeof(user_requested_size_lo) == 4,107"Expression below requires this");108return FIRST_32_SECOND_64(0, ((uptr)user_requested_size_hi << 32)) +109user_requested_size_lo;110}111112void SetUsedSize(uptr size) {113user_requested_size_lo = size;114static_assert(sizeof(user_requested_size_lo) == 4,115"Expression below requires this");116user_requested_size_hi = FIRST_32_SECOND_64(0, size >> 32);117CHECK_EQ(UsedSize(), size);118}119120void SetAllocContext(u32 tid, u32 stack) {121AtomicContextStore(&alloc_context_id, tid, stack);122}123124void GetAllocContext(u32 &tid, u32 &stack) const {125AtomicContextLoad(&alloc_context_id, tid, stack);126}127};128129class ChunkBase : public ChunkHeader {130atomic_uint64_t free_context_id;131132public:133void SetFreeContext(u32 tid, u32 stack) {134AtomicContextStore(&free_context_id, tid, stack);135}136137void GetFreeContext(u32 &tid, u32 &stack) const {138AtomicContextLoad(&free_context_id, tid, stack);139}140};141142static const uptr kChunkHeaderSize = sizeof(ChunkHeader);143static const uptr kChunkHeader2Size = sizeof(ChunkBase) - kChunkHeaderSize;144COMPILER_CHECK(kChunkHeaderSize == 16);145COMPILER_CHECK(kChunkHeader2Size <= 16);146147enum {148// Either just allocated by underlying allocator, but AsanChunk is not yet149// ready, or almost returned to undelying allocator and AsanChunk is already150// meaningless.151CHUNK_INVALID = 0,152// The chunk is allocated and not yet freed.153CHUNK_ALLOCATED = 2,154// The chunk was freed and put into quarantine zone.155CHUNK_QUARANTINE = 3,156};157158class AsanChunk : public ChunkBase {159public:160uptr Beg() { return reinterpret_cast<uptr>(this) + kChunkHeaderSize; }161bool AddrIsInside(uptr addr) {162return (addr >= Beg()) && (addr < Beg() + UsedSize());163}164};165166class LargeChunkHeader {167static constexpr uptr kAllocBegMagic =168FIRST_32_SECOND_64(0xCC6E96B9, 0xCC6E96B9CC6E96B9ULL);169atomic_uintptr_t magic;170AsanChunk *chunk_header;171172public:173AsanChunk *Get() const {174return atomic_load(&magic, memory_order_acquire) == kAllocBegMagic175? chunk_header176: nullptr;177}178179void Set(AsanChunk *p) {180if (p) {181chunk_header = p;182atomic_store(&magic, kAllocBegMagic, memory_order_release);183return;184}185186uptr old = kAllocBegMagic;187if (!atomic_compare_exchange_strong(&magic, &old, 0,188memory_order_release)) {189CHECK_EQ(old, kAllocBegMagic);190}191}192};193194static void FillChunk(AsanChunk *m) {195// FIXME: Use ReleaseMemoryPagesToOS.196Flags &fl = *flags();197198if (fl.max_free_fill_size > 0) {199// We have to skip the chunk header, it contains free_context_id.200uptr scribble_start = (uptr)m + kChunkHeaderSize + kChunkHeader2Size;201if (m->UsedSize() >= kChunkHeader2Size) { // Skip Header2 in user area.202uptr size_to_fill = m->UsedSize() - kChunkHeader2Size;203size_to_fill = Min(size_to_fill, (uptr)fl.max_free_fill_size);204REAL(memset)((void *)scribble_start, fl.free_fill_byte, size_to_fill);205}206}207}208209struct QuarantineCallback {210QuarantineCallback(AllocatorCache *cache, BufferedStackTrace *stack)211: cache_(cache),212stack_(stack) {213}214215void PreQuarantine(AsanChunk *m) const {216FillChunk(m);217// Poison the region.218PoisonShadow(m->Beg(), RoundUpTo(m->UsedSize(), ASAN_SHADOW_GRANULARITY),219kAsanHeapFreeMagic);220}221222void Recycle(AsanChunk *m) const {223void *p = get_allocator().GetBlockBegin(m);224225// The secondary will immediately unpoison and unmap the memory, so this226// branch is unnecessary.227if (get_allocator().FromPrimary(p)) {228if (p != m) {229// Clear the magic value, as allocator internals may overwrite the230// contents of deallocated chunk, confusing GetAsanChunk lookup.231reinterpret_cast<LargeChunkHeader *>(p)->Set(nullptr);232}233234u8 old_chunk_state = CHUNK_QUARANTINE;235if (!atomic_compare_exchange_strong(&m->chunk_state, &old_chunk_state,236CHUNK_INVALID,237memory_order_acquire)) {238CHECK_EQ(old_chunk_state, CHUNK_QUARANTINE);239}240241PoisonShadow(m->Beg(), RoundUpTo(m->UsedSize(), ASAN_SHADOW_GRANULARITY),242kAsanHeapLeftRedzoneMagic);243}244245// Statistics.246AsanStats &thread_stats = GetCurrentThreadStats();247thread_stats.real_frees++;248thread_stats.really_freed += m->UsedSize();249250get_allocator().Deallocate(cache_, p);251}252253void RecyclePassThrough(AsanChunk *m) const {254// Recycle for the secondary will immediately unpoison and unmap the255// memory, so quarantine preparation is unnecessary.256if (get_allocator().FromPrimary(m)) {257// The primary allocation may need pattern fill if enabled.258FillChunk(m);259}260Recycle(m);261}262263void *Allocate(uptr size) const {264void *res = get_allocator().Allocate(cache_, size, 1);265// TODO(alekseys): Consider making quarantine OOM-friendly.266if (UNLIKELY(!res))267ReportOutOfMemory(size, stack_);268return res;269}270271void Deallocate(void *p) const { get_allocator().Deallocate(cache_, p); }272273private:274AllocatorCache* const cache_;275BufferedStackTrace* const stack_;276};277278typedef Quarantine<QuarantineCallback, AsanChunk> AsanQuarantine;279typedef AsanQuarantine::Cache QuarantineCache;280281void AsanMapUnmapCallback::OnMap(uptr p, uptr size) const {282PoisonShadow(p, size, kAsanHeapLeftRedzoneMagic);283// Statistics.284AsanStats &thread_stats = GetCurrentThreadStats();285thread_stats.mmaps++;286thread_stats.mmaped += size;287}288289void AsanMapUnmapCallback::OnMapSecondary(uptr p, uptr size, uptr user_begin,290uptr user_size) const {291uptr user_end = RoundDownTo(user_begin + user_size, ASAN_SHADOW_GRANULARITY);292user_begin = RoundUpTo(user_begin, ASAN_SHADOW_GRANULARITY);293// The secondary mapping will be immediately returned to user, no value294// poisoning that with non-zero just before unpoisoning by Allocate(). So just295// poison head/tail invisible to Allocate().296PoisonShadow(p, user_begin - p, kAsanHeapLeftRedzoneMagic);297PoisonShadow(user_end, size - (user_end - p), kAsanHeapLeftRedzoneMagic);298// Statistics.299AsanStats &thread_stats = GetCurrentThreadStats();300thread_stats.mmaps++;301thread_stats.mmaped += size;302}303304void AsanMapUnmapCallback::OnUnmap(uptr p, uptr size) const {305PoisonShadow(p, size, 0);306// We are about to unmap a chunk of user memory.307// Mark the corresponding shadow memory as not needed.308FlushUnneededASanShadowMemory(p, size);309// Statistics.310AsanStats &thread_stats = GetCurrentThreadStats();311thread_stats.munmaps++;312thread_stats.munmaped += size;313}314315// We can not use THREADLOCAL because it is not supported on some of the316// platforms we care about (OSX 10.6, Android).317// static THREADLOCAL AllocatorCache cache;318AllocatorCache *GetAllocatorCache(AsanThreadLocalMallocStorage *ms) {319CHECK(ms);320return &ms->allocator_cache;321}322323QuarantineCache *GetQuarantineCache(AsanThreadLocalMallocStorage *ms) {324CHECK(ms);325CHECK_LE(sizeof(QuarantineCache), sizeof(ms->quarantine_cache));326return reinterpret_cast<QuarantineCache *>(ms->quarantine_cache);327}328329void AllocatorOptions::SetFrom(const Flags *f, const CommonFlags *cf) {330quarantine_size_mb = f->quarantine_size_mb;331thread_local_quarantine_size_kb = f->thread_local_quarantine_size_kb;332min_redzone = f->redzone;333max_redzone = f->max_redzone;334may_return_null = cf->allocator_may_return_null;335alloc_dealloc_mismatch = f->alloc_dealloc_mismatch;336release_to_os_interval_ms = cf->allocator_release_to_os_interval_ms;337}338339void AllocatorOptions::CopyTo(Flags *f, CommonFlags *cf) {340f->quarantine_size_mb = quarantine_size_mb;341f->thread_local_quarantine_size_kb = thread_local_quarantine_size_kb;342f->redzone = min_redzone;343f->max_redzone = max_redzone;344cf->allocator_may_return_null = may_return_null;345f->alloc_dealloc_mismatch = alloc_dealloc_mismatch;346cf->allocator_release_to_os_interval_ms = release_to_os_interval_ms;347}348349struct Allocator {350static const uptr kMaxAllowedMallocSize =351FIRST_32_SECOND_64(3UL << 30, 1ULL << 40);352353AsanAllocator allocator;354AsanQuarantine quarantine;355StaticSpinMutex fallback_mutex;356AllocatorCache fallback_allocator_cache;357QuarantineCache fallback_quarantine_cache;358359uptr max_user_defined_malloc_size;360361// ------------------- Options --------------------------362atomic_uint16_t min_redzone;363atomic_uint16_t max_redzone;364atomic_uint8_t alloc_dealloc_mismatch;365366// ------------------- Initialization ------------------------367explicit Allocator(LinkerInitialized)368: quarantine(LINKER_INITIALIZED),369fallback_quarantine_cache(LINKER_INITIALIZED) {}370371void CheckOptions(const AllocatorOptions &options) const {372CHECK_GE(options.min_redzone, 16);373CHECK_GE(options.max_redzone, options.min_redzone);374CHECK_LE(options.max_redzone, 2048);375CHECK(IsPowerOfTwo(options.min_redzone));376CHECK(IsPowerOfTwo(options.max_redzone));377}378379void SharedInitCode(const AllocatorOptions &options) {380CheckOptions(options);381quarantine.Init((uptr)options.quarantine_size_mb << 20,382(uptr)options.thread_local_quarantine_size_kb << 10);383atomic_store(&alloc_dealloc_mismatch, options.alloc_dealloc_mismatch,384memory_order_release);385atomic_store(&min_redzone, options.min_redzone, memory_order_release);386atomic_store(&max_redzone, options.max_redzone, memory_order_release);387}388389void InitLinkerInitialized(const AllocatorOptions &options) {390SetAllocatorMayReturnNull(options.may_return_null);391allocator.InitLinkerInitialized(options.release_to_os_interval_ms);392SharedInitCode(options);393max_user_defined_malloc_size = common_flags()->max_allocation_size_mb394? common_flags()->max_allocation_size_mb395<< 20396: kMaxAllowedMallocSize;397}398399void RePoisonChunk(uptr chunk) {400// This could be a user-facing chunk (with redzones), or some internal401// housekeeping chunk, like TransferBatch. Start by assuming the former.402AsanChunk *ac = GetAsanChunk((void *)chunk);403uptr allocated_size = allocator.GetActuallyAllocatedSize((void *)chunk);404if (ac && atomic_load(&ac->chunk_state, memory_order_acquire) ==405CHUNK_ALLOCATED) {406uptr beg = ac->Beg();407uptr end = ac->Beg() + ac->UsedSize();408uptr chunk_end = chunk + allocated_size;409if (chunk < beg && beg < end && end <= chunk_end) {410// Looks like a valid AsanChunk in use, poison redzones only.411PoisonShadow(chunk, beg - chunk, kAsanHeapLeftRedzoneMagic);412uptr end_aligned_down = RoundDownTo(end, ASAN_SHADOW_GRANULARITY);413FastPoisonShadowPartialRightRedzone(414end_aligned_down, end - end_aligned_down,415chunk_end - end_aligned_down, kAsanHeapLeftRedzoneMagic);416return;417}418}419420// This is either not an AsanChunk or freed or quarantined AsanChunk.421// In either case, poison everything.422PoisonShadow(chunk, allocated_size, kAsanHeapLeftRedzoneMagic);423}424425void ReInitialize(const AllocatorOptions &options) {426SetAllocatorMayReturnNull(options.may_return_null);427allocator.SetReleaseToOSIntervalMs(options.release_to_os_interval_ms);428SharedInitCode(options);429430// Poison all existing allocation's redzones.431if (CanPoisonMemory()) {432allocator.ForceLock();433allocator.ForEachChunk(434[](uptr chunk, void *alloc) {435((Allocator *)alloc)->RePoisonChunk(chunk);436},437this);438allocator.ForceUnlock();439}440}441442void GetOptions(AllocatorOptions *options) const {443options->quarantine_size_mb = quarantine.GetMaxSize() >> 20;444options->thread_local_quarantine_size_kb =445quarantine.GetMaxCacheSize() >> 10;446options->min_redzone = atomic_load(&min_redzone, memory_order_acquire);447options->max_redzone = atomic_load(&max_redzone, memory_order_acquire);448options->may_return_null = AllocatorMayReturnNull();449options->alloc_dealloc_mismatch =450atomic_load(&alloc_dealloc_mismatch, memory_order_acquire);451options->release_to_os_interval_ms = allocator.ReleaseToOSIntervalMs();452}453454// -------------------- Helper methods. -------------------------455uptr ComputeRZLog(uptr user_requested_size) {456u32 rz_log = user_requested_size <= 64 - 16 ? 0457: user_requested_size <= 128 - 32 ? 1458: user_requested_size <= 512 - 64 ? 2459: user_requested_size <= 4096 - 128 ? 3460: user_requested_size <= (1 << 14) - 256 ? 4461: user_requested_size <= (1 << 15) - 512 ? 5462: user_requested_size <= (1 << 16) - 1024 ? 6463: 7;464u32 hdr_log = RZSize2Log(RoundUpToPowerOfTwo(sizeof(ChunkHeader)));465u32 min_log = RZSize2Log(atomic_load(&min_redzone, memory_order_acquire));466u32 max_log = RZSize2Log(atomic_load(&max_redzone, memory_order_acquire));467return Min(Max(rz_log, Max(min_log, hdr_log)), Max(max_log, hdr_log));468}469470static uptr ComputeUserRequestedAlignmentLog(uptr user_requested_alignment) {471if (user_requested_alignment < 8)472return 0;473if (user_requested_alignment > 512)474user_requested_alignment = 512;475return Log2(user_requested_alignment) - 2;476}477478static uptr ComputeUserAlignment(uptr user_requested_alignment_log) {479if (user_requested_alignment_log == 0)480return 0;481return 1LL << (user_requested_alignment_log + 2);482}483484// We have an address between two chunks, and we want to report just one.485AsanChunk *ChooseChunk(uptr addr, AsanChunk *left_chunk,486AsanChunk *right_chunk) {487if (!left_chunk)488return right_chunk;489if (!right_chunk)490return left_chunk;491// Prefer an allocated chunk over freed chunk and freed chunk492// over available chunk.493u8 left_state = atomic_load(&left_chunk->chunk_state, memory_order_relaxed);494u8 right_state =495atomic_load(&right_chunk->chunk_state, memory_order_relaxed);496if (left_state != right_state) {497if (left_state == CHUNK_ALLOCATED)498return left_chunk;499if (right_state == CHUNK_ALLOCATED)500return right_chunk;501if (left_state == CHUNK_QUARANTINE)502return left_chunk;503if (right_state == CHUNK_QUARANTINE)504return right_chunk;505}506// Same chunk_state: choose based on offset.507sptr l_offset = 0, r_offset = 0;508CHECK(AsanChunkView(left_chunk).AddrIsAtRight(addr, 1, &l_offset));509CHECK(AsanChunkView(right_chunk).AddrIsAtLeft(addr, 1, &r_offset));510if (l_offset < r_offset)511return left_chunk;512return right_chunk;513}514515bool UpdateAllocationStack(uptr addr, BufferedStackTrace *stack) {516AsanChunk *m = GetAsanChunkByAddr(addr);517if (!m) return false;518if (atomic_load(&m->chunk_state, memory_order_acquire) != CHUNK_ALLOCATED)519return false;520if (m->Beg() != addr) return false;521AsanThread *t = GetCurrentThread();522m->SetAllocContext(t ? t->tid() : kMainTid, StackDepotPut(*stack));523return true;524}525526// -------------------- Allocation/Deallocation routines ---------------527void *Allocate(uptr size, uptr alignment, BufferedStackTrace *stack,528AllocType alloc_type, bool can_fill) {529if (UNLIKELY(!AsanInited()))530AsanInitFromRtl();531if (UNLIKELY(IsRssLimitExceeded())) {532if (AllocatorMayReturnNull())533return nullptr;534ReportRssLimitExceeded(stack);535}536Flags &fl = *flags();537CHECK(stack);538const uptr min_alignment = ASAN_SHADOW_GRANULARITY;539const uptr user_requested_alignment_log =540ComputeUserRequestedAlignmentLog(alignment);541if (alignment < min_alignment)542alignment = min_alignment;543if (size == 0) {544// We'd be happy to avoid allocating memory for zero-size requests, but545// some programs/tests depend on this behavior and assume that malloc546// would not return NULL even for zero-size allocations. Moreover, it547// looks like operator new should never return NULL, and results of548// consecutive "new" calls must be different even if the allocated size549// is zero.550size = 1;551}552CHECK(IsPowerOfTwo(alignment));553uptr rz_log = ComputeRZLog(size);554uptr rz_size = RZLog2Size(rz_log);555uptr rounded_size = RoundUpTo(Max(size, kChunkHeader2Size), alignment);556uptr needed_size = rounded_size + rz_size;557if (alignment > min_alignment)558needed_size += alignment;559bool from_primary = PrimaryAllocator::CanAllocate(needed_size, alignment);560// If we are allocating from the secondary allocator, there will be no561// automatic right redzone, so add the right redzone manually.562if (!from_primary)563needed_size += rz_size;564CHECK(IsAligned(needed_size, min_alignment));565if (size > kMaxAllowedMallocSize || needed_size > kMaxAllowedMallocSize ||566size > max_user_defined_malloc_size) {567if (AllocatorMayReturnNull()) {568Report("WARNING: AddressSanitizer failed to allocate 0x%zx bytes\n",569size);570return nullptr;571}572uptr malloc_limit =573Min(kMaxAllowedMallocSize, max_user_defined_malloc_size);574ReportAllocationSizeTooBig(size, needed_size, malloc_limit, stack);575}576577AsanThread *t = GetCurrentThread();578void *allocated;579if (t) {580AllocatorCache *cache = GetAllocatorCache(&t->malloc_storage());581allocated = allocator.Allocate(cache, needed_size, 8);582} else {583SpinMutexLock l(&fallback_mutex);584AllocatorCache *cache = &fallback_allocator_cache;585allocated = allocator.Allocate(cache, needed_size, 8);586}587if (UNLIKELY(!allocated)) {588SetAllocatorOutOfMemory();589if (AllocatorMayReturnNull())590return nullptr;591ReportOutOfMemory(size, stack);592}593594uptr alloc_beg = reinterpret_cast<uptr>(allocated);595uptr alloc_end = alloc_beg + needed_size;596uptr user_beg = alloc_beg + rz_size;597if (!IsAligned(user_beg, alignment))598user_beg = RoundUpTo(user_beg, alignment);599uptr user_end = user_beg + size;600CHECK_LE(user_end, alloc_end);601uptr chunk_beg = user_beg - kChunkHeaderSize;602AsanChunk *m = reinterpret_cast<AsanChunk *>(chunk_beg);603m->alloc_type = alloc_type;604CHECK(size);605m->SetUsedSize(size);606m->user_requested_alignment_log = user_requested_alignment_log;607608m->SetAllocContext(t ? t->tid() : kMainTid, StackDepotPut(*stack));609610if (!from_primary || *(u8 *)MEM_TO_SHADOW((uptr)allocated) == 0) {611// The allocator provides an unpoisoned chunk. This is possible for the612// secondary allocator, or if CanPoisonMemory() was false for some time,613// for example, due to flags()->start_disabled. Anyway, poison left and614// right of the block before using it for anything else.615uptr tail_beg = RoundUpTo(user_end, ASAN_SHADOW_GRANULARITY);616uptr tail_end = alloc_beg + allocator.GetActuallyAllocatedSize(allocated);617PoisonShadow(alloc_beg, user_beg - alloc_beg, kAsanHeapLeftRedzoneMagic);618PoisonShadow(tail_beg, tail_end - tail_beg, kAsanHeapLeftRedzoneMagic);619}620621uptr size_rounded_down_to_granularity =622RoundDownTo(size, ASAN_SHADOW_GRANULARITY);623// Unpoison the bulk of the memory region.624if (size_rounded_down_to_granularity)625PoisonShadow(user_beg, size_rounded_down_to_granularity, 0);626// Deal with the end of the region if size is not aligned to granularity.627if (size != size_rounded_down_to_granularity && CanPoisonMemory()) {628u8 *shadow =629(u8 *)MemToShadow(user_beg + size_rounded_down_to_granularity);630*shadow = fl.poison_partial ? (size & (ASAN_SHADOW_GRANULARITY - 1)) : 0;631}632633AsanStats &thread_stats = GetCurrentThreadStats();634thread_stats.mallocs++;635thread_stats.malloced += size;636thread_stats.malloced_redzones += needed_size - size;637if (needed_size > SizeClassMap::kMaxSize)638thread_stats.malloc_large++;639else640thread_stats.malloced_by_size[SizeClassMap::ClassID(needed_size)]++;641642void *res = reinterpret_cast<void *>(user_beg);643if (can_fill && fl.max_malloc_fill_size) {644uptr fill_size = Min(size, (uptr)fl.max_malloc_fill_size);645REAL(memset)(res, fl.malloc_fill_byte, fill_size);646}647#if CAN_SANITIZE_LEAKS648m->lsan_tag = __lsan::DisabledInThisThread() ? __lsan::kIgnored649: __lsan::kDirectlyLeaked;650#endif651// Must be the last mutation of metadata in this function.652atomic_store(&m->chunk_state, CHUNK_ALLOCATED, memory_order_release);653if (alloc_beg != chunk_beg) {654CHECK_LE(alloc_beg + sizeof(LargeChunkHeader), chunk_beg);655reinterpret_cast<LargeChunkHeader *>(alloc_beg)->Set(m);656}657RunMallocHooks(res, size);658return res;659}660661// Set quarantine flag if chunk is allocated, issue ASan error report on662// available and quarantined chunks. Return true on success, false otherwise.663bool AtomicallySetQuarantineFlagIfAllocated(AsanChunk *m, void *ptr,664BufferedStackTrace *stack) {665u8 old_chunk_state = CHUNK_ALLOCATED;666// Flip the chunk_state atomically to avoid race on double-free.667if (!atomic_compare_exchange_strong(&m->chunk_state, &old_chunk_state,668CHUNK_QUARANTINE,669memory_order_acquire)) {670ReportInvalidFree(ptr, old_chunk_state, stack);671// It's not safe to push a chunk in quarantine on invalid free.672return false;673}674CHECK_EQ(CHUNK_ALLOCATED, old_chunk_state);675// It was a user data.676m->SetFreeContext(kInvalidTid, 0);677return true;678}679680// Expects the chunk to already be marked as quarantined by using681// AtomicallySetQuarantineFlagIfAllocated.682void QuarantineChunk(AsanChunk *m, void *ptr, BufferedStackTrace *stack) {683CHECK_EQ(atomic_load(&m->chunk_state, memory_order_relaxed),684CHUNK_QUARANTINE);685AsanThread *t = GetCurrentThread();686m->SetFreeContext(t ? t->tid() : 0, StackDepotPut(*stack));687688// Push into quarantine.689if (t) {690AsanThreadLocalMallocStorage *ms = &t->malloc_storage();691AllocatorCache *ac = GetAllocatorCache(ms);692quarantine.Put(GetQuarantineCache(ms), QuarantineCallback(ac, stack), m,693m->UsedSize());694} else {695SpinMutexLock l(&fallback_mutex);696AllocatorCache *ac = &fallback_allocator_cache;697quarantine.Put(&fallback_quarantine_cache, QuarantineCallback(ac, stack),698m, m->UsedSize());699}700}701702void Deallocate(void *ptr, uptr delete_size, uptr delete_alignment,703BufferedStackTrace *stack, AllocType alloc_type) {704uptr p = reinterpret_cast<uptr>(ptr);705if (p == 0) return;706707uptr chunk_beg = p - kChunkHeaderSize;708AsanChunk *m = reinterpret_cast<AsanChunk *>(chunk_beg);709710// On Windows, uninstrumented DLLs may allocate memory before ASan hooks711// malloc. Don't report an invalid free in this case.712if (SANITIZER_WINDOWS &&713!get_allocator().PointerIsMine(ptr)) {714if (!IsSystemHeapAddress(p))715ReportFreeNotMalloced(p, stack);716return;717}718719if (RunFreeHooks(ptr)) {720// Someone used __sanitizer_ignore_free_hook() and decided that they721// didn't want the memory to __sanitizer_ignore_free_hook freed right now.722// When they call free() on this pointer again at a later time, we should723// ignore the alloc-type mismatch and allow them to deallocate the pointer724// through free(), rather than the initial alloc type.725m->alloc_type = FROM_MALLOC;726return;727}728729// Must mark the chunk as quarantined before any changes to its metadata.730// Do not quarantine given chunk if we failed to set CHUNK_QUARANTINE flag.731if (!AtomicallySetQuarantineFlagIfAllocated(m, ptr, stack)) return;732733if (m->alloc_type != alloc_type) {734if (atomic_load(&alloc_dealloc_mismatch, memory_order_acquire)) {735ReportAllocTypeMismatch((uptr)ptr, stack, (AllocType)m->alloc_type,736(AllocType)alloc_type);737}738} else {739if (flags()->new_delete_type_mismatch &&740(alloc_type == FROM_NEW || alloc_type == FROM_NEW_BR) &&741((delete_size && delete_size != m->UsedSize()) ||742ComputeUserRequestedAlignmentLog(delete_alignment) !=743m->user_requested_alignment_log)) {744ReportNewDeleteTypeMismatch(p, delete_size, delete_alignment, stack);745}746}747748AsanStats &thread_stats = GetCurrentThreadStats();749thread_stats.frees++;750thread_stats.freed += m->UsedSize();751752QuarantineChunk(m, ptr, stack);753}754755void *Reallocate(void *old_ptr, uptr new_size, BufferedStackTrace *stack) {756CHECK(old_ptr && new_size);757uptr p = reinterpret_cast<uptr>(old_ptr);758uptr chunk_beg = p - kChunkHeaderSize;759AsanChunk *m = reinterpret_cast<AsanChunk *>(chunk_beg);760761AsanStats &thread_stats = GetCurrentThreadStats();762thread_stats.reallocs++;763thread_stats.realloced += new_size;764765void *new_ptr = Allocate(new_size, 8, stack, FROM_MALLOC, true);766if (new_ptr) {767u8 chunk_state = atomic_load(&m->chunk_state, memory_order_acquire);768if (chunk_state != CHUNK_ALLOCATED)769ReportInvalidFree(old_ptr, chunk_state, stack);770CHECK_NE(REAL(memcpy), nullptr);771uptr memcpy_size = Min(new_size, m->UsedSize());772// If realloc() races with free(), we may start copying freed memory.773// However, we will report racy double-free later anyway.774REAL(memcpy)(new_ptr, old_ptr, memcpy_size);775Deallocate(old_ptr, 0, 0, stack, FROM_MALLOC);776}777return new_ptr;778}779780void *Calloc(uptr nmemb, uptr size, BufferedStackTrace *stack) {781if (UNLIKELY(CheckForCallocOverflow(size, nmemb))) {782if (AllocatorMayReturnNull())783return nullptr;784ReportCallocOverflow(nmemb, size, stack);785}786void *ptr = Allocate(nmemb * size, 8, stack, FROM_MALLOC, false);787// If the memory comes from the secondary allocator no need to clear it788// as it comes directly from mmap.789if (ptr && allocator.FromPrimary(ptr))790REAL(memset)(ptr, 0, nmemb * size);791return ptr;792}793794void ReportInvalidFree(void *ptr, u8 chunk_state, BufferedStackTrace *stack) {795if (chunk_state == CHUNK_QUARANTINE)796ReportDoubleFree((uptr)ptr, stack);797else798ReportFreeNotMalloced((uptr)ptr, stack);799}800801void CommitBack(AsanThreadLocalMallocStorage *ms, BufferedStackTrace *stack) {802AllocatorCache *ac = GetAllocatorCache(ms);803quarantine.Drain(GetQuarantineCache(ms), QuarantineCallback(ac, stack));804allocator.SwallowCache(ac);805}806807// -------------------------- Chunk lookup ----------------------808809// Assumes alloc_beg == allocator.GetBlockBegin(alloc_beg).810// Returns nullptr if AsanChunk is not yet initialized just after811// get_allocator().Allocate(), or is being destroyed just before812// get_allocator().Deallocate().813AsanChunk *GetAsanChunk(void *alloc_beg) {814if (!alloc_beg)815return nullptr;816AsanChunk *p = reinterpret_cast<LargeChunkHeader *>(alloc_beg)->Get();817if (!p) {818if (!allocator.FromPrimary(alloc_beg))819return nullptr;820p = reinterpret_cast<AsanChunk *>(alloc_beg);821}822u8 state = atomic_load(&p->chunk_state, memory_order_relaxed);823// It does not guaranty that Chunk is initialized, but it's824// definitely not for any other value.825if (state == CHUNK_ALLOCATED || state == CHUNK_QUARANTINE)826return p;827return nullptr;828}829830AsanChunk *GetAsanChunkByAddr(uptr p) {831void *alloc_beg = allocator.GetBlockBegin(reinterpret_cast<void *>(p));832return GetAsanChunk(alloc_beg);833}834835// Allocator must be locked when this function is called.836AsanChunk *GetAsanChunkByAddrFastLocked(uptr p) {837void *alloc_beg =838allocator.GetBlockBeginFastLocked(reinterpret_cast<void *>(p));839return GetAsanChunk(alloc_beg);840}841842uptr AllocationSize(uptr p) {843AsanChunk *m = GetAsanChunkByAddr(p);844if (!m) return 0;845if (atomic_load(&m->chunk_state, memory_order_acquire) != CHUNK_ALLOCATED)846return 0;847if (m->Beg() != p) return 0;848return m->UsedSize();849}850851uptr AllocationSizeFast(uptr p) {852return reinterpret_cast<AsanChunk *>(p - kChunkHeaderSize)->UsedSize();853}854855AsanChunkView FindHeapChunkByAddress(uptr addr) {856AsanChunk *m1 = GetAsanChunkByAddr(addr);857sptr offset = 0;858if (!m1 || AsanChunkView(m1).AddrIsAtLeft(addr, 1, &offset)) {859// The address is in the chunk's left redzone, so maybe it is actually860// a right buffer overflow from the other chunk before.861// Search a bit before to see if there is another chunk.862AsanChunk *m2 = nullptr;863for (uptr l = 1; l < GetPageSizeCached(); l++) {864m2 = GetAsanChunkByAddr(addr - l);865if (m2 == m1) continue; // Still the same chunk.866break;867}868if (m2 && AsanChunkView(m2).AddrIsAtRight(addr, 1, &offset))869m1 = ChooseChunk(addr, m2, m1);870}871return AsanChunkView(m1);872}873874void Purge(BufferedStackTrace *stack) {875AsanThread *t = GetCurrentThread();876if (t) {877AsanThreadLocalMallocStorage *ms = &t->malloc_storage();878quarantine.DrainAndRecycle(GetQuarantineCache(ms),879QuarantineCallback(GetAllocatorCache(ms),880stack));881}882{883SpinMutexLock l(&fallback_mutex);884quarantine.DrainAndRecycle(&fallback_quarantine_cache,885QuarantineCallback(&fallback_allocator_cache,886stack));887}888889allocator.ForceReleaseToOS();890}891892void PrintStats() {893allocator.PrintStats();894quarantine.PrintStats();895}896897void ForceLock() SANITIZER_ACQUIRE(fallback_mutex) {898allocator.ForceLock();899fallback_mutex.Lock();900}901902void ForceUnlock() SANITIZER_RELEASE(fallback_mutex) {903fallback_mutex.Unlock();904allocator.ForceUnlock();905}906};907908static Allocator instance(LINKER_INITIALIZED);909910static AsanAllocator &get_allocator() {911return instance.allocator;912}913914bool AsanChunkView::IsValid() const {915return chunk_ && atomic_load(&chunk_->chunk_state, memory_order_relaxed) !=916CHUNK_INVALID;917}918bool AsanChunkView::IsAllocated() const {919return chunk_ && atomic_load(&chunk_->chunk_state, memory_order_relaxed) ==920CHUNK_ALLOCATED;921}922bool AsanChunkView::IsQuarantined() const {923return chunk_ && atomic_load(&chunk_->chunk_state, memory_order_relaxed) ==924CHUNK_QUARANTINE;925}926uptr AsanChunkView::Beg() const { return chunk_->Beg(); }927uptr AsanChunkView::End() const { return Beg() + UsedSize(); }928uptr AsanChunkView::UsedSize() const { return chunk_->UsedSize(); }929u32 AsanChunkView::UserRequestedAlignment() const {930return Allocator::ComputeUserAlignment(chunk_->user_requested_alignment_log);931}932933uptr AsanChunkView::AllocTid() const {934u32 tid = 0;935u32 stack = 0;936chunk_->GetAllocContext(tid, stack);937return tid;938}939940uptr AsanChunkView::FreeTid() const {941if (!IsQuarantined())942return kInvalidTid;943u32 tid = 0;944u32 stack = 0;945chunk_->GetFreeContext(tid, stack);946return tid;947}948949AllocType AsanChunkView::GetAllocType() const {950return (AllocType)chunk_->alloc_type;951}952953u32 AsanChunkView::GetAllocStackId() const {954u32 tid = 0;955u32 stack = 0;956chunk_->GetAllocContext(tid, stack);957return stack;958}959960u32 AsanChunkView::GetFreeStackId() const {961if (!IsQuarantined())962return 0;963u32 tid = 0;964u32 stack = 0;965chunk_->GetFreeContext(tid, stack);966return stack;967}968969void InitializeAllocator(const AllocatorOptions &options) {970instance.InitLinkerInitialized(options);971}972973void ReInitializeAllocator(const AllocatorOptions &options) {974instance.ReInitialize(options);975}976977void GetAllocatorOptions(AllocatorOptions *options) {978instance.GetOptions(options);979}980981AsanChunkView FindHeapChunkByAddress(uptr addr) {982return instance.FindHeapChunkByAddress(addr);983}984AsanChunkView FindHeapChunkByAllocBeg(uptr addr) {985return AsanChunkView(instance.GetAsanChunk(reinterpret_cast<void*>(addr)));986}987988void AsanThreadLocalMallocStorage::CommitBack() {989GET_STACK_TRACE_MALLOC;990instance.CommitBack(this, &stack);991}992993void PrintInternalAllocatorStats() {994instance.PrintStats();995}996997void asan_free(void *ptr, BufferedStackTrace *stack, AllocType alloc_type) {998instance.Deallocate(ptr, 0, 0, stack, alloc_type);999}10001001void asan_delete(void *ptr, uptr size, uptr alignment,1002BufferedStackTrace *stack, AllocType alloc_type) {1003instance.Deallocate(ptr, size, alignment, stack, alloc_type);1004}10051006void *asan_malloc(uptr size, BufferedStackTrace *stack) {1007return SetErrnoOnNull(instance.Allocate(size, 8, stack, FROM_MALLOC, true));1008}10091010void *asan_calloc(uptr nmemb, uptr size, BufferedStackTrace *stack) {1011return SetErrnoOnNull(instance.Calloc(nmemb, size, stack));1012}10131014void *asan_reallocarray(void *p, uptr nmemb, uptr size,1015BufferedStackTrace *stack) {1016if (UNLIKELY(CheckForCallocOverflow(size, nmemb))) {1017errno = errno_ENOMEM;1018if (AllocatorMayReturnNull())1019return nullptr;1020ReportReallocArrayOverflow(nmemb, size, stack);1021}1022return asan_realloc(p, nmemb * size, stack);1023}10241025void *asan_realloc(void *p, uptr size, BufferedStackTrace *stack) {1026if (!p)1027return SetErrnoOnNull(instance.Allocate(size, 8, stack, FROM_MALLOC, true));1028if (size == 0) {1029if (flags()->allocator_frees_and_returns_null_on_realloc_zero) {1030instance.Deallocate(p, 0, 0, stack, FROM_MALLOC);1031return nullptr;1032}1033// Allocate a size of 1 if we shouldn't free() on Realloc to 01034size = 1;1035}1036return SetErrnoOnNull(instance.Reallocate(p, size, stack));1037}10381039void *asan_valloc(uptr size, BufferedStackTrace *stack) {1040return SetErrnoOnNull(1041instance.Allocate(size, GetPageSizeCached(), stack, FROM_MALLOC, true));1042}10431044void *asan_pvalloc(uptr size, BufferedStackTrace *stack) {1045uptr PageSize = GetPageSizeCached();1046if (UNLIKELY(CheckForPvallocOverflow(size, PageSize))) {1047errno = errno_ENOMEM;1048if (AllocatorMayReturnNull())1049return nullptr;1050ReportPvallocOverflow(size, stack);1051}1052// pvalloc(0) should allocate one page.1053size = size ? RoundUpTo(size, PageSize) : PageSize;1054return SetErrnoOnNull(1055instance.Allocate(size, PageSize, stack, FROM_MALLOC, true));1056}10571058void *asan_memalign(uptr alignment, uptr size, BufferedStackTrace *stack,1059AllocType alloc_type) {1060if (UNLIKELY(!IsPowerOfTwo(alignment))) {1061errno = errno_EINVAL;1062if (AllocatorMayReturnNull())1063return nullptr;1064ReportInvalidAllocationAlignment(alignment, stack);1065}1066return SetErrnoOnNull(1067instance.Allocate(size, alignment, stack, alloc_type, true));1068}10691070void *asan_aligned_alloc(uptr alignment, uptr size, BufferedStackTrace *stack) {1071if (UNLIKELY(!CheckAlignedAllocAlignmentAndSize(alignment, size))) {1072errno = errno_EINVAL;1073if (AllocatorMayReturnNull())1074return nullptr;1075ReportInvalidAlignedAllocAlignment(size, alignment, stack);1076}1077return SetErrnoOnNull(1078instance.Allocate(size, alignment, stack, FROM_MALLOC, true));1079}10801081int asan_posix_memalign(void **memptr, uptr alignment, uptr size,1082BufferedStackTrace *stack) {1083if (UNLIKELY(!CheckPosixMemalignAlignment(alignment))) {1084if (AllocatorMayReturnNull())1085return errno_EINVAL;1086ReportInvalidPosixMemalignAlignment(alignment, stack);1087}1088void *ptr = instance.Allocate(size, alignment, stack, FROM_MALLOC, true);1089if (UNLIKELY(!ptr))1090// OOM error is already taken care of by Allocate.1091return errno_ENOMEM;1092CHECK(IsAligned((uptr)ptr, alignment));1093*memptr = ptr;1094return 0;1095}10961097uptr asan_malloc_usable_size(const void *ptr, uptr pc, uptr bp) {1098if (!ptr) return 0;1099uptr usable_size = instance.AllocationSize(reinterpret_cast<uptr>(ptr));1100if (flags()->check_malloc_usable_size && (usable_size == 0)) {1101GET_STACK_TRACE_FATAL(pc, bp);1102ReportMallocUsableSizeNotOwned((uptr)ptr, &stack);1103}1104return usable_size;1105}11061107uptr asan_mz_size(const void *ptr) {1108return instance.AllocationSize(reinterpret_cast<uptr>(ptr));1109}11101111void asan_mz_force_lock() SANITIZER_NO_THREAD_SAFETY_ANALYSIS {1112instance.ForceLock();1113}11141115void asan_mz_force_unlock() SANITIZER_NO_THREAD_SAFETY_ANALYSIS {1116instance.ForceUnlock();1117}11181119} // namespace __asan11201121// --- Implementation of LSan-specific functions --- {{{11122namespace __lsan {1123void LockAllocator() {1124__asan::get_allocator().ForceLock();1125}11261127void UnlockAllocator() {1128__asan::get_allocator().ForceUnlock();1129}11301131void GetAllocatorGlobalRange(uptr *begin, uptr *end) {1132*begin = (uptr)&__asan::get_allocator();1133*end = *begin + sizeof(__asan::get_allocator());1134}11351136uptr PointsIntoChunk(void *p) {1137uptr addr = reinterpret_cast<uptr>(p);1138__asan::AsanChunk *m = __asan::instance.GetAsanChunkByAddrFastLocked(addr);1139if (!m || atomic_load(&m->chunk_state, memory_order_acquire) !=1140__asan::CHUNK_ALLOCATED)1141return 0;1142uptr chunk = m->Beg();1143if (m->AddrIsInside(addr))1144return chunk;1145if (IsSpecialCaseOfOperatorNew0(chunk, m->UsedSize(), addr))1146return chunk;1147return 0;1148}11491150uptr GetUserBegin(uptr chunk) {1151// FIXME: All usecases provide chunk address, GetAsanChunkByAddrFastLocked is1152// not needed.1153__asan::AsanChunk *m = __asan::instance.GetAsanChunkByAddrFastLocked(chunk);1154return m ? m->Beg() : 0;1155}11561157uptr GetUserAddr(uptr chunk) {1158return chunk;1159}11601161LsanMetadata::LsanMetadata(uptr chunk) {1162metadata_ = chunk ? reinterpret_cast<void *>(chunk - __asan::kChunkHeaderSize)1163: nullptr;1164}11651166bool LsanMetadata::allocated() const {1167if (!metadata_)1168return false;1169__asan::AsanChunk *m = reinterpret_cast<__asan::AsanChunk *>(metadata_);1170return atomic_load(&m->chunk_state, memory_order_relaxed) ==1171__asan::CHUNK_ALLOCATED;1172}11731174ChunkTag LsanMetadata::tag() const {1175__asan::AsanChunk *m = reinterpret_cast<__asan::AsanChunk *>(metadata_);1176return static_cast<ChunkTag>(m->lsan_tag);1177}11781179void LsanMetadata::set_tag(ChunkTag value) {1180__asan::AsanChunk *m = reinterpret_cast<__asan::AsanChunk *>(metadata_);1181m->lsan_tag = value;1182}11831184uptr LsanMetadata::requested_size() const {1185__asan::AsanChunk *m = reinterpret_cast<__asan::AsanChunk *>(metadata_);1186return m->UsedSize();1187}11881189u32 LsanMetadata::stack_trace_id() const {1190__asan::AsanChunk *m = reinterpret_cast<__asan::AsanChunk *>(metadata_);1191u32 tid = 0;1192u32 stack = 0;1193m->GetAllocContext(tid, stack);1194return stack;1195}11961197void ForEachChunk(ForEachChunkCallback callback, void *arg) {1198__asan::get_allocator().ForEachChunk(callback, arg);1199}12001201IgnoreObjectResult IgnoreObject(const void *p) {1202uptr addr = reinterpret_cast<uptr>(p);1203__asan::AsanChunk *m = __asan::instance.GetAsanChunkByAddr(addr);1204if (!m ||1205(atomic_load(&m->chunk_state, memory_order_acquire) !=1206__asan::CHUNK_ALLOCATED) ||1207!m->AddrIsInside(addr)) {1208return kIgnoreObjectInvalid;1209}1210if (m->lsan_tag == kIgnored)1211return kIgnoreObjectAlreadyIgnored;1212m->lsan_tag = __lsan::kIgnored;1213return kIgnoreObjectSuccess;1214}12151216} // namespace __lsan12171218// ---------------------- Interface ---------------- {{{11219using namespace __asan;12201221static const void *AllocationBegin(const void *p) {1222AsanChunk *m = __asan::instance.GetAsanChunkByAddr((uptr)p);1223if (!m)1224return nullptr;1225if (atomic_load(&m->chunk_state, memory_order_acquire) != CHUNK_ALLOCATED)1226return nullptr;1227if (m->UsedSize() == 0)1228return nullptr;1229return (const void *)(m->Beg());1230}12311232// ASan allocator doesn't reserve extra bytes, so normally we would1233// just return "size". We don't want to expose our redzone sizes, etc here.1234uptr __sanitizer_get_estimated_allocated_size(uptr size) {1235return size;1236}12371238int __sanitizer_get_ownership(const void *p) {1239uptr ptr = reinterpret_cast<uptr>(p);1240return instance.AllocationSize(ptr) > 0;1241}12421243uptr __sanitizer_get_allocated_size(const void *p) {1244if (!p) return 0;1245uptr ptr = reinterpret_cast<uptr>(p);1246uptr allocated_size = instance.AllocationSize(ptr);1247// Die if p is not malloced or if it is already freed.1248if (allocated_size == 0) {1249GET_STACK_TRACE_FATAL_HERE;1250ReportSanitizerGetAllocatedSizeNotOwned(ptr, &stack);1251}1252return allocated_size;1253}12541255uptr __sanitizer_get_allocated_size_fast(const void *p) {1256DCHECK_EQ(p, __sanitizer_get_allocated_begin(p));1257uptr ret = instance.AllocationSizeFast(reinterpret_cast<uptr>(p));1258DCHECK_EQ(ret, __sanitizer_get_allocated_size(p));1259return ret;1260}12611262const void *__sanitizer_get_allocated_begin(const void *p) {1263return AllocationBegin(p);1264}12651266void __sanitizer_purge_allocator() {1267GET_STACK_TRACE_MALLOC;1268instance.Purge(&stack);1269}12701271int __asan_update_allocation_context(void* addr) {1272GET_STACK_TRACE_MALLOC;1273return instance.UpdateAllocationStack((uptr)addr, &stack);1274}127512761277