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