[ASan/Win] Add DLL thunks for the fake stack interface
[blocksruntime.git] / lib / asan / asan_mapping.h
blob8941475a9b7c4c9e0d1c6c152046ef2359f87ebd
1 //===-- asan_mapping.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.
12 // Defines ASan memory mapping.
13 //===----------------------------------------------------------------------===//
14 #ifndef ASAN_MAPPING_H
15 #define ASAN_MAPPING_H
17 #include "asan_internal.h"
19 // The full explanation of the memory mapping could be found here:
20 // http://code.google.com/p/address-sanitizer/wiki/AddressSanitizerAlgorithm
22 // Typical shadow mapping on Linux/x86_64 with SHADOW_OFFSET == 0x00007fff8000:
23 // || `[0x10007fff8000, 0x7fffffffffff]` || HighMem ||
24 // || `[0x02008fff7000, 0x10007fff7fff]` || HighShadow ||
25 // || `[0x00008fff7000, 0x02008fff6fff]` || ShadowGap ||
26 // || `[0x00007fff8000, 0x00008fff6fff]` || LowShadow ||
27 // || `[0x000000000000, 0x00007fff7fff]` || LowMem ||
29 // When SHADOW_OFFSET is zero (-pie):
30 // || `[0x100000000000, 0x7fffffffffff]` || HighMem ||
31 // || `[0x020000000000, 0x0fffffffffff]` || HighShadow ||
32 // || `[0x000000040000, 0x01ffffffffff]` || ShadowGap ||
34 // Special case when something is already mapped between
35 // 0x003000000000 and 0x005000000000 (e.g. when prelink is installed):
36 // || `[0x10007fff8000, 0x7fffffffffff]` || HighMem ||
37 // || `[0x02008fff7000, 0x10007fff7fff]` || HighShadow ||
38 // || `[0x005000000000, 0x02008fff6fff]` || ShadowGap3 ||
39 // || `[0x003000000000, 0x004fffffffff]` || MidMem ||
40 // || `[0x000a7fff8000, 0x002fffffffff]` || ShadowGap2 ||
41 // || `[0x00067fff8000, 0x000a7fff7fff]` || MidShadow ||
42 // || `[0x00008fff7000, 0x00067fff7fff]` || ShadowGap ||
43 // || `[0x00007fff8000, 0x00008fff6fff]` || LowShadow ||
44 // || `[0x000000000000, 0x00007fff7fff]` || LowMem ||
46 // Default Linux/i386 mapping on x86_64 machine:
47 // || `[0x40000000, 0xffffffff]` || HighMem ||
48 // || `[0x28000000, 0x3fffffff]` || HighShadow ||
49 // || `[0x24000000, 0x27ffffff]` || ShadowGap ||
50 // || `[0x20000000, 0x23ffffff]` || LowShadow ||
51 // || `[0x00000000, 0x1fffffff]` || LowMem ||
53 // Default Linux/i386 mapping on i386 machine
54 // (addresses starting with 0xc0000000 are reserved
55 // for kernel and thus not sanitized):
56 // || `[0x38000000, 0xbfffffff]` || HighMem ||
57 // || `[0x27000000, 0x37ffffff]` || HighShadow ||
58 // || `[0x24000000, 0x26ffffff]` || ShadowGap ||
59 // || `[0x20000000, 0x23ffffff]` || LowShadow ||
60 // || `[0x00000000, 0x1fffffff]` || LowMem ||
62 // Default Linux/MIPS mapping:
63 // || `[0x2aaa8000, 0xffffffff]` || HighMem ||
64 // || `[0x0fffd000, 0x2aaa7fff]` || HighShadow ||
65 // || `[0x0bffd000, 0x0fffcfff]` || ShadowGap ||
66 // || `[0x0aaa8000, 0x0bffcfff]` || LowShadow ||
67 // || `[0x00000000, 0x0aaa7fff]` || LowMem ||
69 static const u64 kDefaultShadowScale = 3;
70 static const u64 kDefaultShadowOffset32 = 1ULL << 29; // 0x20000000
71 static const u64 kIosShadowOffset32 = 1ULL << 30; // 0x40000000
72 static const u64 kDefaultShadowOffset64 = 1ULL << 44;
73 static const u64 kDefaultShort64bitShadowOffset = 0x7FFF8000; // < 2G.
74 static const u64 kAArch64_ShadowOffset64 = 1ULL << 36;
75 static const u64 kMIPS32_ShadowOffset32 = 0x0aaa8000;
77 #define SHADOW_SCALE kDefaultShadowScale
78 #if SANITIZER_ANDROID
79 # define SHADOW_OFFSET (0)
80 #else
81 # if SANITIZER_WORDSIZE == 32
82 # if defined(__mips__)
83 # define SHADOW_OFFSET kMIPS32_ShadowOffset32
84 # else
85 # if SANITIZER_IOS
86 # define SHADOW_OFFSET kIosShadowOffset32
87 # else
88 # define SHADOW_OFFSET kDefaultShadowOffset32
89 # endif
90 # endif
91 # else
92 # if defined(__aarch64__)
93 # define SHADOW_OFFSET kAArch64_ShadowOffset64
94 # elif SANITIZER_MAC
95 # define SHADOW_OFFSET kDefaultShadowOffset64
96 # else
97 # define SHADOW_OFFSET kDefaultShort64bitShadowOffset
98 # endif
99 # endif
100 #endif
102 #define SHADOW_GRANULARITY (1ULL << SHADOW_SCALE)
103 #define MEM_TO_SHADOW(mem) (((mem) >> SHADOW_SCALE) + (SHADOW_OFFSET))
104 #define SHADOW_TO_MEM(shadow) (((shadow) - SHADOW_OFFSET) << SHADOW_SCALE)
106 #define kLowMemBeg 0
107 #define kLowMemEnd (SHADOW_OFFSET ? SHADOW_OFFSET - 1 : 0)
109 #define kLowShadowBeg SHADOW_OFFSET
110 #define kLowShadowEnd MEM_TO_SHADOW(kLowMemEnd)
112 #define kHighMemBeg (MEM_TO_SHADOW(kHighMemEnd) + 1)
114 #define kHighShadowBeg MEM_TO_SHADOW(kHighMemBeg)
115 #define kHighShadowEnd MEM_TO_SHADOW(kHighMemEnd)
117 # define kMidShadowBeg MEM_TO_SHADOW(kMidMemBeg)
118 # define kMidShadowEnd MEM_TO_SHADOW(kMidMemEnd)
120 // With the zero shadow base we can not actually map pages starting from 0.
121 // This constant is somewhat arbitrary.
122 #define kZeroBaseShadowStart (1 << 18)
124 #define kShadowGapBeg (kLowShadowEnd ? kLowShadowEnd + 1 \
125 : kZeroBaseShadowStart)
126 #define kShadowGapEnd ((kMidMemBeg ? kMidShadowBeg : kHighShadowBeg) - 1)
128 #define kShadowGap2Beg (kMidMemBeg ? kMidShadowEnd + 1 : 0)
129 #define kShadowGap2End (kMidMemBeg ? kMidMemBeg - 1 : 0)
131 #define kShadowGap3Beg (kMidMemBeg ? kMidMemEnd + 1 : 0)
132 #define kShadowGap3End (kMidMemBeg ? kHighShadowBeg - 1 : 0)
134 #define DO_ASAN_MAPPING_PROFILE 0 // Set to 1 to profile the functions below.
136 #if DO_ASAN_MAPPING_PROFILE
137 # define PROFILE_ASAN_MAPPING() AsanMappingProfile[__LINE__]++;
138 #else
139 # define PROFILE_ASAN_MAPPING()
140 #endif
142 // If 1, all shadow boundaries are constants.
143 // Don't set to 1 other than for testing.
144 #define ASAN_FIXED_MAPPING 0
146 namespace __asan {
148 extern uptr AsanMappingProfile[];
150 #if ASAN_FIXED_MAPPING
151 // Fixed mapping for 64-bit Linux. Mostly used for performance comparison
152 // with non-fixed mapping. As of r175253 (Feb 2013) the performance
153 // difference between fixed and non-fixed mapping is below the noise level.
154 static uptr kHighMemEnd = 0x7fffffffffffULL;
155 static uptr kMidMemBeg = 0x3000000000ULL;
156 static uptr kMidMemEnd = 0x4fffffffffULL;
157 #else
158 extern uptr kHighMemEnd, kMidMemBeg, kMidMemEnd; // Initialized in __asan_init.
159 #endif
161 static inline bool AddrIsInLowMem(uptr a) {
162 PROFILE_ASAN_MAPPING();
163 return a < kLowMemEnd;
166 static inline bool AddrIsInLowShadow(uptr a) {
167 PROFILE_ASAN_MAPPING();
168 return a >= kLowShadowBeg && a <= kLowShadowEnd;
171 static inline bool AddrIsInHighMem(uptr a) {
172 PROFILE_ASAN_MAPPING();
173 return a >= kHighMemBeg && a <= kHighMemEnd;
176 static inline bool AddrIsInMidMem(uptr a) {
177 PROFILE_ASAN_MAPPING();
178 return kMidMemBeg && a >= kMidMemBeg && a <= kMidMemEnd;
181 static inline bool AddrIsInMem(uptr a) {
182 PROFILE_ASAN_MAPPING();
183 return AddrIsInLowMem(a) || AddrIsInMidMem(a) || AddrIsInHighMem(a);
186 static inline uptr MemToShadow(uptr p) {
187 PROFILE_ASAN_MAPPING();
188 CHECK(AddrIsInMem(p));
189 return MEM_TO_SHADOW(p);
192 static inline bool AddrIsInHighShadow(uptr a) {
193 PROFILE_ASAN_MAPPING();
194 return a >= kHighShadowBeg && a <= kHighMemEnd;
197 static inline bool AddrIsInMidShadow(uptr a) {
198 PROFILE_ASAN_MAPPING();
199 return kMidMemBeg && a >= kMidShadowBeg && a <= kMidMemEnd;
202 static inline bool AddrIsInShadow(uptr a) {
203 PROFILE_ASAN_MAPPING();
204 return AddrIsInLowShadow(a) || AddrIsInMidShadow(a) || AddrIsInHighShadow(a);
207 static inline bool AddrIsInShadowGap(uptr a) {
208 PROFILE_ASAN_MAPPING();
209 if (kMidMemBeg) {
210 if (a <= kShadowGapEnd)
211 return SHADOW_OFFSET == 0 || a >= kShadowGapBeg;
212 return (a >= kShadowGap2Beg && a <= kShadowGap2End) ||
213 (a >= kShadowGap3Beg && a <= kShadowGap3End);
215 // In zero-based shadow mode we treat addresses near zero as addresses
216 // in shadow gap as well.
217 if (SHADOW_OFFSET == 0)
218 return a <= kShadowGapEnd;
219 return a >= kShadowGapBeg && a <= kShadowGapEnd;
222 static inline bool AddrIsAlignedByGranularity(uptr a) {
223 PROFILE_ASAN_MAPPING();
224 return (a & (SHADOW_GRANULARITY - 1)) == 0;
227 static inline bool AddressIsPoisoned(uptr a) {
228 PROFILE_ASAN_MAPPING();
229 const uptr kAccessSize = 1;
230 u8 *shadow_address = (u8*)MEM_TO_SHADOW(a);
231 s8 shadow_value = *shadow_address;
232 if (shadow_value) {
233 u8 last_accessed_byte = (a & (SHADOW_GRANULARITY - 1))
234 + kAccessSize - 1;
235 return (last_accessed_byte >= shadow_value);
237 return false;
240 // Must be after all calls to PROFILE_ASAN_MAPPING().
241 static const uptr kAsanMappingProfileSize = __LINE__;
243 } // namespace __asan
245 #endif // ASAN_MAPPING_H