Alexey Samsonov | 7354509 | 2012-08-09 07:40:58 +0000 | [diff] [blame] | 1 | //===-- 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 Samsonov | 9873792 | 2012-08-10 15:13:05 +0000 | [diff] [blame] | 14 | #include "asan_flags.h" |
Alexey Samsonov | 7354509 | 2012-08-09 07:40:58 +0000 | [diff] [blame] | 15 | #include "asan_internal.h" |
Alexey Samsonov | e218beb | 2012-08-09 09:06:52 +0000 | [diff] [blame] | 16 | #include "asan_mapping.h" |
Alexey Samsonov | 7354509 | 2012-08-09 07:40:58 +0000 | [diff] [blame] | 17 | #include "asan_report.h" |
| 18 | #include "asan_stack.h" |
Alexey Samsonov | e4bfca2 | 2012-08-09 09:27:24 +0000 | [diff] [blame] | 19 | #include "asan_thread.h" |
Kostya Serebryany | 58f5455 | 2012-12-18 07:32:16 +0000 | [diff] [blame] | 20 | #include "sanitizer_common/sanitizer_common.h" |
Sergey Matveev | ed20ebe | 2013-05-06 11:27:58 +0000 | [diff] [blame] | 21 | #include "sanitizer_common/sanitizer_flags.h" |
Kostya Serebryany | 58f5455 | 2012-12-18 07:32:16 +0000 | [diff] [blame] | 22 | #include "sanitizer_common/sanitizer_report_decorator.h" |
Kostya Serebryany | 6d95869 | 2013-10-18 14:50:44 +0000 | [diff] [blame] | 23 | #include "sanitizer_common/sanitizer_stackdepot.h" |
Alexey Samsonov | 9c92748 | 2012-12-26 14:44:46 +0000 | [diff] [blame] | 24 | #include "sanitizer_common/sanitizer_symbolizer.h" |
Alexey Samsonov | 7354509 | 2012-08-09 07:40:58 +0000 | [diff] [blame] | 25 | |
| 26 | namespace __asan { |
| 27 | |
Alexey Samsonov | f657a19 | 2012-08-13 11:23:40 +0000 | [diff] [blame] | 28 | // -------------------- User-specified callbacks ----------------- {{{1 |
Alexey Samsonov | c98570b | 2012-08-09 10:56:57 +0000 | [diff] [blame] | 29 | static void (*error_report_callback)(const char*); |
| 30 | static char *error_message_buffer = 0; |
| 31 | static uptr error_message_buffer_pos = 0; |
| 32 | static uptr error_message_buffer_size = 0; |
| 33 | |
Stephen Hines | 6d18623 | 2014-11-26 17:56:19 -0800 | [diff] [blame] | 34 | struct 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 | |
| 44 | static bool report_happened = false; |
| 45 | static ReportData report_data = {}; |
| 46 | |
Alexey Samsonov | c98570b | 2012-08-09 10:56:57 +0000 | [diff] [blame] | 47 | void 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 Hines | 86277eb | 2015-03-23 12:06:32 -0700 | [diff] [blame] | 56 | error_message_buffer_pos += Min(remaining, length); |
Alexey Samsonov | c98570b | 2012-08-09 10:56:57 +0000 | [diff] [blame] | 57 | } |
| 58 | } |
| 59 | |
Kostya Serebryany | 58f5455 | 2012-12-18 07:32:16 +0000 | [diff] [blame] | 60 | // ---------------------- Decorator ------------------------------ {{{1 |
Stephen Hines | 2d1fdb2 | 2014-05-28 23:58:16 -0700 | [diff] [blame] | 61 | class Decorator: public __sanitizer::SanitizerCommonDecorator { |
Kostya Serebryany | 58f5455 | 2012-12-18 07:32:16 +0000 | [diff] [blame] | 62 | public: |
Stephen Hines | 2d1fdb2 | 2014-05-28 23:58:16 -0700 | [diff] [blame] | 63 | Decorator() : SanitizerCommonDecorator() { } |
Kostya Serebryany | 58f5455 | 2012-12-18 07:32:16 +0000 | [diff] [blame] | 64 | 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 Serebryany | 9514a53 | 2012-12-19 09:53:32 +0000 | [diff] [blame] | 70 | |
| 71 | const char *ShadowByte(u8 byte) { |
| 72 | switch (byte) { |
| 73 | case kAsanHeapLeftRedzoneMagic: |
| 74 | case kAsanHeapRightRedzoneMagic: |
Stephen Hines | 6d18623 | 2014-11-26 17:56:19 -0800 | [diff] [blame] | 75 | case kAsanArrayCookieMagic: |
Kostya Serebryany | 9514a53 | 2012-12-19 09:53:32 +0000 | [diff] [blame] | 76 | 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 Hines | 2d1fdb2 | 2014-05-28 23:58:16 -0700 | [diff] [blame] | 89 | case kAsanContiguousContainerOOBMagic: |
Stephen Hines | 86277eb | 2015-03-23 12:06:32 -0700 | [diff] [blame] | 90 | case kAsanAllocaLeftMagic: |
| 91 | case kAsanAllocaRightMagic: |
Kostya Serebryany | 9514a53 | 2012-12-19 09:53:32 +0000 | [diff] [blame] | 92 | return Blue(); |
| 93 | case kAsanStackUseAfterScopeMagic: |
| 94 | return Magenta(); |
| 95 | case kAsanGlobalRedzoneMagic: |
| 96 | return Red(); |
| 97 | case kAsanInternalHeapMagic: |
| 98 | return Yellow(); |
Stephen Hines | 6d18623 | 2014-11-26 17:56:19 -0800 | [diff] [blame] | 99 | case kAsanIntraObjectRedzone: |
| 100 | return Yellow(); |
Kostya Serebryany | 9514a53 | 2012-12-19 09:53:32 +0000 | [diff] [blame] | 101 | default: |
| 102 | return Default(); |
| 103 | } |
| 104 | } |
| 105 | const char *EndShadowByte() { return Default(); } |
Stephen Hines | 6d18623 | 2014-11-26 17:56:19 -0800 | [diff] [blame] | 106 | const char *MemoryByte() { return Magenta(); } |
| 107 | const char *EndMemoryByte() { return Default(); } |
Kostya Serebryany | 58f5455 | 2012-12-18 07:32:16 +0000 | [diff] [blame] | 108 | }; |
| 109 | |
Alexey Samsonov | 9873792 | 2012-08-10 15:13:05 +0000 | [diff] [blame] | 110 | // ---------------------- Helper functions ----------------------- {{{1 |
| 111 | |
Stephen Hines | 6d18623 | 2014-11-26 17:56:19 -0800 | [diff] [blame] | 112 | static void PrintMemoryByte(InternalScopedString *str, const char *before, |
| 113 | u8 byte, bool in_shadow, const char *after = "\n") { |
Kostya Serebryany | 9514a53 | 2012-12-19 09:53:32 +0000 | [diff] [blame] | 114 | Decorator d; |
Stephen Hines | 6d18623 | 2014-11-26 17:56:19 -0800 | [diff] [blame] | 115 | 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 | |
| 121 | static void PrintShadowByte(InternalScopedString *str, const char *before, |
| 122 | u8 byte, const char *after = "\n") { |
| 123 | PrintMemoryByte(str, before, byte, /*in_shadow*/true, after); |
Kostya Serebryany | 9514a53 | 2012-12-19 09:53:32 +0000 | [diff] [blame] | 124 | } |
| 125 | |
Stephen Hines | 2d1fdb2 | 2014-05-28 23:58:16 -0700 | [diff] [blame] | 126 | static void PrintShadowBytes(InternalScopedString *str, const char *before, |
| 127 | u8 *bytes, u8 *guilty, uptr n) { |
Kostya Serebryany | 9514a53 | 2012-12-19 09:53:32 +0000 | [diff] [blame] | 128 | Decorator d; |
Stephen Hines | 2d1fdb2 | 2014-05-28 23:58:16 -0700 | [diff] [blame] | 129 | if (before) str->append("%s%p:", before, bytes); |
Kostya Serebryany | 9514a53 | 2012-12-19 09:53:32 +0000 | [diff] [blame] | 130 | for (uptr i = 0; i < n; i++) { |
| 131 | u8 *p = bytes + i; |
Stephen Hines | 2d1fdb2 | 2014-05-28 23:58:16 -0700 | [diff] [blame] | 132 | const char *before = |
| 133 | p == guilty ? "[" : (p - 1 == guilty && i != 0) ? "" : " "; |
Kostya Serebryany | 9514a53 | 2012-12-19 09:53:32 +0000 | [diff] [blame] | 134 | const char *after = p == guilty ? "]" : ""; |
Stephen Hines | 2d1fdb2 | 2014-05-28 23:58:16 -0700 | [diff] [blame] | 135 | PrintShadowByte(str, before, *p, after); |
Alexey Samsonov | 9873792 | 2012-08-10 15:13:05 +0000 | [diff] [blame] | 136 | } |
Stephen Hines | 2d1fdb2 | 2014-05-28 23:58:16 -0700 | [diff] [blame] | 137 | str->append("\n"); |
Alexey Samsonov | 9873792 | 2012-08-10 15:13:05 +0000 | [diff] [blame] | 138 | } |
| 139 | |
Stephen Hines | 2d1fdb2 | 2014-05-28 23:58:16 -0700 | [diff] [blame] | 140 | static 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 Hines | 6d18623 | 2014-11-26 17:56:19 -0800 | [diff] [blame] | 173 | PrintShadowByte(str, " Array cookie: ", |
| 174 | kAsanArrayCookieMagic); |
| 175 | PrintShadowByte(str, " Intra object redzone: ", |
| 176 | kAsanIntraObjectRedzone); |
Stephen Hines | 2d1fdb2 | 2014-05-28 23:58:16 -0700 | [diff] [blame] | 177 | PrintShadowByte(str, " ASan internal: ", kAsanInternalHeapMagic); |
Stephen Hines | 86277eb | 2015-03-23 12:06:32 -0700 | [diff] [blame] | 178 | PrintShadowByte(str, " Left alloca redzone: ", kAsanAllocaLeftMagic); |
| 179 | PrintShadowByte(str, " Right alloca redzone: ", kAsanAllocaRightMagic); |
Alexey Samsonov | 9873792 | 2012-08-10 15:13:05 +0000 | [diff] [blame] | 180 | } |
| 181 | |
Stephen Hines | 6d18623 | 2014-11-26 17:56:19 -0800 | [diff] [blame] | 182 | void 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 Serebryany | 95f630a | 2013-01-28 07:34:22 +0000 | [diff] [blame] | 198 | static void PrintShadowMemoryForAddress(uptr addr) { |
Stephen Hines | 2d1fdb2 | 2014-05-28 23:58:16 -0700 | [diff] [blame] | 199 | if (!AddrIsInMem(addr)) return; |
Kostya Serebryany | 95f630a | 2013-01-28 07:34:22 +0000 | [diff] [blame] | 200 | 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 Hines | 2d1fdb2 | 2014-05-28 23:58:16 -0700 | [diff] [blame] | 203 | InternalScopedString str(4096 * 8); |
| 204 | str.append("Shadow bytes around the buggy address:\n"); |
Kostya Serebryany | 95f630a | 2013-01-28 07:34:22 +0000 | [diff] [blame] | 205 | for (int i = -5; i <= 5; i++) { |
| 206 | const char *prefix = (i == 0) ? "=>" : " "; |
Stephen Hines | 2d1fdb2 | 2014-05-28 23:58:16 -0700 | [diff] [blame] | 207 | PrintShadowBytes(&str, prefix, (u8 *)(aligned_shadow + i * n_bytes_per_row), |
| 208 | (u8 *)shadow_addr, n_bytes_per_row); |
Kostya Serebryany | 95f630a | 2013-01-28 07:34:22 +0000 | [diff] [blame] | 209 | } |
Stephen Hines | 2d1fdb2 | 2014-05-28 23:58:16 -0700 | [diff] [blame] | 210 | if (flags()->print_legend) PrintLegend(&str); |
| 211 | Printf("%s", str.data()); |
Kostya Serebryany | 95f630a | 2013-01-28 07:34:22 +0000 | [diff] [blame] | 212 | } |
| 213 | |
Alexey Samsonov | 9873792 | 2012-08-10 15:13:05 +0000 | [diff] [blame] | 214 | static void PrintZoneForPointer(uptr ptr, uptr zone_ptr, |
| 215 | const char *zone_name) { |
| 216 | if (zone_ptr) { |
| 217 | if (zone_name) { |
Kostya Serebryany | 283c296 | 2012-08-28 11:34:40 +0000 | [diff] [blame] | 218 | Printf("malloc_zone_from_ptr(%p) = %p, which is %s\n", |
Alexey Samsonov | 9873792 | 2012-08-10 15:13:05 +0000 | [diff] [blame] | 219 | ptr, zone_ptr, zone_name); |
| 220 | } else { |
Kostya Serebryany | 283c296 | 2012-08-28 11:34:40 +0000 | [diff] [blame] | 221 | Printf("malloc_zone_from_ptr(%p) = %p, which doesn't have a name\n", |
Alexey Samsonov | 9873792 | 2012-08-10 15:13:05 +0000 | [diff] [blame] | 222 | ptr, zone_ptr); |
| 223 | } |
| 224 | } else { |
Kostya Serebryany | 283c296 | 2012-08-28 11:34:40 +0000 | [diff] [blame] | 225 | Printf("malloc_zone_from_ptr(%p) = 0\n", ptr); |
Alexey Samsonov | 9873792 | 2012-08-10 15:13:05 +0000 | [diff] [blame] | 226 | } |
| 227 | } |
| 228 | |
Timur Iskhodzhanov | 997454a | 2013-09-10 08:36:21 +0000 | [diff] [blame] | 229 | static void DescribeThread(AsanThread *t) { |
| 230 | if (t) |
| 231 | DescribeThread(t->context()); |
| 232 | } |
| 233 | |
Alexey Samsonov | e218beb | 2012-08-09 09:06:52 +0000 | [diff] [blame] | 234 | // ---------------------- Address Descriptions ------------------- {{{1 |
| 235 | |
Alexey Samsonov | e4bfca2 | 2012-08-09 09:27:24 +0000 | [diff] [blame] | 236 | static bool IsASCII(unsigned char c) { |
Alexey Samsonov | 9873792 | 2012-08-10 15:13:05 +0000 | [diff] [blame] | 237 | return /*0x00 <= c &&*/ c <= 0x7F; |
Alexey Samsonov | e4bfca2 | 2012-08-09 09:27:24 +0000 | [diff] [blame] | 238 | } |
| 239 | |
Alexey Samsonov | c942427 | 2013-03-27 10:41:22 +0000 | [diff] [blame] | 240 | static 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 Hines | 2d1fdb2 | 2014-05-28 23:58:16 -0700 | [diff] [blame] | 243 | 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 Hines | 6d18623 | 2014-11-26 17:56:19 -0800 | [diff] [blame] | 249 | return should_demangle ? Symbolizer::GetOrInit()->Demangle(name) : name; |
Alexey Samsonov | c942427 | 2013-03-27 10:41:22 +0000 | [diff] [blame] | 250 | } |
| 251 | |
Alexey Samsonov | 939316c | 2013-04-01 08:57:38 +0000 | [diff] [blame] | 252 | // Check if the global is a zero-terminated ASCII string. If so, print it. |
Stephen Hines | 2d1fdb2 | 2014-05-28 23:58:16 -0700 | [diff] [blame] | 253 | static void PrintGlobalNameIfASCII(InternalScopedString *str, |
| 254 | const __asan_global &g) { |
Alexey Samsonov | 939316c | 2013-04-01 08:57:38 +0000 | [diff] [blame] | 255 | 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 Hines | 2d1fdb2 | 2014-05-28 23:58:16 -0700 | [diff] [blame] | 260 | str->append(" '%s' is ascii string '%s'\n", MaybeDemangleGlobalName(g.name), |
| 261 | (char *)g.beg); |
Alexey Samsonov | 939316c | 2013-04-01 08:57:38 +0000 | [diff] [blame] | 262 | } |
| 263 | |
Stephen Hines | 6a211c5 | 2014-07-21 00:49:56 -0700 | [diff] [blame] | 264 | static 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 | |
| 273 | static 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 Nainar | cdce50b | 2015-07-01 12:26:56 -0700 | [diff] [blame] | 284 | static void DescribeAddressRelativeToGlobal(uptr addr, uptr size, |
| 285 | const __asan_global &g) { |
Stephen Hines | 2d1fdb2 | 2014-05-28 23:58:16 -0700 | [diff] [blame] | 286 | InternalScopedString str(4096); |
Kostya Serebryany | 58f5455 | 2012-12-18 07:32:16 +0000 | [diff] [blame] | 287 | Decorator d; |
Stephen Hines | 2d1fdb2 | 2014-05-28 23:58:16 -0700 | [diff] [blame] | 288 | str.append("%s", d.Location()); |
Alexey Samsonov | e4bfca2 | 2012-08-09 09:27:24 +0000 | [diff] [blame] | 289 | if (addr < g.beg) { |
Stephen Hines | 2d1fdb2 | 2014-05-28 23:58:16 -0700 | [diff] [blame] | 290 | str.append("%p is located %zd bytes to the left", (void *)addr, |
| 291 | g.beg - addr); |
Evgeniy Stepanov | 589dcda | 2013-02-05 14:32:03 +0000 | [diff] [blame] | 292 | } else if (addr + size > g.beg + g.size) { |
| 293 | if (addr < g.beg + g.size) |
| 294 | addr = g.beg + g.size; |
Stephen Hines | 2d1fdb2 | 2014-05-28 23:58:16 -0700 | [diff] [blame] | 295 | str.append("%p is located %zd bytes to the right", (void *)addr, |
| 296 | addr - (g.beg + g.size)); |
Alexey Samsonov | e4bfca2 | 2012-08-09 09:27:24 +0000 | [diff] [blame] | 297 | } else { |
Evgeniy Stepanov | 589dcda | 2013-02-05 14:32:03 +0000 | [diff] [blame] | 298 | // Can it happen? |
Stephen Hines | 2d1fdb2 | 2014-05-28 23:58:16 -0700 | [diff] [blame] | 299 | str.append("%p is located %zd bytes inside", (void *)addr, addr - g.beg); |
Alexey Samsonov | e4bfca2 | 2012-08-09 09:27:24 +0000 | [diff] [blame] | 300 | } |
Stephen Hines | 6a211c5 | 2014-07-21 00:49:56 -0700 | [diff] [blame] | 301 | 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 Hines | 2d1fdb2 | 2014-05-28 23:58:16 -0700 | [diff] [blame] | 305 | str.append("%s", d.EndLocation()); |
| 306 | PrintGlobalNameIfASCII(&str, g); |
| 307 | Printf("%s", str.data()); |
Pirama Arumuga Nainar | cdce50b | 2015-07-01 12:26:56 -0700 | [diff] [blame] | 308 | } |
| 309 | |
| 310 | static 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 Samsonov | e4bfca2 | 2012-08-09 09:27:24 +0000 | [diff] [blame] | 328 | return true; |
| 329 | } |
| 330 | |
Stephen Hines | 6d18623 | 2014-11-26 17:56:19 -0800 | [diff] [blame] | 331 | bool DescribeAddressIfShadow(uptr addr, AddressDescription *descr, bool print) { |
Alexey Samsonov | e218beb | 2012-08-09 09:06:52 +0000 | [diff] [blame] | 332 | if (AddrIsInMem(addr)) |
| 333 | return false; |
Stephen Hines | 6d18623 | 2014-11-26 17:56:19 -0800 | [diff] [blame] | 334 | 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 Samsonov | e218beb | 2012-08-09 09:06:52 +0000 | [diff] [blame] | 345 | return true; |
| 346 | } |
| 347 | CHECK(0 && "Address is not in memory and not in shadow?"); |
| 348 | return false; |
| 349 | } |
| 350 | |
Kostya Serebryany | 50f3daa | 2013-03-22 10:36:24 +0000 | [diff] [blame] | 351 | // Return " (thread_name) " or an empty string if the name is empty. |
| 352 | const 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 | |
| 363 | const 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 Hines | 6d18623 | 2014-11-26 17:56:19 -0800 | [diff] [blame] | 371 | static 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 Serebryany | edb39c7 | 2013-09-03 13:58:04 +0000 | [diff] [blame] | 375 | uptr addr_end = addr + access_size; |
| 376 | const char *pos_descr = 0; |
Stephen Hines | 6d18623 | 2014-11-26 17:56:19 -0800 | [diff] [blame] | 377 | // If the variable [var.beg, var_end) is the nearest variable to the |
Kostya Serebryany | edb39c7 | 2013-09-03 13:58:04 +0000 | [diff] [blame] | 378 | // current memory access, indicate it in the log. |
Stephen Hines | 6d18623 | 2014-11-26 17:56:19 -0800 | [diff] [blame] | 379 | if (addr >= var.beg) { |
Kostya Serebryany | edb39c7 | 2013-09-03 13:58:04 +0000 | [diff] [blame] | 380 | 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 Hines | 6d18623 | 2014-11-26 17:56:19 -0800 | [diff] [blame] | 388 | if (addr_end > var.beg) |
Kostya Serebryany | edb39c7 | 2013-09-03 13:58:04 +0000 | [diff] [blame] | 389 | pos_descr = "partially underflows"; |
| 390 | else if (addr >= prev_var_end && |
Stephen Hines | 6d18623 | 2014-11-26 17:56:19 -0800 | [diff] [blame] | 391 | addr - prev_var_end >= var.beg - addr_end) |
Kostya Serebryany | edb39c7 | 2013-09-03 13:58:04 +0000 | [diff] [blame] | 392 | pos_descr = "underflows"; |
| 393 | } |
Stephen Hines | 2d1fdb2 | 2014-05-28 23:58:16 -0700 | [diff] [blame] | 394 | InternalScopedString str(1024); |
Stephen Hines | 6d18623 | 2014-11-26 17:56:19 -0800 | [diff] [blame] | 395 | 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 Serebryany | edb39c7 | 2013-09-03 13:58:04 +0000 | [diff] [blame] | 402 | 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 Hines | 2d1fdb2 | 2014-05-28 23:58:16 -0700 | [diff] [blame] | 406 | str.append("%s <== Memory access at offset %zd %s this variable%s\n", |
| 407 | d.Location(), addr, pos_descr, d.EndLocation()); |
Kostya Serebryany | edb39c7 | 2013-09-03 13:58:04 +0000 | [diff] [blame] | 408 | } else { |
Stephen Hines | 2d1fdb2 | 2014-05-28 23:58:16 -0700 | [diff] [blame] | 409 | str.append("\n"); |
Kostya Serebryany | edb39c7 | 2013-09-03 13:58:04 +0000 | [diff] [blame] | 410 | } |
Stephen Hines | 2d1fdb2 | 2014-05-28 23:58:16 -0700 | [diff] [blame] | 411 | Printf("%s", str.data()); |
Kostya Serebryany | edb39c7 | 2013-09-03 13:58:04 +0000 | [diff] [blame] | 412 | } |
| 413 | |
Stephen Hines | 6d18623 | 2014-11-26 17:56:19 -0800 | [diff] [blame] | 414 | bool 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 Serebryany | edb39c7 | 2013-09-03 13:58:04 +0000 | [diff] [blame] | 440 | |
Alexey Samsonov | e218beb | 2012-08-09 09:06:52 +0000 | [diff] [blame] | 441 | bool DescribeAddressIfStack(uptr addr, uptr access_size) { |
Alexey Samsonov | def1be9 | 2013-03-21 11:23:41 +0000 | [diff] [blame] | 442 | AsanThread *t = FindThreadByStackAddress(addr); |
Alexey Samsonov | e218beb | 2012-08-09 09:06:52 +0000 | [diff] [blame] | 443 | if (!t) return false; |
Kostya Serebryany | d570bb4 | 2013-05-22 14:21:34 +0000 | [diff] [blame] | 444 | |
Kostya Serebryany | 58f5455 | 2012-12-18 07:32:16 +0000 | [diff] [blame] | 445 | Decorator d; |
Stephen Hines | 6d18623 | 2014-11-26 17:56:19 -0800 | [diff] [blame] | 446 | char tname[128]; |
Kostya Serebryany | 58f5455 | 2012-12-18 07:32:16 +0000 | [diff] [blame] | 447 | Printf("%s", d.Location()); |
Stephen Hines | 6d18623 | 2014-11-26 17:56:19 -0800 | [diff] [blame] | 448 | 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 Serebryany | 50f3daa | 2013-03-22 10:36:24 +0000 | [diff] [blame] | 459 | // 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 Hines | 6d18623 | 2014-11-26 17:56:19 -0800 | [diff] [blame] | 466 | #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 Serebryany | 58f5455 | 2012-12-18 07:32:16 +0000 | [diff] [blame] | 473 | Printf("%s", d.EndLocation()); |
Stephen Hines | 6d18623 | 2014-11-26 17:56:19 -0800 | [diff] [blame] | 474 | StackTrace alloca_stack(&access.frame_pc, 1); |
Stephen Hines | 2d1fdb2 | 2014-05-28 23:58:16 -0700 | [diff] [blame] | 475 | alloca_stack.Print(); |
Stephen Hines | 6d18623 | 2014-11-26 17:56:19 -0800 | [diff] [blame] | 476 | |
| 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 Samsonov | e218beb | 2012-08-09 09:06:52 +0000 | [diff] [blame] | 485 | // Report the number of stack objects. |
Kostya Serebryany | 283c296 | 2012-08-28 11:34:40 +0000 | [diff] [blame] | 486 | Printf(" This frame has %zu object(s):\n", n_objects); |
Kostya Serebryany | edb39c7 | 2013-09-03 13:58:04 +0000 | [diff] [blame] | 487 | |
Alexey Samsonov | e218beb | 2012-08-09 09:06:52 +0000 | [diff] [blame] | 488 | // Report all objects in this frame. |
Kostya Serebryany | 89fe564 | 2013-09-03 14:53:02 +0000 | [diff] [blame] | 489 | for (uptr i = 0; i < n_objects; i++) { |
Kostya Serebryany | edb39c7 | 2013-09-03 13:58:04 +0000 | [diff] [blame] | 490 | uptr prev_var_end = i ? vars[i - 1].beg + vars[i - 1].size : 0; |
Timur Iskhodzhanov | 22917e9 | 2013-09-03 15:09:21 +0000 | [diff] [blame] | 491 | uptr next_var_beg = i + 1 < n_objects ? vars[i + 1].beg : ~(0UL); |
Stephen Hines | 6d18623 | 2014-11-26 17:56:19 -0800 | [diff] [blame] | 492 | PrintAccessAndVarIntersection(vars[i], access.offset, access_size, |
Kostya Serebryany | edb39c7 | 2013-09-03 13:58:04 +0000 | [diff] [blame] | 493 | prev_var_end, next_var_beg); |
Alexey Samsonov | e218beb | 2012-08-09 09:06:52 +0000 | [diff] [blame] | 494 | } |
Kostya Serebryany | 283c296 | 2012-08-28 11:34:40 +0000 | [diff] [blame] | 495 | Printf("HINT: this may be a false positive if your program uses " |
Stephen Hines | 6d18623 | 2014-11-26 17:56:19 -0800 | [diff] [blame] | 496 | "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 Iskhodzhanov | 997454a | 2013-09-10 08:36:21 +0000 | [diff] [blame] | 502 | DescribeThread(t); |
Alexey Samsonov | e218beb | 2012-08-09 09:06:52 +0000 | [diff] [blame] | 503 | return true; |
| 504 | } |
| 505 | |
Alexey Samsonov | 5c153fa | 2012-09-18 07:38:10 +0000 | [diff] [blame] | 506 | static void DescribeAccessToHeapChunk(AsanChunkView chunk, uptr addr, |
| 507 | uptr access_size) { |
Evgeniy Stepanov | 589dcda | 2013-02-05 14:32:03 +0000 | [diff] [blame] | 508 | sptr offset; |
Kostya Serebryany | 58f5455 | 2012-12-18 07:32:16 +0000 | [diff] [blame] | 509 | Decorator d; |
Stephen Hines | 2d1fdb2 | 2014-05-28 23:58:16 -0700 | [diff] [blame] | 510 | InternalScopedString str(4096); |
| 511 | str.append("%s", d.Location()); |
Evgeniy Stepanov | 589dcda | 2013-02-05 14:32:03 +0000 | [diff] [blame] | 512 | if (chunk.AddrIsAtLeft(addr, access_size, &offset)) { |
Stephen Hines | 2d1fdb2 | 2014-05-28 23:58:16 -0700 | [diff] [blame] | 513 | str.append("%p is located %zd bytes to the left of", (void *)addr, offset); |
Alexey Samsonov | 5c153fa | 2012-09-18 07:38:10 +0000 | [diff] [blame] | 514 | } else if (chunk.AddrIsAtRight(addr, access_size, &offset)) { |
Evgeniy Stepanov | 589dcda | 2013-02-05 14:32:03 +0000 | [diff] [blame] | 515 | if (offset < 0) { |
| 516 | addr -= offset; |
| 517 | offset = 0; |
| 518 | } |
Stephen Hines | 2d1fdb2 | 2014-05-28 23:58:16 -0700 | [diff] [blame] | 519 | str.append("%p is located %zd bytes to the right of", (void *)addr, offset); |
Evgeniy Stepanov | 589dcda | 2013-02-05 14:32:03 +0000 | [diff] [blame] | 520 | } else if (chunk.AddrIsInside(addr, access_size, &offset)) { |
Stephen Hines | 2d1fdb2 | 2014-05-28 23:58:16 -0700 | [diff] [blame] | 521 | str.append("%p is located %zd bytes inside of", (void*)addr, offset); |
Alexey Samsonov | 5c153fa | 2012-09-18 07:38:10 +0000 | [diff] [blame] | 522 | } else { |
Stephen Hines | 2d1fdb2 | 2014-05-28 23:58:16 -0700 | [diff] [blame] | 523 | str.append("%p is located somewhere around (this is AddressSanitizer bug!)", |
| 524 | (void *)addr); |
Alexey Samsonov | 5c153fa | 2012-09-18 07:38:10 +0000 | [diff] [blame] | 525 | } |
Stephen Hines | 2d1fdb2 | 2014-05-28 23:58:16 -0700 | [diff] [blame] | 526 | 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 Samsonov | 5c153fa | 2012-09-18 07:38:10 +0000 | [diff] [blame] | 530 | } |
| 531 | |
| 532 | void DescribeHeapAddress(uptr addr, uptr access_size) { |
| 533 | AsanChunkView chunk = FindHeapChunkByAddress(addr); |
Alexey Samsonov | d9def29 | 2013-10-14 11:13:54 +0000 | [diff] [blame] | 534 | if (!chunk.IsValid()) { |
| 535 | Printf("AddressSanitizer can not describe address in more detail " |
| 536 | "(wild memory access suspected).\n"); |
| 537 | return; |
| 538 | } |
Alexey Samsonov | 5c153fa | 2012-09-18 07:38:10 +0000 | [diff] [blame] | 539 | DescribeAccessToHeapChunk(chunk, addr, access_size); |
| 540 | CHECK(chunk.AllocTid() != kInvalidTid); |
Alexey Samsonov | def1be9 | 2013-03-21 11:23:41 +0000 | [diff] [blame] | 541 | asanThreadRegistry().CheckLocked(); |
| 542 | AsanThreadContext *alloc_thread = |
| 543 | GetThreadContextByTidLocked(chunk.AllocTid()); |
Stephen Hines | 6d18623 | 2014-11-26 17:56:19 -0800 | [diff] [blame] | 544 | StackTrace alloc_stack = chunk.GetAllocStack(); |
Kostya Serebryany | 716e2f2 | 2012-12-07 15:15:01 +0000 | [diff] [blame] | 545 | char tname[128]; |
Kostya Serebryany | 58f5455 | 2012-12-18 07:32:16 +0000 | [diff] [blame] | 546 | Decorator d; |
Timur Iskhodzhanov | 997454a | 2013-09-10 08:36:21 +0000 | [diff] [blame] | 547 | AsanThreadContext *free_thread = 0; |
Alexey Samsonov | 5c153fa | 2012-09-18 07:38:10 +0000 | [diff] [blame] | 548 | if (chunk.FreeTid() != kInvalidTid) { |
Timur Iskhodzhanov | 997454a | 2013-09-10 08:36:21 +0000 | [diff] [blame] | 549 | free_thread = GetThreadContextByTidLocked(chunk.FreeTid()); |
Kostya Serebryany | 58f5455 | 2012-12-18 07:32:16 +0000 | [diff] [blame] | 550 | Printf("%sfreed by thread T%d%s here:%s\n", d.Allocation(), |
Alexey Samsonov | def1be9 | 2013-03-21 11:23:41 +0000 | [diff] [blame] | 551 | free_thread->tid, |
Kostya Serebryany | 58f5455 | 2012-12-18 07:32:16 +0000 | [diff] [blame] | 552 | ThreadNameWithParenthesis(free_thread, tname, sizeof(tname)), |
| 553 | d.EndAllocation()); |
Stephen Hines | 6d18623 | 2014-11-26 17:56:19 -0800 | [diff] [blame] | 554 | StackTrace free_stack = chunk.GetFreeStack(); |
Stephen Hines | 2d1fdb2 | 2014-05-28 23:58:16 -0700 | [diff] [blame] | 555 | free_stack.Print(); |
Kostya Serebryany | 58f5455 | 2012-12-18 07:32:16 +0000 | [diff] [blame] | 556 | Printf("%spreviously allocated by thread T%d%s here:%s\n", |
Alexey Samsonov | def1be9 | 2013-03-21 11:23:41 +0000 | [diff] [blame] | 557 | d.Allocation(), alloc_thread->tid, |
Kostya Serebryany | 58f5455 | 2012-12-18 07:32:16 +0000 | [diff] [blame] | 558 | ThreadNameWithParenthesis(alloc_thread, tname, sizeof(tname)), |
| 559 | d.EndAllocation()); |
Alexey Samsonov | 5c153fa | 2012-09-18 07:38:10 +0000 | [diff] [blame] | 560 | } else { |
Kostya Serebryany | 58f5455 | 2012-12-18 07:32:16 +0000 | [diff] [blame] | 561 | Printf("%sallocated by thread T%d%s here:%s\n", d.Allocation(), |
Alexey Samsonov | def1be9 | 2013-03-21 11:23:41 +0000 | [diff] [blame] | 562 | alloc_thread->tid, |
Kostya Serebryany | 58f5455 | 2012-12-18 07:32:16 +0000 | [diff] [blame] | 563 | ThreadNameWithParenthesis(alloc_thread, tname, sizeof(tname)), |
| 564 | d.EndAllocation()); |
Alexey Samsonov | 5c153fa | 2012-09-18 07:38:10 +0000 | [diff] [blame] | 565 | } |
Stephen Hines | 2d1fdb2 | 2014-05-28 23:58:16 -0700 | [diff] [blame] | 566 | alloc_stack.Print(); |
Timur Iskhodzhanov | 997454a | 2013-09-10 08:36:21 +0000 | [diff] [blame] | 567 | DescribeThread(GetCurrentThread()); |
| 568 | if (free_thread) |
| 569 | DescribeThread(free_thread); |
| 570 | DescribeThread(alloc_thread); |
Alexey Samsonov | 5c153fa | 2012-09-18 07:38:10 +0000 | [diff] [blame] | 571 | } |
| 572 | |
Pirama Arumuga Nainar | cdce50b | 2015-07-01 12:26:56 -0700 | [diff] [blame] | 573 | static void DescribeAddress(uptr addr, uptr access_size, const char *bug_type) { |
Alexey Samsonov | e218beb | 2012-08-09 09:06:52 +0000 | [diff] [blame] | 574 | // Check if this is shadow or shadow gap. |
| 575 | if (DescribeAddressIfShadow(addr)) |
| 576 | return; |
| 577 | CHECK(AddrIsInMem(addr)); |
Pirama Arumuga Nainar | cdce50b | 2015-07-01 12:26:56 -0700 | [diff] [blame] | 578 | if (DescribeAddressIfGlobal(addr, access_size, bug_type)) |
Alexey Samsonov | e218beb | 2012-08-09 09:06:52 +0000 | [diff] [blame] | 579 | return; |
| 580 | if (DescribeAddressIfStack(addr, access_size)) |
| 581 | return; |
| 582 | // Assume it is a heap address. |
| 583 | DescribeHeapAddress(addr, access_size); |
| 584 | } |
| 585 | |
Alexey Samsonov | 71b42c9 | 2012-09-05 07:37:15 +0000 | [diff] [blame] | 586 | // ------------------- Thread description -------------------- {{{1 |
| 587 | |
Alexey Samsonov | def1be9 | 2013-03-21 11:23:41 +0000 | [diff] [blame] | 588 | void DescribeThread(AsanThreadContext *context) { |
| 589 | CHECK(context); |
| 590 | asanThreadRegistry().CheckLocked(); |
Alexey Samsonov | 71b42c9 | 2012-09-05 07:37:15 +0000 | [diff] [blame] | 591 | // No need to announce the main thread. |
Alexey Samsonov | def1be9 | 2013-03-21 11:23:41 +0000 | [diff] [blame] | 592 | if (context->tid == 0 || context->announced) { |
Alexey Samsonov | 71b42c9 | 2012-09-05 07:37:15 +0000 | [diff] [blame] | 593 | return; |
| 594 | } |
Alexey Samsonov | def1be9 | 2013-03-21 11:23:41 +0000 | [diff] [blame] | 595 | context->announced = true; |
Kostya Serebryany | 716e2f2 | 2012-12-07 15:15:01 +0000 | [diff] [blame] | 596 | char tname[128]; |
Stephen Hines | 2d1fdb2 | 2014-05-28 23:58:16 -0700 | [diff] [blame] | 597 | InternalScopedString str(1024); |
| 598 | str.append("Thread T%d%s", context->tid, |
| 599 | ThreadNameWithParenthesis(context->tid, tname, sizeof(tname))); |
Pirama Arumuga Nainar | 259f706 | 2015-05-06 11:49:53 -0700 | [diff] [blame] | 600 | if (context->parent_tid == kInvalidTid) { |
| 601 | str.append(" created by unknown thread\n"); |
| 602 | Printf("%s", str.data()); |
| 603 | return; |
| 604 | } |
Stephen Hines | 2d1fdb2 | 2014-05-28 23:58:16 -0700 | [diff] [blame] | 605 | 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 Hines | 6d18623 | 2014-11-26 17:56:19 -0800 | [diff] [blame] | 609 | StackDepotGet(context->stack_id).Print(); |
Alexey Samsonov | 71b42c9 | 2012-09-05 07:37:15 +0000 | [diff] [blame] | 610 | // Recursively described parent thread if needed. |
| 611 | if (flags()->print_full_thread_history) { |
Alexey Samsonov | def1be9 | 2013-03-21 11:23:41 +0000 | [diff] [blame] | 612 | AsanThreadContext *parent_context = |
| 613 | GetThreadContextByTidLocked(context->parent_tid); |
| 614 | DescribeThread(parent_context); |
Alexey Samsonov | 71b42c9 | 2012-09-05 07:37:15 +0000 | [diff] [blame] | 615 | } |
| 616 | } |
| 617 | |
Alexey Samsonov | e218beb | 2012-08-09 09:06:52 +0000 | [diff] [blame] | 618 | // -------------------- Different kinds of reports ----------------- {{{1 |
| 619 | |
Alexey Samsonov | 9873792 | 2012-08-10 15:13:05 +0000 | [diff] [blame] | 620 | // Use ScopedInErrorReport to run common actions just before and |
| 621 | // immediately after printing error report. |
| 622 | class ScopedInErrorReport { |
| 623 | public: |
Stephen Hines | 6d18623 | 2014-11-26 17:56:19 -0800 | [diff] [blame] | 624 | explicit ScopedInErrorReport(ReportData *report = nullptr) { |
Alexey Samsonov | 9873792 | 2012-08-10 15:13:05 +0000 | [diff] [blame] | 625 | static atomic_uint32_t num_calls; |
Alexey Samsonov | 62e2709 | 2012-09-17 08:02:19 +0000 | [diff] [blame] | 626 | static u32 reporting_thread_tid; |
Alexey Samsonov | 9873792 | 2012-08-10 15:13:05 +0000 | [diff] [blame] | 627 | 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 Hines | 6d18623 | 2014-11-26 17:56:19 -0800 | [diff] [blame] | 631 | Report("AddressSanitizer: while reporting a bug found another one. " |
Alexey Samsonov | 9873792 | 2012-08-10 15:13:05 +0000 | [diff] [blame] | 632 | "Ignoring.\n"); |
Alexey Samsonov | 89c1384 | 2013-03-20 09:23:28 +0000 | [diff] [blame] | 633 | u32 current_tid = GetCurrentTidOrInvalid(); |
Alexey Samsonov | 62e2709 | 2012-09-17 08:02:19 +0000 | [diff] [blame] | 634 | 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 Samsonov | f882247 | 2013-02-20 13:54:32 +0000 | [diff] [blame] | 640 | // If we're still not dead for some reason, use raw _exit() instead of |
Alexey Samsonov | 031633b | 2012-11-19 11:22:22 +0000 | [diff] [blame] | 641 | // Die() to bypass any additional checks. |
Alexey Samsonov | f882247 | 2013-02-20 13:54:32 +0000 | [diff] [blame] | 642 | internal__exit(flags()->exitcode); |
Alexey Samsonov | 9873792 | 2012-08-10 15:13:05 +0000 | [diff] [blame] | 643 | } |
Stephen Hines | 6d18623 | 2014-11-26 17:56:19 -0800 | [diff] [blame] | 644 | if (report) report_data = *report; |
| 645 | report_happened = true; |
Alexey Samsonov | 6a08d29 | 2012-12-07 22:01:28 +0000 | [diff] [blame] | 646 | ASAN_ON_ERROR(); |
Alexey Samsonov | 7ed46ff | 2013-04-05 07:30:29 +0000 | [diff] [blame] | 647 | // 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 Samsonov | def1be9 | 2013-03-21 11:23:41 +0000 | [diff] [blame] | 650 | // recursive reports. |
| 651 | asanThreadRegistry().Lock(); |
Alexey Samsonov | 7ed46ff | 2013-04-05 07:30:29 +0000 | [diff] [blame] | 652 | CommonSanitizerReportMutex.Lock(); |
Alexey Samsonov | 89c1384 | 2013-03-20 09:23:28 +0000 | [diff] [blame] | 653 | reporting_thread_tid = GetCurrentTidOrInvalid(); |
Kostya Serebryany | 283c296 | 2012-08-28 11:34:40 +0000 | [diff] [blame] | 654 | Printf("====================================================" |
Alexey Samsonov | 62e2709 | 2012-09-17 08:02:19 +0000 | [diff] [blame] | 655 | "=============\n"); |
Alexey Samsonov | 9873792 | 2012-08-10 15:13:05 +0000 | [diff] [blame] | 656 | } |
| 657 | // Destructor is NORETURN, as functions that report errors are. |
| 658 | NORETURN ~ScopedInErrorReport() { |
| 659 | // Make sure the current thread is announced. |
Timur Iskhodzhanov | 997454a | 2013-09-10 08:36:21 +0000 | [diff] [blame] | 660 | DescribeThread(GetCurrentThread()); |
Stephen Hines | 2d1fdb2 | 2014-05-28 23:58:16 -0700 | [diff] [blame] | 661 | // We may want to grab this lock again when printing stats. |
| 662 | asanThreadRegistry().Unlock(); |
Alexey Samsonov | 9873792 | 2012-08-10 15:13:05 +0000 | [diff] [blame] | 663 | // Print memory stats. |
Kostya Serebryany | 95f630a | 2013-01-28 07:34:22 +0000 | [diff] [blame] | 664 | if (flags()->print_stats) |
| 665 | __asan_print_accumulated_stats(); |
Alexey Samsonov | 9873792 | 2012-08-10 15:13:05 +0000 | [diff] [blame] | 666 | if (error_report_callback) { |
| 667 | error_report_callback(error_message_buffer); |
| 668 | } |
Kostya Serebryany | 283c296 | 2012-08-28 11:34:40 +0000 | [diff] [blame] | 669 | Report("ABORTING\n"); |
Alexey Samsonov | 9873792 | 2012-08-10 15:13:05 +0000 | [diff] [blame] | 670 | Die(); |
| 671 | } |
| 672 | }; |
| 673 | |
Stephen Hines | 86277eb | 2015-03-23 12:06:32 -0700 | [diff] [blame] | 674 | void ReportStackOverflow(const SignalContext &sig) { |
Alexey Samsonov | 9873792 | 2012-08-10 15:13:05 +0000 | [diff] [blame] | 675 | ScopedInErrorReport in_report; |
Kostya Serebryany | 58f5455 | 2012-12-18 07:32:16 +0000 | [diff] [blame] | 676 | Decorator d; |
| 677 | Printf("%s", d.Warning()); |
Stephen Hines | 2d1fdb2 | 2014-05-28 23:58:16 -0700 | [diff] [blame] | 678 | Report( |
| 679 | "ERROR: AddressSanitizer: stack-overflow on address %p" |
Stephen Hines | 6d18623 | 2014-11-26 17:56:19 -0800 | [diff] [blame] | 680 | " (pc %p bp %p sp %p T%d)\n", |
Stephen Hines | 86277eb | 2015-03-23 12:06:32 -0700 | [diff] [blame] | 681 | (void *)sig.addr, (void *)sig.pc, (void *)sig.bp, (void *)sig.sp, |
Stephen Hines | 2d1fdb2 | 2014-05-28 23:58:16 -0700 | [diff] [blame] | 682 | GetCurrentTidOrInvalid()); |
Kostya Serebryany | 58f5455 | 2012-12-18 07:32:16 +0000 | [diff] [blame] | 683 | Printf("%s", d.EndWarning()); |
Stephen Hines | 86277eb | 2015-03-23 12:06:32 -0700 | [diff] [blame] | 684 | GET_STACK_TRACE_SIGNAL(sig); |
Stephen Hines | 2d1fdb2 | 2014-05-28 23:58:16 -0700 | [diff] [blame] | 685 | stack.Print(); |
| 686 | ReportErrorSummary("stack-overflow", &stack); |
| 687 | } |
| 688 | |
Stephen Hines | 86277eb | 2015-03-23 12:06:32 -0700 | [diff] [blame] | 689 | void ReportSIGSEGV(const char *description, const SignalContext &sig) { |
Stephen Hines | 2d1fdb2 | 2014-05-28 23:58:16 -0700 | [diff] [blame] | 690 | ScopedInErrorReport in_report; |
| 691 | Decorator d; |
| 692 | Printf("%s", d.Warning()); |
| 693 | Report( |
Stephen Hines | 6d18623 | 2014-11-26 17:56:19 -0800 | [diff] [blame] | 694 | "ERROR: AddressSanitizer: %s on unknown address %p" |
| 695 | " (pc %p bp %p sp %p T%d)\n", |
Stephen Hines | 86277eb | 2015-03-23 12:06:32 -0700 | [diff] [blame] | 696 | description, (void *)sig.addr, (void *)sig.pc, (void *)sig.bp, |
| 697 | (void *)sig.sp, GetCurrentTidOrInvalid()); |
| 698 | if (sig.pc < GetPageSizeCached()) { |
Stephen Hines | 6d18623 | 2014-11-26 17:56:19 -0800 | [diff] [blame] | 699 | Report("Hint: pc points to the zero page.\n"); |
| 700 | } |
Stephen Hines | 2d1fdb2 | 2014-05-28 23:58:16 -0700 | [diff] [blame] | 701 | Printf("%s", d.EndWarning()); |
Stephen Hines | 86277eb | 2015-03-23 12:06:32 -0700 | [diff] [blame] | 702 | GET_STACK_TRACE_SIGNAL(sig); |
Stephen Hines | 2d1fdb2 | 2014-05-28 23:58:16 -0700 | [diff] [blame] | 703 | stack.Print(); |
Stephen Hines | 86277eb | 2015-03-23 12:06:32 -0700 | [diff] [blame] | 704 | MaybeDumpInstructionBytes(sig.pc); |
Alexey Samsonov | d9def29 | 2013-10-14 11:13:54 +0000 | [diff] [blame] | 705 | Printf("AddressSanitizer can not provide additional info.\n"); |
Alexey Samsonov | 2fb0872 | 2013-11-01 17:02:14 +0000 | [diff] [blame] | 706 | ReportErrorSummary("SEGV", &stack); |
Alexey Samsonov | 7354509 | 2012-08-09 07:40:58 +0000 | [diff] [blame] | 707 | } |
| 708 | |
Stephen Hines | 6d18623 | 2014-11-26 17:56:19 -0800 | [diff] [blame] | 709 | void ReportDoubleFree(uptr addr, BufferedStackTrace *free_stack) { |
Alexey Samsonov | 9873792 | 2012-08-10 15:13:05 +0000 | [diff] [blame] | 710 | ScopedInErrorReport in_report; |
Kostya Serebryany | 58f5455 | 2012-12-18 07:32:16 +0000 | [diff] [blame] | 711 | Decorator d; |
| 712 | Printf("%s", d.Warning()); |
Kostya Serebryany | a89a35a | 2013-03-26 08:01:37 +0000 | [diff] [blame] | 713 | 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 Serebryany | 58f5455 | 2012-12-18 07:32:16 +0000 | [diff] [blame] | 719 | Printf("%s", d.EndWarning()); |
Alexey Samsonov | 1b17f5b | 2013-11-13 14:46:58 +0000 | [diff] [blame] | 720 | CHECK_GT(free_stack->size, 0); |
| 721 | GET_STACK_TRACE_FATAL(free_stack->trace[0], free_stack->top_frame_bp); |
Stephen Hines | 2d1fdb2 | 2014-05-28 23:58:16 -0700 | [diff] [blame] | 722 | stack.Print(); |
Alexey Samsonov | f7c1d18 | 2012-08-09 08:15:46 +0000 | [diff] [blame] | 723 | DescribeHeapAddress(addr, 1); |
Alexey Samsonov | 1b17f5b | 2013-11-13 14:46:58 +0000 | [diff] [blame] | 724 | ReportErrorSummary("double-free", &stack); |
Alexey Samsonov | f7c1d18 | 2012-08-09 08:15:46 +0000 | [diff] [blame] | 725 | } |
| 726 | |
Stephen Hines | 6d18623 | 2014-11-26 17:56:19 -0800 | [diff] [blame] | 727 | void 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 | |
| 751 | void ReportFreeNotMalloced(uptr addr, BufferedStackTrace *free_stack) { |
Alexey Samsonov | 9873792 | 2012-08-10 15:13:05 +0000 | [diff] [blame] | 752 | ScopedInErrorReport in_report; |
Kostya Serebryany | 58f5455 | 2012-12-18 07:32:16 +0000 | [diff] [blame] | 753 | Decorator d; |
| 754 | Printf("%s", d.Warning()); |
Kostya Serebryany | a89a35a | 2013-03-26 08:01:37 +0000 | [diff] [blame] | 755 | char tname[128]; |
| 756 | u32 curr_tid = GetCurrentTidOrInvalid(); |
Kostya Serebryany | 69d8ede | 2012-10-15 13:04:58 +0000 | [diff] [blame] | 757 | Report("ERROR: AddressSanitizer: attempting free on address " |
Kostya Serebryany | a89a35a | 2013-03-26 08:01:37 +0000 | [diff] [blame] | 758 | "which was not malloc()-ed: %p in thread T%d%s\n", addr, |
| 759 | curr_tid, ThreadNameWithParenthesis(curr_tid, tname, sizeof(tname))); |
Kostya Serebryany | 58f5455 | 2012-12-18 07:32:16 +0000 | [diff] [blame] | 760 | Printf("%s", d.EndWarning()); |
Alexey Samsonov | 1b17f5b | 2013-11-13 14:46:58 +0000 | [diff] [blame] | 761 | CHECK_GT(free_stack->size, 0); |
| 762 | GET_STACK_TRACE_FATAL(free_stack->trace[0], free_stack->top_frame_bp); |
Stephen Hines | 2d1fdb2 | 2014-05-28 23:58:16 -0700 | [diff] [blame] | 763 | stack.Print(); |
Alexey Samsonov | 9873792 | 2012-08-10 15:13:05 +0000 | [diff] [blame] | 764 | DescribeHeapAddress(addr, 1); |
Alexey Samsonov | 1b17f5b | 2013-11-13 14:46:58 +0000 | [diff] [blame] | 765 | ReportErrorSummary("bad-free", &stack); |
Alexey Samsonov | f7c1d18 | 2012-08-09 08:15:46 +0000 | [diff] [blame] | 766 | } |
| 767 | |
Stephen Hines | 6d18623 | 2014-11-26 17:56:19 -0800 | [diff] [blame] | 768 | void ReportAllocTypeMismatch(uptr addr, BufferedStackTrace *free_stack, |
Kostya Serebryany | fe6d916 | 2012-12-21 08:53:59 +0000 | [diff] [blame] | 769 | 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 Samsonov | 1b17f5b | 2013-11-13 14:46:58 +0000 | [diff] [blame] | 782 | CHECK_GT(free_stack->size, 0); |
| 783 | GET_STACK_TRACE_FATAL(free_stack->trace[0], free_stack->top_frame_bp); |
Stephen Hines | 2d1fdb2 | 2014-05-28 23:58:16 -0700 | [diff] [blame] | 784 | stack.Print(); |
Kostya Serebryany | fe6d916 | 2012-12-21 08:53:59 +0000 | [diff] [blame] | 785 | DescribeHeapAddress(addr, 1); |
Alexey Samsonov | 1b17f5b | 2013-11-13 14:46:58 +0000 | [diff] [blame] | 786 | ReportErrorSummary("alloc-dealloc-mismatch", &stack); |
Kostya Serebryany | fe6d916 | 2012-12-21 08:53:59 +0000 | [diff] [blame] | 787 | Report("HINT: if you don't care about these warnings you may set " |
| 788 | "ASAN_OPTIONS=alloc_dealloc_mismatch=0\n"); |
| 789 | } |
| 790 | |
Stephen Hines | 6d18623 | 2014-11-26 17:56:19 -0800 | [diff] [blame] | 791 | void ReportMallocUsableSizeNotOwned(uptr addr, BufferedStackTrace *stack) { |
Alexey Samsonov | 9873792 | 2012-08-10 15:13:05 +0000 | [diff] [blame] | 792 | ScopedInErrorReport in_report; |
Kostya Serebryany | 58f5455 | 2012-12-18 07:32:16 +0000 | [diff] [blame] | 793 | Decorator d; |
| 794 | Printf("%s", d.Warning()); |
Kostya Serebryany | 69d8ede | 2012-10-15 13:04:58 +0000 | [diff] [blame] | 795 | Report("ERROR: AddressSanitizer: attempting to call " |
Alexey Samsonov | f7c1d18 | 2012-08-09 08:15:46 +0000 | [diff] [blame] | 796 | "malloc_usable_size() for pointer which is " |
| 797 | "not owned: %p\n", addr); |
Kostya Serebryany | 58f5455 | 2012-12-18 07:32:16 +0000 | [diff] [blame] | 798 | Printf("%s", d.EndWarning()); |
Stephen Hines | 2d1fdb2 | 2014-05-28 23:58:16 -0700 | [diff] [blame] | 799 | stack->Print(); |
Alexey Samsonov | f7c1d18 | 2012-08-09 08:15:46 +0000 | [diff] [blame] | 800 | DescribeHeapAddress(addr, 1); |
Alexey Samsonov | 2fb0872 | 2013-11-01 17:02:14 +0000 | [diff] [blame] | 801 | ReportErrorSummary("bad-malloc_usable_size", stack); |
Alexey Samsonov | f7c1d18 | 2012-08-09 08:15:46 +0000 | [diff] [blame] | 802 | } |
| 803 | |
Stephen Hines | 6d18623 | 2014-11-26 17:56:19 -0800 | [diff] [blame] | 804 | void ReportSanitizerGetAllocatedSizeNotOwned(uptr addr, |
| 805 | BufferedStackTrace *stack) { |
Alexey Samsonov | 9873792 | 2012-08-10 15:13:05 +0000 | [diff] [blame] | 806 | ScopedInErrorReport in_report; |
Kostya Serebryany | 58f5455 | 2012-12-18 07:32:16 +0000 | [diff] [blame] | 807 | Decorator d; |
| 808 | Printf("%s", d.Warning()); |
Kostya Serebryany | 69d8ede | 2012-10-15 13:04:58 +0000 | [diff] [blame] | 809 | Report("ERROR: AddressSanitizer: attempting to call " |
Stephen Hines | 6a211c5 | 2014-07-21 00:49:56 -0700 | [diff] [blame] | 810 | "__sanitizer_get_allocated_size() for pointer which is " |
Alexey Samsonov | f7c1d18 | 2012-08-09 08:15:46 +0000 | [diff] [blame] | 811 | "not owned: %p\n", addr); |
Kostya Serebryany | 58f5455 | 2012-12-18 07:32:16 +0000 | [diff] [blame] | 812 | Printf("%s", d.EndWarning()); |
Stephen Hines | 2d1fdb2 | 2014-05-28 23:58:16 -0700 | [diff] [blame] | 813 | stack->Print(); |
Alexey Samsonov | f7c1d18 | 2012-08-09 08:15:46 +0000 | [diff] [blame] | 814 | DescribeHeapAddress(addr, 1); |
Stephen Hines | 6a211c5 | 2014-07-21 00:49:56 -0700 | [diff] [blame] | 815 | ReportErrorSummary("bad-__sanitizer_get_allocated_size", stack); |
Alexey Samsonov | f7c1d18 | 2012-08-09 08:15:46 +0000 | [diff] [blame] | 816 | } |
| 817 | |
Stephen Hines | 6d18623 | 2014-11-26 17:56:19 -0800 | [diff] [blame] | 818 | void ReportStringFunctionMemoryRangesOverlap(const char *function, |
| 819 | const char *offset1, uptr length1, |
| 820 | const char *offset2, uptr length2, |
| 821 | BufferedStackTrace *stack) { |
Alexey Samsonov | 9873792 | 2012-08-10 15:13:05 +0000 | [diff] [blame] | 822 | ScopedInErrorReport in_report; |
Kostya Serebryany | 58f5455 | 2012-12-18 07:32:16 +0000 | [diff] [blame] | 823 | Decorator d; |
Kostya Serebryany | 2673fd8 | 2013-02-06 12:36:49 +0000 | [diff] [blame] | 824 | char bug_type[100]; |
| 825 | internal_snprintf(bug_type, sizeof(bug_type), "%s-param-overlap", function); |
Kostya Serebryany | 58f5455 | 2012-12-18 07:32:16 +0000 | [diff] [blame] | 826 | Printf("%s", d.Warning()); |
Kostya Serebryany | 2673fd8 | 2013-02-06 12:36:49 +0000 | [diff] [blame] | 827 | Report("ERROR: AddressSanitizer: %s: " |
Alexey Samsonov | 487fee7 | 2012-08-09 08:32:33 +0000 | [diff] [blame] | 828 | "memory ranges [%p,%p) and [%p, %p) overlap\n", \ |
Kostya Serebryany | 2673fd8 | 2013-02-06 12:36:49 +0000 | [diff] [blame] | 829 | bug_type, offset1, offset1 + length1, offset2, offset2 + length2); |
Kostya Serebryany | 58f5455 | 2012-12-18 07:32:16 +0000 | [diff] [blame] | 830 | Printf("%s", d.EndWarning()); |
Stephen Hines | 2d1fdb2 | 2014-05-28 23:58:16 -0700 | [diff] [blame] | 831 | stack->Print(); |
Pirama Arumuga Nainar | cdce50b | 2015-07-01 12:26:56 -0700 | [diff] [blame] | 832 | DescribeAddress((uptr)offset1, length1, bug_type); |
| 833 | DescribeAddress((uptr)offset2, length2, bug_type); |
Alexey Samsonov | 2fb0872 | 2013-11-01 17:02:14 +0000 | [diff] [blame] | 834 | ReportErrorSummary(bug_type, stack); |
Alexey Samsonov | 487fee7 | 2012-08-09 08:32:33 +0000 | [diff] [blame] | 835 | } |
Alexey Samsonov | f7c1d18 | 2012-08-09 08:15:46 +0000 | [diff] [blame] | 836 | |
Stephen Hines | 2d1fdb2 | 2014-05-28 23:58:16 -0700 | [diff] [blame] | 837 | void ReportStringFunctionSizeOverflow(uptr offset, uptr size, |
Stephen Hines | 6d18623 | 2014-11-26 17:56:19 -0800 | [diff] [blame] | 838 | BufferedStackTrace *stack) { |
Stephen Hines | 2d1fdb2 | 2014-05-28 23:58:16 -0700 | [diff] [blame] | 839 | 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 Nainar | cdce50b | 2015-07-01 12:26:56 -0700 | [diff] [blame] | 846 | DescribeAddress(offset, size, bug_type); |
Stephen Hines | 2d1fdb2 | 2014-05-28 23:58:16 -0700 | [diff] [blame] | 847 | ReportErrorSummary(bug_type, stack); |
| 848 | } |
| 849 | |
| 850 | void ReportBadParamsToAnnotateContiguousContainer(uptr beg, uptr end, |
| 851 | uptr old_mid, uptr new_mid, |
Stephen Hines | 6d18623 | 2014-11-26 17:56:19 -0800 | [diff] [blame] | 852 | BufferedStackTrace *stack) { |
Stephen Hines | 2d1fdb2 | 2014-05-28 23:58:16 -0700 | [diff] [blame] | 853 | 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 Hines | 86277eb | 2015-03-23 12:06:32 -0700 | [diff] [blame] | 861 | uptr granularity = SHADOW_GRANULARITY; |
| 862 | if (!IsAligned(beg, granularity)) |
| 863 | Report("ERROR: beg is not aligned by %d\n", granularity); |
Stephen Hines | 2d1fdb2 | 2014-05-28 23:58:16 -0700 | [diff] [blame] | 864 | stack->Print(); |
| 865 | ReportErrorSummary("bad-__sanitizer_annotate_contiguous_container", stack); |
| 866 | } |
| 867 | |
Stephen Hines | 6a211c5 | 2014-07-21 00:49:56 -0700 | [diff] [blame] | 868 | void ReportODRViolation(const __asan_global *g1, u32 stack_id1, |
| 869 | const __asan_global *g2, u32 stack_id2) { |
Stephen Hines | 2d1fdb2 | 2014-05-28 23:58:16 -0700 | [diff] [blame] | 870 | 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 Hines | 6a211c5 | 2014-07-21 00:49:56 -0700 | [diff] [blame] | 875 | InternalScopedString g1_loc(256), g2_loc(256); |
| 876 | PrintGlobalLocation(&g1_loc, *g1); |
| 877 | PrintGlobalLocation(&g2_loc, *g2); |
Stephen Hines | 6d18623 | 2014-11-26 17:56:19 -0800 | [diff] [blame] | 878 | 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 Hines | 6a211c5 | 2014-07-21 00:49:56 -0700 | [diff] [blame] | 882 | if (stack_id1 && stack_id2) { |
| 883 | Printf("These globals were registered at these points:\n"); |
| 884 | Printf(" [1]:\n"); |
Stephen Hines | 6d18623 | 2014-11-26 17:56:19 -0800 | [diff] [blame] | 885 | StackDepotGet(stack_id1).Print(); |
Stephen Hines | 6a211c5 | 2014-07-21 00:49:56 -0700 | [diff] [blame] | 886 | Printf(" [2]:\n"); |
Stephen Hines | 6d18623 | 2014-11-26 17:56:19 -0800 | [diff] [blame] | 887 | StackDepotGet(stack_id2).Print(); |
Stephen Hines | 6a211c5 | 2014-07-21 00:49:56 -0700 | [diff] [blame] | 888 | } |
Stephen Hines | 2d1fdb2 | 2014-05-28 23:58:16 -0700 | [diff] [blame] | 889 | Report("HINT: if you don't care about these warnings you may set " |
| 890 | "ASAN_OPTIONS=detect_odr_violation=0\n"); |
Stephen Hines | 6d18623 | 2014-11-26 17:56:19 -0800 | [diff] [blame] | 891 | 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 Hines | 2d1fdb2 | 2014-05-28 23:58:16 -0700 | [diff] [blame] | 895 | } |
| 896 | |
| 897 | // ----------------------- CheckForInvalidPointerPair ----------- {{{1 |
| 898 | static NOINLINE void |
| 899 | ReportInvalidPointerPair(uptr pc, uptr bp, uptr sp, uptr a1, uptr a2) { |
| 900 | ScopedInErrorReport in_report; |
Pirama Arumuga Nainar | cdce50b | 2015-07-01 12:26:56 -0700 | [diff] [blame] | 901 | const char *bug_type = "invalid-pointer-pair"; |
Stephen Hines | 2d1fdb2 | 2014-05-28 23:58:16 -0700 | [diff] [blame] | 902 | 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 Nainar | cdce50b | 2015-07-01 12:26:56 -0700 | [diff] [blame] | 908 | DescribeAddress(a1, 1, bug_type); |
| 909 | DescribeAddress(a2, 1, bug_type); |
| 910 | ReportErrorSummary(bug_type, &stack); |
Stephen Hines | 2d1fdb2 | 2014-05-28 23:58:16 -0700 | [diff] [blame] | 911 | } |
| 912 | |
| 913 | static 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 Samsonov | 663c501 | 2012-08-09 12:15:40 +0000 | [diff] [blame] | 926 | // ----------------------- Mac-specific reports ----------------- {{{1 |
| 927 | |
Stephen Hines | 6d18623 | 2014-11-26 17:56:19 -0800 | [diff] [blame] | 928 | void WarnMacFreeUnallocated(uptr addr, uptr zone_ptr, const char *zone_name, |
| 929 | BufferedStackTrace *stack) { |
Alexey Samsonov | 9873792 | 2012-08-10 15:13:05 +0000 | [diff] [blame] | 930 | // Just print a warning here. |
Kostya Serebryany | 283c296 | 2012-08-28 11:34:40 +0000 | [diff] [blame] | 931 | Printf("free_common(%p) -- attempting to free unallocated memory.\n" |
Alexey Samsonov | 663c501 | 2012-08-09 12:15:40 +0000 | [diff] [blame] | 932 | "AddressSanitizer is ignoring this error on Mac OS now.\n", |
| 933 | addr); |
| 934 | PrintZoneForPointer(addr, zone_ptr, zone_name); |
Stephen Hines | 2d1fdb2 | 2014-05-28 23:58:16 -0700 | [diff] [blame] | 935 | stack->Print(); |
Alexey Samsonov | 9873792 | 2012-08-10 15:13:05 +0000 | [diff] [blame] | 936 | DescribeHeapAddress(addr, 1); |
Alexey Samsonov | 663c501 | 2012-08-09 12:15:40 +0000 | [diff] [blame] | 937 | } |
| 938 | |
Stephen Hines | 6d18623 | 2014-11-26 17:56:19 -0800 | [diff] [blame] | 939 | void ReportMacMzReallocUnknown(uptr addr, uptr zone_ptr, const char *zone_name, |
| 940 | BufferedStackTrace *stack) { |
Alexey Samsonov | 9873792 | 2012-08-10 15:13:05 +0000 | [diff] [blame] | 941 | ScopedInErrorReport in_report; |
Kostya Serebryany | 283c296 | 2012-08-28 11:34:40 +0000 | [diff] [blame] | 942 | Printf("mz_realloc(%p) -- attempting to realloc unallocated memory.\n" |
Alexey Samsonov | 663c501 | 2012-08-09 12:15:40 +0000 | [diff] [blame] | 943 | "This is an unrecoverable problem, exiting now.\n", |
| 944 | addr); |
| 945 | PrintZoneForPointer(addr, zone_ptr, zone_name); |
Stephen Hines | 2d1fdb2 | 2014-05-28 23:58:16 -0700 | [diff] [blame] | 946 | stack->Print(); |
Alexey Samsonov | 9873792 | 2012-08-10 15:13:05 +0000 | [diff] [blame] | 947 | DescribeHeapAddress(addr, 1); |
Alexey Samsonov | 663c501 | 2012-08-09 12:15:40 +0000 | [diff] [blame] | 948 | } |
| 949 | |
Stephen Hines | 6d18623 | 2014-11-26 17:56:19 -0800 | [diff] [blame] | 950 | void ReportMacCfReallocUnknown(uptr addr, uptr zone_ptr, const char *zone_name, |
| 951 | BufferedStackTrace *stack) { |
Alexey Samsonov | 9873792 | 2012-08-10 15:13:05 +0000 | [diff] [blame] | 952 | ScopedInErrorReport in_report; |
Kostya Serebryany | 283c296 | 2012-08-28 11:34:40 +0000 | [diff] [blame] | 953 | Printf("cf_realloc(%p) -- attempting to realloc unallocated memory.\n" |
Alexey Samsonov | 663c501 | 2012-08-09 12:15:40 +0000 | [diff] [blame] | 954 | "This is an unrecoverable problem, exiting now.\n", |
| 955 | addr); |
| 956 | PrintZoneForPointer(addr, zone_ptr, zone_name); |
Stephen Hines | 2d1fdb2 | 2014-05-28 23:58:16 -0700 | [diff] [blame] | 957 | stack->Print(); |
Alexey Samsonov | 9873792 | 2012-08-10 15:13:05 +0000 | [diff] [blame] | 958 | DescribeHeapAddress(addr, 1); |
Alexey Samsonov | c98570b | 2012-08-09 10:56:57 +0000 | [diff] [blame] | 959 | } |
| 960 | |
Alexey Samsonov | 812ff90 | 2012-08-09 11:29:13 +0000 | [diff] [blame] | 961 | } // namespace __asan |
| 962 | |
| 963 | // --------------------------- Interface --------------------- {{{1 |
| 964 | using namespace __asan; // NOLINT |
| 965 | |
Stephen Hines | 2d1fdb2 | 2014-05-28 23:58:16 -0700 | [diff] [blame] | 966 | void __asan_report_error(uptr pc, uptr bp, uptr sp, uptr addr, int is_write, |
Pirama Arumuga Nainar | 7c91505 | 2015-04-08 08:58:29 -0700 | [diff] [blame] | 967 | uptr access_size, u32 exp) { |
Stephen Hines | 86277eb | 2015-03-23 12:06:32 -0700 | [diff] [blame] | 968 | ENABLE_FRAME_POINTER; |
| 969 | |
Pirama Arumuga Nainar | 7c91505 | 2015-04-08 08:58:29 -0700 | [diff] [blame] | 970 | // 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 Samsonov | 9873792 | 2012-08-10 15:13:05 +0000 | [diff] [blame] | 979 | // Determine the error type. |
Alexey Samsonov | c98570b | 2012-08-09 10:56:57 +0000 | [diff] [blame] | 980 | 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 Hines | 6d18623 | 2014-11-26 17:56:19 -0800 | [diff] [blame] | 992 | case kAsanArrayCookieMagic: |
Alexey Samsonov | c98570b | 2012-08-09 10:56:57 +0000 | [diff] [blame] | 993 | 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 Serebryany | 3945c58 | 2012-08-21 14:10:25 +0000 | [diff] [blame] | 1001 | case kAsanInitializationOrderMagic: |
| 1002 | bug_descr = "initialization-order-fiasco"; |
| 1003 | break; |
Alexey Samsonov | c98570b | 2012-08-09 10:56:57 +0000 | [diff] [blame] | 1004 | 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 Hines | 2d1fdb2 | 2014-05-28 23:58:16 -0700 | [diff] [blame] | 1015 | case kAsanContiguousContainerOOBMagic: |
| 1016 | bug_descr = "container-overflow"; |
| 1017 | break; |
Alexey Samsonov | d4b5db8 | 2012-12-04 01:38:15 +0000 | [diff] [blame] | 1018 | case kAsanStackUseAfterScopeMagic: |
| 1019 | bug_descr = "stack-use-after-scope"; |
| 1020 | break; |
Alexey Samsonov | c98570b | 2012-08-09 10:56:57 +0000 | [diff] [blame] | 1021 | case kAsanGlobalRedzoneMagic: |
| 1022 | bug_descr = "global-buffer-overflow"; |
| 1023 | break; |
Stephen Hines | 6d18623 | 2014-11-26 17:56:19 -0800 | [diff] [blame] | 1024 | case kAsanIntraObjectRedzone: |
| 1025 | bug_descr = "intra-object-overflow"; |
| 1026 | break; |
Stephen Hines | 86277eb | 2015-03-23 12:06:32 -0700 | [diff] [blame] | 1027 | case kAsanAllocaLeftMagic: |
| 1028 | case kAsanAllocaRightMagic: |
| 1029 | bug_descr = "dynamic-stack-buffer-overflow"; |
| 1030 | break; |
Alexey Samsonov | c98570b | 2012-08-09 10:56:57 +0000 | [diff] [blame] | 1031 | } |
| 1032 | } |
Stephen Hines | 6d18623 | 2014-11-26 17:56:19 -0800 | [diff] [blame] | 1033 | |
| 1034 | ReportData report = { pc, sp, bp, addr, (bool)is_write, access_size, |
| 1035 | bug_descr }; |
| 1036 | ScopedInErrorReport in_report(&report); |
| 1037 | |
Kostya Serebryany | 58f5455 | 2012-12-18 07:32:16 +0000 | [diff] [blame] | 1038 | Decorator d; |
| 1039 | Printf("%s", d.Warning()); |
Kostya Serebryany | 69d8ede | 2012-10-15 13:04:58 +0000 | [diff] [blame] | 1040 | Report("ERROR: AddressSanitizer: %s on address " |
Stephen Hines | 6d18623 | 2014-11-26 17:56:19 -0800 | [diff] [blame] | 1041 | "%p at pc %p bp %p sp %p\n", |
Alexey Samsonov | c98570b | 2012-08-09 10:56:57 +0000 | [diff] [blame] | 1042 | bug_descr, (void*)addr, pc, bp, sp); |
Kostya Serebryany | 58f5455 | 2012-12-18 07:32:16 +0000 | [diff] [blame] | 1043 | Printf("%s", d.EndWarning()); |
Alexey Samsonov | c98570b | 2012-08-09 10:56:57 +0000 | [diff] [blame] | 1044 | |
Alexey Samsonov | 89c1384 | 2013-03-20 09:23:28 +0000 | [diff] [blame] | 1045 | u32 curr_tid = GetCurrentTidOrInvalid(); |
Kostya Serebryany | 716e2f2 | 2012-12-07 15:15:01 +0000 | [diff] [blame] | 1046 | char tname[128]; |
Kostya Serebryany | 58f5455 | 2012-12-18 07:32:16 +0000 | [diff] [blame] | 1047 | 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 Samsonov | c98570b | 2012-08-09 10:56:57 +0000 | [diff] [blame] | 1053 | |
Kostya Serebryany | a30c8f9 | 2012-12-13 09:34:23 +0000 | [diff] [blame] | 1054 | GET_STACK_TRACE_FATAL(pc, bp); |
Stephen Hines | 2d1fdb2 | 2014-05-28 23:58:16 -0700 | [diff] [blame] | 1055 | stack.Print(); |
Alexey Samsonov | c98570b | 2012-08-09 10:56:57 +0000 | [diff] [blame] | 1056 | |
Pirama Arumuga Nainar | cdce50b | 2015-07-01 12:26:56 -0700 | [diff] [blame] | 1057 | DescribeAddress(addr, access_size, bug_descr); |
Alexey Samsonov | 2fb0872 | 2013-11-01 17:02:14 +0000 | [diff] [blame] | 1058 | ReportErrorSummary(bug_descr, &stack); |
Alexey Samsonov | 9873792 | 2012-08-10 15:13:05 +0000 | [diff] [blame] | 1059 | PrintShadowMemoryForAddress(addr); |
Alexey Samsonov | c98570b | 2012-08-09 10:56:57 +0000 | [diff] [blame] | 1060 | } |
| 1061 | |
Alexey Samsonov | c98570b | 2012-08-09 10:56:57 +0000 | [diff] [blame] | 1062 | void 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 Hines | 2d1fdb2 | 2014-05-28 23:58:16 -0700 | [diff] [blame] | 1067 | (char*)MmapOrDie(error_message_buffer_size, __func__); |
Alexey Samsonov | c98570b | 2012-08-09 10:56:57 +0000 | [diff] [blame] | 1068 | error_message_buffer_pos = 0; |
| 1069 | } |
| 1070 | } |
Alexey Samsonov | f657a19 | 2012-08-13 11:23:40 +0000 | [diff] [blame] | 1071 | |
Kostya Serebryany | 17a7c67 | 2012-12-29 10:18:31 +0000 | [diff] [blame] | 1072 | void __asan_describe_address(uptr addr) { |
Stephen Hines | 6d18623 | 2014-11-26 17:56:19 -0800 | [diff] [blame] | 1073 | // Thread registry must be locked while we're describing an address. |
| 1074 | asanThreadRegistry().Lock(); |
Pirama Arumuga Nainar | cdce50b | 2015-07-01 12:26:56 -0700 | [diff] [blame] | 1075 | DescribeAddress(addr, 1, ""); |
Stephen Hines | 6d18623 | 2014-11-26 17:56:19 -0800 | [diff] [blame] | 1076 | asanThreadRegistry().Unlock(); |
| 1077 | } |
| 1078 | |
| 1079 | int __asan_report_present() { |
| 1080 | return report_happened ? 1 : 0; |
| 1081 | } |
| 1082 | |
| 1083 | uptr __asan_get_report_pc() { |
| 1084 | return report_data.pc; |
| 1085 | } |
| 1086 | |
| 1087 | uptr __asan_get_report_bp() { |
| 1088 | return report_data.bp; |
| 1089 | } |
| 1090 | |
| 1091 | uptr __asan_get_report_sp() { |
| 1092 | return report_data.sp; |
| 1093 | } |
| 1094 | |
| 1095 | uptr __asan_get_report_address() { |
| 1096 | return report_data.addr; |
| 1097 | } |
| 1098 | |
| 1099 | int __asan_get_report_access_type() { |
| 1100 | return report_data.is_write ? 1 : 0; |
| 1101 | } |
| 1102 | |
| 1103 | uptr __asan_get_report_access_size() { |
| 1104 | return report_data.access_size; |
| 1105 | } |
| 1106 | |
| 1107 | const char *__asan_get_report_description() { |
| 1108 | return report_data.description; |
Kostya Serebryany | 17a7c67 | 2012-12-29 10:18:31 +0000 | [diff] [blame] | 1109 | } |
| 1110 | |
Stephen Hines | 2d1fdb2 | 2014-05-28 23:58:16 -0700 | [diff] [blame] | 1111 | extern "C" { |
| 1112 | SANITIZER_INTERFACE_ATTRIBUTE |
| 1113 | void __sanitizer_ptr_sub(void *a, void *b) { |
| 1114 | CheckForInvalidPointerPair(a, b); |
| 1115 | } |
| 1116 | SANITIZER_INTERFACE_ATTRIBUTE |
| 1117 | void __sanitizer_ptr_cmp(void *a, void *b) { |
| 1118 | CheckForInvalidPointerPair(a, b); |
| 1119 | } |
| 1120 | } // extern "C" |
| 1121 | |
Alexey Samsonov | 6a08d29 | 2012-12-07 22:01:28 +0000 | [diff] [blame] | 1122 | #if !SANITIZER_SUPPORTS_WEAK_HOOKS |
Alexey Samsonov | 8663343 | 2012-10-02 14:06:39 +0000 | [diff] [blame] | 1123 | // Provide default implementation of __asan_on_error that does nothing |
| 1124 | // and may be overriden by user. |
Timur Iskhodzhanov | 3c80c6c | 2013-08-13 11:42:45 +0000 | [diff] [blame] | 1125 | SANITIZER_INTERFACE_ATTRIBUTE SANITIZER_WEAK_ATTRIBUTE NOINLINE |
Alexey Samsonov | 8663343 | 2012-10-02 14:06:39 +0000 | [diff] [blame] | 1126 | void __asan_on_error() {} |
Alexey Samsonov | 6a08d29 | 2012-12-07 22:01:28 +0000 | [diff] [blame] | 1127 | #endif |