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 "sanitizer_common/sanitizer_common.h"
19 #include "sanitizer_common/sanitizer_internal_defs.h"
20 #include "sanitizer_common/sanitizer_stacktrace.h"
21 #include "sanitizer_common/sanitizer_libc.h"
23 #if !defined(__linux__) && !defined(__APPLE__) && !defined(_WIN32)
24 # error "This operating system is not supported by AddressSanitizer"
27 #define ASAN_DEFAULT_FAILURE_EXITCODE 1
29 #if defined(__linux__)
35 #if defined(__APPLE__)
42 # define ASAN_WINDOWS 1
44 # define ASAN_WINDOWS 0
47 #if defined(__ANDROID__) || defined(ANDROID)
48 # define ASAN_ANDROID 1
50 # define ASAN_ANDROID 0
54 #define ASAN_POSIX (ASAN_LINUX || ASAN_MAC)
56 #if __has_feature(address_sanitizer)
57 # error "The AddressSanitizer run-time should not be"
58 " instrumented by AddressSanitizer"
61 // Build-time configuration options.
63 // If set, asan will install its own SEGV signal handler.
64 #ifndef ASAN_NEEDS_SEGV
65 # if ASAN_ANDROID == 1
66 # define ASAN_NEEDS_SEGV 0
68 # define ASAN_NEEDS_SEGV 1
72 // If set, asan will intercept C++ exception api call(s).
73 #ifndef ASAN_HAS_EXCEPTIONS
74 # define ASAN_HAS_EXCEPTIONS 1
77 // If set, asan uses the values of SHADOW_SCALE and SHADOW_OFFSET
78 // provided by the instrumented objects. Otherwise constants are used.
79 #ifndef ASAN_FLEXIBLE_MAPPING_AND_OFFSET
80 # define ASAN_FLEXIBLE_MAPPING_AND_OFFSET 0
83 // If set, values like allocator chunk size, as well as defaults for some flags
84 // will be changed towards less memory overhead.
85 #ifndef ASAN_LOW_MEMORY
86 #if SANITIZER_WORDSIZE == 32
87 # define ASAN_LOW_MEMORY 1
89 # define ASAN_LOW_MEMORY 0
93 // All internal functions in asan reside inside the __asan namespace
94 // to avoid namespace collisions with the user programs.
95 // Seperate namespace also makes it simpler to distinguish the asan run-time
96 // functions from the instrumented user code in a profile.
100 using __sanitizer::StackTrace;
103 void NORETURN ShowStatsAndAbort();
105 void ReplaceOperatorsNewAndDelete();
106 // asan_malloc_linux.cc / asan_malloc_mac.cc
107 void ReplaceSystemMalloc();
109 // asan_linux.cc / asan_mac.cc / asan_win.cc
110 void *AsanDoesNotSupportStaticLinkage();
112 void GetPcSpBp(void *context, uptr *pc, uptr *sp, uptr *bp);
115 bool AsanInterceptsSignal(int signum);
116 void SetAlternateSignalStack();
117 void UnsetAlternateSignalStack();
118 void InstallSignalHandlers();
119 void ReadContextStack(void *context, uptr *stack, uptr *ssize);
120 void AsanPlatformThreadInit();
122 // Wrapper for TLS/TSD.
123 void AsanTSDInit(void (*destructor)(void *tsd));
125 void AsanTSDSet(void *tsd);
127 void AppendToErrorMessageBuffer(const char *buffer);
130 // Poisons the shadow memory for "size" bytes starting from "addr".
131 void PoisonShadow(uptr addr, uptr size, u8 value);
132 // Poisons the shadow memory for "redzone_size" bytes starting from
134 void PoisonShadowPartialRightRedzone(uptr addr,
139 // Platfrom-specific options.
141 bool PlatformHasDifferentMemcpyAndMemmove();
142 # define PLATFORM_HAS_DIFFERENT_MEMCPY_AND_MEMMOVE \
143 (PlatformHasDifferentMemcpyAndMemmove())
145 # define PLATFORM_HAS_DIFFERENT_MEMCPY_AND_MEMMOVE true
148 // Add convenient macro for interface functions that may be represented as
150 #define ASAN_MALLOC_HOOK(ptr, size) \
151 if (&__asan_malloc_hook) __asan_malloc_hook(ptr, size)
152 #define ASAN_FREE_HOOK(ptr) \
153 if (&__asan_free_hook) __asan_free_hook(ptr)
154 #define ASAN_ON_ERROR() \
155 if (&__asan_on_error) __asan_on_error()
157 extern int asan_inited;
158 // Used to avoid infinite recursion in __asan_init().
159 extern bool asan_init_is_running;
160 extern void (*death_callback)(void);
162 // These magic values are written to shadow for better error reporting.
163 const int kAsanHeapLeftRedzoneMagic = 0xfa;
164 const int kAsanHeapRightRedzoneMagic = 0xfb;
165 const int kAsanHeapFreeMagic = 0xfd;
166 const int kAsanStackLeftRedzoneMagic = 0xf1;
167 const int kAsanStackMidRedzoneMagic = 0xf2;
168 const int kAsanStackRightRedzoneMagic = 0xf3;
169 const int kAsanStackPartialRedzoneMagic = 0xf4;
170 const int kAsanStackAfterReturnMagic = 0xf5;
171 const int kAsanInitializationOrderMagic = 0xf6;
172 const int kAsanUserPoisonedMemoryMagic = 0xf7;
173 const int kAsanStackUseAfterScopeMagic = 0xf8;
174 const int kAsanGlobalRedzoneMagic = 0xf9;
175 const int kAsanInternalHeapMagic = 0xfe;
177 static const uptr kCurrentStackFrameMagic = 0x41B58AB3;
178 static const uptr kRetiredStackFrameMagic = 0x45E0360E;
180 } // namespace __asan
182 #endif // ASAN_INTERNAL_H