blob: aeeebf452ca880a354eb441cbd63cac93899307a [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
Kostya Serebryany9514a532012-12-19 09:53:32 +000047bool PrintsToTtyCached() {
48 static int cached = 0;
49 static bool prints_to_tty;
50 if (!cached) { // Ok wrt threads since we are printing only from one thread.
51 prints_to_tty = PrintsToTty();
52 cached = 1;
53 }
54 return prints_to_tty;
55}
Kostya Serebryany58f54552012-12-18 07:32:16 +000056class Decorator: private __sanitizer::AnsiColorDecorator {
57 public:
Kostya Serebryany9514a532012-12-19 09:53:32 +000058 Decorator() : __sanitizer::AnsiColorDecorator(PrintsToTtyCached()) { }
Kostya Serebryany58f54552012-12-18 07:32:16 +000059 const char *Warning() { return Red(); }
60 const char *EndWarning() { return Default(); }
61 const char *Access() { return Blue(); }
62 const char *EndAccess() { return Default(); }
63 const char *Location() { return Green(); }
64 const char *EndLocation() { return Default(); }
65 const char *Allocation() { return Magenta(); }
66 const char *EndAllocation() { return Default(); }
Kostya Serebryany9514a532012-12-19 09:53:32 +000067
68 const char *ShadowByte(u8 byte) {
69 switch (byte) {
70 case kAsanHeapLeftRedzoneMagic:
71 case kAsanHeapRightRedzoneMagic:
72 return Red();
73 case kAsanHeapFreeMagic:
74 return Magenta();
75 case kAsanStackLeftRedzoneMagic:
76 case kAsanStackMidRedzoneMagic:
77 case kAsanStackRightRedzoneMagic:
78 case kAsanStackPartialRedzoneMagic:
79 return Red();
80 case kAsanStackAfterReturnMagic:
81 return Magenta();
82 case kAsanInitializationOrderMagic:
83 return Cyan();
84 case kAsanUserPoisonedMemoryMagic:
85 return Blue();
86 case kAsanStackUseAfterScopeMagic:
87 return Magenta();
88 case kAsanGlobalRedzoneMagic:
89 return Red();
90 case kAsanInternalHeapMagic:
91 return Yellow();
92 default:
93 return Default();
94 }
95 }
96 const char *EndShadowByte() { return Default(); }
Kostya Serebryany58f54552012-12-18 07:32:16 +000097};
98
Alexey Samsonov98737922012-08-10 15:13:05 +000099// ---------------------- Helper functions ----------------------- {{{1
100
Kostya Serebryany9514a532012-12-19 09:53:32 +0000101static void PrintShadowByte(const char *before, u8 byte,
102 const char *after = "\n") {
103 Decorator d;
104 Printf("%s%s%x%x%s%s", before,
105 d.ShadowByte(byte), byte >> 4, byte & 15, d.EndShadowByte(), after);
106}
107
108static void PrintShadowBytes(const char *before, u8 *bytes,
109 u8 *guilty, uptr n) {
110 Decorator d;
111 if (before)
112 Printf("%s%p:", before, bytes);
113 for (uptr i = 0; i < n; i++) {
114 u8 *p = bytes + i;
115 const char *before = p == guilty ? "[" :
116 p - 1 == guilty ? "" : " ";
117 const char *after = p == guilty ? "]" : "";
118 PrintShadowByte(before, *p, after);
Alexey Samsonov98737922012-08-10 15:13:05 +0000119 }
Kostya Serebryany283c2962012-08-28 11:34:40 +0000120 Printf("\n");
Alexey Samsonov98737922012-08-10 15:13:05 +0000121}
122
Kostya Serebryany95f630a2013-01-28 07:34:22 +0000123static void PrintLegend() {
Kostya Serebryany9514a532012-12-19 09:53:32 +0000124 Printf("Shadow byte legend (one shadow byte represents %d "
125 "application bytes):\n", (int)SHADOW_GRANULARITY);
126 PrintShadowByte(" Addressable: ", 0);
127 Printf(" Partially addressable: ");
128 for (uptr i = 1; i < SHADOW_GRANULARITY; i++)
129 PrintShadowByte("", i, " ");
130 Printf("\n");
131 PrintShadowByte(" Heap left redzone: ", kAsanHeapLeftRedzoneMagic);
Alexey Samsonovf3f2f5c2013-04-10 07:00:25 +0000132 PrintShadowByte(" Heap right redzone: ", kAsanHeapRightRedzoneMagic);
133 PrintShadowByte(" Freed heap region: ", kAsanHeapFreeMagic);
Kostya Serebryany9514a532012-12-19 09:53:32 +0000134 PrintShadowByte(" Stack left redzone: ", kAsanStackLeftRedzoneMagic);
135 PrintShadowByte(" Stack mid redzone: ", kAsanStackMidRedzoneMagic);
136 PrintShadowByte(" Stack right redzone: ", kAsanStackRightRedzoneMagic);
137 PrintShadowByte(" Stack partial redzone: ", kAsanStackPartialRedzoneMagic);
Kostya Serebryany9514a532012-12-19 09:53:32 +0000138 PrintShadowByte(" Stack after return: ", kAsanStackAfterReturnMagic);
139 PrintShadowByte(" Stack use after scope: ", kAsanStackUseAfterScopeMagic);
140 PrintShadowByte(" Global redzone: ", kAsanGlobalRedzoneMagic);
141 PrintShadowByte(" Global init order: ", kAsanInitializationOrderMagic);
142 PrintShadowByte(" Poisoned by user: ", kAsanUserPoisonedMemoryMagic);
143 PrintShadowByte(" ASan internal: ", kAsanInternalHeapMagic);
Alexey Samsonov98737922012-08-10 15:13:05 +0000144}
145
Kostya Serebryany95f630a2013-01-28 07:34:22 +0000146static void PrintShadowMemoryForAddress(uptr addr) {
147 if (!AddrIsInMem(addr))
148 return;
149 uptr shadow_addr = MemToShadow(addr);
150 const uptr n_bytes_per_row = 16;
151 uptr aligned_shadow = shadow_addr & ~(n_bytes_per_row - 1);
152 Printf("Shadow bytes around the buggy address:\n");
153 for (int i = -5; i <= 5; i++) {
154 const char *prefix = (i == 0) ? "=>" : " ";
155 PrintShadowBytes(prefix,
156 (u8*)(aligned_shadow + i * n_bytes_per_row),
157 (u8*)shadow_addr, n_bytes_per_row);
158 }
159 if (flags()->print_legend)
160 PrintLegend();
161}
162
Alexey Samsonov98737922012-08-10 15:13:05 +0000163static void PrintZoneForPointer(uptr ptr, uptr zone_ptr,
164 const char *zone_name) {
165 if (zone_ptr) {
166 if (zone_name) {
Kostya Serebryany283c2962012-08-28 11:34:40 +0000167 Printf("malloc_zone_from_ptr(%p) = %p, which is %s\n",
Alexey Samsonov98737922012-08-10 15:13:05 +0000168 ptr, zone_ptr, zone_name);
169 } else {
Kostya Serebryany283c2962012-08-28 11:34:40 +0000170 Printf("malloc_zone_from_ptr(%p) = %p, which doesn't have a name\n",
Alexey Samsonov98737922012-08-10 15:13:05 +0000171 ptr, zone_ptr);
172 }
173 } else {
Kostya Serebryany283c2962012-08-28 11:34:40 +0000174 Printf("malloc_zone_from_ptr(%p) = 0\n", ptr);
Alexey Samsonov98737922012-08-10 15:13:05 +0000175 }
176}
177
Alexey Samsonove218beb2012-08-09 09:06:52 +0000178// ---------------------- Address Descriptions ------------------- {{{1
179
Alexey Samsonove4bfca22012-08-09 09:27:24 +0000180static bool IsASCII(unsigned char c) {
Alexey Samsonov98737922012-08-10 15:13:05 +0000181 return /*0x00 <= c &&*/ c <= 0x7F;
Alexey Samsonove4bfca22012-08-09 09:27:24 +0000182}
183
Alexey Samsonovc9424272013-03-27 10:41:22 +0000184static const char *MaybeDemangleGlobalName(const char *name) {
185 // We can spoil names of globals with C linkage, so use an heuristic
186 // approach to check if the name should be demangled.
187 return (name[0] == '_' && name[1] == 'Z') ? Demangle(name) : name;
188}
189
Alexey Samsonov939316c2013-04-01 08:57:38 +0000190// Check if the global is a zero-terminated ASCII string. If so, print it.
191static void PrintGlobalNameIfASCII(const __asan_global &g) {
192 for (uptr p = g.beg; p < g.beg + g.size - 1; p++) {
193 unsigned char c = *(unsigned char*)p;
194 if (c == '\0' || !IsASCII(c)) return;
195 }
196 if (*(char*)(g.beg + g.size - 1) != '\0') return;
197 Printf(" '%s' is ascii string '%s'\n",
198 MaybeDemangleGlobalName(g.name), (char*)g.beg);
199}
200
Evgeniy Stepanov589dcda2013-02-05 14:32:03 +0000201bool DescribeAddressRelativeToGlobal(uptr addr, uptr size,
202 const __asan_global &g) {
Kostya Serebryanya3b0e5e2013-01-23 11:14:21 +0000203 static const uptr kMinimalDistanceFromAnotherGlobal = 64;
204 if (addr <= g.beg - kMinimalDistanceFromAnotherGlobal) return false;
Alexey Samsonove4bfca22012-08-09 09:27:24 +0000205 if (addr >= g.beg + g.size_with_redzone) return false;
Kostya Serebryany58f54552012-12-18 07:32:16 +0000206 Decorator d;
207 Printf("%s", d.Location());
Alexey Samsonove4bfca22012-08-09 09:27:24 +0000208 if (addr < g.beg) {
Evgeniy Stepanov589dcda2013-02-05 14:32:03 +0000209 Printf("%p is located %zd bytes to the left", (void*)addr, g.beg - addr);
210 } else if (addr + size > g.beg + g.size) {
211 if (addr < g.beg + g.size)
212 addr = g.beg + g.size;
213 Printf("%p is located %zd bytes to the right", (void*)addr,
214 addr - (g.beg + g.size));
Alexey Samsonove4bfca22012-08-09 09:27:24 +0000215 } else {
Evgeniy Stepanov589dcda2013-02-05 14:32:03 +0000216 // Can it happen?
217 Printf("%p is located %zd bytes inside", (void*)addr, addr - g.beg);
Alexey Samsonove4bfca22012-08-09 09:27:24 +0000218 }
Kostya Serebryany60c9f442013-03-18 08:04:55 +0000219 Printf(" of global variable '%s' from '%s' (0x%zx) of size %zu\n",
Alexey Samsonovc9424272013-03-27 10:41:22 +0000220 MaybeDemangleGlobalName(g.name), g.module_name, g.beg, g.size);
Kostya Serebryany58f54552012-12-18 07:32:16 +0000221 Printf("%s", d.EndLocation());
Alexey Samsonove4bfca22012-08-09 09:27:24 +0000222 PrintGlobalNameIfASCII(g);
223 return true;
224}
225
Alexey Samsonove218beb2012-08-09 09:06:52 +0000226bool DescribeAddressIfShadow(uptr addr) {
227 if (AddrIsInMem(addr))
228 return false;
229 static const char kAddrInShadowReport[] =
230 "Address %p is located in the %s.\n";
231 if (AddrIsInShadowGap(addr)) {
Kostya Serebryany283c2962012-08-28 11:34:40 +0000232 Printf(kAddrInShadowReport, addr, "shadow gap area");
Alexey Samsonove218beb2012-08-09 09:06:52 +0000233 return true;
234 }
235 if (AddrIsInHighShadow(addr)) {
Kostya Serebryany283c2962012-08-28 11:34:40 +0000236 Printf(kAddrInShadowReport, addr, "high shadow area");
Alexey Samsonove218beb2012-08-09 09:06:52 +0000237 return true;
238 }
239 if (AddrIsInLowShadow(addr)) {
Kostya Serebryany283c2962012-08-28 11:34:40 +0000240 Printf(kAddrInShadowReport, addr, "low shadow area");
Alexey Samsonove218beb2012-08-09 09:06:52 +0000241 return true;
242 }
243 CHECK(0 && "Address is not in memory and not in shadow?");
244 return false;
245}
246
Kostya Serebryany50f3daa2013-03-22 10:36:24 +0000247// Return " (thread_name) " or an empty string if the name is empty.
248const char *ThreadNameWithParenthesis(AsanThreadContext *t, char buff[],
249 uptr buff_len) {
250 const char *name = t->name;
251 if (name[0] == '\0') return "";
252 buff[0] = 0;
253 internal_strncat(buff, " (", 3);
254 internal_strncat(buff, name, buff_len - 4);
255 internal_strncat(buff, ")", 2);
256 return buff;
257}
258
259const char *ThreadNameWithParenthesis(u32 tid, char buff[],
260 uptr buff_len) {
261 if (tid == kInvalidTid) return "";
262 asanThreadRegistry().CheckLocked();
263 AsanThreadContext *t = GetThreadContextByTidLocked(tid);
264 return ThreadNameWithParenthesis(t, buff, buff_len);
265}
266
Alexey Samsonove218beb2012-08-09 09:06:52 +0000267bool DescribeAddressIfStack(uptr addr, uptr access_size) {
Alexey Samsonovdef1be92013-03-21 11:23:41 +0000268 AsanThread *t = FindThreadByStackAddress(addr);
Alexey Samsonove218beb2012-08-09 09:06:52 +0000269 if (!t) return false;
270 const sptr kBufSize = 4095;
271 char buf[kBufSize];
272 uptr offset = 0;
Kostya Serebryany50f3daa2013-03-22 10:36:24 +0000273 uptr frame_pc = 0;
274 char tname[128];
275 const char *frame_descr = t->GetFrameNameByAddr(addr, &offset, &frame_pc);
Kostya Serebryanyd570bb42013-05-22 14:21:34 +0000276
277#ifdef __powerpc64__
278 // On PowerPC64, the address of a function actually points to a
279 // three-doubleword data structure with the first field containing
280 // the address of the function's code.
281 frame_pc = *reinterpret_cast<uptr *>(frame_pc);
282#endif
283
Alexey Samsonove218beb2012-08-09 09:06:52 +0000284 // This string is created by the compiler and has the following form:
Kostya Serebryany50f3daa2013-03-22 10:36:24 +0000285 // "n alloc_1 alloc_2 ... alloc_n"
Alexey Samsonove218beb2012-08-09 09:06:52 +0000286 // where alloc_i looks like "offset size len ObjectName ".
287 CHECK(frame_descr);
Kostya Serebryany58f54552012-12-18 07:32:16 +0000288 Decorator d;
289 Printf("%s", d.Location());
Kostya Serebryany50f3daa2013-03-22 10:36:24 +0000290 Printf("Address %p is located in stack of thread T%d%s "
291 "at offset %zu in frame\n",
292 addr, t->tid(),
293 ThreadNameWithParenthesis(t->tid(), tname, sizeof(tname)),
294 offset);
295 // Now we print the frame where the alloca has happened.
296 // We print this frame as a stack trace with one element.
297 // The symbolizer may print more than one frame if inlining was involved.
298 // The frame numbers may be different than those in the stack trace printed
299 // previously. That's unfortunate, but I have no better solution,
300 // especially given that the alloca may be from entirely different place
301 // (e.g. use-after-scope, or different thread's stack).
302 StackTrace alloca_stack;
303 alloca_stack.trace[0] = frame_pc + 16;
304 alloca_stack.size = 1;
Kostya Serebryany58f54552012-12-18 07:32:16 +0000305 Printf("%s", d.EndLocation());
Kostya Serebryany50f3daa2013-03-22 10:36:24 +0000306 PrintStack(&alloca_stack);
Alexey Samsonove218beb2012-08-09 09:06:52 +0000307 // Report the number of stack objects.
308 char *p;
Kostya Serebryany50f3daa2013-03-22 10:36:24 +0000309 uptr n_objects = internal_simple_strtoll(frame_descr, &p, 10);
Kostya Serebryanya27bdf72013-04-05 14:40:25 +0000310 CHECK_GT(n_objects, 0);
Kostya Serebryany283c2962012-08-28 11:34:40 +0000311 Printf(" This frame has %zu object(s):\n", n_objects);
Alexey Samsonove218beb2012-08-09 09:06:52 +0000312 // Report all objects in this frame.
313 for (uptr i = 0; i < n_objects; i++) {
314 uptr beg, size;
315 sptr len;
316 beg = internal_simple_strtoll(p, &p, 10);
317 size = internal_simple_strtoll(p, &p, 10);
318 len = internal_simple_strtoll(p, &p, 10);
319 if (beg <= 0 || size <= 0 || len < 0 || *p != ' ') {
Kostya Serebryany283c2962012-08-28 11:34:40 +0000320 Printf("AddressSanitizer can't parse the stack frame "
Alexey Samsonove218beb2012-08-09 09:06:52 +0000321 "descriptor: |%s|\n", frame_descr);
322 break;
323 }
324 p++;
325 buf[0] = 0;
326 internal_strncat(buf, p, Min(kBufSize, len));
327 p += len;
Kostya Serebryany283c2962012-08-28 11:34:40 +0000328 Printf(" [%zu, %zu) '%s'\n", beg, beg + size, buf);
Alexey Samsonove218beb2012-08-09 09:06:52 +0000329 }
Kostya Serebryany283c2962012-08-28 11:34:40 +0000330 Printf("HINT: this may be a false positive if your program uses "
Alexey Samsonov08700282012-11-23 09:46:34 +0000331 "some custom stack unwind mechanism or swapcontext\n"
Alexey Samsonove218beb2012-08-09 09:06:52 +0000332 " (longjmp and C++ exceptions *are* supported)\n");
Alexey Samsonovdef1be92013-03-21 11:23:41 +0000333 DescribeThread(t->context());
Alexey Samsonove218beb2012-08-09 09:06:52 +0000334 return true;
335}
336
Alexey Samsonov5c153fa2012-09-18 07:38:10 +0000337static void DescribeAccessToHeapChunk(AsanChunkView chunk, uptr addr,
338 uptr access_size) {
Evgeniy Stepanov589dcda2013-02-05 14:32:03 +0000339 sptr offset;
Kostya Serebryany58f54552012-12-18 07:32:16 +0000340 Decorator d;
341 Printf("%s", d.Location());
Evgeniy Stepanov589dcda2013-02-05 14:32:03 +0000342 if (chunk.AddrIsAtLeft(addr, access_size, &offset)) {
343 Printf("%p is located %zd bytes to the left of", (void*)addr, offset);
Alexey Samsonov5c153fa2012-09-18 07:38:10 +0000344 } else if (chunk.AddrIsAtRight(addr, access_size, &offset)) {
Evgeniy Stepanov589dcda2013-02-05 14:32:03 +0000345 if (offset < 0) {
346 addr -= offset;
347 offset = 0;
348 }
349 Printf("%p is located %zd bytes to the right of", (void*)addr, offset);
350 } else if (chunk.AddrIsInside(addr, access_size, &offset)) {
351 Printf("%p is located %zd bytes inside of", (void*)addr, offset);
Alexey Samsonov5c153fa2012-09-18 07:38:10 +0000352 } else {
Evgeniy Stepanov589dcda2013-02-05 14:32:03 +0000353 Printf("%p is located somewhere around (this is AddressSanitizer bug!)",
354 (void*)addr);
Alexey Samsonov5c153fa2012-09-18 07:38:10 +0000355 }
356 Printf(" %zu-byte region [%p,%p)\n", chunk.UsedSize(),
357 (void*)(chunk.Beg()), (void*)(chunk.End()));
Kostya Serebryany58f54552012-12-18 07:32:16 +0000358 Printf("%s", d.EndLocation());
Alexey Samsonov5c153fa2012-09-18 07:38:10 +0000359}
360
361void DescribeHeapAddress(uptr addr, uptr access_size) {
362 AsanChunkView chunk = FindHeapChunkByAddress(addr);
363 if (!chunk.IsValid()) return;
364 DescribeAccessToHeapChunk(chunk, addr, access_size);
365 CHECK(chunk.AllocTid() != kInvalidTid);
Alexey Samsonovdef1be92013-03-21 11:23:41 +0000366 asanThreadRegistry().CheckLocked();
367 AsanThreadContext *alloc_thread =
368 GetThreadContextByTidLocked(chunk.AllocTid());
Alexey Samsonov5c153fa2012-09-18 07:38:10 +0000369 StackTrace alloc_stack;
370 chunk.GetAllocStack(&alloc_stack);
Alexey Samsonov89c13842013-03-20 09:23:28 +0000371 AsanThread *t = GetCurrentThread();
Alexey Samsonov5c153fa2012-09-18 07:38:10 +0000372 CHECK(t);
Kostya Serebryany716e2f22012-12-07 15:15:01 +0000373 char tname[128];
Kostya Serebryany58f54552012-12-18 07:32:16 +0000374 Decorator d;
Alexey Samsonov5c153fa2012-09-18 07:38:10 +0000375 if (chunk.FreeTid() != kInvalidTid) {
Alexey Samsonovdef1be92013-03-21 11:23:41 +0000376 AsanThreadContext *free_thread =
377 GetThreadContextByTidLocked(chunk.FreeTid());
Kostya Serebryany58f54552012-12-18 07:32:16 +0000378 Printf("%sfreed by thread T%d%s here:%s\n", d.Allocation(),
Alexey Samsonovdef1be92013-03-21 11:23:41 +0000379 free_thread->tid,
Kostya Serebryany58f54552012-12-18 07:32:16 +0000380 ThreadNameWithParenthesis(free_thread, tname, sizeof(tname)),
381 d.EndAllocation());
Alexey Samsonov5c153fa2012-09-18 07:38:10 +0000382 StackTrace free_stack;
383 chunk.GetFreeStack(&free_stack);
384 PrintStack(&free_stack);
Kostya Serebryany58f54552012-12-18 07:32:16 +0000385 Printf("%spreviously allocated by thread T%d%s here:%s\n",
Alexey Samsonovdef1be92013-03-21 11:23:41 +0000386 d.Allocation(), alloc_thread->tid,
Kostya Serebryany58f54552012-12-18 07:32:16 +0000387 ThreadNameWithParenthesis(alloc_thread, tname, sizeof(tname)),
388 d.EndAllocation());
Alexey Samsonov5c153fa2012-09-18 07:38:10 +0000389 PrintStack(&alloc_stack);
Alexey Samsonovdef1be92013-03-21 11:23:41 +0000390 DescribeThread(t->context());
Alexey Samsonov5c153fa2012-09-18 07:38:10 +0000391 DescribeThread(free_thread);
392 DescribeThread(alloc_thread);
393 } else {
Kostya Serebryany58f54552012-12-18 07:32:16 +0000394 Printf("%sallocated by thread T%d%s here:%s\n", d.Allocation(),
Alexey Samsonovdef1be92013-03-21 11:23:41 +0000395 alloc_thread->tid,
Kostya Serebryany58f54552012-12-18 07:32:16 +0000396 ThreadNameWithParenthesis(alloc_thread, tname, sizeof(tname)),
397 d.EndAllocation());
Alexey Samsonov5c153fa2012-09-18 07:38:10 +0000398 PrintStack(&alloc_stack);
Alexey Samsonovdef1be92013-03-21 11:23:41 +0000399 DescribeThread(t->context());
Alexey Samsonov5c153fa2012-09-18 07:38:10 +0000400 DescribeThread(alloc_thread);
401 }
402}
403
Alexey Samsonove218beb2012-08-09 09:06:52 +0000404void DescribeAddress(uptr addr, uptr access_size) {
405 // Check if this is shadow or shadow gap.
406 if (DescribeAddressIfShadow(addr))
407 return;
408 CHECK(AddrIsInMem(addr));
Evgeniy Stepanov589dcda2013-02-05 14:32:03 +0000409 if (DescribeAddressIfGlobal(addr, access_size))
Alexey Samsonove218beb2012-08-09 09:06:52 +0000410 return;
411 if (DescribeAddressIfStack(addr, access_size))
412 return;
413 // Assume it is a heap address.
414 DescribeHeapAddress(addr, access_size);
415}
416
Alexey Samsonov71b42c92012-09-05 07:37:15 +0000417// ------------------- Thread description -------------------- {{{1
418
Alexey Samsonovdef1be92013-03-21 11:23:41 +0000419void DescribeThread(AsanThreadContext *context) {
420 CHECK(context);
421 asanThreadRegistry().CheckLocked();
Alexey Samsonov71b42c92012-09-05 07:37:15 +0000422 // No need to announce the main thread.
Alexey Samsonovdef1be92013-03-21 11:23:41 +0000423 if (context->tid == 0 || context->announced) {
Alexey Samsonov71b42c92012-09-05 07:37:15 +0000424 return;
425 }
Alexey Samsonovdef1be92013-03-21 11:23:41 +0000426 context->announced = true;
Kostya Serebryany716e2f22012-12-07 15:15:01 +0000427 char tname[128];
Alexey Samsonovdef1be92013-03-21 11:23:41 +0000428 Printf("Thread T%d%s", context->tid,
429 ThreadNameWithParenthesis(context->tid, tname, sizeof(tname)));
Kostya Serebryany716e2f22012-12-07 15:15:01 +0000430 Printf(" created by T%d%s here:\n",
Alexey Samsonovdef1be92013-03-21 11:23:41 +0000431 context->parent_tid,
432 ThreadNameWithParenthesis(context->parent_tid,
Kostya Serebryany716e2f22012-12-07 15:15:01 +0000433 tname, sizeof(tname)));
Alexey Samsonovdef1be92013-03-21 11:23:41 +0000434 PrintStack(&context->stack);
Alexey Samsonov71b42c92012-09-05 07:37:15 +0000435 // Recursively described parent thread if needed.
436 if (flags()->print_full_thread_history) {
Alexey Samsonovdef1be92013-03-21 11:23:41 +0000437 AsanThreadContext *parent_context =
438 GetThreadContextByTidLocked(context->parent_tid);
439 DescribeThread(parent_context);
Alexey Samsonov71b42c92012-09-05 07:37:15 +0000440 }
441}
442
Alexey Samsonove218beb2012-08-09 09:06:52 +0000443// -------------------- Different kinds of reports ----------------- {{{1
444
Alexey Samsonov98737922012-08-10 15:13:05 +0000445// Use ScopedInErrorReport to run common actions just before and
446// immediately after printing error report.
447class ScopedInErrorReport {
448 public:
449 ScopedInErrorReport() {
450 static atomic_uint32_t num_calls;
Alexey Samsonov62e27092012-09-17 08:02:19 +0000451 static u32 reporting_thread_tid;
Alexey Samsonov98737922012-08-10 15:13:05 +0000452 if (atomic_fetch_add(&num_calls, 1, memory_order_relaxed) != 0) {
453 // Do not print more than one report, otherwise they will mix up.
454 // Error reporting functions shouldn't return at this situation, as
455 // they are defined as no-return.
Kostya Serebryany283c2962012-08-28 11:34:40 +0000456 Report("AddressSanitizer: while reporting a bug found another one."
Alexey Samsonov98737922012-08-10 15:13:05 +0000457 "Ignoring.\n");
Alexey Samsonov89c13842013-03-20 09:23:28 +0000458 u32 current_tid = GetCurrentTidOrInvalid();
Alexey Samsonov62e27092012-09-17 08:02:19 +0000459 if (current_tid != reporting_thread_tid) {
460 // ASan found two bugs in different threads simultaneously. Sleep
461 // long enough to make sure that the thread which started to print
462 // an error report will finish doing it.
463 SleepForSeconds(Max(100, flags()->sleep_before_dying + 1));
464 }
Alexey Samsonovf8822472013-02-20 13:54:32 +0000465 // If we're still not dead for some reason, use raw _exit() instead of
Alexey Samsonov031633b2012-11-19 11:22:22 +0000466 // Die() to bypass any additional checks.
Alexey Samsonovf8822472013-02-20 13:54:32 +0000467 internal__exit(flags()->exitcode);
Alexey Samsonov98737922012-08-10 15:13:05 +0000468 }
Alexey Samsonov6a08d292012-12-07 22:01:28 +0000469 ASAN_ON_ERROR();
Alexey Samsonov7ed46ff2013-04-05 07:30:29 +0000470 // Make sure the registry and sanitizer report mutexes are locked while
471 // we're printing an error report.
472 // We can lock them only here to avoid self-deadlock in case of
Alexey Samsonovdef1be92013-03-21 11:23:41 +0000473 // recursive reports.
474 asanThreadRegistry().Lock();
Alexey Samsonov7ed46ff2013-04-05 07:30:29 +0000475 CommonSanitizerReportMutex.Lock();
Alexey Samsonov89c13842013-03-20 09:23:28 +0000476 reporting_thread_tid = GetCurrentTidOrInvalid();
Kostya Serebryany283c2962012-08-28 11:34:40 +0000477 Printf("===================================================="
Alexey Samsonov62e27092012-09-17 08:02:19 +0000478 "=============\n");
479 if (reporting_thread_tid != kInvalidTid) {
Alexey Samsonov98737922012-08-10 15:13:05 +0000480 // We started reporting an error message. Stop using the fake stack
481 // in case we call an instrumented function from a symbolizer.
Alexey Samsonov89c13842013-03-20 09:23:28 +0000482 AsanThread *curr_thread = GetCurrentThread();
Alexey Samsonov62e27092012-09-17 08:02:19 +0000483 CHECK(curr_thread);
Alexey Samsonov98737922012-08-10 15:13:05 +0000484 curr_thread->fake_stack().StopUsingFakeStack();
485 }
486 }
487 // Destructor is NORETURN, as functions that report errors are.
488 NORETURN ~ScopedInErrorReport() {
489 // Make sure the current thread is announced.
Alexey Samsonov89c13842013-03-20 09:23:28 +0000490 AsanThread *curr_thread = GetCurrentThread();
Alexey Samsonov98737922012-08-10 15:13:05 +0000491 if (curr_thread) {
Alexey Samsonovdef1be92013-03-21 11:23:41 +0000492 DescribeThread(curr_thread->context());
Alexey Samsonov98737922012-08-10 15:13:05 +0000493 }
494 // Print memory stats.
Kostya Serebryany95f630a2013-01-28 07:34:22 +0000495 if (flags()->print_stats)
496 __asan_print_accumulated_stats();
Alexey Samsonov98737922012-08-10 15:13:05 +0000497 if (error_report_callback) {
498 error_report_callback(error_message_buffer);
499 }
Kostya Serebryany283c2962012-08-28 11:34:40 +0000500 Report("ABORTING\n");
Alexey Samsonov98737922012-08-10 15:13:05 +0000501 Die();
502 }
503};
504
Kostya Serebryany2673fd82013-02-06 12:36:49 +0000505static void ReportSummary(const char *error_type, StackTrace *stack) {
506 if (!stack->size) return;
507 if (IsSymbolizerAvailable()) {
508 AddressInfo ai;
509 // Currently, we include the first stack frame into the report summary.
510 // Maybe sometimes we need to choose another frame (e.g. skip memcpy/etc).
Alexey Samsonov22c1c182013-04-11 11:45:04 +0000511 uptr pc = StackTrace::GetPreviousInstructionPc(stack->trace[0]);
512 SymbolizeCode(pc, &ai, 1);
Kostya Serebryany2673fd82013-02-06 12:36:49 +0000513 ReportErrorSummary(error_type,
Sergey Matveeved20ebe2013-05-06 11:27:58 +0000514 StripPathPrefix(ai.file,
515 common_flags()->strip_path_prefix),
Kostya Serebryany2673fd82013-02-06 12:36:49 +0000516 ai.line, ai.function);
517 }
518 // FIXME: do we need to print anything at all if there is no symbolizer?
519}
520
Alexey Samsonov73545092012-08-09 07:40:58 +0000521void ReportSIGSEGV(uptr pc, uptr sp, uptr bp, uptr addr) {
Alexey Samsonov98737922012-08-10 15:13:05 +0000522 ScopedInErrorReport in_report;
Kostya Serebryany58f54552012-12-18 07:32:16 +0000523 Decorator d;
524 Printf("%s", d.Warning());
Kostya Serebryany69d8ede2012-10-15 13:04:58 +0000525 Report("ERROR: AddressSanitizer: SEGV on unknown address %p"
Alexey Samsonov73545092012-08-09 07:40:58 +0000526 " (pc %p sp %p bp %p T%d)\n",
527 (void*)addr, (void*)pc, (void*)sp, (void*)bp,
Alexey Samsonov89c13842013-03-20 09:23:28 +0000528 GetCurrentTidOrInvalid());
Kostya Serebryany58f54552012-12-18 07:32:16 +0000529 Printf("%s", d.EndWarning());
Kostya Serebryany283c2962012-08-28 11:34:40 +0000530 Printf("AddressSanitizer can not provide additional info.\n");
Kostya Serebryanya30c8f92012-12-13 09:34:23 +0000531 GET_STACK_TRACE_FATAL(pc, bp);
Kostya Serebryanycc347222012-08-28 13:49:49 +0000532 PrintStack(&stack);
Kostya Serebryany2673fd82013-02-06 12:36:49 +0000533 ReportSummary("SEGV", &stack);
Alexey Samsonov73545092012-08-09 07:40:58 +0000534}
535
Kostya Serebryanyc3390df2012-08-28 11:54:30 +0000536void ReportDoubleFree(uptr addr, StackTrace *stack) {
Alexey Samsonov98737922012-08-10 15:13:05 +0000537 ScopedInErrorReport in_report;
Kostya Serebryany58f54552012-12-18 07:32:16 +0000538 Decorator d;
539 Printf("%s", d.Warning());
Kostya Serebryanya89a35a2013-03-26 08:01:37 +0000540 char tname[128];
541 u32 curr_tid = GetCurrentTidOrInvalid();
542 Report("ERROR: AddressSanitizer: attempting double-free on %p in "
543 "thread T%d%s:\n",
544 addr, curr_tid,
545 ThreadNameWithParenthesis(curr_tid, tname, sizeof(tname)));
546
Kostya Serebryany58f54552012-12-18 07:32:16 +0000547 Printf("%s", d.EndWarning());
Kostya Serebryanycc347222012-08-28 13:49:49 +0000548 PrintStack(stack);
Alexey Samsonovf7c1d182012-08-09 08:15:46 +0000549 DescribeHeapAddress(addr, 1);
Kostya Serebryany2673fd82013-02-06 12:36:49 +0000550 ReportSummary("double-free", stack);
Alexey Samsonovf7c1d182012-08-09 08:15:46 +0000551}
552
Kostya Serebryanyc3390df2012-08-28 11:54:30 +0000553void ReportFreeNotMalloced(uptr addr, StackTrace *stack) {
Alexey Samsonov98737922012-08-10 15:13:05 +0000554 ScopedInErrorReport in_report;
Kostya Serebryany58f54552012-12-18 07:32:16 +0000555 Decorator d;
556 Printf("%s", d.Warning());
Kostya Serebryanya89a35a2013-03-26 08:01:37 +0000557 char tname[128];
558 u32 curr_tid = GetCurrentTidOrInvalid();
Kostya Serebryany69d8ede2012-10-15 13:04:58 +0000559 Report("ERROR: AddressSanitizer: attempting free on address "
Kostya Serebryanya89a35a2013-03-26 08:01:37 +0000560 "which was not malloc()-ed: %p in thread T%d%s\n", addr,
561 curr_tid, ThreadNameWithParenthesis(curr_tid, tname, sizeof(tname)));
Kostya Serebryany58f54552012-12-18 07:32:16 +0000562 Printf("%s", d.EndWarning());
Kostya Serebryanycc347222012-08-28 13:49:49 +0000563 PrintStack(stack);
Alexey Samsonov98737922012-08-10 15:13:05 +0000564 DescribeHeapAddress(addr, 1);
Kostya Serebryany2673fd82013-02-06 12:36:49 +0000565 ReportSummary("bad-free", stack);
Alexey Samsonovf7c1d182012-08-09 08:15:46 +0000566}
567
Kostya Serebryanyfe6d9162012-12-21 08:53:59 +0000568void ReportAllocTypeMismatch(uptr addr, StackTrace *stack,
569 AllocType alloc_type,
570 AllocType dealloc_type) {
571 static const char *alloc_names[] =
572 {"INVALID", "malloc", "operator new", "operator new []"};
573 static const char *dealloc_names[] =
574 {"INVALID", "free", "operator delete", "operator delete []"};
575 CHECK_NE(alloc_type, dealloc_type);
576 ScopedInErrorReport in_report;
577 Decorator d;
578 Printf("%s", d.Warning());
579 Report("ERROR: AddressSanitizer: alloc-dealloc-mismatch (%s vs %s) on %p\n",
580 alloc_names[alloc_type], dealloc_names[dealloc_type], addr);
581 Printf("%s", d.EndWarning());
582 PrintStack(stack);
583 DescribeHeapAddress(addr, 1);
Kostya Serebryany2673fd82013-02-06 12:36:49 +0000584 ReportSummary("alloc-dealloc-mismatch", stack);
Kostya Serebryanyfe6d9162012-12-21 08:53:59 +0000585 Report("HINT: if you don't care about these warnings you may set "
586 "ASAN_OPTIONS=alloc_dealloc_mismatch=0\n");
587}
588
Kostya Serebryanyc3390df2012-08-28 11:54:30 +0000589void ReportMallocUsableSizeNotOwned(uptr addr, StackTrace *stack) {
Alexey Samsonov98737922012-08-10 15:13:05 +0000590 ScopedInErrorReport in_report;
Kostya Serebryany58f54552012-12-18 07:32:16 +0000591 Decorator d;
592 Printf("%s", d.Warning());
Kostya Serebryany69d8ede2012-10-15 13:04:58 +0000593 Report("ERROR: AddressSanitizer: attempting to call "
Alexey Samsonovf7c1d182012-08-09 08:15:46 +0000594 "malloc_usable_size() for pointer which is "
595 "not owned: %p\n", addr);
Kostya Serebryany58f54552012-12-18 07:32:16 +0000596 Printf("%s", d.EndWarning());
Kostya Serebryanycc347222012-08-28 13:49:49 +0000597 PrintStack(stack);
Alexey Samsonovf7c1d182012-08-09 08:15:46 +0000598 DescribeHeapAddress(addr, 1);
Kostya Serebryany2673fd82013-02-06 12:36:49 +0000599 ReportSummary("bad-malloc_usable_size", stack);
Alexey Samsonovf7c1d182012-08-09 08:15:46 +0000600}
601
Kostya Serebryanyc3390df2012-08-28 11:54:30 +0000602void ReportAsanGetAllocatedSizeNotOwned(uptr addr, StackTrace *stack) {
Alexey Samsonov98737922012-08-10 15:13:05 +0000603 ScopedInErrorReport in_report;
Kostya Serebryany58f54552012-12-18 07:32:16 +0000604 Decorator d;
605 Printf("%s", d.Warning());
Kostya Serebryany69d8ede2012-10-15 13:04:58 +0000606 Report("ERROR: AddressSanitizer: attempting to call "
Alexey Samsonovf7c1d182012-08-09 08:15:46 +0000607 "__asan_get_allocated_size() for pointer which is "
608 "not owned: %p\n", addr);
Kostya Serebryany58f54552012-12-18 07:32:16 +0000609 Printf("%s", d.EndWarning());
Kostya Serebryanycc347222012-08-28 13:49:49 +0000610 PrintStack(stack);
Alexey Samsonovf7c1d182012-08-09 08:15:46 +0000611 DescribeHeapAddress(addr, 1);
Kostya Serebryany2673fd82013-02-06 12:36:49 +0000612 ReportSummary("bad-__asan_get_allocated_size", stack);
Alexey Samsonovf7c1d182012-08-09 08:15:46 +0000613}
614
Alexey Samsonov487fee72012-08-09 08:32:33 +0000615void ReportStringFunctionMemoryRangesOverlap(
616 const char *function, const char *offset1, uptr length1,
Kostya Serebryanyc3390df2012-08-28 11:54:30 +0000617 const char *offset2, uptr length2, StackTrace *stack) {
Alexey Samsonov98737922012-08-10 15:13:05 +0000618 ScopedInErrorReport in_report;
Kostya Serebryany58f54552012-12-18 07:32:16 +0000619 Decorator d;
Kostya Serebryany2673fd82013-02-06 12:36:49 +0000620 char bug_type[100];
621 internal_snprintf(bug_type, sizeof(bug_type), "%s-param-overlap", function);
Kostya Serebryany58f54552012-12-18 07:32:16 +0000622 Printf("%s", d.Warning());
Kostya Serebryany2673fd82013-02-06 12:36:49 +0000623 Report("ERROR: AddressSanitizer: %s: "
Alexey Samsonov487fee72012-08-09 08:32:33 +0000624 "memory ranges [%p,%p) and [%p, %p) overlap\n", \
Kostya Serebryany2673fd82013-02-06 12:36:49 +0000625 bug_type, offset1, offset1 + length1, offset2, offset2 + length2);
Kostya Serebryany58f54552012-12-18 07:32:16 +0000626 Printf("%s", d.EndWarning());
Kostya Serebryanycc347222012-08-28 13:49:49 +0000627 PrintStack(stack);
Alexey Samsonov98737922012-08-10 15:13:05 +0000628 DescribeAddress((uptr)offset1, length1);
629 DescribeAddress((uptr)offset2, length2);
Kostya Serebryany2673fd82013-02-06 12:36:49 +0000630 ReportSummary(bug_type, stack);
Alexey Samsonov487fee72012-08-09 08:32:33 +0000631}
Alexey Samsonovf7c1d182012-08-09 08:15:46 +0000632
Alexey Samsonov663c5012012-08-09 12:15:40 +0000633// ----------------------- Mac-specific reports ----------------- {{{1
634
Alexey Samsonov663c5012012-08-09 12:15:40 +0000635void WarnMacFreeUnallocated(
Kostya Serebryanyc3390df2012-08-28 11:54:30 +0000636 uptr addr, uptr zone_ptr, const char *zone_name, StackTrace *stack) {
Alexey Samsonov98737922012-08-10 15:13:05 +0000637 // Just print a warning here.
Kostya Serebryany283c2962012-08-28 11:34:40 +0000638 Printf("free_common(%p) -- attempting to free unallocated memory.\n"
Alexey Samsonov663c5012012-08-09 12:15:40 +0000639 "AddressSanitizer is ignoring this error on Mac OS now.\n",
640 addr);
641 PrintZoneForPointer(addr, zone_ptr, zone_name);
Kostya Serebryanycc347222012-08-28 13:49:49 +0000642 PrintStack(stack);
Alexey Samsonov98737922012-08-10 15:13:05 +0000643 DescribeHeapAddress(addr, 1);
Alexey Samsonov663c5012012-08-09 12:15:40 +0000644}
645
646void ReportMacMzReallocUnknown(
Kostya Serebryanyc3390df2012-08-28 11:54:30 +0000647 uptr addr, uptr zone_ptr, const char *zone_name, StackTrace *stack) {
Alexey Samsonov98737922012-08-10 15:13:05 +0000648 ScopedInErrorReport in_report;
Kostya Serebryany283c2962012-08-28 11:34:40 +0000649 Printf("mz_realloc(%p) -- attempting to realloc unallocated memory.\n"
Alexey Samsonov663c5012012-08-09 12:15:40 +0000650 "This is an unrecoverable problem, exiting now.\n",
651 addr);
652 PrintZoneForPointer(addr, zone_ptr, zone_name);
Kostya Serebryanycc347222012-08-28 13:49:49 +0000653 PrintStack(stack);
Alexey Samsonov98737922012-08-10 15:13:05 +0000654 DescribeHeapAddress(addr, 1);
Alexey Samsonov663c5012012-08-09 12:15:40 +0000655}
656
657void ReportMacCfReallocUnknown(
Kostya Serebryanyc3390df2012-08-28 11:54:30 +0000658 uptr addr, uptr zone_ptr, const char *zone_name, StackTrace *stack) {
Alexey Samsonov98737922012-08-10 15:13:05 +0000659 ScopedInErrorReport in_report;
Kostya Serebryany283c2962012-08-28 11:34:40 +0000660 Printf("cf_realloc(%p) -- attempting to realloc unallocated memory.\n"
Alexey Samsonov663c5012012-08-09 12:15:40 +0000661 "This is an unrecoverable problem, exiting now.\n",
662 addr);
663 PrintZoneForPointer(addr, zone_ptr, zone_name);
Kostya Serebryanycc347222012-08-28 13:49:49 +0000664 PrintStack(stack);
Alexey Samsonov98737922012-08-10 15:13:05 +0000665 DescribeHeapAddress(addr, 1);
Alexey Samsonovc98570b2012-08-09 10:56:57 +0000666}
667
Alexey Samsonov812ff902012-08-09 11:29:13 +0000668} // namespace __asan
669
670// --------------------------- Interface --------------------- {{{1
671using namespace __asan; // NOLINT
672
673void __asan_report_error(uptr pc, uptr bp, uptr sp,
674 uptr addr, bool is_write, uptr access_size) {
Alexey Samsonov98737922012-08-10 15:13:05 +0000675 ScopedInErrorReport in_report;
Alexey Samsonovc98570b2012-08-09 10:56:57 +0000676
Alexey Samsonov98737922012-08-10 15:13:05 +0000677 // Determine the error type.
Alexey Samsonovc98570b2012-08-09 10:56:57 +0000678 const char *bug_descr = "unknown-crash";
679 if (AddrIsInMem(addr)) {
680 u8 *shadow_addr = (u8*)MemToShadow(addr);
681 // If we are accessing 16 bytes, look at the second shadow byte.
682 if (*shadow_addr == 0 && access_size > SHADOW_GRANULARITY)
683 shadow_addr++;
684 // If we are in the partial right redzone, look at the next shadow byte.
685 if (*shadow_addr > 0 && *shadow_addr < 128)
686 shadow_addr++;
687 switch (*shadow_addr) {
688 case kAsanHeapLeftRedzoneMagic:
689 case kAsanHeapRightRedzoneMagic:
690 bug_descr = "heap-buffer-overflow";
691 break;
692 case kAsanHeapFreeMagic:
693 bug_descr = "heap-use-after-free";
694 break;
695 case kAsanStackLeftRedzoneMagic:
696 bug_descr = "stack-buffer-underflow";
697 break;
Kostya Serebryany3945c582012-08-21 14:10:25 +0000698 case kAsanInitializationOrderMagic:
699 bug_descr = "initialization-order-fiasco";
700 break;
Alexey Samsonovc98570b2012-08-09 10:56:57 +0000701 case kAsanStackMidRedzoneMagic:
702 case kAsanStackRightRedzoneMagic:
703 case kAsanStackPartialRedzoneMagic:
704 bug_descr = "stack-buffer-overflow";
705 break;
706 case kAsanStackAfterReturnMagic:
707 bug_descr = "stack-use-after-return";
708 break;
709 case kAsanUserPoisonedMemoryMagic:
710 bug_descr = "use-after-poison";
711 break;
Alexey Samsonovd4b5db82012-12-04 01:38:15 +0000712 case kAsanStackUseAfterScopeMagic:
713 bug_descr = "stack-use-after-scope";
714 break;
Alexey Samsonovc98570b2012-08-09 10:56:57 +0000715 case kAsanGlobalRedzoneMagic:
716 bug_descr = "global-buffer-overflow";
717 break;
718 }
719 }
Kostya Serebryany58f54552012-12-18 07:32:16 +0000720 Decorator d;
721 Printf("%s", d.Warning());
Kostya Serebryany69d8ede2012-10-15 13:04:58 +0000722 Report("ERROR: AddressSanitizer: %s on address "
Alexey Samsonovc98570b2012-08-09 10:56:57 +0000723 "%p at pc 0x%zx bp 0x%zx sp 0x%zx\n",
724 bug_descr, (void*)addr, pc, bp, sp);
Kostya Serebryany58f54552012-12-18 07:32:16 +0000725 Printf("%s", d.EndWarning());
Alexey Samsonovc98570b2012-08-09 10:56:57 +0000726
Alexey Samsonov89c13842013-03-20 09:23:28 +0000727 u32 curr_tid = GetCurrentTidOrInvalid();
Kostya Serebryany716e2f22012-12-07 15:15:01 +0000728 char tname[128];
Kostya Serebryany58f54552012-12-18 07:32:16 +0000729 Printf("%s%s of size %zu at %p thread T%d%s%s\n",
730 d.Access(),
731 access_size ? (is_write ? "WRITE" : "READ") : "ACCESS",
732 access_size, (void*)addr, curr_tid,
733 ThreadNameWithParenthesis(curr_tid, tname, sizeof(tname)),
734 d.EndAccess());
Alexey Samsonovc98570b2012-08-09 10:56:57 +0000735
Kostya Serebryanya30c8f92012-12-13 09:34:23 +0000736 GET_STACK_TRACE_FATAL(pc, bp);
Kostya Serebryanycc347222012-08-28 13:49:49 +0000737 PrintStack(&stack);
Alexey Samsonovc98570b2012-08-09 10:56:57 +0000738
739 DescribeAddress(addr, access_size);
Kostya Serebryany2673fd82013-02-06 12:36:49 +0000740 ReportSummary(bug_descr, &stack);
Alexey Samsonov98737922012-08-10 15:13:05 +0000741 PrintShadowMemoryForAddress(addr);
Alexey Samsonovc98570b2012-08-09 10:56:57 +0000742}
743
Alexey Samsonovc98570b2012-08-09 10:56:57 +0000744void NOINLINE __asan_set_error_report_callback(void (*callback)(const char*)) {
745 error_report_callback = callback;
746 if (callback) {
747 error_message_buffer_size = 1 << 16;
748 error_message_buffer =
749 (char*)MmapOrDie(error_message_buffer_size, __FUNCTION__);
750 error_message_buffer_pos = 0;
751 }
752}
Alexey Samsonovf657a192012-08-13 11:23:40 +0000753
Kostya Serebryany17a7c672012-12-29 10:18:31 +0000754void __asan_describe_address(uptr addr) {
755 DescribeAddress(addr, 1);
756}
757
Alexey Samsonov6a08d292012-12-07 22:01:28 +0000758#if !SANITIZER_SUPPORTS_WEAK_HOOKS
Alexey Samsonov86633432012-10-02 14:06:39 +0000759// Provide default implementation of __asan_on_error that does nothing
760// and may be overriden by user.
761SANITIZER_WEAK_ATTRIBUTE SANITIZER_INTERFACE_ATTRIBUTE NOINLINE
762void __asan_on_error() {}
Alexey Samsonov6a08d292012-12-07 22:01:28 +0000763#endif