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