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