Path: blob/main/contrib/llvm-project/compiler-rt/lib/hwasan/hwasan_report.cpp
35235 views
//===-- hwasan_report.cpp -------------------------------------------------===//1//2// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.3// See https://llvm.org/LICENSE.txt for license information.4// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception5//6//===----------------------------------------------------------------------===//7//8// This file is a part of HWAddressSanitizer.9//10// Error reporting.11//===----------------------------------------------------------------------===//1213#include "hwasan_report.h"1415#include <dlfcn.h>1617#include "hwasan.h"18#include "hwasan_allocator.h"19#include "hwasan_globals.h"20#include "hwasan_mapping.h"21#include "hwasan_thread.h"22#include "hwasan_thread_list.h"23#include "sanitizer_common/sanitizer_allocator_internal.h"24#include "sanitizer_common/sanitizer_array_ref.h"25#include "sanitizer_common/sanitizer_common.h"26#include "sanitizer_common/sanitizer_flags.h"27#include "sanitizer_common/sanitizer_internal_defs.h"28#include "sanitizer_common/sanitizer_mutex.h"29#include "sanitizer_common/sanitizer_placement_new.h"30#include "sanitizer_common/sanitizer_report_decorator.h"31#include "sanitizer_common/sanitizer_stackdepot.h"32#include "sanitizer_common/sanitizer_stacktrace_printer.h"33#include "sanitizer_common/sanitizer_symbolizer.h"3435using namespace __sanitizer;3637namespace __hwasan {3839class ScopedReport {40public:41explicit ScopedReport(bool fatal) : fatal(fatal) {42Lock lock(&error_message_lock_);43error_message_ptr_ = &error_message_;44++hwasan_report_count;45}4647~ScopedReport() {48void (*report_cb)(const char *);49{50Lock lock(&error_message_lock_);51report_cb = error_report_callback_;52error_message_ptr_ = nullptr;53}54if (report_cb)55report_cb(error_message_.data());56if (fatal)57SetAbortMessage(error_message_.data());58if (common_flags()->print_module_map >= 2 ||59(fatal && common_flags()->print_module_map))60DumpProcessMap();61if (fatal)62Die();63}6465static void MaybeAppendToErrorMessage(const char *msg) {66Lock lock(&error_message_lock_);67if (!error_message_ptr_)68return;69error_message_ptr_->Append(msg);70}7172static void SetErrorReportCallback(void (*callback)(const char *)) {73Lock lock(&error_message_lock_);74error_report_callback_ = callback;75}7677private:78InternalScopedString error_message_;79bool fatal;8081static Mutex error_message_lock_;82static InternalScopedString *error_message_ptr_83SANITIZER_GUARDED_BY(error_message_lock_);84static void (*error_report_callback_)(const char *);85};8687Mutex ScopedReport::error_message_lock_;88InternalScopedString *ScopedReport::error_message_ptr_;89void (*ScopedReport::error_report_callback_)(const char *);9091// If there is an active ScopedReport, append to its error message.92void AppendToErrorMessageBuffer(const char *buffer) {93ScopedReport::MaybeAppendToErrorMessage(buffer);94}9596static StackTrace GetStackTraceFromId(u32 id) {97CHECK(id);98StackTrace res = StackDepotGet(id);99CHECK(res.trace);100return res;101}102103static void MaybePrintAndroidHelpUrl() {104#if SANITIZER_ANDROID105Printf(106"Learn more about HWASan reports: "107"https://source.android.com/docs/security/test/memory-safety/"108"hwasan-reports\n");109#endif110}111112namespace {113// A RAII object that holds a copy of the current thread stack ring buffer.114// The actual stack buffer may change while we are iterating over it (for115// example, Printf may call syslog() which can itself be built with hwasan).116class SavedStackAllocations {117public:118SavedStackAllocations() = default;119120explicit SavedStackAllocations(Thread *t) { CopyFrom(t); }121122void CopyFrom(Thread *t) {123StackAllocationsRingBuffer *rb = t->stack_allocations();124uptr size = rb->size() * sizeof(uptr);125void *storage =126MmapAlignedOrDieOnFatalError(size, size * 2, "saved stack allocations");127new (&rb_) StackAllocationsRingBuffer(*rb, storage);128thread_id_ = t->unique_id();129}130131~SavedStackAllocations() {132if (rb_) {133StackAllocationsRingBuffer *rb = get();134UnmapOrDie(rb->StartOfStorage(), rb->size() * sizeof(uptr));135}136}137138const StackAllocationsRingBuffer *get() const {139return (const StackAllocationsRingBuffer *)&rb_;140}141142StackAllocationsRingBuffer *get() {143return (StackAllocationsRingBuffer *)&rb_;144}145146u32 thread_id() const { return thread_id_; }147148private:149uptr rb_ = 0;150u32 thread_id_;151};152153class Decorator: public __sanitizer::SanitizerCommonDecorator {154public:155Decorator() : SanitizerCommonDecorator() { }156const char *Access() { return Blue(); }157const char *Allocation() const { return Magenta(); }158const char *Origin() const { return Magenta(); }159const char *Name() const { return Green(); }160const char *Location() { return Green(); }161const char *Thread() { return Green(); }162};163} // namespace164165static bool FindHeapAllocation(HeapAllocationsRingBuffer *rb, uptr tagged_addr,166HeapAllocationRecord *har, uptr *ring_index,167uptr *num_matching_addrs,168uptr *num_matching_addrs_4b) {169if (!rb) return false;170171*num_matching_addrs = 0;172*num_matching_addrs_4b = 0;173for (uptr i = 0, size = rb->size(); i < size; i++) {174auto h = (*rb)[i];175if (h.tagged_addr <= tagged_addr &&176h.tagged_addr + h.requested_size > tagged_addr) {177*har = h;178*ring_index = i;179return true;180}181182// Measure the number of heap ring buffer entries that would have matched183// if we had only one entry per address (e.g. if the ring buffer data was184// stored at the address itself). This will help us tune the allocator185// implementation for MTE.186if (UntagAddr(h.tagged_addr) <= UntagAddr(tagged_addr) &&187UntagAddr(h.tagged_addr) + h.requested_size > UntagAddr(tagged_addr)) {188++*num_matching_addrs;189}190191// Measure the number of heap ring buffer entries that would have matched192// if we only had 4 tag bits, which is the case for MTE.193auto untag_4b = [](uptr p) {194return p & ((1ULL << 60) - 1);195};196if (untag_4b(h.tagged_addr) <= untag_4b(tagged_addr) &&197untag_4b(h.tagged_addr) + h.requested_size > untag_4b(tagged_addr)) {198++*num_matching_addrs_4b;199}200}201return false;202}203204static void PrintStackAllocations(const StackAllocationsRingBuffer *sa,205tag_t addr_tag, uptr untagged_addr) {206uptr frames = Min((uptr)flags()->stack_history_size, sa->size());207bool found_local = false;208InternalScopedString location;209for (uptr i = 0; i < frames; i++) {210const uptr *record_addr = &(*sa)[i];211uptr record = *record_addr;212if (!record)213break;214tag_t base_tag =215reinterpret_cast<uptr>(record_addr) >> kRecordAddrBaseTagShift;216const uptr fp = (record >> kRecordFPShift) << kRecordFPLShift;217CHECK_LT(fp, kRecordFPModulus);218uptr pc_mask = (1ULL << kRecordFPShift) - 1;219uptr pc = record & pc_mask;220FrameInfo frame;221if (!Symbolizer::GetOrInit()->SymbolizeFrame(pc, &frame))222continue;223for (LocalInfo &local : frame.locals) {224if (!local.has_frame_offset || !local.has_size || !local.has_tag_offset)225continue;226if (!(local.name && internal_strlen(local.name)) &&227!(local.function_name && internal_strlen(local.function_name)) &&228!(local.decl_file && internal_strlen(local.decl_file)))229continue;230tag_t obj_tag = base_tag ^ local.tag_offset;231if (obj_tag != addr_tag)232continue;233234// We only store bits 4-19 of FP (bits 0-3 are guaranteed to be zero).235// So we know only `FP % kRecordFPModulus`, and we can only calculate236// `local_beg % kRecordFPModulus`.237// Out of all possible `local_beg` we will only consider 2 candidates238// nearest to the `untagged_addr`.239uptr local_beg_mod = (fp + local.frame_offset) % kRecordFPModulus;240// Pick `local_beg` in the same 1 MiB block as `untagged_addr`.241uptr local_beg =242RoundDownTo(untagged_addr, kRecordFPModulus) + local_beg_mod;243// Pick the largest `local_beg <= untagged_addr`. It's either the current244// one or the one before.245if (local_beg > untagged_addr)246local_beg -= kRecordFPModulus;247248uptr offset = -1ull;249const char *whence;250const char *cause = nullptr;251uptr best_beg;252253// Try two 1 MiB blocks options and pick nearest one.254for (uptr i = 0; i < 2; ++i, local_beg += kRecordFPModulus) {255uptr local_end = local_beg + local.size;256if (local_beg > local_end)257continue; // This is a wraparound.258if (local_beg <= untagged_addr && untagged_addr < local_end) {259offset = untagged_addr - local_beg;260whence = "inside";261cause = "use-after-scope";262best_beg = local_beg;263break; // This is as close at it can be.264}265266if (untagged_addr >= local_end) {267uptr new_offset = untagged_addr - local_end;268if (new_offset < offset) {269offset = new_offset;270whence = "after";271cause = "stack-buffer-overflow";272best_beg = local_beg;273}274} else {275uptr new_offset = local_beg - untagged_addr;276if (new_offset < offset) {277offset = new_offset;278whence = "before";279cause = "stack-buffer-overflow";280best_beg = local_beg;281}282}283}284285// To fail the `untagged_addr` must be near nullptr, which is impossible286// with Linux user space memory layout.287if (!cause)288continue;289290if (!found_local) {291Printf("\nPotentially referenced stack objects:\n");292found_local = true;293}294295Decorator d;296Printf("%s", d.Error());297Printf("Cause: %s\n", cause);298Printf("%s", d.Default());299Printf("%s", d.Location());300StackTracePrinter::GetOrInit()->RenderSourceLocation(301&location, local.decl_file, local.decl_line, /* column= */ 0,302common_flags()->symbolize_vs_style,303common_flags()->strip_path_prefix);304Printf(305"%p is located %zd bytes %s a %zd-byte local variable %s "306"[%p,%p) "307"in %s %s\n",308untagged_addr, offset, whence, local.size, local.name, best_beg,309best_beg + local.size, local.function_name, location.data());310location.clear();311Printf("%s\n", d.Default());312}313frame.Clear();314}315316if (found_local)317return;318319// We didn't find any locals. Most likely we don't have symbols, so dump320// the information that we have for offline analysis.321InternalScopedString frame_desc;322Printf("Previously allocated frames:\n");323for (uptr i = 0; i < frames; i++) {324const uptr *record_addr = &(*sa)[i];325uptr record = *record_addr;326if (!record)327break;328uptr pc_mask = (1ULL << 48) - 1;329uptr pc = record & pc_mask;330frame_desc.AppendF(" record_addr:%p record:0x%zx",331reinterpret_cast<const void *>(record_addr), record);332SymbolizedStackHolder symbolized_stack(333Symbolizer::GetOrInit()->SymbolizePC(pc));334const SymbolizedStack *frame = symbolized_stack.get();335if (frame) {336StackTracePrinter::GetOrInit()->RenderFrame(337&frame_desc, " %F %L", 0, frame->info.address, &frame->info,338common_flags()->symbolize_vs_style,339common_flags()->strip_path_prefix);340}341Printf("%s\n", frame_desc.data());342frame_desc.clear();343}344}345346// Returns true if tag == *tag_ptr, reading tags from short granules if347// necessary. This may return a false positive if tags 1-15 are used as a348// regular tag rather than a short granule marker.349static bool TagsEqual(tag_t tag, tag_t *tag_ptr) {350if (tag == *tag_ptr)351return true;352if (*tag_ptr == 0 || *tag_ptr > kShadowAlignment - 1)353return false;354uptr mem = ShadowToMem(reinterpret_cast<uptr>(tag_ptr));355tag_t inline_tag = *reinterpret_cast<tag_t *>(mem + kShadowAlignment - 1);356return tag == inline_tag;357}358359// HWASan globals store the size of the global in the descriptor. In cases where360// we don't have a binary with symbols, we can't grab the size of the global361// from the debug info - but we might be able to retrieve it from the362// descriptor. Returns zero if the lookup failed.363static uptr GetGlobalSizeFromDescriptor(uptr ptr) {364// Find the ELF object that this global resides in.365Dl_info info;366if (dladdr(reinterpret_cast<void *>(ptr), &info) == 0)367return 0;368auto *ehdr = reinterpret_cast<const ElfW(Ehdr) *>(info.dli_fbase);369auto *phdr_begin = reinterpret_cast<const ElfW(Phdr) *>(370reinterpret_cast<const u8 *>(ehdr) + ehdr->e_phoff);371372// Get the load bias. This is normally the same as the dli_fbase address on373// position-independent code, but can be different on non-PIE executables,374// binaries using LLD's partitioning feature, or binaries compiled with a375// linker script.376ElfW(Addr) load_bias = 0;377for (const auto &phdr :378ArrayRef<const ElfW(Phdr)>(phdr_begin, phdr_begin + ehdr->e_phnum)) {379if (phdr.p_type != PT_LOAD || phdr.p_offset != 0)380continue;381load_bias = reinterpret_cast<ElfW(Addr)>(ehdr) - phdr.p_vaddr;382break;383}384385// Walk all globals in this ELF object, looking for the one we're interested386// in. Once we find it, we can stop iterating and return the size of the387// global we're interested in.388for (const hwasan_global &global :389HwasanGlobalsFor(load_bias, phdr_begin, ehdr->e_phnum))390if (global.addr() <= ptr && ptr < global.addr() + global.size())391return global.size();392393return 0;394}395396void ReportStats() {}397398constexpr uptr kDumpWidth = 16;399constexpr uptr kShadowLines = 17;400constexpr uptr kShadowDumpSize = kShadowLines * kDumpWidth;401402constexpr uptr kShortLines = 3;403constexpr uptr kShortDumpSize = kShortLines * kDumpWidth;404constexpr uptr kShortDumpOffset = (kShadowLines - kShortLines) / 2 * kDumpWidth;405406static uptr GetPrintTagStart(uptr addr) {407addr = MemToShadow(addr);408addr = RoundDownTo(addr, kDumpWidth);409addr -= kDumpWidth * (kShadowLines / 2);410return addr;411}412413template <typename PrintTag>414static void PrintTagInfoAroundAddr(uptr addr, uptr num_rows,415InternalScopedString &s,416PrintTag print_tag) {417uptr center_row_beg = RoundDownTo(addr, kDumpWidth);418uptr beg_row = center_row_beg - kDumpWidth * (num_rows / 2);419uptr end_row = center_row_beg + kDumpWidth * ((num_rows + 1) / 2);420for (uptr row = beg_row; row < end_row; row += kDumpWidth) {421s.Append(row == center_row_beg ? "=>" : " ");422s.AppendF("%p:", (void *)ShadowToMem(row));423for (uptr i = 0; i < kDumpWidth; i++) {424s.Append(row + i == addr ? "[" : " ");425print_tag(s, row + i);426s.Append(row + i == addr ? "]" : " ");427}428s.Append("\n");429}430}431432template <typename GetTag, typename GetShortTag>433static void PrintTagsAroundAddr(uptr addr, GetTag get_tag,434GetShortTag get_short_tag) {435InternalScopedString s;436addr = MemToShadow(addr);437s.AppendF(438"\nMemory tags around the buggy address (one tag corresponds to %zd "439"bytes):\n",440kShadowAlignment);441PrintTagInfoAroundAddr(addr, kShadowLines, s,442[&](InternalScopedString &s, uptr tag_addr) {443tag_t tag = get_tag(tag_addr);444s.AppendF("%02x", tag);445});446447s.AppendF(448"Tags for short granules around the buggy address (one tag corresponds "449"to %zd bytes):\n",450kShadowAlignment);451PrintTagInfoAroundAddr(addr, kShortLines, s,452[&](InternalScopedString &s, uptr tag_addr) {453tag_t tag = get_tag(tag_addr);454if (tag >= 1 && tag <= kShadowAlignment) {455tag_t short_tag = get_short_tag(tag_addr);456s.AppendF("%02x", short_tag);457} else {458s.Append("..");459}460});461s.Append(462"See "463"https://clang.llvm.org/docs/"464"HardwareAssistedAddressSanitizerDesign.html#short-granules for a "465"description of short granule tags\n");466Printf("%s", s.data());467}468469static uptr GetTopPc(const StackTrace *stack) {470return stack->size ? StackTrace::GetPreviousInstructionPc(stack->trace[0])471: 0;472}473474namespace {475class BaseReport {476public:477BaseReport(StackTrace *stack, bool fatal, uptr tagged_addr, uptr access_size)478: scoped_report(fatal),479stack(stack),480tagged_addr(tagged_addr),481access_size(access_size),482untagged_addr(UntagAddr(tagged_addr)),483ptr_tag(GetTagFromPointer(tagged_addr)),484mismatch_offset(FindMismatchOffset()),485heap(CopyHeapChunk()),486allocations(CopyAllocations()),487candidate(FindBufferOverflowCandidate()),488shadow(CopyShadow()) {}489490protected:491struct OverflowCandidate {492uptr untagged_addr = 0;493bool after = false;494bool is_close = false;495496struct {497uptr begin = 0;498uptr end = 0;499u32 thread_id = 0;500u32 stack_id = 0;501bool is_allocated = false;502} heap;503};504505struct HeapAllocation {506HeapAllocationRecord har = {};507uptr ring_index = 0;508uptr num_matching_addrs = 0;509uptr num_matching_addrs_4b = 0;510u32 free_thread_id = 0;511};512513struct Allocations {514ArrayRef<SavedStackAllocations> stack;515ArrayRef<HeapAllocation> heap;516};517518struct HeapChunk {519uptr begin = 0;520uptr size = 0;521u32 stack_id = 0;522bool from_small_heap = false;523bool is_allocated = false;524};525526struct Shadow {527uptr addr = 0;528tag_t tags[kShadowDumpSize] = {};529tag_t short_tags[kShortDumpSize] = {};530};531532sptr FindMismatchOffset() const;533Shadow CopyShadow() const;534tag_t GetTagCopy(uptr addr) const;535tag_t GetShortTagCopy(uptr addr) const;536HeapChunk CopyHeapChunk() const;537Allocations CopyAllocations();538OverflowCandidate FindBufferOverflowCandidate() const;539void PrintAddressDescription() const;540void PrintHeapOrGlobalCandidate() const;541void PrintTags(uptr addr) const;542543SavedStackAllocations stack_allocations_storage[16];544HeapAllocation heap_allocations_storage[256];545546const ScopedReport scoped_report;547const StackTrace *stack = nullptr;548const uptr tagged_addr = 0;549const uptr access_size = 0;550const uptr untagged_addr = 0;551const tag_t ptr_tag = 0;552const sptr mismatch_offset = 0;553554const HeapChunk heap;555const Allocations allocations;556const OverflowCandidate candidate;557558const Shadow shadow;559};560561sptr BaseReport::FindMismatchOffset() const {562if (!access_size)563return 0;564sptr offset =565__hwasan_test_shadow(reinterpret_cast<void *>(tagged_addr), access_size);566CHECK_GE(offset, 0);567CHECK_LT(offset, static_cast<sptr>(access_size));568tag_t *tag_ptr =569reinterpret_cast<tag_t *>(MemToShadow(untagged_addr + offset));570tag_t mem_tag = *tag_ptr;571572if (mem_tag && mem_tag < kShadowAlignment) {573tag_t *granule_ptr = reinterpret_cast<tag_t *>((untagged_addr + offset) &574~(kShadowAlignment - 1));575// If offset is 0, (untagged_addr + offset) is not aligned to granules.576// This is the offset of the leftmost accessed byte within the bad granule.577u8 in_granule_offset = (untagged_addr + offset) & (kShadowAlignment - 1);578tag_t short_tag = granule_ptr[kShadowAlignment - 1];579// The first mismatch was a short granule that matched the ptr_tag.580if (short_tag == ptr_tag) {581// If the access starts after the end of the short granule, then the first582// bad byte is the first byte of the access; otherwise it is the first583// byte past the end of the short granule584if (mem_tag > in_granule_offset) {585offset += mem_tag - in_granule_offset;586}587}588}589return offset;590}591592BaseReport::Shadow BaseReport::CopyShadow() const {593Shadow result;594if (!MemIsApp(untagged_addr))595return result;596597result.addr = GetPrintTagStart(untagged_addr + mismatch_offset);598uptr tag_addr = result.addr;599uptr short_end = kShortDumpOffset + ARRAY_SIZE(shadow.short_tags);600for (uptr i = 0; i < ARRAY_SIZE(result.tags); ++i, ++tag_addr) {601if (!MemIsShadow(tag_addr))602continue;603result.tags[i] = *reinterpret_cast<tag_t *>(tag_addr);604if (i < kShortDumpOffset || i >= short_end)605continue;606uptr granule_addr = ShadowToMem(tag_addr);607if (1 <= result.tags[i] && result.tags[i] <= kShadowAlignment &&608IsAccessibleMemoryRange(granule_addr, kShadowAlignment)) {609result.short_tags[i - kShortDumpOffset] =610*reinterpret_cast<tag_t *>(granule_addr + kShadowAlignment - 1);611}612}613return result;614}615616tag_t BaseReport::GetTagCopy(uptr addr) const {617CHECK_GE(addr, shadow.addr);618uptr idx = addr - shadow.addr;619CHECK_LT(idx, ARRAY_SIZE(shadow.tags));620return shadow.tags[idx];621}622623tag_t BaseReport::GetShortTagCopy(uptr addr) const {624CHECK_GE(addr, shadow.addr + kShortDumpOffset);625uptr idx = addr - shadow.addr - kShortDumpOffset;626CHECK_LT(idx, ARRAY_SIZE(shadow.short_tags));627return shadow.short_tags[idx];628}629630BaseReport::HeapChunk BaseReport::CopyHeapChunk() const {631HeapChunk result = {};632if (MemIsShadow(untagged_addr))633return result;634HwasanChunkView chunk = FindHeapChunkByAddress(untagged_addr);635result.begin = chunk.Beg();636if (result.begin) {637result.size = chunk.ActualSize();638result.from_small_heap = chunk.FromSmallHeap();639result.is_allocated = chunk.IsAllocated();640result.stack_id = chunk.GetAllocStackId();641}642return result;643}644645BaseReport::Allocations BaseReport::CopyAllocations() {646if (MemIsShadow(untagged_addr))647return {};648uptr stack_allocations_count = 0;649uptr heap_allocations_count = 0;650hwasanThreadList().VisitAllLiveThreads([&](Thread *t) {651if (stack_allocations_count < ARRAY_SIZE(stack_allocations_storage) &&652t->AddrIsInStack(untagged_addr)) {653stack_allocations_storage[stack_allocations_count++].CopyFrom(t);654}655656if (heap_allocations_count < ARRAY_SIZE(heap_allocations_storage)) {657// Scan all threads' ring buffers to find if it's a heap-use-after-free.658HeapAllocationRecord har;659uptr ring_index, num_matching_addrs, num_matching_addrs_4b;660if (FindHeapAllocation(t->heap_allocations(), tagged_addr, &har,661&ring_index, &num_matching_addrs,662&num_matching_addrs_4b)) {663auto &ha = heap_allocations_storage[heap_allocations_count++];664ha.har = har;665ha.ring_index = ring_index;666ha.num_matching_addrs = num_matching_addrs;667ha.num_matching_addrs_4b = num_matching_addrs_4b;668ha.free_thread_id = t->unique_id();669}670}671});672673return {{stack_allocations_storage, stack_allocations_count},674{heap_allocations_storage, heap_allocations_count}};675}676677BaseReport::OverflowCandidate BaseReport::FindBufferOverflowCandidate() const {678OverflowCandidate result = {};679if (MemIsShadow(untagged_addr))680return result;681// Check if this looks like a heap buffer overflow by scanning682// the shadow left and right and looking for the first adjacent683// object with a different memory tag. If that tag matches ptr_tag,684// check the allocator if it has a live chunk there.685tag_t *tag_ptr = reinterpret_cast<tag_t *>(MemToShadow(untagged_addr));686tag_t *candidate_tag_ptr = nullptr, *left = tag_ptr, *right = tag_ptr;687uptr candidate_distance = 0;688for (; candidate_distance < 1000; candidate_distance++) {689if (MemIsShadow(reinterpret_cast<uptr>(left)) && TagsEqual(ptr_tag, left)) {690candidate_tag_ptr = left;691break;692}693--left;694if (MemIsShadow(reinterpret_cast<uptr>(right)) &&695TagsEqual(ptr_tag, right)) {696candidate_tag_ptr = right;697break;698}699++right;700}701702constexpr auto kCloseCandidateDistance = 1;703result.is_close = candidate_distance <= kCloseCandidateDistance;704705result.after = candidate_tag_ptr == left;706result.untagged_addr = ShadowToMem(reinterpret_cast<uptr>(candidate_tag_ptr));707HwasanChunkView chunk = FindHeapChunkByAddress(result.untagged_addr);708if (chunk.IsAllocated()) {709result.heap.is_allocated = true;710result.heap.begin = chunk.Beg();711result.heap.end = chunk.End();712result.heap.thread_id = chunk.GetAllocThreadId();713result.heap.stack_id = chunk.GetAllocStackId();714}715return result;716}717718void BaseReport::PrintHeapOrGlobalCandidate() const {719Decorator d;720if (candidate.heap.is_allocated) {721uptr offset;722const char *whence;723if (candidate.heap.begin <= untagged_addr &&724untagged_addr < candidate.heap.end) {725offset = untagged_addr - candidate.heap.begin;726whence = "inside";727} else if (candidate.after) {728offset = untagged_addr - candidate.heap.end;729whence = "after";730} else {731offset = candidate.heap.begin - untagged_addr;732whence = "before";733}734Printf("%s", d.Error());735Printf("\nCause: heap-buffer-overflow\n");736Printf("%s", d.Default());737Printf("%s", d.Location());738Printf("%p is located %zd bytes %s a %zd-byte region [%p,%p)\n",739untagged_addr, offset, whence,740candidate.heap.end - candidate.heap.begin, candidate.heap.begin,741candidate.heap.end);742Printf("%s", d.Allocation());743Printf("allocated by thread T%u here:\n", candidate.heap.thread_id);744Printf("%s", d.Default());745GetStackTraceFromId(candidate.heap.stack_id).Print();746return;747}748// Check whether the address points into a loaded library. If so, this is749// most likely a global variable.750const char *module_name;751uptr module_address;752Symbolizer *sym = Symbolizer::GetOrInit();753if (sym->GetModuleNameAndOffsetForPC(candidate.untagged_addr, &module_name,754&module_address)) {755Printf("%s", d.Error());756Printf("\nCause: global-overflow\n");757Printf("%s", d.Default());758DataInfo info;759Printf("%s", d.Location());760if (sym->SymbolizeData(candidate.untagged_addr, &info) && info.start) {761Printf(762"%p is located %zd bytes %s a %zd-byte global variable "763"%s [%p,%p) in %s\n",764untagged_addr,765candidate.after ? untagged_addr - (info.start + info.size)766: info.start - untagged_addr,767candidate.after ? "after" : "before", info.size, info.name,768info.start, info.start + info.size, module_name);769} else {770uptr size = GetGlobalSizeFromDescriptor(candidate.untagged_addr);771if (size == 0)772// We couldn't find the size of the global from the descriptors.773Printf(774"%p is located %s a global variable in "775"\n #0 0x%x (%s+0x%x)\n",776untagged_addr, candidate.after ? "after" : "before",777candidate.untagged_addr, module_name, module_address);778else779Printf(780"%p is located %s a %zd-byte global variable in "781"\n #0 0x%x (%s+0x%x)\n",782untagged_addr, candidate.after ? "after" : "before", size,783candidate.untagged_addr, module_name, module_address);784}785Printf("%s", d.Default());786}787}788789void BaseReport::PrintAddressDescription() const {790Decorator d;791int num_descriptions_printed = 0;792793if (MemIsShadow(untagged_addr)) {794Printf("%s%p is HWAsan shadow memory.\n%s", d.Location(), untagged_addr,795d.Default());796return;797}798799// Print some very basic information about the address, if it's a heap.800if (heap.begin) {801Printf(802"%s[%p,%p) is a %s %s heap chunk; "803"size: %zd offset: %zd\n%s",804d.Location(), heap.begin, heap.begin + heap.size,805heap.from_small_heap ? "small" : "large",806heap.is_allocated ? "allocated" : "unallocated", heap.size,807untagged_addr - heap.begin, d.Default());808}809810auto announce_by_id = [](u32 thread_id) {811hwasanThreadList().VisitAllLiveThreads([&](Thread *t) {812if (thread_id == t->unique_id())813t->Announce();814});815};816817// Check stack first. If the address is on the stack of a live thread, we818// know it cannot be a heap / global overflow.819for (const auto &sa : allocations.stack) {820Printf("%s", d.Error());821Printf("\nCause: stack tag-mismatch\n");822Printf("%s", d.Location());823Printf("Address %p is located in stack of thread T%zd\n", untagged_addr,824sa.thread_id());825Printf("%s", d.Default());826announce_by_id(sa.thread_id());827PrintStackAllocations(sa.get(), ptr_tag, untagged_addr);828num_descriptions_printed++;829}830831if (allocations.stack.empty() && candidate.untagged_addr &&832candidate.is_close) {833PrintHeapOrGlobalCandidate();834num_descriptions_printed++;835}836837for (const auto &ha : allocations.heap) {838const HeapAllocationRecord har = ha.har;839840Printf("%s", d.Error());841Printf("\nCause: use-after-free\n");842Printf("%s", d.Location());843Printf("%p is located %zd bytes inside a %zd-byte region [%p,%p)\n",844untagged_addr, untagged_addr - UntagAddr(har.tagged_addr),845har.requested_size, UntagAddr(har.tagged_addr),846UntagAddr(har.tagged_addr) + har.requested_size);847Printf("%s", d.Allocation());848Printf("freed by thread T%u here:\n", ha.free_thread_id);849Printf("%s", d.Default());850GetStackTraceFromId(har.free_context_id).Print();851852Printf("%s", d.Allocation());853Printf("previously allocated by thread T%u here:\n", har.alloc_thread_id);854Printf("%s", d.Default());855GetStackTraceFromId(har.alloc_context_id).Print();856857// Print a developer note: the index of this heap object858// in the thread's deallocation ring buffer.859Printf("hwasan_dev_note_heap_rb_distance: %zd %zd\n", ha.ring_index + 1,860flags()->heap_history_size);861Printf("hwasan_dev_note_num_matching_addrs: %zd\n", ha.num_matching_addrs);862Printf("hwasan_dev_note_num_matching_addrs_4b: %zd\n",863ha.num_matching_addrs_4b);864865announce_by_id(ha.free_thread_id);866// TODO: announce_by_id(har.alloc_thread_id);867num_descriptions_printed++;868}869870if (candidate.untagged_addr && num_descriptions_printed == 0) {871PrintHeapOrGlobalCandidate();872num_descriptions_printed++;873}874875// Print the remaining threads, as an extra information, 1 line per thread.876if (flags()->print_live_threads_info) {877Printf("\n");878hwasanThreadList().VisitAllLiveThreads([&](Thread *t) { t->Announce(); });879}880881if (!num_descriptions_printed)882// We exhausted our possibilities. Bail out.883Printf("HWAddressSanitizer can not describe address in more detail.\n");884if (num_descriptions_printed > 1) {885Printf(886"There are %d potential causes, printed above in order "887"of likeliness.\n",888num_descriptions_printed);889}890}891892void BaseReport::PrintTags(uptr addr) const {893if (shadow.addr) {894PrintTagsAroundAddr(895addr, [&](uptr addr) { return GetTagCopy(addr); },896[&](uptr addr) { return GetShortTagCopy(addr); });897}898}899900class InvalidFreeReport : public BaseReport {901public:902InvalidFreeReport(StackTrace *stack, uptr tagged_addr)903: BaseReport(stack, flags()->halt_on_error, tagged_addr, 0) {}904~InvalidFreeReport();905906private:907};908909InvalidFreeReport::~InvalidFreeReport() {910Decorator d;911Printf("%s", d.Error());912uptr pc = GetTopPc(stack);913const char *bug_type = "invalid-free";914const Thread *thread = GetCurrentThread();915if (thread) {916Report("ERROR: %s: %s on address %p at pc %p on thread T%zd\n",917SanitizerToolName, bug_type, untagged_addr, pc, thread->unique_id());918} else {919Report("ERROR: %s: %s on address %p at pc %p on unknown thread\n",920SanitizerToolName, bug_type, untagged_addr, pc);921}922Printf("%s", d.Access());923if (shadow.addr) {924Printf("tags: %02x/%02x (ptr/mem)\n", ptr_tag,925GetTagCopy(MemToShadow(untagged_addr)));926}927Printf("%s", d.Default());928929stack->Print();930931PrintAddressDescription();932PrintTags(untagged_addr);933MaybePrintAndroidHelpUrl();934ReportErrorSummary(bug_type, stack);935}936937class TailOverwrittenReport : public BaseReport {938public:939explicit TailOverwrittenReport(StackTrace *stack, uptr tagged_addr,940uptr orig_size, const u8 *expected)941: BaseReport(stack, flags()->halt_on_error, tagged_addr, 0),942orig_size(orig_size),943tail_size(kShadowAlignment - (orig_size % kShadowAlignment)) {944CHECK_GT(tail_size, 0U);945CHECK_LT(tail_size, kShadowAlignment);946internal_memcpy(tail_copy,947reinterpret_cast<u8 *>(untagged_addr + orig_size),948tail_size);949internal_memcpy(actual_expected, expected, tail_size);950// Short granule is stashed in the last byte of the magic string. To avoid951// confusion, make the expected magic string contain the short granule tag.952if (orig_size % kShadowAlignment != 0)953actual_expected[tail_size - 1] = ptr_tag;954}955~TailOverwrittenReport();956957private:958const uptr orig_size = 0;959const uptr tail_size = 0;960u8 actual_expected[kShadowAlignment] = {};961u8 tail_copy[kShadowAlignment] = {};962};963964TailOverwrittenReport::~TailOverwrittenReport() {965Decorator d;966Printf("%s", d.Error());967const char *bug_type = "allocation-tail-overwritten";968Report("ERROR: %s: %s; heap object [%p,%p) of size %zd\n", SanitizerToolName,969bug_type, untagged_addr, untagged_addr + orig_size, orig_size);970Printf("\n%s", d.Default());971Printf(972"Stack of invalid access unknown. Issue detected at deallocation "973"time.\n");974Printf("%s", d.Allocation());975Printf("deallocated here:\n");976Printf("%s", d.Default());977stack->Print();978if (heap.begin) {979Printf("%s", d.Allocation());980Printf("allocated here:\n");981Printf("%s", d.Default());982GetStackTraceFromId(heap.stack_id).Print();983}984985InternalScopedString s;986u8 *tail = tail_copy;987s.Append("Tail contains: ");988for (uptr i = 0; i < kShadowAlignment - tail_size; i++) s.Append(".. ");989for (uptr i = 0; i < tail_size; i++) s.AppendF("%02x ", tail[i]);990s.Append("\n");991s.Append("Expected: ");992for (uptr i = 0; i < kShadowAlignment - tail_size; i++) s.Append(".. ");993for (uptr i = 0; i < tail_size; i++) s.AppendF("%02x ", actual_expected[i]);994s.Append("\n");995s.Append(" ");996for (uptr i = 0; i < kShadowAlignment - tail_size; i++) s.Append(" ");997for (uptr i = 0; i < tail_size; i++)998s.AppendF("%s ", actual_expected[i] != tail[i] ? "^^" : " ");9991000s.AppendF(1001"\nThis error occurs when a buffer overflow overwrites memory\n"1002"after a heap object, but within the %zd-byte granule, e.g.\n"1003" char *x = new char[20];\n"1004" x[25] = 42;\n"1005"%s does not detect such bugs in uninstrumented code at the time of "1006"write,"1007"\nbut can detect them at the time of free/delete.\n"1008"To disable this feature set HWASAN_OPTIONS=free_checks_tail_magic=0\n",1009kShadowAlignment, SanitizerToolName);1010Printf("%s", s.data());1011GetCurrentThread()->Announce();1012PrintTags(untagged_addr);1013MaybePrintAndroidHelpUrl();1014ReportErrorSummary(bug_type, stack);1015}10161017class TagMismatchReport : public BaseReport {1018public:1019explicit TagMismatchReport(StackTrace *stack, uptr tagged_addr,1020uptr access_size, bool is_store, bool fatal,1021uptr *registers_frame)1022: BaseReport(stack, fatal, tagged_addr, access_size),1023is_store(is_store),1024registers_frame(registers_frame) {}1025~TagMismatchReport();10261027private:1028const bool is_store;1029const uptr *registers_frame;1030};10311032TagMismatchReport::~TagMismatchReport() {1033Decorator d;1034// TODO: when possible, try to print heap-use-after-free, etc.1035const char *bug_type = "tag-mismatch";1036uptr pc = GetTopPc(stack);1037Printf("%s", d.Error());1038Report("ERROR: %s: %s on address %p at pc %p\n", SanitizerToolName, bug_type,1039untagged_addr, pc);10401041Thread *t = GetCurrentThread();10421043tag_t mem_tag = GetTagCopy(MemToShadow(untagged_addr + mismatch_offset));10441045Printf("%s", d.Access());1046if (mem_tag && mem_tag < kShadowAlignment) {1047tag_t short_tag =1048GetShortTagCopy(MemToShadow(untagged_addr + mismatch_offset));1049Printf(1050"%s of size %zu at %p tags: %02x/%02x(%02x) (ptr/mem) in thread T%zd\n",1051is_store ? "WRITE" : "READ", access_size, untagged_addr, ptr_tag,1052mem_tag, short_tag, t->unique_id());1053} else {1054Printf("%s of size %zu at %p tags: %02x/%02x (ptr/mem) in thread T%zd\n",1055is_store ? "WRITE" : "READ", access_size, untagged_addr, ptr_tag,1056mem_tag, t->unique_id());1057}1058if (mismatch_offset)1059Printf("Invalid access starting at offset %zu\n", mismatch_offset);1060Printf("%s", d.Default());10611062stack->Print();10631064PrintAddressDescription();1065t->Announce();10661067PrintTags(untagged_addr + mismatch_offset);10681069if (registers_frame)1070ReportRegisters(registers_frame, pc);10711072MaybePrintAndroidHelpUrl();1073ReportErrorSummary(bug_type, stack);1074}1075} // namespace10761077void ReportInvalidFree(StackTrace *stack, uptr tagged_addr) {1078InvalidFreeReport R(stack, tagged_addr);1079}10801081void ReportTailOverwritten(StackTrace *stack, uptr tagged_addr, uptr orig_size,1082const u8 *expected) {1083TailOverwrittenReport R(stack, tagged_addr, orig_size, expected);1084}10851086void ReportTagMismatch(StackTrace *stack, uptr tagged_addr, uptr access_size,1087bool is_store, bool fatal, uptr *registers_frame) {1088TagMismatchReport R(stack, tagged_addr, access_size, is_store, fatal,1089registers_frame);1090}10911092// See the frame breakdown defined in __hwasan_tag_mismatch (from1093// hwasan_tag_mismatch_{aarch64,riscv64}.S).1094void ReportRegisters(const uptr *frame, uptr pc) {1095Printf("\nRegisters where the failure occurred (pc %p):\n", pc);10961097// We explicitly print a single line (4 registers/line) each iteration to1098// reduce the amount of logcat error messages printed. Each Printf() will1099// result in a new logcat line, irrespective of whether a newline is present,1100// and so we wish to reduce the number of Printf() calls we have to make.1101#if defined(__aarch64__)1102Printf(" x0 %016llx x1 %016llx x2 %016llx x3 %016llx\n",1103frame[0], frame[1], frame[2], frame[3]);1104#elif SANITIZER_RISCV641105Printf(" sp %016llx x1 %016llx x2 %016llx x3 %016llx\n",1106reinterpret_cast<const u8 *>(frame) + 256, frame[1], frame[2],1107frame[3]);1108#endif1109Printf(" x4 %016llx x5 %016llx x6 %016llx x7 %016llx\n",1110frame[4], frame[5], frame[6], frame[7]);1111Printf(" x8 %016llx x9 %016llx x10 %016llx x11 %016llx\n",1112frame[8], frame[9], frame[10], frame[11]);1113Printf(" x12 %016llx x13 %016llx x14 %016llx x15 %016llx\n",1114frame[12], frame[13], frame[14], frame[15]);1115Printf(" x16 %016llx x17 %016llx x18 %016llx x19 %016llx\n",1116frame[16], frame[17], frame[18], frame[19]);1117Printf(" x20 %016llx x21 %016llx x22 %016llx x23 %016llx\n",1118frame[20], frame[21], frame[22], frame[23]);1119Printf(" x24 %016llx x25 %016llx x26 %016llx x27 %016llx\n",1120frame[24], frame[25], frame[26], frame[27]);1121// hwasan_check* reduces the stack pointer by 256, then __hwasan_tag_mismatch1122// passes it to this function.1123#if defined(__aarch64__)1124Printf(" x28 %016llx x29 %016llx x30 %016llx sp %016llx\n", frame[28],1125frame[29], frame[30], reinterpret_cast<const u8 *>(frame) + 256);1126#elif SANITIZER_RISCV641127Printf(" x28 %016llx x29 %016llx x30 %016llx x31 %016llx\n", frame[28],1128frame[29], frame[30], frame[31]);1129#else1130#endif1131}11321133} // namespace __hwasan11341135void __hwasan_set_error_report_callback(void (*callback)(const char *)) {1136__hwasan::ScopedReport::SetErrorReportCallback(callback);1137}113811391140