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