asan_internal.h revision 57db4bae483e7268dd7fc5bc42b0e55564014048
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_stacktrace.h"
21#include "sanitizer_common/sanitizer_libc.h"
22
23#if !defined(__linux__) && !defined(__APPLE__) && !defined(_WIN32)
24# error "This operating system is not supported by AddressSanitizer"
25#endif
26
27#define ASAN_DEFAULT_FAILURE_EXITCODE 1
28
29#if defined(__linux__)
30# define ASAN_LINUX   1
31#else
32# define ASAN_LINUX   0
33#endif
34
35#if defined(__APPLE__)
36# define ASAN_MAC     1
37#else
38# define ASAN_MAC     0
39#endif
40
41#if defined(_WIN32)
42# define ASAN_WINDOWS 1
43#else
44# define ASAN_WINDOWS 0
45#endif
46
47#if defined(__ANDROID__) || defined(ANDROID)
48# define ASAN_ANDROID 1
49#else
50# define ASAN_ANDROID 0
51#endif
52
53
54#define ASAN_POSIX (ASAN_LINUX || ASAN_MAC)
55
56#if __has_feature(address_sanitizer)
57# error "The AddressSanitizer run-time should not be"
58        " instrumented by AddressSanitizer"
59#endif
60
61// Build-time configuration options.
62
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
67# else
68#  define ASAN_NEEDS_SEGV 1
69# endif
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#if SANITIZER_WORDSIZE == 32
87#  define ASAN_LOW_MEMORY 1
88#else
89#  define ASAN_LOW_MEMORY 0
90# endif
91#endif
92
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.
97namespace __asan {
98
99class AsanThread;
100using __sanitizer::StackTrace;
101
102// asan_rtl.cc
103void NORETURN ShowStatsAndAbort();
104
105void ReplaceOperatorsNewAndDelete();
106// asan_malloc_linux.cc / asan_malloc_mac.cc
107void ReplaceSystemMalloc();
108
109// asan_linux.cc / asan_mac.cc / asan_win.cc
110void *AsanDoesNotSupportStaticLinkage();
111
112void GetPcSpBp(void *context, uptr *pc, uptr *sp, uptr *bp);
113
114void MaybeReexec();
115bool AsanInterceptsSignal(int signum);
116void SetAlternateSignalStack();
117void UnsetAlternateSignalStack();
118void InstallSignalHandlers();
119void ReadContextStack(void *context, uptr *stack, uptr *ssize);
120void AsanPlatformThreadInit();
121
122// Wrapper for TLS/TSD.
123void AsanTSDInit(void (*destructor)(void *tsd));
124void *AsanTSDGet();
125void AsanTSDSet(void *tsd);
126
127void AppendToErrorMessageBuffer(const char *buffer);
128
129// asan_poisoning.cc
130// Poisons the shadow memory for "size" bytes starting from "addr".
131void PoisonShadow(uptr addr, uptr size, u8 value);
132// Poisons the shadow memory for "redzone_size" bytes starting from
133// "addr + size".
134void PoisonShadowPartialRightRedzone(uptr addr,
135                                     uptr size,
136                                     uptr redzone_size,
137                                     u8 value);
138
139// Platfrom-specific options.
140#ifdef __APPLE__
141bool PlatformHasDifferentMemcpyAndMemmove();
142# define PLATFORM_HAS_DIFFERENT_MEMCPY_AND_MEMMOVE \
143    (PlatformHasDifferentMemcpyAndMemmove())
144#else
145# define PLATFORM_HAS_DIFFERENT_MEMCPY_AND_MEMMOVE true
146#endif  // __APPLE__
147
148// Add convenient macro for interface functions that may be represented as
149// weak hooks.
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()
156
157extern int asan_inited;
158// Used to avoid infinite recursion in __asan_init().
159extern bool asan_init_is_running;
160extern void (*death_callback)(void);
161
162// These magic values are written to shadow for better error reporting.
163const int kAsanHeapLeftRedzoneMagic = 0xfa;
164const int kAsanHeapRightRedzoneMagic = 0xfb;
165const int kAsanHeapFreeMagic = 0xfd;
166const int kAsanStackLeftRedzoneMagic = 0xf1;
167const int kAsanStackMidRedzoneMagic = 0xf2;
168const int kAsanStackRightRedzoneMagic = 0xf3;
169const int kAsanStackPartialRedzoneMagic = 0xf4;
170const int kAsanStackAfterReturnMagic = 0xf5;
171const int kAsanInitializationOrderMagic = 0xf6;
172const int kAsanUserPoisonedMemoryMagic = 0xf7;
173const int kAsanStackUseAfterScopeMagic = 0xf8;
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