xref: /freebsd/contrib/llvm-project/compiler-rt/lib/asan/asan_mapping.h (revision 6ba2210ee039f2f12878c217bcf058e9c8b26b29)
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 "asan_internal.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/AArch64 (39-bit VMA) mapping:
76 // || `[0x2000000000, 0x7fffffffff]` || highmem    ||
77 // || `[0x1400000000, 0x1fffffffff]` || highshadow ||
78 // || `[0x1200000000, 0x13ffffffff]` || shadowgap  ||
79 // || `[0x1000000000, 0x11ffffffff]` || lowshadow  ||
80 // || `[0x0000000000, 0x0fffffffff]` || lowmem     ||
81 //
82 // RISC-V has only 38 bits for task size
83 // Low mem size is set with kRiscv64_ShadowOffset64 in
84 // compiler-rt/lib/asan/asan_allocator.h and in
85 // llvm/lib/Transforms/Instrumentation/AddressSanitizer.cpp with
86 // kRiscv64_ShadowOffset64, High mem top border is set with
87 // GetMaxVirtualAddress() in
88 // compiler-rt/lib/sanitizer_common/sanitizer_linux.cpp
89 // Default Linux/RISCV64 Sv39/Sv48 mapping:
90 // || `[0x000820000000, 0x003fffffffff]` || HighMem    ||
91 // || `[0x000124000000, 0x00081fffffff]` || HighShadow ||
92 // || `[0x000024000000, 0x000123ffffff]` || ShadowGap  ||
93 // || `[0x000020000000, 0x000023ffffff]` || LowShadow  ||
94 // || `[0x000000000000, 0x00001fffffff]` || LowMem     ||
95 //
96 // Default Linux/AArch64 (42-bit VMA) mapping:
97 // || `[0x10000000000, 0x3ffffffffff]` || highmem    ||
98 // || `[0x0a000000000, 0x0ffffffffff]` || highshadow ||
99 // || `[0x09000000000, 0x09fffffffff]` || shadowgap  ||
100 // || `[0x08000000000, 0x08fffffffff]` || lowshadow  ||
101 // || `[0x00000000000, 0x07fffffffff]` || lowmem     ||
102 //
103 // Default Linux/S390 mapping:
104 // || `[0x30000000, 0x7fffffff]` || HighMem    ||
105 // || `[0x26000000, 0x2fffffff]` || HighShadow ||
106 // || `[0x24000000, 0x25ffffff]` || ShadowGap  ||
107 // || `[0x20000000, 0x23ffffff]` || LowShadow  ||
108 // || `[0x00000000, 0x1fffffff]` || LowMem     ||
109 //
110 // Default Linux/SystemZ mapping:
111 // || `[0x14000000000000, 0x1fffffffffffff]` || HighMem    ||
112 // || `[0x12800000000000, 0x13ffffffffffff]` || HighShadow ||
113 // || `[0x12000000000000, 0x127fffffffffff]` || ShadowGap  ||
114 // || `[0x10000000000000, 0x11ffffffffffff]` || LowShadow  ||
115 // || `[0x00000000000000, 0x0fffffffffffff]` || LowMem     ||
116 //
117 // Default Linux/SPARC64 (52-bit VMA) mapping:
118 // || `[0x8000000000000, 0xfffffffffffff]` || HighMem    ||
119 // || `[0x1080000000000, 0x207ffffffffff]` || HighShadow ||
120 // || `[0x0090000000000, 0x107ffffffffff]` || ShadowGap  ||
121 // || `[0x0080000000000, 0x008ffffffffff]` || LowShadow  ||
122 // || `[0x0000000000000, 0x007ffffffffff]` || LowMem     ||
123 //
124 // Shadow mapping on FreeBSD/x86-64 with SHADOW_OFFSET == 0x400000000000:
125 // || `[0x500000000000, 0x7fffffffffff]` || HighMem    ||
126 // || `[0x4a0000000000, 0x4fffffffffff]` || HighShadow ||
127 // || `[0x480000000000, 0x49ffffffffff]` || ShadowGap  ||
128 // || `[0x400000000000, 0x47ffffffffff]` || LowShadow  ||
129 // || `[0x000000000000, 0x3fffffffffff]` || LowMem     ||
130 //
131 // Shadow mapping on FreeBSD/i386 with SHADOW_OFFSET == 0x40000000:
132 // || `[0x60000000, 0xffffffff]` || HighMem    ||
133 // || `[0x4c000000, 0x5fffffff]` || HighShadow ||
134 // || `[0x48000000, 0x4bffffff]` || ShadowGap  ||
135 // || `[0x40000000, 0x47ffffff]` || LowShadow  ||
136 // || `[0x00000000, 0x3fffffff]` || LowMem     ||
137 //
138 // Shadow mapping on NetBSD/x86-64 with SHADOW_OFFSET == 0x400000000000:
139 // || `[0x4feffffffe01, 0x7f7ffffff000]` || HighMem    ||
140 // || `[0x49fdffffffc0, 0x4feffffffe00]` || HighShadow ||
141 // || `[0x480000000000, 0x49fdffffffbf]` || ShadowGap  ||
142 // || `[0x400000000000, 0x47ffffffffff]` || LowShadow  ||
143 // || `[0x000000000000, 0x3fffffffffff]` || LowMem     ||
144 //
145 // Shadow mapping on NetBSD/i386 with SHADOW_OFFSET == 0x40000000:
146 // || `[0x60000000, 0xfffff000]` || HighMem    ||
147 // || `[0x4c000000, 0x5fffffff]` || HighShadow ||
148 // || `[0x48000000, 0x4bffffff]` || ShadowGap  ||
149 // || `[0x40000000, 0x47ffffff]` || LowShadow  ||
150 // || `[0x00000000, 0x3fffffff]` || LowMem     ||
151 //
152 // Default Windows/i386 mapping:
153 // (the exact location of HighShadow/HighMem may vary depending
154 //  on WoW64, /LARGEADDRESSAWARE, etc).
155 // || `[0x50000000, 0xffffffff]` || HighMem    ||
156 // || `[0x3a000000, 0x4fffffff]` || HighShadow ||
157 // || `[0x36000000, 0x39ffffff]` || ShadowGap  ||
158 // || `[0x30000000, 0x35ffffff]` || LowShadow  ||
159 // || `[0x00000000, 0x2fffffff]` || LowMem     ||
160 //
161 // Shadow mapping on Myriad2 (for shadow scale 5):
162 // || `[0x9ff80000, 0x9fffffff]` || ShadowGap  ||
163 // || `[0x9f000000, 0x9ff7ffff]` || LowShadow  ||
164 // || `[0x80000000, 0x9effffff]` || LowMem     ||
165 // || `[0x00000000, 0x7fffffff]` || Ignored    ||
166 
167 #if defined(ASAN_SHADOW_SCALE)
168 static const u64 kDefaultShadowScale = ASAN_SHADOW_SCALE;
169 #else
170 static const u64 kDefaultShadowScale = SANITIZER_MYRIAD2 ? 5 : 3;
171 #endif
172 static const u64 kDefaultShadowSentinel = ~(uptr)0;
173 static const u64 kDefaultShadowOffset32 = 1ULL << 29;  // 0x20000000
174 static const u64 kDefaultShadowOffset64 = 1ULL << 44;
175 static const u64 kDefaultShort64bitShadowOffset =
176     0x7FFFFFFF & (~0xFFFULL << kDefaultShadowScale);  // < 2G.
177 static const u64 kAArch64_ShadowOffset64 = 1ULL << 36;
178 static const u64 kRiscv64_ShadowOffset64 = 0x20000000;
179 static const u64 kMIPS32_ShadowOffset32 = 0x0aaa0000;
180 static const u64 kMIPS64_ShadowOffset64 = 1ULL << 37;
181 static const u64 kPPC64_ShadowOffset64 = 1ULL << 44;
182 static const u64 kSystemZ_ShadowOffset64 = 1ULL << 52;
183 static const u64 kSPARC64_ShadowOffset64 = 1ULL << 43;  // 0x80000000000
184 static const u64 kFreeBSD_ShadowOffset32 = 1ULL << 30;  // 0x40000000
185 static const u64 kFreeBSD_ShadowOffset64 = 1ULL << 46;  // 0x400000000000
186 static const u64 kNetBSD_ShadowOffset32 = 1ULL << 30;  // 0x40000000
187 static const u64 kNetBSD_ShadowOffset64 = 1ULL << 46;  // 0x400000000000
188 static const u64 kWindowsShadowOffset32 = 3ULL << 28;  // 0x30000000
189 
190 static const u64 kMyriadMemoryOffset32 = 0x80000000ULL;
191 static const u64 kMyriadMemorySize32 = 0x20000000ULL;
192 static const u64 kMyriadMemoryEnd32 =
193     kMyriadMemoryOffset32 + kMyriadMemorySize32 - 1;
194 static const u64 kMyriadShadowOffset32 =
195     (kMyriadMemoryOffset32 + kMyriadMemorySize32 -
196      (kMyriadMemorySize32 >> kDefaultShadowScale));
197 static const u64 kMyriadCacheBitMask32 = 0x40000000ULL;
198 
199 #define SHADOW_SCALE kDefaultShadowScale
200 
201 #if SANITIZER_FUCHSIA
202 #  define SHADOW_OFFSET (0)
203 #elif SANITIZER_WORDSIZE == 32
204 #  if SANITIZER_ANDROID
205 #    define SHADOW_OFFSET __asan_shadow_memory_dynamic_address
206 #  elif defined(__mips__)
207 #    define SHADOW_OFFSET kMIPS32_ShadowOffset32
208 #  elif SANITIZER_FREEBSD
209 #    define SHADOW_OFFSET kFreeBSD_ShadowOffset32
210 #  elif SANITIZER_NETBSD
211 #    define SHADOW_OFFSET kNetBSD_ShadowOffset32
212 #  elif SANITIZER_WINDOWS
213 #    define SHADOW_OFFSET kWindowsShadowOffset32
214 #  elif SANITIZER_IOS
215 #    define SHADOW_OFFSET __asan_shadow_memory_dynamic_address
216 #  elif SANITIZER_MYRIAD2
217 #    define SHADOW_OFFSET kMyriadShadowOffset32
218 #  else
219 #    define SHADOW_OFFSET kDefaultShadowOffset32
220 #  endif
221 #else
222 #  if SANITIZER_IOS
223 #    define SHADOW_OFFSET __asan_shadow_memory_dynamic_address
224 #  elif SANITIZER_MAC && defined(__aarch64__)
225 #    define SHADOW_OFFSET __asan_shadow_memory_dynamic_address
226 #elif SANITIZER_RISCV64
227 #define SHADOW_OFFSET kRiscv64_ShadowOffset64
228 #  elif defined(__aarch64__)
229 #    define SHADOW_OFFSET kAArch64_ShadowOffset64
230 #  elif defined(__powerpc64__)
231 #    define SHADOW_OFFSET kPPC64_ShadowOffset64
232 #  elif defined(__s390x__)
233 #    define SHADOW_OFFSET kSystemZ_ShadowOffset64
234 #  elif SANITIZER_FREEBSD
235 #    define SHADOW_OFFSET kFreeBSD_ShadowOffset64
236 #  elif SANITIZER_NETBSD
237 #    define SHADOW_OFFSET kNetBSD_ShadowOffset64
238 #  elif SANITIZER_MAC
239 #   define SHADOW_OFFSET kDefaultShadowOffset64
240 #  elif defined(__mips64)
241 #   define SHADOW_OFFSET kMIPS64_ShadowOffset64
242 #elif defined(__sparc__)
243 #define SHADOW_OFFSET kSPARC64_ShadowOffset64
244 #  elif SANITIZER_WINDOWS64
245 #   define SHADOW_OFFSET __asan_shadow_memory_dynamic_address
246 #  else
247 #   define SHADOW_OFFSET kDefaultShort64bitShadowOffset
248 #  endif
249 #endif
250 
251 #if SANITIZER_ANDROID && defined(__arm__)
252 # define ASAN_PREMAP_SHADOW 1
253 #else
254 # define ASAN_PREMAP_SHADOW 0
255 #endif
256 
257 #define SHADOW_GRANULARITY (1ULL << SHADOW_SCALE)
258 
259 #define DO_ASAN_MAPPING_PROFILE 0  // Set to 1 to profile the functions below.
260 
261 #if DO_ASAN_MAPPING_PROFILE
262 # define PROFILE_ASAN_MAPPING() AsanMappingProfile[__LINE__]++;
263 #else
264 # define PROFILE_ASAN_MAPPING()
265 #endif
266 
267 // If 1, all shadow boundaries are constants.
268 // Don't set to 1 other than for testing.
269 #define ASAN_FIXED_MAPPING 0
270 
271 namespace __asan {
272 
273 extern uptr AsanMappingProfile[];
274 
275 #if ASAN_FIXED_MAPPING
276 // Fixed mapping for 64-bit Linux. Mostly used for performance comparison
277 // with non-fixed mapping. As of r175253 (Feb 2013) the performance
278 // difference between fixed and non-fixed mapping is below the noise level.
279 static uptr kHighMemEnd = 0x7fffffffffffULL;
280 static uptr kMidMemBeg =    0x3000000000ULL;
281 static uptr kMidMemEnd =    0x4fffffffffULL;
282 #else
283 extern uptr kHighMemEnd, kMidMemBeg, kMidMemEnd;  // Initialized in __asan_init.
284 #endif
285 
286 }  // namespace __asan
287 
288 #if SANITIZER_MYRIAD2
289 #include "asan_mapping_myriad.h"
290 #elif defined(__sparc__) && SANITIZER_WORDSIZE == 64
291 #include "asan_mapping_sparc64.h"
292 #else
293 #define MEM_TO_SHADOW(mem) (((mem) >> SHADOW_SCALE) + (SHADOW_OFFSET))
294 
295 #define kLowMemBeg      0
296 #define kLowMemEnd      (SHADOW_OFFSET ? SHADOW_OFFSET - 1 : 0)
297 
298 #define kLowShadowBeg   SHADOW_OFFSET
299 #define kLowShadowEnd   MEM_TO_SHADOW(kLowMemEnd)
300 
301 #define kHighMemBeg     (MEM_TO_SHADOW(kHighMemEnd) + 1)
302 
303 #define kHighShadowBeg  MEM_TO_SHADOW(kHighMemBeg)
304 #define kHighShadowEnd  MEM_TO_SHADOW(kHighMemEnd)
305 
306 # define kMidShadowBeg MEM_TO_SHADOW(kMidMemBeg)
307 # define kMidShadowEnd MEM_TO_SHADOW(kMidMemEnd)
308 
309 // With the zero shadow base we can not actually map pages starting from 0.
310 // This constant is somewhat arbitrary.
311 #define kZeroBaseShadowStart 0
312 #define kZeroBaseMaxShadowStart (1 << 18)
313 
314 #define kShadowGapBeg   (kLowShadowEnd ? kLowShadowEnd + 1 \
315                                        : kZeroBaseShadowStart)
316 #define kShadowGapEnd   ((kMidMemBeg ? kMidShadowBeg : kHighShadowBeg) - 1)
317 
318 #define kShadowGap2Beg (kMidMemBeg ? kMidShadowEnd + 1 : 0)
319 #define kShadowGap2End (kMidMemBeg ? kMidMemBeg - 1 : 0)
320 
321 #define kShadowGap3Beg (kMidMemBeg ? kMidMemEnd + 1 : 0)
322 #define kShadowGap3End (kMidMemBeg ? kHighShadowBeg - 1 : 0)
323 
324 namespace __asan {
325 
326 static inline bool AddrIsInLowMem(uptr a) {
327   PROFILE_ASAN_MAPPING();
328   return a <= kLowMemEnd;
329 }
330 
331 static inline bool AddrIsInLowShadow(uptr a) {
332   PROFILE_ASAN_MAPPING();
333   return a >= kLowShadowBeg && a <= kLowShadowEnd;
334 }
335 
336 static inline bool AddrIsInMidMem(uptr a) {
337   PROFILE_ASAN_MAPPING();
338   return kMidMemBeg && a >= kMidMemBeg && a <= kMidMemEnd;
339 }
340 
341 static inline bool AddrIsInMidShadow(uptr a) {
342   PROFILE_ASAN_MAPPING();
343   return kMidMemBeg && a >= kMidShadowBeg && a <= kMidShadowEnd;
344 }
345 
346 static inline bool AddrIsInHighMem(uptr a) {
347   PROFILE_ASAN_MAPPING();
348   return kHighMemBeg && a >= kHighMemBeg && a <= kHighMemEnd;
349 }
350 
351 static inline bool AddrIsInHighShadow(uptr a) {
352   PROFILE_ASAN_MAPPING();
353   return kHighMemBeg && a >= kHighShadowBeg && a <= kHighShadowEnd;
354 }
355 
356 static inline bool AddrIsInShadowGap(uptr a) {
357   PROFILE_ASAN_MAPPING();
358   if (kMidMemBeg) {
359     if (a <= kShadowGapEnd)
360       return SHADOW_OFFSET == 0 || a >= kShadowGapBeg;
361     return (a >= kShadowGap2Beg && a <= kShadowGap2End) ||
362            (a >= kShadowGap3Beg && a <= kShadowGap3End);
363   }
364   // In zero-based shadow mode we treat addresses near zero as addresses
365   // in shadow gap as well.
366   if (SHADOW_OFFSET == 0)
367     return a <= kShadowGapEnd;
368   return a >= kShadowGapBeg && a <= kShadowGapEnd;
369 }
370 
371 }  // namespace __asan
372 
373 #endif  // SANITIZER_MYRIAD2
374 
375 namespace __asan {
376 
377 static inline uptr MemToShadowSize(uptr size) { return size >> SHADOW_SCALE; }
378 
379 static inline bool AddrIsInMem(uptr a) {
380   PROFILE_ASAN_MAPPING();
381   return AddrIsInLowMem(a) || AddrIsInMidMem(a) || AddrIsInHighMem(a) ||
382       (flags()->protect_shadow_gap == 0 && AddrIsInShadowGap(a));
383 }
384 
385 static inline uptr MemToShadow(uptr p) {
386   PROFILE_ASAN_MAPPING();
387   CHECK(AddrIsInMem(p));
388   return MEM_TO_SHADOW(p);
389 }
390 
391 static inline bool AddrIsInShadow(uptr a) {
392   PROFILE_ASAN_MAPPING();
393   return AddrIsInLowShadow(a) || AddrIsInMidShadow(a) || AddrIsInHighShadow(a);
394 }
395 
396 static inline bool AddrIsAlignedByGranularity(uptr a) {
397   PROFILE_ASAN_MAPPING();
398   return (a & (SHADOW_GRANULARITY - 1)) == 0;
399 }
400 
401 static inline bool AddressIsPoisoned(uptr a) {
402   PROFILE_ASAN_MAPPING();
403   if (SANITIZER_MYRIAD2 && !AddrIsInMem(a) && !AddrIsInShadow(a))
404     return false;
405   const uptr kAccessSize = 1;
406   u8 *shadow_address = (u8*)MEM_TO_SHADOW(a);
407   s8 shadow_value = *shadow_address;
408   if (shadow_value) {
409     u8 last_accessed_byte = (a & (SHADOW_GRANULARITY - 1))
410                                  + kAccessSize - 1;
411     return (last_accessed_byte >= shadow_value);
412   }
413   return false;
414 }
415 
416 // Must be after all calls to PROFILE_ASAN_MAPPING().
417 static const uptr kAsanMappingProfileSize = __LINE__;
418 
419 }  // namespace __asan
420 
421 #endif  // ASAN_MAPPING_H
422