asan_rtl.cc revision b5433dd977c6b528b7e0d83fa1fc45d6503a1226
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_internal.h"
17#include "asan_mapping.h"
18#include "asan_poisoning.h"
19#include "asan_report.h"
20#include "asan_stack.h"
21#include "asan_stats.h"
22#include "asan_thread.h"
23#include "sanitizer_common/sanitizer_atomic.h"
24#include "sanitizer_common/sanitizer_flags.h"
25#include "sanitizer_common/sanitizer_libc.h"
26#include "sanitizer_common/sanitizer_symbolizer.h"
27#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
140  internal_memset(f, 0, sizeof(*f));
141  f->quarantine_size = (ASAN_LOW_MEMORY) ? 1UL << 26 : 1UL << 28;
142  f->verbosity = 0;
143  f->redzone = 16;
144  f->debug = false;
145  f->report_globals = 1;
146  f->check_initialization_order = false;
147  f->replace_str = true;
148  f->replace_intrin = true;
149  f->mac_ignore_invalid_free = false;
150  f->use_fake_stack = true;
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->disable_core = (SANITIZER_WORDSIZE == 64);
166  f->allow_reexec = true;
167  f->print_full_thread_history = true;
168  f->log_path = 0;
169  f->poison_heap = true;
170  // Turn off alloc/dealloc mismatch checker on Mac for now.
171  // TODO(glider): Fix known issues and enable this back.
172  f->alloc_dealloc_mismatch = (SANITIZER_MAC == 0);;
173  f->use_stack_depot = true;
174  f->strict_memcmp = true;
175  f->strict_init_order = false;
176  f->detect_leaks = false;
177
178  // Override from compile definition.
179  ParseFlagsFromString(f, MaybeUseAsanDefaultOptionsCompileDefiniton());
180
181  // Override from user-specified string.
182  ParseFlagsFromString(f, MaybeCallAsanDefaultOptions());
183  if (flags()->verbosity) {
184    Report("Using the defaults from __asan_default_options: %s\n",
185           MaybeCallAsanDefaultOptions());
186  }
187
188  // Override from command line.
189  ParseFlagsFromString(f, env);
190
191#if !CAN_SANITIZE_LEAKS
192  if (f->detect_leaks) {
193    Report("%s: detect_leaks is not supported on this platform.\n",
194           SanitizerToolName);
195    f->detect_leaks = false;
196  }
197#endif
198
199  if (f->detect_leaks && !f->use_stack_depot) {
200    Report("%s: detect_leaks is ignored (requires use_stack_depot).\n",
201           SanitizerToolName);
202    f->detect_leaks = false;
203  }
204}
205
206// -------------------------- Globals --------------------- {{{1
207int asan_inited;
208bool asan_init_is_running;
209void (*death_callback)(void);
210
211#if !ASAN_FIXED_MAPPING
212uptr kHighMemEnd, kMidMemBeg, kMidMemEnd;
213#endif
214
215// -------------------------- Misc ---------------- {{{1
216void ShowStatsAndAbort() {
217  __asan_print_accumulated_stats();
218  Die();
219}
220
221// ---------------------- mmap -------------------- {{{1
222// Reserve memory range [beg, end].
223static void ReserveShadowMemoryRange(uptr beg, uptr end) {
224  CHECK_EQ((beg % GetPageSizeCached()), 0);
225  CHECK_EQ(((end + 1) % GetPageSizeCached()), 0);
226  uptr size = end - beg + 1;
227  void *res = MmapFixedNoReserve(beg, size);
228  if (res != (void*)beg) {
229    Report("ReserveShadowMemoryRange failed while trying to map 0x%zx bytes. "
230           "Perhaps you're using ulimit -v\n", size);
231    Abort();
232  }
233}
234
235// --------------- LowLevelAllocateCallbac ---------- {{{1
236static void OnLowLevelAllocate(uptr ptr, uptr size) {
237  PoisonShadow(ptr, size, kAsanInternalHeapMagic);
238}
239
240// -------------------------- Run-time entry ------------------- {{{1
241// exported functions
242#define ASAN_REPORT_ERROR(type, is_write, size)                     \
243extern "C" NOINLINE INTERFACE_ATTRIBUTE                        \
244void __asan_report_ ## type ## size(uptr addr);                \
245void __asan_report_ ## type ## size(uptr addr) {               \
246  GET_CALLER_PC_BP_SP;                                              \
247  __asan_report_error(pc, bp, sp, addr, is_write, size);            \
248}
249
250ASAN_REPORT_ERROR(load, false, 1)
251ASAN_REPORT_ERROR(load, false, 2)
252ASAN_REPORT_ERROR(load, false, 4)
253ASAN_REPORT_ERROR(load, false, 8)
254ASAN_REPORT_ERROR(load, false, 16)
255ASAN_REPORT_ERROR(store, true, 1)
256ASAN_REPORT_ERROR(store, true, 2)
257ASAN_REPORT_ERROR(store, true, 4)
258ASAN_REPORT_ERROR(store, true, 8)
259ASAN_REPORT_ERROR(store, true, 16)
260
261#define ASAN_REPORT_ERROR_N(type, is_write)                    \
262extern "C" NOINLINE INTERFACE_ATTRIBUTE                        \
263void __asan_report_ ## type ## _n(uptr addr, uptr size);       \
264void __asan_report_ ## type ## _n(uptr addr, uptr size) {      \
265  GET_CALLER_PC_BP_SP;                                         \
266  __asan_report_error(pc, bp, sp, addr, is_write, size);       \
267}
268
269ASAN_REPORT_ERROR_N(load, false)
270ASAN_REPORT_ERROR_N(store, true)
271
272// Force the linker to keep the symbols for various ASan interface functions.
273// We want to keep those in the executable in order to let the instrumented
274// dynamic libraries access the symbol even if it is not used by the executable
275// itself. This should help if the build system is removing dead code at link
276// time.
277static NOINLINE void force_interface_symbols() {
278  volatile int fake_condition = 0;  // prevent dead condition elimination.
279  // __asan_report_* functions are noreturn, so we need a switch to prevent
280  // the compiler from removing any of them.
281  switch (fake_condition) {
282    case 1: __asan_report_load1(0); break;
283    case 2: __asan_report_load2(0); break;
284    case 3: __asan_report_load4(0); break;
285    case 4: __asan_report_load8(0); break;
286    case 5: __asan_report_load16(0); break;
287    case 6: __asan_report_store1(0); break;
288    case 7: __asan_report_store2(0); break;
289    case 8: __asan_report_store4(0); break;
290    case 9: __asan_report_store8(0); break;
291    case 10: __asan_report_store16(0); break;
292    case 12: __asan_register_globals(0, 0); break;
293    case 13: __asan_unregister_globals(0, 0); break;
294    case 14: __asan_set_death_callback(0); break;
295    case 15: __asan_set_error_report_callback(0); break;
296    case 16: __asan_handle_no_return(); break;
297    case 17: __asan_address_is_poisoned(0); break;
298    case 18: __asan_get_allocated_size(0); break;
299    case 19: __asan_get_current_allocated_bytes(); break;
300    case 20: __asan_get_estimated_allocated_size(0); break;
301    case 21: __asan_get_free_bytes(); break;
302    case 22: __asan_get_heap_size(); break;
303    case 23: __asan_get_ownership(0); break;
304    case 24: __asan_get_unmapped_bytes(); break;
305    case 25: __asan_poison_memory_region(0, 0); break;
306    case 26: __asan_unpoison_memory_region(0, 0); break;
307    case 27: __asan_set_error_exit_code(0); break;
308    case 28: __asan_stack_free(0, 0, 0); break;
309    case 29: __asan_stack_malloc(0, 0); break;
310    case 30: __asan_before_dynamic_init(0); break;
311    case 31: __asan_after_dynamic_init(); break;
312    case 32: __asan_poison_stack_memory(0, 0); break;
313    case 33: __asan_unpoison_stack_memory(0, 0); break;
314    case 34: __asan_region_is_poisoned(0, 0); break;
315    case 35: __asan_describe_address(0); break;
316  }
317}
318
319static void asan_atexit() {
320  Printf("AddressSanitizer exit stats:\n");
321  __asan_print_accumulated_stats();
322  // Print AsanMappingProfile.
323  for (uptr i = 0; i < kAsanMappingProfileSize; i++) {
324    if (AsanMappingProfile[i] == 0) continue;
325    Printf("asan_mapping.h:%zd -- %zd\n", i, AsanMappingProfile[i]);
326  }
327}
328
329static void InitializeHighMemEnd() {
330#if !ASAN_FIXED_MAPPING
331#if SANITIZER_WORDSIZE == 64
332# if defined(__powerpc64__)
333  // FIXME:
334  // On PowerPC64 we have two different address space layouts: 44- and 46-bit.
335  // We somehow need to figure our which one we are using now and choose
336  // one of 0x00000fffffffffffUL and 0x00003fffffffffffUL.
337  // Note that with 'ulimit -s unlimited' the stack is moved away from the top
338  // of the address space, so simply checking the stack address is not enough.
339  kHighMemEnd = (1ULL << 44) - 1;  // 0x00000fffffffffffUL
340# else
341  kHighMemEnd = (1ULL << 47) - 1;  // 0x00007fffffffffffUL;
342# endif
343#else  // SANITIZER_WORDSIZE == 32
344  kHighMemEnd = (1ULL << 32) - 1;  // 0xffffffff;
345#endif  // SANITIZER_WORDSIZE
346#endif  // !ASAN_FIXED_MAPPING
347}
348
349static void ProtectGap(uptr a, uptr size) {
350  CHECK_EQ(a, (uptr)Mprotect(a, size));
351}
352
353static void PrintAddressSpaceLayout() {
354  Printf("|| `[%p, %p]` || HighMem    ||\n",
355         (void*)kHighMemBeg, (void*)kHighMemEnd);
356  Printf("|| `[%p, %p]` || HighShadow ||\n",
357         (void*)kHighShadowBeg, (void*)kHighShadowEnd);
358  if (kMidMemBeg) {
359    Printf("|| `[%p, %p]` || ShadowGap3 ||\n",
360           (void*)kShadowGap3Beg, (void*)kShadowGap3End);
361    Printf("|| `[%p, %p]` || MidMem     ||\n",
362           (void*)kMidMemBeg, (void*)kMidMemEnd);
363    Printf("|| `[%p, %p]` || ShadowGap2 ||\n",
364           (void*)kShadowGap2Beg, (void*)kShadowGap2End);
365    Printf("|| `[%p, %p]` || MidShadow  ||\n",
366           (void*)kMidShadowBeg, (void*)kMidShadowEnd);
367  }
368  Printf("|| `[%p, %p]` || ShadowGap  ||\n",
369         (void*)kShadowGapBeg, (void*)kShadowGapEnd);
370  if (kLowShadowBeg) {
371    Printf("|| `[%p, %p]` || LowShadow  ||\n",
372           (void*)kLowShadowBeg, (void*)kLowShadowEnd);
373    Printf("|| `[%p, %p]` || LowMem     ||\n",
374           (void*)kLowMemBeg, (void*)kLowMemEnd);
375  }
376  Printf("MemToShadow(shadow): %p %p %p %p",
377         (void*)MEM_TO_SHADOW(kLowShadowBeg),
378         (void*)MEM_TO_SHADOW(kLowShadowEnd),
379         (void*)MEM_TO_SHADOW(kHighShadowBeg),
380         (void*)MEM_TO_SHADOW(kHighShadowEnd));
381  if (kMidMemBeg) {
382    Printf(" %p %p",
383           (void*)MEM_TO_SHADOW(kMidShadowBeg),
384           (void*)MEM_TO_SHADOW(kMidShadowEnd));
385  }
386  Printf("\n");
387  Printf("red_zone=%zu\n", (uptr)flags()->redzone);
388  Printf("malloc_context_size=%zu\n",
389         (uptr)common_flags()->malloc_context_size);
390
391  Printf("SHADOW_SCALE: %zx\n", (uptr)SHADOW_SCALE);
392  Printf("SHADOW_GRANULARITY: %zx\n", (uptr)SHADOW_GRANULARITY);
393  Printf("SHADOW_OFFSET: %zx\n", (uptr)SHADOW_OFFSET);
394  CHECK(SHADOW_SCALE >= 3 && SHADOW_SCALE <= 7);
395  if (kMidMemBeg)
396    CHECK(kMidShadowBeg > kLowShadowEnd &&
397          kMidMemBeg > kMidShadowEnd &&
398          kHighShadowBeg > kMidMemEnd);
399}
400
401}  // namespace __asan
402
403// ---------------------- Interface ---------------- {{{1
404using namespace __asan;  // NOLINT
405
406#if !SANITIZER_SUPPORTS_WEAK_HOOKS
407extern "C" {
408SANITIZER_WEAK_ATTRIBUTE SANITIZER_INTERFACE_ATTRIBUTE
409const char* __asan_default_options() { return ""; }
410}  // extern "C"
411#endif
412
413int NOINLINE __asan_set_error_exit_code(int exit_code) {
414  int old = flags()->exitcode;
415  flags()->exitcode = exit_code;
416  return old;
417}
418
419void NOINLINE __asan_handle_no_return() {
420  int local_stack;
421  AsanThread *curr_thread = GetCurrentThread();
422  CHECK(curr_thread);
423  uptr PageSize = GetPageSizeCached();
424  uptr top = curr_thread->stack_top();
425  uptr bottom = ((uptr)&local_stack - PageSize) & ~(PageSize-1);
426  static const uptr kMaxExpectedCleanupSize = 64 << 20;  // 64M
427  if (top - bottom > kMaxExpectedCleanupSize) {
428    static bool reported_warning = false;
429    if (reported_warning)
430      return;
431    reported_warning = true;
432    Report("WARNING: ASan is ignoring requested __asan_handle_no_return: "
433           "stack top: %p; bottom %p; size: %p (%zd)\n"
434           "False positive error reports may follow\n"
435           "For details see "
436           "http://code.google.com/p/address-sanitizer/issues/detail?id=189\n",
437           top, bottom, top - bottom, top - bottom);
438    return;
439  }
440  PoisonShadow(bottom, top - bottom, 0);
441}
442
443void NOINLINE __asan_set_death_callback(void (*callback)(void)) {
444  death_callback = callback;
445}
446
447void __asan_init() {
448  if (asan_inited) return;
449  SanitizerToolName = "AddressSanitizer";
450  CHECK(!asan_init_is_running && "ASan init calls itself!");
451  asan_init_is_running = true;
452  InitializeHighMemEnd();
453
454  // Make sure we are not statically linked.
455  AsanDoesNotSupportStaticLinkage();
456
457  // Install tool-specific callbacks in sanitizer_common.
458  SetDieCallback(AsanDie);
459  SetCheckFailedCallback(AsanCheckFailed);
460  SetPrintfAndReportCallback(AppendToErrorMessageBuffer);
461
462  // Initialize flags. This must be done early, because most of the
463  // initialization steps look at flags().
464  const char *options = GetEnv("ASAN_OPTIONS");
465  InitializeFlags(flags(), options);
466  __sanitizer_set_report_path(flags()->log_path);
467
468  if (flags()->verbosity && options) {
469    Report("Parsed ASAN_OPTIONS: %s\n", options);
470  }
471
472  // Re-exec ourselves if we need to set additional env or command line args.
473  MaybeReexec();
474
475  // Setup internal allocator callback.
476  SetLowLevelAllocateCallback(OnLowLevelAllocate);
477
478  if (flags()->atexit) {
479    Atexit(asan_atexit);
480  }
481
482  // interceptors
483  InitializeAsanInterceptors();
484
485  ReplaceSystemMalloc();
486  ReplaceOperatorsNewAndDelete();
487
488  uptr shadow_start = kLowShadowBeg;
489  if (kLowShadowBeg) shadow_start -= GetMmapGranularity();
490  uptr shadow_end = kHighShadowEnd;
491  bool full_shadow_is_available =
492      MemoryRangeIsAvailable(shadow_start, shadow_end);
493
494#if SANITIZER_LINUX && defined(__x86_64__) && !ASAN_FIXED_MAPPING
495  if (!full_shadow_is_available) {
496    kMidMemBeg = kLowMemEnd < 0x3000000000ULL ? 0x3000000000ULL : 0;
497    kMidMemEnd = kLowMemEnd < 0x3000000000ULL ? 0x4fffffffffULL : 0;
498  }
499#endif
500
501  if (flags()->verbosity)
502    PrintAddressSpaceLayout();
503
504  if (flags()->disable_core) {
505    DisableCoreDumper();
506  }
507
508  if (full_shadow_is_available) {
509    // mmap the low shadow plus at least one page at the left.
510    if (kLowShadowBeg)
511      ReserveShadowMemoryRange(shadow_start, kLowShadowEnd);
512    // mmap the high shadow.
513    ReserveShadowMemoryRange(kHighShadowBeg, kHighShadowEnd);
514    // protect the gap.
515    ProtectGap(kShadowGapBeg, kShadowGapEnd - kShadowGapBeg + 1);
516  } else if (kMidMemBeg &&
517      MemoryRangeIsAvailable(shadow_start, kMidMemBeg - 1) &&
518      MemoryRangeIsAvailable(kMidMemEnd + 1, shadow_end)) {
519    CHECK(kLowShadowBeg != kLowShadowEnd);
520    // mmap the low shadow plus at least one page at the left.
521    ReserveShadowMemoryRange(shadow_start, kLowShadowEnd);
522    // mmap the mid shadow.
523    ReserveShadowMemoryRange(kMidShadowBeg, kMidShadowEnd);
524    // mmap the high shadow.
525    ReserveShadowMemoryRange(kHighShadowBeg, kHighShadowEnd);
526    // protect the gaps.
527    ProtectGap(kShadowGapBeg, kShadowGapEnd - kShadowGapBeg + 1);
528    ProtectGap(kShadowGap2Beg, kShadowGap2End - kShadowGap2Beg + 1);
529    ProtectGap(kShadowGap3Beg, kShadowGap3End - kShadowGap3Beg + 1);
530  } else {
531    Report("Shadow memory range interleaves with an existing memory mapping. "
532           "ASan cannot proceed correctly. ABORTING.\n");
533    DumpProcessMap();
534    Die();
535  }
536
537  InstallSignalHandlers();
538  // Start symbolizer process if necessary.
539  const char* external_symbolizer = common_flags()->external_symbolizer_path;
540  if (common_flags()->symbolize && external_symbolizer &&
541      external_symbolizer[0]) {
542    InitializeExternalSymbolizer(external_symbolizer);
543  }
544
545  // On Linux AsanThread::ThreadStart() calls malloc() that's why asan_inited
546  // should be set to 1 prior to initializing the threads.
547  asan_inited = 1;
548  asan_init_is_running = false;
549
550  // Create main thread.
551  AsanTSDInit(AsanThread::TSDDtor);
552  AsanThread *main_thread = AsanThread::Create(0, 0);
553  CreateThreadContextArgs create_main_args = { main_thread, 0 };
554  u32 main_tid = asanThreadRegistry().CreateThread(
555      0, true, 0, &create_main_args);
556  CHECK_EQ(0, main_tid);
557  SetCurrentThread(main_thread);
558  main_thread->ThreadStart(internal_getpid());
559  force_interface_symbols();  // no-op.
560
561  InitializeAllocator();
562
563#if 0
564  __lsan::InitCommonLsan();
565  if (flags()->detect_leaks) {
566    Atexit(__lsan::DoLeakCheck);
567  }
568#endif
569
570  if (flags()->verbosity) {
571    Report("AddressSanitizer Init done\n");
572  }
573}
574