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 | //===----------------------------------------------------------------------===// |
Pirama Arumuga Nainar | 799172d | 2016-03-03 15:50:30 -0800 | [diff] [blame^] | 14 | |
Alexey Samsonov | 9873792 | 2012-08-10 15:13:05 +0000 | [diff] [blame] | 15 | #include "asan_flags.h" |
Alexey Samsonov | 7354509 | 2012-08-09 07:40:58 +0000 | [diff] [blame] | 16 | #include "asan_internal.h" |
Alexey Samsonov | e218beb | 2012-08-09 09:06:52 +0000 | [diff] [blame] | 17 | #include "asan_mapping.h" |
Alexey Samsonov | 7354509 | 2012-08-09 07:40:58 +0000 | [diff] [blame] | 18 | #include "asan_report.h" |
| 19 | #include "asan_stack.h" |
Alexey Samsonov | e4bfca2 | 2012-08-09 09:27:24 +0000 | [diff] [blame] | 20 | #include "asan_thread.h" |
Kostya Serebryany | 58f5455 | 2012-12-18 07:32:16 +0000 | [diff] [blame] | 21 | #include "sanitizer_common/sanitizer_common.h" |
Sergey Matveev | ed20ebe | 2013-05-06 11:27:58 +0000 | [diff] [blame] | 22 | #include "sanitizer_common/sanitizer_flags.h" |
Kostya Serebryany | 58f5455 | 2012-12-18 07:32:16 +0000 | [diff] [blame] | 23 | #include "sanitizer_common/sanitizer_report_decorator.h" |
Kostya Serebryany | 6d95869 | 2013-10-18 14:50:44 +0000 | [diff] [blame] | 24 | #include "sanitizer_common/sanitizer_stackdepot.h" |
Alexey Samsonov | 9c92748 | 2012-12-26 14:44:46 +0000 | [diff] [blame] | 25 | #include "sanitizer_common/sanitizer_symbolizer.h" |
Alexey Samsonov | 7354509 | 2012-08-09 07:40:58 +0000 | [diff] [blame] | 26 | |
| 27 | namespace __asan { |
| 28 | |
Alexey Samsonov | f657a19 | 2012-08-13 11:23:40 +0000 | [diff] [blame] | 29 | // -------------------- User-specified callbacks ----------------- {{{1 |
Alexey Samsonov | c98570b | 2012-08-09 10:56:57 +0000 | [diff] [blame] | 30 | static void (*error_report_callback)(const char*); |
Pirama Arumuga Nainar | 799172d | 2016-03-03 15:50:30 -0800 | [diff] [blame^] | 31 | static char *error_message_buffer = nullptr; |
Alexey Samsonov | c98570b | 2012-08-09 10:56:57 +0000 | [diff] [blame] | 32 | static uptr error_message_buffer_pos = 0; |
Pirama Arumuga Nainar | 799172d | 2016-03-03 15:50:30 -0800 | [diff] [blame^] | 33 | static BlockingMutex error_message_buf_mutex(LINKER_INITIALIZED); |
| 34 | static const unsigned kAsanBuggyPcPoolSize = 25; |
| 35 | static __sanitizer::atomic_uintptr_t AsanBuggyPcPool[kAsanBuggyPcPoolSize]; |
Alexey Samsonov | c98570b | 2012-08-09 10:56:57 +0000 | [diff] [blame] | 36 | |
Stephen Hines | 6d18623 | 2014-11-26 17:56:19 -0800 | [diff] [blame] | 37 | struct 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 | |
| 47 | static bool report_happened = false; |
| 48 | static ReportData report_data = {}; |
| 49 | |
Alexey Samsonov | c98570b | 2012-08-09 10:56:57 +0000 | [diff] [blame] | 50 | void AppendToErrorMessageBuffer(const char *buffer) { |
Pirama Arumuga Nainar | 799172d | 2016-03-03 15:50:30 -0800 | [diff] [blame^] | 51 | 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 Samsonov | c98570b | 2012-08-09 10:56:57 +0000 | [diff] [blame] | 56 | } |
Pirama Arumuga Nainar | 799172d | 2016-03-03 15:50:30 -0800 | [diff] [blame^] | 57 | 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 Samsonov | c98570b | 2012-08-09 10:56:57 +0000 | [diff] [blame] | 65 | } |
| 66 | |
Kostya Serebryany | 58f5455 | 2012-12-18 07:32:16 +0000 | [diff] [blame] | 67 | // ---------------------- Decorator ------------------------------ {{{1 |
Stephen Hines | 2d1fdb2 | 2014-05-28 23:58:16 -0700 | [diff] [blame] | 68 | class Decorator: public __sanitizer::SanitizerCommonDecorator { |
Kostya Serebryany | 58f5455 | 2012-12-18 07:32:16 +0000 | [diff] [blame] | 69 | public: |
Stephen Hines | 2d1fdb2 | 2014-05-28 23:58:16 -0700 | [diff] [blame] | 70 | Decorator() : SanitizerCommonDecorator() { } |
Kostya Serebryany | 58f5455 | 2012-12-18 07:32:16 +0000 | [diff] [blame] | 71 | 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 Serebryany | 9514a53 | 2012-12-19 09:53:32 +0000 | [diff] [blame] | 77 | |
| 78 | const char *ShadowByte(u8 byte) { |
| 79 | switch (byte) { |
| 80 | case kAsanHeapLeftRedzoneMagic: |
| 81 | case kAsanHeapRightRedzoneMagic: |
Stephen Hines | 6d18623 | 2014-11-26 17:56:19 -0800 | [diff] [blame] | 82 | case kAsanArrayCookieMagic: |
Kostya Serebryany | 9514a53 | 2012-12-19 09:53:32 +0000 | [diff] [blame] | 83 | 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 Hines | 2d1fdb2 | 2014-05-28 23:58:16 -0700 | [diff] [blame] | 96 | case kAsanContiguousContainerOOBMagic: |
Stephen Hines | 86277eb | 2015-03-23 12:06:32 -0700 | [diff] [blame] | 97 | case kAsanAllocaLeftMagic: |
| 98 | case kAsanAllocaRightMagic: |
Kostya Serebryany | 9514a53 | 2012-12-19 09:53:32 +0000 | [diff] [blame] | 99 | return Blue(); |
| 100 | case kAsanStackUseAfterScopeMagic: |
| 101 | return Magenta(); |
| 102 | case kAsanGlobalRedzoneMagic: |
| 103 | return Red(); |
| 104 | case kAsanInternalHeapMagic: |
| 105 | return Yellow(); |
Stephen Hines | 6d18623 | 2014-11-26 17:56:19 -0800 | [diff] [blame] | 106 | case kAsanIntraObjectRedzone: |
| 107 | return Yellow(); |
Kostya Serebryany | 9514a53 | 2012-12-19 09:53:32 +0000 | [diff] [blame] | 108 | default: |
| 109 | return Default(); |
| 110 | } |
| 111 | } |
| 112 | const char *EndShadowByte() { return Default(); } |
Stephen Hines | 6d18623 | 2014-11-26 17:56:19 -0800 | [diff] [blame] | 113 | const char *MemoryByte() { return Magenta(); } |
| 114 | const char *EndMemoryByte() { return Default(); } |
Kostya Serebryany | 58f5455 | 2012-12-18 07:32:16 +0000 | [diff] [blame] | 115 | }; |
| 116 | |
Alexey Samsonov | 9873792 | 2012-08-10 15:13:05 +0000 | [diff] [blame] | 117 | // ---------------------- Helper functions ----------------------- {{{1 |
| 118 | |
Stephen Hines | 6d18623 | 2014-11-26 17:56:19 -0800 | [diff] [blame] | 119 | static void PrintMemoryByte(InternalScopedString *str, const char *before, |
| 120 | u8 byte, bool in_shadow, const char *after = "\n") { |
Kostya Serebryany | 9514a53 | 2012-12-19 09:53:32 +0000 | [diff] [blame] | 121 | Decorator d; |
Stephen Hines | 6d18623 | 2014-11-26 17:56:19 -0800 | [diff] [blame] | 122 | 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 | |
| 128 | static void PrintShadowByte(InternalScopedString *str, const char *before, |
| 129 | u8 byte, const char *after = "\n") { |
| 130 | PrintMemoryByte(str, before, byte, /*in_shadow*/true, after); |
Kostya Serebryany | 9514a53 | 2012-12-19 09:53:32 +0000 | [diff] [blame] | 131 | } |
| 132 | |
Stephen Hines | 2d1fdb2 | 2014-05-28 23:58:16 -0700 | [diff] [blame] | 133 | static void PrintShadowBytes(InternalScopedString *str, const char *before, |
| 134 | u8 *bytes, u8 *guilty, uptr n) { |
Kostya Serebryany | 9514a53 | 2012-12-19 09:53:32 +0000 | [diff] [blame] | 135 | Decorator d; |
Stephen Hines | 2d1fdb2 | 2014-05-28 23:58:16 -0700 | [diff] [blame] | 136 | if (before) str->append("%s%p:", before, bytes); |
Kostya Serebryany | 9514a53 | 2012-12-19 09:53:32 +0000 | [diff] [blame] | 137 | for (uptr i = 0; i < n; i++) { |
| 138 | u8 *p = bytes + i; |
Stephen Hines | 2d1fdb2 | 2014-05-28 23:58:16 -0700 | [diff] [blame] | 139 | const char *before = |
| 140 | p == guilty ? "[" : (p - 1 == guilty && i != 0) ? "" : " "; |
Kostya Serebryany | 9514a53 | 2012-12-19 09:53:32 +0000 | [diff] [blame] | 141 | const char *after = p == guilty ? "]" : ""; |
Stephen Hines | 2d1fdb2 | 2014-05-28 23:58:16 -0700 | [diff] [blame] | 142 | PrintShadowByte(str, before, *p, after); |
Alexey Samsonov | 9873792 | 2012-08-10 15:13:05 +0000 | [diff] [blame] | 143 | } |
Stephen Hines | 2d1fdb2 | 2014-05-28 23:58:16 -0700 | [diff] [blame] | 144 | str->append("\n"); |
Alexey Samsonov | 9873792 | 2012-08-10 15:13:05 +0000 | [diff] [blame] | 145 | } |
| 146 | |
Stephen Hines | 2d1fdb2 | 2014-05-28 23:58:16 -0700 | [diff] [blame] | 147 | static 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 Hines | 6d18623 | 2014-11-26 17:56:19 -0800 | [diff] [blame] | 180 | PrintShadowByte(str, " Array cookie: ", |
| 181 | kAsanArrayCookieMagic); |
| 182 | PrintShadowByte(str, " Intra object redzone: ", |
| 183 | kAsanIntraObjectRedzone); |
Stephen Hines | 2d1fdb2 | 2014-05-28 23:58:16 -0700 | [diff] [blame] | 184 | PrintShadowByte(str, " ASan internal: ", kAsanInternalHeapMagic); |
Stephen Hines | 86277eb | 2015-03-23 12:06:32 -0700 | [diff] [blame] | 185 | PrintShadowByte(str, " Left alloca redzone: ", kAsanAllocaLeftMagic); |
| 186 | PrintShadowByte(str, " Right alloca redzone: ", kAsanAllocaRightMagic); |
Alexey Samsonov | 9873792 | 2012-08-10 15:13:05 +0000 | [diff] [blame] | 187 | } |
| 188 | |
Stephen Hines | 6d18623 | 2014-11-26 17:56:19 -0800 | [diff] [blame] | 189 | void 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 Serebryany | 95f630a | 2013-01-28 07:34:22 +0000 | [diff] [blame] | 205 | static void PrintShadowMemoryForAddress(uptr addr) { |
Stephen Hines | 2d1fdb2 | 2014-05-28 23:58:16 -0700 | [diff] [blame] | 206 | if (!AddrIsInMem(addr)) return; |
Kostya Serebryany | 95f630a | 2013-01-28 07:34:22 +0000 | [diff] [blame] | 207 | 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 Hines | 2d1fdb2 | 2014-05-28 23:58:16 -0700 | [diff] [blame] | 210 | InternalScopedString str(4096 * 8); |
| 211 | str.append("Shadow bytes around the buggy address:\n"); |
Kostya Serebryany | 95f630a | 2013-01-28 07:34:22 +0000 | [diff] [blame] | 212 | for (int i = -5; i <= 5; i++) { |
| 213 | const char *prefix = (i == 0) ? "=>" : " "; |
Stephen Hines | 2d1fdb2 | 2014-05-28 23:58:16 -0700 | [diff] [blame] | 214 | PrintShadowBytes(&str, prefix, (u8 *)(aligned_shadow + i * n_bytes_per_row), |
| 215 | (u8 *)shadow_addr, n_bytes_per_row); |
Kostya Serebryany | 95f630a | 2013-01-28 07:34:22 +0000 | [diff] [blame] | 216 | } |
Stephen Hines | 2d1fdb2 | 2014-05-28 23:58:16 -0700 | [diff] [blame] | 217 | if (flags()->print_legend) PrintLegend(&str); |
| 218 | Printf("%s", str.data()); |
Kostya Serebryany | 95f630a | 2013-01-28 07:34:22 +0000 | [diff] [blame] | 219 | } |
| 220 | |
Alexey Samsonov | 9873792 | 2012-08-10 15:13:05 +0000 | [diff] [blame] | 221 | static void PrintZoneForPointer(uptr ptr, uptr zone_ptr, |
| 222 | const char *zone_name) { |
| 223 | if (zone_ptr) { |
| 224 | if (zone_name) { |
Kostya Serebryany | 283c296 | 2012-08-28 11:34:40 +0000 | [diff] [blame] | 225 | Printf("malloc_zone_from_ptr(%p) = %p, which is %s\n", |
Alexey Samsonov | 9873792 | 2012-08-10 15:13:05 +0000 | [diff] [blame] | 226 | ptr, zone_ptr, zone_name); |
| 227 | } else { |
Kostya Serebryany | 283c296 | 2012-08-28 11:34:40 +0000 | [diff] [blame] | 228 | 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] | 229 | ptr, zone_ptr); |
| 230 | } |
| 231 | } else { |
Kostya Serebryany | 283c296 | 2012-08-28 11:34:40 +0000 | [diff] [blame] | 232 | Printf("malloc_zone_from_ptr(%p) = 0\n", ptr); |
Alexey Samsonov | 9873792 | 2012-08-10 15:13:05 +0000 | [diff] [blame] | 233 | } |
| 234 | } |
| 235 | |
Timur Iskhodzhanov | 997454a | 2013-09-10 08:36:21 +0000 | [diff] [blame] | 236 | static void DescribeThread(AsanThread *t) { |
| 237 | if (t) |
| 238 | DescribeThread(t->context()); |
| 239 | } |
| 240 | |
Alexey Samsonov | e218beb | 2012-08-09 09:06:52 +0000 | [diff] [blame] | 241 | // ---------------------- Address Descriptions ------------------- {{{1 |
| 242 | |
Alexey Samsonov | e4bfca2 | 2012-08-09 09:27:24 +0000 | [diff] [blame] | 243 | static bool IsASCII(unsigned char c) { |
Alexey Samsonov | 9873792 | 2012-08-10 15:13:05 +0000 | [diff] [blame] | 244 | return /*0x00 <= c &&*/ c <= 0x7F; |
Alexey Samsonov | e4bfca2 | 2012-08-09 09:27:24 +0000 | [diff] [blame] | 245 | } |
| 246 | |
Alexey Samsonov | c942427 | 2013-03-27 10:41:22 +0000 | [diff] [blame] | 247 | static 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 Hines | 2d1fdb2 | 2014-05-28 23:58:16 -0700 | [diff] [blame] | 250 | 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 Hines | 6d18623 | 2014-11-26 17:56:19 -0800 | [diff] [blame] | 256 | return should_demangle ? Symbolizer::GetOrInit()->Demangle(name) : name; |
Alexey Samsonov | c942427 | 2013-03-27 10:41:22 +0000 | [diff] [blame] | 257 | } |
| 258 | |
Alexey Samsonov | 939316c | 2013-04-01 08:57:38 +0000 | [diff] [blame] | 259 | // 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] | 260 | static void PrintGlobalNameIfASCII(InternalScopedString *str, |
| 261 | const __asan_global &g) { |
Alexey Samsonov | 939316c | 2013-04-01 08:57:38 +0000 | [diff] [blame] | 262 | 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 Hines | 2d1fdb2 | 2014-05-28 23:58:16 -0700 | [diff] [blame] | 267 | str->append(" '%s' is ascii string '%s'\n", MaybeDemangleGlobalName(g.name), |
| 268 | (char *)g.beg); |
Alexey Samsonov | 939316c | 2013-04-01 08:57:38 +0000 | [diff] [blame] | 269 | } |
| 270 | |
Stephen Hines | 6a211c5 | 2014-07-21 00:49:56 -0700 | [diff] [blame] | 271 | static 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 | |
| 280 | static 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 Nainar | cdce50b | 2015-07-01 12:26:56 -0700 | [diff] [blame] | 291 | static void DescribeAddressRelativeToGlobal(uptr addr, uptr size, |
| 292 | const __asan_global &g) { |
Stephen Hines | 2d1fdb2 | 2014-05-28 23:58:16 -0700 | [diff] [blame] | 293 | InternalScopedString str(4096); |
Kostya Serebryany | 58f5455 | 2012-12-18 07:32:16 +0000 | [diff] [blame] | 294 | Decorator d; |
Stephen Hines | 2d1fdb2 | 2014-05-28 23:58:16 -0700 | [diff] [blame] | 295 | str.append("%s", d.Location()); |
Alexey Samsonov | e4bfca2 | 2012-08-09 09:27:24 +0000 | [diff] [blame] | 296 | if (addr < g.beg) { |
Stephen Hines | 2d1fdb2 | 2014-05-28 23:58:16 -0700 | [diff] [blame] | 297 | str.append("%p is located %zd bytes to the left", (void *)addr, |
| 298 | g.beg - addr); |
Evgeniy Stepanov | 589dcda | 2013-02-05 14:32:03 +0000 | [diff] [blame] | 299 | } else if (addr + size > g.beg + g.size) { |
| 300 | if (addr < g.beg + g.size) |
| 301 | addr = g.beg + g.size; |
Stephen Hines | 2d1fdb2 | 2014-05-28 23:58:16 -0700 | [diff] [blame] | 302 | str.append("%p is located %zd bytes to the right", (void *)addr, |
| 303 | addr - (g.beg + g.size)); |
Alexey Samsonov | e4bfca2 | 2012-08-09 09:27:24 +0000 | [diff] [blame] | 304 | } else { |
Evgeniy Stepanov | 589dcda | 2013-02-05 14:32:03 +0000 | [diff] [blame] | 305 | // Can it happen? |
Stephen Hines | 2d1fdb2 | 2014-05-28 23:58:16 -0700 | [diff] [blame] | 306 | 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] | 307 | } |
Stephen Hines | 6a211c5 | 2014-07-21 00:49:56 -0700 | [diff] [blame] | 308 | 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 Hines | 2d1fdb2 | 2014-05-28 23:58:16 -0700 | [diff] [blame] | 312 | str.append("%s", d.EndLocation()); |
| 313 | PrintGlobalNameIfASCII(&str, g); |
| 314 | Printf("%s", str.data()); |
Pirama Arumuga Nainar | cdce50b | 2015-07-01 12:26:56 -0700 | [diff] [blame] | 315 | } |
| 316 | |
| 317 | static 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 Samsonov | e4bfca2 | 2012-08-09 09:27:24 +0000 | [diff] [blame] | 335 | return true; |
| 336 | } |
| 337 | |
Stephen Hines | 6d18623 | 2014-11-26 17:56:19 -0800 | [diff] [blame] | 338 | bool DescribeAddressIfShadow(uptr addr, AddressDescription *descr, bool print) { |
Alexey Samsonov | e218beb | 2012-08-09 09:06:52 +0000 | [diff] [blame] | 339 | if (AddrIsInMem(addr)) |
| 340 | return false; |
Stephen Hines | 6d18623 | 2014-11-26 17:56:19 -0800 | [diff] [blame] | 341 | 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 Samsonov | e218beb | 2012-08-09 09:06:52 +0000 | [diff] [blame] | 352 | return true; |
| 353 | } |
| 354 | CHECK(0 && "Address is not in memory and not in shadow?"); |
| 355 | return false; |
| 356 | } |
| 357 | |
Kostya Serebryany | 50f3daa | 2013-03-22 10:36:24 +0000 | [diff] [blame] | 358 | // Return " (thread_name) " or an empty string if the name is empty. |
| 359 | const 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 | |
| 370 | const 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 Hines | 6d18623 | 2014-11-26 17:56:19 -0800 | [diff] [blame] | 378 | static 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 Serebryany | edb39c7 | 2013-09-03 13:58:04 +0000 | [diff] [blame] | 382 | uptr addr_end = addr + access_size; |
Pirama Arumuga Nainar | 799172d | 2016-03-03 15:50:30 -0800 | [diff] [blame^] | 383 | const char *pos_descr = nullptr; |
Stephen Hines | 6d18623 | 2014-11-26 17:56:19 -0800 | [diff] [blame] | 384 | // 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] | 385 | // current memory access, indicate it in the log. |
Stephen Hines | 6d18623 | 2014-11-26 17:56:19 -0800 | [diff] [blame] | 386 | if (addr >= var.beg) { |
Kostya Serebryany | edb39c7 | 2013-09-03 13:58:04 +0000 | [diff] [blame] | 387 | 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 Hines | 6d18623 | 2014-11-26 17:56:19 -0800 | [diff] [blame] | 395 | if (addr_end > var.beg) |
Kostya Serebryany | edb39c7 | 2013-09-03 13:58:04 +0000 | [diff] [blame] | 396 | pos_descr = "partially underflows"; |
| 397 | else if (addr >= prev_var_end && |
Stephen Hines | 6d18623 | 2014-11-26 17:56:19 -0800 | [diff] [blame] | 398 | addr - prev_var_end >= var.beg - addr_end) |
Kostya Serebryany | edb39c7 | 2013-09-03 13:58:04 +0000 | [diff] [blame] | 399 | pos_descr = "underflows"; |
| 400 | } |
Stephen Hines | 2d1fdb2 | 2014-05-28 23:58:16 -0700 | [diff] [blame] | 401 | InternalScopedString str(1024); |
Stephen Hines | 6d18623 | 2014-11-26 17:56:19 -0800 | [diff] [blame] | 402 | 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 Serebryany | edb39c7 | 2013-09-03 13:58:04 +0000 | [diff] [blame] | 409 | 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 Hines | 2d1fdb2 | 2014-05-28 23:58:16 -0700 | [diff] [blame] | 413 | str.append("%s <== Memory access at offset %zd %s this variable%s\n", |
| 414 | d.Location(), addr, pos_descr, d.EndLocation()); |
Kostya Serebryany | edb39c7 | 2013-09-03 13:58:04 +0000 | [diff] [blame] | 415 | } else { |
Stephen Hines | 2d1fdb2 | 2014-05-28 23:58:16 -0700 | [diff] [blame] | 416 | str.append("\n"); |
Kostya Serebryany | edb39c7 | 2013-09-03 13:58:04 +0000 | [diff] [blame] | 417 | } |
Stephen Hines | 2d1fdb2 | 2014-05-28 23:58:16 -0700 | [diff] [blame] | 418 | Printf("%s", str.data()); |
Kostya Serebryany | edb39c7 | 2013-09-03 13:58:04 +0000 | [diff] [blame] | 419 | } |
| 420 | |
Stephen Hines | 6d18623 | 2014-11-26 17:56:19 -0800 | [diff] [blame] | 421 | bool 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 Serebryany | edb39c7 | 2013-09-03 13:58:04 +0000 | [diff] [blame] | 447 | |
Alexey Samsonov | e218beb | 2012-08-09 09:06:52 +0000 | [diff] [blame] | 448 | bool DescribeAddressIfStack(uptr addr, uptr access_size) { |
Alexey Samsonov | def1be9 | 2013-03-21 11:23:41 +0000 | [diff] [blame] | 449 | AsanThread *t = FindThreadByStackAddress(addr); |
Alexey Samsonov | e218beb | 2012-08-09 09:06:52 +0000 | [diff] [blame] | 450 | if (!t) return false; |
Kostya Serebryany | d570bb4 | 2013-05-22 14:21:34 +0000 | [diff] [blame] | 451 | |
Kostya Serebryany | 58f5455 | 2012-12-18 07:32:16 +0000 | [diff] [blame] | 452 | Decorator d; |
Stephen Hines | 6d18623 | 2014-11-26 17:56:19 -0800 | [diff] [blame] | 453 | char tname[128]; |
Kostya Serebryany | 58f5455 | 2012-12-18 07:32:16 +0000 | [diff] [blame] | 454 | Printf("%s", d.Location()); |
Stephen Hines | 6d18623 | 2014-11-26 17:56:19 -0800 | [diff] [blame] | 455 | 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 Serebryany | 50f3daa | 2013-03-22 10:36:24 +0000 | [diff] [blame] | 466 | // 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 Hines | 6d18623 | 2014-11-26 17:56:19 -0800 | [diff] [blame] | 473 | #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 Serebryany | 58f5455 | 2012-12-18 07:32:16 +0000 | [diff] [blame] | 480 | Printf("%s", d.EndLocation()); |
Stephen Hines | 6d18623 | 2014-11-26 17:56:19 -0800 | [diff] [blame] | 481 | StackTrace alloca_stack(&access.frame_pc, 1); |
Stephen Hines | 2d1fdb2 | 2014-05-28 23:58:16 -0700 | [diff] [blame] | 482 | alloca_stack.Print(); |
Stephen Hines | 6d18623 | 2014-11-26 17:56:19 -0800 | [diff] [blame] | 483 | |
| 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 Samsonov | e218beb | 2012-08-09 09:06:52 +0000 | [diff] [blame] | 492 | // Report the number of stack objects. |
Kostya Serebryany | 283c296 | 2012-08-28 11:34:40 +0000 | [diff] [blame] | 493 | Printf(" This frame has %zu object(s):\n", n_objects); |
Kostya Serebryany | edb39c7 | 2013-09-03 13:58:04 +0000 | [diff] [blame] | 494 | |
Alexey Samsonov | e218beb | 2012-08-09 09:06:52 +0000 | [diff] [blame] | 495 | // Report all objects in this frame. |
Kostya Serebryany | 89fe564 | 2013-09-03 14:53:02 +0000 | [diff] [blame] | 496 | for (uptr i = 0; i < n_objects; i++) { |
Kostya Serebryany | edb39c7 | 2013-09-03 13:58:04 +0000 | [diff] [blame] | 497 | 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] | 498 | 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] | 499 | PrintAccessAndVarIntersection(vars[i], access.offset, access_size, |
Kostya Serebryany | edb39c7 | 2013-09-03 13:58:04 +0000 | [diff] [blame] | 500 | prev_var_end, next_var_beg); |
Alexey Samsonov | e218beb | 2012-08-09 09:06:52 +0000 | [diff] [blame] | 501 | } |
Kostya Serebryany | 283c296 | 2012-08-28 11:34:40 +0000 | [diff] [blame] | 502 | Printf("HINT: this may be a false positive if your program uses " |
Stephen Hines | 6d18623 | 2014-11-26 17:56:19 -0800 | [diff] [blame] | 503 | "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 Iskhodzhanov | 997454a | 2013-09-10 08:36:21 +0000 | [diff] [blame] | 509 | DescribeThread(t); |
Alexey Samsonov | e218beb | 2012-08-09 09:06:52 +0000 | [diff] [blame] | 510 | return true; |
| 511 | } |
| 512 | |
Alexey Samsonov | 5c153fa | 2012-09-18 07:38:10 +0000 | [diff] [blame] | 513 | static void DescribeAccessToHeapChunk(AsanChunkView chunk, uptr addr, |
| 514 | uptr access_size) { |
Evgeniy Stepanov | 589dcda | 2013-02-05 14:32:03 +0000 | [diff] [blame] | 515 | sptr offset; |
Kostya Serebryany | 58f5455 | 2012-12-18 07:32:16 +0000 | [diff] [blame] | 516 | Decorator d; |
Stephen Hines | 2d1fdb2 | 2014-05-28 23:58:16 -0700 | [diff] [blame] | 517 | InternalScopedString str(4096); |
| 518 | str.append("%s", d.Location()); |
Evgeniy Stepanov | 589dcda | 2013-02-05 14:32:03 +0000 | [diff] [blame] | 519 | if (chunk.AddrIsAtLeft(addr, access_size, &offset)) { |
Stephen Hines | 2d1fdb2 | 2014-05-28 23:58:16 -0700 | [diff] [blame] | 520 | 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] | 521 | } else if (chunk.AddrIsAtRight(addr, access_size, &offset)) { |
Evgeniy Stepanov | 589dcda | 2013-02-05 14:32:03 +0000 | [diff] [blame] | 522 | if (offset < 0) { |
| 523 | addr -= offset; |
| 524 | offset = 0; |
| 525 | } |
Stephen Hines | 2d1fdb2 | 2014-05-28 23:58:16 -0700 | [diff] [blame] | 526 | 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] | 527 | } else if (chunk.AddrIsInside(addr, access_size, &offset)) { |
Stephen Hines | 2d1fdb2 | 2014-05-28 23:58:16 -0700 | [diff] [blame] | 528 | str.append("%p is located %zd bytes inside of", (void*)addr, offset); |
Alexey Samsonov | 5c153fa | 2012-09-18 07:38:10 +0000 | [diff] [blame] | 529 | } else { |
Stephen Hines | 2d1fdb2 | 2014-05-28 23:58:16 -0700 | [diff] [blame] | 530 | str.append("%p is located somewhere around (this is AddressSanitizer bug!)", |
| 531 | (void *)addr); |
Alexey Samsonov | 5c153fa | 2012-09-18 07:38:10 +0000 | [diff] [blame] | 532 | } |
Stephen Hines | 2d1fdb2 | 2014-05-28 23:58:16 -0700 | [diff] [blame] | 533 | 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 Samsonov | 5c153fa | 2012-09-18 07:38:10 +0000 | [diff] [blame] | 537 | } |
| 538 | |
| 539 | void DescribeHeapAddress(uptr addr, uptr access_size) { |
| 540 | AsanChunkView chunk = FindHeapChunkByAddress(addr); |
Alexey Samsonov | d9def29 | 2013-10-14 11:13:54 +0000 | [diff] [blame] | 541 | if (!chunk.IsValid()) { |
| 542 | Printf("AddressSanitizer can not describe address in more detail " |
| 543 | "(wild memory access suspected).\n"); |
| 544 | return; |
| 545 | } |
Alexey Samsonov | 5c153fa | 2012-09-18 07:38:10 +0000 | [diff] [blame] | 546 | DescribeAccessToHeapChunk(chunk, addr, access_size); |
| 547 | CHECK(chunk.AllocTid() != kInvalidTid); |
Alexey Samsonov | def1be9 | 2013-03-21 11:23:41 +0000 | [diff] [blame] | 548 | asanThreadRegistry().CheckLocked(); |
| 549 | AsanThreadContext *alloc_thread = |
| 550 | GetThreadContextByTidLocked(chunk.AllocTid()); |
Stephen Hines | 6d18623 | 2014-11-26 17:56:19 -0800 | [diff] [blame] | 551 | StackTrace alloc_stack = chunk.GetAllocStack(); |
Kostya Serebryany | 716e2f2 | 2012-12-07 15:15:01 +0000 | [diff] [blame] | 552 | char tname[128]; |
Kostya Serebryany | 58f5455 | 2012-12-18 07:32:16 +0000 | [diff] [blame] | 553 | Decorator d; |
Pirama Arumuga Nainar | 799172d | 2016-03-03 15:50:30 -0800 | [diff] [blame^] | 554 | AsanThreadContext *free_thread = nullptr; |
Alexey Samsonov | 5c153fa | 2012-09-18 07:38:10 +0000 | [diff] [blame] | 555 | if (chunk.FreeTid() != kInvalidTid) { |
Timur Iskhodzhanov | 997454a | 2013-09-10 08:36:21 +0000 | [diff] [blame] | 556 | free_thread = GetThreadContextByTidLocked(chunk.FreeTid()); |
Kostya Serebryany | 58f5455 | 2012-12-18 07:32:16 +0000 | [diff] [blame] | 557 | Printf("%sfreed by thread T%d%s here:%s\n", d.Allocation(), |
Alexey Samsonov | def1be9 | 2013-03-21 11:23:41 +0000 | [diff] [blame] | 558 | free_thread->tid, |
Kostya Serebryany | 58f5455 | 2012-12-18 07:32:16 +0000 | [diff] [blame] | 559 | ThreadNameWithParenthesis(free_thread, tname, sizeof(tname)), |
| 560 | d.EndAllocation()); |
Stephen Hines | 6d18623 | 2014-11-26 17:56:19 -0800 | [diff] [blame] | 561 | StackTrace free_stack = chunk.GetFreeStack(); |
Stephen Hines | 2d1fdb2 | 2014-05-28 23:58:16 -0700 | [diff] [blame] | 562 | free_stack.Print(); |
Kostya Serebryany | 58f5455 | 2012-12-18 07:32:16 +0000 | [diff] [blame] | 563 | Printf("%spreviously allocated by thread T%d%s here:%s\n", |
Alexey Samsonov | def1be9 | 2013-03-21 11:23:41 +0000 | [diff] [blame] | 564 | d.Allocation(), alloc_thread->tid, |
Kostya Serebryany | 58f5455 | 2012-12-18 07:32:16 +0000 | [diff] [blame] | 565 | ThreadNameWithParenthesis(alloc_thread, tname, sizeof(tname)), |
| 566 | d.EndAllocation()); |
Alexey Samsonov | 5c153fa | 2012-09-18 07:38:10 +0000 | [diff] [blame] | 567 | } else { |
Kostya Serebryany | 58f5455 | 2012-12-18 07:32:16 +0000 | [diff] [blame] | 568 | Printf("%sallocated by thread T%d%s here:%s\n", d.Allocation(), |
Alexey Samsonov | def1be9 | 2013-03-21 11:23:41 +0000 | [diff] [blame] | 569 | alloc_thread->tid, |
Kostya Serebryany | 58f5455 | 2012-12-18 07:32:16 +0000 | [diff] [blame] | 570 | ThreadNameWithParenthesis(alloc_thread, tname, sizeof(tname)), |
| 571 | d.EndAllocation()); |
Alexey Samsonov | 5c153fa | 2012-09-18 07:38:10 +0000 | [diff] [blame] | 572 | } |
Stephen Hines | 2d1fdb2 | 2014-05-28 23:58:16 -0700 | [diff] [blame] | 573 | alloc_stack.Print(); |
Timur Iskhodzhanov | 997454a | 2013-09-10 08:36:21 +0000 | [diff] [blame] | 574 | DescribeThread(GetCurrentThread()); |
| 575 | if (free_thread) |
| 576 | DescribeThread(free_thread); |
| 577 | DescribeThread(alloc_thread); |
Alexey Samsonov | 5c153fa | 2012-09-18 07:38:10 +0000 | [diff] [blame] | 578 | } |
| 579 | |
Pirama Arumuga Nainar | cdce50b | 2015-07-01 12:26:56 -0700 | [diff] [blame] | 580 | static void DescribeAddress(uptr addr, uptr access_size, const char *bug_type) { |
Alexey Samsonov | e218beb | 2012-08-09 09:06:52 +0000 | [diff] [blame] | 581 | // Check if this is shadow or shadow gap. |
| 582 | if (DescribeAddressIfShadow(addr)) |
| 583 | return; |
| 584 | CHECK(AddrIsInMem(addr)); |
Pirama Arumuga Nainar | cdce50b | 2015-07-01 12:26:56 -0700 | [diff] [blame] | 585 | if (DescribeAddressIfGlobal(addr, access_size, bug_type)) |
Alexey Samsonov | e218beb | 2012-08-09 09:06:52 +0000 | [diff] [blame] | 586 | return; |
| 587 | if (DescribeAddressIfStack(addr, access_size)) |
| 588 | return; |
| 589 | // Assume it is a heap address. |
| 590 | DescribeHeapAddress(addr, access_size); |
| 591 | } |
| 592 | |
Alexey Samsonov | 71b42c9 | 2012-09-05 07:37:15 +0000 | [diff] [blame] | 593 | // ------------------- Thread description -------------------- {{{1 |
| 594 | |
Alexey Samsonov | def1be9 | 2013-03-21 11:23:41 +0000 | [diff] [blame] | 595 | void DescribeThread(AsanThreadContext *context) { |
| 596 | CHECK(context); |
| 597 | asanThreadRegistry().CheckLocked(); |
Alexey Samsonov | 71b42c9 | 2012-09-05 07:37:15 +0000 | [diff] [blame] | 598 | // No need to announce the main thread. |
Alexey Samsonov | def1be9 | 2013-03-21 11:23:41 +0000 | [diff] [blame] | 599 | if (context->tid == 0 || context->announced) { |
Alexey Samsonov | 71b42c9 | 2012-09-05 07:37:15 +0000 | [diff] [blame] | 600 | return; |
| 601 | } |
Alexey Samsonov | def1be9 | 2013-03-21 11:23:41 +0000 | [diff] [blame] | 602 | context->announced = true; |
Kostya Serebryany | 716e2f2 | 2012-12-07 15:15:01 +0000 | [diff] [blame] | 603 | char tname[128]; |
Stephen Hines | 2d1fdb2 | 2014-05-28 23:58:16 -0700 | [diff] [blame] | 604 | InternalScopedString str(1024); |
| 605 | str.append("Thread T%d%s", context->tid, |
| 606 | ThreadNameWithParenthesis(context->tid, tname, sizeof(tname))); |
Pirama Arumuga Nainar | 259f706 | 2015-05-06 11:49:53 -0700 | [diff] [blame] | 607 | if (context->parent_tid == kInvalidTid) { |
| 608 | str.append(" created by unknown thread\n"); |
| 609 | Printf("%s", str.data()); |
| 610 | return; |
| 611 | } |
Stephen Hines | 2d1fdb2 | 2014-05-28 23:58:16 -0700 | [diff] [blame] | 612 | 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 Hines | 6d18623 | 2014-11-26 17:56:19 -0800 | [diff] [blame] | 616 | StackDepotGet(context->stack_id).Print(); |
Alexey Samsonov | 71b42c9 | 2012-09-05 07:37:15 +0000 | [diff] [blame] | 617 | // Recursively described parent thread if needed. |
| 618 | if (flags()->print_full_thread_history) { |
Alexey Samsonov | def1be9 | 2013-03-21 11:23:41 +0000 | [diff] [blame] | 619 | AsanThreadContext *parent_context = |
| 620 | GetThreadContextByTidLocked(context->parent_tid); |
| 621 | DescribeThread(parent_context); |
Alexey Samsonov | 71b42c9 | 2012-09-05 07:37:15 +0000 | [diff] [blame] | 622 | } |
| 623 | } |
| 624 | |
Alexey Samsonov | e218beb | 2012-08-09 09:06:52 +0000 | [diff] [blame] | 625 | // -------------------- Different kinds of reports ----------------- {{{1 |
| 626 | |
Alexey Samsonov | 9873792 | 2012-08-10 15:13:05 +0000 | [diff] [blame] | 627 | // Use ScopedInErrorReport to run common actions just before and |
| 628 | // immediately after printing error report. |
| 629 | class ScopedInErrorReport { |
| 630 | public: |
Pirama Arumuga Nainar | 799172d | 2016-03-03 15:50:30 -0800 | [diff] [blame^] | 631 | 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 Samsonov | 9873792 | 2012-08-10 15:13:05 +0000 | [diff] [blame] | 658 | // 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 Nainar | 799172d | 2016-03-03 15:50:30 -0800 | [diff] [blame^] | 660 | // they are effectively no-returns. |
| 661 | |
Stephen Hines | 6d18623 | 2014-11-26 17:56:19 -0800 | [diff] [blame] | 662 | Report("AddressSanitizer: while reporting a bug found another one. " |
Pirama Arumuga Nainar | 799172d | 2016-03-03 15:50:30 -0800 | [diff] [blame^] | 663 | "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 Samsonov | f882247 | 2013-02-20 13:54:32 +0000 | [diff] [blame] | 669 | // 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] | 670 | // Die() to bypass any additional checks. |
Pirama Arumuga Nainar | 799172d | 2016-03-03 15:50:30 -0800 | [diff] [blame^] | 671 | 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 Samsonov | 9873792 | 2012-08-10 15:13:05 +0000 | [diff] [blame] | 676 | } |
Pirama Arumuga Nainar | 799172d | 2016-03-03 15:50:30 -0800 | [diff] [blame^] | 677 | |
| 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 Hines | 6d18623 | 2014-11-26 17:56:19 -0800 | [diff] [blame] | 718 | if (report) report_data = *report; |
| 719 | report_happened = true; |
Alexey Samsonov | 6a08d29 | 2012-12-07 22:01:28 +0000 | [diff] [blame] | 720 | ASAN_ON_ERROR(); |
Alexey Samsonov | 7ed46ff | 2013-04-05 07:30:29 +0000 | [diff] [blame] | 721 | // 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 Samsonov | def1be9 | 2013-03-21 11:23:41 +0000 | [diff] [blame] | 724 | // recursive reports. |
| 725 | asanThreadRegistry().Lock(); |
Alexey Samsonov | 7ed46ff | 2013-04-05 07:30:29 +0000 | [diff] [blame] | 726 | CommonSanitizerReportMutex.Lock(); |
Pirama Arumuga Nainar | 799172d | 2016-03-03 15:50:30 -0800 | [diff] [blame^] | 727 | reporting_thread_tid_ = GetCurrentTidOrInvalid(); |
Kostya Serebryany | 283c296 | 2012-08-28 11:34:40 +0000 | [diff] [blame] | 728 | Printf("====================================================" |
Alexey Samsonov | 62e2709 | 2012-09-17 08:02:19 +0000 | [diff] [blame] | 729 | "=============\n"); |
Alexey Samsonov | 9873792 | 2012-08-10 15:13:05 +0000 | [diff] [blame] | 730 | } |
Pirama Arumuga Nainar | 799172d | 2016-03-03 15:50:30 -0800 | [diff] [blame^] | 731 | |
| 732 | static StaticSpinMutex lock_; |
| 733 | static u32 reporting_thread_tid_; |
| 734 | bool halt_on_error_; |
Alexey Samsonov | 9873792 | 2012-08-10 15:13:05 +0000 | [diff] [blame] | 735 | }; |
| 736 | |
Pirama Arumuga Nainar | 799172d | 2016-03-03 15:50:30 -0800 | [diff] [blame^] | 737 | StaticSpinMutex ScopedInErrorReport::lock_; |
| 738 | u32 ScopedInErrorReport::reporting_thread_tid_; |
| 739 | |
Stephen Hines | 86277eb | 2015-03-23 12:06:32 -0700 | [diff] [blame] | 740 | void ReportStackOverflow(const SignalContext &sig) { |
Alexey Samsonov | 9873792 | 2012-08-10 15:13:05 +0000 | [diff] [blame] | 741 | ScopedInErrorReport in_report; |
Kostya Serebryany | 58f5455 | 2012-12-18 07:32:16 +0000 | [diff] [blame] | 742 | Decorator d; |
| 743 | Printf("%s", d.Warning()); |
Stephen Hines | 2d1fdb2 | 2014-05-28 23:58:16 -0700 | [diff] [blame] | 744 | Report( |
| 745 | "ERROR: AddressSanitizer: stack-overflow on address %p" |
Stephen Hines | 6d18623 | 2014-11-26 17:56:19 -0800 | [diff] [blame] | 746 | " (pc %p bp %p sp %p T%d)\n", |
Stephen Hines | 86277eb | 2015-03-23 12:06:32 -0700 | [diff] [blame] | 747 | (void *)sig.addr, (void *)sig.pc, (void *)sig.bp, (void *)sig.sp, |
Stephen Hines | 2d1fdb2 | 2014-05-28 23:58:16 -0700 | [diff] [blame] | 748 | GetCurrentTidOrInvalid()); |
Kostya Serebryany | 58f5455 | 2012-12-18 07:32:16 +0000 | [diff] [blame] | 749 | Printf("%s", d.EndWarning()); |
Stephen Hines | 86277eb | 2015-03-23 12:06:32 -0700 | [diff] [blame] | 750 | GET_STACK_TRACE_SIGNAL(sig); |
Stephen Hines | 2d1fdb2 | 2014-05-28 23:58:16 -0700 | [diff] [blame] | 751 | stack.Print(); |
| 752 | ReportErrorSummary("stack-overflow", &stack); |
| 753 | } |
| 754 | |
Pirama Arumuga Nainar | 799172d | 2016-03-03 15:50:30 -0800 | [diff] [blame^] | 755 | void ReportDeadlySignal(const char *description, const SignalContext &sig) { |
| 756 | ScopedInErrorReport in_report(/*report*/nullptr, /*fatal*/true); |
Stephen Hines | 2d1fdb2 | 2014-05-28 23:58:16 -0700 | [diff] [blame] | 757 | Decorator d; |
| 758 | Printf("%s", d.Warning()); |
| 759 | Report( |
Stephen Hines | 6d18623 | 2014-11-26 17:56:19 -0800 | [diff] [blame] | 760 | "ERROR: AddressSanitizer: %s on unknown address %p" |
| 761 | " (pc %p bp %p sp %p T%d)\n", |
Stephen Hines | 86277eb | 2015-03-23 12:06:32 -0700 | [diff] [blame] | 762 | description, (void *)sig.addr, (void *)sig.pc, (void *)sig.bp, |
| 763 | (void *)sig.sp, GetCurrentTidOrInvalid()); |
| 764 | if (sig.pc < GetPageSizeCached()) { |
Stephen Hines | 6d18623 | 2014-11-26 17:56:19 -0800 | [diff] [blame] | 765 | Report("Hint: pc points to the zero page.\n"); |
| 766 | } |
Stephen Hines | 2d1fdb2 | 2014-05-28 23:58:16 -0700 | [diff] [blame] | 767 | Printf("%s", d.EndWarning()); |
Stephen Hines | 86277eb | 2015-03-23 12:06:32 -0700 | [diff] [blame] | 768 | GET_STACK_TRACE_SIGNAL(sig); |
Stephen Hines | 2d1fdb2 | 2014-05-28 23:58:16 -0700 | [diff] [blame] | 769 | stack.Print(); |
Stephen Hines | 86277eb | 2015-03-23 12:06:32 -0700 | [diff] [blame] | 770 | MaybeDumpInstructionBytes(sig.pc); |
Alexey Samsonov | d9def29 | 2013-10-14 11:13:54 +0000 | [diff] [blame] | 771 | Printf("AddressSanitizer can not provide additional info.\n"); |
Pirama Arumuga Nainar | 799172d | 2016-03-03 15:50:30 -0800 | [diff] [blame^] | 772 | ReportErrorSummary(description, &stack); |
Alexey Samsonov | 7354509 | 2012-08-09 07:40:58 +0000 | [diff] [blame] | 773 | } |
| 774 | |
Stephen Hines | 6d18623 | 2014-11-26 17:56:19 -0800 | [diff] [blame] | 775 | void ReportDoubleFree(uptr addr, BufferedStackTrace *free_stack) { |
Alexey Samsonov | 9873792 | 2012-08-10 15:13:05 +0000 | [diff] [blame] | 776 | ScopedInErrorReport in_report; |
Kostya Serebryany | 58f5455 | 2012-12-18 07:32:16 +0000 | [diff] [blame] | 777 | Decorator d; |
| 778 | Printf("%s", d.Warning()); |
Kostya Serebryany | a89a35a | 2013-03-26 08:01:37 +0000 | [diff] [blame] | 779 | 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 Serebryany | 58f5455 | 2012-12-18 07:32:16 +0000 | [diff] [blame] | 785 | Printf("%s", d.EndWarning()); |
Alexey Samsonov | 1b17f5b | 2013-11-13 14:46:58 +0000 | [diff] [blame] | 786 | CHECK_GT(free_stack->size, 0); |
| 787 | 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] | 788 | stack.Print(); |
Alexey Samsonov | f7c1d18 | 2012-08-09 08:15:46 +0000 | [diff] [blame] | 789 | DescribeHeapAddress(addr, 1); |
Alexey Samsonov | 1b17f5b | 2013-11-13 14:46:58 +0000 | [diff] [blame] | 790 | ReportErrorSummary("double-free", &stack); |
Alexey Samsonov | f7c1d18 | 2012-08-09 08:15:46 +0000 | [diff] [blame] | 791 | } |
| 792 | |
Stephen Hines | 6d18623 | 2014-11-26 17:56:19 -0800 | [diff] [blame] | 793 | void 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 Nainar | 799172d | 2016-03-03 15:50:30 -0800 | [diff] [blame^] | 813 | Report("HINT: if you don't care about these errors you may set " |
Stephen Hines | 6d18623 | 2014-11-26 17:56:19 -0800 | [diff] [blame] | 814 | "ASAN_OPTIONS=new_delete_type_mismatch=0\n"); |
| 815 | } |
| 816 | |
| 817 | void ReportFreeNotMalloced(uptr addr, BufferedStackTrace *free_stack) { |
Alexey Samsonov | 9873792 | 2012-08-10 15:13:05 +0000 | [diff] [blame] | 818 | ScopedInErrorReport in_report; |
Kostya Serebryany | 58f5455 | 2012-12-18 07:32:16 +0000 | [diff] [blame] | 819 | Decorator d; |
| 820 | Printf("%s", d.Warning()); |
Kostya Serebryany | a89a35a | 2013-03-26 08:01:37 +0000 | [diff] [blame] | 821 | char tname[128]; |
| 822 | u32 curr_tid = GetCurrentTidOrInvalid(); |
Kostya Serebryany | 69d8ede | 2012-10-15 13:04:58 +0000 | [diff] [blame] | 823 | Report("ERROR: AddressSanitizer: attempting free on address " |
Kostya Serebryany | a89a35a | 2013-03-26 08:01:37 +0000 | [diff] [blame] | 824 | "which was not malloc()-ed: %p in thread T%d%s\n", addr, |
| 825 | curr_tid, ThreadNameWithParenthesis(curr_tid, tname, sizeof(tname))); |
Kostya Serebryany | 58f5455 | 2012-12-18 07:32:16 +0000 | [diff] [blame] | 826 | Printf("%s", d.EndWarning()); |
Alexey Samsonov | 1b17f5b | 2013-11-13 14:46:58 +0000 | [diff] [blame] | 827 | CHECK_GT(free_stack->size, 0); |
| 828 | 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] | 829 | stack.Print(); |
Alexey Samsonov | 9873792 | 2012-08-10 15:13:05 +0000 | [diff] [blame] | 830 | DescribeHeapAddress(addr, 1); |
Alexey Samsonov | 1b17f5b | 2013-11-13 14:46:58 +0000 | [diff] [blame] | 831 | ReportErrorSummary("bad-free", &stack); |
Alexey Samsonov | f7c1d18 | 2012-08-09 08:15:46 +0000 | [diff] [blame] | 832 | } |
| 833 | |
Stephen Hines | 6d18623 | 2014-11-26 17:56:19 -0800 | [diff] [blame] | 834 | void ReportAllocTypeMismatch(uptr addr, BufferedStackTrace *free_stack, |
Kostya Serebryany | fe6d916 | 2012-12-21 08:53:59 +0000 | [diff] [blame] | 835 | 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 Samsonov | 1b17f5b | 2013-11-13 14:46:58 +0000 | [diff] [blame] | 848 | CHECK_GT(free_stack->size, 0); |
| 849 | 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] | 850 | stack.Print(); |
Kostya Serebryany | fe6d916 | 2012-12-21 08:53:59 +0000 | [diff] [blame] | 851 | DescribeHeapAddress(addr, 1); |
Alexey Samsonov | 1b17f5b | 2013-11-13 14:46:58 +0000 | [diff] [blame] | 852 | ReportErrorSummary("alloc-dealloc-mismatch", &stack); |
Pirama Arumuga Nainar | 799172d | 2016-03-03 15:50:30 -0800 | [diff] [blame^] | 853 | Report("HINT: if you don't care about these errors you may set " |
Kostya Serebryany | fe6d916 | 2012-12-21 08:53:59 +0000 | [diff] [blame] | 854 | "ASAN_OPTIONS=alloc_dealloc_mismatch=0\n"); |
| 855 | } |
| 856 | |
Stephen Hines | 6d18623 | 2014-11-26 17:56:19 -0800 | [diff] [blame] | 857 | void ReportMallocUsableSizeNotOwned(uptr addr, BufferedStackTrace *stack) { |
Alexey Samsonov | 9873792 | 2012-08-10 15:13:05 +0000 | [diff] [blame] | 858 | ScopedInErrorReport in_report; |
Kostya Serebryany | 58f5455 | 2012-12-18 07:32:16 +0000 | [diff] [blame] | 859 | Decorator d; |
| 860 | Printf("%s", d.Warning()); |
Kostya Serebryany | 69d8ede | 2012-10-15 13:04:58 +0000 | [diff] [blame] | 861 | Report("ERROR: AddressSanitizer: attempting to call " |
Alexey Samsonov | f7c1d18 | 2012-08-09 08:15:46 +0000 | [diff] [blame] | 862 | "malloc_usable_size() for pointer which is " |
| 863 | "not owned: %p\n", addr); |
Kostya Serebryany | 58f5455 | 2012-12-18 07:32:16 +0000 | [diff] [blame] | 864 | Printf("%s", d.EndWarning()); |
Stephen Hines | 2d1fdb2 | 2014-05-28 23:58:16 -0700 | [diff] [blame] | 865 | stack->Print(); |
Alexey Samsonov | f7c1d18 | 2012-08-09 08:15:46 +0000 | [diff] [blame] | 866 | DescribeHeapAddress(addr, 1); |
Alexey Samsonov | 2fb0872 | 2013-11-01 17:02:14 +0000 | [diff] [blame] | 867 | ReportErrorSummary("bad-malloc_usable_size", stack); |
Alexey Samsonov | f7c1d18 | 2012-08-09 08:15:46 +0000 | [diff] [blame] | 868 | } |
| 869 | |
Stephen Hines | 6d18623 | 2014-11-26 17:56:19 -0800 | [diff] [blame] | 870 | void ReportSanitizerGetAllocatedSizeNotOwned(uptr addr, |
| 871 | BufferedStackTrace *stack) { |
Alexey Samsonov | 9873792 | 2012-08-10 15:13:05 +0000 | [diff] [blame] | 872 | ScopedInErrorReport in_report; |
Kostya Serebryany | 58f5455 | 2012-12-18 07:32:16 +0000 | [diff] [blame] | 873 | Decorator d; |
| 874 | Printf("%s", d.Warning()); |
Kostya Serebryany | 69d8ede | 2012-10-15 13:04:58 +0000 | [diff] [blame] | 875 | Report("ERROR: AddressSanitizer: attempting to call " |
Stephen Hines | 6a211c5 | 2014-07-21 00:49:56 -0700 | [diff] [blame] | 876 | "__sanitizer_get_allocated_size() for pointer which is " |
Alexey Samsonov | f7c1d18 | 2012-08-09 08:15:46 +0000 | [diff] [blame] | 877 | "not owned: %p\n", addr); |
Kostya Serebryany | 58f5455 | 2012-12-18 07:32:16 +0000 | [diff] [blame] | 878 | Printf("%s", d.EndWarning()); |
Stephen Hines | 2d1fdb2 | 2014-05-28 23:58:16 -0700 | [diff] [blame] | 879 | stack->Print(); |
Alexey Samsonov | f7c1d18 | 2012-08-09 08:15:46 +0000 | [diff] [blame] | 880 | DescribeHeapAddress(addr, 1); |
Stephen Hines | 6a211c5 | 2014-07-21 00:49:56 -0700 | [diff] [blame] | 881 | ReportErrorSummary("bad-__sanitizer_get_allocated_size", stack); |
Alexey Samsonov | f7c1d18 | 2012-08-09 08:15:46 +0000 | [diff] [blame] | 882 | } |
| 883 | |
Stephen Hines | 6d18623 | 2014-11-26 17:56:19 -0800 | [diff] [blame] | 884 | void ReportStringFunctionMemoryRangesOverlap(const char *function, |
| 885 | const char *offset1, uptr length1, |
| 886 | const char *offset2, uptr length2, |
| 887 | BufferedStackTrace *stack) { |
Alexey Samsonov | 9873792 | 2012-08-10 15:13:05 +0000 | [diff] [blame] | 888 | ScopedInErrorReport in_report; |
Kostya Serebryany | 58f5455 | 2012-12-18 07:32:16 +0000 | [diff] [blame] | 889 | Decorator d; |
Kostya Serebryany | 2673fd8 | 2013-02-06 12:36:49 +0000 | [diff] [blame] | 890 | char bug_type[100]; |
| 891 | internal_snprintf(bug_type, sizeof(bug_type), "%s-param-overlap", function); |
Kostya Serebryany | 58f5455 | 2012-12-18 07:32:16 +0000 | [diff] [blame] | 892 | Printf("%s", d.Warning()); |
Kostya Serebryany | 2673fd8 | 2013-02-06 12:36:49 +0000 | [diff] [blame] | 893 | Report("ERROR: AddressSanitizer: %s: " |
Alexey Samsonov | 487fee7 | 2012-08-09 08:32:33 +0000 | [diff] [blame] | 894 | "memory ranges [%p,%p) and [%p, %p) overlap\n", \ |
Kostya Serebryany | 2673fd8 | 2013-02-06 12:36:49 +0000 | [diff] [blame] | 895 | bug_type, offset1, offset1 + length1, offset2, offset2 + length2); |
Kostya Serebryany | 58f5455 | 2012-12-18 07:32:16 +0000 | [diff] [blame] | 896 | Printf("%s", d.EndWarning()); |
Stephen Hines | 2d1fdb2 | 2014-05-28 23:58:16 -0700 | [diff] [blame] | 897 | stack->Print(); |
Pirama Arumuga Nainar | cdce50b | 2015-07-01 12:26:56 -0700 | [diff] [blame] | 898 | DescribeAddress((uptr)offset1, length1, bug_type); |
| 899 | DescribeAddress((uptr)offset2, length2, bug_type); |
Alexey Samsonov | 2fb0872 | 2013-11-01 17:02:14 +0000 | [diff] [blame] | 900 | ReportErrorSummary(bug_type, stack); |
Alexey Samsonov | 487fee7 | 2012-08-09 08:32:33 +0000 | [diff] [blame] | 901 | } |
Alexey Samsonov | f7c1d18 | 2012-08-09 08:15:46 +0000 | [diff] [blame] | 902 | |
Stephen Hines | 2d1fdb2 | 2014-05-28 23:58:16 -0700 | [diff] [blame] | 903 | void ReportStringFunctionSizeOverflow(uptr offset, uptr size, |
Stephen Hines | 6d18623 | 2014-11-26 17:56:19 -0800 | [diff] [blame] | 904 | BufferedStackTrace *stack) { |
Stephen Hines | 2d1fdb2 | 2014-05-28 23:58:16 -0700 | [diff] [blame] | 905 | 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 Nainar | cdce50b | 2015-07-01 12:26:56 -0700 | [diff] [blame] | 912 | DescribeAddress(offset, size, bug_type); |
Stephen Hines | 2d1fdb2 | 2014-05-28 23:58:16 -0700 | [diff] [blame] | 913 | ReportErrorSummary(bug_type, stack); |
| 914 | } |
| 915 | |
| 916 | void ReportBadParamsToAnnotateContiguousContainer(uptr beg, uptr end, |
| 917 | uptr old_mid, uptr new_mid, |
Stephen Hines | 6d18623 | 2014-11-26 17:56:19 -0800 | [diff] [blame] | 918 | BufferedStackTrace *stack) { |
Stephen Hines | 2d1fdb2 | 2014-05-28 23:58:16 -0700 | [diff] [blame] | 919 | 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 Hines | 86277eb | 2015-03-23 12:06:32 -0700 | [diff] [blame] | 927 | uptr granularity = SHADOW_GRANULARITY; |
| 928 | if (!IsAligned(beg, granularity)) |
| 929 | Report("ERROR: beg is not aligned by %d\n", granularity); |
Stephen Hines | 2d1fdb2 | 2014-05-28 23:58:16 -0700 | [diff] [blame] | 930 | stack->Print(); |
| 931 | ReportErrorSummary("bad-__sanitizer_annotate_contiguous_container", stack); |
| 932 | } |
| 933 | |
Stephen Hines | 6a211c5 | 2014-07-21 00:49:56 -0700 | [diff] [blame] | 934 | void ReportODRViolation(const __asan_global *g1, u32 stack_id1, |
| 935 | const __asan_global *g2, u32 stack_id2) { |
Stephen Hines | 2d1fdb2 | 2014-05-28 23:58:16 -0700 | [diff] [blame] | 936 | 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 Hines | 6a211c5 | 2014-07-21 00:49:56 -0700 | [diff] [blame] | 941 | InternalScopedString g1_loc(256), g2_loc(256); |
| 942 | PrintGlobalLocation(&g1_loc, *g1); |
| 943 | PrintGlobalLocation(&g2_loc, *g2); |
Stephen Hines | 6d18623 | 2014-11-26 17:56:19 -0800 | [diff] [blame] | 944 | 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 Hines | 6a211c5 | 2014-07-21 00:49:56 -0700 | [diff] [blame] | 948 | if (stack_id1 && stack_id2) { |
| 949 | Printf("These globals were registered at these points:\n"); |
| 950 | Printf(" [1]:\n"); |
Stephen Hines | 6d18623 | 2014-11-26 17:56:19 -0800 | [diff] [blame] | 951 | StackDepotGet(stack_id1).Print(); |
Stephen Hines | 6a211c5 | 2014-07-21 00:49:56 -0700 | [diff] [blame] | 952 | Printf(" [2]:\n"); |
Stephen Hines | 6d18623 | 2014-11-26 17:56:19 -0800 | [diff] [blame] | 953 | StackDepotGet(stack_id2).Print(); |
Stephen Hines | 6a211c5 | 2014-07-21 00:49:56 -0700 | [diff] [blame] | 954 | } |
Pirama Arumuga Nainar | 799172d | 2016-03-03 15:50:30 -0800 | [diff] [blame^] | 955 | Report("HINT: if you don't care about these errors you may set " |
Stephen Hines | 2d1fdb2 | 2014-05-28 23:58:16 -0700 | [diff] [blame] | 956 | "ASAN_OPTIONS=detect_odr_violation=0\n"); |
Stephen Hines | 6d18623 | 2014-11-26 17:56:19 -0800 | [diff] [blame] | 957 | 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 Hines | 2d1fdb2 | 2014-05-28 23:58:16 -0700 | [diff] [blame] | 961 | } |
| 962 | |
| 963 | // ----------------------- CheckForInvalidPointerPair ----------- {{{1 |
| 964 | static NOINLINE void |
| 965 | ReportInvalidPointerPair(uptr pc, uptr bp, uptr sp, uptr a1, uptr a2) { |
| 966 | ScopedInErrorReport in_report; |
Pirama Arumuga Nainar | cdce50b | 2015-07-01 12:26:56 -0700 | [diff] [blame] | 967 | const char *bug_type = "invalid-pointer-pair"; |
Stephen Hines | 2d1fdb2 | 2014-05-28 23:58:16 -0700 | [diff] [blame] | 968 | 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 Nainar | cdce50b | 2015-07-01 12:26:56 -0700 | [diff] [blame] | 974 | DescribeAddress(a1, 1, bug_type); |
| 975 | DescribeAddress(a2, 1, bug_type); |
| 976 | ReportErrorSummary(bug_type, &stack); |
Stephen Hines | 2d1fdb2 | 2014-05-28 23:58:16 -0700 | [diff] [blame] | 977 | } |
| 978 | |
| 979 | static 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 Samsonov | 663c501 | 2012-08-09 12:15:40 +0000 | [diff] [blame] | 992 | // ----------------------- Mac-specific reports ----------------- {{{1 |
| 993 | |
Stephen Hines | 6d18623 | 2014-11-26 17:56:19 -0800 | [diff] [blame] | 994 | void ReportMacMzReallocUnknown(uptr addr, uptr zone_ptr, const char *zone_name, |
| 995 | BufferedStackTrace *stack) { |
Alexey Samsonov | 9873792 | 2012-08-10 15:13:05 +0000 | [diff] [blame] | 996 | ScopedInErrorReport in_report; |
Kostya Serebryany | 283c296 | 2012-08-28 11:34:40 +0000 | [diff] [blame] | 997 | Printf("mz_realloc(%p) -- attempting to realloc unallocated memory.\n" |
Alexey Samsonov | 663c501 | 2012-08-09 12:15:40 +0000 | [diff] [blame] | 998 | "This is an unrecoverable problem, exiting now.\n", |
| 999 | addr); |
| 1000 | PrintZoneForPointer(addr, zone_ptr, zone_name); |
Stephen Hines | 2d1fdb2 | 2014-05-28 23:58:16 -0700 | [diff] [blame] | 1001 | stack->Print(); |
Alexey Samsonov | 9873792 | 2012-08-10 15:13:05 +0000 | [diff] [blame] | 1002 | DescribeHeapAddress(addr, 1); |
Alexey Samsonov | 663c501 | 2012-08-09 12:15:40 +0000 | [diff] [blame] | 1003 | } |
| 1004 | |
Pirama Arumuga Nainar | 799172d | 2016-03-03 15:50:30 -0800 | [diff] [blame^] | 1005 | // -------------- SuppressErrorReport -------------- {{{1 |
| 1006 | // Avoid error reports duplicating for ASan recover mode. |
| 1007 | static 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 Samsonov | c98570b | 2012-08-09 10:56:57 +0000 | [diff] [blame] | 1017 | } |
| 1018 | |
Pirama Arumuga Nainar | 799172d | 2016-03-03 15:50:30 -0800 | [diff] [blame^] | 1019 | void 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 Hines | 86277eb | 2015-03-23 12:06:32 -0700 | [diff] [blame] | 1022 | ENABLE_FRAME_POINTER; |
| 1023 | |
Pirama Arumuga Nainar | 7c91505 | 2015-04-08 08:58:29 -0700 | [diff] [blame] | 1024 | // 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 Samsonov | 9873792 | 2012-08-10 15:13:05 +0000 | [diff] [blame] | 1033 | // Determine the error type. |
Alexey Samsonov | c98570b | 2012-08-09 10:56:57 +0000 | [diff] [blame] | 1034 | 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 Hines | 6d18623 | 2014-11-26 17:56:19 -0800 | [diff] [blame] | 1046 | case kAsanArrayCookieMagic: |
Alexey Samsonov | c98570b | 2012-08-09 10:56:57 +0000 | [diff] [blame] | 1047 | 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 Serebryany | 3945c58 | 2012-08-21 14:10:25 +0000 | [diff] [blame] | 1055 | case kAsanInitializationOrderMagic: |
| 1056 | bug_descr = "initialization-order-fiasco"; |
| 1057 | break; |
Alexey Samsonov | c98570b | 2012-08-09 10:56:57 +0000 | [diff] [blame] | 1058 | 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 Hines | 2d1fdb2 | 2014-05-28 23:58:16 -0700 | [diff] [blame] | 1069 | case kAsanContiguousContainerOOBMagic: |
| 1070 | bug_descr = "container-overflow"; |
| 1071 | break; |
Alexey Samsonov | d4b5db8 | 2012-12-04 01:38:15 +0000 | [diff] [blame] | 1072 | case kAsanStackUseAfterScopeMagic: |
| 1073 | bug_descr = "stack-use-after-scope"; |
| 1074 | break; |
Alexey Samsonov | c98570b | 2012-08-09 10:56:57 +0000 | [diff] [blame] | 1075 | case kAsanGlobalRedzoneMagic: |
| 1076 | bug_descr = "global-buffer-overflow"; |
| 1077 | break; |
Stephen Hines | 6d18623 | 2014-11-26 17:56:19 -0800 | [diff] [blame] | 1078 | case kAsanIntraObjectRedzone: |
| 1079 | bug_descr = "intra-object-overflow"; |
| 1080 | break; |
Stephen Hines | 86277eb | 2015-03-23 12:06:32 -0700 | [diff] [blame] | 1081 | case kAsanAllocaLeftMagic: |
| 1082 | case kAsanAllocaRightMagic: |
| 1083 | bug_descr = "dynamic-stack-buffer-overflow"; |
| 1084 | break; |
Alexey Samsonov | c98570b | 2012-08-09 10:56:57 +0000 | [diff] [blame] | 1085 | } |
| 1086 | } |
Stephen Hines | 6d18623 | 2014-11-26 17:56:19 -0800 | [diff] [blame] | 1087 | |
| 1088 | ReportData report = { pc, sp, bp, addr, (bool)is_write, access_size, |
| 1089 | bug_descr }; |
Pirama Arumuga Nainar | 799172d | 2016-03-03 15:50:30 -0800 | [diff] [blame^] | 1090 | ScopedInErrorReport in_report(&report, fatal); |
Stephen Hines | 6d18623 | 2014-11-26 17:56:19 -0800 | [diff] [blame] | 1091 | |
Kostya Serebryany | 58f5455 | 2012-12-18 07:32:16 +0000 | [diff] [blame] | 1092 | Decorator d; |
| 1093 | Printf("%s", d.Warning()); |
Kostya Serebryany | 69d8ede | 2012-10-15 13:04:58 +0000 | [diff] [blame] | 1094 | Report("ERROR: AddressSanitizer: %s on address " |
Stephen Hines | 6d18623 | 2014-11-26 17:56:19 -0800 | [diff] [blame] | 1095 | "%p at pc %p bp %p sp %p\n", |
Alexey Samsonov | c98570b | 2012-08-09 10:56:57 +0000 | [diff] [blame] | 1096 | bug_descr, (void*)addr, pc, bp, sp); |
Kostya Serebryany | 58f5455 | 2012-12-18 07:32:16 +0000 | [diff] [blame] | 1097 | Printf("%s", d.EndWarning()); |
Alexey Samsonov | c98570b | 2012-08-09 10:56:57 +0000 | [diff] [blame] | 1098 | |
Alexey Samsonov | 89c1384 | 2013-03-20 09:23:28 +0000 | [diff] [blame] | 1099 | u32 curr_tid = GetCurrentTidOrInvalid(); |
Kostya Serebryany | 716e2f2 | 2012-12-07 15:15:01 +0000 | [diff] [blame] | 1100 | char tname[128]; |
Kostya Serebryany | 58f5455 | 2012-12-18 07:32:16 +0000 | [diff] [blame] | 1101 | 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 Samsonov | c98570b | 2012-08-09 10:56:57 +0000 | [diff] [blame] | 1107 | |
Kostya Serebryany | a30c8f9 | 2012-12-13 09:34:23 +0000 | [diff] [blame] | 1108 | GET_STACK_TRACE_FATAL(pc, bp); |
Stephen Hines | 2d1fdb2 | 2014-05-28 23:58:16 -0700 | [diff] [blame] | 1109 | stack.Print(); |
Alexey Samsonov | c98570b | 2012-08-09 10:56:57 +0000 | [diff] [blame] | 1110 | |
Pirama Arumuga Nainar | cdce50b | 2015-07-01 12:26:56 -0700 | [diff] [blame] | 1111 | DescribeAddress(addr, access_size, bug_descr); |
Alexey Samsonov | 2fb0872 | 2013-11-01 17:02:14 +0000 | [diff] [blame] | 1112 | ReportErrorSummary(bug_descr, &stack); |
Alexey Samsonov | 9873792 | 2012-08-10 15:13:05 +0000 | [diff] [blame] | 1113 | PrintShadowMemoryForAddress(addr); |
Alexey Samsonov | c98570b | 2012-08-09 10:56:57 +0000 | [diff] [blame] | 1114 | } |
| 1115 | |
Pirama Arumuga Nainar | 799172d | 2016-03-03 15:50:30 -0800 | [diff] [blame^] | 1116 | } // namespace __asan |
| 1117 | |
| 1118 | // --------------------------- Interface --------------------- {{{1 |
| 1119 | using namespace __asan; // NOLINT |
| 1120 | |
| 1121 | void __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 Samsonov | c98570b | 2012-08-09 10:56:57 +0000 | [diff] [blame] | 1128 | void NOINLINE __asan_set_error_report_callback(void (*callback)(const char*)) { |
Pirama Arumuga Nainar | 799172d | 2016-03-03 15:50:30 -0800 | [diff] [blame^] | 1129 | BlockingMutexLock l(&error_message_buf_mutex); |
Alexey Samsonov | c98570b | 2012-08-09 10:56:57 +0000 | [diff] [blame] | 1130 | error_report_callback = callback; |
Alexey Samsonov | c98570b | 2012-08-09 10:56:57 +0000 | [diff] [blame] | 1131 | } |
Alexey Samsonov | f657a19 | 2012-08-13 11:23:40 +0000 | [diff] [blame] | 1132 | |
Kostya Serebryany | 17a7c67 | 2012-12-29 10:18:31 +0000 | [diff] [blame] | 1133 | void __asan_describe_address(uptr addr) { |
Stephen Hines | 6d18623 | 2014-11-26 17:56:19 -0800 | [diff] [blame] | 1134 | // Thread registry must be locked while we're describing an address. |
| 1135 | asanThreadRegistry().Lock(); |
Pirama Arumuga Nainar | cdce50b | 2015-07-01 12:26:56 -0700 | [diff] [blame] | 1136 | DescribeAddress(addr, 1, ""); |
Stephen Hines | 6d18623 | 2014-11-26 17:56:19 -0800 | [diff] [blame] | 1137 | asanThreadRegistry().Unlock(); |
| 1138 | } |
| 1139 | |
| 1140 | int __asan_report_present() { |
| 1141 | return report_happened ? 1 : 0; |
| 1142 | } |
| 1143 | |
| 1144 | uptr __asan_get_report_pc() { |
| 1145 | return report_data.pc; |
| 1146 | } |
| 1147 | |
| 1148 | uptr __asan_get_report_bp() { |
| 1149 | return report_data.bp; |
| 1150 | } |
| 1151 | |
| 1152 | uptr __asan_get_report_sp() { |
| 1153 | return report_data.sp; |
| 1154 | } |
| 1155 | |
| 1156 | uptr __asan_get_report_address() { |
| 1157 | return report_data.addr; |
| 1158 | } |
| 1159 | |
| 1160 | int __asan_get_report_access_type() { |
| 1161 | return report_data.is_write ? 1 : 0; |
| 1162 | } |
| 1163 | |
| 1164 | uptr __asan_get_report_access_size() { |
| 1165 | return report_data.access_size; |
| 1166 | } |
| 1167 | |
| 1168 | const char *__asan_get_report_description() { |
| 1169 | return report_data.description; |
Kostya Serebryany | 17a7c67 | 2012-12-29 10:18:31 +0000 | [diff] [blame] | 1170 | } |
| 1171 | |
Stephen Hines | 2d1fdb2 | 2014-05-28 23:58:16 -0700 | [diff] [blame] | 1172 | extern "C" { |
| 1173 | SANITIZER_INTERFACE_ATTRIBUTE |
| 1174 | void __sanitizer_ptr_sub(void *a, void *b) { |
| 1175 | CheckForInvalidPointerPair(a, b); |
| 1176 | } |
| 1177 | SANITIZER_INTERFACE_ATTRIBUTE |
| 1178 | void __sanitizer_ptr_cmp(void *a, void *b) { |
| 1179 | CheckForInvalidPointerPair(a, b); |
| 1180 | } |
Pirama Arumuga Nainar | 799172d | 2016-03-03 15:50:30 -0800 | [diff] [blame^] | 1181 | } // extern "C" |
Stephen Hines | 2d1fdb2 | 2014-05-28 23:58:16 -0700 | [diff] [blame] | 1182 | |
Alexey Samsonov | 6a08d29 | 2012-12-07 22:01:28 +0000 | [diff] [blame] | 1183 | #if !SANITIZER_SUPPORTS_WEAK_HOOKS |
Alexey Samsonov | 8663343 | 2012-10-02 14:06:39 +0000 | [diff] [blame] | 1184 | // Provide default implementation of __asan_on_error that does nothing |
| 1185 | // and may be overriden by user. |
Timur Iskhodzhanov | 3c80c6c | 2013-08-13 11:42:45 +0000 | [diff] [blame] | 1186 | SANITIZER_INTERFACE_ATTRIBUTE SANITIZER_WEAK_ATTRIBUTE NOINLINE |
Alexey Samsonov | 8663343 | 2012-10-02 14:06:39 +0000 | [diff] [blame] | 1187 | void __asan_on_error() {} |
Alexey Samsonov | 6a08d29 | 2012-12-07 22:01:28 +0000 | [diff] [blame] | 1188 | #endif |