1 1.1 mrg //===-- hwasan_report.cpp -------------------------------------------------===// 2 1.1 mrg // 3 1.1 mrg // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 4 1.1 mrg // See https://llvm.org/LICENSE.txt for license information. 5 1.1 mrg // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 6 1.1 mrg // 7 1.1 mrg //===----------------------------------------------------------------------===// 8 1.1 mrg // 9 1.1 mrg // This file is a part of HWAddressSanitizer. 10 1.1 mrg // 11 1.1 mrg // Error reporting. 12 1.1 mrg //===----------------------------------------------------------------------===// 13 1.1 mrg 14 1.1 mrg #include "hwasan_report.h" 15 1.1 mrg 16 1.1 mrg #include <dlfcn.h> 17 1.1 mrg 18 1.1 mrg #include "hwasan.h" 19 1.1 mrg #include "hwasan_allocator.h" 20 1.1 mrg #include "hwasan_globals.h" 21 1.1 mrg #include "hwasan_mapping.h" 22 1.1 mrg #include "hwasan_thread.h" 23 1.1 mrg #include "hwasan_thread_list.h" 24 1.1 mrg #include "sanitizer_common/sanitizer_allocator_internal.h" 25 1.1 mrg #include "sanitizer_common/sanitizer_common.h" 26 1.1 mrg #include "sanitizer_common/sanitizer_flags.h" 27 1.1 mrg #include "sanitizer_common/sanitizer_mutex.h" 28 1.1 mrg #include "sanitizer_common/sanitizer_report_decorator.h" 29 1.1 mrg #include "sanitizer_common/sanitizer_stackdepot.h" 30 1.1 mrg #include "sanitizer_common/sanitizer_stacktrace_printer.h" 31 1.1 mrg #include "sanitizer_common/sanitizer_symbolizer.h" 32 1.1 mrg 33 1.1 mrg using namespace __sanitizer; 34 1.1 mrg 35 1.1 mrg namespace __hwasan { 36 1.1 mrg 37 1.1 mrg class ScopedReport { 38 1.1 mrg public: 39 1.1 mrg ScopedReport(bool fatal = false) : error_message_(1), fatal(fatal) { 40 1.1 mrg Lock lock(&error_message_lock_); 41 1.1 mrg error_message_ptr_ = fatal ? &error_message_ : nullptr; 42 1.1 mrg ++hwasan_report_count; 43 1.1 mrg } 44 1.1 mrg 45 1.1 mrg ~ScopedReport() { 46 1.1 mrg void (*report_cb)(const char *); 47 1.1 mrg { 48 1.1 mrg Lock lock(&error_message_lock_); 49 1.1 mrg report_cb = error_report_callback_; 50 1.1 mrg error_message_ptr_ = nullptr; 51 1.1 mrg } 52 1.1 mrg if (report_cb) 53 1.1 mrg report_cb(error_message_.data()); 54 1.1 mrg if (fatal) 55 1.1 mrg SetAbortMessage(error_message_.data()); 56 1.1 mrg if (common_flags()->print_module_map >= 2 || 57 1.1 mrg (fatal && common_flags()->print_module_map)) 58 1.1 mrg DumpProcessMap(); 59 1.1 mrg if (fatal) 60 1.1 mrg Die(); 61 1.1 mrg } 62 1.1 mrg 63 1.1 mrg static void MaybeAppendToErrorMessage(const char *msg) { 64 1.1 mrg Lock lock(&error_message_lock_); 65 1.1 mrg if (!error_message_ptr_) 66 1.1 mrg return; 67 1.1 mrg uptr len = internal_strlen(msg); 68 1.1 mrg uptr old_size = error_message_ptr_->size(); 69 1.1 mrg error_message_ptr_->resize(old_size + len); 70 1.1 mrg // overwrite old trailing '\0', keep new trailing '\0' untouched. 71 1.1 mrg internal_memcpy(&(*error_message_ptr_)[old_size - 1], msg, len); 72 1.1 mrg } 73 1.1 mrg 74 1.1 mrg static void SetErrorReportCallback(void (*callback)(const char *)) { 75 1.1 mrg Lock lock(&error_message_lock_); 76 1.1 mrg error_report_callback_ = callback; 77 1.1 mrg } 78 1.1 mrg 79 1.1 mrg private: 80 1.1 mrg ScopedErrorReportLock error_report_lock_; 81 1.1 mrg InternalMmapVector<char> error_message_; 82 1.1 mrg bool fatal; 83 1.1 mrg 84 1.1 mrg static InternalMmapVector<char> *error_message_ptr_; 85 1.1 mrg static Mutex error_message_lock_; 86 1.1 mrg static void (*error_report_callback_)(const char *); 87 1.1 mrg }; 88 1.1 mrg 89 1.1 mrg InternalMmapVector<char> *ScopedReport::error_message_ptr_; 90 1.1 mrg Mutex ScopedReport::error_message_lock_; 91 1.1 mrg void (*ScopedReport::error_report_callback_)(const char *); 92 1.1 mrg 93 1.1 mrg // If there is an active ScopedReport, append to its error message. 94 1.1 mrg void AppendToErrorMessageBuffer(const char *buffer) { 95 1.1 mrg ScopedReport::MaybeAppendToErrorMessage(buffer); 96 1.1 mrg } 97 1.1 mrg 98 1.1 mrg static StackTrace GetStackTraceFromId(u32 id) { 99 1.1 mrg CHECK(id); 100 1.1 mrg StackTrace res = StackDepotGet(id); 101 1.1 mrg CHECK(res.trace); 102 1.1 mrg return res; 103 1.1 mrg } 104 1.1 mrg 105 1.1 mrg // A RAII object that holds a copy of the current thread stack ring buffer. 106 1.1 mrg // The actual stack buffer may change while we are iterating over it (for 107 1.1 mrg // example, Printf may call syslog() which can itself be built with hwasan). 108 1.1 mrg class SavedStackAllocations { 109 1.1 mrg public: 110 1.1 mrg SavedStackAllocations(StackAllocationsRingBuffer *rb) { 111 1.1 mrg uptr size = rb->size() * sizeof(uptr); 112 1.1 mrg void *storage = 113 1.1 mrg MmapAlignedOrDieOnFatalError(size, size * 2, "saved stack allocations"); 114 1.1 mrg new (&rb_) StackAllocationsRingBuffer(*rb, storage); 115 1.1 mrg } 116 1.1 mrg 117 1.1 mrg ~SavedStackAllocations() { 118 1.1 mrg StackAllocationsRingBuffer *rb = get(); 119 1.1 mrg UnmapOrDie(rb->StartOfStorage(), rb->size() * sizeof(uptr)); 120 1.1 mrg } 121 1.1 mrg 122 1.1 mrg StackAllocationsRingBuffer *get() { 123 1.1 mrg return (StackAllocationsRingBuffer *)&rb_; 124 1.1 mrg } 125 1.1 mrg 126 1.1 mrg private: 127 1.1 mrg uptr rb_; 128 1.1 mrg }; 129 1.1 mrg 130 1.1 mrg class Decorator: public __sanitizer::SanitizerCommonDecorator { 131 1.1 mrg public: 132 1.1 mrg Decorator() : SanitizerCommonDecorator() { } 133 1.1 mrg const char *Access() { return Blue(); } 134 1.1 mrg const char *Allocation() const { return Magenta(); } 135 1.1 mrg const char *Origin() const { return Magenta(); } 136 1.1 mrg const char *Name() const { return Green(); } 137 1.1 mrg const char *Location() { return Green(); } 138 1.1 mrg const char *Thread() { return Green(); } 139 1.1 mrg }; 140 1.1 mrg 141 1.1 mrg static bool FindHeapAllocation(HeapAllocationsRingBuffer *rb, uptr tagged_addr, 142 1.1 mrg HeapAllocationRecord *har, uptr *ring_index, 143 1.1 mrg uptr *num_matching_addrs, 144 1.1 mrg uptr *num_matching_addrs_4b) { 145 1.1 mrg if (!rb) return false; 146 1.1 mrg 147 1.1 mrg *num_matching_addrs = 0; 148 1.1 mrg *num_matching_addrs_4b = 0; 149 1.1 mrg for (uptr i = 0, size = rb->size(); i < size; i++) { 150 1.1 mrg auto h = (*rb)[i]; 151 1.1 mrg if (h.tagged_addr <= tagged_addr && 152 1.1 mrg h.tagged_addr + h.requested_size > tagged_addr) { 153 1.1 mrg *har = h; 154 1.1 mrg *ring_index = i; 155 1.1 mrg return true; 156 1.1 mrg } 157 1.1 mrg 158 1.1 mrg // Measure the number of heap ring buffer entries that would have matched 159 1.1 mrg // if we had only one entry per address (e.g. if the ring buffer data was 160 1.1 mrg // stored at the address itself). This will help us tune the allocator 161 1.1 mrg // implementation for MTE. 162 1.1 mrg if (UntagAddr(h.tagged_addr) <= UntagAddr(tagged_addr) && 163 1.1 mrg UntagAddr(h.tagged_addr) + h.requested_size > UntagAddr(tagged_addr)) { 164 1.1 mrg ++*num_matching_addrs; 165 1.1 mrg } 166 1.1 mrg 167 1.1 mrg // Measure the number of heap ring buffer entries that would have matched 168 1.1 mrg // if we only had 4 tag bits, which is the case for MTE. 169 1.1 mrg auto untag_4b = [](uptr p) { 170 1.1 mrg return p & ((1ULL << 60) - 1); 171 1.1 mrg }; 172 1.1 mrg if (untag_4b(h.tagged_addr) <= untag_4b(tagged_addr) && 173 1.1 mrg untag_4b(h.tagged_addr) + h.requested_size > untag_4b(tagged_addr)) { 174 1.1 mrg ++*num_matching_addrs_4b; 175 1.1 mrg } 176 1.1 mrg } 177 1.1 mrg return false; 178 1.1 mrg } 179 1.1 mrg 180 1.1 mrg static void PrintStackAllocations(StackAllocationsRingBuffer *sa, 181 1.1 mrg tag_t addr_tag, uptr untagged_addr) { 182 1.1 mrg uptr frames = Min((uptr)flags()->stack_history_size, sa->size()); 183 1.1 mrg bool found_local = false; 184 1.1 mrg for (uptr i = 0; i < frames; i++) { 185 1.1 mrg const uptr *record_addr = &(*sa)[i]; 186 1.1 mrg uptr record = *record_addr; 187 1.1 mrg if (!record) 188 1.1 mrg break; 189 1.1 mrg tag_t base_tag = 190 1.1 mrg reinterpret_cast<uptr>(record_addr) >> kRecordAddrBaseTagShift; 191 1.1 mrg uptr fp = (record >> kRecordFPShift) << kRecordFPLShift; 192 1.1 mrg uptr pc_mask = (1ULL << kRecordFPShift) - 1; 193 1.1 mrg uptr pc = record & pc_mask; 194 1.1 mrg FrameInfo frame; 195 1.1 mrg if (Symbolizer::GetOrInit()->SymbolizeFrame(pc, &frame)) { 196 1.1 mrg for (LocalInfo &local : frame.locals) { 197 1.1 mrg if (!local.has_frame_offset || !local.has_size || !local.has_tag_offset) 198 1.1 mrg continue; 199 1.1 mrg tag_t obj_tag = base_tag ^ local.tag_offset; 200 1.1 mrg if (obj_tag != addr_tag) 201 1.1 mrg continue; 202 1.1 mrg // Calculate the offset from the object address to the faulting 203 1.1 mrg // address. Because we only store bits 4-19 of FP (bits 0-3 are 204 1.1 mrg // guaranteed to be zero), the calculation is performed mod 2^20 and may 205 1.1 mrg // harmlessly underflow if the address mod 2^20 is below the object 206 1.1 mrg // address. 207 1.1 mrg uptr obj_offset = 208 1.1 mrg (untagged_addr - fp - local.frame_offset) & (kRecordFPModulus - 1); 209 1.1 mrg if (obj_offset >= local.size) 210 1.1 mrg continue; 211 1.1 mrg if (!found_local) { 212 1.1 mrg Printf("Potentially referenced stack objects:\n"); 213 1.1 mrg found_local = true; 214 1.1 mrg } 215 1.1 mrg Printf(" %s in %s %s:%d\n", local.name, local.function_name, 216 1.1 mrg local.decl_file, local.decl_line); 217 1.1 mrg } 218 1.1 mrg frame.Clear(); 219 1.1 mrg } 220 1.1 mrg } 221 1.1 mrg 222 1.1 mrg if (found_local) 223 1.1 mrg return; 224 1.1 mrg 225 1.1 mrg // We didn't find any locals. Most likely we don't have symbols, so dump 226 1.1 mrg // the information that we have for offline analysis. 227 1.1 mrg InternalScopedString frame_desc; 228 1.1 mrg Printf("Previously allocated frames:\n"); 229 1.1 mrg for (uptr i = 0; i < frames; i++) { 230 1.1 mrg const uptr *record_addr = &(*sa)[i]; 231 1.1 mrg uptr record = *record_addr; 232 1.1 mrg if (!record) 233 1.1 mrg break; 234 1.1 mrg uptr pc_mask = (1ULL << 48) - 1; 235 1.1 mrg uptr pc = record & pc_mask; 236 1.1 mrg frame_desc.append(" record_addr:0x%zx record:0x%zx", 237 1.1 mrg reinterpret_cast<uptr>(record_addr), record); 238 1.1 mrg if (SymbolizedStack *frame = Symbolizer::GetOrInit()->SymbolizePC(pc)) { 239 1.1 mrg RenderFrame(&frame_desc, " %F %L", 0, frame->info.address, &frame->info, 240 1.1 mrg common_flags()->symbolize_vs_style, 241 1.1 mrg common_flags()->strip_path_prefix); 242 1.1 mrg frame->ClearAll(); 243 1.1 mrg } 244 1.1 mrg Printf("%s\n", frame_desc.data()); 245 1.1 mrg frame_desc.clear(); 246 1.1 mrg } 247 1.1 mrg } 248 1.1 mrg 249 1.1 mrg // Returns true if tag == *tag_ptr, reading tags from short granules if 250 1.1 mrg // necessary. This may return a false positive if tags 1-15 are used as a 251 1.1 mrg // regular tag rather than a short granule marker. 252 1.1 mrg static bool TagsEqual(tag_t tag, tag_t *tag_ptr) { 253 1.1 mrg if (tag == *tag_ptr) 254 1.1 mrg return true; 255 1.1 mrg if (*tag_ptr == 0 || *tag_ptr > kShadowAlignment - 1) 256 1.1 mrg return false; 257 1.1 mrg uptr mem = ShadowToMem(reinterpret_cast<uptr>(tag_ptr)); 258 1.1 mrg tag_t inline_tag = *reinterpret_cast<tag_t *>(mem + kShadowAlignment - 1); 259 1.1 mrg return tag == inline_tag; 260 1.1 mrg } 261 1.1 mrg 262 1.1 mrg // HWASan globals store the size of the global in the descriptor. In cases where 263 1.1 mrg // we don't have a binary with symbols, we can't grab the size of the global 264 1.1 mrg // from the debug info - but we might be able to retrieve it from the 265 1.1 mrg // descriptor. Returns zero if the lookup failed. 266 1.1 mrg static uptr GetGlobalSizeFromDescriptor(uptr ptr) { 267 1.1 mrg // Find the ELF object that this global resides in. 268 1.1 mrg Dl_info info; 269 1.1 mrg if (dladdr(reinterpret_cast<void *>(ptr), &info) == 0) 270 1.1 mrg return 0; 271 1.1 mrg auto *ehdr = reinterpret_cast<const ElfW(Ehdr) *>(info.dli_fbase); 272 1.1 mrg auto *phdr_begin = reinterpret_cast<const ElfW(Phdr) *>( 273 1.1 mrg reinterpret_cast<const u8 *>(ehdr) + ehdr->e_phoff); 274 1.1 mrg 275 1.1 mrg // Get the load bias. This is normally the same as the dli_fbase address on 276 1.1 mrg // position-independent code, but can be different on non-PIE executables, 277 1.1 mrg // binaries using LLD's partitioning feature, or binaries compiled with a 278 1.1 mrg // linker script. 279 1.1 mrg ElfW(Addr) load_bias = 0; 280 1.1 mrg for (const auto &phdr : 281 1.1 mrg ArrayRef<const ElfW(Phdr)>(phdr_begin, phdr_begin + ehdr->e_phnum)) { 282 1.1 mrg if (phdr.p_type != PT_LOAD || phdr.p_offset != 0) 283 1.1 mrg continue; 284 1.1 mrg load_bias = reinterpret_cast<ElfW(Addr)>(ehdr) - phdr.p_vaddr; 285 1.1 mrg break; 286 1.1 mrg } 287 1.1 mrg 288 1.1 mrg // Walk all globals in this ELF object, looking for the one we're interested 289 1.1 mrg // in. Once we find it, we can stop iterating and return the size of the 290 1.1 mrg // global we're interested in. 291 1.1 mrg for (const hwasan_global &global : 292 1.1 mrg HwasanGlobalsFor(load_bias, phdr_begin, ehdr->e_phnum)) 293 1.1 mrg if (global.addr() <= ptr && ptr < global.addr() + global.size()) 294 1.1 mrg return global.size(); 295 1.1 mrg 296 1.1 mrg return 0; 297 1.1 mrg } 298 1.1 mrg 299 1.1 mrg static void ShowHeapOrGlobalCandidate(uptr untagged_addr, tag_t *candidate, 300 1.1 mrg tag_t *left, tag_t *right) { 301 1.1 mrg Decorator d; 302 1.1 mrg uptr mem = ShadowToMem(reinterpret_cast<uptr>(candidate)); 303 1.1 mrg HwasanChunkView chunk = FindHeapChunkByAddress(mem); 304 1.1 mrg if (chunk.IsAllocated()) { 305 1.1 mrg uptr offset; 306 1.1 mrg const char *whence; 307 1.1 mrg if (untagged_addr < chunk.End() && untagged_addr >= chunk.Beg()) { 308 1.1 mrg offset = untagged_addr - chunk.Beg(); 309 1.1 mrg whence = "inside"; 310 1.1 mrg } else if (candidate == left) { 311 1.1 mrg offset = untagged_addr - chunk.End(); 312 1.1 mrg whence = "to the right of"; 313 1.1 mrg } else { 314 1.1 mrg offset = chunk.Beg() - untagged_addr; 315 1.1 mrg whence = "to the left of"; 316 1.1 mrg } 317 1.1 mrg Printf("%s", d.Error()); 318 1.1 mrg Printf("\nCause: heap-buffer-overflow\n"); 319 1.1 mrg Printf("%s", d.Default()); 320 1.1 mrg Printf("%s", d.Location()); 321 1.1 mrg Printf("%p is located %zd bytes %s %zd-byte region [%p,%p)\n", 322 1.1 mrg untagged_addr, offset, whence, chunk.UsedSize(), chunk.Beg(), 323 1.1 mrg chunk.End()); 324 1.1 mrg Printf("%s", d.Allocation()); 325 1.1 mrg Printf("allocated here:\n"); 326 1.1 mrg Printf("%s", d.Default()); 327 1.1 mrg GetStackTraceFromId(chunk.GetAllocStackId()).Print(); 328 1.1 mrg return; 329 1.1 mrg } 330 1.1 mrg // Check whether the address points into a loaded library. If so, this is 331 1.1 mrg // most likely a global variable. 332 1.1 mrg const char *module_name; 333 1.1 mrg uptr module_address; 334 1.1 mrg Symbolizer *sym = Symbolizer::GetOrInit(); 335 1.1 mrg if (sym->GetModuleNameAndOffsetForPC(mem, &module_name, &module_address)) { 336 1.1 mrg Printf("%s", d.Error()); 337 1.1 mrg Printf("\nCause: global-overflow\n"); 338 1.1 mrg Printf("%s", d.Default()); 339 1.1 mrg DataInfo info; 340 1.1 mrg Printf("%s", d.Location()); 341 1.1 mrg if (sym->SymbolizeData(mem, &info) && info.start) { 342 1.1 mrg Printf( 343 1.1 mrg "%p is located %zd bytes to the %s of %zd-byte global variable " 344 1.1 mrg "%s [%p,%p) in %s\n", 345 1.1 mrg untagged_addr, 346 1.1 mrg candidate == left ? untagged_addr - (info.start + info.size) 347 1.1 mrg : info.start - untagged_addr, 348 1.1 mrg candidate == left ? "right" : "left", info.size, info.name, 349 1.1 mrg info.start, info.start + info.size, module_name); 350 1.1 mrg } else { 351 1.1 mrg uptr size = GetGlobalSizeFromDescriptor(mem); 352 1.1 mrg if (size == 0) 353 1.1 mrg // We couldn't find the size of the global from the descriptors. 354 1.1 mrg Printf( 355 1.1 mrg "%p is located to the %s of a global variable in " 356 1.1 mrg "\n #0 0x%x (%s+0x%x)\n", 357 1.1 mrg untagged_addr, candidate == left ? "right" : "left", mem, 358 1.1 mrg module_name, module_address); 359 1.1 mrg else 360 1.1 mrg Printf( 361 1.1 mrg "%p is located to the %s of a %zd-byte global variable in " 362 1.1 mrg "\n #0 0x%x (%s+0x%x)\n", 363 1.1 mrg untagged_addr, candidate == left ? "right" : "left", size, mem, 364 1.1 mrg module_name, module_address); 365 1.1 mrg } 366 1.1 mrg Printf("%s", d.Default()); 367 1.1 mrg } 368 1.1 mrg } 369 1.1 mrg 370 1.1 mrg void PrintAddressDescription( 371 1.1 mrg uptr tagged_addr, uptr access_size, 372 1.1 mrg StackAllocationsRingBuffer *current_stack_allocations) { 373 1.1 mrg Decorator d; 374 1.1 mrg int num_descriptions_printed = 0; 375 1.1 mrg uptr untagged_addr = UntagAddr(tagged_addr); 376 1.1 mrg 377 1.1 mrg if (MemIsShadow(untagged_addr)) { 378 1.1 mrg Printf("%s%p is HWAsan shadow memory.\n%s", d.Location(), untagged_addr, 379 1.1 mrg d.Default()); 380 1.1 mrg return; 381 1.1 mrg } 382 1.1 mrg 383 1.1 mrg // Print some very basic information about the address, if it's a heap. 384 1.1 mrg HwasanChunkView chunk = FindHeapChunkByAddress(untagged_addr); 385 1.1 mrg if (uptr beg = chunk.Beg()) { 386 1.1 mrg uptr size = chunk.ActualSize(); 387 1.1 mrg Printf("%s[%p,%p) is a %s %s heap chunk; " 388 1.1 mrg "size: %zd offset: %zd\n%s", 389 1.1 mrg d.Location(), 390 1.1 mrg beg, beg + size, 391 1.1 mrg chunk.FromSmallHeap() ? "small" : "large", 392 1.1 mrg chunk.IsAllocated() ? "allocated" : "unallocated", 393 1.1 mrg size, untagged_addr - beg, 394 1.1 mrg d.Default()); 395 1.1 mrg } 396 1.1 mrg 397 1.1 mrg tag_t addr_tag = GetTagFromPointer(tagged_addr); 398 1.1 mrg 399 1.1 mrg bool on_stack = false; 400 1.1 mrg // Check stack first. If the address is on the stack of a live thread, we 401 1.1 mrg // know it cannot be a heap / global overflow. 402 1.1 mrg hwasanThreadList().VisitAllLiveThreads([&](Thread *t) { 403 1.1 mrg if (t->AddrIsInStack(untagged_addr)) { 404 1.1 mrg on_stack = true; 405 1.1 mrg // TODO(fmayer): figure out how to distinguish use-after-return and 406 1.1 mrg // stack-buffer-overflow. 407 1.1 mrg Printf("%s", d.Error()); 408 1.1 mrg Printf("\nCause: stack tag-mismatch\n"); 409 1.1 mrg Printf("%s", d.Location()); 410 1.1 mrg Printf("Address %p is located in stack of thread T%zd\n", untagged_addr, 411 1.1 mrg t->unique_id()); 412 1.1 mrg Printf("%s", d.Default()); 413 1.1 mrg t->Announce(); 414 1.1 mrg 415 1.1 mrg auto *sa = (t == GetCurrentThread() && current_stack_allocations) 416 1.1 mrg ? current_stack_allocations 417 1.1 mrg : t->stack_allocations(); 418 1.1 mrg PrintStackAllocations(sa, addr_tag, untagged_addr); 419 1.1 mrg num_descriptions_printed++; 420 1.1 mrg } 421 1.1 mrg }); 422 1.1 mrg 423 1.1 mrg // Check if this looks like a heap buffer overflow by scanning 424 1.1 mrg // the shadow left and right and looking for the first adjacent 425 1.1 mrg // object with a different memory tag. If that tag matches addr_tag, 426 1.1 mrg // check the allocator if it has a live chunk there. 427 1.1 mrg tag_t *tag_ptr = reinterpret_cast<tag_t*>(MemToShadow(untagged_addr)); 428 1.1 mrg tag_t *candidate = nullptr, *left = tag_ptr, *right = tag_ptr; 429 1.1 mrg uptr candidate_distance = 0; 430 1.1 mrg for (; candidate_distance < 1000; candidate_distance++) { 431 1.1 mrg if (MemIsShadow(reinterpret_cast<uptr>(left)) && 432 1.1 mrg TagsEqual(addr_tag, left)) { 433 1.1 mrg candidate = left; 434 1.1 mrg break; 435 1.1 mrg } 436 1.1 mrg --left; 437 1.1 mrg if (MemIsShadow(reinterpret_cast<uptr>(right)) && 438 1.1 mrg TagsEqual(addr_tag, right)) { 439 1.1 mrg candidate = right; 440 1.1 mrg break; 441 1.1 mrg } 442 1.1 mrg ++right; 443 1.1 mrg } 444 1.1 mrg 445 1.1 mrg constexpr auto kCloseCandidateDistance = 1; 446 1.1 mrg 447 1.1 mrg if (!on_stack && candidate && candidate_distance <= kCloseCandidateDistance) { 448 1.1 mrg ShowHeapOrGlobalCandidate(untagged_addr, candidate, left, right); 449 1.1 mrg num_descriptions_printed++; 450 1.1 mrg } 451 1.1 mrg 452 1.1 mrg hwasanThreadList().VisitAllLiveThreads([&](Thread *t) { 453 1.1 mrg // Scan all threads' ring buffers to find if it's a heap-use-after-free. 454 1.1 mrg HeapAllocationRecord har; 455 1.1 mrg uptr ring_index, num_matching_addrs, num_matching_addrs_4b; 456 1.1 mrg if (FindHeapAllocation(t->heap_allocations(), tagged_addr, &har, 457 1.1 mrg &ring_index, &num_matching_addrs, 458 1.1 mrg &num_matching_addrs_4b)) { 459 1.1 mrg Printf("%s", d.Error()); 460 1.1 mrg Printf("\nCause: use-after-free\n"); 461 1.1 mrg Printf("%s", d.Location()); 462 1.1 mrg Printf("%p is located %zd bytes inside of %zd-byte region [%p,%p)\n", 463 1.1 mrg untagged_addr, untagged_addr - UntagAddr(har.tagged_addr), 464 1.1 mrg har.requested_size, UntagAddr(har.tagged_addr), 465 1.1 mrg UntagAddr(har.tagged_addr) + har.requested_size); 466 1.1 mrg Printf("%s", d.Allocation()); 467 1.1 mrg Printf("freed by thread T%zd here:\n", t->unique_id()); 468 1.1 mrg Printf("%s", d.Default()); 469 1.1 mrg GetStackTraceFromId(har.free_context_id).Print(); 470 1.1 mrg 471 1.1 mrg Printf("%s", d.Allocation()); 472 1.1 mrg Printf("previously allocated here:\n", t); 473 1.1 mrg Printf("%s", d.Default()); 474 1.1 mrg GetStackTraceFromId(har.alloc_context_id).Print(); 475 1.1 mrg 476 1.1 mrg // Print a developer note: the index of this heap object 477 1.1 mrg // in the thread's deallocation ring buffer. 478 1.1 mrg Printf("hwasan_dev_note_heap_rb_distance: %zd %zd\n", ring_index + 1, 479 1.1 mrg flags()->heap_history_size); 480 1.1 mrg Printf("hwasan_dev_note_num_matching_addrs: %zd\n", num_matching_addrs); 481 1.1 mrg Printf("hwasan_dev_note_num_matching_addrs_4b: %zd\n", 482 1.1 mrg num_matching_addrs_4b); 483 1.1 mrg 484 1.1 mrg t->Announce(); 485 1.1 mrg num_descriptions_printed++; 486 1.1 mrg } 487 1.1 mrg }); 488 1.1 mrg 489 1.1 mrg if (candidate && num_descriptions_printed == 0) { 490 1.1 mrg ShowHeapOrGlobalCandidate(untagged_addr, candidate, left, right); 491 1.1 mrg num_descriptions_printed++; 492 1.1 mrg } 493 1.1 mrg 494 1.1 mrg // Print the remaining threads, as an extra information, 1 line per thread. 495 1.1 mrg hwasanThreadList().VisitAllLiveThreads([&](Thread *t) { t->Announce(); }); 496 1.1 mrg 497 1.1 mrg if (!num_descriptions_printed) 498 1.1 mrg // We exhausted our possibilities. Bail out. 499 1.1 mrg Printf("HWAddressSanitizer can not describe address in more detail.\n"); 500 1.1 mrg if (num_descriptions_printed > 1) { 501 1.1 mrg Printf( 502 1.1 mrg "There are %d potential causes, printed above in order " 503 1.1 mrg "of likeliness.\n", 504 1.1 mrg num_descriptions_printed); 505 1.1 mrg } 506 1.1 mrg } 507 1.1 mrg 508 1.1 mrg void ReportStats() {} 509 1.1 mrg 510 1.1 mrg static void PrintTagInfoAroundAddr(tag_t *tag_ptr, uptr num_rows, 511 1.1 mrg void (*print_tag)(InternalScopedString &s, 512 1.1 mrg tag_t *tag)) { 513 1.1 mrg const uptr row_len = 16; // better be power of two. 514 1.1 mrg tag_t *center_row_beg = reinterpret_cast<tag_t *>( 515 1.1 mrg RoundDownTo(reinterpret_cast<uptr>(tag_ptr), row_len)); 516 1.1 mrg tag_t *beg_row = center_row_beg - row_len * (num_rows / 2); 517 1.1 mrg tag_t *end_row = center_row_beg + row_len * ((num_rows + 1) / 2); 518 1.1 mrg InternalScopedString s; 519 1.1 mrg for (tag_t *row = beg_row; row < end_row; row += row_len) { 520 1.1 mrg s.append("%s", row == center_row_beg ? "=>" : " "); 521 1.1 mrg s.append("%p:", (void *)row); 522 1.1 mrg for (uptr i = 0; i < row_len; i++) { 523 1.1 mrg s.append("%s", row + i == tag_ptr ? "[" : " "); 524 1.1 mrg print_tag(s, &row[i]); 525 1.1 mrg s.append("%s", row + i == tag_ptr ? "]" : " "); 526 1.1 mrg } 527 1.1 mrg s.append("\n"); 528 1.1 mrg } 529 1.1 mrg Printf("%s", s.data()); 530 1.1 mrg } 531 1.1 mrg 532 1.1 mrg static void PrintTagsAroundAddr(tag_t *tag_ptr) { 533 1.1 mrg Printf( 534 1.1 mrg "Memory tags around the buggy address (one tag corresponds to %zd " 535 1.1 mrg "bytes):\n", kShadowAlignment); 536 1.1 mrg PrintTagInfoAroundAddr(tag_ptr, 17, [](InternalScopedString &s, tag_t *tag) { 537 1.1 mrg s.append("%02x", *tag); 538 1.1 mrg }); 539 1.1 mrg 540 1.1 mrg Printf( 541 1.1 mrg "Tags for short granules around the buggy address (one tag corresponds " 542 1.1 mrg "to %zd bytes):\n", 543 1.1 mrg kShadowAlignment); 544 1.1 mrg PrintTagInfoAroundAddr(tag_ptr, 3, [](InternalScopedString &s, tag_t *tag) { 545 1.1 mrg if (*tag >= 1 && *tag <= kShadowAlignment) { 546 1.1 mrg uptr granule_addr = ShadowToMem(reinterpret_cast<uptr>(tag)); 547 1.1 mrg s.append("%02x", 548 1.1 mrg *reinterpret_cast<u8 *>(granule_addr + kShadowAlignment - 1)); 549 1.1 mrg } else { 550 1.1 mrg s.append(".."); 551 1.1 mrg } 552 1.1 mrg }); 553 1.1 mrg Printf( 554 1.1 mrg "See " 555 1.1 mrg "https://clang.llvm.org/docs/" 556 1.1 mrg "HardwareAssistedAddressSanitizerDesign.html#short-granules for a " 557 1.1 mrg "description of short granule tags\n"); 558 1.1 mrg } 559 1.1 mrg 560 1.1 mrg uptr GetTopPc(StackTrace *stack) { 561 1.1 mrg return stack->size ? StackTrace::GetPreviousInstructionPc(stack->trace[0]) 562 1.1 mrg : 0; 563 1.1 mrg } 564 1.1 mrg 565 1.1 mrg void ReportInvalidFree(StackTrace *stack, uptr tagged_addr) { 566 1.1 mrg ScopedReport R(flags()->halt_on_error); 567 1.1 mrg 568 1.1 mrg uptr untagged_addr = UntagAddr(tagged_addr); 569 1.1 mrg tag_t ptr_tag = GetTagFromPointer(tagged_addr); 570 1.1 mrg tag_t *tag_ptr = nullptr; 571 1.1 mrg tag_t mem_tag = 0; 572 1.1 mrg if (MemIsApp(untagged_addr)) { 573 1.1 mrg tag_ptr = reinterpret_cast<tag_t *>(MemToShadow(untagged_addr)); 574 1.1 mrg if (MemIsShadow(reinterpret_cast<uptr>(tag_ptr))) 575 1.1 mrg mem_tag = *tag_ptr; 576 1.1 mrg else 577 1.1 mrg tag_ptr = nullptr; 578 1.1 mrg } 579 1.1 mrg Decorator d; 580 1.1 mrg Printf("%s", d.Error()); 581 1.1 mrg uptr pc = GetTopPc(stack); 582 1.1 mrg const char *bug_type = "invalid-free"; 583 1.1 mrg const Thread *thread = GetCurrentThread(); 584 1.1 mrg if (thread) { 585 1.1 mrg Report("ERROR: %s: %s on address %p at pc %p on thread T%zd\n", 586 1.1 mrg SanitizerToolName, bug_type, untagged_addr, pc, thread->unique_id()); 587 1.1 mrg } else { 588 1.1 mrg Report("ERROR: %s: %s on address %p at pc %p on unknown thread\n", 589 1.1 mrg SanitizerToolName, bug_type, untagged_addr, pc); 590 1.1 mrg } 591 1.1 mrg Printf("%s", d.Access()); 592 1.1 mrg if (tag_ptr) 593 1.1 mrg Printf("tags: %02x/%02x (ptr/mem)\n", ptr_tag, mem_tag); 594 1.1 mrg Printf("%s", d.Default()); 595 1.1 mrg 596 1.1 mrg stack->Print(); 597 1.1 mrg 598 1.1 mrg PrintAddressDescription(tagged_addr, 0, nullptr); 599 1.1 mrg 600 1.1 mrg if (tag_ptr) 601 1.1 mrg PrintTagsAroundAddr(tag_ptr); 602 1.1 mrg 603 1.1 mrg ReportErrorSummary(bug_type, stack); 604 1.1 mrg } 605 1.1 mrg 606 1.1 mrg void ReportTailOverwritten(StackTrace *stack, uptr tagged_addr, uptr orig_size, 607 1.1 mrg const u8 *expected) { 608 1.1 mrg uptr tail_size = kShadowAlignment - (orig_size % kShadowAlignment); 609 1.1 mrg u8 actual_expected[kShadowAlignment]; 610 1.1 mrg internal_memcpy(actual_expected, expected, tail_size); 611 1.1 mrg tag_t ptr_tag = GetTagFromPointer(tagged_addr); 612 1.1 mrg // Short granule is stashed in the last byte of the magic string. To avoid 613 1.1 mrg // confusion, make the expected magic string contain the short granule tag. 614 1.1 mrg if (orig_size % kShadowAlignment != 0) { 615 1.1 mrg actual_expected[tail_size - 1] = ptr_tag; 616 1.1 mrg } 617 1.1 mrg 618 1.1 mrg ScopedReport R(flags()->halt_on_error); 619 1.1 mrg Decorator d; 620 1.1 mrg uptr untagged_addr = UntagAddr(tagged_addr); 621 1.1 mrg Printf("%s", d.Error()); 622 1.1 mrg const char *bug_type = "allocation-tail-overwritten"; 623 1.1 mrg Report("ERROR: %s: %s; heap object [%p,%p) of size %zd\n", SanitizerToolName, 624 1.1 mrg bug_type, untagged_addr, untagged_addr + orig_size, orig_size); 625 1.1 mrg Printf("\n%s", d.Default()); 626 1.1 mrg Printf( 627 1.1 mrg "Stack of invalid access unknown. Issue detected at deallocation " 628 1.1 mrg "time.\n"); 629 1.1 mrg Printf("%s", d.Allocation()); 630 1.1 mrg Printf("deallocated here:\n"); 631 1.1 mrg Printf("%s", d.Default()); 632 1.1 mrg stack->Print(); 633 1.1 mrg HwasanChunkView chunk = FindHeapChunkByAddress(untagged_addr); 634 1.1 mrg if (chunk.Beg()) { 635 1.1 mrg Printf("%s", d.Allocation()); 636 1.1 mrg Printf("allocated here:\n"); 637 1.1 mrg Printf("%s", d.Default()); 638 1.1 mrg GetStackTraceFromId(chunk.GetAllocStackId()).Print(); 639 1.1 mrg } 640 1.1 mrg 641 1.1 mrg InternalScopedString s; 642 1.1 mrg CHECK_GT(tail_size, 0U); 643 1.1 mrg CHECK_LT(tail_size, kShadowAlignment); 644 1.1 mrg u8 *tail = reinterpret_cast<u8*>(untagged_addr + orig_size); 645 1.1 mrg s.append("Tail contains: "); 646 1.1 mrg for (uptr i = 0; i < kShadowAlignment - tail_size; i++) 647 1.1 mrg s.append(".. "); 648 1.1 mrg for (uptr i = 0; i < tail_size; i++) 649 1.1 mrg s.append("%02x ", tail[i]); 650 1.1 mrg s.append("\n"); 651 1.1 mrg s.append("Expected: "); 652 1.1 mrg for (uptr i = 0; i < kShadowAlignment - tail_size; i++) 653 1.1 mrg s.append(".. "); 654 1.1 mrg for (uptr i = 0; i < tail_size; i++) s.append("%02x ", actual_expected[i]); 655 1.1 mrg s.append("\n"); 656 1.1 mrg s.append(" "); 657 1.1 mrg for (uptr i = 0; i < kShadowAlignment - tail_size; i++) 658 1.1 mrg s.append(" "); 659 1.1 mrg for (uptr i = 0; i < tail_size; i++) 660 1.1 mrg s.append("%s ", actual_expected[i] != tail[i] ? "^^" : " "); 661 1.1 mrg 662 1.1 mrg s.append("\nThis error occurs when a buffer overflow overwrites memory\n" 663 1.1 mrg "to the right of a heap object, but within the %zd-byte granule, e.g.\n" 664 1.1 mrg " char *x = new char[20];\n" 665 1.1 mrg " x[25] = 42;\n" 666 1.1 mrg "%s does not detect such bugs in uninstrumented code at the time of write," 667 1.1 mrg "\nbut can detect them at the time of free/delete.\n" 668 1.1 mrg "To disable this feature set HWASAN_OPTIONS=free_checks_tail_magic=0\n", 669 1.1 mrg kShadowAlignment, SanitizerToolName); 670 1.1 mrg Printf("%s", s.data()); 671 1.1 mrg GetCurrentThread()->Announce(); 672 1.1 mrg 673 1.1 mrg tag_t *tag_ptr = reinterpret_cast<tag_t*>(MemToShadow(untagged_addr)); 674 1.1 mrg PrintTagsAroundAddr(tag_ptr); 675 1.1 mrg 676 1.1 mrg ReportErrorSummary(bug_type, stack); 677 1.1 mrg } 678 1.1 mrg 679 1.1 mrg void ReportTagMismatch(StackTrace *stack, uptr tagged_addr, uptr access_size, 680 1.1 mrg bool is_store, bool fatal, uptr *registers_frame) { 681 1.1 mrg ScopedReport R(fatal); 682 1.1 mrg SavedStackAllocations current_stack_allocations( 683 1.1 mrg GetCurrentThread()->stack_allocations()); 684 1.1 mrg 685 1.1 mrg Decorator d; 686 1.1 mrg uptr untagged_addr = UntagAddr(tagged_addr); 687 1.1 mrg // TODO: when possible, try to print heap-use-after-free, etc. 688 1.1 mrg const char *bug_type = "tag-mismatch"; 689 1.1 mrg uptr pc = GetTopPc(stack); 690 1.1 mrg Printf("%s", d.Error()); 691 1.1 mrg Report("ERROR: %s: %s on address %p at pc %p\n", SanitizerToolName, bug_type, 692 1.1 mrg untagged_addr, pc); 693 1.1 mrg 694 1.1 mrg Thread *t = GetCurrentThread(); 695 1.1 mrg 696 1.1 mrg sptr offset = 697 1.1 mrg __hwasan_test_shadow(reinterpret_cast<void *>(tagged_addr), access_size); 698 1.1 mrg CHECK(offset >= 0 && offset < static_cast<sptr>(access_size)); 699 1.1 mrg tag_t ptr_tag = GetTagFromPointer(tagged_addr); 700 1.1 mrg tag_t *tag_ptr = 701 1.1 mrg reinterpret_cast<tag_t *>(MemToShadow(untagged_addr + offset)); 702 1.1 mrg tag_t mem_tag = *tag_ptr; 703 1.1 mrg 704 1.1 mrg Printf("%s", d.Access()); 705 1.1 mrg if (mem_tag && mem_tag < kShadowAlignment) { 706 1.1 mrg tag_t *granule_ptr = reinterpret_cast<tag_t *>((untagged_addr + offset) & 707 1.1 mrg ~(kShadowAlignment - 1)); 708 1.1 mrg // If offset is 0, (untagged_addr + offset) is not aligned to granules. 709 1.1 mrg // This is the offset of the leftmost accessed byte within the bad granule. 710 1.1 mrg u8 in_granule_offset = (untagged_addr + offset) & (kShadowAlignment - 1); 711 1.1 mrg tag_t short_tag = granule_ptr[kShadowAlignment - 1]; 712 1.1 mrg // The first mismatch was a short granule that matched the ptr_tag. 713 1.1 mrg if (short_tag == ptr_tag) { 714 1.1 mrg // If the access starts after the end of the short granule, then the first 715 1.1 mrg // bad byte is the first byte of the access; otherwise it is the first 716 1.1 mrg // byte past the end of the short granule 717 1.1 mrg if (mem_tag > in_granule_offset) { 718 1.1 mrg offset += mem_tag - in_granule_offset; 719 1.1 mrg } 720 1.1 mrg } 721 1.1 mrg Printf( 722 1.1 mrg "%s of size %zu at %p tags: %02x/%02x(%02x) (ptr/mem) in thread T%zd\n", 723 1.1 mrg is_store ? "WRITE" : "READ", access_size, untagged_addr, ptr_tag, 724 1.1 mrg mem_tag, short_tag, t->unique_id()); 725 1.1 mrg } else { 726 1.1 mrg Printf("%s of size %zu at %p tags: %02x/%02x (ptr/mem) in thread T%zd\n", 727 1.1 mrg is_store ? "WRITE" : "READ", access_size, untagged_addr, ptr_tag, 728 1.1 mrg mem_tag, t->unique_id()); 729 1.1 mrg } 730 1.1 mrg if (offset != 0) 731 1.1 mrg Printf("Invalid access starting at offset %zu\n", offset); 732 1.1 mrg Printf("%s", d.Default()); 733 1.1 mrg 734 1.1 mrg stack->Print(); 735 1.1 mrg 736 1.1 mrg PrintAddressDescription(tagged_addr, access_size, 737 1.1 mrg current_stack_allocations.get()); 738 1.1 mrg t->Announce(); 739 1.1 mrg 740 1.1 mrg PrintTagsAroundAddr(tag_ptr); 741 1.1 mrg 742 1.1 mrg if (registers_frame) 743 1.1 mrg ReportRegisters(registers_frame, pc); 744 1.1 mrg 745 1.1 mrg ReportErrorSummary(bug_type, stack); 746 1.1 mrg } 747 1.1 mrg 748 1.1 mrg // See the frame breakdown defined in __hwasan_tag_mismatch (from 749 1.1 mrg // hwasan_tag_mismatch_aarch64.S). 750 1.1 mrg void ReportRegisters(uptr *frame, uptr pc) { 751 1.1 mrg Printf("Registers where the failure occurred (pc %p):\n", pc); 752 1.1 mrg 753 1.1 mrg // We explicitly print a single line (4 registers/line) each iteration to 754 1.1 mrg // reduce the amount of logcat error messages printed. Each Printf() will 755 1.1 mrg // result in a new logcat line, irrespective of whether a newline is present, 756 1.1 mrg // and so we wish to reduce the number of Printf() calls we have to make. 757 1.1 mrg Printf(" x0 %016llx x1 %016llx x2 %016llx x3 %016llx\n", 758 1.1 mrg frame[0], frame[1], frame[2], frame[3]); 759 1.1 mrg Printf(" x4 %016llx x5 %016llx x6 %016llx x7 %016llx\n", 760 1.1 mrg frame[4], frame[5], frame[6], frame[7]); 761 1.1 mrg Printf(" x8 %016llx x9 %016llx x10 %016llx x11 %016llx\n", 762 1.1 mrg frame[8], frame[9], frame[10], frame[11]); 763 1.1 mrg Printf(" x12 %016llx x13 %016llx x14 %016llx x15 %016llx\n", 764 1.1 mrg frame[12], frame[13], frame[14], frame[15]); 765 1.1 mrg Printf(" x16 %016llx x17 %016llx x18 %016llx x19 %016llx\n", 766 1.1 mrg frame[16], frame[17], frame[18], frame[19]); 767 1.1 mrg Printf(" x20 %016llx x21 %016llx x22 %016llx x23 %016llx\n", 768 1.1 mrg frame[20], frame[21], frame[22], frame[23]); 769 1.1 mrg Printf(" x24 %016llx x25 %016llx x26 %016llx x27 %016llx\n", 770 1.1 mrg frame[24], frame[25], frame[26], frame[27]); 771 1.1 mrg // hwasan_check* reduces the stack pointer by 256, then __hwasan_tag_mismatch 772 1.1 mrg // passes it to this function. 773 1.1 mrg Printf(" x28 %016llx x29 %016llx x30 %016llx sp %016llx\n", frame[28], 774 1.1 mrg frame[29], frame[30], reinterpret_cast<u8 *>(frame) + 256); 775 1.1 mrg } 776 1.1 mrg 777 1.1 mrg } // namespace __hwasan 778 1.1 mrg 779 1.1 mrg void __hwasan_set_error_report_callback(void (*callback)(const char *)) { 780 1.1 mrg __hwasan::ScopedReport::SetErrorReportCallback(callback); 781 1.1 mrg } 782