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