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