168d75effSDimitry Andric //===-- asan_errors.cpp -----------------------------------------*- C++ -*-===// 268d75effSDimitry Andric // 368d75effSDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 468d75effSDimitry Andric // See https://llvm.org/LICENSE.txt for license information. 568d75effSDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 668d75effSDimitry Andric // 768d75effSDimitry Andric //===----------------------------------------------------------------------===// 868d75effSDimitry Andric // 968d75effSDimitry Andric // This file is a part of AddressSanitizer, an address sanity checker. 1068d75effSDimitry Andric // 1168d75effSDimitry Andric // ASan implementation for error structures. 1268d75effSDimitry Andric //===----------------------------------------------------------------------===// 1368d75effSDimitry Andric 1468d75effSDimitry Andric #include "asan_errors.h" 1568d75effSDimitry Andric #include "asan_descriptions.h" 1668d75effSDimitry Andric #include "asan_mapping.h" 1768d75effSDimitry Andric #include "asan_report.h" 1868d75effSDimitry Andric #include "asan_stack.h" 1968d75effSDimitry Andric #include "sanitizer_common/sanitizer_stackdepot.h" 2068d75effSDimitry Andric 2168d75effSDimitry Andric namespace __asan { 2268d75effSDimitry Andric 2368d75effSDimitry Andric static void OnStackUnwind(const SignalContext &sig, 2468d75effSDimitry Andric const void *callback_context, 2568d75effSDimitry Andric BufferedStackTrace *stack) { 2668d75effSDimitry Andric bool fast = common_flags()->fast_unwind_on_fatal; 2768d75effSDimitry Andric #if SANITIZER_FREEBSD || SANITIZER_NETBSD 2868d75effSDimitry Andric // On FreeBSD the slow unwinding that leverages _Unwind_Backtrace() 2968d75effSDimitry Andric // yields the call stack of the signal's handler and not of the code 3068d75effSDimitry Andric // that raised the signal (as it does on Linux). 3168d75effSDimitry Andric fast = true; 3268d75effSDimitry Andric #endif 3368d75effSDimitry Andric // Tests and maybe some users expect that scariness is going to be printed 3468d75effSDimitry Andric // just before the stack. As only asan has scariness score we have no 3568d75effSDimitry Andric // corresponding code in the sanitizer_common and we use this callback to 3668d75effSDimitry Andric // print it. 3768d75effSDimitry Andric static_cast<const ScarinessScoreBase *>(callback_context)->Print(); 3868d75effSDimitry Andric stack->Unwind(StackTrace::GetNextInstructionPc(sig.pc), sig.bp, sig.context, 3968d75effSDimitry Andric fast); 4068d75effSDimitry Andric } 4168d75effSDimitry Andric 4268d75effSDimitry Andric void ErrorDeadlySignal::Print() { 4368d75effSDimitry Andric ReportDeadlySignal(signal, tid, &OnStackUnwind, &scariness); 4468d75effSDimitry Andric } 4568d75effSDimitry Andric 4668d75effSDimitry Andric void ErrorDoubleFree::Print() { 4768d75effSDimitry Andric Decorator d; 4868d75effSDimitry Andric Printf("%s", d.Error()); 4968d75effSDimitry Andric Report( 5068d75effSDimitry Andric "ERROR: AddressSanitizer: attempting %s on %p in thread %s:\n", 5168d75effSDimitry Andric scariness.GetDescription(), addr_description.addr, 5268d75effSDimitry Andric AsanThreadIdAndName(tid).c_str()); 5368d75effSDimitry Andric Printf("%s", d.Default()); 5468d75effSDimitry Andric scariness.Print(); 5568d75effSDimitry Andric GET_STACK_TRACE_FATAL(second_free_stack->trace[0], 5668d75effSDimitry Andric second_free_stack->top_frame_bp); 5768d75effSDimitry Andric stack.Print(); 5868d75effSDimitry Andric addr_description.Print(); 5968d75effSDimitry Andric ReportErrorSummary(scariness.GetDescription(), &stack); 6068d75effSDimitry Andric } 6168d75effSDimitry Andric 6268d75effSDimitry Andric void ErrorNewDeleteTypeMismatch::Print() { 6368d75effSDimitry Andric Decorator d; 6468d75effSDimitry Andric Printf("%s", d.Error()); 6568d75effSDimitry Andric Report( 6668d75effSDimitry Andric "ERROR: AddressSanitizer: %s on %p in thread %s:\n", 6768d75effSDimitry Andric scariness.GetDescription(), addr_description.addr, 6868d75effSDimitry Andric AsanThreadIdAndName(tid).c_str()); 6968d75effSDimitry Andric Printf("%s object passed to delete has wrong type:\n", d.Default()); 7068d75effSDimitry Andric if (delete_size != 0) { 7168d75effSDimitry Andric Printf( 7268d75effSDimitry Andric " size of the allocated type: %zd bytes;\n" 7368d75effSDimitry Andric " size of the deallocated type: %zd bytes.\n", 7468d75effSDimitry Andric addr_description.chunk_access.chunk_size, delete_size); 7568d75effSDimitry Andric } 7668d75effSDimitry Andric const uptr user_alignment = 7768d75effSDimitry Andric addr_description.chunk_access.user_requested_alignment; 7868d75effSDimitry Andric if (delete_alignment != user_alignment) { 7968d75effSDimitry Andric char user_alignment_str[32]; 8068d75effSDimitry Andric char delete_alignment_str[32]; 8168d75effSDimitry Andric internal_snprintf(user_alignment_str, sizeof(user_alignment_str), 8268d75effSDimitry Andric "%zd bytes", user_alignment); 8368d75effSDimitry Andric internal_snprintf(delete_alignment_str, sizeof(delete_alignment_str), 8468d75effSDimitry Andric "%zd bytes", delete_alignment); 8568d75effSDimitry Andric static const char *kDefaultAlignment = "default-aligned"; 8668d75effSDimitry Andric Printf( 8768d75effSDimitry Andric " alignment of the allocated type: %s;\n" 8868d75effSDimitry Andric " alignment of the deallocated type: %s.\n", 8968d75effSDimitry Andric user_alignment > 0 ? user_alignment_str : kDefaultAlignment, 9068d75effSDimitry Andric delete_alignment > 0 ? delete_alignment_str : kDefaultAlignment); 9168d75effSDimitry Andric } 9268d75effSDimitry Andric CHECK_GT(free_stack->size, 0); 9368d75effSDimitry Andric scariness.Print(); 9468d75effSDimitry Andric GET_STACK_TRACE_FATAL(free_stack->trace[0], free_stack->top_frame_bp); 9568d75effSDimitry Andric stack.Print(); 9668d75effSDimitry Andric addr_description.Print(); 9768d75effSDimitry Andric ReportErrorSummary(scariness.GetDescription(), &stack); 9868d75effSDimitry Andric Report( 9968d75effSDimitry Andric "HINT: if you don't care about these errors you may set " 10068d75effSDimitry Andric "ASAN_OPTIONS=new_delete_type_mismatch=0\n"); 10168d75effSDimitry Andric } 10268d75effSDimitry Andric 10368d75effSDimitry Andric void ErrorFreeNotMalloced::Print() { 10468d75effSDimitry Andric Decorator d; 10568d75effSDimitry Andric Printf("%s", d.Error()); 10668d75effSDimitry Andric Report( 10768d75effSDimitry Andric "ERROR: AddressSanitizer: attempting free on address " 10868d75effSDimitry Andric "which was not malloc()-ed: %p in thread %s\n", 10968d75effSDimitry Andric addr_description.Address(), AsanThreadIdAndName(tid).c_str()); 11068d75effSDimitry Andric Printf("%s", d.Default()); 11168d75effSDimitry Andric CHECK_GT(free_stack->size, 0); 11268d75effSDimitry Andric scariness.Print(); 11368d75effSDimitry Andric GET_STACK_TRACE_FATAL(free_stack->trace[0], free_stack->top_frame_bp); 11468d75effSDimitry Andric stack.Print(); 11568d75effSDimitry Andric addr_description.Print(); 11668d75effSDimitry Andric ReportErrorSummary(scariness.GetDescription(), &stack); 11768d75effSDimitry Andric } 11868d75effSDimitry Andric 11968d75effSDimitry Andric void ErrorAllocTypeMismatch::Print() { 12068d75effSDimitry Andric static const char *alloc_names[] = {"INVALID", "malloc", "operator new", 12168d75effSDimitry Andric "operator new []"}; 12268d75effSDimitry Andric static const char *dealloc_names[] = {"INVALID", "free", "operator delete", 12368d75effSDimitry Andric "operator delete []"}; 12468d75effSDimitry Andric CHECK_NE(alloc_type, dealloc_type); 12568d75effSDimitry Andric Decorator d; 12668d75effSDimitry Andric Printf("%s", d.Error()); 12768d75effSDimitry Andric Report("ERROR: AddressSanitizer: %s (%s vs %s) on %p\n", 12868d75effSDimitry Andric scariness.GetDescription(), alloc_names[alloc_type], 12968d75effSDimitry Andric dealloc_names[dealloc_type], addr_description.Address()); 13068d75effSDimitry Andric Printf("%s", d.Default()); 13168d75effSDimitry Andric CHECK_GT(dealloc_stack->size, 0); 13268d75effSDimitry Andric scariness.Print(); 13368d75effSDimitry Andric GET_STACK_TRACE_FATAL(dealloc_stack->trace[0], dealloc_stack->top_frame_bp); 13468d75effSDimitry Andric stack.Print(); 13568d75effSDimitry Andric addr_description.Print(); 13668d75effSDimitry Andric ReportErrorSummary(scariness.GetDescription(), &stack); 13768d75effSDimitry Andric Report( 13868d75effSDimitry Andric "HINT: if you don't care about these errors you may set " 13968d75effSDimitry Andric "ASAN_OPTIONS=alloc_dealloc_mismatch=0\n"); 14068d75effSDimitry Andric } 14168d75effSDimitry Andric 14268d75effSDimitry Andric void ErrorMallocUsableSizeNotOwned::Print() { 14368d75effSDimitry Andric Decorator d; 14468d75effSDimitry Andric Printf("%s", d.Error()); 14568d75effSDimitry Andric Report( 14668d75effSDimitry Andric "ERROR: AddressSanitizer: attempting to call malloc_usable_size() for " 14768d75effSDimitry Andric "pointer which is not owned: %p\n", 14868d75effSDimitry Andric addr_description.Address()); 14968d75effSDimitry Andric Printf("%s", d.Default()); 15068d75effSDimitry Andric stack->Print(); 15168d75effSDimitry Andric addr_description.Print(); 15268d75effSDimitry Andric ReportErrorSummary(scariness.GetDescription(), stack); 15368d75effSDimitry Andric } 15468d75effSDimitry Andric 15568d75effSDimitry Andric void ErrorSanitizerGetAllocatedSizeNotOwned::Print() { 15668d75effSDimitry Andric Decorator d; 15768d75effSDimitry Andric Printf("%s", d.Error()); 15868d75effSDimitry Andric Report( 15968d75effSDimitry Andric "ERROR: AddressSanitizer: attempting to call " 16068d75effSDimitry Andric "__sanitizer_get_allocated_size() for pointer which is not owned: %p\n", 16168d75effSDimitry Andric addr_description.Address()); 16268d75effSDimitry Andric Printf("%s", d.Default()); 16368d75effSDimitry Andric stack->Print(); 16468d75effSDimitry Andric addr_description.Print(); 16568d75effSDimitry Andric ReportErrorSummary(scariness.GetDescription(), stack); 16668d75effSDimitry Andric } 16768d75effSDimitry Andric 16868d75effSDimitry Andric void ErrorCallocOverflow::Print() { 16968d75effSDimitry Andric Decorator d; 17068d75effSDimitry Andric Printf("%s", d.Error()); 17168d75effSDimitry Andric Report( 17268d75effSDimitry Andric "ERROR: AddressSanitizer: calloc parameters overflow: count * size " 17368d75effSDimitry Andric "(%zd * %zd) cannot be represented in type size_t (thread %s)\n", 17468d75effSDimitry Andric count, size, AsanThreadIdAndName(tid).c_str()); 17568d75effSDimitry Andric Printf("%s", d.Default()); 17668d75effSDimitry Andric stack->Print(); 17768d75effSDimitry Andric PrintHintAllocatorCannotReturnNull(); 17868d75effSDimitry Andric ReportErrorSummary(scariness.GetDescription(), stack); 17968d75effSDimitry Andric } 18068d75effSDimitry Andric 18168d75effSDimitry Andric void ErrorReallocArrayOverflow::Print() { 18268d75effSDimitry Andric Decorator d; 18368d75effSDimitry Andric Printf("%s", d.Error()); 18468d75effSDimitry Andric Report( 18568d75effSDimitry Andric "ERROR: AddressSanitizer: reallocarray parameters overflow: count * size " 18668d75effSDimitry Andric "(%zd * %zd) cannot be represented in type size_t (thread %s)\n", 18768d75effSDimitry Andric count, size, AsanThreadIdAndName(tid).c_str()); 18868d75effSDimitry Andric Printf("%s", d.Default()); 18968d75effSDimitry Andric stack->Print(); 19068d75effSDimitry Andric PrintHintAllocatorCannotReturnNull(); 19168d75effSDimitry Andric ReportErrorSummary(scariness.GetDescription(), stack); 19268d75effSDimitry Andric } 19368d75effSDimitry Andric 19468d75effSDimitry Andric void ErrorPvallocOverflow::Print() { 19568d75effSDimitry Andric Decorator d; 19668d75effSDimitry Andric Printf("%s", d.Error()); 19768d75effSDimitry Andric Report( 19868d75effSDimitry Andric "ERROR: AddressSanitizer: pvalloc parameters overflow: size 0x%zx " 19968d75effSDimitry Andric "rounded up to system page size 0x%zx cannot be represented in type " 20068d75effSDimitry Andric "size_t (thread %s)\n", 20168d75effSDimitry Andric size, GetPageSizeCached(), AsanThreadIdAndName(tid).c_str()); 20268d75effSDimitry Andric Printf("%s", d.Default()); 20368d75effSDimitry Andric stack->Print(); 20468d75effSDimitry Andric PrintHintAllocatorCannotReturnNull(); 20568d75effSDimitry Andric ReportErrorSummary(scariness.GetDescription(), stack); 20668d75effSDimitry Andric } 20768d75effSDimitry Andric 20868d75effSDimitry Andric void ErrorInvalidAllocationAlignment::Print() { 20968d75effSDimitry Andric Decorator d; 21068d75effSDimitry Andric Printf("%s", d.Error()); 21168d75effSDimitry Andric Report( 21268d75effSDimitry Andric "ERROR: AddressSanitizer: invalid allocation alignment: %zd, " 21368d75effSDimitry Andric "alignment must be a power of two (thread %s)\n", 21468d75effSDimitry Andric alignment, AsanThreadIdAndName(tid).c_str()); 21568d75effSDimitry Andric Printf("%s", d.Default()); 21668d75effSDimitry Andric stack->Print(); 21768d75effSDimitry Andric PrintHintAllocatorCannotReturnNull(); 21868d75effSDimitry Andric ReportErrorSummary(scariness.GetDescription(), stack); 21968d75effSDimitry Andric } 22068d75effSDimitry Andric 22168d75effSDimitry Andric void ErrorInvalidAlignedAllocAlignment::Print() { 22268d75effSDimitry Andric Decorator d; 22368d75effSDimitry Andric Printf("%s", d.Error()); 22468d75effSDimitry Andric #if SANITIZER_POSIX 22568d75effSDimitry Andric Report("ERROR: AddressSanitizer: invalid alignment requested in " 22668d75effSDimitry Andric "aligned_alloc: %zd, alignment must be a power of two and the " 22768d75effSDimitry Andric "requested size 0x%zx must be a multiple of alignment " 22868d75effSDimitry Andric "(thread %s)\n", alignment, size, AsanThreadIdAndName(tid).c_str()); 22968d75effSDimitry Andric #else 23068d75effSDimitry Andric Report("ERROR: AddressSanitizer: invalid alignment requested in " 23168d75effSDimitry Andric "aligned_alloc: %zd, the requested size 0x%zx must be a multiple of " 23268d75effSDimitry Andric "alignment (thread %s)\n", alignment, size, 23368d75effSDimitry Andric AsanThreadIdAndName(tid).c_str()); 23468d75effSDimitry Andric #endif 23568d75effSDimitry Andric Printf("%s", d.Default()); 23668d75effSDimitry Andric stack->Print(); 23768d75effSDimitry Andric PrintHintAllocatorCannotReturnNull(); 23868d75effSDimitry Andric ReportErrorSummary(scariness.GetDescription(), stack); 23968d75effSDimitry Andric } 24068d75effSDimitry Andric 24168d75effSDimitry Andric void ErrorInvalidPosixMemalignAlignment::Print() { 24268d75effSDimitry Andric Decorator d; 24368d75effSDimitry Andric Printf("%s", d.Error()); 24468d75effSDimitry Andric Report( 24568d75effSDimitry Andric "ERROR: AddressSanitizer: invalid alignment requested in posix_memalign: " 24668d75effSDimitry Andric "%zd, alignment must be a power of two and a multiple of sizeof(void*) " 24768d75effSDimitry Andric "== %zd (thread %s)\n", 24868d75effSDimitry Andric alignment, sizeof(void *), AsanThreadIdAndName(tid).c_str()); 24968d75effSDimitry Andric Printf("%s", d.Default()); 25068d75effSDimitry Andric stack->Print(); 25168d75effSDimitry Andric PrintHintAllocatorCannotReturnNull(); 25268d75effSDimitry Andric ReportErrorSummary(scariness.GetDescription(), stack); 25368d75effSDimitry Andric } 25468d75effSDimitry Andric 25568d75effSDimitry Andric void ErrorAllocationSizeTooBig::Print() { 25668d75effSDimitry Andric Decorator d; 25768d75effSDimitry Andric Printf("%s", d.Error()); 25868d75effSDimitry Andric Report( 25968d75effSDimitry Andric "ERROR: AddressSanitizer: requested allocation size 0x%zx (0x%zx after " 26068d75effSDimitry Andric "adjustments for alignment, red zones etc.) exceeds maximum supported " 26168d75effSDimitry Andric "size of 0x%zx (thread %s)\n", 26268d75effSDimitry Andric user_size, total_size, max_size, AsanThreadIdAndName(tid).c_str()); 26368d75effSDimitry Andric Printf("%s", d.Default()); 26468d75effSDimitry Andric stack->Print(); 26568d75effSDimitry Andric PrintHintAllocatorCannotReturnNull(); 26668d75effSDimitry Andric ReportErrorSummary(scariness.GetDescription(), stack); 26768d75effSDimitry Andric } 26868d75effSDimitry Andric 26968d75effSDimitry Andric void ErrorRssLimitExceeded::Print() { 27068d75effSDimitry Andric Decorator d; 27168d75effSDimitry Andric Printf("%s", d.Error()); 27268d75effSDimitry Andric Report( 27368d75effSDimitry Andric "ERROR: AddressSanitizer: specified RSS limit exceeded, currently set to " 27468d75effSDimitry Andric "soft_rss_limit_mb=%zd\n", common_flags()->soft_rss_limit_mb); 27568d75effSDimitry Andric Printf("%s", d.Default()); 27668d75effSDimitry Andric stack->Print(); 27768d75effSDimitry Andric PrintHintAllocatorCannotReturnNull(); 27868d75effSDimitry Andric ReportErrorSummary(scariness.GetDescription(), stack); 27968d75effSDimitry Andric } 28068d75effSDimitry Andric 28168d75effSDimitry Andric void ErrorOutOfMemory::Print() { 28268d75effSDimitry Andric Decorator d; 28368d75effSDimitry Andric Printf("%s", d.Error()); 28468d75effSDimitry Andric Report( 28568d75effSDimitry Andric "ERROR: AddressSanitizer: allocator is out of memory trying to allocate " 28668d75effSDimitry Andric "0x%zx bytes\n", requested_size); 28768d75effSDimitry Andric Printf("%s", d.Default()); 28868d75effSDimitry Andric stack->Print(); 28968d75effSDimitry Andric PrintHintAllocatorCannotReturnNull(); 29068d75effSDimitry Andric ReportErrorSummary(scariness.GetDescription(), stack); 29168d75effSDimitry Andric } 29268d75effSDimitry Andric 29368d75effSDimitry Andric void ErrorStringFunctionMemoryRangesOverlap::Print() { 29468d75effSDimitry Andric Decorator d; 29568d75effSDimitry Andric char bug_type[100]; 29668d75effSDimitry Andric internal_snprintf(bug_type, sizeof(bug_type), "%s-param-overlap", function); 29768d75effSDimitry Andric Printf("%s", d.Error()); 29868d75effSDimitry Andric Report( 29968d75effSDimitry Andric "ERROR: AddressSanitizer: %s: memory ranges [%p,%p) and [%p, %p) " 30068d75effSDimitry Andric "overlap\n", 30168d75effSDimitry Andric bug_type, addr1_description.Address(), 30268d75effSDimitry Andric addr1_description.Address() + length1, addr2_description.Address(), 30368d75effSDimitry Andric addr2_description.Address() + length2); 30468d75effSDimitry Andric Printf("%s", d.Default()); 30568d75effSDimitry Andric scariness.Print(); 30668d75effSDimitry Andric stack->Print(); 30768d75effSDimitry Andric addr1_description.Print(); 30868d75effSDimitry Andric addr2_description.Print(); 30968d75effSDimitry Andric ReportErrorSummary(bug_type, stack); 31068d75effSDimitry Andric } 31168d75effSDimitry Andric 31268d75effSDimitry Andric void ErrorStringFunctionSizeOverflow::Print() { 31368d75effSDimitry Andric Decorator d; 31468d75effSDimitry Andric Printf("%s", d.Error()); 31568d75effSDimitry Andric Report("ERROR: AddressSanitizer: %s: (size=%zd)\n", 31668d75effSDimitry Andric scariness.GetDescription(), size); 31768d75effSDimitry Andric Printf("%s", d.Default()); 31868d75effSDimitry Andric scariness.Print(); 31968d75effSDimitry Andric stack->Print(); 32068d75effSDimitry Andric addr_description.Print(); 32168d75effSDimitry Andric ReportErrorSummary(scariness.GetDescription(), stack); 32268d75effSDimitry Andric } 32368d75effSDimitry Andric 32468d75effSDimitry Andric void ErrorBadParamsToAnnotateContiguousContainer::Print() { 32568d75effSDimitry Andric Report( 32668d75effSDimitry Andric "ERROR: AddressSanitizer: bad parameters to " 32768d75effSDimitry Andric "__sanitizer_annotate_contiguous_container:\n" 32868d75effSDimitry Andric " beg : %p\n" 32968d75effSDimitry Andric " end : %p\n" 33068d75effSDimitry Andric " old_mid : %p\n" 33168d75effSDimitry Andric " new_mid : %p\n", 33268d75effSDimitry Andric beg, end, old_mid, new_mid); 33368d75effSDimitry Andric uptr granularity = SHADOW_GRANULARITY; 33468d75effSDimitry Andric if (!IsAligned(beg, granularity)) 33568d75effSDimitry Andric Report("ERROR: beg is not aligned by %d\n", granularity); 33668d75effSDimitry Andric stack->Print(); 33768d75effSDimitry Andric ReportErrorSummary(scariness.GetDescription(), stack); 33868d75effSDimitry Andric } 33968d75effSDimitry Andric 34068d75effSDimitry Andric void ErrorODRViolation::Print() { 34168d75effSDimitry Andric Decorator d; 34268d75effSDimitry Andric Printf("%s", d.Error()); 34368d75effSDimitry Andric Report("ERROR: AddressSanitizer: %s (%p):\n", scariness.GetDescription(), 34468d75effSDimitry Andric global1.beg); 34568d75effSDimitry Andric Printf("%s", d.Default()); 346*fe6060f1SDimitry Andric InternalScopedString g1_loc; 347*fe6060f1SDimitry Andric InternalScopedString g2_loc; 34868d75effSDimitry Andric PrintGlobalLocation(&g1_loc, global1); 34968d75effSDimitry Andric PrintGlobalLocation(&g2_loc, global2); 35068d75effSDimitry Andric Printf(" [1] size=%zd '%s' %s\n", global1.size, 35168d75effSDimitry Andric MaybeDemangleGlobalName(global1.name), g1_loc.data()); 35268d75effSDimitry Andric Printf(" [2] size=%zd '%s' %s\n", global2.size, 35368d75effSDimitry Andric MaybeDemangleGlobalName(global2.name), g2_loc.data()); 35468d75effSDimitry Andric if (stack_id1 && stack_id2) { 35568d75effSDimitry Andric Printf("These globals were registered at these points:\n"); 35668d75effSDimitry Andric Printf(" [1]:\n"); 35768d75effSDimitry Andric StackDepotGet(stack_id1).Print(); 35868d75effSDimitry Andric Printf(" [2]:\n"); 35968d75effSDimitry Andric StackDepotGet(stack_id2).Print(); 36068d75effSDimitry Andric } 36168d75effSDimitry Andric Report( 36268d75effSDimitry Andric "HINT: if you don't care about these errors you may set " 36368d75effSDimitry Andric "ASAN_OPTIONS=detect_odr_violation=0\n"); 364*fe6060f1SDimitry Andric InternalScopedString error_msg; 36568d75effSDimitry Andric error_msg.append("%s: global '%s' at %s", scariness.GetDescription(), 36668d75effSDimitry Andric MaybeDemangleGlobalName(global1.name), g1_loc.data()); 36768d75effSDimitry Andric ReportErrorSummary(error_msg.data()); 36868d75effSDimitry Andric } 36968d75effSDimitry Andric 37068d75effSDimitry Andric void ErrorInvalidPointerPair::Print() { 37168d75effSDimitry Andric Decorator d; 37268d75effSDimitry Andric Printf("%s", d.Error()); 37368d75effSDimitry Andric Report("ERROR: AddressSanitizer: %s: %p %p\n", scariness.GetDescription(), 37468d75effSDimitry Andric addr1_description.Address(), addr2_description.Address()); 37568d75effSDimitry Andric Printf("%s", d.Default()); 37668d75effSDimitry Andric GET_STACK_TRACE_FATAL(pc, bp); 37768d75effSDimitry Andric stack.Print(); 37868d75effSDimitry Andric addr1_description.Print(); 37968d75effSDimitry Andric addr2_description.Print(); 38068d75effSDimitry Andric ReportErrorSummary(scariness.GetDescription(), &stack); 38168d75effSDimitry Andric } 38268d75effSDimitry Andric 38368d75effSDimitry Andric static bool AdjacentShadowValuesAreFullyPoisoned(u8 *s) { 38468d75effSDimitry Andric return s[-1] > 127 && s[1] > 127; 38568d75effSDimitry Andric } 38668d75effSDimitry Andric 38768d75effSDimitry Andric ErrorGeneric::ErrorGeneric(u32 tid, uptr pc_, uptr bp_, uptr sp_, uptr addr, 38868d75effSDimitry Andric bool is_write_, uptr access_size_) 38968d75effSDimitry Andric : ErrorBase(tid), 39068d75effSDimitry Andric addr_description(addr, access_size_, /*shouldLockThreadRegistry=*/false), 39168d75effSDimitry Andric pc(pc_), 39268d75effSDimitry Andric bp(bp_), 39368d75effSDimitry Andric sp(sp_), 39468d75effSDimitry Andric access_size(access_size_), 39568d75effSDimitry Andric is_write(is_write_), 39668d75effSDimitry Andric shadow_val(0) { 39768d75effSDimitry Andric scariness.Clear(); 39868d75effSDimitry Andric if (access_size) { 39968d75effSDimitry Andric if (access_size <= 9) { 40068d75effSDimitry Andric char desr[] = "?-byte"; 40168d75effSDimitry Andric desr[0] = '0' + access_size; 40268d75effSDimitry Andric scariness.Scare(access_size + access_size / 2, desr); 40368d75effSDimitry Andric } else if (access_size >= 10) { 40468d75effSDimitry Andric scariness.Scare(15, "multi-byte"); 40568d75effSDimitry Andric } 40668d75effSDimitry Andric is_write ? scariness.Scare(20, "write") : scariness.Scare(1, "read"); 40768d75effSDimitry Andric 40868d75effSDimitry Andric // Determine the error type. 40968d75effSDimitry Andric bug_descr = "unknown-crash"; 41068d75effSDimitry Andric if (AddrIsInMem(addr)) { 41168d75effSDimitry Andric u8 *shadow_addr = (u8 *)MemToShadow(addr); 41268d75effSDimitry Andric // If we are accessing 16 bytes, look at the second shadow byte. 41368d75effSDimitry Andric if (*shadow_addr == 0 && access_size > SHADOW_GRANULARITY) shadow_addr++; 41468d75effSDimitry Andric // If we are in the partial right redzone, look at the next shadow byte. 41568d75effSDimitry Andric if (*shadow_addr > 0 && *shadow_addr < 128) shadow_addr++; 41668d75effSDimitry Andric bool far_from_bounds = false; 41768d75effSDimitry Andric shadow_val = *shadow_addr; 41868d75effSDimitry Andric int bug_type_score = 0; 41968d75effSDimitry Andric // For use-after-frees reads are almost as bad as writes. 42068d75effSDimitry Andric int read_after_free_bonus = 0; 42168d75effSDimitry Andric switch (shadow_val) { 42268d75effSDimitry Andric case kAsanHeapLeftRedzoneMagic: 42368d75effSDimitry Andric case kAsanArrayCookieMagic: 42468d75effSDimitry Andric bug_descr = "heap-buffer-overflow"; 42568d75effSDimitry Andric bug_type_score = 10; 42668d75effSDimitry Andric far_from_bounds = AdjacentShadowValuesAreFullyPoisoned(shadow_addr); 42768d75effSDimitry Andric break; 42868d75effSDimitry Andric case kAsanHeapFreeMagic: 42968d75effSDimitry Andric bug_descr = "heap-use-after-free"; 43068d75effSDimitry Andric bug_type_score = 20; 43168d75effSDimitry Andric if (!is_write) read_after_free_bonus = 18; 43268d75effSDimitry Andric break; 43368d75effSDimitry Andric case kAsanStackLeftRedzoneMagic: 43468d75effSDimitry Andric bug_descr = "stack-buffer-underflow"; 43568d75effSDimitry Andric bug_type_score = 25; 43668d75effSDimitry Andric far_from_bounds = AdjacentShadowValuesAreFullyPoisoned(shadow_addr); 43768d75effSDimitry Andric break; 43868d75effSDimitry Andric case kAsanInitializationOrderMagic: 43968d75effSDimitry Andric bug_descr = "initialization-order-fiasco"; 44068d75effSDimitry Andric bug_type_score = 1; 44168d75effSDimitry Andric break; 44268d75effSDimitry Andric case kAsanStackMidRedzoneMagic: 44368d75effSDimitry Andric case kAsanStackRightRedzoneMagic: 44468d75effSDimitry Andric bug_descr = "stack-buffer-overflow"; 44568d75effSDimitry Andric bug_type_score = 25; 44668d75effSDimitry Andric far_from_bounds = AdjacentShadowValuesAreFullyPoisoned(shadow_addr); 44768d75effSDimitry Andric break; 44868d75effSDimitry Andric case kAsanStackAfterReturnMagic: 44968d75effSDimitry Andric bug_descr = "stack-use-after-return"; 45068d75effSDimitry Andric bug_type_score = 30; 45168d75effSDimitry Andric if (!is_write) read_after_free_bonus = 18; 45268d75effSDimitry Andric break; 45368d75effSDimitry Andric case kAsanUserPoisonedMemoryMagic: 45468d75effSDimitry Andric bug_descr = "use-after-poison"; 45568d75effSDimitry Andric bug_type_score = 20; 45668d75effSDimitry Andric break; 45768d75effSDimitry Andric case kAsanContiguousContainerOOBMagic: 45868d75effSDimitry Andric bug_descr = "container-overflow"; 45968d75effSDimitry Andric bug_type_score = 10; 46068d75effSDimitry Andric break; 46168d75effSDimitry Andric case kAsanStackUseAfterScopeMagic: 46268d75effSDimitry Andric bug_descr = "stack-use-after-scope"; 46368d75effSDimitry Andric bug_type_score = 10; 46468d75effSDimitry Andric break; 46568d75effSDimitry Andric case kAsanGlobalRedzoneMagic: 46668d75effSDimitry Andric bug_descr = "global-buffer-overflow"; 46768d75effSDimitry Andric bug_type_score = 10; 46868d75effSDimitry Andric far_from_bounds = AdjacentShadowValuesAreFullyPoisoned(shadow_addr); 46968d75effSDimitry Andric break; 47068d75effSDimitry Andric case kAsanIntraObjectRedzone: 47168d75effSDimitry Andric bug_descr = "intra-object-overflow"; 47268d75effSDimitry Andric bug_type_score = 10; 47368d75effSDimitry Andric break; 47468d75effSDimitry Andric case kAsanAllocaLeftMagic: 47568d75effSDimitry Andric case kAsanAllocaRightMagic: 47668d75effSDimitry Andric bug_descr = "dynamic-stack-buffer-overflow"; 47768d75effSDimitry Andric bug_type_score = 25; 47868d75effSDimitry Andric far_from_bounds = AdjacentShadowValuesAreFullyPoisoned(shadow_addr); 47968d75effSDimitry Andric break; 48068d75effSDimitry Andric } 48168d75effSDimitry Andric scariness.Scare(bug_type_score + read_after_free_bonus, bug_descr); 48268d75effSDimitry Andric if (far_from_bounds) scariness.Scare(10, "far-from-bounds"); 48368d75effSDimitry Andric } 48468d75effSDimitry Andric } 48568d75effSDimitry Andric } 48668d75effSDimitry Andric 48768d75effSDimitry Andric static void PrintContainerOverflowHint() { 48868d75effSDimitry Andric Printf("HINT: if you don't care about these errors you may set " 48968d75effSDimitry Andric "ASAN_OPTIONS=detect_container_overflow=0.\n" 49068d75effSDimitry Andric "If you suspect a false positive see also: " 49168d75effSDimitry Andric "https://github.com/google/sanitizers/wiki/" 49268d75effSDimitry Andric "AddressSanitizerContainerOverflow.\n"); 49368d75effSDimitry Andric } 49468d75effSDimitry Andric 49568d75effSDimitry Andric static void PrintShadowByte(InternalScopedString *str, const char *before, 49668d75effSDimitry Andric u8 byte, const char *after = "\n") { 49768d75effSDimitry Andric PrintMemoryByte(str, before, byte, /*in_shadow*/true, after); 49868d75effSDimitry Andric } 49968d75effSDimitry Andric 50068d75effSDimitry Andric static void PrintLegend(InternalScopedString *str) { 50168d75effSDimitry Andric str->append( 50268d75effSDimitry Andric "Shadow byte legend (one shadow byte represents %d " 50368d75effSDimitry Andric "application bytes):\n", 50468d75effSDimitry Andric (int)SHADOW_GRANULARITY); 50568d75effSDimitry Andric PrintShadowByte(str, " Addressable: ", 0); 50668d75effSDimitry Andric str->append(" Partially addressable: "); 50768d75effSDimitry Andric for (u8 i = 1; i < SHADOW_GRANULARITY; i++) PrintShadowByte(str, "", i, " "); 50868d75effSDimitry Andric str->append("\n"); 50968d75effSDimitry Andric PrintShadowByte(str, " Heap left redzone: ", 51068d75effSDimitry Andric kAsanHeapLeftRedzoneMagic); 51168d75effSDimitry Andric PrintShadowByte(str, " Freed heap region: ", kAsanHeapFreeMagic); 51268d75effSDimitry Andric PrintShadowByte(str, " Stack left redzone: ", 51368d75effSDimitry Andric kAsanStackLeftRedzoneMagic); 51468d75effSDimitry Andric PrintShadowByte(str, " Stack mid redzone: ", 51568d75effSDimitry Andric kAsanStackMidRedzoneMagic); 51668d75effSDimitry Andric PrintShadowByte(str, " Stack right redzone: ", 51768d75effSDimitry Andric kAsanStackRightRedzoneMagic); 51868d75effSDimitry Andric PrintShadowByte(str, " Stack after return: ", 51968d75effSDimitry Andric kAsanStackAfterReturnMagic); 52068d75effSDimitry Andric PrintShadowByte(str, " Stack use after scope: ", 52168d75effSDimitry Andric kAsanStackUseAfterScopeMagic); 52268d75effSDimitry Andric PrintShadowByte(str, " Global redzone: ", kAsanGlobalRedzoneMagic); 52368d75effSDimitry Andric PrintShadowByte(str, " Global init order: ", 52468d75effSDimitry Andric kAsanInitializationOrderMagic); 52568d75effSDimitry Andric PrintShadowByte(str, " Poisoned by user: ", 52668d75effSDimitry Andric kAsanUserPoisonedMemoryMagic); 52768d75effSDimitry Andric PrintShadowByte(str, " Container overflow: ", 52868d75effSDimitry Andric kAsanContiguousContainerOOBMagic); 52968d75effSDimitry Andric PrintShadowByte(str, " Array cookie: ", 53068d75effSDimitry Andric kAsanArrayCookieMagic); 53168d75effSDimitry Andric PrintShadowByte(str, " Intra object redzone: ", 53268d75effSDimitry Andric kAsanIntraObjectRedzone); 53368d75effSDimitry Andric PrintShadowByte(str, " ASan internal: ", kAsanInternalHeapMagic); 53468d75effSDimitry Andric PrintShadowByte(str, " Left alloca redzone: ", kAsanAllocaLeftMagic); 53568d75effSDimitry Andric PrintShadowByte(str, " Right alloca redzone: ", kAsanAllocaRightMagic); 53668d75effSDimitry Andric } 53768d75effSDimitry Andric 53868d75effSDimitry Andric static void PrintShadowBytes(InternalScopedString *str, const char *before, 53968d75effSDimitry Andric u8 *bytes, u8 *guilty, uptr n) { 54068d75effSDimitry Andric Decorator d; 54168d75effSDimitry Andric if (before) str->append("%s%p:", before, bytes); 54268d75effSDimitry Andric for (uptr i = 0; i < n; i++) { 54368d75effSDimitry Andric u8 *p = bytes + i; 54468d75effSDimitry Andric const char *before = 54568d75effSDimitry Andric p == guilty ? "[" : (p - 1 == guilty && i != 0) ? "" : " "; 54668d75effSDimitry Andric const char *after = p == guilty ? "]" : ""; 54768d75effSDimitry Andric PrintShadowByte(str, before, *p, after); 54868d75effSDimitry Andric } 54968d75effSDimitry Andric str->append("\n"); 55068d75effSDimitry Andric } 55168d75effSDimitry Andric 55268d75effSDimitry Andric static void PrintShadowMemoryForAddress(uptr addr) { 55368d75effSDimitry Andric if (!AddrIsInMem(addr)) return; 55468d75effSDimitry Andric uptr shadow_addr = MemToShadow(addr); 55568d75effSDimitry Andric const uptr n_bytes_per_row = 16; 55668d75effSDimitry Andric uptr aligned_shadow = shadow_addr & ~(n_bytes_per_row - 1); 557*fe6060f1SDimitry Andric InternalScopedString str; 55868d75effSDimitry Andric str.append("Shadow bytes around the buggy address:\n"); 55968d75effSDimitry Andric for (int i = -5; i <= 5; i++) { 56068d75effSDimitry Andric uptr row_shadow_addr = aligned_shadow + i * n_bytes_per_row; 56168d75effSDimitry Andric // Skip rows that would be outside the shadow range. This can happen when 56268d75effSDimitry Andric // the user address is near the bottom, top, or shadow gap of the address 56368d75effSDimitry Andric // space. 56468d75effSDimitry Andric if (!AddrIsInShadow(row_shadow_addr)) continue; 56568d75effSDimitry Andric const char *prefix = (i == 0) ? "=>" : " "; 56668d75effSDimitry Andric PrintShadowBytes(&str, prefix, (u8 *)row_shadow_addr, (u8 *)shadow_addr, 56768d75effSDimitry Andric n_bytes_per_row); 56868d75effSDimitry Andric } 56968d75effSDimitry Andric if (flags()->print_legend) PrintLegend(&str); 57068d75effSDimitry Andric Printf("%s", str.data()); 57168d75effSDimitry Andric } 57268d75effSDimitry Andric 57368d75effSDimitry Andric void ErrorGeneric::Print() { 57468d75effSDimitry Andric Decorator d; 57568d75effSDimitry Andric Printf("%s", d.Error()); 57668d75effSDimitry Andric uptr addr = addr_description.Address(); 57768d75effSDimitry Andric Report("ERROR: AddressSanitizer: %s on address %p at pc %p bp %p sp %p\n", 57868d75effSDimitry Andric bug_descr, (void *)addr, pc, bp, sp); 57968d75effSDimitry Andric Printf("%s", d.Default()); 58068d75effSDimitry Andric 58168d75effSDimitry Andric Printf("%s%s of size %zu at %p thread %s%s\n", d.Access(), 58268d75effSDimitry Andric access_size ? (is_write ? "WRITE" : "READ") : "ACCESS", access_size, 58368d75effSDimitry Andric (void *)addr, AsanThreadIdAndName(tid).c_str(), d.Default()); 58468d75effSDimitry Andric 58568d75effSDimitry Andric scariness.Print(); 58668d75effSDimitry Andric GET_STACK_TRACE_FATAL(pc, bp); 58768d75effSDimitry Andric stack.Print(); 58868d75effSDimitry Andric 58968d75effSDimitry Andric // Pass bug_descr because we have a special case for 59068d75effSDimitry Andric // initialization-order-fiasco 59168d75effSDimitry Andric addr_description.Print(bug_descr); 59268d75effSDimitry Andric if (shadow_val == kAsanContiguousContainerOOBMagic) 59368d75effSDimitry Andric PrintContainerOverflowHint(); 59468d75effSDimitry Andric ReportErrorSummary(bug_descr, &stack); 59568d75effSDimitry Andric PrintShadowMemoryForAddress(addr); 59668d75effSDimitry Andric } 59768d75effSDimitry Andric 59868d75effSDimitry Andric } // namespace __asan 599