blob: ca0600890d78e039fe6cebf392fe0386c420d83b [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"
Kostya Serebryany6d958692013-10-18 14:50:44 +000023#include "sanitizer_common/sanitizer_stackdepot.h"
Alexey Samsonov9c927482012-12-26 14:44:46 +000024#include "sanitizer_common/sanitizer_symbolizer.h"
Alexey Samsonov73545092012-08-09 07:40:58 +000025
26namespace __asan {
27
Alexey Samsonovf657a192012-08-13 11:23:40 +000028// -------------------- User-specified callbacks ----------------- {{{1
Alexey Samsonovc98570b2012-08-09 10:56:57 +000029static void (*error_report_callback)(const char*);
30static char *error_message_buffer = 0;
31static uptr error_message_buffer_pos = 0;
32static uptr error_message_buffer_size = 0;
33
34void AppendToErrorMessageBuffer(const char *buffer) {
35 if (error_message_buffer) {
36 uptr length = internal_strlen(buffer);
37 CHECK_GE(error_message_buffer_size, error_message_buffer_pos);
38 uptr remaining = error_message_buffer_size - error_message_buffer_pos;
39 internal_strncpy(error_message_buffer + error_message_buffer_pos,
40 buffer, remaining);
41 error_message_buffer[error_message_buffer_size - 1] = '\0';
42 // FIXME: reallocate the buffer instead of truncating the message.
43 error_message_buffer_pos += remaining > length ? length : remaining;
44 }
45}
46
Kostya Serebryany58f54552012-12-18 07:32:16 +000047// ---------------------- Decorator ------------------------------ {{{1
48class Decorator: private __sanitizer::AnsiColorDecorator {
49 public:
Kostya Serebryany9514a532012-12-19 09:53:32 +000050 Decorator() : __sanitizer::AnsiColorDecorator(PrintsToTtyCached()) { }
Kostya Serebryany58f54552012-12-18 07:32:16 +000051 const char *Warning() { return Red(); }
52 const char *EndWarning() { return Default(); }
53 const char *Access() { return Blue(); }
54 const char *EndAccess() { return Default(); }
55 const char *Location() { return Green(); }
56 const char *EndLocation() { return Default(); }
57 const char *Allocation() { return Magenta(); }
58 const char *EndAllocation() { return Default(); }
Kostya Serebryany9514a532012-12-19 09:53:32 +000059
60 const char *ShadowByte(u8 byte) {
61 switch (byte) {
62 case kAsanHeapLeftRedzoneMagic:
63 case kAsanHeapRightRedzoneMagic:
64 return Red();
65 case kAsanHeapFreeMagic:
66 return Magenta();
67 case kAsanStackLeftRedzoneMagic:
68 case kAsanStackMidRedzoneMagic:
69 case kAsanStackRightRedzoneMagic:
70 case kAsanStackPartialRedzoneMagic:
71 return Red();
72 case kAsanStackAfterReturnMagic:
73 return Magenta();
74 case kAsanInitializationOrderMagic:
75 return Cyan();
76 case kAsanUserPoisonedMemoryMagic:
77 return Blue();
78 case kAsanStackUseAfterScopeMagic:
79 return Magenta();
80 case kAsanGlobalRedzoneMagic:
81 return Red();
82 case kAsanInternalHeapMagic:
83 return Yellow();
84 default:
85 return Default();
86 }
87 }
88 const char *EndShadowByte() { return Default(); }
Kostya Serebryany58f54552012-12-18 07:32:16 +000089};
90
Alexey Samsonov98737922012-08-10 15:13:05 +000091// ---------------------- Helper functions ----------------------- {{{1
92
Kostya Serebryany9514a532012-12-19 09:53:32 +000093static void PrintShadowByte(const char *before, u8 byte,
94 const char *after = "\n") {
95 Decorator d;
96 Printf("%s%s%x%x%s%s", before,
97 d.ShadowByte(byte), byte >> 4, byte & 15, d.EndShadowByte(), after);
98}
99
100static void PrintShadowBytes(const char *before, u8 *bytes,
101 u8 *guilty, uptr n) {
102 Decorator d;
103 if (before)
104 Printf("%s%p:", before, bytes);
105 for (uptr i = 0; i < n; i++) {
106 u8 *p = bytes + i;
107 const char *before = p == guilty ? "[" :
Kostya Serebryany696902a2013-09-03 09:44:56 +0000108 (p - 1 == guilty && i != 0) ? "" : " ";
Kostya Serebryany9514a532012-12-19 09:53:32 +0000109 const char *after = p == guilty ? "]" : "";
110 PrintShadowByte(before, *p, after);
Alexey Samsonov98737922012-08-10 15:13:05 +0000111 }
Kostya Serebryany283c2962012-08-28 11:34:40 +0000112 Printf("\n");
Alexey Samsonov98737922012-08-10 15:13:05 +0000113}
114
Kostya Serebryany95f630a2013-01-28 07:34:22 +0000115static void PrintLegend() {
Kostya Serebryany9514a532012-12-19 09:53:32 +0000116 Printf("Shadow byte legend (one shadow byte represents %d "
117 "application bytes):\n", (int)SHADOW_GRANULARITY);
118 PrintShadowByte(" Addressable: ", 0);
119 Printf(" Partially addressable: ");
Timur Iskhodzhanov5e97ba32013-05-29 14:11:44 +0000120 for (u8 i = 1; i < SHADOW_GRANULARITY; i++)
Kostya Serebryany9514a532012-12-19 09:53:32 +0000121 PrintShadowByte("", i, " ");
122 Printf("\n");
123 PrintShadowByte(" Heap left redzone: ", kAsanHeapLeftRedzoneMagic);
Alexey Samsonovf3f2f5c2013-04-10 07:00:25 +0000124 PrintShadowByte(" Heap right redzone: ", kAsanHeapRightRedzoneMagic);
125 PrintShadowByte(" Freed heap region: ", kAsanHeapFreeMagic);
Kostya Serebryany9514a532012-12-19 09:53:32 +0000126 PrintShadowByte(" Stack left redzone: ", kAsanStackLeftRedzoneMagic);
127 PrintShadowByte(" Stack mid redzone: ", kAsanStackMidRedzoneMagic);
128 PrintShadowByte(" Stack right redzone: ", kAsanStackRightRedzoneMagic);
129 PrintShadowByte(" Stack partial redzone: ", kAsanStackPartialRedzoneMagic);
Kostya Serebryany9514a532012-12-19 09:53:32 +0000130 PrintShadowByte(" Stack after return: ", kAsanStackAfterReturnMagic);
131 PrintShadowByte(" Stack use after scope: ", kAsanStackUseAfterScopeMagic);
132 PrintShadowByte(" Global redzone: ", kAsanGlobalRedzoneMagic);
133 PrintShadowByte(" Global init order: ", kAsanInitializationOrderMagic);
134 PrintShadowByte(" Poisoned by user: ", kAsanUserPoisonedMemoryMagic);
135 PrintShadowByte(" ASan internal: ", kAsanInternalHeapMagic);
Alexey Samsonov98737922012-08-10 15:13:05 +0000136}
137
Kostya Serebryany95f630a2013-01-28 07:34:22 +0000138static void PrintShadowMemoryForAddress(uptr addr) {
139 if (!AddrIsInMem(addr))
140 return;
141 uptr shadow_addr = MemToShadow(addr);
142 const uptr n_bytes_per_row = 16;
143 uptr aligned_shadow = shadow_addr & ~(n_bytes_per_row - 1);
144 Printf("Shadow bytes around the buggy address:\n");
145 for (int i = -5; i <= 5; i++) {
146 const char *prefix = (i == 0) ? "=>" : " ";
147 PrintShadowBytes(prefix,
148 (u8*)(aligned_shadow + i * n_bytes_per_row),
149 (u8*)shadow_addr, n_bytes_per_row);
150 }
151 if (flags()->print_legend)
152 PrintLegend();
153}
154
Alexey Samsonov98737922012-08-10 15:13:05 +0000155static void PrintZoneForPointer(uptr ptr, uptr zone_ptr,
156 const char *zone_name) {
157 if (zone_ptr) {
158 if (zone_name) {
Kostya Serebryany283c2962012-08-28 11:34:40 +0000159 Printf("malloc_zone_from_ptr(%p) = %p, which is %s\n",
Alexey Samsonov98737922012-08-10 15:13:05 +0000160 ptr, zone_ptr, zone_name);
161 } else {
Kostya Serebryany283c2962012-08-28 11:34:40 +0000162 Printf("malloc_zone_from_ptr(%p) = %p, which doesn't have a name\n",
Alexey Samsonov98737922012-08-10 15:13:05 +0000163 ptr, zone_ptr);
164 }
165 } else {
Kostya Serebryany283c2962012-08-28 11:34:40 +0000166 Printf("malloc_zone_from_ptr(%p) = 0\n", ptr);
Alexey Samsonov98737922012-08-10 15:13:05 +0000167 }
168}
169
Timur Iskhodzhanov997454a2013-09-10 08:36:21 +0000170static void DescribeThread(AsanThread *t) {
171 if (t)
172 DescribeThread(t->context());
173}
174
Alexey Samsonove218beb2012-08-09 09:06:52 +0000175// ---------------------- Address Descriptions ------------------- {{{1
176
Alexey Samsonove4bfca22012-08-09 09:27:24 +0000177static bool IsASCII(unsigned char c) {
Alexey Samsonov98737922012-08-10 15:13:05 +0000178 return /*0x00 <= c &&*/ c <= 0x7F;
Alexey Samsonove4bfca22012-08-09 09:27:24 +0000179}
180
Alexey Samsonovc9424272013-03-27 10:41:22 +0000181static const char *MaybeDemangleGlobalName(const char *name) {
182 // We can spoil names of globals with C linkage, so use an heuristic
183 // approach to check if the name should be demangled.
Peter Collingbournec1a1ed62013-10-25 23:03:29 +0000184 return (name[0] == '_' && name[1] == 'Z')
185 ? Symbolizer::Get()->Demangle(name)
Alexey Samsonov22e21b02013-09-16 15:45:06 +0000186 : name;
Alexey Samsonovc9424272013-03-27 10:41:22 +0000187}
188
Alexey Samsonov939316c2013-04-01 08:57:38 +0000189// Check if the global is a zero-terminated ASCII string. If so, print it.
190static void PrintGlobalNameIfASCII(const __asan_global &g) {
191 for (uptr p = g.beg; p < g.beg + g.size - 1; p++) {
192 unsigned char c = *(unsigned char*)p;
193 if (c == '\0' || !IsASCII(c)) return;
194 }
195 if (*(char*)(g.beg + g.size - 1) != '\0') return;
196 Printf(" '%s' is ascii string '%s'\n",
197 MaybeDemangleGlobalName(g.name), (char*)g.beg);
198}
199
Evgeniy Stepanov589dcda2013-02-05 14:32:03 +0000200bool DescribeAddressRelativeToGlobal(uptr addr, uptr size,
201 const __asan_global &g) {
Kostya Serebryanya3b0e5e2013-01-23 11:14:21 +0000202 static const uptr kMinimalDistanceFromAnotherGlobal = 64;
203 if (addr <= g.beg - kMinimalDistanceFromAnotherGlobal) return false;
Alexey Samsonove4bfca22012-08-09 09:27:24 +0000204 if (addr >= g.beg + g.size_with_redzone) return false;
Kostya Serebryany58f54552012-12-18 07:32:16 +0000205 Decorator d;
206 Printf("%s", d.Location());
Alexey Samsonove4bfca22012-08-09 09:27:24 +0000207 if (addr < g.beg) {
Evgeniy Stepanov589dcda2013-02-05 14:32:03 +0000208 Printf("%p is located %zd bytes to the left", (void*)addr, g.beg - addr);
209 } else if (addr + size > g.beg + g.size) {
210 if (addr < g.beg + g.size)
211 addr = g.beg + g.size;
212 Printf("%p is located %zd bytes to the right", (void*)addr,
213 addr - (g.beg + g.size));
Alexey Samsonove4bfca22012-08-09 09:27:24 +0000214 } else {
Evgeniy Stepanov589dcda2013-02-05 14:32:03 +0000215 // Can it happen?
216 Printf("%p is located %zd bytes inside", (void*)addr, addr - g.beg);
Alexey Samsonove4bfca22012-08-09 09:27:24 +0000217 }
Kostya Serebryany60c9f442013-03-18 08:04:55 +0000218 Printf(" of global variable '%s' from '%s' (0x%zx) of size %zu\n",
Alexey Samsonovc9424272013-03-27 10:41:22 +0000219 MaybeDemangleGlobalName(g.name), g.module_name, g.beg, g.size);
Kostya Serebryany58f54552012-12-18 07:32:16 +0000220 Printf("%s", d.EndLocation());
Alexey Samsonove4bfca22012-08-09 09:27:24 +0000221 PrintGlobalNameIfASCII(g);
222 return true;
223}
224
Alexey Samsonove218beb2012-08-09 09:06:52 +0000225bool DescribeAddressIfShadow(uptr addr) {
226 if (AddrIsInMem(addr))
227 return false;
228 static const char kAddrInShadowReport[] =
229 "Address %p is located in the %s.\n";
230 if (AddrIsInShadowGap(addr)) {
Kostya Serebryany283c2962012-08-28 11:34:40 +0000231 Printf(kAddrInShadowReport, addr, "shadow gap area");
Alexey Samsonove218beb2012-08-09 09:06:52 +0000232 return true;
233 }
234 if (AddrIsInHighShadow(addr)) {
Kostya Serebryany283c2962012-08-28 11:34:40 +0000235 Printf(kAddrInShadowReport, addr, "high shadow area");
Alexey Samsonove218beb2012-08-09 09:06:52 +0000236 return true;
237 }
238 if (AddrIsInLowShadow(addr)) {
Kostya Serebryany283c2962012-08-28 11:34:40 +0000239 Printf(kAddrInShadowReport, addr, "low shadow area");
Alexey Samsonove218beb2012-08-09 09:06:52 +0000240 return true;
241 }
242 CHECK(0 && "Address is not in memory and not in shadow?");
243 return false;
244}
245
Kostya Serebryany50f3daa2013-03-22 10:36:24 +0000246// Return " (thread_name) " or an empty string if the name is empty.
247const char *ThreadNameWithParenthesis(AsanThreadContext *t, char buff[],
248 uptr buff_len) {
249 const char *name = t->name;
250 if (name[0] == '\0') return "";
251 buff[0] = 0;
252 internal_strncat(buff, " (", 3);
253 internal_strncat(buff, name, buff_len - 4);
254 internal_strncat(buff, ")", 2);
255 return buff;
256}
257
258const char *ThreadNameWithParenthesis(u32 tid, char buff[],
259 uptr buff_len) {
260 if (tid == kInvalidTid) return "";
261 asanThreadRegistry().CheckLocked();
262 AsanThreadContext *t = GetThreadContextByTidLocked(tid);
263 return ThreadNameWithParenthesis(t, buff, buff_len);
264}
265
Kostya Serebryanyedb39c72013-09-03 13:58:04 +0000266void PrintAccessAndVarIntersection(const char *var_name,
267 uptr var_beg, uptr var_size,
268 uptr addr, uptr access_size,
269 uptr prev_var_end, uptr next_var_beg) {
270 uptr var_end = var_beg + var_size;
271 uptr addr_end = addr + access_size;
272 const char *pos_descr = 0;
273 // If the variable [var_beg, var_end) is the nearest variable to the
274 // current memory access, indicate it in the log.
275 if (addr >= var_beg) {
276 if (addr_end <= var_end)
277 pos_descr = "is inside"; // May happen if this is a use-after-return.
278 else if (addr < var_end)
279 pos_descr = "partially overflows";
280 else if (addr_end <= next_var_beg &&
281 next_var_beg - addr_end >= addr - var_end)
282 pos_descr = "overflows";
283 } else {
284 if (addr_end > var_beg)
285 pos_descr = "partially underflows";
286 else if (addr >= prev_var_end &&
287 addr - prev_var_end >= var_beg - addr_end)
288 pos_descr = "underflows";
289 }
290 Printf(" [%zd, %zd) '%s'", var_beg, var_beg + var_size, var_name);
291 if (pos_descr) {
292 Decorator d;
293 // FIXME: we may want to also print the size of the access here,
294 // but in case of accesses generated by memset it may be confusing.
295 Printf("%s <== Memory access at offset %zd %s this variable%s\n",
296 d.Location(), addr, pos_descr, d.EndLocation());
297 } else {
298 Printf("\n");
299 }
300}
301
302struct StackVarDescr {
303 uptr beg;
304 uptr size;
305 const char *name_pos;
Kostya Serebryany89fe5642013-09-03 14:53:02 +0000306 uptr name_len;
Kostya Serebryanyedb39c72013-09-03 13:58:04 +0000307};
308
Alexey Samsonove218beb2012-08-09 09:06:52 +0000309bool DescribeAddressIfStack(uptr addr, uptr access_size) {
Alexey Samsonovdef1be92013-03-21 11:23:41 +0000310 AsanThread *t = FindThreadByStackAddress(addr);
Alexey Samsonove218beb2012-08-09 09:06:52 +0000311 if (!t) return false;
Kostya Serebryany89fe5642013-09-03 14:53:02 +0000312 const uptr kBufSize = 4095;
Alexey Samsonove218beb2012-08-09 09:06:52 +0000313 char buf[kBufSize];
314 uptr offset = 0;
Kostya Serebryany50f3daa2013-03-22 10:36:24 +0000315 uptr frame_pc = 0;
316 char tname[128];
317 const char *frame_descr = t->GetFrameNameByAddr(addr, &offset, &frame_pc);
Kostya Serebryanyd570bb42013-05-22 14:21:34 +0000318
319#ifdef __powerpc64__
320 // On PowerPC64, the address of a function actually points to a
321 // three-doubleword data structure with the first field containing
322 // the address of the function's code.
323 frame_pc = *reinterpret_cast<uptr *>(frame_pc);
324#endif
325
Alexey Samsonove218beb2012-08-09 09:06:52 +0000326 // This string is created by the compiler and has the following form:
Kostya Serebryany50f3daa2013-03-22 10:36:24 +0000327 // "n alloc_1 alloc_2 ... alloc_n"
Alexey Samsonove218beb2012-08-09 09:06:52 +0000328 // where alloc_i looks like "offset size len ObjectName ".
329 CHECK(frame_descr);
Kostya Serebryany58f54552012-12-18 07:32:16 +0000330 Decorator d;
331 Printf("%s", d.Location());
Kostya Serebryany50f3daa2013-03-22 10:36:24 +0000332 Printf("Address %p is located in stack of thread T%d%s "
333 "at offset %zu in frame\n",
334 addr, t->tid(),
335 ThreadNameWithParenthesis(t->tid(), tname, sizeof(tname)),
336 offset);
337 // Now we print the frame where the alloca has happened.
338 // We print this frame as a stack trace with one element.
339 // The symbolizer may print more than one frame if inlining was involved.
340 // The frame numbers may be different than those in the stack trace printed
341 // previously. That's unfortunate, but I have no better solution,
342 // especially given that the alloca may be from entirely different place
343 // (e.g. use-after-scope, or different thread's stack).
344 StackTrace alloca_stack;
345 alloca_stack.trace[0] = frame_pc + 16;
346 alloca_stack.size = 1;
Kostya Serebryany58f54552012-12-18 07:32:16 +0000347 Printf("%s", d.EndLocation());
Kostya Serebryany50f3daa2013-03-22 10:36:24 +0000348 PrintStack(&alloca_stack);
Alexey Samsonove218beb2012-08-09 09:06:52 +0000349 // Report the number of stack objects.
350 char *p;
Timur Iskhodzhanov22917e92013-09-03 15:09:21 +0000351 uptr n_objects = (uptr)internal_simple_strtoll(frame_descr, &p, 10);
Kostya Serebryanya27bdf72013-04-05 14:40:25 +0000352 CHECK_GT(n_objects, 0);
Kostya Serebryany283c2962012-08-28 11:34:40 +0000353 Printf(" This frame has %zu object(s):\n", n_objects);
Kostya Serebryanyedb39c72013-09-03 13:58:04 +0000354
Alexey Samsonove218beb2012-08-09 09:06:52 +0000355 // Report all objects in this frame.
Kostya Serebryanyedb39c72013-09-03 13:58:04 +0000356 InternalScopedBuffer<StackVarDescr> vars(n_objects);
Kostya Serebryany89fe5642013-09-03 14:53:02 +0000357 for (uptr i = 0; i < n_objects; i++) {
358 uptr beg, size;
359 uptr len;
360 beg = (uptr)internal_simple_strtoll(p, &p, 10);
361 size = (uptr)internal_simple_strtoll(p, &p, 10);
362 len = (uptr)internal_simple_strtoll(p, &p, 10);
363 if (beg == 0 || size == 0 || *p != ' ') {
Kostya Serebryany283c2962012-08-28 11:34:40 +0000364 Printf("AddressSanitizer can't parse the stack frame "
Alexey Samsonove218beb2012-08-09 09:06:52 +0000365 "descriptor: |%s|\n", frame_descr);
366 break;
367 }
368 p++;
Kostya Serebryanyedb39c72013-09-03 13:58:04 +0000369 vars[i].beg = beg;
370 vars[i].size = size;
371 vars[i].name_pos = p;
372 vars[i].name_len = len;
Alexey Samsonove218beb2012-08-09 09:06:52 +0000373 p += len;
Kostya Serebryanyedb39c72013-09-03 13:58:04 +0000374 }
Kostya Serebryany89fe5642013-09-03 14:53:02 +0000375 for (uptr i = 0; i < n_objects; i++) {
Kostya Serebryanyedb39c72013-09-03 13:58:04 +0000376 buf[0] = 0;
377 internal_strncat(buf, vars[i].name_pos,
378 static_cast<uptr>(Min(kBufSize, vars[i].name_len)));
379 uptr prev_var_end = i ? vars[i - 1].beg + vars[i - 1].size : 0;
Timur Iskhodzhanov22917e92013-09-03 15:09:21 +0000380 uptr next_var_beg = i + 1 < n_objects ? vars[i + 1].beg : ~(0UL);
Kostya Serebryanyedb39c72013-09-03 13:58:04 +0000381 PrintAccessAndVarIntersection(buf, vars[i].beg, vars[i].size,
382 offset, access_size,
383 prev_var_end, next_var_beg);
Alexey Samsonove218beb2012-08-09 09:06:52 +0000384 }
Kostya Serebryany283c2962012-08-28 11:34:40 +0000385 Printf("HINT: this may be a false positive if your program uses "
Alexey Samsonov08700282012-11-23 09:46:34 +0000386 "some custom stack unwind mechanism or swapcontext\n"
Alexey Samsonove218beb2012-08-09 09:06:52 +0000387 " (longjmp and C++ exceptions *are* supported)\n");
Timur Iskhodzhanov997454a2013-09-10 08:36:21 +0000388 DescribeThread(t);
Alexey Samsonove218beb2012-08-09 09:06:52 +0000389 return true;
390}
391
Alexey Samsonov5c153fa2012-09-18 07:38:10 +0000392static void DescribeAccessToHeapChunk(AsanChunkView chunk, uptr addr,
393 uptr access_size) {
Evgeniy Stepanov589dcda2013-02-05 14:32:03 +0000394 sptr offset;
Kostya Serebryany58f54552012-12-18 07:32:16 +0000395 Decorator d;
396 Printf("%s", d.Location());
Evgeniy Stepanov589dcda2013-02-05 14:32:03 +0000397 if (chunk.AddrIsAtLeft(addr, access_size, &offset)) {
398 Printf("%p is located %zd bytes to the left of", (void*)addr, offset);
Alexey Samsonov5c153fa2012-09-18 07:38:10 +0000399 } else if (chunk.AddrIsAtRight(addr, access_size, &offset)) {
Evgeniy Stepanov589dcda2013-02-05 14:32:03 +0000400 if (offset < 0) {
401 addr -= offset;
402 offset = 0;
403 }
404 Printf("%p is located %zd bytes to the right of", (void*)addr, offset);
405 } else if (chunk.AddrIsInside(addr, access_size, &offset)) {
406 Printf("%p is located %zd bytes inside of", (void*)addr, offset);
Alexey Samsonov5c153fa2012-09-18 07:38:10 +0000407 } else {
Evgeniy Stepanov589dcda2013-02-05 14:32:03 +0000408 Printf("%p is located somewhere around (this is AddressSanitizer bug!)",
409 (void*)addr);
Alexey Samsonov5c153fa2012-09-18 07:38:10 +0000410 }
411 Printf(" %zu-byte region [%p,%p)\n", chunk.UsedSize(),
412 (void*)(chunk.Beg()), (void*)(chunk.End()));
Kostya Serebryany58f54552012-12-18 07:32:16 +0000413 Printf("%s", d.EndLocation());
Alexey Samsonov5c153fa2012-09-18 07:38:10 +0000414}
415
416void DescribeHeapAddress(uptr addr, uptr access_size) {
417 AsanChunkView chunk = FindHeapChunkByAddress(addr);
Alexey Samsonovd9def292013-10-14 11:13:54 +0000418 if (!chunk.IsValid()) {
419 Printf("AddressSanitizer can not describe address in more detail "
420 "(wild memory access suspected).\n");
421 return;
422 }
Alexey Samsonov5c153fa2012-09-18 07:38:10 +0000423 DescribeAccessToHeapChunk(chunk, addr, access_size);
424 CHECK(chunk.AllocTid() != kInvalidTid);
Alexey Samsonovdef1be92013-03-21 11:23:41 +0000425 asanThreadRegistry().CheckLocked();
426 AsanThreadContext *alloc_thread =
427 GetThreadContextByTidLocked(chunk.AllocTid());
Alexey Samsonov5c153fa2012-09-18 07:38:10 +0000428 StackTrace alloc_stack;
429 chunk.GetAllocStack(&alloc_stack);
Kostya Serebryany716e2f22012-12-07 15:15:01 +0000430 char tname[128];
Kostya Serebryany58f54552012-12-18 07:32:16 +0000431 Decorator d;
Timur Iskhodzhanov997454a2013-09-10 08:36:21 +0000432 AsanThreadContext *free_thread = 0;
Alexey Samsonov5c153fa2012-09-18 07:38:10 +0000433 if (chunk.FreeTid() != kInvalidTid) {
Timur Iskhodzhanov997454a2013-09-10 08:36:21 +0000434 free_thread = GetThreadContextByTidLocked(chunk.FreeTid());
Kostya Serebryany58f54552012-12-18 07:32:16 +0000435 Printf("%sfreed by thread T%d%s here:%s\n", d.Allocation(),
Alexey Samsonovdef1be92013-03-21 11:23:41 +0000436 free_thread->tid,
Kostya Serebryany58f54552012-12-18 07:32:16 +0000437 ThreadNameWithParenthesis(free_thread, tname, sizeof(tname)),
438 d.EndAllocation());
Alexey Samsonov5c153fa2012-09-18 07:38:10 +0000439 StackTrace free_stack;
440 chunk.GetFreeStack(&free_stack);
441 PrintStack(&free_stack);
Kostya Serebryany58f54552012-12-18 07:32:16 +0000442 Printf("%spreviously allocated by thread T%d%s here:%s\n",
Alexey Samsonovdef1be92013-03-21 11:23:41 +0000443 d.Allocation(), alloc_thread->tid,
Kostya Serebryany58f54552012-12-18 07:32:16 +0000444 ThreadNameWithParenthesis(alloc_thread, tname, sizeof(tname)),
445 d.EndAllocation());
Alexey Samsonov5c153fa2012-09-18 07:38:10 +0000446 } else {
Kostya Serebryany58f54552012-12-18 07:32:16 +0000447 Printf("%sallocated by thread T%d%s here:%s\n", d.Allocation(),
Alexey Samsonovdef1be92013-03-21 11:23:41 +0000448 alloc_thread->tid,
Kostya Serebryany58f54552012-12-18 07:32:16 +0000449 ThreadNameWithParenthesis(alloc_thread, tname, sizeof(tname)),
450 d.EndAllocation());
Alexey Samsonov5c153fa2012-09-18 07:38:10 +0000451 }
Timur Iskhodzhanov997454a2013-09-10 08:36:21 +0000452 PrintStack(&alloc_stack);
453 DescribeThread(GetCurrentThread());
454 if (free_thread)
455 DescribeThread(free_thread);
456 DescribeThread(alloc_thread);
Alexey Samsonov5c153fa2012-09-18 07:38:10 +0000457}
458
Alexey Samsonove218beb2012-08-09 09:06:52 +0000459void DescribeAddress(uptr addr, uptr access_size) {
460 // Check if this is shadow or shadow gap.
461 if (DescribeAddressIfShadow(addr))
462 return;
463 CHECK(AddrIsInMem(addr));
Evgeniy Stepanov589dcda2013-02-05 14:32:03 +0000464 if (DescribeAddressIfGlobal(addr, access_size))
Alexey Samsonove218beb2012-08-09 09:06:52 +0000465 return;
466 if (DescribeAddressIfStack(addr, access_size))
467 return;
468 // Assume it is a heap address.
469 DescribeHeapAddress(addr, access_size);
470}
471
Alexey Samsonov71b42c92012-09-05 07:37:15 +0000472// ------------------- Thread description -------------------- {{{1
473
Alexey Samsonovdef1be92013-03-21 11:23:41 +0000474void DescribeThread(AsanThreadContext *context) {
475 CHECK(context);
476 asanThreadRegistry().CheckLocked();
Alexey Samsonov71b42c92012-09-05 07:37:15 +0000477 // No need to announce the main thread.
Alexey Samsonovdef1be92013-03-21 11:23:41 +0000478 if (context->tid == 0 || context->announced) {
Alexey Samsonov71b42c92012-09-05 07:37:15 +0000479 return;
480 }
Alexey Samsonovdef1be92013-03-21 11:23:41 +0000481 context->announced = true;
Kostya Serebryany716e2f22012-12-07 15:15:01 +0000482 char tname[128];
Alexey Samsonovdef1be92013-03-21 11:23:41 +0000483 Printf("Thread T%d%s", context->tid,
484 ThreadNameWithParenthesis(context->tid, tname, sizeof(tname)));
Kostya Serebryany716e2f22012-12-07 15:15:01 +0000485 Printf(" created by T%d%s here:\n",
Alexey Samsonovdef1be92013-03-21 11:23:41 +0000486 context->parent_tid,
487 ThreadNameWithParenthesis(context->parent_tid,
Kostya Serebryany716e2f22012-12-07 15:15:01 +0000488 tname, sizeof(tname)));
Kostya Serebryany6d958692013-10-18 14:50:44 +0000489 uptr stack_size;
490 const uptr *stack_trace = StackDepotGet(context->stack_id, &stack_size);
491 PrintStack(stack_trace, stack_size);
Alexey Samsonov71b42c92012-09-05 07:37:15 +0000492 // Recursively described parent thread if needed.
493 if (flags()->print_full_thread_history) {
Alexey Samsonovdef1be92013-03-21 11:23:41 +0000494 AsanThreadContext *parent_context =
495 GetThreadContextByTidLocked(context->parent_tid);
496 DescribeThread(parent_context);
Alexey Samsonov71b42c92012-09-05 07:37:15 +0000497 }
498}
499
Alexey Samsonove218beb2012-08-09 09:06:52 +0000500// -------------------- Different kinds of reports ----------------- {{{1
501
Alexey Samsonov98737922012-08-10 15:13:05 +0000502// Use ScopedInErrorReport to run common actions just before and
503// immediately after printing error report.
504class ScopedInErrorReport {
505 public:
506 ScopedInErrorReport() {
507 static atomic_uint32_t num_calls;
Alexey Samsonov62e27092012-09-17 08:02:19 +0000508 static u32 reporting_thread_tid;
Alexey Samsonov98737922012-08-10 15:13:05 +0000509 if (atomic_fetch_add(&num_calls, 1, memory_order_relaxed) != 0) {
510 // Do not print more than one report, otherwise they will mix up.
511 // Error reporting functions shouldn't return at this situation, as
512 // they are defined as no-return.
Kostya Serebryany283c2962012-08-28 11:34:40 +0000513 Report("AddressSanitizer: while reporting a bug found another one."
Alexey Samsonov98737922012-08-10 15:13:05 +0000514 "Ignoring.\n");
Alexey Samsonov89c13842013-03-20 09:23:28 +0000515 u32 current_tid = GetCurrentTidOrInvalid();
Alexey Samsonov62e27092012-09-17 08:02:19 +0000516 if (current_tid != reporting_thread_tid) {
517 // ASan found two bugs in different threads simultaneously. Sleep
518 // long enough to make sure that the thread which started to print
519 // an error report will finish doing it.
520 SleepForSeconds(Max(100, flags()->sleep_before_dying + 1));
521 }
Alexey Samsonovf8822472013-02-20 13:54:32 +0000522 // If we're still not dead for some reason, use raw _exit() instead of
Alexey Samsonov031633b2012-11-19 11:22:22 +0000523 // Die() to bypass any additional checks.
Alexey Samsonovf8822472013-02-20 13:54:32 +0000524 internal__exit(flags()->exitcode);
Alexey Samsonov98737922012-08-10 15:13:05 +0000525 }
Alexey Samsonov6a08d292012-12-07 22:01:28 +0000526 ASAN_ON_ERROR();
Alexey Samsonov7ed46ff2013-04-05 07:30:29 +0000527 // Make sure the registry and sanitizer report mutexes are locked while
528 // we're printing an error report.
529 // We can lock them only here to avoid self-deadlock in case of
Alexey Samsonovdef1be92013-03-21 11:23:41 +0000530 // recursive reports.
531 asanThreadRegistry().Lock();
Alexey Samsonov7ed46ff2013-04-05 07:30:29 +0000532 CommonSanitizerReportMutex.Lock();
Alexey Samsonov89c13842013-03-20 09:23:28 +0000533 reporting_thread_tid = GetCurrentTidOrInvalid();
Kostya Serebryany283c2962012-08-28 11:34:40 +0000534 Printf("===================================================="
Alexey Samsonov62e27092012-09-17 08:02:19 +0000535 "=============\n");
Alexey Samsonov98737922012-08-10 15:13:05 +0000536 }
537 // Destructor is NORETURN, as functions that report errors are.
538 NORETURN ~ScopedInErrorReport() {
539 // Make sure the current thread is announced.
Timur Iskhodzhanov997454a2013-09-10 08:36:21 +0000540 DescribeThread(GetCurrentThread());
Alexey Samsonov98737922012-08-10 15:13:05 +0000541 // Print memory stats.
Kostya Serebryany95f630a2013-01-28 07:34:22 +0000542 if (flags()->print_stats)
543 __asan_print_accumulated_stats();
Alexey Samsonov98737922012-08-10 15:13:05 +0000544 if (error_report_callback) {
545 error_report_callback(error_message_buffer);
546 }
Kostya Serebryany283c2962012-08-28 11:34:40 +0000547 Report("ABORTING\n");
Alexey Samsonov98737922012-08-10 15:13:05 +0000548 Die();
549 }
550};
551
Kostya Serebryany2673fd82013-02-06 12:36:49 +0000552static void ReportSummary(const char *error_type, StackTrace *stack) {
Nick Lewycky4b861aa2013-10-23 06:19:04 +0000553 AddressInfo ai;
Timur Iskhodzhanove38046a2013-10-27 13:37:23 +0000554 // FIXME: The symbolizer interface should be changed in order to support
555 // Windows where we don't have a symbolizer.
556 if (!SANITIZER_WINDOWS && Symbolizer::Get()->IsAvailable()) {
Kostya Serebryany2673fd82013-02-06 12:36:49 +0000557 // Currently, we include the first stack frame into the report summary.
558 // Maybe sometimes we need to choose another frame (e.g. skip memcpy/etc).
Alexey Samsonov22c1c182013-04-11 11:45:04 +0000559 uptr pc = StackTrace::GetPreviousInstructionPc(stack->trace[0]);
Peter Collingbournec1a1ed62013-10-25 23:03:29 +0000560 Symbolizer::Get()->SymbolizeCode(pc, &ai, 1);
Kostya Serebryany2673fd82013-02-06 12:36:49 +0000561 }
Nick Lewycky4b861aa2013-10-23 06:19:04 +0000562 ReportErrorSummary(error_type, ai.file, ai.line, ai.function);
Kostya Serebryany2673fd82013-02-06 12:36:49 +0000563}
564
Alexey Samsonov73545092012-08-09 07:40:58 +0000565void ReportSIGSEGV(uptr pc, uptr sp, uptr bp, uptr addr) {
Alexey Samsonov98737922012-08-10 15:13:05 +0000566 ScopedInErrorReport in_report;
Kostya Serebryany58f54552012-12-18 07:32:16 +0000567 Decorator d;
568 Printf("%s", d.Warning());
Kostya Serebryany69d8ede2012-10-15 13:04:58 +0000569 Report("ERROR: AddressSanitizer: SEGV on unknown address %p"
Alexey Samsonov73545092012-08-09 07:40:58 +0000570 " (pc %p sp %p bp %p T%d)\n",
571 (void*)addr, (void*)pc, (void*)sp, (void*)bp,
Alexey Samsonov89c13842013-03-20 09:23:28 +0000572 GetCurrentTidOrInvalid());
Kostya Serebryany58f54552012-12-18 07:32:16 +0000573 Printf("%s", d.EndWarning());
Kostya Serebryanya30c8f92012-12-13 09:34:23 +0000574 GET_STACK_TRACE_FATAL(pc, bp);
Kostya Serebryanycc347222012-08-28 13:49:49 +0000575 PrintStack(&stack);
Alexey Samsonovd9def292013-10-14 11:13:54 +0000576 Printf("AddressSanitizer can not provide additional info.\n");
Kostya Serebryany2673fd82013-02-06 12:36:49 +0000577 ReportSummary("SEGV", &stack);
Alexey Samsonov73545092012-08-09 07:40:58 +0000578}
579
Kostya Serebryanyc3390df2012-08-28 11:54:30 +0000580void ReportDoubleFree(uptr addr, StackTrace *stack) {
Alexey Samsonov98737922012-08-10 15:13:05 +0000581 ScopedInErrorReport in_report;
Kostya Serebryany58f54552012-12-18 07:32:16 +0000582 Decorator d;
583 Printf("%s", d.Warning());
Kostya Serebryanya89a35a2013-03-26 08:01:37 +0000584 char tname[128];
585 u32 curr_tid = GetCurrentTidOrInvalid();
586 Report("ERROR: AddressSanitizer: attempting double-free on %p in "
587 "thread T%d%s:\n",
588 addr, curr_tid,
589 ThreadNameWithParenthesis(curr_tid, tname, sizeof(tname)));
590
Kostya Serebryany58f54552012-12-18 07:32:16 +0000591 Printf("%s", d.EndWarning());
Kostya Serebryanycc347222012-08-28 13:49:49 +0000592 PrintStack(stack);
Alexey Samsonovf7c1d182012-08-09 08:15:46 +0000593 DescribeHeapAddress(addr, 1);
Kostya Serebryany2673fd82013-02-06 12:36:49 +0000594 ReportSummary("double-free", stack);
Alexey Samsonovf7c1d182012-08-09 08:15:46 +0000595}
596
Kostya Serebryanyc3390df2012-08-28 11:54:30 +0000597void ReportFreeNotMalloced(uptr addr, StackTrace *stack) {
Alexey Samsonov98737922012-08-10 15:13:05 +0000598 ScopedInErrorReport in_report;
Kostya Serebryany58f54552012-12-18 07:32:16 +0000599 Decorator d;
600 Printf("%s", d.Warning());
Kostya Serebryanya89a35a2013-03-26 08:01:37 +0000601 char tname[128];
602 u32 curr_tid = GetCurrentTidOrInvalid();
Kostya Serebryany69d8ede2012-10-15 13:04:58 +0000603 Report("ERROR: AddressSanitizer: attempting free on address "
Kostya Serebryanya89a35a2013-03-26 08:01:37 +0000604 "which was not malloc()-ed: %p in thread T%d%s\n", addr,
605 curr_tid, ThreadNameWithParenthesis(curr_tid, tname, sizeof(tname)));
Kostya Serebryany58f54552012-12-18 07:32:16 +0000606 Printf("%s", d.EndWarning());
Kostya Serebryanycc347222012-08-28 13:49:49 +0000607 PrintStack(stack);
Alexey Samsonov98737922012-08-10 15:13:05 +0000608 DescribeHeapAddress(addr, 1);
Kostya Serebryany2673fd82013-02-06 12:36:49 +0000609 ReportSummary("bad-free", stack);
Alexey Samsonovf7c1d182012-08-09 08:15:46 +0000610}
611
Kostya Serebryanyfe6d9162012-12-21 08:53:59 +0000612void ReportAllocTypeMismatch(uptr addr, StackTrace *stack,
613 AllocType alloc_type,
614 AllocType dealloc_type) {
615 static const char *alloc_names[] =
616 {"INVALID", "malloc", "operator new", "operator new []"};
617 static const char *dealloc_names[] =
618 {"INVALID", "free", "operator delete", "operator delete []"};
619 CHECK_NE(alloc_type, dealloc_type);
620 ScopedInErrorReport in_report;
621 Decorator d;
622 Printf("%s", d.Warning());
623 Report("ERROR: AddressSanitizer: alloc-dealloc-mismatch (%s vs %s) on %p\n",
624 alloc_names[alloc_type], dealloc_names[dealloc_type], addr);
625 Printf("%s", d.EndWarning());
626 PrintStack(stack);
627 DescribeHeapAddress(addr, 1);
Kostya Serebryany2673fd82013-02-06 12:36:49 +0000628 ReportSummary("alloc-dealloc-mismatch", stack);
Kostya Serebryanyfe6d9162012-12-21 08:53:59 +0000629 Report("HINT: if you don't care about these warnings you may set "
630 "ASAN_OPTIONS=alloc_dealloc_mismatch=0\n");
631}
632
Kostya Serebryanyc3390df2012-08-28 11:54:30 +0000633void ReportMallocUsableSizeNotOwned(uptr addr, StackTrace *stack) {
Alexey Samsonov98737922012-08-10 15:13:05 +0000634 ScopedInErrorReport in_report;
Kostya Serebryany58f54552012-12-18 07:32:16 +0000635 Decorator d;
636 Printf("%s", d.Warning());
Kostya Serebryany69d8ede2012-10-15 13:04:58 +0000637 Report("ERROR: AddressSanitizer: attempting to call "
Alexey Samsonovf7c1d182012-08-09 08:15:46 +0000638 "malloc_usable_size() for pointer which is "
639 "not owned: %p\n", addr);
Kostya Serebryany58f54552012-12-18 07:32:16 +0000640 Printf("%s", d.EndWarning());
Kostya Serebryanycc347222012-08-28 13:49:49 +0000641 PrintStack(stack);
Alexey Samsonovf7c1d182012-08-09 08:15:46 +0000642 DescribeHeapAddress(addr, 1);
Kostya Serebryany2673fd82013-02-06 12:36:49 +0000643 ReportSummary("bad-malloc_usable_size", stack);
Alexey Samsonovf7c1d182012-08-09 08:15:46 +0000644}
645
Kostya Serebryanyc3390df2012-08-28 11:54:30 +0000646void ReportAsanGetAllocatedSizeNotOwned(uptr addr, StackTrace *stack) {
Alexey Samsonov98737922012-08-10 15:13:05 +0000647 ScopedInErrorReport in_report;
Kostya Serebryany58f54552012-12-18 07:32:16 +0000648 Decorator d;
649 Printf("%s", d.Warning());
Kostya Serebryany69d8ede2012-10-15 13:04:58 +0000650 Report("ERROR: AddressSanitizer: attempting to call "
Alexey Samsonovf7c1d182012-08-09 08:15:46 +0000651 "__asan_get_allocated_size() for pointer which is "
652 "not owned: %p\n", addr);
Kostya Serebryany58f54552012-12-18 07:32:16 +0000653 Printf("%s", d.EndWarning());
Kostya Serebryanycc347222012-08-28 13:49:49 +0000654 PrintStack(stack);
Alexey Samsonovf7c1d182012-08-09 08:15:46 +0000655 DescribeHeapAddress(addr, 1);
Kostya Serebryany2673fd82013-02-06 12:36:49 +0000656 ReportSummary("bad-__asan_get_allocated_size", stack);
Alexey Samsonovf7c1d182012-08-09 08:15:46 +0000657}
658
Alexey Samsonov487fee72012-08-09 08:32:33 +0000659void ReportStringFunctionMemoryRangesOverlap(
660 const char *function, const char *offset1, uptr length1,
Kostya Serebryanyc3390df2012-08-28 11:54:30 +0000661 const char *offset2, uptr length2, StackTrace *stack) {
Alexey Samsonov98737922012-08-10 15:13:05 +0000662 ScopedInErrorReport in_report;
Kostya Serebryany58f54552012-12-18 07:32:16 +0000663 Decorator d;
Kostya Serebryany2673fd82013-02-06 12:36:49 +0000664 char bug_type[100];
665 internal_snprintf(bug_type, sizeof(bug_type), "%s-param-overlap", function);
Kostya Serebryany58f54552012-12-18 07:32:16 +0000666 Printf("%s", d.Warning());
Kostya Serebryany2673fd82013-02-06 12:36:49 +0000667 Report("ERROR: AddressSanitizer: %s: "
Alexey Samsonov487fee72012-08-09 08:32:33 +0000668 "memory ranges [%p,%p) and [%p, %p) overlap\n", \
Kostya Serebryany2673fd82013-02-06 12:36:49 +0000669 bug_type, offset1, offset1 + length1, offset2, offset2 + length2);
Kostya Serebryany58f54552012-12-18 07:32:16 +0000670 Printf("%s", d.EndWarning());
Kostya Serebryanycc347222012-08-28 13:49:49 +0000671 PrintStack(stack);
Alexey Samsonov98737922012-08-10 15:13:05 +0000672 DescribeAddress((uptr)offset1, length1);
673 DescribeAddress((uptr)offset2, length2);
Kostya Serebryany2673fd82013-02-06 12:36:49 +0000674 ReportSummary(bug_type, stack);
Alexey Samsonov487fee72012-08-09 08:32:33 +0000675}
Alexey Samsonovf7c1d182012-08-09 08:15:46 +0000676
Alexey Samsonov663c5012012-08-09 12:15:40 +0000677// ----------------------- Mac-specific reports ----------------- {{{1
678
Alexey Samsonov663c5012012-08-09 12:15:40 +0000679void WarnMacFreeUnallocated(
Kostya Serebryanyc3390df2012-08-28 11:54:30 +0000680 uptr addr, uptr zone_ptr, const char *zone_name, StackTrace *stack) {
Alexey Samsonov98737922012-08-10 15:13:05 +0000681 // Just print a warning here.
Kostya Serebryany283c2962012-08-28 11:34:40 +0000682 Printf("free_common(%p) -- attempting to free unallocated memory.\n"
Alexey Samsonov663c5012012-08-09 12:15:40 +0000683 "AddressSanitizer is ignoring this error on Mac OS now.\n",
684 addr);
685 PrintZoneForPointer(addr, zone_ptr, zone_name);
Kostya Serebryanycc347222012-08-28 13:49:49 +0000686 PrintStack(stack);
Alexey Samsonov98737922012-08-10 15:13:05 +0000687 DescribeHeapAddress(addr, 1);
Alexey Samsonov663c5012012-08-09 12:15:40 +0000688}
689
690void ReportMacMzReallocUnknown(
Kostya Serebryanyc3390df2012-08-28 11:54:30 +0000691 uptr addr, uptr zone_ptr, const char *zone_name, StackTrace *stack) {
Alexey Samsonov98737922012-08-10 15:13:05 +0000692 ScopedInErrorReport in_report;
Kostya Serebryany283c2962012-08-28 11:34:40 +0000693 Printf("mz_realloc(%p) -- attempting to realloc unallocated memory.\n"
Alexey Samsonov663c5012012-08-09 12:15:40 +0000694 "This is an unrecoverable problem, exiting now.\n",
695 addr);
696 PrintZoneForPointer(addr, zone_ptr, zone_name);
Kostya Serebryanycc347222012-08-28 13:49:49 +0000697 PrintStack(stack);
Alexey Samsonov98737922012-08-10 15:13:05 +0000698 DescribeHeapAddress(addr, 1);
Alexey Samsonov663c5012012-08-09 12:15:40 +0000699}
700
701void ReportMacCfReallocUnknown(
Kostya Serebryanyc3390df2012-08-28 11:54:30 +0000702 uptr addr, uptr zone_ptr, const char *zone_name, StackTrace *stack) {
Alexey Samsonov98737922012-08-10 15:13:05 +0000703 ScopedInErrorReport in_report;
Kostya Serebryany283c2962012-08-28 11:34:40 +0000704 Printf("cf_realloc(%p) -- attempting to realloc unallocated memory.\n"
Alexey Samsonov663c5012012-08-09 12:15:40 +0000705 "This is an unrecoverable problem, exiting now.\n",
706 addr);
707 PrintZoneForPointer(addr, zone_ptr, zone_name);
Kostya Serebryanycc347222012-08-28 13:49:49 +0000708 PrintStack(stack);
Alexey Samsonov98737922012-08-10 15:13:05 +0000709 DescribeHeapAddress(addr, 1);
Alexey Samsonovc98570b2012-08-09 10:56:57 +0000710}
711
Alexey Samsonov812ff902012-08-09 11:29:13 +0000712} // namespace __asan
713
714// --------------------------- Interface --------------------- {{{1
715using namespace __asan; // NOLINT
716
717void __asan_report_error(uptr pc, uptr bp, uptr sp,
718 uptr addr, bool is_write, uptr access_size) {
Alexey Samsonov98737922012-08-10 15:13:05 +0000719 ScopedInErrorReport in_report;
Alexey Samsonovc98570b2012-08-09 10:56:57 +0000720
Alexey Samsonov98737922012-08-10 15:13:05 +0000721 // Determine the error type.
Alexey Samsonovc98570b2012-08-09 10:56:57 +0000722 const char *bug_descr = "unknown-crash";
723 if (AddrIsInMem(addr)) {
724 u8 *shadow_addr = (u8*)MemToShadow(addr);
725 // If we are accessing 16 bytes, look at the second shadow byte.
726 if (*shadow_addr == 0 && access_size > SHADOW_GRANULARITY)
727 shadow_addr++;
728 // If we are in the partial right redzone, look at the next shadow byte.
729 if (*shadow_addr > 0 && *shadow_addr < 128)
730 shadow_addr++;
731 switch (*shadow_addr) {
732 case kAsanHeapLeftRedzoneMagic:
733 case kAsanHeapRightRedzoneMagic:
734 bug_descr = "heap-buffer-overflow";
735 break;
736 case kAsanHeapFreeMagic:
737 bug_descr = "heap-use-after-free";
738 break;
739 case kAsanStackLeftRedzoneMagic:
740 bug_descr = "stack-buffer-underflow";
741 break;
Kostya Serebryany3945c582012-08-21 14:10:25 +0000742 case kAsanInitializationOrderMagic:
743 bug_descr = "initialization-order-fiasco";
744 break;
Alexey Samsonovc98570b2012-08-09 10:56:57 +0000745 case kAsanStackMidRedzoneMagic:
746 case kAsanStackRightRedzoneMagic:
747 case kAsanStackPartialRedzoneMagic:
748 bug_descr = "stack-buffer-overflow";
749 break;
750 case kAsanStackAfterReturnMagic:
751 bug_descr = "stack-use-after-return";
752 break;
753 case kAsanUserPoisonedMemoryMagic:
754 bug_descr = "use-after-poison";
755 break;
Alexey Samsonovd4b5db82012-12-04 01:38:15 +0000756 case kAsanStackUseAfterScopeMagic:
757 bug_descr = "stack-use-after-scope";
758 break;
Alexey Samsonovc98570b2012-08-09 10:56:57 +0000759 case kAsanGlobalRedzoneMagic:
760 bug_descr = "global-buffer-overflow";
761 break;
762 }
763 }
Kostya Serebryany58f54552012-12-18 07:32:16 +0000764 Decorator d;
765 Printf("%s", d.Warning());
Kostya Serebryany69d8ede2012-10-15 13:04:58 +0000766 Report("ERROR: AddressSanitizer: %s on address "
Alexey Samsonovc98570b2012-08-09 10:56:57 +0000767 "%p at pc 0x%zx bp 0x%zx sp 0x%zx\n",
768 bug_descr, (void*)addr, pc, bp, sp);
Kostya Serebryany58f54552012-12-18 07:32:16 +0000769 Printf("%s", d.EndWarning());
Alexey Samsonovc98570b2012-08-09 10:56:57 +0000770
Alexey Samsonov89c13842013-03-20 09:23:28 +0000771 u32 curr_tid = GetCurrentTidOrInvalid();
Kostya Serebryany716e2f22012-12-07 15:15:01 +0000772 char tname[128];
Kostya Serebryany58f54552012-12-18 07:32:16 +0000773 Printf("%s%s of size %zu at %p thread T%d%s%s\n",
774 d.Access(),
775 access_size ? (is_write ? "WRITE" : "READ") : "ACCESS",
776 access_size, (void*)addr, curr_tid,
777 ThreadNameWithParenthesis(curr_tid, tname, sizeof(tname)),
778 d.EndAccess());
Alexey Samsonovc98570b2012-08-09 10:56:57 +0000779
Kostya Serebryanya30c8f92012-12-13 09:34:23 +0000780 GET_STACK_TRACE_FATAL(pc, bp);
Kostya Serebryanycc347222012-08-28 13:49:49 +0000781 PrintStack(&stack);
Alexey Samsonovc98570b2012-08-09 10:56:57 +0000782
783 DescribeAddress(addr, access_size);
Kostya Serebryany2673fd82013-02-06 12:36:49 +0000784 ReportSummary(bug_descr, &stack);
Alexey Samsonov98737922012-08-10 15:13:05 +0000785 PrintShadowMemoryForAddress(addr);
Alexey Samsonovc98570b2012-08-09 10:56:57 +0000786}
787
Alexey Samsonovc98570b2012-08-09 10:56:57 +0000788void NOINLINE __asan_set_error_report_callback(void (*callback)(const char*)) {
789 error_report_callback = callback;
790 if (callback) {
791 error_message_buffer_size = 1 << 16;
792 error_message_buffer =
793 (char*)MmapOrDie(error_message_buffer_size, __FUNCTION__);
794 error_message_buffer_pos = 0;
795 }
796}
Alexey Samsonovf657a192012-08-13 11:23:40 +0000797
Kostya Serebryany17a7c672012-12-29 10:18:31 +0000798void __asan_describe_address(uptr addr) {
799 DescribeAddress(addr, 1);
800}
801
Alexey Samsonov6a08d292012-12-07 22:01:28 +0000802#if !SANITIZER_SUPPORTS_WEAK_HOOKS
Alexey Samsonov86633432012-10-02 14:06:39 +0000803// Provide default implementation of __asan_on_error that does nothing
804// and may be overriden by user.
Timur Iskhodzhanov3c80c6c2013-08-13 11:42:45 +0000805SANITIZER_INTERFACE_ATTRIBUTE SANITIZER_WEAK_ATTRIBUTE NOINLINE
Alexey Samsonov86633432012-10-02 14:06:39 +0000806void __asan_on_error() {}
Alexey Samsonov6a08d292012-12-07 22:01:28 +0000807#endif