blob: c1681e64446475119c733008f67ae61e148f221d [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
Stephen Hines6d186232014-11-26 17:56:19 -080034struct ReportData {
35 uptr pc;
36 uptr sp;
37 uptr bp;
38 uptr addr;
39 bool is_write;
40 uptr access_size;
41 const char *description;
42};
43
44static bool report_happened = false;
45static ReportData report_data = {};
46
Alexey Samsonovc98570b2012-08-09 10:56:57 +000047void AppendToErrorMessageBuffer(const char *buffer) {
48 if (error_message_buffer) {
49 uptr length = internal_strlen(buffer);
50 CHECK_GE(error_message_buffer_size, error_message_buffer_pos);
51 uptr remaining = error_message_buffer_size - error_message_buffer_pos;
52 internal_strncpy(error_message_buffer + error_message_buffer_pos,
53 buffer, remaining);
54 error_message_buffer[error_message_buffer_size - 1] = '\0';
55 // FIXME: reallocate the buffer instead of truncating the message.
Stephen Hines86277eb2015-03-23 12:06:32 -070056 error_message_buffer_pos += Min(remaining, length);
Alexey Samsonovc98570b2012-08-09 10:56:57 +000057 }
58}
59
Kostya Serebryany58f54552012-12-18 07:32:16 +000060// ---------------------- Decorator ------------------------------ {{{1
Stephen Hines2d1fdb22014-05-28 23:58:16 -070061class Decorator: public __sanitizer::SanitizerCommonDecorator {
Kostya Serebryany58f54552012-12-18 07:32:16 +000062 public:
Stephen Hines2d1fdb22014-05-28 23:58:16 -070063 Decorator() : SanitizerCommonDecorator() { }
Kostya Serebryany58f54552012-12-18 07:32:16 +000064 const char *Access() { return Blue(); }
65 const char *EndAccess() { return Default(); }
66 const char *Location() { return Green(); }
67 const char *EndLocation() { return Default(); }
68 const char *Allocation() { return Magenta(); }
69 const char *EndAllocation() { return Default(); }
Kostya Serebryany9514a532012-12-19 09:53:32 +000070
71 const char *ShadowByte(u8 byte) {
72 switch (byte) {
73 case kAsanHeapLeftRedzoneMagic:
74 case kAsanHeapRightRedzoneMagic:
Stephen Hines6d186232014-11-26 17:56:19 -080075 case kAsanArrayCookieMagic:
Kostya Serebryany9514a532012-12-19 09:53:32 +000076 return Red();
77 case kAsanHeapFreeMagic:
78 return Magenta();
79 case kAsanStackLeftRedzoneMagic:
80 case kAsanStackMidRedzoneMagic:
81 case kAsanStackRightRedzoneMagic:
82 case kAsanStackPartialRedzoneMagic:
83 return Red();
84 case kAsanStackAfterReturnMagic:
85 return Magenta();
86 case kAsanInitializationOrderMagic:
87 return Cyan();
88 case kAsanUserPoisonedMemoryMagic:
Stephen Hines2d1fdb22014-05-28 23:58:16 -070089 case kAsanContiguousContainerOOBMagic:
Stephen Hines86277eb2015-03-23 12:06:32 -070090 case kAsanAllocaLeftMagic:
91 case kAsanAllocaRightMagic:
Kostya Serebryany9514a532012-12-19 09:53:32 +000092 return Blue();
93 case kAsanStackUseAfterScopeMagic:
94 return Magenta();
95 case kAsanGlobalRedzoneMagic:
96 return Red();
97 case kAsanInternalHeapMagic:
98 return Yellow();
Stephen Hines6d186232014-11-26 17:56:19 -080099 case kAsanIntraObjectRedzone:
100 return Yellow();
Kostya Serebryany9514a532012-12-19 09:53:32 +0000101 default:
102 return Default();
103 }
104 }
105 const char *EndShadowByte() { return Default(); }
Stephen Hines6d186232014-11-26 17:56:19 -0800106 const char *MemoryByte() { return Magenta(); }
107 const char *EndMemoryByte() { return Default(); }
Kostya Serebryany58f54552012-12-18 07:32:16 +0000108};
109
Alexey Samsonov98737922012-08-10 15:13:05 +0000110// ---------------------- Helper functions ----------------------- {{{1
111
Stephen Hines6d186232014-11-26 17:56:19 -0800112static void PrintMemoryByte(InternalScopedString *str, const char *before,
113 u8 byte, bool in_shadow, const char *after = "\n") {
Kostya Serebryany9514a532012-12-19 09:53:32 +0000114 Decorator d;
Stephen Hines6d186232014-11-26 17:56:19 -0800115 str->append("%s%s%x%x%s%s", before,
116 in_shadow ? d.ShadowByte(byte) : d.MemoryByte(),
117 byte >> 4, byte & 15,
118 in_shadow ? d.EndShadowByte() : d.EndMemoryByte(), after);
119}
120
121static void PrintShadowByte(InternalScopedString *str, const char *before,
122 u8 byte, const char *after = "\n") {
123 PrintMemoryByte(str, before, byte, /*in_shadow*/true, after);
Kostya Serebryany9514a532012-12-19 09:53:32 +0000124}
125
Stephen Hines2d1fdb22014-05-28 23:58:16 -0700126static void PrintShadowBytes(InternalScopedString *str, const char *before,
127 u8 *bytes, u8 *guilty, uptr n) {
Kostya Serebryany9514a532012-12-19 09:53:32 +0000128 Decorator d;
Stephen Hines2d1fdb22014-05-28 23:58:16 -0700129 if (before) str->append("%s%p:", before, bytes);
Kostya Serebryany9514a532012-12-19 09:53:32 +0000130 for (uptr i = 0; i < n; i++) {
131 u8 *p = bytes + i;
Stephen Hines2d1fdb22014-05-28 23:58:16 -0700132 const char *before =
133 p == guilty ? "[" : (p - 1 == guilty && i != 0) ? "" : " ";
Kostya Serebryany9514a532012-12-19 09:53:32 +0000134 const char *after = p == guilty ? "]" : "";
Stephen Hines2d1fdb22014-05-28 23:58:16 -0700135 PrintShadowByte(str, before, *p, after);
Alexey Samsonov98737922012-08-10 15:13:05 +0000136 }
Stephen Hines2d1fdb22014-05-28 23:58:16 -0700137 str->append("\n");
Alexey Samsonov98737922012-08-10 15:13:05 +0000138}
139
Stephen Hines2d1fdb22014-05-28 23:58:16 -0700140static void PrintLegend(InternalScopedString *str) {
141 str->append(
142 "Shadow byte legend (one shadow byte represents %d "
143 "application bytes):\n",
144 (int)SHADOW_GRANULARITY);
145 PrintShadowByte(str, " Addressable: ", 0);
146 str->append(" Partially addressable: ");
147 for (u8 i = 1; i < SHADOW_GRANULARITY; i++) PrintShadowByte(str, "", i, " ");
148 str->append("\n");
149 PrintShadowByte(str, " Heap left redzone: ",
150 kAsanHeapLeftRedzoneMagic);
151 PrintShadowByte(str, " Heap right redzone: ",
152 kAsanHeapRightRedzoneMagic);
153 PrintShadowByte(str, " Freed heap region: ", kAsanHeapFreeMagic);
154 PrintShadowByte(str, " Stack left redzone: ",
155 kAsanStackLeftRedzoneMagic);
156 PrintShadowByte(str, " Stack mid redzone: ",
157 kAsanStackMidRedzoneMagic);
158 PrintShadowByte(str, " Stack right redzone: ",
159 kAsanStackRightRedzoneMagic);
160 PrintShadowByte(str, " Stack partial redzone: ",
161 kAsanStackPartialRedzoneMagic);
162 PrintShadowByte(str, " Stack after return: ",
163 kAsanStackAfterReturnMagic);
164 PrintShadowByte(str, " Stack use after scope: ",
165 kAsanStackUseAfterScopeMagic);
166 PrintShadowByte(str, " Global redzone: ", kAsanGlobalRedzoneMagic);
167 PrintShadowByte(str, " Global init order: ",
168 kAsanInitializationOrderMagic);
169 PrintShadowByte(str, " Poisoned by user: ",
170 kAsanUserPoisonedMemoryMagic);
171 PrintShadowByte(str, " Container overflow: ",
172 kAsanContiguousContainerOOBMagic);
Stephen Hines6d186232014-11-26 17:56:19 -0800173 PrintShadowByte(str, " Array cookie: ",
174 kAsanArrayCookieMagic);
175 PrintShadowByte(str, " Intra object redzone: ",
176 kAsanIntraObjectRedzone);
Stephen Hines2d1fdb22014-05-28 23:58:16 -0700177 PrintShadowByte(str, " ASan internal: ", kAsanInternalHeapMagic);
Stephen Hines86277eb2015-03-23 12:06:32 -0700178 PrintShadowByte(str, " Left alloca redzone: ", kAsanAllocaLeftMagic);
179 PrintShadowByte(str, " Right alloca redzone: ", kAsanAllocaRightMagic);
Alexey Samsonov98737922012-08-10 15:13:05 +0000180}
181
Stephen Hines6d186232014-11-26 17:56:19 -0800182void MaybeDumpInstructionBytes(uptr pc) {
183 if (!flags()->dump_instruction_bytes || (pc < GetPageSizeCached()))
184 return;
185 InternalScopedString str(1024);
186 str.append("First 16 instruction bytes at pc: ");
187 if (IsAccessibleMemoryRange(pc, 16)) {
188 for (int i = 0; i < 16; ++i) {
189 PrintMemoryByte(&str, "", ((u8 *)pc)[i], /*in_shadow*/false, " ");
190 }
191 str.append("\n");
192 } else {
193 str.append("unaccessible\n");
194 }
195 Report("%s", str.data());
196}
197
Kostya Serebryany95f630a2013-01-28 07:34:22 +0000198static void PrintShadowMemoryForAddress(uptr addr) {
Stephen Hines2d1fdb22014-05-28 23:58:16 -0700199 if (!AddrIsInMem(addr)) return;
Kostya Serebryany95f630a2013-01-28 07:34:22 +0000200 uptr shadow_addr = MemToShadow(addr);
201 const uptr n_bytes_per_row = 16;
202 uptr aligned_shadow = shadow_addr & ~(n_bytes_per_row - 1);
Stephen Hines2d1fdb22014-05-28 23:58:16 -0700203 InternalScopedString str(4096 * 8);
204 str.append("Shadow bytes around the buggy address:\n");
Kostya Serebryany95f630a2013-01-28 07:34:22 +0000205 for (int i = -5; i <= 5; i++) {
206 const char *prefix = (i == 0) ? "=>" : " ";
Stephen Hines2d1fdb22014-05-28 23:58:16 -0700207 PrintShadowBytes(&str, prefix, (u8 *)(aligned_shadow + i * n_bytes_per_row),
208 (u8 *)shadow_addr, n_bytes_per_row);
Kostya Serebryany95f630a2013-01-28 07:34:22 +0000209 }
Stephen Hines2d1fdb22014-05-28 23:58:16 -0700210 if (flags()->print_legend) PrintLegend(&str);
211 Printf("%s", str.data());
Kostya Serebryany95f630a2013-01-28 07:34:22 +0000212}
213
Alexey Samsonov98737922012-08-10 15:13:05 +0000214static void PrintZoneForPointer(uptr ptr, uptr zone_ptr,
215 const char *zone_name) {
216 if (zone_ptr) {
217 if (zone_name) {
Kostya Serebryany283c2962012-08-28 11:34:40 +0000218 Printf("malloc_zone_from_ptr(%p) = %p, which is %s\n",
Alexey Samsonov98737922012-08-10 15:13:05 +0000219 ptr, zone_ptr, zone_name);
220 } else {
Kostya Serebryany283c2962012-08-28 11:34:40 +0000221 Printf("malloc_zone_from_ptr(%p) = %p, which doesn't have a name\n",
Alexey Samsonov98737922012-08-10 15:13:05 +0000222 ptr, zone_ptr);
223 }
224 } else {
Kostya Serebryany283c2962012-08-28 11:34:40 +0000225 Printf("malloc_zone_from_ptr(%p) = 0\n", ptr);
Alexey Samsonov98737922012-08-10 15:13:05 +0000226 }
227}
228
Timur Iskhodzhanov997454a2013-09-10 08:36:21 +0000229static void DescribeThread(AsanThread *t) {
230 if (t)
231 DescribeThread(t->context());
232}
233
Alexey Samsonove218beb2012-08-09 09:06:52 +0000234// ---------------------- Address Descriptions ------------------- {{{1
235
Alexey Samsonove4bfca22012-08-09 09:27:24 +0000236static bool IsASCII(unsigned char c) {
Alexey Samsonov98737922012-08-10 15:13:05 +0000237 return /*0x00 <= c &&*/ c <= 0x7F;
Alexey Samsonove4bfca22012-08-09 09:27:24 +0000238}
239
Alexey Samsonovc9424272013-03-27 10:41:22 +0000240static const char *MaybeDemangleGlobalName(const char *name) {
241 // We can spoil names of globals with C linkage, so use an heuristic
242 // approach to check if the name should be demangled.
Stephen Hines2d1fdb22014-05-28 23:58:16 -0700243 bool should_demangle = false;
244 if (name[0] == '_' && name[1] == 'Z')
245 should_demangle = true;
246 else if (SANITIZER_WINDOWS && name[0] == '\01' && name[1] == '?')
247 should_demangle = true;
248
Stephen Hines6d186232014-11-26 17:56:19 -0800249 return should_demangle ? Symbolizer::GetOrInit()->Demangle(name) : name;
Alexey Samsonovc9424272013-03-27 10:41:22 +0000250}
251
Alexey Samsonov939316c2013-04-01 08:57:38 +0000252// Check if the global is a zero-terminated ASCII string. If so, print it.
Stephen Hines2d1fdb22014-05-28 23:58:16 -0700253static void PrintGlobalNameIfASCII(InternalScopedString *str,
254 const __asan_global &g) {
Alexey Samsonov939316c2013-04-01 08:57:38 +0000255 for (uptr p = g.beg; p < g.beg + g.size - 1; p++) {
256 unsigned char c = *(unsigned char*)p;
257 if (c == '\0' || !IsASCII(c)) return;
258 }
259 if (*(char*)(g.beg + g.size - 1) != '\0') return;
Stephen Hines2d1fdb22014-05-28 23:58:16 -0700260 str->append(" '%s' is ascii string '%s'\n", MaybeDemangleGlobalName(g.name),
261 (char *)g.beg);
Alexey Samsonov939316c2013-04-01 08:57:38 +0000262}
263
Stephen Hines6a211c52014-07-21 00:49:56 -0700264static const char *GlobalFilename(const __asan_global &g) {
265 const char *res = g.module_name;
266 // Prefer the filename from source location, if is available.
267 if (g.location)
268 res = g.location->filename;
269 CHECK(res);
270 return res;
271}
272
273static void PrintGlobalLocation(InternalScopedString *str,
274 const __asan_global &g) {
275 str->append("%s", GlobalFilename(g));
276 if (!g.location)
277 return;
278 if (g.location->line_no)
279 str->append(":%d", g.location->line_no);
280 if (g.location->column_no)
281 str->append(":%d", g.location->column_no);
282}
283
Pirama Arumuga Nainarcdce50b2015-07-01 12:26:56 -0700284static void DescribeAddressRelativeToGlobal(uptr addr, uptr size,
285 const __asan_global &g) {
Stephen Hines2d1fdb22014-05-28 23:58:16 -0700286 InternalScopedString str(4096);
Kostya Serebryany58f54552012-12-18 07:32:16 +0000287 Decorator d;
Stephen Hines2d1fdb22014-05-28 23:58:16 -0700288 str.append("%s", d.Location());
Alexey Samsonove4bfca22012-08-09 09:27:24 +0000289 if (addr < g.beg) {
Stephen Hines2d1fdb22014-05-28 23:58:16 -0700290 str.append("%p is located %zd bytes to the left", (void *)addr,
291 g.beg - addr);
Evgeniy Stepanov589dcda2013-02-05 14:32:03 +0000292 } else if (addr + size > g.beg + g.size) {
293 if (addr < g.beg + g.size)
294 addr = g.beg + g.size;
Stephen Hines2d1fdb22014-05-28 23:58:16 -0700295 str.append("%p is located %zd bytes to the right", (void *)addr,
296 addr - (g.beg + g.size));
Alexey Samsonove4bfca22012-08-09 09:27:24 +0000297 } else {
Evgeniy Stepanov589dcda2013-02-05 14:32:03 +0000298 // Can it happen?
Stephen Hines2d1fdb22014-05-28 23:58:16 -0700299 str.append("%p is located %zd bytes inside", (void *)addr, addr - g.beg);
Alexey Samsonove4bfca22012-08-09 09:27:24 +0000300 }
Stephen Hines6a211c52014-07-21 00:49:56 -0700301 str.append(" of global variable '%s' defined in '",
302 MaybeDemangleGlobalName(g.name));
303 PrintGlobalLocation(&str, g);
304 str.append("' (0x%zx) of size %zu\n", g.beg, g.size);
Stephen Hines2d1fdb22014-05-28 23:58:16 -0700305 str.append("%s", d.EndLocation());
306 PrintGlobalNameIfASCII(&str, g);
307 Printf("%s", str.data());
Pirama Arumuga Nainarcdce50b2015-07-01 12:26:56 -0700308}
309
310static bool DescribeAddressIfGlobal(uptr addr, uptr size,
311 const char *bug_type) {
312 // Assume address is close to at most four globals.
313 const int kMaxGlobalsInReport = 4;
314 __asan_global globals[kMaxGlobalsInReport];
315 u32 reg_sites[kMaxGlobalsInReport];
316 int globals_num =
317 GetGlobalsForAddress(addr, globals, reg_sites, ARRAY_SIZE(globals));
318 if (globals_num == 0)
319 return false;
320 for (int i = 0; i < globals_num; i++) {
321 DescribeAddressRelativeToGlobal(addr, size, globals[i]);
322 if (0 == internal_strcmp(bug_type, "initialization-order-fiasco") &&
323 reg_sites[i]) {
324 Printf(" registered at:\n");
325 StackDepotGet(reg_sites[i]).Print();
326 }
327 }
Alexey Samsonove4bfca22012-08-09 09:27:24 +0000328 return true;
329}
330
Stephen Hines6d186232014-11-26 17:56:19 -0800331bool DescribeAddressIfShadow(uptr addr, AddressDescription *descr, bool print) {
Alexey Samsonove218beb2012-08-09 09:06:52 +0000332 if (AddrIsInMem(addr))
333 return false;
Stephen Hines6d186232014-11-26 17:56:19 -0800334 const char *area_type = nullptr;
335 if (AddrIsInShadowGap(addr)) area_type = "shadow gap";
336 else if (AddrIsInHighShadow(addr)) area_type = "high shadow";
337 else if (AddrIsInLowShadow(addr)) area_type = "low shadow";
338 if (area_type != nullptr) {
339 if (print) {
340 Printf("Address %p is located in the %s area.\n", addr, area_type);
341 } else {
342 CHECK(descr);
343 descr->region_kind = area_type;
344 }
Alexey Samsonove218beb2012-08-09 09:06:52 +0000345 return true;
346 }
347 CHECK(0 && "Address is not in memory and not in shadow?");
348 return false;
349}
350
Kostya Serebryany50f3daa2013-03-22 10:36:24 +0000351// Return " (thread_name) " or an empty string if the name is empty.
352const char *ThreadNameWithParenthesis(AsanThreadContext *t, char buff[],
353 uptr buff_len) {
354 const char *name = t->name;
355 if (name[0] == '\0') return "";
356 buff[0] = 0;
357 internal_strncat(buff, " (", 3);
358 internal_strncat(buff, name, buff_len - 4);
359 internal_strncat(buff, ")", 2);
360 return buff;
361}
362
363const char *ThreadNameWithParenthesis(u32 tid, char buff[],
364 uptr buff_len) {
365 if (tid == kInvalidTid) return "";
366 asanThreadRegistry().CheckLocked();
367 AsanThreadContext *t = GetThreadContextByTidLocked(tid);
368 return ThreadNameWithParenthesis(t, buff, buff_len);
369}
370
Stephen Hines6d186232014-11-26 17:56:19 -0800371static void PrintAccessAndVarIntersection(const StackVarDescr &var, uptr addr,
372 uptr access_size, uptr prev_var_end,
373 uptr next_var_beg) {
374 uptr var_end = var.beg + var.size;
Kostya Serebryanyedb39c72013-09-03 13:58:04 +0000375 uptr addr_end = addr + access_size;
376 const char *pos_descr = 0;
Stephen Hines6d186232014-11-26 17:56:19 -0800377 // If the variable [var.beg, var_end) is the nearest variable to the
Kostya Serebryanyedb39c72013-09-03 13:58:04 +0000378 // current memory access, indicate it in the log.
Stephen Hines6d186232014-11-26 17:56:19 -0800379 if (addr >= var.beg) {
Kostya Serebryanyedb39c72013-09-03 13:58:04 +0000380 if (addr_end <= var_end)
381 pos_descr = "is inside"; // May happen if this is a use-after-return.
382 else if (addr < var_end)
383 pos_descr = "partially overflows";
384 else if (addr_end <= next_var_beg &&
385 next_var_beg - addr_end >= addr - var_end)
386 pos_descr = "overflows";
387 } else {
Stephen Hines6d186232014-11-26 17:56:19 -0800388 if (addr_end > var.beg)
Kostya Serebryanyedb39c72013-09-03 13:58:04 +0000389 pos_descr = "partially underflows";
390 else if (addr >= prev_var_end &&
Stephen Hines6d186232014-11-26 17:56:19 -0800391 addr - prev_var_end >= var.beg - addr_end)
Kostya Serebryanyedb39c72013-09-03 13:58:04 +0000392 pos_descr = "underflows";
393 }
Stephen Hines2d1fdb22014-05-28 23:58:16 -0700394 InternalScopedString str(1024);
Stephen Hines6d186232014-11-26 17:56:19 -0800395 str.append(" [%zd, %zd)", var.beg, var_end);
396 // Render variable name.
397 str.append(" '");
398 for (uptr i = 0; i < var.name_len; ++i) {
399 str.append("%c", var.name_pos[i]);
400 }
401 str.append("'");
Kostya Serebryanyedb39c72013-09-03 13:58:04 +0000402 if (pos_descr) {
403 Decorator d;
404 // FIXME: we may want to also print the size of the access here,
405 // but in case of accesses generated by memset it may be confusing.
Stephen Hines2d1fdb22014-05-28 23:58:16 -0700406 str.append("%s <== Memory access at offset %zd %s this variable%s\n",
407 d.Location(), addr, pos_descr, d.EndLocation());
Kostya Serebryanyedb39c72013-09-03 13:58:04 +0000408 } else {
Stephen Hines2d1fdb22014-05-28 23:58:16 -0700409 str.append("\n");
Kostya Serebryanyedb39c72013-09-03 13:58:04 +0000410 }
Stephen Hines2d1fdb22014-05-28 23:58:16 -0700411 Printf("%s", str.data());
Kostya Serebryanyedb39c72013-09-03 13:58:04 +0000412}
413
Stephen Hines6d186232014-11-26 17:56:19 -0800414bool ParseFrameDescription(const char *frame_descr,
415 InternalMmapVector<StackVarDescr> *vars) {
416 CHECK(frame_descr);
417 char *p;
418 // This string is created by the compiler and has the following form:
419 // "n alloc_1 alloc_2 ... alloc_n"
420 // where alloc_i looks like "offset size len ObjectName".
421 uptr n_objects = (uptr)internal_simple_strtoll(frame_descr, &p, 10);
422 if (n_objects == 0)
423 return false;
424
425 for (uptr i = 0; i < n_objects; i++) {
426 uptr beg = (uptr)internal_simple_strtoll(p, &p, 10);
427 uptr size = (uptr)internal_simple_strtoll(p, &p, 10);
428 uptr len = (uptr)internal_simple_strtoll(p, &p, 10);
429 if (beg == 0 || size == 0 || *p != ' ') {
430 return false;
431 }
432 p++;
433 StackVarDescr var = {beg, size, p, len};
434 vars->push_back(var);
435 p += len;
436 }
437
438 return true;
439}
Kostya Serebryanyedb39c72013-09-03 13:58:04 +0000440
Alexey Samsonove218beb2012-08-09 09:06:52 +0000441bool DescribeAddressIfStack(uptr addr, uptr access_size) {
Alexey Samsonovdef1be92013-03-21 11:23:41 +0000442 AsanThread *t = FindThreadByStackAddress(addr);
Alexey Samsonove218beb2012-08-09 09:06:52 +0000443 if (!t) return false;
Kostya Serebryanyd570bb42013-05-22 14:21:34 +0000444
Kostya Serebryany58f54552012-12-18 07:32:16 +0000445 Decorator d;
Stephen Hines6d186232014-11-26 17:56:19 -0800446 char tname[128];
Kostya Serebryany58f54552012-12-18 07:32:16 +0000447 Printf("%s", d.Location());
Stephen Hines6d186232014-11-26 17:56:19 -0800448 Printf("Address %p is located in stack of thread T%d%s", addr, t->tid(),
449 ThreadNameWithParenthesis(t->tid(), tname, sizeof(tname)));
450
451 // Try to fetch precise stack frame for this access.
452 AsanThread::StackFrameAccess access;
453 if (!t->GetStackFrameAccessByAddr(addr, &access)) {
454 Printf("%s\n", d.EndLocation());
455 return true;
456 }
457 Printf(" at offset %zu in frame%s\n", access.offset, d.EndLocation());
458
Kostya Serebryany50f3daa2013-03-22 10:36:24 +0000459 // Now we print the frame where the alloca has happened.
460 // We print this frame as a stack trace with one element.
461 // The symbolizer may print more than one frame if inlining was involved.
462 // The frame numbers may be different than those in the stack trace printed
463 // previously. That's unfortunate, but I have no better solution,
464 // especially given that the alloca may be from entirely different place
465 // (e.g. use-after-scope, or different thread's stack).
Stephen Hines6d186232014-11-26 17:56:19 -0800466#if defined(__powerpc64__) && defined(__BIG_ENDIAN__)
467 // On PowerPC64 ELFv1, the address of a function actually points to a
468 // three-doubleword data structure with the first field containing
469 // the address of the function's code.
470 access.frame_pc = *reinterpret_cast<uptr *>(access.frame_pc);
471#endif
472 access.frame_pc += 16;
Kostya Serebryany58f54552012-12-18 07:32:16 +0000473 Printf("%s", d.EndLocation());
Stephen Hines6d186232014-11-26 17:56:19 -0800474 StackTrace alloca_stack(&access.frame_pc, 1);
Stephen Hines2d1fdb22014-05-28 23:58:16 -0700475 alloca_stack.Print();
Stephen Hines6d186232014-11-26 17:56:19 -0800476
477 InternalMmapVector<StackVarDescr> vars(16);
478 if (!ParseFrameDescription(access.frame_descr, &vars)) {
479 Printf("AddressSanitizer can't parse the stack frame "
480 "descriptor: |%s|\n", access.frame_descr);
481 // 'addr' is a stack address, so return true even if we can't parse frame
482 return true;
483 }
484 uptr n_objects = vars.size();
Alexey Samsonove218beb2012-08-09 09:06:52 +0000485 // Report the number of stack objects.
Kostya Serebryany283c2962012-08-28 11:34:40 +0000486 Printf(" This frame has %zu object(s):\n", n_objects);
Kostya Serebryanyedb39c72013-09-03 13:58:04 +0000487
Alexey Samsonove218beb2012-08-09 09:06:52 +0000488 // Report all objects in this frame.
Kostya Serebryany89fe5642013-09-03 14:53:02 +0000489 for (uptr i = 0; i < n_objects; i++) {
Kostya Serebryanyedb39c72013-09-03 13:58:04 +0000490 uptr prev_var_end = i ? vars[i - 1].beg + vars[i - 1].size : 0;
Timur Iskhodzhanov22917e92013-09-03 15:09:21 +0000491 uptr next_var_beg = i + 1 < n_objects ? vars[i + 1].beg : ~(0UL);
Stephen Hines6d186232014-11-26 17:56:19 -0800492 PrintAccessAndVarIntersection(vars[i], access.offset, access_size,
Kostya Serebryanyedb39c72013-09-03 13:58:04 +0000493 prev_var_end, next_var_beg);
Alexey Samsonove218beb2012-08-09 09:06:52 +0000494 }
Kostya Serebryany283c2962012-08-28 11:34:40 +0000495 Printf("HINT: this may be a false positive if your program uses "
Stephen Hines6d186232014-11-26 17:56:19 -0800496 "some custom stack unwind mechanism or swapcontext\n");
497 if (SANITIZER_WINDOWS)
498 Printf(" (longjmp, SEH and C++ exceptions *are* supported)\n");
499 else
500 Printf(" (longjmp and C++ exceptions *are* supported)\n");
501
Timur Iskhodzhanov997454a2013-09-10 08:36:21 +0000502 DescribeThread(t);
Alexey Samsonove218beb2012-08-09 09:06:52 +0000503 return true;
504}
505
Alexey Samsonov5c153fa2012-09-18 07:38:10 +0000506static void DescribeAccessToHeapChunk(AsanChunkView chunk, uptr addr,
507 uptr access_size) {
Evgeniy Stepanov589dcda2013-02-05 14:32:03 +0000508 sptr offset;
Kostya Serebryany58f54552012-12-18 07:32:16 +0000509 Decorator d;
Stephen Hines2d1fdb22014-05-28 23:58:16 -0700510 InternalScopedString str(4096);
511 str.append("%s", d.Location());
Evgeniy Stepanov589dcda2013-02-05 14:32:03 +0000512 if (chunk.AddrIsAtLeft(addr, access_size, &offset)) {
Stephen Hines2d1fdb22014-05-28 23:58:16 -0700513 str.append("%p is located %zd bytes to the left of", (void *)addr, offset);
Alexey Samsonov5c153fa2012-09-18 07:38:10 +0000514 } else if (chunk.AddrIsAtRight(addr, access_size, &offset)) {
Evgeniy Stepanov589dcda2013-02-05 14:32:03 +0000515 if (offset < 0) {
516 addr -= offset;
517 offset = 0;
518 }
Stephen Hines2d1fdb22014-05-28 23:58:16 -0700519 str.append("%p is located %zd bytes to the right of", (void *)addr, offset);
Evgeniy Stepanov589dcda2013-02-05 14:32:03 +0000520 } else if (chunk.AddrIsInside(addr, access_size, &offset)) {
Stephen Hines2d1fdb22014-05-28 23:58:16 -0700521 str.append("%p is located %zd bytes inside of", (void*)addr, offset);
Alexey Samsonov5c153fa2012-09-18 07:38:10 +0000522 } else {
Stephen Hines2d1fdb22014-05-28 23:58:16 -0700523 str.append("%p is located somewhere around (this is AddressSanitizer bug!)",
524 (void *)addr);
Alexey Samsonov5c153fa2012-09-18 07:38:10 +0000525 }
Stephen Hines2d1fdb22014-05-28 23:58:16 -0700526 str.append(" %zu-byte region [%p,%p)\n", chunk.UsedSize(),
527 (void *)(chunk.Beg()), (void *)(chunk.End()));
528 str.append("%s", d.EndLocation());
529 Printf("%s", str.data());
Alexey Samsonov5c153fa2012-09-18 07:38:10 +0000530}
531
532void DescribeHeapAddress(uptr addr, uptr access_size) {
533 AsanChunkView chunk = FindHeapChunkByAddress(addr);
Alexey Samsonovd9def292013-10-14 11:13:54 +0000534 if (!chunk.IsValid()) {
535 Printf("AddressSanitizer can not describe address in more detail "
536 "(wild memory access suspected).\n");
537 return;
538 }
Alexey Samsonov5c153fa2012-09-18 07:38:10 +0000539 DescribeAccessToHeapChunk(chunk, addr, access_size);
540 CHECK(chunk.AllocTid() != kInvalidTid);
Alexey Samsonovdef1be92013-03-21 11:23:41 +0000541 asanThreadRegistry().CheckLocked();
542 AsanThreadContext *alloc_thread =
543 GetThreadContextByTidLocked(chunk.AllocTid());
Stephen Hines6d186232014-11-26 17:56:19 -0800544 StackTrace alloc_stack = chunk.GetAllocStack();
Kostya Serebryany716e2f22012-12-07 15:15:01 +0000545 char tname[128];
Kostya Serebryany58f54552012-12-18 07:32:16 +0000546 Decorator d;
Timur Iskhodzhanov997454a2013-09-10 08:36:21 +0000547 AsanThreadContext *free_thread = 0;
Alexey Samsonov5c153fa2012-09-18 07:38:10 +0000548 if (chunk.FreeTid() != kInvalidTid) {
Timur Iskhodzhanov997454a2013-09-10 08:36:21 +0000549 free_thread = GetThreadContextByTidLocked(chunk.FreeTid());
Kostya Serebryany58f54552012-12-18 07:32:16 +0000550 Printf("%sfreed by thread T%d%s here:%s\n", d.Allocation(),
Alexey Samsonovdef1be92013-03-21 11:23:41 +0000551 free_thread->tid,
Kostya Serebryany58f54552012-12-18 07:32:16 +0000552 ThreadNameWithParenthesis(free_thread, tname, sizeof(tname)),
553 d.EndAllocation());
Stephen Hines6d186232014-11-26 17:56:19 -0800554 StackTrace free_stack = chunk.GetFreeStack();
Stephen Hines2d1fdb22014-05-28 23:58:16 -0700555 free_stack.Print();
Kostya Serebryany58f54552012-12-18 07:32:16 +0000556 Printf("%spreviously allocated by thread T%d%s here:%s\n",
Alexey Samsonovdef1be92013-03-21 11:23:41 +0000557 d.Allocation(), alloc_thread->tid,
Kostya Serebryany58f54552012-12-18 07:32:16 +0000558 ThreadNameWithParenthesis(alloc_thread, tname, sizeof(tname)),
559 d.EndAllocation());
Alexey Samsonov5c153fa2012-09-18 07:38:10 +0000560 } else {
Kostya Serebryany58f54552012-12-18 07:32:16 +0000561 Printf("%sallocated by thread T%d%s here:%s\n", d.Allocation(),
Alexey Samsonovdef1be92013-03-21 11:23:41 +0000562 alloc_thread->tid,
Kostya Serebryany58f54552012-12-18 07:32:16 +0000563 ThreadNameWithParenthesis(alloc_thread, tname, sizeof(tname)),
564 d.EndAllocation());
Alexey Samsonov5c153fa2012-09-18 07:38:10 +0000565 }
Stephen Hines2d1fdb22014-05-28 23:58:16 -0700566 alloc_stack.Print();
Timur Iskhodzhanov997454a2013-09-10 08:36:21 +0000567 DescribeThread(GetCurrentThread());
568 if (free_thread)
569 DescribeThread(free_thread);
570 DescribeThread(alloc_thread);
Alexey Samsonov5c153fa2012-09-18 07:38:10 +0000571}
572
Pirama Arumuga Nainarcdce50b2015-07-01 12:26:56 -0700573static void DescribeAddress(uptr addr, uptr access_size, const char *bug_type) {
Alexey Samsonove218beb2012-08-09 09:06:52 +0000574 // Check if this is shadow or shadow gap.
575 if (DescribeAddressIfShadow(addr))
576 return;
577 CHECK(AddrIsInMem(addr));
Pirama Arumuga Nainarcdce50b2015-07-01 12:26:56 -0700578 if (DescribeAddressIfGlobal(addr, access_size, bug_type))
Alexey Samsonove218beb2012-08-09 09:06:52 +0000579 return;
580 if (DescribeAddressIfStack(addr, access_size))
581 return;
582 // Assume it is a heap address.
583 DescribeHeapAddress(addr, access_size);
584}
585
Alexey Samsonov71b42c92012-09-05 07:37:15 +0000586// ------------------- Thread description -------------------- {{{1
587
Alexey Samsonovdef1be92013-03-21 11:23:41 +0000588void DescribeThread(AsanThreadContext *context) {
589 CHECK(context);
590 asanThreadRegistry().CheckLocked();
Alexey Samsonov71b42c92012-09-05 07:37:15 +0000591 // No need to announce the main thread.
Alexey Samsonovdef1be92013-03-21 11:23:41 +0000592 if (context->tid == 0 || context->announced) {
Alexey Samsonov71b42c92012-09-05 07:37:15 +0000593 return;
594 }
Alexey Samsonovdef1be92013-03-21 11:23:41 +0000595 context->announced = true;
Kostya Serebryany716e2f22012-12-07 15:15:01 +0000596 char tname[128];
Stephen Hines2d1fdb22014-05-28 23:58:16 -0700597 InternalScopedString str(1024);
598 str.append("Thread T%d%s", context->tid,
599 ThreadNameWithParenthesis(context->tid, tname, sizeof(tname)));
Pirama Arumuga Nainar259f7062015-05-06 11:49:53 -0700600 if (context->parent_tid == kInvalidTid) {
601 str.append(" created by unknown thread\n");
602 Printf("%s", str.data());
603 return;
604 }
Stephen Hines2d1fdb22014-05-28 23:58:16 -0700605 str.append(
606 " created by T%d%s here:\n", context->parent_tid,
607 ThreadNameWithParenthesis(context->parent_tid, tname, sizeof(tname)));
608 Printf("%s", str.data());
Stephen Hines6d186232014-11-26 17:56:19 -0800609 StackDepotGet(context->stack_id).Print();
Alexey Samsonov71b42c92012-09-05 07:37:15 +0000610 // Recursively described parent thread if needed.
611 if (flags()->print_full_thread_history) {
Alexey Samsonovdef1be92013-03-21 11:23:41 +0000612 AsanThreadContext *parent_context =
613 GetThreadContextByTidLocked(context->parent_tid);
614 DescribeThread(parent_context);
Alexey Samsonov71b42c92012-09-05 07:37:15 +0000615 }
616}
617
Alexey Samsonove218beb2012-08-09 09:06:52 +0000618// -------------------- Different kinds of reports ----------------- {{{1
619
Alexey Samsonov98737922012-08-10 15:13:05 +0000620// Use ScopedInErrorReport to run common actions just before and
621// immediately after printing error report.
622class ScopedInErrorReport {
623 public:
Stephen Hines6d186232014-11-26 17:56:19 -0800624 explicit ScopedInErrorReport(ReportData *report = nullptr) {
Alexey Samsonov98737922012-08-10 15:13:05 +0000625 static atomic_uint32_t num_calls;
Alexey Samsonov62e27092012-09-17 08:02:19 +0000626 static u32 reporting_thread_tid;
Alexey Samsonov98737922012-08-10 15:13:05 +0000627 if (atomic_fetch_add(&num_calls, 1, memory_order_relaxed) != 0) {
628 // Do not print more than one report, otherwise they will mix up.
629 // Error reporting functions shouldn't return at this situation, as
630 // they are defined as no-return.
Stephen Hines6d186232014-11-26 17:56:19 -0800631 Report("AddressSanitizer: while reporting a bug found another one. "
Alexey Samsonov98737922012-08-10 15:13:05 +0000632 "Ignoring.\n");
Alexey Samsonov89c13842013-03-20 09:23:28 +0000633 u32 current_tid = GetCurrentTidOrInvalid();
Alexey Samsonov62e27092012-09-17 08:02:19 +0000634 if (current_tid != reporting_thread_tid) {
635 // ASan found two bugs in different threads simultaneously. Sleep
636 // long enough to make sure that the thread which started to print
637 // an error report will finish doing it.
638 SleepForSeconds(Max(100, flags()->sleep_before_dying + 1));
639 }
Alexey Samsonovf8822472013-02-20 13:54:32 +0000640 // If we're still not dead for some reason, use raw _exit() instead of
Alexey Samsonov031633b2012-11-19 11:22:22 +0000641 // Die() to bypass any additional checks.
Alexey Samsonovf8822472013-02-20 13:54:32 +0000642 internal__exit(flags()->exitcode);
Alexey Samsonov98737922012-08-10 15:13:05 +0000643 }
Stephen Hines6d186232014-11-26 17:56:19 -0800644 if (report) report_data = *report;
645 report_happened = true;
Alexey Samsonov6a08d292012-12-07 22:01:28 +0000646 ASAN_ON_ERROR();
Alexey Samsonov7ed46ff2013-04-05 07:30:29 +0000647 // Make sure the registry and sanitizer report mutexes are locked while
648 // we're printing an error report.
649 // We can lock them only here to avoid self-deadlock in case of
Alexey Samsonovdef1be92013-03-21 11:23:41 +0000650 // recursive reports.
651 asanThreadRegistry().Lock();
Alexey Samsonov7ed46ff2013-04-05 07:30:29 +0000652 CommonSanitizerReportMutex.Lock();
Alexey Samsonov89c13842013-03-20 09:23:28 +0000653 reporting_thread_tid = GetCurrentTidOrInvalid();
Kostya Serebryany283c2962012-08-28 11:34:40 +0000654 Printf("===================================================="
Alexey Samsonov62e27092012-09-17 08:02:19 +0000655 "=============\n");
Alexey Samsonov98737922012-08-10 15:13:05 +0000656 }
657 // Destructor is NORETURN, as functions that report errors are.
658 NORETURN ~ScopedInErrorReport() {
659 // Make sure the current thread is announced.
Timur Iskhodzhanov997454a2013-09-10 08:36:21 +0000660 DescribeThread(GetCurrentThread());
Stephen Hines2d1fdb22014-05-28 23:58:16 -0700661 // We may want to grab this lock again when printing stats.
662 asanThreadRegistry().Unlock();
Alexey Samsonov98737922012-08-10 15:13:05 +0000663 // Print memory stats.
Kostya Serebryany95f630a2013-01-28 07:34:22 +0000664 if (flags()->print_stats)
665 __asan_print_accumulated_stats();
Alexey Samsonov98737922012-08-10 15:13:05 +0000666 if (error_report_callback) {
667 error_report_callback(error_message_buffer);
668 }
Kostya Serebryany283c2962012-08-28 11:34:40 +0000669 Report("ABORTING\n");
Alexey Samsonov98737922012-08-10 15:13:05 +0000670 Die();
671 }
672};
673
Stephen Hines86277eb2015-03-23 12:06:32 -0700674void ReportStackOverflow(const SignalContext &sig) {
Alexey Samsonov98737922012-08-10 15:13:05 +0000675 ScopedInErrorReport in_report;
Kostya Serebryany58f54552012-12-18 07:32:16 +0000676 Decorator d;
677 Printf("%s", d.Warning());
Stephen Hines2d1fdb22014-05-28 23:58:16 -0700678 Report(
679 "ERROR: AddressSanitizer: stack-overflow on address %p"
Stephen Hines6d186232014-11-26 17:56:19 -0800680 " (pc %p bp %p sp %p T%d)\n",
Stephen Hines86277eb2015-03-23 12:06:32 -0700681 (void *)sig.addr, (void *)sig.pc, (void *)sig.bp, (void *)sig.sp,
Stephen Hines2d1fdb22014-05-28 23:58:16 -0700682 GetCurrentTidOrInvalid());
Kostya Serebryany58f54552012-12-18 07:32:16 +0000683 Printf("%s", d.EndWarning());
Stephen Hines86277eb2015-03-23 12:06:32 -0700684 GET_STACK_TRACE_SIGNAL(sig);
Stephen Hines2d1fdb22014-05-28 23:58:16 -0700685 stack.Print();
686 ReportErrorSummary("stack-overflow", &stack);
687}
688
Stephen Hines86277eb2015-03-23 12:06:32 -0700689void ReportSIGSEGV(const char *description, const SignalContext &sig) {
Stephen Hines2d1fdb22014-05-28 23:58:16 -0700690 ScopedInErrorReport in_report;
691 Decorator d;
692 Printf("%s", d.Warning());
693 Report(
Stephen Hines6d186232014-11-26 17:56:19 -0800694 "ERROR: AddressSanitizer: %s on unknown address %p"
695 " (pc %p bp %p sp %p T%d)\n",
Stephen Hines86277eb2015-03-23 12:06:32 -0700696 description, (void *)sig.addr, (void *)sig.pc, (void *)sig.bp,
697 (void *)sig.sp, GetCurrentTidOrInvalid());
698 if (sig.pc < GetPageSizeCached()) {
Stephen Hines6d186232014-11-26 17:56:19 -0800699 Report("Hint: pc points to the zero page.\n");
700 }
Stephen Hines2d1fdb22014-05-28 23:58:16 -0700701 Printf("%s", d.EndWarning());
Stephen Hines86277eb2015-03-23 12:06:32 -0700702 GET_STACK_TRACE_SIGNAL(sig);
Stephen Hines2d1fdb22014-05-28 23:58:16 -0700703 stack.Print();
Stephen Hines86277eb2015-03-23 12:06:32 -0700704 MaybeDumpInstructionBytes(sig.pc);
Alexey Samsonovd9def292013-10-14 11:13:54 +0000705 Printf("AddressSanitizer can not provide additional info.\n");
Alexey Samsonov2fb08722013-11-01 17:02:14 +0000706 ReportErrorSummary("SEGV", &stack);
Alexey Samsonov73545092012-08-09 07:40:58 +0000707}
708
Stephen Hines6d186232014-11-26 17:56:19 -0800709void ReportDoubleFree(uptr addr, BufferedStackTrace *free_stack) {
Alexey Samsonov98737922012-08-10 15:13:05 +0000710 ScopedInErrorReport in_report;
Kostya Serebryany58f54552012-12-18 07:32:16 +0000711 Decorator d;
712 Printf("%s", d.Warning());
Kostya Serebryanya89a35a2013-03-26 08:01:37 +0000713 char tname[128];
714 u32 curr_tid = GetCurrentTidOrInvalid();
715 Report("ERROR: AddressSanitizer: attempting double-free on %p in "
716 "thread T%d%s:\n",
717 addr, curr_tid,
718 ThreadNameWithParenthesis(curr_tid, tname, sizeof(tname)));
Kostya Serebryany58f54552012-12-18 07:32:16 +0000719 Printf("%s", d.EndWarning());
Alexey Samsonov1b17f5b2013-11-13 14:46:58 +0000720 CHECK_GT(free_stack->size, 0);
721 GET_STACK_TRACE_FATAL(free_stack->trace[0], free_stack->top_frame_bp);
Stephen Hines2d1fdb22014-05-28 23:58:16 -0700722 stack.Print();
Alexey Samsonovf7c1d182012-08-09 08:15:46 +0000723 DescribeHeapAddress(addr, 1);
Alexey Samsonov1b17f5b2013-11-13 14:46:58 +0000724 ReportErrorSummary("double-free", &stack);
Alexey Samsonovf7c1d182012-08-09 08:15:46 +0000725}
726
Stephen Hines6d186232014-11-26 17:56:19 -0800727void ReportNewDeleteSizeMismatch(uptr addr, uptr delete_size,
728 BufferedStackTrace *free_stack) {
729 ScopedInErrorReport in_report;
730 Decorator d;
731 Printf("%s", d.Warning());
732 char tname[128];
733 u32 curr_tid = GetCurrentTidOrInvalid();
734 Report("ERROR: AddressSanitizer: new-delete-type-mismatch on %p in "
735 "thread T%d%s:\n",
736 addr, curr_tid,
737 ThreadNameWithParenthesis(curr_tid, tname, sizeof(tname)));
738 Printf("%s object passed to delete has wrong type:\n", d.EndWarning());
739 Printf(" size of the allocated type: %zd bytes;\n"
740 " size of the deallocated type: %zd bytes.\n",
741 asan_mz_size(reinterpret_cast<void*>(addr)), delete_size);
742 CHECK_GT(free_stack->size, 0);
743 GET_STACK_TRACE_FATAL(free_stack->trace[0], free_stack->top_frame_bp);
744 stack.Print();
745 DescribeHeapAddress(addr, 1);
746 ReportErrorSummary("new-delete-type-mismatch", &stack);
747 Report("HINT: if you don't care about these warnings you may set "
748 "ASAN_OPTIONS=new_delete_type_mismatch=0\n");
749}
750
751void ReportFreeNotMalloced(uptr addr, BufferedStackTrace *free_stack) {
Alexey Samsonov98737922012-08-10 15:13:05 +0000752 ScopedInErrorReport in_report;
Kostya Serebryany58f54552012-12-18 07:32:16 +0000753 Decorator d;
754 Printf("%s", d.Warning());
Kostya Serebryanya89a35a2013-03-26 08:01:37 +0000755 char tname[128];
756 u32 curr_tid = GetCurrentTidOrInvalid();
Kostya Serebryany69d8ede2012-10-15 13:04:58 +0000757 Report("ERROR: AddressSanitizer: attempting free on address "
Kostya Serebryanya89a35a2013-03-26 08:01:37 +0000758 "which was not malloc()-ed: %p in thread T%d%s\n", addr,
759 curr_tid, ThreadNameWithParenthesis(curr_tid, tname, sizeof(tname)));
Kostya Serebryany58f54552012-12-18 07:32:16 +0000760 Printf("%s", d.EndWarning());
Alexey Samsonov1b17f5b2013-11-13 14:46:58 +0000761 CHECK_GT(free_stack->size, 0);
762 GET_STACK_TRACE_FATAL(free_stack->trace[0], free_stack->top_frame_bp);
Stephen Hines2d1fdb22014-05-28 23:58:16 -0700763 stack.Print();
Alexey Samsonov98737922012-08-10 15:13:05 +0000764 DescribeHeapAddress(addr, 1);
Alexey Samsonov1b17f5b2013-11-13 14:46:58 +0000765 ReportErrorSummary("bad-free", &stack);
Alexey Samsonovf7c1d182012-08-09 08:15:46 +0000766}
767
Stephen Hines6d186232014-11-26 17:56:19 -0800768void ReportAllocTypeMismatch(uptr addr, BufferedStackTrace *free_stack,
Kostya Serebryanyfe6d9162012-12-21 08:53:59 +0000769 AllocType alloc_type,
770 AllocType dealloc_type) {
771 static const char *alloc_names[] =
772 {"INVALID", "malloc", "operator new", "operator new []"};
773 static const char *dealloc_names[] =
774 {"INVALID", "free", "operator delete", "operator delete []"};
775 CHECK_NE(alloc_type, dealloc_type);
776 ScopedInErrorReport in_report;
777 Decorator d;
778 Printf("%s", d.Warning());
779 Report("ERROR: AddressSanitizer: alloc-dealloc-mismatch (%s vs %s) on %p\n",
780 alloc_names[alloc_type], dealloc_names[dealloc_type], addr);
781 Printf("%s", d.EndWarning());
Alexey Samsonov1b17f5b2013-11-13 14:46:58 +0000782 CHECK_GT(free_stack->size, 0);
783 GET_STACK_TRACE_FATAL(free_stack->trace[0], free_stack->top_frame_bp);
Stephen Hines2d1fdb22014-05-28 23:58:16 -0700784 stack.Print();
Kostya Serebryanyfe6d9162012-12-21 08:53:59 +0000785 DescribeHeapAddress(addr, 1);
Alexey Samsonov1b17f5b2013-11-13 14:46:58 +0000786 ReportErrorSummary("alloc-dealloc-mismatch", &stack);
Kostya Serebryanyfe6d9162012-12-21 08:53:59 +0000787 Report("HINT: if you don't care about these warnings you may set "
788 "ASAN_OPTIONS=alloc_dealloc_mismatch=0\n");
789}
790
Stephen Hines6d186232014-11-26 17:56:19 -0800791void ReportMallocUsableSizeNotOwned(uptr addr, BufferedStackTrace *stack) {
Alexey Samsonov98737922012-08-10 15:13:05 +0000792 ScopedInErrorReport in_report;
Kostya Serebryany58f54552012-12-18 07:32:16 +0000793 Decorator d;
794 Printf("%s", d.Warning());
Kostya Serebryany69d8ede2012-10-15 13:04:58 +0000795 Report("ERROR: AddressSanitizer: attempting to call "
Alexey Samsonovf7c1d182012-08-09 08:15:46 +0000796 "malloc_usable_size() for pointer which is "
797 "not owned: %p\n", addr);
Kostya Serebryany58f54552012-12-18 07:32:16 +0000798 Printf("%s", d.EndWarning());
Stephen Hines2d1fdb22014-05-28 23:58:16 -0700799 stack->Print();
Alexey Samsonovf7c1d182012-08-09 08:15:46 +0000800 DescribeHeapAddress(addr, 1);
Alexey Samsonov2fb08722013-11-01 17:02:14 +0000801 ReportErrorSummary("bad-malloc_usable_size", stack);
Alexey Samsonovf7c1d182012-08-09 08:15:46 +0000802}
803
Stephen Hines6d186232014-11-26 17:56:19 -0800804void ReportSanitizerGetAllocatedSizeNotOwned(uptr addr,
805 BufferedStackTrace *stack) {
Alexey Samsonov98737922012-08-10 15:13:05 +0000806 ScopedInErrorReport in_report;
Kostya Serebryany58f54552012-12-18 07:32:16 +0000807 Decorator d;
808 Printf("%s", d.Warning());
Kostya Serebryany69d8ede2012-10-15 13:04:58 +0000809 Report("ERROR: AddressSanitizer: attempting to call "
Stephen Hines6a211c52014-07-21 00:49:56 -0700810 "__sanitizer_get_allocated_size() for pointer which is "
Alexey Samsonovf7c1d182012-08-09 08:15:46 +0000811 "not owned: %p\n", addr);
Kostya Serebryany58f54552012-12-18 07:32:16 +0000812 Printf("%s", d.EndWarning());
Stephen Hines2d1fdb22014-05-28 23:58:16 -0700813 stack->Print();
Alexey Samsonovf7c1d182012-08-09 08:15:46 +0000814 DescribeHeapAddress(addr, 1);
Stephen Hines6a211c52014-07-21 00:49:56 -0700815 ReportErrorSummary("bad-__sanitizer_get_allocated_size", stack);
Alexey Samsonovf7c1d182012-08-09 08:15:46 +0000816}
817
Stephen Hines6d186232014-11-26 17:56:19 -0800818void ReportStringFunctionMemoryRangesOverlap(const char *function,
819 const char *offset1, uptr length1,
820 const char *offset2, uptr length2,
821 BufferedStackTrace *stack) {
Alexey Samsonov98737922012-08-10 15:13:05 +0000822 ScopedInErrorReport in_report;
Kostya Serebryany58f54552012-12-18 07:32:16 +0000823 Decorator d;
Kostya Serebryany2673fd82013-02-06 12:36:49 +0000824 char bug_type[100];
825 internal_snprintf(bug_type, sizeof(bug_type), "%s-param-overlap", function);
Kostya Serebryany58f54552012-12-18 07:32:16 +0000826 Printf("%s", d.Warning());
Kostya Serebryany2673fd82013-02-06 12:36:49 +0000827 Report("ERROR: AddressSanitizer: %s: "
Alexey Samsonov487fee72012-08-09 08:32:33 +0000828 "memory ranges [%p,%p) and [%p, %p) overlap\n", \
Kostya Serebryany2673fd82013-02-06 12:36:49 +0000829 bug_type, offset1, offset1 + length1, offset2, offset2 + length2);
Kostya Serebryany58f54552012-12-18 07:32:16 +0000830 Printf("%s", d.EndWarning());
Stephen Hines2d1fdb22014-05-28 23:58:16 -0700831 stack->Print();
Pirama Arumuga Nainarcdce50b2015-07-01 12:26:56 -0700832 DescribeAddress((uptr)offset1, length1, bug_type);
833 DescribeAddress((uptr)offset2, length2, bug_type);
Alexey Samsonov2fb08722013-11-01 17:02:14 +0000834 ReportErrorSummary(bug_type, stack);
Alexey Samsonov487fee72012-08-09 08:32:33 +0000835}
Alexey Samsonovf7c1d182012-08-09 08:15:46 +0000836
Stephen Hines2d1fdb22014-05-28 23:58:16 -0700837void ReportStringFunctionSizeOverflow(uptr offset, uptr size,
Stephen Hines6d186232014-11-26 17:56:19 -0800838 BufferedStackTrace *stack) {
Stephen Hines2d1fdb22014-05-28 23:58:16 -0700839 ScopedInErrorReport in_report;
840 Decorator d;
841 const char *bug_type = "negative-size-param";
842 Printf("%s", d.Warning());
843 Report("ERROR: AddressSanitizer: %s: (size=%zd)\n", bug_type, size);
844 Printf("%s", d.EndWarning());
845 stack->Print();
Pirama Arumuga Nainarcdce50b2015-07-01 12:26:56 -0700846 DescribeAddress(offset, size, bug_type);
Stephen Hines2d1fdb22014-05-28 23:58:16 -0700847 ReportErrorSummary(bug_type, stack);
848}
849
850void ReportBadParamsToAnnotateContiguousContainer(uptr beg, uptr end,
851 uptr old_mid, uptr new_mid,
Stephen Hines6d186232014-11-26 17:56:19 -0800852 BufferedStackTrace *stack) {
Stephen Hines2d1fdb22014-05-28 23:58:16 -0700853 ScopedInErrorReport in_report;
854 Report("ERROR: AddressSanitizer: bad parameters to "
855 "__sanitizer_annotate_contiguous_container:\n"
856 " beg : %p\n"
857 " end : %p\n"
858 " old_mid : %p\n"
859 " new_mid : %p\n",
860 beg, end, old_mid, new_mid);
Stephen Hines86277eb2015-03-23 12:06:32 -0700861 uptr granularity = SHADOW_GRANULARITY;
862 if (!IsAligned(beg, granularity))
863 Report("ERROR: beg is not aligned by %d\n", granularity);
Stephen Hines2d1fdb22014-05-28 23:58:16 -0700864 stack->Print();
865 ReportErrorSummary("bad-__sanitizer_annotate_contiguous_container", stack);
866}
867
Stephen Hines6a211c52014-07-21 00:49:56 -0700868void ReportODRViolation(const __asan_global *g1, u32 stack_id1,
869 const __asan_global *g2, u32 stack_id2) {
Stephen Hines2d1fdb22014-05-28 23:58:16 -0700870 ScopedInErrorReport in_report;
871 Decorator d;
872 Printf("%s", d.Warning());
873 Report("ERROR: AddressSanitizer: odr-violation (%p):\n", g1->beg);
874 Printf("%s", d.EndWarning());
Stephen Hines6a211c52014-07-21 00:49:56 -0700875 InternalScopedString g1_loc(256), g2_loc(256);
876 PrintGlobalLocation(&g1_loc, *g1);
877 PrintGlobalLocation(&g2_loc, *g2);
Stephen Hines6d186232014-11-26 17:56:19 -0800878 Printf(" [1] size=%zd '%s' %s\n", g1->size,
879 MaybeDemangleGlobalName(g1->name), g1_loc.data());
880 Printf(" [2] size=%zd '%s' %s\n", g2->size,
881 MaybeDemangleGlobalName(g2->name), g2_loc.data());
Stephen Hines6a211c52014-07-21 00:49:56 -0700882 if (stack_id1 && stack_id2) {
883 Printf("These globals were registered at these points:\n");
884 Printf(" [1]:\n");
Stephen Hines6d186232014-11-26 17:56:19 -0800885 StackDepotGet(stack_id1).Print();
Stephen Hines6a211c52014-07-21 00:49:56 -0700886 Printf(" [2]:\n");
Stephen Hines6d186232014-11-26 17:56:19 -0800887 StackDepotGet(stack_id2).Print();
Stephen Hines6a211c52014-07-21 00:49:56 -0700888 }
Stephen Hines2d1fdb22014-05-28 23:58:16 -0700889 Report("HINT: if you don't care about these warnings you may set "
890 "ASAN_OPTIONS=detect_odr_violation=0\n");
Stephen Hines6d186232014-11-26 17:56:19 -0800891 InternalScopedString error_msg(256);
892 error_msg.append("odr-violation: global '%s' at %s",
893 MaybeDemangleGlobalName(g1->name), g1_loc.data());
894 ReportErrorSummary(error_msg.data());
Stephen Hines2d1fdb22014-05-28 23:58:16 -0700895}
896
897// ----------------------- CheckForInvalidPointerPair ----------- {{{1
898static NOINLINE void
899ReportInvalidPointerPair(uptr pc, uptr bp, uptr sp, uptr a1, uptr a2) {
900 ScopedInErrorReport in_report;
Pirama Arumuga Nainarcdce50b2015-07-01 12:26:56 -0700901 const char *bug_type = "invalid-pointer-pair";
Stephen Hines2d1fdb22014-05-28 23:58:16 -0700902 Decorator d;
903 Printf("%s", d.Warning());
904 Report("ERROR: AddressSanitizer: invalid-pointer-pair: %p %p\n", a1, a2);
905 Printf("%s", d.EndWarning());
906 GET_STACK_TRACE_FATAL(pc, bp);
907 stack.Print();
Pirama Arumuga Nainarcdce50b2015-07-01 12:26:56 -0700908 DescribeAddress(a1, 1, bug_type);
909 DescribeAddress(a2, 1, bug_type);
910 ReportErrorSummary(bug_type, &stack);
Stephen Hines2d1fdb22014-05-28 23:58:16 -0700911}
912
913static INLINE void CheckForInvalidPointerPair(void *p1, void *p2) {
914 if (!flags()->detect_invalid_pointer_pairs) return;
915 uptr a1 = reinterpret_cast<uptr>(p1);
916 uptr a2 = reinterpret_cast<uptr>(p2);
917 AsanChunkView chunk1 = FindHeapChunkByAddress(a1);
918 AsanChunkView chunk2 = FindHeapChunkByAddress(a2);
919 bool valid1 = chunk1.IsValid();
920 bool valid2 = chunk2.IsValid();
921 if ((valid1 != valid2) || (valid1 && valid2 && !chunk1.Eq(chunk2))) {
922 GET_CALLER_PC_BP_SP; \
923 return ReportInvalidPointerPair(pc, bp, sp, a1, a2);
924 }
925}
Alexey Samsonov663c5012012-08-09 12:15:40 +0000926// ----------------------- Mac-specific reports ----------------- {{{1
927
Stephen Hines6d186232014-11-26 17:56:19 -0800928void WarnMacFreeUnallocated(uptr addr, uptr zone_ptr, const char *zone_name,
929 BufferedStackTrace *stack) {
Alexey Samsonov98737922012-08-10 15:13:05 +0000930 // Just print a warning here.
Kostya Serebryany283c2962012-08-28 11:34:40 +0000931 Printf("free_common(%p) -- attempting to free unallocated memory.\n"
Alexey Samsonov663c5012012-08-09 12:15:40 +0000932 "AddressSanitizer is ignoring this error on Mac OS now.\n",
933 addr);
934 PrintZoneForPointer(addr, zone_ptr, zone_name);
Stephen Hines2d1fdb22014-05-28 23:58:16 -0700935 stack->Print();
Alexey Samsonov98737922012-08-10 15:13:05 +0000936 DescribeHeapAddress(addr, 1);
Alexey Samsonov663c5012012-08-09 12:15:40 +0000937}
938
Stephen Hines6d186232014-11-26 17:56:19 -0800939void ReportMacMzReallocUnknown(uptr addr, uptr zone_ptr, const char *zone_name,
940 BufferedStackTrace *stack) {
Alexey Samsonov98737922012-08-10 15:13:05 +0000941 ScopedInErrorReport in_report;
Kostya Serebryany283c2962012-08-28 11:34:40 +0000942 Printf("mz_realloc(%p) -- attempting to realloc unallocated memory.\n"
Alexey Samsonov663c5012012-08-09 12:15:40 +0000943 "This is an unrecoverable problem, exiting now.\n",
944 addr);
945 PrintZoneForPointer(addr, zone_ptr, zone_name);
Stephen Hines2d1fdb22014-05-28 23:58:16 -0700946 stack->Print();
Alexey Samsonov98737922012-08-10 15:13:05 +0000947 DescribeHeapAddress(addr, 1);
Alexey Samsonov663c5012012-08-09 12:15:40 +0000948}
949
Stephen Hines6d186232014-11-26 17:56:19 -0800950void ReportMacCfReallocUnknown(uptr addr, uptr zone_ptr, const char *zone_name,
951 BufferedStackTrace *stack) {
Alexey Samsonov98737922012-08-10 15:13:05 +0000952 ScopedInErrorReport in_report;
Kostya Serebryany283c2962012-08-28 11:34:40 +0000953 Printf("cf_realloc(%p) -- attempting to realloc unallocated memory.\n"
Alexey Samsonov663c5012012-08-09 12:15:40 +0000954 "This is an unrecoverable problem, exiting now.\n",
955 addr);
956 PrintZoneForPointer(addr, zone_ptr, zone_name);
Stephen Hines2d1fdb22014-05-28 23:58:16 -0700957 stack->Print();
Alexey Samsonov98737922012-08-10 15:13:05 +0000958 DescribeHeapAddress(addr, 1);
Alexey Samsonovc98570b2012-08-09 10:56:57 +0000959}
960
Alexey Samsonov812ff902012-08-09 11:29:13 +0000961} // namespace __asan
962
963// --------------------------- Interface --------------------- {{{1
964using namespace __asan; // NOLINT
965
Stephen Hines2d1fdb22014-05-28 23:58:16 -0700966void __asan_report_error(uptr pc, uptr bp, uptr sp, uptr addr, int is_write,
Pirama Arumuga Nainar7c915052015-04-08 08:58:29 -0700967 uptr access_size, u32 exp) {
Stephen Hines86277eb2015-03-23 12:06:32 -0700968 ENABLE_FRAME_POINTER;
969
Pirama Arumuga Nainar7c915052015-04-08 08:58:29 -0700970 // Optimization experiments.
971 // The experiments can be used to evaluate potential optimizations that remove
972 // instrumentation (assess false negatives). Instead of completely removing
973 // some instrumentation, compiler can emit special calls into runtime
974 // (e.g. __asan_report_exp_load1 instead of __asan_report_load1) and pass
975 // mask of experiments (exp).
976 // The reaction to a non-zero value of exp is to be defined.
977 (void)exp;
978
Alexey Samsonov98737922012-08-10 15:13:05 +0000979 // Determine the error type.
Alexey Samsonovc98570b2012-08-09 10:56:57 +0000980 const char *bug_descr = "unknown-crash";
981 if (AddrIsInMem(addr)) {
982 u8 *shadow_addr = (u8*)MemToShadow(addr);
983 // If we are accessing 16 bytes, look at the second shadow byte.
984 if (*shadow_addr == 0 && access_size > SHADOW_GRANULARITY)
985 shadow_addr++;
986 // If we are in the partial right redzone, look at the next shadow byte.
987 if (*shadow_addr > 0 && *shadow_addr < 128)
988 shadow_addr++;
989 switch (*shadow_addr) {
990 case kAsanHeapLeftRedzoneMagic:
991 case kAsanHeapRightRedzoneMagic:
Stephen Hines6d186232014-11-26 17:56:19 -0800992 case kAsanArrayCookieMagic:
Alexey Samsonovc98570b2012-08-09 10:56:57 +0000993 bug_descr = "heap-buffer-overflow";
994 break;
995 case kAsanHeapFreeMagic:
996 bug_descr = "heap-use-after-free";
997 break;
998 case kAsanStackLeftRedzoneMagic:
999 bug_descr = "stack-buffer-underflow";
1000 break;
Kostya Serebryany3945c582012-08-21 14:10:25 +00001001 case kAsanInitializationOrderMagic:
1002 bug_descr = "initialization-order-fiasco";
1003 break;
Alexey Samsonovc98570b2012-08-09 10:56:57 +00001004 case kAsanStackMidRedzoneMagic:
1005 case kAsanStackRightRedzoneMagic:
1006 case kAsanStackPartialRedzoneMagic:
1007 bug_descr = "stack-buffer-overflow";
1008 break;
1009 case kAsanStackAfterReturnMagic:
1010 bug_descr = "stack-use-after-return";
1011 break;
1012 case kAsanUserPoisonedMemoryMagic:
1013 bug_descr = "use-after-poison";
1014 break;
Stephen Hines2d1fdb22014-05-28 23:58:16 -07001015 case kAsanContiguousContainerOOBMagic:
1016 bug_descr = "container-overflow";
1017 break;
Alexey Samsonovd4b5db82012-12-04 01:38:15 +00001018 case kAsanStackUseAfterScopeMagic:
1019 bug_descr = "stack-use-after-scope";
1020 break;
Alexey Samsonovc98570b2012-08-09 10:56:57 +00001021 case kAsanGlobalRedzoneMagic:
1022 bug_descr = "global-buffer-overflow";
1023 break;
Stephen Hines6d186232014-11-26 17:56:19 -08001024 case kAsanIntraObjectRedzone:
1025 bug_descr = "intra-object-overflow";
1026 break;
Stephen Hines86277eb2015-03-23 12:06:32 -07001027 case kAsanAllocaLeftMagic:
1028 case kAsanAllocaRightMagic:
1029 bug_descr = "dynamic-stack-buffer-overflow";
1030 break;
Alexey Samsonovc98570b2012-08-09 10:56:57 +00001031 }
1032 }
Stephen Hines6d186232014-11-26 17:56:19 -08001033
1034 ReportData report = { pc, sp, bp, addr, (bool)is_write, access_size,
1035 bug_descr };
1036 ScopedInErrorReport in_report(&report);
1037
Kostya Serebryany58f54552012-12-18 07:32:16 +00001038 Decorator d;
1039 Printf("%s", d.Warning());
Kostya Serebryany69d8ede2012-10-15 13:04:58 +00001040 Report("ERROR: AddressSanitizer: %s on address "
Stephen Hines6d186232014-11-26 17:56:19 -08001041 "%p at pc %p bp %p sp %p\n",
Alexey Samsonovc98570b2012-08-09 10:56:57 +00001042 bug_descr, (void*)addr, pc, bp, sp);
Kostya Serebryany58f54552012-12-18 07:32:16 +00001043 Printf("%s", d.EndWarning());
Alexey Samsonovc98570b2012-08-09 10:56:57 +00001044
Alexey Samsonov89c13842013-03-20 09:23:28 +00001045 u32 curr_tid = GetCurrentTidOrInvalid();
Kostya Serebryany716e2f22012-12-07 15:15:01 +00001046 char tname[128];
Kostya Serebryany58f54552012-12-18 07:32:16 +00001047 Printf("%s%s of size %zu at %p thread T%d%s%s\n",
1048 d.Access(),
1049 access_size ? (is_write ? "WRITE" : "READ") : "ACCESS",
1050 access_size, (void*)addr, curr_tid,
1051 ThreadNameWithParenthesis(curr_tid, tname, sizeof(tname)),
1052 d.EndAccess());
Alexey Samsonovc98570b2012-08-09 10:56:57 +00001053
Kostya Serebryanya30c8f92012-12-13 09:34:23 +00001054 GET_STACK_TRACE_FATAL(pc, bp);
Stephen Hines2d1fdb22014-05-28 23:58:16 -07001055 stack.Print();
Alexey Samsonovc98570b2012-08-09 10:56:57 +00001056
Pirama Arumuga Nainarcdce50b2015-07-01 12:26:56 -07001057 DescribeAddress(addr, access_size, bug_descr);
Alexey Samsonov2fb08722013-11-01 17:02:14 +00001058 ReportErrorSummary(bug_descr, &stack);
Alexey Samsonov98737922012-08-10 15:13:05 +00001059 PrintShadowMemoryForAddress(addr);
Alexey Samsonovc98570b2012-08-09 10:56:57 +00001060}
1061
Alexey Samsonovc98570b2012-08-09 10:56:57 +00001062void NOINLINE __asan_set_error_report_callback(void (*callback)(const char*)) {
1063 error_report_callback = callback;
1064 if (callback) {
1065 error_message_buffer_size = 1 << 16;
1066 error_message_buffer =
Stephen Hines2d1fdb22014-05-28 23:58:16 -07001067 (char*)MmapOrDie(error_message_buffer_size, __func__);
Alexey Samsonovc98570b2012-08-09 10:56:57 +00001068 error_message_buffer_pos = 0;
1069 }
1070}
Alexey Samsonovf657a192012-08-13 11:23:40 +00001071
Kostya Serebryany17a7c672012-12-29 10:18:31 +00001072void __asan_describe_address(uptr addr) {
Stephen Hines6d186232014-11-26 17:56:19 -08001073 // Thread registry must be locked while we're describing an address.
1074 asanThreadRegistry().Lock();
Pirama Arumuga Nainarcdce50b2015-07-01 12:26:56 -07001075 DescribeAddress(addr, 1, "");
Stephen Hines6d186232014-11-26 17:56:19 -08001076 asanThreadRegistry().Unlock();
1077}
1078
1079int __asan_report_present() {
1080 return report_happened ? 1 : 0;
1081}
1082
1083uptr __asan_get_report_pc() {
1084 return report_data.pc;
1085}
1086
1087uptr __asan_get_report_bp() {
1088 return report_data.bp;
1089}
1090
1091uptr __asan_get_report_sp() {
1092 return report_data.sp;
1093}
1094
1095uptr __asan_get_report_address() {
1096 return report_data.addr;
1097}
1098
1099int __asan_get_report_access_type() {
1100 return report_data.is_write ? 1 : 0;
1101}
1102
1103uptr __asan_get_report_access_size() {
1104 return report_data.access_size;
1105}
1106
1107const char *__asan_get_report_description() {
1108 return report_data.description;
Kostya Serebryany17a7c672012-12-29 10:18:31 +00001109}
1110
Stephen Hines2d1fdb22014-05-28 23:58:16 -07001111extern "C" {
1112SANITIZER_INTERFACE_ATTRIBUTE
1113void __sanitizer_ptr_sub(void *a, void *b) {
1114 CheckForInvalidPointerPair(a, b);
1115}
1116SANITIZER_INTERFACE_ATTRIBUTE
1117void __sanitizer_ptr_cmp(void *a, void *b) {
1118 CheckForInvalidPointerPair(a, b);
1119}
1120} // extern "C"
1121
Alexey Samsonov6a08d292012-12-07 22:01:28 +00001122#if !SANITIZER_SUPPORTS_WEAK_HOOKS
Alexey Samsonov86633432012-10-02 14:06:39 +00001123// Provide default implementation of __asan_on_error that does nothing
1124// and may be overriden by user.
Timur Iskhodzhanov3c80c6c2013-08-13 11:42:45 +00001125SANITIZER_INTERFACE_ATTRIBUTE SANITIZER_WEAK_ATTRIBUTE NOINLINE
Alexey Samsonov86633432012-10-02 14:06:39 +00001126void __asan_on_error() {}
Alexey Samsonov6a08d292012-12-07 22:01:28 +00001127#endif