1 //===-- asan_mapping.h ------------------------------------------*- C++ -*-===//
2 //
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
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 // This file is a part of AddressSanitizer, an address sanity checker.
10 //
11 // Defines ASan memory mapping.
12 //===----------------------------------------------------------------------===//
13 #ifndef ASAN_MAPPING_H
14 #define ASAN_MAPPING_H
15
16 #include "sanitizer_common/sanitizer_platform.h"
17
18 // The full explanation of the memory mapping could be found here:
19 // https://github.com/google/sanitizers/wiki/AddressSanitizerAlgorithm
20 //
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 ||
27 //
28 // When SHADOW_OFFSET is zero (-pie):
29 // || `[0x100000000000, 0x7fffffffffff]` || HighMem ||
30 // || `[0x020000000000, 0x0fffffffffff]` || HighShadow ||
31 // || `[0x000000040000, 0x01ffffffffff]` || ShadowGap ||
32 //
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 ||
44 //
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 ||
51 //
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 ||
60 //
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 ||
67 //
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 ||
74 //
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).
78 //
79 // || `[0x1555550000, 0x3fffffffff]` || HighMem ||
80 // || `[0x0fffffa000, 0x1555555fff]` || HighShadow ||
81 // || `[0x0effffa000, 0x0fffff9fff]` || ShadowGap ||
82 // || `[0x0d55550000, 0x0effff9fff]` || LowShadow ||
83 // || `[0x0000000000, 0x0d5554ffff]` || LowMem ||
84 //
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 ||
91 //
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 ||
98 //
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 ||
105 //
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 ||
112 //
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 ||
119 //
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 ||
126 //
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 ||
133 //
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 ||
140 //
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 ||
147 //
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 ||
154 //
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 ||
163
164 #define ASAN_SHADOW_SCALE 3
165
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
179 # elif SANITIZER_IOS
180 # define ASAN_SHADOW_OFFSET_DYNAMIC
181 # else
182 # define ASAN_SHADOW_OFFSET_CONST 0x20000000
183 # endif
184 #else
185 # if SANITIZER_IOS
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 0x0000100000000000
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
213 # else
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)."
217 # endif
218 # define ASAN_SHADOW_OFFSET_CONST 0x000000007fff8000
219 # endif
220 #endif
221
222 #if defined(__cplusplus)
223 # include "asan_internal.h"
224
225 static const u64 kDefaultShadowSentinel = ~(uptr)0;
226
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
232 # else
233 # error "ASAN_SHADOW_OFFSET can't be determined."
234 # endif
235
236 # if SANITIZER_ANDROID && defined(__arm__)
237 # define ASAN_PREMAP_SHADOW 1
238 # else
239 # define ASAN_PREMAP_SHADOW 0
240 # endif
241
242 # define ASAN_SHADOW_GRANULARITY (1ULL << ASAN_SHADOW_SCALE)
243
244 # define DO_ASAN_MAPPING_PROFILE 0 // Set to 1 to profile the functions below.
245
246 # if DO_ASAN_MAPPING_PROFILE
247 # define PROFILE_ASAN_MAPPING() AsanMappingProfile[__LINE__]++;
248 # else
249 # define PROFILE_ASAN_MAPPING()
250 # endif
251
252 // If 1, all shadow boundaries are constants.
253 // Don't set to 1 other than for testing.
254 # define ASAN_FIXED_MAPPING 0
255
256 namespace __asan {
257
258 extern uptr AsanMappingProfile[];
259
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;
267 # else
268 extern uptr kHighMemEnd, kMidMemBeg, kMidMemEnd; // Initialized in __asan_init.
269 # endif
270
271 } // namespace __asan
272
273 # if defined(__sparc__) && SANITIZER_WORDSIZE == 64
274 # include "asan_mapping_sparc64.h"
275 # else
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))
280
281 # define kLowMemBeg 0
282 # define kLowMemEnd (ASAN_SHADOW_OFFSET ? ASAN_SHADOW_OFFSET - 1 : 0)
283
284 # define kLowShadowBeg ASAN_SHADOW_OFFSET
285 # define kLowShadowEnd MEM_TO_SHADOW(kLowMemEnd)
286
287 # define kHighMemBeg (MEM_TO_SHADOW(kHighMemEnd) + 1)
288
289 # define kHighShadowBeg MEM_TO_SHADOW(kHighMemBeg)
290 # define kHighShadowEnd MEM_TO_SHADOW(kHighMemEnd)
291
292 # define kMidShadowBeg MEM_TO_SHADOW(kMidMemBeg)
293 # define kMidShadowEnd MEM_TO_SHADOW(kMidMemEnd)
294
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)
299
300 # define kShadowGapBeg \
301 (kLowShadowEnd ? kLowShadowEnd + 1 : kZeroBaseShadowStart)
302 # define kShadowGapEnd ((kMidMemBeg ? kMidShadowBeg : kHighShadowBeg) - 1)
303
304 # define kShadowGap2Beg (kMidMemBeg ? kMidShadowEnd + 1 : 0)
305 # define kShadowGap2End (kMidMemBeg ? kMidMemBeg - 1 : 0)
306
307 # define kShadowGap3Beg (kMidMemBeg ? kMidMemEnd + 1 : 0)
308 # define kShadowGap3End (kMidMemBeg ? kHighShadowBeg - 1 : 0)
309
310 namespace __asan {
311
AddrIsInLowMem(uptr a)312 static inline bool AddrIsInLowMem(uptr a) {
313 PROFILE_ASAN_MAPPING();
314 return a <= kLowMemEnd;
315 }
316
AddrIsInLowShadow(uptr a)317 static inline bool AddrIsInLowShadow(uptr a) {
318 PROFILE_ASAN_MAPPING();
319 return a >= kLowShadowBeg && a <= kLowShadowEnd;
320 }
321
AddrIsInMidMem(uptr a)322 static inline bool AddrIsInMidMem(uptr a) {
323 PROFILE_ASAN_MAPPING();
324 return kMidMemBeg && a >= kMidMemBeg && a <= kMidMemEnd;
325 }
326
AddrIsInMidShadow(uptr a)327 static inline bool AddrIsInMidShadow(uptr a) {
328 PROFILE_ASAN_MAPPING();
329 return kMidMemBeg && a >= kMidShadowBeg && a <= kMidShadowEnd;
330 }
331
AddrIsInHighMem(uptr a)332 static inline bool AddrIsInHighMem(uptr a) {
333 PROFILE_ASAN_MAPPING();
334 return kHighMemBeg && a >= kHighMemBeg && a <= kHighMemEnd;
335 }
336
AddrIsInHighShadow(uptr a)337 static inline bool AddrIsInHighShadow(uptr a) {
338 PROFILE_ASAN_MAPPING();
339 return kHighMemBeg && a >= kHighShadowBeg && a <= kHighShadowEnd;
340 }
341
AddrIsInShadowGap(uptr a)342 static inline bool AddrIsInShadowGap(uptr a) {
343 PROFILE_ASAN_MAPPING();
344 if (kMidMemBeg) {
345 if (a <= kShadowGapEnd)
346 return ASAN_SHADOW_OFFSET == 0 || a >= kShadowGapBeg;
347 return (a >= kShadowGap2Beg && a <= kShadowGap2End) ||
348 (a >= kShadowGap3Beg && a <= kShadowGap3End);
349 }
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;
355 }
356
357 } // namespace __asan
358
359 # endif
360
361 namespace __asan {
362
MemToShadowSize(uptr size)363 static inline uptr MemToShadowSize(uptr size) {
364 return size >> ASAN_SHADOW_SCALE;
365 }
366
AddrIsInMem(uptr a)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));
371 }
372
MemToShadow(uptr p)373 static inline uptr MemToShadow(uptr p) {
374 PROFILE_ASAN_MAPPING();
375 CHECK(AddrIsInMem(p));
376 return MEM_TO_SHADOW(p);
377 }
378
AddrIsInShadow(uptr a)379 static inline bool AddrIsInShadow(uptr a) {
380 PROFILE_ASAN_MAPPING();
381 return AddrIsInLowShadow(a) || AddrIsInMidShadow(a) || AddrIsInHighShadow(a);
382 }
383
ShadowToMem(uptr p)384 static inline uptr ShadowToMem(uptr p) {
385 PROFILE_ASAN_MAPPING();
386 CHECK(AddrIsInShadow(p));
387 return SHADOW_TO_MEM(p);
388 }
389
AddrIsAlignedByGranularity(uptr a)390 static inline bool AddrIsAlignedByGranularity(uptr a) {
391 PROFILE_ASAN_MAPPING();
392 return (a & (ASAN_SHADOW_GRANULARITY - 1)) == 0;
393 }
394
AddressIsPoisoned(uptr a)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;
400 if (shadow_value) {
401 u8 last_accessed_byte =
402 (a & (ASAN_SHADOW_GRANULARITY - 1)) + kAccessSize - 1;
403 return (last_accessed_byte >= shadow_value);
404 }
405 return false;
406 }
407
408 // Must be after all calls to PROFILE_ASAN_MAPPING().
409 static const uptr kAsanMappingProfileSize = __LINE__;
410
411 } // namespace __asan
412
413 #endif // __cplusplus
414
415 #endif // ASAN_MAPPING_H
416