xref: /aosp_15_r20/external/compiler-rt/lib/asan/asan_internal.h (revision 7c3d14c8b49c529e04be81a3ce6f5cc23712e4c6)
1*7c3d14c8STreehugger Robot //===-- asan_internal.h -----------------------------------------*- C++ -*-===//
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 // ASan-private header which defines various general utilities.
13*7c3d14c8STreehugger Robot //===----------------------------------------------------------------------===//
14*7c3d14c8STreehugger Robot #ifndef ASAN_INTERNAL_H
15*7c3d14c8STreehugger Robot #define ASAN_INTERNAL_H
16*7c3d14c8STreehugger Robot 
17*7c3d14c8STreehugger Robot #include "asan_flags.h"
18*7c3d14c8STreehugger Robot #include "asan_interface_internal.h"
19*7c3d14c8STreehugger Robot #include "sanitizer_common/sanitizer_common.h"
20*7c3d14c8STreehugger Robot #include "sanitizer_common/sanitizer_internal_defs.h"
21*7c3d14c8STreehugger Robot #include "sanitizer_common/sanitizer_stacktrace.h"
22*7c3d14c8STreehugger Robot #include "sanitizer_common/sanitizer_libc.h"
23*7c3d14c8STreehugger Robot 
24*7c3d14c8STreehugger Robot #if __has_feature(address_sanitizer) || defined(__SANITIZE_ADDRESS__)
25*7c3d14c8STreehugger Robot # error "The AddressSanitizer run-time should not be"
26*7c3d14c8STreehugger Robot         " instrumented by AddressSanitizer"
27*7c3d14c8STreehugger Robot #endif
28*7c3d14c8STreehugger Robot 
29*7c3d14c8STreehugger Robot // Build-time configuration options.
30*7c3d14c8STreehugger Robot 
31*7c3d14c8STreehugger Robot // If set, asan will intercept C++ exception api call(s).
32*7c3d14c8STreehugger Robot #ifndef ASAN_HAS_EXCEPTIONS
33*7c3d14c8STreehugger Robot # define ASAN_HAS_EXCEPTIONS 1
34*7c3d14c8STreehugger Robot #endif
35*7c3d14c8STreehugger Robot 
36*7c3d14c8STreehugger Robot // If set, values like allocator chunk size, as well as defaults for some flags
37*7c3d14c8STreehugger Robot // will be changed towards less memory overhead.
38*7c3d14c8STreehugger Robot #ifndef ASAN_LOW_MEMORY
39*7c3d14c8STreehugger Robot # if SANITIZER_IOS || (SANITIZER_WORDSIZE == 32)
40*7c3d14c8STreehugger Robot #  define ASAN_LOW_MEMORY 1
41*7c3d14c8STreehugger Robot # else
42*7c3d14c8STreehugger Robot #  define ASAN_LOW_MEMORY 0
43*7c3d14c8STreehugger Robot # endif
44*7c3d14c8STreehugger Robot #endif
45*7c3d14c8STreehugger Robot 
46*7c3d14c8STreehugger Robot #ifndef ASAN_DYNAMIC
47*7c3d14c8STreehugger Robot # ifdef PIC
48*7c3d14c8STreehugger Robot #  define ASAN_DYNAMIC 1
49*7c3d14c8STreehugger Robot # else
50*7c3d14c8STreehugger Robot #  define ASAN_DYNAMIC 0
51*7c3d14c8STreehugger Robot # endif
52*7c3d14c8STreehugger Robot #endif
53*7c3d14c8STreehugger Robot 
54*7c3d14c8STreehugger Robot // All internal functions in asan reside inside the __asan namespace
55*7c3d14c8STreehugger Robot // to avoid namespace collisions with the user programs.
56*7c3d14c8STreehugger Robot // Separate namespace also makes it simpler to distinguish the asan run-time
57*7c3d14c8STreehugger Robot // functions from the instrumented user code in a profile.
58*7c3d14c8STreehugger Robot namespace __asan {
59*7c3d14c8STreehugger Robot 
60*7c3d14c8STreehugger Robot class AsanThread;
61*7c3d14c8STreehugger Robot using __sanitizer::StackTrace;
62*7c3d14c8STreehugger Robot 
63*7c3d14c8STreehugger Robot void AsanInitFromRtl();
64*7c3d14c8STreehugger Robot 
65*7c3d14c8STreehugger Robot // asan_win.cc
66*7c3d14c8STreehugger Robot void InitializePlatformExceptionHandlers();
67*7c3d14c8STreehugger Robot 
68*7c3d14c8STreehugger Robot // asan_rtl.cc
69*7c3d14c8STreehugger Robot void NORETURN ShowStatsAndAbort();
70*7c3d14c8STreehugger Robot 
71*7c3d14c8STreehugger Robot // asan_malloc_linux.cc / asan_malloc_mac.cc
72*7c3d14c8STreehugger Robot void ReplaceSystemMalloc();
73*7c3d14c8STreehugger Robot 
74*7c3d14c8STreehugger Robot // asan_linux.cc / asan_mac.cc / asan_win.cc
75*7c3d14c8STreehugger Robot void *AsanDoesNotSupportStaticLinkage();
76*7c3d14c8STreehugger Robot void AsanCheckDynamicRTPrereqs();
77*7c3d14c8STreehugger Robot void AsanCheckIncompatibleRT();
78*7c3d14c8STreehugger Robot 
79*7c3d14c8STreehugger Robot // Support function for __asan_(un)register_image_globals. Searches for the
80*7c3d14c8STreehugger Robot // loaded image containing `needle' and then enumerates all global metadata
81*7c3d14c8STreehugger Robot // structures declared in that image, applying `op' (e.g.,
82*7c3d14c8STreehugger Robot // __asan_(un)register_globals) to them.
83*7c3d14c8STreehugger Robot typedef void (*globals_op_fptr)(__asan_global *, uptr);
84*7c3d14c8STreehugger Robot void AsanApplyToGlobals(globals_op_fptr op, const void *needle);
85*7c3d14c8STreehugger Robot 
86*7c3d14c8STreehugger Robot void AsanOnDeadlySignal(int, void *siginfo, void *context);
87*7c3d14c8STreehugger Robot 
88*7c3d14c8STreehugger Robot void ReadContextStack(void *context, uptr *stack, uptr *ssize);
89*7c3d14c8STreehugger Robot void StopInitOrderChecking();
90*7c3d14c8STreehugger Robot 
91*7c3d14c8STreehugger Robot // Wrapper for TLS/TSD.
92*7c3d14c8STreehugger Robot void AsanTSDInit(void (*destructor)(void *tsd));
93*7c3d14c8STreehugger Robot void *AsanTSDGet();
94*7c3d14c8STreehugger Robot void AsanTSDSet(void *tsd);
95*7c3d14c8STreehugger Robot void PlatformTSDDtor(void *tsd);
96*7c3d14c8STreehugger Robot 
97*7c3d14c8STreehugger Robot void AppendToErrorMessageBuffer(const char *buffer);
98*7c3d14c8STreehugger Robot 
99*7c3d14c8STreehugger Robot void *AsanDlSymNext(const char *sym);
100*7c3d14c8STreehugger Robot 
101*7c3d14c8STreehugger Robot void ReserveShadowMemoryRange(uptr beg, uptr end, const char *name);
102*7c3d14c8STreehugger Robot 
103*7c3d14c8STreehugger Robot // Platform-specific options.
104*7c3d14c8STreehugger Robot #if SANITIZER_MAC
105*7c3d14c8STreehugger Robot bool PlatformHasDifferentMemcpyAndMemmove();
106*7c3d14c8STreehugger Robot # define PLATFORM_HAS_DIFFERENT_MEMCPY_AND_MEMMOVE \
107*7c3d14c8STreehugger Robot     (PlatformHasDifferentMemcpyAndMemmove())
108*7c3d14c8STreehugger Robot #elif SANITIZER_WINDOWS64
109*7c3d14c8STreehugger Robot # define PLATFORM_HAS_DIFFERENT_MEMCPY_AND_MEMMOVE false
110*7c3d14c8STreehugger Robot #else
111*7c3d14c8STreehugger Robot # define PLATFORM_HAS_DIFFERENT_MEMCPY_AND_MEMMOVE true
112*7c3d14c8STreehugger Robot #endif  // SANITIZER_MAC
113*7c3d14c8STreehugger Robot 
114*7c3d14c8STreehugger Robot // Add convenient macro for interface functions that may be represented as
115*7c3d14c8STreehugger Robot // weak hooks.
116*7c3d14c8STreehugger Robot #define ASAN_MALLOC_HOOK(ptr, size)                                   \
117*7c3d14c8STreehugger Robot   do {                                                                \
118*7c3d14c8STreehugger Robot     if (&__sanitizer_malloc_hook) __sanitizer_malloc_hook(ptr, size); \
119*7c3d14c8STreehugger Robot     RunMallocHooks(ptr, size);                                        \
120*7c3d14c8STreehugger Robot   } while (false)
121*7c3d14c8STreehugger Robot #define ASAN_FREE_HOOK(ptr)                                 \
122*7c3d14c8STreehugger Robot   do {                                                      \
123*7c3d14c8STreehugger Robot     if (&__sanitizer_free_hook) __sanitizer_free_hook(ptr); \
124*7c3d14c8STreehugger Robot     RunFreeHooks(ptr);                                      \
125*7c3d14c8STreehugger Robot   } while (false)
126*7c3d14c8STreehugger Robot #define ASAN_ON_ERROR() \
127*7c3d14c8STreehugger Robot   if (&__asan_on_error) __asan_on_error()
128*7c3d14c8STreehugger Robot 
129*7c3d14c8STreehugger Robot extern int asan_inited;
130*7c3d14c8STreehugger Robot // Used to avoid infinite recursion in __asan_init().
131*7c3d14c8STreehugger Robot extern bool asan_init_is_running;
132*7c3d14c8STreehugger Robot extern void (*death_callback)(void);
133*7c3d14c8STreehugger Robot // These magic values are written to shadow for better error reporting.
134*7c3d14c8STreehugger Robot const int kAsanHeapLeftRedzoneMagic = 0xfa;
135*7c3d14c8STreehugger Robot const int kAsanHeapRightRedzoneMagic = 0xfb;
136*7c3d14c8STreehugger Robot const int kAsanHeapFreeMagic = 0xfd;
137*7c3d14c8STreehugger Robot const int kAsanStackLeftRedzoneMagic = 0xf1;
138*7c3d14c8STreehugger Robot const int kAsanStackMidRedzoneMagic = 0xf2;
139*7c3d14c8STreehugger Robot const int kAsanStackRightRedzoneMagic = 0xf3;
140*7c3d14c8STreehugger Robot const int kAsanStackPartialRedzoneMagic = 0xf4;
141*7c3d14c8STreehugger Robot const int kAsanStackAfterReturnMagic = 0xf5;
142*7c3d14c8STreehugger Robot const int kAsanInitializationOrderMagic = 0xf6;
143*7c3d14c8STreehugger Robot const int kAsanUserPoisonedMemoryMagic = 0xf7;
144*7c3d14c8STreehugger Robot const int kAsanContiguousContainerOOBMagic = 0xfc;
145*7c3d14c8STreehugger Robot const int kAsanStackUseAfterScopeMagic = 0xf8;
146*7c3d14c8STreehugger Robot const int kAsanGlobalRedzoneMagic = 0xf9;
147*7c3d14c8STreehugger Robot const int kAsanInternalHeapMagic = 0xfe;
148*7c3d14c8STreehugger Robot const int kAsanArrayCookieMagic = 0xac;
149*7c3d14c8STreehugger Robot const int kAsanIntraObjectRedzone = 0xbb;
150*7c3d14c8STreehugger Robot const int kAsanAllocaLeftMagic = 0xca;
151*7c3d14c8STreehugger Robot const int kAsanAllocaRightMagic = 0xcb;
152*7c3d14c8STreehugger Robot 
153*7c3d14c8STreehugger Robot static const uptr kCurrentStackFrameMagic = 0x41B58AB3;
154*7c3d14c8STreehugger Robot static const uptr kRetiredStackFrameMagic = 0x45E0360E;
155*7c3d14c8STreehugger Robot 
156*7c3d14c8STreehugger Robot }  // namespace __asan
157*7c3d14c8STreehugger Robot 
158*7c3d14c8STreehugger Robot #endif  // ASAN_INTERNAL_H
159