Path: blob/main/contrib/llvm-project/compiler-rt/lib/asan/asan_errors.cpp
35233 views
//===-- asan_errors.cpp -----------------------------------------*- C++ -*-===//1//2// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.3// See https://llvm.org/LICENSE.txt for license information.4// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception5//6//===----------------------------------------------------------------------===//7//8// This file is a part of AddressSanitizer, an address sanity checker.9//10// ASan implementation for error structures.11//===----------------------------------------------------------------------===//1213#include "asan_errors.h"14#include "asan_descriptions.h"15#include "asan_mapping.h"16#include "asan_report.h"17#include "asan_stack.h"18#include "sanitizer_common/sanitizer_stackdepot.h"1920namespace __asan {2122static void OnStackUnwind(const SignalContext &sig,23const void *callback_context,24BufferedStackTrace *stack) {25bool fast = common_flags()->fast_unwind_on_fatal;26#if SANITIZER_FREEBSD || SANITIZER_NETBSD27// On FreeBSD the slow unwinding that leverages _Unwind_Backtrace()28// yields the call stack of the signal's handler and not of the code29// that raised the signal (as it does on Linux).30fast = true;31#endif32// Tests and maybe some users expect that scariness is going to be printed33// just before the stack. As only asan has scariness score we have no34// corresponding code in the sanitizer_common and we use this callback to35// print it.36static_cast<const ScarinessScoreBase *>(callback_context)->Print();37stack->Unwind(StackTrace::GetNextInstructionPc(sig.pc), sig.bp, sig.context,38fast);39}4041void ErrorDeadlySignal::Print() {42ReportDeadlySignal(signal, tid, &OnStackUnwind, &scariness);43}4445void ErrorDoubleFree::Print() {46Decorator d;47Printf("%s", d.Error());48Report("ERROR: AddressSanitizer: attempting %s on %p in thread %s:\n",49scariness.GetDescription(), (void *)addr_description.addr,50AsanThreadIdAndName(tid).c_str());51Printf("%s", d.Default());52scariness.Print();53GET_STACK_TRACE_FATAL(second_free_stack->trace[0],54second_free_stack->top_frame_bp);55stack.Print();56addr_description.Print();57ReportErrorSummary(scariness.GetDescription(), &stack);58}5960void ErrorNewDeleteTypeMismatch::Print() {61Decorator d;62Printf("%s", d.Error());63Report("ERROR: AddressSanitizer: %s on %p in thread %s:\n",64scariness.GetDescription(), (void *)addr_description.addr,65AsanThreadIdAndName(tid).c_str());66Printf("%s object passed to delete has wrong type:\n", d.Default());67if (delete_size != 0) {68Printf(69" size of the allocated type: %zd bytes;\n"70" size of the deallocated type: %zd bytes.\n",71addr_description.chunk_access.chunk_size, delete_size);72}73const uptr user_alignment =74addr_description.chunk_access.user_requested_alignment;75if (delete_alignment != user_alignment) {76char user_alignment_str[32];77char delete_alignment_str[32];78internal_snprintf(user_alignment_str, sizeof(user_alignment_str),79"%zd bytes", user_alignment);80internal_snprintf(delete_alignment_str, sizeof(delete_alignment_str),81"%zd bytes", delete_alignment);82static const char *kDefaultAlignment = "default-aligned";83Printf(84" alignment of the allocated type: %s;\n"85" alignment of the deallocated type: %s.\n",86user_alignment > 0 ? user_alignment_str : kDefaultAlignment,87delete_alignment > 0 ? delete_alignment_str : kDefaultAlignment);88}89CHECK_GT(free_stack->size, 0);90scariness.Print();91GET_STACK_TRACE_FATAL(free_stack->trace[0], free_stack->top_frame_bp);92stack.Print();93addr_description.Print();94ReportErrorSummary(scariness.GetDescription(), &stack);95Report(96"HINT: if you don't care about these errors you may set "97"ASAN_OPTIONS=new_delete_type_mismatch=0\n");98}99100void ErrorFreeNotMalloced::Print() {101Decorator d;102Printf("%s", d.Error());103Report(104"ERROR: AddressSanitizer: attempting free on address "105"which was not malloc()-ed: %p in thread %s\n",106(void *)addr_description.Address(), AsanThreadIdAndName(tid).c_str());107Printf("%s", d.Default());108CHECK_GT(free_stack->size, 0);109scariness.Print();110GET_STACK_TRACE_FATAL(free_stack->trace[0], free_stack->top_frame_bp);111stack.Print();112addr_description.Print();113ReportErrorSummary(scariness.GetDescription(), &stack);114}115116void ErrorAllocTypeMismatch::Print() {117static const char *alloc_names[] = {"INVALID", "malloc", "operator new",118"operator new []"};119static const char *dealloc_names[] = {"INVALID", "free", "operator delete",120"operator delete []"};121CHECK_NE(alloc_type, dealloc_type);122Decorator d;123Printf("%s", d.Error());124Report("ERROR: AddressSanitizer: %s (%s vs %s) on %p\n",125scariness.GetDescription(), alloc_names[alloc_type],126dealloc_names[dealloc_type], (void *)addr_description.Address());127Printf("%s", d.Default());128CHECK_GT(dealloc_stack->size, 0);129scariness.Print();130GET_STACK_TRACE_FATAL(dealloc_stack->trace[0], dealloc_stack->top_frame_bp);131stack.Print();132addr_description.Print();133ReportErrorSummary(scariness.GetDescription(), &stack);134Report(135"HINT: if you don't care about these errors you may set "136"ASAN_OPTIONS=alloc_dealloc_mismatch=0\n");137}138139void ErrorMallocUsableSizeNotOwned::Print() {140Decorator d;141Printf("%s", d.Error());142Report(143"ERROR: AddressSanitizer: attempting to call malloc_usable_size() for "144"pointer which is not owned: %p\n",145(void *)addr_description.Address());146Printf("%s", d.Default());147stack->Print();148addr_description.Print();149ReportErrorSummary(scariness.GetDescription(), stack);150}151152void ErrorSanitizerGetAllocatedSizeNotOwned::Print() {153Decorator d;154Printf("%s", d.Error());155Report(156"ERROR: AddressSanitizer: attempting to call "157"__sanitizer_get_allocated_size() for pointer which is not owned: %p\n",158(void *)addr_description.Address());159Printf("%s", d.Default());160stack->Print();161addr_description.Print();162ReportErrorSummary(scariness.GetDescription(), stack);163}164165void ErrorCallocOverflow::Print() {166Decorator d;167Printf("%s", d.Error());168Report(169"ERROR: AddressSanitizer: calloc parameters overflow: count * size "170"(%zd * %zd) cannot be represented in type size_t (thread %s)\n",171count, size, AsanThreadIdAndName(tid).c_str());172Printf("%s", d.Default());173stack->Print();174PrintHintAllocatorCannotReturnNull();175ReportErrorSummary(scariness.GetDescription(), stack);176}177178void ErrorReallocArrayOverflow::Print() {179Decorator d;180Printf("%s", d.Error());181Report(182"ERROR: AddressSanitizer: reallocarray parameters overflow: count * size "183"(%zd * %zd) cannot be represented in type size_t (thread %s)\n",184count, size, AsanThreadIdAndName(tid).c_str());185Printf("%s", d.Default());186stack->Print();187PrintHintAllocatorCannotReturnNull();188ReportErrorSummary(scariness.GetDescription(), stack);189}190191void ErrorPvallocOverflow::Print() {192Decorator d;193Printf("%s", d.Error());194Report(195"ERROR: AddressSanitizer: pvalloc parameters overflow: size 0x%zx "196"rounded up to system page size 0x%zx cannot be represented in type "197"size_t (thread %s)\n",198size, GetPageSizeCached(), AsanThreadIdAndName(tid).c_str());199Printf("%s", d.Default());200stack->Print();201PrintHintAllocatorCannotReturnNull();202ReportErrorSummary(scariness.GetDescription(), stack);203}204205void ErrorInvalidAllocationAlignment::Print() {206Decorator d;207Printf("%s", d.Error());208Report(209"ERROR: AddressSanitizer: invalid allocation alignment: %zd, "210"alignment must be a power of two (thread %s)\n",211alignment, AsanThreadIdAndName(tid).c_str());212Printf("%s", d.Default());213stack->Print();214PrintHintAllocatorCannotReturnNull();215ReportErrorSummary(scariness.GetDescription(), stack);216}217218void ErrorInvalidAlignedAllocAlignment::Print() {219Decorator d;220Printf("%s", d.Error());221#if SANITIZER_POSIX222Report("ERROR: AddressSanitizer: invalid alignment requested in "223"aligned_alloc: %zd, alignment must be a power of two and the "224"requested size 0x%zx must be a multiple of alignment "225"(thread %s)\n", alignment, size, AsanThreadIdAndName(tid).c_str());226#else227Report("ERROR: AddressSanitizer: invalid alignment requested in "228"aligned_alloc: %zd, the requested size 0x%zx must be a multiple of "229"alignment (thread %s)\n", alignment, size,230AsanThreadIdAndName(tid).c_str());231#endif232Printf("%s", d.Default());233stack->Print();234PrintHintAllocatorCannotReturnNull();235ReportErrorSummary(scariness.GetDescription(), stack);236}237238void ErrorInvalidPosixMemalignAlignment::Print() {239Decorator d;240Printf("%s", d.Error());241Report(242"ERROR: AddressSanitizer: invalid alignment requested in posix_memalign: "243"%zd, alignment must be a power of two and a multiple of sizeof(void*) "244"== %zd (thread %s)\n",245alignment, sizeof(void *), AsanThreadIdAndName(tid).c_str());246Printf("%s", d.Default());247stack->Print();248PrintHintAllocatorCannotReturnNull();249ReportErrorSummary(scariness.GetDescription(), stack);250}251252void ErrorAllocationSizeTooBig::Print() {253Decorator d;254Printf("%s", d.Error());255Report(256"ERROR: AddressSanitizer: requested allocation size 0x%zx (0x%zx after "257"adjustments for alignment, red zones etc.) exceeds maximum supported "258"size of 0x%zx (thread %s)\n",259user_size, total_size, max_size, AsanThreadIdAndName(tid).c_str());260Printf("%s", d.Default());261stack->Print();262PrintHintAllocatorCannotReturnNull();263ReportErrorSummary(scariness.GetDescription(), stack);264}265266void ErrorRssLimitExceeded::Print() {267Decorator d;268Printf("%s", d.Error());269Report(270"ERROR: AddressSanitizer: specified RSS limit exceeded, currently set to "271"soft_rss_limit_mb=%zd\n", common_flags()->soft_rss_limit_mb);272Printf("%s", d.Default());273stack->Print();274PrintHintAllocatorCannotReturnNull();275ReportErrorSummary(scariness.GetDescription(), stack);276}277278void ErrorOutOfMemory::Print() {279Decorator d;280Printf("%s", d.Error());281ERROR_OOM("allocator is trying to allocate 0x%zx bytes\n", requested_size);282Printf("%s", d.Default());283stack->Print();284PrintHintAllocatorCannotReturnNull();285ReportErrorSummary(scariness.GetDescription(), stack);286}287288void ErrorStringFunctionMemoryRangesOverlap::Print() {289Decorator d;290char bug_type[100];291internal_snprintf(bug_type, sizeof(bug_type), "%s-param-overlap", function);292Printf("%s", d.Error());293Report(294"ERROR: AddressSanitizer: %s: memory ranges [%p,%p) and [%p, %p) "295"overlap\n",296bug_type, (void *)addr1_description.Address(),297(void *)(addr1_description.Address() + length1),298(void *)addr2_description.Address(),299(void *)(addr2_description.Address() + length2));300Printf("%s", d.Default());301scariness.Print();302stack->Print();303addr1_description.Print();304addr2_description.Print();305ReportErrorSummary(bug_type, stack);306}307308void ErrorStringFunctionSizeOverflow::Print() {309Decorator d;310Printf("%s", d.Error());311Report("ERROR: AddressSanitizer: %s: (size=%zd)\n",312scariness.GetDescription(), size);313Printf("%s", d.Default());314scariness.Print();315stack->Print();316addr_description.Print();317ReportErrorSummary(scariness.GetDescription(), stack);318}319320void ErrorBadParamsToAnnotateContiguousContainer::Print() {321Report(322"ERROR: AddressSanitizer: bad parameters to "323"__sanitizer_annotate_contiguous_container:\n"324" beg : %p\n"325" end : %p\n"326" old_mid : %p\n"327" new_mid : %p\n",328(void *)beg, (void *)end, (void *)old_mid, (void *)new_mid);329uptr granularity = ASAN_SHADOW_GRANULARITY;330if (!IsAligned(beg, granularity))331Report("ERROR: beg is not aligned by %zu\n", granularity);332stack->Print();333ReportErrorSummary(scariness.GetDescription(), stack);334}335336void ErrorBadParamsToAnnotateDoubleEndedContiguousContainer::Print() {337Report(338"ERROR: AddressSanitizer: bad parameters to "339"__sanitizer_annotate_double_ended_contiguous_container:\n"340" storage_beg : %p\n"341" storage_end : %p\n"342" old_container_beg : %p\n"343" old_container_end : %p\n"344" new_container_beg : %p\n"345" new_container_end : %p\n",346(void *)storage_beg, (void *)storage_end, (void *)old_container_beg,347(void *)old_container_end, (void *)new_container_beg,348(void *)new_container_end);349uptr granularity = ASAN_SHADOW_GRANULARITY;350if (!IsAligned(storage_beg, granularity))351Report("ERROR: storage_beg is not aligned by %zu\n", granularity);352stack->Print();353ReportErrorSummary(scariness.GetDescription(), stack);354}355356void ErrorODRViolation::Print() {357Decorator d;358Printf("%s", d.Error());359Report("ERROR: AddressSanitizer: %s (%p):\n", scariness.GetDescription(),360(void *)global1.beg);361Printf("%s", d.Default());362InternalScopedString g1_loc;363InternalScopedString g2_loc;364PrintGlobalLocation(&g1_loc, global1, /*print_module_name=*/true);365PrintGlobalLocation(&g2_loc, global2, /*print_module_name=*/true);366Printf(" [1] size=%zd '%s' %s\n", global1.size,367MaybeDemangleGlobalName(global1.name), g1_loc.data());368Printf(" [2] size=%zd '%s' %s\n", global2.size,369MaybeDemangleGlobalName(global2.name), g2_loc.data());370if (stack_id1 && stack_id2) {371Printf("These globals were registered at these points:\n");372Printf(" [1]:\n");373StackDepotGet(stack_id1).Print();374Printf(" [2]:\n");375StackDepotGet(stack_id2).Print();376}377Report(378"HINT: if you don't care about these errors you may set "379"ASAN_OPTIONS=detect_odr_violation=0\n");380InternalScopedString error_msg;381error_msg.AppendF("%s: global '%s' at %s", scariness.GetDescription(),382MaybeDemangleGlobalName(global1.name), g1_loc.data());383ReportErrorSummary(error_msg.data());384}385386void ErrorInvalidPointerPair::Print() {387Decorator d;388Printf("%s", d.Error());389Report("ERROR: AddressSanitizer: %s: %p %p\n", scariness.GetDescription(),390(void *)addr1_description.Address(),391(void *)addr2_description.Address());392Printf("%s", d.Default());393GET_STACK_TRACE_FATAL(pc, bp);394stack.Print();395addr1_description.Print();396addr2_description.Print();397ReportErrorSummary(scariness.GetDescription(), &stack);398}399400static bool AdjacentShadowValuesAreFullyPoisoned(u8 *s) {401return s[-1] > 127 && s[1] > 127;402}403404ErrorGeneric::ErrorGeneric(u32 tid, uptr pc_, uptr bp_, uptr sp_, uptr addr,405bool is_write_, uptr access_size_)406: ErrorBase(tid),407addr_description(addr, access_size_, /*shouldLockThreadRegistry=*/false),408pc(pc_),409bp(bp_),410sp(sp_),411access_size(access_size_),412is_write(is_write_),413shadow_val(0) {414scariness.Clear();415if (access_size) {416if (access_size <= 9) {417char desr[] = "?-byte";418desr[0] = '0' + access_size;419scariness.Scare(access_size + access_size / 2, desr);420} else if (access_size >= 10) {421scariness.Scare(15, "multi-byte");422}423is_write ? scariness.Scare(20, "write") : scariness.Scare(1, "read");424425// Determine the error type.426bug_descr = "unknown-crash";427if (AddrIsInMem(addr)) {428u8 *shadow_addr = (u8 *)MemToShadow(addr);429// If we are accessing 16 bytes, look at the second shadow byte.430if (*shadow_addr == 0 && access_size > ASAN_SHADOW_GRANULARITY)431shadow_addr++;432// If we are in the partial right redzone, look at the next shadow byte.433if (*shadow_addr > 0 && *shadow_addr < 128) shadow_addr++;434bool far_from_bounds = false;435shadow_val = *shadow_addr;436int bug_type_score = 0;437// For use-after-frees reads are almost as bad as writes.438int read_after_free_bonus = 0;439switch (shadow_val) {440case kAsanHeapLeftRedzoneMagic:441case kAsanArrayCookieMagic:442bug_descr = "heap-buffer-overflow";443bug_type_score = 10;444far_from_bounds = AdjacentShadowValuesAreFullyPoisoned(shadow_addr);445break;446case kAsanHeapFreeMagic:447bug_descr = "heap-use-after-free";448bug_type_score = 20;449if (!is_write) read_after_free_bonus = 18;450break;451case kAsanStackLeftRedzoneMagic:452bug_descr = "stack-buffer-underflow";453bug_type_score = 25;454far_from_bounds = AdjacentShadowValuesAreFullyPoisoned(shadow_addr);455break;456case kAsanInitializationOrderMagic:457bug_descr = "initialization-order-fiasco";458bug_type_score = 1;459break;460case kAsanStackMidRedzoneMagic:461case kAsanStackRightRedzoneMagic:462bug_descr = "stack-buffer-overflow";463bug_type_score = 25;464far_from_bounds = AdjacentShadowValuesAreFullyPoisoned(shadow_addr);465break;466case kAsanStackAfterReturnMagic:467bug_descr = "stack-use-after-return";468bug_type_score = 30;469if (!is_write) read_after_free_bonus = 18;470break;471case kAsanUserPoisonedMemoryMagic:472bug_descr = "use-after-poison";473bug_type_score = 20;474break;475case kAsanContiguousContainerOOBMagic:476bug_descr = "container-overflow";477bug_type_score = 10;478break;479case kAsanStackUseAfterScopeMagic:480bug_descr = "stack-use-after-scope";481bug_type_score = 10;482break;483case kAsanGlobalRedzoneMagic:484bug_descr = "global-buffer-overflow";485bug_type_score = 10;486far_from_bounds = AdjacentShadowValuesAreFullyPoisoned(shadow_addr);487break;488case kAsanIntraObjectRedzone:489bug_descr = "intra-object-overflow";490bug_type_score = 10;491break;492case kAsanAllocaLeftMagic:493case kAsanAllocaRightMagic:494bug_descr = "dynamic-stack-buffer-overflow";495bug_type_score = 25;496far_from_bounds = AdjacentShadowValuesAreFullyPoisoned(shadow_addr);497break;498}499scariness.Scare(bug_type_score + read_after_free_bonus, bug_descr);500if (far_from_bounds) scariness.Scare(10, "far-from-bounds");501}502}503}504505static void PrintContainerOverflowHint() {506Printf("HINT: if you don't care about these errors you may set "507"ASAN_OPTIONS=detect_container_overflow=0.\n"508"If you suspect a false positive see also: "509"https://github.com/google/sanitizers/wiki/"510"AddressSanitizerContainerOverflow.\n");511}512513static void PrintShadowByte(InternalScopedString *str, const char *before,514u8 byte, const char *after = "\n") {515PrintMemoryByte(str, before, byte, /*in_shadow*/true, after);516}517518static void PrintLegend(InternalScopedString *str) {519str->AppendF(520"Shadow byte legend (one shadow byte represents %d "521"application bytes):\n",522(int)ASAN_SHADOW_GRANULARITY);523PrintShadowByte(str, " Addressable: ", 0);524str->AppendF(" Partially addressable: ");525for (u8 i = 1; i < ASAN_SHADOW_GRANULARITY; i++)526PrintShadowByte(str, "", i, " ");527str->AppendF("\n");528PrintShadowByte(str, " Heap left redzone: ",529kAsanHeapLeftRedzoneMagic);530PrintShadowByte(str, " Freed heap region: ", kAsanHeapFreeMagic);531PrintShadowByte(str, " Stack left redzone: ",532kAsanStackLeftRedzoneMagic);533PrintShadowByte(str, " Stack mid redzone: ",534kAsanStackMidRedzoneMagic);535PrintShadowByte(str, " Stack right redzone: ",536kAsanStackRightRedzoneMagic);537PrintShadowByte(str, " Stack after return: ",538kAsanStackAfterReturnMagic);539PrintShadowByte(str, " Stack use after scope: ",540kAsanStackUseAfterScopeMagic);541PrintShadowByte(str, " Global redzone: ", kAsanGlobalRedzoneMagic);542PrintShadowByte(str, " Global init order: ",543kAsanInitializationOrderMagic);544PrintShadowByte(str, " Poisoned by user: ",545kAsanUserPoisonedMemoryMagic);546PrintShadowByte(str, " Container overflow: ",547kAsanContiguousContainerOOBMagic);548PrintShadowByte(str, " Array cookie: ",549kAsanArrayCookieMagic);550PrintShadowByte(str, " Intra object redzone: ",551kAsanIntraObjectRedzone);552PrintShadowByte(str, " ASan internal: ", kAsanInternalHeapMagic);553PrintShadowByte(str, " Left alloca redzone: ", kAsanAllocaLeftMagic);554PrintShadowByte(str, " Right alloca redzone: ", kAsanAllocaRightMagic);555}556557static void PrintShadowBytes(InternalScopedString *str, const char *before,558u8 *bytes, u8 *guilty, uptr n) {559Decorator d;560if (before)561str->AppendF("%s%p:", before,562(void *)ShadowToMem(reinterpret_cast<uptr>(bytes)));563for (uptr i = 0; i < n; i++) {564u8 *p = bytes + i;565const char *before =566p == guilty ? "[" : (p - 1 == guilty && i != 0) ? "" : " ";567const char *after = p == guilty ? "]" : "";568PrintShadowByte(str, before, *p, after);569}570str->AppendF("\n");571}572573static void PrintShadowMemoryForAddress(uptr addr) {574if (!AddrIsInMem(addr)) return;575uptr shadow_addr = MemToShadow(addr);576const uptr n_bytes_per_row = 16;577uptr aligned_shadow = shadow_addr & ~(n_bytes_per_row - 1);578InternalScopedString str;579str.AppendF("Shadow bytes around the buggy address:\n");580for (int i = -5; i <= 5; i++) {581uptr row_shadow_addr = aligned_shadow + i * n_bytes_per_row;582// Skip rows that would be outside the shadow range. This can happen when583// the user address is near the bottom, top, or shadow gap of the address584// space.585if (!AddrIsInShadow(row_shadow_addr)) continue;586const char *prefix = (i == 0) ? "=>" : " ";587PrintShadowBytes(&str, prefix, (u8 *)row_shadow_addr, (u8 *)shadow_addr,588n_bytes_per_row);589}590if (flags()->print_legend) PrintLegend(&str);591Printf("%s", str.data());592}593594void ErrorGeneric::Print() {595Decorator d;596Printf("%s", d.Error());597uptr addr = addr_description.Address();598Report("ERROR: AddressSanitizer: %s on address %p at pc %p bp %p sp %p\n",599bug_descr, (void *)addr, (void *)pc, (void *)bp, (void *)sp);600Printf("%s", d.Default());601602Printf("%s%s of size %zu at %p thread %s%s\n", d.Access(),603access_size ? (is_write ? "WRITE" : "READ") : "ACCESS", access_size,604(void *)addr, AsanThreadIdAndName(tid).c_str(), d.Default());605606scariness.Print();607GET_STACK_TRACE_FATAL(pc, bp);608stack.Print();609610// Pass bug_descr because we have a special case for611// initialization-order-fiasco612addr_description.Print(bug_descr);613if (shadow_val == kAsanContiguousContainerOOBMagic)614PrintContainerOverflowHint();615ReportErrorSummary(bug_descr, &stack);616PrintShadowMemoryForAddress(addr);617}618619} // namespace __asan620621622