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