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