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