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