168d75effSDimitry Andric //===-- asan_allocator.cpp ------------------------------------------------===// 268d75effSDimitry Andric // 368d75effSDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 468d75effSDimitry Andric // See https://llvm.org/LICENSE.txt for license information. 568d75effSDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 668d75effSDimitry Andric // 768d75effSDimitry Andric //===----------------------------------------------------------------------===// 868d75effSDimitry Andric // 968d75effSDimitry Andric // This file is a part of AddressSanitizer, an address sanity checker. 1068d75effSDimitry Andric // 1168d75effSDimitry Andric // Implementation of ASan's memory allocator, 2-nd version. 1268d75effSDimitry Andric // This variant uses the allocator from sanitizer_common, i.e. the one shared 1368d75effSDimitry Andric // with ThreadSanitizer and MemorySanitizer. 1468d75effSDimitry Andric // 1568d75effSDimitry Andric //===----------------------------------------------------------------------===// 1668d75effSDimitry Andric 1768d75effSDimitry Andric #include "asan_allocator.h" 1868d75effSDimitry Andric #include "asan_mapping.h" 1968d75effSDimitry Andric #include "asan_poisoning.h" 2068d75effSDimitry Andric #include "asan_report.h" 2168d75effSDimitry Andric #include "asan_stack.h" 2268d75effSDimitry Andric #include "asan_thread.h" 2368d75effSDimitry Andric #include "sanitizer_common/sanitizer_allocator_checks.h" 2468d75effSDimitry Andric #include "sanitizer_common/sanitizer_allocator_interface.h" 2568d75effSDimitry Andric #include "sanitizer_common/sanitizer_errno.h" 2668d75effSDimitry Andric #include "sanitizer_common/sanitizer_flags.h" 2768d75effSDimitry Andric #include "sanitizer_common/sanitizer_internal_defs.h" 2868d75effSDimitry Andric #include "sanitizer_common/sanitizer_list.h" 2968d75effSDimitry Andric #include "sanitizer_common/sanitizer_stackdepot.h" 3068d75effSDimitry Andric #include "sanitizer_common/sanitizer_quarantine.h" 3168d75effSDimitry Andric #include "lsan/lsan_common.h" 3268d75effSDimitry Andric 3368d75effSDimitry Andric namespace __asan { 3468d75effSDimitry Andric 3568d75effSDimitry Andric // Valid redzone sizes are 16, 32, 64, ... 2048, so we encode them in 3 bits. 3668d75effSDimitry Andric // We use adaptive redzones: for larger allocation larger redzones are used. 3768d75effSDimitry Andric static u32 RZLog2Size(u32 rz_log) { 3868d75effSDimitry Andric CHECK_LT(rz_log, 8); 3968d75effSDimitry Andric return 16 << rz_log; 4068d75effSDimitry Andric } 4168d75effSDimitry Andric 4268d75effSDimitry Andric static u32 RZSize2Log(u32 rz_size) { 4368d75effSDimitry Andric CHECK_GE(rz_size, 16); 4468d75effSDimitry Andric CHECK_LE(rz_size, 2048); 4568d75effSDimitry Andric CHECK(IsPowerOfTwo(rz_size)); 4668d75effSDimitry Andric u32 res = Log2(rz_size) - 4; 4768d75effSDimitry Andric CHECK_EQ(rz_size, RZLog2Size(res)); 4868d75effSDimitry Andric return res; 4968d75effSDimitry Andric } 5068d75effSDimitry Andric 5168d75effSDimitry Andric static AsanAllocator &get_allocator(); 5268d75effSDimitry Andric 5368d75effSDimitry Andric // The memory chunk allocated from the underlying allocator looks like this: 5468d75effSDimitry Andric // L L L L L L H H U U U U U U R R 5568d75effSDimitry Andric // L -- left redzone words (0 or more bytes) 5668d75effSDimitry Andric // H -- ChunkHeader (16 bytes), which is also a part of the left redzone. 5768d75effSDimitry Andric // U -- user memory. 5868d75effSDimitry Andric // R -- right redzone (0 or more bytes) 5968d75effSDimitry Andric // ChunkBase consists of ChunkHeader and other bytes that overlap with user 6068d75effSDimitry Andric // memory. 6168d75effSDimitry Andric 6268d75effSDimitry Andric // If the left redzone is greater than the ChunkHeader size we store a magic 6368d75effSDimitry Andric // value in the first uptr word of the memory block and store the address of 6468d75effSDimitry Andric // ChunkBase in the next uptr. 6568d75effSDimitry Andric // M B L L L L L L L L L H H U U U U U U 6668d75effSDimitry Andric // | ^ 6768d75effSDimitry Andric // ---------------------| 6868d75effSDimitry Andric // M -- magic value kAllocBegMagic 6968d75effSDimitry Andric // B -- address of ChunkHeader pointing to the first 'H' 7068d75effSDimitry Andric static const uptr kAllocBegMagic = 0xCC6E96B9; 7168d75effSDimitry Andric 7268d75effSDimitry Andric struct ChunkHeader { 7368d75effSDimitry Andric // 1-st 8 bytes. 7468d75effSDimitry Andric u32 chunk_state : 8; // Must be first. 7568d75effSDimitry Andric u32 alloc_tid : 24; 7668d75effSDimitry Andric 7768d75effSDimitry Andric u32 free_tid : 24; 7868d75effSDimitry Andric u32 from_memalign : 1; 7968d75effSDimitry Andric u32 alloc_type : 2; 8068d75effSDimitry Andric u32 rz_log : 3; 8168d75effSDimitry Andric u32 lsan_tag : 2; 8268d75effSDimitry Andric // 2-nd 8 bytes 8368d75effSDimitry Andric // This field is used for small sizes. For large sizes it is equal to 8468d75effSDimitry Andric // SizeClassMap::kMaxSize and the actual size is stored in the 8568d75effSDimitry Andric // SecondaryAllocator's metadata. 8668d75effSDimitry Andric u32 user_requested_size : 29; 8768d75effSDimitry Andric // align < 8 -> 0 8868d75effSDimitry Andric // else -> log2(min(align, 512)) - 2 8968d75effSDimitry Andric u32 user_requested_alignment_log : 3; 9068d75effSDimitry Andric u32 alloc_context_id; 9168d75effSDimitry Andric }; 9268d75effSDimitry Andric 9368d75effSDimitry Andric struct ChunkBase : ChunkHeader { 9468d75effSDimitry Andric // Header2, intersects with user memory. 9568d75effSDimitry Andric u32 free_context_id; 9668d75effSDimitry Andric }; 9768d75effSDimitry Andric 9868d75effSDimitry Andric static const uptr kChunkHeaderSize = sizeof(ChunkHeader); 9968d75effSDimitry Andric static const uptr kChunkHeader2Size = sizeof(ChunkBase) - kChunkHeaderSize; 10068d75effSDimitry Andric COMPILER_CHECK(kChunkHeaderSize == 16); 10168d75effSDimitry Andric COMPILER_CHECK(kChunkHeader2Size <= 16); 10268d75effSDimitry Andric 10368d75effSDimitry Andric // Every chunk of memory allocated by this allocator can be in one of 3 states: 10468d75effSDimitry Andric // CHUNK_AVAILABLE: the chunk is in the free list and ready to be allocated. 10568d75effSDimitry Andric // CHUNK_ALLOCATED: the chunk is allocated and not yet freed. 10668d75effSDimitry Andric // CHUNK_QUARANTINE: the chunk was freed and put into quarantine zone. 10768d75effSDimitry Andric enum { 10868d75effSDimitry Andric CHUNK_AVAILABLE = 0, // 0 is the default value even if we didn't set it. 10968d75effSDimitry Andric CHUNK_ALLOCATED = 2, 11068d75effSDimitry Andric CHUNK_QUARANTINE = 3 11168d75effSDimitry Andric }; 11268d75effSDimitry Andric 11368d75effSDimitry Andric struct AsanChunk: ChunkBase { 11468d75effSDimitry Andric uptr Beg() { return reinterpret_cast<uptr>(this) + kChunkHeaderSize; } 11568d75effSDimitry Andric uptr UsedSize(bool locked_version = false) { 11668d75effSDimitry Andric if (user_requested_size != SizeClassMap::kMaxSize) 11768d75effSDimitry Andric return user_requested_size; 11868d75effSDimitry Andric return *reinterpret_cast<uptr *>( 11968d75effSDimitry Andric get_allocator().GetMetaData(AllocBeg(locked_version))); 12068d75effSDimitry Andric } 12168d75effSDimitry Andric void *AllocBeg(bool locked_version = false) { 12268d75effSDimitry Andric if (from_memalign) { 12368d75effSDimitry Andric if (locked_version) 12468d75effSDimitry Andric return get_allocator().GetBlockBeginFastLocked( 12568d75effSDimitry Andric reinterpret_cast<void *>(this)); 12668d75effSDimitry Andric return get_allocator().GetBlockBegin(reinterpret_cast<void *>(this)); 12768d75effSDimitry Andric } 12868d75effSDimitry Andric return reinterpret_cast<void*>(Beg() - RZLog2Size(rz_log)); 12968d75effSDimitry Andric } 13068d75effSDimitry Andric bool AddrIsInside(uptr addr, bool locked_version = false) { 13168d75effSDimitry Andric return (addr >= Beg()) && (addr < Beg() + UsedSize(locked_version)); 13268d75effSDimitry Andric } 13368d75effSDimitry Andric }; 13468d75effSDimitry Andric 13568d75effSDimitry Andric struct QuarantineCallback { 13668d75effSDimitry Andric QuarantineCallback(AllocatorCache *cache, BufferedStackTrace *stack) 13768d75effSDimitry Andric : cache_(cache), 13868d75effSDimitry Andric stack_(stack) { 13968d75effSDimitry Andric } 14068d75effSDimitry Andric 14168d75effSDimitry Andric void Recycle(AsanChunk *m) { 14268d75effSDimitry Andric CHECK_EQ(m->chunk_state, CHUNK_QUARANTINE); 14368d75effSDimitry Andric atomic_store((atomic_uint8_t*)m, CHUNK_AVAILABLE, memory_order_relaxed); 14468d75effSDimitry Andric CHECK_NE(m->alloc_tid, kInvalidTid); 14568d75effSDimitry Andric CHECK_NE(m->free_tid, kInvalidTid); 14668d75effSDimitry Andric PoisonShadow(m->Beg(), 14768d75effSDimitry Andric RoundUpTo(m->UsedSize(), SHADOW_GRANULARITY), 14868d75effSDimitry Andric kAsanHeapLeftRedzoneMagic); 14968d75effSDimitry Andric void *p = reinterpret_cast<void *>(m->AllocBeg()); 15068d75effSDimitry Andric if (p != m) { 15168d75effSDimitry Andric uptr *alloc_magic = reinterpret_cast<uptr *>(p); 15268d75effSDimitry Andric CHECK_EQ(alloc_magic[0], kAllocBegMagic); 15368d75effSDimitry Andric // Clear the magic value, as allocator internals may overwrite the 15468d75effSDimitry Andric // contents of deallocated chunk, confusing GetAsanChunk lookup. 15568d75effSDimitry Andric alloc_magic[0] = 0; 15668d75effSDimitry Andric CHECK_EQ(alloc_magic[1], reinterpret_cast<uptr>(m)); 15768d75effSDimitry Andric } 15868d75effSDimitry Andric 15968d75effSDimitry Andric // Statistics. 16068d75effSDimitry Andric AsanStats &thread_stats = GetCurrentThreadStats(); 16168d75effSDimitry Andric thread_stats.real_frees++; 16268d75effSDimitry Andric thread_stats.really_freed += m->UsedSize(); 16368d75effSDimitry Andric 16468d75effSDimitry Andric get_allocator().Deallocate(cache_, p); 16568d75effSDimitry Andric } 16668d75effSDimitry Andric 16768d75effSDimitry Andric void *Allocate(uptr size) { 16868d75effSDimitry Andric void *res = get_allocator().Allocate(cache_, size, 1); 16968d75effSDimitry Andric // TODO(alekseys): Consider making quarantine OOM-friendly. 17068d75effSDimitry Andric if (UNLIKELY(!res)) 17168d75effSDimitry Andric ReportOutOfMemory(size, stack_); 17268d75effSDimitry Andric return res; 17368d75effSDimitry Andric } 17468d75effSDimitry Andric 17568d75effSDimitry Andric void Deallocate(void *p) { 17668d75effSDimitry Andric get_allocator().Deallocate(cache_, p); 17768d75effSDimitry Andric } 17868d75effSDimitry Andric 17968d75effSDimitry Andric private: 18068d75effSDimitry Andric AllocatorCache* const cache_; 18168d75effSDimitry Andric BufferedStackTrace* const stack_; 18268d75effSDimitry Andric }; 18368d75effSDimitry Andric 18468d75effSDimitry Andric typedef Quarantine<QuarantineCallback, AsanChunk> AsanQuarantine; 18568d75effSDimitry Andric typedef AsanQuarantine::Cache QuarantineCache; 18668d75effSDimitry Andric 18768d75effSDimitry Andric void AsanMapUnmapCallback::OnMap(uptr p, uptr size) const { 18868d75effSDimitry Andric PoisonShadow(p, size, kAsanHeapLeftRedzoneMagic); 18968d75effSDimitry Andric // Statistics. 19068d75effSDimitry Andric AsanStats &thread_stats = GetCurrentThreadStats(); 19168d75effSDimitry Andric thread_stats.mmaps++; 19268d75effSDimitry Andric thread_stats.mmaped += size; 19368d75effSDimitry Andric } 19468d75effSDimitry Andric void AsanMapUnmapCallback::OnUnmap(uptr p, uptr size) const { 19568d75effSDimitry Andric PoisonShadow(p, size, 0); 19668d75effSDimitry Andric // We are about to unmap a chunk of user memory. 19768d75effSDimitry Andric // Mark the corresponding shadow memory as not needed. 19868d75effSDimitry Andric FlushUnneededASanShadowMemory(p, size); 19968d75effSDimitry Andric // Statistics. 20068d75effSDimitry Andric AsanStats &thread_stats = GetCurrentThreadStats(); 20168d75effSDimitry Andric thread_stats.munmaps++; 20268d75effSDimitry Andric thread_stats.munmaped += size; 20368d75effSDimitry Andric } 20468d75effSDimitry Andric 20568d75effSDimitry Andric // We can not use THREADLOCAL because it is not supported on some of the 20668d75effSDimitry Andric // platforms we care about (OSX 10.6, Android). 20768d75effSDimitry Andric // static THREADLOCAL AllocatorCache cache; 20868d75effSDimitry Andric AllocatorCache *GetAllocatorCache(AsanThreadLocalMallocStorage *ms) { 20968d75effSDimitry Andric CHECK(ms); 21068d75effSDimitry Andric return &ms->allocator_cache; 21168d75effSDimitry Andric } 21268d75effSDimitry Andric 21368d75effSDimitry Andric QuarantineCache *GetQuarantineCache(AsanThreadLocalMallocStorage *ms) { 21468d75effSDimitry Andric CHECK(ms); 21568d75effSDimitry Andric CHECK_LE(sizeof(QuarantineCache), sizeof(ms->quarantine_cache)); 21668d75effSDimitry Andric return reinterpret_cast<QuarantineCache *>(ms->quarantine_cache); 21768d75effSDimitry Andric } 21868d75effSDimitry Andric 21968d75effSDimitry Andric void AllocatorOptions::SetFrom(const Flags *f, const CommonFlags *cf) { 22068d75effSDimitry Andric quarantine_size_mb = f->quarantine_size_mb; 22168d75effSDimitry Andric thread_local_quarantine_size_kb = f->thread_local_quarantine_size_kb; 22268d75effSDimitry Andric min_redzone = f->redzone; 22368d75effSDimitry Andric max_redzone = f->max_redzone; 22468d75effSDimitry Andric may_return_null = cf->allocator_may_return_null; 22568d75effSDimitry Andric alloc_dealloc_mismatch = f->alloc_dealloc_mismatch; 22668d75effSDimitry Andric release_to_os_interval_ms = cf->allocator_release_to_os_interval_ms; 22768d75effSDimitry Andric } 22868d75effSDimitry Andric 22968d75effSDimitry Andric void AllocatorOptions::CopyTo(Flags *f, CommonFlags *cf) { 23068d75effSDimitry Andric f->quarantine_size_mb = quarantine_size_mb; 23168d75effSDimitry Andric f->thread_local_quarantine_size_kb = thread_local_quarantine_size_kb; 23268d75effSDimitry Andric f->redzone = min_redzone; 23368d75effSDimitry Andric f->max_redzone = max_redzone; 23468d75effSDimitry Andric cf->allocator_may_return_null = may_return_null; 23568d75effSDimitry Andric f->alloc_dealloc_mismatch = alloc_dealloc_mismatch; 23668d75effSDimitry Andric cf->allocator_release_to_os_interval_ms = release_to_os_interval_ms; 23768d75effSDimitry Andric } 23868d75effSDimitry Andric 23968d75effSDimitry Andric struct Allocator { 24068d75effSDimitry Andric static const uptr kMaxAllowedMallocSize = 24168d75effSDimitry Andric FIRST_32_SECOND_64(3UL << 30, 1ULL << 40); 24268d75effSDimitry Andric 24368d75effSDimitry Andric AsanAllocator allocator; 24468d75effSDimitry Andric AsanQuarantine quarantine; 24568d75effSDimitry Andric StaticSpinMutex fallback_mutex; 24668d75effSDimitry Andric AllocatorCache fallback_allocator_cache; 24768d75effSDimitry Andric QuarantineCache fallback_quarantine_cache; 24868d75effSDimitry Andric 249*480093f4SDimitry Andric uptr max_user_defined_malloc_size; 25068d75effSDimitry Andric atomic_uint8_t rss_limit_exceeded; 25168d75effSDimitry Andric 25268d75effSDimitry Andric // ------------------- Options -------------------------- 25368d75effSDimitry Andric atomic_uint16_t min_redzone; 25468d75effSDimitry Andric atomic_uint16_t max_redzone; 25568d75effSDimitry Andric atomic_uint8_t alloc_dealloc_mismatch; 25668d75effSDimitry Andric 25768d75effSDimitry Andric // ------------------- Initialization ------------------------ 25868d75effSDimitry Andric explicit Allocator(LinkerInitialized) 25968d75effSDimitry Andric : quarantine(LINKER_INITIALIZED), 26068d75effSDimitry Andric fallback_quarantine_cache(LINKER_INITIALIZED) {} 26168d75effSDimitry Andric 26268d75effSDimitry Andric void CheckOptions(const AllocatorOptions &options) const { 26368d75effSDimitry Andric CHECK_GE(options.min_redzone, 16); 26468d75effSDimitry Andric CHECK_GE(options.max_redzone, options.min_redzone); 26568d75effSDimitry Andric CHECK_LE(options.max_redzone, 2048); 26668d75effSDimitry Andric CHECK(IsPowerOfTwo(options.min_redzone)); 26768d75effSDimitry Andric CHECK(IsPowerOfTwo(options.max_redzone)); 26868d75effSDimitry Andric } 26968d75effSDimitry Andric 27068d75effSDimitry Andric void SharedInitCode(const AllocatorOptions &options) { 27168d75effSDimitry Andric CheckOptions(options); 27268d75effSDimitry Andric quarantine.Init((uptr)options.quarantine_size_mb << 20, 27368d75effSDimitry Andric (uptr)options.thread_local_quarantine_size_kb << 10); 27468d75effSDimitry Andric atomic_store(&alloc_dealloc_mismatch, options.alloc_dealloc_mismatch, 27568d75effSDimitry Andric memory_order_release); 27668d75effSDimitry Andric atomic_store(&min_redzone, options.min_redzone, memory_order_release); 27768d75effSDimitry Andric atomic_store(&max_redzone, options.max_redzone, memory_order_release); 27868d75effSDimitry Andric } 27968d75effSDimitry Andric 28068d75effSDimitry Andric void InitLinkerInitialized(const AllocatorOptions &options) { 28168d75effSDimitry Andric SetAllocatorMayReturnNull(options.may_return_null); 28268d75effSDimitry Andric allocator.InitLinkerInitialized(options.release_to_os_interval_ms); 28368d75effSDimitry Andric SharedInitCode(options); 284*480093f4SDimitry Andric max_user_defined_malloc_size = common_flags()->max_allocation_size_mb 285*480093f4SDimitry Andric ? common_flags()->max_allocation_size_mb 286*480093f4SDimitry Andric << 20 287*480093f4SDimitry Andric : kMaxAllowedMallocSize; 28868d75effSDimitry Andric } 28968d75effSDimitry Andric 29068d75effSDimitry Andric bool RssLimitExceeded() { 29168d75effSDimitry Andric return atomic_load(&rss_limit_exceeded, memory_order_relaxed); 29268d75effSDimitry Andric } 29368d75effSDimitry Andric 29468d75effSDimitry Andric void SetRssLimitExceeded(bool limit_exceeded) { 29568d75effSDimitry Andric atomic_store(&rss_limit_exceeded, limit_exceeded, memory_order_relaxed); 29668d75effSDimitry Andric } 29768d75effSDimitry Andric 29868d75effSDimitry Andric void RePoisonChunk(uptr chunk) { 29968d75effSDimitry Andric // This could be a user-facing chunk (with redzones), or some internal 30068d75effSDimitry Andric // housekeeping chunk, like TransferBatch. Start by assuming the former. 30168d75effSDimitry Andric AsanChunk *ac = GetAsanChunk((void *)chunk); 30268d75effSDimitry Andric uptr allocated_size = allocator.GetActuallyAllocatedSize((void *)ac); 30368d75effSDimitry Andric uptr beg = ac->Beg(); 30468d75effSDimitry Andric uptr end = ac->Beg() + ac->UsedSize(true); 30568d75effSDimitry Andric uptr chunk_end = chunk + allocated_size; 30668d75effSDimitry Andric if (chunk < beg && beg < end && end <= chunk_end && 30768d75effSDimitry Andric ac->chunk_state == CHUNK_ALLOCATED) { 30868d75effSDimitry Andric // Looks like a valid AsanChunk in use, poison redzones only. 30968d75effSDimitry Andric PoisonShadow(chunk, beg - chunk, kAsanHeapLeftRedzoneMagic); 31068d75effSDimitry Andric uptr end_aligned_down = RoundDownTo(end, SHADOW_GRANULARITY); 31168d75effSDimitry Andric FastPoisonShadowPartialRightRedzone( 31268d75effSDimitry Andric end_aligned_down, end - end_aligned_down, 31368d75effSDimitry Andric chunk_end - end_aligned_down, kAsanHeapLeftRedzoneMagic); 31468d75effSDimitry Andric } else { 31568d75effSDimitry Andric // This is either not an AsanChunk or freed or quarantined AsanChunk. 31668d75effSDimitry Andric // In either case, poison everything. 31768d75effSDimitry Andric PoisonShadow(chunk, allocated_size, kAsanHeapLeftRedzoneMagic); 31868d75effSDimitry Andric } 31968d75effSDimitry Andric } 32068d75effSDimitry Andric 32168d75effSDimitry Andric void ReInitialize(const AllocatorOptions &options) { 32268d75effSDimitry Andric SetAllocatorMayReturnNull(options.may_return_null); 32368d75effSDimitry Andric allocator.SetReleaseToOSIntervalMs(options.release_to_os_interval_ms); 32468d75effSDimitry Andric SharedInitCode(options); 32568d75effSDimitry Andric 32668d75effSDimitry Andric // Poison all existing allocation's redzones. 32768d75effSDimitry Andric if (CanPoisonMemory()) { 32868d75effSDimitry Andric allocator.ForceLock(); 32968d75effSDimitry Andric allocator.ForEachChunk( 33068d75effSDimitry Andric [](uptr chunk, void *alloc) { 33168d75effSDimitry Andric ((Allocator *)alloc)->RePoisonChunk(chunk); 33268d75effSDimitry Andric }, 33368d75effSDimitry Andric this); 33468d75effSDimitry Andric allocator.ForceUnlock(); 33568d75effSDimitry Andric } 33668d75effSDimitry Andric } 33768d75effSDimitry Andric 33868d75effSDimitry Andric void GetOptions(AllocatorOptions *options) const { 33968d75effSDimitry Andric options->quarantine_size_mb = quarantine.GetSize() >> 20; 34068d75effSDimitry Andric options->thread_local_quarantine_size_kb = quarantine.GetCacheSize() >> 10; 34168d75effSDimitry Andric options->min_redzone = atomic_load(&min_redzone, memory_order_acquire); 34268d75effSDimitry Andric options->max_redzone = atomic_load(&max_redzone, memory_order_acquire); 34368d75effSDimitry Andric options->may_return_null = AllocatorMayReturnNull(); 34468d75effSDimitry Andric options->alloc_dealloc_mismatch = 34568d75effSDimitry Andric atomic_load(&alloc_dealloc_mismatch, memory_order_acquire); 34668d75effSDimitry Andric options->release_to_os_interval_ms = allocator.ReleaseToOSIntervalMs(); 34768d75effSDimitry Andric } 34868d75effSDimitry Andric 34968d75effSDimitry Andric // -------------------- Helper methods. ------------------------- 35068d75effSDimitry Andric uptr ComputeRZLog(uptr user_requested_size) { 35168d75effSDimitry Andric u32 rz_log = 35268d75effSDimitry Andric user_requested_size <= 64 - 16 ? 0 : 35368d75effSDimitry Andric user_requested_size <= 128 - 32 ? 1 : 35468d75effSDimitry Andric user_requested_size <= 512 - 64 ? 2 : 35568d75effSDimitry Andric user_requested_size <= 4096 - 128 ? 3 : 35668d75effSDimitry Andric user_requested_size <= (1 << 14) - 256 ? 4 : 35768d75effSDimitry Andric user_requested_size <= (1 << 15) - 512 ? 5 : 35868d75effSDimitry Andric user_requested_size <= (1 << 16) - 1024 ? 6 : 7; 35968d75effSDimitry Andric u32 min_rz = atomic_load(&min_redzone, memory_order_acquire); 36068d75effSDimitry Andric u32 max_rz = atomic_load(&max_redzone, memory_order_acquire); 36168d75effSDimitry Andric return Min(Max(rz_log, RZSize2Log(min_rz)), RZSize2Log(max_rz)); 36268d75effSDimitry Andric } 36368d75effSDimitry Andric 36468d75effSDimitry Andric static uptr ComputeUserRequestedAlignmentLog(uptr user_requested_alignment) { 36568d75effSDimitry Andric if (user_requested_alignment < 8) 36668d75effSDimitry Andric return 0; 36768d75effSDimitry Andric if (user_requested_alignment > 512) 36868d75effSDimitry Andric user_requested_alignment = 512; 36968d75effSDimitry Andric return Log2(user_requested_alignment) - 2; 37068d75effSDimitry Andric } 37168d75effSDimitry Andric 37268d75effSDimitry Andric static uptr ComputeUserAlignment(uptr user_requested_alignment_log) { 37368d75effSDimitry Andric if (user_requested_alignment_log == 0) 37468d75effSDimitry Andric return 0; 37568d75effSDimitry Andric return 1LL << (user_requested_alignment_log + 2); 37668d75effSDimitry Andric } 37768d75effSDimitry Andric 37868d75effSDimitry Andric // We have an address between two chunks, and we want to report just one. 37968d75effSDimitry Andric AsanChunk *ChooseChunk(uptr addr, AsanChunk *left_chunk, 38068d75effSDimitry Andric AsanChunk *right_chunk) { 38168d75effSDimitry Andric // Prefer an allocated chunk over freed chunk and freed chunk 38268d75effSDimitry Andric // over available chunk. 38368d75effSDimitry Andric if (left_chunk->chunk_state != right_chunk->chunk_state) { 38468d75effSDimitry Andric if (left_chunk->chunk_state == CHUNK_ALLOCATED) 38568d75effSDimitry Andric return left_chunk; 38668d75effSDimitry Andric if (right_chunk->chunk_state == CHUNK_ALLOCATED) 38768d75effSDimitry Andric return right_chunk; 38868d75effSDimitry Andric if (left_chunk->chunk_state == CHUNK_QUARANTINE) 38968d75effSDimitry Andric return left_chunk; 39068d75effSDimitry Andric if (right_chunk->chunk_state == CHUNK_QUARANTINE) 39168d75effSDimitry Andric return right_chunk; 39268d75effSDimitry Andric } 39368d75effSDimitry Andric // Same chunk_state: choose based on offset. 39468d75effSDimitry Andric sptr l_offset = 0, r_offset = 0; 39568d75effSDimitry Andric CHECK(AsanChunkView(left_chunk).AddrIsAtRight(addr, 1, &l_offset)); 39668d75effSDimitry Andric CHECK(AsanChunkView(right_chunk).AddrIsAtLeft(addr, 1, &r_offset)); 39768d75effSDimitry Andric if (l_offset < r_offset) 39868d75effSDimitry Andric return left_chunk; 39968d75effSDimitry Andric return right_chunk; 40068d75effSDimitry Andric } 40168d75effSDimitry Andric 402*480093f4SDimitry Andric bool UpdateAllocationStack(uptr addr, BufferedStackTrace *stack) { 403*480093f4SDimitry Andric AsanChunk *m = GetAsanChunkByAddr(addr); 404*480093f4SDimitry Andric if (!m) return false; 405*480093f4SDimitry Andric if (m->chunk_state != CHUNK_ALLOCATED) return false; 406*480093f4SDimitry Andric if (m->Beg() != addr) return false; 407*480093f4SDimitry Andric atomic_store((atomic_uint32_t *)&m->alloc_context_id, StackDepotPut(*stack), 408*480093f4SDimitry Andric memory_order_relaxed); 409*480093f4SDimitry Andric return true; 410*480093f4SDimitry Andric } 411*480093f4SDimitry Andric 41268d75effSDimitry Andric // -------------------- Allocation/Deallocation routines --------------- 41368d75effSDimitry Andric void *Allocate(uptr size, uptr alignment, BufferedStackTrace *stack, 41468d75effSDimitry Andric AllocType alloc_type, bool can_fill) { 41568d75effSDimitry Andric if (UNLIKELY(!asan_inited)) 41668d75effSDimitry Andric AsanInitFromRtl(); 41768d75effSDimitry Andric if (RssLimitExceeded()) { 41868d75effSDimitry Andric if (AllocatorMayReturnNull()) 41968d75effSDimitry Andric return nullptr; 42068d75effSDimitry Andric ReportRssLimitExceeded(stack); 42168d75effSDimitry Andric } 42268d75effSDimitry Andric Flags &fl = *flags(); 42368d75effSDimitry Andric CHECK(stack); 42468d75effSDimitry Andric const uptr min_alignment = SHADOW_GRANULARITY; 42568d75effSDimitry Andric const uptr user_requested_alignment_log = 42668d75effSDimitry Andric ComputeUserRequestedAlignmentLog(alignment); 42768d75effSDimitry Andric if (alignment < min_alignment) 42868d75effSDimitry Andric alignment = min_alignment; 42968d75effSDimitry Andric if (size == 0) { 43068d75effSDimitry Andric // We'd be happy to avoid allocating memory for zero-size requests, but 43168d75effSDimitry Andric // some programs/tests depend on this behavior and assume that malloc 43268d75effSDimitry Andric // would not return NULL even for zero-size allocations. Moreover, it 43368d75effSDimitry Andric // looks like operator new should never return NULL, and results of 43468d75effSDimitry Andric // consecutive "new" calls must be different even if the allocated size 43568d75effSDimitry Andric // is zero. 43668d75effSDimitry Andric size = 1; 43768d75effSDimitry Andric } 43868d75effSDimitry Andric CHECK(IsPowerOfTwo(alignment)); 43968d75effSDimitry Andric uptr rz_log = ComputeRZLog(size); 44068d75effSDimitry Andric uptr rz_size = RZLog2Size(rz_log); 44168d75effSDimitry Andric uptr rounded_size = RoundUpTo(Max(size, kChunkHeader2Size), alignment); 44268d75effSDimitry Andric uptr needed_size = rounded_size + rz_size; 44368d75effSDimitry Andric if (alignment > min_alignment) 44468d75effSDimitry Andric needed_size += alignment; 44568d75effSDimitry Andric bool using_primary_allocator = true; 44668d75effSDimitry Andric // If we are allocating from the secondary allocator, there will be no 44768d75effSDimitry Andric // automatic right redzone, so add the right redzone manually. 44868d75effSDimitry Andric if (!PrimaryAllocator::CanAllocate(needed_size, alignment)) { 44968d75effSDimitry Andric needed_size += rz_size; 45068d75effSDimitry Andric using_primary_allocator = false; 45168d75effSDimitry Andric } 45268d75effSDimitry Andric CHECK(IsAligned(needed_size, min_alignment)); 453*480093f4SDimitry Andric if (size > kMaxAllowedMallocSize || needed_size > kMaxAllowedMallocSize || 454*480093f4SDimitry Andric size > max_user_defined_malloc_size) { 45568d75effSDimitry Andric if (AllocatorMayReturnNull()) { 45668d75effSDimitry Andric Report("WARNING: AddressSanitizer failed to allocate 0x%zx bytes\n", 45768d75effSDimitry Andric (void*)size); 45868d75effSDimitry Andric return nullptr; 45968d75effSDimitry Andric } 460*480093f4SDimitry Andric uptr malloc_limit = 461*480093f4SDimitry Andric Min(kMaxAllowedMallocSize, max_user_defined_malloc_size); 462*480093f4SDimitry Andric ReportAllocationSizeTooBig(size, needed_size, malloc_limit, stack); 46368d75effSDimitry Andric } 46468d75effSDimitry Andric 46568d75effSDimitry Andric AsanThread *t = GetCurrentThread(); 46668d75effSDimitry Andric void *allocated; 46768d75effSDimitry Andric if (t) { 46868d75effSDimitry Andric AllocatorCache *cache = GetAllocatorCache(&t->malloc_storage()); 46968d75effSDimitry Andric allocated = allocator.Allocate(cache, needed_size, 8); 47068d75effSDimitry Andric } else { 47168d75effSDimitry Andric SpinMutexLock l(&fallback_mutex); 47268d75effSDimitry Andric AllocatorCache *cache = &fallback_allocator_cache; 47368d75effSDimitry Andric allocated = allocator.Allocate(cache, needed_size, 8); 47468d75effSDimitry Andric } 47568d75effSDimitry Andric if (UNLIKELY(!allocated)) { 47668d75effSDimitry Andric SetAllocatorOutOfMemory(); 47768d75effSDimitry Andric if (AllocatorMayReturnNull()) 47868d75effSDimitry Andric return nullptr; 47968d75effSDimitry Andric ReportOutOfMemory(size, stack); 48068d75effSDimitry Andric } 48168d75effSDimitry Andric 48268d75effSDimitry Andric if (*(u8 *)MEM_TO_SHADOW((uptr)allocated) == 0 && CanPoisonMemory()) { 48368d75effSDimitry Andric // Heap poisoning is enabled, but the allocator provides an unpoisoned 48468d75effSDimitry Andric // chunk. This is possible if CanPoisonMemory() was false for some 48568d75effSDimitry Andric // time, for example, due to flags()->start_disabled. 48668d75effSDimitry Andric // Anyway, poison the block before using it for anything else. 48768d75effSDimitry Andric uptr allocated_size = allocator.GetActuallyAllocatedSize(allocated); 48868d75effSDimitry Andric PoisonShadow((uptr)allocated, allocated_size, kAsanHeapLeftRedzoneMagic); 48968d75effSDimitry Andric } 49068d75effSDimitry Andric 49168d75effSDimitry Andric uptr alloc_beg = reinterpret_cast<uptr>(allocated); 49268d75effSDimitry Andric uptr alloc_end = alloc_beg + needed_size; 49368d75effSDimitry Andric uptr beg_plus_redzone = alloc_beg + rz_size; 49468d75effSDimitry Andric uptr user_beg = beg_plus_redzone; 49568d75effSDimitry Andric if (!IsAligned(user_beg, alignment)) 49668d75effSDimitry Andric user_beg = RoundUpTo(user_beg, alignment); 49768d75effSDimitry Andric uptr user_end = user_beg + size; 49868d75effSDimitry Andric CHECK_LE(user_end, alloc_end); 49968d75effSDimitry Andric uptr chunk_beg = user_beg - kChunkHeaderSize; 50068d75effSDimitry Andric AsanChunk *m = reinterpret_cast<AsanChunk *>(chunk_beg); 50168d75effSDimitry Andric m->alloc_type = alloc_type; 50268d75effSDimitry Andric m->rz_log = rz_log; 50368d75effSDimitry Andric u32 alloc_tid = t ? t->tid() : 0; 50468d75effSDimitry Andric m->alloc_tid = alloc_tid; 50568d75effSDimitry Andric CHECK_EQ(alloc_tid, m->alloc_tid); // Does alloc_tid fit into the bitfield? 50668d75effSDimitry Andric m->free_tid = kInvalidTid; 50768d75effSDimitry Andric m->from_memalign = user_beg != beg_plus_redzone; 50868d75effSDimitry Andric if (alloc_beg != chunk_beg) { 50968d75effSDimitry Andric CHECK_LE(alloc_beg+ 2 * sizeof(uptr), chunk_beg); 51068d75effSDimitry Andric reinterpret_cast<uptr *>(alloc_beg)[0] = kAllocBegMagic; 51168d75effSDimitry Andric reinterpret_cast<uptr *>(alloc_beg)[1] = chunk_beg; 51268d75effSDimitry Andric } 51368d75effSDimitry Andric if (using_primary_allocator) { 51468d75effSDimitry Andric CHECK(size); 51568d75effSDimitry Andric m->user_requested_size = size; 51668d75effSDimitry Andric CHECK(allocator.FromPrimary(allocated)); 51768d75effSDimitry Andric } else { 51868d75effSDimitry Andric CHECK(!allocator.FromPrimary(allocated)); 51968d75effSDimitry Andric m->user_requested_size = SizeClassMap::kMaxSize; 52068d75effSDimitry Andric uptr *meta = reinterpret_cast<uptr *>(allocator.GetMetaData(allocated)); 52168d75effSDimitry Andric meta[0] = size; 52268d75effSDimitry Andric meta[1] = chunk_beg; 52368d75effSDimitry Andric } 52468d75effSDimitry Andric m->user_requested_alignment_log = user_requested_alignment_log; 52568d75effSDimitry Andric 52668d75effSDimitry Andric m->alloc_context_id = StackDepotPut(*stack); 52768d75effSDimitry Andric 52868d75effSDimitry Andric uptr size_rounded_down_to_granularity = 52968d75effSDimitry Andric RoundDownTo(size, SHADOW_GRANULARITY); 53068d75effSDimitry Andric // Unpoison the bulk of the memory region. 53168d75effSDimitry Andric if (size_rounded_down_to_granularity) 53268d75effSDimitry Andric PoisonShadow(user_beg, size_rounded_down_to_granularity, 0); 53368d75effSDimitry Andric // Deal with the end of the region if size is not aligned to granularity. 53468d75effSDimitry Andric if (size != size_rounded_down_to_granularity && CanPoisonMemory()) { 53568d75effSDimitry Andric u8 *shadow = 53668d75effSDimitry Andric (u8 *)MemToShadow(user_beg + size_rounded_down_to_granularity); 53768d75effSDimitry Andric *shadow = fl.poison_partial ? (size & (SHADOW_GRANULARITY - 1)) : 0; 53868d75effSDimitry Andric } 53968d75effSDimitry Andric 54068d75effSDimitry Andric AsanStats &thread_stats = GetCurrentThreadStats(); 54168d75effSDimitry Andric thread_stats.mallocs++; 54268d75effSDimitry Andric thread_stats.malloced += size; 54368d75effSDimitry Andric thread_stats.malloced_redzones += needed_size - size; 54468d75effSDimitry Andric if (needed_size > SizeClassMap::kMaxSize) 54568d75effSDimitry Andric thread_stats.malloc_large++; 54668d75effSDimitry Andric else 54768d75effSDimitry Andric thread_stats.malloced_by_size[SizeClassMap::ClassID(needed_size)]++; 54868d75effSDimitry Andric 54968d75effSDimitry Andric void *res = reinterpret_cast<void *>(user_beg); 55068d75effSDimitry Andric if (can_fill && fl.max_malloc_fill_size) { 55168d75effSDimitry Andric uptr fill_size = Min(size, (uptr)fl.max_malloc_fill_size); 55268d75effSDimitry Andric REAL(memset)(res, fl.malloc_fill_byte, fill_size); 55368d75effSDimitry Andric } 55468d75effSDimitry Andric #if CAN_SANITIZE_LEAKS 55568d75effSDimitry Andric m->lsan_tag = __lsan::DisabledInThisThread() ? __lsan::kIgnored 55668d75effSDimitry Andric : __lsan::kDirectlyLeaked; 55768d75effSDimitry Andric #endif 55868d75effSDimitry Andric // Must be the last mutation of metadata in this function. 55968d75effSDimitry Andric atomic_store((atomic_uint8_t *)m, CHUNK_ALLOCATED, memory_order_release); 56068d75effSDimitry Andric ASAN_MALLOC_HOOK(res, size); 56168d75effSDimitry Andric return res; 56268d75effSDimitry Andric } 56368d75effSDimitry Andric 56468d75effSDimitry Andric // Set quarantine flag if chunk is allocated, issue ASan error report on 56568d75effSDimitry Andric // available and quarantined chunks. Return true on success, false otherwise. 56668d75effSDimitry Andric bool AtomicallySetQuarantineFlagIfAllocated(AsanChunk *m, void *ptr, 56768d75effSDimitry Andric BufferedStackTrace *stack) { 56868d75effSDimitry Andric u8 old_chunk_state = CHUNK_ALLOCATED; 56968d75effSDimitry Andric // Flip the chunk_state atomically to avoid race on double-free. 57068d75effSDimitry Andric if (!atomic_compare_exchange_strong((atomic_uint8_t *)m, &old_chunk_state, 57168d75effSDimitry Andric CHUNK_QUARANTINE, 57268d75effSDimitry Andric memory_order_acquire)) { 57368d75effSDimitry Andric ReportInvalidFree(ptr, old_chunk_state, stack); 57468d75effSDimitry Andric // It's not safe to push a chunk in quarantine on invalid free. 57568d75effSDimitry Andric return false; 57668d75effSDimitry Andric } 57768d75effSDimitry Andric CHECK_EQ(CHUNK_ALLOCATED, old_chunk_state); 57868d75effSDimitry Andric return true; 57968d75effSDimitry Andric } 58068d75effSDimitry Andric 58168d75effSDimitry Andric // Expects the chunk to already be marked as quarantined by using 58268d75effSDimitry Andric // AtomicallySetQuarantineFlagIfAllocated. 58368d75effSDimitry Andric void QuarantineChunk(AsanChunk *m, void *ptr, BufferedStackTrace *stack) { 58468d75effSDimitry Andric CHECK_EQ(m->chunk_state, CHUNK_QUARANTINE); 58568d75effSDimitry Andric CHECK_GE(m->alloc_tid, 0); 58668d75effSDimitry Andric if (SANITIZER_WORDSIZE == 64) // On 32-bits this resides in user area. 58768d75effSDimitry Andric CHECK_EQ(m->free_tid, kInvalidTid); 58868d75effSDimitry Andric AsanThread *t = GetCurrentThread(); 58968d75effSDimitry Andric m->free_tid = t ? t->tid() : 0; 59068d75effSDimitry Andric m->free_context_id = StackDepotPut(*stack); 59168d75effSDimitry Andric 59268d75effSDimitry Andric Flags &fl = *flags(); 59368d75effSDimitry Andric if (fl.max_free_fill_size > 0) { 59468d75effSDimitry Andric // We have to skip the chunk header, it contains free_context_id. 59568d75effSDimitry Andric uptr scribble_start = (uptr)m + kChunkHeaderSize + kChunkHeader2Size; 59668d75effSDimitry Andric if (m->UsedSize() >= kChunkHeader2Size) { // Skip Header2 in user area. 59768d75effSDimitry Andric uptr size_to_fill = m->UsedSize() - kChunkHeader2Size; 59868d75effSDimitry Andric size_to_fill = Min(size_to_fill, (uptr)fl.max_free_fill_size); 59968d75effSDimitry Andric REAL(memset)((void *)scribble_start, fl.free_fill_byte, size_to_fill); 60068d75effSDimitry Andric } 60168d75effSDimitry Andric } 60268d75effSDimitry Andric 60368d75effSDimitry Andric // Poison the region. 60468d75effSDimitry Andric PoisonShadow(m->Beg(), 60568d75effSDimitry Andric RoundUpTo(m->UsedSize(), SHADOW_GRANULARITY), 60668d75effSDimitry Andric kAsanHeapFreeMagic); 60768d75effSDimitry Andric 60868d75effSDimitry Andric AsanStats &thread_stats = GetCurrentThreadStats(); 60968d75effSDimitry Andric thread_stats.frees++; 61068d75effSDimitry Andric thread_stats.freed += m->UsedSize(); 61168d75effSDimitry Andric 61268d75effSDimitry Andric // Push into quarantine. 61368d75effSDimitry Andric if (t) { 61468d75effSDimitry Andric AsanThreadLocalMallocStorage *ms = &t->malloc_storage(); 61568d75effSDimitry Andric AllocatorCache *ac = GetAllocatorCache(ms); 61668d75effSDimitry Andric quarantine.Put(GetQuarantineCache(ms), QuarantineCallback(ac, stack), m, 61768d75effSDimitry Andric m->UsedSize()); 61868d75effSDimitry Andric } else { 61968d75effSDimitry Andric SpinMutexLock l(&fallback_mutex); 62068d75effSDimitry Andric AllocatorCache *ac = &fallback_allocator_cache; 62168d75effSDimitry Andric quarantine.Put(&fallback_quarantine_cache, QuarantineCallback(ac, stack), 62268d75effSDimitry Andric m, m->UsedSize()); 62368d75effSDimitry Andric } 62468d75effSDimitry Andric } 62568d75effSDimitry Andric 62668d75effSDimitry Andric void Deallocate(void *ptr, uptr delete_size, uptr delete_alignment, 62768d75effSDimitry Andric BufferedStackTrace *stack, AllocType alloc_type) { 62868d75effSDimitry Andric uptr p = reinterpret_cast<uptr>(ptr); 62968d75effSDimitry Andric if (p == 0) return; 63068d75effSDimitry Andric 63168d75effSDimitry Andric uptr chunk_beg = p - kChunkHeaderSize; 63268d75effSDimitry Andric AsanChunk *m = reinterpret_cast<AsanChunk *>(chunk_beg); 63368d75effSDimitry Andric 63468d75effSDimitry Andric // On Windows, uninstrumented DLLs may allocate memory before ASan hooks 63568d75effSDimitry Andric // malloc. Don't report an invalid free in this case. 63668d75effSDimitry Andric if (SANITIZER_WINDOWS && 63768d75effSDimitry Andric !get_allocator().PointerIsMine(ptr)) { 63868d75effSDimitry Andric if (!IsSystemHeapAddress(p)) 63968d75effSDimitry Andric ReportFreeNotMalloced(p, stack); 64068d75effSDimitry Andric return; 64168d75effSDimitry Andric } 64268d75effSDimitry Andric 64368d75effSDimitry Andric ASAN_FREE_HOOK(ptr); 64468d75effSDimitry Andric 64568d75effSDimitry Andric // Must mark the chunk as quarantined before any changes to its metadata. 64668d75effSDimitry Andric // Do not quarantine given chunk if we failed to set CHUNK_QUARANTINE flag. 64768d75effSDimitry Andric if (!AtomicallySetQuarantineFlagIfAllocated(m, ptr, stack)) return; 64868d75effSDimitry Andric 64968d75effSDimitry Andric if (m->alloc_type != alloc_type) { 65068d75effSDimitry Andric if (atomic_load(&alloc_dealloc_mismatch, memory_order_acquire)) { 65168d75effSDimitry Andric ReportAllocTypeMismatch((uptr)ptr, stack, (AllocType)m->alloc_type, 65268d75effSDimitry Andric (AllocType)alloc_type); 65368d75effSDimitry Andric } 65468d75effSDimitry Andric } else { 65568d75effSDimitry Andric if (flags()->new_delete_type_mismatch && 65668d75effSDimitry Andric (alloc_type == FROM_NEW || alloc_type == FROM_NEW_BR) && 65768d75effSDimitry Andric ((delete_size && delete_size != m->UsedSize()) || 65868d75effSDimitry Andric ComputeUserRequestedAlignmentLog(delete_alignment) != 65968d75effSDimitry Andric m->user_requested_alignment_log)) { 66068d75effSDimitry Andric ReportNewDeleteTypeMismatch(p, delete_size, delete_alignment, stack); 66168d75effSDimitry Andric } 66268d75effSDimitry Andric } 66368d75effSDimitry Andric 66468d75effSDimitry Andric QuarantineChunk(m, ptr, stack); 66568d75effSDimitry Andric } 66668d75effSDimitry Andric 66768d75effSDimitry Andric void *Reallocate(void *old_ptr, uptr new_size, BufferedStackTrace *stack) { 66868d75effSDimitry Andric CHECK(old_ptr && new_size); 66968d75effSDimitry Andric uptr p = reinterpret_cast<uptr>(old_ptr); 67068d75effSDimitry Andric uptr chunk_beg = p - kChunkHeaderSize; 67168d75effSDimitry Andric AsanChunk *m = reinterpret_cast<AsanChunk *>(chunk_beg); 67268d75effSDimitry Andric 67368d75effSDimitry Andric AsanStats &thread_stats = GetCurrentThreadStats(); 67468d75effSDimitry Andric thread_stats.reallocs++; 67568d75effSDimitry Andric thread_stats.realloced += new_size; 67668d75effSDimitry Andric 67768d75effSDimitry Andric void *new_ptr = Allocate(new_size, 8, stack, FROM_MALLOC, true); 67868d75effSDimitry Andric if (new_ptr) { 67968d75effSDimitry Andric u8 chunk_state = m->chunk_state; 68068d75effSDimitry Andric if (chunk_state != CHUNK_ALLOCATED) 68168d75effSDimitry Andric ReportInvalidFree(old_ptr, chunk_state, stack); 68268d75effSDimitry Andric CHECK_NE(REAL(memcpy), nullptr); 68368d75effSDimitry Andric uptr memcpy_size = Min(new_size, m->UsedSize()); 68468d75effSDimitry Andric // If realloc() races with free(), we may start copying freed memory. 68568d75effSDimitry Andric // However, we will report racy double-free later anyway. 68668d75effSDimitry Andric REAL(memcpy)(new_ptr, old_ptr, memcpy_size); 68768d75effSDimitry Andric Deallocate(old_ptr, 0, 0, stack, FROM_MALLOC); 68868d75effSDimitry Andric } 68968d75effSDimitry Andric return new_ptr; 69068d75effSDimitry Andric } 69168d75effSDimitry Andric 69268d75effSDimitry Andric void *Calloc(uptr nmemb, uptr size, BufferedStackTrace *stack) { 69368d75effSDimitry Andric if (UNLIKELY(CheckForCallocOverflow(size, nmemb))) { 69468d75effSDimitry Andric if (AllocatorMayReturnNull()) 69568d75effSDimitry Andric return nullptr; 69668d75effSDimitry Andric ReportCallocOverflow(nmemb, size, stack); 69768d75effSDimitry Andric } 69868d75effSDimitry Andric void *ptr = Allocate(nmemb * size, 8, stack, FROM_MALLOC, false); 69968d75effSDimitry Andric // If the memory comes from the secondary allocator no need to clear it 70068d75effSDimitry Andric // as it comes directly from mmap. 70168d75effSDimitry Andric if (ptr && allocator.FromPrimary(ptr)) 70268d75effSDimitry Andric REAL(memset)(ptr, 0, nmemb * size); 70368d75effSDimitry Andric return ptr; 70468d75effSDimitry Andric } 70568d75effSDimitry Andric 70668d75effSDimitry Andric void ReportInvalidFree(void *ptr, u8 chunk_state, BufferedStackTrace *stack) { 70768d75effSDimitry Andric if (chunk_state == CHUNK_QUARANTINE) 70868d75effSDimitry Andric ReportDoubleFree((uptr)ptr, stack); 70968d75effSDimitry Andric else 71068d75effSDimitry Andric ReportFreeNotMalloced((uptr)ptr, stack); 71168d75effSDimitry Andric } 71268d75effSDimitry Andric 71368d75effSDimitry Andric void CommitBack(AsanThreadLocalMallocStorage *ms, BufferedStackTrace *stack) { 71468d75effSDimitry Andric AllocatorCache *ac = GetAllocatorCache(ms); 71568d75effSDimitry Andric quarantine.Drain(GetQuarantineCache(ms), QuarantineCallback(ac, stack)); 71668d75effSDimitry Andric allocator.SwallowCache(ac); 71768d75effSDimitry Andric } 71868d75effSDimitry Andric 71968d75effSDimitry Andric // -------------------------- Chunk lookup ---------------------- 72068d75effSDimitry Andric 72168d75effSDimitry Andric // Assumes alloc_beg == allocator.GetBlockBegin(alloc_beg). 72268d75effSDimitry Andric AsanChunk *GetAsanChunk(void *alloc_beg) { 72368d75effSDimitry Andric if (!alloc_beg) return nullptr; 72468d75effSDimitry Andric if (!allocator.FromPrimary(alloc_beg)) { 72568d75effSDimitry Andric uptr *meta = reinterpret_cast<uptr *>(allocator.GetMetaData(alloc_beg)); 72668d75effSDimitry Andric AsanChunk *m = reinterpret_cast<AsanChunk *>(meta[1]); 72768d75effSDimitry Andric return m; 72868d75effSDimitry Andric } 72968d75effSDimitry Andric uptr *alloc_magic = reinterpret_cast<uptr *>(alloc_beg); 73068d75effSDimitry Andric if (alloc_magic[0] == kAllocBegMagic) 73168d75effSDimitry Andric return reinterpret_cast<AsanChunk *>(alloc_magic[1]); 73268d75effSDimitry Andric return reinterpret_cast<AsanChunk *>(alloc_beg); 73368d75effSDimitry Andric } 73468d75effSDimitry Andric 73568d75effSDimitry Andric AsanChunk *GetAsanChunkByAddr(uptr p) { 73668d75effSDimitry Andric void *alloc_beg = allocator.GetBlockBegin(reinterpret_cast<void *>(p)); 73768d75effSDimitry Andric return GetAsanChunk(alloc_beg); 73868d75effSDimitry Andric } 73968d75effSDimitry Andric 74068d75effSDimitry Andric // Allocator must be locked when this function is called. 74168d75effSDimitry Andric AsanChunk *GetAsanChunkByAddrFastLocked(uptr p) { 74268d75effSDimitry Andric void *alloc_beg = 74368d75effSDimitry Andric allocator.GetBlockBeginFastLocked(reinterpret_cast<void *>(p)); 74468d75effSDimitry Andric return GetAsanChunk(alloc_beg); 74568d75effSDimitry Andric } 74668d75effSDimitry Andric 74768d75effSDimitry Andric uptr AllocationSize(uptr p) { 74868d75effSDimitry Andric AsanChunk *m = GetAsanChunkByAddr(p); 74968d75effSDimitry Andric if (!m) return 0; 75068d75effSDimitry Andric if (m->chunk_state != CHUNK_ALLOCATED) return 0; 75168d75effSDimitry Andric if (m->Beg() != p) return 0; 75268d75effSDimitry Andric return m->UsedSize(); 75368d75effSDimitry Andric } 75468d75effSDimitry Andric 75568d75effSDimitry Andric AsanChunkView FindHeapChunkByAddress(uptr addr) { 75668d75effSDimitry Andric AsanChunk *m1 = GetAsanChunkByAddr(addr); 75768d75effSDimitry Andric if (!m1) return AsanChunkView(m1); 75868d75effSDimitry Andric sptr offset = 0; 75968d75effSDimitry Andric if (AsanChunkView(m1).AddrIsAtLeft(addr, 1, &offset)) { 76068d75effSDimitry Andric // The address is in the chunk's left redzone, so maybe it is actually 76168d75effSDimitry Andric // a right buffer overflow from the other chunk to the left. 76268d75effSDimitry Andric // Search a bit to the left to see if there is another chunk. 76368d75effSDimitry Andric AsanChunk *m2 = nullptr; 76468d75effSDimitry Andric for (uptr l = 1; l < GetPageSizeCached(); l++) { 76568d75effSDimitry Andric m2 = GetAsanChunkByAddr(addr - l); 76668d75effSDimitry Andric if (m2 == m1) continue; // Still the same chunk. 76768d75effSDimitry Andric break; 76868d75effSDimitry Andric } 76968d75effSDimitry Andric if (m2 && AsanChunkView(m2).AddrIsAtRight(addr, 1, &offset)) 77068d75effSDimitry Andric m1 = ChooseChunk(addr, m2, m1); 77168d75effSDimitry Andric } 77268d75effSDimitry Andric return AsanChunkView(m1); 77368d75effSDimitry Andric } 77468d75effSDimitry Andric 77568d75effSDimitry Andric void Purge(BufferedStackTrace *stack) { 77668d75effSDimitry Andric AsanThread *t = GetCurrentThread(); 77768d75effSDimitry Andric if (t) { 77868d75effSDimitry Andric AsanThreadLocalMallocStorage *ms = &t->malloc_storage(); 77968d75effSDimitry Andric quarantine.DrainAndRecycle(GetQuarantineCache(ms), 78068d75effSDimitry Andric QuarantineCallback(GetAllocatorCache(ms), 78168d75effSDimitry Andric stack)); 78268d75effSDimitry Andric } 78368d75effSDimitry Andric { 78468d75effSDimitry Andric SpinMutexLock l(&fallback_mutex); 78568d75effSDimitry Andric quarantine.DrainAndRecycle(&fallback_quarantine_cache, 78668d75effSDimitry Andric QuarantineCallback(&fallback_allocator_cache, 78768d75effSDimitry Andric stack)); 78868d75effSDimitry Andric } 78968d75effSDimitry Andric 79068d75effSDimitry Andric allocator.ForceReleaseToOS(); 79168d75effSDimitry Andric } 79268d75effSDimitry Andric 79368d75effSDimitry Andric void PrintStats() { 79468d75effSDimitry Andric allocator.PrintStats(); 79568d75effSDimitry Andric quarantine.PrintStats(); 79668d75effSDimitry Andric } 79768d75effSDimitry Andric 79868d75effSDimitry Andric void ForceLock() { 79968d75effSDimitry Andric allocator.ForceLock(); 80068d75effSDimitry Andric fallback_mutex.Lock(); 80168d75effSDimitry Andric } 80268d75effSDimitry Andric 80368d75effSDimitry Andric void ForceUnlock() { 80468d75effSDimitry Andric fallback_mutex.Unlock(); 80568d75effSDimitry Andric allocator.ForceUnlock(); 80668d75effSDimitry Andric } 80768d75effSDimitry Andric }; 80868d75effSDimitry Andric 80968d75effSDimitry Andric static Allocator instance(LINKER_INITIALIZED); 81068d75effSDimitry Andric 81168d75effSDimitry Andric static AsanAllocator &get_allocator() { 81268d75effSDimitry Andric return instance.allocator; 81368d75effSDimitry Andric } 81468d75effSDimitry Andric 81568d75effSDimitry Andric bool AsanChunkView::IsValid() const { 81668d75effSDimitry Andric return chunk_ && chunk_->chunk_state != CHUNK_AVAILABLE; 81768d75effSDimitry Andric } 81868d75effSDimitry Andric bool AsanChunkView::IsAllocated() const { 81968d75effSDimitry Andric return chunk_ && chunk_->chunk_state == CHUNK_ALLOCATED; 82068d75effSDimitry Andric } 82168d75effSDimitry Andric bool AsanChunkView::IsQuarantined() const { 82268d75effSDimitry Andric return chunk_ && chunk_->chunk_state == CHUNK_QUARANTINE; 82368d75effSDimitry Andric } 82468d75effSDimitry Andric uptr AsanChunkView::Beg() const { return chunk_->Beg(); } 82568d75effSDimitry Andric uptr AsanChunkView::End() const { return Beg() + UsedSize(); } 82668d75effSDimitry Andric uptr AsanChunkView::UsedSize() const { return chunk_->UsedSize(); } 82768d75effSDimitry Andric u32 AsanChunkView::UserRequestedAlignment() const { 82868d75effSDimitry Andric return Allocator::ComputeUserAlignment(chunk_->user_requested_alignment_log); 82968d75effSDimitry Andric } 83068d75effSDimitry Andric uptr AsanChunkView::AllocTid() const { return chunk_->alloc_tid; } 83168d75effSDimitry Andric uptr AsanChunkView::FreeTid() const { return chunk_->free_tid; } 83268d75effSDimitry Andric AllocType AsanChunkView::GetAllocType() const { 83368d75effSDimitry Andric return (AllocType)chunk_->alloc_type; 83468d75effSDimitry Andric } 83568d75effSDimitry Andric 83668d75effSDimitry Andric static StackTrace GetStackTraceFromId(u32 id) { 83768d75effSDimitry Andric CHECK(id); 83868d75effSDimitry Andric StackTrace res = StackDepotGet(id); 83968d75effSDimitry Andric CHECK(res.trace); 84068d75effSDimitry Andric return res; 84168d75effSDimitry Andric } 84268d75effSDimitry Andric 84368d75effSDimitry Andric u32 AsanChunkView::GetAllocStackId() const { return chunk_->alloc_context_id; } 84468d75effSDimitry Andric u32 AsanChunkView::GetFreeStackId() const { return chunk_->free_context_id; } 84568d75effSDimitry Andric 84668d75effSDimitry Andric StackTrace AsanChunkView::GetAllocStack() const { 84768d75effSDimitry Andric return GetStackTraceFromId(GetAllocStackId()); 84868d75effSDimitry Andric } 84968d75effSDimitry Andric 85068d75effSDimitry Andric StackTrace AsanChunkView::GetFreeStack() const { 85168d75effSDimitry Andric return GetStackTraceFromId(GetFreeStackId()); 85268d75effSDimitry Andric } 85368d75effSDimitry Andric 85468d75effSDimitry Andric void InitializeAllocator(const AllocatorOptions &options) { 85568d75effSDimitry Andric instance.InitLinkerInitialized(options); 85668d75effSDimitry Andric } 85768d75effSDimitry Andric 85868d75effSDimitry Andric void ReInitializeAllocator(const AllocatorOptions &options) { 85968d75effSDimitry Andric instance.ReInitialize(options); 86068d75effSDimitry Andric } 86168d75effSDimitry Andric 86268d75effSDimitry Andric void GetAllocatorOptions(AllocatorOptions *options) { 86368d75effSDimitry Andric instance.GetOptions(options); 86468d75effSDimitry Andric } 86568d75effSDimitry Andric 86668d75effSDimitry Andric AsanChunkView FindHeapChunkByAddress(uptr addr) { 86768d75effSDimitry Andric return instance.FindHeapChunkByAddress(addr); 86868d75effSDimitry Andric } 86968d75effSDimitry Andric AsanChunkView FindHeapChunkByAllocBeg(uptr addr) { 87068d75effSDimitry Andric return AsanChunkView(instance.GetAsanChunk(reinterpret_cast<void*>(addr))); 87168d75effSDimitry Andric } 87268d75effSDimitry Andric 87368d75effSDimitry Andric void AsanThreadLocalMallocStorage::CommitBack() { 87468d75effSDimitry Andric GET_STACK_TRACE_MALLOC; 87568d75effSDimitry Andric instance.CommitBack(this, &stack); 87668d75effSDimitry Andric } 87768d75effSDimitry Andric 87868d75effSDimitry Andric void PrintInternalAllocatorStats() { 87968d75effSDimitry Andric instance.PrintStats(); 88068d75effSDimitry Andric } 88168d75effSDimitry Andric 88268d75effSDimitry Andric void asan_free(void *ptr, BufferedStackTrace *stack, AllocType alloc_type) { 88368d75effSDimitry Andric instance.Deallocate(ptr, 0, 0, stack, alloc_type); 88468d75effSDimitry Andric } 88568d75effSDimitry Andric 88668d75effSDimitry Andric void asan_delete(void *ptr, uptr size, uptr alignment, 88768d75effSDimitry Andric BufferedStackTrace *stack, AllocType alloc_type) { 88868d75effSDimitry Andric instance.Deallocate(ptr, size, alignment, stack, alloc_type); 88968d75effSDimitry Andric } 89068d75effSDimitry Andric 89168d75effSDimitry Andric void *asan_malloc(uptr size, BufferedStackTrace *stack) { 89268d75effSDimitry Andric return SetErrnoOnNull(instance.Allocate(size, 8, stack, FROM_MALLOC, true)); 89368d75effSDimitry Andric } 89468d75effSDimitry Andric 89568d75effSDimitry Andric void *asan_calloc(uptr nmemb, uptr size, BufferedStackTrace *stack) { 89668d75effSDimitry Andric return SetErrnoOnNull(instance.Calloc(nmemb, size, stack)); 89768d75effSDimitry Andric } 89868d75effSDimitry Andric 89968d75effSDimitry Andric void *asan_reallocarray(void *p, uptr nmemb, uptr size, 90068d75effSDimitry Andric BufferedStackTrace *stack) { 90168d75effSDimitry Andric if (UNLIKELY(CheckForCallocOverflow(size, nmemb))) { 90268d75effSDimitry Andric errno = errno_ENOMEM; 90368d75effSDimitry Andric if (AllocatorMayReturnNull()) 90468d75effSDimitry Andric return nullptr; 90568d75effSDimitry Andric ReportReallocArrayOverflow(nmemb, size, stack); 90668d75effSDimitry Andric } 90768d75effSDimitry Andric return asan_realloc(p, nmemb * size, stack); 90868d75effSDimitry Andric } 90968d75effSDimitry Andric 91068d75effSDimitry Andric void *asan_realloc(void *p, uptr size, BufferedStackTrace *stack) { 91168d75effSDimitry Andric if (!p) 91268d75effSDimitry Andric return SetErrnoOnNull(instance.Allocate(size, 8, stack, FROM_MALLOC, true)); 91368d75effSDimitry Andric if (size == 0) { 91468d75effSDimitry Andric if (flags()->allocator_frees_and_returns_null_on_realloc_zero) { 91568d75effSDimitry Andric instance.Deallocate(p, 0, 0, stack, FROM_MALLOC); 91668d75effSDimitry Andric return nullptr; 91768d75effSDimitry Andric } 91868d75effSDimitry Andric // Allocate a size of 1 if we shouldn't free() on Realloc to 0 91968d75effSDimitry Andric size = 1; 92068d75effSDimitry Andric } 92168d75effSDimitry Andric return SetErrnoOnNull(instance.Reallocate(p, size, stack)); 92268d75effSDimitry Andric } 92368d75effSDimitry Andric 92468d75effSDimitry Andric void *asan_valloc(uptr size, BufferedStackTrace *stack) { 92568d75effSDimitry Andric return SetErrnoOnNull( 92668d75effSDimitry Andric instance.Allocate(size, GetPageSizeCached(), stack, FROM_MALLOC, true)); 92768d75effSDimitry Andric } 92868d75effSDimitry Andric 92968d75effSDimitry Andric void *asan_pvalloc(uptr size, BufferedStackTrace *stack) { 93068d75effSDimitry Andric uptr PageSize = GetPageSizeCached(); 93168d75effSDimitry Andric if (UNLIKELY(CheckForPvallocOverflow(size, PageSize))) { 93268d75effSDimitry Andric errno = errno_ENOMEM; 93368d75effSDimitry Andric if (AllocatorMayReturnNull()) 93468d75effSDimitry Andric return nullptr; 93568d75effSDimitry Andric ReportPvallocOverflow(size, stack); 93668d75effSDimitry Andric } 93768d75effSDimitry Andric // pvalloc(0) should allocate one page. 93868d75effSDimitry Andric size = size ? RoundUpTo(size, PageSize) : PageSize; 93968d75effSDimitry Andric return SetErrnoOnNull( 94068d75effSDimitry Andric instance.Allocate(size, PageSize, stack, FROM_MALLOC, true)); 94168d75effSDimitry Andric } 94268d75effSDimitry Andric 94368d75effSDimitry Andric void *asan_memalign(uptr alignment, uptr size, BufferedStackTrace *stack, 94468d75effSDimitry Andric AllocType alloc_type) { 94568d75effSDimitry Andric if (UNLIKELY(!IsPowerOfTwo(alignment))) { 94668d75effSDimitry Andric errno = errno_EINVAL; 94768d75effSDimitry Andric if (AllocatorMayReturnNull()) 94868d75effSDimitry Andric return nullptr; 94968d75effSDimitry Andric ReportInvalidAllocationAlignment(alignment, stack); 95068d75effSDimitry Andric } 95168d75effSDimitry Andric return SetErrnoOnNull( 95268d75effSDimitry Andric instance.Allocate(size, alignment, stack, alloc_type, true)); 95368d75effSDimitry Andric } 95468d75effSDimitry Andric 95568d75effSDimitry Andric void *asan_aligned_alloc(uptr alignment, uptr size, BufferedStackTrace *stack) { 95668d75effSDimitry Andric if (UNLIKELY(!CheckAlignedAllocAlignmentAndSize(alignment, size))) { 95768d75effSDimitry Andric errno = errno_EINVAL; 95868d75effSDimitry Andric if (AllocatorMayReturnNull()) 95968d75effSDimitry Andric return nullptr; 96068d75effSDimitry Andric ReportInvalidAlignedAllocAlignment(size, alignment, stack); 96168d75effSDimitry Andric } 96268d75effSDimitry Andric return SetErrnoOnNull( 96368d75effSDimitry Andric instance.Allocate(size, alignment, stack, FROM_MALLOC, true)); 96468d75effSDimitry Andric } 96568d75effSDimitry Andric 96668d75effSDimitry Andric int asan_posix_memalign(void **memptr, uptr alignment, uptr size, 96768d75effSDimitry Andric BufferedStackTrace *stack) { 96868d75effSDimitry Andric if (UNLIKELY(!CheckPosixMemalignAlignment(alignment))) { 96968d75effSDimitry Andric if (AllocatorMayReturnNull()) 97068d75effSDimitry Andric return errno_EINVAL; 97168d75effSDimitry Andric ReportInvalidPosixMemalignAlignment(alignment, stack); 97268d75effSDimitry Andric } 97368d75effSDimitry Andric void *ptr = instance.Allocate(size, alignment, stack, FROM_MALLOC, true); 97468d75effSDimitry Andric if (UNLIKELY(!ptr)) 97568d75effSDimitry Andric // OOM error is already taken care of by Allocate. 97668d75effSDimitry Andric return errno_ENOMEM; 97768d75effSDimitry Andric CHECK(IsAligned((uptr)ptr, alignment)); 97868d75effSDimitry Andric *memptr = ptr; 97968d75effSDimitry Andric return 0; 98068d75effSDimitry Andric } 98168d75effSDimitry Andric 98268d75effSDimitry Andric uptr asan_malloc_usable_size(const void *ptr, uptr pc, uptr bp) { 98368d75effSDimitry Andric if (!ptr) return 0; 98468d75effSDimitry Andric uptr usable_size = instance.AllocationSize(reinterpret_cast<uptr>(ptr)); 98568d75effSDimitry Andric if (flags()->check_malloc_usable_size && (usable_size == 0)) { 98668d75effSDimitry Andric GET_STACK_TRACE_FATAL(pc, bp); 98768d75effSDimitry Andric ReportMallocUsableSizeNotOwned((uptr)ptr, &stack); 98868d75effSDimitry Andric } 98968d75effSDimitry Andric return usable_size; 99068d75effSDimitry Andric } 99168d75effSDimitry Andric 99268d75effSDimitry Andric uptr asan_mz_size(const void *ptr) { 99368d75effSDimitry Andric return instance.AllocationSize(reinterpret_cast<uptr>(ptr)); 99468d75effSDimitry Andric } 99568d75effSDimitry Andric 99668d75effSDimitry Andric void asan_mz_force_lock() { 99768d75effSDimitry Andric instance.ForceLock(); 99868d75effSDimitry Andric } 99968d75effSDimitry Andric 100068d75effSDimitry Andric void asan_mz_force_unlock() { 100168d75effSDimitry Andric instance.ForceUnlock(); 100268d75effSDimitry Andric } 100368d75effSDimitry Andric 100468d75effSDimitry Andric void AsanSoftRssLimitExceededCallback(bool limit_exceeded) { 100568d75effSDimitry Andric instance.SetRssLimitExceeded(limit_exceeded); 100668d75effSDimitry Andric } 100768d75effSDimitry Andric 100868d75effSDimitry Andric } // namespace __asan 100968d75effSDimitry Andric 101068d75effSDimitry Andric // --- Implementation of LSan-specific functions --- {{{1 101168d75effSDimitry Andric namespace __lsan { 101268d75effSDimitry Andric void LockAllocator() { 101368d75effSDimitry Andric __asan::get_allocator().ForceLock(); 101468d75effSDimitry Andric } 101568d75effSDimitry Andric 101668d75effSDimitry Andric void UnlockAllocator() { 101768d75effSDimitry Andric __asan::get_allocator().ForceUnlock(); 101868d75effSDimitry Andric } 101968d75effSDimitry Andric 102068d75effSDimitry Andric void GetAllocatorGlobalRange(uptr *begin, uptr *end) { 102168d75effSDimitry Andric *begin = (uptr)&__asan::get_allocator(); 102268d75effSDimitry Andric *end = *begin + sizeof(__asan::get_allocator()); 102368d75effSDimitry Andric } 102468d75effSDimitry Andric 102568d75effSDimitry Andric uptr PointsIntoChunk(void* p) { 102668d75effSDimitry Andric uptr addr = reinterpret_cast<uptr>(p); 102768d75effSDimitry Andric __asan::AsanChunk *m = __asan::instance.GetAsanChunkByAddrFastLocked(addr); 102868d75effSDimitry Andric if (!m) return 0; 102968d75effSDimitry Andric uptr chunk = m->Beg(); 103068d75effSDimitry Andric if (m->chunk_state != __asan::CHUNK_ALLOCATED) 103168d75effSDimitry Andric return 0; 103268d75effSDimitry Andric if (m->AddrIsInside(addr, /*locked_version=*/true)) 103368d75effSDimitry Andric return chunk; 103468d75effSDimitry Andric if (IsSpecialCaseOfOperatorNew0(chunk, m->UsedSize(/*locked_version*/ true), 103568d75effSDimitry Andric addr)) 103668d75effSDimitry Andric return chunk; 103768d75effSDimitry Andric return 0; 103868d75effSDimitry Andric } 103968d75effSDimitry Andric 104068d75effSDimitry Andric uptr GetUserBegin(uptr chunk) { 104168d75effSDimitry Andric __asan::AsanChunk *m = __asan::instance.GetAsanChunkByAddrFastLocked(chunk); 104268d75effSDimitry Andric CHECK(m); 104368d75effSDimitry Andric return m->Beg(); 104468d75effSDimitry Andric } 104568d75effSDimitry Andric 104668d75effSDimitry Andric LsanMetadata::LsanMetadata(uptr chunk) { 104768d75effSDimitry Andric metadata_ = reinterpret_cast<void *>(chunk - __asan::kChunkHeaderSize); 104868d75effSDimitry Andric } 104968d75effSDimitry Andric 105068d75effSDimitry Andric bool LsanMetadata::allocated() const { 105168d75effSDimitry Andric __asan::AsanChunk *m = reinterpret_cast<__asan::AsanChunk *>(metadata_); 105268d75effSDimitry Andric return m->chunk_state == __asan::CHUNK_ALLOCATED; 105368d75effSDimitry Andric } 105468d75effSDimitry Andric 105568d75effSDimitry Andric ChunkTag LsanMetadata::tag() const { 105668d75effSDimitry Andric __asan::AsanChunk *m = reinterpret_cast<__asan::AsanChunk *>(metadata_); 105768d75effSDimitry Andric return static_cast<ChunkTag>(m->lsan_tag); 105868d75effSDimitry Andric } 105968d75effSDimitry Andric 106068d75effSDimitry Andric void LsanMetadata::set_tag(ChunkTag value) { 106168d75effSDimitry Andric __asan::AsanChunk *m = reinterpret_cast<__asan::AsanChunk *>(metadata_); 106268d75effSDimitry Andric m->lsan_tag = value; 106368d75effSDimitry Andric } 106468d75effSDimitry Andric 106568d75effSDimitry Andric uptr LsanMetadata::requested_size() const { 106668d75effSDimitry Andric __asan::AsanChunk *m = reinterpret_cast<__asan::AsanChunk *>(metadata_); 106768d75effSDimitry Andric return m->UsedSize(/*locked_version=*/true); 106868d75effSDimitry Andric } 106968d75effSDimitry Andric 107068d75effSDimitry Andric u32 LsanMetadata::stack_trace_id() const { 107168d75effSDimitry Andric __asan::AsanChunk *m = reinterpret_cast<__asan::AsanChunk *>(metadata_); 107268d75effSDimitry Andric return m->alloc_context_id; 107368d75effSDimitry Andric } 107468d75effSDimitry Andric 107568d75effSDimitry Andric void ForEachChunk(ForEachChunkCallback callback, void *arg) { 107668d75effSDimitry Andric __asan::get_allocator().ForEachChunk(callback, arg); 107768d75effSDimitry Andric } 107868d75effSDimitry Andric 107968d75effSDimitry Andric IgnoreObjectResult IgnoreObjectLocked(const void *p) { 108068d75effSDimitry Andric uptr addr = reinterpret_cast<uptr>(p); 108168d75effSDimitry Andric __asan::AsanChunk *m = __asan::instance.GetAsanChunkByAddr(addr); 108268d75effSDimitry Andric if (!m) return kIgnoreObjectInvalid; 108368d75effSDimitry Andric if ((m->chunk_state == __asan::CHUNK_ALLOCATED) && m->AddrIsInside(addr)) { 108468d75effSDimitry Andric if (m->lsan_tag == kIgnored) 108568d75effSDimitry Andric return kIgnoreObjectAlreadyIgnored; 108668d75effSDimitry Andric m->lsan_tag = __lsan::kIgnored; 108768d75effSDimitry Andric return kIgnoreObjectSuccess; 108868d75effSDimitry Andric } else { 108968d75effSDimitry Andric return kIgnoreObjectInvalid; 109068d75effSDimitry Andric } 109168d75effSDimitry Andric } 109268d75effSDimitry Andric } // namespace __lsan 109368d75effSDimitry Andric 109468d75effSDimitry Andric // ---------------------- Interface ---------------- {{{1 109568d75effSDimitry Andric using namespace __asan; 109668d75effSDimitry Andric 109768d75effSDimitry Andric // ASan allocator doesn't reserve extra bytes, so normally we would 109868d75effSDimitry Andric // just return "size". We don't want to expose our redzone sizes, etc here. 109968d75effSDimitry Andric uptr __sanitizer_get_estimated_allocated_size(uptr size) { 110068d75effSDimitry Andric return size; 110168d75effSDimitry Andric } 110268d75effSDimitry Andric 110368d75effSDimitry Andric int __sanitizer_get_ownership(const void *p) { 110468d75effSDimitry Andric uptr ptr = reinterpret_cast<uptr>(p); 110568d75effSDimitry Andric return instance.AllocationSize(ptr) > 0; 110668d75effSDimitry Andric } 110768d75effSDimitry Andric 110868d75effSDimitry Andric uptr __sanitizer_get_allocated_size(const void *p) { 110968d75effSDimitry Andric if (!p) return 0; 111068d75effSDimitry Andric uptr ptr = reinterpret_cast<uptr>(p); 111168d75effSDimitry Andric uptr allocated_size = instance.AllocationSize(ptr); 111268d75effSDimitry Andric // Die if p is not malloced or if it is already freed. 111368d75effSDimitry Andric if (allocated_size == 0) { 111468d75effSDimitry Andric GET_STACK_TRACE_FATAL_HERE; 111568d75effSDimitry Andric ReportSanitizerGetAllocatedSizeNotOwned(ptr, &stack); 111668d75effSDimitry Andric } 111768d75effSDimitry Andric return allocated_size; 111868d75effSDimitry Andric } 111968d75effSDimitry Andric 112068d75effSDimitry Andric void __sanitizer_purge_allocator() { 112168d75effSDimitry Andric GET_STACK_TRACE_MALLOC; 112268d75effSDimitry Andric instance.Purge(&stack); 112368d75effSDimitry Andric } 112468d75effSDimitry Andric 1125*480093f4SDimitry Andric int __asan_update_allocation_context(void* addr) { 1126*480093f4SDimitry Andric GET_STACK_TRACE_MALLOC; 1127*480093f4SDimitry Andric return instance.UpdateAllocationStack((uptr)addr, &stack); 1128*480093f4SDimitry Andric } 1129*480093f4SDimitry Andric 113068d75effSDimitry Andric #if !SANITIZER_SUPPORTS_WEAK_HOOKS 113168d75effSDimitry Andric // Provide default (no-op) implementation of malloc hooks. 113268d75effSDimitry Andric SANITIZER_INTERFACE_WEAK_DEF(void, __sanitizer_malloc_hook, 113368d75effSDimitry Andric void *ptr, uptr size) { 113468d75effSDimitry Andric (void)ptr; 113568d75effSDimitry Andric (void)size; 113668d75effSDimitry Andric } 113768d75effSDimitry Andric 113868d75effSDimitry Andric SANITIZER_INTERFACE_WEAK_DEF(void, __sanitizer_free_hook, void *ptr) { 113968d75effSDimitry Andric (void)ptr; 114068d75effSDimitry Andric } 114168d75effSDimitry Andric #endif 1142