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