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