asan_internal.h revision 70e177e29c6f9ac987b65a79f6b4f3ebdabc75cc
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#define ASAN_POSIX (ASAN_LINUX || ASAN_MAC)
52
53#if __has_feature(address_sanitizer)
54# error "The AddressSanitizer run-time should not be"
55        " instrumented by AddressSanitizer"
56#endif
57
58// Build-time configuration options.
59
60// If set, asan will install its own SEGV signal handler.
61#ifndef ASAN_NEEDS_SEGV
62# define ASAN_NEEDS_SEGV 1
63#endif
64
65// If set, asan will intercept C++ exception api call(s).
66#ifndef ASAN_HAS_EXCEPTIONS
67# define ASAN_HAS_EXCEPTIONS 1
68#endif
69
70// If set, asan uses the values of SHADOW_SCALE and SHADOW_OFFSET
71// provided by the instrumented objects. Otherwise constants are used.
72#ifndef ASAN_FLEXIBLE_MAPPING_AND_OFFSET
73# define ASAN_FLEXIBLE_MAPPING_AND_OFFSET 0
74#endif
75
76// If set, values like allocator chunk size, as well as defaults for some flags
77// will be changed towards less memory overhead.
78#ifndef ASAN_LOW_MEMORY
79# define ASAN_LOW_MEMORY 0
80#endif
81
82// All internal functions in asan reside inside the __asan namespace
83// to avoid namespace collisions with the user programs.
84// Seperate namespace also makes it simpler to distinguish the asan run-time
85// functions from the instrumented user code in a profile.
86namespace __asan {
87
88class AsanThread;
89struct AsanStackTrace;
90
91// asan_rtl.cc
92void NORETURN ShowStatsAndAbort();
93
94void ReplaceOperatorsNewAndDelete();
95// asan_malloc_linux.cc / asan_malloc_mac.cc
96void ReplaceSystemMalloc();
97
98// asan_linux.cc / asan_mac.cc / asan_win.cc
99void *AsanDoesNotSupportStaticLinkage();
100
101void GetPcSpBp(void *context, uptr *pc, uptr *sp, uptr *bp);
102
103void MaybeReexec();
104bool AsanInterceptsSignal(int signum);
105void SetAlternateSignalStack();
106void UnsetAlternateSignalStack();
107void InstallSignalHandlers();
108void AsanPlatformThreadInit();
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 int asan_inited;
140// Used to avoid infinite recursion in __asan_init().
141extern bool asan_init_is_running;
142extern void (*death_callback)(void);
143
144#define ASAN_ARRAY_SIZE(a) (sizeof(a)/sizeof((a)[0]))
145
146#if !defined(_WIN32) || defined(__clang__)
147# define GET_CALLER_PC() (uptr)__builtin_return_address(0)
148# define GET_CURRENT_FRAME() (uptr)__builtin_frame_address(0)
149#else
150# define GET_CALLER_PC() (uptr)_ReturnAddress()
151// CaptureStackBackTrace doesn't need to know BP on Windows.
152// FIXME: This macro is still used when printing error reports though it's not
153// clear if the BP value is needed in the ASan reports on Windows.
154# define GET_CURRENT_FRAME() (uptr)0xDEADBEEF
155#endif
156
157#ifdef _WIN32
158bool WinSymbolize(const void *addr, char *out_buffer, int buffer_size);
159#endif  // _WIN32
160
161// These magic values are written to shadow for better error reporting.
162const int kAsanHeapLeftRedzoneMagic = 0xfa;
163const int kAsanHeapRightRedzoneMagic = 0xfb;
164const int kAsanHeapFreeMagic = 0xfd;
165const int kAsanStackLeftRedzoneMagic = 0xf1;
166const int kAsanStackMidRedzoneMagic = 0xf2;
167const int kAsanStackRightRedzoneMagic = 0xf3;
168const int kAsanStackPartialRedzoneMagic = 0xf4;
169const int kAsanStackAfterReturnMagic = 0xf5;
170const int kAsanInitializationOrderMagic = 0xf6;
171const int kAsanUserPoisonedMemoryMagic = 0xf7;
172const int kAsanGlobalRedzoneMagic = 0xf9;
173const int kAsanInternalHeapMagic = 0xfe;
174
175static const uptr kCurrentStackFrameMagic = 0x41B58AB3;
176static const uptr kRetiredStackFrameMagic = 0x45E0360E;
177
178}  // namespace __asan
179
180#endif  // ASAN_INTERNAL_H
181