1 //===-- asan_mapping.h ------------------------------------------*- C++ -*-===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // This file is a part of AddressSanitizer, an address sanity checker.
11 //
12 // Defines ASan memory mapping.
13 //===----------------------------------------------------------------------===//
14 #ifndef ASAN_MAPPING_H
15 #define ASAN_MAPPING_H
16 
17 #include "asan_internal.h"
18 
19 // The full explanation of the memory mapping could be found here:
20 // https://github.com/google/sanitizers/wiki/AddressSanitizerAlgorithm
21 //
22 // Typical shadow mapping on Linux/x86_64 with SHADOW_OFFSET == 0x00007fff8000:
23 // || `[0x10007fff8000, 0x7fffffffffff]` || HighMem    ||
24 // || `[0x02008fff7000, 0x10007fff7fff]` || HighShadow ||
25 // || `[0x00008fff7000, 0x02008fff6fff]` || ShadowGap  ||
26 // || `[0x00007fff8000, 0x00008fff6fff]` || LowShadow  ||
27 // || `[0x000000000000, 0x00007fff7fff]` || LowMem     ||
28 //
29 // When SHADOW_OFFSET is zero (-pie):
30 // || `[0x100000000000, 0x7fffffffffff]` || HighMem    ||
31 // || `[0x020000000000, 0x0fffffffffff]` || HighShadow ||
32 // || `[0x000000040000, 0x01ffffffffff]` || ShadowGap  ||
33 //
34 // Special case when something is already mapped between
35 // 0x003000000000 and 0x005000000000 (e.g. when prelink is installed):
36 // || `[0x10007fff8000, 0x7fffffffffff]` || HighMem    ||
37 // || `[0x02008fff7000, 0x10007fff7fff]` || HighShadow ||
38 // || `[0x005000000000, 0x02008fff6fff]` || ShadowGap3 ||
39 // || `[0x003000000000, 0x004fffffffff]` || MidMem     ||
40 // || `[0x000a7fff8000, 0x002fffffffff]` || ShadowGap2 ||
41 // || `[0x00067fff8000, 0x000a7fff7fff]` || MidShadow  ||
42 // || `[0x00008fff7000, 0x00067fff7fff]` || ShadowGap  ||
43 // || `[0x00007fff8000, 0x00008fff6fff]` || LowShadow  ||
44 // || `[0x000000000000, 0x00007fff7fff]` || LowMem     ||
45 //
46 // Default Linux/i386 mapping on x86_64 machine:
47 // || `[0x40000000, 0xffffffff]` || HighMem    ||
48 // || `[0x28000000, 0x3fffffff]` || HighShadow ||
49 // || `[0x24000000, 0x27ffffff]` || ShadowGap  ||
50 // || `[0x20000000, 0x23ffffff]` || LowShadow  ||
51 // || `[0x00000000, 0x1fffffff]` || LowMem     ||
52 //
53 // Default Linux/i386 mapping on i386 machine
54 // (addresses starting with 0xc0000000 are reserved
55 // for kernel and thus not sanitized):
56 // || `[0x38000000, 0xbfffffff]` || HighMem    ||
57 // || `[0x27000000, 0x37ffffff]` || HighShadow ||
58 // || `[0x24000000, 0x26ffffff]` || ShadowGap  ||
59 // || `[0x20000000, 0x23ffffff]` || LowShadow  ||
60 // || `[0x00000000, 0x1fffffff]` || LowMem     ||
61 //
62 // Default Linux/MIPS32 mapping:
63 // || `[0x2aaa0000, 0xffffffff]` || HighMem    ||
64 // || `[0x0fff4000, 0x2aa9ffff]` || HighShadow ||
65 // || `[0x0bff4000, 0x0fff3fff]` || ShadowGap  ||
66 // || `[0x0aaa0000, 0x0bff3fff]` || LowShadow  ||
67 // || `[0x00000000, 0x0aa9ffff]` || LowMem     ||
68 //
69 // Default Linux/MIPS64 mapping:
70 // || `[0x4000000000, 0xffffffffff]` || HighMem    ||
71 // || `[0x2800000000, 0x3fffffffff]` || HighShadow ||
72 // || `[0x2400000000, 0x27ffffffff]` || ShadowGap  ||
73 // || `[0x2000000000, 0x23ffffffff]` || LowShadow  ||
74 // || `[0x0000000000, 0x1fffffffff]` || LowMem     ||
75 //
76 // Default Linux/AArch64 (39-bit VMA) mapping:
77 // || `[0x2000000000, 0x7fffffffff]` || highmem    ||
78 // || `[0x1400000000, 0x1fffffffff]` || highshadow ||
79 // || `[0x1200000000, 0x13ffffffff]` || shadowgap  ||
80 // || `[0x1000000000, 0x11ffffffff]` || lowshadow  ||
81 // || `[0x0000000000, 0x0fffffffff]` || lowmem     ||
82 //
83 // Default Linux/AArch64 (42-bit VMA) mapping:
84 // || `[0x10000000000, 0x3ffffffffff]` || highmem    ||
85 // || `[0x0a000000000, 0x0ffffffffff]` || highshadow ||
86 // || `[0x09000000000, 0x09fffffffff]` || shadowgap  ||
87 // || `[0x08000000000, 0x08fffffffff]` || lowshadow  ||
88 // || `[0x00000000000, 0x07fffffffff]` || lowmem     ||
89 //
90 // Shadow mapping on FreeBSD/x86-64 with SHADOW_OFFSET == 0x400000000000:
91 // || `[0x500000000000, 0x7fffffffffff]` || HighMem    ||
92 // || `[0x4a0000000000, 0x4fffffffffff]` || HighShadow ||
93 // || `[0x480000000000, 0x49ffffffffff]` || ShadowGap  ||
94 // || `[0x400000000000, 0x47ffffffffff]` || LowShadow  ||
95 // || `[0x000000000000, 0x3fffffffffff]` || LowMem     ||
96 //
97 // Shadow mapping on FreeBSD/i386 with SHADOW_OFFSET == 0x40000000:
98 // || `[0x60000000, 0xffffffff]` || HighMem    ||
99 // || `[0x4c000000, 0x5fffffff]` || HighShadow ||
100 // || `[0x48000000, 0x4bffffff]` || ShadowGap  ||
101 // || `[0x40000000, 0x47ffffff]` || LowShadow  ||
102 // || `[0x00000000, 0x3fffffff]` || LowMem     ||
103 //
104 // Default Windows/i386 mapping:
105 // (the exact location of HighShadow/HighMem may vary depending
106 //  on WoW64, /LARGEADDRESSAWARE, etc).
107 // || `[0x50000000, 0xffffffff]` || HighMem    ||
108 // || `[0x3a000000, 0x4fffffff]` || HighShadow ||
109 // || `[0x36000000, 0x39ffffff]` || ShadowGap  ||
110 // || `[0x30000000, 0x35ffffff]` || LowShadow  ||
111 // || `[0x00000000, 0x2fffffff]` || LowMem     ||
112 
113 static const u64 kDefaultShadowScale = 3;
114 static const u64 kDefaultShadowOffset32 = 1ULL << 29;  // 0x20000000
115 static const u64 kDefaultShadowOffset64 = 1ULL << 44;
116 static const u64 kDefaultShort64bitShadowOffset = 0x7FFF8000;  // < 2G.
117 static const u64 kIosShadowOffset32 = 1ULL << 30;  // 0x40000000
118 static const u64 kIosShadowOffset64 = 0x130000000;
119 static const u64 kIosSimShadowOffset32 = 1ULL << 30;
120 static const u64 kIosSimShadowOffset64 = kDefaultShadowOffset64;
121 static const u64 kAArch64_ShadowOffset64 = 1ULL << 36;
122 static const u64 kMIPS32_ShadowOffset32 = 0x0aaa0000;
123 static const u64 kMIPS64_ShadowOffset64 = 1ULL << 37;
124 static const u64 kPPC64_ShadowOffset64 = 1ULL << 41;
125 static const u64 kFreeBSD_ShadowOffset32 = 1ULL << 30;  // 0x40000000
126 static const u64 kFreeBSD_ShadowOffset64 = 1ULL << 46;  // 0x400000000000
127 static const u64 kWindowsShadowOffset32 = 3ULL << 28;  // 0x30000000
128 
129 #define SHADOW_SCALE kDefaultShadowScale
130 
131 
132 #if SANITIZER_WORDSIZE == 32
133 #  if SANITIZER_ANDROID
134 #    define SHADOW_OFFSET (0)
135 #  elif defined(__mips__)
136 #    define SHADOW_OFFSET kMIPS32_ShadowOffset32
137 #  elif SANITIZER_FREEBSD
138 #    define SHADOW_OFFSET kFreeBSD_ShadowOffset32
139 #  elif SANITIZER_WINDOWS
140 #    define SHADOW_OFFSET kWindowsShadowOffset32
141 #  elif SANITIZER_IOSSIM
142 #    define SHADOW_OFFSET kIosSimShadowOffset32
143 #  elif SANITIZER_IOS
144 #    define SHADOW_OFFSET kIosShadowOffset32
145 #  else
146 #    define SHADOW_OFFSET kDefaultShadowOffset32
147 #  endif
148 #else
149 #  if defined(__aarch64__)
150 #    define SHADOW_OFFSET kAArch64_ShadowOffset64
151 #  elif defined(__powerpc64__)
152 #    define SHADOW_OFFSET kPPC64_ShadowOffset64
153 #  elif SANITIZER_FREEBSD
154 #    define SHADOW_OFFSET kFreeBSD_ShadowOffset64
155 #  elif SANITIZER_MAC
156 #   define SHADOW_OFFSET kDefaultShadowOffset64
157 #  elif defined(__mips64)
158 #   define SHADOW_OFFSET kMIPS64_ShadowOffset64
159 #  elif SANITIZER_IOSSIM
160 #    define SHADOW_OFFSET kIosSimShadowOffset64
161 #  elif SANITIZER_IOS
162 #    define SHADOW_OFFSET kIosShadowOffset64
163 #  else
164 #   define SHADOW_OFFSET kDefaultShort64bitShadowOffset
165 #  endif
166 #endif
167 
168 #define SHADOW_GRANULARITY (1ULL << SHADOW_SCALE)
169 #define MEM_TO_SHADOW(mem) (((mem) >> SHADOW_SCALE) + (SHADOW_OFFSET))
170 #define SHADOW_TO_MEM(shadow) (((shadow) - SHADOW_OFFSET) << SHADOW_SCALE)
171 
172 #define kLowMemBeg      0
173 #define kLowMemEnd      (SHADOW_OFFSET ? SHADOW_OFFSET - 1 : 0)
174 
175 #define kLowShadowBeg   SHADOW_OFFSET
176 #define kLowShadowEnd   MEM_TO_SHADOW(kLowMemEnd)
177 
178 #define kHighMemBeg     (MEM_TO_SHADOW(kHighMemEnd) + 1)
179 
180 #define kHighShadowBeg  MEM_TO_SHADOW(kHighMemBeg)
181 #define kHighShadowEnd  MEM_TO_SHADOW(kHighMemEnd)
182 
183 # define kMidShadowBeg MEM_TO_SHADOW(kMidMemBeg)
184 # define kMidShadowEnd MEM_TO_SHADOW(kMidMemEnd)
185 
186 // With the zero shadow base we can not actually map pages starting from 0.
187 // This constant is somewhat arbitrary.
188 #define kZeroBaseShadowStart 0
189 #define kZeroBaseMaxShadowStart (1 << 18)
190 
191 #define kShadowGapBeg   (kLowShadowEnd ? kLowShadowEnd + 1 \
192                                        : kZeroBaseShadowStart)
193 #define kShadowGapEnd   ((kMidMemBeg ? kMidShadowBeg : kHighShadowBeg) - 1)
194 
195 #define kShadowGap2Beg (kMidMemBeg ? kMidShadowEnd + 1 : 0)
196 #define kShadowGap2End (kMidMemBeg ? kMidMemBeg - 1 : 0)
197 
198 #define kShadowGap3Beg (kMidMemBeg ? kMidMemEnd + 1 : 0)
199 #define kShadowGap3End (kMidMemBeg ? kHighShadowBeg - 1 : 0)
200 
201 #define DO_ASAN_MAPPING_PROFILE 0  // Set to 1 to profile the functions below.
202 
203 #if DO_ASAN_MAPPING_PROFILE
204 # define PROFILE_ASAN_MAPPING() AsanMappingProfile[__LINE__]++;
205 #else
206 # define PROFILE_ASAN_MAPPING()
207 #endif
208 
209 // If 1, all shadow boundaries are constants.
210 // Don't set to 1 other than for testing.
211 #define ASAN_FIXED_MAPPING 0
212 
213 namespace __asan {
214 
215 extern uptr AsanMappingProfile[];
216 
217 #if ASAN_FIXED_MAPPING
218 // Fixed mapping for 64-bit Linux. Mostly used for performance comparison
219 // with non-fixed mapping. As of r175253 (Feb 2013) the performance
220 // difference between fixed and non-fixed mapping is below the noise level.
221 static uptr kHighMemEnd = 0x7fffffffffffULL;
222 static uptr kMidMemBeg =    0x3000000000ULL;
223 static uptr kMidMemEnd =    0x4fffffffffULL;
224 #else
225 extern uptr kHighMemEnd, kMidMemBeg, kMidMemEnd;  // Initialized in __asan_init.
226 #endif
227 
AddrIsInLowMem(uptr a)228 static inline bool AddrIsInLowMem(uptr a) {
229   PROFILE_ASAN_MAPPING();
230   return a < kLowMemEnd;
231 }
232 
AddrIsInLowShadow(uptr a)233 static inline bool AddrIsInLowShadow(uptr a) {
234   PROFILE_ASAN_MAPPING();
235   return a >= kLowShadowBeg && a <= kLowShadowEnd;
236 }
237 
AddrIsInHighMem(uptr a)238 static inline bool AddrIsInHighMem(uptr a) {
239   PROFILE_ASAN_MAPPING();
240   return a >= kHighMemBeg && a <= kHighMemEnd;
241 }
242 
AddrIsInMidMem(uptr a)243 static inline bool AddrIsInMidMem(uptr a) {
244   PROFILE_ASAN_MAPPING();
245   return kMidMemBeg && a >= kMidMemBeg && a <= kMidMemEnd;
246 }
247 
AddrIsInMem(uptr a)248 static inline bool AddrIsInMem(uptr a) {
249   PROFILE_ASAN_MAPPING();
250   return AddrIsInLowMem(a) || AddrIsInMidMem(a) || AddrIsInHighMem(a);
251 }
252 
MemToShadow(uptr p)253 static inline uptr MemToShadow(uptr p) {
254   PROFILE_ASAN_MAPPING();
255   CHECK(AddrIsInMem(p));
256   return MEM_TO_SHADOW(p);
257 }
258 
AddrIsInHighShadow(uptr a)259 static inline bool AddrIsInHighShadow(uptr a) {
260   PROFILE_ASAN_MAPPING();
261   return a >= kHighShadowBeg && a <= kHighMemEnd;
262 }
263 
AddrIsInMidShadow(uptr a)264 static inline bool AddrIsInMidShadow(uptr a) {
265   PROFILE_ASAN_MAPPING();
266   return kMidMemBeg && a >= kMidShadowBeg && a <= kMidMemEnd;
267 }
268 
AddrIsInShadow(uptr a)269 static inline bool AddrIsInShadow(uptr a) {
270   PROFILE_ASAN_MAPPING();
271   return AddrIsInLowShadow(a) || AddrIsInMidShadow(a) || AddrIsInHighShadow(a);
272 }
273 
AddrIsInShadowGap(uptr a)274 static inline bool AddrIsInShadowGap(uptr a) {
275   PROFILE_ASAN_MAPPING();
276   if (kMidMemBeg) {
277     if (a <= kShadowGapEnd)
278       return SHADOW_OFFSET == 0 || a >= kShadowGapBeg;
279     return (a >= kShadowGap2Beg && a <= kShadowGap2End) ||
280            (a >= kShadowGap3Beg && a <= kShadowGap3End);
281   }
282   // In zero-based shadow mode we treat addresses near zero as addresses
283   // in shadow gap as well.
284   if (SHADOW_OFFSET == 0)
285     return a <= kShadowGapEnd;
286   return a >= kShadowGapBeg && a <= kShadowGapEnd;
287 }
288 
AddrIsAlignedByGranularity(uptr a)289 static inline bool AddrIsAlignedByGranularity(uptr a) {
290   PROFILE_ASAN_MAPPING();
291   return (a & (SHADOW_GRANULARITY - 1)) == 0;
292 }
293 
AddressIsPoisoned(uptr a)294 static inline bool AddressIsPoisoned(uptr a) {
295   PROFILE_ASAN_MAPPING();
296   const uptr kAccessSize = 1;
297   u8 *shadow_address = (u8*)MEM_TO_SHADOW(a);
298   s8 shadow_value = *shadow_address;
299   if (shadow_value) {
300     u8 last_accessed_byte = (a & (SHADOW_GRANULARITY - 1))
301                                  + kAccessSize - 1;
302     return (last_accessed_byte >= shadow_value);
303   }
304   return false;
305 }
306 
307 // Must be after all calls to PROFILE_ASAN_MAPPING().
308 static const uptr kAsanMappingProfileSize = __LINE__;
309 
310 }  // namespace __asan
311 
312 #endif  // ASAN_MAPPING_H
313