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.
11 //
12 // Defines ASan memory mapping.
13 //===----------------------------------------------------------------------===//
14 #ifndef ASAN_MAPPING_H
15 #define ASAN_MAPPING_H
16
17 #include "asan_internal.h"
18
19 // The full explanation of the memory mapping could be found here:
20 // https://github.com/google/sanitizers/wiki/AddressSanitizerAlgorithm
21 //
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 ||
28 //
29 // When SHADOW_OFFSET is zero (-pie):
30 // || `[0x100000000000, 0x7fffffffffff]` || HighMem ||
31 // || `[0x020000000000, 0x0fffffffffff]` || HighShadow ||
32 // || `[0x000000040000, 0x01ffffffffff]` || ShadowGap ||
33 //
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 ||
45 //
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 ||
52 //
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 ||
61 //
62 // Default Linux/MIPS32 mapping:
63 // || `[0x2aaa0000, 0xffffffff]` || HighMem ||
64 // || `[0x0fff4000, 0x2aa9ffff]` || HighShadow ||
65 // || `[0x0bff4000, 0x0fff3fff]` || ShadowGap ||
66 // || `[0x0aaa0000, 0x0bff3fff]` || LowShadow ||
67 // || `[0x00000000, 0x0aa9ffff]` || LowMem ||
68 //
69 // Default Linux/MIPS64 mapping:
70 // || `[0x4000000000, 0xffffffffff]` || HighMem ||
71 // || `[0x2800000000, 0x3fffffffff]` || HighShadow ||
72 // || `[0x2400000000, 0x27ffffffff]` || ShadowGap ||
73 // || `[0x2000000000, 0x23ffffffff]` || LowShadow ||
74 // || `[0x0000000000, 0x1fffffffff]` || LowMem ||
75 //
76 // Default Linux/AArch64 (39-bit VMA) mapping:
77 // || `[0x2000000000, 0x7fffffffff]` || highmem ||
78 // || `[0x1400000000, 0x1fffffffff]` || highshadow ||
79 // || `[0x1200000000, 0x13ffffffff]` || shadowgap ||
80 // || `[0x1000000000, 0x11ffffffff]` || lowshadow ||
81 // || `[0x0000000000, 0x0fffffffff]` || lowmem ||
82 //
83 // Default Linux/AArch64 (42-bit VMA) mapping:
84 // || `[0x10000000000, 0x3ffffffffff]` || highmem ||
85 // || `[0x0a000000000, 0x0ffffffffff]` || highshadow ||
86 // || `[0x09000000000, 0x09fffffffff]` || shadowgap ||
87 // || `[0x08000000000, 0x08fffffffff]` || lowshadow ||
88 // || `[0x00000000000, 0x07fffffffff]` || lowmem ||
89 //
90 // Default Linux/S390 mapping:
91 // || `[0x30000000, 0x7fffffff]` || HighMem ||
92 // || `[0x26000000, 0x2fffffff]` || HighShadow ||
93 // || `[0x24000000, 0x25ffffff]` || ShadowGap ||
94 // || `[0x20000000, 0x23ffffff]` || LowShadow ||
95 // || `[0x00000000, 0x1fffffff]` || LowMem ||
96 //
97 // Default Linux/SystemZ mapping:
98 // || `[0x14000000000000, 0x1fffffffffffff]` || HighMem ||
99 // || `[0x12800000000000, 0x13ffffffffffff]` || HighShadow ||
100 // || `[0x12000000000000, 0x127fffffffffff]` || ShadowGap ||
101 // || `[0x10000000000000, 0x11ffffffffffff]` || LowShadow ||
102 // || `[0x00000000000000, 0x0fffffffffffff]` || LowMem ||
103 //
104 // Shadow mapping on FreeBSD/x86-64 with SHADOW_OFFSET == 0x400000000000:
105 // || `[0x500000000000, 0x7fffffffffff]` || HighMem ||
106 // || `[0x4a0000000000, 0x4fffffffffff]` || HighShadow ||
107 // || `[0x480000000000, 0x49ffffffffff]` || ShadowGap ||
108 // || `[0x400000000000, 0x47ffffffffff]` || LowShadow ||
109 // || `[0x000000000000, 0x3fffffffffff]` || LowMem ||
110 //
111 // Shadow mapping on FreeBSD/i386 with SHADOW_OFFSET == 0x40000000:
112 // || `[0x60000000, 0xffffffff]` || HighMem ||
113 // || `[0x4c000000, 0x5fffffff]` || HighShadow ||
114 // || `[0x48000000, 0x4bffffff]` || ShadowGap ||
115 // || `[0x40000000, 0x47ffffff]` || LowShadow ||
116 // || `[0x00000000, 0x3fffffff]` || LowMem ||
117 //
118 // Default Windows/i386 mapping:
119 // (the exact location of HighShadow/HighMem may vary depending
120 // on WoW64, /LARGEADDRESSAWARE, etc).
121 // || `[0x50000000, 0xffffffff]` || HighMem ||
122 // || `[0x3a000000, 0x4fffffff]` || HighShadow ||
123 // || `[0x36000000, 0x39ffffff]` || ShadowGap ||
124 // || `[0x30000000, 0x35ffffff]` || LowShadow ||
125 // || `[0x00000000, 0x2fffffff]` || LowMem ||
126
127 static const u64 kDefaultShadowScale = 3;
128 static const u64 kDefaultShadowOffset32 = 1ULL << 29; // 0x20000000
129 static const u64 kDefaultShadowOffset64 = 1ULL << 44;
130 static const u64 kDefaultShort64bitShadowOffset = 0x7FFF8000; // < 2G.
131 static const u64 kIosShadowOffset32 = 1ULL << 30; // 0x40000000
132 static const u64 kIosShadowOffset64 = 0x120200000;
133 static const u64 kIosSimShadowOffset32 = 1ULL << 30;
134 static const u64 kIosSimShadowOffset64 = kDefaultShadowOffset64;
135 static const u64 kAArch64_ShadowOffset64 = 1ULL << 36;
136 static const u64 kMIPS32_ShadowOffset32 = 0x0aaa0000;
137 static const u64 kMIPS64_ShadowOffset64 = 1ULL << 37;
138 static const u64 kPPC64_ShadowOffset64 = 1ULL << 41;
139 static const u64 kSystemZ_ShadowOffset64 = 1ULL << 52;
140 static const u64 kFreeBSD_ShadowOffset32 = 1ULL << 30; // 0x40000000
141 static const u64 kFreeBSD_ShadowOffset64 = 1ULL << 46; // 0x400000000000
142 static const u64 kWindowsShadowOffset32 = 3ULL << 28; // 0x30000000
143 static const u64 kWindowsShadowOffset64 = 1ULL << 45; // 32TB
144
145 #define SHADOW_SCALE kDefaultShadowScale
146
147
148 #if SANITIZER_WORDSIZE == 32
149 # if SANITIZER_ANDROID
150 # define SHADOW_OFFSET (0)
151 # elif defined(__mips__)
152 # define SHADOW_OFFSET kMIPS32_ShadowOffset32
153 # elif SANITIZER_FREEBSD
154 # define SHADOW_OFFSET kFreeBSD_ShadowOffset32
155 # elif SANITIZER_WINDOWS
156 # define SHADOW_OFFSET kWindowsShadowOffset32
157 # elif SANITIZER_IOS
158 # if SANITIZER_IOSSIM
159 # define SHADOW_OFFSET kIosSimShadowOffset32
160 # else
161 # define SHADOW_OFFSET kIosShadowOffset32
162 # endif
163 # else
164 # define SHADOW_OFFSET kDefaultShadowOffset32
165 # endif
166 #else
167 # if SANITIZER_IOS
168 # if SANITIZER_IOSSIM
169 # define SHADOW_OFFSET kIosSimShadowOffset64
170 # else
171 # define SHADOW_OFFSET kIosShadowOffset64
172 # endif
173 # elif defined(__aarch64__)
174 # define SHADOW_OFFSET kAArch64_ShadowOffset64
175 # elif defined(__powerpc64__)
176 # define SHADOW_OFFSET kPPC64_ShadowOffset64
177 # elif defined(__s390x__)
178 # define SHADOW_OFFSET kSystemZ_ShadowOffset64
179 # elif SANITIZER_FREEBSD
180 # define SHADOW_OFFSET kFreeBSD_ShadowOffset64
181 # elif SANITIZER_MAC
182 # define SHADOW_OFFSET kDefaultShadowOffset64
183 # elif defined(__mips64)
184 # define SHADOW_OFFSET kMIPS64_ShadowOffset64
185 # elif SANITIZER_WINDOWS64
186 # define SHADOW_OFFSET kWindowsShadowOffset64
187 # else
188 # define SHADOW_OFFSET kDefaultShort64bitShadowOffset
189 # endif
190 #endif
191
192 #define SHADOW_GRANULARITY (1ULL << SHADOW_SCALE)
193 #define MEM_TO_SHADOW(mem) (((mem) >> SHADOW_SCALE) + (SHADOW_OFFSET))
194 #define SHADOW_TO_MEM(shadow) (((shadow) - SHADOW_OFFSET) << SHADOW_SCALE)
195
196 #define kLowMemBeg 0
197 #define kLowMemEnd (SHADOW_OFFSET ? SHADOW_OFFSET - 1 : 0)
198
199 #define kLowShadowBeg SHADOW_OFFSET
200 #define kLowShadowEnd MEM_TO_SHADOW(kLowMemEnd)
201
202 #define kHighMemBeg (MEM_TO_SHADOW(kHighMemEnd) + 1)
203
204 #define kHighShadowBeg MEM_TO_SHADOW(kHighMemBeg)
205 #define kHighShadowEnd MEM_TO_SHADOW(kHighMemEnd)
206
207 # define kMidShadowBeg MEM_TO_SHADOW(kMidMemBeg)
208 # define kMidShadowEnd MEM_TO_SHADOW(kMidMemEnd)
209
210 // With the zero shadow base we can not actually map pages starting from 0.
211 // This constant is somewhat arbitrary.
212 #define kZeroBaseShadowStart 0
213 #define kZeroBaseMaxShadowStart (1 << 18)
214
215 #define kShadowGapBeg (kLowShadowEnd ? kLowShadowEnd + 1 \
216 : kZeroBaseShadowStart)
217 #define kShadowGapEnd ((kMidMemBeg ? kMidShadowBeg : kHighShadowBeg) - 1)
218
219 #define kShadowGap2Beg (kMidMemBeg ? kMidShadowEnd + 1 : 0)
220 #define kShadowGap2End (kMidMemBeg ? kMidMemBeg - 1 : 0)
221
222 #define kShadowGap3Beg (kMidMemBeg ? kMidMemEnd + 1 : 0)
223 #define kShadowGap3End (kMidMemBeg ? kHighShadowBeg - 1 : 0)
224
225 #define DO_ASAN_MAPPING_PROFILE 0 // Set to 1 to profile the functions below.
226
227 #if DO_ASAN_MAPPING_PROFILE
228 # define PROFILE_ASAN_MAPPING() AsanMappingProfile[__LINE__]++;
229 #else
230 # define PROFILE_ASAN_MAPPING()
231 #endif
232
233 // If 1, all shadow boundaries are constants.
234 // Don't set to 1 other than for testing.
235 #define ASAN_FIXED_MAPPING 0
236
237 namespace __asan {
238
239 extern uptr AsanMappingProfile[];
240
241 #if ASAN_FIXED_MAPPING
242 // Fixed mapping for 64-bit Linux. Mostly used for performance comparison
243 // with non-fixed mapping. As of r175253 (Feb 2013) the performance
244 // difference between fixed and non-fixed mapping is below the noise level.
245 static uptr kHighMemEnd = 0x7fffffffffffULL;
246 static uptr kMidMemBeg = 0x3000000000ULL;
247 static uptr kMidMemEnd = 0x4fffffffffULL;
248 #else
249 extern uptr kHighMemEnd, kMidMemBeg, kMidMemEnd; // Initialized in __asan_init.
250 #endif
251
AddrIsInLowMem(uptr a)252 static inline bool AddrIsInLowMem(uptr a) {
253 PROFILE_ASAN_MAPPING();
254 return a < kLowMemEnd;
255 }
256
AddrIsInLowShadow(uptr a)257 static inline bool AddrIsInLowShadow(uptr a) {
258 PROFILE_ASAN_MAPPING();
259 return a >= kLowShadowBeg && a <= kLowShadowEnd;
260 }
261
AddrIsInHighMem(uptr a)262 static inline bool AddrIsInHighMem(uptr a) {
263 PROFILE_ASAN_MAPPING();
264 return a >= kHighMemBeg && a <= kHighMemEnd;
265 }
266
AddrIsInMidMem(uptr a)267 static inline bool AddrIsInMidMem(uptr a) {
268 PROFILE_ASAN_MAPPING();
269 return kMidMemBeg && a >= kMidMemBeg && a <= kMidMemEnd;
270 }
271
AddrIsInMem(uptr a)272 static inline bool AddrIsInMem(uptr a) {
273 PROFILE_ASAN_MAPPING();
274 return AddrIsInLowMem(a) || AddrIsInMidMem(a) || AddrIsInHighMem(a);
275 }
276
MemToShadow(uptr p)277 static inline uptr MemToShadow(uptr p) {
278 PROFILE_ASAN_MAPPING();
279 CHECK(AddrIsInMem(p));
280 return MEM_TO_SHADOW(p);
281 }
282
AddrIsInHighShadow(uptr a)283 static inline bool AddrIsInHighShadow(uptr a) {
284 PROFILE_ASAN_MAPPING();
285 return a >= kHighShadowBeg && a <= kHighMemEnd;
286 }
287
AddrIsInMidShadow(uptr a)288 static inline bool AddrIsInMidShadow(uptr a) {
289 PROFILE_ASAN_MAPPING();
290 return kMidMemBeg && a >= kMidShadowBeg && a <= kMidMemEnd;
291 }
292
AddrIsInShadow(uptr a)293 static inline bool AddrIsInShadow(uptr a) {
294 PROFILE_ASAN_MAPPING();
295 return AddrIsInLowShadow(a) || AddrIsInMidShadow(a) || AddrIsInHighShadow(a);
296 }
297
AddrIsInShadowGap(uptr a)298 static inline bool AddrIsInShadowGap(uptr a) {
299 PROFILE_ASAN_MAPPING();
300 if (kMidMemBeg) {
301 if (a <= kShadowGapEnd)
302 return SHADOW_OFFSET == 0 || a >= kShadowGapBeg;
303 return (a >= kShadowGap2Beg && a <= kShadowGap2End) ||
304 (a >= kShadowGap3Beg && a <= kShadowGap3End);
305 }
306 // In zero-based shadow mode we treat addresses near zero as addresses
307 // in shadow gap as well.
308 if (SHADOW_OFFSET == 0)
309 return a <= kShadowGapEnd;
310 return a >= kShadowGapBeg && a <= kShadowGapEnd;
311 }
312
AddrIsAlignedByGranularity(uptr a)313 static inline bool AddrIsAlignedByGranularity(uptr a) {
314 PROFILE_ASAN_MAPPING();
315 return (a & (SHADOW_GRANULARITY - 1)) == 0;
316 }
317
AddressIsPoisoned(uptr a)318 static inline bool AddressIsPoisoned(uptr a) {
319 PROFILE_ASAN_MAPPING();
320 const uptr kAccessSize = 1;
321 u8 *shadow_address = (u8*)MEM_TO_SHADOW(a);
322 s8 shadow_value = *shadow_address;
323 if (shadow_value) {
324 u8 last_accessed_byte = (a & (SHADOW_GRANULARITY - 1))
325 + kAccessSize - 1;
326 return (last_accessed_byte >= shadow_value);
327 }
328 return false;
329 }
330
331 // Must be after all calls to PROFILE_ASAN_MAPPING().
332 static const uptr kAsanMappingProfileSize = __LINE__;
333
334 } // namespace __asan
335
336 #endif // ASAN_MAPPING_H
337