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" |
Alexey Samsonov | 7354509 | 2012-08-09 07:40:58 +0000 | [diff] [blame] | 20 | #include "asan_thread_registry.h" |
Kostya Serebryany | 58f5455 | 2012-12-18 07:32:16 +0000 | [diff] [blame] | 21 | #include "sanitizer_common/sanitizer_common.h" |
| 22 | #include "sanitizer_common/sanitizer_report_decorator.h" |
Alexey Samsonov | 9c92748 | 2012-12-26 14:44:46 +0000 | [diff] [blame] | 23 | #include "sanitizer_common/sanitizer_symbolizer.h" |
Alexey Samsonov | 7354509 | 2012-08-09 07:40:58 +0000 | [diff] [blame] | 24 | |
| 25 | namespace __asan { |
| 26 | |
Alexey Samsonov | f657a19 | 2012-08-13 11:23:40 +0000 | [diff] [blame] | 27 | // -------------------- User-specified callbacks ----------------- {{{1 |
Alexey Samsonov | c98570b | 2012-08-09 10:56:57 +0000 | [diff] [blame] | 28 | static void (*error_report_callback)(const char*); |
| 29 | static char *error_message_buffer = 0; |
| 30 | static uptr error_message_buffer_pos = 0; |
| 31 | static uptr error_message_buffer_size = 0; |
| 32 | |
| 33 | void AppendToErrorMessageBuffer(const char *buffer) { |
| 34 | if (error_message_buffer) { |
| 35 | uptr length = internal_strlen(buffer); |
| 36 | CHECK_GE(error_message_buffer_size, error_message_buffer_pos); |
| 37 | uptr remaining = error_message_buffer_size - error_message_buffer_pos; |
| 38 | internal_strncpy(error_message_buffer + error_message_buffer_pos, |
| 39 | buffer, remaining); |
| 40 | error_message_buffer[error_message_buffer_size - 1] = '\0'; |
| 41 | // FIXME: reallocate the buffer instead of truncating the message. |
| 42 | error_message_buffer_pos += remaining > length ? length : remaining; |
| 43 | } |
| 44 | } |
| 45 | |
Kostya Serebryany | 58f5455 | 2012-12-18 07:32:16 +0000 | [diff] [blame] | 46 | // ---------------------- Decorator ------------------------------ {{{1 |
Kostya Serebryany | 9514a53 | 2012-12-19 09:53:32 +0000 | [diff] [blame] | 47 | bool PrintsToTtyCached() { |
| 48 | static int cached = 0; |
| 49 | static bool prints_to_tty; |
| 50 | if (!cached) { // Ok wrt threads since we are printing only from one thread. |
| 51 | prints_to_tty = PrintsToTty(); |
| 52 | cached = 1; |
| 53 | } |
| 54 | return prints_to_tty; |
| 55 | } |
Kostya Serebryany | 58f5455 | 2012-12-18 07:32:16 +0000 | [diff] [blame] | 56 | class Decorator: private __sanitizer::AnsiColorDecorator { |
| 57 | public: |
Kostya Serebryany | 9514a53 | 2012-12-19 09:53:32 +0000 | [diff] [blame] | 58 | Decorator() : __sanitizer::AnsiColorDecorator(PrintsToTtyCached()) { } |
Kostya Serebryany | 58f5455 | 2012-12-18 07:32:16 +0000 | [diff] [blame] | 59 | const char *Warning() { return Red(); } |
| 60 | const char *EndWarning() { return Default(); } |
| 61 | const char *Access() { return Blue(); } |
| 62 | const char *EndAccess() { return Default(); } |
| 63 | const char *Location() { return Green(); } |
| 64 | const char *EndLocation() { return Default(); } |
| 65 | const char *Allocation() { return Magenta(); } |
| 66 | const char *EndAllocation() { return Default(); } |
Kostya Serebryany | 9514a53 | 2012-12-19 09:53:32 +0000 | [diff] [blame] | 67 | |
| 68 | const char *ShadowByte(u8 byte) { |
| 69 | switch (byte) { |
| 70 | case kAsanHeapLeftRedzoneMagic: |
| 71 | case kAsanHeapRightRedzoneMagic: |
| 72 | return Red(); |
| 73 | case kAsanHeapFreeMagic: |
| 74 | return Magenta(); |
| 75 | case kAsanStackLeftRedzoneMagic: |
| 76 | case kAsanStackMidRedzoneMagic: |
| 77 | case kAsanStackRightRedzoneMagic: |
| 78 | case kAsanStackPartialRedzoneMagic: |
| 79 | return Red(); |
| 80 | case kAsanStackAfterReturnMagic: |
| 81 | return Magenta(); |
| 82 | case kAsanInitializationOrderMagic: |
| 83 | return Cyan(); |
| 84 | case kAsanUserPoisonedMemoryMagic: |
| 85 | return Blue(); |
| 86 | case kAsanStackUseAfterScopeMagic: |
| 87 | return Magenta(); |
| 88 | case kAsanGlobalRedzoneMagic: |
| 89 | return Red(); |
| 90 | case kAsanInternalHeapMagic: |
| 91 | return Yellow(); |
| 92 | default: |
| 93 | return Default(); |
| 94 | } |
| 95 | } |
| 96 | const char *EndShadowByte() { return Default(); } |
Kostya Serebryany | 58f5455 | 2012-12-18 07:32:16 +0000 | [diff] [blame] | 97 | }; |
| 98 | |
Alexey Samsonov | 9873792 | 2012-08-10 15:13:05 +0000 | [diff] [blame] | 99 | // ---------------------- Helper functions ----------------------- {{{1 |
| 100 | |
Kostya Serebryany | 9514a53 | 2012-12-19 09:53:32 +0000 | [diff] [blame] | 101 | static void PrintShadowByte(const char *before, u8 byte, |
| 102 | const char *after = "\n") { |
| 103 | Decorator d; |
| 104 | Printf("%s%s%x%x%s%s", before, |
| 105 | d.ShadowByte(byte), byte >> 4, byte & 15, d.EndShadowByte(), after); |
| 106 | } |
| 107 | |
| 108 | static void PrintShadowBytes(const char *before, u8 *bytes, |
| 109 | u8 *guilty, uptr n) { |
| 110 | Decorator d; |
| 111 | if (before) |
| 112 | Printf("%s%p:", before, bytes); |
| 113 | for (uptr i = 0; i < n; i++) { |
| 114 | u8 *p = bytes + i; |
| 115 | const char *before = p == guilty ? "[" : |
| 116 | p - 1 == guilty ? "" : " "; |
| 117 | const char *after = p == guilty ? "]" : ""; |
| 118 | PrintShadowByte(before, *p, after); |
Alexey Samsonov | 9873792 | 2012-08-10 15:13:05 +0000 | [diff] [blame] | 119 | } |
Kostya Serebryany | 283c296 | 2012-08-28 11:34:40 +0000 | [diff] [blame] | 120 | Printf("\n"); |
Alexey Samsonov | 9873792 | 2012-08-10 15:13:05 +0000 | [diff] [blame] | 121 | } |
| 122 | |
| 123 | static void PrintShadowMemoryForAddress(uptr addr) { |
| 124 | if (!AddrIsInMem(addr)) |
| 125 | return; |
| 126 | uptr shadow_addr = MemToShadow(addr); |
Kostya Serebryany | 9514a53 | 2012-12-19 09:53:32 +0000 | [diff] [blame] | 127 | const uptr n_bytes_per_row = 16; |
| 128 | uptr aligned_shadow = shadow_addr & ~(n_bytes_per_row - 1); |
| 129 | Printf("Shadow bytes around the buggy address:\n"); |
| 130 | for (int i = -5; i <= 5; i++) { |
Alexey Samsonov | 9873792 | 2012-08-10 15:13:05 +0000 | [diff] [blame] | 131 | const char *prefix = (i == 0) ? "=>" : " "; |
Kostya Serebryany | 9514a53 | 2012-12-19 09:53:32 +0000 | [diff] [blame] | 132 | PrintShadowBytes(prefix, |
| 133 | (u8*)(aligned_shadow + i * n_bytes_per_row), |
| 134 | (u8*)shadow_addr, n_bytes_per_row); |
Alexey Samsonov | 9873792 | 2012-08-10 15:13:05 +0000 | [diff] [blame] | 135 | } |
Kostya Serebryany | 9514a53 | 2012-12-19 09:53:32 +0000 | [diff] [blame] | 136 | Printf("Shadow byte legend (one shadow byte represents %d " |
| 137 | "application bytes):\n", (int)SHADOW_GRANULARITY); |
| 138 | PrintShadowByte(" Addressable: ", 0); |
| 139 | Printf(" Partially addressable: "); |
| 140 | for (uptr i = 1; i < SHADOW_GRANULARITY; i++) |
| 141 | PrintShadowByte("", i, " "); |
| 142 | Printf("\n"); |
| 143 | PrintShadowByte(" Heap left redzone: ", kAsanHeapLeftRedzoneMagic); |
| 144 | PrintShadowByte(" Heap righ redzone: ", kAsanHeapRightRedzoneMagic); |
| 145 | PrintShadowByte(" Freed Heap region: ", kAsanHeapFreeMagic); |
| 146 | PrintShadowByte(" Stack left redzone: ", kAsanStackLeftRedzoneMagic); |
| 147 | PrintShadowByte(" Stack mid redzone: ", kAsanStackMidRedzoneMagic); |
| 148 | PrintShadowByte(" Stack right redzone: ", kAsanStackRightRedzoneMagic); |
| 149 | PrintShadowByte(" Stack partial redzone: ", kAsanStackPartialRedzoneMagic); |
Kostya Serebryany | 9514a53 | 2012-12-19 09:53:32 +0000 | [diff] [blame] | 150 | PrintShadowByte(" Stack after return: ", kAsanStackAfterReturnMagic); |
| 151 | PrintShadowByte(" Stack use after scope: ", kAsanStackUseAfterScopeMagic); |
| 152 | PrintShadowByte(" Global redzone: ", kAsanGlobalRedzoneMagic); |
| 153 | PrintShadowByte(" Global init order: ", kAsanInitializationOrderMagic); |
| 154 | PrintShadowByte(" Poisoned by user: ", kAsanUserPoisonedMemoryMagic); |
| 155 | PrintShadowByte(" ASan internal: ", kAsanInternalHeapMagic); |
Alexey Samsonov | 9873792 | 2012-08-10 15:13:05 +0000 | [diff] [blame] | 156 | } |
| 157 | |
| 158 | static void PrintZoneForPointer(uptr ptr, uptr zone_ptr, |
| 159 | const char *zone_name) { |
| 160 | if (zone_ptr) { |
| 161 | if (zone_name) { |
Kostya Serebryany | 283c296 | 2012-08-28 11:34:40 +0000 | [diff] [blame] | 162 | Printf("malloc_zone_from_ptr(%p) = %p, which is %s\n", |
Alexey Samsonov | 9873792 | 2012-08-10 15:13:05 +0000 | [diff] [blame] | 163 | ptr, zone_ptr, zone_name); |
| 164 | } else { |
Kostya Serebryany | 283c296 | 2012-08-28 11:34:40 +0000 | [diff] [blame] | 165 | 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] | 166 | ptr, zone_ptr); |
| 167 | } |
| 168 | } else { |
Kostya Serebryany | 283c296 | 2012-08-28 11:34:40 +0000 | [diff] [blame] | 169 | Printf("malloc_zone_from_ptr(%p) = 0\n", ptr); |
Alexey Samsonov | 9873792 | 2012-08-10 15:13:05 +0000 | [diff] [blame] | 170 | } |
| 171 | } |
| 172 | |
Alexey Samsonov | e218beb | 2012-08-09 09:06:52 +0000 | [diff] [blame] | 173 | // ---------------------- Address Descriptions ------------------- {{{1 |
| 174 | |
Alexey Samsonov | e4bfca2 | 2012-08-09 09:27:24 +0000 | [diff] [blame] | 175 | static bool IsASCII(unsigned char c) { |
Alexey Samsonov | 9873792 | 2012-08-10 15:13:05 +0000 | [diff] [blame] | 176 | return /*0x00 <= c &&*/ c <= 0x7F; |
Alexey Samsonov | e4bfca2 | 2012-08-09 09:27:24 +0000 | [diff] [blame] | 177 | } |
| 178 | |
| 179 | // Check if the global is a zero-terminated ASCII string. If so, print it. |
| 180 | static void PrintGlobalNameIfASCII(const __asan_global &g) { |
| 181 | for (uptr p = g.beg; p < g.beg + g.size - 1; p++) { |
| 182 | if (!IsASCII(*(unsigned char*)p)) return; |
| 183 | } |
| 184 | if (*(char*)(g.beg + g.size - 1) != 0) return; |
Kostya Serebryany | 283c296 | 2012-08-28 11:34:40 +0000 | [diff] [blame] | 185 | Printf(" '%s' is ascii string '%s'\n", g.name, (char*)g.beg); |
Alexey Samsonov | e4bfca2 | 2012-08-09 09:27:24 +0000 | [diff] [blame] | 186 | } |
| 187 | |
| 188 | bool DescribeAddressRelativeToGlobal(uptr addr, const __asan_global &g) { |
| 189 | if (addr < g.beg - kGlobalAndStackRedzone) return false; |
| 190 | if (addr >= g.beg + g.size_with_redzone) return false; |
Kostya Serebryany | 58f5455 | 2012-12-18 07:32:16 +0000 | [diff] [blame] | 191 | Decorator d; |
| 192 | Printf("%s", d.Location()); |
Kostya Serebryany | 283c296 | 2012-08-28 11:34:40 +0000 | [diff] [blame] | 193 | Printf("%p is located ", (void*)addr); |
Alexey Samsonov | e4bfca2 | 2012-08-09 09:27:24 +0000 | [diff] [blame] | 194 | if (addr < g.beg) { |
Kostya Serebryany | 283c296 | 2012-08-28 11:34:40 +0000 | [diff] [blame] | 195 | Printf("%zd bytes to the left", g.beg - addr); |
Alexey Samsonov | e4bfca2 | 2012-08-09 09:27:24 +0000 | [diff] [blame] | 196 | } else if (addr >= g.beg + g.size) { |
Kostya Serebryany | 283c296 | 2012-08-28 11:34:40 +0000 | [diff] [blame] | 197 | Printf("%zd bytes to the right", addr - (g.beg + g.size)); |
Alexey Samsonov | e4bfca2 | 2012-08-09 09:27:24 +0000 | [diff] [blame] | 198 | } else { |
Kostya Serebryany | 283c296 | 2012-08-28 11:34:40 +0000 | [diff] [blame] | 199 | Printf("%zd bytes inside", addr - g.beg); // Can it happen? |
Alexey Samsonov | e4bfca2 | 2012-08-09 09:27:24 +0000 | [diff] [blame] | 200 | } |
Kostya Serebryany | 283c296 | 2012-08-28 11:34:40 +0000 | [diff] [blame] | 201 | Printf(" of global variable '%s' (0x%zx) of size %zu\n", |
Alexey Samsonov | e4bfca2 | 2012-08-09 09:27:24 +0000 | [diff] [blame] | 202 | g.name, g.beg, g.size); |
Kostya Serebryany | 58f5455 | 2012-12-18 07:32:16 +0000 | [diff] [blame] | 203 | Printf("%s", d.EndLocation()); |
Alexey Samsonov | e4bfca2 | 2012-08-09 09:27:24 +0000 | [diff] [blame] | 204 | PrintGlobalNameIfASCII(g); |
| 205 | return true; |
| 206 | } |
| 207 | |
Alexey Samsonov | e218beb | 2012-08-09 09:06:52 +0000 | [diff] [blame] | 208 | bool DescribeAddressIfShadow(uptr addr) { |
| 209 | if (AddrIsInMem(addr)) |
| 210 | return false; |
| 211 | static const char kAddrInShadowReport[] = |
| 212 | "Address %p is located in the %s.\n"; |
| 213 | if (AddrIsInShadowGap(addr)) { |
Kostya Serebryany | 283c296 | 2012-08-28 11:34:40 +0000 | [diff] [blame] | 214 | Printf(kAddrInShadowReport, addr, "shadow gap area"); |
Alexey Samsonov | e218beb | 2012-08-09 09:06:52 +0000 | [diff] [blame] | 215 | return true; |
| 216 | } |
| 217 | if (AddrIsInHighShadow(addr)) { |
Kostya Serebryany | 283c296 | 2012-08-28 11:34:40 +0000 | [diff] [blame] | 218 | Printf(kAddrInShadowReport, addr, "high shadow area"); |
Alexey Samsonov | e218beb | 2012-08-09 09:06:52 +0000 | [diff] [blame] | 219 | return true; |
| 220 | } |
| 221 | if (AddrIsInLowShadow(addr)) { |
Kostya Serebryany | 283c296 | 2012-08-28 11:34:40 +0000 | [diff] [blame] | 222 | Printf(kAddrInShadowReport, addr, "low shadow area"); |
Alexey Samsonov | e218beb | 2012-08-09 09:06:52 +0000 | [diff] [blame] | 223 | return true; |
| 224 | } |
| 225 | CHECK(0 && "Address is not in memory and not in shadow?"); |
| 226 | return false; |
| 227 | } |
| 228 | |
| 229 | bool DescribeAddressIfStack(uptr addr, uptr access_size) { |
| 230 | AsanThread *t = asanThreadRegistry().FindThreadByStackAddress(addr); |
| 231 | if (!t) return false; |
| 232 | const sptr kBufSize = 4095; |
| 233 | char buf[kBufSize]; |
| 234 | uptr offset = 0; |
| 235 | const char *frame_descr = t->GetFrameNameByAddr(addr, &offset); |
| 236 | // This string is created by the compiler and has the following form: |
| 237 | // "FunctioName n alloc_1 alloc_2 ... alloc_n" |
| 238 | // where alloc_i looks like "offset size len ObjectName ". |
| 239 | CHECK(frame_descr); |
| 240 | // Report the function name and the offset. |
| 241 | const char *name_end = internal_strchr(frame_descr, ' '); |
| 242 | CHECK(name_end); |
| 243 | buf[0] = 0; |
| 244 | internal_strncat(buf, frame_descr, |
| 245 | Min(kBufSize, |
| 246 | static_cast<sptr>(name_end - frame_descr))); |
Kostya Serebryany | 58f5455 | 2012-12-18 07:32:16 +0000 | [diff] [blame] | 247 | Decorator d; |
| 248 | Printf("%s", d.Location()); |
Kostya Serebryany | 283c296 | 2012-08-28 11:34:40 +0000 | [diff] [blame] | 249 | Printf("Address %p is located at offset %zu " |
Alexey Samsonov | e218beb | 2012-08-09 09:06:52 +0000 | [diff] [blame] | 250 | "in frame <%s> of T%d's stack:\n", |
Alexey Samsonov | 9c92748 | 2012-12-26 14:44:46 +0000 | [diff] [blame] | 251 | (void*)addr, offset, Demangle(buf), t->tid()); |
Kostya Serebryany | 58f5455 | 2012-12-18 07:32:16 +0000 | [diff] [blame] | 252 | Printf("%s", d.EndLocation()); |
Alexey Samsonov | e218beb | 2012-08-09 09:06:52 +0000 | [diff] [blame] | 253 | // Report the number of stack objects. |
| 254 | char *p; |
| 255 | uptr n_objects = internal_simple_strtoll(name_end, &p, 10); |
| 256 | CHECK(n_objects > 0); |
Kostya Serebryany | 283c296 | 2012-08-28 11:34:40 +0000 | [diff] [blame] | 257 | Printf(" This frame has %zu object(s):\n", n_objects); |
Alexey Samsonov | e218beb | 2012-08-09 09:06:52 +0000 | [diff] [blame] | 258 | // Report all objects in this frame. |
| 259 | for (uptr i = 0; i < n_objects; i++) { |
| 260 | uptr beg, size; |
| 261 | sptr len; |
| 262 | beg = internal_simple_strtoll(p, &p, 10); |
| 263 | size = internal_simple_strtoll(p, &p, 10); |
| 264 | len = internal_simple_strtoll(p, &p, 10); |
| 265 | if (beg <= 0 || size <= 0 || len < 0 || *p != ' ') { |
Kostya Serebryany | 283c296 | 2012-08-28 11:34:40 +0000 | [diff] [blame] | 266 | Printf("AddressSanitizer can't parse the stack frame " |
Alexey Samsonov | e218beb | 2012-08-09 09:06:52 +0000 | [diff] [blame] | 267 | "descriptor: |%s|\n", frame_descr); |
| 268 | break; |
| 269 | } |
| 270 | p++; |
| 271 | buf[0] = 0; |
| 272 | internal_strncat(buf, p, Min(kBufSize, len)); |
| 273 | p += len; |
Kostya Serebryany | 283c296 | 2012-08-28 11:34:40 +0000 | [diff] [blame] | 274 | Printf(" [%zu, %zu) '%s'\n", beg, beg + size, buf); |
Alexey Samsonov | e218beb | 2012-08-09 09:06:52 +0000 | [diff] [blame] | 275 | } |
Kostya Serebryany | 283c296 | 2012-08-28 11:34:40 +0000 | [diff] [blame] | 276 | Printf("HINT: this may be a false positive if your program uses " |
Alexey Samsonov | 0870028 | 2012-11-23 09:46:34 +0000 | [diff] [blame] | 277 | "some custom stack unwind mechanism or swapcontext\n" |
Alexey Samsonov | e218beb | 2012-08-09 09:06:52 +0000 | [diff] [blame] | 278 | " (longjmp and C++ exceptions *are* supported)\n"); |
Alexey Samsonov | 71b42c9 | 2012-09-05 07:37:15 +0000 | [diff] [blame] | 279 | DescribeThread(t->summary()); |
Alexey Samsonov | e218beb | 2012-08-09 09:06:52 +0000 | [diff] [blame] | 280 | return true; |
| 281 | } |
| 282 | |
Alexey Samsonov | 5c153fa | 2012-09-18 07:38:10 +0000 | [diff] [blame] | 283 | static void DescribeAccessToHeapChunk(AsanChunkView chunk, uptr addr, |
| 284 | uptr access_size) { |
| 285 | uptr offset; |
Kostya Serebryany | 58f5455 | 2012-12-18 07:32:16 +0000 | [diff] [blame] | 286 | Decorator d; |
| 287 | Printf("%s", d.Location()); |
Alexey Samsonov | 5c153fa | 2012-09-18 07:38:10 +0000 | [diff] [blame] | 288 | Printf("%p is located ", (void*)addr); |
| 289 | if (chunk.AddrIsInside(addr, access_size, &offset)) { |
| 290 | Printf("%zu bytes inside of", offset); |
| 291 | } else if (chunk.AddrIsAtLeft(addr, access_size, &offset)) { |
| 292 | Printf("%zu bytes to the left of", offset); |
| 293 | } else if (chunk.AddrIsAtRight(addr, access_size, &offset)) { |
| 294 | Printf("%zu bytes to the right of", offset); |
| 295 | } else { |
| 296 | Printf(" somewhere around (this is AddressSanitizer bug!)"); |
| 297 | } |
| 298 | Printf(" %zu-byte region [%p,%p)\n", chunk.UsedSize(), |
| 299 | (void*)(chunk.Beg()), (void*)(chunk.End())); |
Kostya Serebryany | 58f5455 | 2012-12-18 07:32:16 +0000 | [diff] [blame] | 300 | Printf("%s", d.EndLocation()); |
Alexey Samsonov | 5c153fa | 2012-09-18 07:38:10 +0000 | [diff] [blame] | 301 | } |
| 302 | |
Kostya Serebryany | 716e2f2 | 2012-12-07 15:15:01 +0000 | [diff] [blame] | 303 | // Return " (thread_name) " or an empty string if the name is empty. |
| 304 | const char *ThreadNameWithParenthesis(AsanThreadSummary *t, char buff[], |
| 305 | uptr buff_len) { |
| 306 | const char *name = t->name(); |
| 307 | if (*name == 0) return ""; |
| 308 | buff[0] = 0; |
| 309 | internal_strncat(buff, " (", 3); |
| 310 | internal_strncat(buff, name, buff_len - 4); |
| 311 | internal_strncat(buff, ")", 2); |
| 312 | return buff; |
| 313 | } |
| 314 | |
| 315 | const char *ThreadNameWithParenthesis(u32 tid, char buff[], |
| 316 | uptr buff_len) { |
| 317 | if (tid == kInvalidTid) return ""; |
| 318 | AsanThreadSummary *t = asanThreadRegistry().FindByTid(tid); |
| 319 | return ThreadNameWithParenthesis(t, buff, buff_len); |
| 320 | } |
| 321 | |
Alexey Samsonov | 5c153fa | 2012-09-18 07:38:10 +0000 | [diff] [blame] | 322 | void DescribeHeapAddress(uptr addr, uptr access_size) { |
| 323 | AsanChunkView chunk = FindHeapChunkByAddress(addr); |
| 324 | if (!chunk.IsValid()) return; |
| 325 | DescribeAccessToHeapChunk(chunk, addr, access_size); |
| 326 | CHECK(chunk.AllocTid() != kInvalidTid); |
| 327 | AsanThreadSummary *alloc_thread = |
| 328 | asanThreadRegistry().FindByTid(chunk.AllocTid()); |
| 329 | StackTrace alloc_stack; |
| 330 | chunk.GetAllocStack(&alloc_stack); |
| 331 | AsanThread *t = asanThreadRegistry().GetCurrent(); |
| 332 | CHECK(t); |
Kostya Serebryany | 716e2f2 | 2012-12-07 15:15:01 +0000 | [diff] [blame] | 333 | char tname[128]; |
Kostya Serebryany | 58f5455 | 2012-12-18 07:32:16 +0000 | [diff] [blame] | 334 | Decorator d; |
Alexey Samsonov | 5c153fa | 2012-09-18 07:38:10 +0000 | [diff] [blame] | 335 | if (chunk.FreeTid() != kInvalidTid) { |
| 336 | AsanThreadSummary *free_thread = |
| 337 | asanThreadRegistry().FindByTid(chunk.FreeTid()); |
Kostya Serebryany | 58f5455 | 2012-12-18 07:32:16 +0000 | [diff] [blame] | 338 | Printf("%sfreed by thread T%d%s here:%s\n", d.Allocation(), |
| 339 | free_thread->tid(), |
| 340 | ThreadNameWithParenthesis(free_thread, tname, sizeof(tname)), |
| 341 | d.EndAllocation()); |
Alexey Samsonov | 5c153fa | 2012-09-18 07:38:10 +0000 | [diff] [blame] | 342 | StackTrace free_stack; |
| 343 | chunk.GetFreeStack(&free_stack); |
| 344 | PrintStack(&free_stack); |
Kostya Serebryany | 58f5455 | 2012-12-18 07:32:16 +0000 | [diff] [blame] | 345 | Printf("%spreviously allocated by thread T%d%s here:%s\n", |
| 346 | d.Allocation(), alloc_thread->tid(), |
| 347 | ThreadNameWithParenthesis(alloc_thread, tname, sizeof(tname)), |
| 348 | d.EndAllocation()); |
Alexey Samsonov | 5c153fa | 2012-09-18 07:38:10 +0000 | [diff] [blame] | 349 | PrintStack(&alloc_stack); |
| 350 | DescribeThread(t->summary()); |
| 351 | DescribeThread(free_thread); |
| 352 | DescribeThread(alloc_thread); |
| 353 | } else { |
Kostya Serebryany | 58f5455 | 2012-12-18 07:32:16 +0000 | [diff] [blame] | 354 | Printf("%sallocated by thread T%d%s here:%s\n", d.Allocation(), |
| 355 | alloc_thread->tid(), |
| 356 | ThreadNameWithParenthesis(alloc_thread, tname, sizeof(tname)), |
| 357 | d.EndAllocation()); |
Alexey Samsonov | 5c153fa | 2012-09-18 07:38:10 +0000 | [diff] [blame] | 358 | PrintStack(&alloc_stack); |
| 359 | DescribeThread(t->summary()); |
| 360 | DescribeThread(alloc_thread); |
| 361 | } |
| 362 | } |
| 363 | |
Alexey Samsonov | e218beb | 2012-08-09 09:06:52 +0000 | [diff] [blame] | 364 | void DescribeAddress(uptr addr, uptr access_size) { |
| 365 | // Check if this is shadow or shadow gap. |
| 366 | if (DescribeAddressIfShadow(addr)) |
| 367 | return; |
| 368 | CHECK(AddrIsInMem(addr)); |
| 369 | if (DescribeAddressIfGlobal(addr)) |
| 370 | return; |
| 371 | if (DescribeAddressIfStack(addr, access_size)) |
| 372 | return; |
| 373 | // Assume it is a heap address. |
| 374 | DescribeHeapAddress(addr, access_size); |
| 375 | } |
| 376 | |
Alexey Samsonov | 71b42c9 | 2012-09-05 07:37:15 +0000 | [diff] [blame] | 377 | // ------------------- Thread description -------------------- {{{1 |
| 378 | |
| 379 | void DescribeThread(AsanThreadSummary *summary) { |
| 380 | CHECK(summary); |
| 381 | // No need to announce the main thread. |
| 382 | if (summary->tid() == 0 || summary->announced()) { |
| 383 | return; |
| 384 | } |
| 385 | summary->set_announced(true); |
Kostya Serebryany | 716e2f2 | 2012-12-07 15:15:01 +0000 | [diff] [blame] | 386 | char tname[128]; |
| 387 | Printf("Thread T%d%s", summary->tid(), |
| 388 | ThreadNameWithParenthesis(summary->tid(), tname, sizeof(tname))); |
| 389 | Printf(" created by T%d%s here:\n", |
| 390 | summary->parent_tid(), |
| 391 | ThreadNameWithParenthesis(summary->parent_tid(), |
| 392 | tname, sizeof(tname))); |
Alexey Samsonov | 71b42c9 | 2012-09-05 07:37:15 +0000 | [diff] [blame] | 393 | PrintStack(summary->stack()); |
| 394 | // Recursively described parent thread if needed. |
| 395 | if (flags()->print_full_thread_history) { |
| 396 | AsanThreadSummary *parent_summary = |
| 397 | asanThreadRegistry().FindByTid(summary->parent_tid()); |
| 398 | DescribeThread(parent_summary); |
| 399 | } |
| 400 | } |
| 401 | |
Alexey Samsonov | e218beb | 2012-08-09 09:06:52 +0000 | [diff] [blame] | 402 | // -------------------- Different kinds of reports ----------------- {{{1 |
| 403 | |
Alexey Samsonov | 9873792 | 2012-08-10 15:13:05 +0000 | [diff] [blame] | 404 | // Use ScopedInErrorReport to run common actions just before and |
| 405 | // immediately after printing error report. |
| 406 | class ScopedInErrorReport { |
| 407 | public: |
| 408 | ScopedInErrorReport() { |
| 409 | static atomic_uint32_t num_calls; |
Alexey Samsonov | 62e2709 | 2012-09-17 08:02:19 +0000 | [diff] [blame] | 410 | static u32 reporting_thread_tid; |
Alexey Samsonov | 9873792 | 2012-08-10 15:13:05 +0000 | [diff] [blame] | 411 | if (atomic_fetch_add(&num_calls, 1, memory_order_relaxed) != 0) { |
| 412 | // Do not print more than one report, otherwise they will mix up. |
| 413 | // Error reporting functions shouldn't return at this situation, as |
| 414 | // they are defined as no-return. |
Kostya Serebryany | 283c296 | 2012-08-28 11:34:40 +0000 | [diff] [blame] | 415 | Report("AddressSanitizer: while reporting a bug found another one." |
Alexey Samsonov | 9873792 | 2012-08-10 15:13:05 +0000 | [diff] [blame] | 416 | "Ignoring.\n"); |
Alexey Samsonov | 62e2709 | 2012-09-17 08:02:19 +0000 | [diff] [blame] | 417 | u32 current_tid = asanThreadRegistry().GetCurrentTidOrInvalid(); |
| 418 | if (current_tid != reporting_thread_tid) { |
| 419 | // ASan found two bugs in different threads simultaneously. Sleep |
| 420 | // long enough to make sure that the thread which started to print |
| 421 | // an error report will finish doing it. |
| 422 | SleepForSeconds(Max(100, flags()->sleep_before_dying + 1)); |
| 423 | } |
Alexey Samsonov | 031633b | 2012-11-19 11:22:22 +0000 | [diff] [blame] | 424 | // If we're still not dead for some reason, use raw Exit() instead of |
| 425 | // Die() to bypass any additional checks. |
| 426 | Exit(flags()->exitcode); |
Alexey Samsonov | 9873792 | 2012-08-10 15:13:05 +0000 | [diff] [blame] | 427 | } |
Alexey Samsonov | 6a08d29 | 2012-12-07 22:01:28 +0000 | [diff] [blame] | 428 | ASAN_ON_ERROR(); |
Alexey Samsonov | 62e2709 | 2012-09-17 08:02:19 +0000 | [diff] [blame] | 429 | reporting_thread_tid = asanThreadRegistry().GetCurrentTidOrInvalid(); |
Kostya Serebryany | 283c296 | 2012-08-28 11:34:40 +0000 | [diff] [blame] | 430 | Printf("====================================================" |
Alexey Samsonov | 62e2709 | 2012-09-17 08:02:19 +0000 | [diff] [blame] | 431 | "=============\n"); |
| 432 | if (reporting_thread_tid != kInvalidTid) { |
Alexey Samsonov | 9873792 | 2012-08-10 15:13:05 +0000 | [diff] [blame] | 433 | // We started reporting an error message. Stop using the fake stack |
| 434 | // in case we call an instrumented function from a symbolizer. |
Alexey Samsonov | 62e2709 | 2012-09-17 08:02:19 +0000 | [diff] [blame] | 435 | AsanThread *curr_thread = asanThreadRegistry().GetCurrent(); |
| 436 | CHECK(curr_thread); |
Alexey Samsonov | 9873792 | 2012-08-10 15:13:05 +0000 | [diff] [blame] | 437 | curr_thread->fake_stack().StopUsingFakeStack(); |
| 438 | } |
| 439 | } |
| 440 | // Destructor is NORETURN, as functions that report errors are. |
| 441 | NORETURN ~ScopedInErrorReport() { |
| 442 | // Make sure the current thread is announced. |
| 443 | AsanThread *curr_thread = asanThreadRegistry().GetCurrent(); |
| 444 | if (curr_thread) { |
Alexey Samsonov | 71b42c9 | 2012-09-05 07:37:15 +0000 | [diff] [blame] | 445 | DescribeThread(curr_thread->summary()); |
Alexey Samsonov | 9873792 | 2012-08-10 15:13:05 +0000 | [diff] [blame] | 446 | } |
| 447 | // Print memory stats. |
| 448 | __asan_print_accumulated_stats(); |
| 449 | if (error_report_callback) { |
| 450 | error_report_callback(error_message_buffer); |
| 451 | } |
Kostya Serebryany | 283c296 | 2012-08-28 11:34:40 +0000 | [diff] [blame] | 452 | Report("ABORTING\n"); |
Alexey Samsonov | 9873792 | 2012-08-10 15:13:05 +0000 | [diff] [blame] | 453 | Die(); |
| 454 | } |
| 455 | }; |
| 456 | |
Alexey Samsonov | 7354509 | 2012-08-09 07:40:58 +0000 | [diff] [blame] | 457 | void ReportSIGSEGV(uptr pc, uptr sp, uptr bp, uptr addr) { |
Alexey Samsonov | 9873792 | 2012-08-10 15:13:05 +0000 | [diff] [blame] | 458 | ScopedInErrorReport in_report; |
Kostya Serebryany | 58f5455 | 2012-12-18 07:32:16 +0000 | [diff] [blame] | 459 | Decorator d; |
| 460 | Printf("%s", d.Warning()); |
Kostya Serebryany | 69d8ede | 2012-10-15 13:04:58 +0000 | [diff] [blame] | 461 | Report("ERROR: AddressSanitizer: SEGV on unknown address %p" |
Alexey Samsonov | 7354509 | 2012-08-09 07:40:58 +0000 | [diff] [blame] | 462 | " (pc %p sp %p bp %p T%d)\n", |
| 463 | (void*)addr, (void*)pc, (void*)sp, (void*)bp, |
| 464 | asanThreadRegistry().GetCurrentTidOrInvalid()); |
Kostya Serebryany | 58f5455 | 2012-12-18 07:32:16 +0000 | [diff] [blame] | 465 | Printf("%s", d.EndWarning()); |
Kostya Serebryany | 283c296 | 2012-08-28 11:34:40 +0000 | [diff] [blame] | 466 | Printf("AddressSanitizer can not provide additional info.\n"); |
Kostya Serebryany | a30c8f9 | 2012-12-13 09:34:23 +0000 | [diff] [blame] | 467 | GET_STACK_TRACE_FATAL(pc, bp); |
Kostya Serebryany | cc34722 | 2012-08-28 13:49:49 +0000 | [diff] [blame] | 468 | PrintStack(&stack); |
Alexey Samsonov | 7354509 | 2012-08-09 07:40:58 +0000 | [diff] [blame] | 469 | } |
| 470 | |
Kostya Serebryany | c3390df | 2012-08-28 11:54:30 +0000 | [diff] [blame] | 471 | void ReportDoubleFree(uptr addr, StackTrace *stack) { |
Alexey Samsonov | 9873792 | 2012-08-10 15:13:05 +0000 | [diff] [blame] | 472 | ScopedInErrorReport in_report; |
Kostya Serebryany | 58f5455 | 2012-12-18 07:32:16 +0000 | [diff] [blame] | 473 | Decorator d; |
| 474 | Printf("%s", d.Warning()); |
Kostya Serebryany | 69d8ede | 2012-10-15 13:04:58 +0000 | [diff] [blame] | 475 | Report("ERROR: AddressSanitizer: attempting double-free on %p:\n", addr); |
Kostya Serebryany | 58f5455 | 2012-12-18 07:32:16 +0000 | [diff] [blame] | 476 | Printf("%s", d.EndWarning()); |
Kostya Serebryany | cc34722 | 2012-08-28 13:49:49 +0000 | [diff] [blame] | 477 | PrintStack(stack); |
Alexey Samsonov | f7c1d18 | 2012-08-09 08:15:46 +0000 | [diff] [blame] | 478 | DescribeHeapAddress(addr, 1); |
Alexey Samsonov | f7c1d18 | 2012-08-09 08:15:46 +0000 | [diff] [blame] | 479 | } |
| 480 | |
Kostya Serebryany | c3390df | 2012-08-28 11:54:30 +0000 | [diff] [blame] | 481 | void ReportFreeNotMalloced(uptr addr, StackTrace *stack) { |
Alexey Samsonov | 9873792 | 2012-08-10 15:13:05 +0000 | [diff] [blame] | 482 | ScopedInErrorReport in_report; |
Kostya Serebryany | 58f5455 | 2012-12-18 07:32:16 +0000 | [diff] [blame] | 483 | Decorator d; |
| 484 | Printf("%s", d.Warning()); |
Kostya Serebryany | 69d8ede | 2012-10-15 13:04:58 +0000 | [diff] [blame] | 485 | Report("ERROR: AddressSanitizer: attempting free on address " |
Alexey Samsonov | f7c1d18 | 2012-08-09 08:15:46 +0000 | [diff] [blame] | 486 | "which was not malloc()-ed: %p\n", addr); |
Kostya Serebryany | 58f5455 | 2012-12-18 07:32:16 +0000 | [diff] [blame] | 487 | Printf("%s", d.EndWarning()); |
Kostya Serebryany | cc34722 | 2012-08-28 13:49:49 +0000 | [diff] [blame] | 488 | PrintStack(stack); |
Alexey Samsonov | 9873792 | 2012-08-10 15:13:05 +0000 | [diff] [blame] | 489 | DescribeHeapAddress(addr, 1); |
Alexey Samsonov | f7c1d18 | 2012-08-09 08:15:46 +0000 | [diff] [blame] | 490 | } |
| 491 | |
Kostya Serebryany | fe6d916 | 2012-12-21 08:53:59 +0000 | [diff] [blame] | 492 | void ReportAllocTypeMismatch(uptr addr, StackTrace *stack, |
| 493 | AllocType alloc_type, |
| 494 | AllocType dealloc_type) { |
| 495 | static const char *alloc_names[] = |
| 496 | {"INVALID", "malloc", "operator new", "operator new []"}; |
| 497 | static const char *dealloc_names[] = |
| 498 | {"INVALID", "free", "operator delete", "operator delete []"}; |
| 499 | CHECK_NE(alloc_type, dealloc_type); |
| 500 | ScopedInErrorReport in_report; |
| 501 | Decorator d; |
| 502 | Printf("%s", d.Warning()); |
| 503 | Report("ERROR: AddressSanitizer: alloc-dealloc-mismatch (%s vs %s) on %p\n", |
| 504 | alloc_names[alloc_type], dealloc_names[dealloc_type], addr); |
| 505 | Printf("%s", d.EndWarning()); |
| 506 | PrintStack(stack); |
| 507 | DescribeHeapAddress(addr, 1); |
| 508 | Report("HINT: if you don't care about these warnings you may set " |
| 509 | "ASAN_OPTIONS=alloc_dealloc_mismatch=0\n"); |
| 510 | } |
| 511 | |
Kostya Serebryany | c3390df | 2012-08-28 11:54:30 +0000 | [diff] [blame] | 512 | void ReportMallocUsableSizeNotOwned(uptr addr, StackTrace *stack) { |
Alexey Samsonov | 9873792 | 2012-08-10 15:13:05 +0000 | [diff] [blame] | 513 | ScopedInErrorReport in_report; |
Kostya Serebryany | 58f5455 | 2012-12-18 07:32:16 +0000 | [diff] [blame] | 514 | Decorator d; |
| 515 | Printf("%s", d.Warning()); |
Kostya Serebryany | 69d8ede | 2012-10-15 13:04:58 +0000 | [diff] [blame] | 516 | Report("ERROR: AddressSanitizer: attempting to call " |
Alexey Samsonov | f7c1d18 | 2012-08-09 08:15:46 +0000 | [diff] [blame] | 517 | "malloc_usable_size() for pointer which is " |
| 518 | "not owned: %p\n", addr); |
Kostya Serebryany | 58f5455 | 2012-12-18 07:32:16 +0000 | [diff] [blame] | 519 | Printf("%s", d.EndWarning()); |
Kostya Serebryany | cc34722 | 2012-08-28 13:49:49 +0000 | [diff] [blame] | 520 | PrintStack(stack); |
Alexey Samsonov | f7c1d18 | 2012-08-09 08:15:46 +0000 | [diff] [blame] | 521 | DescribeHeapAddress(addr, 1); |
Alexey Samsonov | f7c1d18 | 2012-08-09 08:15:46 +0000 | [diff] [blame] | 522 | } |
| 523 | |
Kostya Serebryany | c3390df | 2012-08-28 11:54:30 +0000 | [diff] [blame] | 524 | void ReportAsanGetAllocatedSizeNotOwned(uptr addr, StackTrace *stack) { |
Alexey Samsonov | 9873792 | 2012-08-10 15:13:05 +0000 | [diff] [blame] | 525 | ScopedInErrorReport in_report; |
Kostya Serebryany | 58f5455 | 2012-12-18 07:32:16 +0000 | [diff] [blame] | 526 | Decorator d; |
| 527 | Printf("%s", d.Warning()); |
Kostya Serebryany | 69d8ede | 2012-10-15 13:04:58 +0000 | [diff] [blame] | 528 | Report("ERROR: AddressSanitizer: attempting to call " |
Alexey Samsonov | f7c1d18 | 2012-08-09 08:15:46 +0000 | [diff] [blame] | 529 | "__asan_get_allocated_size() for pointer which is " |
| 530 | "not owned: %p\n", addr); |
Kostya Serebryany | 58f5455 | 2012-12-18 07:32:16 +0000 | [diff] [blame] | 531 | Printf("%s", d.EndWarning()); |
Kostya Serebryany | cc34722 | 2012-08-28 13:49:49 +0000 | [diff] [blame] | 532 | PrintStack(stack); |
Alexey Samsonov | f7c1d18 | 2012-08-09 08:15:46 +0000 | [diff] [blame] | 533 | DescribeHeapAddress(addr, 1); |
Alexey Samsonov | f7c1d18 | 2012-08-09 08:15:46 +0000 | [diff] [blame] | 534 | } |
| 535 | |
Alexey Samsonov | 487fee7 | 2012-08-09 08:32:33 +0000 | [diff] [blame] | 536 | void ReportStringFunctionMemoryRangesOverlap( |
| 537 | const char *function, const char *offset1, uptr length1, |
Kostya Serebryany | c3390df | 2012-08-28 11:54:30 +0000 | [diff] [blame] | 538 | const char *offset2, uptr length2, StackTrace *stack) { |
Alexey Samsonov | 9873792 | 2012-08-10 15:13:05 +0000 | [diff] [blame] | 539 | ScopedInErrorReport in_report; |
Kostya Serebryany | 58f5455 | 2012-12-18 07:32:16 +0000 | [diff] [blame] | 540 | Decorator d; |
| 541 | Printf("%s", d.Warning()); |
Kostya Serebryany | 69d8ede | 2012-10-15 13:04:58 +0000 | [diff] [blame] | 542 | Report("ERROR: AddressSanitizer: %s-param-overlap: " |
Alexey Samsonov | 487fee7 | 2012-08-09 08:32:33 +0000 | [diff] [blame] | 543 | "memory ranges [%p,%p) and [%p, %p) overlap\n", \ |
| 544 | function, offset1, offset1 + length1, offset2, offset2 + length2); |
Kostya Serebryany | 58f5455 | 2012-12-18 07:32:16 +0000 | [diff] [blame] | 545 | Printf("%s", d.EndWarning()); |
Kostya Serebryany | cc34722 | 2012-08-28 13:49:49 +0000 | [diff] [blame] | 546 | PrintStack(stack); |
Alexey Samsonov | 9873792 | 2012-08-10 15:13:05 +0000 | [diff] [blame] | 547 | DescribeAddress((uptr)offset1, length1); |
| 548 | DescribeAddress((uptr)offset2, length2); |
Alexey Samsonov | 487fee7 | 2012-08-09 08:32:33 +0000 | [diff] [blame] | 549 | } |
Alexey Samsonov | f7c1d18 | 2012-08-09 08:15:46 +0000 | [diff] [blame] | 550 | |
Alexey Samsonov | 663c501 | 2012-08-09 12:15:40 +0000 | [diff] [blame] | 551 | // ----------------------- Mac-specific reports ----------------- {{{1 |
| 552 | |
Alexey Samsonov | 663c501 | 2012-08-09 12:15:40 +0000 | [diff] [blame] | 553 | void WarnMacFreeUnallocated( |
Kostya Serebryany | c3390df | 2012-08-28 11:54:30 +0000 | [diff] [blame] | 554 | uptr addr, uptr zone_ptr, const char *zone_name, StackTrace *stack) { |
Alexey Samsonov | 9873792 | 2012-08-10 15:13:05 +0000 | [diff] [blame] | 555 | // Just print a warning here. |
Kostya Serebryany | 283c296 | 2012-08-28 11:34:40 +0000 | [diff] [blame] | 556 | Printf("free_common(%p) -- attempting to free unallocated memory.\n" |
Alexey Samsonov | 663c501 | 2012-08-09 12:15:40 +0000 | [diff] [blame] | 557 | "AddressSanitizer is ignoring this error on Mac OS now.\n", |
| 558 | addr); |
| 559 | PrintZoneForPointer(addr, zone_ptr, zone_name); |
Kostya Serebryany | cc34722 | 2012-08-28 13:49:49 +0000 | [diff] [blame] | 560 | PrintStack(stack); |
Alexey Samsonov | 9873792 | 2012-08-10 15:13:05 +0000 | [diff] [blame] | 561 | DescribeHeapAddress(addr, 1); |
Alexey Samsonov | 663c501 | 2012-08-09 12:15:40 +0000 | [diff] [blame] | 562 | } |
| 563 | |
| 564 | void ReportMacMzReallocUnknown( |
Kostya Serebryany | c3390df | 2012-08-28 11:54:30 +0000 | [diff] [blame] | 565 | uptr addr, uptr zone_ptr, const char *zone_name, StackTrace *stack) { |
Alexey Samsonov | 9873792 | 2012-08-10 15:13:05 +0000 | [diff] [blame] | 566 | ScopedInErrorReport in_report; |
Kostya Serebryany | 283c296 | 2012-08-28 11:34:40 +0000 | [diff] [blame] | 567 | Printf("mz_realloc(%p) -- attempting to realloc unallocated memory.\n" |
Alexey Samsonov | 663c501 | 2012-08-09 12:15:40 +0000 | [diff] [blame] | 568 | "This is an unrecoverable problem, exiting now.\n", |
| 569 | addr); |
| 570 | PrintZoneForPointer(addr, zone_ptr, zone_name); |
Kostya Serebryany | cc34722 | 2012-08-28 13:49:49 +0000 | [diff] [blame] | 571 | PrintStack(stack); |
Alexey Samsonov | 9873792 | 2012-08-10 15:13:05 +0000 | [diff] [blame] | 572 | DescribeHeapAddress(addr, 1); |
Alexey Samsonov | 663c501 | 2012-08-09 12:15:40 +0000 | [diff] [blame] | 573 | } |
| 574 | |
| 575 | void ReportMacCfReallocUnknown( |
Kostya Serebryany | c3390df | 2012-08-28 11:54:30 +0000 | [diff] [blame] | 576 | uptr addr, uptr zone_ptr, const char *zone_name, StackTrace *stack) { |
Alexey Samsonov | 9873792 | 2012-08-10 15:13:05 +0000 | [diff] [blame] | 577 | ScopedInErrorReport in_report; |
Kostya Serebryany | 283c296 | 2012-08-28 11:34:40 +0000 | [diff] [blame] | 578 | Printf("cf_realloc(%p) -- attempting to realloc unallocated memory.\n" |
Alexey Samsonov | 663c501 | 2012-08-09 12:15:40 +0000 | [diff] [blame] | 579 | "This is an unrecoverable problem, exiting now.\n", |
| 580 | addr); |
| 581 | PrintZoneForPointer(addr, zone_ptr, zone_name); |
Kostya Serebryany | cc34722 | 2012-08-28 13:49:49 +0000 | [diff] [blame] | 582 | PrintStack(stack); |
Alexey Samsonov | 9873792 | 2012-08-10 15:13:05 +0000 | [diff] [blame] | 583 | DescribeHeapAddress(addr, 1); |
Alexey Samsonov | c98570b | 2012-08-09 10:56:57 +0000 | [diff] [blame] | 584 | } |
| 585 | |
Alexey Samsonov | 812ff90 | 2012-08-09 11:29:13 +0000 | [diff] [blame] | 586 | } // namespace __asan |
| 587 | |
| 588 | // --------------------------- Interface --------------------- {{{1 |
| 589 | using namespace __asan; // NOLINT |
| 590 | |
| 591 | void __asan_report_error(uptr pc, uptr bp, uptr sp, |
| 592 | uptr addr, bool is_write, uptr access_size) { |
Alexey Samsonov | 9873792 | 2012-08-10 15:13:05 +0000 | [diff] [blame] | 593 | ScopedInErrorReport in_report; |
Alexey Samsonov | c98570b | 2012-08-09 10:56:57 +0000 | [diff] [blame] | 594 | |
Alexey Samsonov | 9873792 | 2012-08-10 15:13:05 +0000 | [diff] [blame] | 595 | // Determine the error type. |
Alexey Samsonov | c98570b | 2012-08-09 10:56:57 +0000 | [diff] [blame] | 596 | const char *bug_descr = "unknown-crash"; |
| 597 | if (AddrIsInMem(addr)) { |
| 598 | u8 *shadow_addr = (u8*)MemToShadow(addr); |
| 599 | // If we are accessing 16 bytes, look at the second shadow byte. |
| 600 | if (*shadow_addr == 0 && access_size > SHADOW_GRANULARITY) |
| 601 | shadow_addr++; |
| 602 | // If we are in the partial right redzone, look at the next shadow byte. |
| 603 | if (*shadow_addr > 0 && *shadow_addr < 128) |
| 604 | shadow_addr++; |
| 605 | switch (*shadow_addr) { |
| 606 | case kAsanHeapLeftRedzoneMagic: |
| 607 | case kAsanHeapRightRedzoneMagic: |
| 608 | bug_descr = "heap-buffer-overflow"; |
| 609 | break; |
| 610 | case kAsanHeapFreeMagic: |
| 611 | bug_descr = "heap-use-after-free"; |
| 612 | break; |
| 613 | case kAsanStackLeftRedzoneMagic: |
| 614 | bug_descr = "stack-buffer-underflow"; |
| 615 | break; |
Kostya Serebryany | 3945c58 | 2012-08-21 14:10:25 +0000 | [diff] [blame] | 616 | case kAsanInitializationOrderMagic: |
| 617 | bug_descr = "initialization-order-fiasco"; |
| 618 | break; |
Alexey Samsonov | c98570b | 2012-08-09 10:56:57 +0000 | [diff] [blame] | 619 | case kAsanStackMidRedzoneMagic: |
| 620 | case kAsanStackRightRedzoneMagic: |
| 621 | case kAsanStackPartialRedzoneMagic: |
| 622 | bug_descr = "stack-buffer-overflow"; |
| 623 | break; |
| 624 | case kAsanStackAfterReturnMagic: |
| 625 | bug_descr = "stack-use-after-return"; |
| 626 | break; |
| 627 | case kAsanUserPoisonedMemoryMagic: |
| 628 | bug_descr = "use-after-poison"; |
| 629 | break; |
Alexey Samsonov | d4b5db8 | 2012-12-04 01:38:15 +0000 | [diff] [blame] | 630 | case kAsanStackUseAfterScopeMagic: |
| 631 | bug_descr = "stack-use-after-scope"; |
| 632 | break; |
Alexey Samsonov | c98570b | 2012-08-09 10:56:57 +0000 | [diff] [blame] | 633 | case kAsanGlobalRedzoneMagic: |
| 634 | bug_descr = "global-buffer-overflow"; |
| 635 | break; |
| 636 | } |
| 637 | } |
Kostya Serebryany | 58f5455 | 2012-12-18 07:32:16 +0000 | [diff] [blame] | 638 | Decorator d; |
| 639 | Printf("%s", d.Warning()); |
Kostya Serebryany | 69d8ede | 2012-10-15 13:04:58 +0000 | [diff] [blame] | 640 | Report("ERROR: AddressSanitizer: %s on address " |
Alexey Samsonov | c98570b | 2012-08-09 10:56:57 +0000 | [diff] [blame] | 641 | "%p at pc 0x%zx bp 0x%zx sp 0x%zx\n", |
| 642 | bug_descr, (void*)addr, pc, bp, sp); |
Kostya Serebryany | 58f5455 | 2012-12-18 07:32:16 +0000 | [diff] [blame] | 643 | Printf("%s", d.EndWarning()); |
Alexey Samsonov | c98570b | 2012-08-09 10:56:57 +0000 | [diff] [blame] | 644 | |
Alexey Samsonov | 9873792 | 2012-08-10 15:13:05 +0000 | [diff] [blame] | 645 | u32 curr_tid = asanThreadRegistry().GetCurrentTidOrInvalid(); |
Kostya Serebryany | 716e2f2 | 2012-12-07 15:15:01 +0000 | [diff] [blame] | 646 | char tname[128]; |
Kostya Serebryany | 58f5455 | 2012-12-18 07:32:16 +0000 | [diff] [blame] | 647 | Printf("%s%s of size %zu at %p thread T%d%s%s\n", |
| 648 | d.Access(), |
| 649 | access_size ? (is_write ? "WRITE" : "READ") : "ACCESS", |
| 650 | access_size, (void*)addr, curr_tid, |
| 651 | ThreadNameWithParenthesis(curr_tid, tname, sizeof(tname)), |
| 652 | d.EndAccess()); |
Alexey Samsonov | c98570b | 2012-08-09 10:56:57 +0000 | [diff] [blame] | 653 | |
Kostya Serebryany | a30c8f9 | 2012-12-13 09:34:23 +0000 | [diff] [blame] | 654 | GET_STACK_TRACE_FATAL(pc, bp); |
Kostya Serebryany | cc34722 | 2012-08-28 13:49:49 +0000 | [diff] [blame] | 655 | PrintStack(&stack); |
Alexey Samsonov | c98570b | 2012-08-09 10:56:57 +0000 | [diff] [blame] | 656 | |
| 657 | DescribeAddress(addr, access_size); |
| 658 | |
Alexey Samsonov | 9873792 | 2012-08-10 15:13:05 +0000 | [diff] [blame] | 659 | PrintShadowMemoryForAddress(addr); |
Alexey Samsonov | c98570b | 2012-08-09 10:56:57 +0000 | [diff] [blame] | 660 | } |
| 661 | |
Alexey Samsonov | c98570b | 2012-08-09 10:56:57 +0000 | [diff] [blame] | 662 | void NOINLINE __asan_set_error_report_callback(void (*callback)(const char*)) { |
| 663 | error_report_callback = callback; |
| 664 | if (callback) { |
| 665 | error_message_buffer_size = 1 << 16; |
| 666 | error_message_buffer = |
| 667 | (char*)MmapOrDie(error_message_buffer_size, __FUNCTION__); |
| 668 | error_message_buffer_pos = 0; |
| 669 | } |
| 670 | } |
Alexey Samsonov | f657a19 | 2012-08-13 11:23:40 +0000 | [diff] [blame] | 671 | |
Kostya Serebryany | 17a7c67 | 2012-12-29 10:18:31 +0000 | [diff] [blame^] | 672 | void __asan_describe_address(uptr addr) { |
| 673 | DescribeAddress(addr, 1); |
| 674 | } |
| 675 | |
Alexey Samsonov | 6a08d29 | 2012-12-07 22:01:28 +0000 | [diff] [blame] | 676 | #if !SANITIZER_SUPPORTS_WEAK_HOOKS |
Alexey Samsonov | 8663343 | 2012-10-02 14:06:39 +0000 | [diff] [blame] | 677 | // Provide default implementation of __asan_on_error that does nothing |
| 678 | // and may be overriden by user. |
| 679 | SANITIZER_WEAK_ATTRIBUTE SANITIZER_INTERFACE_ATTRIBUTE NOINLINE |
| 680 | void __asan_on_error() {} |
Alexey Samsonov | 6a08d29 | 2012-12-07 22:01:28 +0000 | [diff] [blame] | 681 | #endif |