2011-11-30 09:07:02 +08:00
|
|
|
//===-- asan_rtl.cc ---------------------------------------------*- C++ -*-===//
|
|
|
|
//
|
|
|
|
// 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"
|
2012-01-05 08:44:33 +08:00
|
|
|
#include "asan_procmaps.h"
|
2011-11-30 09:07:02 +08:00
|
|
|
#include "asan_stack.h"
|
|
|
|
#include "asan_stats.h"
|
|
|
|
#include "asan_thread.h"
|
|
|
|
#include "asan_thread_registry.h"
|
|
|
|
|
|
|
|
namespace __asan {
|
|
|
|
|
|
|
|
// -------------------------- Flags ------------------------- {{{1
|
|
|
|
static const size_t kMallocContextSize = 30;
|
|
|
|
static int FLAG_atexit;
|
|
|
|
|
|
|
|
size_t FLAG_redzone; // power of two, >= 32
|
|
|
|
size_t FLAG_quarantine_size;
|
|
|
|
int FLAG_demangle;
|
|
|
|
bool FLAG_symbolize;
|
|
|
|
int FLAG_v;
|
|
|
|
int FLAG_debug;
|
|
|
|
bool FLAG_poison_shadow;
|
|
|
|
int FLAG_report_globals;
|
|
|
|
size_t FLAG_malloc_context_size = kMallocContextSize;
|
|
|
|
uintptr_t FLAG_large_malloc;
|
|
|
|
bool FLAG_handle_segv;
|
|
|
|
bool FLAG_replace_str;
|
|
|
|
bool FLAG_replace_intrin;
|
|
|
|
bool FLAG_replace_cfallocator; // Used on Mac only.
|
|
|
|
size_t FLAG_max_malloc_fill_size = 0;
|
|
|
|
bool FLAG_use_fake_stack;
|
|
|
|
int FLAG_exitcode = EXIT_FAILURE;
|
|
|
|
bool FLAG_allow_user_poisoning;
|
2012-01-31 08:52:18 +08:00
|
|
|
int FLAG_sleep_before_dying;
|
2011-11-30 09:07:02 +08:00
|
|
|
|
|
|
|
// -------------------------- Globals --------------------- {{{1
|
|
|
|
int asan_inited;
|
|
|
|
bool asan_init_is_running;
|
2012-02-14 05:24:29 +08:00
|
|
|
static void (*death_callback)(void);
|
2011-11-30 09:07:02 +08:00
|
|
|
|
|
|
|
// -------------------------- Misc ---------------- {{{1
|
|
|
|
void ShowStatsAndAbort() {
|
|
|
|
__asan_print_accumulated_stats();
|
2012-01-10 07:11:26 +08:00
|
|
|
AsanDie();
|
2011-11-30 09:07:02 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
static void PrintBytes(const char *before, uintptr_t *a) {
|
|
|
|
uint8_t *bytes = (uint8_t*)a;
|
|
|
|
size_t byte_num = (__WORDSIZE) / 8;
|
|
|
|
Printf("%s%p:", before, (uintptr_t)a);
|
|
|
|
for (size_t i = 0; i < byte_num; i++) {
|
|
|
|
Printf(" %lx%lx", bytes[i] >> 4, bytes[i] & 15);
|
|
|
|
}
|
|
|
|
Printf("\n");
|
|
|
|
}
|
|
|
|
|
2012-01-10 07:11:26 +08:00
|
|
|
size_t ReadFileToBuffer(const char *file_name, char **buff,
|
2012-01-05 08:44:33 +08:00
|
|
|
size_t *buff_size, size_t max_len) {
|
2011-12-29 06:58:01 +08:00
|
|
|
const size_t kMinFileLen = kPageSize;
|
2012-01-10 07:11:26 +08:00
|
|
|
size_t read_len = 0;
|
2011-12-29 06:58:01 +08:00
|
|
|
*buff = 0;
|
2012-01-05 08:44:33 +08:00
|
|
|
*buff_size = 0;
|
2011-12-29 06:58:01 +08:00
|
|
|
// The files we usually open are not seekable, so try different buffer sizes.
|
|
|
|
for (size_t size = kMinFileLen; size <= max_len; size *= 2) {
|
|
|
|
int fd = AsanOpenReadonly(file_name);
|
|
|
|
if (fd < 0) return -1;
|
2012-01-05 08:44:33 +08:00
|
|
|
AsanUnmapOrDie(*buff, *buff_size);
|
2011-12-29 06:58:01 +08:00
|
|
|
*buff = (char*)AsanMmapSomewhereOrDie(size, __FUNCTION__);
|
2012-01-05 08:44:33 +08:00
|
|
|
*buff_size = size;
|
2012-01-18 02:00:07 +08:00
|
|
|
// Read up to one page at a time.
|
|
|
|
read_len = 0;
|
|
|
|
bool reached_eof = false;
|
|
|
|
while (read_len + kPageSize <= size) {
|
|
|
|
size_t just_read = AsanRead(fd, *buff + read_len, kPageSize);
|
|
|
|
if (just_read == 0) {
|
|
|
|
reached_eof = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
read_len += just_read;
|
|
|
|
}
|
2011-12-29 06:58:01 +08:00
|
|
|
AsanClose(fd);
|
2012-01-18 02:00:07 +08:00
|
|
|
if (reached_eof) // We've read the whole file.
|
2011-12-29 06:58:01 +08:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
return read_len;
|
|
|
|
}
|
|
|
|
|
2012-02-14 05:24:29 +08:00
|
|
|
void AsanDie() {
|
2012-02-16 08:40:18 +08:00
|
|
|
static int num_calls = 0;
|
|
|
|
if (AtomicInc(&num_calls) > 1) return; // Don't die twice.
|
2012-02-14 05:24:29 +08:00
|
|
|
if (FLAG_sleep_before_dying) {
|
|
|
|
Report("Sleeping for %d second(s)\n", FLAG_sleep_before_dying);
|
|
|
|
SleepForSeconds(FLAG_sleep_before_dying);
|
|
|
|
}
|
|
|
|
if (death_callback)
|
|
|
|
death_callback();
|
|
|
|
Exit(FLAG_exitcode);
|
|
|
|
}
|
|
|
|
|
2011-11-30 09:07:02 +08:00
|
|
|
// ---------------------- mmap -------------------- {{{1
|
2011-12-29 06:58:01 +08:00
|
|
|
void OutOfMemoryMessageAndDie(const char *mem_type, size_t size) {
|
2011-11-30 09:07:02 +08:00
|
|
|
Report("ERROR: AddressSanitizer failed to allocate "
|
|
|
|
"0x%lx (%ld) bytes of %s\n",
|
|
|
|
size, size, mem_type);
|
2011-12-29 06:58:01 +08:00
|
|
|
PRINT_CURRENT_STACK();
|
|
|
|
ShowStatsAndAbort();
|
2011-11-30 09:07:02 +08:00
|
|
|
}
|
|
|
|
|
2011-12-29 07:28:54 +08:00
|
|
|
// Reserve memory range [beg, end].
|
|
|
|
static void ReserveShadowMemoryRange(uintptr_t beg, uintptr_t end) {
|
2011-11-30 09:07:02 +08:00
|
|
|
CHECK((beg % kPageSize) == 0);
|
|
|
|
CHECK(((end + 1) % kPageSize) == 0);
|
2011-12-29 07:28:54 +08:00
|
|
|
size_t size = end - beg + 1;
|
|
|
|
void *res = AsanMmapFixedNoReserve(beg, size);
|
|
|
|
CHECK(res == (void*)beg && "ReserveShadowMemoryRange failed");
|
2011-11-30 09:07:02 +08:00
|
|
|
}
|
|
|
|
|
2011-12-03 05:02:20 +08:00
|
|
|
// ---------------------- LowLevelAllocator ------------- {{{1
|
|
|
|
void *LowLevelAllocator::Allocate(size_t size) {
|
|
|
|
CHECK((size & (size - 1)) == 0 && "size must be a power of two");
|
|
|
|
if (allocated_end_ - allocated_current_ < size) {
|
|
|
|
size_t size_to_allocate = Max(size, kPageSize);
|
2011-12-29 06:58:01 +08:00
|
|
|
allocated_current_ =
|
|
|
|
(char*)AsanMmapSomewhereOrDie(size_to_allocate, __FUNCTION__);
|
2011-12-03 05:02:20 +08:00
|
|
|
allocated_end_ = allocated_current_ + size_to_allocate;
|
2011-12-16 01:41:30 +08:00
|
|
|
PoisonShadow((uintptr_t)allocated_current_, size_to_allocate,
|
|
|
|
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
|
|
|
|
static bool DescribeStackAddress(uintptr_t addr, uintptr_t access_size) {
|
|
|
|
AsanThread *t = asanThreadRegistry().FindThreadByStackAddress(addr);
|
|
|
|
if (!t) return false;
|
|
|
|
const intptr_t kBufSize = 4095;
|
|
|
|
char buf[kBufSize];
|
|
|
|
uintptr_t 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.
|
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,
|
|
|
|
static_cast<intptr_t>(name_end - frame_descr)));
|
2011-11-30 09:07:02 +08:00
|
|
|
Printf("Address %p is located at offset %ld "
|
|
|
|
"in frame <%s> of T%d's stack:\n",
|
|
|
|
addr, offset, buf, t->tid());
|
|
|
|
// Report the number of stack objects.
|
|
|
|
char *p;
|
2012-02-18 00:15:09 +08:00
|
|
|
size_t n_objects = internal_simple_strtoll(name_end, &p, 10);
|
2011-11-30 09:07:02 +08:00
|
|
|
CHECK(n_objects > 0);
|
|
|
|
Printf(" This frame has %ld object(s):\n", n_objects);
|
|
|
|
// Report all objects in this frame.
|
|
|
|
for (size_t i = 0; i < n_objects; i++) {
|
|
|
|
size_t beg, size;
|
|
|
|
intptr_t 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 != ' ') {
|
|
|
|
Printf("AddressSanitizer can't parse the stack frame descriptor: |%s|\n",
|
|
|
|
frame_descr);
|
|
|
|
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;
|
|
|
|
Printf(" [%ld, %ld) '%s'\n", beg, beg + size, buf);
|
|
|
|
}
|
|
|
|
Printf("HINT: this may be a false positive if your program uses "
|
|
|
|
"some custom stack unwind mechanism\n"
|
|
|
|
" (longjmp and C++ exceptions *are* supported)\n");
|
|
|
|
t->summary()->Announce();
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2012-02-03 16:37:19 +08:00
|
|
|
static NOINLINE void DescribeAddress(uintptr_t addr, uintptr_t 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-02-03 16:37:19 +08:00
|
|
|
NOINLINE ASAN_INTERFACE_ATTRIBUTE \
|
|
|
|
extern "C" void __asan_report_ ## type ## size(uintptr_t addr); \
|
2011-12-28 08:59:39 +08:00
|
|
|
extern "C" void __asan_report_ ## type ## size(uintptr_t addr) { \
|
|
|
|
GET_BP_PC_SP; \
|
|
|
|
__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.
|
2011-12-28 08:59:39 +08:00
|
|
|
static void force_interface_symbols() {
|
2011-11-30 09:07:02 +08:00
|
|
|
volatile int fake_condition = 0; // prevent dead condition elimination.
|
|
|
|
if (fake_condition) {
|
|
|
|
__asan_report_load1(NULL);
|
|
|
|
__asan_report_load2(NULL);
|
|
|
|
__asan_report_load4(NULL);
|
|
|
|
__asan_report_load8(NULL);
|
|
|
|
__asan_report_load16(NULL);
|
|
|
|
__asan_report_store1(NULL);
|
|
|
|
__asan_report_store2(NULL);
|
|
|
|
__asan_report_store4(NULL);
|
|
|
|
__asan_report_store8(NULL);
|
|
|
|
__asan_report_store16(NULL);
|
|
|
|
__asan_register_global(0, 0, NULL);
|
|
|
|
__asan_register_globals(NULL, 0);
|
2011-12-29 07:35:46 +08:00
|
|
|
__asan_unregister_globals(NULL, 0);
|
2011-11-30 09:07:02 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// -------------------------- Init ------------------- {{{1
|
|
|
|
static int64_t IntFlagValue(const char *flags, const char *flag,
|
|
|
|
int64_t default_val) {
|
|
|
|
if (!flags) return default_val;
|
2012-01-10 06:20:49 +08:00
|
|
|
const char *str = internal_strstr(flags, flag);
|
2011-11-30 09:07:02 +08:00
|
|
|
if (!str) return default_val;
|
2012-02-18 00:15:09 +08:00
|
|
|
return internal_atoll(str + internal_strlen(flag));
|
2011-11-30 09:07:02 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
static void asan_atexit() {
|
|
|
|
Printf("AddressSanitizer exit stats:\n");
|
|
|
|
__asan_print_accumulated_stats();
|
|
|
|
}
|
|
|
|
|
|
|
|
void CheckFailed(const char *cond, const char *file, int line) {
|
2012-01-10 03:18:27 +08:00
|
|
|
Report("CHECK failed: %s at %s:%d\n", cond, file, line);
|
2011-11-30 09:07:02 +08:00
|
|
|
PRINT_CURRENT_STACK();
|
|
|
|
ShowStatsAndAbort();
|
|
|
|
}
|
|
|
|
|
|
|
|
} // 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-02-09 05:33:27 +08:00
|
|
|
void __asan_handle_no_return() {
|
|
|
|
int local_stack;
|
|
|
|
AsanThread *curr_thread = asanThreadRegistry().GetCurrent();
|
|
|
|
CHECK(curr_thread);
|
|
|
|
uintptr_t top = curr_thread->stack_top();
|
|
|
|
uintptr_t bottom = ((uintptr_t)&local_stack - kPageSize) & ~(kPageSize-1);
|
|
|
|
PoisonShadow(bottom, top - bottom, 0);
|
|
|
|
}
|
|
|
|
|
2012-02-14 05:24:29 +08:00
|
|
|
void __asan_set_death_callback(void (*callback)(void)) {
|
|
|
|
death_callback = callback;
|
|
|
|
}
|
|
|
|
|
2011-11-30 09:07:02 +08:00
|
|
|
void __asan_report_error(uintptr_t pc, uintptr_t bp, uintptr_t sp,
|
|
|
|
uintptr_t addr, bool is_write, size_t access_size) {
|
|
|
|
// Do not print more than one report, otherwise they will mix up.
|
|
|
|
static int num_calls = 0;
|
|
|
|
if (AtomicInc(&num_calls) > 1) return;
|
|
|
|
|
|
|
|
Printf("=================================================================\n");
|
|
|
|
const char *bug_descr = "unknown-crash";
|
|
|
|
if (AddrIsInMem(addr)) {
|
|
|
|
uint8_t *shadow_addr = (uint8_t*)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();
|
|
|
|
int curr_tid = asanThreadRegistry().GetCurrentTidOrMinusOne();
|
|
|
|
|
|
|
|
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();
|
|
|
|
}
|
|
|
|
|
2011-11-30 09:07:02 +08:00
|
|
|
Report("ERROR: AddressSanitizer %s on address "
|
|
|
|
"%p at pc 0x%lx bp 0x%lx sp 0x%lx\n",
|
|
|
|
bug_descr, addr, pc, bp, sp);
|
|
|
|
|
|
|
|
Printf("%s of size %d at %p thread T%d\n",
|
|
|
|
access_size ? (is_write ? "WRITE" : "READ") : "ACCESS",
|
2011-12-09 09:49:31 +08:00
|
|
|
access_size, addr, curr_tid);
|
2011-11-30 09:07:02 +08:00
|
|
|
|
|
|
|
if (FLAG_debug) {
|
|
|
|
PrintBytes("PC: ", (uintptr_t*)pc);
|
|
|
|
}
|
|
|
|
|
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);
|
|
|
|
|
|
|
|
uintptr_t shadow_addr = MemToShadow(addr);
|
|
|
|
Report("ABORTING\n");
|
|
|
|
__asan_print_accumulated_stats();
|
|
|
|
Printf("Shadow byte and word:\n");
|
|
|
|
Printf(" %p: %x\n", shadow_addr, *(unsigned char*)shadow_addr);
|
|
|
|
uintptr_t aligned_shadow = shadow_addr & ~(kWordSize - 1);
|
|
|
|
PrintBytes(" ", (uintptr_t*)(aligned_shadow));
|
|
|
|
Printf("More shadow bytes:\n");
|
|
|
|
PrintBytes(" ", (uintptr_t*)(aligned_shadow-4*kWordSize));
|
|
|
|
PrintBytes(" ", (uintptr_t*)(aligned_shadow-3*kWordSize));
|
|
|
|
PrintBytes(" ", (uintptr_t*)(aligned_shadow-2*kWordSize));
|
|
|
|
PrintBytes(" ", (uintptr_t*)(aligned_shadow-1*kWordSize));
|
|
|
|
PrintBytes("=>", (uintptr_t*)(aligned_shadow+0*kWordSize));
|
|
|
|
PrintBytes(" ", (uintptr_t*)(aligned_shadow+1*kWordSize));
|
|
|
|
PrintBytes(" ", (uintptr_t*)(aligned_shadow+2*kWordSize));
|
|
|
|
PrintBytes(" ", (uintptr_t*)(aligned_shadow+3*kWordSize));
|
|
|
|
PrintBytes(" ", (uintptr_t*)(aligned_shadow+4*kWordSize));
|
2012-01-10 07:11:26 +08:00
|
|
|
AsanDie();
|
2011-11-30 09:07:02 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void __asan_init() {
|
|
|
|
if (asan_inited) return;
|
|
|
|
asan_init_is_running = true;
|
|
|
|
|
|
|
|
// Make sure we are not statically linked.
|
|
|
|
AsanDoesNotSupportStaticLinkage();
|
|
|
|
|
|
|
|
// flags
|
2012-01-13 20:59:48 +08:00
|
|
|
const char *options = AsanGetEnv("ASAN_OPTIONS");
|
2011-11-30 09:07:02 +08:00
|
|
|
FLAG_malloc_context_size =
|
|
|
|
IntFlagValue(options, "malloc_context_size=", kMallocContextSize);
|
|
|
|
CHECK(FLAG_malloc_context_size <= kMallocContextSize);
|
|
|
|
|
|
|
|
FLAG_max_malloc_fill_size =
|
|
|
|
IntFlagValue(options, "max_malloc_fill_size=", 0);
|
|
|
|
|
|
|
|
FLAG_v = IntFlagValue(options, "verbosity=", 0);
|
|
|
|
|
2012-02-16 21:35:11 +08:00
|
|
|
#if ASAN_LOW_MEMORY == 1
|
|
|
|
FLAG_quarantine_size =
|
2012-02-17 16:31:10 +08:00
|
|
|
IntFlagValue(options, "quarantine_size=", 1UL << 24); // 16M
|
2012-02-16 21:35:11 +08:00
|
|
|
FLAG_redzone = IntFlagValue(options, "redzone=", 64);
|
|
|
|
#else
|
|
|
|
FLAG_quarantine_size =
|
2012-02-17 16:31:10 +08:00
|
|
|
IntFlagValue(options, "quarantine_size=", 1UL << 28); // 256M
|
2011-11-30 09:07:02 +08:00
|
|
|
FLAG_redzone = IntFlagValue(options, "redzone=", 128);
|
2012-02-16 21:35:11 +08:00
|
|
|
#endif
|
2011-11-30 09:07:02 +08:00
|
|
|
CHECK(FLAG_redzone >= 32);
|
|
|
|
CHECK((FLAG_redzone & (FLAG_redzone - 1)) == 0);
|
|
|
|
|
|
|
|
FLAG_atexit = IntFlagValue(options, "atexit=", 0);
|
|
|
|
FLAG_poison_shadow = IntFlagValue(options, "poison_shadow=", 1);
|
|
|
|
FLAG_report_globals = IntFlagValue(options, "report_globals=", 1);
|
2011-12-09 02:30:42 +08:00
|
|
|
FLAG_handle_segv = IntFlagValue(options, "handle_segv=", ASAN_NEEDS_SEGV);
|
2011-11-30 09:07:02 +08:00
|
|
|
FLAG_symbolize = IntFlagValue(options, "symbolize=", 1);
|
|
|
|
FLAG_demangle = IntFlagValue(options, "demangle=", 1);
|
|
|
|
FLAG_debug = IntFlagValue(options, "debug=", 0);
|
|
|
|
FLAG_replace_cfallocator = IntFlagValue(options, "replace_cfallocator=", 1);
|
|
|
|
FLAG_replace_str = IntFlagValue(options, "replace_str=", 1);
|
2011-12-29 03:55:30 +08:00
|
|
|
FLAG_replace_intrin = IntFlagValue(options, "replace_intrin=", 1);
|
2011-11-30 09:07:02 +08:00
|
|
|
FLAG_use_fake_stack = IntFlagValue(options, "use_fake_stack=", 1);
|
|
|
|
FLAG_exitcode = IntFlagValue(options, "exitcode=", EXIT_FAILURE);
|
|
|
|
FLAG_allow_user_poisoning = IntFlagValue(options,
|
|
|
|
"allow_user_poisoning=", 1);
|
2012-01-31 08:52:18 +08:00
|
|
|
FLAG_sleep_before_dying = IntFlagValue(options, "sleep_before_dying=", 0);
|
2011-11-30 09:07:02 +08:00
|
|
|
|
|
|
|
if (FLAG_atexit) {
|
|
|
|
atexit(asan_atexit);
|
|
|
|
}
|
|
|
|
|
|
|
|
// interceptors
|
|
|
|
InitializeAsanInterceptors();
|
|
|
|
|
|
|
|
ReplaceSystemMalloc();
|
2012-01-10 03:18:27 +08:00
|
|
|
InstallSignalHandlers();
|
2011-11-30 09:07:02 +08:00
|
|
|
|
|
|
|
if (FLAG_v) {
|
|
|
|
Printf("|| `[%p, %p]` || HighMem ||\n", kHighMemBeg, kHighMemEnd);
|
|
|
|
Printf("|| `[%p, %p]` || HighShadow ||\n",
|
|
|
|
kHighShadowBeg, kHighShadowEnd);
|
|
|
|
Printf("|| `[%p, %p]` || ShadowGap ||\n",
|
|
|
|
kShadowGapBeg, kShadowGapEnd);
|
|
|
|
Printf("|| `[%p, %p]` || LowShadow ||\n",
|
|
|
|
kLowShadowBeg, kLowShadowEnd);
|
|
|
|
Printf("|| `[%p, %p]` || LowMem ||\n", kLowMemBeg, kLowMemEnd);
|
|
|
|
Printf("MemToShadow(shadow): %p %p %p %p\n",
|
|
|
|
MEM_TO_SHADOW(kLowShadowBeg),
|
|
|
|
MEM_TO_SHADOW(kLowShadowEnd),
|
|
|
|
MEM_TO_SHADOW(kHighShadowBeg),
|
|
|
|
MEM_TO_SHADOW(kHighShadowEnd));
|
|
|
|
Printf("red_zone=%ld\n", FLAG_redzone);
|
|
|
|
Printf("malloc_context_size=%ld\n", (int)FLAG_malloc_context_size);
|
|
|
|
|
|
|
|
Printf("SHADOW_SCALE: %lx\n", SHADOW_SCALE);
|
|
|
|
Printf("SHADOW_GRANULARITY: %lx\n", SHADOW_GRANULARITY);
|
|
|
|
Printf("SHADOW_OFFSET: %lx\n", SHADOW_OFFSET);
|
|
|
|
CHECK(SHADOW_SCALE >= 3 && SHADOW_SCALE <= 7);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (__WORDSIZE == 64) {
|
|
|
|
// Disable core dumper -- it makes little sense to dump 16T+ core.
|
2012-01-06 10:12:25 +08:00
|
|
|
AsanDisableCoreDumper();
|
2011-11-30 09:07:02 +08:00
|
|
|
}
|
|
|
|
|
2012-02-13 23:11:23 +08:00
|
|
|
if (AsanShadowRangeIsAvailable()) {
|
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
|
2011-12-29 07:28:54 +08:00
|
|
|
void *prot = AsanMprotect(kShadowGapBeg, kShadowGapEnd - kShadowGapBeg + 1);
|
|
|
|
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-02-22 16:27:32 +08:00
|
|
|
AsanProcMaps proc_maps;
|
|
|
|
proc_maps.Dump();
|
2012-02-13 23:11:23 +08:00
|
|
|
AsanDie();
|
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.
|
|
|
|
#pragma section(".CRT$XIB",long,read)
|
|
|
|
__declspec(allocate(".CRT$XIB")) void (*__asan_preinit)() = __asan_init;
|
2012-01-11 16:17:19 +08:00
|
|
|
#endif
|