blob: a571a717ab5ddee866e7176e912943e1cf5c7658 [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
25void ErrorStackOverflow::Print() {
26 Decorator d;
27 Printf("%s", d.Warning());
28 Report(
29 "ERROR: AddressSanitizer: stack-overflow on address %p"
30 " (pc %p bp %p sp %p T%d)\n",
31 (void *)addr, (void *)pc, (void *)bp, (void *)sp, tid);
32 Printf("%s", d.EndWarning());
33 scariness.Print();
34 BufferedStackTrace stack;
35 GetStackTraceWithPcBpAndContext(&stack, kStackTraceMax, pc, bp, context,
36 common_flags()->fast_unwind_on_fatal);
37 stack.Print();
38 ReportErrorSummary("stack-overflow", &stack);
39}
40
Filipe Cabecinhas1989be72016-09-08 12:58:15 +000041static void MaybeDumpInstructionBytes(uptr pc) {
42 if (!flags()->dump_instruction_bytes || (pc < GetPageSizeCached())) return;
43 InternalScopedString str(1024);
44 str.append("First 16 instruction bytes at pc: ");
45 if (IsAccessibleMemoryRange(pc, 16)) {
46 for (int i = 0; i < 16; ++i) {
47 PrintMemoryByte(&str, "", ((u8 *)pc)[i], /*in_shadow*/ false, " ");
48 }
49 str.append("\n");
50 } else {
51 str.append("unaccessible\n");
52 }
53 Report("%s", str.data());
54}
55
56void ErrorDeadlySignal::Print() {
57 Decorator d;
58 Printf("%s", d.Warning());
59 const char *description = DescribeSignalOrException(signo);
60 Report(
61 "ERROR: AddressSanitizer: %s on unknown address %p (pc %p bp %p sp %p "
62 "T%d)\n",
63 description, (void *)addr, (void *)pc, (void *)bp, (void *)sp, tid);
64 Printf("%s", d.EndWarning());
65 if (pc < GetPageSizeCached()) Report("Hint: pc points to the zero page.\n");
66 if (is_memory_access) {
67 const char *access_type =
68 write_flag == SignalContext::WRITE
69 ? "WRITE"
70 : (write_flag == SignalContext::READ ? "READ" : "UNKNOWN");
71 Report("The signal is caused by a %s memory access.\n", access_type);
72 if (addr < GetPageSizeCached())
73 Report("Hint: address points to the zero page.\n");
74 }
75 scariness.Print();
76 BufferedStackTrace stack;
77 GetStackTraceWithPcBpAndContext(&stack, kStackTraceMax, pc, bp, context,
78 common_flags()->fast_unwind_on_fatal);
79 stack.Print();
80 MaybeDumpInstructionBytes(pc);
81 Printf("AddressSanitizer can not provide additional info.\n");
82 ReportErrorSummary(description, &stack);
83}
84
Filipe Cabecinhasb16672d2016-08-31 07:38:09 +000085void ErrorDoubleFree::Print() {
86 Decorator d;
87 Printf("%s", d.Warning());
88 char tname[128];
89 Report(
90 "ERROR: AddressSanitizer: attempting double-free on %p in "
91 "thread T%d%s:\n",
92 addr_description.addr, tid,
93 ThreadNameWithParenthesis(tid, tname, sizeof(tname)));
94 Printf("%s", d.EndWarning());
Filipe Cabecinhas453b5552016-08-31 09:39:47 +000095 scariness.Print();
Filipe Cabecinhasb16672d2016-08-31 07:38:09 +000096 GET_STACK_TRACE_FATAL(second_free_stack->trace[0],
97 second_free_stack->top_frame_bp);
98 stack.Print();
99 addr_description.Print();
100 ReportErrorSummary("double-free", &stack);
101}
102
Filipe Cabecinhas25ad7b52016-09-07 14:20:54 +0000103void ErrorNewDeleteSizeMismatch::Print() {
104 Decorator d;
105 Printf("%s", d.Warning());
106 char tname[128];
107 Report(
108 "ERROR: AddressSanitizer: new-delete-type-mismatch on %p in thread "
109 "T%d%s:\n",
110 addr_description.addr, tid,
111 ThreadNameWithParenthesis(tid, tname, sizeof(tname)));
112 Printf("%s object passed to delete has wrong type:\n", d.EndWarning());
113 Printf(
114 " size of the allocated type: %zd bytes;\n"
115 " size of the deallocated type: %zd bytes.\n",
116 addr_description.chunk_access.chunk_size, delete_size);
117 CHECK_GT(free_stack->size, 0);
118 scariness.Print();
119 GET_STACK_TRACE_FATAL(free_stack->trace[0], free_stack->top_frame_bp);
120 stack.Print();
121 addr_description.Print();
122 ReportErrorSummary("new-delete-type-mismatch", &stack);
123 Report(
124 "HINT: if you don't care about these errors you may set "
125 "ASAN_OPTIONS=new_delete_type_mismatch=0\n");
126}
127
Filipe Cabecinhas6fb54622016-09-13 20:47:29 +0000128void ErrorFreeNotMalloced::Print() {
129 Decorator d;
130 Printf("%s", d.Warning());
131 char tname[128];
132 Report(
133 "ERROR: AddressSanitizer: attempting free on address "
134 "which was not malloc()-ed: %p in thread T%d%s\n",
135 addr_description.Address(), tid,
136 ThreadNameWithParenthesis(tid, tname, sizeof(tname)));
137 Printf("%s", d.EndWarning());
138 CHECK_GT(free_stack->size, 0);
139 scariness.Print();
140 GET_STACK_TRACE_FATAL(free_stack->trace[0], free_stack->top_frame_bp);
141 stack.Print();
142 addr_description.Print();
143 ReportErrorSummary("bad-free", &stack);
144}
145
Filipe Cabecinhas92c5b5d2016-09-13 20:47:33 +0000146void ErrorAllocTypeMismatch::Print() {
147 static const char *alloc_names[] = {"INVALID", "malloc", "operator new",
148 "operator new []"};
149 static const char *dealloc_names[] = {"INVALID", "free", "operator delete",
150 "operator delete []"};
151 CHECK_NE(alloc_type, dealloc_type);
152 Decorator d;
153 Printf("%s", d.Warning());
154 Report("ERROR: AddressSanitizer: alloc-dealloc-mismatch (%s vs %s) on %p\n",
155 alloc_names[alloc_type], dealloc_names[dealloc_type],
156 addr_description.addr);
157 Printf("%s", d.EndWarning());
158 CHECK_GT(dealloc_stack->size, 0);
159 scariness.Print();
160 GET_STACK_TRACE_FATAL(dealloc_stack->trace[0], dealloc_stack->top_frame_bp);
161 stack.Print();
162 addr_description.Print();
163 ReportErrorSummary("alloc-dealloc-mismatch", &stack);
164 Report(
165 "HINT: if you don't care about these errors you may set "
166 "ASAN_OPTIONS=alloc_dealloc_mismatch=0\n");
167}
168
Filipe Cabecinhas5f862c22016-09-13 20:47:37 +0000169void ErrorMallocUsableSizeNotOwned::Print() {
170 Decorator d;
171 Printf("%s", d.Warning());
172 Report(
173 "ERROR: AddressSanitizer: attempting to call malloc_usable_size() for "
174 "pointer which is not owned: %p\n",
175 addr_description.Address());
176 Printf("%s", d.EndWarning());
177 stack->Print();
178 addr_description.Print();
179 ReportErrorSummary("bad-malloc_usable_size", stack);
180}
181
Filipe Cabecinhasb0de43a2016-09-13 20:47:42 +0000182void ErrorSanitizerGetAllocatedSizeNotOwned::Print() {
183 Decorator d;
184 Printf("%s", d.Warning());
185 Report(
186 "ERROR: AddressSanitizer: attempting to call "
187 "__sanitizer_get_allocated_size() for pointer which is not owned: %p\n",
188 addr_description.Address());
189 Printf("%s", d.EndWarning());
190 stack->Print();
191 addr_description.Print();
192 ReportErrorSummary("bad-__sanitizer_get_allocated_size", stack);
193}
194
Filipe Cabecinhas7a196b92016-09-14 07:37:14 +0000195void ErrorStringFunctionMemoryRangesOverlap::Print() {
196 Decorator d;
197 char bug_type[100];
198 internal_snprintf(bug_type, sizeof(bug_type), "%s-param-overlap", function);
199 Printf("%s", d.Warning());
200 Report(
201 "ERROR: AddressSanitizer: %s: memory ranges [%p,%p) and [%p, %p) "
202 "overlap\n",
203 bug_type, addr1_description.Address(),
204 addr1_description.Address() + length1, addr2_description.Address(),
205 addr2_description.Address() + length2);
206 Printf("%s", d.EndWarning());
207 scariness.Print();
208 stack->Print();
209 addr1_description.Print();
210 addr2_description.Print();
211 ReportErrorSummary(bug_type, stack);
212}
213
Filipe Cabecinhas36229e92016-09-14 07:37:20 +0000214void ErrorStringFunctionSizeOverflow::Print() {
215 Decorator d;
216 Printf("%s", d.Warning());
217 const char *bug_type = "negative-size-param";
218 Report("ERROR: AddressSanitizer: %s: (size=%zd)\n", bug_type, size);
219 Printf("%s", d.EndWarning());
220 scariness.Print();
221 stack->Print();
222 addr_description.Print();
223 ReportErrorSummary(bug_type, stack);
224}
225
Filipe Cabecinhasb50a5b32016-09-15 08:10:48 +0000226void ErrorBadParamsToAnnotateContiguousContainer::Print() {
227 Report(
228 "ERROR: AddressSanitizer: bad parameters to "
229 "__sanitizer_annotate_contiguous_container:\n"
230 " beg : %p\n"
231 " end : %p\n"
232 " old_mid : %p\n"
233 " new_mid : %p\n",
234 beg, end, old_mid, new_mid);
235 uptr granularity = SHADOW_GRANULARITY;
236 if (!IsAligned(beg, granularity))
237 Report("ERROR: beg is not aligned by %d\n", granularity);
238 stack->Print();
239 ReportErrorSummary("bad-__sanitizer_annotate_contiguous_container", stack);
240}
241
Filipe Cabecinhas719db0c2016-09-15 08:10:52 +0000242void ErrorODRViolation::Print() {
243 Decorator d;
244 Printf("%s", d.Warning());
245 Report("ERROR: AddressSanitizer: odr-violation (%p):\n", global1.beg);
246 Printf("%s", d.EndWarning());
247 InternalScopedString g1_loc(256), g2_loc(256);
248 PrintGlobalLocation(&g1_loc, global1);
249 PrintGlobalLocation(&g2_loc, global2);
250 Printf(" [1] size=%zd '%s' %s\n", global1.size,
251 MaybeDemangleGlobalName(global1.name), g1_loc.data());
252 Printf(" [2] size=%zd '%s' %s\n", global2.size,
253 MaybeDemangleGlobalName(global2.name), g2_loc.data());
254 if (stack_id1 && stack_id2) {
255 Printf("These globals were registered at these points:\n");
256 Printf(" [1]:\n");
257 StackDepotGet(stack_id1).Print();
258 Printf(" [2]:\n");
259 StackDepotGet(stack_id2).Print();
260 }
261 Report(
262 "HINT: if you don't care about these errors you may set "
263 "ASAN_OPTIONS=detect_odr_violation=0\n");
264 InternalScopedString error_msg(256);
265 error_msg.append("odr-violation: global '%s' at %s",
266 MaybeDemangleGlobalName(global1.name), g1_loc.data());
267 ReportErrorSummary(error_msg.data());
268}
269
Filipe Cabecinhas1b3742e2016-09-15 08:10:56 +0000270void ErrorInvalidPointerPair::Print() {
271 const char *bug_type = "invalid-pointer-pair";
272 Decorator d;
273 Printf("%s", d.Warning());
Filipe Cabecinhas490f96c2016-09-21 19:21:01 +0000274 Report("ERROR: AddressSanitizer: invalid-pointer-pair: %p %p\n",
275 addr1_description.Address(), addr2_description.Address());
Filipe Cabecinhas1b3742e2016-09-15 08:10:56 +0000276 Printf("%s", d.EndWarning());
277 GET_STACK_TRACE_FATAL(pc, bp);
278 stack.Print();
Filipe Cabecinhas490f96c2016-09-21 19:21:01 +0000279 addr1_description.Print();
280 addr2_description.Print();
Filipe Cabecinhas1b3742e2016-09-15 08:10:56 +0000281 ReportErrorSummary(bug_type, &stack);
282}
283
Filipe Cabecinhasa8b5f5e2016-09-21 20:18:18 +0000284static bool AdjacentShadowValuesAreFullyPoisoned(u8 *s) {
285 return s[-1] > 127 && s[1] > 127;
286}
287
288ErrorGeneric::ErrorGeneric(u32 tid, uptr pc_, uptr bp_, uptr sp_, uptr addr,
289 bool is_write_, uptr access_size_)
290 : ErrorBase(tid),
291 addr_description(addr, access_size_, /*shouldLockThreadRegistry=*/false),
292 pc(pc_),
293 bp(bp_),
294 sp(sp_),
295 access_size(access_size_),
296 is_write(is_write_),
297 shadow_val(0) {
298 scariness.Clear();
299 if (access_size) {
300 if (access_size <= 9) {
301 char desr[] = "?-byte";
302 desr[0] = '0' + access_size;
303 scariness.Scare(access_size + access_size / 2, desr);
304 } else if (access_size >= 10) {
305 scariness.Scare(15, "multi-byte");
306 }
307 is_write ? scariness.Scare(20, "write") : scariness.Scare(1, "read");
308
309 // Determine the error type.
310 bug_descr = "unknown-crash";
311 if (AddrIsInMem(addr)) {
312 u8 *shadow_addr = (u8 *)MemToShadow(addr);
313 // If we are accessing 16 bytes, look at the second shadow byte.
314 if (*shadow_addr == 0 && access_size > SHADOW_GRANULARITY) shadow_addr++;
315 // If we are in the partial right redzone, look at the next shadow byte.
316 if (*shadow_addr > 0 && *shadow_addr < 128) shadow_addr++;
317 bool far_from_bounds = false;
318 shadow_val = *shadow_addr;
319 int bug_type_score = 0;
320 // For use-after-frees reads are almost as bad as writes.
321 int read_after_free_bonus = 0;
322 switch (shadow_val) {
323 case kAsanHeapLeftRedzoneMagic:
324 case kAsanArrayCookieMagic:
325 bug_descr = "heap-buffer-overflow";
326 bug_type_score = 10;
327 far_from_bounds = AdjacentShadowValuesAreFullyPoisoned(shadow_addr);
328 break;
329 case kAsanHeapFreeMagic:
330 bug_descr = "heap-use-after-free";
331 bug_type_score = 20;
332 if (!is_write) read_after_free_bonus = 18;
333 break;
334 case kAsanStackLeftRedzoneMagic:
335 bug_descr = "stack-buffer-underflow";
336 bug_type_score = 25;
337 far_from_bounds = AdjacentShadowValuesAreFullyPoisoned(shadow_addr);
338 break;
339 case kAsanInitializationOrderMagic:
340 bug_descr = "initialization-order-fiasco";
341 bug_type_score = 1;
342 break;
343 case kAsanStackMidRedzoneMagic:
344 case kAsanStackRightRedzoneMagic:
345 bug_descr = "stack-buffer-overflow";
346 bug_type_score = 25;
347 far_from_bounds = AdjacentShadowValuesAreFullyPoisoned(shadow_addr);
348 break;
349 case kAsanStackAfterReturnMagic:
350 bug_descr = "stack-use-after-return";
351 bug_type_score = 30;
352 if (!is_write) read_after_free_bonus = 18;
353 break;
354 case kAsanUserPoisonedMemoryMagic:
355 bug_descr = "use-after-poison";
356 bug_type_score = 20;
357 break;
358 case kAsanContiguousContainerOOBMagic:
359 bug_descr = "container-overflow";
360 bug_type_score = 10;
361 break;
362 case kAsanStackUseAfterScopeMagic:
363 bug_descr = "stack-use-after-scope";
364 bug_type_score = 10;
365 break;
366 case kAsanGlobalRedzoneMagic:
367 bug_descr = "global-buffer-overflow";
368 bug_type_score = 10;
369 far_from_bounds = AdjacentShadowValuesAreFullyPoisoned(shadow_addr);
370 break;
371 case kAsanIntraObjectRedzone:
372 bug_descr = "intra-object-overflow";
373 bug_type_score = 10;
374 break;
375 case kAsanAllocaLeftMagic:
376 case kAsanAllocaRightMagic:
377 bug_descr = "dynamic-stack-buffer-overflow";
378 bug_type_score = 25;
379 far_from_bounds = AdjacentShadowValuesAreFullyPoisoned(shadow_addr);
380 break;
381 }
382 scariness.Scare(bug_type_score + read_after_free_bonus, bug_descr);
383 if (far_from_bounds) scariness.Scare(10, "far-from-bounds");
384 }
385 }
386}
387
388static void PrintContainerOverflowHint() {
389 Printf("HINT: if you don't care about these errors you may set "
390 "ASAN_OPTIONS=detect_container_overflow=0.\n"
391 "If you suspect a false positive see also: "
392 "https://github.com/google/sanitizers/wiki/"
393 "AddressSanitizerContainerOverflow.\n");
394}
395
396static void PrintShadowByte(InternalScopedString *str, const char *before,
397 u8 byte, const char *after = "\n") {
398 PrintMemoryByte(str, before, byte, /*in_shadow*/true, after);
399}
400
401static void PrintLegend(InternalScopedString *str) {
402 str->append(
403 "Shadow byte legend (one shadow byte represents %d "
404 "application bytes):\n",
405 (int)SHADOW_GRANULARITY);
406 PrintShadowByte(str, " Addressable: ", 0);
407 str->append(" Partially addressable: ");
408 for (u8 i = 1; i < SHADOW_GRANULARITY; i++) PrintShadowByte(str, "", i, " ");
409 str->append("\n");
410 PrintShadowByte(str, " Heap left redzone: ",
411 kAsanHeapLeftRedzoneMagic);
412 PrintShadowByte(str, " Freed heap region: ", kAsanHeapFreeMagic);
413 PrintShadowByte(str, " Stack left redzone: ",
414 kAsanStackLeftRedzoneMagic);
415 PrintShadowByte(str, " Stack mid redzone: ",
416 kAsanStackMidRedzoneMagic);
417 PrintShadowByte(str, " Stack right redzone: ",
418 kAsanStackRightRedzoneMagic);
419 PrintShadowByte(str, " Stack after return: ",
420 kAsanStackAfterReturnMagic);
421 PrintShadowByte(str, " Stack use after scope: ",
422 kAsanStackUseAfterScopeMagic);
423 PrintShadowByte(str, " Global redzone: ", kAsanGlobalRedzoneMagic);
424 PrintShadowByte(str, " Global init order: ",
425 kAsanInitializationOrderMagic);
426 PrintShadowByte(str, " Poisoned by user: ",
427 kAsanUserPoisonedMemoryMagic);
428 PrintShadowByte(str, " Container overflow: ",
429 kAsanContiguousContainerOOBMagic);
430 PrintShadowByte(str, " Array cookie: ",
431 kAsanArrayCookieMagic);
432 PrintShadowByte(str, " Intra object redzone: ",
433 kAsanIntraObjectRedzone);
434 PrintShadowByte(str, " ASan internal: ", kAsanInternalHeapMagic);
435 PrintShadowByte(str, " Left alloca redzone: ", kAsanAllocaLeftMagic);
436 PrintShadowByte(str, " Right alloca redzone: ", kAsanAllocaRightMagic);
437}
438
439static void PrintShadowBytes(InternalScopedString *str, const char *before,
440 u8 *bytes, u8 *guilty, uptr n) {
441 Decorator d;
442 if (before) str->append("%s%p:", before, bytes);
443 for (uptr i = 0; i < n; i++) {
444 u8 *p = bytes + i;
445 const char *before =
446 p == guilty ? "[" : (p - 1 == guilty && i != 0) ? "" : " ";
447 const char *after = p == guilty ? "]" : "";
448 PrintShadowByte(str, before, *p, after);
449 }
450 str->append("\n");
451}
452
453static void PrintShadowMemoryForAddress(uptr addr) {
454 if (!AddrIsInMem(addr)) return;
455 uptr shadow_addr = MemToShadow(addr);
456 const uptr n_bytes_per_row = 16;
457 uptr aligned_shadow = shadow_addr & ~(n_bytes_per_row - 1);
458 InternalScopedString str(4096 * 8);
459 str.append("Shadow bytes around the buggy address:\n");
460 for (int i = -5; i <= 5; i++) {
461 const char *prefix = (i == 0) ? "=>" : " ";
462 PrintShadowBytes(&str, prefix, (u8 *)(aligned_shadow + i * n_bytes_per_row),
463 (u8 *)shadow_addr, n_bytes_per_row);
464 }
465 if (flags()->print_legend) PrintLegend(&str);
466 Printf("%s", str.data());
467}
468
469void ErrorGeneric::Print() {
470 Decorator d;
471 Printf("%s", d.Warning());
472 uptr addr = addr_description.Address();
473 Report("ERROR: AddressSanitizer: %s on address %p at pc %p bp %p sp %p\n",
474 bug_descr, (void *)addr, pc, bp, sp);
475 Printf("%s", d.EndWarning());
476
477 char tname[128];
478 Printf("%s%s of size %zu at %p thread T%d%s%s\n", d.Access(),
479 access_size ? (is_write ? "WRITE" : "READ") : "ACCESS", access_size,
480 (void *)addr, tid,
481 ThreadNameWithParenthesis(tid, tname, sizeof(tname)), d.EndAccess());
482
483 scariness.Print();
484 GET_STACK_TRACE_FATAL(pc, bp);
485 stack.Print();
486
487 // Pass bug_descr because we have a special case for
488 // initialization-order-fiasco
489 addr_description.Print(bug_descr);
490 if (shadow_val == kAsanContiguousContainerOOBMagic)
491 PrintContainerOverflowHint();
492 ReportErrorSummary(bug_descr, &stack);
493 PrintShadowMemoryForAddress(addr);
494}
495
Filipe Cabecinhasfddfdca2016-08-30 17:08:55 +0000496} // namespace __asan