asan_internal.h revision c3390df6670cb166119b961eb27a033fb9073496
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 "asan_flags.h"
18#include "sanitizer_common/sanitizer_common.h"
19#include "sanitizer_common/sanitizer_internal_defs.h"
20#include "sanitizer_common/sanitizer_libc.h"
21
22#if !defined(__linux__) && !defined(__APPLE__) && !defined(_WIN32)
23# error "This operating system is not supported by AddressSanitizer"
24#endif
25
26#if defined(_WIN32)
27extern "C" void* _ReturnAddress(void);
28# pragma intrinsic(_ReturnAddress)
29#endif  // defined(_WIN32)
30
31#define ASAN_DEFAULT_FAILURE_EXITCODE 1
32
33#if defined(__linux__)
34# define ASAN_LINUX   1
35#else
36# define ASAN_LINUX   0
37#endif
38
39#if defined(__APPLE__)
40# define ASAN_MAC     1
41#else
42# define ASAN_MAC     0
43#endif
44
45#if defined(_WIN32)
46# define ASAN_WINDOWS 1
47#else
48# define ASAN_WINDOWS 0
49#endif
50
51#if defined(__ANDROID__) || defined(ANDROID)
52# define ASAN_ANDROID 1
53#else
54# define ASAN_ANDROID 0
55#endif
56
57
58#define ASAN_POSIX (ASAN_LINUX || ASAN_MAC)
59
60#if __has_feature(address_sanitizer)
61# error "The AddressSanitizer run-time should not be"
62        " instrumented by AddressSanitizer"
63#endif
64
65// Build-time configuration options.
66
67// If set, asan will install its own SEGV signal handler.
68#ifndef ASAN_NEEDS_SEGV
69# define ASAN_NEEDS_SEGV 1
70#endif
71
72// If set, asan will intercept C++ exception api call(s).
73#ifndef ASAN_HAS_EXCEPTIONS
74# define ASAN_HAS_EXCEPTIONS 1
75#endif
76
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
81#endif
82
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# define ASAN_LOW_MEMORY 0
87#endif
88
89// All internal functions in asan reside inside the __asan namespace
90// to avoid namespace collisions with the user programs.
91// Seperate namespace also makes it simpler to distinguish the asan run-time
92// functions from the instrumented user code in a profile.
93namespace __asan {
94
95class AsanThread;
96struct StackTrace;
97
98// asan_rtl.cc
99void NORETURN ShowStatsAndAbort();
100
101void ReplaceOperatorsNewAndDelete();
102// asan_malloc_linux.cc / asan_malloc_mac.cc
103void ReplaceSystemMalloc();
104
105// asan_linux.cc / asan_mac.cc / asan_win.cc
106void *AsanDoesNotSupportStaticLinkage();
107
108void GetPcSpBp(void *context, uptr *pc, uptr *sp, uptr *bp);
109
110void MaybeReexec();
111bool AsanInterceptsSignal(int signum);
112void SetAlternateSignalStack();
113void UnsetAlternateSignalStack();
114void InstallSignalHandlers();
115void AsanPlatformThreadInit();
116
117// Wrapper for TLS/TSD.
118void AsanTSDInit(void (*destructor)(void *tsd));
119void *AsanTSDGet();
120void AsanTSDSet(void *tsd);
121
122void AppendToErrorMessageBuffer(const char *buffer);
123
124// asan_poisoning.cc
125// Poisons the shadow memory for "size" bytes starting from "addr".
126void PoisonShadow(uptr addr, uptr size, u8 value);
127// Poisons the shadow memory for "redzone_size" bytes starting from
128// "addr + size".
129void PoisonShadowPartialRightRedzone(uptr addr,
130                                     uptr size,
131                                     uptr redzone_size,
132                                     u8 value);
133
134// Platfrom-specific options.
135#ifdef __APPLE__
136bool PlatformHasDifferentMemcpyAndMemmove();
137# define PLATFORM_HAS_DIFFERENT_MEMCPY_AND_MEMMOVE \
138    (PlatformHasDifferentMemcpyAndMemmove())
139#else
140# define PLATFORM_HAS_DIFFERENT_MEMCPY_AND_MEMMOVE true
141#endif  // __APPLE__
142
143extern int asan_inited;
144// Used to avoid infinite recursion in __asan_init().
145extern bool asan_init_is_running;
146extern void (*death_callback)(void);
147
148#if !defined(_WIN32) || defined(__clang__)
149# define GET_CALLER_PC() (uptr)__builtin_return_address(0)
150# define GET_CURRENT_FRAME() (uptr)__builtin_frame_address(0)
151#else
152# define GET_CALLER_PC() (uptr)_ReturnAddress()
153// CaptureStackBackTrace doesn't need to know BP on Windows.
154// FIXME: This macro is still used when printing error reports though it's not
155// clear if the BP value is needed in the ASan reports on Windows.
156# define GET_CURRENT_FRAME() (uptr)0xDEADBEEF
157#endif
158
159#ifdef _WIN32
160bool WinSymbolize(const void *addr, char *out_buffer, int buffer_size);
161#endif  // _WIN32
162
163// These magic values are written to shadow for better error reporting.
164const int kAsanHeapLeftRedzoneMagic = 0xfa;
165const int kAsanHeapRightRedzoneMagic = 0xfb;
166const int kAsanHeapFreeMagic = 0xfd;
167const int kAsanStackLeftRedzoneMagic = 0xf1;
168const int kAsanStackMidRedzoneMagic = 0xf2;
169const int kAsanStackRightRedzoneMagic = 0xf3;
170const int kAsanStackPartialRedzoneMagic = 0xf4;
171const int kAsanStackAfterReturnMagic = 0xf5;
172const int kAsanInitializationOrderMagic = 0xf6;
173const int kAsanUserPoisonedMemoryMagic = 0xf7;
174const int kAsanGlobalRedzoneMagic = 0xf9;
175const int kAsanInternalHeapMagic = 0xfe;
176
177static const uptr kCurrentStackFrameMagic = 0x41B58AB3;
178static const uptr kRetiredStackFrameMagic = 0x45E0360E;
179
180}  // namespace __asan
181
182#endif  // ASAN_INTERNAL_H
183