blob: 0706e61f27090fc9c8ba367da89c7b95d21585e0 [file] [log] [blame]
Alexey Samsonov73545092012-08-09 07:40:58 +00001//===-- asan_report.cc ----------------------------------------------------===//
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// This file contains error reporting code.
13//===----------------------------------------------------------------------===//
Alexey Samsonov98737922012-08-10 15:13:05 +000014#include "asan_flags.h"
Alexey Samsonov73545092012-08-09 07:40:58 +000015#include "asan_internal.h"
Alexey Samsonove218beb2012-08-09 09:06:52 +000016#include "asan_mapping.h"
Alexey Samsonov73545092012-08-09 07:40:58 +000017#include "asan_report.h"
18#include "asan_stack.h"
Alexey Samsonove4bfca22012-08-09 09:27:24 +000019#include "asan_thread.h"
Kostya Serebryany58f54552012-12-18 07:32:16 +000020#include "sanitizer_common/sanitizer_common.h"
Sergey Matveeved20ebe2013-05-06 11:27:58 +000021#include "sanitizer_common/sanitizer_flags.h"
Kostya Serebryany58f54552012-12-18 07:32:16 +000022#include "sanitizer_common/sanitizer_report_decorator.h"
Alexey Samsonov9c927482012-12-26 14:44:46 +000023#include "sanitizer_common/sanitizer_symbolizer.h"
Alexey Samsonov73545092012-08-09 07:40:58 +000024
25namespace __asan {
26
Alexey Samsonovf657a192012-08-13 11:23:40 +000027// -------------------- User-specified callbacks ----------------- {{{1
Alexey Samsonovc98570b2012-08-09 10:56:57 +000028static void (*error_report_callback)(const char*);
29static char *error_message_buffer = 0;
30static uptr error_message_buffer_pos = 0;
31static uptr error_message_buffer_size = 0;
32
33void AppendToErrorMessageBuffer(const char *buffer) {
34 if (error_message_buffer) {
35 uptr length = internal_strlen(buffer);
36 CHECK_GE(error_message_buffer_size, error_message_buffer_pos);
37 uptr remaining = error_message_buffer_size - error_message_buffer_pos;
38 internal_strncpy(error_message_buffer + error_message_buffer_pos,
39 buffer, remaining);
40 error_message_buffer[error_message_buffer_size - 1] = '\0';
41 // FIXME: reallocate the buffer instead of truncating the message.
42 error_message_buffer_pos += remaining > length ? length : remaining;
43 }
44}
45
Kostya Serebryany58f54552012-12-18 07:32:16 +000046// ---------------------- Decorator ------------------------------ {{{1
47class Decorator: private __sanitizer::AnsiColorDecorator {
48 public:
Kostya Serebryany9514a532012-12-19 09:53:32 +000049 Decorator() : __sanitizer::AnsiColorDecorator(PrintsToTtyCached()) { }
Kostya Serebryany58f54552012-12-18 07:32:16 +000050 const char *Warning() { return Red(); }
51 const char *EndWarning() { return Default(); }
52 const char *Access() { return Blue(); }
53 const char *EndAccess() { return Default(); }
54 const char *Location() { return Green(); }
55 const char *EndLocation() { return Default(); }
56 const char *Allocation() { return Magenta(); }
57 const char *EndAllocation() { return Default(); }
Kostya Serebryany9514a532012-12-19 09:53:32 +000058
59 const char *ShadowByte(u8 byte) {
60 switch (byte) {
61 case kAsanHeapLeftRedzoneMagic:
62 case kAsanHeapRightRedzoneMagic:
63 return Red();
64 case kAsanHeapFreeMagic:
65 return Magenta();
66 case kAsanStackLeftRedzoneMagic:
67 case kAsanStackMidRedzoneMagic:
68 case kAsanStackRightRedzoneMagic:
69 case kAsanStackPartialRedzoneMagic:
70 return Red();
71 case kAsanStackAfterReturnMagic:
72 return Magenta();
73 case kAsanInitializationOrderMagic:
74 return Cyan();
75 case kAsanUserPoisonedMemoryMagic:
76 return Blue();
77 case kAsanStackUseAfterScopeMagic:
78 return Magenta();
79 case kAsanGlobalRedzoneMagic:
80 return Red();
81 case kAsanInternalHeapMagic:
82 return Yellow();
83 default:
84 return Default();
85 }
86 }
87 const char *EndShadowByte() { return Default(); }
Kostya Serebryany58f54552012-12-18 07:32:16 +000088};
89
Alexey Samsonov98737922012-08-10 15:13:05 +000090// ---------------------- Helper functions ----------------------- {{{1
91
Kostya Serebryany9514a532012-12-19 09:53:32 +000092static void PrintShadowByte(const char *before, u8 byte,
93 const char *after = "\n") {
94 Decorator d;
95 Printf("%s%s%x%x%s%s", before,
96 d.ShadowByte(byte), byte >> 4, byte & 15, d.EndShadowByte(), after);
97}
98
99static void PrintShadowBytes(const char *before, u8 *bytes,
100 u8 *guilty, uptr n) {
101 Decorator d;
102 if (before)
103 Printf("%s%p:", before, bytes);
104 for (uptr i = 0; i < n; i++) {
105 u8 *p = bytes + i;
106 const char *before = p == guilty ? "[" :
Kostya Serebryany696902a2013-09-03 09:44:56 +0000107 (p - 1 == guilty && i != 0) ? "" : " ";
Kostya Serebryany9514a532012-12-19 09:53:32 +0000108 const char *after = p == guilty ? "]" : "";
109 PrintShadowByte(before, *p, after);
Alexey Samsonov98737922012-08-10 15:13:05 +0000110 }
Kostya Serebryany283c2962012-08-28 11:34:40 +0000111 Printf("\n");
Alexey Samsonov98737922012-08-10 15:13:05 +0000112}
113
Kostya Serebryany95f630a2013-01-28 07:34:22 +0000114static void PrintLegend() {
Kostya Serebryany9514a532012-12-19 09:53:32 +0000115 Printf("Shadow byte legend (one shadow byte represents %d "
116 "application bytes):\n", (int)SHADOW_GRANULARITY);
117 PrintShadowByte(" Addressable: ", 0);
118 Printf(" Partially addressable: ");
Timur Iskhodzhanov5e97ba32013-05-29 14:11:44 +0000119 for (u8 i = 1; i < SHADOW_GRANULARITY; i++)
Kostya Serebryany9514a532012-12-19 09:53:32 +0000120 PrintShadowByte("", i, " ");
121 Printf("\n");
122 PrintShadowByte(" Heap left redzone: ", kAsanHeapLeftRedzoneMagic);
Alexey Samsonovf3f2f5c2013-04-10 07:00:25 +0000123 PrintShadowByte(" Heap right redzone: ", kAsanHeapRightRedzoneMagic);
124 PrintShadowByte(" Freed heap region: ", kAsanHeapFreeMagic);
Kostya Serebryany9514a532012-12-19 09:53:32 +0000125 PrintShadowByte(" Stack left redzone: ", kAsanStackLeftRedzoneMagic);
126 PrintShadowByte(" Stack mid redzone: ", kAsanStackMidRedzoneMagic);
127 PrintShadowByte(" Stack right redzone: ", kAsanStackRightRedzoneMagic);
128 PrintShadowByte(" Stack partial redzone: ", kAsanStackPartialRedzoneMagic);
Kostya Serebryany9514a532012-12-19 09:53:32 +0000129 PrintShadowByte(" Stack after return: ", kAsanStackAfterReturnMagic);
130 PrintShadowByte(" Stack use after scope: ", kAsanStackUseAfterScopeMagic);
131 PrintShadowByte(" Global redzone: ", kAsanGlobalRedzoneMagic);
132 PrintShadowByte(" Global init order: ", kAsanInitializationOrderMagic);
133 PrintShadowByte(" Poisoned by user: ", kAsanUserPoisonedMemoryMagic);
134 PrintShadowByte(" ASan internal: ", kAsanInternalHeapMagic);
Alexey Samsonov98737922012-08-10 15:13:05 +0000135}
136
Kostya Serebryany95f630a2013-01-28 07:34:22 +0000137static void PrintShadowMemoryForAddress(uptr addr) {
138 if (!AddrIsInMem(addr))
139 return;
140 uptr shadow_addr = MemToShadow(addr);
141 const uptr n_bytes_per_row = 16;
142 uptr aligned_shadow = shadow_addr & ~(n_bytes_per_row - 1);
143 Printf("Shadow bytes around the buggy address:\n");
144 for (int i = -5; i <= 5; i++) {
145 const char *prefix = (i == 0) ? "=>" : " ";
146 PrintShadowBytes(prefix,
147 (u8*)(aligned_shadow + i * n_bytes_per_row),
148 (u8*)shadow_addr, n_bytes_per_row);
149 }
150 if (flags()->print_legend)
151 PrintLegend();
152}
153
Alexey Samsonov98737922012-08-10 15:13:05 +0000154static void PrintZoneForPointer(uptr ptr, uptr zone_ptr,
155 const char *zone_name) {
156 if (zone_ptr) {
157 if (zone_name) {
Kostya Serebryany283c2962012-08-28 11:34:40 +0000158 Printf("malloc_zone_from_ptr(%p) = %p, which is %s\n",
Alexey Samsonov98737922012-08-10 15:13:05 +0000159 ptr, zone_ptr, zone_name);
160 } else {
Kostya Serebryany283c2962012-08-28 11:34:40 +0000161 Printf("malloc_zone_from_ptr(%p) = %p, which doesn't have a name\n",
Alexey Samsonov98737922012-08-10 15:13:05 +0000162 ptr, zone_ptr);
163 }
164 } else {
Kostya Serebryany283c2962012-08-28 11:34:40 +0000165 Printf("malloc_zone_from_ptr(%p) = 0\n", ptr);
Alexey Samsonov98737922012-08-10 15:13:05 +0000166 }
167}
168
Timur Iskhodzhanov997454a2013-09-10 08:36:21 +0000169static void DescribeThread(AsanThread *t) {
170 if (t)
171 DescribeThread(t->context());
172}
173
Alexey Samsonove218beb2012-08-09 09:06:52 +0000174// ---------------------- Address Descriptions ------------------- {{{1
175
Alexey Samsonove4bfca22012-08-09 09:27:24 +0000176static bool IsASCII(unsigned char c) {
Alexey Samsonov98737922012-08-10 15:13:05 +0000177 return /*0x00 <= c &&*/ c <= 0x7F;
Alexey Samsonove4bfca22012-08-09 09:27:24 +0000178}
179
Alexey Samsonovc9424272013-03-27 10:41:22 +0000180static const char *MaybeDemangleGlobalName(const char *name) {
181 // We can spoil names of globals with C linkage, so use an heuristic
182 // approach to check if the name should be demangled.
Alexey Samsonov7847d772013-09-10 14:36:16 +0000183 return (name[0] == '_' && name[1] == 'Z') ? getSymbolizer()->Demangle(name)
184 : name;
Alexey Samsonovc9424272013-03-27 10:41:22 +0000185}
186
Alexey Samsonov939316c2013-04-01 08:57:38 +0000187// Check if the global is a zero-terminated ASCII string. If so, print it.
188static void PrintGlobalNameIfASCII(const __asan_global &g) {
189 for (uptr p = g.beg; p < g.beg + g.size - 1; p++) {
190 unsigned char c = *(unsigned char*)p;
191 if (c == '\0' || !IsASCII(c)) return;
192 }
193 if (*(char*)(g.beg + g.size - 1) != '\0') return;
194 Printf(" '%s' is ascii string '%s'\n",
195 MaybeDemangleGlobalName(g.name), (char*)g.beg);
196}
197
Evgeniy Stepanov589dcda2013-02-05 14:32:03 +0000198bool DescribeAddressRelativeToGlobal(uptr addr, uptr size,
199 const __asan_global &g) {
Kostya Serebryanya3b0e5e2013-01-23 11:14:21 +0000200 static const uptr kMinimalDistanceFromAnotherGlobal = 64;
201 if (addr <= g.beg - kMinimalDistanceFromAnotherGlobal) return false;
Alexey Samsonove4bfca22012-08-09 09:27:24 +0000202 if (addr >= g.beg + g.size_with_redzone) return false;
Kostya Serebryany58f54552012-12-18 07:32:16 +0000203 Decorator d;
204 Printf("%s", d.Location());
Alexey Samsonove4bfca22012-08-09 09:27:24 +0000205 if (addr < g.beg) {
Evgeniy Stepanov589dcda2013-02-05 14:32:03 +0000206 Printf("%p is located %zd bytes to the left", (void*)addr, g.beg - addr);
207 } else if (addr + size > g.beg + g.size) {
208 if (addr < g.beg + g.size)
209 addr = g.beg + g.size;
210 Printf("%p is located %zd bytes to the right", (void*)addr,
211 addr - (g.beg + g.size));
Alexey Samsonove4bfca22012-08-09 09:27:24 +0000212 } else {
Evgeniy Stepanov589dcda2013-02-05 14:32:03 +0000213 // Can it happen?
214 Printf("%p is located %zd bytes inside", (void*)addr, addr - g.beg);
Alexey Samsonove4bfca22012-08-09 09:27:24 +0000215 }
Kostya Serebryany60c9f442013-03-18 08:04:55 +0000216 Printf(" of global variable '%s' from '%s' (0x%zx) of size %zu\n",
Alexey Samsonovc9424272013-03-27 10:41:22 +0000217 MaybeDemangleGlobalName(g.name), g.module_name, g.beg, g.size);
Kostya Serebryany58f54552012-12-18 07:32:16 +0000218 Printf("%s", d.EndLocation());
Alexey Samsonove4bfca22012-08-09 09:27:24 +0000219 PrintGlobalNameIfASCII(g);
220 return true;
221}
222
Alexey Samsonove218beb2012-08-09 09:06:52 +0000223bool DescribeAddressIfShadow(uptr addr) {
224 if (AddrIsInMem(addr))
225 return false;
226 static const char kAddrInShadowReport[] =
227 "Address %p is located in the %s.\n";
228 if (AddrIsInShadowGap(addr)) {
Kostya Serebryany283c2962012-08-28 11:34:40 +0000229 Printf(kAddrInShadowReport, addr, "shadow gap area");
Alexey Samsonove218beb2012-08-09 09:06:52 +0000230 return true;
231 }
232 if (AddrIsInHighShadow(addr)) {
Kostya Serebryany283c2962012-08-28 11:34:40 +0000233 Printf(kAddrInShadowReport, addr, "high shadow area");
Alexey Samsonove218beb2012-08-09 09:06:52 +0000234 return true;
235 }
236 if (AddrIsInLowShadow(addr)) {
Kostya Serebryany283c2962012-08-28 11:34:40 +0000237 Printf(kAddrInShadowReport, addr, "low shadow area");
Alexey Samsonove218beb2012-08-09 09:06:52 +0000238 return true;
239 }
240 CHECK(0 && "Address is not in memory and not in shadow?");
241 return false;
242}
243
Kostya Serebryany50f3daa2013-03-22 10:36:24 +0000244// Return " (thread_name) " or an empty string if the name is empty.
245const char *ThreadNameWithParenthesis(AsanThreadContext *t, char buff[],
246 uptr buff_len) {
247 const char *name = t->name;
248 if (name[0] == '\0') return "";
249 buff[0] = 0;
250 internal_strncat(buff, " (", 3);
251 internal_strncat(buff, name, buff_len - 4);
252 internal_strncat(buff, ")", 2);
253 return buff;
254}
255
256const char *ThreadNameWithParenthesis(u32 tid, char buff[],
257 uptr buff_len) {
258 if (tid == kInvalidTid) return "";
259 asanThreadRegistry().CheckLocked();
260 AsanThreadContext *t = GetThreadContextByTidLocked(tid);
261 return ThreadNameWithParenthesis(t, buff, buff_len);
262}
263
Kostya Serebryanyedb39c72013-09-03 13:58:04 +0000264void PrintAccessAndVarIntersection(const char *var_name,
265 uptr var_beg, uptr var_size,
266 uptr addr, uptr access_size,
267 uptr prev_var_end, uptr next_var_beg) {
268 uptr var_end = var_beg + var_size;
269 uptr addr_end = addr + access_size;
270 const char *pos_descr = 0;
271 // If the variable [var_beg, var_end) is the nearest variable to the
272 // current memory access, indicate it in the log.
273 if (addr >= var_beg) {
274 if (addr_end <= var_end)
275 pos_descr = "is inside"; // May happen if this is a use-after-return.
276 else if (addr < var_end)
277 pos_descr = "partially overflows";
278 else if (addr_end <= next_var_beg &&
279 next_var_beg - addr_end >= addr - var_end)
280 pos_descr = "overflows";
281 } else {
282 if (addr_end > var_beg)
283 pos_descr = "partially underflows";
284 else if (addr >= prev_var_end &&
285 addr - prev_var_end >= var_beg - addr_end)
286 pos_descr = "underflows";
287 }
288 Printf(" [%zd, %zd) '%s'", var_beg, var_beg + var_size, var_name);
289 if (pos_descr) {
290 Decorator d;
291 // FIXME: we may want to also print the size of the access here,
292 // but in case of accesses generated by memset it may be confusing.
293 Printf("%s <== Memory access at offset %zd %s this variable%s\n",
294 d.Location(), addr, pos_descr, d.EndLocation());
295 } else {
296 Printf("\n");
297 }
298}
299
300struct StackVarDescr {
301 uptr beg;
302 uptr size;
303 const char *name_pos;
Kostya Serebryany89fe5642013-09-03 14:53:02 +0000304 uptr name_len;
Kostya Serebryanyedb39c72013-09-03 13:58:04 +0000305};
306
Alexey Samsonove218beb2012-08-09 09:06:52 +0000307bool DescribeAddressIfStack(uptr addr, uptr access_size) {
Alexey Samsonovdef1be92013-03-21 11:23:41 +0000308 AsanThread *t = FindThreadByStackAddress(addr);
Alexey Samsonove218beb2012-08-09 09:06:52 +0000309 if (!t) return false;
Kostya Serebryany89fe5642013-09-03 14:53:02 +0000310 const uptr kBufSize = 4095;
Alexey Samsonove218beb2012-08-09 09:06:52 +0000311 char buf[kBufSize];
312 uptr offset = 0;
Kostya Serebryany50f3daa2013-03-22 10:36:24 +0000313 uptr frame_pc = 0;
314 char tname[128];
315 const char *frame_descr = t->GetFrameNameByAddr(addr, &offset, &frame_pc);
Kostya Serebryanyd570bb42013-05-22 14:21:34 +0000316
317#ifdef __powerpc64__
318 // On PowerPC64, the address of a function actually points to a
319 // three-doubleword data structure with the first field containing
320 // the address of the function's code.
321 frame_pc = *reinterpret_cast<uptr *>(frame_pc);
322#endif
323
Alexey Samsonove218beb2012-08-09 09:06:52 +0000324 // This string is created by the compiler and has the following form:
Kostya Serebryany50f3daa2013-03-22 10:36:24 +0000325 // "n alloc_1 alloc_2 ... alloc_n"
Alexey Samsonove218beb2012-08-09 09:06:52 +0000326 // where alloc_i looks like "offset size len ObjectName ".
327 CHECK(frame_descr);
Kostya Serebryany58f54552012-12-18 07:32:16 +0000328 Decorator d;
329 Printf("%s", d.Location());
Kostya Serebryany50f3daa2013-03-22 10:36:24 +0000330 Printf("Address %p is located in stack of thread T%d%s "
331 "at offset %zu in frame\n",
332 addr, t->tid(),
333 ThreadNameWithParenthesis(t->tid(), tname, sizeof(tname)),
334 offset);
335 // Now we print the frame where the alloca has happened.
336 // We print this frame as a stack trace with one element.
337 // The symbolizer may print more than one frame if inlining was involved.
338 // The frame numbers may be different than those in the stack trace printed
339 // previously. That's unfortunate, but I have no better solution,
340 // especially given that the alloca may be from entirely different place
341 // (e.g. use-after-scope, or different thread's stack).
342 StackTrace alloca_stack;
343 alloca_stack.trace[0] = frame_pc + 16;
344 alloca_stack.size = 1;
Kostya Serebryany58f54552012-12-18 07:32:16 +0000345 Printf("%s", d.EndLocation());
Kostya Serebryany50f3daa2013-03-22 10:36:24 +0000346 PrintStack(&alloca_stack);
Alexey Samsonove218beb2012-08-09 09:06:52 +0000347 // Report the number of stack objects.
348 char *p;
Timur Iskhodzhanov22917e92013-09-03 15:09:21 +0000349 uptr n_objects = (uptr)internal_simple_strtoll(frame_descr, &p, 10);
Kostya Serebryanya27bdf72013-04-05 14:40:25 +0000350 CHECK_GT(n_objects, 0);
Kostya Serebryany283c2962012-08-28 11:34:40 +0000351 Printf(" This frame has %zu object(s):\n", n_objects);
Kostya Serebryanyedb39c72013-09-03 13:58:04 +0000352
Alexey Samsonove218beb2012-08-09 09:06:52 +0000353 // Report all objects in this frame.
Kostya Serebryanyedb39c72013-09-03 13:58:04 +0000354 InternalScopedBuffer<StackVarDescr> vars(n_objects);
Kostya Serebryany89fe5642013-09-03 14:53:02 +0000355 for (uptr i = 0; i < n_objects; i++) {
356 uptr beg, size;
357 uptr len;
358 beg = (uptr)internal_simple_strtoll(p, &p, 10);
359 size = (uptr)internal_simple_strtoll(p, &p, 10);
360 len = (uptr)internal_simple_strtoll(p, &p, 10);
361 if (beg == 0 || size == 0 || *p != ' ') {
Kostya Serebryany283c2962012-08-28 11:34:40 +0000362 Printf("AddressSanitizer can't parse the stack frame "
Alexey Samsonove218beb2012-08-09 09:06:52 +0000363 "descriptor: |%s|\n", frame_descr);
364 break;
365 }
366 p++;
Kostya Serebryanyedb39c72013-09-03 13:58:04 +0000367 vars[i].beg = beg;
368 vars[i].size = size;
369 vars[i].name_pos = p;
370 vars[i].name_len = len;
Alexey Samsonove218beb2012-08-09 09:06:52 +0000371 p += len;
Kostya Serebryanyedb39c72013-09-03 13:58:04 +0000372 }
Kostya Serebryany89fe5642013-09-03 14:53:02 +0000373 for (uptr i = 0; i < n_objects; i++) {
Kostya Serebryanyedb39c72013-09-03 13:58:04 +0000374 buf[0] = 0;
375 internal_strncat(buf, vars[i].name_pos,
376 static_cast<uptr>(Min(kBufSize, vars[i].name_len)));
377 uptr prev_var_end = i ? vars[i - 1].beg + vars[i - 1].size : 0;
Timur Iskhodzhanov22917e92013-09-03 15:09:21 +0000378 uptr next_var_beg = i + 1 < n_objects ? vars[i + 1].beg : ~(0UL);
Kostya Serebryanyedb39c72013-09-03 13:58:04 +0000379 PrintAccessAndVarIntersection(buf, vars[i].beg, vars[i].size,
380 offset, access_size,
381 prev_var_end, next_var_beg);
Alexey Samsonove218beb2012-08-09 09:06:52 +0000382 }
Kostya Serebryany283c2962012-08-28 11:34:40 +0000383 Printf("HINT: this may be a false positive if your program uses "
Alexey Samsonov08700282012-11-23 09:46:34 +0000384 "some custom stack unwind mechanism or swapcontext\n"
Alexey Samsonove218beb2012-08-09 09:06:52 +0000385 " (longjmp and C++ exceptions *are* supported)\n");
Timur Iskhodzhanov997454a2013-09-10 08:36:21 +0000386 DescribeThread(t);
Alexey Samsonove218beb2012-08-09 09:06:52 +0000387 return true;
388}
389
Alexey Samsonov5c153fa2012-09-18 07:38:10 +0000390static void DescribeAccessToHeapChunk(AsanChunkView chunk, uptr addr,
391 uptr access_size) {
Evgeniy Stepanov589dcda2013-02-05 14:32:03 +0000392 sptr offset;
Kostya Serebryany58f54552012-12-18 07:32:16 +0000393 Decorator d;
394 Printf("%s", d.Location());
Evgeniy Stepanov589dcda2013-02-05 14:32:03 +0000395 if (chunk.AddrIsAtLeft(addr, access_size, &offset)) {
396 Printf("%p is located %zd bytes to the left of", (void*)addr, offset);
Alexey Samsonov5c153fa2012-09-18 07:38:10 +0000397 } else if (chunk.AddrIsAtRight(addr, access_size, &offset)) {
Evgeniy Stepanov589dcda2013-02-05 14:32:03 +0000398 if (offset < 0) {
399 addr -= offset;
400 offset = 0;
401 }
402 Printf("%p is located %zd bytes to the right of", (void*)addr, offset);
403 } else if (chunk.AddrIsInside(addr, access_size, &offset)) {
404 Printf("%p is located %zd bytes inside of", (void*)addr, offset);
Alexey Samsonov5c153fa2012-09-18 07:38:10 +0000405 } else {
Evgeniy Stepanov589dcda2013-02-05 14:32:03 +0000406 Printf("%p is located somewhere around (this is AddressSanitizer bug!)",
407 (void*)addr);
Alexey Samsonov5c153fa2012-09-18 07:38:10 +0000408 }
409 Printf(" %zu-byte region [%p,%p)\n", chunk.UsedSize(),
410 (void*)(chunk.Beg()), (void*)(chunk.End()));
Kostya Serebryany58f54552012-12-18 07:32:16 +0000411 Printf("%s", d.EndLocation());
Alexey Samsonov5c153fa2012-09-18 07:38:10 +0000412}
413
414void DescribeHeapAddress(uptr addr, uptr access_size) {
415 AsanChunkView chunk = FindHeapChunkByAddress(addr);
416 if (!chunk.IsValid()) return;
417 DescribeAccessToHeapChunk(chunk, addr, access_size);
418 CHECK(chunk.AllocTid() != kInvalidTid);
Alexey Samsonovdef1be92013-03-21 11:23:41 +0000419 asanThreadRegistry().CheckLocked();
420 AsanThreadContext *alloc_thread =
421 GetThreadContextByTidLocked(chunk.AllocTid());
Alexey Samsonov5c153fa2012-09-18 07:38:10 +0000422 StackTrace alloc_stack;
423 chunk.GetAllocStack(&alloc_stack);
Kostya Serebryany716e2f22012-12-07 15:15:01 +0000424 char tname[128];
Kostya Serebryany58f54552012-12-18 07:32:16 +0000425 Decorator d;
Timur Iskhodzhanov997454a2013-09-10 08:36:21 +0000426 AsanThreadContext *free_thread = 0;
Alexey Samsonov5c153fa2012-09-18 07:38:10 +0000427 if (chunk.FreeTid() != kInvalidTid) {
Timur Iskhodzhanov997454a2013-09-10 08:36:21 +0000428 free_thread = GetThreadContextByTidLocked(chunk.FreeTid());
Kostya Serebryany58f54552012-12-18 07:32:16 +0000429 Printf("%sfreed by thread T%d%s here:%s\n", d.Allocation(),
Alexey Samsonovdef1be92013-03-21 11:23:41 +0000430 free_thread->tid,
Kostya Serebryany58f54552012-12-18 07:32:16 +0000431 ThreadNameWithParenthesis(free_thread, tname, sizeof(tname)),
432 d.EndAllocation());
Alexey Samsonov5c153fa2012-09-18 07:38:10 +0000433 StackTrace free_stack;
434 chunk.GetFreeStack(&free_stack);
435 PrintStack(&free_stack);
Kostya Serebryany58f54552012-12-18 07:32:16 +0000436 Printf("%spreviously allocated by thread T%d%s here:%s\n",
Alexey Samsonovdef1be92013-03-21 11:23:41 +0000437 d.Allocation(), alloc_thread->tid,
Kostya Serebryany58f54552012-12-18 07:32:16 +0000438 ThreadNameWithParenthesis(alloc_thread, tname, sizeof(tname)),
439 d.EndAllocation());
Alexey Samsonov5c153fa2012-09-18 07:38:10 +0000440 } else {
Kostya Serebryany58f54552012-12-18 07:32:16 +0000441 Printf("%sallocated by thread T%d%s here:%s\n", d.Allocation(),
Alexey Samsonovdef1be92013-03-21 11:23:41 +0000442 alloc_thread->tid,
Kostya Serebryany58f54552012-12-18 07:32:16 +0000443 ThreadNameWithParenthesis(alloc_thread, tname, sizeof(tname)),
444 d.EndAllocation());
Alexey Samsonov5c153fa2012-09-18 07:38:10 +0000445 }
Timur Iskhodzhanov997454a2013-09-10 08:36:21 +0000446 PrintStack(&alloc_stack);
447 DescribeThread(GetCurrentThread());
448 if (free_thread)
449 DescribeThread(free_thread);
450 DescribeThread(alloc_thread);
Alexey Samsonov5c153fa2012-09-18 07:38:10 +0000451}
452
Alexey Samsonove218beb2012-08-09 09:06:52 +0000453void DescribeAddress(uptr addr, uptr access_size) {
454 // Check if this is shadow or shadow gap.
455 if (DescribeAddressIfShadow(addr))
456 return;
457 CHECK(AddrIsInMem(addr));
Evgeniy Stepanov589dcda2013-02-05 14:32:03 +0000458 if (DescribeAddressIfGlobal(addr, access_size))
Alexey Samsonove218beb2012-08-09 09:06:52 +0000459 return;
460 if (DescribeAddressIfStack(addr, access_size))
461 return;
462 // Assume it is a heap address.
463 DescribeHeapAddress(addr, access_size);
464}
465
Alexey Samsonov71b42c92012-09-05 07:37:15 +0000466// ------------------- Thread description -------------------- {{{1
467
Alexey Samsonovdef1be92013-03-21 11:23:41 +0000468void DescribeThread(AsanThreadContext *context) {
469 CHECK(context);
470 asanThreadRegistry().CheckLocked();
Alexey Samsonov71b42c92012-09-05 07:37:15 +0000471 // No need to announce the main thread.
Alexey Samsonovdef1be92013-03-21 11:23:41 +0000472 if (context->tid == 0 || context->announced) {
Alexey Samsonov71b42c92012-09-05 07:37:15 +0000473 return;
474 }
Alexey Samsonovdef1be92013-03-21 11:23:41 +0000475 context->announced = true;
Kostya Serebryany716e2f22012-12-07 15:15:01 +0000476 char tname[128];
Alexey Samsonovdef1be92013-03-21 11:23:41 +0000477 Printf("Thread T%d%s", context->tid,
478 ThreadNameWithParenthesis(context->tid, tname, sizeof(tname)));
Kostya Serebryany716e2f22012-12-07 15:15:01 +0000479 Printf(" created by T%d%s here:\n",
Alexey Samsonovdef1be92013-03-21 11:23:41 +0000480 context->parent_tid,
481 ThreadNameWithParenthesis(context->parent_tid,
Kostya Serebryany716e2f22012-12-07 15:15:01 +0000482 tname, sizeof(tname)));
Alexey Samsonovdef1be92013-03-21 11:23:41 +0000483 PrintStack(&context->stack);
Alexey Samsonov71b42c92012-09-05 07:37:15 +0000484 // Recursively described parent thread if needed.
485 if (flags()->print_full_thread_history) {
Alexey Samsonovdef1be92013-03-21 11:23:41 +0000486 AsanThreadContext *parent_context =
487 GetThreadContextByTidLocked(context->parent_tid);
488 DescribeThread(parent_context);
Alexey Samsonov71b42c92012-09-05 07:37:15 +0000489 }
490}
491
Alexey Samsonove218beb2012-08-09 09:06:52 +0000492// -------------------- Different kinds of reports ----------------- {{{1
493
Alexey Samsonov98737922012-08-10 15:13:05 +0000494// Use ScopedInErrorReport to run common actions just before and
495// immediately after printing error report.
496class ScopedInErrorReport {
497 public:
498 ScopedInErrorReport() {
499 static atomic_uint32_t num_calls;
Alexey Samsonov62e27092012-09-17 08:02:19 +0000500 static u32 reporting_thread_tid;
Alexey Samsonov98737922012-08-10 15:13:05 +0000501 if (atomic_fetch_add(&num_calls, 1, memory_order_relaxed) != 0) {
502 // Do not print more than one report, otherwise they will mix up.
503 // Error reporting functions shouldn't return at this situation, as
504 // they are defined as no-return.
Kostya Serebryany283c2962012-08-28 11:34:40 +0000505 Report("AddressSanitizer: while reporting a bug found another one."
Alexey Samsonov98737922012-08-10 15:13:05 +0000506 "Ignoring.\n");
Alexey Samsonov89c13842013-03-20 09:23:28 +0000507 u32 current_tid = GetCurrentTidOrInvalid();
Alexey Samsonov62e27092012-09-17 08:02:19 +0000508 if (current_tid != reporting_thread_tid) {
509 // ASan found two bugs in different threads simultaneously. Sleep
510 // long enough to make sure that the thread which started to print
511 // an error report will finish doing it.
512 SleepForSeconds(Max(100, flags()->sleep_before_dying + 1));
513 }
Alexey Samsonovf8822472013-02-20 13:54:32 +0000514 // If we're still not dead for some reason, use raw _exit() instead of
Alexey Samsonov031633b2012-11-19 11:22:22 +0000515 // Die() to bypass any additional checks.
Alexey Samsonovf8822472013-02-20 13:54:32 +0000516 internal__exit(flags()->exitcode);
Alexey Samsonov98737922012-08-10 15:13:05 +0000517 }
Alexey Samsonov6a08d292012-12-07 22:01:28 +0000518 ASAN_ON_ERROR();
Alexey Samsonov7ed46ff2013-04-05 07:30:29 +0000519 // Make sure the registry and sanitizer report mutexes are locked while
520 // we're printing an error report.
521 // We can lock them only here to avoid self-deadlock in case of
Alexey Samsonovdef1be92013-03-21 11:23:41 +0000522 // recursive reports.
523 asanThreadRegistry().Lock();
Alexey Samsonov7ed46ff2013-04-05 07:30:29 +0000524 CommonSanitizerReportMutex.Lock();
Alexey Samsonov89c13842013-03-20 09:23:28 +0000525 reporting_thread_tid = GetCurrentTidOrInvalid();
Kostya Serebryany283c2962012-08-28 11:34:40 +0000526 Printf("===================================================="
Alexey Samsonov62e27092012-09-17 08:02:19 +0000527 "=============\n");
Alexey Samsonov98737922012-08-10 15:13:05 +0000528 }
529 // Destructor is NORETURN, as functions that report errors are.
530 NORETURN ~ScopedInErrorReport() {
531 // Make sure the current thread is announced.
Timur Iskhodzhanov997454a2013-09-10 08:36:21 +0000532 DescribeThread(GetCurrentThread());
Alexey Samsonov98737922012-08-10 15:13:05 +0000533 // Print memory stats.
Kostya Serebryany95f630a2013-01-28 07:34:22 +0000534 if (flags()->print_stats)
535 __asan_print_accumulated_stats();
Alexey Samsonov98737922012-08-10 15:13:05 +0000536 if (error_report_callback) {
537 error_report_callback(error_message_buffer);
538 }
Kostya Serebryany283c2962012-08-28 11:34:40 +0000539 Report("ABORTING\n");
Alexey Samsonov98737922012-08-10 15:13:05 +0000540 Die();
541 }
542};
543
Kostya Serebryany2673fd82013-02-06 12:36:49 +0000544static void ReportSummary(const char *error_type, StackTrace *stack) {
545 if (!stack->size) return;
Alexey Samsonov7847d772013-09-10 14:36:16 +0000546 if (getSymbolizer()->IsAvailable()) {
Kostya Serebryany2673fd82013-02-06 12:36:49 +0000547 AddressInfo ai;
548 // Currently, we include the first stack frame into the report summary.
549 // Maybe sometimes we need to choose another frame (e.g. skip memcpy/etc).
Alexey Samsonov22c1c182013-04-11 11:45:04 +0000550 uptr pc = StackTrace::GetPreviousInstructionPc(stack->trace[0]);
Alexey Samsonov7847d772013-09-10 14:36:16 +0000551 getSymbolizer()->SymbolizeCode(pc, &ai, 1);
Kostya Serebryany2673fd82013-02-06 12:36:49 +0000552 ReportErrorSummary(error_type,
Sergey Matveeved20ebe2013-05-06 11:27:58 +0000553 StripPathPrefix(ai.file,
554 common_flags()->strip_path_prefix),
Kostya Serebryany2673fd82013-02-06 12:36:49 +0000555 ai.line, ai.function);
556 }
557 // FIXME: do we need to print anything at all if there is no symbolizer?
558}
559
Alexey Samsonov73545092012-08-09 07:40:58 +0000560void ReportSIGSEGV(uptr pc, uptr sp, uptr bp, uptr addr) {
Alexey Samsonov98737922012-08-10 15:13:05 +0000561 ScopedInErrorReport in_report;
Kostya Serebryany58f54552012-12-18 07:32:16 +0000562 Decorator d;
563 Printf("%s", d.Warning());
Kostya Serebryany69d8ede2012-10-15 13:04:58 +0000564 Report("ERROR: AddressSanitizer: SEGV on unknown address %p"
Alexey Samsonov73545092012-08-09 07:40:58 +0000565 " (pc %p sp %p bp %p T%d)\n",
566 (void*)addr, (void*)pc, (void*)sp, (void*)bp,
Alexey Samsonov89c13842013-03-20 09:23:28 +0000567 GetCurrentTidOrInvalid());
Kostya Serebryany58f54552012-12-18 07:32:16 +0000568 Printf("%s", d.EndWarning());
Kostya Serebryany283c2962012-08-28 11:34:40 +0000569 Printf("AddressSanitizer can not provide additional info.\n");
Kostya Serebryanya30c8f92012-12-13 09:34:23 +0000570 GET_STACK_TRACE_FATAL(pc, bp);
Kostya Serebryanycc347222012-08-28 13:49:49 +0000571 PrintStack(&stack);
Kostya Serebryany2673fd82013-02-06 12:36:49 +0000572 ReportSummary("SEGV", &stack);
Alexey Samsonov73545092012-08-09 07:40:58 +0000573}
574
Kostya Serebryanyc3390df2012-08-28 11:54:30 +0000575void ReportDoubleFree(uptr addr, StackTrace *stack) {
Alexey Samsonov98737922012-08-10 15:13:05 +0000576 ScopedInErrorReport in_report;
Kostya Serebryany58f54552012-12-18 07:32:16 +0000577 Decorator d;
578 Printf("%s", d.Warning());
Kostya Serebryanya89a35a2013-03-26 08:01:37 +0000579 char tname[128];
580 u32 curr_tid = GetCurrentTidOrInvalid();
581 Report("ERROR: AddressSanitizer: attempting double-free on %p in "
582 "thread T%d%s:\n",
583 addr, curr_tid,
584 ThreadNameWithParenthesis(curr_tid, tname, sizeof(tname)));
585
Kostya Serebryany58f54552012-12-18 07:32:16 +0000586 Printf("%s", d.EndWarning());
Kostya Serebryanycc347222012-08-28 13:49:49 +0000587 PrintStack(stack);
Alexey Samsonovf7c1d182012-08-09 08:15:46 +0000588 DescribeHeapAddress(addr, 1);
Kostya Serebryany2673fd82013-02-06 12:36:49 +0000589 ReportSummary("double-free", stack);
Alexey Samsonovf7c1d182012-08-09 08:15:46 +0000590}
591
Kostya Serebryanyc3390df2012-08-28 11:54:30 +0000592void ReportFreeNotMalloced(uptr addr, StackTrace *stack) {
Alexey Samsonov98737922012-08-10 15:13:05 +0000593 ScopedInErrorReport in_report;
Kostya Serebryany58f54552012-12-18 07:32:16 +0000594 Decorator d;
595 Printf("%s", d.Warning());
Kostya Serebryanya89a35a2013-03-26 08:01:37 +0000596 char tname[128];
597 u32 curr_tid = GetCurrentTidOrInvalid();
Kostya Serebryany69d8ede2012-10-15 13:04:58 +0000598 Report("ERROR: AddressSanitizer: attempting free on address "
Kostya Serebryanya89a35a2013-03-26 08:01:37 +0000599 "which was not malloc()-ed: %p in thread T%d%s\n", addr,
600 curr_tid, ThreadNameWithParenthesis(curr_tid, tname, sizeof(tname)));
Kostya Serebryany58f54552012-12-18 07:32:16 +0000601 Printf("%s", d.EndWarning());
Kostya Serebryanycc347222012-08-28 13:49:49 +0000602 PrintStack(stack);
Alexey Samsonov98737922012-08-10 15:13:05 +0000603 DescribeHeapAddress(addr, 1);
Kostya Serebryany2673fd82013-02-06 12:36:49 +0000604 ReportSummary("bad-free", stack);
Alexey Samsonovf7c1d182012-08-09 08:15:46 +0000605}
606
Kostya Serebryanyfe6d9162012-12-21 08:53:59 +0000607void ReportAllocTypeMismatch(uptr addr, StackTrace *stack,
608 AllocType alloc_type,
609 AllocType dealloc_type) {
610 static const char *alloc_names[] =
611 {"INVALID", "malloc", "operator new", "operator new []"};
612 static const char *dealloc_names[] =
613 {"INVALID", "free", "operator delete", "operator delete []"};
614 CHECK_NE(alloc_type, dealloc_type);
615 ScopedInErrorReport in_report;
616 Decorator d;
617 Printf("%s", d.Warning());
618 Report("ERROR: AddressSanitizer: alloc-dealloc-mismatch (%s vs %s) on %p\n",
619 alloc_names[alloc_type], dealloc_names[dealloc_type], addr);
620 Printf("%s", d.EndWarning());
621 PrintStack(stack);
622 DescribeHeapAddress(addr, 1);
Kostya Serebryany2673fd82013-02-06 12:36:49 +0000623 ReportSummary("alloc-dealloc-mismatch", stack);
Kostya Serebryanyfe6d9162012-12-21 08:53:59 +0000624 Report("HINT: if you don't care about these warnings you may set "
625 "ASAN_OPTIONS=alloc_dealloc_mismatch=0\n");
626}
627
Kostya Serebryanyc3390df2012-08-28 11:54:30 +0000628void ReportMallocUsableSizeNotOwned(uptr addr, StackTrace *stack) {
Alexey Samsonov98737922012-08-10 15:13:05 +0000629 ScopedInErrorReport in_report;
Kostya Serebryany58f54552012-12-18 07:32:16 +0000630 Decorator d;
631 Printf("%s", d.Warning());
Kostya Serebryany69d8ede2012-10-15 13:04:58 +0000632 Report("ERROR: AddressSanitizer: attempting to call "
Alexey Samsonovf7c1d182012-08-09 08:15:46 +0000633 "malloc_usable_size() for pointer which is "
634 "not owned: %p\n", addr);
Kostya Serebryany58f54552012-12-18 07:32:16 +0000635 Printf("%s", d.EndWarning());
Kostya Serebryanycc347222012-08-28 13:49:49 +0000636 PrintStack(stack);
Alexey Samsonovf7c1d182012-08-09 08:15:46 +0000637 DescribeHeapAddress(addr, 1);
Kostya Serebryany2673fd82013-02-06 12:36:49 +0000638 ReportSummary("bad-malloc_usable_size", stack);
Alexey Samsonovf7c1d182012-08-09 08:15:46 +0000639}
640
Kostya Serebryanyc3390df2012-08-28 11:54:30 +0000641void ReportAsanGetAllocatedSizeNotOwned(uptr addr, StackTrace *stack) {
Alexey Samsonov98737922012-08-10 15:13:05 +0000642 ScopedInErrorReport in_report;
Kostya Serebryany58f54552012-12-18 07:32:16 +0000643 Decorator d;
644 Printf("%s", d.Warning());
Kostya Serebryany69d8ede2012-10-15 13:04:58 +0000645 Report("ERROR: AddressSanitizer: attempting to call "
Alexey Samsonovf7c1d182012-08-09 08:15:46 +0000646 "__asan_get_allocated_size() for pointer which is "
647 "not owned: %p\n", addr);
Kostya Serebryany58f54552012-12-18 07:32:16 +0000648 Printf("%s", d.EndWarning());
Kostya Serebryanycc347222012-08-28 13:49:49 +0000649 PrintStack(stack);
Alexey Samsonovf7c1d182012-08-09 08:15:46 +0000650 DescribeHeapAddress(addr, 1);
Kostya Serebryany2673fd82013-02-06 12:36:49 +0000651 ReportSummary("bad-__asan_get_allocated_size", stack);
Alexey Samsonovf7c1d182012-08-09 08:15:46 +0000652}
653
Alexey Samsonov487fee72012-08-09 08:32:33 +0000654void ReportStringFunctionMemoryRangesOverlap(
655 const char *function, const char *offset1, uptr length1,
Kostya Serebryanyc3390df2012-08-28 11:54:30 +0000656 const char *offset2, uptr length2, StackTrace *stack) {
Alexey Samsonov98737922012-08-10 15:13:05 +0000657 ScopedInErrorReport in_report;
Kostya Serebryany58f54552012-12-18 07:32:16 +0000658 Decorator d;
Kostya Serebryany2673fd82013-02-06 12:36:49 +0000659 char bug_type[100];
660 internal_snprintf(bug_type, sizeof(bug_type), "%s-param-overlap", function);
Kostya Serebryany58f54552012-12-18 07:32:16 +0000661 Printf("%s", d.Warning());
Kostya Serebryany2673fd82013-02-06 12:36:49 +0000662 Report("ERROR: AddressSanitizer: %s: "
Alexey Samsonov487fee72012-08-09 08:32:33 +0000663 "memory ranges [%p,%p) and [%p, %p) overlap\n", \
Kostya Serebryany2673fd82013-02-06 12:36:49 +0000664 bug_type, offset1, offset1 + length1, offset2, offset2 + length2);
Kostya Serebryany58f54552012-12-18 07:32:16 +0000665 Printf("%s", d.EndWarning());
Kostya Serebryanycc347222012-08-28 13:49:49 +0000666 PrintStack(stack);
Alexey Samsonov98737922012-08-10 15:13:05 +0000667 DescribeAddress((uptr)offset1, length1);
668 DescribeAddress((uptr)offset2, length2);
Kostya Serebryany2673fd82013-02-06 12:36:49 +0000669 ReportSummary(bug_type, stack);
Alexey Samsonov487fee72012-08-09 08:32:33 +0000670}
Alexey Samsonovf7c1d182012-08-09 08:15:46 +0000671
Alexey Samsonov663c5012012-08-09 12:15:40 +0000672// ----------------------- Mac-specific reports ----------------- {{{1
673
Alexey Samsonov663c5012012-08-09 12:15:40 +0000674void WarnMacFreeUnallocated(
Kostya Serebryanyc3390df2012-08-28 11:54:30 +0000675 uptr addr, uptr zone_ptr, const char *zone_name, StackTrace *stack) {
Alexey Samsonov98737922012-08-10 15:13:05 +0000676 // Just print a warning here.
Kostya Serebryany283c2962012-08-28 11:34:40 +0000677 Printf("free_common(%p) -- attempting to free unallocated memory.\n"
Alexey Samsonov663c5012012-08-09 12:15:40 +0000678 "AddressSanitizer is ignoring this error on Mac OS now.\n",
679 addr);
680 PrintZoneForPointer(addr, zone_ptr, zone_name);
Kostya Serebryanycc347222012-08-28 13:49:49 +0000681 PrintStack(stack);
Alexey Samsonov98737922012-08-10 15:13:05 +0000682 DescribeHeapAddress(addr, 1);
Alexey Samsonov663c5012012-08-09 12:15:40 +0000683}
684
685void ReportMacMzReallocUnknown(
Kostya Serebryanyc3390df2012-08-28 11:54:30 +0000686 uptr addr, uptr zone_ptr, const char *zone_name, StackTrace *stack) {
Alexey Samsonov98737922012-08-10 15:13:05 +0000687 ScopedInErrorReport in_report;
Kostya Serebryany283c2962012-08-28 11:34:40 +0000688 Printf("mz_realloc(%p) -- attempting to realloc unallocated memory.\n"
Alexey Samsonov663c5012012-08-09 12:15:40 +0000689 "This is an unrecoverable problem, exiting now.\n",
690 addr);
691 PrintZoneForPointer(addr, zone_ptr, zone_name);
Kostya Serebryanycc347222012-08-28 13:49:49 +0000692 PrintStack(stack);
Alexey Samsonov98737922012-08-10 15:13:05 +0000693 DescribeHeapAddress(addr, 1);
Alexey Samsonov663c5012012-08-09 12:15:40 +0000694}
695
696void ReportMacCfReallocUnknown(
Kostya Serebryanyc3390df2012-08-28 11:54:30 +0000697 uptr addr, uptr zone_ptr, const char *zone_name, StackTrace *stack) {
Alexey Samsonov98737922012-08-10 15:13:05 +0000698 ScopedInErrorReport in_report;
Kostya Serebryany283c2962012-08-28 11:34:40 +0000699 Printf("cf_realloc(%p) -- attempting to realloc unallocated memory.\n"
Alexey Samsonov663c5012012-08-09 12:15:40 +0000700 "This is an unrecoverable problem, exiting now.\n",
701 addr);
702 PrintZoneForPointer(addr, zone_ptr, zone_name);
Kostya Serebryanycc347222012-08-28 13:49:49 +0000703 PrintStack(stack);
Alexey Samsonov98737922012-08-10 15:13:05 +0000704 DescribeHeapAddress(addr, 1);
Alexey Samsonovc98570b2012-08-09 10:56:57 +0000705}
706
Alexey Samsonov812ff902012-08-09 11:29:13 +0000707} // namespace __asan
708
709// --------------------------- Interface --------------------- {{{1
710using namespace __asan; // NOLINT
711
712void __asan_report_error(uptr pc, uptr bp, uptr sp,
713 uptr addr, bool is_write, uptr access_size) {
Alexey Samsonov98737922012-08-10 15:13:05 +0000714 ScopedInErrorReport in_report;
Alexey Samsonovc98570b2012-08-09 10:56:57 +0000715
Alexey Samsonov98737922012-08-10 15:13:05 +0000716 // Determine the error type.
Alexey Samsonovc98570b2012-08-09 10:56:57 +0000717 const char *bug_descr = "unknown-crash";
718 if (AddrIsInMem(addr)) {
719 u8 *shadow_addr = (u8*)MemToShadow(addr);
720 // If we are accessing 16 bytes, look at the second shadow byte.
721 if (*shadow_addr == 0 && access_size > SHADOW_GRANULARITY)
722 shadow_addr++;
723 // If we are in the partial right redzone, look at the next shadow byte.
724 if (*shadow_addr > 0 && *shadow_addr < 128)
725 shadow_addr++;
726 switch (*shadow_addr) {
727 case kAsanHeapLeftRedzoneMagic:
728 case kAsanHeapRightRedzoneMagic:
729 bug_descr = "heap-buffer-overflow";
730 break;
731 case kAsanHeapFreeMagic:
732 bug_descr = "heap-use-after-free";
733 break;
734 case kAsanStackLeftRedzoneMagic:
735 bug_descr = "stack-buffer-underflow";
736 break;
Kostya Serebryany3945c582012-08-21 14:10:25 +0000737 case kAsanInitializationOrderMagic:
738 bug_descr = "initialization-order-fiasco";
739 break;
Alexey Samsonovc98570b2012-08-09 10:56:57 +0000740 case kAsanStackMidRedzoneMagic:
741 case kAsanStackRightRedzoneMagic:
742 case kAsanStackPartialRedzoneMagic:
743 bug_descr = "stack-buffer-overflow";
744 break;
745 case kAsanStackAfterReturnMagic:
746 bug_descr = "stack-use-after-return";
747 break;
748 case kAsanUserPoisonedMemoryMagic:
749 bug_descr = "use-after-poison";
750 break;
Alexey Samsonovd4b5db82012-12-04 01:38:15 +0000751 case kAsanStackUseAfterScopeMagic:
752 bug_descr = "stack-use-after-scope";
753 break;
Alexey Samsonovc98570b2012-08-09 10:56:57 +0000754 case kAsanGlobalRedzoneMagic:
755 bug_descr = "global-buffer-overflow";
756 break;
757 }
758 }
Kostya Serebryany58f54552012-12-18 07:32:16 +0000759 Decorator d;
760 Printf("%s", d.Warning());
Kostya Serebryany69d8ede2012-10-15 13:04:58 +0000761 Report("ERROR: AddressSanitizer: %s on address "
Alexey Samsonovc98570b2012-08-09 10:56:57 +0000762 "%p at pc 0x%zx bp 0x%zx sp 0x%zx\n",
763 bug_descr, (void*)addr, pc, bp, sp);
Kostya Serebryany58f54552012-12-18 07:32:16 +0000764 Printf("%s", d.EndWarning());
Alexey Samsonovc98570b2012-08-09 10:56:57 +0000765
Alexey Samsonov89c13842013-03-20 09:23:28 +0000766 u32 curr_tid = GetCurrentTidOrInvalid();
Kostya Serebryany716e2f22012-12-07 15:15:01 +0000767 char tname[128];
Kostya Serebryany58f54552012-12-18 07:32:16 +0000768 Printf("%s%s of size %zu at %p thread T%d%s%s\n",
769 d.Access(),
770 access_size ? (is_write ? "WRITE" : "READ") : "ACCESS",
771 access_size, (void*)addr, curr_tid,
772 ThreadNameWithParenthesis(curr_tid, tname, sizeof(tname)),
773 d.EndAccess());
Alexey Samsonovc98570b2012-08-09 10:56:57 +0000774
Kostya Serebryanya30c8f92012-12-13 09:34:23 +0000775 GET_STACK_TRACE_FATAL(pc, bp);
Kostya Serebryanycc347222012-08-28 13:49:49 +0000776 PrintStack(&stack);
Alexey Samsonovc98570b2012-08-09 10:56:57 +0000777
778 DescribeAddress(addr, access_size);
Kostya Serebryany2673fd82013-02-06 12:36:49 +0000779 ReportSummary(bug_descr, &stack);
Alexey Samsonov98737922012-08-10 15:13:05 +0000780 PrintShadowMemoryForAddress(addr);
Alexey Samsonovc98570b2012-08-09 10:56:57 +0000781}
782
Alexey Samsonovc98570b2012-08-09 10:56:57 +0000783void NOINLINE __asan_set_error_report_callback(void (*callback)(const char*)) {
784 error_report_callback = callback;
785 if (callback) {
786 error_message_buffer_size = 1 << 16;
787 error_message_buffer =
788 (char*)MmapOrDie(error_message_buffer_size, __FUNCTION__);
789 error_message_buffer_pos = 0;
790 }
791}
Alexey Samsonovf657a192012-08-13 11:23:40 +0000792
Kostya Serebryany17a7c672012-12-29 10:18:31 +0000793void __asan_describe_address(uptr addr) {
794 DescribeAddress(addr, 1);
795}
796
Alexey Samsonov6a08d292012-12-07 22:01:28 +0000797#if !SANITIZER_SUPPORTS_WEAK_HOOKS
Alexey Samsonov86633432012-10-02 14:06:39 +0000798// Provide default implementation of __asan_on_error that does nothing
799// and may be overriden by user.
Timur Iskhodzhanov3c80c6c2013-08-13 11:42:45 +0000800SANITIZER_INTERFACE_ATTRIBUTE SANITIZER_WEAK_ATTRIBUTE NOINLINE
Alexey Samsonov86633432012-10-02 14:06:39 +0000801void __asan_on_error() {}
Alexey Samsonov6a08d292012-12-07 22:01:28 +0000802#endif