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: 47 // || `[0x40000000, 0xffffffff]` || HighMem || 48 // || `[0x28000000, 0x3fffffff]` || HighShadow || 49 // || `[0x24000000, 0x27ffffff]` || ShadowGap || 50 // || `[0x20000000, 0x23ffffff]` || LowShadow || 51 // || `[0x00000000, 0x1fffffff]` || LowMem || 52 // 53 // Default Linux/MIPS mapping: 54 // || `[0x2aaa8000, 0xffffffff]` || HighMem || 55 // || `[0x0fffd000, 0x2aaa7fff]` || HighShadow || 56 // || `[0x0bffd000, 0x0fffcfff]` || ShadowGap || 57 // || `[0x0aaa8000, 0x0bffcfff]` || LowShadow || 58 // || `[0x00000000, 0x0aaa7fff]` || LowMem || 59 60 #if ASAN_FLEXIBLE_MAPPING_AND_OFFSET == 1 61 extern SANITIZER_INTERFACE_ATTRIBUTE uptr __asan_mapping_scale; 62 extern SANITIZER_INTERFACE_ATTRIBUTE uptr __asan_mapping_offset; 63 # define SHADOW_SCALE (__asan_mapping_scale) 64 # define SHADOW_OFFSET (__asan_mapping_offset) 65 #else 66 # if SANITIZER_ANDROID 67 # define SHADOW_SCALE (3) 68 # define SHADOW_OFFSET (0) 69 # else 70 # define SHADOW_SCALE (3) 71 # if SANITIZER_WORDSIZE == 32 72 # if defined(__mips__) 73 # define SHADOW_OFFSET 0x0aaa8000 74 # else 75 # define SHADOW_OFFSET (1 << 29) 76 # endif 77 # else 78 # if defined(__powerpc64__) 79 # define SHADOW_OFFSET (1ULL << 41) 80 # else 81 # if SANITIZER_MAC 82 # define SHADOW_OFFSET (1ULL << 44) 83 # else 84 # define SHADOW_OFFSET 0x7fff8000ULL 85 # endif 86 # endif 87 # endif 88 # endif 89 #endif // ASAN_FLEXIBLE_MAPPING_AND_OFFSET 90 91 #define SHADOW_GRANULARITY (1ULL << SHADOW_SCALE) 92 #define MEM_TO_SHADOW(mem) (((mem) >> SHADOW_SCALE) + (SHADOW_OFFSET)) 93 #define SHADOW_TO_MEM(shadow) (((shadow) - SHADOW_OFFSET) << SHADOW_SCALE) 94 95 #define kLowMemBeg 0 96 #define kLowMemEnd (SHADOW_OFFSET ? SHADOW_OFFSET - 1 : 0) 97 98 #define kLowShadowBeg SHADOW_OFFSET 99 #define kLowShadowEnd MEM_TO_SHADOW(kLowMemEnd) 100 101 #define kHighMemBeg (MEM_TO_SHADOW(kHighMemEnd) + 1) 102 103 #define kHighShadowBeg MEM_TO_SHADOW(kHighMemBeg) 104 #define kHighShadowEnd MEM_TO_SHADOW(kHighMemEnd) 105 106 # define kMidShadowBeg MEM_TO_SHADOW(kMidMemBeg) 107 # define kMidShadowEnd MEM_TO_SHADOW(kMidMemEnd) 108 109 // With the zero shadow base we can not actually map pages starting from 0. 110 // This constant is somewhat arbitrary. 111 #define kZeroBaseShadowStart (1 << 18) 112 113 #define kShadowGapBeg (kLowShadowEnd ? kLowShadowEnd + 1 \ 114 : kZeroBaseShadowStart) 115 #define kShadowGapEnd ((kMidMemBeg ? kMidShadowBeg : kHighShadowBeg) - 1) 116 117 #define kShadowGap2Beg (kMidMemBeg ? kMidShadowEnd + 1 : 0) 118 #define kShadowGap2End (kMidMemBeg ? kMidMemBeg - 1 : 0) 119 120 #define kShadowGap3Beg (kMidMemBeg ? kMidMemEnd + 1 : 0) 121 #define kShadowGap3End (kMidMemBeg ? kHighShadowBeg - 1 : 0) 122 123 #define DO_ASAN_MAPPING_PROFILE 0 // Set to 1 to profile the functions below. 124 125 #if DO_ASAN_MAPPING_PROFILE 126 # define PROFILE_ASAN_MAPPING() AsanMappingProfile[__LINE__]++; 127 #else 128 # define PROFILE_ASAN_MAPPING() 129 #endif 130 131 // If 1, all shadow boundaries are constants. 132 // Don't set to 1 other than for testing. 133 #define ASAN_FIXED_MAPPING 0 134 135 namespace __asan { 136 137 extern uptr AsanMappingProfile[]; 138 139 #if ASAN_FIXED_MAPPING 140 // Fixed mapping for 64-bit Linux. Mostly used for performance comparison 141 // with non-fixed mapping. As of r175253 (Feb 2013) the performance 142 // difference between fixed and non-fixed mapping is below the noise level. 143 static uptr kHighMemEnd = 0x7fffffffffffULL; 144 static uptr kMidMemBeg = 0x3000000000ULL; 145 static uptr kMidMemEnd = 0x4fffffffffULL; 146 #else 147 SANITIZER_INTERFACE_ATTRIBUTE 148 extern uptr kHighMemEnd, kMidMemBeg, kMidMemEnd; // Initialized in __asan_init. 149 #endif 150 151 static inline bool AddrIsInLowMem(uptr a) { 152 PROFILE_ASAN_MAPPING(); 153 return a < kLowMemEnd; 154 } 155 156 static inline bool AddrIsInLowShadow(uptr a) { 157 PROFILE_ASAN_MAPPING(); 158 return a >= kLowShadowBeg && a <= kLowShadowEnd; 159 } 160 161 static inline bool AddrIsInHighMem(uptr a) { 162 PROFILE_ASAN_MAPPING(); 163 return a >= kHighMemBeg && a <= kHighMemEnd; 164 } 165 166 static inline bool AddrIsInMidMem(uptr a) { 167 PROFILE_ASAN_MAPPING(); 168 return kMidMemBeg && a >= kMidMemBeg && a <= kMidMemEnd; 169 } 170 171 static inline bool AddrIsInMem(uptr a) { 172 PROFILE_ASAN_MAPPING(); 173 return AddrIsInLowMem(a) || AddrIsInMidMem(a) || AddrIsInHighMem(a); 174 } 175 176 static inline uptr MemToShadow(uptr p) { 177 PROFILE_ASAN_MAPPING(); 178 CHECK(AddrIsInMem(p)); 179 return MEM_TO_SHADOW(p); 180 } 181 182 static inline bool AddrIsInHighShadow(uptr a) { 183 PROFILE_ASAN_MAPPING(); 184 return a >= kHighShadowBeg && a <= kHighMemEnd; 185 } 186 187 static inline bool AddrIsInMidShadow(uptr a) { 188 PROFILE_ASAN_MAPPING(); 189 return kMidMemBeg && a >= kMidShadowBeg && a <= kMidMemEnd; 190 } 191 192 static inline bool AddrIsInShadow(uptr a) { 193 PROFILE_ASAN_MAPPING(); 194 return AddrIsInLowShadow(a) || AddrIsInMidShadow(a) || AddrIsInHighShadow(a); 195 } 196 197 static inline bool AddrIsInShadowGap(uptr a) { 198 PROFILE_ASAN_MAPPING(); 199 if (kMidMemBeg) { 200 if (a <= kShadowGapEnd) 201 return SHADOW_OFFSET == 0 || a >= kShadowGapBeg; 202 return (a >= kShadowGap2Beg && a <= kShadowGap2End) || 203 (a >= kShadowGap3Beg && a <= kShadowGap3End); 204 } 205 // In zero-based shadow mode we treat addresses near zero as addresses 206 // in shadow gap as well. 207 if (SHADOW_OFFSET == 0) 208 return a <= kShadowGapEnd; 209 return a >= kShadowGapBeg && a <= kShadowGapEnd; 210 } 211 212 static inline bool AddrIsAlignedByGranularity(uptr a) { 213 PROFILE_ASAN_MAPPING(); 214 return (a & (SHADOW_GRANULARITY - 1)) == 0; 215 } 216 217 static inline bool AddressIsPoisoned(uptr a) { 218 PROFILE_ASAN_MAPPING(); 219 const uptr kAccessSize = 1; 220 u8 *shadow_address = (u8*)MEM_TO_SHADOW(a); 221 s8 shadow_value = *shadow_address; 222 if (shadow_value) { 223 u8 last_accessed_byte = (a & (SHADOW_GRANULARITY - 1)) 224 + kAccessSize - 1; 225 return (last_accessed_byte >= shadow_value); 226 } 227 return false; 228 } 229 230 // Must be after all calls to PROFILE_ASAN_MAPPING(). 231 static const uptr kAsanMappingProfileSize = __LINE__; 232 233 } // namespace __asan 234 235 #endif // ASAN_MAPPING_H 236