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