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