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