blob: 6413b987b24c848ad8ae818397cd38538fb0a758 [file] [log] [blame]
Filipe Cabecinhasfddfdca2016-08-30 17:08:55 +00001//===-- asan_errors.cc ------------------------------------------*- C++ -*-===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This file is a part of AddressSanitizer, an address sanity checker.
11//
12// ASan implementation for error structures.
13//===----------------------------------------------------------------------===//
14
15#include "asan_errors.h"
Filipe Cabecinhas1989be72016-09-08 12:58:15 +000016#include <signal.h>
Filipe Cabecinhasb16672d2016-08-31 07:38:09 +000017#include "asan_descriptions.h"
Filipe Cabecinhasb50a5b32016-09-15 08:10:48 +000018#include "asan_mapping.h"
Filipe Cabecinhas1989be72016-09-08 12:58:15 +000019#include "asan_report.h"
Filipe Cabecinhasfddfdca2016-08-30 17:08:55 +000020#include "asan_stack.h"
Filipe Cabecinhas719db0c2016-09-15 08:10:52 +000021#include "sanitizer_common/sanitizer_stackdepot.h"
Filipe Cabecinhasfddfdca2016-08-30 17:08:55 +000022
23namespace __asan {
24
Vitaly Buka21ddc622017-09-14 22:44:03 +000025static void OnStackUnwind(const SignalContext &sig,
26 const void *callback_context,
27 BufferedStackTrace *stack) {
Vitaly Buka86dd0882017-09-18 06:56:57 +000028 bool fast = common_flags()->fast_unwind_on_fatal;
29#if SANITIZER_FREEBSD || SANITIZER_NETBSD
30 // On FreeBSD the slow unwinding that leverages _Unwind_Backtrace()
31 // yields the call stack of the signal's handler and not of the code
32 // that raised the signal (as it does on Linux).
33 fast = true;
34#endif
Vitaly Buka21ddc622017-09-14 22:44:03 +000035 // Tests and maybe some users expect that scariness is going to be printed
36 // just before the stack. As only asan has scariness score we have no
37 // corresponding code in the sanitizer_common and we use this callback to
38 // print it.
39 static_cast<const ScarinessScoreBase *>(callback_context)->Print();
40 GetStackTraceWithPcBpAndContext(stack, kStackTraceMax, sig.pc, sig.bp,
Vitaly Buka86dd0882017-09-18 06:56:57 +000041 sig.context, fast);
Vitaly Buka21ddc622017-09-14 22:44:03 +000042}
43
Filipe Cabecinhas1989be72016-09-08 12:58:15 +000044void ErrorDeadlySignal::Print() {
Vitaly Buka21ddc622017-09-14 22:44:03 +000045 ReportDeadlySignal(signal, tid, &OnStackUnwind, &scariness);
Filipe Cabecinhas1989be72016-09-08 12:58:15 +000046}
47
Filipe Cabecinhasb16672d2016-08-31 07:38:09 +000048void ErrorDoubleFree::Print() {
49 Decorator d;
50 Printf("%s", d.Warning());
51 char tname[128];
52 Report(
Kuba Mracek48090f52016-11-28 21:18:15 +000053 "ERROR: AddressSanitizer: attempting %s on %p in "
Filipe Cabecinhasb16672d2016-08-31 07:38:09 +000054 "thread T%d%s:\n",
Kuba Mracek48090f52016-11-28 21:18:15 +000055 scariness.GetDescription(), addr_description.addr, tid,
Filipe Cabecinhasb16672d2016-08-31 07:38:09 +000056 ThreadNameWithParenthesis(tid, tname, sizeof(tname)));
Vitaly Buka36266b62017-09-11 20:55:49 +000057 Printf("%s", d.Default());
Filipe Cabecinhas453b5552016-08-31 09:39:47 +000058 scariness.Print();
Filipe Cabecinhasb16672d2016-08-31 07:38:09 +000059 GET_STACK_TRACE_FATAL(second_free_stack->trace[0],
60 second_free_stack->top_frame_bp);
61 stack.Print();
62 addr_description.Print();
Kuba Mracek48090f52016-11-28 21:18:15 +000063 ReportErrorSummary(scariness.GetDescription(), &stack);
Filipe Cabecinhasb16672d2016-08-31 07:38:09 +000064}
65
Alex Shlyapnikova53b55f2017-10-25 17:21:37 +000066void ErrorNewDeleteTypeMismatch::Print() {
Filipe Cabecinhas25ad7b52016-09-07 14:20:54 +000067 Decorator d;
68 Printf("%s", d.Warning());
69 char tname[128];
70 Report(
Kuba Mracek48090f52016-11-28 21:18:15 +000071 "ERROR: AddressSanitizer: %s on %p in thread "
Filipe Cabecinhas25ad7b52016-09-07 14:20:54 +000072 "T%d%s:\n",
Kuba Mracek48090f52016-11-28 21:18:15 +000073 scariness.GetDescription(), addr_description.addr, tid,
Filipe Cabecinhas25ad7b52016-09-07 14:20:54 +000074 ThreadNameWithParenthesis(tid, tname, sizeof(tname)));
Vitaly Buka36266b62017-09-11 20:55:49 +000075 Printf("%s object passed to delete has wrong type:\n", d.Default());
Alex Shlyapnikova53b55f2017-10-25 17:21:37 +000076 if (delete_size != 0) {
77 Printf(
78 " size of the allocated type: %zd bytes;\n"
79 " size of the deallocated type: %zd bytes.\n",
80 addr_description.chunk_access.chunk_size, delete_size);
81 }
82 const uptr user_alignment =
83 addr_description.chunk_access.user_requested_alignment;
84 if (delete_alignment != user_alignment) {
85 char user_alignment_str[32];
86 char delete_alignment_str[32];
87 internal_snprintf(user_alignment_str, sizeof(user_alignment_str),
88 "%zd bytes", user_alignment);
89 internal_snprintf(delete_alignment_str, sizeof(delete_alignment_str),
90 "%zd bytes", delete_alignment);
91 static const char *kDefaultAlignment = "default-aligned";
92 Printf(
93 " alignment of the allocated type: %s;\n"
94 " alignment of the deallocated type: %s.\n",
95 user_alignment > 0 ? user_alignment_str : kDefaultAlignment,
96 delete_alignment > 0 ? delete_alignment_str : kDefaultAlignment);
97 }
Filipe Cabecinhas25ad7b52016-09-07 14:20:54 +000098 CHECK_GT(free_stack->size, 0);
99 scariness.Print();
100 GET_STACK_TRACE_FATAL(free_stack->trace[0], free_stack->top_frame_bp);
101 stack.Print();
102 addr_description.Print();
Kuba Mracek48090f52016-11-28 21:18:15 +0000103 ReportErrorSummary(scariness.GetDescription(), &stack);
Filipe Cabecinhas25ad7b52016-09-07 14:20:54 +0000104 Report(
105 "HINT: if you don't care about these errors you may set "
106 "ASAN_OPTIONS=new_delete_type_mismatch=0\n");
107}
108
Filipe Cabecinhas6fb54622016-09-13 20:47:29 +0000109void ErrorFreeNotMalloced::Print() {
110 Decorator d;
111 Printf("%s", d.Warning());
112 char tname[128];
113 Report(
114 "ERROR: AddressSanitizer: attempting free on address "
115 "which was not malloc()-ed: %p in thread T%d%s\n",
116 addr_description.Address(), tid,
117 ThreadNameWithParenthesis(tid, tname, sizeof(tname)));
Vitaly Buka36266b62017-09-11 20:55:49 +0000118 Printf("%s", d.Default());
Filipe Cabecinhas6fb54622016-09-13 20:47:29 +0000119 CHECK_GT(free_stack->size, 0);
120 scariness.Print();
121 GET_STACK_TRACE_FATAL(free_stack->trace[0], free_stack->top_frame_bp);
122 stack.Print();
123 addr_description.Print();
Kuba Mracek48090f52016-11-28 21:18:15 +0000124 ReportErrorSummary(scariness.GetDescription(), &stack);
Filipe Cabecinhas6fb54622016-09-13 20:47:29 +0000125}
126
Filipe Cabecinhas92c5b5d2016-09-13 20:47:33 +0000127void ErrorAllocTypeMismatch::Print() {
128 static const char *alloc_names[] = {"INVALID", "malloc", "operator new",
129 "operator new []"};
130 static const char *dealloc_names[] = {"INVALID", "free", "operator delete",
131 "operator delete []"};
132 CHECK_NE(alloc_type, dealloc_type);
133 Decorator d;
134 Printf("%s", d.Warning());
Kuba Mracek48090f52016-11-28 21:18:15 +0000135 Report("ERROR: AddressSanitizer: %s (%s vs %s) on %p\n",
136 scariness.GetDescription(),
Filipe Cabecinhas92c5b5d2016-09-13 20:47:33 +0000137 alloc_names[alloc_type], dealloc_names[dealloc_type],
138 addr_description.addr);
Vitaly Buka36266b62017-09-11 20:55:49 +0000139 Printf("%s", d.Default());
Filipe Cabecinhas92c5b5d2016-09-13 20:47:33 +0000140 CHECK_GT(dealloc_stack->size, 0);
141 scariness.Print();
142 GET_STACK_TRACE_FATAL(dealloc_stack->trace[0], dealloc_stack->top_frame_bp);
143 stack.Print();
144 addr_description.Print();
Kuba Mracek48090f52016-11-28 21:18:15 +0000145 ReportErrorSummary(scariness.GetDescription(), &stack);
Filipe Cabecinhas92c5b5d2016-09-13 20:47:33 +0000146 Report(
147 "HINT: if you don't care about these errors you may set "
148 "ASAN_OPTIONS=alloc_dealloc_mismatch=0\n");
149}
150
Filipe Cabecinhas5f862c22016-09-13 20:47:37 +0000151void ErrorMallocUsableSizeNotOwned::Print() {
152 Decorator d;
153 Printf("%s", d.Warning());
154 Report(
155 "ERROR: AddressSanitizer: attempting to call malloc_usable_size() for "
156 "pointer which is not owned: %p\n",
157 addr_description.Address());
Vitaly Buka36266b62017-09-11 20:55:49 +0000158 Printf("%s", d.Default());
Filipe Cabecinhas5f862c22016-09-13 20:47:37 +0000159 stack->Print();
160 addr_description.Print();
Kuba Mracek48090f52016-11-28 21:18:15 +0000161 ReportErrorSummary(scariness.GetDescription(), stack);
Filipe Cabecinhas5f862c22016-09-13 20:47:37 +0000162}
163
Filipe Cabecinhasb0de43a2016-09-13 20:47:42 +0000164void ErrorSanitizerGetAllocatedSizeNotOwned::Print() {
165 Decorator d;
166 Printf("%s", d.Warning());
167 Report(
168 "ERROR: AddressSanitizer: attempting to call "
169 "__sanitizer_get_allocated_size() for pointer which is not owned: %p\n",
170 addr_description.Address());
Vitaly Buka36266b62017-09-11 20:55:49 +0000171 Printf("%s", d.Default());
Filipe Cabecinhasb0de43a2016-09-13 20:47:42 +0000172 stack->Print();
173 addr_description.Print();
Kuba Mracek48090f52016-11-28 21:18:15 +0000174 ReportErrorSummary(scariness.GetDescription(), stack);
Filipe Cabecinhasb0de43a2016-09-13 20:47:42 +0000175}
176
Filipe Cabecinhas7a196b92016-09-14 07:37:14 +0000177void ErrorStringFunctionMemoryRangesOverlap::Print() {
178 Decorator d;
179 char bug_type[100];
180 internal_snprintf(bug_type, sizeof(bug_type), "%s-param-overlap", function);
181 Printf("%s", d.Warning());
182 Report(
183 "ERROR: AddressSanitizer: %s: memory ranges [%p,%p) and [%p, %p) "
184 "overlap\n",
185 bug_type, addr1_description.Address(),
186 addr1_description.Address() + length1, addr2_description.Address(),
187 addr2_description.Address() + length2);
Vitaly Buka36266b62017-09-11 20:55:49 +0000188 Printf("%s", d.Default());
Filipe Cabecinhas7a196b92016-09-14 07:37:14 +0000189 scariness.Print();
190 stack->Print();
191 addr1_description.Print();
192 addr2_description.Print();
193 ReportErrorSummary(bug_type, stack);
194}
195
Filipe Cabecinhas36229e92016-09-14 07:37:20 +0000196void ErrorStringFunctionSizeOverflow::Print() {
197 Decorator d;
198 Printf("%s", d.Warning());
Kuba Mracek48090f52016-11-28 21:18:15 +0000199 Report("ERROR: AddressSanitizer: %s: (size=%zd)\n",
200 scariness.GetDescription(), size);
Vitaly Buka36266b62017-09-11 20:55:49 +0000201 Printf("%s", d.Default());
Filipe Cabecinhas36229e92016-09-14 07:37:20 +0000202 scariness.Print();
203 stack->Print();
204 addr_description.Print();
Kuba Mracek48090f52016-11-28 21:18:15 +0000205 ReportErrorSummary(scariness.GetDescription(), stack);
Filipe Cabecinhas36229e92016-09-14 07:37:20 +0000206}
207
Filipe Cabecinhasb50a5b32016-09-15 08:10:48 +0000208void ErrorBadParamsToAnnotateContiguousContainer::Print() {
209 Report(
210 "ERROR: AddressSanitizer: bad parameters to "
211 "__sanitizer_annotate_contiguous_container:\n"
212 " beg : %p\n"
213 " end : %p\n"
214 " old_mid : %p\n"
215 " new_mid : %p\n",
216 beg, end, old_mid, new_mid);
217 uptr granularity = SHADOW_GRANULARITY;
218 if (!IsAligned(beg, granularity))
219 Report("ERROR: beg is not aligned by %d\n", granularity);
220 stack->Print();
Kuba Mracek48090f52016-11-28 21:18:15 +0000221 ReportErrorSummary(scariness.GetDescription(), stack);
Filipe Cabecinhasb50a5b32016-09-15 08:10:48 +0000222}
223
Filipe Cabecinhas719db0c2016-09-15 08:10:52 +0000224void ErrorODRViolation::Print() {
225 Decorator d;
226 Printf("%s", d.Warning());
Kuba Mracek48090f52016-11-28 21:18:15 +0000227 Report("ERROR: AddressSanitizer: %s (%p):\n", scariness.GetDescription(),
228 global1.beg);
Vitaly Buka36266b62017-09-11 20:55:49 +0000229 Printf("%s", d.Default());
Filipe Cabecinhas719db0c2016-09-15 08:10:52 +0000230 InternalScopedString g1_loc(256), g2_loc(256);
231 PrintGlobalLocation(&g1_loc, global1);
232 PrintGlobalLocation(&g2_loc, global2);
233 Printf(" [1] size=%zd '%s' %s\n", global1.size,
234 MaybeDemangleGlobalName(global1.name), g1_loc.data());
235 Printf(" [2] size=%zd '%s' %s\n", global2.size,
236 MaybeDemangleGlobalName(global2.name), g2_loc.data());
237 if (stack_id1 && stack_id2) {
238 Printf("These globals were registered at these points:\n");
239 Printf(" [1]:\n");
240 StackDepotGet(stack_id1).Print();
241 Printf(" [2]:\n");
242 StackDepotGet(stack_id2).Print();
243 }
244 Report(
245 "HINT: if you don't care about these errors you may set "
246 "ASAN_OPTIONS=detect_odr_violation=0\n");
247 InternalScopedString error_msg(256);
Kuba Mracek48090f52016-11-28 21:18:15 +0000248 error_msg.append("%s: global '%s' at %s", scariness.GetDescription(),
Filipe Cabecinhas719db0c2016-09-15 08:10:52 +0000249 MaybeDemangleGlobalName(global1.name), g1_loc.data());
250 ReportErrorSummary(error_msg.data());
251}
252
Filipe Cabecinhas1b3742e2016-09-15 08:10:56 +0000253void ErrorInvalidPointerPair::Print() {
Filipe Cabecinhas1b3742e2016-09-15 08:10:56 +0000254 Decorator d;
255 Printf("%s", d.Warning());
Kuba Mracek48090f52016-11-28 21:18:15 +0000256 Report("ERROR: AddressSanitizer: %s: %p %p\n", scariness.GetDescription(),
Filipe Cabecinhas490f96c2016-09-21 19:21:01 +0000257 addr1_description.Address(), addr2_description.Address());
Vitaly Buka36266b62017-09-11 20:55:49 +0000258 Printf("%s", d.Default());
Filipe Cabecinhas1b3742e2016-09-15 08:10:56 +0000259 GET_STACK_TRACE_FATAL(pc, bp);
260 stack.Print();
Filipe Cabecinhas490f96c2016-09-21 19:21:01 +0000261 addr1_description.Print();
262 addr2_description.Print();
Kuba Mracek48090f52016-11-28 21:18:15 +0000263 ReportErrorSummary(scariness.GetDescription(), &stack);
Filipe Cabecinhas1b3742e2016-09-15 08:10:56 +0000264}
265
Filipe Cabecinhasa8b5f5e2016-09-21 20:18:18 +0000266static bool AdjacentShadowValuesAreFullyPoisoned(u8 *s) {
267 return s[-1] > 127 && s[1] > 127;
268}
269
270ErrorGeneric::ErrorGeneric(u32 tid, uptr pc_, uptr bp_, uptr sp_, uptr addr,
271 bool is_write_, uptr access_size_)
272 : ErrorBase(tid),
273 addr_description(addr, access_size_, /*shouldLockThreadRegistry=*/false),
274 pc(pc_),
275 bp(bp_),
276 sp(sp_),
277 access_size(access_size_),
278 is_write(is_write_),
279 shadow_val(0) {
280 scariness.Clear();
281 if (access_size) {
282 if (access_size <= 9) {
283 char desr[] = "?-byte";
284 desr[0] = '0' + access_size;
285 scariness.Scare(access_size + access_size / 2, desr);
286 } else if (access_size >= 10) {
287 scariness.Scare(15, "multi-byte");
288 }
289 is_write ? scariness.Scare(20, "write") : scariness.Scare(1, "read");
290
291 // Determine the error type.
292 bug_descr = "unknown-crash";
293 if (AddrIsInMem(addr)) {
294 u8 *shadow_addr = (u8 *)MemToShadow(addr);
295 // If we are accessing 16 bytes, look at the second shadow byte.
296 if (*shadow_addr == 0 && access_size > SHADOW_GRANULARITY) shadow_addr++;
297 // If we are in the partial right redzone, look at the next shadow byte.
298 if (*shadow_addr > 0 && *shadow_addr < 128) shadow_addr++;
299 bool far_from_bounds = false;
300 shadow_val = *shadow_addr;
301 int bug_type_score = 0;
302 // For use-after-frees reads are almost as bad as writes.
303 int read_after_free_bonus = 0;
304 switch (shadow_val) {
305 case kAsanHeapLeftRedzoneMagic:
306 case kAsanArrayCookieMagic:
307 bug_descr = "heap-buffer-overflow";
308 bug_type_score = 10;
309 far_from_bounds = AdjacentShadowValuesAreFullyPoisoned(shadow_addr);
310 break;
311 case kAsanHeapFreeMagic:
312 bug_descr = "heap-use-after-free";
313 bug_type_score = 20;
314 if (!is_write) read_after_free_bonus = 18;
315 break;
316 case kAsanStackLeftRedzoneMagic:
317 bug_descr = "stack-buffer-underflow";
318 bug_type_score = 25;
319 far_from_bounds = AdjacentShadowValuesAreFullyPoisoned(shadow_addr);
320 break;
321 case kAsanInitializationOrderMagic:
322 bug_descr = "initialization-order-fiasco";
323 bug_type_score = 1;
324 break;
325 case kAsanStackMidRedzoneMagic:
326 case kAsanStackRightRedzoneMagic:
327 bug_descr = "stack-buffer-overflow";
328 bug_type_score = 25;
329 far_from_bounds = AdjacentShadowValuesAreFullyPoisoned(shadow_addr);
330 break;
331 case kAsanStackAfterReturnMagic:
332 bug_descr = "stack-use-after-return";
333 bug_type_score = 30;
334 if (!is_write) read_after_free_bonus = 18;
335 break;
336 case kAsanUserPoisonedMemoryMagic:
337 bug_descr = "use-after-poison";
338 bug_type_score = 20;
339 break;
340 case kAsanContiguousContainerOOBMagic:
341 bug_descr = "container-overflow";
342 bug_type_score = 10;
343 break;
344 case kAsanStackUseAfterScopeMagic:
345 bug_descr = "stack-use-after-scope";
346 bug_type_score = 10;
347 break;
348 case kAsanGlobalRedzoneMagic:
349 bug_descr = "global-buffer-overflow";
350 bug_type_score = 10;
351 far_from_bounds = AdjacentShadowValuesAreFullyPoisoned(shadow_addr);
352 break;
353 case kAsanIntraObjectRedzone:
354 bug_descr = "intra-object-overflow";
355 bug_type_score = 10;
356 break;
357 case kAsanAllocaLeftMagic:
358 case kAsanAllocaRightMagic:
359 bug_descr = "dynamic-stack-buffer-overflow";
360 bug_type_score = 25;
361 far_from_bounds = AdjacentShadowValuesAreFullyPoisoned(shadow_addr);
362 break;
363 }
364 scariness.Scare(bug_type_score + read_after_free_bonus, bug_descr);
365 if (far_from_bounds) scariness.Scare(10, "far-from-bounds");
366 }
367 }
368}
369
370static void PrintContainerOverflowHint() {
371 Printf("HINT: if you don't care about these errors you may set "
372 "ASAN_OPTIONS=detect_container_overflow=0.\n"
373 "If you suspect a false positive see also: "
374 "https://github.com/google/sanitizers/wiki/"
375 "AddressSanitizerContainerOverflow.\n");
376}
377
378static void PrintShadowByte(InternalScopedString *str, const char *before,
379 u8 byte, const char *after = "\n") {
380 PrintMemoryByte(str, before, byte, /*in_shadow*/true, after);
381}
382
383static void PrintLegend(InternalScopedString *str) {
384 str->append(
385 "Shadow byte legend (one shadow byte represents %d "
386 "application bytes):\n",
387 (int)SHADOW_GRANULARITY);
388 PrintShadowByte(str, " Addressable: ", 0);
389 str->append(" Partially addressable: ");
390 for (u8 i = 1; i < SHADOW_GRANULARITY; i++) PrintShadowByte(str, "", i, " ");
391 str->append("\n");
392 PrintShadowByte(str, " Heap left redzone: ",
393 kAsanHeapLeftRedzoneMagic);
394 PrintShadowByte(str, " Freed heap region: ", kAsanHeapFreeMagic);
395 PrintShadowByte(str, " Stack left redzone: ",
396 kAsanStackLeftRedzoneMagic);
397 PrintShadowByte(str, " Stack mid redzone: ",
398 kAsanStackMidRedzoneMagic);
399 PrintShadowByte(str, " Stack right redzone: ",
400 kAsanStackRightRedzoneMagic);
401 PrintShadowByte(str, " Stack after return: ",
402 kAsanStackAfterReturnMagic);
403 PrintShadowByte(str, " Stack use after scope: ",
404 kAsanStackUseAfterScopeMagic);
405 PrintShadowByte(str, " Global redzone: ", kAsanGlobalRedzoneMagic);
406 PrintShadowByte(str, " Global init order: ",
407 kAsanInitializationOrderMagic);
408 PrintShadowByte(str, " Poisoned by user: ",
409 kAsanUserPoisonedMemoryMagic);
410 PrintShadowByte(str, " Container overflow: ",
411 kAsanContiguousContainerOOBMagic);
412 PrintShadowByte(str, " Array cookie: ",
413 kAsanArrayCookieMagic);
414 PrintShadowByte(str, " Intra object redzone: ",
415 kAsanIntraObjectRedzone);
416 PrintShadowByte(str, " ASan internal: ", kAsanInternalHeapMagic);
417 PrintShadowByte(str, " Left alloca redzone: ", kAsanAllocaLeftMagic);
418 PrintShadowByte(str, " Right alloca redzone: ", kAsanAllocaRightMagic);
419}
420
421static void PrintShadowBytes(InternalScopedString *str, const char *before,
422 u8 *bytes, u8 *guilty, uptr n) {
423 Decorator d;
424 if (before) str->append("%s%p:", before, bytes);
425 for (uptr i = 0; i < n; i++) {
426 u8 *p = bytes + i;
427 const char *before =
428 p == guilty ? "[" : (p - 1 == guilty && i != 0) ? "" : " ";
429 const char *after = p == guilty ? "]" : "";
430 PrintShadowByte(str, before, *p, after);
431 }
432 str->append("\n");
433}
434
435static void PrintShadowMemoryForAddress(uptr addr) {
436 if (!AddrIsInMem(addr)) return;
437 uptr shadow_addr = MemToShadow(addr);
438 const uptr n_bytes_per_row = 16;
439 uptr aligned_shadow = shadow_addr & ~(n_bytes_per_row - 1);
440 InternalScopedString str(4096 * 8);
441 str.append("Shadow bytes around the buggy address:\n");
442 for (int i = -5; i <= 5; i++) {
Reid Kleckner03d02a02017-10-25 16:54:12 +0000443 uptr row_shadow_addr = aligned_shadow + i * n_bytes_per_row;
444 // Skip rows that would be outside the shadow range. This can happen when
445 // the user address is near the bottom, top, or shadow gap of the address
446 // space.
447 if (!AddrIsInShadow(row_shadow_addr)) continue;
Filipe Cabecinhasa8b5f5e2016-09-21 20:18:18 +0000448 const char *prefix = (i == 0) ? "=>" : " ";
Reid Kleckner03d02a02017-10-25 16:54:12 +0000449 PrintShadowBytes(&str, prefix, (u8 *)row_shadow_addr, (u8 *)shadow_addr,
450 n_bytes_per_row);
Filipe Cabecinhasa8b5f5e2016-09-21 20:18:18 +0000451 }
452 if (flags()->print_legend) PrintLegend(&str);
453 Printf("%s", str.data());
454}
455
456void ErrorGeneric::Print() {
457 Decorator d;
458 Printf("%s", d.Warning());
459 uptr addr = addr_description.Address();
460 Report("ERROR: AddressSanitizer: %s on address %p at pc %p bp %p sp %p\n",
461 bug_descr, (void *)addr, pc, bp, sp);
Vitaly Buka36266b62017-09-11 20:55:49 +0000462 Printf("%s", d.Default());
Filipe Cabecinhasa8b5f5e2016-09-21 20:18:18 +0000463
464 char tname[128];
465 Printf("%s%s of size %zu at %p thread T%d%s%s\n", d.Access(),
466 access_size ? (is_write ? "WRITE" : "READ") : "ACCESS", access_size,
467 (void *)addr, tid,
Vitaly Buka36266b62017-09-11 20:55:49 +0000468 ThreadNameWithParenthesis(tid, tname, sizeof(tname)), d.Default());
Filipe Cabecinhasa8b5f5e2016-09-21 20:18:18 +0000469
470 scariness.Print();
471 GET_STACK_TRACE_FATAL(pc, bp);
472 stack.Print();
473
474 // Pass bug_descr because we have a special case for
475 // initialization-order-fiasco
476 addr_description.Print(bug_descr);
477 if (shadow_val == kAsanContiguousContainerOOBMagic)
478 PrintContainerOverflowHint();
479 ReportErrorSummary(bug_descr, &stack);
480 PrintShadowMemoryForAddress(addr);
481}
482
Filipe Cabecinhasfddfdca2016-08-30 17:08:55 +0000483} // namespace __asan