asan_rtl.cc revision dbd69cc9ccda50efd0305cd0ce9bad50defe4b42
190dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)//===-- asan_rtl.cc -------------------------------------------------------===// 290dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)// 390dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)// The LLVM Compiler Infrastructure 490dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)// 590dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)// This file is distributed under the University of Illinois Open Source 690dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)// License. See LICENSE.TXT for details. 790dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)// 890dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)//===----------------------------------------------------------------------===// 990dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)// 1090dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)// This file is a part of AddressSanitizer, an address sanity checker. 1190dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)// 1290dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)// Main file of the ASan run-time library. 137dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch//===----------------------------------------------------------------------===// 14558790d6acca3451cf3a6b497803a5f07d0bec58Ben Murdoch#include "asan_allocator.h" 15558790d6acca3451cf3a6b497803a5f07d0bec58Ben Murdoch#include "asan_interceptors.h" 1690dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)#include "asan_interface_internal.h" 1790dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)#include "asan_internal.h" 1890dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)#include "asan_mapping.h" 1990dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)#include "asan_poisoning.h" 2090dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)#include "asan_report.h" 2190dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)#include "asan_stack.h" 2290dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)#include "asan_stats.h" 2390dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)#include "asan_thread.h" 2490dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)#include "sanitizer_common/sanitizer_atomic.h" 2590dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)#include "sanitizer_common/sanitizer_flags.h" 2690dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)#include "sanitizer_common/sanitizer_libc.h" 2790dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)#include "sanitizer_common/sanitizer_symbolizer.h" 287dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch#include "lsan/lsan_common.h" 29eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch 307dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdochint __asan_option_detect_stack_use_after_return; // Global interface symbol. 3190dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) 32558790d6acca3451cf3a6b497803a5f07d0bec58Ben Murdochnamespace __asan { 3390dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) 3490dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)uptr AsanMappingProfile[kAsanMappingProfileSize]; 357dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch 367dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdochstatic void AsanDie() { 377dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch static atomic_uint32_t num_calls; 387dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch if (atomic_fetch_add(&num_calls, 1, memory_order_relaxed) != 0) { 397dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch // Don't die twice - run a busy loop. 407dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch while (1) { } 4190dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) } 42 if (flags()->sleep_before_dying) { 43 Report("Sleeping for %d second(s)\n", flags()->sleep_before_dying); 44 SleepForSeconds(flags()->sleep_before_dying); 45 } 46 if (flags()->unmap_shadow_on_exit) { 47 if (kMidMemBeg) { 48 UnmapOrDie((void*)kLowShadowBeg, kMidMemBeg - kLowShadowBeg); 49 UnmapOrDie((void*)kMidMemEnd, kHighShadowEnd - kMidMemEnd); 50 } else { 51 UnmapOrDie((void*)kLowShadowBeg, kHighShadowEnd - kLowShadowBeg); 52 } 53 } 54 if (death_callback) 55 death_callback(); 56 if (flags()->abort_on_error) 57 Abort(); 58 internal__exit(flags()->exitcode); 59} 60 61static void AsanCheckFailed(const char *file, int line, const char *cond, 62 u64 v1, u64 v2) { 63 Report("AddressSanitizer CHECK failed: %s:%d \"%s\" (0x%zx, 0x%zx)\n", 64 file, line, cond, (uptr)v1, (uptr)v2); 65 // FIXME: check for infinite recursion without a thread-local counter here. 66 PRINT_CURRENT_STACK(); 67 Die(); 68} 69 70// -------------------------- Flags ------------------------- {{{1 71static const int kDefaultMallocContextSize = 30; 72 73Flags asan_flags_dont_use_directly; // use via flags(). 74 75static const char *MaybeCallAsanDefaultOptions() { 76 return (&__asan_default_options) ? __asan_default_options() : ""; 77} 78 79static const char *MaybeUseAsanDefaultOptionsCompileDefiniton() { 80#ifdef ASAN_DEFAULT_OPTIONS 81// Stringize the macro value. 82# define ASAN_STRINGIZE(x) #x 83# define ASAN_STRINGIZE_OPTIONS(options) ASAN_STRINGIZE(options) 84 return ASAN_STRINGIZE_OPTIONS(ASAN_DEFAULT_OPTIONS); 85#else 86 return ""; 87#endif 88} 89 90static void ParseFlagsFromString(Flags *f, const char *str) { 91 ParseCommonFlagsFromString(str); 92 CHECK((uptr)common_flags()->malloc_context_size <= kStackTraceMax); 93 94 ParseFlag(str, &f->quarantine_size, "quarantine_size"); 95 ParseFlag(str, &f->redzone, "redzone"); 96 CHECK_GE(f->redzone, 16); 97 CHECK(IsPowerOfTwo(f->redzone)); 98 99 ParseFlag(str, &f->debug, "debug"); 100 ParseFlag(str, &f->report_globals, "report_globals"); 101 ParseFlag(str, &f->check_initialization_order, "check_initialization_order"); 102 103 ParseFlag(str, &f->replace_str, "replace_str"); 104 ParseFlag(str, &f->replace_intrin, "replace_intrin"); 105 ParseFlag(str, &f->mac_ignore_invalid_free, "mac_ignore_invalid_free"); 106 ParseFlag(str, &f->detect_stack_use_after_return, 107 "detect_stack_use_after_return"); 108 ParseFlag(str, &f->uar_stack_size_log, "uar_stack_size_log"); 109 ParseFlag(str, &f->max_malloc_fill_size, "max_malloc_fill_size"); 110 ParseFlag(str, &f->malloc_fill_byte, "malloc_fill_byte"); 111 ParseFlag(str, &f->exitcode, "exitcode"); 112 ParseFlag(str, &f->allow_user_poisoning, "allow_user_poisoning"); 113 ParseFlag(str, &f->sleep_before_dying, "sleep_before_dying"); 114 ParseFlag(str, &f->handle_segv, "handle_segv"); 115 ParseFlag(str, &f->allow_user_segv_handler, "allow_user_segv_handler"); 116 ParseFlag(str, &f->use_sigaltstack, "use_sigaltstack"); 117 ParseFlag(str, &f->check_malloc_usable_size, "check_malloc_usable_size"); 118 ParseFlag(str, &f->unmap_shadow_on_exit, "unmap_shadow_on_exit"); 119 ParseFlag(str, &f->abort_on_error, "abort_on_error"); 120 ParseFlag(str, &f->print_stats, "print_stats"); 121 ParseFlag(str, &f->print_legend, "print_legend"); 122 ParseFlag(str, &f->atexit, "atexit"); 123 ParseFlag(str, &f->coverage, "coverage"); 124 ParseFlag(str, &f->disable_core, "disable_core"); 125 ParseFlag(str, &f->allow_reexec, "allow_reexec"); 126 ParseFlag(str, &f->print_full_thread_history, "print_full_thread_history"); 127 ParseFlag(str, &f->poison_heap, "poison_heap"); 128 ParseFlag(str, &f->poison_partial, "poison_partial"); 129 ParseFlag(str, &f->alloc_dealloc_mismatch, "alloc_dealloc_mismatch"); 130 ParseFlag(str, &f->strict_memcmp, "strict_memcmp"); 131 ParseFlag(str, &f->strict_init_order, "strict_init_order"); 132} 133 134void InitializeFlags(Flags *f, const char *env) { 135 CommonFlags *cf = common_flags(); 136 SetCommonFlagDefaults(); 137 cf->external_symbolizer_path = GetEnv("ASAN_SYMBOLIZER_PATH"); 138 cf->malloc_context_size = kDefaultMallocContextSize; 139 140 internal_memset(f, 0, sizeof(*f)); 141 f->quarantine_size = (ASAN_LOW_MEMORY) ? 1UL << 26 : 1UL << 28; 142 f->redzone = 16; 143 f->debug = false; 144 f->report_globals = 1; 145 f->check_initialization_order = false; 146 f->replace_str = true; 147 f->replace_intrin = true; 148 f->mac_ignore_invalid_free = false; 149 f->detect_stack_use_after_return = false; // Also needs the compiler flag. 150 f->uar_stack_size_log = 0; 151 f->max_malloc_fill_size = 0x1000; // By default, fill only the first 4K. 152 f->malloc_fill_byte = 0xbe; 153 f->exitcode = ASAN_DEFAULT_FAILURE_EXITCODE; 154 f->allow_user_poisoning = true; 155 f->sleep_before_dying = 0; 156 f->handle_segv = ASAN_NEEDS_SEGV; 157 f->allow_user_segv_handler = false; 158 f->use_sigaltstack = false; 159 f->check_malloc_usable_size = true; 160 f->unmap_shadow_on_exit = false; 161 f->abort_on_error = false; 162 f->print_stats = false; 163 f->print_legend = true; 164 f->atexit = false; 165 f->coverage = false; 166 f->disable_core = (SANITIZER_WORDSIZE == 64); 167 f->allow_reexec = true; 168 f->print_full_thread_history = true; 169 f->poison_heap = true; 170 f->poison_partial = true; 171 // Turn off alloc/dealloc mismatch checker on Mac and Windows for now. 172 // TODO(glider,timurrrr): Fix known issues and enable this back. 173 f->alloc_dealloc_mismatch = (SANITIZER_MAC == 0) && (SANITIZER_WINDOWS == 0); 174 f->strict_memcmp = true; 175 f->strict_init_order = false; 176 177 // Override from compile definition. 178 ParseFlagsFromString(f, MaybeUseAsanDefaultOptionsCompileDefiniton()); 179 180 // Override from user-specified string. 181 ParseFlagsFromString(f, MaybeCallAsanDefaultOptions()); 182 if (common_flags()->verbosity) { 183 Report("Using the defaults from __asan_default_options: %s\n", 184 MaybeCallAsanDefaultOptions()); 185 } 186 187 // Override from command line. 188 ParseFlagsFromString(f, env); 189 190#if !CAN_SANITIZE_LEAKS 191 if (cf->detect_leaks) { 192 Report("%s: detect_leaks is not supported on this platform.\n", 193 SanitizerToolName); 194 cf->detect_leaks = false; 195 } 196#endif 197 198 // Make "strict_init_order" imply "check_initialization_order". 199 // TODO(samsonov): Use a single runtime flag for an init-order checker. 200 if (f->strict_init_order) { 201 f->check_initialization_order = true; 202 } 203} 204 205// -------------------------- Globals --------------------- {{{1 206int asan_inited; 207bool asan_init_is_running; 208void (*death_callback)(void); 209 210#if !ASAN_FIXED_MAPPING 211uptr kHighMemEnd, kMidMemBeg, kMidMemEnd; 212#endif 213 214// -------------------------- Misc ---------------- {{{1 215void ShowStatsAndAbort() { 216 __asan_print_accumulated_stats(); 217 Die(); 218} 219 220// ---------------------- mmap -------------------- {{{1 221// Reserve memory range [beg, end]. 222static void ReserveShadowMemoryRange(uptr beg, uptr end) { 223 CHECK_EQ((beg % GetPageSizeCached()), 0); 224 CHECK_EQ(((end + 1) % GetPageSizeCached()), 0); 225 uptr size = end - beg + 1; 226 void *res = MmapFixedNoReserve(beg, size); 227 if (res != (void*)beg) { 228 Report("ReserveShadowMemoryRange failed while trying to map 0x%zx bytes. " 229 "Perhaps you're using ulimit -v\n", size); 230 Abort(); 231 } 232} 233 234// --------------- LowLevelAllocateCallbac ---------- {{{1 235static void OnLowLevelAllocate(uptr ptr, uptr size) { 236 PoisonShadow(ptr, size, kAsanInternalHeapMagic); 237} 238 239// -------------------------- Run-time entry ------------------- {{{1 240// exported functions 241#define ASAN_REPORT_ERROR(type, is_write, size) \ 242extern "C" NOINLINE INTERFACE_ATTRIBUTE \ 243void __asan_report_ ## type ## size(uptr addr); \ 244void __asan_report_ ## type ## size(uptr addr) { \ 245 GET_CALLER_PC_BP_SP; \ 246 __asan_report_error(pc, bp, sp, addr, is_write, size); \ 247} 248 249ASAN_REPORT_ERROR(load, false, 1) 250ASAN_REPORT_ERROR(load, false, 2) 251ASAN_REPORT_ERROR(load, false, 4) 252ASAN_REPORT_ERROR(load, false, 8) 253ASAN_REPORT_ERROR(load, false, 16) 254ASAN_REPORT_ERROR(store, true, 1) 255ASAN_REPORT_ERROR(store, true, 2) 256ASAN_REPORT_ERROR(store, true, 4) 257ASAN_REPORT_ERROR(store, true, 8) 258ASAN_REPORT_ERROR(store, true, 16) 259 260#define ASAN_REPORT_ERROR_N(type, is_write) \ 261extern "C" NOINLINE INTERFACE_ATTRIBUTE \ 262void __asan_report_ ## type ## _n(uptr addr, uptr size); \ 263void __asan_report_ ## type ## _n(uptr addr, uptr size) { \ 264 GET_CALLER_PC_BP_SP; \ 265 __asan_report_error(pc, bp, sp, addr, is_write, size); \ 266} 267 268ASAN_REPORT_ERROR_N(load, false) 269ASAN_REPORT_ERROR_N(store, true) 270 271// Force the linker to keep the symbols for various ASan interface functions. 272// We want to keep those in the executable in order to let the instrumented 273// dynamic libraries access the symbol even if it is not used by the executable 274// itself. This should help if the build system is removing dead code at link 275// time. 276static NOINLINE void force_interface_symbols() { 277 volatile int fake_condition = 0; // prevent dead condition elimination. 278 // __asan_report_* functions are noreturn, so we need a switch to prevent 279 // the compiler from removing any of them. 280 switch (fake_condition) { 281 case 1: __asan_report_load1(0); break; 282 case 2: __asan_report_load2(0); break; 283 case 3: __asan_report_load4(0); break; 284 case 4: __asan_report_load8(0); break; 285 case 5: __asan_report_load16(0); break; 286 case 6: __asan_report_store1(0); break; 287 case 7: __asan_report_store2(0); break; 288 case 8: __asan_report_store4(0); break; 289 case 9: __asan_report_store8(0); break; 290 case 10: __asan_report_store16(0); break; 291 case 12: __asan_register_globals(0, 0); break; 292 case 13: __asan_unregister_globals(0, 0); break; 293 case 14: __asan_set_death_callback(0); break; 294 case 15: __asan_set_error_report_callback(0); break; 295 case 16: __asan_handle_no_return(); break; 296 case 17: __asan_address_is_poisoned(0); break; 297 case 18: __asan_get_allocated_size(0); break; 298 case 19: __asan_get_current_allocated_bytes(); break; 299 case 20: __asan_get_estimated_allocated_size(0); break; 300 case 21: __asan_get_free_bytes(); break; 301 case 22: __asan_get_heap_size(); break; 302 case 23: __asan_get_ownership(0); break; 303 case 24: __asan_get_unmapped_bytes(); break; 304 case 25: __asan_poison_memory_region(0, 0); break; 305 case 26: __asan_unpoison_memory_region(0, 0); break; 306 case 27: __asan_set_error_exit_code(0); break; 307 case 30: __asan_before_dynamic_init(0); break; 308 case 31: __asan_after_dynamic_init(); break; 309 case 32: __asan_poison_stack_memory(0, 0); break; 310 case 33: __asan_unpoison_stack_memory(0, 0); break; 311 case 34: __asan_region_is_poisoned(0, 0); break; 312 case 35: __asan_describe_address(0); break; 313 } 314} 315 316static void asan_atexit() { 317 Printf("AddressSanitizer exit stats:\n"); 318 __asan_print_accumulated_stats(); 319 // Print AsanMappingProfile. 320 for (uptr i = 0; i < kAsanMappingProfileSize; i++) { 321 if (AsanMappingProfile[i] == 0) continue; 322 Printf("asan_mapping.h:%zd -- %zd\n", i, AsanMappingProfile[i]); 323 } 324} 325 326static void InitializeHighMemEnd() { 327#if !ASAN_FIXED_MAPPING 328 kHighMemEnd = GetMaxVirtualAddress(); 329 // Increase kHighMemEnd to make sure it's properly 330 // aligned together with kHighMemBeg: 331 kHighMemEnd |= SHADOW_GRANULARITY * GetPageSizeCached() - 1; 332#endif // !ASAN_FIXED_MAPPING 333 CHECK_EQ((kHighMemBeg % GetPageSizeCached()), 0); 334} 335 336static void ProtectGap(uptr a, uptr size) { 337 CHECK_EQ(a, (uptr)Mprotect(a, size)); 338} 339 340static void PrintAddressSpaceLayout() { 341 Printf("|| `[%p, %p]` || HighMem ||\n", 342 (void*)kHighMemBeg, (void*)kHighMemEnd); 343 Printf("|| `[%p, %p]` || HighShadow ||\n", 344 (void*)kHighShadowBeg, (void*)kHighShadowEnd); 345 if (kMidMemBeg) { 346 Printf("|| `[%p, %p]` || ShadowGap3 ||\n", 347 (void*)kShadowGap3Beg, (void*)kShadowGap3End); 348 Printf("|| `[%p, %p]` || MidMem ||\n", 349 (void*)kMidMemBeg, (void*)kMidMemEnd); 350 Printf("|| `[%p, %p]` || ShadowGap2 ||\n", 351 (void*)kShadowGap2Beg, (void*)kShadowGap2End); 352 Printf("|| `[%p, %p]` || MidShadow ||\n", 353 (void*)kMidShadowBeg, (void*)kMidShadowEnd); 354 } 355 Printf("|| `[%p, %p]` || ShadowGap ||\n", 356 (void*)kShadowGapBeg, (void*)kShadowGapEnd); 357 if (kLowShadowBeg) { 358 Printf("|| `[%p, %p]` || LowShadow ||\n", 359 (void*)kLowShadowBeg, (void*)kLowShadowEnd); 360 Printf("|| `[%p, %p]` || LowMem ||\n", 361 (void*)kLowMemBeg, (void*)kLowMemEnd); 362 } 363 Printf("MemToShadow(shadow): %p %p %p %p", 364 (void*)MEM_TO_SHADOW(kLowShadowBeg), 365 (void*)MEM_TO_SHADOW(kLowShadowEnd), 366 (void*)MEM_TO_SHADOW(kHighShadowBeg), 367 (void*)MEM_TO_SHADOW(kHighShadowEnd)); 368 if (kMidMemBeg) { 369 Printf(" %p %p", 370 (void*)MEM_TO_SHADOW(kMidShadowBeg), 371 (void*)MEM_TO_SHADOW(kMidShadowEnd)); 372 } 373 Printf("\n"); 374 Printf("red_zone=%zu\n", (uptr)flags()->redzone); 375 Printf("quarantine_size=%zuM\n", (uptr)flags()->quarantine_size >> 20); 376 Printf("malloc_context_size=%zu\n", 377 (uptr)common_flags()->malloc_context_size); 378 379 Printf("SHADOW_SCALE: %zx\n", (uptr)SHADOW_SCALE); 380 Printf("SHADOW_GRANULARITY: %zx\n", (uptr)SHADOW_GRANULARITY); 381 Printf("SHADOW_OFFSET: %zx\n", (uptr)SHADOW_OFFSET); 382 CHECK(SHADOW_SCALE >= 3 && SHADOW_SCALE <= 7); 383 if (kMidMemBeg) 384 CHECK(kMidShadowBeg > kLowShadowEnd && 385 kMidMemBeg > kMidShadowEnd && 386 kHighShadowBeg > kMidMemEnd); 387} 388 389} // namespace __asan 390 391// ---------------------- Interface ---------------- {{{1 392using namespace __asan; // NOLINT 393 394#if !SANITIZER_SUPPORTS_WEAK_HOOKS 395extern "C" { 396SANITIZER_INTERFACE_ATTRIBUTE SANITIZER_WEAK_ATTRIBUTE 397const char* __asan_default_options() { return ""; } 398} // extern "C" 399#endif 400 401int NOINLINE __asan_set_error_exit_code(int exit_code) { 402 int old = flags()->exitcode; 403 flags()->exitcode = exit_code; 404 return old; 405} 406 407void NOINLINE __asan_handle_no_return() { 408 int local_stack; 409 AsanThread *curr_thread = GetCurrentThread(); 410 CHECK(curr_thread); 411 uptr PageSize = GetPageSizeCached(); 412 uptr top = curr_thread->stack_top(); 413 uptr bottom = ((uptr)&local_stack - PageSize) & ~(PageSize-1); 414 static const uptr kMaxExpectedCleanupSize = 64 << 20; // 64M 415 if (top - bottom > kMaxExpectedCleanupSize) { 416 static bool reported_warning = false; 417 if (reported_warning) 418 return; 419 reported_warning = true; 420 Report("WARNING: ASan is ignoring requested __asan_handle_no_return: " 421 "stack top: %p; bottom %p; size: %p (%zd)\n" 422 "False positive error reports may follow\n" 423 "For details see " 424 "http://code.google.com/p/address-sanitizer/issues/detail?id=189\n", 425 top, bottom, top - bottom, top - bottom); 426 return; 427 } 428 PoisonShadow(bottom, top - bottom, 0); 429 if (curr_thread->has_fake_stack()) 430 curr_thread->fake_stack()->HandleNoReturn(); 431} 432 433void NOINLINE __asan_set_death_callback(void (*callback)(void)) { 434 death_callback = callback; 435} 436 437void __asan_init() { 438 if (asan_inited) return; 439 SanitizerToolName = "AddressSanitizer"; 440 CHECK(!asan_init_is_running && "ASan init calls itself!"); 441 asan_init_is_running = true; 442 InitializeHighMemEnd(); 443 444 // Make sure we are not statically linked. 445 AsanDoesNotSupportStaticLinkage(); 446 447 // Install tool-specific callbacks in sanitizer_common. 448 SetDieCallback(AsanDie); 449 SetCheckFailedCallback(AsanCheckFailed); 450 SetPrintfAndReportCallback(AppendToErrorMessageBuffer); 451 452 // Initialize flags. This must be done early, because most of the 453 // initialization steps look at flags(). 454 const char *options = GetEnv("ASAN_OPTIONS"); 455 InitializeFlags(flags(), options); 456 __sanitizer_set_report_path(common_flags()->log_path); 457 __asan_option_detect_stack_use_after_return = 458 flags()->detect_stack_use_after_return; 459 460 if (common_flags()->verbosity && options) { 461 Report("Parsed ASAN_OPTIONS: %s\n", options); 462 } 463 464 // Re-exec ourselves if we need to set additional env or command line args. 465 MaybeReexec(); 466 467 // Setup internal allocator callback. 468 SetLowLevelAllocateCallback(OnLowLevelAllocate); 469 470 InitializeAsanInterceptors(); 471 472 ReplaceSystemMalloc(); 473 ReplaceOperatorsNewAndDelete(); 474 475 uptr shadow_start = kLowShadowBeg; 476 if (kLowShadowBeg) 477 shadow_start -= GetMmapGranularity(); 478 bool full_shadow_is_available = 479 MemoryRangeIsAvailable(shadow_start, kHighShadowEnd); 480 481#if SANITIZER_LINUX && defined(__x86_64__) && !ASAN_FIXED_MAPPING 482 if (!full_shadow_is_available) { 483 kMidMemBeg = kLowMemEnd < 0x3000000000ULL ? 0x3000000000ULL : 0; 484 kMidMemEnd = kLowMemEnd < 0x3000000000ULL ? 0x4fffffffffULL : 0; 485 } 486#endif 487 488 if (common_flags()->verbosity) 489 PrintAddressSpaceLayout(); 490 491 if (flags()->disable_core) { 492 DisableCoreDumper(); 493 } 494 495 if (full_shadow_is_available) { 496 // mmap the low shadow plus at least one page at the left. 497 if (kLowShadowBeg) 498 ReserveShadowMemoryRange(shadow_start, kLowShadowEnd); 499 // mmap the high shadow. 500 ReserveShadowMemoryRange(kHighShadowBeg, kHighShadowEnd); 501 // protect the gap. 502 ProtectGap(kShadowGapBeg, kShadowGapEnd - kShadowGapBeg + 1); 503 } else if (kMidMemBeg && 504 MemoryRangeIsAvailable(shadow_start, kMidMemBeg - 1) && 505 MemoryRangeIsAvailable(kMidMemEnd + 1, kHighShadowEnd)) { 506 CHECK(kLowShadowBeg != kLowShadowEnd); 507 // mmap the low shadow plus at least one page at the left. 508 ReserveShadowMemoryRange(shadow_start, kLowShadowEnd); 509 // mmap the mid shadow. 510 ReserveShadowMemoryRange(kMidShadowBeg, kMidShadowEnd); 511 // mmap the high shadow. 512 ReserveShadowMemoryRange(kHighShadowBeg, kHighShadowEnd); 513 // protect the gaps. 514 ProtectGap(kShadowGapBeg, kShadowGapEnd - kShadowGapBeg + 1); 515 ProtectGap(kShadowGap2Beg, kShadowGap2End - kShadowGap2Beg + 1); 516 ProtectGap(kShadowGap3Beg, kShadowGap3End - kShadowGap3Beg + 1); 517 } else { 518 Report("Shadow memory range interleaves with an existing memory mapping. " 519 "ASan cannot proceed correctly. ABORTING.\n"); 520 DumpProcessMap(); 521 Die(); 522 } 523 524 AsanTSDInit(PlatformTSDDtor); 525 InstallSignalHandlers(); 526 527 // Allocator should be initialized before starting external symbolizer, as 528 // fork() on Mac locks the allocator. 529 InitializeAllocator(); 530 531 // Start symbolizer process if necessary. 532 if (common_flags()->symbolize) { 533 Symbolizer::Init(common_flags()->external_symbolizer_path); 534 } else { 535 Symbolizer::Disable(); 536 } 537 538 // On Linux AsanThread::ThreadStart() calls malloc() that's why asan_inited 539 // should be set to 1 prior to initializing the threads. 540 asan_inited = 1; 541 asan_init_is_running = false; 542 543 if (flags()->atexit) 544 Atexit(asan_atexit); 545 546 if (flags()->coverage) 547 Atexit(__sanitizer_cov_dump); 548 549 // interceptors 550 InitTlsSize(); 551 552 // Create main thread. 553 AsanThread *main_thread = AsanThread::Create(0, 0); 554 CreateThreadContextArgs create_main_args = { main_thread, 0 }; 555 u32 main_tid = asanThreadRegistry().CreateThread( 556 0, true, 0, &create_main_args); 557 CHECK_EQ(0, main_tid); 558 SetCurrentThread(main_thread); 559 main_thread->ThreadStart(internal_getpid()); 560 force_interface_symbols(); // no-op. 561 562#if CAN_SANITIZE_LEAKS 563 __lsan::InitCommonLsan(); 564 if (common_flags()->detect_leaks && common_flags()->leak_check_at_exit) { 565 Atexit(__lsan::DoLeakCheck); 566 } 567#endif // CAN_SANITIZE_LEAKS 568 569 if (common_flags()->verbosity) { 570 Report("AddressSanitizer Init done\n"); 571 } 572} 573