Home | History | Annotate | Line # | Download | only in asan
      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 
     15 #include "asan_activation.h"
     16 #include "asan_allocator.h"
     17 #include "asan_interceptors.h"
     18 #include "asan_interface_internal.h"
     19 #include "asan_internal.h"
     20 #include "asan_mapping.h"
     21 #include "asan_poisoning.h"
     22 #include "asan_report.h"
     23 #include "asan_stack.h"
     24 #include "asan_stats.h"
     25 #include "asan_suppressions.h"
     26 #include "asan_thread.h"
     27 #include "sanitizer_common/sanitizer_atomic.h"
     28 #include "sanitizer_common/sanitizer_flags.h"
     29 #include "sanitizer_common/sanitizer_libc.h"
     30 #include "sanitizer_common/sanitizer_symbolizer.h"
     31 #include "lsan/lsan_common.h"
     32 #include "ubsan/ubsan_init.h"
     33 #include "ubsan/ubsan_platform.h"
     34 
     35 uptr __asan_shadow_memory_dynamic_address;  // Global interface symbol.
     36 int __asan_option_detect_stack_use_after_return;  // Global interface symbol.
     37 uptr *__asan_test_only_reported_buggy_pointer;  // Used only for testing asan.
     38 
     39 namespace __asan {
     40 
     41 uptr AsanMappingProfile[kAsanMappingProfileSize];
     42 
     43 static void AsanDie() {
     44   static atomic_uint32_t num_calls;
     45   if (atomic_fetch_add(&num_calls, 1, memory_order_relaxed) != 0) {
     46     // Don't die twice - run a busy loop.
     47     while (1) { }
     48   }
     49   if (common_flags()->print_module_map >= 1) PrintModuleMap();
     50   if (flags()->sleep_before_dying) {
     51     Report("Sleeping for %d second(s)\n", flags()->sleep_before_dying);
     52     SleepForSeconds(flags()->sleep_before_dying);
     53   }
     54   if (flags()->unmap_shadow_on_exit) {
     55     if (kMidMemBeg) {
     56       UnmapOrDie((void*)kLowShadowBeg, kMidMemBeg - kLowShadowBeg);
     57       UnmapOrDie((void*)kMidMemEnd, kHighShadowEnd - kMidMemEnd);
     58     } else {
     59       if (kHighShadowEnd)
     60         UnmapOrDie((void*)kLowShadowBeg, kHighShadowEnd - kLowShadowBeg);
     61     }
     62   }
     63 }
     64 
     65 static void AsanCheckFailed(const char *file, int line, const char *cond,
     66                             u64 v1, u64 v2) {
     67   Report("AddressSanitizer CHECK failed: %s:%d \"%s\" (0x%zx, 0x%zx)\n", file,
     68          line, cond, (uptr)v1, (uptr)v2);
     69 
     70   // Print a stack trace the first time we come here. Otherwise, we probably
     71   // failed a CHECK during symbolization.
     72   static atomic_uint32_t num_calls;
     73   if (atomic_fetch_add(&num_calls, 1, memory_order_relaxed) == 0) {
     74     PRINT_CURRENT_STACK_CHECK();
     75   }
     76 
     77   Die();
     78 }
     79 
     80 // -------------------------- Globals --------------------- {{{1
     81 int asan_inited;
     82 bool asan_init_is_running;
     83 
     84 #if !ASAN_FIXED_MAPPING
     85 uptr kHighMemEnd, kMidMemBeg, kMidMemEnd;
     86 #endif
     87 
     88 // -------------------------- Misc ---------------- {{{1
     89 void ShowStatsAndAbort() {
     90   __asan_print_accumulated_stats();
     91   Die();
     92 }
     93 
     94 // --------------- LowLevelAllocateCallbac ---------- {{{1
     95 static void OnLowLevelAllocate(uptr ptr, uptr size) {
     96   PoisonShadow(ptr, size, kAsanInternalHeapMagic);
     97 }
     98 
     99 // -------------------------- Run-time entry ------------------- {{{1
    100 // exported functions
    101 #define ASAN_REPORT_ERROR(type, is_write, size)                     \
    102 extern "C" NOINLINE INTERFACE_ATTRIBUTE                             \
    103 void __asan_report_ ## type ## size(uptr addr) {                    \
    104   GET_CALLER_PC_BP_SP;                                              \
    105   ReportGenericError(pc, bp, sp, addr, is_write, size, 0, true);    \
    106 }                                                                   \
    107 extern "C" NOINLINE INTERFACE_ATTRIBUTE                             \
    108 void __asan_report_exp_ ## type ## size(uptr addr, u32 exp) {       \
    109   GET_CALLER_PC_BP_SP;                                              \
    110   ReportGenericError(pc, bp, sp, addr, is_write, size, exp, true);  \
    111 }                                                                   \
    112 extern "C" NOINLINE INTERFACE_ATTRIBUTE                             \
    113 void __asan_report_ ## type ## size ## _noabort(uptr addr) {        \
    114   GET_CALLER_PC_BP_SP;                                              \
    115   ReportGenericError(pc, bp, sp, addr, is_write, size, 0, false);   \
    116 }                                                                   \
    117 
    118 ASAN_REPORT_ERROR(load, false, 1)
    119 ASAN_REPORT_ERROR(load, false, 2)
    120 ASAN_REPORT_ERROR(load, false, 4)
    121 ASAN_REPORT_ERROR(load, false, 8)
    122 ASAN_REPORT_ERROR(load, false, 16)
    123 ASAN_REPORT_ERROR(store, true, 1)
    124 ASAN_REPORT_ERROR(store, true, 2)
    125 ASAN_REPORT_ERROR(store, true, 4)
    126 ASAN_REPORT_ERROR(store, true, 8)
    127 ASAN_REPORT_ERROR(store, true, 16)
    128 
    129 #define ASAN_REPORT_ERROR_N(type, is_write)                                 \
    130 extern "C" NOINLINE INTERFACE_ATTRIBUTE                                     \
    131 void __asan_report_ ## type ## _n(uptr addr, uptr size) {                   \
    132   GET_CALLER_PC_BP_SP;                                                      \
    133   ReportGenericError(pc, bp, sp, addr, is_write, size, 0, true);            \
    134 }                                                                           \
    135 extern "C" NOINLINE INTERFACE_ATTRIBUTE                                     \
    136 void __asan_report_exp_ ## type ## _n(uptr addr, uptr size, u32 exp) {      \
    137   GET_CALLER_PC_BP_SP;                                                      \
    138   ReportGenericError(pc, bp, sp, addr, is_write, size, exp, true);          \
    139 }                                                                           \
    140 extern "C" NOINLINE INTERFACE_ATTRIBUTE                                     \
    141 void __asan_report_ ## type ## _n_noabort(uptr addr, uptr size) {           \
    142   GET_CALLER_PC_BP_SP;                                                      \
    143   ReportGenericError(pc, bp, sp, addr, is_write, size, 0, false);           \
    144 }                                                                           \
    145 
    146 ASAN_REPORT_ERROR_N(load, false)
    147 ASAN_REPORT_ERROR_N(store, true)
    148 
    149 #define ASAN_MEMORY_ACCESS_CALLBACK_BODY(type, is_write, size, exp_arg, fatal) \
    150     if (SANITIZER_MYRIAD2 && !AddrIsInMem(addr) && !AddrIsInShadow(addr))      \
    151       return;                                                                  \
    152     uptr sp = MEM_TO_SHADOW(addr);                                             \
    153     uptr s = size <= SHADOW_GRANULARITY ? *reinterpret_cast<u8 *>(sp)          \
    154                                         : *reinterpret_cast<u16 *>(sp);        \
    155     if (UNLIKELY(s)) {                                                         \
    156       if (UNLIKELY(size >= SHADOW_GRANULARITY ||                               \
    157                    ((s8)((addr & (SHADOW_GRANULARITY - 1)) + size - 1)) >=     \
    158                        (s8)s)) {                                               \
    159         if (__asan_test_only_reported_buggy_pointer) {                         \
    160           *__asan_test_only_reported_buggy_pointer = addr;                     \
    161         } else {                                                               \
    162           GET_CALLER_PC_BP_SP;                                                 \
    163           ReportGenericError(pc, bp, sp, addr, is_write, size, exp_arg,        \
    164                               fatal);                                          \
    165         }                                                                      \
    166       }                                                                        \
    167     }
    168 
    169 #define ASAN_MEMORY_ACCESS_CALLBACK(type, is_write, size)                      \
    170   extern "C" NOINLINE INTERFACE_ATTRIBUTE                                      \
    171   void __asan_##type##size(uptr addr) {                                        \
    172     ASAN_MEMORY_ACCESS_CALLBACK_BODY(type, is_write, size, 0, true)            \
    173   }                                                                            \
    174   extern "C" NOINLINE INTERFACE_ATTRIBUTE                                      \
    175   void __asan_exp_##type##size(uptr addr, u32 exp) {                           \
    176     ASAN_MEMORY_ACCESS_CALLBACK_BODY(type, is_write, size, exp, true)          \
    177   }                                                                            \
    178   extern "C" NOINLINE INTERFACE_ATTRIBUTE                                      \
    179   void __asan_##type##size ## _noabort(uptr addr) {                            \
    180     ASAN_MEMORY_ACCESS_CALLBACK_BODY(type, is_write, size, 0, false)           \
    181   }                                                                            \
    182 
    183 ASAN_MEMORY_ACCESS_CALLBACK(load, false, 1)
    184 ASAN_MEMORY_ACCESS_CALLBACK(load, false, 2)
    185 ASAN_MEMORY_ACCESS_CALLBACK(load, false, 4)
    186 ASAN_MEMORY_ACCESS_CALLBACK(load, false, 8)
    187 ASAN_MEMORY_ACCESS_CALLBACK(load, false, 16)
    188 ASAN_MEMORY_ACCESS_CALLBACK(store, true, 1)
    189 ASAN_MEMORY_ACCESS_CALLBACK(store, true, 2)
    190 ASAN_MEMORY_ACCESS_CALLBACK(store, true, 4)
    191 ASAN_MEMORY_ACCESS_CALLBACK(store, true, 8)
    192 ASAN_MEMORY_ACCESS_CALLBACK(store, true, 16)
    193 
    194 extern "C"
    195 NOINLINE INTERFACE_ATTRIBUTE
    196 void __asan_loadN(uptr addr, uptr size) {
    197   if (__asan_region_is_poisoned(addr, size)) {
    198     GET_CALLER_PC_BP_SP;
    199     ReportGenericError(pc, bp, sp, addr, false, size, 0, true);
    200   }
    201 }
    202 
    203 extern "C"
    204 NOINLINE INTERFACE_ATTRIBUTE
    205 void __asan_exp_loadN(uptr addr, uptr size, u32 exp) {
    206   if (__asan_region_is_poisoned(addr, size)) {
    207     GET_CALLER_PC_BP_SP;
    208     ReportGenericError(pc, bp, sp, addr, false, size, exp, true);
    209   }
    210 }
    211 
    212 extern "C"
    213 NOINLINE INTERFACE_ATTRIBUTE
    214 void __asan_loadN_noabort(uptr addr, uptr size) {
    215   if (__asan_region_is_poisoned(addr, size)) {
    216     GET_CALLER_PC_BP_SP;
    217     ReportGenericError(pc, bp, sp, addr, false, size, 0, false);
    218   }
    219 }
    220 
    221 extern "C"
    222 NOINLINE INTERFACE_ATTRIBUTE
    223 void __asan_storeN(uptr addr, uptr size) {
    224   if (__asan_region_is_poisoned(addr, size)) {
    225     GET_CALLER_PC_BP_SP;
    226     ReportGenericError(pc, bp, sp, addr, true, size, 0, true);
    227   }
    228 }
    229 
    230 extern "C"
    231 NOINLINE INTERFACE_ATTRIBUTE
    232 void __asan_exp_storeN(uptr addr, uptr size, u32 exp) {
    233   if (__asan_region_is_poisoned(addr, size)) {
    234     GET_CALLER_PC_BP_SP;
    235     ReportGenericError(pc, bp, sp, addr, true, size, exp, true);
    236   }
    237 }
    238 
    239 extern "C"
    240 NOINLINE INTERFACE_ATTRIBUTE
    241 void __asan_storeN_noabort(uptr addr, uptr size) {
    242   if (__asan_region_is_poisoned(addr, size)) {
    243     GET_CALLER_PC_BP_SP;
    244     ReportGenericError(pc, bp, sp, addr, true, size, 0, false);
    245   }
    246 }
    247 
    248 // Force the linker to keep the symbols for various ASan interface functions.
    249 // We want to keep those in the executable in order to let the instrumented
    250 // dynamic libraries access the symbol even if it is not used by the executable
    251 // itself. This should help if the build system is removing dead code at link
    252 // time.
    253 static NOINLINE void force_interface_symbols() {
    254   volatile int fake_condition = 0;  // prevent dead condition elimination.
    255   // __asan_report_* functions are noreturn, so we need a switch to prevent
    256   // the compiler from removing any of them.
    257   // clang-format off
    258   switch (fake_condition) {
    259     case 1: __asan_report_load1(0); break;
    260     case 2: __asan_report_load2(0); break;
    261     case 3: __asan_report_load4(0); break;
    262     case 4: __asan_report_load8(0); break;
    263     case 5: __asan_report_load16(0); break;
    264     case 6: __asan_report_load_n(0, 0); break;
    265     case 7: __asan_report_store1(0); break;
    266     case 8: __asan_report_store2(0); break;
    267     case 9: __asan_report_store4(0); break;
    268     case 10: __asan_report_store8(0); break;
    269     case 11: __asan_report_store16(0); break;
    270     case 12: __asan_report_store_n(0, 0); break;
    271     case 13: __asan_report_exp_load1(0, 0); break;
    272     case 14: __asan_report_exp_load2(0, 0); break;
    273     case 15: __asan_report_exp_load4(0, 0); break;
    274     case 16: __asan_report_exp_load8(0, 0); break;
    275     case 17: __asan_report_exp_load16(0, 0); break;
    276     case 18: __asan_report_exp_load_n(0, 0, 0); break;
    277     case 19: __asan_report_exp_store1(0, 0); break;
    278     case 20: __asan_report_exp_store2(0, 0); break;
    279     case 21: __asan_report_exp_store4(0, 0); break;
    280     case 22: __asan_report_exp_store8(0, 0); break;
    281     case 23: __asan_report_exp_store16(0, 0); break;
    282     case 24: __asan_report_exp_store_n(0, 0, 0); break;
    283     case 25: __asan_register_globals(nullptr, 0); break;
    284     case 26: __asan_unregister_globals(nullptr, 0); break;
    285     case 27: __asan_set_death_callback(nullptr); break;
    286     case 28: __asan_set_error_report_callback(nullptr); break;
    287     case 29: __asan_handle_no_return(); break;
    288     case 30: __asan_address_is_poisoned(nullptr); break;
    289     case 31: __asan_poison_memory_region(nullptr, 0); break;
    290     case 32: __asan_unpoison_memory_region(nullptr, 0); break;
    291     case 34: __asan_before_dynamic_init(nullptr); break;
    292     case 35: __asan_after_dynamic_init(); break;
    293     case 36: __asan_poison_stack_memory(0, 0); break;
    294     case 37: __asan_unpoison_stack_memory(0, 0); break;
    295     case 38: __asan_region_is_poisoned(0, 0); break;
    296     case 39: __asan_describe_address(0); break;
    297     case 40: __asan_set_shadow_00(0, 0); break;
    298     case 41: __asan_set_shadow_f1(0, 0); break;
    299     case 42: __asan_set_shadow_f2(0, 0); break;
    300     case 43: __asan_set_shadow_f3(0, 0); break;
    301     case 44: __asan_set_shadow_f5(0, 0); break;
    302     case 45: __asan_set_shadow_f8(0, 0); break;
    303   }
    304   // clang-format on
    305 }
    306 
    307 static void asan_atexit() {
    308   Printf("AddressSanitizer exit stats:\n");
    309   __asan_print_accumulated_stats();
    310   // Print AsanMappingProfile.
    311   for (uptr i = 0; i < kAsanMappingProfileSize; i++) {
    312     if (AsanMappingProfile[i] == 0) continue;
    313     Printf("asan_mapping.h:%zd -- %zd\n", i, AsanMappingProfile[i]);
    314   }
    315 }
    316 
    317 static void InitializeHighMemEnd() {
    318 #if !SANITIZER_MYRIAD2
    319 #if !ASAN_FIXED_MAPPING
    320   kHighMemEnd = GetMaxUserVirtualAddress();
    321   // Increase kHighMemEnd to make sure it's properly
    322   // aligned together with kHighMemBeg:
    323   kHighMemEnd |= SHADOW_GRANULARITY * GetMmapGranularity() - 1;
    324 #endif  // !ASAN_FIXED_MAPPING
    325   CHECK_EQ((kHighMemBeg % GetMmapGranularity()), 0);
    326 #endif  // !SANITIZER_MYRIAD2
    327 }
    328 
    329 void PrintAddressSpaceLayout() {
    330   if (kHighMemBeg) {
    331     Printf("|| `[%p, %p]` || HighMem    ||\n",
    332            (void*)kHighMemBeg, (void*)kHighMemEnd);
    333     Printf("|| `[%p, %p]` || HighShadow ||\n",
    334            (void*)kHighShadowBeg, (void*)kHighShadowEnd);
    335   }
    336   if (kMidMemBeg) {
    337     Printf("|| `[%p, %p]` || ShadowGap3 ||\n",
    338            (void*)kShadowGap3Beg, (void*)kShadowGap3End);
    339     Printf("|| `[%p, %p]` || MidMem     ||\n",
    340            (void*)kMidMemBeg, (void*)kMidMemEnd);
    341     Printf("|| `[%p, %p]` || ShadowGap2 ||\n",
    342            (void*)kShadowGap2Beg, (void*)kShadowGap2End);
    343     Printf("|| `[%p, %p]` || MidShadow  ||\n",
    344            (void*)kMidShadowBeg, (void*)kMidShadowEnd);
    345   }
    346   Printf("|| `[%p, %p]` || ShadowGap  ||\n",
    347          (void*)kShadowGapBeg, (void*)kShadowGapEnd);
    348   if (kLowShadowBeg) {
    349     Printf("|| `[%p, %p]` || LowShadow  ||\n",
    350            (void*)kLowShadowBeg, (void*)kLowShadowEnd);
    351     Printf("|| `[%p, %p]` || LowMem     ||\n",
    352            (void*)kLowMemBeg, (void*)kLowMemEnd);
    353   }
    354   Printf("MemToShadow(shadow): %p %p",
    355          (void*)MEM_TO_SHADOW(kLowShadowBeg),
    356          (void*)MEM_TO_SHADOW(kLowShadowEnd));
    357   if (kHighMemBeg) {
    358     Printf(" %p %p",
    359            (void*)MEM_TO_SHADOW(kHighShadowBeg),
    360            (void*)MEM_TO_SHADOW(kHighShadowEnd));
    361   }
    362   if (kMidMemBeg) {
    363     Printf(" %p %p",
    364            (void*)MEM_TO_SHADOW(kMidShadowBeg),
    365            (void*)MEM_TO_SHADOW(kMidShadowEnd));
    366   }
    367   Printf("\n");
    368   Printf("redzone=%zu\n", (uptr)flags()->redzone);
    369   Printf("max_redzone=%zu\n", (uptr)flags()->max_redzone);
    370   Printf("quarantine_size_mb=%zuM\n", (uptr)flags()->quarantine_size_mb);
    371   Printf("thread_local_quarantine_size_kb=%zuK\n",
    372          (uptr)flags()->thread_local_quarantine_size_kb);
    373   Printf("malloc_context_size=%zu\n",
    374          (uptr)common_flags()->malloc_context_size);
    375 
    376   Printf("SHADOW_SCALE: %d\n", (int)SHADOW_SCALE);
    377   Printf("SHADOW_GRANULARITY: %d\n", (int)SHADOW_GRANULARITY);
    378   Printf("SHADOW_OFFSET: 0x%zx\n", (uptr)SHADOW_OFFSET);
    379   CHECK(SHADOW_SCALE >= 3 && SHADOW_SCALE <= 7);
    380   if (kMidMemBeg)
    381     CHECK(kMidShadowBeg > kLowShadowEnd &&
    382           kMidMemBeg > kMidShadowEnd &&
    383           kHighShadowBeg > kMidMemEnd);
    384 }
    385 
    386 static bool UNUSED __local_asan_dyninit = [] {
    387   MaybeStartBackgroudThread();
    388   SetSoftRssLimitExceededCallback(AsanSoftRssLimitExceededCallback);
    389 
    390   return false;
    391 }();
    392 
    393 static void AsanInitInternal() {
    394   if (LIKELY(asan_inited)) return;
    395   SanitizerToolName = "AddressSanitizer";
    396   CHECK(!asan_init_is_running && "ASan init calls itself!");
    397   asan_init_is_running = true;
    398 
    399   CacheBinaryName();
    400   CheckASLR();
    401 
    402   // Initialize flags. This must be done early, because most of the
    403   // initialization steps look at flags().
    404   InitializeFlags();
    405 
    406   // Stop performing init at this point if we are being loaded via
    407   // dlopen() and the platform supports it.
    408   if (SANITIZER_SUPPORTS_INIT_FOR_DLOPEN && UNLIKELY(HandleDlopenInit())) {
    409     asan_init_is_running = false;
    410     VReport(1, "AddressSanitizer init is being performed for dlopen().\n");
    411     return;
    412   }
    413 
    414   AsanCheckIncompatibleRT();
    415   AsanCheckDynamicRTPrereqs();
    416   AvoidCVE_2016_2143();
    417 
    418   SetCanPoisonMemory(flags()->poison_heap);
    419   SetMallocContextSize(common_flags()->malloc_context_size);
    420 
    421   InitializePlatformExceptionHandlers();
    422 
    423   InitializeHighMemEnd();
    424 
    425   // Make sure we are not statically linked.
    426   AsanDoesNotSupportStaticLinkage();
    427 
    428   // Install tool-specific callbacks in sanitizer_common.
    429   AddDieCallback(AsanDie);
    430   SetCheckFailedCallback(AsanCheckFailed);
    431   SetPrintfAndReportCallback(AppendToErrorMessageBuffer);
    432 
    433   __sanitizer_set_report_path(common_flags()->log_path);
    434 
    435   __asan_option_detect_stack_use_after_return =
    436       flags()->detect_stack_use_after_return;
    437 
    438   __sanitizer::InitializePlatformEarly();
    439 
    440   // Re-exec ourselves if we need to set additional env or command line args.
    441   MaybeReexec();
    442 
    443   // Setup internal allocator callback.
    444   SetLowLevelAllocateMinAlignment(SHADOW_GRANULARITY);
    445   SetLowLevelAllocateCallback(OnLowLevelAllocate);
    446 
    447   InitializeAsanInterceptors();
    448 
    449   // Enable system log ("adb logcat") on Android.
    450   // Doing this before interceptors are initialized crashes in:
    451   // AsanInitInternal -> android_log_write -> __interceptor_strcmp
    452   AndroidLogInit();
    453 
    454   ReplaceSystemMalloc();
    455 
    456   DisableCoreDumperIfNecessary();
    457 
    458   InitializeShadowMemory();
    459 
    460   AsanTSDInit(PlatformTSDDtor);
    461   InstallDeadlySignalHandlers(AsanOnDeadlySignal);
    462 
    463   AllocatorOptions allocator_options;
    464   allocator_options.SetFrom(flags(), common_flags());
    465   InitializeAllocator(allocator_options);
    466 
    467   // On Linux AsanThread::ThreadStart() calls malloc() that's why asan_inited
    468   // should be set to 1 prior to initializing the threads.
    469   asan_inited = 1;
    470   asan_init_is_running = false;
    471 
    472   if (flags()->atexit)
    473     Atexit(asan_atexit);
    474 
    475   InitializeCoverage(common_flags()->coverage, common_flags()->coverage_dir);
    476 
    477   // Now that ASan runtime is (mostly) initialized, deactivate it if
    478   // necessary, so that it can be re-activated when requested.
    479   if (flags()->start_deactivated)
    480     AsanDeactivate();
    481 
    482   // interceptors
    483   InitTlsSize();
    484 
    485   // Create main thread.
    486   AsanThread *main_thread = CreateMainThread();
    487   CHECK_EQ(0, main_thread->tid());
    488   force_interface_symbols();  // no-op.
    489   SanitizerInitializeUnwinder();
    490 
    491   if (CAN_SANITIZE_LEAKS) {
    492     __lsan::InitCommonLsan();
    493     if (common_flags()->detect_leaks && common_flags()->leak_check_at_exit) {
    494       if (flags()->halt_on_error)
    495         Atexit(__lsan::DoLeakCheck);
    496       else
    497         Atexit(__lsan::DoRecoverableLeakCheckVoid);
    498     }
    499   }
    500 
    501 #if CAN_SANITIZE_UB
    502   __ubsan::InitAsPlugin();
    503 #endif
    504 
    505   InitializeSuppressions();
    506 
    507   if (CAN_SANITIZE_LEAKS) {
    508     // LateInitialize() calls dlsym, which can allocate an error string buffer
    509     // in the TLS.  Let's ignore the allocation to avoid reporting a leak.
    510     __lsan::ScopedInterceptorDisabler disabler;
    511     Symbolizer::LateInitialize();
    512   } else {
    513     Symbolizer::LateInitialize();
    514   }
    515 
    516   VReport(1, "AddressSanitizer Init done\n");
    517 
    518   if (flags()->sleep_after_init) {
    519     Report("Sleeping for %d second(s)\n", flags()->sleep_after_init);
    520     SleepForSeconds(flags()->sleep_after_init);
    521   }
    522 }
    523 
    524 // Initialize as requested from some part of ASan runtime library (interceptors,
    525 // allocator, etc).
    526 void AsanInitFromRtl() {
    527   AsanInitInternal();
    528 }
    529 
    530 #if ASAN_DYNAMIC
    531 // Initialize runtime in case it's LD_PRELOAD-ed into unsanitized executable
    532 // (and thus normal initializers from .preinit_array or modules haven't run).
    533 
    534 class AsanInitializer {
    535 public:  // NOLINT
    536   AsanInitializer() {
    537     AsanInitFromRtl();
    538   }
    539 };
    540 
    541 static AsanInitializer asan_initializer;
    542 #endif  // ASAN_DYNAMIC
    543 
    544 } // namespace __asan
    545 
    546 // ---------------------- Interface ---------------- {{{1
    547 using namespace __asan;  // NOLINT
    548 
    549 void NOINLINE __asan_handle_no_return() {
    550   if (asan_init_is_running)
    551     return;
    552 
    553   int local_stack;
    554   AsanThread *curr_thread = GetCurrentThread();
    555   uptr PageSize = GetPageSizeCached();
    556   uptr top, bottom;
    557   if (curr_thread) {
    558     top = curr_thread->stack_top();
    559     bottom = ((uptr)&local_stack - PageSize) & ~(PageSize - 1);
    560   } else if (SANITIZER_RTEMS) {
    561     // Give up On RTEMS.
    562     return;
    563   } else {
    564     CHECK(!SANITIZER_FUCHSIA);
    565     // If we haven't seen this thread, try asking the OS for stack bounds.
    566     uptr tls_addr, tls_size, stack_size;
    567     GetThreadStackAndTls(/*main=*/false, &bottom, &stack_size, &tls_addr,
    568                          &tls_size);
    569     top = bottom + stack_size;
    570   }
    571   static const uptr kMaxExpectedCleanupSize = 64 << 20;  // 64M
    572   if (top - bottom > kMaxExpectedCleanupSize) {
    573     static bool reported_warning = false;
    574     if (reported_warning)
    575       return;
    576     reported_warning = true;
    577     Report("WARNING: ASan is ignoring requested __asan_handle_no_return: "
    578            "stack top: %p; bottom %p; size: %p (%zd)\n"
    579            "False positive error reports may follow\n"
    580            "For details see "
    581            "https://github.com/google/sanitizers/issues/189\n",
    582            top, bottom, top - bottom, top - bottom);
    583     return;
    584   }
    585   PoisonShadow(bottom, top - bottom, 0);
    586   if (curr_thread && curr_thread->has_fake_stack())
    587     curr_thread->fake_stack()->HandleNoReturn();
    588 }
    589 
    590 void NOINLINE __asan_set_death_callback(void (*callback)(void)) {
    591   SetUserDieCallback(callback);
    592 }
    593 
    594 // Initialize as requested from instrumented application code.
    595 // We use this call as a trigger to wake up ASan from deactivated state.
    596 void __asan_init() {
    597   AsanActivate();
    598   AsanInitInternal();
    599 }
    600 
    601 void __asan_version_mismatch_check() {
    602   // Do nothing.
    603 }
    604