2012-08-09 15:40:58 +08:00
|
|
|
//===-- asan_report.cc ----------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// This file is a part of AddressSanitizer, an address sanity checker.
|
|
|
|
//
|
|
|
|
// This file contains error reporting code.
|
|
|
|
//===----------------------------------------------------------------------===//
|
2012-08-10 23:13:05 +08:00
|
|
|
#include "asan_flags.h"
|
2012-08-09 15:40:58 +08:00
|
|
|
#include "asan_internal.h"
|
2012-08-09 17:06:52 +08:00
|
|
|
#include "asan_mapping.h"
|
2012-08-09 15:40:58 +08:00
|
|
|
#include "asan_report.h"
|
|
|
|
#include "asan_stack.h"
|
2012-08-09 17:27:24 +08:00
|
|
|
#include "asan_thread.h"
|
2012-12-18 15:32:16 +08:00
|
|
|
#include "sanitizer_common/sanitizer_common.h"
|
2013-05-06 19:27:58 +08:00
|
|
|
#include "sanitizer_common/sanitizer_flags.h"
|
2012-12-18 15:32:16 +08:00
|
|
|
#include "sanitizer_common/sanitizer_report_decorator.h"
|
2012-12-26 22:44:46 +08:00
|
|
|
#include "sanitizer_common/sanitizer_symbolizer.h"
|
2012-08-09 15:40:58 +08:00
|
|
|
|
|
|
|
namespace __asan {
|
|
|
|
|
2012-08-13 19:23:40 +08:00
|
|
|
// -------------------- User-specified callbacks ----------------- {{{1
|
2012-08-09 18:56:57 +08:00
|
|
|
static void (*error_report_callback)(const char*);
|
|
|
|
static char *error_message_buffer = 0;
|
|
|
|
static uptr error_message_buffer_pos = 0;
|
|
|
|
static uptr error_message_buffer_size = 0;
|
|
|
|
|
|
|
|
void AppendToErrorMessageBuffer(const char *buffer) {
|
|
|
|
if (error_message_buffer) {
|
|
|
|
uptr length = internal_strlen(buffer);
|
|
|
|
CHECK_GE(error_message_buffer_size, error_message_buffer_pos);
|
|
|
|
uptr remaining = error_message_buffer_size - error_message_buffer_pos;
|
|
|
|
internal_strncpy(error_message_buffer + error_message_buffer_pos,
|
|
|
|
buffer, remaining);
|
|
|
|
error_message_buffer[error_message_buffer_size - 1] = '\0';
|
|
|
|
// FIXME: reallocate the buffer instead of truncating the message.
|
|
|
|
error_message_buffer_pos += remaining > length ? length : remaining;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-12-18 15:32:16 +08:00
|
|
|
// ---------------------- Decorator ------------------------------ {{{1
|
2012-12-19 17:53:32 +08:00
|
|
|
bool PrintsToTtyCached() {
|
2013-08-16 19:26:26 +08:00
|
|
|
// FIXME: Add proper Windows support to AnsiColorDecorator and re-enable color
|
|
|
|
// printing on Windows.
|
|
|
|
if (SANITIZER_WINDOWS)
|
|
|
|
return 0;
|
|
|
|
|
2012-12-19 17:53:32 +08:00
|
|
|
static int cached = 0;
|
|
|
|
static bool prints_to_tty;
|
|
|
|
if (!cached) { // Ok wrt threads since we are printing only from one thread.
|
|
|
|
prints_to_tty = PrintsToTty();
|
|
|
|
cached = 1;
|
|
|
|
}
|
|
|
|
return prints_to_tty;
|
|
|
|
}
|
2012-12-18 15:32:16 +08:00
|
|
|
class Decorator: private __sanitizer::AnsiColorDecorator {
|
|
|
|
public:
|
2012-12-19 17:53:32 +08:00
|
|
|
Decorator() : __sanitizer::AnsiColorDecorator(PrintsToTtyCached()) { }
|
2012-12-18 15:32:16 +08:00
|
|
|
const char *Warning() { return Red(); }
|
|
|
|
const char *EndWarning() { return Default(); }
|
|
|
|
const char *Access() { return Blue(); }
|
|
|
|
const char *EndAccess() { return Default(); }
|
|
|
|
const char *Location() { return Green(); }
|
|
|
|
const char *EndLocation() { return Default(); }
|
|
|
|
const char *Allocation() { return Magenta(); }
|
|
|
|
const char *EndAllocation() { return Default(); }
|
2012-12-19 17:53:32 +08:00
|
|
|
|
|
|
|
const char *ShadowByte(u8 byte) {
|
|
|
|
switch (byte) {
|
|
|
|
case kAsanHeapLeftRedzoneMagic:
|
|
|
|
case kAsanHeapRightRedzoneMagic:
|
|
|
|
return Red();
|
|
|
|
case kAsanHeapFreeMagic:
|
|
|
|
return Magenta();
|
|
|
|
case kAsanStackLeftRedzoneMagic:
|
|
|
|
case kAsanStackMidRedzoneMagic:
|
|
|
|
case kAsanStackRightRedzoneMagic:
|
|
|
|
case kAsanStackPartialRedzoneMagic:
|
|
|
|
return Red();
|
|
|
|
case kAsanStackAfterReturnMagic:
|
|
|
|
return Magenta();
|
|
|
|
case kAsanInitializationOrderMagic:
|
|
|
|
return Cyan();
|
|
|
|
case kAsanUserPoisonedMemoryMagic:
|
|
|
|
return Blue();
|
|
|
|
case kAsanStackUseAfterScopeMagic:
|
|
|
|
return Magenta();
|
|
|
|
case kAsanGlobalRedzoneMagic:
|
|
|
|
return Red();
|
|
|
|
case kAsanInternalHeapMagic:
|
|
|
|
return Yellow();
|
|
|
|
default:
|
|
|
|
return Default();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
const char *EndShadowByte() { return Default(); }
|
2012-12-18 15:32:16 +08:00
|
|
|
};
|
|
|
|
|
2012-08-10 23:13:05 +08:00
|
|
|
// ---------------------- Helper functions ----------------------- {{{1
|
|
|
|
|
2012-12-19 17:53:32 +08:00
|
|
|
static void PrintShadowByte(const char *before, u8 byte,
|
|
|
|
const char *after = "\n") {
|
|
|
|
Decorator d;
|
|
|
|
Printf("%s%s%x%x%s%s", before,
|
|
|
|
d.ShadowByte(byte), byte >> 4, byte & 15, d.EndShadowByte(), after);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void PrintShadowBytes(const char *before, u8 *bytes,
|
|
|
|
u8 *guilty, uptr n) {
|
|
|
|
Decorator d;
|
|
|
|
if (before)
|
|
|
|
Printf("%s%p:", before, bytes);
|
|
|
|
for (uptr i = 0; i < n; i++) {
|
|
|
|
u8 *p = bytes + i;
|
|
|
|
const char *before = p == guilty ? "[" :
|
|
|
|
p - 1 == guilty ? "" : " ";
|
|
|
|
const char *after = p == guilty ? "]" : "";
|
|
|
|
PrintShadowByte(before, *p, after);
|
2012-08-10 23:13:05 +08:00
|
|
|
}
|
2012-08-28 19:34:40 +08:00
|
|
|
Printf("\n");
|
2012-08-10 23:13:05 +08:00
|
|
|
}
|
|
|
|
|
2013-01-28 15:34:22 +08:00
|
|
|
static void PrintLegend() {
|
2012-12-19 17:53:32 +08:00
|
|
|
Printf("Shadow byte legend (one shadow byte represents %d "
|
|
|
|
"application bytes):\n", (int)SHADOW_GRANULARITY);
|
|
|
|
PrintShadowByte(" Addressable: ", 0);
|
|
|
|
Printf(" Partially addressable: ");
|
2013-05-29 22:11:44 +08:00
|
|
|
for (u8 i = 1; i < SHADOW_GRANULARITY; i++)
|
2012-12-19 17:53:32 +08:00
|
|
|
PrintShadowByte("", i, " ");
|
|
|
|
Printf("\n");
|
|
|
|
PrintShadowByte(" Heap left redzone: ", kAsanHeapLeftRedzoneMagic);
|
2013-04-10 15:00:25 +08:00
|
|
|
PrintShadowByte(" Heap right redzone: ", kAsanHeapRightRedzoneMagic);
|
|
|
|
PrintShadowByte(" Freed heap region: ", kAsanHeapFreeMagic);
|
2012-12-19 17:53:32 +08:00
|
|
|
PrintShadowByte(" Stack left redzone: ", kAsanStackLeftRedzoneMagic);
|
|
|
|
PrintShadowByte(" Stack mid redzone: ", kAsanStackMidRedzoneMagic);
|
|
|
|
PrintShadowByte(" Stack right redzone: ", kAsanStackRightRedzoneMagic);
|
|
|
|
PrintShadowByte(" Stack partial redzone: ", kAsanStackPartialRedzoneMagic);
|
|
|
|
PrintShadowByte(" Stack after return: ", kAsanStackAfterReturnMagic);
|
|
|
|
PrintShadowByte(" Stack use after scope: ", kAsanStackUseAfterScopeMagic);
|
|
|
|
PrintShadowByte(" Global redzone: ", kAsanGlobalRedzoneMagic);
|
|
|
|
PrintShadowByte(" Global init order: ", kAsanInitializationOrderMagic);
|
|
|
|
PrintShadowByte(" Poisoned by user: ", kAsanUserPoisonedMemoryMagic);
|
|
|
|
PrintShadowByte(" ASan internal: ", kAsanInternalHeapMagic);
|
2012-08-10 23:13:05 +08:00
|
|
|
}
|
|
|
|
|
2013-01-28 15:34:22 +08:00
|
|
|
static void PrintShadowMemoryForAddress(uptr addr) {
|
|
|
|
if (!AddrIsInMem(addr))
|
|
|
|
return;
|
|
|
|
uptr shadow_addr = MemToShadow(addr);
|
|
|
|
const uptr n_bytes_per_row = 16;
|
|
|
|
uptr aligned_shadow = shadow_addr & ~(n_bytes_per_row - 1);
|
|
|
|
Printf("Shadow bytes around the buggy address:\n");
|
|
|
|
for (int i = -5; i <= 5; i++) {
|
|
|
|
const char *prefix = (i == 0) ? "=>" : " ";
|
|
|
|
PrintShadowBytes(prefix,
|
|
|
|
(u8*)(aligned_shadow + i * n_bytes_per_row),
|
|
|
|
(u8*)shadow_addr, n_bytes_per_row);
|
|
|
|
}
|
|
|
|
if (flags()->print_legend)
|
|
|
|
PrintLegend();
|
|
|
|
}
|
|
|
|
|
2012-08-10 23:13:05 +08:00
|
|
|
static void PrintZoneForPointer(uptr ptr, uptr zone_ptr,
|
|
|
|
const char *zone_name) {
|
|
|
|
if (zone_ptr) {
|
|
|
|
if (zone_name) {
|
2012-08-28 19:34:40 +08:00
|
|
|
Printf("malloc_zone_from_ptr(%p) = %p, which is %s\n",
|
2012-08-10 23:13:05 +08:00
|
|
|
ptr, zone_ptr, zone_name);
|
|
|
|
} else {
|
2012-08-28 19:34:40 +08:00
|
|
|
Printf("malloc_zone_from_ptr(%p) = %p, which doesn't have a name\n",
|
2012-08-10 23:13:05 +08:00
|
|
|
ptr, zone_ptr);
|
|
|
|
}
|
|
|
|
} else {
|
2012-08-28 19:34:40 +08:00
|
|
|
Printf("malloc_zone_from_ptr(%p) = 0\n", ptr);
|
2012-08-10 23:13:05 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-08-09 17:06:52 +08:00
|
|
|
// ---------------------- Address Descriptions ------------------- {{{1
|
|
|
|
|
2012-08-09 17:27:24 +08:00
|
|
|
static bool IsASCII(unsigned char c) {
|
2012-08-10 23:13:05 +08:00
|
|
|
return /*0x00 <= c &&*/ c <= 0x7F;
|
2012-08-09 17:27:24 +08:00
|
|
|
}
|
|
|
|
|
2013-03-27 18:41:22 +08:00
|
|
|
static const char *MaybeDemangleGlobalName(const char *name) {
|
|
|
|
// We can spoil names of globals with C linkage, so use an heuristic
|
|
|
|
// approach to check if the name should be demangled.
|
|
|
|
return (name[0] == '_' && name[1] == 'Z') ? Demangle(name) : name;
|
|
|
|
}
|
|
|
|
|
2013-04-01 16:57:38 +08:00
|
|
|
// Check if the global is a zero-terminated ASCII string. If so, print it.
|
|
|
|
static void PrintGlobalNameIfASCII(const __asan_global &g) {
|
|
|
|
for (uptr p = g.beg; p < g.beg + g.size - 1; p++) {
|
|
|
|
unsigned char c = *(unsigned char*)p;
|
|
|
|
if (c == '\0' || !IsASCII(c)) return;
|
|
|
|
}
|
|
|
|
if (*(char*)(g.beg + g.size - 1) != '\0') return;
|
|
|
|
Printf(" '%s' is ascii string '%s'\n",
|
|
|
|
MaybeDemangleGlobalName(g.name), (char*)g.beg);
|
|
|
|
}
|
|
|
|
|
2013-02-05 22:32:03 +08:00
|
|
|
bool DescribeAddressRelativeToGlobal(uptr addr, uptr size,
|
|
|
|
const __asan_global &g) {
|
2013-01-23 19:14:21 +08:00
|
|
|
static const uptr kMinimalDistanceFromAnotherGlobal = 64;
|
|
|
|
if (addr <= g.beg - kMinimalDistanceFromAnotherGlobal) return false;
|
2012-08-09 17:27:24 +08:00
|
|
|
if (addr >= g.beg + g.size_with_redzone) return false;
|
2012-12-18 15:32:16 +08:00
|
|
|
Decorator d;
|
|
|
|
Printf("%s", d.Location());
|
2012-08-09 17:27:24 +08:00
|
|
|
if (addr < g.beg) {
|
2013-02-05 22:32:03 +08:00
|
|
|
Printf("%p is located %zd bytes to the left", (void*)addr, g.beg - addr);
|
|
|
|
} else if (addr + size > g.beg + g.size) {
|
|
|
|
if (addr < g.beg + g.size)
|
|
|
|
addr = g.beg + g.size;
|
|
|
|
Printf("%p is located %zd bytes to the right", (void*)addr,
|
|
|
|
addr - (g.beg + g.size));
|
2012-08-09 17:27:24 +08:00
|
|
|
} else {
|
2013-02-05 22:32:03 +08:00
|
|
|
// Can it happen?
|
|
|
|
Printf("%p is located %zd bytes inside", (void*)addr, addr - g.beg);
|
2012-08-09 17:27:24 +08:00
|
|
|
}
|
2013-03-18 16:04:55 +08:00
|
|
|
Printf(" of global variable '%s' from '%s' (0x%zx) of size %zu\n",
|
2013-03-27 18:41:22 +08:00
|
|
|
MaybeDemangleGlobalName(g.name), g.module_name, g.beg, g.size);
|
2012-12-18 15:32:16 +08:00
|
|
|
Printf("%s", d.EndLocation());
|
2012-08-09 17:27:24 +08:00
|
|
|
PrintGlobalNameIfASCII(g);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2012-08-09 17:06:52 +08:00
|
|
|
bool DescribeAddressIfShadow(uptr addr) {
|
|
|
|
if (AddrIsInMem(addr))
|
|
|
|
return false;
|
|
|
|
static const char kAddrInShadowReport[] =
|
|
|
|
"Address %p is located in the %s.\n";
|
|
|
|
if (AddrIsInShadowGap(addr)) {
|
2012-08-28 19:34:40 +08:00
|
|
|
Printf(kAddrInShadowReport, addr, "shadow gap area");
|
2012-08-09 17:06:52 +08:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
if (AddrIsInHighShadow(addr)) {
|
2012-08-28 19:34:40 +08:00
|
|
|
Printf(kAddrInShadowReport, addr, "high shadow area");
|
2012-08-09 17:06:52 +08:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
if (AddrIsInLowShadow(addr)) {
|
2012-08-28 19:34:40 +08:00
|
|
|
Printf(kAddrInShadowReport, addr, "low shadow area");
|
2012-08-09 17:06:52 +08:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
CHECK(0 && "Address is not in memory and not in shadow?");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
[asan] Change the way we report the alloca frame on stack-buff-overflow.
Before: the function name was stored by the compiler as a constant string
and the run-time was printing it.
Now: the PC is stored instead and the run-time prints the full symbolized frame.
This adds a couple of instructions into every function with non-empty stack frame,
but also reduces the binary size because we store less strings (I saw 2% size reduction).
This change bumps the asan ABI version to v3.
compiler-rt part, llvm part will follow.
Example of report (now):
==31711==ERROR: AddressSanitizer: stack-buffer-overflow on address 0x7fffa77cf1c5 at pc 0x41feb0 bp 0x7fffa77cefb0 sp 0x7fffa77cefa8
READ of size 1 at 0x7fffa77cf1c5 thread T0
#0 0x41feaf in Frame0(int, char*, char*, char*) stack-oob-frames.cc:20
#1 0x41f7ff in Frame1(int, char*, char*) stack-oob-frames.cc:24
#2 0x41f477 in Frame2(int, char*) stack-oob-frames.cc:28
#3 0x41f194 in Frame3(int) stack-oob-frames.cc:32
#4 0x41eee0 in main stack-oob-frames.cc:38
#5 0x7f0c5566f76c (/lib/x86_64-linux-gnu/libc.so.6+0x2176c)
#6 0x41eb1c (/usr/local/google/kcc/llvm_cmake/a.out+0x41eb1c)
Address 0x7fffa77cf1c5 is located in stack of thread T0 at offset 293 in frame
#0 0x41f87f in Frame0(int, char*, char*, char*) stack-oob-frames.cc:12 <<<<<<<<<<<<<< this is new
This frame has 6 object(s):
[32, 36) 'frame.addr'
[96, 104) 'a.addr'
[160, 168) 'b.addr'
[224, 232) 'c.addr'
[288, 292) 's'
[352, 360) 'd'
llvm-svn: 177723
2013-03-22 18:36:24 +08:00
|
|
|
// Return " (thread_name) " or an empty string if the name is empty.
|
|
|
|
const char *ThreadNameWithParenthesis(AsanThreadContext *t, char buff[],
|
|
|
|
uptr buff_len) {
|
|
|
|
const char *name = t->name;
|
|
|
|
if (name[0] == '\0') return "";
|
|
|
|
buff[0] = 0;
|
|
|
|
internal_strncat(buff, " (", 3);
|
|
|
|
internal_strncat(buff, name, buff_len - 4);
|
|
|
|
internal_strncat(buff, ")", 2);
|
|
|
|
return buff;
|
|
|
|
}
|
|
|
|
|
|
|
|
const char *ThreadNameWithParenthesis(u32 tid, char buff[],
|
|
|
|
uptr buff_len) {
|
|
|
|
if (tid == kInvalidTid) return "";
|
|
|
|
asanThreadRegistry().CheckLocked();
|
|
|
|
AsanThreadContext *t = GetThreadContextByTidLocked(tid);
|
|
|
|
return ThreadNameWithParenthesis(t, buff, buff_len);
|
|
|
|
}
|
|
|
|
|
2012-08-09 17:06:52 +08:00
|
|
|
bool DescribeAddressIfStack(uptr addr, uptr access_size) {
|
2013-03-21 19:23:41 +08:00
|
|
|
AsanThread *t = FindThreadByStackAddress(addr);
|
2012-08-09 17:06:52 +08:00
|
|
|
if (!t) return false;
|
2013-05-29 22:11:44 +08:00
|
|
|
const s64 kBufSize = 4095;
|
2012-08-09 17:06:52 +08:00
|
|
|
char buf[kBufSize];
|
|
|
|
uptr offset = 0;
|
[asan] Change the way we report the alloca frame on stack-buff-overflow.
Before: the function name was stored by the compiler as a constant string
and the run-time was printing it.
Now: the PC is stored instead and the run-time prints the full symbolized frame.
This adds a couple of instructions into every function with non-empty stack frame,
but also reduces the binary size because we store less strings (I saw 2% size reduction).
This change bumps the asan ABI version to v3.
compiler-rt part, llvm part will follow.
Example of report (now):
==31711==ERROR: AddressSanitizer: stack-buffer-overflow on address 0x7fffa77cf1c5 at pc 0x41feb0 bp 0x7fffa77cefb0 sp 0x7fffa77cefa8
READ of size 1 at 0x7fffa77cf1c5 thread T0
#0 0x41feaf in Frame0(int, char*, char*, char*) stack-oob-frames.cc:20
#1 0x41f7ff in Frame1(int, char*, char*) stack-oob-frames.cc:24
#2 0x41f477 in Frame2(int, char*) stack-oob-frames.cc:28
#3 0x41f194 in Frame3(int) stack-oob-frames.cc:32
#4 0x41eee0 in main stack-oob-frames.cc:38
#5 0x7f0c5566f76c (/lib/x86_64-linux-gnu/libc.so.6+0x2176c)
#6 0x41eb1c (/usr/local/google/kcc/llvm_cmake/a.out+0x41eb1c)
Address 0x7fffa77cf1c5 is located in stack of thread T0 at offset 293 in frame
#0 0x41f87f in Frame0(int, char*, char*, char*) stack-oob-frames.cc:12 <<<<<<<<<<<<<< this is new
This frame has 6 object(s):
[32, 36) 'frame.addr'
[96, 104) 'a.addr'
[160, 168) 'b.addr'
[224, 232) 'c.addr'
[288, 292) 's'
[352, 360) 'd'
llvm-svn: 177723
2013-03-22 18:36:24 +08:00
|
|
|
uptr frame_pc = 0;
|
|
|
|
char tname[128];
|
|
|
|
const char *frame_descr = t->GetFrameNameByAddr(addr, &offset, &frame_pc);
|
2013-05-22 22:21:34 +08:00
|
|
|
|
|
|
|
#ifdef __powerpc64__
|
|
|
|
// On PowerPC64, the address of a function actually points to a
|
|
|
|
// three-doubleword data structure with the first field containing
|
|
|
|
// the address of the function's code.
|
|
|
|
frame_pc = *reinterpret_cast<uptr *>(frame_pc);
|
|
|
|
#endif
|
|
|
|
|
2012-08-09 17:06:52 +08:00
|
|
|
// This string is created by the compiler and has the following form:
|
[asan] Change the way we report the alloca frame on stack-buff-overflow.
Before: the function name was stored by the compiler as a constant string
and the run-time was printing it.
Now: the PC is stored instead and the run-time prints the full symbolized frame.
This adds a couple of instructions into every function with non-empty stack frame,
but also reduces the binary size because we store less strings (I saw 2% size reduction).
This change bumps the asan ABI version to v3.
compiler-rt part, llvm part will follow.
Example of report (now):
==31711==ERROR: AddressSanitizer: stack-buffer-overflow on address 0x7fffa77cf1c5 at pc 0x41feb0 bp 0x7fffa77cefb0 sp 0x7fffa77cefa8
READ of size 1 at 0x7fffa77cf1c5 thread T0
#0 0x41feaf in Frame0(int, char*, char*, char*) stack-oob-frames.cc:20
#1 0x41f7ff in Frame1(int, char*, char*) stack-oob-frames.cc:24
#2 0x41f477 in Frame2(int, char*) stack-oob-frames.cc:28
#3 0x41f194 in Frame3(int) stack-oob-frames.cc:32
#4 0x41eee0 in main stack-oob-frames.cc:38
#5 0x7f0c5566f76c (/lib/x86_64-linux-gnu/libc.so.6+0x2176c)
#6 0x41eb1c (/usr/local/google/kcc/llvm_cmake/a.out+0x41eb1c)
Address 0x7fffa77cf1c5 is located in stack of thread T0 at offset 293 in frame
#0 0x41f87f in Frame0(int, char*, char*, char*) stack-oob-frames.cc:12 <<<<<<<<<<<<<< this is new
This frame has 6 object(s):
[32, 36) 'frame.addr'
[96, 104) 'a.addr'
[160, 168) 'b.addr'
[224, 232) 'c.addr'
[288, 292) 's'
[352, 360) 'd'
llvm-svn: 177723
2013-03-22 18:36:24 +08:00
|
|
|
// "n alloc_1 alloc_2 ... alloc_n"
|
2012-08-09 17:06:52 +08:00
|
|
|
// where alloc_i looks like "offset size len ObjectName ".
|
|
|
|
CHECK(frame_descr);
|
2012-12-18 15:32:16 +08:00
|
|
|
Decorator d;
|
|
|
|
Printf("%s", d.Location());
|
[asan] Change the way we report the alloca frame on stack-buff-overflow.
Before: the function name was stored by the compiler as a constant string
and the run-time was printing it.
Now: the PC is stored instead and the run-time prints the full symbolized frame.
This adds a couple of instructions into every function with non-empty stack frame,
but also reduces the binary size because we store less strings (I saw 2% size reduction).
This change bumps the asan ABI version to v3.
compiler-rt part, llvm part will follow.
Example of report (now):
==31711==ERROR: AddressSanitizer: stack-buffer-overflow on address 0x7fffa77cf1c5 at pc 0x41feb0 bp 0x7fffa77cefb0 sp 0x7fffa77cefa8
READ of size 1 at 0x7fffa77cf1c5 thread T0
#0 0x41feaf in Frame0(int, char*, char*, char*) stack-oob-frames.cc:20
#1 0x41f7ff in Frame1(int, char*, char*) stack-oob-frames.cc:24
#2 0x41f477 in Frame2(int, char*) stack-oob-frames.cc:28
#3 0x41f194 in Frame3(int) stack-oob-frames.cc:32
#4 0x41eee0 in main stack-oob-frames.cc:38
#5 0x7f0c5566f76c (/lib/x86_64-linux-gnu/libc.so.6+0x2176c)
#6 0x41eb1c (/usr/local/google/kcc/llvm_cmake/a.out+0x41eb1c)
Address 0x7fffa77cf1c5 is located in stack of thread T0 at offset 293 in frame
#0 0x41f87f in Frame0(int, char*, char*, char*) stack-oob-frames.cc:12 <<<<<<<<<<<<<< this is new
This frame has 6 object(s):
[32, 36) 'frame.addr'
[96, 104) 'a.addr'
[160, 168) 'b.addr'
[224, 232) 'c.addr'
[288, 292) 's'
[352, 360) 'd'
llvm-svn: 177723
2013-03-22 18:36:24 +08:00
|
|
|
Printf("Address %p is located in stack of thread T%d%s "
|
|
|
|
"at offset %zu in frame\n",
|
|
|
|
addr, t->tid(),
|
|
|
|
ThreadNameWithParenthesis(t->tid(), tname, sizeof(tname)),
|
|
|
|
offset);
|
|
|
|
// Now we print the frame where the alloca has happened.
|
|
|
|
// We print this frame as a stack trace with one element.
|
|
|
|
// The symbolizer may print more than one frame if inlining was involved.
|
|
|
|
// The frame numbers may be different than those in the stack trace printed
|
|
|
|
// previously. That's unfortunate, but I have no better solution,
|
|
|
|
// especially given that the alloca may be from entirely different place
|
|
|
|
// (e.g. use-after-scope, or different thread's stack).
|
|
|
|
StackTrace alloca_stack;
|
|
|
|
alloca_stack.trace[0] = frame_pc + 16;
|
|
|
|
alloca_stack.size = 1;
|
2012-12-18 15:32:16 +08:00
|
|
|
Printf("%s", d.EndLocation());
|
[asan] Change the way we report the alloca frame on stack-buff-overflow.
Before: the function name was stored by the compiler as a constant string
and the run-time was printing it.
Now: the PC is stored instead and the run-time prints the full symbolized frame.
This adds a couple of instructions into every function with non-empty stack frame,
but also reduces the binary size because we store less strings (I saw 2% size reduction).
This change bumps the asan ABI version to v3.
compiler-rt part, llvm part will follow.
Example of report (now):
==31711==ERROR: AddressSanitizer: stack-buffer-overflow on address 0x7fffa77cf1c5 at pc 0x41feb0 bp 0x7fffa77cefb0 sp 0x7fffa77cefa8
READ of size 1 at 0x7fffa77cf1c5 thread T0
#0 0x41feaf in Frame0(int, char*, char*, char*) stack-oob-frames.cc:20
#1 0x41f7ff in Frame1(int, char*, char*) stack-oob-frames.cc:24
#2 0x41f477 in Frame2(int, char*) stack-oob-frames.cc:28
#3 0x41f194 in Frame3(int) stack-oob-frames.cc:32
#4 0x41eee0 in main stack-oob-frames.cc:38
#5 0x7f0c5566f76c (/lib/x86_64-linux-gnu/libc.so.6+0x2176c)
#6 0x41eb1c (/usr/local/google/kcc/llvm_cmake/a.out+0x41eb1c)
Address 0x7fffa77cf1c5 is located in stack of thread T0 at offset 293 in frame
#0 0x41f87f in Frame0(int, char*, char*, char*) stack-oob-frames.cc:12 <<<<<<<<<<<<<< this is new
This frame has 6 object(s):
[32, 36) 'frame.addr'
[96, 104) 'a.addr'
[160, 168) 'b.addr'
[224, 232) 'c.addr'
[288, 292) 's'
[352, 360) 'd'
llvm-svn: 177723
2013-03-22 18:36:24 +08:00
|
|
|
PrintStack(&alloca_stack);
|
2012-08-09 17:06:52 +08:00
|
|
|
// Report the number of stack objects.
|
|
|
|
char *p;
|
2013-05-29 22:11:44 +08:00
|
|
|
s64 n_objects = internal_simple_strtoll(frame_descr, &p, 10);
|
2013-04-05 22:40:25 +08:00
|
|
|
CHECK_GT(n_objects, 0);
|
2012-08-28 19:34:40 +08:00
|
|
|
Printf(" This frame has %zu object(s):\n", n_objects);
|
2012-08-09 17:06:52 +08:00
|
|
|
// Report all objects in this frame.
|
2013-05-29 22:11:44 +08:00
|
|
|
for (s64 i = 0; i < n_objects; i++) {
|
|
|
|
s64 beg, size;
|
|
|
|
s64 len;
|
2012-08-09 17:06:52 +08:00
|
|
|
beg = internal_simple_strtoll(p, &p, 10);
|
|
|
|
size = internal_simple_strtoll(p, &p, 10);
|
|
|
|
len = internal_simple_strtoll(p, &p, 10);
|
|
|
|
if (beg <= 0 || size <= 0 || len < 0 || *p != ' ') {
|
2012-08-28 19:34:40 +08:00
|
|
|
Printf("AddressSanitizer can't parse the stack frame "
|
2012-08-09 17:06:52 +08:00
|
|
|
"descriptor: |%s|\n", frame_descr);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
p++;
|
|
|
|
buf[0] = 0;
|
2013-05-29 22:11:44 +08:00
|
|
|
internal_strncat(buf, p, static_cast<uptr>(Min(kBufSize, len)));
|
2012-08-09 17:06:52 +08:00
|
|
|
p += len;
|
2013-05-29 22:11:44 +08:00
|
|
|
Printf(" [%lld, %lld) '%s'\n", beg, beg + size, buf);
|
2012-08-09 17:06:52 +08:00
|
|
|
}
|
2012-08-28 19:34:40 +08:00
|
|
|
Printf("HINT: this may be a false positive if your program uses "
|
2012-11-23 17:46:34 +08:00
|
|
|
"some custom stack unwind mechanism or swapcontext\n"
|
2012-08-09 17:06:52 +08:00
|
|
|
" (longjmp and C++ exceptions *are* supported)\n");
|
2013-03-21 19:23:41 +08:00
|
|
|
DescribeThread(t->context());
|
2012-08-09 17:06:52 +08:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2012-09-18 15:38:10 +08:00
|
|
|
static void DescribeAccessToHeapChunk(AsanChunkView chunk, uptr addr,
|
|
|
|
uptr access_size) {
|
2013-02-05 22:32:03 +08:00
|
|
|
sptr offset;
|
2012-12-18 15:32:16 +08:00
|
|
|
Decorator d;
|
|
|
|
Printf("%s", d.Location());
|
2013-02-05 22:32:03 +08:00
|
|
|
if (chunk.AddrIsAtLeft(addr, access_size, &offset)) {
|
|
|
|
Printf("%p is located %zd bytes to the left of", (void*)addr, offset);
|
2012-09-18 15:38:10 +08:00
|
|
|
} else if (chunk.AddrIsAtRight(addr, access_size, &offset)) {
|
2013-02-05 22:32:03 +08:00
|
|
|
if (offset < 0) {
|
|
|
|
addr -= offset;
|
|
|
|
offset = 0;
|
|
|
|
}
|
|
|
|
Printf("%p is located %zd bytes to the right of", (void*)addr, offset);
|
|
|
|
} else if (chunk.AddrIsInside(addr, access_size, &offset)) {
|
|
|
|
Printf("%p is located %zd bytes inside of", (void*)addr, offset);
|
2012-09-18 15:38:10 +08:00
|
|
|
} else {
|
2013-02-05 22:32:03 +08:00
|
|
|
Printf("%p is located somewhere around (this is AddressSanitizer bug!)",
|
|
|
|
(void*)addr);
|
2012-09-18 15:38:10 +08:00
|
|
|
}
|
|
|
|
Printf(" %zu-byte region [%p,%p)\n", chunk.UsedSize(),
|
|
|
|
(void*)(chunk.Beg()), (void*)(chunk.End()));
|
2012-12-18 15:32:16 +08:00
|
|
|
Printf("%s", d.EndLocation());
|
2012-09-18 15:38:10 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void DescribeHeapAddress(uptr addr, uptr access_size) {
|
|
|
|
AsanChunkView chunk = FindHeapChunkByAddress(addr);
|
|
|
|
if (!chunk.IsValid()) return;
|
|
|
|
DescribeAccessToHeapChunk(chunk, addr, access_size);
|
|
|
|
CHECK(chunk.AllocTid() != kInvalidTid);
|
2013-03-21 19:23:41 +08:00
|
|
|
asanThreadRegistry().CheckLocked();
|
|
|
|
AsanThreadContext *alloc_thread =
|
|
|
|
GetThreadContextByTidLocked(chunk.AllocTid());
|
2012-09-18 15:38:10 +08:00
|
|
|
StackTrace alloc_stack;
|
|
|
|
chunk.GetAllocStack(&alloc_stack);
|
2013-03-20 17:23:28 +08:00
|
|
|
AsanThread *t = GetCurrentThread();
|
2012-09-18 15:38:10 +08:00
|
|
|
CHECK(t);
|
2012-12-07 23:15:01 +08:00
|
|
|
char tname[128];
|
2012-12-18 15:32:16 +08:00
|
|
|
Decorator d;
|
2012-09-18 15:38:10 +08:00
|
|
|
if (chunk.FreeTid() != kInvalidTid) {
|
2013-03-21 19:23:41 +08:00
|
|
|
AsanThreadContext *free_thread =
|
|
|
|
GetThreadContextByTidLocked(chunk.FreeTid());
|
2012-12-18 15:32:16 +08:00
|
|
|
Printf("%sfreed by thread T%d%s here:%s\n", d.Allocation(),
|
2013-03-21 19:23:41 +08:00
|
|
|
free_thread->tid,
|
2012-12-18 15:32:16 +08:00
|
|
|
ThreadNameWithParenthesis(free_thread, tname, sizeof(tname)),
|
|
|
|
d.EndAllocation());
|
2012-09-18 15:38:10 +08:00
|
|
|
StackTrace free_stack;
|
|
|
|
chunk.GetFreeStack(&free_stack);
|
|
|
|
PrintStack(&free_stack);
|
2012-12-18 15:32:16 +08:00
|
|
|
Printf("%spreviously allocated by thread T%d%s here:%s\n",
|
2013-03-21 19:23:41 +08:00
|
|
|
d.Allocation(), alloc_thread->tid,
|
2012-12-18 15:32:16 +08:00
|
|
|
ThreadNameWithParenthesis(alloc_thread, tname, sizeof(tname)),
|
|
|
|
d.EndAllocation());
|
2012-09-18 15:38:10 +08:00
|
|
|
PrintStack(&alloc_stack);
|
2013-03-21 19:23:41 +08:00
|
|
|
DescribeThread(t->context());
|
2012-09-18 15:38:10 +08:00
|
|
|
DescribeThread(free_thread);
|
|
|
|
DescribeThread(alloc_thread);
|
|
|
|
} else {
|
2012-12-18 15:32:16 +08:00
|
|
|
Printf("%sallocated by thread T%d%s here:%s\n", d.Allocation(),
|
2013-03-21 19:23:41 +08:00
|
|
|
alloc_thread->tid,
|
2012-12-18 15:32:16 +08:00
|
|
|
ThreadNameWithParenthesis(alloc_thread, tname, sizeof(tname)),
|
|
|
|
d.EndAllocation());
|
2012-09-18 15:38:10 +08:00
|
|
|
PrintStack(&alloc_stack);
|
2013-03-21 19:23:41 +08:00
|
|
|
DescribeThread(t->context());
|
2012-09-18 15:38:10 +08:00
|
|
|
DescribeThread(alloc_thread);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-08-09 17:06:52 +08:00
|
|
|
void DescribeAddress(uptr addr, uptr access_size) {
|
|
|
|
// Check if this is shadow or shadow gap.
|
|
|
|
if (DescribeAddressIfShadow(addr))
|
|
|
|
return;
|
|
|
|
CHECK(AddrIsInMem(addr));
|
2013-02-05 22:32:03 +08:00
|
|
|
if (DescribeAddressIfGlobal(addr, access_size))
|
2012-08-09 17:06:52 +08:00
|
|
|
return;
|
|
|
|
if (DescribeAddressIfStack(addr, access_size))
|
|
|
|
return;
|
|
|
|
// Assume it is a heap address.
|
|
|
|
DescribeHeapAddress(addr, access_size);
|
|
|
|
}
|
|
|
|
|
2012-09-05 15:37:15 +08:00
|
|
|
// ------------------- Thread description -------------------- {{{1
|
|
|
|
|
2013-03-21 19:23:41 +08:00
|
|
|
void DescribeThread(AsanThreadContext *context) {
|
|
|
|
CHECK(context);
|
|
|
|
asanThreadRegistry().CheckLocked();
|
2012-09-05 15:37:15 +08:00
|
|
|
// No need to announce the main thread.
|
2013-03-21 19:23:41 +08:00
|
|
|
if (context->tid == 0 || context->announced) {
|
2012-09-05 15:37:15 +08:00
|
|
|
return;
|
|
|
|
}
|
2013-03-21 19:23:41 +08:00
|
|
|
context->announced = true;
|
2012-12-07 23:15:01 +08:00
|
|
|
char tname[128];
|
2013-03-21 19:23:41 +08:00
|
|
|
Printf("Thread T%d%s", context->tid,
|
|
|
|
ThreadNameWithParenthesis(context->tid, tname, sizeof(tname)));
|
2012-12-07 23:15:01 +08:00
|
|
|
Printf(" created by T%d%s here:\n",
|
2013-03-21 19:23:41 +08:00
|
|
|
context->parent_tid,
|
|
|
|
ThreadNameWithParenthesis(context->parent_tid,
|
2012-12-07 23:15:01 +08:00
|
|
|
tname, sizeof(tname)));
|
2013-03-21 19:23:41 +08:00
|
|
|
PrintStack(&context->stack);
|
2012-09-05 15:37:15 +08:00
|
|
|
// Recursively described parent thread if needed.
|
|
|
|
if (flags()->print_full_thread_history) {
|
2013-03-21 19:23:41 +08:00
|
|
|
AsanThreadContext *parent_context =
|
|
|
|
GetThreadContextByTidLocked(context->parent_tid);
|
|
|
|
DescribeThread(parent_context);
|
2012-09-05 15:37:15 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-08-09 17:06:52 +08:00
|
|
|
// -------------------- Different kinds of reports ----------------- {{{1
|
|
|
|
|
2012-08-10 23:13:05 +08:00
|
|
|
// Use ScopedInErrorReport to run common actions just before and
|
|
|
|
// immediately after printing error report.
|
|
|
|
class ScopedInErrorReport {
|
|
|
|
public:
|
|
|
|
ScopedInErrorReport() {
|
|
|
|
static atomic_uint32_t num_calls;
|
2012-09-17 16:02:19 +08:00
|
|
|
static u32 reporting_thread_tid;
|
2012-08-10 23:13:05 +08:00
|
|
|
if (atomic_fetch_add(&num_calls, 1, memory_order_relaxed) != 0) {
|
|
|
|
// Do not print more than one report, otherwise they will mix up.
|
|
|
|
// Error reporting functions shouldn't return at this situation, as
|
|
|
|
// they are defined as no-return.
|
2012-08-28 19:34:40 +08:00
|
|
|
Report("AddressSanitizer: while reporting a bug found another one."
|
2012-08-10 23:13:05 +08:00
|
|
|
"Ignoring.\n");
|
2013-03-20 17:23:28 +08:00
|
|
|
u32 current_tid = GetCurrentTidOrInvalid();
|
2012-09-17 16:02:19 +08:00
|
|
|
if (current_tid != reporting_thread_tid) {
|
|
|
|
// ASan found two bugs in different threads simultaneously. Sleep
|
|
|
|
// long enough to make sure that the thread which started to print
|
|
|
|
// an error report will finish doing it.
|
|
|
|
SleepForSeconds(Max(100, flags()->sleep_before_dying + 1));
|
|
|
|
}
|
2013-02-20 21:54:32 +08:00
|
|
|
// If we're still not dead for some reason, use raw _exit() instead of
|
2012-11-19 19:22:22 +08:00
|
|
|
// Die() to bypass any additional checks.
|
2013-02-20 21:54:32 +08:00
|
|
|
internal__exit(flags()->exitcode);
|
2012-08-10 23:13:05 +08:00
|
|
|
}
|
2012-12-08 06:01:28 +08:00
|
|
|
ASAN_ON_ERROR();
|
2013-04-05 15:30:29 +08:00
|
|
|
// Make sure the registry and sanitizer report mutexes are locked while
|
|
|
|
// we're printing an error report.
|
|
|
|
// We can lock them only here to avoid self-deadlock in case of
|
2013-03-21 19:23:41 +08:00
|
|
|
// recursive reports.
|
|
|
|
asanThreadRegistry().Lock();
|
2013-04-05 15:30:29 +08:00
|
|
|
CommonSanitizerReportMutex.Lock();
|
2013-03-20 17:23:28 +08:00
|
|
|
reporting_thread_tid = GetCurrentTidOrInvalid();
|
2012-08-28 19:34:40 +08:00
|
|
|
Printf("===================================================="
|
2012-09-17 16:02:19 +08:00
|
|
|
"=============\n");
|
|
|
|
if (reporting_thread_tid != kInvalidTid) {
|
2012-08-10 23:13:05 +08:00
|
|
|
// We started reporting an error message. Stop using the fake stack
|
|
|
|
// in case we call an instrumented function from a symbolizer.
|
2013-03-20 17:23:28 +08:00
|
|
|
AsanThread *curr_thread = GetCurrentThread();
|
2012-09-17 16:02:19 +08:00
|
|
|
CHECK(curr_thread);
|
2013-06-26 20:16:05 +08:00
|
|
|
if (curr_thread->fake_stack())
|
|
|
|
curr_thread->fake_stack()->StopUsingFakeStack();
|
2012-08-10 23:13:05 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
// Destructor is NORETURN, as functions that report errors are.
|
|
|
|
NORETURN ~ScopedInErrorReport() {
|
|
|
|
// Make sure the current thread is announced.
|
2013-03-20 17:23:28 +08:00
|
|
|
AsanThread *curr_thread = GetCurrentThread();
|
2012-08-10 23:13:05 +08:00
|
|
|
if (curr_thread) {
|
2013-03-21 19:23:41 +08:00
|
|
|
DescribeThread(curr_thread->context());
|
2012-08-10 23:13:05 +08:00
|
|
|
}
|
|
|
|
// Print memory stats.
|
2013-01-28 15:34:22 +08:00
|
|
|
if (flags()->print_stats)
|
|
|
|
__asan_print_accumulated_stats();
|
2012-08-10 23:13:05 +08:00
|
|
|
if (error_report_callback) {
|
|
|
|
error_report_callback(error_message_buffer);
|
|
|
|
}
|
2012-08-28 19:34:40 +08:00
|
|
|
Report("ABORTING\n");
|
2012-08-10 23:13:05 +08:00
|
|
|
Die();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2013-02-06 20:36:49 +08:00
|
|
|
static void ReportSummary(const char *error_type, StackTrace *stack) {
|
|
|
|
if (!stack->size) return;
|
|
|
|
if (IsSymbolizerAvailable()) {
|
|
|
|
AddressInfo ai;
|
|
|
|
// Currently, we include the first stack frame into the report summary.
|
|
|
|
// Maybe sometimes we need to choose another frame (e.g. skip memcpy/etc).
|
2013-04-11 19:45:04 +08:00
|
|
|
uptr pc = StackTrace::GetPreviousInstructionPc(stack->trace[0]);
|
|
|
|
SymbolizeCode(pc, &ai, 1);
|
2013-02-06 20:36:49 +08:00
|
|
|
ReportErrorSummary(error_type,
|
2013-05-06 19:27:58 +08:00
|
|
|
StripPathPrefix(ai.file,
|
|
|
|
common_flags()->strip_path_prefix),
|
2013-02-06 20:36:49 +08:00
|
|
|
ai.line, ai.function);
|
|
|
|
}
|
|
|
|
// FIXME: do we need to print anything at all if there is no symbolizer?
|
|
|
|
}
|
|
|
|
|
2012-08-09 15:40:58 +08:00
|
|
|
void ReportSIGSEGV(uptr pc, uptr sp, uptr bp, uptr addr) {
|
2012-08-10 23:13:05 +08:00
|
|
|
ScopedInErrorReport in_report;
|
2012-12-18 15:32:16 +08:00
|
|
|
Decorator d;
|
|
|
|
Printf("%s", d.Warning());
|
2012-10-15 21:04:58 +08:00
|
|
|
Report("ERROR: AddressSanitizer: SEGV on unknown address %p"
|
2012-08-09 15:40:58 +08:00
|
|
|
" (pc %p sp %p bp %p T%d)\n",
|
|
|
|
(void*)addr, (void*)pc, (void*)sp, (void*)bp,
|
2013-03-20 17:23:28 +08:00
|
|
|
GetCurrentTidOrInvalid());
|
2012-12-18 15:32:16 +08:00
|
|
|
Printf("%s", d.EndWarning());
|
2012-08-28 19:34:40 +08:00
|
|
|
Printf("AddressSanitizer can not provide additional info.\n");
|
2012-12-13 17:34:23 +08:00
|
|
|
GET_STACK_TRACE_FATAL(pc, bp);
|
2012-08-28 21:49:49 +08:00
|
|
|
PrintStack(&stack);
|
2013-02-06 20:36:49 +08:00
|
|
|
ReportSummary("SEGV", &stack);
|
2012-08-09 15:40:58 +08:00
|
|
|
}
|
|
|
|
|
2012-08-28 19:54:30 +08:00
|
|
|
void ReportDoubleFree(uptr addr, StackTrace *stack) {
|
2012-08-10 23:13:05 +08:00
|
|
|
ScopedInErrorReport in_report;
|
2012-12-18 15:32:16 +08:00
|
|
|
Decorator d;
|
|
|
|
Printf("%s", d.Warning());
|
2013-03-26 16:01:37 +08:00
|
|
|
char tname[128];
|
|
|
|
u32 curr_tid = GetCurrentTidOrInvalid();
|
|
|
|
Report("ERROR: AddressSanitizer: attempting double-free on %p in "
|
|
|
|
"thread T%d%s:\n",
|
|
|
|
addr, curr_tid,
|
|
|
|
ThreadNameWithParenthesis(curr_tid, tname, sizeof(tname)));
|
|
|
|
|
2012-12-18 15:32:16 +08:00
|
|
|
Printf("%s", d.EndWarning());
|
2012-08-28 21:49:49 +08:00
|
|
|
PrintStack(stack);
|
2012-08-09 16:15:46 +08:00
|
|
|
DescribeHeapAddress(addr, 1);
|
2013-02-06 20:36:49 +08:00
|
|
|
ReportSummary("double-free", stack);
|
2012-08-09 16:15:46 +08:00
|
|
|
}
|
|
|
|
|
2012-08-28 19:54:30 +08:00
|
|
|
void ReportFreeNotMalloced(uptr addr, StackTrace *stack) {
|
2012-08-10 23:13:05 +08:00
|
|
|
ScopedInErrorReport in_report;
|
2012-12-18 15:32:16 +08:00
|
|
|
Decorator d;
|
|
|
|
Printf("%s", d.Warning());
|
2013-03-26 16:01:37 +08:00
|
|
|
char tname[128];
|
|
|
|
u32 curr_tid = GetCurrentTidOrInvalid();
|
2012-10-15 21:04:58 +08:00
|
|
|
Report("ERROR: AddressSanitizer: attempting free on address "
|
2013-03-26 16:01:37 +08:00
|
|
|
"which was not malloc()-ed: %p in thread T%d%s\n", addr,
|
|
|
|
curr_tid, ThreadNameWithParenthesis(curr_tid, tname, sizeof(tname)));
|
2012-12-18 15:32:16 +08:00
|
|
|
Printf("%s", d.EndWarning());
|
2012-08-28 21:49:49 +08:00
|
|
|
PrintStack(stack);
|
2012-08-10 23:13:05 +08:00
|
|
|
DescribeHeapAddress(addr, 1);
|
2013-02-06 20:36:49 +08:00
|
|
|
ReportSummary("bad-free", stack);
|
2012-08-09 16:15:46 +08:00
|
|
|
}
|
|
|
|
|
2012-12-21 16:53:59 +08:00
|
|
|
void ReportAllocTypeMismatch(uptr addr, StackTrace *stack,
|
|
|
|
AllocType alloc_type,
|
|
|
|
AllocType dealloc_type) {
|
|
|
|
static const char *alloc_names[] =
|
|
|
|
{"INVALID", "malloc", "operator new", "operator new []"};
|
|
|
|
static const char *dealloc_names[] =
|
|
|
|
{"INVALID", "free", "operator delete", "operator delete []"};
|
|
|
|
CHECK_NE(alloc_type, dealloc_type);
|
|
|
|
ScopedInErrorReport in_report;
|
|
|
|
Decorator d;
|
|
|
|
Printf("%s", d.Warning());
|
|
|
|
Report("ERROR: AddressSanitizer: alloc-dealloc-mismatch (%s vs %s) on %p\n",
|
|
|
|
alloc_names[alloc_type], dealloc_names[dealloc_type], addr);
|
|
|
|
Printf("%s", d.EndWarning());
|
|
|
|
PrintStack(stack);
|
|
|
|
DescribeHeapAddress(addr, 1);
|
2013-02-06 20:36:49 +08:00
|
|
|
ReportSummary("alloc-dealloc-mismatch", stack);
|
2012-12-21 16:53:59 +08:00
|
|
|
Report("HINT: if you don't care about these warnings you may set "
|
|
|
|
"ASAN_OPTIONS=alloc_dealloc_mismatch=0\n");
|
|
|
|
}
|
|
|
|
|
2012-08-28 19:54:30 +08:00
|
|
|
void ReportMallocUsableSizeNotOwned(uptr addr, StackTrace *stack) {
|
2012-08-10 23:13:05 +08:00
|
|
|
ScopedInErrorReport in_report;
|
2012-12-18 15:32:16 +08:00
|
|
|
Decorator d;
|
|
|
|
Printf("%s", d.Warning());
|
2012-10-15 21:04:58 +08:00
|
|
|
Report("ERROR: AddressSanitizer: attempting to call "
|
2012-08-09 16:15:46 +08:00
|
|
|
"malloc_usable_size() for pointer which is "
|
|
|
|
"not owned: %p\n", addr);
|
2012-12-18 15:32:16 +08:00
|
|
|
Printf("%s", d.EndWarning());
|
2012-08-28 21:49:49 +08:00
|
|
|
PrintStack(stack);
|
2012-08-09 16:15:46 +08:00
|
|
|
DescribeHeapAddress(addr, 1);
|
2013-02-06 20:36:49 +08:00
|
|
|
ReportSummary("bad-malloc_usable_size", stack);
|
2012-08-09 16:15:46 +08:00
|
|
|
}
|
|
|
|
|
2012-08-28 19:54:30 +08:00
|
|
|
void ReportAsanGetAllocatedSizeNotOwned(uptr addr, StackTrace *stack) {
|
2012-08-10 23:13:05 +08:00
|
|
|
ScopedInErrorReport in_report;
|
2012-12-18 15:32:16 +08:00
|
|
|
Decorator d;
|
|
|
|
Printf("%s", d.Warning());
|
2012-10-15 21:04:58 +08:00
|
|
|
Report("ERROR: AddressSanitizer: attempting to call "
|
2012-08-09 16:15:46 +08:00
|
|
|
"__asan_get_allocated_size() for pointer which is "
|
|
|
|
"not owned: %p\n", addr);
|
2012-12-18 15:32:16 +08:00
|
|
|
Printf("%s", d.EndWarning());
|
2012-08-28 21:49:49 +08:00
|
|
|
PrintStack(stack);
|
2012-08-09 16:15:46 +08:00
|
|
|
DescribeHeapAddress(addr, 1);
|
2013-02-06 20:36:49 +08:00
|
|
|
ReportSummary("bad-__asan_get_allocated_size", stack);
|
2012-08-09 16:15:46 +08:00
|
|
|
}
|
|
|
|
|
2012-08-09 16:32:33 +08:00
|
|
|
void ReportStringFunctionMemoryRangesOverlap(
|
|
|
|
const char *function, const char *offset1, uptr length1,
|
2012-08-28 19:54:30 +08:00
|
|
|
const char *offset2, uptr length2, StackTrace *stack) {
|
2012-08-10 23:13:05 +08:00
|
|
|
ScopedInErrorReport in_report;
|
2012-12-18 15:32:16 +08:00
|
|
|
Decorator d;
|
2013-02-06 20:36:49 +08:00
|
|
|
char bug_type[100];
|
|
|
|
internal_snprintf(bug_type, sizeof(bug_type), "%s-param-overlap", function);
|
2012-12-18 15:32:16 +08:00
|
|
|
Printf("%s", d.Warning());
|
2013-02-06 20:36:49 +08:00
|
|
|
Report("ERROR: AddressSanitizer: %s: "
|
2012-08-09 16:32:33 +08:00
|
|
|
"memory ranges [%p,%p) and [%p, %p) overlap\n", \
|
2013-02-06 20:36:49 +08:00
|
|
|
bug_type, offset1, offset1 + length1, offset2, offset2 + length2);
|
2012-12-18 15:32:16 +08:00
|
|
|
Printf("%s", d.EndWarning());
|
2012-08-28 21:49:49 +08:00
|
|
|
PrintStack(stack);
|
2012-08-10 23:13:05 +08:00
|
|
|
DescribeAddress((uptr)offset1, length1);
|
|
|
|
DescribeAddress((uptr)offset2, length2);
|
2013-02-06 20:36:49 +08:00
|
|
|
ReportSummary(bug_type, stack);
|
2012-08-09 16:32:33 +08:00
|
|
|
}
|
2012-08-09 16:15:46 +08:00
|
|
|
|
2012-08-09 20:15:40 +08:00
|
|
|
// ----------------------- Mac-specific reports ----------------- {{{1
|
|
|
|
|
|
|
|
void WarnMacFreeUnallocated(
|
2012-08-28 19:54:30 +08:00
|
|
|
uptr addr, uptr zone_ptr, const char *zone_name, StackTrace *stack) {
|
2012-08-10 23:13:05 +08:00
|
|
|
// Just print a warning here.
|
2012-08-28 19:34:40 +08:00
|
|
|
Printf("free_common(%p) -- attempting to free unallocated memory.\n"
|
2012-08-09 20:15:40 +08:00
|
|
|
"AddressSanitizer is ignoring this error on Mac OS now.\n",
|
|
|
|
addr);
|
|
|
|
PrintZoneForPointer(addr, zone_ptr, zone_name);
|
2012-08-28 21:49:49 +08:00
|
|
|
PrintStack(stack);
|
2012-08-10 23:13:05 +08:00
|
|
|
DescribeHeapAddress(addr, 1);
|
2012-08-09 20:15:40 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void ReportMacMzReallocUnknown(
|
2012-08-28 19:54:30 +08:00
|
|
|
uptr addr, uptr zone_ptr, const char *zone_name, StackTrace *stack) {
|
2012-08-10 23:13:05 +08:00
|
|
|
ScopedInErrorReport in_report;
|
2012-08-28 19:34:40 +08:00
|
|
|
Printf("mz_realloc(%p) -- attempting to realloc unallocated memory.\n"
|
2012-08-09 20:15:40 +08:00
|
|
|
"This is an unrecoverable problem, exiting now.\n",
|
|
|
|
addr);
|
|
|
|
PrintZoneForPointer(addr, zone_ptr, zone_name);
|
2012-08-28 21:49:49 +08:00
|
|
|
PrintStack(stack);
|
2012-08-10 23:13:05 +08:00
|
|
|
DescribeHeapAddress(addr, 1);
|
2012-08-09 20:15:40 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void ReportMacCfReallocUnknown(
|
2012-08-28 19:54:30 +08:00
|
|
|
uptr addr, uptr zone_ptr, const char *zone_name, StackTrace *stack) {
|
2012-08-10 23:13:05 +08:00
|
|
|
ScopedInErrorReport in_report;
|
2012-08-28 19:34:40 +08:00
|
|
|
Printf("cf_realloc(%p) -- attempting to realloc unallocated memory.\n"
|
2012-08-09 20:15:40 +08:00
|
|
|
"This is an unrecoverable problem, exiting now.\n",
|
|
|
|
addr);
|
|
|
|
PrintZoneForPointer(addr, zone_ptr, zone_name);
|
2012-08-28 21:49:49 +08:00
|
|
|
PrintStack(stack);
|
2012-08-10 23:13:05 +08:00
|
|
|
DescribeHeapAddress(addr, 1);
|
2012-08-09 18:56:57 +08:00
|
|
|
}
|
|
|
|
|
2012-08-09 19:29:13 +08:00
|
|
|
} // namespace __asan
|
|
|
|
|
|
|
|
// --------------------------- Interface --------------------- {{{1
|
|
|
|
using namespace __asan; // NOLINT
|
|
|
|
|
|
|
|
void __asan_report_error(uptr pc, uptr bp, uptr sp,
|
|
|
|
uptr addr, bool is_write, uptr access_size) {
|
2012-08-10 23:13:05 +08:00
|
|
|
ScopedInErrorReport in_report;
|
2012-08-09 18:56:57 +08:00
|
|
|
|
2012-08-10 23:13:05 +08:00
|
|
|
// Determine the error type.
|
2012-08-09 18:56:57 +08:00
|
|
|
const char *bug_descr = "unknown-crash";
|
|
|
|
if (AddrIsInMem(addr)) {
|
|
|
|
u8 *shadow_addr = (u8*)MemToShadow(addr);
|
|
|
|
// If we are accessing 16 bytes, look at the second shadow byte.
|
|
|
|
if (*shadow_addr == 0 && access_size > SHADOW_GRANULARITY)
|
|
|
|
shadow_addr++;
|
|
|
|
// If we are in the partial right redzone, look at the next shadow byte.
|
|
|
|
if (*shadow_addr > 0 && *shadow_addr < 128)
|
|
|
|
shadow_addr++;
|
|
|
|
switch (*shadow_addr) {
|
|
|
|
case kAsanHeapLeftRedzoneMagic:
|
|
|
|
case kAsanHeapRightRedzoneMagic:
|
|
|
|
bug_descr = "heap-buffer-overflow";
|
|
|
|
break;
|
|
|
|
case kAsanHeapFreeMagic:
|
|
|
|
bug_descr = "heap-use-after-free";
|
|
|
|
break;
|
|
|
|
case kAsanStackLeftRedzoneMagic:
|
|
|
|
bug_descr = "stack-buffer-underflow";
|
|
|
|
break;
|
2012-08-21 22:10:25 +08:00
|
|
|
case kAsanInitializationOrderMagic:
|
|
|
|
bug_descr = "initialization-order-fiasco";
|
|
|
|
break;
|
2012-08-09 18:56:57 +08:00
|
|
|
case kAsanStackMidRedzoneMagic:
|
|
|
|
case kAsanStackRightRedzoneMagic:
|
|
|
|
case kAsanStackPartialRedzoneMagic:
|
|
|
|
bug_descr = "stack-buffer-overflow";
|
|
|
|
break;
|
|
|
|
case kAsanStackAfterReturnMagic:
|
|
|
|
bug_descr = "stack-use-after-return";
|
|
|
|
break;
|
|
|
|
case kAsanUserPoisonedMemoryMagic:
|
|
|
|
bug_descr = "use-after-poison";
|
|
|
|
break;
|
2012-12-04 09:38:15 +08:00
|
|
|
case kAsanStackUseAfterScopeMagic:
|
|
|
|
bug_descr = "stack-use-after-scope";
|
|
|
|
break;
|
2012-08-09 18:56:57 +08:00
|
|
|
case kAsanGlobalRedzoneMagic:
|
|
|
|
bug_descr = "global-buffer-overflow";
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2012-12-18 15:32:16 +08:00
|
|
|
Decorator d;
|
|
|
|
Printf("%s", d.Warning());
|
2012-10-15 21:04:58 +08:00
|
|
|
Report("ERROR: AddressSanitizer: %s on address "
|
2012-08-09 18:56:57 +08:00
|
|
|
"%p at pc 0x%zx bp 0x%zx sp 0x%zx\n",
|
|
|
|
bug_descr, (void*)addr, pc, bp, sp);
|
2012-12-18 15:32:16 +08:00
|
|
|
Printf("%s", d.EndWarning());
|
2012-08-09 18:56:57 +08:00
|
|
|
|
2013-03-20 17:23:28 +08:00
|
|
|
u32 curr_tid = GetCurrentTidOrInvalid();
|
2012-12-07 23:15:01 +08:00
|
|
|
char tname[128];
|
2012-12-18 15:32:16 +08:00
|
|
|
Printf("%s%s of size %zu at %p thread T%d%s%s\n",
|
|
|
|
d.Access(),
|
|
|
|
access_size ? (is_write ? "WRITE" : "READ") : "ACCESS",
|
|
|
|
access_size, (void*)addr, curr_tid,
|
|
|
|
ThreadNameWithParenthesis(curr_tid, tname, sizeof(tname)),
|
|
|
|
d.EndAccess());
|
2012-08-09 18:56:57 +08:00
|
|
|
|
2012-12-13 17:34:23 +08:00
|
|
|
GET_STACK_TRACE_FATAL(pc, bp);
|
2012-08-28 21:49:49 +08:00
|
|
|
PrintStack(&stack);
|
2012-08-09 18:56:57 +08:00
|
|
|
|
|
|
|
DescribeAddress(addr, access_size);
|
2013-02-06 20:36:49 +08:00
|
|
|
ReportSummary(bug_descr, &stack);
|
2012-08-10 23:13:05 +08:00
|
|
|
PrintShadowMemoryForAddress(addr);
|
2012-08-09 18:56:57 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void NOINLINE __asan_set_error_report_callback(void (*callback)(const char*)) {
|
|
|
|
error_report_callback = callback;
|
|
|
|
if (callback) {
|
|
|
|
error_message_buffer_size = 1 << 16;
|
|
|
|
error_message_buffer =
|
|
|
|
(char*)MmapOrDie(error_message_buffer_size, __FUNCTION__);
|
|
|
|
error_message_buffer_pos = 0;
|
|
|
|
}
|
|
|
|
}
|
2012-08-13 19:23:40 +08:00
|
|
|
|
2012-12-29 18:18:31 +08:00
|
|
|
void __asan_describe_address(uptr addr) {
|
|
|
|
DescribeAddress(addr, 1);
|
|
|
|
}
|
|
|
|
|
2012-12-08 06:01:28 +08:00
|
|
|
#if !SANITIZER_SUPPORTS_WEAK_HOOKS
|
2012-10-02 22:06:39 +08:00
|
|
|
// Provide default implementation of __asan_on_error that does nothing
|
|
|
|
// and may be overriden by user.
|
2013-08-13 19:42:45 +08:00
|
|
|
SANITIZER_INTERFACE_ATTRIBUTE SANITIZER_WEAK_ATTRIBUTE NOINLINE
|
2012-10-02 22:06:39 +08:00
|
|
|
void __asan_on_error() {}
|
2012-12-08 06:01:28 +08:00
|
|
|
#endif
|