asan_report.cc revision ac3ae5dc29d3623ada2bcb0db22ee88b0382e3b1
1//===-- asan_report.cc ----------------------------------------------------===// 2// 3// The LLVM Compiler Infrastructure 4// 5// This file is distributed under the University of Illinois Open Source 6// License. See LICENSE.TXT for details. 7// 8//===----------------------------------------------------------------------===// 9// 10// This file is a part of AddressSanitizer, an address sanity checker. 11// 12// This file contains error reporting code. 13//===----------------------------------------------------------------------===// 14#include "asan_flags.h" 15#include "asan_internal.h" 16#include "asan_mapping.h" 17#include "asan_report.h" 18#include "asan_stack.h" 19#include "asan_thread.h" 20#include "sanitizer_common/sanitizer_common.h" 21#include "sanitizer_common/sanitizer_flags.h" 22#include "sanitizer_common/sanitizer_report_decorator.h" 23#include "sanitizer_common/sanitizer_symbolizer.h" 24 25namespace __asan { 26 27// -------------------- User-specified callbacks ----------------- {{{1 28static void (*error_report_callback)(const char*); 29static char *error_message_buffer = 0; 30static uptr error_message_buffer_pos = 0; 31static uptr error_message_buffer_size = 0; 32 33void AppendToErrorMessageBuffer(const char *buffer) { 34 if (error_message_buffer) { 35 uptr length = internal_strlen(buffer); 36 CHECK_GE(error_message_buffer_size, error_message_buffer_pos); 37 uptr remaining = error_message_buffer_size - error_message_buffer_pos; 38 internal_strncpy(error_message_buffer + error_message_buffer_pos, 39 buffer, remaining); 40 error_message_buffer[error_message_buffer_size - 1] = '\0'; 41 // FIXME: reallocate the buffer instead of truncating the message. 42 error_message_buffer_pos += remaining > length ? length : remaining; 43 } 44} 45 46// ---------------------- Decorator ------------------------------ {{{1 47class Decorator: private __sanitizer::AnsiColorDecorator { 48 public: 49 Decorator() : __sanitizer::AnsiColorDecorator(PrintsToTtyCached()) { } 50 const char *Warning() { return Red(); } 51 const char *EndWarning() { return Default(); } 52 const char *Access() { return Blue(); } 53 const char *EndAccess() { return Default(); } 54 const char *Location() { return Green(); } 55 const char *EndLocation() { return Default(); } 56 const char *Allocation() { return Magenta(); } 57 const char *EndAllocation() { return Default(); } 58 59 const char *ShadowByte(u8 byte) { 60 switch (byte) { 61 case kAsanHeapLeftRedzoneMagic: 62 case kAsanHeapRightRedzoneMagic: 63 return Red(); 64 case kAsanHeapFreeMagic: 65 return Magenta(); 66 case kAsanStackLeftRedzoneMagic: 67 case kAsanStackMidRedzoneMagic: 68 case kAsanStackRightRedzoneMagic: 69 case kAsanStackPartialRedzoneMagic: 70 return Red(); 71 case kAsanStackAfterReturnMagic: 72 return Magenta(); 73 case kAsanInitializationOrderMagic: 74 return Cyan(); 75 case kAsanUserPoisonedMemoryMagic: 76 return Blue(); 77 case kAsanStackUseAfterScopeMagic: 78 return Magenta(); 79 case kAsanGlobalRedzoneMagic: 80 return Red(); 81 case kAsanInternalHeapMagic: 82 return Yellow(); 83 default: 84 return Default(); 85 } 86 } 87 const char *EndShadowByte() { return Default(); } 88}; 89 90// ---------------------- Helper functions ----------------------- {{{1 91 92static void PrintShadowByte(const char *before, u8 byte, 93 const char *after = "\n") { 94 Decorator d; 95 Printf("%s%s%x%x%s%s", before, 96 d.ShadowByte(byte), byte >> 4, byte & 15, d.EndShadowByte(), after); 97} 98 99static void PrintShadowBytes(const char *before, u8 *bytes, 100 u8 *guilty, uptr n) { 101 Decorator d; 102 if (before) 103 Printf("%s%p:", before, bytes); 104 for (uptr i = 0; i < n; i++) { 105 u8 *p = bytes + i; 106 const char *before = p == guilty ? "[" : 107 (p - 1 == guilty && i != 0) ? "" : " "; 108 const char *after = p == guilty ? "]" : ""; 109 PrintShadowByte(before, *p, after); 110 } 111 Printf("\n"); 112} 113 114static void PrintLegend() { 115 Printf("Shadow byte legend (one shadow byte represents %d " 116 "application bytes):\n", (int)SHADOW_GRANULARITY); 117 PrintShadowByte(" Addressable: ", 0); 118 Printf(" Partially addressable: "); 119 for (u8 i = 1; i < SHADOW_GRANULARITY; i++) 120 PrintShadowByte("", i, " "); 121 Printf("\n"); 122 PrintShadowByte(" Heap left redzone: ", kAsanHeapLeftRedzoneMagic); 123 PrintShadowByte(" Heap right redzone: ", kAsanHeapRightRedzoneMagic); 124 PrintShadowByte(" Freed heap region: ", kAsanHeapFreeMagic); 125 PrintShadowByte(" Stack left redzone: ", kAsanStackLeftRedzoneMagic); 126 PrintShadowByte(" Stack mid redzone: ", kAsanStackMidRedzoneMagic); 127 PrintShadowByte(" Stack right redzone: ", kAsanStackRightRedzoneMagic); 128 PrintShadowByte(" Stack partial redzone: ", kAsanStackPartialRedzoneMagic); 129 PrintShadowByte(" Stack after return: ", kAsanStackAfterReturnMagic); 130 PrintShadowByte(" Stack use after scope: ", kAsanStackUseAfterScopeMagic); 131 PrintShadowByte(" Global redzone: ", kAsanGlobalRedzoneMagic); 132 PrintShadowByte(" Global init order: ", kAsanInitializationOrderMagic); 133 PrintShadowByte(" Poisoned by user: ", kAsanUserPoisonedMemoryMagic); 134 PrintShadowByte(" ASan internal: ", kAsanInternalHeapMagic); 135} 136 137static void PrintShadowMemoryForAddress(uptr addr) { 138 if (!AddrIsInMem(addr)) 139 return; 140 uptr shadow_addr = MemToShadow(addr); 141 const uptr n_bytes_per_row = 16; 142 uptr aligned_shadow = shadow_addr & ~(n_bytes_per_row - 1); 143 Printf("Shadow bytes around the buggy address:\n"); 144 for (int i = -5; i <= 5; i++) { 145 const char *prefix = (i == 0) ? "=>" : " "; 146 PrintShadowBytes(prefix, 147 (u8*)(aligned_shadow + i * n_bytes_per_row), 148 (u8*)shadow_addr, n_bytes_per_row); 149 } 150 if (flags()->print_legend) 151 PrintLegend(); 152} 153 154static void PrintZoneForPointer(uptr ptr, uptr zone_ptr, 155 const char *zone_name) { 156 if (zone_ptr) { 157 if (zone_name) { 158 Printf("malloc_zone_from_ptr(%p) = %p, which is %s\n", 159 ptr, zone_ptr, zone_name); 160 } else { 161 Printf("malloc_zone_from_ptr(%p) = %p, which doesn't have a name\n", 162 ptr, zone_ptr); 163 } 164 } else { 165 Printf("malloc_zone_from_ptr(%p) = 0\n", ptr); 166 } 167} 168 169static void DescribeThread(AsanThread *t) { 170 if (t) 171 DescribeThread(t->context()); 172} 173 174// ---------------------- Address Descriptions ------------------- {{{1 175 176static bool IsASCII(unsigned char c) { 177 return /*0x00 <= c &&*/ c <= 0x7F; 178} 179 180static const char *MaybeDemangleGlobalName(const char *name) { 181 // We can spoil names of globals with C linkage, so use an heuristic 182 // approach to check if the name should be demangled. 183 return (name[0] == '_' && name[1] == 'Z') ? getSymbolizer()->Demangle(name) 184 : name; 185} 186 187// Check if the global is a zero-terminated ASCII string. If so, print it. 188static void PrintGlobalNameIfASCII(const __asan_global &g) { 189 for (uptr p = g.beg; p < g.beg + g.size - 1; p++) { 190 unsigned char c = *(unsigned char*)p; 191 if (c == '\0' || !IsASCII(c)) return; 192 } 193 if (*(char*)(g.beg + g.size - 1) != '\0') return; 194 Printf(" '%s' is ascii string '%s'\n", 195 MaybeDemangleGlobalName(g.name), (char*)g.beg); 196} 197 198bool DescribeAddressRelativeToGlobal(uptr addr, uptr size, 199 const __asan_global &g) { 200 static const uptr kMinimalDistanceFromAnotherGlobal = 64; 201 if (addr <= g.beg - kMinimalDistanceFromAnotherGlobal) return false; 202 if (addr >= g.beg + g.size_with_redzone) return false; 203 Decorator d; 204 Printf("%s", d.Location()); 205 if (addr < g.beg) { 206 Printf("%p is located %zd bytes to the left", (void*)addr, g.beg - addr); 207 } else if (addr + size > g.beg + g.size) { 208 if (addr < g.beg + g.size) 209 addr = g.beg + g.size; 210 Printf("%p is located %zd bytes to the right", (void*)addr, 211 addr - (g.beg + g.size)); 212 } else { 213 // Can it happen? 214 Printf("%p is located %zd bytes inside", (void*)addr, addr - g.beg); 215 } 216 Printf(" of global variable '%s' from '%s' (0x%zx) of size %zu\n", 217 MaybeDemangleGlobalName(g.name), g.module_name, g.beg, g.size); 218 Printf("%s", d.EndLocation()); 219 PrintGlobalNameIfASCII(g); 220 return true; 221} 222 223bool DescribeAddressIfShadow(uptr addr) { 224 if (AddrIsInMem(addr)) 225 return false; 226 static const char kAddrInShadowReport[] = 227 "Address %p is located in the %s.\n"; 228 if (AddrIsInShadowGap(addr)) { 229 Printf(kAddrInShadowReport, addr, "shadow gap area"); 230 return true; 231 } 232 if (AddrIsInHighShadow(addr)) { 233 Printf(kAddrInShadowReport, addr, "high shadow area"); 234 return true; 235 } 236 if (AddrIsInLowShadow(addr)) { 237 Printf(kAddrInShadowReport, addr, "low shadow area"); 238 return true; 239 } 240 CHECK(0 && "Address is not in memory and not in shadow?"); 241 return false; 242} 243 244// Return " (thread_name) " or an empty string if the name is empty. 245const char *ThreadNameWithParenthesis(AsanThreadContext *t, char buff[], 246 uptr buff_len) { 247 const char *name = t->name; 248 if (name[0] == '\0') return ""; 249 buff[0] = 0; 250 internal_strncat(buff, " (", 3); 251 internal_strncat(buff, name, buff_len - 4); 252 internal_strncat(buff, ")", 2); 253 return buff; 254} 255 256const char *ThreadNameWithParenthesis(u32 tid, char buff[], 257 uptr buff_len) { 258 if (tid == kInvalidTid) return ""; 259 asanThreadRegistry().CheckLocked(); 260 AsanThreadContext *t = GetThreadContextByTidLocked(tid); 261 return ThreadNameWithParenthesis(t, buff, buff_len); 262} 263 264void PrintAccessAndVarIntersection(const char *var_name, 265 uptr var_beg, uptr var_size, 266 uptr addr, uptr access_size, 267 uptr prev_var_end, uptr next_var_beg) { 268 uptr var_end = var_beg + var_size; 269 uptr addr_end = addr + access_size; 270 const char *pos_descr = 0; 271 // If the variable [var_beg, var_end) is the nearest variable to the 272 // current memory access, indicate it in the log. 273 if (addr >= var_beg) { 274 if (addr_end <= var_end) 275 pos_descr = "is inside"; // May happen if this is a use-after-return. 276 else if (addr < var_end) 277 pos_descr = "partially overflows"; 278 else if (addr_end <= next_var_beg && 279 next_var_beg - addr_end >= addr - var_end) 280 pos_descr = "overflows"; 281 } else { 282 if (addr_end > var_beg) 283 pos_descr = "partially underflows"; 284 else if (addr >= prev_var_end && 285 addr - prev_var_end >= var_beg - addr_end) 286 pos_descr = "underflows"; 287 } 288 Printf(" [%zd, %zd) '%s'", var_beg, var_beg + var_size, var_name); 289 if (pos_descr) { 290 Decorator d; 291 // FIXME: we may want to also print the size of the access here, 292 // but in case of accesses generated by memset it may be confusing. 293 Printf("%s <== Memory access at offset %zd %s this variable%s\n", 294 d.Location(), addr, pos_descr, d.EndLocation()); 295 } else { 296 Printf("\n"); 297 } 298} 299 300struct StackVarDescr { 301 uptr beg; 302 uptr size; 303 const char *name_pos; 304 uptr name_len; 305}; 306 307bool DescribeAddressIfStack(uptr addr, uptr access_size) { 308 AsanThread *t = FindThreadByStackAddress(addr); 309 if (!t) return false; 310 const uptr kBufSize = 4095; 311 char buf[kBufSize]; 312 uptr offset = 0; 313 uptr frame_pc = 0; 314 char tname[128]; 315 const char *frame_descr = t->GetFrameNameByAddr(addr, &offset, &frame_pc); 316 317#ifdef __powerpc64__ 318 // On PowerPC64, the address of a function actually points to a 319 // three-doubleword data structure with the first field containing 320 // the address of the function's code. 321 frame_pc = *reinterpret_cast<uptr *>(frame_pc); 322#endif 323 324 // This string is created by the compiler and has the following form: 325 // "n alloc_1 alloc_2 ... alloc_n" 326 // where alloc_i looks like "offset size len ObjectName ". 327 CHECK(frame_descr); 328 Decorator d; 329 Printf("%s", d.Location()); 330 Printf("Address %p is located in stack of thread T%d%s " 331 "at offset %zu in frame\n", 332 addr, t->tid(), 333 ThreadNameWithParenthesis(t->tid(), tname, sizeof(tname)), 334 offset); 335 // Now we print the frame where the alloca has happened. 336 // We print this frame as a stack trace with one element. 337 // The symbolizer may print more than one frame if inlining was involved. 338 // The frame numbers may be different than those in the stack trace printed 339 // previously. That's unfortunate, but I have no better solution, 340 // especially given that the alloca may be from entirely different place 341 // (e.g. use-after-scope, or different thread's stack). 342 StackTrace alloca_stack; 343 alloca_stack.trace[0] = frame_pc + 16; 344 alloca_stack.size = 1; 345 Printf("%s", d.EndLocation()); 346 PrintStack(&alloca_stack); 347 // Report the number of stack objects. 348 char *p; 349 uptr n_objects = (uptr)internal_simple_strtoll(frame_descr, &p, 10); 350 CHECK_GT(n_objects, 0); 351 Printf(" This frame has %zu object(s):\n", n_objects); 352 353 // Report all objects in this frame. 354 InternalScopedBuffer<StackVarDescr> vars(n_objects); 355 for (uptr i = 0; i < n_objects; i++) { 356 uptr beg, size; 357 uptr len; 358 beg = (uptr)internal_simple_strtoll(p, &p, 10); 359 size = (uptr)internal_simple_strtoll(p, &p, 10); 360 len = (uptr)internal_simple_strtoll(p, &p, 10); 361 if (beg == 0 || size == 0 || *p != ' ') { 362 Printf("AddressSanitizer can't parse the stack frame " 363 "descriptor: |%s|\n", frame_descr); 364 break; 365 } 366 p++; 367 vars[i].beg = beg; 368 vars[i].size = size; 369 vars[i].name_pos = p; 370 vars[i].name_len = len; 371 p += len; 372 } 373 for (uptr i = 0; i < n_objects; i++) { 374 buf[0] = 0; 375 internal_strncat(buf, vars[i].name_pos, 376 static_cast<uptr>(Min(kBufSize, vars[i].name_len))); 377 uptr prev_var_end = i ? vars[i - 1].beg + vars[i - 1].size : 0; 378 uptr next_var_beg = i + 1 < n_objects ? vars[i + 1].beg : ~(0UL); 379 PrintAccessAndVarIntersection(buf, vars[i].beg, vars[i].size, 380 offset, access_size, 381 prev_var_end, next_var_beg); 382 } 383 Printf("HINT: this may be a false positive if your program uses " 384 "some custom stack unwind mechanism or swapcontext\n" 385 " (longjmp and C++ exceptions *are* supported)\n"); 386 DescribeThread(t); 387 return true; 388} 389 390static void DescribeAccessToHeapChunk(AsanChunkView chunk, uptr addr, 391 uptr access_size) { 392 sptr offset; 393 Decorator d; 394 Printf("%s", d.Location()); 395 if (chunk.AddrIsAtLeft(addr, access_size, &offset)) { 396 Printf("%p is located %zd bytes to the left of", (void*)addr, offset); 397 } else if (chunk.AddrIsAtRight(addr, access_size, &offset)) { 398 if (offset < 0) { 399 addr -= offset; 400 offset = 0; 401 } 402 Printf("%p is located %zd bytes to the right of", (void*)addr, offset); 403 } else if (chunk.AddrIsInside(addr, access_size, &offset)) { 404 Printf("%p is located %zd bytes inside of", (void*)addr, offset); 405 } else { 406 Printf("%p is located somewhere around (this is AddressSanitizer bug!)", 407 (void*)addr); 408 } 409 Printf(" %zu-byte region [%p,%p)\n", chunk.UsedSize(), 410 (void*)(chunk.Beg()), (void*)(chunk.End())); 411 Printf("%s", d.EndLocation()); 412} 413 414void DescribeHeapAddress(uptr addr, uptr access_size) { 415 AsanChunkView chunk = FindHeapChunkByAddress(addr); 416 if (!chunk.IsValid()) return; 417 DescribeAccessToHeapChunk(chunk, addr, access_size); 418 CHECK(chunk.AllocTid() != kInvalidTid); 419 asanThreadRegistry().CheckLocked(); 420 AsanThreadContext *alloc_thread = 421 GetThreadContextByTidLocked(chunk.AllocTid()); 422 StackTrace alloc_stack; 423 chunk.GetAllocStack(&alloc_stack); 424 char tname[128]; 425 Decorator d; 426 AsanThreadContext *free_thread = 0; 427 if (chunk.FreeTid() != kInvalidTid) { 428 free_thread = GetThreadContextByTidLocked(chunk.FreeTid()); 429 Printf("%sfreed by thread T%d%s here:%s\n", d.Allocation(), 430 free_thread->tid, 431 ThreadNameWithParenthesis(free_thread, tname, sizeof(tname)), 432 d.EndAllocation()); 433 StackTrace free_stack; 434 chunk.GetFreeStack(&free_stack); 435 PrintStack(&free_stack); 436 Printf("%spreviously allocated by thread T%d%s here:%s\n", 437 d.Allocation(), alloc_thread->tid, 438 ThreadNameWithParenthesis(alloc_thread, tname, sizeof(tname)), 439 d.EndAllocation()); 440 } else { 441 Printf("%sallocated by thread T%d%s here:%s\n", d.Allocation(), 442 alloc_thread->tid, 443 ThreadNameWithParenthesis(alloc_thread, tname, sizeof(tname)), 444 d.EndAllocation()); 445 } 446 PrintStack(&alloc_stack); 447 DescribeThread(GetCurrentThread()); 448 if (free_thread) 449 DescribeThread(free_thread); 450 DescribeThread(alloc_thread); 451} 452 453void DescribeAddress(uptr addr, uptr access_size) { 454 // Check if this is shadow or shadow gap. 455 if (DescribeAddressIfShadow(addr)) 456 return; 457 CHECK(AddrIsInMem(addr)); 458 if (DescribeAddressIfGlobal(addr, access_size)) 459 return; 460 if (DescribeAddressIfStack(addr, access_size)) 461 return; 462 // Assume it is a heap address. 463 DescribeHeapAddress(addr, access_size); 464} 465 466// ------------------- Thread description -------------------- {{{1 467 468void DescribeThread(AsanThreadContext *context) { 469 CHECK(context); 470 asanThreadRegistry().CheckLocked(); 471 // No need to announce the main thread. 472 if (context->tid == 0 || context->announced) { 473 return; 474 } 475 context->announced = true; 476 char tname[128]; 477 Printf("Thread T%d%s", context->tid, 478 ThreadNameWithParenthesis(context->tid, tname, sizeof(tname))); 479 Printf(" created by T%d%s here:\n", 480 context->parent_tid, 481 ThreadNameWithParenthesis(context->parent_tid, 482 tname, sizeof(tname))); 483 PrintStack(&context->stack); 484 // Recursively described parent thread if needed. 485 if (flags()->print_full_thread_history) { 486 AsanThreadContext *parent_context = 487 GetThreadContextByTidLocked(context->parent_tid); 488 DescribeThread(parent_context); 489 } 490} 491 492// -------------------- Different kinds of reports ----------------- {{{1 493 494// Use ScopedInErrorReport to run common actions just before and 495// immediately after printing error report. 496class ScopedInErrorReport { 497 public: 498 ScopedInErrorReport() { 499 static atomic_uint32_t num_calls; 500 static u32 reporting_thread_tid; 501 if (atomic_fetch_add(&num_calls, 1, memory_order_relaxed) != 0) { 502 // Do not print more than one report, otherwise they will mix up. 503 // Error reporting functions shouldn't return at this situation, as 504 // they are defined as no-return. 505 Report("AddressSanitizer: while reporting a bug found another one." 506 "Ignoring.\n"); 507 u32 current_tid = GetCurrentTidOrInvalid(); 508 if (current_tid != reporting_thread_tid) { 509 // ASan found two bugs in different threads simultaneously. Sleep 510 // long enough to make sure that the thread which started to print 511 // an error report will finish doing it. 512 SleepForSeconds(Max(100, flags()->sleep_before_dying + 1)); 513 } 514 // If we're still not dead for some reason, use raw _exit() instead of 515 // Die() to bypass any additional checks. 516 internal__exit(flags()->exitcode); 517 } 518 ASAN_ON_ERROR(); 519 // Make sure the registry and sanitizer report mutexes are locked while 520 // we're printing an error report. 521 // We can lock them only here to avoid self-deadlock in case of 522 // recursive reports. 523 asanThreadRegistry().Lock(); 524 CommonSanitizerReportMutex.Lock(); 525 reporting_thread_tid = GetCurrentTidOrInvalid(); 526 Printf("====================================================" 527 "=============\n"); 528 } 529 // Destructor is NORETURN, as functions that report errors are. 530 NORETURN ~ScopedInErrorReport() { 531 // Make sure the current thread is announced. 532 DescribeThread(GetCurrentThread()); 533 // Print memory stats. 534 if (flags()->print_stats) 535 __asan_print_accumulated_stats(); 536 if (error_report_callback) { 537 error_report_callback(error_message_buffer); 538 } 539 Report("ABORTING\n"); 540 Die(); 541 } 542}; 543 544static void ReportSummary(const char *error_type, StackTrace *stack) { 545 if (!stack->size) return; 546 if (getSymbolizer()->IsAvailable()) { 547 AddressInfo ai; 548 // Currently, we include the first stack frame into the report summary. 549 // Maybe sometimes we need to choose another frame (e.g. skip memcpy/etc). 550 uptr pc = StackTrace::GetPreviousInstructionPc(stack->trace[0]); 551 getSymbolizer()->SymbolizeCode(pc, &ai, 1); 552 ReportErrorSummary(error_type, 553 StripPathPrefix(ai.file, 554 common_flags()->strip_path_prefix), 555 ai.line, ai.function); 556 } 557 // FIXME: do we need to print anything at all if there is no symbolizer? 558} 559 560void ReportSIGSEGV(uptr pc, uptr sp, uptr bp, uptr addr) { 561 ScopedInErrorReport in_report; 562 Decorator d; 563 Printf("%s", d.Warning()); 564 Report("ERROR: AddressSanitizer: SEGV on unknown address %p" 565 " (pc %p sp %p bp %p T%d)\n", 566 (void*)addr, (void*)pc, (void*)sp, (void*)bp, 567 GetCurrentTidOrInvalid()); 568 Printf("%s", d.EndWarning()); 569 Printf("AddressSanitizer can not provide additional info.\n"); 570 GET_STACK_TRACE_FATAL(pc, bp); 571 PrintStack(&stack); 572 ReportSummary("SEGV", &stack); 573} 574 575void ReportDoubleFree(uptr addr, StackTrace *stack) { 576 ScopedInErrorReport in_report; 577 Decorator d; 578 Printf("%s", d.Warning()); 579 char tname[128]; 580 u32 curr_tid = GetCurrentTidOrInvalid(); 581 Report("ERROR: AddressSanitizer: attempting double-free on %p in " 582 "thread T%d%s:\n", 583 addr, curr_tid, 584 ThreadNameWithParenthesis(curr_tid, tname, sizeof(tname))); 585 586 Printf("%s", d.EndWarning()); 587 PrintStack(stack); 588 DescribeHeapAddress(addr, 1); 589 ReportSummary("double-free", stack); 590} 591 592void ReportFreeNotMalloced(uptr addr, StackTrace *stack) { 593 ScopedInErrorReport in_report; 594 Decorator d; 595 Printf("%s", d.Warning()); 596 char tname[128]; 597 u32 curr_tid = GetCurrentTidOrInvalid(); 598 Report("ERROR: AddressSanitizer: attempting free on address " 599 "which was not malloc()-ed: %p in thread T%d%s\n", addr, 600 curr_tid, ThreadNameWithParenthesis(curr_tid, tname, sizeof(tname))); 601 Printf("%s", d.EndWarning()); 602 PrintStack(stack); 603 DescribeHeapAddress(addr, 1); 604 ReportSummary("bad-free", stack); 605} 606 607void ReportAllocTypeMismatch(uptr addr, StackTrace *stack, 608 AllocType alloc_type, 609 AllocType dealloc_type) { 610 static const char *alloc_names[] = 611 {"INVALID", "malloc", "operator new", "operator new []"}; 612 static const char *dealloc_names[] = 613 {"INVALID", "free", "operator delete", "operator delete []"}; 614 CHECK_NE(alloc_type, dealloc_type); 615 ScopedInErrorReport in_report; 616 Decorator d; 617 Printf("%s", d.Warning()); 618 Report("ERROR: AddressSanitizer: alloc-dealloc-mismatch (%s vs %s) on %p\n", 619 alloc_names[alloc_type], dealloc_names[dealloc_type], addr); 620 Printf("%s", d.EndWarning()); 621 PrintStack(stack); 622 DescribeHeapAddress(addr, 1); 623 ReportSummary("alloc-dealloc-mismatch", stack); 624 Report("HINT: if you don't care about these warnings you may set " 625 "ASAN_OPTIONS=alloc_dealloc_mismatch=0\n"); 626} 627 628void ReportMallocUsableSizeNotOwned(uptr addr, StackTrace *stack) { 629 ScopedInErrorReport in_report; 630 Decorator d; 631 Printf("%s", d.Warning()); 632 Report("ERROR: AddressSanitizer: attempting to call " 633 "malloc_usable_size() for pointer which is " 634 "not owned: %p\n", addr); 635 Printf("%s", d.EndWarning()); 636 PrintStack(stack); 637 DescribeHeapAddress(addr, 1); 638 ReportSummary("bad-malloc_usable_size", stack); 639} 640 641void ReportAsanGetAllocatedSizeNotOwned(uptr addr, StackTrace *stack) { 642 ScopedInErrorReport in_report; 643 Decorator d; 644 Printf("%s", d.Warning()); 645 Report("ERROR: AddressSanitizer: attempting to call " 646 "__asan_get_allocated_size() for pointer which is " 647 "not owned: %p\n", addr); 648 Printf("%s", d.EndWarning()); 649 PrintStack(stack); 650 DescribeHeapAddress(addr, 1); 651 ReportSummary("bad-__asan_get_allocated_size", stack); 652} 653 654void ReportStringFunctionMemoryRangesOverlap( 655 const char *function, const char *offset1, uptr length1, 656 const char *offset2, uptr length2, StackTrace *stack) { 657 ScopedInErrorReport in_report; 658 Decorator d; 659 char bug_type[100]; 660 internal_snprintf(bug_type, sizeof(bug_type), "%s-param-overlap", function); 661 Printf("%s", d.Warning()); 662 Report("ERROR: AddressSanitizer: %s: " 663 "memory ranges [%p,%p) and [%p, %p) overlap\n", \ 664 bug_type, offset1, offset1 + length1, offset2, offset2 + length2); 665 Printf("%s", d.EndWarning()); 666 PrintStack(stack); 667 DescribeAddress((uptr)offset1, length1); 668 DescribeAddress((uptr)offset2, length2); 669 ReportSummary(bug_type, stack); 670} 671 672// ----------------------- Mac-specific reports ----------------- {{{1 673 674void WarnMacFreeUnallocated( 675 uptr addr, uptr zone_ptr, const char *zone_name, StackTrace *stack) { 676 // Just print a warning here. 677 Printf("free_common(%p) -- attempting to free unallocated memory.\n" 678 "AddressSanitizer is ignoring this error on Mac OS now.\n", 679 addr); 680 PrintZoneForPointer(addr, zone_ptr, zone_name); 681 PrintStack(stack); 682 DescribeHeapAddress(addr, 1); 683} 684 685void ReportMacMzReallocUnknown( 686 uptr addr, uptr zone_ptr, const char *zone_name, StackTrace *stack) { 687 ScopedInErrorReport in_report; 688 Printf("mz_realloc(%p) -- attempting to realloc unallocated memory.\n" 689 "This is an unrecoverable problem, exiting now.\n", 690 addr); 691 PrintZoneForPointer(addr, zone_ptr, zone_name); 692 PrintStack(stack); 693 DescribeHeapAddress(addr, 1); 694} 695 696void ReportMacCfReallocUnknown( 697 uptr addr, uptr zone_ptr, const char *zone_name, StackTrace *stack) { 698 ScopedInErrorReport in_report; 699 Printf("cf_realloc(%p) -- attempting to realloc unallocated memory.\n" 700 "This is an unrecoverable problem, exiting now.\n", 701 addr); 702 PrintZoneForPointer(addr, zone_ptr, zone_name); 703 PrintStack(stack); 704 DescribeHeapAddress(addr, 1); 705} 706 707} // namespace __asan 708 709// --------------------------- Interface --------------------- {{{1 710using namespace __asan; // NOLINT 711 712void __asan_report_error(uptr pc, uptr bp, uptr sp, 713 uptr addr, bool is_write, uptr access_size) { 714 ScopedInErrorReport in_report; 715 716 // Determine the error type. 717 const char *bug_descr = "unknown-crash"; 718 if (AddrIsInMem(addr)) { 719 u8 *shadow_addr = (u8*)MemToShadow(addr); 720 // If we are accessing 16 bytes, look at the second shadow byte. 721 if (*shadow_addr == 0 && access_size > SHADOW_GRANULARITY) 722 shadow_addr++; 723 // If we are in the partial right redzone, look at the next shadow byte. 724 if (*shadow_addr > 0 && *shadow_addr < 128) 725 shadow_addr++; 726 switch (*shadow_addr) { 727 case kAsanHeapLeftRedzoneMagic: 728 case kAsanHeapRightRedzoneMagic: 729 bug_descr = "heap-buffer-overflow"; 730 break; 731 case kAsanHeapFreeMagic: 732 bug_descr = "heap-use-after-free"; 733 break; 734 case kAsanStackLeftRedzoneMagic: 735 bug_descr = "stack-buffer-underflow"; 736 break; 737 case kAsanInitializationOrderMagic: 738 bug_descr = "initialization-order-fiasco"; 739 break; 740 case kAsanStackMidRedzoneMagic: 741 case kAsanStackRightRedzoneMagic: 742 case kAsanStackPartialRedzoneMagic: 743 bug_descr = "stack-buffer-overflow"; 744 break; 745 case kAsanStackAfterReturnMagic: 746 bug_descr = "stack-use-after-return"; 747 break; 748 case kAsanUserPoisonedMemoryMagic: 749 bug_descr = "use-after-poison"; 750 break; 751 case kAsanStackUseAfterScopeMagic: 752 bug_descr = "stack-use-after-scope"; 753 break; 754 case kAsanGlobalRedzoneMagic: 755 bug_descr = "global-buffer-overflow"; 756 break; 757 } 758 } 759 Decorator d; 760 Printf("%s", d.Warning()); 761 Report("ERROR: AddressSanitizer: %s on address " 762 "%p at pc 0x%zx bp 0x%zx sp 0x%zx\n", 763 bug_descr, (void*)addr, pc, bp, sp); 764 Printf("%s", d.EndWarning()); 765 766 u32 curr_tid = GetCurrentTidOrInvalid(); 767 char tname[128]; 768 Printf("%s%s of size %zu at %p thread T%d%s%s\n", 769 d.Access(), 770 access_size ? (is_write ? "WRITE" : "READ") : "ACCESS", 771 access_size, (void*)addr, curr_tid, 772 ThreadNameWithParenthesis(curr_tid, tname, sizeof(tname)), 773 d.EndAccess()); 774 775 GET_STACK_TRACE_FATAL(pc, bp); 776 PrintStack(&stack); 777 778 DescribeAddress(addr, access_size); 779 ReportSummary(bug_descr, &stack); 780 PrintShadowMemoryForAddress(addr); 781} 782 783void NOINLINE __asan_set_error_report_callback(void (*callback)(const char*)) { 784 error_report_callback = callback; 785 if (callback) { 786 error_message_buffer_size = 1 << 16; 787 error_message_buffer = 788 (char*)MmapOrDie(error_message_buffer_size, __FUNCTION__); 789 error_message_buffer_pos = 0; 790 } 791} 792 793void __asan_describe_address(uptr addr) { 794 DescribeAddress(addr, 1); 795} 796 797#if !SANITIZER_SUPPORTS_WEAK_HOOKS 798// Provide default implementation of __asan_on_error that does nothing 799// and may be overriden by user. 800SANITIZER_INTERFACE_ATTRIBUTE SANITIZER_WEAK_ATTRIBUTE NOINLINE 801void __asan_on_error() {} 802#endif 803