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