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 // http://code.google.com/p/address-sanitizer/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 // Shadow mapping on FreeBSD/x86-64 with SHADOW_OFFSET == 0x400000000000:
77 // || `[0x500000000000, 0x7fffffffffff]` || HighMem    ||
78 // || `[0x4a0000000000, 0x4fffffffffff]` || HighShadow ||
79 // || `[0x480000000000, 0x49ffffffffff]` || ShadowGap  ||
80 // || `[0x400000000000, 0x47ffffffffff]` || LowShadow  ||
81 // || `[0x000000000000, 0x3fffffffffff]` || LowMem     ||
82 //
83 // Shadow mapping on FreeBSD/i386 with SHADOW_OFFSET == 0x40000000:
84 // || `[0x60000000, 0xffffffff]` || HighMem    ||
85 // || `[0x4c000000, 0x5fffffff]` || HighShadow ||
86 // || `[0x48000000, 0x4bffffff]` || ShadowGap  ||
87 // || `[0x40000000, 0x47ffffff]` || LowShadow  ||
88 // || `[0x00000000, 0x3fffffff]` || LowMem     ||
89 //
90 // Default Windows/i386 mapping:
91 // (the exact location of HighShadow/HighMem may vary depending
92 //  on WoW64, /LARGEADDRESSAWARE, etc).
93 // || `[0x50000000, 0xffffffff]` || HighMem    ||
94 // || `[0x3a000000, 0x4fffffff]` || HighShadow ||
95 // || `[0x36000000, 0x39ffffff]` || ShadowGap  ||
96 // || `[0x30000000, 0x35ffffff]` || LowShadow  ||
97 // || `[0x00000000, 0x2fffffff]` || LowMem     ||
98 
99 static const u64 kDefaultShadowScale = 3;
100 static const u64 kDefaultShadowOffset32 = 1ULL << 29;  // 0x20000000
101 static const u64 kIosShadowOffset32 = 1ULL << 30;  // 0x40000000
102 static const u64 kDefaultShadowOffset64 = 1ULL << 44;
103 static const u64 kDefaultShort64bitShadowOffset = 0x7FFF8000;  // < 2G.
104 static const u64 kAArch64_ShadowOffset64 = 1ULL << 36;
105 static const u64 kMIPS32_ShadowOffset32 = 0x0aaa0000;
106 static const u64 kMIPS64_ShadowOffset64 = 1ULL << 37;
107 static const u64 kPPC64_ShadowOffset64 = 1ULL << 41;
108 static const u64 kFreeBSD_ShadowOffset32 = 1ULL << 30;  // 0x40000000
109 static const u64 kFreeBSD_ShadowOffset64 = 1ULL << 46;  // 0x400000000000
110 static const u64 kWindowsShadowOffset32 = 3ULL << 28;  // 0x30000000
111 
112 #define SHADOW_SCALE kDefaultShadowScale
113 #if SANITIZER_ANDROID
114 # define SHADOW_OFFSET (0)
115 #else
116 # if SANITIZER_WORDSIZE == 32
117 #  if defined(__mips__)
118 #    define SHADOW_OFFSET kMIPS32_ShadowOffset32
119 #  elif SANITIZER_FREEBSD
120 #    define SHADOW_OFFSET kFreeBSD_ShadowOffset32
121 #  elif SANITIZER_IOS
122 #    define SHADOW_OFFSET kIosShadowOffset32
123 #  elif SANITIZER_WINDOWS
124 #    define SHADOW_OFFSET kWindowsShadowOffset32
125 #  else
126 #    define SHADOW_OFFSET kDefaultShadowOffset32
127 #  endif
128 # else
129 #  if defined(__aarch64__)
130 #    define SHADOW_OFFSET kAArch64_ShadowOffset64
131 #  elif defined(__powerpc64__)
132 #    define SHADOW_OFFSET kPPC64_ShadowOffset64
133 #  elif SANITIZER_FREEBSD
134 #    define SHADOW_OFFSET kFreeBSD_ShadowOffset64
135 #  elif SANITIZER_MAC
136 #   define SHADOW_OFFSET kDefaultShadowOffset64
137 #  elif defined(__mips64)
138 #   define SHADOW_OFFSET kMIPS64_ShadowOffset64
139 #  else
140 #   define SHADOW_OFFSET kDefaultShort64bitShadowOffset
141 #  endif
142 # endif
143 #endif
144 
145 #define SHADOW_GRANULARITY (1ULL << SHADOW_SCALE)
146 #define MEM_TO_SHADOW(mem) (((mem) >> SHADOW_SCALE) + (SHADOW_OFFSET))
147 #define SHADOW_TO_MEM(shadow) (((shadow) - SHADOW_OFFSET) << SHADOW_SCALE)
148 
149 #define kLowMemBeg      0
150 #define kLowMemEnd      (SHADOW_OFFSET ? SHADOW_OFFSET - 1 : 0)
151 
152 #define kLowShadowBeg   SHADOW_OFFSET
153 #define kLowShadowEnd   MEM_TO_SHADOW(kLowMemEnd)
154 
155 #define kHighMemBeg     (MEM_TO_SHADOW(kHighMemEnd) + 1)
156 
157 #define kHighShadowBeg  MEM_TO_SHADOW(kHighMemBeg)
158 #define kHighShadowEnd  MEM_TO_SHADOW(kHighMemEnd)
159 
160 # define kMidShadowBeg MEM_TO_SHADOW(kMidMemBeg)
161 # define kMidShadowEnd MEM_TO_SHADOW(kMidMemEnd)
162 
163 // With the zero shadow base we can not actually map pages starting from 0.
164 // This constant is somewhat arbitrary.
165 #define kZeroBaseShadowStart (1 << 18)
166 
167 #define kShadowGapBeg   (kLowShadowEnd ? kLowShadowEnd + 1 \
168                                        : kZeroBaseShadowStart)
169 #define kShadowGapEnd   ((kMidMemBeg ? kMidShadowBeg : kHighShadowBeg) - 1)
170 
171 #define kShadowGap2Beg (kMidMemBeg ? kMidShadowEnd + 1 : 0)
172 #define kShadowGap2End (kMidMemBeg ? kMidMemBeg - 1 : 0)
173 
174 #define kShadowGap3Beg (kMidMemBeg ? kMidMemEnd + 1 : 0)
175 #define kShadowGap3End (kMidMemBeg ? kHighShadowBeg - 1 : 0)
176 
177 #define DO_ASAN_MAPPING_PROFILE 0  // Set to 1 to profile the functions below.
178 
179 #if DO_ASAN_MAPPING_PROFILE
180 # define PROFILE_ASAN_MAPPING() AsanMappingProfile[__LINE__]++;
181 #else
182 # define PROFILE_ASAN_MAPPING()
183 #endif
184 
185 // If 1, all shadow boundaries are constants.
186 // Don't set to 1 other than for testing.
187 #define ASAN_FIXED_MAPPING 0
188 
189 namespace __asan {
190 
191 extern uptr AsanMappingProfile[];
192 
193 #if ASAN_FIXED_MAPPING
194 // Fixed mapping for 64-bit Linux. Mostly used for performance comparison
195 // with non-fixed mapping. As of r175253 (Feb 2013) the performance
196 // difference between fixed and non-fixed mapping is below the noise level.
197 static uptr kHighMemEnd = 0x7fffffffffffULL;
198 static uptr kMidMemBeg =    0x3000000000ULL;
199 static uptr kMidMemEnd =    0x4fffffffffULL;
200 #else
201 extern uptr kHighMemEnd, kMidMemBeg, kMidMemEnd;  // Initialized in __asan_init.
202 #endif
203 
AddrIsInLowMem(uptr a)204 static inline bool AddrIsInLowMem(uptr a) {
205   PROFILE_ASAN_MAPPING();
206   return a < kLowMemEnd;
207 }
208 
AddrIsInLowShadow(uptr a)209 static inline bool AddrIsInLowShadow(uptr a) {
210   PROFILE_ASAN_MAPPING();
211   return a >= kLowShadowBeg && a <= kLowShadowEnd;
212 }
213 
AddrIsInHighMem(uptr a)214 static inline bool AddrIsInHighMem(uptr a) {
215   PROFILE_ASAN_MAPPING();
216   return a >= kHighMemBeg && a <= kHighMemEnd;
217 }
218 
AddrIsInMidMem(uptr a)219 static inline bool AddrIsInMidMem(uptr a) {
220   PROFILE_ASAN_MAPPING();
221   return kMidMemBeg && a >= kMidMemBeg && a <= kMidMemEnd;
222 }
223 
AddrIsInMem(uptr a)224 static inline bool AddrIsInMem(uptr a) {
225   PROFILE_ASAN_MAPPING();
226   return AddrIsInLowMem(a) || AddrIsInMidMem(a) || AddrIsInHighMem(a);
227 }
228 
MemToShadow(uptr p)229 static inline uptr MemToShadow(uptr p) {
230   PROFILE_ASAN_MAPPING();
231   CHECK(AddrIsInMem(p));
232   return MEM_TO_SHADOW(p);
233 }
234 
AddrIsInHighShadow(uptr a)235 static inline bool AddrIsInHighShadow(uptr a) {
236   PROFILE_ASAN_MAPPING();
237   return a >= kHighShadowBeg && a <= kHighMemEnd;
238 }
239 
AddrIsInMidShadow(uptr a)240 static inline bool AddrIsInMidShadow(uptr a) {
241   PROFILE_ASAN_MAPPING();
242   return kMidMemBeg && a >= kMidShadowBeg && a <= kMidMemEnd;
243 }
244 
AddrIsInShadow(uptr a)245 static inline bool AddrIsInShadow(uptr a) {
246   PROFILE_ASAN_MAPPING();
247   return AddrIsInLowShadow(a) || AddrIsInMidShadow(a) || AddrIsInHighShadow(a);
248 }
249 
AddrIsInShadowGap(uptr a)250 static inline bool AddrIsInShadowGap(uptr a) {
251   PROFILE_ASAN_MAPPING();
252   if (kMidMemBeg) {
253     if (a <= kShadowGapEnd)
254       return SHADOW_OFFSET == 0 || a >= kShadowGapBeg;
255     return (a >= kShadowGap2Beg && a <= kShadowGap2End) ||
256            (a >= kShadowGap3Beg && a <= kShadowGap3End);
257   }
258   // In zero-based shadow mode we treat addresses near zero as addresses
259   // in shadow gap as well.
260   if (SHADOW_OFFSET == 0)
261     return a <= kShadowGapEnd;
262   return a >= kShadowGapBeg && a <= kShadowGapEnd;
263 }
264 
AddrIsAlignedByGranularity(uptr a)265 static inline bool AddrIsAlignedByGranularity(uptr a) {
266   PROFILE_ASAN_MAPPING();
267   return (a & (SHADOW_GRANULARITY - 1)) == 0;
268 }
269 
AddressIsPoisoned(uptr a)270 static inline bool AddressIsPoisoned(uptr a) {
271   PROFILE_ASAN_MAPPING();
272   const uptr kAccessSize = 1;
273   u8 *shadow_address = (u8*)MEM_TO_SHADOW(a);
274   s8 shadow_value = *shadow_address;
275   if (shadow_value) {
276     u8 last_accessed_byte = (a & (SHADOW_GRANULARITY - 1))
277                                  + kAccessSize - 1;
278     return (last_accessed_byte >= shadow_value);
279   }
280   return false;
281 }
282 
283 // Must be after all calls to PROFILE_ASAN_MAPPING().
284 static const uptr kAsanMappingProfileSize = __LINE__;
285 
286 }  // namespace __asan
287 
288 #endif  // ASAN_MAPPING_H
289