asan_internal.h revision cb8c4dce691097718d5af41b36899b72ef4b1d84
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
139struct Flags {
140  int quarantine_size;
141  bool symbolize;
142  int  verbosity;
143  int redzone;
144  int  debug;
145  bool poison_shadow;
146  int  report_globals;
147  int malloc_context_size;
148  bool replace_str;
149  bool replace_intrin;
150  bool replace_cfallocator;
151  bool mac_ignore_invalid_free;
152  bool use_fake_stack;
153  int max_malloc_fill_size;
154  int  exitcode;
155  bool allow_user_poisoning;
156  int  sleep_before_dying;
157  bool handle_segv;
158  bool use_sigaltstack;
159  bool check_malloc_usable_size;
160  bool unmap_shadow_on_exit;
161  bool abort_on_error;
162  bool atexit;
163  bool disable_core;
164};
165Flags *flags();
166void InitializeFlags(Flags *f, const char *env);
167
168extern int asan_inited;
169// Used to avoid infinite recursion in __asan_init().
170extern bool asan_init_is_running;
171extern void (*death_callback)(void);
172
173enum LinkerInitialized { LINKER_INITIALIZED = 0 };
174
175#define ASAN_ARRAY_SIZE(a) (sizeof(a)/sizeof((a)[0]))
176
177#if !defined(_WIN32) || defined(__clang__)
178# define GET_CALLER_PC() (uptr)__builtin_return_address(0)
179# define GET_CURRENT_FRAME() (uptr)__builtin_frame_address(0)
180#else
181# define GET_CALLER_PC() (uptr)_ReturnAddress()
182// CaptureStackBackTrace doesn't need to know BP on Windows.
183// FIXME: This macro is still used when printing error reports though it's not
184// clear if the BP value is needed in the ASan reports on Windows.
185# define GET_CURRENT_FRAME() (uptr)0xDEADBEEF
186#endif
187
188#ifdef _WIN32
189# ifndef ASAN_USE_EXTERNAL_SYMBOLIZER
190#  define ASAN_USE_EXTERNAL_SYMBOLIZER __asan_WinSymbolize
191bool __asan_WinSymbolize(const void *addr, char *out_buffer, int buffer_size);
192# endif
193#endif  // _WIN32
194
195// These magic values are written to shadow for better error reporting.
196const int kAsanHeapLeftRedzoneMagic = 0xfa;
197const int kAsanHeapRightRedzoneMagic = 0xfb;
198const int kAsanHeapFreeMagic = 0xfd;
199const int kAsanStackLeftRedzoneMagic = 0xf1;
200const int kAsanStackMidRedzoneMagic = 0xf2;
201const int kAsanStackRightRedzoneMagic = 0xf3;
202const int kAsanStackPartialRedzoneMagic = 0xf4;
203const int kAsanStackAfterReturnMagic = 0xf5;
204const int kAsanUserPoisonedMemoryMagic = 0xf7;
205const int kAsanGlobalRedzoneMagic = 0xf9;
206const int kAsanInternalHeapMagic = 0xfe;
207
208static const uptr kCurrentStackFrameMagic = 0x41B58AB3;
209static const uptr kRetiredStackFrameMagic = 0x45E0360E;
210
211// -------------------------- LowLevelAllocator ----- {{{1
212// A simple low-level memory allocator for internal use.
213class LowLevelAllocator {
214 public:
215  explicit LowLevelAllocator(LinkerInitialized) {}
216  // 'size' must be a power of two.
217  // Requires an external lock.
218  void *Allocate(uptr size);
219 private:
220  char *allocated_end_;
221  char *allocated_current_;
222};
223
224}  // namespace __asan
225
226#endif  // ASAN_INTERNAL_H
227