blob: 0fb60846c3b471a8217ecbd73e1b80250c6315bc [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//===----------------------------------------------------------------------===//
Pirama Arumuga Nainar799172d2016-03-03 15:50:30 -080014
Alexey Samsonov98737922012-08-10 15:13:05 +000015#include "asan_flags.h"
Alexey Samsonov73545092012-08-09 07:40:58 +000016#include "asan_internal.h"
Alexey Samsonove218beb2012-08-09 09:06:52 +000017#include "asan_mapping.h"
Alexey Samsonov73545092012-08-09 07:40:58 +000018#include "asan_report.h"
19#include "asan_stack.h"
Alexey Samsonove4bfca22012-08-09 09:27:24 +000020#include "asan_thread.h"
Kostya Serebryany58f54552012-12-18 07:32:16 +000021#include "sanitizer_common/sanitizer_common.h"
Sergey Matveeved20ebe2013-05-06 11:27:58 +000022#include "sanitizer_common/sanitizer_flags.h"
Kostya Serebryany58f54552012-12-18 07:32:16 +000023#include "sanitizer_common/sanitizer_report_decorator.h"
Kostya Serebryany6d958692013-10-18 14:50:44 +000024#include "sanitizer_common/sanitizer_stackdepot.h"
Alexey Samsonov9c927482012-12-26 14:44:46 +000025#include "sanitizer_common/sanitizer_symbolizer.h"
Alexey Samsonov73545092012-08-09 07:40:58 +000026
27namespace __asan {
28
Alexey Samsonovf657a192012-08-13 11:23:40 +000029// -------------------- User-specified callbacks ----------------- {{{1
Alexey Samsonovc98570b2012-08-09 10:56:57 +000030static void (*error_report_callback)(const char*);
Pirama Arumuga Nainar799172d2016-03-03 15:50:30 -080031static char *error_message_buffer = nullptr;
Alexey Samsonovc98570b2012-08-09 10:56:57 +000032static uptr error_message_buffer_pos = 0;
Pirama Arumuga Nainar799172d2016-03-03 15:50:30 -080033static BlockingMutex error_message_buf_mutex(LINKER_INITIALIZED);
34static const unsigned kAsanBuggyPcPoolSize = 25;
35static __sanitizer::atomic_uintptr_t AsanBuggyPcPool[kAsanBuggyPcPoolSize];
Alexey Samsonovc98570b2012-08-09 10:56:57 +000036
Stephen Hines6d186232014-11-26 17:56:19 -080037struct ReportData {
38 uptr pc;
39 uptr sp;
40 uptr bp;
41 uptr addr;
42 bool is_write;
43 uptr access_size;
44 const char *description;
45};
46
47static bool report_happened = false;
48static ReportData report_data = {};
49
Alexey Samsonovc98570b2012-08-09 10:56:57 +000050void AppendToErrorMessageBuffer(const char *buffer) {
Pirama Arumuga Nainar799172d2016-03-03 15:50:30 -080051 BlockingMutexLock l(&error_message_buf_mutex);
52 if (!error_message_buffer) {
53 error_message_buffer =
54 (char*)MmapOrDieQuietly(kErrorMessageBufferSize, __func__);
55 error_message_buffer_pos = 0;
Alexey Samsonovc98570b2012-08-09 10:56:57 +000056 }
Pirama Arumuga Nainar799172d2016-03-03 15:50:30 -080057 uptr length = internal_strlen(buffer);
58 RAW_CHECK(kErrorMessageBufferSize >= error_message_buffer_pos);
59 uptr remaining = kErrorMessageBufferSize - error_message_buffer_pos;
60 internal_strncpy(error_message_buffer + error_message_buffer_pos,
61 buffer, remaining);
62 error_message_buffer[kErrorMessageBufferSize - 1] = '\0';
63 // FIXME: reallocate the buffer instead of truncating the message.
64 error_message_buffer_pos += Min(remaining, length);
Alexey Samsonovc98570b2012-08-09 10:56:57 +000065}
66
Kostya Serebryany58f54552012-12-18 07:32:16 +000067// ---------------------- Decorator ------------------------------ {{{1
Stephen Hines2d1fdb22014-05-28 23:58:16 -070068class Decorator: public __sanitizer::SanitizerCommonDecorator {
Kostya Serebryany58f54552012-12-18 07:32:16 +000069 public:
Stephen Hines2d1fdb22014-05-28 23:58:16 -070070 Decorator() : SanitizerCommonDecorator() { }
Kostya Serebryany58f54552012-12-18 07:32:16 +000071 const char *Access() { return Blue(); }
72 const char *EndAccess() { return Default(); }
73 const char *Location() { return Green(); }
74 const char *EndLocation() { return Default(); }
75 const char *Allocation() { return Magenta(); }
76 const char *EndAllocation() { return Default(); }
Kostya Serebryany9514a532012-12-19 09:53:32 +000077
78 const char *ShadowByte(u8 byte) {
79 switch (byte) {
80 case kAsanHeapLeftRedzoneMagic:
81 case kAsanHeapRightRedzoneMagic:
Stephen Hines6d186232014-11-26 17:56:19 -080082 case kAsanArrayCookieMagic:
Kostya Serebryany9514a532012-12-19 09:53:32 +000083 return Red();
84 case kAsanHeapFreeMagic:
85 return Magenta();
86 case kAsanStackLeftRedzoneMagic:
87 case kAsanStackMidRedzoneMagic:
88 case kAsanStackRightRedzoneMagic:
89 case kAsanStackPartialRedzoneMagic:
90 return Red();
91 case kAsanStackAfterReturnMagic:
92 return Magenta();
93 case kAsanInitializationOrderMagic:
94 return Cyan();
95 case kAsanUserPoisonedMemoryMagic:
Stephen Hines2d1fdb22014-05-28 23:58:16 -070096 case kAsanContiguousContainerOOBMagic:
Stephen Hines86277eb2015-03-23 12:06:32 -070097 case kAsanAllocaLeftMagic:
98 case kAsanAllocaRightMagic:
Kostya Serebryany9514a532012-12-19 09:53:32 +000099 return Blue();
100 case kAsanStackUseAfterScopeMagic:
101 return Magenta();
102 case kAsanGlobalRedzoneMagic:
103 return Red();
104 case kAsanInternalHeapMagic:
105 return Yellow();
Stephen Hines6d186232014-11-26 17:56:19 -0800106 case kAsanIntraObjectRedzone:
107 return Yellow();
Kostya Serebryany9514a532012-12-19 09:53:32 +0000108 default:
109 return Default();
110 }
111 }
112 const char *EndShadowByte() { return Default(); }
Stephen Hines6d186232014-11-26 17:56:19 -0800113 const char *MemoryByte() { return Magenta(); }
114 const char *EndMemoryByte() { return Default(); }
Kostya Serebryany58f54552012-12-18 07:32:16 +0000115};
116
Alexey Samsonov98737922012-08-10 15:13:05 +0000117// ---------------------- Helper functions ----------------------- {{{1
118
Stephen Hines6d186232014-11-26 17:56:19 -0800119static void PrintMemoryByte(InternalScopedString *str, const char *before,
120 u8 byte, bool in_shadow, const char *after = "\n") {
Kostya Serebryany9514a532012-12-19 09:53:32 +0000121 Decorator d;
Stephen Hines6d186232014-11-26 17:56:19 -0800122 str->append("%s%s%x%x%s%s", before,
123 in_shadow ? d.ShadowByte(byte) : d.MemoryByte(),
124 byte >> 4, byte & 15,
125 in_shadow ? d.EndShadowByte() : d.EndMemoryByte(), after);
126}
127
128static void PrintShadowByte(InternalScopedString *str, const char *before,
129 u8 byte, const char *after = "\n") {
130 PrintMemoryByte(str, before, byte, /*in_shadow*/true, after);
Kostya Serebryany9514a532012-12-19 09:53:32 +0000131}
132
Stephen Hines2d1fdb22014-05-28 23:58:16 -0700133static void PrintShadowBytes(InternalScopedString *str, const char *before,
134 u8 *bytes, u8 *guilty, uptr n) {
Kostya Serebryany9514a532012-12-19 09:53:32 +0000135 Decorator d;
Stephen Hines2d1fdb22014-05-28 23:58:16 -0700136 if (before) str->append("%s%p:", before, bytes);
Kostya Serebryany9514a532012-12-19 09:53:32 +0000137 for (uptr i = 0; i < n; i++) {
138 u8 *p = bytes + i;
Stephen Hines2d1fdb22014-05-28 23:58:16 -0700139 const char *before =
140 p == guilty ? "[" : (p - 1 == guilty && i != 0) ? "" : " ";
Kostya Serebryany9514a532012-12-19 09:53:32 +0000141 const char *after = p == guilty ? "]" : "";
Stephen Hines2d1fdb22014-05-28 23:58:16 -0700142 PrintShadowByte(str, before, *p, after);
Alexey Samsonov98737922012-08-10 15:13:05 +0000143 }
Stephen Hines2d1fdb22014-05-28 23:58:16 -0700144 str->append("\n");
Alexey Samsonov98737922012-08-10 15:13:05 +0000145}
146
Stephen Hines2d1fdb22014-05-28 23:58:16 -0700147static void PrintLegend(InternalScopedString *str) {
148 str->append(
149 "Shadow byte legend (one shadow byte represents %d "
150 "application bytes):\n",
151 (int)SHADOW_GRANULARITY);
152 PrintShadowByte(str, " Addressable: ", 0);
153 str->append(" Partially addressable: ");
154 for (u8 i = 1; i < SHADOW_GRANULARITY; i++) PrintShadowByte(str, "", i, " ");
155 str->append("\n");
156 PrintShadowByte(str, " Heap left redzone: ",
157 kAsanHeapLeftRedzoneMagic);
158 PrintShadowByte(str, " Heap right redzone: ",
159 kAsanHeapRightRedzoneMagic);
160 PrintShadowByte(str, " Freed heap region: ", kAsanHeapFreeMagic);
161 PrintShadowByte(str, " Stack left redzone: ",
162 kAsanStackLeftRedzoneMagic);
163 PrintShadowByte(str, " Stack mid redzone: ",
164 kAsanStackMidRedzoneMagic);
165 PrintShadowByte(str, " Stack right redzone: ",
166 kAsanStackRightRedzoneMagic);
167 PrintShadowByte(str, " Stack partial redzone: ",
168 kAsanStackPartialRedzoneMagic);
169 PrintShadowByte(str, " Stack after return: ",
170 kAsanStackAfterReturnMagic);
171 PrintShadowByte(str, " Stack use after scope: ",
172 kAsanStackUseAfterScopeMagic);
173 PrintShadowByte(str, " Global redzone: ", kAsanGlobalRedzoneMagic);
174 PrintShadowByte(str, " Global init order: ",
175 kAsanInitializationOrderMagic);
176 PrintShadowByte(str, " Poisoned by user: ",
177 kAsanUserPoisonedMemoryMagic);
178 PrintShadowByte(str, " Container overflow: ",
179 kAsanContiguousContainerOOBMagic);
Stephen Hines6d186232014-11-26 17:56:19 -0800180 PrintShadowByte(str, " Array cookie: ",
181 kAsanArrayCookieMagic);
182 PrintShadowByte(str, " Intra object redzone: ",
183 kAsanIntraObjectRedzone);
Stephen Hines2d1fdb22014-05-28 23:58:16 -0700184 PrintShadowByte(str, " ASan internal: ", kAsanInternalHeapMagic);
Stephen Hines86277eb2015-03-23 12:06:32 -0700185 PrintShadowByte(str, " Left alloca redzone: ", kAsanAllocaLeftMagic);
186 PrintShadowByte(str, " Right alloca redzone: ", kAsanAllocaRightMagic);
Alexey Samsonov98737922012-08-10 15:13:05 +0000187}
188
Stephen Hines6d186232014-11-26 17:56:19 -0800189void MaybeDumpInstructionBytes(uptr pc) {
190 if (!flags()->dump_instruction_bytes || (pc < GetPageSizeCached()))
191 return;
192 InternalScopedString str(1024);
193 str.append("First 16 instruction bytes at pc: ");
194 if (IsAccessibleMemoryRange(pc, 16)) {
195 for (int i = 0; i < 16; ++i) {
196 PrintMemoryByte(&str, "", ((u8 *)pc)[i], /*in_shadow*/false, " ");
197 }
198 str.append("\n");
199 } else {
200 str.append("unaccessible\n");
201 }
202 Report("%s", str.data());
203}
204
Kostya Serebryany95f630a2013-01-28 07:34:22 +0000205static void PrintShadowMemoryForAddress(uptr addr) {
Stephen Hines2d1fdb22014-05-28 23:58:16 -0700206 if (!AddrIsInMem(addr)) return;
Kostya Serebryany95f630a2013-01-28 07:34:22 +0000207 uptr shadow_addr = MemToShadow(addr);
208 const uptr n_bytes_per_row = 16;
209 uptr aligned_shadow = shadow_addr & ~(n_bytes_per_row - 1);
Stephen Hines2d1fdb22014-05-28 23:58:16 -0700210 InternalScopedString str(4096 * 8);
211 str.append("Shadow bytes around the buggy address:\n");
Kostya Serebryany95f630a2013-01-28 07:34:22 +0000212 for (int i = -5; i <= 5; i++) {
213 const char *prefix = (i == 0) ? "=>" : " ";
Stephen Hines2d1fdb22014-05-28 23:58:16 -0700214 PrintShadowBytes(&str, prefix, (u8 *)(aligned_shadow + i * n_bytes_per_row),
215 (u8 *)shadow_addr, n_bytes_per_row);
Kostya Serebryany95f630a2013-01-28 07:34:22 +0000216 }
Stephen Hines2d1fdb22014-05-28 23:58:16 -0700217 if (flags()->print_legend) PrintLegend(&str);
218 Printf("%s", str.data());
Kostya Serebryany95f630a2013-01-28 07:34:22 +0000219}
220
Alexey Samsonov98737922012-08-10 15:13:05 +0000221static void PrintZoneForPointer(uptr ptr, uptr zone_ptr,
222 const char *zone_name) {
223 if (zone_ptr) {
224 if (zone_name) {
Kostya Serebryany283c2962012-08-28 11:34:40 +0000225 Printf("malloc_zone_from_ptr(%p) = %p, which is %s\n",
Alexey Samsonov98737922012-08-10 15:13:05 +0000226 ptr, zone_ptr, zone_name);
227 } else {
Kostya Serebryany283c2962012-08-28 11:34:40 +0000228 Printf("malloc_zone_from_ptr(%p) = %p, which doesn't have a name\n",
Alexey Samsonov98737922012-08-10 15:13:05 +0000229 ptr, zone_ptr);
230 }
231 } else {
Kostya Serebryany283c2962012-08-28 11:34:40 +0000232 Printf("malloc_zone_from_ptr(%p) = 0\n", ptr);
Alexey Samsonov98737922012-08-10 15:13:05 +0000233 }
234}
235
Timur Iskhodzhanov997454a2013-09-10 08:36:21 +0000236static void DescribeThread(AsanThread *t) {
237 if (t)
238 DescribeThread(t->context());
239}
240
Alexey Samsonove218beb2012-08-09 09:06:52 +0000241// ---------------------- Address Descriptions ------------------- {{{1
242
Alexey Samsonove4bfca22012-08-09 09:27:24 +0000243static bool IsASCII(unsigned char c) {
Alexey Samsonov98737922012-08-10 15:13:05 +0000244 return /*0x00 <= c &&*/ c <= 0x7F;
Alexey Samsonove4bfca22012-08-09 09:27:24 +0000245}
246
Alexey Samsonovc9424272013-03-27 10:41:22 +0000247static const char *MaybeDemangleGlobalName(const char *name) {
248 // We can spoil names of globals with C linkage, so use an heuristic
249 // approach to check if the name should be demangled.
Stephen Hines2d1fdb22014-05-28 23:58:16 -0700250 bool should_demangle = false;
251 if (name[0] == '_' && name[1] == 'Z')
252 should_demangle = true;
253 else if (SANITIZER_WINDOWS && name[0] == '\01' && name[1] == '?')
254 should_demangle = true;
255
Stephen Hines6d186232014-11-26 17:56:19 -0800256 return should_demangle ? Symbolizer::GetOrInit()->Demangle(name) : name;
Alexey Samsonovc9424272013-03-27 10:41:22 +0000257}
258
Alexey Samsonov939316c2013-04-01 08:57:38 +0000259// Check if the global is a zero-terminated ASCII string. If so, print it.
Stephen Hines2d1fdb22014-05-28 23:58:16 -0700260static void PrintGlobalNameIfASCII(InternalScopedString *str,
261 const __asan_global &g) {
Alexey Samsonov939316c2013-04-01 08:57:38 +0000262 for (uptr p = g.beg; p < g.beg + g.size - 1; p++) {
263 unsigned char c = *(unsigned char*)p;
264 if (c == '\0' || !IsASCII(c)) return;
265 }
266 if (*(char*)(g.beg + g.size - 1) != '\0') return;
Stephen Hines2d1fdb22014-05-28 23:58:16 -0700267 str->append(" '%s' is ascii string '%s'\n", MaybeDemangleGlobalName(g.name),
268 (char *)g.beg);
Alexey Samsonov939316c2013-04-01 08:57:38 +0000269}
270
Stephen Hines6a211c52014-07-21 00:49:56 -0700271static const char *GlobalFilename(const __asan_global &g) {
272 const char *res = g.module_name;
273 // Prefer the filename from source location, if is available.
274 if (g.location)
275 res = g.location->filename;
276 CHECK(res);
277 return res;
278}
279
280static void PrintGlobalLocation(InternalScopedString *str,
281 const __asan_global &g) {
282 str->append("%s", GlobalFilename(g));
283 if (!g.location)
284 return;
285 if (g.location->line_no)
286 str->append(":%d", g.location->line_no);
287 if (g.location->column_no)
288 str->append(":%d", g.location->column_no);
289}
290
Pirama Arumuga Nainarcdce50b2015-07-01 12:26:56 -0700291static void DescribeAddressRelativeToGlobal(uptr addr, uptr size,
292 const __asan_global &g) {
Stephen Hines2d1fdb22014-05-28 23:58:16 -0700293 InternalScopedString str(4096);
Kostya Serebryany58f54552012-12-18 07:32:16 +0000294 Decorator d;
Stephen Hines2d1fdb22014-05-28 23:58:16 -0700295 str.append("%s", d.Location());
Alexey Samsonove4bfca22012-08-09 09:27:24 +0000296 if (addr < g.beg) {
Stephen Hines2d1fdb22014-05-28 23:58:16 -0700297 str.append("%p is located %zd bytes to the left", (void *)addr,
298 g.beg - addr);
Evgeniy Stepanov589dcda2013-02-05 14:32:03 +0000299 } else if (addr + size > g.beg + g.size) {
300 if (addr < g.beg + g.size)
301 addr = g.beg + g.size;
Stephen Hines2d1fdb22014-05-28 23:58:16 -0700302 str.append("%p is located %zd bytes to the right", (void *)addr,
303 addr - (g.beg + g.size));
Alexey Samsonove4bfca22012-08-09 09:27:24 +0000304 } else {
Evgeniy Stepanov589dcda2013-02-05 14:32:03 +0000305 // Can it happen?
Stephen Hines2d1fdb22014-05-28 23:58:16 -0700306 str.append("%p is located %zd bytes inside", (void *)addr, addr - g.beg);
Alexey Samsonove4bfca22012-08-09 09:27:24 +0000307 }
Stephen Hines6a211c52014-07-21 00:49:56 -0700308 str.append(" of global variable '%s' defined in '",
309 MaybeDemangleGlobalName(g.name));
310 PrintGlobalLocation(&str, g);
311 str.append("' (0x%zx) of size %zu\n", g.beg, g.size);
Stephen Hines2d1fdb22014-05-28 23:58:16 -0700312 str.append("%s", d.EndLocation());
313 PrintGlobalNameIfASCII(&str, g);
314 Printf("%s", str.data());
Pirama Arumuga Nainarcdce50b2015-07-01 12:26:56 -0700315}
316
317static bool DescribeAddressIfGlobal(uptr addr, uptr size,
318 const char *bug_type) {
319 // Assume address is close to at most four globals.
320 const int kMaxGlobalsInReport = 4;
321 __asan_global globals[kMaxGlobalsInReport];
322 u32 reg_sites[kMaxGlobalsInReport];
323 int globals_num =
324 GetGlobalsForAddress(addr, globals, reg_sites, ARRAY_SIZE(globals));
325 if (globals_num == 0)
326 return false;
327 for (int i = 0; i < globals_num; i++) {
328 DescribeAddressRelativeToGlobal(addr, size, globals[i]);
329 if (0 == internal_strcmp(bug_type, "initialization-order-fiasco") &&
330 reg_sites[i]) {
331 Printf(" registered at:\n");
332 StackDepotGet(reg_sites[i]).Print();
333 }
334 }
Alexey Samsonove4bfca22012-08-09 09:27:24 +0000335 return true;
336}
337
Stephen Hines6d186232014-11-26 17:56:19 -0800338bool DescribeAddressIfShadow(uptr addr, AddressDescription *descr, bool print) {
Alexey Samsonove218beb2012-08-09 09:06:52 +0000339 if (AddrIsInMem(addr))
340 return false;
Stephen Hines6d186232014-11-26 17:56:19 -0800341 const char *area_type = nullptr;
342 if (AddrIsInShadowGap(addr)) area_type = "shadow gap";
343 else if (AddrIsInHighShadow(addr)) area_type = "high shadow";
344 else if (AddrIsInLowShadow(addr)) area_type = "low shadow";
345 if (area_type != nullptr) {
346 if (print) {
347 Printf("Address %p is located in the %s area.\n", addr, area_type);
348 } else {
349 CHECK(descr);
350 descr->region_kind = area_type;
351 }
Alexey Samsonove218beb2012-08-09 09:06:52 +0000352 return true;
353 }
354 CHECK(0 && "Address is not in memory and not in shadow?");
355 return false;
356}
357
Kostya Serebryany50f3daa2013-03-22 10:36:24 +0000358// Return " (thread_name) " or an empty string if the name is empty.
359const char *ThreadNameWithParenthesis(AsanThreadContext *t, char buff[],
360 uptr buff_len) {
361 const char *name = t->name;
362 if (name[0] == '\0') return "";
363 buff[0] = 0;
364 internal_strncat(buff, " (", 3);
365 internal_strncat(buff, name, buff_len - 4);
366 internal_strncat(buff, ")", 2);
367 return buff;
368}
369
370const char *ThreadNameWithParenthesis(u32 tid, char buff[],
371 uptr buff_len) {
372 if (tid == kInvalidTid) return "";
373 asanThreadRegistry().CheckLocked();
374 AsanThreadContext *t = GetThreadContextByTidLocked(tid);
375 return ThreadNameWithParenthesis(t, buff, buff_len);
376}
377
Stephen Hines6d186232014-11-26 17:56:19 -0800378static void PrintAccessAndVarIntersection(const StackVarDescr &var, uptr addr,
379 uptr access_size, uptr prev_var_end,
380 uptr next_var_beg) {
381 uptr var_end = var.beg + var.size;
Kostya Serebryanyedb39c72013-09-03 13:58:04 +0000382 uptr addr_end = addr + access_size;
Pirama Arumuga Nainar799172d2016-03-03 15:50:30 -0800383 const char *pos_descr = nullptr;
Stephen Hines6d186232014-11-26 17:56:19 -0800384 // If the variable [var.beg, var_end) is the nearest variable to the
Kostya Serebryanyedb39c72013-09-03 13:58:04 +0000385 // current memory access, indicate it in the log.
Stephen Hines6d186232014-11-26 17:56:19 -0800386 if (addr >= var.beg) {
Kostya Serebryanyedb39c72013-09-03 13:58:04 +0000387 if (addr_end <= var_end)
388 pos_descr = "is inside"; // May happen if this is a use-after-return.
389 else if (addr < var_end)
390 pos_descr = "partially overflows";
391 else if (addr_end <= next_var_beg &&
392 next_var_beg - addr_end >= addr - var_end)
393 pos_descr = "overflows";
394 } else {
Stephen Hines6d186232014-11-26 17:56:19 -0800395 if (addr_end > var.beg)
Kostya Serebryanyedb39c72013-09-03 13:58:04 +0000396 pos_descr = "partially underflows";
397 else if (addr >= prev_var_end &&
Stephen Hines6d186232014-11-26 17:56:19 -0800398 addr - prev_var_end >= var.beg - addr_end)
Kostya Serebryanyedb39c72013-09-03 13:58:04 +0000399 pos_descr = "underflows";
400 }
Stephen Hines2d1fdb22014-05-28 23:58:16 -0700401 InternalScopedString str(1024);
Stephen Hines6d186232014-11-26 17:56:19 -0800402 str.append(" [%zd, %zd)", var.beg, var_end);
403 // Render variable name.
404 str.append(" '");
405 for (uptr i = 0; i < var.name_len; ++i) {
406 str.append("%c", var.name_pos[i]);
407 }
408 str.append("'");
Kostya Serebryanyedb39c72013-09-03 13:58:04 +0000409 if (pos_descr) {
410 Decorator d;
411 // FIXME: we may want to also print the size of the access here,
412 // but in case of accesses generated by memset it may be confusing.
Stephen Hines2d1fdb22014-05-28 23:58:16 -0700413 str.append("%s <== Memory access at offset %zd %s this variable%s\n",
414 d.Location(), addr, pos_descr, d.EndLocation());
Kostya Serebryanyedb39c72013-09-03 13:58:04 +0000415 } else {
Stephen Hines2d1fdb22014-05-28 23:58:16 -0700416 str.append("\n");
Kostya Serebryanyedb39c72013-09-03 13:58:04 +0000417 }
Stephen Hines2d1fdb22014-05-28 23:58:16 -0700418 Printf("%s", str.data());
Kostya Serebryanyedb39c72013-09-03 13:58:04 +0000419}
420
Stephen Hines6d186232014-11-26 17:56:19 -0800421bool ParseFrameDescription(const char *frame_descr,
422 InternalMmapVector<StackVarDescr> *vars) {
423 CHECK(frame_descr);
424 char *p;
425 // This string is created by the compiler and has the following form:
426 // "n alloc_1 alloc_2 ... alloc_n"
427 // where alloc_i looks like "offset size len ObjectName".
428 uptr n_objects = (uptr)internal_simple_strtoll(frame_descr, &p, 10);
429 if (n_objects == 0)
430 return false;
431
432 for (uptr i = 0; i < n_objects; i++) {
433 uptr beg = (uptr)internal_simple_strtoll(p, &p, 10);
434 uptr size = (uptr)internal_simple_strtoll(p, &p, 10);
435 uptr len = (uptr)internal_simple_strtoll(p, &p, 10);
436 if (beg == 0 || size == 0 || *p != ' ') {
437 return false;
438 }
439 p++;
440 StackVarDescr var = {beg, size, p, len};
441 vars->push_back(var);
442 p += len;
443 }
444
445 return true;
446}
Kostya Serebryanyedb39c72013-09-03 13:58:04 +0000447
Alexey Samsonove218beb2012-08-09 09:06:52 +0000448bool DescribeAddressIfStack(uptr addr, uptr access_size) {
Alexey Samsonovdef1be92013-03-21 11:23:41 +0000449 AsanThread *t = FindThreadByStackAddress(addr);
Alexey Samsonove218beb2012-08-09 09:06:52 +0000450 if (!t) return false;
Kostya Serebryanyd570bb42013-05-22 14:21:34 +0000451
Kostya Serebryany58f54552012-12-18 07:32:16 +0000452 Decorator d;
Stephen Hines6d186232014-11-26 17:56:19 -0800453 char tname[128];
Kostya Serebryany58f54552012-12-18 07:32:16 +0000454 Printf("%s", d.Location());
Stephen Hines6d186232014-11-26 17:56:19 -0800455 Printf("Address %p is located in stack of thread T%d%s", addr, t->tid(),
456 ThreadNameWithParenthesis(t->tid(), tname, sizeof(tname)));
457
458 // Try to fetch precise stack frame for this access.
459 AsanThread::StackFrameAccess access;
460 if (!t->GetStackFrameAccessByAddr(addr, &access)) {
461 Printf("%s\n", d.EndLocation());
462 return true;
463 }
464 Printf(" at offset %zu in frame%s\n", access.offset, d.EndLocation());
465
Kostya Serebryany50f3daa2013-03-22 10:36:24 +0000466 // Now we print the frame where the alloca has happened.
467 // We print this frame as a stack trace with one element.
468 // The symbolizer may print more than one frame if inlining was involved.
469 // The frame numbers may be different than those in the stack trace printed
470 // previously. That's unfortunate, but I have no better solution,
471 // especially given that the alloca may be from entirely different place
472 // (e.g. use-after-scope, or different thread's stack).
Stephen Hines6d186232014-11-26 17:56:19 -0800473#if defined(__powerpc64__) && defined(__BIG_ENDIAN__)
474 // On PowerPC64 ELFv1, the address of a function actually points to a
475 // three-doubleword data structure with the first field containing
476 // the address of the function's code.
477 access.frame_pc = *reinterpret_cast<uptr *>(access.frame_pc);
478#endif
479 access.frame_pc += 16;
Kostya Serebryany58f54552012-12-18 07:32:16 +0000480 Printf("%s", d.EndLocation());
Stephen Hines6d186232014-11-26 17:56:19 -0800481 StackTrace alloca_stack(&access.frame_pc, 1);
Stephen Hines2d1fdb22014-05-28 23:58:16 -0700482 alloca_stack.Print();
Stephen Hines6d186232014-11-26 17:56:19 -0800483
484 InternalMmapVector<StackVarDescr> vars(16);
485 if (!ParseFrameDescription(access.frame_descr, &vars)) {
486 Printf("AddressSanitizer can't parse the stack frame "
487 "descriptor: |%s|\n", access.frame_descr);
488 // 'addr' is a stack address, so return true even if we can't parse frame
489 return true;
490 }
491 uptr n_objects = vars.size();
Alexey Samsonove218beb2012-08-09 09:06:52 +0000492 // Report the number of stack objects.
Kostya Serebryany283c2962012-08-28 11:34:40 +0000493 Printf(" This frame has %zu object(s):\n", n_objects);
Kostya Serebryanyedb39c72013-09-03 13:58:04 +0000494
Alexey Samsonove218beb2012-08-09 09:06:52 +0000495 // Report all objects in this frame.
Kostya Serebryany89fe5642013-09-03 14:53:02 +0000496 for (uptr i = 0; i < n_objects; i++) {
Kostya Serebryanyedb39c72013-09-03 13:58:04 +0000497 uptr prev_var_end = i ? vars[i - 1].beg + vars[i - 1].size : 0;
Timur Iskhodzhanov22917e92013-09-03 15:09:21 +0000498 uptr next_var_beg = i + 1 < n_objects ? vars[i + 1].beg : ~(0UL);
Stephen Hines6d186232014-11-26 17:56:19 -0800499 PrintAccessAndVarIntersection(vars[i], access.offset, access_size,
Kostya Serebryanyedb39c72013-09-03 13:58:04 +0000500 prev_var_end, next_var_beg);
Alexey Samsonove218beb2012-08-09 09:06:52 +0000501 }
Kostya Serebryany283c2962012-08-28 11:34:40 +0000502 Printf("HINT: this may be a false positive if your program uses "
Stephen Hines6d186232014-11-26 17:56:19 -0800503 "some custom stack unwind mechanism or swapcontext\n");
504 if (SANITIZER_WINDOWS)
505 Printf(" (longjmp, SEH and C++ exceptions *are* supported)\n");
506 else
507 Printf(" (longjmp and C++ exceptions *are* supported)\n");
508
Timur Iskhodzhanov997454a2013-09-10 08:36:21 +0000509 DescribeThread(t);
Alexey Samsonove218beb2012-08-09 09:06:52 +0000510 return true;
511}
512
Alexey Samsonov5c153fa2012-09-18 07:38:10 +0000513static void DescribeAccessToHeapChunk(AsanChunkView chunk, uptr addr,
514 uptr access_size) {
Evgeniy Stepanov589dcda2013-02-05 14:32:03 +0000515 sptr offset;
Kostya Serebryany58f54552012-12-18 07:32:16 +0000516 Decorator d;
Stephen Hines2d1fdb22014-05-28 23:58:16 -0700517 InternalScopedString str(4096);
518 str.append("%s", d.Location());
Evgeniy Stepanov589dcda2013-02-05 14:32:03 +0000519 if (chunk.AddrIsAtLeft(addr, access_size, &offset)) {
Stephen Hines2d1fdb22014-05-28 23:58:16 -0700520 str.append("%p is located %zd bytes to the left of", (void *)addr, offset);
Alexey Samsonov5c153fa2012-09-18 07:38:10 +0000521 } else if (chunk.AddrIsAtRight(addr, access_size, &offset)) {
Evgeniy Stepanov589dcda2013-02-05 14:32:03 +0000522 if (offset < 0) {
523 addr -= offset;
524 offset = 0;
525 }
Stephen Hines2d1fdb22014-05-28 23:58:16 -0700526 str.append("%p is located %zd bytes to the right of", (void *)addr, offset);
Evgeniy Stepanov589dcda2013-02-05 14:32:03 +0000527 } else if (chunk.AddrIsInside(addr, access_size, &offset)) {
Stephen Hines2d1fdb22014-05-28 23:58:16 -0700528 str.append("%p is located %zd bytes inside of", (void*)addr, offset);
Alexey Samsonov5c153fa2012-09-18 07:38:10 +0000529 } else {
Stephen Hines2d1fdb22014-05-28 23:58:16 -0700530 str.append("%p is located somewhere around (this is AddressSanitizer bug!)",
531 (void *)addr);
Alexey Samsonov5c153fa2012-09-18 07:38:10 +0000532 }
Stephen Hines2d1fdb22014-05-28 23:58:16 -0700533 str.append(" %zu-byte region [%p,%p)\n", chunk.UsedSize(),
534 (void *)(chunk.Beg()), (void *)(chunk.End()));
535 str.append("%s", d.EndLocation());
536 Printf("%s", str.data());
Alexey Samsonov5c153fa2012-09-18 07:38:10 +0000537}
538
539void DescribeHeapAddress(uptr addr, uptr access_size) {
540 AsanChunkView chunk = FindHeapChunkByAddress(addr);
Alexey Samsonovd9def292013-10-14 11:13:54 +0000541 if (!chunk.IsValid()) {
542 Printf("AddressSanitizer can not describe address in more detail "
543 "(wild memory access suspected).\n");
544 return;
545 }
Alexey Samsonov5c153fa2012-09-18 07:38:10 +0000546 DescribeAccessToHeapChunk(chunk, addr, access_size);
547 CHECK(chunk.AllocTid() != kInvalidTid);
Alexey Samsonovdef1be92013-03-21 11:23:41 +0000548 asanThreadRegistry().CheckLocked();
549 AsanThreadContext *alloc_thread =
550 GetThreadContextByTidLocked(chunk.AllocTid());
Stephen Hines6d186232014-11-26 17:56:19 -0800551 StackTrace alloc_stack = chunk.GetAllocStack();
Kostya Serebryany716e2f22012-12-07 15:15:01 +0000552 char tname[128];
Kostya Serebryany58f54552012-12-18 07:32:16 +0000553 Decorator d;
Pirama Arumuga Nainar799172d2016-03-03 15:50:30 -0800554 AsanThreadContext *free_thread = nullptr;
Alexey Samsonov5c153fa2012-09-18 07:38:10 +0000555 if (chunk.FreeTid() != kInvalidTid) {
Timur Iskhodzhanov997454a2013-09-10 08:36:21 +0000556 free_thread = GetThreadContextByTidLocked(chunk.FreeTid());
Kostya Serebryany58f54552012-12-18 07:32:16 +0000557 Printf("%sfreed by thread T%d%s here:%s\n", d.Allocation(),
Alexey Samsonovdef1be92013-03-21 11:23:41 +0000558 free_thread->tid,
Kostya Serebryany58f54552012-12-18 07:32:16 +0000559 ThreadNameWithParenthesis(free_thread, tname, sizeof(tname)),
560 d.EndAllocation());
Stephen Hines6d186232014-11-26 17:56:19 -0800561 StackTrace free_stack = chunk.GetFreeStack();
Stephen Hines2d1fdb22014-05-28 23:58:16 -0700562 free_stack.Print();
Kostya Serebryany58f54552012-12-18 07:32:16 +0000563 Printf("%spreviously allocated by thread T%d%s here:%s\n",
Alexey Samsonovdef1be92013-03-21 11:23:41 +0000564 d.Allocation(), alloc_thread->tid,
Kostya Serebryany58f54552012-12-18 07:32:16 +0000565 ThreadNameWithParenthesis(alloc_thread, tname, sizeof(tname)),
566 d.EndAllocation());
Alexey Samsonov5c153fa2012-09-18 07:38:10 +0000567 } else {
Kostya Serebryany58f54552012-12-18 07:32:16 +0000568 Printf("%sallocated by thread T%d%s here:%s\n", d.Allocation(),
Alexey Samsonovdef1be92013-03-21 11:23:41 +0000569 alloc_thread->tid,
Kostya Serebryany58f54552012-12-18 07:32:16 +0000570 ThreadNameWithParenthesis(alloc_thread, tname, sizeof(tname)),
571 d.EndAllocation());
Alexey Samsonov5c153fa2012-09-18 07:38:10 +0000572 }
Stephen Hines2d1fdb22014-05-28 23:58:16 -0700573 alloc_stack.Print();
Timur Iskhodzhanov997454a2013-09-10 08:36:21 +0000574 DescribeThread(GetCurrentThread());
575 if (free_thread)
576 DescribeThread(free_thread);
577 DescribeThread(alloc_thread);
Alexey Samsonov5c153fa2012-09-18 07:38:10 +0000578}
579
Pirama Arumuga Nainarcdce50b2015-07-01 12:26:56 -0700580static void DescribeAddress(uptr addr, uptr access_size, const char *bug_type) {
Alexey Samsonove218beb2012-08-09 09:06:52 +0000581 // Check if this is shadow or shadow gap.
582 if (DescribeAddressIfShadow(addr))
583 return;
584 CHECK(AddrIsInMem(addr));
Pirama Arumuga Nainarcdce50b2015-07-01 12:26:56 -0700585 if (DescribeAddressIfGlobal(addr, access_size, bug_type))
Alexey Samsonove218beb2012-08-09 09:06:52 +0000586 return;
587 if (DescribeAddressIfStack(addr, access_size))
588 return;
589 // Assume it is a heap address.
590 DescribeHeapAddress(addr, access_size);
591}
592
Alexey Samsonov71b42c92012-09-05 07:37:15 +0000593// ------------------- Thread description -------------------- {{{1
594
Alexey Samsonovdef1be92013-03-21 11:23:41 +0000595void DescribeThread(AsanThreadContext *context) {
596 CHECK(context);
597 asanThreadRegistry().CheckLocked();
Alexey Samsonov71b42c92012-09-05 07:37:15 +0000598 // No need to announce the main thread.
Alexey Samsonovdef1be92013-03-21 11:23:41 +0000599 if (context->tid == 0 || context->announced) {
Alexey Samsonov71b42c92012-09-05 07:37:15 +0000600 return;
601 }
Alexey Samsonovdef1be92013-03-21 11:23:41 +0000602 context->announced = true;
Kostya Serebryany716e2f22012-12-07 15:15:01 +0000603 char tname[128];
Stephen Hines2d1fdb22014-05-28 23:58:16 -0700604 InternalScopedString str(1024);
605 str.append("Thread T%d%s", context->tid,
606 ThreadNameWithParenthesis(context->tid, tname, sizeof(tname)));
Pirama Arumuga Nainar259f7062015-05-06 11:49:53 -0700607 if (context->parent_tid == kInvalidTid) {
608 str.append(" created by unknown thread\n");
609 Printf("%s", str.data());
610 return;
611 }
Stephen Hines2d1fdb22014-05-28 23:58:16 -0700612 str.append(
613 " created by T%d%s here:\n", context->parent_tid,
614 ThreadNameWithParenthesis(context->parent_tid, tname, sizeof(tname)));
615 Printf("%s", str.data());
Stephen Hines6d186232014-11-26 17:56:19 -0800616 StackDepotGet(context->stack_id).Print();
Alexey Samsonov71b42c92012-09-05 07:37:15 +0000617 // Recursively described parent thread if needed.
618 if (flags()->print_full_thread_history) {
Alexey Samsonovdef1be92013-03-21 11:23:41 +0000619 AsanThreadContext *parent_context =
620 GetThreadContextByTidLocked(context->parent_tid);
621 DescribeThread(parent_context);
Alexey Samsonov71b42c92012-09-05 07:37:15 +0000622 }
623}
624
Alexey Samsonove218beb2012-08-09 09:06:52 +0000625// -------------------- Different kinds of reports ----------------- {{{1
626
Alexey Samsonov98737922012-08-10 15:13:05 +0000627// Use ScopedInErrorReport to run common actions just before and
628// immediately after printing error report.
629class ScopedInErrorReport {
630 public:
Pirama Arumuga Nainar799172d2016-03-03 15:50:30 -0800631 explicit ScopedInErrorReport(ReportData *report = nullptr,
632 bool fatal = false) {
633 halt_on_error_ = fatal || flags()->halt_on_error;
634
635 if (lock_.TryLock()) {
636 StartReporting(report);
637 return;
638 }
639
640 // ASan found two bugs in different threads simultaneously.
641
642 u32 current_tid = GetCurrentTidOrInvalid();
643 if (reporting_thread_tid_ == current_tid ||
644 reporting_thread_tid_ == kInvalidTid) {
645 // This is either asynch signal or nested error during error reporting.
646 // Fail simple to avoid deadlocks in Report().
647
648 // Can't use Report() here because of potential deadlocks
649 // in nested signal handlers.
650 const char msg[] = "AddressSanitizer: nested bug in the same thread, "
651 "aborting.\n";
652 WriteToFile(kStderrFd, msg, sizeof(msg));
653
654 internal__exit(common_flags()->exitcode);
655 }
656
657 if (halt_on_error_) {
Alexey Samsonov98737922012-08-10 15:13:05 +0000658 // Do not print more than one report, otherwise they will mix up.
659 // Error reporting functions shouldn't return at this situation, as
Pirama Arumuga Nainar799172d2016-03-03 15:50:30 -0800660 // they are effectively no-returns.
661
Stephen Hines6d186232014-11-26 17:56:19 -0800662 Report("AddressSanitizer: while reporting a bug found another one. "
Pirama Arumuga Nainar799172d2016-03-03 15:50:30 -0800663 "Ignoring.\n");
664
665 // Sleep long enough to make sure that the thread which started
666 // to print an error report will finish doing it.
667 SleepForSeconds(Max(100, flags()->sleep_before_dying + 1));
668
Alexey Samsonovf8822472013-02-20 13:54:32 +0000669 // If we're still not dead for some reason, use raw _exit() instead of
Alexey Samsonov031633b2012-11-19 11:22:22 +0000670 // Die() to bypass any additional checks.
Pirama Arumuga Nainar799172d2016-03-03 15:50:30 -0800671 internal__exit(common_flags()->exitcode);
672 } else {
673 // The other thread will eventually finish reporting
674 // so it's safe to wait
675 lock_.Lock();
Alexey Samsonov98737922012-08-10 15:13:05 +0000676 }
Pirama Arumuga Nainar799172d2016-03-03 15:50:30 -0800677
678 StartReporting(report);
679 }
680
681 ~ScopedInErrorReport() {
682 // Make sure the current thread is announced.
683 DescribeThread(GetCurrentThread());
684 // We may want to grab this lock again when printing stats.
685 asanThreadRegistry().Unlock();
686 // Print memory stats.
687 if (flags()->print_stats)
688 __asan_print_accumulated_stats();
689
690 // Copy the message buffer so that we could start logging without holding a
691 // lock that gets aquired during printing.
692 InternalScopedBuffer<char> buffer_copy(kErrorMessageBufferSize);
693 {
694 BlockingMutexLock l(&error_message_buf_mutex);
695 internal_memcpy(buffer_copy.data(),
696 error_message_buffer, kErrorMessageBufferSize);
697 }
698
699 // Remove color sequences since logs cannot print them.
700 RemoveANSIEscapeSequencesFromString(buffer_copy.data());
701
702 LogFullErrorReport(buffer_copy.data());
703
704 if (error_report_callback) {
705 error_report_callback(buffer_copy.data());
706 }
707 CommonSanitizerReportMutex.Unlock();
708 reporting_thread_tid_ = kInvalidTid;
709 lock_.Unlock();
710 if (halt_on_error_) {
711 Report("ABORTING\n");
712 Die();
713 }
714 }
715
716 private:
717 void StartReporting(ReportData *report) {
Stephen Hines6d186232014-11-26 17:56:19 -0800718 if (report) report_data = *report;
719 report_happened = true;
Alexey Samsonov6a08d292012-12-07 22:01:28 +0000720 ASAN_ON_ERROR();
Alexey Samsonov7ed46ff2013-04-05 07:30:29 +0000721 // Make sure the registry and sanitizer report mutexes are locked while
722 // we're printing an error report.
723 // We can lock them only here to avoid self-deadlock in case of
Alexey Samsonovdef1be92013-03-21 11:23:41 +0000724 // recursive reports.
725 asanThreadRegistry().Lock();
Alexey Samsonov7ed46ff2013-04-05 07:30:29 +0000726 CommonSanitizerReportMutex.Lock();
Pirama Arumuga Nainar799172d2016-03-03 15:50:30 -0800727 reporting_thread_tid_ = GetCurrentTidOrInvalid();
Kostya Serebryany283c2962012-08-28 11:34:40 +0000728 Printf("===================================================="
Alexey Samsonov62e27092012-09-17 08:02:19 +0000729 "=============\n");
Alexey Samsonov98737922012-08-10 15:13:05 +0000730 }
Pirama Arumuga Nainar799172d2016-03-03 15:50:30 -0800731
732 static StaticSpinMutex lock_;
733 static u32 reporting_thread_tid_;
734 bool halt_on_error_;
Alexey Samsonov98737922012-08-10 15:13:05 +0000735};
736
Pirama Arumuga Nainar799172d2016-03-03 15:50:30 -0800737StaticSpinMutex ScopedInErrorReport::lock_;
738u32 ScopedInErrorReport::reporting_thread_tid_;
739
Stephen Hines86277eb2015-03-23 12:06:32 -0700740void ReportStackOverflow(const SignalContext &sig) {
Alexey Samsonov98737922012-08-10 15:13:05 +0000741 ScopedInErrorReport in_report;
Kostya Serebryany58f54552012-12-18 07:32:16 +0000742 Decorator d;
743 Printf("%s", d.Warning());
Stephen Hines2d1fdb22014-05-28 23:58:16 -0700744 Report(
745 "ERROR: AddressSanitizer: stack-overflow on address %p"
Stephen Hines6d186232014-11-26 17:56:19 -0800746 " (pc %p bp %p sp %p T%d)\n",
Stephen Hines86277eb2015-03-23 12:06:32 -0700747 (void *)sig.addr, (void *)sig.pc, (void *)sig.bp, (void *)sig.sp,
Stephen Hines2d1fdb22014-05-28 23:58:16 -0700748 GetCurrentTidOrInvalid());
Kostya Serebryany58f54552012-12-18 07:32:16 +0000749 Printf("%s", d.EndWarning());
Stephen Hines86277eb2015-03-23 12:06:32 -0700750 GET_STACK_TRACE_SIGNAL(sig);
Stephen Hines2d1fdb22014-05-28 23:58:16 -0700751 stack.Print();
752 ReportErrorSummary("stack-overflow", &stack);
753}
754
Pirama Arumuga Nainar799172d2016-03-03 15:50:30 -0800755void ReportDeadlySignal(const char *description, const SignalContext &sig) {
756 ScopedInErrorReport in_report(/*report*/nullptr, /*fatal*/true);
Stephen Hines2d1fdb22014-05-28 23:58:16 -0700757 Decorator d;
758 Printf("%s", d.Warning());
759 Report(
Stephen Hines6d186232014-11-26 17:56:19 -0800760 "ERROR: AddressSanitizer: %s on unknown address %p"
761 " (pc %p bp %p sp %p T%d)\n",
Stephen Hines86277eb2015-03-23 12:06:32 -0700762 description, (void *)sig.addr, (void *)sig.pc, (void *)sig.bp,
763 (void *)sig.sp, GetCurrentTidOrInvalid());
764 if (sig.pc < GetPageSizeCached()) {
Stephen Hines6d186232014-11-26 17:56:19 -0800765 Report("Hint: pc points to the zero page.\n");
766 }
Stephen Hines2d1fdb22014-05-28 23:58:16 -0700767 Printf("%s", d.EndWarning());
Stephen Hines86277eb2015-03-23 12:06:32 -0700768 GET_STACK_TRACE_SIGNAL(sig);
Stephen Hines2d1fdb22014-05-28 23:58:16 -0700769 stack.Print();
Stephen Hines86277eb2015-03-23 12:06:32 -0700770 MaybeDumpInstructionBytes(sig.pc);
Alexey Samsonovd9def292013-10-14 11:13:54 +0000771 Printf("AddressSanitizer can not provide additional info.\n");
Pirama Arumuga Nainar799172d2016-03-03 15:50:30 -0800772 ReportErrorSummary(description, &stack);
Alexey Samsonov73545092012-08-09 07:40:58 +0000773}
774
Stephen Hines6d186232014-11-26 17:56:19 -0800775void ReportDoubleFree(uptr addr, BufferedStackTrace *free_stack) {
Alexey Samsonov98737922012-08-10 15:13:05 +0000776 ScopedInErrorReport in_report;
Kostya Serebryany58f54552012-12-18 07:32:16 +0000777 Decorator d;
778 Printf("%s", d.Warning());
Kostya Serebryanya89a35a2013-03-26 08:01:37 +0000779 char tname[128];
780 u32 curr_tid = GetCurrentTidOrInvalid();
781 Report("ERROR: AddressSanitizer: attempting double-free on %p in "
782 "thread T%d%s:\n",
783 addr, curr_tid,
784 ThreadNameWithParenthesis(curr_tid, tname, sizeof(tname)));
Kostya Serebryany58f54552012-12-18 07:32:16 +0000785 Printf("%s", d.EndWarning());
Alexey Samsonov1b17f5b2013-11-13 14:46:58 +0000786 CHECK_GT(free_stack->size, 0);
787 GET_STACK_TRACE_FATAL(free_stack->trace[0], free_stack->top_frame_bp);
Stephen Hines2d1fdb22014-05-28 23:58:16 -0700788 stack.Print();
Alexey Samsonovf7c1d182012-08-09 08:15:46 +0000789 DescribeHeapAddress(addr, 1);
Alexey Samsonov1b17f5b2013-11-13 14:46:58 +0000790 ReportErrorSummary("double-free", &stack);
Alexey Samsonovf7c1d182012-08-09 08:15:46 +0000791}
792
Stephen Hines6d186232014-11-26 17:56:19 -0800793void ReportNewDeleteSizeMismatch(uptr addr, uptr delete_size,
794 BufferedStackTrace *free_stack) {
795 ScopedInErrorReport in_report;
796 Decorator d;
797 Printf("%s", d.Warning());
798 char tname[128];
799 u32 curr_tid = GetCurrentTidOrInvalid();
800 Report("ERROR: AddressSanitizer: new-delete-type-mismatch on %p in "
801 "thread T%d%s:\n",
802 addr, curr_tid,
803 ThreadNameWithParenthesis(curr_tid, tname, sizeof(tname)));
804 Printf("%s object passed to delete has wrong type:\n", d.EndWarning());
805 Printf(" size of the allocated type: %zd bytes;\n"
806 " size of the deallocated type: %zd bytes.\n",
807 asan_mz_size(reinterpret_cast<void*>(addr)), delete_size);
808 CHECK_GT(free_stack->size, 0);
809 GET_STACK_TRACE_FATAL(free_stack->trace[0], free_stack->top_frame_bp);
810 stack.Print();
811 DescribeHeapAddress(addr, 1);
812 ReportErrorSummary("new-delete-type-mismatch", &stack);
Pirama Arumuga Nainar799172d2016-03-03 15:50:30 -0800813 Report("HINT: if you don't care about these errors you may set "
Stephen Hines6d186232014-11-26 17:56:19 -0800814 "ASAN_OPTIONS=new_delete_type_mismatch=0\n");
815}
816
817void ReportFreeNotMalloced(uptr addr, BufferedStackTrace *free_stack) {
Alexey Samsonov98737922012-08-10 15:13:05 +0000818 ScopedInErrorReport in_report;
Kostya Serebryany58f54552012-12-18 07:32:16 +0000819 Decorator d;
820 Printf("%s", d.Warning());
Kostya Serebryanya89a35a2013-03-26 08:01:37 +0000821 char tname[128];
822 u32 curr_tid = GetCurrentTidOrInvalid();
Kostya Serebryany69d8ede2012-10-15 13:04:58 +0000823 Report("ERROR: AddressSanitizer: attempting free on address "
Kostya Serebryanya89a35a2013-03-26 08:01:37 +0000824 "which was not malloc()-ed: %p in thread T%d%s\n", addr,
825 curr_tid, ThreadNameWithParenthesis(curr_tid, tname, sizeof(tname)));
Kostya Serebryany58f54552012-12-18 07:32:16 +0000826 Printf("%s", d.EndWarning());
Alexey Samsonov1b17f5b2013-11-13 14:46:58 +0000827 CHECK_GT(free_stack->size, 0);
828 GET_STACK_TRACE_FATAL(free_stack->trace[0], free_stack->top_frame_bp);
Stephen Hines2d1fdb22014-05-28 23:58:16 -0700829 stack.Print();
Alexey Samsonov98737922012-08-10 15:13:05 +0000830 DescribeHeapAddress(addr, 1);
Alexey Samsonov1b17f5b2013-11-13 14:46:58 +0000831 ReportErrorSummary("bad-free", &stack);
Alexey Samsonovf7c1d182012-08-09 08:15:46 +0000832}
833
Stephen Hines6d186232014-11-26 17:56:19 -0800834void ReportAllocTypeMismatch(uptr addr, BufferedStackTrace *free_stack,
Kostya Serebryanyfe6d9162012-12-21 08:53:59 +0000835 AllocType alloc_type,
836 AllocType dealloc_type) {
837 static const char *alloc_names[] =
838 {"INVALID", "malloc", "operator new", "operator new []"};
839 static const char *dealloc_names[] =
840 {"INVALID", "free", "operator delete", "operator delete []"};
841 CHECK_NE(alloc_type, dealloc_type);
842 ScopedInErrorReport in_report;
843 Decorator d;
844 Printf("%s", d.Warning());
845 Report("ERROR: AddressSanitizer: alloc-dealloc-mismatch (%s vs %s) on %p\n",
846 alloc_names[alloc_type], dealloc_names[dealloc_type], addr);
847 Printf("%s", d.EndWarning());
Alexey Samsonov1b17f5b2013-11-13 14:46:58 +0000848 CHECK_GT(free_stack->size, 0);
849 GET_STACK_TRACE_FATAL(free_stack->trace[0], free_stack->top_frame_bp);
Stephen Hines2d1fdb22014-05-28 23:58:16 -0700850 stack.Print();
Kostya Serebryanyfe6d9162012-12-21 08:53:59 +0000851 DescribeHeapAddress(addr, 1);
Alexey Samsonov1b17f5b2013-11-13 14:46:58 +0000852 ReportErrorSummary("alloc-dealloc-mismatch", &stack);
Pirama Arumuga Nainar799172d2016-03-03 15:50:30 -0800853 Report("HINT: if you don't care about these errors you may set "
Kostya Serebryanyfe6d9162012-12-21 08:53:59 +0000854 "ASAN_OPTIONS=alloc_dealloc_mismatch=0\n");
855}
856
Stephen Hines6d186232014-11-26 17:56:19 -0800857void ReportMallocUsableSizeNotOwned(uptr addr, BufferedStackTrace *stack) {
Alexey Samsonov98737922012-08-10 15:13:05 +0000858 ScopedInErrorReport in_report;
Kostya Serebryany58f54552012-12-18 07:32:16 +0000859 Decorator d;
860 Printf("%s", d.Warning());
Kostya Serebryany69d8ede2012-10-15 13:04:58 +0000861 Report("ERROR: AddressSanitizer: attempting to call "
Alexey Samsonovf7c1d182012-08-09 08:15:46 +0000862 "malloc_usable_size() for pointer which is "
863 "not owned: %p\n", addr);
Kostya Serebryany58f54552012-12-18 07:32:16 +0000864 Printf("%s", d.EndWarning());
Stephen Hines2d1fdb22014-05-28 23:58:16 -0700865 stack->Print();
Alexey Samsonovf7c1d182012-08-09 08:15:46 +0000866 DescribeHeapAddress(addr, 1);
Alexey Samsonov2fb08722013-11-01 17:02:14 +0000867 ReportErrorSummary("bad-malloc_usable_size", stack);
Alexey Samsonovf7c1d182012-08-09 08:15:46 +0000868}
869
Stephen Hines6d186232014-11-26 17:56:19 -0800870void ReportSanitizerGetAllocatedSizeNotOwned(uptr addr,
871 BufferedStackTrace *stack) {
Alexey Samsonov98737922012-08-10 15:13:05 +0000872 ScopedInErrorReport in_report;
Kostya Serebryany58f54552012-12-18 07:32:16 +0000873 Decorator d;
874 Printf("%s", d.Warning());
Kostya Serebryany69d8ede2012-10-15 13:04:58 +0000875 Report("ERROR: AddressSanitizer: attempting to call "
Stephen Hines6a211c52014-07-21 00:49:56 -0700876 "__sanitizer_get_allocated_size() for pointer which is "
Alexey Samsonovf7c1d182012-08-09 08:15:46 +0000877 "not owned: %p\n", addr);
Kostya Serebryany58f54552012-12-18 07:32:16 +0000878 Printf("%s", d.EndWarning());
Stephen Hines2d1fdb22014-05-28 23:58:16 -0700879 stack->Print();
Alexey Samsonovf7c1d182012-08-09 08:15:46 +0000880 DescribeHeapAddress(addr, 1);
Stephen Hines6a211c52014-07-21 00:49:56 -0700881 ReportErrorSummary("bad-__sanitizer_get_allocated_size", stack);
Alexey Samsonovf7c1d182012-08-09 08:15:46 +0000882}
883
Stephen Hines6d186232014-11-26 17:56:19 -0800884void ReportStringFunctionMemoryRangesOverlap(const char *function,
885 const char *offset1, uptr length1,
886 const char *offset2, uptr length2,
887 BufferedStackTrace *stack) {
Alexey Samsonov98737922012-08-10 15:13:05 +0000888 ScopedInErrorReport in_report;
Kostya Serebryany58f54552012-12-18 07:32:16 +0000889 Decorator d;
Kostya Serebryany2673fd82013-02-06 12:36:49 +0000890 char bug_type[100];
891 internal_snprintf(bug_type, sizeof(bug_type), "%s-param-overlap", function);
Kostya Serebryany58f54552012-12-18 07:32:16 +0000892 Printf("%s", d.Warning());
Kostya Serebryany2673fd82013-02-06 12:36:49 +0000893 Report("ERROR: AddressSanitizer: %s: "
Alexey Samsonov487fee72012-08-09 08:32:33 +0000894 "memory ranges [%p,%p) and [%p, %p) overlap\n", \
Kostya Serebryany2673fd82013-02-06 12:36:49 +0000895 bug_type, offset1, offset1 + length1, offset2, offset2 + length2);
Kostya Serebryany58f54552012-12-18 07:32:16 +0000896 Printf("%s", d.EndWarning());
Stephen Hines2d1fdb22014-05-28 23:58:16 -0700897 stack->Print();
Pirama Arumuga Nainarcdce50b2015-07-01 12:26:56 -0700898 DescribeAddress((uptr)offset1, length1, bug_type);
899 DescribeAddress((uptr)offset2, length2, bug_type);
Alexey Samsonov2fb08722013-11-01 17:02:14 +0000900 ReportErrorSummary(bug_type, stack);
Alexey Samsonov487fee72012-08-09 08:32:33 +0000901}
Alexey Samsonovf7c1d182012-08-09 08:15:46 +0000902
Stephen Hines2d1fdb22014-05-28 23:58:16 -0700903void ReportStringFunctionSizeOverflow(uptr offset, uptr size,
Stephen Hines6d186232014-11-26 17:56:19 -0800904 BufferedStackTrace *stack) {
Stephen Hines2d1fdb22014-05-28 23:58:16 -0700905 ScopedInErrorReport in_report;
906 Decorator d;
907 const char *bug_type = "negative-size-param";
908 Printf("%s", d.Warning());
909 Report("ERROR: AddressSanitizer: %s: (size=%zd)\n", bug_type, size);
910 Printf("%s", d.EndWarning());
911 stack->Print();
Pirama Arumuga Nainarcdce50b2015-07-01 12:26:56 -0700912 DescribeAddress(offset, size, bug_type);
Stephen Hines2d1fdb22014-05-28 23:58:16 -0700913 ReportErrorSummary(bug_type, stack);
914}
915
916void ReportBadParamsToAnnotateContiguousContainer(uptr beg, uptr end,
917 uptr old_mid, uptr new_mid,
Stephen Hines6d186232014-11-26 17:56:19 -0800918 BufferedStackTrace *stack) {
Stephen Hines2d1fdb22014-05-28 23:58:16 -0700919 ScopedInErrorReport in_report;
920 Report("ERROR: AddressSanitizer: bad parameters to "
921 "__sanitizer_annotate_contiguous_container:\n"
922 " beg : %p\n"
923 " end : %p\n"
924 " old_mid : %p\n"
925 " new_mid : %p\n",
926 beg, end, old_mid, new_mid);
Stephen Hines86277eb2015-03-23 12:06:32 -0700927 uptr granularity = SHADOW_GRANULARITY;
928 if (!IsAligned(beg, granularity))
929 Report("ERROR: beg is not aligned by %d\n", granularity);
Stephen Hines2d1fdb22014-05-28 23:58:16 -0700930 stack->Print();
931 ReportErrorSummary("bad-__sanitizer_annotate_contiguous_container", stack);
932}
933
Stephen Hines6a211c52014-07-21 00:49:56 -0700934void ReportODRViolation(const __asan_global *g1, u32 stack_id1,
935 const __asan_global *g2, u32 stack_id2) {
Stephen Hines2d1fdb22014-05-28 23:58:16 -0700936 ScopedInErrorReport in_report;
937 Decorator d;
938 Printf("%s", d.Warning());
939 Report("ERROR: AddressSanitizer: odr-violation (%p):\n", g1->beg);
940 Printf("%s", d.EndWarning());
Stephen Hines6a211c52014-07-21 00:49:56 -0700941 InternalScopedString g1_loc(256), g2_loc(256);
942 PrintGlobalLocation(&g1_loc, *g1);
943 PrintGlobalLocation(&g2_loc, *g2);
Stephen Hines6d186232014-11-26 17:56:19 -0800944 Printf(" [1] size=%zd '%s' %s\n", g1->size,
945 MaybeDemangleGlobalName(g1->name), g1_loc.data());
946 Printf(" [2] size=%zd '%s' %s\n", g2->size,
947 MaybeDemangleGlobalName(g2->name), g2_loc.data());
Stephen Hines6a211c52014-07-21 00:49:56 -0700948 if (stack_id1 && stack_id2) {
949 Printf("These globals were registered at these points:\n");
950 Printf(" [1]:\n");
Stephen Hines6d186232014-11-26 17:56:19 -0800951 StackDepotGet(stack_id1).Print();
Stephen Hines6a211c52014-07-21 00:49:56 -0700952 Printf(" [2]:\n");
Stephen Hines6d186232014-11-26 17:56:19 -0800953 StackDepotGet(stack_id2).Print();
Stephen Hines6a211c52014-07-21 00:49:56 -0700954 }
Pirama Arumuga Nainar799172d2016-03-03 15:50:30 -0800955 Report("HINT: if you don't care about these errors you may set "
Stephen Hines2d1fdb22014-05-28 23:58:16 -0700956 "ASAN_OPTIONS=detect_odr_violation=0\n");
Stephen Hines6d186232014-11-26 17:56:19 -0800957 InternalScopedString error_msg(256);
958 error_msg.append("odr-violation: global '%s' at %s",
959 MaybeDemangleGlobalName(g1->name), g1_loc.data());
960 ReportErrorSummary(error_msg.data());
Stephen Hines2d1fdb22014-05-28 23:58:16 -0700961}
962
963// ----------------------- CheckForInvalidPointerPair ----------- {{{1
964static NOINLINE void
965ReportInvalidPointerPair(uptr pc, uptr bp, uptr sp, uptr a1, uptr a2) {
966 ScopedInErrorReport in_report;
Pirama Arumuga Nainarcdce50b2015-07-01 12:26:56 -0700967 const char *bug_type = "invalid-pointer-pair";
Stephen Hines2d1fdb22014-05-28 23:58:16 -0700968 Decorator d;
969 Printf("%s", d.Warning());
970 Report("ERROR: AddressSanitizer: invalid-pointer-pair: %p %p\n", a1, a2);
971 Printf("%s", d.EndWarning());
972 GET_STACK_TRACE_FATAL(pc, bp);
973 stack.Print();
Pirama Arumuga Nainarcdce50b2015-07-01 12:26:56 -0700974 DescribeAddress(a1, 1, bug_type);
975 DescribeAddress(a2, 1, bug_type);
976 ReportErrorSummary(bug_type, &stack);
Stephen Hines2d1fdb22014-05-28 23:58:16 -0700977}
978
979static INLINE void CheckForInvalidPointerPair(void *p1, void *p2) {
980 if (!flags()->detect_invalid_pointer_pairs) return;
981 uptr a1 = reinterpret_cast<uptr>(p1);
982 uptr a2 = reinterpret_cast<uptr>(p2);
983 AsanChunkView chunk1 = FindHeapChunkByAddress(a1);
984 AsanChunkView chunk2 = FindHeapChunkByAddress(a2);
985 bool valid1 = chunk1.IsValid();
986 bool valid2 = chunk2.IsValid();
987 if ((valid1 != valid2) || (valid1 && valid2 && !chunk1.Eq(chunk2))) {
988 GET_CALLER_PC_BP_SP; \
989 return ReportInvalidPointerPair(pc, bp, sp, a1, a2);
990 }
991}
Alexey Samsonov663c5012012-08-09 12:15:40 +0000992// ----------------------- Mac-specific reports ----------------- {{{1
993
Stephen Hines6d186232014-11-26 17:56:19 -0800994void ReportMacMzReallocUnknown(uptr addr, uptr zone_ptr, const char *zone_name,
995 BufferedStackTrace *stack) {
Alexey Samsonov98737922012-08-10 15:13:05 +0000996 ScopedInErrorReport in_report;
Kostya Serebryany283c2962012-08-28 11:34:40 +0000997 Printf("mz_realloc(%p) -- attempting to realloc unallocated memory.\n"
Alexey Samsonov663c5012012-08-09 12:15:40 +0000998 "This is an unrecoverable problem, exiting now.\n",
999 addr);
1000 PrintZoneForPointer(addr, zone_ptr, zone_name);
Stephen Hines2d1fdb22014-05-28 23:58:16 -07001001 stack->Print();
Alexey Samsonov98737922012-08-10 15:13:05 +00001002 DescribeHeapAddress(addr, 1);
Alexey Samsonov663c5012012-08-09 12:15:40 +00001003}
1004
Pirama Arumuga Nainar799172d2016-03-03 15:50:30 -08001005// -------------- SuppressErrorReport -------------- {{{1
1006// Avoid error reports duplicating for ASan recover mode.
1007static bool SuppressErrorReport(uptr pc) {
1008 if (!common_flags()->suppress_equal_pcs) return false;
1009 for (unsigned i = 0; i < kAsanBuggyPcPoolSize; i++) {
1010 uptr cmp = atomic_load_relaxed(&AsanBuggyPcPool[i]);
1011 if (cmp == 0 && atomic_compare_exchange_strong(&AsanBuggyPcPool[i], &cmp,
1012 pc, memory_order_relaxed))
1013 return false;
1014 if (cmp == pc) return true;
1015 }
1016 Die();
Alexey Samsonovc98570b2012-08-09 10:56:57 +00001017}
1018
Pirama Arumuga Nainar799172d2016-03-03 15:50:30 -08001019void ReportGenericError(uptr pc, uptr bp, uptr sp, uptr addr, bool is_write,
1020 uptr access_size, u32 exp, bool fatal) {
1021 if (!fatal && SuppressErrorReport(pc)) return;
Stephen Hines86277eb2015-03-23 12:06:32 -07001022 ENABLE_FRAME_POINTER;
1023
Pirama Arumuga Nainar7c915052015-04-08 08:58:29 -07001024 // Optimization experiments.
1025 // The experiments can be used to evaluate potential optimizations that remove
1026 // instrumentation (assess false negatives). Instead of completely removing
1027 // some instrumentation, compiler can emit special calls into runtime
1028 // (e.g. __asan_report_exp_load1 instead of __asan_report_load1) and pass
1029 // mask of experiments (exp).
1030 // The reaction to a non-zero value of exp is to be defined.
1031 (void)exp;
1032
Alexey Samsonov98737922012-08-10 15:13:05 +00001033 // Determine the error type.
Alexey Samsonovc98570b2012-08-09 10:56:57 +00001034 const char *bug_descr = "unknown-crash";
1035 if (AddrIsInMem(addr)) {
1036 u8 *shadow_addr = (u8*)MemToShadow(addr);
1037 // If we are accessing 16 bytes, look at the second shadow byte.
1038 if (*shadow_addr == 0 && access_size > SHADOW_GRANULARITY)
1039 shadow_addr++;
1040 // If we are in the partial right redzone, look at the next shadow byte.
1041 if (*shadow_addr > 0 && *shadow_addr < 128)
1042 shadow_addr++;
1043 switch (*shadow_addr) {
1044 case kAsanHeapLeftRedzoneMagic:
1045 case kAsanHeapRightRedzoneMagic:
Stephen Hines6d186232014-11-26 17:56:19 -08001046 case kAsanArrayCookieMagic:
Alexey Samsonovc98570b2012-08-09 10:56:57 +00001047 bug_descr = "heap-buffer-overflow";
1048 break;
1049 case kAsanHeapFreeMagic:
1050 bug_descr = "heap-use-after-free";
1051 break;
1052 case kAsanStackLeftRedzoneMagic:
1053 bug_descr = "stack-buffer-underflow";
1054 break;
Kostya Serebryany3945c582012-08-21 14:10:25 +00001055 case kAsanInitializationOrderMagic:
1056 bug_descr = "initialization-order-fiasco";
1057 break;
Alexey Samsonovc98570b2012-08-09 10:56:57 +00001058 case kAsanStackMidRedzoneMagic:
1059 case kAsanStackRightRedzoneMagic:
1060 case kAsanStackPartialRedzoneMagic:
1061 bug_descr = "stack-buffer-overflow";
1062 break;
1063 case kAsanStackAfterReturnMagic:
1064 bug_descr = "stack-use-after-return";
1065 break;
1066 case kAsanUserPoisonedMemoryMagic:
1067 bug_descr = "use-after-poison";
1068 break;
Stephen Hines2d1fdb22014-05-28 23:58:16 -07001069 case kAsanContiguousContainerOOBMagic:
1070 bug_descr = "container-overflow";
1071 break;
Alexey Samsonovd4b5db82012-12-04 01:38:15 +00001072 case kAsanStackUseAfterScopeMagic:
1073 bug_descr = "stack-use-after-scope";
1074 break;
Alexey Samsonovc98570b2012-08-09 10:56:57 +00001075 case kAsanGlobalRedzoneMagic:
1076 bug_descr = "global-buffer-overflow";
1077 break;
Stephen Hines6d186232014-11-26 17:56:19 -08001078 case kAsanIntraObjectRedzone:
1079 bug_descr = "intra-object-overflow";
1080 break;
Stephen Hines86277eb2015-03-23 12:06:32 -07001081 case kAsanAllocaLeftMagic:
1082 case kAsanAllocaRightMagic:
1083 bug_descr = "dynamic-stack-buffer-overflow";
1084 break;
Alexey Samsonovc98570b2012-08-09 10:56:57 +00001085 }
1086 }
Stephen Hines6d186232014-11-26 17:56:19 -08001087
1088 ReportData report = { pc, sp, bp, addr, (bool)is_write, access_size,
1089 bug_descr };
Pirama Arumuga Nainar799172d2016-03-03 15:50:30 -08001090 ScopedInErrorReport in_report(&report, fatal);
Stephen Hines6d186232014-11-26 17:56:19 -08001091
Kostya Serebryany58f54552012-12-18 07:32:16 +00001092 Decorator d;
1093 Printf("%s", d.Warning());
Kostya Serebryany69d8ede2012-10-15 13:04:58 +00001094 Report("ERROR: AddressSanitizer: %s on address "
Stephen Hines6d186232014-11-26 17:56:19 -08001095 "%p at pc %p bp %p sp %p\n",
Alexey Samsonovc98570b2012-08-09 10:56:57 +00001096 bug_descr, (void*)addr, pc, bp, sp);
Kostya Serebryany58f54552012-12-18 07:32:16 +00001097 Printf("%s", d.EndWarning());
Alexey Samsonovc98570b2012-08-09 10:56:57 +00001098
Alexey Samsonov89c13842013-03-20 09:23:28 +00001099 u32 curr_tid = GetCurrentTidOrInvalid();
Kostya Serebryany716e2f22012-12-07 15:15:01 +00001100 char tname[128];
Kostya Serebryany58f54552012-12-18 07:32:16 +00001101 Printf("%s%s of size %zu at %p thread T%d%s%s\n",
1102 d.Access(),
1103 access_size ? (is_write ? "WRITE" : "READ") : "ACCESS",
1104 access_size, (void*)addr, curr_tid,
1105 ThreadNameWithParenthesis(curr_tid, tname, sizeof(tname)),
1106 d.EndAccess());
Alexey Samsonovc98570b2012-08-09 10:56:57 +00001107
Kostya Serebryanya30c8f92012-12-13 09:34:23 +00001108 GET_STACK_TRACE_FATAL(pc, bp);
Stephen Hines2d1fdb22014-05-28 23:58:16 -07001109 stack.Print();
Alexey Samsonovc98570b2012-08-09 10:56:57 +00001110
Pirama Arumuga Nainarcdce50b2015-07-01 12:26:56 -07001111 DescribeAddress(addr, access_size, bug_descr);
Alexey Samsonov2fb08722013-11-01 17:02:14 +00001112 ReportErrorSummary(bug_descr, &stack);
Alexey Samsonov98737922012-08-10 15:13:05 +00001113 PrintShadowMemoryForAddress(addr);
Alexey Samsonovc98570b2012-08-09 10:56:57 +00001114}
1115
Pirama Arumuga Nainar799172d2016-03-03 15:50:30 -08001116} // namespace __asan
1117
1118// --------------------------- Interface --------------------- {{{1
1119using namespace __asan; // NOLINT
1120
1121void __asan_report_error(uptr pc, uptr bp, uptr sp, uptr addr, int is_write,
1122 uptr access_size, u32 exp) {
1123 ENABLE_FRAME_POINTER;
1124 bool fatal = flags()->halt_on_error;
1125 ReportGenericError(pc, bp, sp, addr, is_write, access_size, exp, fatal);
1126}
1127
Alexey Samsonovc98570b2012-08-09 10:56:57 +00001128void NOINLINE __asan_set_error_report_callback(void (*callback)(const char*)) {
Pirama Arumuga Nainar799172d2016-03-03 15:50:30 -08001129 BlockingMutexLock l(&error_message_buf_mutex);
Alexey Samsonovc98570b2012-08-09 10:56:57 +00001130 error_report_callback = callback;
Alexey Samsonovc98570b2012-08-09 10:56:57 +00001131}
Alexey Samsonovf657a192012-08-13 11:23:40 +00001132
Kostya Serebryany17a7c672012-12-29 10:18:31 +00001133void __asan_describe_address(uptr addr) {
Stephen Hines6d186232014-11-26 17:56:19 -08001134 // Thread registry must be locked while we're describing an address.
1135 asanThreadRegistry().Lock();
Pirama Arumuga Nainarcdce50b2015-07-01 12:26:56 -07001136 DescribeAddress(addr, 1, "");
Stephen Hines6d186232014-11-26 17:56:19 -08001137 asanThreadRegistry().Unlock();
1138}
1139
1140int __asan_report_present() {
1141 return report_happened ? 1 : 0;
1142}
1143
1144uptr __asan_get_report_pc() {
1145 return report_data.pc;
1146}
1147
1148uptr __asan_get_report_bp() {
1149 return report_data.bp;
1150}
1151
1152uptr __asan_get_report_sp() {
1153 return report_data.sp;
1154}
1155
1156uptr __asan_get_report_address() {
1157 return report_data.addr;
1158}
1159
1160int __asan_get_report_access_type() {
1161 return report_data.is_write ? 1 : 0;
1162}
1163
1164uptr __asan_get_report_access_size() {
1165 return report_data.access_size;
1166}
1167
1168const char *__asan_get_report_description() {
1169 return report_data.description;
Kostya Serebryany17a7c672012-12-29 10:18:31 +00001170}
1171
Stephen Hines2d1fdb22014-05-28 23:58:16 -07001172extern "C" {
1173SANITIZER_INTERFACE_ATTRIBUTE
1174void __sanitizer_ptr_sub(void *a, void *b) {
1175 CheckForInvalidPointerPair(a, b);
1176}
1177SANITIZER_INTERFACE_ATTRIBUTE
1178void __sanitizer_ptr_cmp(void *a, void *b) {
1179 CheckForInvalidPointerPair(a, b);
1180}
Pirama Arumuga Nainar799172d2016-03-03 15:50:30 -08001181} // extern "C"
Stephen Hines2d1fdb22014-05-28 23:58:16 -07001182
Alexey Samsonov6a08d292012-12-07 22:01:28 +00001183#if !SANITIZER_SUPPORTS_WEAK_HOOKS
Alexey Samsonov86633432012-10-02 14:06:39 +00001184// Provide default implementation of __asan_on_error that does nothing
1185// and may be overriden by user.
Timur Iskhodzhanov3c80c6c2013-08-13 11:42:45 +00001186SANITIZER_INTERFACE_ATTRIBUTE SANITIZER_WEAK_ATTRIBUTE NOINLINE
Alexey Samsonov86633432012-10-02 14:06:39 +00001187void __asan_on_error() {}
Alexey Samsonov6a08d292012-12-07 22:01:28 +00001188#endif