2012-06-04 21:50:10 +08:00
|
|
|
//===-- asan_rtl.cc -------------------------------------------------------===//
|
2011-11-30 09:07:02 +08:00
|
|
|
//
|
|
|
|
// 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.
|
|
|
|
//
|
|
|
|
// Main file of the ASan run-time library.
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
#include "asan_allocator.h"
|
|
|
|
#include "asan_interceptors.h"
|
|
|
|
#include "asan_interface.h"
|
|
|
|
#include "asan_internal.h"
|
|
|
|
#include "asan_lock.h"
|
|
|
|
#include "asan_mapping.h"
|
|
|
|
#include "asan_stack.h"
|
|
|
|
#include "asan_stats.h"
|
|
|
|
#include "asan_thread.h"
|
|
|
|
#include "asan_thread_registry.h"
|
2012-06-05 15:25:47 +08:00
|
|
|
#include "sanitizer_common/sanitizer_libc.h"
|
2011-11-30 09:07:02 +08:00
|
|
|
|
2012-06-06 15:02:44 +08:00
|
|
|
namespace __sanitizer {
|
|
|
|
using namespace __asan;
|
|
|
|
|
|
|
|
void Die() {
|
|
|
|
static int num_calls = 0;
|
|
|
|
if (AtomicInc(&num_calls) > 1) {
|
|
|
|
// Don't die twice - run a busy loop.
|
|
|
|
while (1) { }
|
|
|
|
}
|
|
|
|
if (FLAG_sleep_before_dying) {
|
2012-06-06 18:46:00 +08:00
|
|
|
Report("Sleeping for %zd second(s)\n", FLAG_sleep_before_dying);
|
2012-06-06 15:02:44 +08:00
|
|
|
SleepForSeconds(FLAG_sleep_before_dying);
|
|
|
|
}
|
|
|
|
if (FLAG_unmap_shadow_on_exit)
|
2012-06-07 00:15:07 +08:00
|
|
|
UnmapOrDie((void*)kLowShadowBeg, kHighShadowEnd - kLowShadowBeg);
|
2012-06-06 15:02:44 +08:00
|
|
|
if (death_callback)
|
|
|
|
death_callback();
|
|
|
|
if (FLAG_abort_on_error)
|
|
|
|
Abort();
|
|
|
|
Exit(FLAG_exitcode);
|
|
|
|
}
|
|
|
|
|
2012-06-06 23:22:20 +08:00
|
|
|
void CheckFailed(const char *file, int line, const char *cond, u64 v1, u64 v2) {
|
|
|
|
AsanReport("AddressSanitizer CHECK failed: %s:%d \"%s\" (%zx, %zx)\n",
|
|
|
|
file, line, cond, (uptr)v1, (uptr)v2);
|
|
|
|
PRINT_CURRENT_STACK();
|
|
|
|
ShowStatsAndAbort();
|
|
|
|
}
|
|
|
|
|
2012-06-06 15:02:44 +08:00
|
|
|
} // namespace __sanitizer
|
|
|
|
|
2011-11-30 09:07:02 +08:00
|
|
|
namespace __asan {
|
|
|
|
|
|
|
|
// -------------------------- Flags ------------------------- {{{1
|
2012-05-31 22:35:53 +08:00
|
|
|
static const uptr kMallocContextSize = 30;
|
2012-05-29 00:21:19 +08:00
|
|
|
|
2012-05-31 22:35:53 +08:00
|
|
|
uptr FLAG_malloc_context_size = kMallocContextSize;
|
|
|
|
uptr FLAG_max_malloc_fill_size = 0;
|
2012-05-31 23:02:07 +08:00
|
|
|
s64 FLAG_v = 0;
|
2012-05-31 22:35:53 +08:00
|
|
|
uptr FLAG_redzone = (ASAN_LOW_MEMORY) ? 64 : 128; // power of two, >= 32
|
|
|
|
uptr FLAG_quarantine_size = (ASAN_LOW_MEMORY) ? 1UL << 24 : 1UL << 28;
|
2012-05-31 23:02:07 +08:00
|
|
|
static s64 FLAG_atexit = 0;
|
2012-05-29 00:21:19 +08:00
|
|
|
bool FLAG_poison_shadow = 1;
|
2012-05-31 23:02:07 +08:00
|
|
|
s64 FLAG_report_globals = 1;
|
2012-05-29 00:21:19 +08:00
|
|
|
bool FLAG_handle_segv = ASAN_NEEDS_SEGV;
|
|
|
|
bool FLAG_use_sigaltstack = 0;
|
2012-06-04 19:20:17 +08:00
|
|
|
bool FLAG_symbolize = 0;
|
2012-05-31 23:02:07 +08:00
|
|
|
s64 FLAG_demangle = 1;
|
|
|
|
s64 FLAG_debug = 0;
|
2012-05-29 00:21:19 +08:00
|
|
|
bool FLAG_replace_cfallocator = 1; // Used on Mac only.
|
2012-06-21 09:01:20 +08:00
|
|
|
bool FLAG_mac_ignore_invalid_free = 0; // Used on Mac only.
|
2012-05-29 00:21:19 +08:00
|
|
|
bool FLAG_replace_str = 1;
|
|
|
|
bool FLAG_replace_intrin = 1;
|
|
|
|
bool FLAG_use_fake_stack = 1;
|
2012-05-31 23:02:07 +08:00
|
|
|
s64 FLAG_exitcode = ASAN_DEFAULT_FAILURE_EXITCODE;
|
2012-05-29 00:21:19 +08:00
|
|
|
bool FLAG_allow_user_poisoning = 1;
|
2012-05-31 23:02:07 +08:00
|
|
|
s64 FLAG_sleep_before_dying = 0;
|
2012-05-29 00:21:19 +08:00
|
|
|
bool FLAG_abort_on_error = 0;
|
|
|
|
bool FLAG_unmap_shadow_on_exit = 0;
|
|
|
|
bool FLAG_disable_core = __WORDSIZE == 64;
|
|
|
|
bool FLAG_check_malloc_usable_size = 1;
|
2011-11-30 09:07:02 +08:00
|
|
|
|
|
|
|
// -------------------------- Globals --------------------- {{{1
|
|
|
|
int asan_inited;
|
|
|
|
bool asan_init_is_running;
|
2012-06-06 15:02:44 +08:00
|
|
|
void (*death_callback)(void);
|
2012-02-27 22:06:48 +08:00
|
|
|
static void (*error_report_callback)(const char*);
|
2012-05-31 22:35:53 +08:00
|
|
|
char *error_message_buffer = 0;
|
|
|
|
uptr error_message_buffer_pos = 0;
|
|
|
|
uptr error_message_buffer_size = 0;
|
2011-11-30 09:07:02 +08:00
|
|
|
|
|
|
|
// -------------------------- Misc ---------------- {{{1
|
|
|
|
void ShowStatsAndAbort() {
|
|
|
|
__asan_print_accumulated_stats();
|
2012-06-06 15:02:44 +08:00
|
|
|
Die();
|
2011-11-30 09:07:02 +08:00
|
|
|
}
|
|
|
|
|
2012-05-31 22:35:53 +08:00
|
|
|
static void PrintBytes(const char *before, uptr *a) {
|
2012-05-31 23:02:07 +08:00
|
|
|
u8 *bytes = (u8*)a;
|
2012-05-31 22:35:53 +08:00
|
|
|
uptr byte_num = (__WORDSIZE) / 8;
|
2012-06-06 21:11:29 +08:00
|
|
|
AsanPrintf("%s%p:", before, (void*)a);
|
2012-05-31 22:35:53 +08:00
|
|
|
for (uptr i = 0; i < byte_num; i++) {
|
2012-06-06 21:11:29 +08:00
|
|
|
AsanPrintf(" %x%x", bytes[i] >> 4, bytes[i] & 15);
|
2011-11-30 09:07:02 +08:00
|
|
|
}
|
2012-06-06 21:11:29 +08:00
|
|
|
AsanPrintf("\n");
|
2011-11-30 09:07:02 +08:00
|
|
|
}
|
|
|
|
|
2012-06-06 21:11:29 +08:00
|
|
|
void AppendToErrorMessageBuffer(const char *buffer) {
|
|
|
|
if (error_message_buffer) {
|
|
|
|
uptr length = (uptr)internal_strlen(buffer);
|
|
|
|
int 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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-11-30 09:07:02 +08:00
|
|
|
// ---------------------- mmap -------------------- {{{1
|
2011-12-29 07:28:54 +08:00
|
|
|
// Reserve memory range [beg, end].
|
2012-05-31 22:35:53 +08:00
|
|
|
static void ReserveShadowMemoryRange(uptr beg, uptr end) {
|
2011-11-30 09:07:02 +08:00
|
|
|
CHECK((beg % kPageSize) == 0);
|
|
|
|
CHECK(((end + 1) % kPageSize) == 0);
|
2012-05-31 22:35:53 +08:00
|
|
|
uptr size = end - beg + 1;
|
2012-06-14 22:42:58 +08:00
|
|
|
void *res = MmapFixedNoReserve(beg, size);
|
2011-12-29 07:28:54 +08:00
|
|
|
CHECK(res == (void*)beg && "ReserveShadowMemoryRange failed");
|
2011-11-30 09:07:02 +08:00
|
|
|
}
|
|
|
|
|
2011-12-03 05:02:20 +08:00
|
|
|
// ---------------------- LowLevelAllocator ------------- {{{1
|
2012-05-31 22:35:53 +08:00
|
|
|
void *LowLevelAllocator::Allocate(uptr size) {
|
2011-12-03 05:02:20 +08:00
|
|
|
CHECK((size & (size - 1)) == 0 && "size must be a power of two");
|
|
|
|
if (allocated_end_ - allocated_current_ < size) {
|
2012-05-31 22:35:53 +08:00
|
|
|
uptr size_to_allocate = Max(size, kPageSize);
|
2011-12-29 06:58:01 +08:00
|
|
|
allocated_current_ =
|
2012-06-07 00:15:07 +08:00
|
|
|
(char*)MmapOrDie(size_to_allocate, __FUNCTION__);
|
2011-12-03 05:02:20 +08:00
|
|
|
allocated_end_ = allocated_current_ + size_to_allocate;
|
2012-05-31 22:35:53 +08:00
|
|
|
PoisonShadow((uptr)allocated_current_, size_to_allocate,
|
2011-12-16 01:41:30 +08:00
|
|
|
kAsanInternalHeapMagic);
|
2011-12-03 05:02:20 +08:00
|
|
|
}
|
|
|
|
CHECK(allocated_end_ - allocated_current_ >= size);
|
|
|
|
void *res = allocated_current_;
|
|
|
|
allocated_current_ += size;
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
2011-11-30 09:07:02 +08:00
|
|
|
// ---------------------- DescribeAddress -------------------- {{{1
|
2012-05-31 22:35:53 +08:00
|
|
|
static bool DescribeStackAddress(uptr addr, uptr access_size) {
|
2011-11-30 09:07:02 +08:00
|
|
|
AsanThread *t = asanThreadRegistry().FindThreadByStackAddress(addr);
|
|
|
|
if (!t) return false;
|
2012-05-31 23:02:07 +08:00
|
|
|
const sptr kBufSize = 4095;
|
2011-11-30 09:07:02 +08:00
|
|
|
char buf[kBufSize];
|
2012-05-31 22:35:53 +08:00
|
|
|
uptr offset = 0;
|
2011-11-30 09:07:02 +08:00
|
|
|
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.
|
2012-02-15 03:33:04 +08:00
|
|
|
const char *name_end = internal_strchr(frame_descr, ' ');
|
2011-11-30 09:07:02 +08:00
|
|
|
CHECK(name_end);
|
|
|
|
buf[0] = 0;
|
2012-01-10 06:20:49 +08:00
|
|
|
internal_strncat(buf, frame_descr,
|
|
|
|
Min(kBufSize,
|
2012-05-31 23:02:07 +08:00
|
|
|
static_cast<sptr>(name_end - frame_descr)));
|
2012-06-06 21:11:29 +08:00
|
|
|
AsanPrintf("Address %p is located at offset %zu "
|
|
|
|
"in frame <%s> of T%d's stack:\n",
|
|
|
|
(void*)addr, offset, buf, t->tid());
|
2011-11-30 09:07:02 +08:00
|
|
|
// Report the number of stack objects.
|
|
|
|
char *p;
|
2012-05-31 22:35:53 +08:00
|
|
|
uptr n_objects = internal_simple_strtoll(name_end, &p, 10);
|
2011-11-30 09:07:02 +08:00
|
|
|
CHECK(n_objects > 0);
|
2012-06-06 21:11:29 +08:00
|
|
|
AsanPrintf(" This frame has %zu object(s):\n", n_objects);
|
2011-11-30 09:07:02 +08:00
|
|
|
// Report all objects in this frame.
|
2012-05-31 22:35:53 +08:00
|
|
|
for (uptr i = 0; i < n_objects; i++) {
|
|
|
|
uptr beg, size;
|
2012-05-31 23:02:07 +08:00
|
|
|
sptr len;
|
2012-02-18 00:15:09 +08:00
|
|
|
beg = internal_simple_strtoll(p, &p, 10);
|
|
|
|
size = internal_simple_strtoll(p, &p, 10);
|
|
|
|
len = internal_simple_strtoll(p, &p, 10);
|
2011-11-30 09:07:02 +08:00
|
|
|
if (beg <= 0 || size <= 0 || len < 0 || *p != ' ') {
|
2012-06-06 21:11:29 +08:00
|
|
|
AsanPrintf("AddressSanitizer can't parse the stack frame "
|
|
|
|
"descriptor: |%s|\n", frame_descr);
|
2011-11-30 09:07:02 +08:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
p++;
|
|
|
|
buf[0] = 0;
|
2012-01-10 06:20:49 +08:00
|
|
|
internal_strncat(buf, p, Min(kBufSize, len));
|
2011-11-30 09:07:02 +08:00
|
|
|
p += len;
|
2012-06-06 21:11:29 +08:00
|
|
|
AsanPrintf(" [%zu, %zu) '%s'\n", beg, beg + size, buf);
|
2011-11-30 09:07:02 +08:00
|
|
|
}
|
2012-06-06 21:11:29 +08:00
|
|
|
AsanPrintf("HINT: this may be a false positive if your program uses "
|
|
|
|
"some custom stack unwind mechanism\n"
|
|
|
|
" (longjmp and C++ exceptions *are* supported)\n");
|
2011-11-30 09:07:02 +08:00
|
|
|
t->summary()->Announce();
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2012-05-31 22:35:53 +08:00
|
|
|
static NOINLINE void DescribeAddress(uptr addr, uptr access_size) {
|
2011-11-30 09:07:02 +08:00
|
|
|
// Check if this is a global.
|
|
|
|
if (DescribeAddrIfGlobal(addr))
|
|
|
|
return;
|
|
|
|
|
|
|
|
if (DescribeStackAddress(addr, access_size))
|
|
|
|
return;
|
|
|
|
|
|
|
|
// finally, check if this is a heap.
|
|
|
|
DescribeHeapAddress(addr, access_size);
|
|
|
|
}
|
|
|
|
|
|
|
|
// -------------------------- Run-time entry ------------------- {{{1
|
|
|
|
// exported functions
|
2011-12-28 08:59:39 +08:00
|
|
|
#define ASAN_REPORT_ERROR(type, is_write, size) \
|
2012-06-05 21:50:57 +08:00
|
|
|
extern "C" NOINLINE INTERFACE_ATTRIBUTE \
|
2012-05-31 22:35:53 +08:00
|
|
|
void __asan_report_ ## type ## size(uptr addr); \
|
|
|
|
void __asan_report_ ## type ## size(uptr addr) { \
|
2012-03-15 09:36:00 +08:00
|
|
|
GET_CALLER_PC_BP_SP; \
|
2011-12-28 08:59:39 +08:00
|
|
|
__asan_report_error(pc, bp, sp, addr, is_write, size); \
|
2011-11-30 09:07:02 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
ASAN_REPORT_ERROR(load, false, 1)
|
|
|
|
ASAN_REPORT_ERROR(load, false, 2)
|
|
|
|
ASAN_REPORT_ERROR(load, false, 4)
|
|
|
|
ASAN_REPORT_ERROR(load, false, 8)
|
|
|
|
ASAN_REPORT_ERROR(load, false, 16)
|
|
|
|
ASAN_REPORT_ERROR(store, true, 1)
|
|
|
|
ASAN_REPORT_ERROR(store, true, 2)
|
|
|
|
ASAN_REPORT_ERROR(store, true, 4)
|
|
|
|
ASAN_REPORT_ERROR(store, true, 8)
|
|
|
|
ASAN_REPORT_ERROR(store, true, 16)
|
|
|
|
|
|
|
|
// Force the linker to keep the symbols for various ASan interface functions.
|
|
|
|
// We want to keep those in the executable in order to let the instrumented
|
|
|
|
// dynamic libraries access the symbol even if it is not used by the executable
|
|
|
|
// itself. This should help if the build system is removing dead code at link
|
|
|
|
// time.
|
2012-02-27 22:06:48 +08:00
|
|
|
static NOINLINE void force_interface_symbols() {
|
2011-11-30 09:07:02 +08:00
|
|
|
volatile int fake_condition = 0; // prevent dead condition elimination.
|
|
|
|
if (fake_condition) {
|
2012-03-15 06:48:09 +08:00
|
|
|
__asan_report_load1(0);
|
|
|
|
__asan_report_load2(0);
|
|
|
|
__asan_report_load4(0);
|
|
|
|
__asan_report_load8(0);
|
|
|
|
__asan_report_load16(0);
|
|
|
|
__asan_report_store1(0);
|
|
|
|
__asan_report_store2(0);
|
|
|
|
__asan_report_store4(0);
|
|
|
|
__asan_report_store8(0);
|
|
|
|
__asan_report_store16(0);
|
2012-05-31 22:35:53 +08:00
|
|
|
__asan_register_global(0, 0, 0);
|
|
|
|
__asan_register_globals(0, 0);
|
|
|
|
__asan_unregister_globals(0, 0);
|
|
|
|
__asan_set_death_callback(0);
|
|
|
|
__asan_set_error_report_callback(0);
|
2012-03-06 19:45:59 +08:00
|
|
|
__asan_handle_no_return();
|
2011-11-30 09:07:02 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// -------------------------- Init ------------------- {{{1
|
2012-05-29 00:21:19 +08:00
|
|
|
static void IntFlagValue(const char *flags, const char *flag,
|
2012-05-31 23:02:07 +08:00
|
|
|
s64 *out_val) {
|
2012-05-29 00:21:19 +08:00
|
|
|
if (!flags) return;
|
|
|
|
const char *str = internal_strstr(flags, flag);
|
|
|
|
if (!str) return;
|
|
|
|
*out_val = internal_atoll(str + internal_strlen(flag));
|
|
|
|
}
|
|
|
|
|
|
|
|
static void BoolFlagValue(const char *flags, const char *flag,
|
|
|
|
bool *out_val) {
|
|
|
|
if (!flags) return;
|
2012-01-10 06:20:49 +08:00
|
|
|
const char *str = internal_strstr(flags, flag);
|
2012-05-29 00:21:19 +08:00
|
|
|
if (!str) return;
|
2012-05-30 23:28:45 +08:00
|
|
|
const char *suffix = str + internal_strlen(flag);
|
2012-05-29 00:21:19 +08:00
|
|
|
if (!internal_atoll(str + internal_strlen(flag))) {
|
2012-05-30 23:28:45 +08:00
|
|
|
if (suffix[0] == '0') {
|
2012-05-29 00:21:19 +08:00
|
|
|
*out_val = false;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
*out_val = true;
|
|
|
|
return;
|
|
|
|
}
|
2012-05-30 23:28:45 +08:00
|
|
|
switch (suffix[0]) {
|
2012-05-29 00:21:19 +08:00
|
|
|
case 'y':
|
|
|
|
case 't': {
|
|
|
|
*out_val = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case 'n':
|
|
|
|
case 'f': {
|
|
|
|
*out_val = false;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
default: return;
|
|
|
|
}
|
2011-11-30 09:07:02 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
static void asan_atexit() {
|
2012-06-06 21:11:29 +08:00
|
|
|
AsanPrintf("AddressSanitizer exit stats:\n");
|
2011-11-30 09:07:02 +08:00
|
|
|
__asan_print_accumulated_stats();
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace __asan
|
|
|
|
|
2012-01-10 02:53:15 +08:00
|
|
|
// ---------------------- Interface ---------------- {{{1
|
2011-11-30 09:07:02 +08:00
|
|
|
using namespace __asan; // NOLINT
|
|
|
|
|
|
|
|
int __asan_set_error_exit_code(int exit_code) {
|
|
|
|
int old = FLAG_exitcode;
|
|
|
|
FLAG_exitcode = exit_code;
|
|
|
|
return old;
|
|
|
|
}
|
|
|
|
|
2012-03-06 19:45:59 +08:00
|
|
|
void NOINLINE __asan_handle_no_return() {
|
2012-02-09 05:33:27 +08:00
|
|
|
int local_stack;
|
|
|
|
AsanThread *curr_thread = asanThreadRegistry().GetCurrent();
|
|
|
|
CHECK(curr_thread);
|
2012-05-31 22:35:53 +08:00
|
|
|
uptr top = curr_thread->stack_top();
|
|
|
|
uptr bottom = ((uptr)&local_stack - kPageSize) & ~(kPageSize-1);
|
2012-02-09 05:33:27 +08:00
|
|
|
PoisonShadow(bottom, top - bottom, 0);
|
|
|
|
}
|
|
|
|
|
2012-03-01 22:39:21 +08:00
|
|
|
void NOINLINE __asan_set_death_callback(void (*callback)(void)) {
|
2012-02-14 05:24:29 +08:00
|
|
|
death_callback = callback;
|
|
|
|
}
|
|
|
|
|
2012-02-27 22:06:48 +08:00
|
|
|
void NOINLINE __asan_set_error_report_callback(void (*callback)(const char*)) {
|
|
|
|
error_report_callback = callback;
|
|
|
|
if (callback) {
|
2012-05-12 20:33:41 +08:00
|
|
|
error_message_buffer_size = 1 << 16;
|
2012-02-27 22:06:48 +08:00
|
|
|
error_message_buffer =
|
2012-06-07 00:15:07 +08:00
|
|
|
(char*)MmapOrDie(error_message_buffer_size, __FUNCTION__);
|
2012-02-27 22:06:48 +08:00
|
|
|
error_message_buffer_pos = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-05-31 22:11:07 +08:00
|
|
|
void __asan_report_error(uptr pc, uptr bp, uptr sp,
|
|
|
|
uptr addr, bool is_write, uptr access_size) {
|
2011-11-30 09:07:02 +08:00
|
|
|
// Do not print more than one report, otherwise they will mix up.
|
|
|
|
static int num_calls = 0;
|
|
|
|
if (AtomicInc(&num_calls) > 1) return;
|
|
|
|
|
2012-06-06 21:11:29 +08:00
|
|
|
AsanPrintf("===================================================="
|
|
|
|
"=============\n");
|
2011-11-30 09:07:02 +08:00
|
|
|
const char *bug_descr = "unknown-crash";
|
|
|
|
if (AddrIsInMem(addr)) {
|
2012-05-31 23:02:07 +08:00
|
|
|
u8 *shadow_addr = (u8*)MemToShadow(addr);
|
2011-12-08 05:30:20 +08:00
|
|
|
// 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) {
|
2011-11-30 09:07:02 +08:00
|
|
|
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;
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-12-09 09:49:31 +08:00
|
|
|
AsanThread *curr_thread = asanThreadRegistry().GetCurrent();
|
2012-06-06 23:06:58 +08:00
|
|
|
u32 curr_tid = asanThreadRegistry().GetCurrentTidOrInvalid();
|
2011-12-09 09:49:31 +08:00
|
|
|
|
|
|
|
if (curr_thread) {
|
|
|
|
// We started reporting an error message. Stop using the fake stack
|
|
|
|
// in case we will call an instrumented function from a symbolizer.
|
|
|
|
curr_thread->fake_stack().StopUsingFakeStack();
|
|
|
|
}
|
|
|
|
|
2012-06-06 21:11:29 +08:00
|
|
|
AsanReport("ERROR: AddressSanitizer %s on address "
|
|
|
|
"%p at pc 0x%zx bp 0x%zx sp 0x%zx\n",
|
|
|
|
bug_descr, (void*)addr, pc, bp, sp);
|
2011-11-30 09:07:02 +08:00
|
|
|
|
2012-06-06 21:11:29 +08:00
|
|
|
AsanPrintf("%s of size %zu at %p thread T%d\n",
|
|
|
|
access_size ? (is_write ? "WRITE" : "READ") : "ACCESS",
|
|
|
|
access_size, (void*)addr, curr_tid);
|
2011-11-30 09:07:02 +08:00
|
|
|
|
|
|
|
if (FLAG_debug) {
|
2012-05-31 22:35:53 +08:00
|
|
|
PrintBytes("PC: ", (uptr*)pc);
|
2011-11-30 09:07:02 +08:00
|
|
|
}
|
|
|
|
|
2012-01-19 19:34:18 +08:00
|
|
|
GET_STACK_TRACE_WITH_PC_AND_BP(kStackTraceMax, pc, bp);
|
2011-11-30 09:07:02 +08:00
|
|
|
stack.PrintStack();
|
|
|
|
|
|
|
|
CHECK(AddrIsInMem(addr));
|
|
|
|
|
|
|
|
DescribeAddress(addr, access_size);
|
|
|
|
|
2012-05-31 22:35:53 +08:00
|
|
|
uptr shadow_addr = MemToShadow(addr);
|
2012-06-06 21:11:29 +08:00
|
|
|
AsanReport("ABORTING\n");
|
2011-11-30 09:07:02 +08:00
|
|
|
__asan_print_accumulated_stats();
|
2012-06-06 21:11:29 +08:00
|
|
|
AsanPrintf("Shadow byte and word:\n");
|
|
|
|
AsanPrintf(" %p: %x\n", (void*)shadow_addr, *(unsigned char*)shadow_addr);
|
2012-05-31 22:35:53 +08:00
|
|
|
uptr aligned_shadow = shadow_addr & ~(kWordSize - 1);
|
|
|
|
PrintBytes(" ", (uptr*)(aligned_shadow));
|
2012-06-06 21:11:29 +08:00
|
|
|
AsanPrintf("More shadow bytes:\n");
|
2012-05-31 22:35:53 +08:00
|
|
|
PrintBytes(" ", (uptr*)(aligned_shadow-4*kWordSize));
|
|
|
|
PrintBytes(" ", (uptr*)(aligned_shadow-3*kWordSize));
|
|
|
|
PrintBytes(" ", (uptr*)(aligned_shadow-2*kWordSize));
|
|
|
|
PrintBytes(" ", (uptr*)(aligned_shadow-1*kWordSize));
|
|
|
|
PrintBytes("=>", (uptr*)(aligned_shadow+0*kWordSize));
|
|
|
|
PrintBytes(" ", (uptr*)(aligned_shadow+1*kWordSize));
|
|
|
|
PrintBytes(" ", (uptr*)(aligned_shadow+2*kWordSize));
|
|
|
|
PrintBytes(" ", (uptr*)(aligned_shadow+3*kWordSize));
|
|
|
|
PrintBytes(" ", (uptr*)(aligned_shadow+4*kWordSize));
|
2012-02-27 22:06:48 +08:00
|
|
|
if (error_report_callback) {
|
|
|
|
error_report_callback(error_message_buffer);
|
|
|
|
}
|
2012-06-06 15:02:44 +08:00
|
|
|
Die();
|
2011-11-30 09:07:02 +08:00
|
|
|
}
|
|
|
|
|
2012-05-25 23:37:16 +08:00
|
|
|
static void ParseAsanOptions(const char *options) {
|
2012-05-29 00:21:19 +08:00
|
|
|
IntFlagValue(options, "malloc_context_size=",
|
2012-05-31 23:02:07 +08:00
|
|
|
(s64*)&FLAG_malloc_context_size);
|
2011-11-30 09:07:02 +08:00
|
|
|
CHECK(FLAG_malloc_context_size <= kMallocContextSize);
|
|
|
|
|
2012-05-29 00:21:19 +08:00
|
|
|
IntFlagValue(options, "max_malloc_fill_size=",
|
2012-05-31 23:02:07 +08:00
|
|
|
(s64*)&FLAG_max_malloc_fill_size);
|
2011-11-30 09:07:02 +08:00
|
|
|
|
2012-05-29 00:21:19 +08:00
|
|
|
IntFlagValue(options, "verbosity=", &FLAG_v);
|
2011-11-30 09:07:02 +08:00
|
|
|
|
2012-05-31 23:02:07 +08:00
|
|
|
IntFlagValue(options, "redzone=", (s64*)&FLAG_redzone);
|
2012-06-07 17:15:48 +08:00
|
|
|
CHECK(FLAG_redzone >= 16);
|
|
|
|
CHECK(IsPowerOfTwo(FLAG_redzone));
|
2012-05-31 23:02:07 +08:00
|
|
|
IntFlagValue(options, "quarantine_size=", (s64*)&FLAG_quarantine_size);
|
2012-05-29 00:21:19 +08:00
|
|
|
|
|
|
|
IntFlagValue(options, "atexit=", &FLAG_atexit);
|
|
|
|
BoolFlagValue(options, "poison_shadow=", &FLAG_poison_shadow);
|
|
|
|
IntFlagValue(options, "report_globals=", &FLAG_report_globals);
|
|
|
|
BoolFlagValue(options, "handle_segv=", &FLAG_handle_segv);
|
|
|
|
BoolFlagValue(options, "use_sigaltstack=", &FLAG_use_sigaltstack);
|
|
|
|
BoolFlagValue(options, "symbolize=", &FLAG_symbolize);
|
|
|
|
IntFlagValue(options, "demangle=", &FLAG_demangle);
|
|
|
|
IntFlagValue(options, "debug=", &FLAG_debug);
|
|
|
|
BoolFlagValue(options, "replace_cfallocator=", &FLAG_replace_cfallocator);
|
2012-06-21 16:13:49 +08:00
|
|
|
BoolFlagValue(options, "mac_ignore_invalid_free=",
|
|
|
|
&FLAG_mac_ignore_invalid_free);
|
2012-05-29 00:21:19 +08:00
|
|
|
BoolFlagValue(options, "replace_str=", &FLAG_replace_str);
|
|
|
|
BoolFlagValue(options, "replace_intrin=", &FLAG_replace_intrin);
|
|
|
|
BoolFlagValue(options, "use_fake_stack=", &FLAG_use_fake_stack);
|
|
|
|
IntFlagValue(options, "exitcode=", &FLAG_exitcode);
|
|
|
|
BoolFlagValue(options, "allow_user_poisoning=", &FLAG_allow_user_poisoning);
|
|
|
|
IntFlagValue(options, "sleep_before_dying=", &FLAG_sleep_before_dying);
|
|
|
|
BoolFlagValue(options, "abort_on_error=", &FLAG_abort_on_error);
|
|
|
|
BoolFlagValue(options, "unmap_shadow_on_exit=", &FLAG_unmap_shadow_on_exit);
|
2012-04-06 09:27:11 +08:00
|
|
|
// By default, disable core dumper on 64-bit --
|
|
|
|
// it makes little sense to dump 16T+ core.
|
2012-05-29 00:21:19 +08:00
|
|
|
BoolFlagValue(options, "disable_core=", &FLAG_disable_core);
|
2011-11-30 09:07:02 +08:00
|
|
|
|
2012-05-25 23:20:13 +08:00
|
|
|
// Allow the users to work around the bug in Nvidia drivers prior to 295.*.
|
2012-05-29 00:21:19 +08:00
|
|
|
BoolFlagValue(options, "check_malloc_usable_size=",
|
|
|
|
&FLAG_check_malloc_usable_size);
|
2012-05-25 23:37:16 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void __asan_init() {
|
|
|
|
if (asan_inited) return;
|
|
|
|
asan_init_is_running = true;
|
|
|
|
|
|
|
|
// Make sure we are not statically linked.
|
|
|
|
AsanDoesNotSupportStaticLinkage();
|
|
|
|
|
2012-05-29 17:39:01 +08:00
|
|
|
#if !defined(_WIN32)
|
2012-05-29 00:21:19 +08:00
|
|
|
if (__asan_default_options) {
|
|
|
|
ParseAsanOptions(__asan_default_options);
|
2012-05-30 22:12:20 +08:00
|
|
|
if (FLAG_v) {
|
|
|
|
Report("Using the defaults from __asan_default_options: %s\n",
|
|
|
|
__asan_default_options);
|
|
|
|
}
|
2012-05-29 00:21:19 +08:00
|
|
|
}
|
2012-05-29 17:39:01 +08:00
|
|
|
#endif
|
2012-05-25 23:37:16 +08:00
|
|
|
// flags
|
2012-06-14 22:07:21 +08:00
|
|
|
const char *options = GetEnv("ASAN_OPTIONS");
|
2012-05-25 23:56:40 +08:00
|
|
|
ParseAsanOptions(options);
|
2012-03-17 00:38:31 +08:00
|
|
|
|
2012-05-29 00:21:19 +08:00
|
|
|
if (FLAG_v && options) {
|
2012-03-17 00:38:31 +08:00
|
|
|
Report("Parsed ASAN_OPTIONS: %s\n", options);
|
|
|
|
}
|
|
|
|
|
2011-11-30 09:07:02 +08:00
|
|
|
if (FLAG_atexit) {
|
2012-02-22 22:07:06 +08:00
|
|
|
Atexit(asan_atexit);
|
2011-11-30 09:07:02 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// interceptors
|
|
|
|
InitializeAsanInterceptors();
|
|
|
|
|
|
|
|
ReplaceSystemMalloc();
|
2012-04-06 16:21:08 +08:00
|
|
|
ReplaceOperatorsNewAndDelete();
|
2011-11-30 09:07:02 +08:00
|
|
|
|
|
|
|
if (FLAG_v) {
|
2012-06-06 18:54:25 +08:00
|
|
|
Printf("|| `[%p, %p]` || HighMem ||\n",
|
|
|
|
(void*)kHighMemBeg, (void*)kHighMemEnd);
|
|
|
|
Printf("|| `[%p, %p]` || HighShadow ||\n",
|
|
|
|
(void*)kHighShadowBeg, (void*)kHighShadowEnd);
|
|
|
|
Printf("|| `[%p, %p]` || ShadowGap ||\n",
|
|
|
|
(void*)kShadowGapBeg, (void*)kShadowGapEnd);
|
|
|
|
Printf("|| `[%p, %p]` || LowShadow ||\n",
|
|
|
|
(void*)kLowShadowBeg, (void*)kLowShadowEnd);
|
|
|
|
Printf("|| `[%p, %p]` || LowMem ||\n",
|
|
|
|
(void*)kLowMemBeg, (void*)kLowMemEnd);
|
2011-11-30 09:07:02 +08:00
|
|
|
Printf("MemToShadow(shadow): %p %p %p %p\n",
|
2012-06-06 18:54:25 +08:00
|
|
|
(void*)MEM_TO_SHADOW(kLowShadowBeg),
|
|
|
|
(void*)MEM_TO_SHADOW(kLowShadowEnd),
|
|
|
|
(void*)MEM_TO_SHADOW(kHighShadowBeg),
|
|
|
|
(void*)MEM_TO_SHADOW(kHighShadowEnd));
|
2012-05-31 22:35:53 +08:00
|
|
|
Printf("red_zone=%zu\n", (uptr)FLAG_redzone);
|
|
|
|
Printf("malloc_context_size=%zu\n", (uptr)FLAG_malloc_context_size);
|
2011-11-30 09:07:02 +08:00
|
|
|
|
2012-05-31 22:35:53 +08:00
|
|
|
Printf("SHADOW_SCALE: %zx\n", (uptr)SHADOW_SCALE);
|
|
|
|
Printf("SHADOW_GRANULARITY: %zx\n", (uptr)SHADOW_GRANULARITY);
|
|
|
|
Printf("SHADOW_OFFSET: %zx\n", (uptr)SHADOW_OFFSET);
|
2011-11-30 09:07:02 +08:00
|
|
|
CHECK(SHADOW_SCALE >= 3 && SHADOW_SCALE <= 7);
|
|
|
|
}
|
|
|
|
|
2012-04-06 09:27:11 +08:00
|
|
|
if (FLAG_disable_core) {
|
2012-06-15 14:08:19 +08:00
|
|
|
DisableCoreDumper();
|
2011-11-30 09:07:02 +08:00
|
|
|
}
|
|
|
|
|
2012-06-15 15:29:14 +08:00
|
|
|
uptr shadow_start = kLowShadowBeg;
|
|
|
|
if (kLowShadowBeg > 0) shadow_start -= kMmapGranularity;
|
|
|
|
uptr shadow_end = kHighShadowEnd;
|
|
|
|
if (MemoryRangeIsAvailable(shadow_start, shadow_end)) {
|
2012-01-10 03:18:27 +08:00
|
|
|
if (kLowShadowBeg != kLowShadowEnd) {
|
2012-02-10 01:20:14 +08:00
|
|
|
// mmap the low shadow plus at least one page.
|
|
|
|
ReserveShadowMemoryRange(kLowShadowBeg - kMmapGranularity, kLowShadowEnd);
|
2011-11-30 09:07:02 +08:00
|
|
|
}
|
2012-01-10 03:18:27 +08:00
|
|
|
// mmap the high shadow.
|
|
|
|
ReserveShadowMemoryRange(kHighShadowBeg, kHighShadowEnd);
|
2011-11-30 09:07:02 +08:00
|
|
|
// protect the gap
|
2012-06-14 22:42:58 +08:00
|
|
|
void *prot = Mprotect(kShadowGapBeg, kShadowGapEnd - kShadowGapBeg + 1);
|
2011-12-29 07:28:54 +08:00
|
|
|
CHECK(prot == (void*)kShadowGapBeg);
|
2012-02-13 23:11:23 +08:00
|
|
|
} else {
|
|
|
|
Report("Shadow memory range interleaves with an existing memory mapping. "
|
|
|
|
"ASan cannot proceed correctly. ABORTING.\n");
|
2012-06-15 14:08:19 +08:00
|
|
|
DumpProcessMap();
|
2012-06-06 15:02:44 +08:00
|
|
|
Die();
|
2011-11-30 09:07:02 +08:00
|
|
|
}
|
|
|
|
|
2012-04-05 18:54:52 +08:00
|
|
|
InstallSignalHandlers();
|
|
|
|
|
2011-11-30 09:07:02 +08:00
|
|
|
// On Linux AsanThread::ThreadStart() calls malloc() that's why asan_inited
|
|
|
|
// should be set to 1 prior to initializing the threads.
|
|
|
|
asan_inited = 1;
|
|
|
|
asan_init_is_running = false;
|
|
|
|
|
|
|
|
asanThreadRegistry().Init();
|
|
|
|
asanThreadRegistry().GetMain()->ThreadStart();
|
2011-12-28 08:59:39 +08:00
|
|
|
force_interface_symbols(); // no-op.
|
2011-11-30 09:07:02 +08:00
|
|
|
|
|
|
|
if (FLAG_v) {
|
2011-12-02 05:40:52 +08:00
|
|
|
Report("AddressSanitizer Init done\n");
|
2011-11-30 09:07:02 +08:00
|
|
|
}
|
|
|
|
}
|
2012-01-11 16:17:19 +08:00
|
|
|
|
|
|
|
#if defined(ASAN_USE_PREINIT_ARRAY)
|
2012-02-22 00:24:23 +08:00
|
|
|
// On Linux, we force __asan_init to be called before anyone else
|
|
|
|
// by placing it into .preinit_array section.
|
|
|
|
// FIXME: do we have anything like this on Mac?
|
|
|
|
__attribute__((section(".preinit_array")))
|
|
|
|
typeof(__asan_init) *__asan_preinit =__asan_init;
|
|
|
|
#elif defined(_WIN32) && defined(_DLL)
|
|
|
|
// On Windows, when using dynamic CRT (/MD), we can put a pointer
|
|
|
|
// to __asan_init into the global list of C initializers.
|
|
|
|
// See crt0dat.c in the CRT sources for the details.
|
2012-02-22 17:28:14 +08:00
|
|
|
#pragma section(".CRT$XIB", long, read) // NOLINT
|
2012-02-22 00:24:23 +08:00
|
|
|
__declspec(allocate(".CRT$XIB")) void (*__asan_preinit)() = __asan_init;
|
2012-01-11 16:17:19 +08:00
|
|
|
#endif
|