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-08-09 15:40:58 +08:00
|
|
|
#include "asan_thread_registry.h"
|
|
|
|
|
|
|
|
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-08-13 19:23:40 +08:00
|
|
|
static void (*on_error_callback)(void);
|
|
|
|
|
2012-08-10 23:13:05 +08:00
|
|
|
// ---------------------- Helper functions ----------------------- {{{1
|
|
|
|
|
|
|
|
static void PrintBytes(const char *before, uptr *a) {
|
|
|
|
u8 *bytes = (u8*)a;
|
|
|
|
uptr byte_num = (__WORDSIZE) / 8;
|
2012-08-28 19:34:40 +08:00
|
|
|
Printf("%s%p:", before, (void*)a);
|
2012-08-10 23:13:05 +08:00
|
|
|
for (uptr i = 0; i < byte_num; i++) {
|
2012-08-28 19:34:40 +08:00
|
|
|
Printf(" %x%x", bytes[i] >> 4, bytes[i] & 15);
|
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
|
|
|
}
|
|
|
|
|
|
|
|
static void PrintShadowMemoryForAddress(uptr addr) {
|
|
|
|
if (!AddrIsInMem(addr))
|
|
|
|
return;
|
|
|
|
uptr shadow_addr = MemToShadow(addr);
|
2012-08-28 19:34:40 +08:00
|
|
|
Printf("Shadow byte and word:\n");
|
|
|
|
Printf(" %p: %x\n", (void*)shadow_addr, *(unsigned char*)shadow_addr);
|
2012-08-10 23:13:05 +08:00
|
|
|
uptr aligned_shadow = shadow_addr & ~(kWordSize - 1);
|
|
|
|
PrintBytes(" ", (uptr*)(aligned_shadow));
|
2012-08-28 19:34:40 +08:00
|
|
|
Printf("More shadow bytes:\n");
|
2012-08-10 23:13:05 +08:00
|
|
|
for (int i = -4; i <= 4; i++) {
|
|
|
|
const char *prefix = (i == 0) ? "=>" : " ";
|
|
|
|
PrintBytes(prefix, (uptr*)(aligned_shadow + i * kWordSize));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
|
|
|
// 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++) {
|
|
|
|
if (!IsASCII(*(unsigned char*)p)) return;
|
|
|
|
}
|
|
|
|
if (*(char*)(g.beg + g.size - 1) != 0) return;
|
2012-08-28 19:34:40 +08:00
|
|
|
Printf(" '%s' is ascii string '%s'\n", g.name, (char*)g.beg);
|
2012-08-09 17:27:24 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
bool DescribeAddressRelativeToGlobal(uptr addr, const __asan_global &g) {
|
|
|
|
if (addr < g.beg - kGlobalAndStackRedzone) return false;
|
|
|
|
if (addr >= g.beg + g.size_with_redzone) return false;
|
2012-08-28 19:34:40 +08:00
|
|
|
Printf("%p is located ", (void*)addr);
|
2012-08-09 17:27:24 +08:00
|
|
|
if (addr < g.beg) {
|
2012-08-28 19:34:40 +08:00
|
|
|
Printf("%zd bytes to the left", g.beg - addr);
|
2012-08-09 17:27:24 +08:00
|
|
|
} else if (addr >= g.beg + g.size) {
|
2012-08-28 19:34:40 +08:00
|
|
|
Printf("%zd bytes to the right", addr - (g.beg + g.size));
|
2012-08-09 17:27:24 +08:00
|
|
|
} else {
|
2012-08-28 19:34:40 +08:00
|
|
|
Printf("%zd bytes inside", addr - g.beg); // Can it happen?
|
2012-08-09 17:27:24 +08:00
|
|
|
}
|
2012-08-28 19:34:40 +08:00
|
|
|
Printf(" of global variable '%s' (0x%zx) of size %zu\n",
|
2012-08-09 17:27:24 +08:00
|
|
|
g.name, g.beg, g.size);
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool DescribeAddressIfStack(uptr addr, uptr access_size) {
|
|
|
|
AsanThread *t = asanThreadRegistry().FindThreadByStackAddress(addr);
|
|
|
|
if (!t) return false;
|
|
|
|
const sptr kBufSize = 4095;
|
|
|
|
char buf[kBufSize];
|
|
|
|
uptr offset = 0;
|
|
|
|
const char *frame_descr = t->GetFrameNameByAddr(addr, &offset);
|
|
|
|
// This string is created by the compiler and has the following form:
|
|
|
|
// "FunctioName n alloc_1 alloc_2 ... alloc_n"
|
|
|
|
// where alloc_i looks like "offset size len ObjectName ".
|
|
|
|
CHECK(frame_descr);
|
|
|
|
// Report the function name and the offset.
|
|
|
|
const char *name_end = internal_strchr(frame_descr, ' ');
|
|
|
|
CHECK(name_end);
|
|
|
|
buf[0] = 0;
|
|
|
|
internal_strncat(buf, frame_descr,
|
|
|
|
Min(kBufSize,
|
|
|
|
static_cast<sptr>(name_end - frame_descr)));
|
2012-08-28 19:34:40 +08:00
|
|
|
Printf("Address %p is located at offset %zu "
|
2012-08-09 17:06:52 +08:00
|
|
|
"in frame <%s> of T%d's stack:\n",
|
|
|
|
(void*)addr, offset, buf, t->tid());
|
|
|
|
// Report the number of stack objects.
|
|
|
|
char *p;
|
|
|
|
uptr n_objects = internal_simple_strtoll(name_end, &p, 10);
|
|
|
|
CHECK(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.
|
|
|
|
for (uptr i = 0; i < n_objects; i++) {
|
|
|
|
uptr beg, size;
|
|
|
|
sptr len;
|
|
|
|
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;
|
|
|
|
internal_strncat(buf, p, Min(kBufSize, len));
|
|
|
|
p += len;
|
2012-08-28 19:34:40 +08:00
|
|
|
Printf(" [%zu, %zu) '%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-08-09 17:06:52 +08:00
|
|
|
"some custom stack unwind mechanism\n"
|
|
|
|
" (longjmp and C++ exceptions *are* supported)\n");
|
2012-09-05 15:37:15 +08:00
|
|
|
DescribeThread(t->summary());
|
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) {
|
|
|
|
uptr offset;
|
|
|
|
Printf("%p is located ", (void*)addr);
|
|
|
|
if (chunk.AddrIsInside(addr, access_size, &offset)) {
|
|
|
|
Printf("%zu bytes inside of", offset);
|
|
|
|
} else if (chunk.AddrIsAtLeft(addr, access_size, &offset)) {
|
|
|
|
Printf("%zu bytes to the left of", offset);
|
|
|
|
} else if (chunk.AddrIsAtRight(addr, access_size, &offset)) {
|
|
|
|
Printf("%zu bytes to the right of", offset);
|
|
|
|
} else {
|
|
|
|
Printf(" somewhere around (this is AddressSanitizer bug!)");
|
|
|
|
}
|
|
|
|
Printf(" %zu-byte region [%p,%p)\n", chunk.UsedSize(),
|
|
|
|
(void*)(chunk.Beg()), (void*)(chunk.End()));
|
|
|
|
}
|
|
|
|
|
|
|
|
void DescribeHeapAddress(uptr addr, uptr access_size) {
|
|
|
|
AsanChunkView chunk = FindHeapChunkByAddress(addr);
|
|
|
|
if (!chunk.IsValid()) return;
|
|
|
|
DescribeAccessToHeapChunk(chunk, addr, access_size);
|
|
|
|
CHECK(chunk.AllocTid() != kInvalidTid);
|
|
|
|
AsanThreadSummary *alloc_thread =
|
|
|
|
asanThreadRegistry().FindByTid(chunk.AllocTid());
|
|
|
|
StackTrace alloc_stack;
|
|
|
|
chunk.GetAllocStack(&alloc_stack);
|
|
|
|
AsanThread *t = asanThreadRegistry().GetCurrent();
|
|
|
|
CHECK(t);
|
|
|
|
if (chunk.FreeTid() != kInvalidTid) {
|
|
|
|
AsanThreadSummary *free_thread =
|
|
|
|
asanThreadRegistry().FindByTid(chunk.FreeTid());
|
|
|
|
Printf("freed by thread T%d here:\n", free_thread->tid());
|
|
|
|
StackTrace free_stack;
|
|
|
|
chunk.GetFreeStack(&free_stack);
|
|
|
|
PrintStack(&free_stack);
|
|
|
|
Printf("previously allocated by thread T%d here:\n", alloc_thread->tid());
|
|
|
|
PrintStack(&alloc_stack);
|
|
|
|
DescribeThread(t->summary());
|
|
|
|
DescribeThread(free_thread);
|
|
|
|
DescribeThread(alloc_thread);
|
|
|
|
} else {
|
|
|
|
Printf("allocated by thread T%d here:\n", alloc_thread->tid());
|
|
|
|
PrintStack(&alloc_stack);
|
|
|
|
DescribeThread(t->summary());
|
|
|
|
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));
|
|
|
|
if (DescribeAddressIfGlobal(addr))
|
|
|
|
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
|
|
|
|
|
|
|
|
void DescribeThread(AsanThreadSummary *summary) {
|
|
|
|
CHECK(summary);
|
|
|
|
// No need to announce the main thread.
|
|
|
|
if (summary->tid() == 0 || summary->announced()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
summary->set_announced(true);
|
|
|
|
Printf("Thread T%d created by T%d here:\n",
|
|
|
|
summary->tid(), summary->parent_tid());
|
|
|
|
PrintStack(summary->stack());
|
|
|
|
// Recursively described parent thread if needed.
|
|
|
|
if (flags()->print_full_thread_history) {
|
|
|
|
AsanThreadSummary *parent_summary =
|
|
|
|
asanThreadRegistry().FindByTid(summary->parent_tid());
|
|
|
|
DescribeThread(parent_summary);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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");
|
2012-09-17 16:02:19 +08:00
|
|
|
u32 current_tid = asanThreadRegistry().GetCurrentTidOrInvalid();
|
|
|
|
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));
|
|
|
|
}
|
2012-08-13 17:57:19 +08:00
|
|
|
Die();
|
2012-08-10 23:13:05 +08:00
|
|
|
}
|
2012-08-13 19:23:40 +08:00
|
|
|
if (on_error_callback) {
|
|
|
|
on_error_callback();
|
|
|
|
}
|
2012-09-17 16:02:19 +08:00
|
|
|
reporting_thread_tid = asanThreadRegistry().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.
|
2012-09-17 16:02:19 +08:00
|
|
|
AsanThread *curr_thread = asanThreadRegistry().GetCurrent();
|
|
|
|
CHECK(curr_thread);
|
2012-08-10 23:13:05 +08:00
|
|
|
curr_thread->fake_stack().StopUsingFakeStack();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// Destructor is NORETURN, as functions that report errors are.
|
|
|
|
NORETURN ~ScopedInErrorReport() {
|
|
|
|
// Make sure the current thread is announced.
|
|
|
|
AsanThread *curr_thread = asanThreadRegistry().GetCurrent();
|
|
|
|
if (curr_thread) {
|
2012-09-05 15:37:15 +08:00
|
|
|
DescribeThread(curr_thread->summary());
|
2012-08-10 23:13:05 +08:00
|
|
|
}
|
|
|
|
// Print memory stats.
|
|
|
|
__asan_print_accumulated_stats();
|
|
|
|
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();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
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-08-28 19:34:40 +08:00
|
|
|
Report("ERROR: AddressSanitizer crashed 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,
|
|
|
|
asanThreadRegistry().GetCurrentTidOrInvalid());
|
2012-08-28 19:34:40 +08:00
|
|
|
Printf("AddressSanitizer can not provide additional info.\n");
|
2012-08-09 15:40:58 +08:00
|
|
|
GET_STACK_TRACE_WITH_PC_AND_BP(kStackTraceMax, pc, bp);
|
2012-08-28 21:49:49 +08:00
|
|
|
PrintStack(&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-08-28 19:34:40 +08:00
|
|
|
Report("ERROR: AddressSanitizer attempting double-free on %p:\n", addr);
|
2012-08-28 21:49:49 +08:00
|
|
|
PrintStack(stack);
|
2012-08-09 16:15:46 +08:00
|
|
|
DescribeHeapAddress(addr, 1);
|
|
|
|
}
|
|
|
|
|
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-08-28 19:34:40 +08:00
|
|
|
Report("ERROR: AddressSanitizer attempting free on address "
|
2012-08-09 16:15:46 +08:00
|
|
|
"which was not malloc()-ed: %p\n", addr);
|
2012-08-28 21:49:49 +08:00
|
|
|
PrintStack(stack);
|
2012-08-10 23:13:05 +08:00
|
|
|
DescribeHeapAddress(addr, 1);
|
2012-08-09 16:15:46 +08:00
|
|
|
}
|
|
|
|
|
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-08-28 19:34:40 +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-08-28 21:49:49 +08:00
|
|
|
PrintStack(stack);
|
2012-08-09 16:15:46 +08:00
|
|
|
DescribeHeapAddress(addr, 1);
|
|
|
|
}
|
|
|
|
|
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-08-28 19:34:40 +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-08-28 21:49:49 +08:00
|
|
|
PrintStack(stack);
|
2012-08-09 16:15:46 +08:00
|
|
|
DescribeHeapAddress(addr, 1);
|
|
|
|
}
|
|
|
|
|
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-08-28 19:34:40 +08:00
|
|
|
Report("ERROR: AddressSanitizer %s-param-overlap: "
|
2012-08-09 16:32:33 +08:00
|
|
|
"memory ranges [%p,%p) and [%p, %p) overlap\n", \
|
|
|
|
function, offset1, offset1 + length1, offset2, offset2 + length2);
|
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);
|
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;
|
|
|
|
case kAsanGlobalRedzoneMagic:
|
|
|
|
bug_descr = "global-buffer-overflow";
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-08-28 19:34:40 +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-08-10 23:13:05 +08:00
|
|
|
u32 curr_tid = asanThreadRegistry().GetCurrentTidOrInvalid();
|
2012-08-28 19:34:40 +08:00
|
|
|
Printf("%s of size %zu at %p thread T%d\n",
|
2012-08-09 18:56:57 +08:00
|
|
|
access_size ? (is_write ? "WRITE" : "READ") : "ACCESS",
|
|
|
|
access_size, (void*)addr, curr_tid);
|
|
|
|
|
|
|
|
GET_STACK_TRACE_WITH_PC_AND_BP(kStackTraceMax, pc, bp);
|
2012-08-28 21:49:49 +08:00
|
|
|
PrintStack(&stack);
|
2012-08-09 18:56:57 +08:00
|
|
|
|
|
|
|
DescribeAddress(addr, access_size);
|
|
|
|
|
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
|
|
|
|
|
|
|
void NOINLINE __asan_set_on_error_callback(void (*callback)(void)) {
|
|
|
|
on_error_callback = callback;
|
|
|
|
}
|