asan_internal.h revision 0aa794d78fbb6359f81025217559f1b03ff07999
1//===-- asan_internal.h -----------------------------------------*- C++ -*-===//
2//
3//                     The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This file is a part of AddressSanitizer, an address sanity checker.
11//
12// ASan-private header which defines various general utilities.
13//===----------------------------------------------------------------------===//
14#ifndef ASAN_INTERNAL_H
15#define ASAN_INTERNAL_H
16
17#include "sanitizer_common/sanitizer_common.h"
18#include "sanitizer_common/sanitizer_internal_defs.h"
19#include "sanitizer_common/sanitizer_libc.h"
20
21#if !defined(__linux__) && !defined(__APPLE__) && !defined(_WIN32)
22# error "This operating system is not supported by AddressSanitizer"
23#endif
24
25#if defined(_WIN32)
26extern "C" void* _ReturnAddress(void);
27# pragma intrinsic(_ReturnAddress)
28#endif  // defined(_WIN32)
29
30#define ASAN_DEFAULT_FAILURE_EXITCODE 1
31
32#if defined(__linux__)
33# define ASAN_LINUX   1
34#else
35# define ASAN_LINUX   0
36#endif
37
38#if defined(__APPLE__)
39# define ASAN_MAC     1
40#else
41# define ASAN_MAC     0
42#endif
43
44#if defined(_WIN32)
45# define ASAN_WINDOWS 1
46#else
47# define ASAN_WINDOWS 0
48#endif
49
50#define ASAN_POSIX (ASAN_LINUX || ASAN_MAC)
51
52#if __has_feature(address_sanitizer)
53# error "The AddressSanitizer run-time should not be"
54        " instrumented by AddressSanitizer"
55#endif
56
57// Build-time configuration options.
58
59// If set, asan will install its own SEGV signal handler.
60#ifndef ASAN_NEEDS_SEGV
61# define ASAN_NEEDS_SEGV 1
62#endif
63
64// If set, asan will intercept C++ exception api call(s).
65#ifndef ASAN_HAS_EXCEPTIONS
66# define ASAN_HAS_EXCEPTIONS 1
67#endif
68
69// If set, asan uses the values of SHADOW_SCALE and SHADOW_OFFSET
70// provided by the instrumented objects. Otherwise constants are used.
71#ifndef ASAN_FLEXIBLE_MAPPING_AND_OFFSET
72# define ASAN_FLEXIBLE_MAPPING_AND_OFFSET 0
73#endif
74
75// If set, values like allocator chunk size, as well as defaults for some flags
76// will be changed towards less memory overhead.
77#ifndef ASAN_LOW_MEMORY
78# define ASAN_LOW_MEMORY 0
79#endif
80
81// All internal functions in asan reside inside the __asan namespace
82// to avoid namespace collisions with the user programs.
83// Seperate namespace also makes it simpler to distinguish the asan run-time
84// functions from the instrumented user code in a profile.
85namespace __asan {
86
87class AsanThread;
88struct AsanStackTrace;
89
90// asan_rtl.cc
91void NORETURN ShowStatsAndAbort();
92
93// asan_globals.cc
94bool DescribeAddrIfGlobal(uptr addr);
95
96void ReplaceOperatorsNewAndDelete();
97// asan_malloc_linux.cc / asan_malloc_mac.cc
98void ReplaceSystemMalloc();
99
100// asan_linux.cc / asan_mac.cc / asan_win.cc
101void *AsanDoesNotSupportStaticLinkage();
102
103void GetPcSpBp(void *context, uptr *pc, uptr *sp, uptr *bp);
104
105bool AsanInterceptsSignal(int signum);
106void SetAlternateSignalStack();
107void UnsetAlternateSignalStack();
108void InstallSignalHandlers();
109
110// Wrapper for TLS/TSD.
111void AsanTSDInit(void (*destructor)(void *tsd));
112void *AsanTSDGet();
113void AsanTSDSet(void *tsd);
114
115void AppendToErrorMessageBuffer(const char *buffer);
116// asan_printf.cc
117void AsanPrintf(const char *format, ...);
118void AsanReport(const char *format, ...);
119
120// asan_poisoning.cc
121// Poisons the shadow memory for "size" bytes starting from "addr".
122void PoisonShadow(uptr addr, uptr size, u8 value);
123// Poisons the shadow memory for "redzone_size" bytes starting from
124// "addr + size".
125void PoisonShadowPartialRightRedzone(uptr addr,
126                                     uptr size,
127                                     uptr redzone_size,
128                                     u8 value);
129
130// Platfrom-specific options.
131#ifdef __APPLE__
132bool PlatformHasDifferentMemcpyAndMemmove();
133# define PLATFORM_HAS_DIFFERENT_MEMCPY_AND_MEMMOVE \
134    (PlatformHasDifferentMemcpyAndMemmove())
135#else
136# define PLATFORM_HAS_DIFFERENT_MEMCPY_AND_MEMMOVE true
137#endif  // __APPLE__
138
139extern uptr  FLAG_quarantine_size;
140extern s64 FLAG_demangle;
141extern bool    FLAG_symbolize;
142extern s64 FLAG_v;
143extern uptr  FLAG_redzone;
144extern s64 FLAG_debug;
145extern bool    FLAG_poison_shadow;
146extern s64 FLAG_report_globals;
147extern uptr  FLAG_malloc_context_size;
148extern bool    FLAG_replace_str;
149extern bool    FLAG_replace_intrin;
150extern bool    FLAG_replace_cfallocator;
151extern bool    FLAG_mac_ignore_invalid_free;
152extern bool    FLAG_fast_unwind;
153extern bool    FLAG_use_fake_stack;
154extern uptr  FLAG_max_malloc_fill_size;
155extern s64 FLAG_exitcode;
156extern bool    FLAG_allow_user_poisoning;
157extern s64 FLAG_sleep_before_dying;
158extern bool    FLAG_handle_segv;
159extern bool    FLAG_use_sigaltstack;
160extern bool    FLAG_check_malloc_usable_size;
161extern bool    FLAG_unmap_shadow_on_exit;
162extern bool    FLAG_abort_on_error;
163
164extern int asan_inited;
165// Used to avoid infinite recursion in __asan_init().
166extern bool asan_init_is_running;
167extern void (*death_callback)(void);
168
169enum LinkerInitialized { LINKER_INITIALIZED = 0 };
170
171#define ASAN_ARRAY_SIZE(a) (sizeof(a)/sizeof((a)[0]))
172
173#if !defined(_WIN32) || defined(__clang__)
174# define GET_CALLER_PC() (uptr)__builtin_return_address(0)
175# define GET_CURRENT_FRAME() (uptr)__builtin_frame_address(0)
176#else
177# define GET_CALLER_PC() (uptr)_ReturnAddress()
178// CaptureStackBackTrace doesn't need to know BP on Windows.
179// FIXME: This macro is still used when printing error reports though it's not
180// clear if the BP value is needed in the ASan reports on Windows.
181# define GET_CURRENT_FRAME() (uptr)0xDEADBEEF
182#endif
183
184#ifdef _WIN32
185# ifndef ASAN_USE_EXTERNAL_SYMBOLIZER
186#  define ASAN_USE_EXTERNAL_SYMBOLIZER __asan_WinSymbolize
187bool __asan_WinSymbolize(const void *addr, char *out_buffer, int buffer_size);
188# endif
189#endif  // _WIN32
190
191// These magic values are written to shadow for better error reporting.
192const int kAsanHeapLeftRedzoneMagic = 0xfa;
193const int kAsanHeapRightRedzoneMagic = 0xfb;
194const int kAsanHeapFreeMagic = 0xfd;
195const int kAsanStackLeftRedzoneMagic = 0xf1;
196const int kAsanStackMidRedzoneMagic = 0xf2;
197const int kAsanStackRightRedzoneMagic = 0xf3;
198const int kAsanStackPartialRedzoneMagic = 0xf4;
199const int kAsanStackAfterReturnMagic = 0xf5;
200const int kAsanUserPoisonedMemoryMagic = 0xf7;
201const int kAsanGlobalRedzoneMagic = 0xf9;
202const int kAsanInternalHeapMagic = 0xfe;
203
204static const uptr kCurrentStackFrameMagic = 0x41B58AB3;
205static const uptr kRetiredStackFrameMagic = 0x45E0360E;
206
207// -------------------------- LowLevelAllocator ----- {{{1
208// A simple low-level memory allocator for internal use.
209class LowLevelAllocator {
210 public:
211  explicit LowLevelAllocator(LinkerInitialized) {}
212  // 'size' must be a power of two.
213  // Requires an external lock.
214  void *Allocate(uptr size);
215 private:
216  char *allocated_end_;
217  char *allocated_current_;
218};
219
220}  // namespace __asan
221
222#endif  // ASAN_INTERNAL_H
223