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