1 //===-- asan_mapping.h ------------------------------------------*- C++ -*-===//
3 // This file is distributed under the University of Illinois Open Source
4 // License. See LICENSE.TXT for details.
6 //===----------------------------------------------------------------------===//
8 // This file is a part of AddressSanitizer, an address sanity checker.
10 // Defines ASan memory mapping.
11 //===----------------------------------------------------------------------===//
12 #ifndef ASAN_MAPPING_H
13 #define ASAN_MAPPING_H
15 #include "asan_internal.h"
17 // The full explanation of the memory mapping could be found here:
18 // https://github.com/google/sanitizers/wiki/AddressSanitizerAlgorithm
20 // Typical shadow mapping on Linux/x86_64 with SHADOW_OFFSET == 0x00007fff8000:
21 // || `[0x10007fff8000, 0x7fffffffffff]` || HighMem ||
22 // || `[0x02008fff7000, 0x10007fff7fff]` || HighShadow ||
23 // || `[0x00008fff7000, 0x02008fff6fff]` || ShadowGap ||
24 // || `[0x00007fff8000, 0x00008fff6fff]` || LowShadow ||
25 // || `[0x000000000000, 0x00007fff7fff]` || LowMem ||
27 // When SHADOW_OFFSET is zero (-pie):
28 // || `[0x100000000000, 0x7fffffffffff]` || HighMem ||
29 // || `[0x020000000000, 0x0fffffffffff]` || HighShadow ||
30 // || `[0x000000040000, 0x01ffffffffff]` || ShadowGap ||
32 // Special case when something is already mapped between
33 // 0x003000000000 and 0x005000000000 (e.g. when prelink is installed):
34 // || `[0x10007fff8000, 0x7fffffffffff]` || HighMem ||
35 // || `[0x02008fff7000, 0x10007fff7fff]` || HighShadow ||
36 // || `[0x005000000000, 0x02008fff6fff]` || ShadowGap3 ||
37 // || `[0x003000000000, 0x004fffffffff]` || MidMem ||
38 // || `[0x000a7fff8000, 0x002fffffffff]` || ShadowGap2 ||
39 // || `[0x00067fff8000, 0x000a7fff7fff]` || MidShadow ||
40 // || `[0x00008fff7000, 0x00067fff7fff]` || ShadowGap ||
41 // || `[0x00007fff8000, 0x00008fff6fff]` || LowShadow ||
42 // || `[0x000000000000, 0x00007fff7fff]` || LowMem ||
44 // Default Linux/i386 mapping on x86_64 machine:
45 // || `[0x40000000, 0xffffffff]` || HighMem ||
46 // || `[0x28000000, 0x3fffffff]` || HighShadow ||
47 // || `[0x24000000, 0x27ffffff]` || ShadowGap ||
48 // || `[0x20000000, 0x23ffffff]` || LowShadow ||
49 // || `[0x00000000, 0x1fffffff]` || LowMem ||
51 // Default Linux/i386 mapping on i386 machine
52 // (addresses starting with 0xc0000000 are reserved
53 // for kernel and thus not sanitized):
54 // || `[0x38000000, 0xbfffffff]` || HighMem ||
55 // || `[0x27000000, 0x37ffffff]` || HighShadow ||
56 // || `[0x24000000, 0x26ffffff]` || ShadowGap ||
57 // || `[0x20000000, 0x23ffffff]` || LowShadow ||
58 // || `[0x00000000, 0x1fffffff]` || LowMem ||
60 // Default Linux/MIPS32 mapping:
61 // || `[0x2aaa0000, 0xffffffff]` || HighMem ||
62 // || `[0x0fff4000, 0x2aa9ffff]` || HighShadow ||
63 // || `[0x0bff4000, 0x0fff3fff]` || ShadowGap ||
64 // || `[0x0aaa0000, 0x0bff3fff]` || LowShadow ||
65 // || `[0x00000000, 0x0aa9ffff]` || LowMem ||
67 // Default Linux/MIPS64 mapping:
68 // || `[0x4000000000, 0xffffffffff]` || HighMem ||
69 // || `[0x2800000000, 0x3fffffffff]` || HighShadow ||
70 // || `[0x2400000000, 0x27ffffffff]` || ShadowGap ||
71 // || `[0x2000000000, 0x23ffffffff]` || LowShadow ||
72 // || `[0x0000000000, 0x1fffffffff]` || LowMem ||
74 // Default Linux/AArch64 (39-bit VMA) mapping:
75 // || `[0x2000000000, 0x7fffffffff]` || highmem ||
76 // || `[0x1400000000, 0x1fffffffff]` || highshadow ||
77 // || `[0x1200000000, 0x13ffffffff]` || shadowgap ||
78 // || `[0x1000000000, 0x11ffffffff]` || lowshadow ||
79 // || `[0x0000000000, 0x0fffffffff]` || lowmem ||
81 // Default Linux/AArch64 (42-bit VMA) mapping:
82 // || `[0x10000000000, 0x3ffffffffff]` || highmem ||
83 // || `[0x0a000000000, 0x0ffffffffff]` || highshadow ||
84 // || `[0x09000000000, 0x09fffffffff]` || shadowgap ||
85 // || `[0x08000000000, 0x08fffffffff]` || lowshadow ||
86 // || `[0x00000000000, 0x07fffffffff]` || lowmem ||
88 // Default Linux/S390 mapping:
89 // || `[0x30000000, 0x7fffffff]` || HighMem ||
90 // || `[0x26000000, 0x2fffffff]` || HighShadow ||
91 // || `[0x24000000, 0x25ffffff]` || ShadowGap ||
92 // || `[0x20000000, 0x23ffffff]` || LowShadow ||
93 // || `[0x00000000, 0x1fffffff]` || LowMem ||
95 // Default Linux/SystemZ mapping:
96 // || `[0x14000000000000, 0x1fffffffffffff]` || HighMem ||
97 // || `[0x12800000000000, 0x13ffffffffffff]` || HighShadow ||
98 // || `[0x12000000000000, 0x127fffffffffff]` || ShadowGap ||
99 // || `[0x10000000000000, 0x11ffffffffffff]` || LowShadow ||
100 // || `[0x00000000000000, 0x0fffffffffffff]` || LowMem ||
102 // Default Linux/SPARC64 (52-bit VMA) mapping:
103 // || `[0x8000000000000, 0xfffffffffffff]` || HighMem ||
104 // || `[0x1080000000000, 0x207ffffffffff]` || HighShadow ||
105 // || `[0x0090000000000, 0x107ffffffffff]` || ShadowGap ||
106 // || `[0x0080000000000, 0x008ffffffffff]` || LowShadow ||
107 // || `[0x0000000000000, 0x007ffffffffff]` || LowMem ||
109 // Shadow mapping on FreeBSD/x86-64 with SHADOW_OFFSET == 0x400000000000:
110 // || `[0x500000000000, 0x7fffffffffff]` || HighMem ||
111 // || `[0x4a0000000000, 0x4fffffffffff]` || HighShadow ||
112 // || `[0x480000000000, 0x49ffffffffff]` || ShadowGap ||
113 // || `[0x400000000000, 0x47ffffffffff]` || LowShadow ||
114 // || `[0x000000000000, 0x3fffffffffff]` || LowMem ||
116 // Shadow mapping on FreeBSD/i386 with SHADOW_OFFSET == 0x40000000:
117 // || `[0x60000000, 0xffffffff]` || HighMem ||
118 // || `[0x4c000000, 0x5fffffff]` || HighShadow ||
119 // || `[0x48000000, 0x4bffffff]` || ShadowGap ||
120 // || `[0x40000000, 0x47ffffff]` || LowShadow ||
121 // || `[0x00000000, 0x3fffffff]` || LowMem ||
123 // Shadow mapping on NetBSD/x86-64 with SHADOW_OFFSET == 0x400000000000:
124 // || `[0x4feffffffe01, 0x7f7ffffff000]` || HighMem ||
125 // || `[0x49fdffffffc0, 0x4feffffffe00]` || HighShadow ||
126 // || `[0x480000000000, 0x49fdffffffbf]` || ShadowGap ||
127 // || `[0x400000000000, 0x47ffffffffff]` || LowShadow ||
128 // || `[0x000000000000, 0x3fffffffffff]` || LowMem ||
130 // Shadow mapping on NetBSD/i386 with SHADOW_OFFSET == 0x40000000:
131 // || `[0x60000000, 0xfffff000]` || HighMem ||
132 // || `[0x4c000000, 0x5fffffff]` || HighShadow ||
133 // || `[0x48000000, 0x4bffffff]` || ShadowGap ||
134 // || `[0x40000000, 0x47ffffff]` || LowShadow ||
135 // || `[0x00000000, 0x3fffffff]` || LowMem ||
137 // Default Windows/i386 mapping:
138 // (the exact location of HighShadow/HighMem may vary depending
139 // on WoW64, /LARGEADDRESSAWARE, etc).
140 // || `[0x50000000, 0xffffffff]` || HighMem ||
141 // || `[0x3a000000, 0x4fffffff]` || HighShadow ||
142 // || `[0x36000000, 0x39ffffff]` || ShadowGap ||
143 // || `[0x30000000, 0x35ffffff]` || LowShadow ||
144 // || `[0x00000000, 0x2fffffff]` || LowMem ||
146 // Shadow mapping on Myriad2 (for shadow scale 5):
147 // || `[0x9ff80000, 0x9fffffff]` || ShadowGap ||
148 // || `[0x9f000000, 0x9ff7ffff]` || LowShadow ||
149 // || `[0x80000000, 0x9effffff]` || LowMem ||
150 // || `[0x00000000, 0x7fffffff]` || Ignored ||
152 #if defined(ASAN_SHADOW_SCALE)
153 static const u64 kDefaultShadowScale
= ASAN_SHADOW_SCALE
;
155 static const u64 kDefaultShadowScale
= SANITIZER_MYRIAD2
? 5 : 3;
157 static const u64 kDefaultShadowSentinel
= ~(uptr
)0;
158 static const u64 kDefaultShadowOffset32
= 1ULL << 29; // 0x20000000
159 static const u64 kDefaultShadowOffset64
= 1ULL << 44;
160 static const u64 kDefaultShort64bitShadowOffset
=
161 0x7FFFFFFF & (~0xFFFULL
<< kDefaultShadowScale
); // < 2G.
162 static const u64 kIosShadowOffset32
= 1ULL << 30; // 0x40000000
163 static const u64 kIosShadowOffset64
= 0x120200000;
164 static const u64 kIosSimShadowOffset32
= 1ULL << 30;
165 static const u64 kIosSimShadowOffset64
= kDefaultShadowOffset64
;
166 static const u64 kAArch64_ShadowOffset64
= 1ULL << 36;
167 static const u64 kMIPS32_ShadowOffset32
= 0x0aaa0000;
168 static const u64 kMIPS64_ShadowOffset64
= 1ULL << 37;
169 static const u64 kPPC64_ShadowOffset64
= 1ULL << 41;
170 static const u64 kSystemZ_ShadowOffset64
= 1ULL << 52;
171 static const u64 kSPARC64_ShadowOffset64
= 1ULL << 43; // 0x80000000000
172 static const u64 kFreeBSD_ShadowOffset32
= 1ULL << 30; // 0x40000000
173 static const u64 kFreeBSD_ShadowOffset64
= 1ULL << 46; // 0x400000000000
174 static const u64 kNetBSD_ShadowOffset32
= 1ULL << 30; // 0x40000000
175 static const u64 kNetBSD_ShadowOffset64
= 1ULL << 46; // 0x400000000000
176 static const u64 kWindowsShadowOffset32
= 3ULL << 28; // 0x30000000
178 static const u64 kMyriadMemoryOffset32
= 0x80000000ULL
;
179 static const u64 kMyriadMemorySize32
= 0x20000000ULL
;
180 static const u64 kMyriadMemoryEnd32
=
181 kMyriadMemoryOffset32
+ kMyriadMemorySize32
- 1;
182 static const u64 kMyriadShadowOffset32
=
183 (kMyriadMemoryOffset32
+ kMyriadMemorySize32
-
184 (kMyriadMemorySize32
>> kDefaultShadowScale
));
185 static const u64 kMyriadCacheBitMask32
= 0x40000000ULL
;
187 #define SHADOW_SCALE kDefaultShadowScale
189 #if SANITIZER_FUCHSIA
190 # define SHADOW_OFFSET (0)
191 #elif SANITIZER_WORDSIZE == 32
192 # if SANITIZER_ANDROID
193 # define SHADOW_OFFSET __asan_shadow_memory_dynamic_address
194 # elif defined(__mips__)
195 # define SHADOW_OFFSET kMIPS32_ShadowOffset32
196 # elif SANITIZER_FREEBSD
197 # define SHADOW_OFFSET kFreeBSD_ShadowOffset32
198 # elif SANITIZER_NETBSD
199 # define SHADOW_OFFSET kNetBSD_ShadowOffset32
200 # elif SANITIZER_WINDOWS
201 # define SHADOW_OFFSET kWindowsShadowOffset32
203 # if SANITIZER_IOSSIM
204 # define SHADOW_OFFSET kIosSimShadowOffset32
206 # define SHADOW_OFFSET kIosShadowOffset32
208 # elif SANITIZER_MYRIAD2
209 # define SHADOW_OFFSET kMyriadShadowOffset32
211 # define SHADOW_OFFSET kDefaultShadowOffset32
215 # if SANITIZER_IOSSIM
216 # define SHADOW_OFFSET kIosSimShadowOffset64
218 # define SHADOW_OFFSET __asan_shadow_memory_dynamic_address
220 # elif defined(__aarch64__)
221 # define SHADOW_OFFSET kAArch64_ShadowOffset64
222 # elif defined(__powerpc64__)
223 # define SHADOW_OFFSET kPPC64_ShadowOffset64
224 # elif defined(__s390x__)
225 # define SHADOW_OFFSET kSystemZ_ShadowOffset64
226 # elif SANITIZER_FREEBSD
227 # define SHADOW_OFFSET kFreeBSD_ShadowOffset64
228 # elif SANITIZER_NETBSD
229 # define SHADOW_OFFSET kNetBSD_ShadowOffset64
231 # define SHADOW_OFFSET kDefaultShadowOffset64
232 # elif defined(__mips64)
233 # define SHADOW_OFFSET kMIPS64_ShadowOffset64
234 # elif defined(__sparc__)
235 # define SHADOW_OFFSET kSPARC64_ShadowOffset64
236 # elif SANITIZER_WINDOWS64
237 # define SHADOW_OFFSET __asan_shadow_memory_dynamic_address
239 # define SHADOW_OFFSET kDefaultShort64bitShadowOffset
243 #if SANITIZER_ANDROID && defined(__arm__)
244 # define ASAN_PREMAP_SHADOW 1
246 # define ASAN_PREMAP_SHADOW 0
249 #define SHADOW_GRANULARITY (1ULL << SHADOW_SCALE)
251 #define DO_ASAN_MAPPING_PROFILE 0 // Set to 1 to profile the functions below.
253 #if DO_ASAN_MAPPING_PROFILE
254 # define PROFILE_ASAN_MAPPING() AsanMappingProfile[__LINE__]++;
256 # define PROFILE_ASAN_MAPPING()
259 // If 1, all shadow boundaries are constants.
260 // Don't set to 1 other than for testing.
261 #define ASAN_FIXED_MAPPING 0
265 extern uptr AsanMappingProfile
[];
267 #if ASAN_FIXED_MAPPING
268 // Fixed mapping for 64-bit Linux. Mostly used for performance comparison
269 // with non-fixed mapping. As of r175253 (Feb 2013) the performance
270 // difference between fixed and non-fixed mapping is below the noise level.
271 static uptr kHighMemEnd
= 0x7fffffffffffULL
;
272 static uptr kMidMemBeg
= 0x3000000000ULL
;
273 static uptr kMidMemEnd
= 0x4fffffffffULL
;
275 extern uptr kHighMemEnd
, kMidMemBeg
, kMidMemEnd
; // Initialized in __asan_init.
278 } // namespace __asan
280 #if SANITIZER_MYRIAD2
281 #include "asan_mapping_myriad.h"
282 #elif defined(__sparc__) && SANITIZER_WORDSIZE == 64
283 #include "asan_mapping_sparc64.h"
285 #define MEM_TO_SHADOW(mem) (((mem) >> SHADOW_SCALE) + (SHADOW_OFFSET))
288 #define kLowMemEnd (SHADOW_OFFSET ? SHADOW_OFFSET - 1 : 0)
290 #define kLowShadowBeg SHADOW_OFFSET
291 #define kLowShadowEnd MEM_TO_SHADOW(kLowMemEnd)
293 #define kHighMemBeg (MEM_TO_SHADOW(kHighMemEnd) + 1)
295 #define kHighShadowBeg MEM_TO_SHADOW(kHighMemBeg)
296 #define kHighShadowEnd MEM_TO_SHADOW(kHighMemEnd)
298 # define kMidShadowBeg MEM_TO_SHADOW(kMidMemBeg)
299 # define kMidShadowEnd MEM_TO_SHADOW(kMidMemEnd)
301 // With the zero shadow base we can not actually map pages starting from 0.
302 // This constant is somewhat arbitrary.
303 #define kZeroBaseShadowStart 0
304 #define kZeroBaseMaxShadowStart (1 << 18)
306 #define kShadowGapBeg (kLowShadowEnd ? kLowShadowEnd + 1 \
307 : kZeroBaseShadowStart)
308 #define kShadowGapEnd ((kMidMemBeg ? kMidShadowBeg : kHighShadowBeg) - 1)
310 #define kShadowGap2Beg (kMidMemBeg ? kMidShadowEnd + 1 : 0)
311 #define kShadowGap2End (kMidMemBeg ? kMidMemBeg - 1 : 0)
313 #define kShadowGap3Beg (kMidMemBeg ? kMidMemEnd + 1 : 0)
314 #define kShadowGap3End (kMidMemBeg ? kHighShadowBeg - 1 : 0)
318 static inline bool AddrIsInLowMem(uptr a
) {
319 PROFILE_ASAN_MAPPING();
320 return a
<= kLowMemEnd
;
323 static inline bool AddrIsInLowShadow(uptr a
) {
324 PROFILE_ASAN_MAPPING();
325 return a
>= kLowShadowBeg
&& a
<= kLowShadowEnd
;
328 static inline bool AddrIsInMidMem(uptr a
) {
329 PROFILE_ASAN_MAPPING();
330 return kMidMemBeg
&& a
>= kMidMemBeg
&& a
<= kMidMemEnd
;
333 static inline bool AddrIsInMidShadow(uptr a
) {
334 PROFILE_ASAN_MAPPING();
335 return kMidMemBeg
&& a
>= kMidShadowBeg
&& a
<= kMidShadowEnd
;
338 static inline bool AddrIsInHighMem(uptr a
) {
339 PROFILE_ASAN_MAPPING();
340 return kHighMemBeg
&& a
>= kHighMemBeg
&& a
<= kHighMemEnd
;
343 static inline bool AddrIsInHighShadow(uptr a
) {
344 PROFILE_ASAN_MAPPING();
345 return kHighMemBeg
&& a
>= kHighShadowBeg
&& a
<= kHighShadowEnd
;
348 static inline bool AddrIsInShadowGap(uptr a
) {
349 PROFILE_ASAN_MAPPING();
351 if (a
<= kShadowGapEnd
)
352 return SHADOW_OFFSET
== 0 || a
>= kShadowGapBeg
;
353 return (a
>= kShadowGap2Beg
&& a
<= kShadowGap2End
) ||
354 (a
>= kShadowGap3Beg
&& a
<= kShadowGap3End
);
356 // In zero-based shadow mode we treat addresses near zero as addresses
357 // in shadow gap as well.
358 if (SHADOW_OFFSET
== 0)
359 return a
<= kShadowGapEnd
;
360 return a
>= kShadowGapBeg
&& a
<= kShadowGapEnd
;
363 } // namespace __asan
365 #endif // SANITIZER_MYRIAD2
369 static inline bool AddrIsInMem(uptr a
) {
370 PROFILE_ASAN_MAPPING();
371 return AddrIsInLowMem(a
) || AddrIsInMidMem(a
) || AddrIsInHighMem(a
) ||
372 (flags()->protect_shadow_gap
== 0 && AddrIsInShadowGap(a
));
375 static inline uptr
MemToShadow(uptr p
) {
376 PROFILE_ASAN_MAPPING();
377 CHECK(AddrIsInMem(p
));
378 return MEM_TO_SHADOW(p
);
381 static inline bool AddrIsInShadow(uptr a
) {
382 PROFILE_ASAN_MAPPING();
383 return AddrIsInLowShadow(a
) || AddrIsInMidShadow(a
) || AddrIsInHighShadow(a
);
386 static inline bool AddrIsAlignedByGranularity(uptr a
) {
387 PROFILE_ASAN_MAPPING();
388 return (a
& (SHADOW_GRANULARITY
- 1)) == 0;
391 static inline bool AddressIsPoisoned(uptr a
) {
392 PROFILE_ASAN_MAPPING();
393 if (SANITIZER_MYRIAD2
&& !AddrIsInMem(a
) && !AddrIsInShadow(a
))
395 const uptr kAccessSize
= 1;
396 u8
*shadow_address
= (u8
*)MEM_TO_SHADOW(a
);
397 s8 shadow_value
= *shadow_address
;
399 u8 last_accessed_byte
= (a
& (SHADOW_GRANULARITY
- 1))
401 return (last_accessed_byte
>= shadow_value
);
406 // Must be after all calls to PROFILE_ASAN_MAPPING().
407 static const uptr kAsanMappingProfileSize
= __LINE__
;
409 } // namespace __asan
411 #endif // ASAN_MAPPING_H