1 //===-- asan_mapping.h ------------------------------------------*- C++ -*-===//
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7 //===----------------------------------------------------------------------===//
9 // This file is a part of AddressSanitizer, an address sanity checker.
11 // Defines ASan memory mapping.
12 //===----------------------------------------------------------------------===//
13 #ifndef ASAN_MAPPING_H
14 #define ASAN_MAPPING_H
16 #include "sanitizer_common/sanitizer_platform.h"
18 // The full explanation of the memory mapping could be found here:
19 // https://github.com/google/sanitizers/wiki/AddressSanitizerAlgorithm
21 // Typical shadow mapping on Linux/x86_64 with SHADOW_OFFSET == 0x00007fff8000:
22 // || `[0x10007fff8000, 0x7fffffffffff]` || HighMem ||
23 // || `[0x02008fff7000, 0x10007fff7fff]` || HighShadow ||
24 // || `[0x00008fff7000, 0x02008fff6fff]` || ShadowGap ||
25 // || `[0x00007fff8000, 0x00008fff6fff]` || LowShadow ||
26 // || `[0x000000000000, 0x00007fff7fff]` || LowMem ||
28 // When SHADOW_OFFSET is zero (-pie):
29 // || `[0x100000000000, 0x7fffffffffff]` || HighMem ||
30 // || `[0x020000000000, 0x0fffffffffff]` || HighShadow ||
31 // || `[0x000000040000, 0x01ffffffffff]` || ShadowGap ||
33 // Special case when something is already mapped between
34 // 0x003000000000 and 0x005000000000 (e.g. when prelink is installed):
35 // || `[0x10007fff8000, 0x7fffffffffff]` || HighMem ||
36 // || `[0x02008fff7000, 0x10007fff7fff]` || HighShadow ||
37 // || `[0x005000000000, 0x02008fff6fff]` || ShadowGap3 ||
38 // || `[0x003000000000, 0x004fffffffff]` || MidMem ||
39 // || `[0x000a7fff8000, 0x002fffffffff]` || ShadowGap2 ||
40 // || `[0x00067fff8000, 0x000a7fff7fff]` || MidShadow ||
41 // || `[0x00008fff7000, 0x00067fff7fff]` || ShadowGap ||
42 // || `[0x00007fff8000, 0x00008fff6fff]` || LowShadow ||
43 // || `[0x000000000000, 0x00007fff7fff]` || LowMem ||
45 // Default Linux/i386 mapping on x86_64 machine:
46 // || `[0x40000000, 0xffffffff]` || HighMem ||
47 // || `[0x28000000, 0x3fffffff]` || HighShadow ||
48 // || `[0x24000000, 0x27ffffff]` || ShadowGap ||
49 // || `[0x20000000, 0x23ffffff]` || LowShadow ||
50 // || `[0x00000000, 0x1fffffff]` || LowMem ||
52 // Default Linux/i386 mapping on i386 machine
53 // (addresses starting with 0xc0000000 are reserved
54 // for kernel and thus not sanitized):
55 // || `[0x38000000, 0xbfffffff]` || HighMem ||
56 // || `[0x27000000, 0x37ffffff]` || HighShadow ||
57 // || `[0x24000000, 0x26ffffff]` || ShadowGap ||
58 // || `[0x20000000, 0x23ffffff]` || LowShadow ||
59 // || `[0x00000000, 0x1fffffff]` || LowMem ||
61 // Default Linux/MIPS32 mapping:
62 // || `[0x2aaa0000, 0xffffffff]` || HighMem ||
63 // || `[0x0fff4000, 0x2aa9ffff]` || HighShadow ||
64 // || `[0x0bff4000, 0x0fff3fff]` || ShadowGap ||
65 // || `[0x0aaa0000, 0x0bff3fff]` || LowShadow ||
66 // || `[0x00000000, 0x0aa9ffff]` || LowMem ||
68 // Default Linux/MIPS64 mapping:
69 // || `[0x4000000000, 0xffffffffff]` || HighMem ||
70 // || `[0x2800000000, 0x3fffffffff]` || HighShadow ||
71 // || `[0x2400000000, 0x27ffffffff]` || ShadowGap ||
72 // || `[0x2000000000, 0x23ffffffff]` || LowShadow ||
73 // || `[0x0000000000, 0x1fffffffff]` || LowMem ||
75 // Default Linux/RISCV64 Sv39 mapping with SHADOW_OFFSET == 0xd55550000;
76 // (the exact location of SHADOW_OFFSET may vary depending the dynamic probing
77 // by FindDynamicShadowStart).
79 // || `[0x1555550000, 0x3fffffffff]` || HighMem ||
80 // || `[0x0fffffa000, 0x1555555fff]` || HighShadow ||
81 // || `[0x0effffa000, 0x0fffff9fff]` || ShadowGap ||
82 // || `[0x0d55550000, 0x0effff9fff]` || LowShadow ||
83 // || `[0x0000000000, 0x0d5554ffff]` || LowMem ||
85 // Default Linux/AArch64 (39-bit VMA) mapping:
86 // || `[0x2000000000, 0x7fffffffff]` || highmem ||
87 // || `[0x1400000000, 0x1fffffffff]` || highshadow ||
88 // || `[0x1200000000, 0x13ffffffff]` || shadowgap ||
89 // || `[0x1000000000, 0x11ffffffff]` || lowshadow ||
90 // || `[0x0000000000, 0x0fffffffff]` || lowmem ||
92 // Default Linux/AArch64 (42-bit VMA) mapping:
93 // || `[0x10000000000, 0x3ffffffffff]` || highmem ||
94 // || `[0x0a000000000, 0x0ffffffffff]` || highshadow ||
95 // || `[0x09000000000, 0x09fffffffff]` || shadowgap ||
96 // || `[0x08000000000, 0x08fffffffff]` || lowshadow ||
97 // || `[0x00000000000, 0x07fffffffff]` || lowmem ||
99 // Default Linux/S390 mapping:
100 // || `[0x30000000, 0x7fffffff]` || HighMem ||
101 // || `[0x26000000, 0x2fffffff]` || HighShadow ||
102 // || `[0x24000000, 0x25ffffff]` || ShadowGap ||
103 // || `[0x20000000, 0x23ffffff]` || LowShadow ||
104 // || `[0x00000000, 0x1fffffff]` || LowMem ||
106 // Default Linux/SystemZ mapping:
107 // || `[0x14000000000000, 0x1fffffffffffff]` || HighMem ||
108 // || `[0x12800000000000, 0x13ffffffffffff]` || HighShadow ||
109 // || `[0x12000000000000, 0x127fffffffffff]` || ShadowGap ||
110 // || `[0x10000000000000, 0x11ffffffffffff]` || LowShadow ||
111 // || `[0x00000000000000, 0x0fffffffffffff]` || LowMem ||
113 // Default Linux/SPARC64 (52-bit VMA) mapping:
114 // || `[0x8000000000000, 0xfffffffffffff]` || HighMem ||
115 // || `[0x1080000000000, 0x207ffffffffff]` || HighShadow ||
116 // || `[0x0090000000000, 0x107ffffffffff]` || ShadowGap ||
117 // || `[0x0080000000000, 0x008ffffffffff]` || LowShadow ||
118 // || `[0x0000000000000, 0x007ffffffffff]` || LowMem ||
120 // Default Linux/LoongArch64 (47-bit VMA) mapping:
121 // || `[0x500000000000, 0x7fffffffffff]` || HighMem ||
122 // || `[0x4a0000000000, 0x4fffffffffff]` || HighShadow ||
123 // || `[0x480000000000, 0x49ffffffffff]` || ShadowGap ||
124 // || `[0x400000000000, 0x47ffffffffff]` || LowShadow ||
125 // || `[0x000000000000, 0x3fffffffffff]` || LowMem ||
127 // Shadow mapping on FreeBSD/x86-64 with SHADOW_OFFSET == 0x400000000000:
128 // || `[0x500000000000, 0x7fffffffffff]` || HighMem ||
129 // || `[0x4a0000000000, 0x4fffffffffff]` || HighShadow ||
130 // || `[0x480000000000, 0x49ffffffffff]` || ShadowGap ||
131 // || `[0x400000000000, 0x47ffffffffff]` || LowShadow ||
132 // || `[0x000000000000, 0x3fffffffffff]` || LowMem ||
134 // Shadow mapping on FreeBSD/i386 with SHADOW_OFFSET == 0x40000000:
135 // || `[0x60000000, 0xffffffff]` || HighMem ||
136 // || `[0x4c000000, 0x5fffffff]` || HighShadow ||
137 // || `[0x48000000, 0x4bffffff]` || ShadowGap ||
138 // || `[0x40000000, 0x47ffffff]` || LowShadow ||
139 // || `[0x00000000, 0x3fffffff]` || LowMem ||
141 // Shadow mapping on NetBSD/x86-64 with SHADOW_OFFSET == 0x400000000000:
142 // || `[0x4feffffffe01, 0x7f7ffffff000]` || HighMem ||
143 // || `[0x49fdffffffc0, 0x4feffffffe00]` || HighShadow ||
144 // || `[0x480000000000, 0x49fdffffffbf]` || ShadowGap ||
145 // || `[0x400000000000, 0x47ffffffffff]` || LowShadow ||
146 // || `[0x000000000000, 0x3fffffffffff]` || LowMem ||
148 // Shadow mapping on NetBSD/i386 with SHADOW_OFFSET == 0x40000000:
149 // || `[0x60000000, 0xfffff000]` || HighMem ||
150 // || `[0x4c000000, 0x5fffffff]` || HighShadow ||
151 // || `[0x48000000, 0x4bffffff]` || ShadowGap ||
152 // || `[0x40000000, 0x47ffffff]` || LowShadow ||
153 // || `[0x00000000, 0x3fffffff]` || LowMem ||
155 // Default Windows/i386 mapping:
156 // (the exact location of HighShadow/HighMem may vary depending
157 // on WoW64, /LARGEADDRESSAWARE, etc).
158 // || `[0x50000000, 0xffffffff]` || HighMem ||
159 // || `[0x3a000000, 0x4fffffff]` || HighShadow ||
160 // || `[0x36000000, 0x39ffffff]` || ShadowGap ||
161 // || `[0x30000000, 0x35ffffff]` || LowShadow ||
162 // || `[0x00000000, 0x2fffffff]` || LowMem ||
164 #define ASAN_SHADOW_SCALE 3
166 #if SANITIZER_FUCHSIA
167 # define ASAN_SHADOW_OFFSET_CONST (0)
168 #elif SANITIZER_WORDSIZE == 32
169 # if SANITIZER_ANDROID
170 # define ASAN_SHADOW_OFFSET_DYNAMIC
171 # elif defined(__mips__)
172 # define ASAN_SHADOW_OFFSET_CONST 0x0aaa0000
173 # elif SANITIZER_FREEBSD
174 # define ASAN_SHADOW_OFFSET_CONST 0x40000000
175 # elif SANITIZER_NETBSD
176 # define ASAN_SHADOW_OFFSET_CONST 0x40000000
177 # elif SANITIZER_WINDOWS
178 # define ASAN_SHADOW_OFFSET_CONST 0x30000000
180 # define ASAN_SHADOW_OFFSET_DYNAMIC
182 # define ASAN_SHADOW_OFFSET_CONST 0x20000000
186 # define ASAN_SHADOW_OFFSET_DYNAMIC
187 # elif SANITIZER_APPLE && defined(__aarch64__)
188 # define ASAN_SHADOW_OFFSET_DYNAMIC
189 # elif SANITIZER_FREEBSD && defined(__aarch64__)
190 # define ASAN_SHADOW_OFFSET_CONST 0x0000800000000000
191 # elif SANITIZER_RISCV64
192 # define ASAN_SHADOW_OFFSET_DYNAMIC
193 # elif defined(__aarch64__)
194 # define ASAN_SHADOW_OFFSET_CONST 0x0000001000000000
195 # elif defined(__powerpc64__)
196 # define ASAN_SHADOW_OFFSET_CONST 0x0000020000000000
197 # elif defined(__s390x__)
198 # define ASAN_SHADOW_OFFSET_CONST 0x0010000000000000
199 # elif SANITIZER_FREEBSD
200 # define ASAN_SHADOW_OFFSET_CONST 0x0000400000000000
201 # elif SANITIZER_NETBSD
202 # define ASAN_SHADOW_OFFSET_CONST 0x0000400000000000
203 # elif SANITIZER_APPLE
204 # define ASAN_SHADOW_OFFSET_CONST 0x0000100000000000
205 # elif defined(__mips64)
206 # define ASAN_SHADOW_OFFSET_CONST 0x0000002000000000
207 # elif defined(__sparc__)
208 # define ASAN_SHADOW_OFFSET_CONST 0x0000080000000000
209 # elif SANITIZER_LOONGARCH64
210 # define ASAN_SHADOW_OFFSET_CONST 0x0000400000000000
211 # elif SANITIZER_WINDOWS64
212 # define ASAN_SHADOW_OFFSET_DYNAMIC
214 # if ASAN_SHADOW_SCALE != 3
215 # error "Value below is based on shadow scale = 3."
216 # error "Original formula was: 0x7FFFFFFF & (~0xFFFULL << SHADOW_SCALE)."
218 # define ASAN_SHADOW_OFFSET_CONST 0x000000007fff8000
222 #if defined(__cplusplus)
223 # include "asan_internal.h"
225 static const u64 kDefaultShadowSentinel
= ~(uptr
)0;
227 # if defined(ASAN_SHADOW_OFFSET_CONST)
228 static const u64 kConstShadowOffset
= ASAN_SHADOW_OFFSET_CONST
;
229 # define ASAN_SHADOW_OFFSET kConstShadowOffset
230 # elif defined(ASAN_SHADOW_OFFSET_DYNAMIC)
231 # define ASAN_SHADOW_OFFSET __asan_shadow_memory_dynamic_address
233 # error "ASAN_SHADOW_OFFSET can't be determined."
236 # if SANITIZER_ANDROID && defined(__arm__)
237 # define ASAN_PREMAP_SHADOW 1
239 # define ASAN_PREMAP_SHADOW 0
242 # define ASAN_SHADOW_GRANULARITY (1ULL << ASAN_SHADOW_SCALE)
244 # define DO_ASAN_MAPPING_PROFILE 0 // Set to 1 to profile the functions below.
246 # if DO_ASAN_MAPPING_PROFILE
247 # define PROFILE_ASAN_MAPPING() AsanMappingProfile[__LINE__]++;
249 # define PROFILE_ASAN_MAPPING()
252 // If 1, all shadow boundaries are constants.
253 // Don't set to 1 other than for testing.
254 # define ASAN_FIXED_MAPPING 0
258 extern uptr AsanMappingProfile
[];
260 # if ASAN_FIXED_MAPPING
261 // Fixed mapping for 64-bit Linux. Mostly used for performance comparison
262 // with non-fixed mapping. As of r175253 (Feb 2013) the performance
263 // difference between fixed and non-fixed mapping is below the noise level.
264 static uptr kHighMemEnd
= 0x7fffffffffffULL
;
265 static uptr kMidMemBeg
= 0x3000000000ULL
;
266 static uptr kMidMemEnd
= 0x4fffffffffULL
;
268 extern uptr kHighMemEnd
, kMidMemBeg
, kMidMemEnd
; // Initialized in __asan_init.
271 } // namespace __asan
273 # if defined(__sparc__) && SANITIZER_WORDSIZE == 64
274 # include "asan_mapping_sparc64.h"
276 # define MEM_TO_SHADOW(mem) \
277 (((mem) >> ASAN_SHADOW_SCALE) + (ASAN_SHADOW_OFFSET))
278 # define SHADOW_TO_MEM(mem) \
279 (((mem) - (ASAN_SHADOW_OFFSET)) << (ASAN_SHADOW_SCALE))
281 # define kLowMemBeg 0
282 # define kLowMemEnd (ASAN_SHADOW_OFFSET ? ASAN_SHADOW_OFFSET - 1 : 0)
284 # define kLowShadowBeg ASAN_SHADOW_OFFSET
285 # define kLowShadowEnd MEM_TO_SHADOW(kLowMemEnd)
287 # define kHighMemBeg (MEM_TO_SHADOW(kHighMemEnd) + 1)
289 # define kHighShadowBeg MEM_TO_SHADOW(kHighMemBeg)
290 # define kHighShadowEnd MEM_TO_SHADOW(kHighMemEnd)
292 # define kMidShadowBeg MEM_TO_SHADOW(kMidMemBeg)
293 # define kMidShadowEnd MEM_TO_SHADOW(kMidMemEnd)
295 // With the zero shadow base we can not actually map pages starting from 0.
296 // This constant is somewhat arbitrary.
297 # define kZeroBaseShadowStart 0
298 # define kZeroBaseMaxShadowStart (1 << 18)
300 # define kShadowGapBeg \
301 (kLowShadowEnd ? kLowShadowEnd + 1 : kZeroBaseShadowStart)
302 # define kShadowGapEnd ((kMidMemBeg ? kMidShadowBeg : kHighShadowBeg) - 1)
304 # define kShadowGap2Beg (kMidMemBeg ? kMidShadowEnd + 1 : 0)
305 # define kShadowGap2End (kMidMemBeg ? kMidMemBeg - 1 : 0)
307 # define kShadowGap3Beg (kMidMemBeg ? kMidMemEnd + 1 : 0)
308 # define kShadowGap3End (kMidMemBeg ? kHighShadowBeg - 1 : 0)
312 static inline bool AddrIsInLowMem(uptr a
) {
313 PROFILE_ASAN_MAPPING();
314 return a
<= kLowMemEnd
;
317 static inline bool AddrIsInLowShadow(uptr a
) {
318 PROFILE_ASAN_MAPPING();
319 return a
>= kLowShadowBeg
&& a
<= kLowShadowEnd
;
322 static inline bool AddrIsInMidMem(uptr a
) {
323 PROFILE_ASAN_MAPPING();
324 return kMidMemBeg
&& a
>= kMidMemBeg
&& a
<= kMidMemEnd
;
327 static inline bool AddrIsInMidShadow(uptr a
) {
328 PROFILE_ASAN_MAPPING();
329 return kMidMemBeg
&& a
>= kMidShadowBeg
&& a
<= kMidShadowEnd
;
332 static inline bool AddrIsInHighMem(uptr a
) {
333 PROFILE_ASAN_MAPPING();
334 return kHighMemBeg
&& a
>= kHighMemBeg
&& a
<= kHighMemEnd
;
337 static inline bool AddrIsInHighShadow(uptr a
) {
338 PROFILE_ASAN_MAPPING();
339 return kHighMemBeg
&& a
>= kHighShadowBeg
&& a
<= kHighShadowEnd
;
342 static inline bool AddrIsInShadowGap(uptr a
) {
343 PROFILE_ASAN_MAPPING();
345 if (a
<= kShadowGapEnd
)
346 return ASAN_SHADOW_OFFSET
== 0 || a
>= kShadowGapBeg
;
347 return (a
>= kShadowGap2Beg
&& a
<= kShadowGap2End
) ||
348 (a
>= kShadowGap3Beg
&& a
<= kShadowGap3End
);
350 // In zero-based shadow mode we treat addresses near zero as addresses
351 // in shadow gap as well.
352 if (ASAN_SHADOW_OFFSET
== 0)
353 return a
<= kShadowGapEnd
;
354 return a
>= kShadowGapBeg
&& a
<= kShadowGapEnd
;
357 } // namespace __asan
363 static inline uptr
MemToShadowSize(uptr size
) {
364 return size
>> ASAN_SHADOW_SCALE
;
367 static inline bool AddrIsInMem(uptr a
) {
368 PROFILE_ASAN_MAPPING();
369 return AddrIsInLowMem(a
) || AddrIsInMidMem(a
) || AddrIsInHighMem(a
) ||
370 (flags()->protect_shadow_gap
== 0 && AddrIsInShadowGap(a
));
373 static inline uptr
MemToShadow(uptr p
) {
374 PROFILE_ASAN_MAPPING();
375 CHECK(AddrIsInMem(p
));
376 return MEM_TO_SHADOW(p
);
379 static inline bool AddrIsInShadow(uptr a
) {
380 PROFILE_ASAN_MAPPING();
381 return AddrIsInLowShadow(a
) || AddrIsInMidShadow(a
) || AddrIsInHighShadow(a
);
384 static inline uptr
ShadowToMem(uptr p
) {
385 PROFILE_ASAN_MAPPING();
386 CHECK(AddrIsInShadow(p
));
387 return SHADOW_TO_MEM(p
);
390 static inline bool AddrIsAlignedByGranularity(uptr a
) {
391 PROFILE_ASAN_MAPPING();
392 return (a
& (ASAN_SHADOW_GRANULARITY
- 1)) == 0;
395 static inline bool AddressIsPoisoned(uptr a
) {
396 PROFILE_ASAN_MAPPING();
397 const uptr kAccessSize
= 1;
398 u8
*shadow_address
= (u8
*)MEM_TO_SHADOW(a
);
399 s8 shadow_value
= *shadow_address
;
401 u8 last_accessed_byte
=
402 (a
& (ASAN_SHADOW_GRANULARITY
- 1)) + kAccessSize
- 1;
403 return (last_accessed_byte
>= shadow_value
);
408 // Must be after all calls to PROFILE_ASAN_MAPPING().
409 static const uptr kAsanMappingProfileSize
= __LINE__
;
411 } // namespace __asan
413 #endif // __cplusplus
415 #endif // ASAN_MAPPING_H