1217222abSNico Weber //===-- asan_errors.cpp -----------------------------------------*- C++ -*-===// 2217222abSNico Weber // 3217222abSNico Weber // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 4217222abSNico Weber // See https://llvm.org/LICENSE.txt for license information. 5217222abSNico Weber // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 6217222abSNico Weber // 7217222abSNico Weber //===----------------------------------------------------------------------===// 8217222abSNico Weber // 9217222abSNico Weber // This file is a part of AddressSanitizer, an address sanity checker. 10217222abSNico Weber // 11217222abSNico Weber // ASan implementation for error structures. 12217222abSNico Weber //===----------------------------------------------------------------------===// 13217222abSNico Weber 14217222abSNico Weber #include "asan_errors.h" 15217222abSNico Weber #include "asan_descriptions.h" 16217222abSNico Weber #include "asan_mapping.h" 17217222abSNico Weber #include "asan_report.h" 18217222abSNico Weber #include "asan_stack.h" 19217222abSNico Weber #include "sanitizer_common/sanitizer_stackdepot.h" 20217222abSNico Weber 21217222abSNico Weber namespace __asan { 22217222abSNico Weber 23217222abSNico Weber static void OnStackUnwind(const SignalContext &sig, 24217222abSNico Weber const void *callback_context, 25217222abSNico Weber BufferedStackTrace *stack) { 26217222abSNico Weber bool fast = common_flags()->fast_unwind_on_fatal; 27217222abSNico Weber #if SANITIZER_FREEBSD || SANITIZER_NETBSD 28217222abSNico Weber // On FreeBSD the slow unwinding that leverages _Unwind_Backtrace() 29217222abSNico Weber // yields the call stack of the signal's handler and not of the code 30217222abSNico Weber // that raised the signal (as it does on Linux). 31217222abSNico Weber fast = true; 32217222abSNico Weber #endif 33217222abSNico Weber // Tests and maybe some users expect that scariness is going to be printed 34217222abSNico Weber // just before the stack. As only asan has scariness score we have no 35217222abSNico Weber // corresponding code in the sanitizer_common and we use this callback to 36217222abSNico Weber // print it. 37217222abSNico Weber static_cast<const ScarinessScoreBase *>(callback_context)->Print(); 38d39e7e2cSVitaly Buka stack->Unwind(StackTrace::GetNextInstructionPc(sig.pc), sig.bp, sig.context, 39d39e7e2cSVitaly Buka fast); 40217222abSNico Weber } 41217222abSNico Weber 42217222abSNico Weber void ErrorDeadlySignal::Print() { 43217222abSNico Weber ReportDeadlySignal(signal, tid, &OnStackUnwind, &scariness); 44217222abSNico Weber } 45217222abSNico Weber 46217222abSNico Weber void ErrorDoubleFree::Print() { 47217222abSNico Weber Decorator d; 48217222abSNico Weber Printf("%s", d.Error()); 49*629b40daSMartin Liska Report("ERROR: AddressSanitizer: attempting %s on %p in thread %s:\n", 50*629b40daSMartin Liska scariness.GetDescription(), (void *)addr_description.addr, 51217222abSNico Weber AsanThreadIdAndName(tid).c_str()); 52217222abSNico Weber Printf("%s", d.Default()); 53217222abSNico Weber scariness.Print(); 54217222abSNico Weber GET_STACK_TRACE_FATAL(second_free_stack->trace[0], 55217222abSNico Weber second_free_stack->top_frame_bp); 56217222abSNico Weber stack.Print(); 57217222abSNico Weber addr_description.Print(); 58217222abSNico Weber ReportErrorSummary(scariness.GetDescription(), &stack); 59217222abSNico Weber } 60217222abSNico Weber 61217222abSNico Weber void ErrorNewDeleteTypeMismatch::Print() { 62217222abSNico Weber Decorator d; 63217222abSNico Weber Printf("%s", d.Error()); 64*629b40daSMartin Liska Report("ERROR: AddressSanitizer: %s on %p in thread %s:\n", 65*629b40daSMartin Liska scariness.GetDescription(), (void *)addr_description.addr, 66217222abSNico Weber AsanThreadIdAndName(tid).c_str()); 67217222abSNico Weber Printf("%s object passed to delete has wrong type:\n", d.Default()); 68217222abSNico Weber if (delete_size != 0) { 69217222abSNico Weber Printf( 70217222abSNico Weber " size of the allocated type: %zd bytes;\n" 71217222abSNico Weber " size of the deallocated type: %zd bytes.\n", 72217222abSNico Weber addr_description.chunk_access.chunk_size, delete_size); 73217222abSNico Weber } 74217222abSNico Weber const uptr user_alignment = 75217222abSNico Weber addr_description.chunk_access.user_requested_alignment; 76217222abSNico Weber if (delete_alignment != user_alignment) { 77217222abSNico Weber char user_alignment_str[32]; 78217222abSNico Weber char delete_alignment_str[32]; 79217222abSNico Weber internal_snprintf(user_alignment_str, sizeof(user_alignment_str), 80217222abSNico Weber "%zd bytes", user_alignment); 81217222abSNico Weber internal_snprintf(delete_alignment_str, sizeof(delete_alignment_str), 82217222abSNico Weber "%zd bytes", delete_alignment); 83217222abSNico Weber static const char *kDefaultAlignment = "default-aligned"; 84217222abSNico Weber Printf( 85217222abSNico Weber " alignment of the allocated type: %s;\n" 86217222abSNico Weber " alignment of the deallocated type: %s.\n", 87217222abSNico Weber user_alignment > 0 ? user_alignment_str : kDefaultAlignment, 88217222abSNico Weber delete_alignment > 0 ? delete_alignment_str : kDefaultAlignment); 89217222abSNico Weber } 90217222abSNico Weber CHECK_GT(free_stack->size, 0); 91217222abSNico Weber scariness.Print(); 92217222abSNico Weber GET_STACK_TRACE_FATAL(free_stack->trace[0], free_stack->top_frame_bp); 93217222abSNico Weber stack.Print(); 94217222abSNico Weber addr_description.Print(); 95217222abSNico Weber ReportErrorSummary(scariness.GetDescription(), &stack); 96217222abSNico Weber Report( 97217222abSNico Weber "HINT: if you don't care about these errors you may set " 98217222abSNico Weber "ASAN_OPTIONS=new_delete_type_mismatch=0\n"); 99217222abSNico Weber } 100217222abSNico Weber 101217222abSNico Weber void ErrorFreeNotMalloced::Print() { 102217222abSNico Weber Decorator d; 103217222abSNico Weber Printf("%s", d.Error()); 104217222abSNico Weber Report( 105217222abSNico Weber "ERROR: AddressSanitizer: attempting free on address " 106217222abSNico Weber "which was not malloc()-ed: %p in thread %s\n", 107*629b40daSMartin Liska (void *)addr_description.Address(), AsanThreadIdAndName(tid).c_str()); 108217222abSNico Weber Printf("%s", d.Default()); 109217222abSNico Weber CHECK_GT(free_stack->size, 0); 110217222abSNico Weber scariness.Print(); 111217222abSNico Weber GET_STACK_TRACE_FATAL(free_stack->trace[0], free_stack->top_frame_bp); 112217222abSNico Weber stack.Print(); 113217222abSNico Weber addr_description.Print(); 114217222abSNico Weber ReportErrorSummary(scariness.GetDescription(), &stack); 115217222abSNico Weber } 116217222abSNico Weber 117217222abSNico Weber void ErrorAllocTypeMismatch::Print() { 118217222abSNico Weber static const char *alloc_names[] = {"INVALID", "malloc", "operator new", 119217222abSNico Weber "operator new []"}; 120217222abSNico Weber static const char *dealloc_names[] = {"INVALID", "free", "operator delete", 121217222abSNico Weber "operator delete []"}; 122217222abSNico Weber CHECK_NE(alloc_type, dealloc_type); 123217222abSNico Weber Decorator d; 124217222abSNico Weber Printf("%s", d.Error()); 125217222abSNico Weber Report("ERROR: AddressSanitizer: %s (%s vs %s) on %p\n", 126217222abSNico Weber scariness.GetDescription(), alloc_names[alloc_type], 127*629b40daSMartin Liska dealloc_names[dealloc_type], (void *)addr_description.Address()); 128217222abSNico Weber Printf("%s", d.Default()); 129217222abSNico Weber CHECK_GT(dealloc_stack->size, 0); 130217222abSNico Weber scariness.Print(); 131217222abSNico Weber GET_STACK_TRACE_FATAL(dealloc_stack->trace[0], dealloc_stack->top_frame_bp); 132217222abSNico Weber stack.Print(); 133217222abSNico Weber addr_description.Print(); 134217222abSNico Weber ReportErrorSummary(scariness.GetDescription(), &stack); 135217222abSNico Weber Report( 136217222abSNico Weber "HINT: if you don't care about these errors you may set " 137217222abSNico Weber "ASAN_OPTIONS=alloc_dealloc_mismatch=0\n"); 138217222abSNico Weber } 139217222abSNico Weber 140217222abSNico Weber void ErrorMallocUsableSizeNotOwned::Print() { 141217222abSNico Weber Decorator d; 142217222abSNico Weber Printf("%s", d.Error()); 143217222abSNico Weber Report( 144217222abSNico Weber "ERROR: AddressSanitizer: attempting to call malloc_usable_size() for " 145217222abSNico Weber "pointer which is not owned: %p\n", 146*629b40daSMartin Liska (void *)addr_description.Address()); 147217222abSNico Weber Printf("%s", d.Default()); 148217222abSNico Weber stack->Print(); 149217222abSNico Weber addr_description.Print(); 150217222abSNico Weber ReportErrorSummary(scariness.GetDescription(), stack); 151217222abSNico Weber } 152217222abSNico Weber 153217222abSNico Weber void ErrorSanitizerGetAllocatedSizeNotOwned::Print() { 154217222abSNico Weber Decorator d; 155217222abSNico Weber Printf("%s", d.Error()); 156217222abSNico Weber Report( 157217222abSNico Weber "ERROR: AddressSanitizer: attempting to call " 158217222abSNico Weber "__sanitizer_get_allocated_size() for pointer which is not owned: %p\n", 159*629b40daSMartin Liska (void *)addr_description.Address()); 160217222abSNico Weber Printf("%s", d.Default()); 161217222abSNico Weber stack->Print(); 162217222abSNico Weber addr_description.Print(); 163217222abSNico Weber ReportErrorSummary(scariness.GetDescription(), stack); 164217222abSNico Weber } 165217222abSNico Weber 166217222abSNico Weber void ErrorCallocOverflow::Print() { 167217222abSNico Weber Decorator d; 168217222abSNico Weber Printf("%s", d.Error()); 169217222abSNico Weber Report( 170217222abSNico Weber "ERROR: AddressSanitizer: calloc parameters overflow: count * size " 171217222abSNico Weber "(%zd * %zd) cannot be represented in type size_t (thread %s)\n", 172217222abSNico Weber count, size, AsanThreadIdAndName(tid).c_str()); 173217222abSNico Weber Printf("%s", d.Default()); 174217222abSNico Weber stack->Print(); 175217222abSNico Weber PrintHintAllocatorCannotReturnNull(); 176217222abSNico Weber ReportErrorSummary(scariness.GetDescription(), stack); 177217222abSNico Weber } 178217222abSNico Weber 179217222abSNico Weber void ErrorReallocArrayOverflow::Print() { 180217222abSNico Weber Decorator d; 181217222abSNico Weber Printf("%s", d.Error()); 182217222abSNico Weber Report( 183217222abSNico Weber "ERROR: AddressSanitizer: reallocarray parameters overflow: count * size " 184217222abSNico Weber "(%zd * %zd) cannot be represented in type size_t (thread %s)\n", 185217222abSNico Weber count, size, AsanThreadIdAndName(tid).c_str()); 186217222abSNico Weber Printf("%s", d.Default()); 187217222abSNico Weber stack->Print(); 188217222abSNico Weber PrintHintAllocatorCannotReturnNull(); 189217222abSNico Weber ReportErrorSummary(scariness.GetDescription(), stack); 190217222abSNico Weber } 191217222abSNico Weber 192217222abSNico Weber void ErrorPvallocOverflow::Print() { 193217222abSNico Weber Decorator d; 194217222abSNico Weber Printf("%s", d.Error()); 195217222abSNico Weber Report( 196217222abSNico Weber "ERROR: AddressSanitizer: pvalloc parameters overflow: size 0x%zx " 197217222abSNico Weber "rounded up to system page size 0x%zx cannot be represented in type " 198217222abSNico Weber "size_t (thread %s)\n", 199217222abSNico Weber size, GetPageSizeCached(), AsanThreadIdAndName(tid).c_str()); 200217222abSNico Weber Printf("%s", d.Default()); 201217222abSNico Weber stack->Print(); 202217222abSNico Weber PrintHintAllocatorCannotReturnNull(); 203217222abSNico Weber ReportErrorSummary(scariness.GetDescription(), stack); 204217222abSNico Weber } 205217222abSNico Weber 206217222abSNico Weber void ErrorInvalidAllocationAlignment::Print() { 207217222abSNico Weber Decorator d; 208217222abSNico Weber Printf("%s", d.Error()); 209217222abSNico Weber Report( 210217222abSNico Weber "ERROR: AddressSanitizer: invalid allocation alignment: %zd, " 211217222abSNico Weber "alignment must be a power of two (thread %s)\n", 212217222abSNico Weber alignment, AsanThreadIdAndName(tid).c_str()); 213217222abSNico Weber Printf("%s", d.Default()); 214217222abSNico Weber stack->Print(); 215217222abSNico Weber PrintHintAllocatorCannotReturnNull(); 216217222abSNico Weber ReportErrorSummary(scariness.GetDescription(), stack); 217217222abSNico Weber } 218217222abSNico Weber 219217222abSNico Weber void ErrorInvalidAlignedAllocAlignment::Print() { 220217222abSNico Weber Decorator d; 221217222abSNico Weber Printf("%s", d.Error()); 222217222abSNico Weber #if SANITIZER_POSIX 223217222abSNico Weber Report("ERROR: AddressSanitizer: invalid alignment requested in " 224217222abSNico Weber "aligned_alloc: %zd, alignment must be a power of two and the " 225217222abSNico Weber "requested size 0x%zx must be a multiple of alignment " 226217222abSNico Weber "(thread %s)\n", alignment, size, AsanThreadIdAndName(tid).c_str()); 227217222abSNico Weber #else 228217222abSNico Weber Report("ERROR: AddressSanitizer: invalid alignment requested in " 229217222abSNico Weber "aligned_alloc: %zd, the requested size 0x%zx must be a multiple of " 230217222abSNico Weber "alignment (thread %s)\n", alignment, size, 231217222abSNico Weber AsanThreadIdAndName(tid).c_str()); 232217222abSNico Weber #endif 233217222abSNico Weber Printf("%s", d.Default()); 234217222abSNico Weber stack->Print(); 235217222abSNico Weber PrintHintAllocatorCannotReturnNull(); 236217222abSNico Weber ReportErrorSummary(scariness.GetDescription(), stack); 237217222abSNico Weber } 238217222abSNico Weber 239217222abSNico Weber void ErrorInvalidPosixMemalignAlignment::Print() { 240217222abSNico Weber Decorator d; 241217222abSNico Weber Printf("%s", d.Error()); 242217222abSNico Weber Report( 243217222abSNico Weber "ERROR: AddressSanitizer: invalid alignment requested in posix_memalign: " 244217222abSNico Weber "%zd, alignment must be a power of two and a multiple of sizeof(void*) " 245217222abSNico Weber "== %zd (thread %s)\n", 246c0fa6322SVitaly Buka alignment, sizeof(void *), AsanThreadIdAndName(tid).c_str()); 247217222abSNico Weber Printf("%s", d.Default()); 248217222abSNico Weber stack->Print(); 249217222abSNico Weber PrintHintAllocatorCannotReturnNull(); 250217222abSNico Weber ReportErrorSummary(scariness.GetDescription(), stack); 251217222abSNico Weber } 252217222abSNico Weber 253217222abSNico Weber void ErrorAllocationSizeTooBig::Print() { 254217222abSNico Weber Decorator d; 255217222abSNico Weber Printf("%s", d.Error()); 256217222abSNico Weber Report( 257217222abSNico Weber "ERROR: AddressSanitizer: requested allocation size 0x%zx (0x%zx after " 258217222abSNico Weber "adjustments for alignment, red zones etc.) exceeds maximum supported " 259217222abSNico Weber "size of 0x%zx (thread %s)\n", 260217222abSNico Weber user_size, total_size, max_size, AsanThreadIdAndName(tid).c_str()); 261217222abSNico Weber Printf("%s", d.Default()); 262217222abSNico Weber stack->Print(); 263217222abSNico Weber PrintHintAllocatorCannotReturnNull(); 264217222abSNico Weber ReportErrorSummary(scariness.GetDescription(), stack); 265217222abSNico Weber } 266217222abSNico Weber 267217222abSNico Weber void ErrorRssLimitExceeded::Print() { 268217222abSNico Weber Decorator d; 269217222abSNico Weber Printf("%s", d.Error()); 270217222abSNico Weber Report( 271217222abSNico Weber "ERROR: AddressSanitizer: specified RSS limit exceeded, currently set to " 272217222abSNico Weber "soft_rss_limit_mb=%zd\n", common_flags()->soft_rss_limit_mb); 273217222abSNico Weber Printf("%s", d.Default()); 274217222abSNico Weber stack->Print(); 275217222abSNico Weber PrintHintAllocatorCannotReturnNull(); 276217222abSNico Weber ReportErrorSummary(scariness.GetDescription(), stack); 277217222abSNico Weber } 278217222abSNico Weber 279217222abSNico Weber void ErrorOutOfMemory::Print() { 280217222abSNico Weber Decorator d; 281217222abSNico Weber Printf("%s", d.Error()); 282217222abSNico Weber Report( 283217222abSNico Weber "ERROR: AddressSanitizer: allocator is out of memory trying to allocate " 284217222abSNico Weber "0x%zx bytes\n", requested_size); 285217222abSNico Weber Printf("%s", d.Default()); 286217222abSNico Weber stack->Print(); 287217222abSNico Weber PrintHintAllocatorCannotReturnNull(); 288217222abSNico Weber ReportErrorSummary(scariness.GetDescription(), stack); 289217222abSNico Weber } 290217222abSNico Weber 291217222abSNico Weber void ErrorStringFunctionMemoryRangesOverlap::Print() { 292217222abSNico Weber Decorator d; 293217222abSNico Weber char bug_type[100]; 294217222abSNico Weber internal_snprintf(bug_type, sizeof(bug_type), "%s-param-overlap", function); 295217222abSNico Weber Printf("%s", d.Error()); 296217222abSNico Weber Report( 297217222abSNico Weber "ERROR: AddressSanitizer: %s: memory ranges [%p,%p) and [%p, %p) " 298217222abSNico Weber "overlap\n", 299*629b40daSMartin Liska bug_type, (void *)addr1_description.Address(), 300*629b40daSMartin Liska (void *)(addr1_description.Address() + length1), 301*629b40daSMartin Liska (void *)addr2_description.Address(), 302*629b40daSMartin Liska (void *)(addr2_description.Address() + length2)); 303217222abSNico Weber Printf("%s", d.Default()); 304217222abSNico Weber scariness.Print(); 305217222abSNico Weber stack->Print(); 306217222abSNico Weber addr1_description.Print(); 307217222abSNico Weber addr2_description.Print(); 308217222abSNico Weber ReportErrorSummary(bug_type, stack); 309217222abSNico Weber } 310217222abSNico Weber 311217222abSNico Weber void ErrorStringFunctionSizeOverflow::Print() { 312217222abSNico Weber Decorator d; 313217222abSNico Weber Printf("%s", d.Error()); 314217222abSNico Weber Report("ERROR: AddressSanitizer: %s: (size=%zd)\n", 315217222abSNico Weber scariness.GetDescription(), size); 316217222abSNico Weber Printf("%s", d.Default()); 317217222abSNico Weber scariness.Print(); 318217222abSNico Weber stack->Print(); 319217222abSNico Weber addr_description.Print(); 320217222abSNico Weber ReportErrorSummary(scariness.GetDescription(), stack); 321217222abSNico Weber } 322217222abSNico Weber 323217222abSNico Weber void ErrorBadParamsToAnnotateContiguousContainer::Print() { 324217222abSNico Weber Report( 325217222abSNico Weber "ERROR: AddressSanitizer: bad parameters to " 326217222abSNico Weber "__sanitizer_annotate_contiguous_container:\n" 327217222abSNico Weber " beg : %p\n" 328217222abSNico Weber " end : %p\n" 329217222abSNico Weber " old_mid : %p\n" 330217222abSNico Weber " new_mid : %p\n", 331*629b40daSMartin Liska (void *)beg, (void *)end, (void *)old_mid, (void *)new_mid); 332217222abSNico Weber uptr granularity = SHADOW_GRANULARITY; 333217222abSNico Weber if (!IsAligned(beg, granularity)) 334*629b40daSMartin Liska Report("ERROR: beg is not aligned by %zu\n", granularity); 335217222abSNico Weber stack->Print(); 336217222abSNico Weber ReportErrorSummary(scariness.GetDescription(), stack); 337217222abSNico Weber } 338217222abSNico Weber 339217222abSNico Weber void ErrorODRViolation::Print() { 340217222abSNico Weber Decorator d; 341217222abSNico Weber Printf("%s", d.Error()); 342217222abSNico Weber Report("ERROR: AddressSanitizer: %s (%p):\n", scariness.GetDescription(), 343*629b40daSMartin Liska (void *)global1.beg); 344217222abSNico Weber Printf("%s", d.Default()); 345e0dadf3dSVitaly Buka InternalScopedString g1_loc; 346e0dadf3dSVitaly Buka InternalScopedString g2_loc; 347217222abSNico Weber PrintGlobalLocation(&g1_loc, global1); 348217222abSNico Weber PrintGlobalLocation(&g2_loc, global2); 349217222abSNico Weber Printf(" [1] size=%zd '%s' %s\n", global1.size, 350217222abSNico Weber MaybeDemangleGlobalName(global1.name), g1_loc.data()); 351217222abSNico Weber Printf(" [2] size=%zd '%s' %s\n", global2.size, 352217222abSNico Weber MaybeDemangleGlobalName(global2.name), g2_loc.data()); 353217222abSNico Weber if (stack_id1 && stack_id2) { 354217222abSNico Weber Printf("These globals were registered at these points:\n"); 355217222abSNico Weber Printf(" [1]:\n"); 356217222abSNico Weber StackDepotGet(stack_id1).Print(); 357217222abSNico Weber Printf(" [2]:\n"); 358217222abSNico Weber StackDepotGet(stack_id2).Print(); 359217222abSNico Weber } 360217222abSNico Weber Report( 361217222abSNico Weber "HINT: if you don't care about these errors you may set " 362217222abSNico Weber "ASAN_OPTIONS=detect_odr_violation=0\n"); 363e0dadf3dSVitaly Buka InternalScopedString error_msg; 364217222abSNico Weber error_msg.append("%s: global '%s' at %s", scariness.GetDescription(), 365217222abSNico Weber MaybeDemangleGlobalName(global1.name), g1_loc.data()); 366217222abSNico Weber ReportErrorSummary(error_msg.data()); 367217222abSNico Weber } 368217222abSNico Weber 369217222abSNico Weber void ErrorInvalidPointerPair::Print() { 370217222abSNico Weber Decorator d; 371217222abSNico Weber Printf("%s", d.Error()); 372217222abSNico Weber Report("ERROR: AddressSanitizer: %s: %p %p\n", scariness.GetDescription(), 373*629b40daSMartin Liska (void *)addr1_description.Address(), 374*629b40daSMartin Liska (void *)addr2_description.Address()); 375217222abSNico Weber Printf("%s", d.Default()); 376217222abSNico Weber GET_STACK_TRACE_FATAL(pc, bp); 377217222abSNico Weber stack.Print(); 378217222abSNico Weber addr1_description.Print(); 379217222abSNico Weber addr2_description.Print(); 380217222abSNico Weber ReportErrorSummary(scariness.GetDescription(), &stack); 381217222abSNico Weber } 382217222abSNico Weber 383217222abSNico Weber static bool AdjacentShadowValuesAreFullyPoisoned(u8 *s) { 384217222abSNico Weber return s[-1] > 127 && s[1] > 127; 385217222abSNico Weber } 386217222abSNico Weber 387217222abSNico Weber ErrorGeneric::ErrorGeneric(u32 tid, uptr pc_, uptr bp_, uptr sp_, uptr addr, 388217222abSNico Weber bool is_write_, uptr access_size_) 389217222abSNico Weber : ErrorBase(tid), 390217222abSNico Weber addr_description(addr, access_size_, /*shouldLockThreadRegistry=*/false), 391217222abSNico Weber pc(pc_), 392217222abSNico Weber bp(bp_), 393217222abSNico Weber sp(sp_), 394217222abSNico Weber access_size(access_size_), 395217222abSNico Weber is_write(is_write_), 396217222abSNico Weber shadow_val(0) { 397217222abSNico Weber scariness.Clear(); 398217222abSNico Weber if (access_size) { 399217222abSNico Weber if (access_size <= 9) { 400217222abSNico Weber char desr[] = "?-byte"; 401217222abSNico Weber desr[0] = '0' + access_size; 402217222abSNico Weber scariness.Scare(access_size + access_size / 2, desr); 403217222abSNico Weber } else if (access_size >= 10) { 404217222abSNico Weber scariness.Scare(15, "multi-byte"); 405217222abSNico Weber } 406217222abSNico Weber is_write ? scariness.Scare(20, "write") : scariness.Scare(1, "read"); 407217222abSNico Weber 408217222abSNico Weber // Determine the error type. 409217222abSNico Weber bug_descr = "unknown-crash"; 410217222abSNico Weber if (AddrIsInMem(addr)) { 411217222abSNico Weber u8 *shadow_addr = (u8 *)MemToShadow(addr); 412217222abSNico Weber // If we are accessing 16 bytes, look at the second shadow byte. 413217222abSNico Weber if (*shadow_addr == 0 && access_size > SHADOW_GRANULARITY) shadow_addr++; 414217222abSNico Weber // If we are in the partial right redzone, look at the next shadow byte. 415217222abSNico Weber if (*shadow_addr > 0 && *shadow_addr < 128) shadow_addr++; 416217222abSNico Weber bool far_from_bounds = false; 417217222abSNico Weber shadow_val = *shadow_addr; 418217222abSNico Weber int bug_type_score = 0; 419217222abSNico Weber // For use-after-frees reads are almost as bad as writes. 420217222abSNico Weber int read_after_free_bonus = 0; 421217222abSNico Weber switch (shadow_val) { 422217222abSNico Weber case kAsanHeapLeftRedzoneMagic: 423217222abSNico Weber case kAsanArrayCookieMagic: 424217222abSNico Weber bug_descr = "heap-buffer-overflow"; 425217222abSNico Weber bug_type_score = 10; 426217222abSNico Weber far_from_bounds = AdjacentShadowValuesAreFullyPoisoned(shadow_addr); 427217222abSNico Weber break; 428217222abSNico Weber case kAsanHeapFreeMagic: 429217222abSNico Weber bug_descr = "heap-use-after-free"; 430217222abSNico Weber bug_type_score = 20; 431217222abSNico Weber if (!is_write) read_after_free_bonus = 18; 432217222abSNico Weber break; 433217222abSNico Weber case kAsanStackLeftRedzoneMagic: 434217222abSNico Weber bug_descr = "stack-buffer-underflow"; 435217222abSNico Weber bug_type_score = 25; 436217222abSNico Weber far_from_bounds = AdjacentShadowValuesAreFullyPoisoned(shadow_addr); 437217222abSNico Weber break; 438217222abSNico Weber case kAsanInitializationOrderMagic: 439217222abSNico Weber bug_descr = "initialization-order-fiasco"; 440217222abSNico Weber bug_type_score = 1; 441217222abSNico Weber break; 442217222abSNico Weber case kAsanStackMidRedzoneMagic: 443217222abSNico Weber case kAsanStackRightRedzoneMagic: 444217222abSNico Weber bug_descr = "stack-buffer-overflow"; 445217222abSNico Weber bug_type_score = 25; 446217222abSNico Weber far_from_bounds = AdjacentShadowValuesAreFullyPoisoned(shadow_addr); 447217222abSNico Weber break; 448217222abSNico Weber case kAsanStackAfterReturnMagic: 449217222abSNico Weber bug_descr = "stack-use-after-return"; 450217222abSNico Weber bug_type_score = 30; 451217222abSNico Weber if (!is_write) read_after_free_bonus = 18; 452217222abSNico Weber break; 453217222abSNico Weber case kAsanUserPoisonedMemoryMagic: 454217222abSNico Weber bug_descr = "use-after-poison"; 455217222abSNico Weber bug_type_score = 20; 456217222abSNico Weber break; 457217222abSNico Weber case kAsanContiguousContainerOOBMagic: 458217222abSNico Weber bug_descr = "container-overflow"; 459217222abSNico Weber bug_type_score = 10; 460217222abSNico Weber break; 461217222abSNico Weber case kAsanStackUseAfterScopeMagic: 462217222abSNico Weber bug_descr = "stack-use-after-scope"; 463217222abSNico Weber bug_type_score = 10; 464217222abSNico Weber break; 465217222abSNico Weber case kAsanGlobalRedzoneMagic: 466217222abSNico Weber bug_descr = "global-buffer-overflow"; 467217222abSNico Weber bug_type_score = 10; 468217222abSNico Weber far_from_bounds = AdjacentShadowValuesAreFullyPoisoned(shadow_addr); 469217222abSNico Weber break; 470217222abSNico Weber case kAsanIntraObjectRedzone: 471217222abSNico Weber bug_descr = "intra-object-overflow"; 472217222abSNico Weber bug_type_score = 10; 473217222abSNico Weber break; 474217222abSNico Weber case kAsanAllocaLeftMagic: 475217222abSNico Weber case kAsanAllocaRightMagic: 476217222abSNico Weber bug_descr = "dynamic-stack-buffer-overflow"; 477217222abSNico Weber bug_type_score = 25; 478217222abSNico Weber far_from_bounds = AdjacentShadowValuesAreFullyPoisoned(shadow_addr); 479217222abSNico Weber break; 480217222abSNico Weber } 481217222abSNico Weber scariness.Scare(bug_type_score + read_after_free_bonus, bug_descr); 482217222abSNico Weber if (far_from_bounds) scariness.Scare(10, "far-from-bounds"); 483217222abSNico Weber } 484217222abSNico Weber } 485217222abSNico Weber } 486217222abSNico Weber 487217222abSNico Weber static void PrintContainerOverflowHint() { 488217222abSNico Weber Printf("HINT: if you don't care about these errors you may set " 489217222abSNico Weber "ASAN_OPTIONS=detect_container_overflow=0.\n" 490217222abSNico Weber "If you suspect a false positive see also: " 491217222abSNico Weber "https://github.com/google/sanitizers/wiki/" 492217222abSNico Weber "AddressSanitizerContainerOverflow.\n"); 493217222abSNico Weber } 494217222abSNico Weber 495217222abSNico Weber static void PrintShadowByte(InternalScopedString *str, const char *before, 496217222abSNico Weber u8 byte, const char *after = "\n") { 497217222abSNico Weber PrintMemoryByte(str, before, byte, /*in_shadow*/true, after); 498217222abSNico Weber } 499217222abSNico Weber 500217222abSNico Weber static void PrintLegend(InternalScopedString *str) { 501217222abSNico Weber str->append( 502217222abSNico Weber "Shadow byte legend (one shadow byte represents %d " 503217222abSNico Weber "application bytes):\n", 504217222abSNico Weber (int)SHADOW_GRANULARITY); 505217222abSNico Weber PrintShadowByte(str, " Addressable: ", 0); 506217222abSNico Weber str->append(" Partially addressable: "); 507217222abSNico Weber for (u8 i = 1; i < SHADOW_GRANULARITY; i++) PrintShadowByte(str, "", i, " "); 508217222abSNico Weber str->append("\n"); 509217222abSNico Weber PrintShadowByte(str, " Heap left redzone: ", 510217222abSNico Weber kAsanHeapLeftRedzoneMagic); 511217222abSNico Weber PrintShadowByte(str, " Freed heap region: ", kAsanHeapFreeMagic); 512217222abSNico Weber PrintShadowByte(str, " Stack left redzone: ", 513217222abSNico Weber kAsanStackLeftRedzoneMagic); 514217222abSNico Weber PrintShadowByte(str, " Stack mid redzone: ", 515217222abSNico Weber kAsanStackMidRedzoneMagic); 516217222abSNico Weber PrintShadowByte(str, " Stack right redzone: ", 517217222abSNico Weber kAsanStackRightRedzoneMagic); 518217222abSNico Weber PrintShadowByte(str, " Stack after return: ", 519217222abSNico Weber kAsanStackAfterReturnMagic); 520217222abSNico Weber PrintShadowByte(str, " Stack use after scope: ", 521217222abSNico Weber kAsanStackUseAfterScopeMagic); 522217222abSNico Weber PrintShadowByte(str, " Global redzone: ", kAsanGlobalRedzoneMagic); 523217222abSNico Weber PrintShadowByte(str, " Global init order: ", 524217222abSNico Weber kAsanInitializationOrderMagic); 525217222abSNico Weber PrintShadowByte(str, " Poisoned by user: ", 526217222abSNico Weber kAsanUserPoisonedMemoryMagic); 527217222abSNico Weber PrintShadowByte(str, " Container overflow: ", 528217222abSNico Weber kAsanContiguousContainerOOBMagic); 529217222abSNico Weber PrintShadowByte(str, " Array cookie: ", 530217222abSNico Weber kAsanArrayCookieMagic); 531217222abSNico Weber PrintShadowByte(str, " Intra object redzone: ", 532217222abSNico Weber kAsanIntraObjectRedzone); 533217222abSNico Weber PrintShadowByte(str, " ASan internal: ", kAsanInternalHeapMagic); 534217222abSNico Weber PrintShadowByte(str, " Left alloca redzone: ", kAsanAllocaLeftMagic); 535217222abSNico Weber PrintShadowByte(str, " Right alloca redzone: ", kAsanAllocaRightMagic); 536217222abSNico Weber } 537217222abSNico Weber 538217222abSNico Weber static void PrintShadowBytes(InternalScopedString *str, const char *before, 539217222abSNico Weber u8 *bytes, u8 *guilty, uptr n) { 540217222abSNico Weber Decorator d; 541217222abSNico Weber if (before) str->append("%s%p:", before, bytes); 542217222abSNico Weber for (uptr i = 0; i < n; i++) { 543217222abSNico Weber u8 *p = bytes + i; 544217222abSNico Weber const char *before = 545217222abSNico Weber p == guilty ? "[" : (p - 1 == guilty && i != 0) ? "" : " "; 546217222abSNico Weber const char *after = p == guilty ? "]" : ""; 547217222abSNico Weber PrintShadowByte(str, before, *p, after); 548217222abSNico Weber } 549217222abSNico Weber str->append("\n"); 550217222abSNico Weber } 551217222abSNico Weber 552217222abSNico Weber static void PrintShadowMemoryForAddress(uptr addr) { 553217222abSNico Weber if (!AddrIsInMem(addr)) return; 554217222abSNico Weber uptr shadow_addr = MemToShadow(addr); 555217222abSNico Weber const uptr n_bytes_per_row = 16; 556217222abSNico Weber uptr aligned_shadow = shadow_addr & ~(n_bytes_per_row - 1); 557e0dadf3dSVitaly Buka InternalScopedString str; 558217222abSNico Weber str.append("Shadow bytes around the buggy address:\n"); 559217222abSNico Weber for (int i = -5; i <= 5; i++) { 560217222abSNico Weber uptr row_shadow_addr = aligned_shadow + i * n_bytes_per_row; 561217222abSNico Weber // Skip rows that would be outside the shadow range. This can happen when 562217222abSNico Weber // the user address is near the bottom, top, or shadow gap of the address 563217222abSNico Weber // space. 564217222abSNico Weber if (!AddrIsInShadow(row_shadow_addr)) continue; 565217222abSNico Weber const char *prefix = (i == 0) ? "=>" : " "; 566217222abSNico Weber PrintShadowBytes(&str, prefix, (u8 *)row_shadow_addr, (u8 *)shadow_addr, 567217222abSNico Weber n_bytes_per_row); 568217222abSNico Weber } 569217222abSNico Weber if (flags()->print_legend) PrintLegend(&str); 570217222abSNico Weber Printf("%s", str.data()); 571217222abSNico Weber } 572217222abSNico Weber 573217222abSNico Weber void ErrorGeneric::Print() { 574217222abSNico Weber Decorator d; 575217222abSNico Weber Printf("%s", d.Error()); 576217222abSNico Weber uptr addr = addr_description.Address(); 577217222abSNico Weber Report("ERROR: AddressSanitizer: %s on address %p at pc %p bp %p sp %p\n", 578*629b40daSMartin Liska bug_descr, (void *)addr, (void *)pc, (void *)bp, (void *)sp); 579217222abSNico Weber Printf("%s", d.Default()); 580217222abSNico Weber 581217222abSNico Weber Printf("%s%s of size %zu at %p thread %s%s\n", d.Access(), 582217222abSNico Weber access_size ? (is_write ? "WRITE" : "READ") : "ACCESS", access_size, 583217222abSNico Weber (void *)addr, AsanThreadIdAndName(tid).c_str(), d.Default()); 584217222abSNico Weber 585217222abSNico Weber scariness.Print(); 586217222abSNico Weber GET_STACK_TRACE_FATAL(pc, bp); 587217222abSNico Weber stack.Print(); 588217222abSNico Weber 589217222abSNico Weber // Pass bug_descr because we have a special case for 590217222abSNico Weber // initialization-order-fiasco 591217222abSNico Weber addr_description.Print(bug_descr); 592217222abSNico Weber if (shadow_val == kAsanContiguousContainerOOBMagic) 593217222abSNico Weber PrintContainerOverflowHint(); 594217222abSNico Weber ReportErrorSummary(bug_descr, &stack); 595217222abSNico Weber PrintShadowMemoryForAddress(addr); 596217222abSNico Weber } 597217222abSNico Weber 598217222abSNico Weber } // namespace __asan 599