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