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_internal.h"
|
|
|
|
#include "asan_mapping.h"
|
2012-08-09 17:06:52 +08:00
|
|
|
#include "asan_report.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"
|
2012-08-29 10:27:54 +08:00
|
|
|
#include "sanitizer/asan_interface.h"
|
2012-06-30 00:58:33 +08:00
|
|
|
#include "sanitizer_common/sanitizer_atomic.h"
|
2012-07-09 22:36:04 +08:00
|
|
|
#include "sanitizer_common/sanitizer_flags.h"
|
2012-06-05 15:25:47 +08:00
|
|
|
#include "sanitizer_common/sanitizer_libc.h"
|
2012-08-23 15:32:06 +08:00
|
|
|
#include "sanitizer_common/sanitizer_symbolizer.h"
|
2011-11-30 09:07:02 +08:00
|
|
|
|
2012-09-11 17:44:48 +08:00
|
|
|
namespace __asan {
|
2012-06-06 15:02:44 +08:00
|
|
|
|
2012-09-11 17:44:48 +08:00
|
|
|
static void AsanDie() {
|
2012-06-30 00:58:33 +08:00
|
|
|
static atomic_uint32_t num_calls;
|
|
|
|
if (atomic_fetch_add(&num_calls, 1, memory_order_relaxed) != 0) {
|
2012-06-06 15:02:44 +08:00
|
|
|
// Don't die twice - run a busy loop.
|
|
|
|
while (1) { }
|
|
|
|
}
|
2012-07-09 22:36:04 +08:00
|
|
|
if (flags()->sleep_before_dying) {
|
2012-08-06 21:00:21 +08:00
|
|
|
Report("Sleeping for %d second(s)\n", flags()->sleep_before_dying);
|
2012-07-09 22:36:04 +08:00
|
|
|
SleepForSeconds(flags()->sleep_before_dying);
|
2012-06-06 15:02:44 +08:00
|
|
|
}
|
2012-07-09 22:36:04 +08:00
|
|
|
if (flags()->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();
|
2012-07-09 22:36:04 +08:00
|
|
|
if (flags()->abort_on_error)
|
2012-06-06 15:02:44 +08:00
|
|
|
Abort();
|
2012-07-09 22:36:04 +08:00
|
|
|
Exit(flags()->exitcode);
|
2012-06-06 15:02:44 +08:00
|
|
|
}
|
|
|
|
|
2012-09-11 17:44:48 +08:00
|
|
|
static void AsanCheckFailed(const char *file, int line, const char *cond,
|
|
|
|
u64 v1, u64 v2) {
|
2012-08-28 19:34:40 +08:00
|
|
|
Report("AddressSanitizer CHECK failed: %s:%d \"%s\" (0x%zx, 0x%zx)\n",
|
2012-06-06 23:22:20 +08:00
|
|
|
file, line, cond, (uptr)v1, (uptr)v2);
|
2012-09-07 23:50:19 +08:00
|
|
|
// FIXME: check for infinite recursion without a thread-local counter here.
|
2012-06-06 23:22:20 +08:00
|
|
|
PRINT_CURRENT_STACK();
|
2012-12-13 19:09:26 +08:00
|
|
|
Die();
|
2012-06-06 23:22:20 +08:00
|
|
|
}
|
|
|
|
|
2011-11-30 09:07:02 +08:00
|
|
|
// -------------------------- Flags ------------------------- {{{1
|
2012-09-06 18:57:03 +08:00
|
|
|
static const int kDeafultMallocContextSize = 30;
|
2012-05-29 00:21:19 +08:00
|
|
|
|
2012-07-09 22:36:04 +08:00
|
|
|
static Flags asan_flags;
|
|
|
|
|
|
|
|
Flags *flags() {
|
|
|
|
return &asan_flags;
|
|
|
|
}
|
|
|
|
|
2012-12-08 06:01:28 +08:00
|
|
|
static const char *MaybeCallAsanDefaultOptions() {
|
|
|
|
return (&__asan_default_options) ? __asan_default_options() : "";
|
|
|
|
}
|
|
|
|
|
2012-07-09 22:36:04 +08:00
|
|
|
static void ParseFlagsFromString(Flags *f, const char *str) {
|
|
|
|
ParseFlag(str, &f->quarantine_size, "quarantine_size");
|
|
|
|
ParseFlag(str, &f->symbolize, "symbolize");
|
|
|
|
ParseFlag(str, &f->verbosity, "verbosity");
|
|
|
|
ParseFlag(str, &f->redzone, "redzone");
|
|
|
|
CHECK(f->redzone >= 16);
|
|
|
|
CHECK(IsPowerOfTwo(f->redzone));
|
|
|
|
|
|
|
|
ParseFlag(str, &f->debug, "debug");
|
|
|
|
ParseFlag(str, &f->report_globals, "report_globals");
|
2012-08-21 22:10:25 +08:00
|
|
|
ParseFlag(str, &f->check_initialization_order, "initialization_order");
|
2012-07-09 22:36:04 +08:00
|
|
|
ParseFlag(str, &f->malloc_context_size, "malloc_context_size");
|
2012-09-06 21:31:13 +08:00
|
|
|
CHECK((uptr)f->malloc_context_size <= kStackTraceMax);
|
2012-07-09 22:36:04 +08:00
|
|
|
|
|
|
|
ParseFlag(str, &f->replace_str, "replace_str");
|
|
|
|
ParseFlag(str, &f->replace_intrin, "replace_intrin");
|
|
|
|
ParseFlag(str, &f->replace_cfallocator, "replace_cfallocator");
|
|
|
|
ParseFlag(str, &f->mac_ignore_invalid_free, "mac_ignore_invalid_free");
|
|
|
|
ParseFlag(str, &f->use_fake_stack, "use_fake_stack");
|
|
|
|
ParseFlag(str, &f->max_malloc_fill_size, "max_malloc_fill_size");
|
|
|
|
ParseFlag(str, &f->exitcode, "exitcode");
|
|
|
|
ParseFlag(str, &f->allow_user_poisoning, "allow_user_poisoning");
|
|
|
|
ParseFlag(str, &f->sleep_before_dying, "sleep_before_dying");
|
|
|
|
ParseFlag(str, &f->handle_segv, "handle_segv");
|
|
|
|
ParseFlag(str, &f->use_sigaltstack, "use_sigaltstack");
|
|
|
|
ParseFlag(str, &f->check_malloc_usable_size, "check_malloc_usable_size");
|
|
|
|
ParseFlag(str, &f->unmap_shadow_on_exit, "unmap_shadow_on_exit");
|
|
|
|
ParseFlag(str, &f->abort_on_error, "abort_on_error");
|
|
|
|
ParseFlag(str, &f->atexit, "atexit");
|
|
|
|
ParseFlag(str, &f->disable_core, "disable_core");
|
2012-08-06 21:00:21 +08:00
|
|
|
ParseFlag(str, &f->strip_path_prefix, "strip_path_prefix");
|
2012-08-24 17:22:05 +08:00
|
|
|
ParseFlag(str, &f->allow_reexec, "allow_reexec");
|
2012-09-05 15:37:15 +08:00
|
|
|
ParseFlag(str, &f->print_full_thread_history, "print_full_thread_history");
|
2012-09-14 12:35:14 +08:00
|
|
|
ParseFlag(str, &f->log_path, "log_path");
|
2012-12-13 17:34:23 +08:00
|
|
|
ParseFlag(str, &f->fast_unwind_on_fatal, "fast_unwind_on_fatal");
|
|
|
|
ParseFlag(str, &f->fast_unwind_on_malloc, "fast_unwind_on_malloc");
|
2012-12-20 19:54:21 +08:00
|
|
|
ParseFlag(str, &f->poison_heap, "poison_heap");
|
2012-12-21 16:53:59 +08:00
|
|
|
ParseFlag(str, &f->alloc_dealloc_mismatch, "alloc_dealloc_mismatch");
|
2012-12-26 14:30:02 +08:00
|
|
|
ParseFlag(str, &f->use_stack_depot, "use_stack_depot");
|
2012-07-09 22:36:04 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void InitializeFlags(Flags *f, const char *env) {
|
|
|
|
internal_memset(f, 0, sizeof(*f));
|
|
|
|
|
2012-09-07 20:13:52 +08:00
|
|
|
f->quarantine_size = (ASAN_LOW_MEMORY) ? 1UL << 26 : 1UL << 28;
|
2012-07-09 22:36:04 +08:00
|
|
|
f->symbolize = false;
|
|
|
|
f->verbosity = 0;
|
2012-12-26 18:41:24 +08:00
|
|
|
f->redzone = ASAN_ALLOCATOR_VERSION == 2 ? 16 : (ASAN_LOW_MEMORY) ? 64 : 128;
|
2012-07-10 15:41:27 +08:00
|
|
|
f->debug = false;
|
2012-07-09 22:36:04 +08:00
|
|
|
f->report_globals = 1;
|
2012-08-21 22:10:25 +08:00
|
|
|
f->check_initialization_order = true;
|
2012-09-06 18:57:03 +08:00
|
|
|
f->malloc_context_size = kDeafultMallocContextSize;
|
2012-07-09 22:36:04 +08:00
|
|
|
f->replace_str = true;
|
|
|
|
f->replace_intrin = true;
|
2012-07-10 15:41:27 +08:00
|
|
|
f->replace_cfallocator = true;
|
|
|
|
f->mac_ignore_invalid_free = false;
|
2012-07-09 22:36:04 +08:00
|
|
|
f->use_fake_stack = true;
|
|
|
|
f->max_malloc_fill_size = 0;
|
|
|
|
f->exitcode = ASAN_DEFAULT_FAILURE_EXITCODE;
|
|
|
|
f->allow_user_poisoning = true;
|
|
|
|
f->sleep_before_dying = 0;
|
|
|
|
f->handle_segv = ASAN_NEEDS_SEGV;
|
|
|
|
f->use_sigaltstack = false;
|
|
|
|
f->check_malloc_usable_size = true;
|
|
|
|
f->unmap_shadow_on_exit = false;
|
|
|
|
f->abort_on_error = false;
|
|
|
|
f->atexit = false;
|
2012-11-21 20:38:58 +08:00
|
|
|
f->disable_core = (SANITIZER_WORDSIZE == 64);
|
2012-08-06 21:00:21 +08:00
|
|
|
f->strip_path_prefix = "";
|
2012-08-24 17:22:05 +08:00
|
|
|
f->allow_reexec = true;
|
2012-09-05 15:37:15 +08:00
|
|
|
f->print_full_thread_history = true;
|
2012-09-14 12:35:14 +08:00
|
|
|
f->log_path = 0;
|
2013-01-14 19:01:34 +08:00
|
|
|
f->fast_unwind_on_fatal = false;
|
2012-12-13 17:34:23 +08:00
|
|
|
f->fast_unwind_on_malloc = true;
|
2012-12-20 19:54:21 +08:00
|
|
|
f->poison_heap = true;
|
2013-01-14 18:18:38 +08:00
|
|
|
// Turn off alloc/dealloc mismatch checker on Mac for now.
|
|
|
|
// TODO(glider): Fix known issues and enable this back.
|
|
|
|
f->alloc_dealloc_mismatch = (ASAN_MAC == 0);
|
2012-12-26 14:30:02 +08:00
|
|
|
f->use_stack_depot = true; // Only affects allocator2.
|
2012-07-09 22:36:04 +08:00
|
|
|
|
|
|
|
// Override from user-specified string.
|
2012-12-08 06:01:28 +08:00
|
|
|
ParseFlagsFromString(f, MaybeCallAsanDefaultOptions());
|
2012-07-25 18:40:57 +08:00
|
|
|
if (flags()->verbosity) {
|
|
|
|
Report("Using the defaults from __asan_default_options: %s\n",
|
2012-12-08 06:01:28 +08:00
|
|
|
MaybeCallAsanDefaultOptions());
|
2012-07-09 22:36:04 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// Override from command line.
|
|
|
|
ParseFlagsFromString(f, env);
|
|
|
|
}
|
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);
|
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
|
|
|
}
|
|
|
|
|
|
|
|
// ---------------------- 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) {
|
2012-11-23 23:38:49 +08:00
|
|
|
CHECK((beg % GetPageSizeCached()) == 0);
|
|
|
|
CHECK(((end + 1) % GetPageSizeCached()) == 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);
|
2012-09-07 23:34:40 +08:00
|
|
|
if (res != (void*)beg) {
|
|
|
|
Report("ReserveShadowMemoryRange failed while trying to map 0x%zx bytes. "
|
|
|
|
"Perhaps you're using ulimit -v\n", size);
|
|
|
|
Abort();
|
|
|
|
}
|
2011-11-30 09:07:02 +08:00
|
|
|
}
|
|
|
|
|
2012-08-27 17:30:58 +08:00
|
|
|
// --------------- LowLevelAllocateCallbac ---------- {{{1
|
|
|
|
static void OnLowLevelAllocate(uptr ptr, uptr size) {
|
|
|
|
PoisonShadow(ptr, size, kAsanInternalHeapMagic);
|
2011-12-03 05:02:20 +08:00
|
|
|
}
|
|
|
|
|
2011-11-30 09:07:02 +08:00
|
|
|
// -------------------------- 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.
|
2012-08-09 17:46:12 +08:00
|
|
|
// __asan_report_* functions are noreturn, so we need a switch to prevent
|
|
|
|
// the compiler from removing any of them.
|
|
|
|
switch (fake_condition) {
|
|
|
|
case 1: __asan_report_load1(0); break;
|
|
|
|
case 2: __asan_report_load2(0); break;
|
|
|
|
case 3: __asan_report_load4(0); break;
|
|
|
|
case 4: __asan_report_load8(0); break;
|
|
|
|
case 5: __asan_report_load16(0); break;
|
|
|
|
case 6: __asan_report_store1(0); break;
|
|
|
|
case 7: __asan_report_store2(0); break;
|
|
|
|
case 8: __asan_report_store4(0); break;
|
|
|
|
case 9: __asan_report_store8(0); break;
|
|
|
|
case 10: __asan_report_store16(0); break;
|
|
|
|
case 12: __asan_register_globals(0, 0); break;
|
|
|
|
case 13: __asan_unregister_globals(0, 0); break;
|
|
|
|
case 14: __asan_set_death_callback(0); break;
|
|
|
|
case 15: __asan_set_error_report_callback(0); break;
|
|
|
|
case 16: __asan_handle_no_return(); break;
|
2012-08-10 00:05:17 +08:00
|
|
|
case 17: __asan_address_is_poisoned(0); break;
|
|
|
|
case 18: __asan_get_allocated_size(0); break;
|
|
|
|
case 19: __asan_get_current_allocated_bytes(); break;
|
|
|
|
case 20: __asan_get_estimated_allocated_size(0); break;
|
|
|
|
case 21: __asan_get_free_bytes(); break;
|
|
|
|
case 22: __asan_get_heap_size(); break;
|
|
|
|
case 23: __asan_get_ownership(0); break;
|
|
|
|
case 24: __asan_get_unmapped_bytes(); break;
|
|
|
|
case 25: __asan_poison_memory_region(0, 0); break;
|
|
|
|
case 26: __asan_unpoison_memory_region(0, 0); break;
|
|
|
|
case 27: __asan_set_error_exit_code(0); break;
|
|
|
|
case 28: __asan_stack_free(0, 0, 0); break;
|
|
|
|
case 29: __asan_stack_malloc(0, 0); break;
|
2012-12-08 06:01:28 +08:00
|
|
|
case 30: __asan_before_dynamic_init(0, 0); break;
|
|
|
|
case 31: __asan_after_dynamic_init(); break;
|
|
|
|
case 32: __asan_poison_stack_memory(0, 0); break;
|
|
|
|
case 33: __asan_unpoison_stack_memory(0, 0); break;
|
2012-12-28 23:24:16 +08:00
|
|
|
case 34: __asan_region_is_poisoned(0, 0); break;
|
2012-12-29 18:18:31 +08:00
|
|
|
case 35: __asan_describe_address(0); break;
|
2011-11-30 09:07:02 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void asan_atexit() {
|
2012-08-28 19:34:40 +08:00
|
|
|
Printf("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
|
|
|
|
|
2012-12-08 06:01:28 +08:00
|
|
|
#if !SANITIZER_SUPPORTS_WEAK_HOOKS
|
|
|
|
extern "C" {
|
|
|
|
SANITIZER_WEAK_ATTRIBUTE SANITIZER_INTERFACE_ATTRIBUTE
|
|
|
|
const char* __asan_default_options() { return ""; }
|
|
|
|
} // extern "C"
|
|
|
|
#endif
|
|
|
|
|
2012-08-10 00:05:17 +08:00
|
|
|
int NOINLINE __asan_set_error_exit_code(int exit_code) {
|
2012-07-09 22:36:04 +08:00
|
|
|
int old = flags()->exitcode;
|
|
|
|
flags()->exitcode = exit_code;
|
2011-11-30 09:07:02 +08:00
|
|
|
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-11-23 23:38:49 +08:00
|
|
|
uptr PageSize = GetPageSizeCached();
|
2012-05-31 22:35:53 +08:00
|
|
|
uptr top = curr_thread->stack_top();
|
2012-11-23 23:38:49 +08:00
|
|
|
uptr bottom = ((uptr)&local_stack - PageSize) & ~(PageSize-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-05-25 23:37:16 +08:00
|
|
|
void __asan_init() {
|
|
|
|
if (asan_inited) return;
|
2012-08-27 17:30:58 +08:00
|
|
|
CHECK(!asan_init_is_running && "ASan init calls itself!");
|
2012-05-25 23:37:16 +08:00
|
|
|
asan_init_is_running = true;
|
|
|
|
|
|
|
|
// Make sure we are not statically linked.
|
|
|
|
AsanDoesNotSupportStaticLinkage();
|
|
|
|
|
2012-09-11 17:44:48 +08:00
|
|
|
// Install tool-specific callbacks in sanitizer_common.
|
|
|
|
SetDieCallback(AsanDie);
|
|
|
|
SetCheckFailedCallback(AsanCheckFailed);
|
2012-08-28 19:34:40 +08:00
|
|
|
SetPrintfAndReportCallback(AppendToErrorMessageBuffer);
|
|
|
|
|
2012-08-24 17:22:05 +08:00
|
|
|
// Initialize flags. This must be done early, because most of the
|
|
|
|
// initialization steps look at flags().
|
2012-06-14 22:07:21 +08:00
|
|
|
const char *options = GetEnv("ASAN_OPTIONS");
|
2012-07-09 22:36:04 +08:00
|
|
|
InitializeFlags(flags(), options);
|
2012-09-14 12:35:14 +08:00
|
|
|
__sanitizer_set_report_path(flags()->log_path);
|
2012-03-17 00:38:31 +08:00
|
|
|
|
2012-07-09 22:36:04 +08:00
|
|
|
if (flags()->verbosity && options) {
|
2012-03-17 00:38:31 +08:00
|
|
|
Report("Parsed ASAN_OPTIONS: %s\n", options);
|
|
|
|
}
|
|
|
|
|
2012-08-24 17:22:05 +08:00
|
|
|
// Re-exec ourselves if we need to set additional env or command line args.
|
|
|
|
MaybeReexec();
|
|
|
|
|
2012-08-27 22:04:54 +08:00
|
|
|
// Setup internal allocator callback.
|
|
|
|
SetLowLevelAllocateCallback(OnLowLevelAllocate);
|
|
|
|
|
2012-07-09 22:36:04 +08:00
|
|
|
if (flags()->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
|
|
|
|
2012-07-09 22:36:04 +08:00
|
|
|
if (flags()->verbosity) {
|
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-07-09 22:36:04 +08:00
|
|
|
Printf("red_zone=%zu\n", (uptr)flags()->redzone);
|
|
|
|
Printf("malloc_context_size=%zu\n", (uptr)flags()->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-07-09 22:36:04 +08:00
|
|
|
if (flags()->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;
|
2012-11-24 13:03:11 +08:00
|
|
|
if (kLowShadowBeg > 0) shadow_start -= GetMmapGranularity();
|
2012-06-15 15:29:14 +08:00
|
|
|
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.
|
2012-11-24 13:03:11 +08:00
|
|
|
ReserveShadowMemoryRange(kLowShadowBeg - GetMmapGranularity(),
|
|
|
|
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();
|
2012-08-23 15:32:06 +08:00
|
|
|
// Start symbolizer process if necessary.
|
|
|
|
if (flags()->symbolize) {
|
|
|
|
const char *external_symbolizer = GetEnv("ASAN_SYMBOLIZER_PATH");
|
|
|
|
if (external_symbolizer) {
|
|
|
|
InitializeExternalSymbolizer(external_symbolizer);
|
|
|
|
}
|
|
|
|
}
|
2012-04-05 18:54:52 +08:00
|
|
|
|
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
|
|
|
|
2012-07-09 22:36:04 +08:00
|
|
|
if (flags()->verbosity) {
|
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
|