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