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 // Shadow mapping on FreeBSD/x86-64 with SHADOW_OFFSET == 0x400000000000:
103 // || `[0x500000000000, 0x7fffffffffff]` || HighMem ||
104 // || `[0x4a0000000000, 0x4fffffffffff]` || HighShadow ||
105 // || `[0x480000000000, 0x49ffffffffff]` || ShadowGap ||
106 // || `[0x400000000000, 0x47ffffffffff]` || LowShadow ||
107 // || `[0x000000000000, 0x3fffffffffff]` || LowMem ||
109 // Shadow mapping on FreeBSD/i386 with SHADOW_OFFSET == 0x40000000:
110 // || `[0x60000000, 0xffffffff]` || HighMem ||
111 // || `[0x4c000000, 0x5fffffff]` || HighShadow ||
112 // || `[0x48000000, 0x4bffffff]` || ShadowGap ||
113 // || `[0x40000000, 0x47ffffff]` || LowShadow ||
114 // || `[0x00000000, 0x3fffffff]` || LowMem ||
116 // Shadow mapping on NetBSD/x86-64 with SHADOW_OFFSET == 0x400000000000:
117 // || `[0x4feffffffe01, 0x7f7ffffff000]` || HighMem ||
118 // || `[0x49fdffffffc0, 0x4feffffffe00]` || HighShadow ||
119 // || `[0x480000000000, 0x49fdffffffbf]` || ShadowGap ||
120 // || `[0x400000000000, 0x47ffffffffff]` || LowShadow ||
121 // || `[0x000000000000, 0x3fffffffffff]` || LowMem ||
123 // Default Windows/i386 mapping:
124 // (the exact location of HighShadow/HighMem may vary depending
125 // on WoW64, /LARGEADDRESSAWARE, etc).
126 // || `[0x50000000, 0xffffffff]` || HighMem ||
127 // || `[0x3a000000, 0x4fffffff]` || HighShadow ||
128 // || `[0x36000000, 0x39ffffff]` || ShadowGap ||
129 // || `[0x30000000, 0x35ffffff]` || LowShadow ||
130 // || `[0x00000000, 0x2fffffff]` || LowMem ||
132 static const u64 kDefaultShadowScale
= 3;
133 static const u64 kDefaultShadowSentinel
= ~(uptr
)0;
134 static const u64 kDefaultShadowOffset32
= 1ULL << 29; // 0x20000000
135 static const u64 kDefaultShadowOffset64
= 1ULL << 44;
136 static const u64 kDefaultShort64bitShadowOffset
= 0x7FFF8000; // < 2G.
137 static const u64 kIosShadowOffset32
= 1ULL << 30; // 0x40000000
138 static const u64 kIosShadowOffset64
= 0x120200000;
139 static const u64 kIosSimShadowOffset32
= 1ULL << 30;
140 static const u64 kIosSimShadowOffset64
= kDefaultShadowOffset64
;
141 static const u64 kAArch64_ShadowOffset64
= 1ULL << 36;
142 static const u64 kMIPS32_ShadowOffset32
= 0x0aaa0000;
143 static const u64 kMIPS64_ShadowOffset64
= 1ULL << 37;
144 static const u64 kPPC64_ShadowOffset64
= 1ULL << 41;
145 static const u64 kSystemZ_ShadowOffset64
= 1ULL << 52;
146 static const u64 kFreeBSD_ShadowOffset32
= 1ULL << 30; // 0x40000000
147 static const u64 kFreeBSD_ShadowOffset64
= 1ULL << 46; // 0x400000000000
148 static const u64 kNetBSD_ShadowOffset64
= 1ULL << 46; // 0x400000000000
149 static const u64 kWindowsShadowOffset32
= 3ULL << 28; // 0x30000000
151 #define SHADOW_SCALE kDefaultShadowScale
153 #if SANITIZER_FUCHSIA
154 # define SHADOW_OFFSET (0)
155 #elif SANITIZER_WORDSIZE == 32
156 # if SANITIZER_ANDROID
157 # define SHADOW_OFFSET (0)
158 # elif defined(__mips__)
159 # define SHADOW_OFFSET kMIPS32_ShadowOffset32
160 # elif SANITIZER_FREEBSD
161 # define SHADOW_OFFSET kFreeBSD_ShadowOffset32
162 # elif SANITIZER_WINDOWS
163 # define SHADOW_OFFSET kWindowsShadowOffset32
165 # if SANITIZER_IOSSIM
166 # define SHADOW_OFFSET kIosSimShadowOffset32
168 # define SHADOW_OFFSET kIosShadowOffset32
171 # define SHADOW_OFFSET kDefaultShadowOffset32
175 # if SANITIZER_IOSSIM
176 # define SHADOW_OFFSET kIosSimShadowOffset64
178 # define SHADOW_OFFSET __asan_shadow_memory_dynamic_address
180 # elif defined(__aarch64__)
181 # define SHADOW_OFFSET kAArch64_ShadowOffset64
182 # elif defined(__powerpc64__)
183 # define SHADOW_OFFSET kPPC64_ShadowOffset64
184 # elif defined(__s390x__)
185 # define SHADOW_OFFSET kSystemZ_ShadowOffset64
186 # elif SANITIZER_FREEBSD
187 # define SHADOW_OFFSET kFreeBSD_ShadowOffset64
188 # elif SANITIZER_NETBSD
189 # define SHADOW_OFFSET kNetBSD_ShadowOffset64
191 # define SHADOW_OFFSET kDefaultShadowOffset64
192 # elif defined(__mips64)
193 # define SHADOW_OFFSET kMIPS64_ShadowOffset64
194 # elif SANITIZER_WINDOWS64
195 # define SHADOW_OFFSET __asan_shadow_memory_dynamic_address
197 # define SHADOW_OFFSET kDefaultShort64bitShadowOffset
201 #define SHADOW_GRANULARITY (1ULL << SHADOW_SCALE)
202 #define MEM_TO_SHADOW(mem) (((mem) >> SHADOW_SCALE) + (SHADOW_OFFSET))
205 #define kLowMemEnd (SHADOW_OFFSET ? SHADOW_OFFSET - 1 : 0)
207 #define kLowShadowBeg SHADOW_OFFSET
208 #define kLowShadowEnd MEM_TO_SHADOW(kLowMemEnd)
210 #define kHighMemBeg (MEM_TO_SHADOW(kHighMemEnd) + 1)
212 #define kHighShadowBeg MEM_TO_SHADOW(kHighMemBeg)
213 #define kHighShadowEnd MEM_TO_SHADOW(kHighMemEnd)
215 # define kMidShadowBeg MEM_TO_SHADOW(kMidMemBeg)
216 # define kMidShadowEnd MEM_TO_SHADOW(kMidMemEnd)
218 // With the zero shadow base we can not actually map pages starting from 0.
219 // This constant is somewhat arbitrary.
220 #define kZeroBaseShadowStart 0
221 #define kZeroBaseMaxShadowStart (1 << 18)
223 #define kShadowGapBeg (kLowShadowEnd ? kLowShadowEnd + 1 \
224 : kZeroBaseShadowStart)
225 #define kShadowGapEnd ((kMidMemBeg ? kMidShadowBeg : kHighShadowBeg) - 1)
227 #define kShadowGap2Beg (kMidMemBeg ? kMidShadowEnd + 1 : 0)
228 #define kShadowGap2End (kMidMemBeg ? kMidMemBeg - 1 : 0)
230 #define kShadowGap3Beg (kMidMemBeg ? kMidMemEnd + 1 : 0)
231 #define kShadowGap3End (kMidMemBeg ? kHighShadowBeg - 1 : 0)
233 #define DO_ASAN_MAPPING_PROFILE 0 // Set to 1 to profile the functions below.
235 #if DO_ASAN_MAPPING_PROFILE
236 # define PROFILE_ASAN_MAPPING() AsanMappingProfile[__LINE__]++;
238 # define PROFILE_ASAN_MAPPING()
241 // If 1, all shadow boundaries are constants.
242 // Don't set to 1 other than for testing.
243 #define ASAN_FIXED_MAPPING 0
247 extern uptr AsanMappingProfile
[];
249 #if ASAN_FIXED_MAPPING
250 // Fixed mapping for 64-bit Linux. Mostly used for performance comparison
251 // with non-fixed mapping. As of r175253 (Feb 2013) the performance
252 // difference between fixed and non-fixed mapping is below the noise level.
253 static uptr kHighMemEnd
= 0x7fffffffffffULL
;
254 static uptr kMidMemBeg
= 0x3000000000ULL
;
255 static uptr kMidMemEnd
= 0x4fffffffffULL
;
257 extern uptr kHighMemEnd
, kMidMemBeg
, kMidMemEnd
; // Initialized in __asan_init.
260 static inline bool AddrIsInLowMem(uptr a
) {
261 PROFILE_ASAN_MAPPING();
262 return a
< kLowMemEnd
;
265 static inline bool AddrIsInLowShadow(uptr a
) {
266 PROFILE_ASAN_MAPPING();
267 return a
>= kLowShadowBeg
&& a
<= kLowShadowEnd
;
270 static inline bool AddrIsInHighMem(uptr a
) {
271 PROFILE_ASAN_MAPPING();
272 return a
>= kHighMemBeg
&& a
<= kHighMemEnd
;
275 static inline bool AddrIsInMidMem(uptr a
) {
276 PROFILE_ASAN_MAPPING();
277 return kMidMemBeg
&& a
>= kMidMemBeg
&& a
<= kMidMemEnd
;
280 static inline bool AddrIsInShadowGap(uptr a
) {
281 PROFILE_ASAN_MAPPING();
283 if (a
<= kShadowGapEnd
)
284 return SHADOW_OFFSET
== 0 || a
>= kShadowGapBeg
;
285 return (a
>= kShadowGap2Beg
&& a
<= kShadowGap2End
) ||
286 (a
>= kShadowGap3Beg
&& a
<= kShadowGap3End
);
288 // In zero-based shadow mode we treat addresses near zero as addresses
289 // in shadow gap as well.
290 if (SHADOW_OFFSET
== 0)
291 return a
<= kShadowGapEnd
;
292 return a
>= kShadowGapBeg
&& a
<= kShadowGapEnd
;
295 static inline bool AddrIsInMem(uptr a
) {
296 PROFILE_ASAN_MAPPING();
297 return AddrIsInLowMem(a
) || AddrIsInMidMem(a
) || AddrIsInHighMem(a
) ||
298 (flags()->protect_shadow_gap
== 0 && AddrIsInShadowGap(a
));
301 static inline uptr
MemToShadow(uptr p
) {
302 PROFILE_ASAN_MAPPING();
303 CHECK(AddrIsInMem(p
));
304 return MEM_TO_SHADOW(p
);
307 static inline bool AddrIsInHighShadow(uptr a
) {
308 PROFILE_ASAN_MAPPING();
309 return a
>= kHighShadowBeg
&& a
<= kHighMemEnd
;
312 static inline bool AddrIsInMidShadow(uptr a
) {
313 PROFILE_ASAN_MAPPING();
314 return kMidMemBeg
&& a
>= kMidShadowBeg
&& a
<= kMidMemEnd
;
317 static inline bool AddrIsInShadow(uptr a
) {
318 PROFILE_ASAN_MAPPING();
319 return AddrIsInLowShadow(a
) || AddrIsInMidShadow(a
) || AddrIsInHighShadow(a
);
322 static inline bool AddrIsAlignedByGranularity(uptr a
) {
323 PROFILE_ASAN_MAPPING();
324 return (a
& (SHADOW_GRANULARITY
- 1)) == 0;
327 static inline bool AddressIsPoisoned(uptr a
) {
328 PROFILE_ASAN_MAPPING();
329 const uptr kAccessSize
= 1;
330 u8
*shadow_address
= (u8
*)MEM_TO_SHADOW(a
);
331 s8 shadow_value
= *shadow_address
;
333 u8 last_accessed_byte
= (a
& (SHADOW_GRANULARITY
- 1))
335 return (last_accessed_byte
>= shadow_value
);
340 // Must be after all calls to PROFILE_ASAN_MAPPING().
341 static const uptr kAsanMappingProfileSize
= __LINE__
;
343 } // namespace __asan
345 #endif // ASAN_MAPPING_H