asan_rtl.cc revision 15a77612e0a89c1df444a2034e531c8968d0cedf
1//===-- asan_rtl.cc -------------------------------------------------------===// 2// 3// The LLVM Compiler Infrastructure 4// 5// This file is distributed under the University of Illinois Open Source 6// License. See LICENSE.TXT for details. 7// 8//===----------------------------------------------------------------------===// 9// 10// This file is a part of AddressSanitizer, an address sanity checker. 11// 12// Main file of the ASan run-time library. 13//===----------------------------------------------------------------------===// 14#include "asan_allocator.h" 15#include "asan_interceptors.h" 16#include "asan_interface.h" 17#include "asan_internal.h" 18#include "asan_lock.h" 19#include "asan_mapping.h" 20#include "asan_stack.h" 21#include "asan_stats.h" 22#include "asan_thread.h" 23#include "asan_thread_registry.h" 24#include "sanitizer_common/sanitizer_libc.h" 25 26namespace __sanitizer { 27using namespace __asan; 28 29void Die() { 30 static int num_calls = 0; 31 if (AtomicInc(&num_calls) > 1) { 32 // Don't die twice - run a busy loop. 33 while (1) { } 34 } 35 if (FLAG_sleep_before_dying) { 36 Report("Sleeping for %zd second(s)\n", FLAG_sleep_before_dying); 37 SleepForSeconds(FLAG_sleep_before_dying); 38 } 39 if (FLAG_unmap_shadow_on_exit) 40 AsanUnmapOrDie((void*)kLowShadowBeg, kHighShadowEnd - kLowShadowBeg); 41 if (death_callback) 42 death_callback(); 43 if (FLAG_abort_on_error) 44 Abort(); 45 Exit(FLAG_exitcode); 46} 47 48void CheckFailed(const char *file, int line, const char *cond, u64 v1, u64 v2) { 49 AsanReport("AddressSanitizer CHECK failed: %s:%d \"%s\" (%zx, %zx)\n", 50 file, line, cond, (uptr)v1, (uptr)v2); 51 PRINT_CURRENT_STACK(); 52 ShowStatsAndAbort(); 53} 54 55} // namespace __sanitizer 56 57namespace __asan { 58 59// -------------------------- Flags ------------------------- {{{1 60static const uptr kMallocContextSize = 30; 61 62uptr FLAG_malloc_context_size = kMallocContextSize; 63uptr FLAG_max_malloc_fill_size = 0; 64s64 FLAG_v = 0; 65uptr FLAG_redzone = (ASAN_LOW_MEMORY) ? 64 : 128; // power of two, >= 32 66uptr FLAG_quarantine_size = (ASAN_LOW_MEMORY) ? 1UL << 24 : 1UL << 28; 67static s64 FLAG_atexit = 0; 68bool FLAG_poison_shadow = 1; 69s64 FLAG_report_globals = 1; 70bool FLAG_handle_segv = ASAN_NEEDS_SEGV; 71bool FLAG_use_sigaltstack = 0; 72bool FLAG_symbolize = 0; 73s64 FLAG_demangle = 1; 74s64 FLAG_debug = 0; 75bool FLAG_replace_cfallocator = 1; // Used on Mac only. 76bool FLAG_replace_str = 1; 77bool FLAG_replace_intrin = 1; 78bool FLAG_use_fake_stack = 1; 79s64 FLAG_exitcode = ASAN_DEFAULT_FAILURE_EXITCODE; 80bool FLAG_allow_user_poisoning = 1; 81s64 FLAG_sleep_before_dying = 0; 82bool FLAG_abort_on_error = 0; 83bool FLAG_unmap_shadow_on_exit = 0; 84bool FLAG_disable_core = __WORDSIZE == 64; 85bool FLAG_check_malloc_usable_size = 1; 86 87// -------------------------- Globals --------------------- {{{1 88int asan_inited; 89bool asan_init_is_running; 90void (*death_callback)(void); 91static void (*error_report_callback)(const char*); 92char *error_message_buffer = 0; 93uptr error_message_buffer_pos = 0; 94uptr error_message_buffer_size = 0; 95 96// -------------------------- Misc ---------------- {{{1 97void ShowStatsAndAbort() { 98 __asan_print_accumulated_stats(); 99 Die(); 100} 101 102static void PrintBytes(const char *before, uptr *a) { 103 u8 *bytes = (u8*)a; 104 uptr byte_num = (__WORDSIZE) / 8; 105 AsanPrintf("%s%p:", before, (void*)a); 106 for (uptr i = 0; i < byte_num; i++) { 107 AsanPrintf(" %x%x", bytes[i] >> 4, bytes[i] & 15); 108 } 109 AsanPrintf("\n"); 110} 111 112uptr ReadFileToBuffer(const char *file_name, char **buff, 113 uptr *buff_size, uptr max_len) { 114 const uptr kMinFileLen = kPageSize; 115 uptr read_len = 0; 116 *buff = 0; 117 *buff_size = 0; 118 // The files we usually open are not seekable, so try different buffer sizes. 119 for (uptr size = kMinFileLen; size <= max_len; size *= 2) { 120 fd_t fd = internal_open(file_name, /*write*/ false); 121 if (fd < 0) return 0; 122 AsanUnmapOrDie(*buff, *buff_size); 123 *buff = (char*)AsanMmapSomewhereOrDie(size, __FUNCTION__); 124 *buff_size = size; 125 // Read up to one page at a time. 126 read_len = 0; 127 bool reached_eof = false; 128 while (read_len + kPageSize <= size) { 129 uptr just_read = internal_read(fd, *buff + read_len, kPageSize); 130 if (just_read == 0) { 131 reached_eof = true; 132 break; 133 } 134 read_len += just_read; 135 } 136 internal_close(fd); 137 if (reached_eof) // We've read the whole file. 138 break; 139 } 140 return read_len; 141} 142 143void AppendToErrorMessageBuffer(const char *buffer) { 144 if (error_message_buffer) { 145 uptr length = (uptr)internal_strlen(buffer); 146 int remaining = error_message_buffer_size - error_message_buffer_pos; 147 internal_strncpy(error_message_buffer + error_message_buffer_pos, 148 buffer, remaining); 149 error_message_buffer[error_message_buffer_size - 1] = '\0'; 150 // FIXME: reallocate the buffer instead of truncating the message. 151 error_message_buffer_pos += remaining > length ? length : remaining; 152 } 153} 154 155// ---------------------- mmap -------------------- {{{1 156void OutOfMemoryMessageAndDie(const char *mem_type, uptr size) { 157 AsanReport("ERROR: AddressSanitizer failed to allocate " 158 "0x%zx (%zd) bytes of %s\n", 159 size, size, mem_type); 160 PRINT_CURRENT_STACK(); 161 ShowStatsAndAbort(); 162} 163 164// Reserve memory range [beg, end]. 165static void ReserveShadowMemoryRange(uptr beg, uptr end) { 166 CHECK((beg % kPageSize) == 0); 167 CHECK(((end + 1) % kPageSize) == 0); 168 uptr size = end - beg + 1; 169 void *res = AsanMmapFixedNoReserve(beg, size); 170 CHECK(res == (void*)beg && "ReserveShadowMemoryRange failed"); 171} 172 173// ---------------------- LowLevelAllocator ------------- {{{1 174void *LowLevelAllocator::Allocate(uptr size) { 175 CHECK((size & (size - 1)) == 0 && "size must be a power of two"); 176 if (allocated_end_ - allocated_current_ < size) { 177 uptr size_to_allocate = Max(size, kPageSize); 178 allocated_current_ = 179 (char*)AsanMmapSomewhereOrDie(size_to_allocate, __FUNCTION__); 180 allocated_end_ = allocated_current_ + size_to_allocate; 181 PoisonShadow((uptr)allocated_current_, size_to_allocate, 182 kAsanInternalHeapMagic); 183 } 184 CHECK(allocated_end_ - allocated_current_ >= size); 185 void *res = allocated_current_; 186 allocated_current_ += size; 187 return res; 188} 189 190// ---------------------- DescribeAddress -------------------- {{{1 191static bool DescribeStackAddress(uptr addr, uptr access_size) { 192 AsanThread *t = asanThreadRegistry().FindThreadByStackAddress(addr); 193 if (!t) return false; 194 const sptr kBufSize = 4095; 195 char buf[kBufSize]; 196 uptr offset = 0; 197 const char *frame_descr = t->GetFrameNameByAddr(addr, &offset); 198 // This string is created by the compiler and has the following form: 199 // "FunctioName n alloc_1 alloc_2 ... alloc_n" 200 // where alloc_i looks like "offset size len ObjectName ". 201 CHECK(frame_descr); 202 // Report the function name and the offset. 203 const char *name_end = internal_strchr(frame_descr, ' '); 204 CHECK(name_end); 205 buf[0] = 0; 206 internal_strncat(buf, frame_descr, 207 Min(kBufSize, 208 static_cast<sptr>(name_end - frame_descr))); 209 AsanPrintf("Address %p is located at offset %zu " 210 "in frame <%s> of T%d's stack:\n", 211 (void*)addr, offset, buf, t->tid()); 212 // Report the number of stack objects. 213 char *p; 214 uptr n_objects = internal_simple_strtoll(name_end, &p, 10); 215 CHECK(n_objects > 0); 216 AsanPrintf(" This frame has %zu object(s):\n", n_objects); 217 // Report all objects in this frame. 218 for (uptr i = 0; i < n_objects; i++) { 219 uptr beg, size; 220 sptr len; 221 beg = internal_simple_strtoll(p, &p, 10); 222 size = internal_simple_strtoll(p, &p, 10); 223 len = internal_simple_strtoll(p, &p, 10); 224 if (beg <= 0 || size <= 0 || len < 0 || *p != ' ') { 225 AsanPrintf("AddressSanitizer can't parse the stack frame " 226 "descriptor: |%s|\n", frame_descr); 227 break; 228 } 229 p++; 230 buf[0] = 0; 231 internal_strncat(buf, p, Min(kBufSize, len)); 232 p += len; 233 AsanPrintf(" [%zu, %zu) '%s'\n", beg, beg + size, buf); 234 } 235 AsanPrintf("HINT: this may be a false positive if your program uses " 236 "some custom stack unwind mechanism\n" 237 " (longjmp and C++ exceptions *are* supported)\n"); 238 t->summary()->Announce(); 239 return true; 240} 241 242static NOINLINE void DescribeAddress(uptr addr, uptr access_size) { 243 // Check if this is a global. 244 if (DescribeAddrIfGlobal(addr)) 245 return; 246 247 if (DescribeStackAddress(addr, access_size)) 248 return; 249 250 // finally, check if this is a heap. 251 DescribeHeapAddress(addr, access_size); 252} 253 254// -------------------------- Run-time entry ------------------- {{{1 255// exported functions 256#define ASAN_REPORT_ERROR(type, is_write, size) \ 257extern "C" NOINLINE INTERFACE_ATTRIBUTE \ 258void __asan_report_ ## type ## size(uptr addr); \ 259void __asan_report_ ## type ## size(uptr addr) { \ 260 GET_CALLER_PC_BP_SP; \ 261 __asan_report_error(pc, bp, sp, addr, is_write, size); \ 262} 263 264ASAN_REPORT_ERROR(load, false, 1) 265ASAN_REPORT_ERROR(load, false, 2) 266ASAN_REPORT_ERROR(load, false, 4) 267ASAN_REPORT_ERROR(load, false, 8) 268ASAN_REPORT_ERROR(load, false, 16) 269ASAN_REPORT_ERROR(store, true, 1) 270ASAN_REPORT_ERROR(store, true, 2) 271ASAN_REPORT_ERROR(store, true, 4) 272ASAN_REPORT_ERROR(store, true, 8) 273ASAN_REPORT_ERROR(store, true, 16) 274 275// Force the linker to keep the symbols for various ASan interface functions. 276// We want to keep those in the executable in order to let the instrumented 277// dynamic libraries access the symbol even if it is not used by the executable 278// itself. This should help if the build system is removing dead code at link 279// time. 280static NOINLINE void force_interface_symbols() { 281 volatile int fake_condition = 0; // prevent dead condition elimination. 282 if (fake_condition) { 283 __asan_report_load1(0); 284 __asan_report_load2(0); 285 __asan_report_load4(0); 286 __asan_report_load8(0); 287 __asan_report_load16(0); 288 __asan_report_store1(0); 289 __asan_report_store2(0); 290 __asan_report_store4(0); 291 __asan_report_store8(0); 292 __asan_report_store16(0); 293 __asan_register_global(0, 0, 0); 294 __asan_register_globals(0, 0); 295 __asan_unregister_globals(0, 0); 296 __asan_set_death_callback(0); 297 __asan_set_error_report_callback(0); 298 __asan_handle_no_return(); 299 } 300} 301 302// -------------------------- Init ------------------- {{{1 303static void IntFlagValue(const char *flags, const char *flag, 304 s64 *out_val) { 305 if (!flags) return; 306 const char *str = internal_strstr(flags, flag); 307 if (!str) return; 308 *out_val = internal_atoll(str + internal_strlen(flag)); 309} 310 311static void BoolFlagValue(const char *flags, const char *flag, 312 bool *out_val) { 313 if (!flags) return; 314 const char *str = internal_strstr(flags, flag); 315 if (!str) return; 316 const char *suffix = str + internal_strlen(flag); 317 if (!internal_atoll(str + internal_strlen(flag))) { 318 if (suffix[0] == '0') { 319 *out_val = false; 320 return; 321 } 322 } else { 323 *out_val = true; 324 return; 325 } 326 switch (suffix[0]) { 327 case 'y': 328 case 't': { 329 *out_val = true; 330 break; 331 } 332 case 'n': 333 case 'f': { 334 *out_val = false; 335 break; 336 } 337 default: return; 338 } 339} 340 341static void asan_atexit() { 342 AsanPrintf("AddressSanitizer exit stats:\n"); 343 __asan_print_accumulated_stats(); 344} 345 346} // namespace __asan 347 348// ---------------------- Interface ---------------- {{{1 349using namespace __asan; // NOLINT 350 351int __asan_set_error_exit_code(int exit_code) { 352 int old = FLAG_exitcode; 353 FLAG_exitcode = exit_code; 354 return old; 355} 356 357void NOINLINE __asan_handle_no_return() { 358 int local_stack; 359 AsanThread *curr_thread = asanThreadRegistry().GetCurrent(); 360 CHECK(curr_thread); 361 uptr top = curr_thread->stack_top(); 362 uptr bottom = ((uptr)&local_stack - kPageSize) & ~(kPageSize-1); 363 PoisonShadow(bottom, top - bottom, 0); 364} 365 366void NOINLINE __asan_set_death_callback(void (*callback)(void)) { 367 death_callback = callback; 368} 369 370void NOINLINE __asan_set_error_report_callback(void (*callback)(const char*)) { 371 error_report_callback = callback; 372 if (callback) { 373 error_message_buffer_size = 1 << 16; 374 error_message_buffer = 375 (char*)AsanMmapSomewhereOrDie(error_message_buffer_size, __FUNCTION__); 376 error_message_buffer_pos = 0; 377 } 378} 379 380void __asan_report_error(uptr pc, uptr bp, uptr sp, 381 uptr addr, bool is_write, uptr access_size) { 382 // Do not print more than one report, otherwise they will mix up. 383 static int num_calls = 0; 384 if (AtomicInc(&num_calls) > 1) return; 385 386 AsanPrintf("====================================================" 387 "=============\n"); 388 const char *bug_descr = "unknown-crash"; 389 if (AddrIsInMem(addr)) { 390 u8 *shadow_addr = (u8*)MemToShadow(addr); 391 // If we are accessing 16 bytes, look at the second shadow byte. 392 if (*shadow_addr == 0 && access_size > SHADOW_GRANULARITY) 393 shadow_addr++; 394 // If we are in the partial right redzone, look at the next shadow byte. 395 if (*shadow_addr > 0 && *shadow_addr < 128) 396 shadow_addr++; 397 switch (*shadow_addr) { 398 case kAsanHeapLeftRedzoneMagic: 399 case kAsanHeapRightRedzoneMagic: 400 bug_descr = "heap-buffer-overflow"; 401 break; 402 case kAsanHeapFreeMagic: 403 bug_descr = "heap-use-after-free"; 404 break; 405 case kAsanStackLeftRedzoneMagic: 406 bug_descr = "stack-buffer-underflow"; 407 break; 408 case kAsanStackMidRedzoneMagic: 409 case kAsanStackRightRedzoneMagic: 410 case kAsanStackPartialRedzoneMagic: 411 bug_descr = "stack-buffer-overflow"; 412 break; 413 case kAsanStackAfterReturnMagic: 414 bug_descr = "stack-use-after-return"; 415 break; 416 case kAsanUserPoisonedMemoryMagic: 417 bug_descr = "use-after-poison"; 418 break; 419 case kAsanGlobalRedzoneMagic: 420 bug_descr = "global-buffer-overflow"; 421 break; 422 } 423 } 424 425 AsanThread *curr_thread = asanThreadRegistry().GetCurrent(); 426 u32 curr_tid = asanThreadRegistry().GetCurrentTidOrInvalid(); 427 428 if (curr_thread) { 429 // We started reporting an error message. Stop using the fake stack 430 // in case we will call an instrumented function from a symbolizer. 431 curr_thread->fake_stack().StopUsingFakeStack(); 432 } 433 434 AsanReport("ERROR: AddressSanitizer %s on address " 435 "%p at pc 0x%zx bp 0x%zx sp 0x%zx\n", 436 bug_descr, (void*)addr, pc, bp, sp); 437 438 AsanPrintf("%s of size %zu at %p thread T%d\n", 439 access_size ? (is_write ? "WRITE" : "READ") : "ACCESS", 440 access_size, (void*)addr, curr_tid); 441 442 if (FLAG_debug) { 443 PrintBytes("PC: ", (uptr*)pc); 444 } 445 446 GET_STACK_TRACE_WITH_PC_AND_BP(kStackTraceMax, pc, bp); 447 stack.PrintStack(); 448 449 CHECK(AddrIsInMem(addr)); 450 451 DescribeAddress(addr, access_size); 452 453 uptr shadow_addr = MemToShadow(addr); 454 AsanReport("ABORTING\n"); 455 __asan_print_accumulated_stats(); 456 AsanPrintf("Shadow byte and word:\n"); 457 AsanPrintf(" %p: %x\n", (void*)shadow_addr, *(unsigned char*)shadow_addr); 458 uptr aligned_shadow = shadow_addr & ~(kWordSize - 1); 459 PrintBytes(" ", (uptr*)(aligned_shadow)); 460 AsanPrintf("More shadow bytes:\n"); 461 PrintBytes(" ", (uptr*)(aligned_shadow-4*kWordSize)); 462 PrintBytes(" ", (uptr*)(aligned_shadow-3*kWordSize)); 463 PrintBytes(" ", (uptr*)(aligned_shadow-2*kWordSize)); 464 PrintBytes(" ", (uptr*)(aligned_shadow-1*kWordSize)); 465 PrintBytes("=>", (uptr*)(aligned_shadow+0*kWordSize)); 466 PrintBytes(" ", (uptr*)(aligned_shadow+1*kWordSize)); 467 PrintBytes(" ", (uptr*)(aligned_shadow+2*kWordSize)); 468 PrintBytes(" ", (uptr*)(aligned_shadow+3*kWordSize)); 469 PrintBytes(" ", (uptr*)(aligned_shadow+4*kWordSize)); 470 if (error_report_callback) { 471 error_report_callback(error_message_buffer); 472 } 473 Die(); 474} 475 476static void ParseAsanOptions(const char *options) { 477 IntFlagValue(options, "malloc_context_size=", 478 (s64*)&FLAG_malloc_context_size); 479 CHECK(FLAG_malloc_context_size <= kMallocContextSize); 480 481 IntFlagValue(options, "max_malloc_fill_size=", 482 (s64*)&FLAG_max_malloc_fill_size); 483 484 IntFlagValue(options, "verbosity=", &FLAG_v); 485 486 IntFlagValue(options, "redzone=", (s64*)&FLAG_redzone); 487 CHECK(FLAG_redzone >= 32); 488 CHECK((FLAG_redzone & (FLAG_redzone - 1)) == 0); 489 IntFlagValue(options, "quarantine_size=", (s64*)&FLAG_quarantine_size); 490 491 IntFlagValue(options, "atexit=", &FLAG_atexit); 492 BoolFlagValue(options, "poison_shadow=", &FLAG_poison_shadow); 493 IntFlagValue(options, "report_globals=", &FLAG_report_globals); 494 BoolFlagValue(options, "handle_segv=", &FLAG_handle_segv); 495 BoolFlagValue(options, "use_sigaltstack=", &FLAG_use_sigaltstack); 496 BoolFlagValue(options, "symbolize=", &FLAG_symbolize); 497 IntFlagValue(options, "demangle=", &FLAG_demangle); 498 IntFlagValue(options, "debug=", &FLAG_debug); 499 BoolFlagValue(options, "replace_cfallocator=", &FLAG_replace_cfallocator); 500 BoolFlagValue(options, "replace_str=", &FLAG_replace_str); 501 BoolFlagValue(options, "replace_intrin=", &FLAG_replace_intrin); 502 BoolFlagValue(options, "use_fake_stack=", &FLAG_use_fake_stack); 503 IntFlagValue(options, "exitcode=", &FLAG_exitcode); 504 BoolFlagValue(options, "allow_user_poisoning=", &FLAG_allow_user_poisoning); 505 IntFlagValue(options, "sleep_before_dying=", &FLAG_sleep_before_dying); 506 BoolFlagValue(options, "abort_on_error=", &FLAG_abort_on_error); 507 BoolFlagValue(options, "unmap_shadow_on_exit=", &FLAG_unmap_shadow_on_exit); 508 // By default, disable core dumper on 64-bit -- 509 // it makes little sense to dump 16T+ core. 510 BoolFlagValue(options, "disable_core=", &FLAG_disable_core); 511 512 // Allow the users to work around the bug in Nvidia drivers prior to 295.*. 513 BoolFlagValue(options, "check_malloc_usable_size=", 514 &FLAG_check_malloc_usable_size); 515} 516 517void __asan_init() { 518 if (asan_inited) return; 519 MiniLibcStub(); // FIXME: remove me once mini libc build is tested properly. 520 asan_init_is_running = true; 521 522 // Make sure we are not statically linked. 523 AsanDoesNotSupportStaticLinkage(); 524 525#if !defined(_WIN32) 526 if (__asan_default_options) { 527 ParseAsanOptions(__asan_default_options); 528 if (FLAG_v) { 529 Report("Using the defaults from __asan_default_options: %s\n", 530 __asan_default_options); 531 } 532 } 533#endif 534 // flags 535 const char *options = AsanGetEnv("ASAN_OPTIONS"); 536 ParseAsanOptions(options); 537 538 if (FLAG_v && options) { 539 Report("Parsed ASAN_OPTIONS: %s\n", options); 540 } 541 542 if (FLAG_atexit) { 543 Atexit(asan_atexit); 544 } 545 546 // interceptors 547 InitializeAsanInterceptors(); 548 549 ReplaceSystemMalloc(); 550 ReplaceOperatorsNewAndDelete(); 551 552 if (FLAG_v) { 553 Printf("|| `[%p, %p]` || HighMem ||\n", 554 (void*)kHighMemBeg, (void*)kHighMemEnd); 555 Printf("|| `[%p, %p]` || HighShadow ||\n", 556 (void*)kHighShadowBeg, (void*)kHighShadowEnd); 557 Printf("|| `[%p, %p]` || ShadowGap ||\n", 558 (void*)kShadowGapBeg, (void*)kShadowGapEnd); 559 Printf("|| `[%p, %p]` || LowShadow ||\n", 560 (void*)kLowShadowBeg, (void*)kLowShadowEnd); 561 Printf("|| `[%p, %p]` || LowMem ||\n", 562 (void*)kLowMemBeg, (void*)kLowMemEnd); 563 Printf("MemToShadow(shadow): %p %p %p %p\n", 564 (void*)MEM_TO_SHADOW(kLowShadowBeg), 565 (void*)MEM_TO_SHADOW(kLowShadowEnd), 566 (void*)MEM_TO_SHADOW(kHighShadowBeg), 567 (void*)MEM_TO_SHADOW(kHighShadowEnd)); 568 Printf("red_zone=%zu\n", (uptr)FLAG_redzone); 569 Printf("malloc_context_size=%zu\n", (uptr)FLAG_malloc_context_size); 570 571 Printf("SHADOW_SCALE: %zx\n", (uptr)SHADOW_SCALE); 572 Printf("SHADOW_GRANULARITY: %zx\n", (uptr)SHADOW_GRANULARITY); 573 Printf("SHADOW_OFFSET: %zx\n", (uptr)SHADOW_OFFSET); 574 CHECK(SHADOW_SCALE >= 3 && SHADOW_SCALE <= 7); 575 } 576 577 if (FLAG_disable_core) { 578 AsanDisableCoreDumper(); 579 } 580 581 if (AsanShadowRangeIsAvailable()) { 582 if (kLowShadowBeg != kLowShadowEnd) { 583 // mmap the low shadow plus at least one page. 584 ReserveShadowMemoryRange(kLowShadowBeg - kMmapGranularity, kLowShadowEnd); 585 } 586 // mmap the high shadow. 587 ReserveShadowMemoryRange(kHighShadowBeg, kHighShadowEnd); 588 // protect the gap 589 void *prot = AsanMprotect(kShadowGapBeg, kShadowGapEnd - kShadowGapBeg + 1); 590 CHECK(prot == (void*)kShadowGapBeg); 591 } else { 592 Report("Shadow memory range interleaves with an existing memory mapping. " 593 "ASan cannot proceed correctly. ABORTING.\n"); 594 AsanDumpProcessMap(); 595 Die(); 596 } 597 598 InstallSignalHandlers(); 599 600 // On Linux AsanThread::ThreadStart() calls malloc() that's why asan_inited 601 // should be set to 1 prior to initializing the threads. 602 asan_inited = 1; 603 asan_init_is_running = false; 604 605 asanThreadRegistry().Init(); 606 asanThreadRegistry().GetMain()->ThreadStart(); 607 force_interface_symbols(); // no-op. 608 609 if (FLAG_v) { 610 Report("AddressSanitizer Init done\n"); 611 } 612} 613 614#if defined(ASAN_USE_PREINIT_ARRAY) 615 // On Linux, we force __asan_init to be called before anyone else 616 // by placing it into .preinit_array section. 617 // FIXME: do we have anything like this on Mac? 618 __attribute__((section(".preinit_array"))) 619 typeof(__asan_init) *__asan_preinit =__asan_init; 620#elif defined(_WIN32) && defined(_DLL) 621 // On Windows, when using dynamic CRT (/MD), we can put a pointer 622 // to __asan_init into the global list of C initializers. 623 // See crt0dat.c in the CRT sources for the details. 624 #pragma section(".CRT$XIB", long, read) // NOLINT 625 __declspec(allocate(".CRT$XIB")) void (*__asan_preinit)() = __asan_init; 626#endif 627