asan_internal.h revision e406c8c47570659287e619e23479f9fb6640299e
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# ifdef ASAN_ANDROID
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 AsanPlatformThreadInit();
120
121// Wrapper for TLS/TSD.
122void AsanTSDInit(void (*destructor)(void *tsd));
123void *AsanTSDGet();
124void AsanTSDSet(void *tsd);
125
126void AppendToErrorMessageBuffer(const char *buffer);
127
128// asan_poisoning.cc
129// Poisons the shadow memory for "size" bytes starting from "addr".
130void PoisonShadow(uptr addr, uptr size, u8 value);
131// Poisons the shadow memory for "redzone_size" bytes starting from
132// "addr + size".
133void PoisonShadowPartialRightRedzone(uptr addr,
134                                     uptr size,
135                                     uptr redzone_size,
136                                     u8 value);
137
138// Platfrom-specific options.
139#ifdef __APPLE__
140bool PlatformHasDifferentMemcpyAndMemmove();
141# define PLATFORM_HAS_DIFFERENT_MEMCPY_AND_MEMMOVE \
142    (PlatformHasDifferentMemcpyAndMemmove())
143#else
144# define PLATFORM_HAS_DIFFERENT_MEMCPY_AND_MEMMOVE true
145#endif  // __APPLE__
146
147extern int asan_inited;
148// Used to avoid infinite recursion in __asan_init().
149extern bool asan_init_is_running;
150extern void (*death_callback)(void);
151
152// These magic values are written to shadow for better error reporting.
153const int kAsanHeapLeftRedzoneMagic = 0xfa;
154const int kAsanHeapRightRedzoneMagic = 0xfb;
155const int kAsanHeapFreeMagic = 0xfd;
156const int kAsanStackLeftRedzoneMagic = 0xf1;
157const int kAsanStackMidRedzoneMagic = 0xf2;
158const int kAsanStackRightRedzoneMagic = 0xf3;
159const int kAsanStackPartialRedzoneMagic = 0xf4;
160const int kAsanStackAfterReturnMagic = 0xf5;
161const int kAsanInitializationOrderMagic = 0xf6;
162const int kAsanUserPoisonedMemoryMagic = 0xf7;
163const int kAsanGlobalRedzoneMagic = 0xf9;
164const int kAsanInternalHeapMagic = 0xfe;
165
166static const uptr kCurrentStackFrameMagic = 0x41B58AB3;
167static const uptr kRetiredStackFrameMagic = 0x45E0360E;
168
169}  // namespace __asan
170
171#endif  // ASAN_INTERNAL_H
172