1 //===-- asan_internal.h -----------------------------------------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file is a part of AddressSanitizer, an address sanity checker.
12 // ASan-private header which defines various general utilities.
13 //===----------------------------------------------------------------------===//
14 #ifndef ASAN_INTERNAL_H
15 #define ASAN_INTERNAL_H
17 #include "asan_flags.h"
18 #include "asan_interface_internal.h"
19 #include "sanitizer_common/sanitizer_common.h"
20 #include "sanitizer_common/sanitizer_internal_defs.h"
21 #include "sanitizer_common/sanitizer_stacktrace.h"
22 #include "sanitizer_common/sanitizer_libc.h"
24 #if __has_feature(address_sanitizer) || defined(__SANITIZE_ADDRESS__)
25 # error "The AddressSanitizer run-time should not be"
26 " instrumented by AddressSanitizer"
29 // Build-time configuration options.
31 // If set, asan will intercept C++ exception api call(s).
32 #ifndef ASAN_HAS_EXCEPTIONS
33 # define ASAN_HAS_EXCEPTIONS 1
36 // If set, values like allocator chunk size, as well as defaults for some flags
37 // will be changed towards less memory overhead.
38 #ifndef ASAN_LOW_MEMORY
39 # if SANITIZER_IOS || SANITIZER_ANDROID
40 # define ASAN_LOW_MEMORY 1
42 # define ASAN_LOW_MEMORY 0
48 # define ASAN_DYNAMIC 1
50 # define ASAN_DYNAMIC 0
54 // All internal functions in asan reside inside the __asan namespace
55 // to avoid namespace collisions with the user programs.
56 // Separate namespace also makes it simpler to distinguish the asan run-time
57 // functions from the instrumented user code in a profile.
61 using __sanitizer::StackTrace;
63 void AsanInitFromRtl();
66 void InitializePlatformExceptionHandlers();
67 // Returns whether an address is a valid allocated system heap block.
68 // 'addr' must point to the beginning of the block.
69 bool IsSystemHeapAddress(uptr addr);
72 void NORETURN ShowStatsAndAbort();
74 // asan_malloc_linux.cc / asan_malloc_mac.cc
75 void ReplaceSystemMalloc();
77 // asan_linux.cc / asan_mac.cc / asan_win.cc
78 uptr FindDynamicShadowStart();
79 void *AsanDoesNotSupportStaticLinkage();
80 void AsanCheckDynamicRTPrereqs();
81 void AsanCheckIncompatibleRT();
83 // Support function for __asan_(un)register_image_globals. Searches for the
84 // loaded image containing `needle' and then enumerates all global metadata
85 // structures declared in that image, applying `op' (e.g.,
86 // __asan_(un)register_globals) to them.
87 typedef void (*globals_op_fptr)(__asan_global *, uptr);
88 void AsanApplyToGlobals(globals_op_fptr op, const void *needle);
90 void AsanOnDeadlySignal(int, void *siginfo, void *context);
92 void ReadContextStack(void *context, uptr *stack, uptr *ssize);
93 void StopInitOrderChecking();
95 // Wrapper for TLS/TSD.
96 void AsanTSDInit(void (*destructor)(void *tsd));
98 void AsanTSDSet(void *tsd);
99 void PlatformTSDDtor(void *tsd);
101 void AppendToErrorMessageBuffer(const char *buffer);
103 void *AsanDlSymNext(const char *sym);
105 void ReserveShadowMemoryRange(uptr beg, uptr end, const char *name);
107 // Add convenient macro for interface functions that may be represented as
109 #define ASAN_MALLOC_HOOK(ptr, size) \
111 if (&__sanitizer_malloc_hook) __sanitizer_malloc_hook(ptr, size); \
112 RunMallocHooks(ptr, size); \
114 #define ASAN_FREE_HOOK(ptr) \
116 if (&__sanitizer_free_hook) __sanitizer_free_hook(ptr); \
119 #define ASAN_ON_ERROR() \
120 if (&__asan_on_error) __asan_on_error()
122 extern int asan_inited;
123 // Used to avoid infinite recursion in __asan_init().
124 extern bool asan_init_is_running;
125 extern void (*death_callback)(void);
126 // These magic values are written to shadow for better error reporting.
127 const int kAsanHeapLeftRedzoneMagic = 0xfa;
128 const int kAsanHeapFreeMagic = 0xfd;
129 const int kAsanStackLeftRedzoneMagic = 0xf1;
130 const int kAsanStackMidRedzoneMagic = 0xf2;
131 const int kAsanStackRightRedzoneMagic = 0xf3;
132 const int kAsanStackAfterReturnMagic = 0xf5;
133 const int kAsanInitializationOrderMagic = 0xf6;
134 const int kAsanUserPoisonedMemoryMagic = 0xf7;
135 const int kAsanContiguousContainerOOBMagic = 0xfc;
136 const int kAsanStackUseAfterScopeMagic = 0xf8;
137 const int kAsanGlobalRedzoneMagic = 0xf9;
138 const int kAsanInternalHeapMagic = 0xfe;
139 const int kAsanArrayCookieMagic = 0xac;
140 const int kAsanIntraObjectRedzone = 0xbb;
141 const int kAsanAllocaLeftMagic = 0xca;
142 const int kAsanAllocaRightMagic = 0xcb;
144 static const uptr kCurrentStackFrameMagic = 0x41B58AB3;
145 static const uptr kRetiredStackFrameMagic = 0x45E0360E;
147 } // namespace __asan
149 #endif // ASAN_INTERNAL_H