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