xref: /freebsd/contrib/llvm-project/compiler-rt/lib/hwasan/hwasan_allocator.cpp (revision 0fca6ea1d4eea4c934cfff25ac9ee8ad6fe95583)
10b57cec5SDimitry Andric //===-- hwasan_allocator.cpp ------------------------ ---------------------===//
20b57cec5SDimitry Andric //
30b57cec5SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
40b57cec5SDimitry Andric // See https://llvm.org/LICENSE.txt for license information.
50b57cec5SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
60b57cec5SDimitry Andric //
70b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
80b57cec5SDimitry Andric //
90b57cec5SDimitry Andric // This file is a part of HWAddressSanitizer.
100b57cec5SDimitry Andric //
110b57cec5SDimitry Andric // HWAddressSanitizer allocator.
120b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
130b57cec5SDimitry Andric 
140b57cec5SDimitry Andric #include "sanitizer_common/sanitizer_atomic.h"
150b57cec5SDimitry Andric #include "sanitizer_common/sanitizer_errno.h"
160b57cec5SDimitry Andric #include "sanitizer_common/sanitizer_stackdepot.h"
170b57cec5SDimitry Andric #include "hwasan.h"
180b57cec5SDimitry Andric #include "hwasan_allocator.h"
190b57cec5SDimitry Andric #include "hwasan_checks.h"
200b57cec5SDimitry Andric #include "hwasan_mapping.h"
210b57cec5SDimitry Andric #include "hwasan_malloc_bisect.h"
220b57cec5SDimitry Andric #include "hwasan_thread.h"
230b57cec5SDimitry Andric #include "hwasan_report.h"
24bdd1243dSDimitry Andric #include "lsan/lsan_common.h"
250b57cec5SDimitry Andric 
260b57cec5SDimitry Andric namespace __hwasan {
270b57cec5SDimitry Andric 
280b57cec5SDimitry Andric static Allocator allocator;
290b57cec5SDimitry Andric static AllocatorCache fallback_allocator_cache;
300b57cec5SDimitry Andric static SpinMutex fallback_mutex;
310b57cec5SDimitry Andric static atomic_uint8_t hwasan_allocator_tagging_enabled;
320b57cec5SDimitry Andric 
33fe6060f1SDimitry Andric static constexpr tag_t kFallbackAllocTag = 0xBB & kTagMask;
34fe6060f1SDimitry Andric static constexpr tag_t kFallbackFreeTag = 0xBC;
350b57cec5SDimitry Andric 
36bdd1243dSDimitry Andric enum {
37bdd1243dSDimitry Andric   // Either just allocated by underlying allocator, but AsanChunk is not yet
38bdd1243dSDimitry Andric   // ready, or almost returned to undelying allocator and AsanChunk is already
39bdd1243dSDimitry Andric   // meaningless.
40bdd1243dSDimitry Andric   CHUNK_INVALID = 0,
41bdd1243dSDimitry Andric   // The chunk is allocated and not yet freed.
42bdd1243dSDimitry Andric   CHUNK_ALLOCATED = 1,
430b57cec5SDimitry Andric };
440b57cec5SDimitry Andric 
45bdd1243dSDimitry Andric 
460b57cec5SDimitry Andric // Initialized in HwasanAllocatorInit, an never changed.
47*0fca6ea1SDimitry Andric alignas(16) static u8 tail_magic[kShadowAlignment - 1];
4806c3fb27SDimitry Andric static uptr max_malloc_size;
490b57cec5SDimitry Andric 
IsAllocated() const500b57cec5SDimitry Andric bool HwasanChunkView::IsAllocated() const {
51bdd1243dSDimitry Andric   return metadata_ && metadata_->IsAllocated();
520b57cec5SDimitry Andric }
530b57cec5SDimitry Andric 
Beg() const540b57cec5SDimitry Andric uptr HwasanChunkView::Beg() const {
550b57cec5SDimitry Andric   return block_;
560b57cec5SDimitry Andric }
End() const570b57cec5SDimitry Andric uptr HwasanChunkView::End() const {
580b57cec5SDimitry Andric   return Beg() + UsedSize();
590b57cec5SDimitry Andric }
UsedSize() const600b57cec5SDimitry Andric uptr HwasanChunkView::UsedSize() const {
61bdd1243dSDimitry Andric   return metadata_->GetRequestedSize();
620b57cec5SDimitry Andric }
GetAllocStackId() const630b57cec5SDimitry Andric u32 HwasanChunkView::GetAllocStackId() const {
64bdd1243dSDimitry Andric   return metadata_->GetAllocStackId();
650b57cec5SDimitry Andric }
660b57cec5SDimitry Andric 
GetAllocThreadId() const6706c3fb27SDimitry Andric u32 HwasanChunkView::GetAllocThreadId() const {
6806c3fb27SDimitry Andric   return metadata_->GetAllocThreadId();
6906c3fb27SDimitry Andric }
7006c3fb27SDimitry Andric 
ActualSize() const710b57cec5SDimitry Andric uptr HwasanChunkView::ActualSize() const {
720b57cec5SDimitry Andric   return allocator.GetActuallyAllocatedSize(reinterpret_cast<void *>(block_));
730b57cec5SDimitry Andric }
740b57cec5SDimitry Andric 
FromSmallHeap() const750b57cec5SDimitry Andric bool HwasanChunkView::FromSmallHeap() const {
760b57cec5SDimitry Andric   return allocator.FromPrimary(reinterpret_cast<void *>(block_));
770b57cec5SDimitry Andric }
780b57cec5SDimitry Andric 
AddrIsInside(uptr addr) const79bdd1243dSDimitry Andric bool HwasanChunkView::AddrIsInside(uptr addr) const {
80bdd1243dSDimitry Andric   return (addr >= Beg()) && (addr < Beg() + UsedSize());
81bdd1243dSDimitry Andric }
82bdd1243dSDimitry Andric 
SetAllocated(u32 stack,u64 size)83bdd1243dSDimitry Andric inline void Metadata::SetAllocated(u32 stack, u64 size) {
84bdd1243dSDimitry Andric   Thread *t = GetCurrentThread();
85bdd1243dSDimitry Andric   u64 context = t ? t->unique_id() : kMainTid;
86bdd1243dSDimitry Andric   context <<= 32;
87bdd1243dSDimitry Andric   context += stack;
88bdd1243dSDimitry Andric   requested_size_low = size & ((1ul << 32) - 1);
89bdd1243dSDimitry Andric   requested_size_high = size >> 32;
90bdd1243dSDimitry Andric   atomic_store(&alloc_context_id, context, memory_order_relaxed);
91bdd1243dSDimitry Andric   atomic_store(&chunk_state, CHUNK_ALLOCATED, memory_order_release);
92bdd1243dSDimitry Andric }
93bdd1243dSDimitry Andric 
SetUnallocated()94bdd1243dSDimitry Andric inline void Metadata::SetUnallocated() {
95bdd1243dSDimitry Andric   atomic_store(&chunk_state, CHUNK_INVALID, memory_order_release);
96bdd1243dSDimitry Andric   requested_size_low = 0;
97bdd1243dSDimitry Andric   requested_size_high = 0;
98bdd1243dSDimitry Andric   atomic_store(&alloc_context_id, 0, memory_order_relaxed);
99bdd1243dSDimitry Andric }
100bdd1243dSDimitry Andric 
IsAllocated() const101bdd1243dSDimitry Andric inline bool Metadata::IsAllocated() const {
10206c3fb27SDimitry Andric   return atomic_load(&chunk_state, memory_order_relaxed) == CHUNK_ALLOCATED;
103bdd1243dSDimitry Andric }
104bdd1243dSDimitry Andric 
GetRequestedSize() const105bdd1243dSDimitry Andric inline u64 Metadata::GetRequestedSize() const {
106bdd1243dSDimitry Andric   return (static_cast<u64>(requested_size_high) << 32) + requested_size_low;
107bdd1243dSDimitry Andric }
108bdd1243dSDimitry Andric 
GetAllocStackId() const109bdd1243dSDimitry Andric inline u32 Metadata::GetAllocStackId() const {
110bdd1243dSDimitry Andric   return atomic_load(&alloc_context_id, memory_order_relaxed);
111bdd1243dSDimitry Andric }
112bdd1243dSDimitry Andric 
GetAllocThreadId() const11306c3fb27SDimitry Andric inline u32 Metadata::GetAllocThreadId() const {
11406c3fb27SDimitry Andric   u64 context = atomic_load(&alloc_context_id, memory_order_relaxed);
11506c3fb27SDimitry Andric   u32 tid = context >> 32;
11606c3fb27SDimitry Andric   return tid;
11706c3fb27SDimitry Andric }
11806c3fb27SDimitry Andric 
GetAllocatorStats(AllocatorStatCounters s)1190b57cec5SDimitry Andric void GetAllocatorStats(AllocatorStatCounters s) {
1200b57cec5SDimitry Andric   allocator.GetStats(s);
1210b57cec5SDimitry Andric }
1220b57cec5SDimitry Andric 
SetLsanTag(__lsan::ChunkTag tag)123bdd1243dSDimitry Andric inline void Metadata::SetLsanTag(__lsan::ChunkTag tag) {
124bdd1243dSDimitry Andric   lsan_tag = tag;
125bdd1243dSDimitry Andric }
126bdd1243dSDimitry Andric 
GetLsanTag() const127bdd1243dSDimitry Andric inline __lsan::ChunkTag Metadata::GetLsanTag() const {
128bdd1243dSDimitry Andric   return static_cast<__lsan::ChunkTag>(lsan_tag);
129bdd1243dSDimitry Andric }
130bdd1243dSDimitry Andric 
GetAliasRegionStart()131fe6060f1SDimitry Andric uptr GetAliasRegionStart() {
132fe6060f1SDimitry Andric #if defined(HWASAN_ALIASING_MODE)
133fe6060f1SDimitry Andric   constexpr uptr kAliasRegionOffset = 1ULL << (kTaggableRegionCheckShift - 1);
134fe6060f1SDimitry Andric   uptr AliasRegionStart =
135fe6060f1SDimitry Andric       __hwasan_shadow_memory_dynamic_address + kAliasRegionOffset;
136fe6060f1SDimitry Andric 
137fe6060f1SDimitry Andric   CHECK_EQ(AliasRegionStart >> kTaggableRegionCheckShift,
138fe6060f1SDimitry Andric            __hwasan_shadow_memory_dynamic_address >> kTaggableRegionCheckShift);
139fe6060f1SDimitry Andric   CHECK_EQ(
140fe6060f1SDimitry Andric       (AliasRegionStart + kAliasRegionOffset - 1) >> kTaggableRegionCheckShift,
141fe6060f1SDimitry Andric       __hwasan_shadow_memory_dynamic_address >> kTaggableRegionCheckShift);
142fe6060f1SDimitry Andric   return AliasRegionStart;
143fe6060f1SDimitry Andric #else
144fe6060f1SDimitry Andric   return 0;
145fe6060f1SDimitry Andric #endif
146fe6060f1SDimitry Andric }
147fe6060f1SDimitry Andric 
HwasanAllocatorInit()1480b57cec5SDimitry Andric void HwasanAllocatorInit() {
1490b57cec5SDimitry Andric   atomic_store_relaxed(&hwasan_allocator_tagging_enabled,
1500b57cec5SDimitry Andric                        !flags()->disable_allocator_tagging);
1510b57cec5SDimitry Andric   SetAllocatorMayReturnNull(common_flags()->allocator_may_return_null);
15206c3fb27SDimitry Andric   allocator.InitLinkerInitialized(
15306c3fb27SDimitry Andric       common_flags()->allocator_release_to_os_interval_ms,
154fe6060f1SDimitry Andric       GetAliasRegionStart());
1550b57cec5SDimitry Andric   for (uptr i = 0; i < sizeof(tail_magic); i++)
1560b57cec5SDimitry Andric     tail_magic[i] = GetCurrentThread()->GenerateRandomTag();
15706c3fb27SDimitry Andric   if (common_flags()->max_allocation_size_mb) {
15806c3fb27SDimitry Andric     max_malloc_size = common_flags()->max_allocation_size_mb << 20;
15906c3fb27SDimitry Andric     max_malloc_size = Min(max_malloc_size, kMaxAllowedMallocSize);
16006c3fb27SDimitry Andric   } else {
16106c3fb27SDimitry Andric     max_malloc_size = kMaxAllowedMallocSize;
16206c3fb27SDimitry Andric   }
1630b57cec5SDimitry Andric }
1640b57cec5SDimitry Andric 
HwasanAllocatorLock()165349cc55cSDimitry Andric void HwasanAllocatorLock() { allocator.ForceLock(); }
166349cc55cSDimitry Andric 
HwasanAllocatorUnlock()167349cc55cSDimitry Andric void HwasanAllocatorUnlock() { allocator.ForceUnlock(); }
168349cc55cSDimitry Andric 
AllocatorThreadStart(AllocatorCache * cache)16906c3fb27SDimitry Andric void AllocatorThreadStart(AllocatorCache *cache) { allocator.InitCache(cache); }
17006c3fb27SDimitry Andric 
AllocatorThreadFinish(AllocatorCache * cache)17106c3fb27SDimitry Andric void AllocatorThreadFinish(AllocatorCache *cache) {
1720b57cec5SDimitry Andric   allocator.SwallowCache(cache);
17306c3fb27SDimitry Andric   allocator.DestroyCache(cache);
1740b57cec5SDimitry Andric }
1750b57cec5SDimitry Andric 
TaggedSize(uptr size)1760b57cec5SDimitry Andric static uptr TaggedSize(uptr size) {
1770b57cec5SDimitry Andric   if (!size) size = 1;
1780b57cec5SDimitry Andric   uptr new_size = RoundUpTo(size, kShadowAlignment);
1790b57cec5SDimitry Andric   CHECK_GE(new_size, size);
1800b57cec5SDimitry Andric   return new_size;
1810b57cec5SDimitry Andric }
1820b57cec5SDimitry Andric 
HwasanAllocate(StackTrace * stack,uptr orig_size,uptr alignment,bool zeroise)1830b57cec5SDimitry Andric static void *HwasanAllocate(StackTrace *stack, uptr orig_size, uptr alignment,
1840b57cec5SDimitry Andric                             bool zeroise) {
18506c3fb27SDimitry Andric   // Keep this consistent with LSAN and ASAN behavior.
18606c3fb27SDimitry Andric   if (UNLIKELY(orig_size == 0))
18706c3fb27SDimitry Andric     orig_size = 1;
18806c3fb27SDimitry Andric   if (UNLIKELY(orig_size > max_malloc_size)) {
1890b57cec5SDimitry Andric     if (AllocatorMayReturnNull()) {
1900b57cec5SDimitry Andric       Report("WARNING: HWAddressSanitizer failed to allocate 0x%zx bytes\n",
1910b57cec5SDimitry Andric              orig_size);
1920b57cec5SDimitry Andric       return nullptr;
1930b57cec5SDimitry Andric     }
19406c3fb27SDimitry Andric     ReportAllocationSizeTooBig(orig_size, max_malloc_size, stack);
1950b57cec5SDimitry Andric   }
1960eae32dcSDimitry Andric   if (UNLIKELY(IsRssLimitExceeded())) {
1970eae32dcSDimitry Andric     if (AllocatorMayReturnNull())
1980eae32dcSDimitry Andric       return nullptr;
1990eae32dcSDimitry Andric     ReportRssLimitExceeded(stack);
2000eae32dcSDimitry Andric   }
2010b57cec5SDimitry Andric 
2020b57cec5SDimitry Andric   alignment = Max(alignment, kShadowAlignment);
2030b57cec5SDimitry Andric   uptr size = TaggedSize(orig_size);
2040b57cec5SDimitry Andric   Thread *t = GetCurrentThread();
2050b57cec5SDimitry Andric   void *allocated;
2060b57cec5SDimitry Andric   if (t) {
2070b57cec5SDimitry Andric     allocated = allocator.Allocate(t->allocator_cache(), size, alignment);
2080b57cec5SDimitry Andric   } else {
2090b57cec5SDimitry Andric     SpinMutexLock l(&fallback_mutex);
2100b57cec5SDimitry Andric     AllocatorCache *cache = &fallback_allocator_cache;
2110b57cec5SDimitry Andric     allocated = allocator.Allocate(cache, size, alignment);
2120b57cec5SDimitry Andric   }
2130b57cec5SDimitry Andric   if (UNLIKELY(!allocated)) {
2140b57cec5SDimitry Andric     SetAllocatorOutOfMemory();
2150b57cec5SDimitry Andric     if (AllocatorMayReturnNull())
2160b57cec5SDimitry Andric       return nullptr;
2170b57cec5SDimitry Andric     ReportOutOfMemory(size, stack);
2180b57cec5SDimitry Andric   }
2190b57cec5SDimitry Andric   if (zeroise) {
22006c3fb27SDimitry Andric     // The secondary allocator mmaps memory, which should be zero-inited so we
22106c3fb27SDimitry Andric     // don't need to explicitly clear it.
22206c3fb27SDimitry Andric     if (allocator.FromPrimary(allocated))
2230b57cec5SDimitry Andric       internal_memset(allocated, 0, size);
2240b57cec5SDimitry Andric   } else if (flags()->max_malloc_fill_size > 0) {
2250b57cec5SDimitry Andric     uptr fill_size = Min(size, (uptr)flags()->max_malloc_fill_size);
2260b57cec5SDimitry Andric     internal_memset(allocated, flags()->malloc_fill_byte, fill_size);
2270b57cec5SDimitry Andric   }
2280b57cec5SDimitry Andric   if (size != orig_size) {
229349cc55cSDimitry Andric     u8 *tail = reinterpret_cast<u8 *>(allocated) + orig_size;
230349cc55cSDimitry Andric     uptr tail_length = size - orig_size;
231349cc55cSDimitry Andric     internal_memcpy(tail, tail_magic, tail_length - 1);
232349cc55cSDimitry Andric     // Short granule is excluded from magic tail, so we explicitly untag.
233349cc55cSDimitry Andric     tail[tail_length - 1] = 0;
2340b57cec5SDimitry Andric   }
2350b57cec5SDimitry Andric 
2360b57cec5SDimitry Andric   void *user_ptr = allocated;
237fe6060f1SDimitry Andric   if (InTaggableRegion(reinterpret_cast<uptr>(user_ptr)) &&
2385f757f3fSDimitry Andric       atomic_load_relaxed(&hwasan_allocator_tagging_enabled) &&
2395f757f3fSDimitry Andric       flags()->tag_in_malloc && malloc_bisect(stack, orig_size)) {
2400b57cec5SDimitry Andric     tag_t tag = t ? t->GenerateRandomTag() : kFallbackAllocTag;
2410b57cec5SDimitry Andric     uptr tag_size = orig_size ? orig_size : 1;
2420b57cec5SDimitry Andric     uptr full_granule_size = RoundDownTo(tag_size, kShadowAlignment);
2435f757f3fSDimitry Andric     user_ptr = (void *)TagMemoryAligned((uptr)user_ptr, full_granule_size, tag);
2440b57cec5SDimitry Andric     if (full_granule_size != tag_size) {
2455f757f3fSDimitry Andric       u8 *short_granule = reinterpret_cast<u8 *>(allocated) + full_granule_size;
2460b57cec5SDimitry Andric       TagMemoryAligned((uptr)short_granule, kShadowAlignment,
2470b57cec5SDimitry Andric                        tag_size % kShadowAlignment);
2480b57cec5SDimitry Andric       short_granule[kShadowAlignment - 1] = tag;
2490b57cec5SDimitry Andric     }
2500b57cec5SDimitry Andric   } else {
2515f757f3fSDimitry Andric     // Tagging can not be completely skipped. If it's disabled, we need to tag
2525f757f3fSDimitry Andric     // with zeros.
2530b57cec5SDimitry Andric     user_ptr = (void *)TagMemoryAligned((uptr)user_ptr, size, 0);
2540b57cec5SDimitry Andric   }
2550b57cec5SDimitry Andric 
256bdd1243dSDimitry Andric   Metadata *meta =
257bdd1243dSDimitry Andric       reinterpret_cast<Metadata *>(allocator.GetMetaData(allocated));
258bdd1243dSDimitry Andric #if CAN_SANITIZE_LEAKS
259bdd1243dSDimitry Andric   meta->SetLsanTag(__lsan::DisabledInThisThread() ? __lsan::kIgnored
260bdd1243dSDimitry Andric                                                   : __lsan::kDirectlyLeaked);
261bdd1243dSDimitry Andric #endif
262bdd1243dSDimitry Andric   meta->SetAllocated(StackDepotPut(*stack), orig_size);
26306c3fb27SDimitry Andric   RunMallocHooks(user_ptr, orig_size);
2640b57cec5SDimitry Andric   return user_ptr;
2650b57cec5SDimitry Andric }
2660b57cec5SDimitry Andric 
PointerAndMemoryTagsMatch(void * tagged_ptr)2670b57cec5SDimitry Andric static bool PointerAndMemoryTagsMatch(void *tagged_ptr) {
2680b57cec5SDimitry Andric   CHECK(tagged_ptr);
2690b57cec5SDimitry Andric   uptr tagged_uptr = reinterpret_cast<uptr>(tagged_ptr);
270fe6060f1SDimitry Andric   if (!InTaggableRegion(tagged_uptr))
271fe6060f1SDimitry Andric     return true;
2720b57cec5SDimitry Andric   tag_t mem_tag = *reinterpret_cast<tag_t *>(
2730b57cec5SDimitry Andric       MemToShadow(reinterpret_cast<uptr>(UntagPtr(tagged_ptr))));
2740b57cec5SDimitry Andric   return PossiblyShortTagMatches(mem_tag, tagged_uptr, 1);
2750b57cec5SDimitry Andric }
2760b57cec5SDimitry Andric 
CheckInvalidFree(StackTrace * stack,void * untagged_ptr,void * tagged_ptr)277349cc55cSDimitry Andric static bool CheckInvalidFree(StackTrace *stack, void *untagged_ptr,
278349cc55cSDimitry Andric                              void *tagged_ptr) {
279349cc55cSDimitry Andric   // This function can return true if halt_on_error is false.
280349cc55cSDimitry Andric   if (!MemIsApp(reinterpret_cast<uptr>(untagged_ptr)) ||
281349cc55cSDimitry Andric       !PointerAndMemoryTagsMatch(tagged_ptr)) {
282349cc55cSDimitry Andric     ReportInvalidFree(stack, reinterpret_cast<uptr>(tagged_ptr));
283349cc55cSDimitry Andric     return true;
284349cc55cSDimitry Andric   }
285349cc55cSDimitry Andric   return false;
286349cc55cSDimitry Andric }
287349cc55cSDimitry Andric 
HwasanDeallocate(StackTrace * stack,void * tagged_ptr)2880b57cec5SDimitry Andric static void HwasanDeallocate(StackTrace *stack, void *tagged_ptr) {
2890b57cec5SDimitry Andric   CHECK(tagged_ptr);
29006c3fb27SDimitry Andric   void *untagged_ptr = UntagPtr(tagged_ptr);
2910b57cec5SDimitry Andric 
292*0fca6ea1SDimitry Andric   if (RunFreeHooks(tagged_ptr))
293*0fca6ea1SDimitry Andric     return;
294*0fca6ea1SDimitry Andric 
295349cc55cSDimitry Andric   if (CheckInvalidFree(stack, untagged_ptr, tagged_ptr))
296349cc55cSDimitry Andric     return;
297349cc55cSDimitry Andric 
2980b57cec5SDimitry Andric   void *aligned_ptr = reinterpret_cast<void *>(
2990b57cec5SDimitry Andric       RoundDownTo(reinterpret_cast<uptr>(untagged_ptr), kShadowAlignment));
300fe6060f1SDimitry Andric   tag_t pointer_tag = GetTagFromPointer(reinterpret_cast<uptr>(tagged_ptr));
3010b57cec5SDimitry Andric   Metadata *meta =
3020b57cec5SDimitry Andric       reinterpret_cast<Metadata *>(allocator.GetMetaData(aligned_ptr));
303349cc55cSDimitry Andric   if (!meta) {
304349cc55cSDimitry Andric     ReportInvalidFree(stack, reinterpret_cast<uptr>(tagged_ptr));
305349cc55cSDimitry Andric     return;
306349cc55cSDimitry Andric   }
30706c3fb27SDimitry Andric 
308bdd1243dSDimitry Andric   uptr orig_size = meta->GetRequestedSize();
3090b57cec5SDimitry Andric   u32 free_context_id = StackDepotPut(*stack);
310bdd1243dSDimitry Andric   u32 alloc_context_id = meta->GetAllocStackId();
31106c3fb27SDimitry Andric   u32 alloc_thread_id = meta->GetAllocThreadId();
31206c3fb27SDimitry Andric 
31306c3fb27SDimitry Andric   bool in_taggable_region =
31406c3fb27SDimitry Andric       InTaggableRegion(reinterpret_cast<uptr>(tagged_ptr));
3150b57cec5SDimitry Andric 
3160b57cec5SDimitry Andric   // Check tail magic.
3170b57cec5SDimitry Andric   uptr tagged_size = TaggedSize(orig_size);
3180b57cec5SDimitry Andric   if (flags()->free_checks_tail_magic && orig_size &&
3190b57cec5SDimitry Andric       tagged_size != orig_size) {
3200b57cec5SDimitry Andric     uptr tail_size = tagged_size - orig_size - 1;
3210b57cec5SDimitry Andric     CHECK_LT(tail_size, kShadowAlignment);
3220b57cec5SDimitry Andric     void *tail_beg = reinterpret_cast<void *>(
3230b57cec5SDimitry Andric         reinterpret_cast<uptr>(aligned_ptr) + orig_size);
324349cc55cSDimitry Andric     tag_t short_granule_memtag = *(reinterpret_cast<tag_t *>(
325349cc55cSDimitry Andric         reinterpret_cast<uptr>(tail_beg) + tail_size));
326349cc55cSDimitry Andric     if (tail_size &&
327349cc55cSDimitry Andric         (internal_memcmp(tail_beg, tail_magic, tail_size) ||
328349cc55cSDimitry Andric          (in_taggable_region && pointer_tag != short_granule_memtag)))
3290b57cec5SDimitry Andric       ReportTailOverwritten(stack, reinterpret_cast<uptr>(tagged_ptr),
3300b57cec5SDimitry Andric                             orig_size, tail_magic);
3310b57cec5SDimitry Andric   }
3320b57cec5SDimitry Andric 
333bdd1243dSDimitry Andric   // TODO(kstoimenov): consider meta->SetUnallocated(free_context_id).
334bdd1243dSDimitry Andric   meta->SetUnallocated();
3350b57cec5SDimitry Andric   // This memory will not be reused by anyone else, so we are free to keep it
3360b57cec5SDimitry Andric   // poisoned.
3370b57cec5SDimitry Andric   Thread *t = GetCurrentThread();
3380b57cec5SDimitry Andric   if (flags()->max_free_fill_size > 0) {
3390b57cec5SDimitry Andric     uptr fill_size =
3400b57cec5SDimitry Andric         Min(TaggedSize(orig_size), (uptr)flags()->max_free_fill_size);
3410b57cec5SDimitry Andric     internal_memset(aligned_ptr, flags()->free_fill_byte, fill_size);
3420b57cec5SDimitry Andric   }
343349cc55cSDimitry Andric   if (in_taggable_region && flags()->tag_in_free && malloc_bisect(stack, 0) &&
3445f757f3fSDimitry Andric       atomic_load_relaxed(&hwasan_allocator_tagging_enabled) &&
3455f757f3fSDimitry Andric       allocator.FromPrimary(untagged_ptr) /* Secondary 0-tag and unmap.*/) {
346fe6060f1SDimitry Andric     // Always store full 8-bit tags on free to maximize UAF detection.
347fe6060f1SDimitry Andric     tag_t tag;
348fe6060f1SDimitry Andric     if (t) {
349fe6060f1SDimitry Andric       // Make sure we are not using a short granule tag as a poison tag. This
350fe6060f1SDimitry Andric       // would make us attempt to read the memory on a UaF.
351fe6060f1SDimitry Andric       // The tag can be zero if tagging is disabled on this thread.
352fe6060f1SDimitry Andric       do {
353fe6060f1SDimitry Andric         tag = t->GenerateRandomTag(/*num_bits=*/8);
354fe6060f1SDimitry Andric       } while (
355fe6060f1SDimitry Andric           UNLIKELY((tag < kShadowAlignment || tag == pointer_tag) && tag != 0));
356fe6060f1SDimitry Andric     } else {
357fe6060f1SDimitry Andric       static_assert(kFallbackFreeTag >= kShadowAlignment,
358fe6060f1SDimitry Andric                     "fallback tag must not be a short granule tag.");
359fe6060f1SDimitry Andric       tag = kFallbackFreeTag;
360fe6060f1SDimitry Andric     }
3610b57cec5SDimitry Andric     TagMemoryAligned(reinterpret_cast<uptr>(aligned_ptr), TaggedSize(orig_size),
362fe6060f1SDimitry Andric                      tag);
363fe6060f1SDimitry Andric   }
3640b57cec5SDimitry Andric   if (t) {
3650b57cec5SDimitry Andric     allocator.Deallocate(t->allocator_cache(), aligned_ptr);
3660b57cec5SDimitry Andric     if (auto *ha = t->heap_allocations())
36706c3fb27SDimitry Andric       ha->push({reinterpret_cast<uptr>(tagged_ptr), alloc_thread_id,
36806c3fb27SDimitry Andric                 alloc_context_id, free_context_id,
36906c3fb27SDimitry Andric                 static_cast<u32>(orig_size)});
3700b57cec5SDimitry Andric   } else {
3710b57cec5SDimitry Andric     SpinMutexLock l(&fallback_mutex);
3720b57cec5SDimitry Andric     AllocatorCache *cache = &fallback_allocator_cache;
3730b57cec5SDimitry Andric     allocator.Deallocate(cache, aligned_ptr);
3740b57cec5SDimitry Andric   }
3750b57cec5SDimitry Andric }
3760b57cec5SDimitry Andric 
HwasanReallocate(StackTrace * stack,void * tagged_ptr_old,uptr new_size,uptr alignment)3770b57cec5SDimitry Andric static void *HwasanReallocate(StackTrace *stack, void *tagged_ptr_old,
3780b57cec5SDimitry Andric                               uptr new_size, uptr alignment) {
37906c3fb27SDimitry Andric   void *untagged_ptr_old = UntagPtr(tagged_ptr_old);
380349cc55cSDimitry Andric   if (CheckInvalidFree(stack, untagged_ptr_old, tagged_ptr_old))
381349cc55cSDimitry Andric     return nullptr;
3820b57cec5SDimitry Andric   void *tagged_ptr_new =
3830b57cec5SDimitry Andric       HwasanAllocate(stack, new_size, alignment, false /*zeroise*/);
3840b57cec5SDimitry Andric   if (tagged_ptr_old && tagged_ptr_new) {
3850b57cec5SDimitry Andric     Metadata *meta =
3860b57cec5SDimitry Andric         reinterpret_cast<Metadata *>(allocator.GetMetaData(untagged_ptr_old));
38706c3fb27SDimitry Andric     void *untagged_ptr_new = UntagPtr(tagged_ptr_new);
38806c3fb27SDimitry Andric     internal_memcpy(untagged_ptr_new, untagged_ptr_old,
389bdd1243dSDimitry Andric                     Min(new_size, static_cast<uptr>(meta->GetRequestedSize())));
3900b57cec5SDimitry Andric     HwasanDeallocate(stack, tagged_ptr_old);
3910b57cec5SDimitry Andric   }
3920b57cec5SDimitry Andric   return tagged_ptr_new;
3930b57cec5SDimitry Andric }
3940b57cec5SDimitry Andric 
HwasanCalloc(StackTrace * stack,uptr nmemb,uptr size)3950b57cec5SDimitry Andric static void *HwasanCalloc(StackTrace *stack, uptr nmemb, uptr size) {
3960b57cec5SDimitry Andric   if (UNLIKELY(CheckForCallocOverflow(size, nmemb))) {
3970b57cec5SDimitry Andric     if (AllocatorMayReturnNull())
3980b57cec5SDimitry Andric       return nullptr;
3990b57cec5SDimitry Andric     ReportCallocOverflow(nmemb, size, stack);
4000b57cec5SDimitry Andric   }
4010b57cec5SDimitry Andric   return HwasanAllocate(stack, nmemb * size, sizeof(u64), true);
4020b57cec5SDimitry Andric }
4030b57cec5SDimitry Andric 
FindHeapChunkByAddress(uptr address)4040b57cec5SDimitry Andric HwasanChunkView FindHeapChunkByAddress(uptr address) {
405349cc55cSDimitry Andric   if (!allocator.PointerIsMine(reinterpret_cast<void *>(address)))
406349cc55cSDimitry Andric     return HwasanChunkView();
4070b57cec5SDimitry Andric   void *block = allocator.GetBlockBegin(reinterpret_cast<void*>(address));
4080b57cec5SDimitry Andric   if (!block)
4090b57cec5SDimitry Andric     return HwasanChunkView();
4100b57cec5SDimitry Andric   Metadata *metadata =
4110b57cec5SDimitry Andric       reinterpret_cast<Metadata*>(allocator.GetMetaData(block));
4120b57cec5SDimitry Andric   return HwasanChunkView(reinterpret_cast<uptr>(block), metadata);
4130b57cec5SDimitry Andric }
4140b57cec5SDimitry Andric 
AllocationBegin(const void * p)41506c3fb27SDimitry Andric static const void *AllocationBegin(const void *p) {
41606c3fb27SDimitry Andric   const void *untagged_ptr = UntagPtr(p);
41706c3fb27SDimitry Andric   if (!untagged_ptr)
41806c3fb27SDimitry Andric     return nullptr;
41906c3fb27SDimitry Andric 
42006c3fb27SDimitry Andric   const void *beg = allocator.GetBlockBegin(untagged_ptr);
42106c3fb27SDimitry Andric   if (!beg)
42206c3fb27SDimitry Andric     return nullptr;
42306c3fb27SDimitry Andric 
42406c3fb27SDimitry Andric   Metadata *b = (Metadata *)allocator.GetMetaData(beg);
42506c3fb27SDimitry Andric   if (b->GetRequestedSize() == 0)
42606c3fb27SDimitry Andric     return nullptr;
42706c3fb27SDimitry Andric 
42806c3fb27SDimitry Andric   tag_t tag = GetTagFromPointer((uptr)p);
42906c3fb27SDimitry Andric   return (const void *)AddTagToPointer((uptr)beg, tag);
43006c3fb27SDimitry Andric }
43106c3fb27SDimitry Andric 
AllocationSize(const void * p)43206c3fb27SDimitry Andric static uptr AllocationSize(const void *p) {
43306c3fb27SDimitry Andric   const void *untagged_ptr = UntagPtr(p);
4340b57cec5SDimitry Andric   if (!untagged_ptr) return 0;
4350b57cec5SDimitry Andric   const void *beg = allocator.GetBlockBegin(untagged_ptr);
43606c3fb27SDimitry Andric   if (!beg)
43706c3fb27SDimitry Andric     return 0;
43806c3fb27SDimitry Andric   Metadata *b = (Metadata *)allocator.GetMetaData(beg);
439bdd1243dSDimitry Andric   return b->GetRequestedSize();
4400b57cec5SDimitry Andric }
4410b57cec5SDimitry Andric 
AllocationSizeFast(const void * p)44206c3fb27SDimitry Andric static uptr AllocationSizeFast(const void *p) {
44306c3fb27SDimitry Andric   const void *untagged_ptr = UntagPtr(p);
44406c3fb27SDimitry Andric   void *aligned_ptr = reinterpret_cast<void *>(
44506c3fb27SDimitry Andric       RoundDownTo(reinterpret_cast<uptr>(untagged_ptr), kShadowAlignment));
44606c3fb27SDimitry Andric   Metadata *meta =
44706c3fb27SDimitry Andric       reinterpret_cast<Metadata *>(allocator.GetMetaData(aligned_ptr));
44806c3fb27SDimitry Andric   return meta->GetRequestedSize();
44906c3fb27SDimitry Andric }
45006c3fb27SDimitry Andric 
hwasan_malloc(uptr size,StackTrace * stack)4510b57cec5SDimitry Andric void *hwasan_malloc(uptr size, StackTrace *stack) {
4520b57cec5SDimitry Andric   return SetErrnoOnNull(HwasanAllocate(stack, size, sizeof(u64), false));
4530b57cec5SDimitry Andric }
4540b57cec5SDimitry Andric 
hwasan_calloc(uptr nmemb,uptr size,StackTrace * stack)4550b57cec5SDimitry Andric void *hwasan_calloc(uptr nmemb, uptr size, StackTrace *stack) {
4560b57cec5SDimitry Andric   return SetErrnoOnNull(HwasanCalloc(stack, nmemb, size));
4570b57cec5SDimitry Andric }
4580b57cec5SDimitry Andric 
hwasan_realloc(void * ptr,uptr size,StackTrace * stack)4590b57cec5SDimitry Andric void *hwasan_realloc(void *ptr, uptr size, StackTrace *stack) {
4600b57cec5SDimitry Andric   if (!ptr)
4610b57cec5SDimitry Andric     return SetErrnoOnNull(HwasanAllocate(stack, size, sizeof(u64), false));
4620b57cec5SDimitry Andric   if (size == 0) {
4630b57cec5SDimitry Andric     HwasanDeallocate(stack, ptr);
4640b57cec5SDimitry Andric     return nullptr;
4650b57cec5SDimitry Andric   }
4660b57cec5SDimitry Andric   return SetErrnoOnNull(HwasanReallocate(stack, ptr, size, sizeof(u64)));
4670b57cec5SDimitry Andric }
4680b57cec5SDimitry Andric 
hwasan_reallocarray(void * ptr,uptr nmemb,uptr size,StackTrace * stack)4690b57cec5SDimitry Andric void *hwasan_reallocarray(void *ptr, uptr nmemb, uptr size, StackTrace *stack) {
4700b57cec5SDimitry Andric   if (UNLIKELY(CheckForCallocOverflow(size, nmemb))) {
4710b57cec5SDimitry Andric     errno = errno_ENOMEM;
4720b57cec5SDimitry Andric     if (AllocatorMayReturnNull())
4730b57cec5SDimitry Andric       return nullptr;
4740b57cec5SDimitry Andric     ReportReallocArrayOverflow(nmemb, size, stack);
4750b57cec5SDimitry Andric   }
4760b57cec5SDimitry Andric   return hwasan_realloc(ptr, nmemb * size, stack);
4770b57cec5SDimitry Andric }
4780b57cec5SDimitry Andric 
hwasan_valloc(uptr size,StackTrace * stack)4790b57cec5SDimitry Andric void *hwasan_valloc(uptr size, StackTrace *stack) {
4800b57cec5SDimitry Andric   return SetErrnoOnNull(
4810b57cec5SDimitry Andric       HwasanAllocate(stack, size, GetPageSizeCached(), false));
4820b57cec5SDimitry Andric }
4830b57cec5SDimitry Andric 
hwasan_pvalloc(uptr size,StackTrace * stack)4840b57cec5SDimitry Andric void *hwasan_pvalloc(uptr size, StackTrace *stack) {
4850b57cec5SDimitry Andric   uptr PageSize = GetPageSizeCached();
4860b57cec5SDimitry Andric   if (UNLIKELY(CheckForPvallocOverflow(size, PageSize))) {
4870b57cec5SDimitry Andric     errno = errno_ENOMEM;
4880b57cec5SDimitry Andric     if (AllocatorMayReturnNull())
4890b57cec5SDimitry Andric       return nullptr;
4900b57cec5SDimitry Andric     ReportPvallocOverflow(size, stack);
4910b57cec5SDimitry Andric   }
4920b57cec5SDimitry Andric   // pvalloc(0) should allocate one page.
4930b57cec5SDimitry Andric   size = size ? RoundUpTo(size, PageSize) : PageSize;
4940b57cec5SDimitry Andric   return SetErrnoOnNull(HwasanAllocate(stack, size, PageSize, false));
4950b57cec5SDimitry Andric }
4960b57cec5SDimitry Andric 
hwasan_aligned_alloc(uptr alignment,uptr size,StackTrace * stack)4970b57cec5SDimitry Andric void *hwasan_aligned_alloc(uptr alignment, uptr size, StackTrace *stack) {
4980b57cec5SDimitry Andric   if (UNLIKELY(!CheckAlignedAllocAlignmentAndSize(alignment, size))) {
4990b57cec5SDimitry Andric     errno = errno_EINVAL;
5000b57cec5SDimitry Andric     if (AllocatorMayReturnNull())
5010b57cec5SDimitry Andric       return nullptr;
5020b57cec5SDimitry Andric     ReportInvalidAlignedAllocAlignment(size, alignment, stack);
5030b57cec5SDimitry Andric   }
5040b57cec5SDimitry Andric   return SetErrnoOnNull(HwasanAllocate(stack, size, alignment, false));
5050b57cec5SDimitry Andric }
5060b57cec5SDimitry Andric 
hwasan_memalign(uptr alignment,uptr size,StackTrace * stack)5070b57cec5SDimitry Andric void *hwasan_memalign(uptr alignment, uptr size, StackTrace *stack) {
5080b57cec5SDimitry Andric   if (UNLIKELY(!IsPowerOfTwo(alignment))) {
5090b57cec5SDimitry Andric     errno = errno_EINVAL;
5100b57cec5SDimitry Andric     if (AllocatorMayReturnNull())
5110b57cec5SDimitry Andric       return nullptr;
5120b57cec5SDimitry Andric     ReportInvalidAllocationAlignment(alignment, stack);
5130b57cec5SDimitry Andric   }
5140b57cec5SDimitry Andric   return SetErrnoOnNull(HwasanAllocate(stack, size, alignment, false));
5150b57cec5SDimitry Andric }
5160b57cec5SDimitry Andric 
hwasan_posix_memalign(void ** memptr,uptr alignment,uptr size,StackTrace * stack)5170b57cec5SDimitry Andric int hwasan_posix_memalign(void **memptr, uptr alignment, uptr size,
5180b57cec5SDimitry Andric                         StackTrace *stack) {
5190b57cec5SDimitry Andric   if (UNLIKELY(!CheckPosixMemalignAlignment(alignment))) {
5200b57cec5SDimitry Andric     if (AllocatorMayReturnNull())
5210b57cec5SDimitry Andric       return errno_EINVAL;
5220b57cec5SDimitry Andric     ReportInvalidPosixMemalignAlignment(alignment, stack);
5230b57cec5SDimitry Andric   }
5240b57cec5SDimitry Andric   void *ptr = HwasanAllocate(stack, size, alignment, false);
5250b57cec5SDimitry Andric   if (UNLIKELY(!ptr))
5260b57cec5SDimitry Andric     // OOM error is already taken care of by HwasanAllocate.
5270b57cec5SDimitry Andric     return errno_ENOMEM;
5280b57cec5SDimitry Andric   CHECK(IsAligned((uptr)ptr, alignment));
529fe6060f1SDimitry Andric   *memptr = ptr;
5300b57cec5SDimitry Andric   return 0;
5310b57cec5SDimitry Andric }
5320b57cec5SDimitry Andric 
hwasan_free(void * ptr,StackTrace * stack)5330b57cec5SDimitry Andric void hwasan_free(void *ptr, StackTrace *stack) {
5340b57cec5SDimitry Andric   return HwasanDeallocate(stack, ptr);
5350b57cec5SDimitry Andric }
5360b57cec5SDimitry Andric 
5370b57cec5SDimitry Andric }  // namespace __hwasan
5380b57cec5SDimitry Andric 
539bdd1243dSDimitry Andric // --- Implementation of LSan-specific functions --- {{{1
540bdd1243dSDimitry Andric namespace __lsan {
541bdd1243dSDimitry Andric 
LockAllocator()542bdd1243dSDimitry Andric void LockAllocator() {
543bdd1243dSDimitry Andric   __hwasan::HwasanAllocatorLock();
544bdd1243dSDimitry Andric }
545bdd1243dSDimitry Andric 
UnlockAllocator()546bdd1243dSDimitry Andric void UnlockAllocator() {
547bdd1243dSDimitry Andric   __hwasan::HwasanAllocatorUnlock();
548bdd1243dSDimitry Andric }
549bdd1243dSDimitry Andric 
GetAllocatorGlobalRange(uptr * begin,uptr * end)550bdd1243dSDimitry Andric void GetAllocatorGlobalRange(uptr *begin, uptr *end) {
551bdd1243dSDimitry Andric   *begin = (uptr)&__hwasan::allocator;
552bdd1243dSDimitry Andric   *end = *begin + sizeof(__hwasan::allocator);
553bdd1243dSDimitry Andric }
554bdd1243dSDimitry Andric 
PointsIntoChunk(void * p)555bdd1243dSDimitry Andric uptr PointsIntoChunk(void *p) {
55606c3fb27SDimitry Andric   p = UntagPtr(p);
557bdd1243dSDimitry Andric   uptr addr = reinterpret_cast<uptr>(p);
558bdd1243dSDimitry Andric   uptr chunk =
559bdd1243dSDimitry Andric       reinterpret_cast<uptr>(__hwasan::allocator.GetBlockBeginFastLocked(p));
560bdd1243dSDimitry Andric   if (!chunk)
561bdd1243dSDimitry Andric     return 0;
562bdd1243dSDimitry Andric   __hwasan::Metadata *metadata = reinterpret_cast<__hwasan::Metadata *>(
563bdd1243dSDimitry Andric       __hwasan::allocator.GetMetaData(reinterpret_cast<void *>(chunk)));
564bdd1243dSDimitry Andric   if (!metadata || !metadata->IsAllocated())
565bdd1243dSDimitry Andric     return 0;
566bdd1243dSDimitry Andric   if (addr < chunk + metadata->GetRequestedSize())
567bdd1243dSDimitry Andric     return chunk;
568bdd1243dSDimitry Andric   if (IsSpecialCaseOfOperatorNew0(chunk, metadata->GetRequestedSize(), addr))
569bdd1243dSDimitry Andric     return chunk;
570bdd1243dSDimitry Andric   return 0;
571bdd1243dSDimitry Andric }
572bdd1243dSDimitry Andric 
GetUserBegin(uptr chunk)573bdd1243dSDimitry Andric uptr GetUserBegin(uptr chunk) {
574bdd1243dSDimitry Andric   CHECK_EQ(UntagAddr(chunk), chunk);
575bdd1243dSDimitry Andric   void *block = __hwasan::allocator.GetBlockBeginFastLocked(
576bdd1243dSDimitry Andric       reinterpret_cast<void *>(chunk));
577bdd1243dSDimitry Andric   if (!block)
578bdd1243dSDimitry Andric     return 0;
579bdd1243dSDimitry Andric   __hwasan::Metadata *metadata = reinterpret_cast<__hwasan::Metadata *>(
580bdd1243dSDimitry Andric       __hwasan::allocator.GetMetaData(block));
581bdd1243dSDimitry Andric   if (!metadata || !metadata->IsAllocated())
582bdd1243dSDimitry Andric     return 0;
583bdd1243dSDimitry Andric 
584bdd1243dSDimitry Andric   return reinterpret_cast<uptr>(block);
585bdd1243dSDimitry Andric }
586bdd1243dSDimitry Andric 
GetUserAddr(uptr chunk)58706c3fb27SDimitry Andric uptr GetUserAddr(uptr chunk) {
58806c3fb27SDimitry Andric   if (!InTaggableRegion(chunk))
58906c3fb27SDimitry Andric     return chunk;
59006c3fb27SDimitry Andric   tag_t mem_tag = *(tag_t *)__hwasan::MemToShadow(chunk);
59106c3fb27SDimitry Andric   return AddTagToPointer(chunk, mem_tag);
59206c3fb27SDimitry Andric }
59306c3fb27SDimitry Andric 
LsanMetadata(uptr chunk)594bdd1243dSDimitry Andric LsanMetadata::LsanMetadata(uptr chunk) {
595bdd1243dSDimitry Andric   CHECK_EQ(UntagAddr(chunk), chunk);
596bdd1243dSDimitry Andric   metadata_ =
597bdd1243dSDimitry Andric       chunk ? __hwasan::allocator.GetMetaData(reinterpret_cast<void *>(chunk))
598bdd1243dSDimitry Andric             : nullptr;
599bdd1243dSDimitry Andric }
600bdd1243dSDimitry Andric 
allocated() const601bdd1243dSDimitry Andric bool LsanMetadata::allocated() const {
602bdd1243dSDimitry Andric   if (!metadata_)
603bdd1243dSDimitry Andric     return false;
604bdd1243dSDimitry Andric   __hwasan::Metadata *m = reinterpret_cast<__hwasan::Metadata *>(metadata_);
605bdd1243dSDimitry Andric   return m->IsAllocated();
606bdd1243dSDimitry Andric }
607bdd1243dSDimitry Andric 
tag() const608bdd1243dSDimitry Andric ChunkTag LsanMetadata::tag() const {
609bdd1243dSDimitry Andric   __hwasan::Metadata *m = reinterpret_cast<__hwasan::Metadata *>(metadata_);
610bdd1243dSDimitry Andric   return m->GetLsanTag();
611bdd1243dSDimitry Andric }
612bdd1243dSDimitry Andric 
set_tag(ChunkTag value)613bdd1243dSDimitry Andric void LsanMetadata::set_tag(ChunkTag value) {
614bdd1243dSDimitry Andric   __hwasan::Metadata *m = reinterpret_cast<__hwasan::Metadata *>(metadata_);
615bdd1243dSDimitry Andric   m->SetLsanTag(value);
616bdd1243dSDimitry Andric }
617bdd1243dSDimitry Andric 
requested_size() const618bdd1243dSDimitry Andric uptr LsanMetadata::requested_size() const {
619bdd1243dSDimitry Andric   __hwasan::Metadata *m = reinterpret_cast<__hwasan::Metadata *>(metadata_);
620bdd1243dSDimitry Andric   return m->GetRequestedSize();
621bdd1243dSDimitry Andric }
622bdd1243dSDimitry Andric 
stack_trace_id() const623bdd1243dSDimitry Andric u32 LsanMetadata::stack_trace_id() const {
624bdd1243dSDimitry Andric   __hwasan::Metadata *m = reinterpret_cast<__hwasan::Metadata *>(metadata_);
625bdd1243dSDimitry Andric   return m->GetAllocStackId();
626bdd1243dSDimitry Andric }
627bdd1243dSDimitry Andric 
ForEachChunk(ForEachChunkCallback callback,void * arg)628bdd1243dSDimitry Andric void ForEachChunk(ForEachChunkCallback callback, void *arg) {
629bdd1243dSDimitry Andric   __hwasan::allocator.ForEachChunk(callback, arg);
630bdd1243dSDimitry Andric }
631bdd1243dSDimitry Andric 
IgnoreObject(const void * p)63206c3fb27SDimitry Andric IgnoreObjectResult IgnoreObject(const void *p) {
63306c3fb27SDimitry Andric   p = UntagPtr(p);
634bdd1243dSDimitry Andric   uptr addr = reinterpret_cast<uptr>(p);
63506c3fb27SDimitry Andric   uptr chunk = reinterpret_cast<uptr>(__hwasan::allocator.GetBlockBegin(p));
636bdd1243dSDimitry Andric   if (!chunk)
637bdd1243dSDimitry Andric     return kIgnoreObjectInvalid;
638bdd1243dSDimitry Andric   __hwasan::Metadata *metadata = reinterpret_cast<__hwasan::Metadata *>(
639bdd1243dSDimitry Andric       __hwasan::allocator.GetMetaData(reinterpret_cast<void *>(chunk)));
640bdd1243dSDimitry Andric   if (!metadata || !metadata->IsAllocated())
641bdd1243dSDimitry Andric     return kIgnoreObjectInvalid;
642bdd1243dSDimitry Andric   if (addr >= chunk + metadata->GetRequestedSize())
643bdd1243dSDimitry Andric     return kIgnoreObjectInvalid;
644bdd1243dSDimitry Andric   if (metadata->GetLsanTag() == kIgnored)
645bdd1243dSDimitry Andric     return kIgnoreObjectAlreadyIgnored;
646bdd1243dSDimitry Andric 
647bdd1243dSDimitry Andric   metadata->SetLsanTag(kIgnored);
648bdd1243dSDimitry Andric   return kIgnoreObjectSuccess;
649bdd1243dSDimitry Andric }
650bdd1243dSDimitry Andric 
651bdd1243dSDimitry Andric }  // namespace __lsan
652bdd1243dSDimitry Andric 
6530b57cec5SDimitry Andric using namespace __hwasan;
6540b57cec5SDimitry Andric 
__hwasan_enable_allocator_tagging()6550b57cec5SDimitry Andric void __hwasan_enable_allocator_tagging() {
6560b57cec5SDimitry Andric   atomic_store_relaxed(&hwasan_allocator_tagging_enabled, 1);
6570b57cec5SDimitry Andric }
6580b57cec5SDimitry Andric 
__hwasan_disable_allocator_tagging()6590b57cec5SDimitry Andric void __hwasan_disable_allocator_tagging() {
6600b57cec5SDimitry Andric   atomic_store_relaxed(&hwasan_allocator_tagging_enabled, 0);
6610b57cec5SDimitry Andric }
6620b57cec5SDimitry Andric 
__sanitizer_get_current_allocated_bytes()6630b57cec5SDimitry Andric uptr __sanitizer_get_current_allocated_bytes() {
6640b57cec5SDimitry Andric   uptr stats[AllocatorStatCount];
6650b57cec5SDimitry Andric   allocator.GetStats(stats);
6660b57cec5SDimitry Andric   return stats[AllocatorStatAllocated];
6670b57cec5SDimitry Andric }
6680b57cec5SDimitry Andric 
__sanitizer_get_heap_size()6690b57cec5SDimitry Andric uptr __sanitizer_get_heap_size() {
6700b57cec5SDimitry Andric   uptr stats[AllocatorStatCount];
6710b57cec5SDimitry Andric   allocator.GetStats(stats);
6720b57cec5SDimitry Andric   return stats[AllocatorStatMapped];
6730b57cec5SDimitry Andric }
6740b57cec5SDimitry Andric 
__sanitizer_get_free_bytes()6750b57cec5SDimitry Andric uptr __sanitizer_get_free_bytes() { return 1; }
6760b57cec5SDimitry Andric 
__sanitizer_get_unmapped_bytes()6770b57cec5SDimitry Andric uptr __sanitizer_get_unmapped_bytes() { return 1; }
6780b57cec5SDimitry Andric 
__sanitizer_get_estimated_allocated_size(uptr size)6790b57cec5SDimitry Andric uptr __sanitizer_get_estimated_allocated_size(uptr size) { return size; }
6800b57cec5SDimitry Andric 
__sanitizer_get_ownership(const void * p)6810b57cec5SDimitry Andric int __sanitizer_get_ownership(const void *p) { return AllocationSize(p) != 0; }
6820b57cec5SDimitry Andric 
__sanitizer_get_allocated_begin(const void * p)68306c3fb27SDimitry Andric const void *__sanitizer_get_allocated_begin(const void *p) {
68406c3fb27SDimitry Andric   return AllocationBegin(p);
68506c3fb27SDimitry Andric }
68606c3fb27SDimitry Andric 
__sanitizer_get_allocated_size(const void * p)6870b57cec5SDimitry Andric uptr __sanitizer_get_allocated_size(const void *p) { return AllocationSize(p); }
68806c3fb27SDimitry Andric 
__sanitizer_get_allocated_size_fast(const void * p)68906c3fb27SDimitry Andric uptr __sanitizer_get_allocated_size_fast(const void *p) {
69006c3fb27SDimitry Andric   DCHECK_EQ(p, __sanitizer_get_allocated_begin(p));
69106c3fb27SDimitry Andric   uptr ret = AllocationSizeFast(p);
69206c3fb27SDimitry Andric   DCHECK_EQ(ret, __sanitizer_get_allocated_size(p));
69306c3fb27SDimitry Andric   return ret;
69406c3fb27SDimitry Andric }
69506c3fb27SDimitry Andric 
__sanitizer_purge_allocator()69606c3fb27SDimitry Andric void __sanitizer_purge_allocator() { allocator.ForceReleaseToOS(); }
697