asan_report.cc revision c98570beff64ec0a513dcc11a4662ffba70e43dd
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//===----------------------------------------------------------------------===// 14#include "asan_internal.h" 15#include "asan_mapping.h" 16#include "asan_report.h" 17#include "asan_stack.h" 18#include "asan_thread.h" 19#include "asan_thread_registry.h" 20 21namespace __asan { 22 23// ---------------------- Error report callback ------------------- {{{1 24static void (*error_report_callback)(const char*); 25static char *error_message_buffer = 0; 26static uptr error_message_buffer_pos = 0; 27static uptr error_message_buffer_size = 0; 28 29void AppendToErrorMessageBuffer(const char *buffer) { 30 if (error_message_buffer) { 31 uptr length = internal_strlen(buffer); 32 CHECK_GE(error_message_buffer_size, error_message_buffer_pos); 33 uptr remaining = error_message_buffer_size - error_message_buffer_pos; 34 internal_strncpy(error_message_buffer + error_message_buffer_pos, 35 buffer, remaining); 36 error_message_buffer[error_message_buffer_size - 1] = '\0'; 37 // FIXME: reallocate the buffer instead of truncating the message. 38 error_message_buffer_pos += remaining > length ? length : remaining; 39 } 40} 41 42// ---------------------- Address Descriptions ------------------- {{{1 43 44static bool IsASCII(unsigned char c) { 45 return 0x00 <= c && c <= 0x7F; 46} 47 48// Check if the global is a zero-terminated ASCII string. If so, print it. 49static void PrintGlobalNameIfASCII(const __asan_global &g) { 50 for (uptr p = g.beg; p < g.beg + g.size - 1; p++) { 51 if (!IsASCII(*(unsigned char*)p)) return; 52 } 53 if (*(char*)(g.beg + g.size - 1) != 0) return; 54 AsanPrintf(" '%s' is ascii string '%s'\n", g.name, (char*)g.beg); 55} 56 57bool DescribeAddressRelativeToGlobal(uptr addr, const __asan_global &g) { 58 if (addr < g.beg - kGlobalAndStackRedzone) return false; 59 if (addr >= g.beg + g.size_with_redzone) return false; 60 AsanPrintf("%p is located ", (void*)addr); 61 if (addr < g.beg) { 62 AsanPrintf("%zd bytes to the left", g.beg - addr); 63 } else if (addr >= g.beg + g.size) { 64 AsanPrintf("%zd bytes to the right", addr - (g.beg + g.size)); 65 } else { 66 AsanPrintf("%zd bytes inside", addr - g.beg); // Can it happen? 67 } 68 AsanPrintf(" of global variable '%s' (0x%zx) of size %zu\n", 69 g.name, g.beg, g.size); 70 PrintGlobalNameIfASCII(g); 71 return true; 72} 73 74bool DescribeAddressIfShadow(uptr addr) { 75 if (AddrIsInMem(addr)) 76 return false; 77 static const char kAddrInShadowReport[] = 78 "Address %p is located in the %s.\n"; 79 if (AddrIsInShadowGap(addr)) { 80 AsanPrintf(kAddrInShadowReport, addr, "shadow gap area"); 81 return true; 82 } 83 if (AddrIsInHighShadow(addr)) { 84 AsanPrintf(kAddrInShadowReport, addr, "high shadow area"); 85 return true; 86 } 87 if (AddrIsInLowShadow(addr)) { 88 AsanPrintf(kAddrInShadowReport, addr, "low shadow area"); 89 return true; 90 } 91 CHECK(0 && "Address is not in memory and not in shadow?"); 92 return false; 93} 94 95bool DescribeAddressIfStack(uptr addr, uptr access_size) { 96 AsanThread *t = asanThreadRegistry().FindThreadByStackAddress(addr); 97 if (!t) return false; 98 const sptr kBufSize = 4095; 99 char buf[kBufSize]; 100 uptr offset = 0; 101 const char *frame_descr = t->GetFrameNameByAddr(addr, &offset); 102 // This string is created by the compiler and has the following form: 103 // "FunctioName n alloc_1 alloc_2 ... alloc_n" 104 // where alloc_i looks like "offset size len ObjectName ". 105 CHECK(frame_descr); 106 // Report the function name and the offset. 107 const char *name_end = internal_strchr(frame_descr, ' '); 108 CHECK(name_end); 109 buf[0] = 0; 110 internal_strncat(buf, frame_descr, 111 Min(kBufSize, 112 static_cast<sptr>(name_end - frame_descr))); 113 AsanPrintf("Address %p is located at offset %zu " 114 "in frame <%s> of T%d's stack:\n", 115 (void*)addr, offset, buf, t->tid()); 116 // Report the number of stack objects. 117 char *p; 118 uptr n_objects = internal_simple_strtoll(name_end, &p, 10); 119 CHECK(n_objects > 0); 120 AsanPrintf(" This frame has %zu object(s):\n", n_objects); 121 // Report all objects in this frame. 122 for (uptr i = 0; i < n_objects; i++) { 123 uptr beg, size; 124 sptr len; 125 beg = internal_simple_strtoll(p, &p, 10); 126 size = internal_simple_strtoll(p, &p, 10); 127 len = internal_simple_strtoll(p, &p, 10); 128 if (beg <= 0 || size <= 0 || len < 0 || *p != ' ') { 129 AsanPrintf("AddressSanitizer can't parse the stack frame " 130 "descriptor: |%s|\n", frame_descr); 131 break; 132 } 133 p++; 134 buf[0] = 0; 135 internal_strncat(buf, p, Min(kBufSize, len)); 136 p += len; 137 AsanPrintf(" [%zu, %zu) '%s'\n", beg, beg + size, buf); 138 } 139 AsanPrintf("HINT: this may be a false positive if your program uses " 140 "some custom stack unwind mechanism\n" 141 " (longjmp and C++ exceptions *are* supported)\n"); 142 t->summary()->Announce(); 143 return true; 144} 145 146void DescribeAddress(uptr addr, uptr access_size) { 147 // Check if this is shadow or shadow gap. 148 if (DescribeAddressIfShadow(addr)) 149 return; 150 CHECK(AddrIsInMem(addr)); 151 if (DescribeAddressIfGlobal(addr)) 152 return; 153 if (DescribeAddressIfStack(addr, access_size)) 154 return; 155 // Assume it is a heap address. 156 DescribeHeapAddress(addr, access_size); 157} 158 159// -------------------- Different kinds of reports ----------------- {{{1 160 161void ReportSIGSEGV(uptr pc, uptr sp, uptr bp, uptr addr) { 162 AsanReport("ERROR: AddressSanitizer crashed on unknown address %p" 163 " (pc %p sp %p bp %p T%d)\n", 164 (void*)addr, (void*)pc, (void*)sp, (void*)bp, 165 asanThreadRegistry().GetCurrentTidOrInvalid()); 166 AsanPrintf("AddressSanitizer can not provide additional info. ABORTING\n"); 167 GET_STACK_TRACE_WITH_PC_AND_BP(kStackTraceMax, pc, bp); 168 stack.PrintStack(); 169 ShowStatsAndAbort(); 170} 171 172void ReportDoubleFree(uptr addr, AsanStackTrace *stack) { 173 AsanReport("ERROR: AddressSanitizer attempting double-free on %p:\n", addr); 174 stack->PrintStack(); 175 DescribeHeapAddress(addr, 1); 176 ShowStatsAndAbort(); 177} 178 179void ReportFreeNotMalloced(uptr addr, AsanStackTrace *stack) { 180 AsanReport("ERROR: AddressSanitizer attempting free on address " 181 "which was not malloc()-ed: %p\n", addr); 182 stack->PrintStack(); 183 ShowStatsAndAbort(); 184} 185 186void ReportMallocUsableSizeNotOwned(uptr addr, AsanStackTrace *stack) { 187 AsanReport("ERROR: AddressSanitizer attempting to call " 188 "malloc_usable_size() for pointer which is " 189 "not owned: %p\n", addr); 190 stack->PrintStack(); 191 DescribeHeapAddress(addr, 1); 192 ShowStatsAndAbort(); 193} 194 195void ReportAsanGetAllocatedSizeNotOwned(uptr addr, AsanStackTrace *stack) { 196 AsanReport("ERROR: AddressSanitizer attempting to call " 197 "__asan_get_allocated_size() for pointer which is " 198 "not owned: %p\n", addr); 199 stack->PrintStack(); 200 DescribeHeapAddress(addr, 1); 201 ShowStatsAndAbort(); 202} 203 204void ReportStringFunctionMemoryRangesOverlap( 205 const char *function, const char *offset1, uptr length1, 206 const char *offset2, uptr length2, AsanStackTrace *stack) { 207 AsanReport("ERROR: AddressSanitizer %s-param-overlap: " 208 "memory ranges [%p,%p) and [%p, %p) overlap\n", \ 209 function, offset1, offset1 + length1, offset2, offset2 + length2); 210 stack->PrintStack(); 211 ShowStatsAndAbort(); 212} 213 214static void PrintBytes(const char *before, uptr *a) { 215 u8 *bytes = (u8*)a; 216 uptr byte_num = (__WORDSIZE) / 8; 217 AsanPrintf("%s%p:", before, (void*)a); 218 for (uptr i = 0; i < byte_num; i++) { 219 AsanPrintf(" %x%x", bytes[i] >> 4, bytes[i] & 15); 220 } 221 AsanPrintf("\n"); 222} 223 224void ReportInvalidMemoryAccess(uptr pc, uptr bp, uptr sp, uptr addr, 225 bool is_write, uptr access_size) { 226 static atomic_uint32_t num_calls; 227 if (atomic_fetch_add(&num_calls, 1, memory_order_relaxed) != 0) { 228 // Do not print more than one report, otherwise they will mix up. 229 // We can not return here because the function is marked as never-return. 230 AsanPrintf("AddressSanitizer: while reporting a bug found another one." 231 "Ignoring.\n"); 232 SleepForSeconds(5); 233 Die(); 234 } 235 236 AsanPrintf("====================================================" 237 "=============\n"); 238 const char *bug_descr = "unknown-crash"; 239 if (AddrIsInMem(addr)) { 240 u8 *shadow_addr = (u8*)MemToShadow(addr); 241 // If we are accessing 16 bytes, look at the second shadow byte. 242 if (*shadow_addr == 0 && access_size > SHADOW_GRANULARITY) 243 shadow_addr++; 244 // If we are in the partial right redzone, look at the next shadow byte. 245 if (*shadow_addr > 0 && *shadow_addr < 128) 246 shadow_addr++; 247 switch (*shadow_addr) { 248 case kAsanHeapLeftRedzoneMagic: 249 case kAsanHeapRightRedzoneMagic: 250 bug_descr = "heap-buffer-overflow"; 251 break; 252 case kAsanHeapFreeMagic: 253 bug_descr = "heap-use-after-free"; 254 break; 255 case kAsanStackLeftRedzoneMagic: 256 bug_descr = "stack-buffer-underflow"; 257 break; 258 case kAsanStackMidRedzoneMagic: 259 case kAsanStackRightRedzoneMagic: 260 case kAsanStackPartialRedzoneMagic: 261 bug_descr = "stack-buffer-overflow"; 262 break; 263 case kAsanStackAfterReturnMagic: 264 bug_descr = "stack-use-after-return"; 265 break; 266 case kAsanUserPoisonedMemoryMagic: 267 bug_descr = "use-after-poison"; 268 break; 269 case kAsanGlobalRedzoneMagic: 270 bug_descr = "global-buffer-overflow"; 271 break; 272 } 273 } 274 275 AsanThread *curr_thread = asanThreadRegistry().GetCurrent(); 276 u32 curr_tid = asanThreadRegistry().GetCurrentTidOrInvalid(); 277 278 if (curr_thread) { 279 // We started reporting an error message. Stop using the fake stack 280 // in case we will call an instrumented function from a symbolizer. 281 curr_thread->fake_stack().StopUsingFakeStack(); 282 } 283 284 AsanReport("ERROR: AddressSanitizer %s on address " 285 "%p at pc 0x%zx bp 0x%zx sp 0x%zx\n", 286 bug_descr, (void*)addr, pc, bp, sp); 287 288 AsanPrintf("%s of size %zu at %p thread T%d\n", 289 access_size ? (is_write ? "WRITE" : "READ") : "ACCESS", 290 access_size, (void*)addr, curr_tid); 291 292 if (flags()->debug) { 293 PrintBytes("PC: ", (uptr*)pc); 294 } 295 296 GET_STACK_TRACE_WITH_PC_AND_BP(kStackTraceMax, pc, bp); 297 stack.PrintStack(); 298 299 DescribeAddress(addr, access_size); 300 301 if (AddrIsInMem(addr)) { 302 uptr shadow_addr = MemToShadow(addr); 303 AsanReport("ABORTING\n"); 304 __asan_print_accumulated_stats(); 305 AsanPrintf("Shadow byte and word:\n"); 306 AsanPrintf(" %p: %x\n", (void*)shadow_addr, *(unsigned char*)shadow_addr); 307 uptr aligned_shadow = shadow_addr & ~(kWordSize - 1); 308 PrintBytes(" ", (uptr*)(aligned_shadow)); 309 AsanPrintf("More shadow bytes:\n"); 310 PrintBytes(" ", (uptr*)(aligned_shadow-4*kWordSize)); 311 PrintBytes(" ", (uptr*)(aligned_shadow-3*kWordSize)); 312 PrintBytes(" ", (uptr*)(aligned_shadow-2*kWordSize)); 313 PrintBytes(" ", (uptr*)(aligned_shadow-1*kWordSize)); 314 PrintBytes("=>", (uptr*)(aligned_shadow+0*kWordSize)); 315 PrintBytes(" ", (uptr*)(aligned_shadow+1*kWordSize)); 316 PrintBytes(" ", (uptr*)(aligned_shadow+2*kWordSize)); 317 PrintBytes(" ", (uptr*)(aligned_shadow+3*kWordSize)); 318 PrintBytes(" ", (uptr*)(aligned_shadow+4*kWordSize)); 319 } 320 if (error_report_callback) { 321 error_report_callback(error_message_buffer); 322 } 323 Die(); 324} 325 326} // namespace __asan 327 328// --------------------------- Interface --------------------- {{{1 329using namespace __asan; // NOLINT 330 331void __asan_report_error(uptr pc, uptr bp, uptr sp, 332 uptr addr, bool is_write, uptr access_size) { 333 ReportInvalidMemoryAccess(pc, bp, sp, addr, is_write, access_size); 334} 335 336void NOINLINE __asan_set_error_report_callback(void (*callback)(const char*)) { 337 error_report_callback = callback; 338 if (callback) { 339 error_message_buffer_size = 1 << 16; 340 error_message_buffer = 341 (char*)MmapOrDie(error_message_buffer_size, __FUNCTION__); 342 error_message_buffer_pos = 0; 343 } 344} 345