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