• 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 #if ASAN_FLEXIBLE_MAPPING_AND_OFFSET == 1
23 extern __attribute__((visibility("default"))) uintptr_t __asan_mapping_scale;
24 extern __attribute__((visibility("default"))) uintptr_t __asan_mapping_offset;
25 # define SHADOW_SCALE (__asan_mapping_scale)
26 # define SHADOW_OFFSET (__asan_mapping_offset)
27 #else
28 # ifdef ANDROID
29 #  define SHADOW_SCALE (3)
30 #  define SHADOW_OFFSET (0)
31 # else
32 #  define SHADOW_SCALE (3)
33 #  if __WORDSIZE == 32
34 #   define SHADOW_OFFSET (1 << 29)
35 #  else
36 #   define SHADOW_OFFSET (1ULL << 44)
37 #  endif
38 # endif
39 #endif  // ASAN_FLEXIBLE_MAPPING_AND_OFFSET
40 
41 #define SHADOW_GRANULARITY (1ULL << SHADOW_SCALE)
42 #define MEM_TO_SHADOW(mem) (((mem) >> SHADOW_SCALE) | (SHADOW_OFFSET))
43 
44 #if __WORDSIZE == 64
45   static const size_t kHighMemEnd = 0x00007fffffffffffUL;
46 #else  // __WORDSIZE == 32
47   static const size_t kHighMemEnd = 0xffffffff;
48 #endif  // __WORDSIZE
49 
50 
51 #define kLowMemBeg      0
52 #define kLowMemEnd      (SHADOW_OFFSET ? SHADOW_OFFSET - 1 : 0)
53 
54 #define kLowShadowBeg   SHADOW_OFFSET
55 #define kLowShadowEnd   MEM_TO_SHADOW(kLowMemEnd)
56 
57 #define kHighMemBeg     (MEM_TO_SHADOW(kHighMemEnd) + 1)
58 
59 #define kHighShadowBeg  MEM_TO_SHADOW(kHighMemBeg)
60 #define kHighShadowEnd  MEM_TO_SHADOW(kHighMemEnd)
61 
62 #define kShadowGapBeg   (kLowShadowEnd ? kLowShadowEnd + 1 : 16 * kPageSize)
63 #define kShadowGapEnd   (kHighShadowBeg - 1)
64 
65 #define kGlobalAndStackRedzone \
66       (SHADOW_GRANULARITY < 32 ? 32 : SHADOW_GRANULARITY)
67 
68 namespace __asan {
69 
AddrIsInLowMem(uintptr_t a)70 static inline bool AddrIsInLowMem(uintptr_t a) {
71   return a < kLowMemEnd;
72 }
73 
AddrIsInLowShadow(uintptr_t a)74 static inline bool AddrIsInLowShadow(uintptr_t a) {
75   return a >= kLowShadowBeg && a <= kLowShadowEnd;
76 }
77 
AddrIsInHighMem(uintptr_t a)78 static inline bool AddrIsInHighMem(uintptr_t a) {
79   return a >= kHighMemBeg && a <= kHighMemEnd;
80 }
81 
AddrIsInMem(uintptr_t a)82 static inline bool AddrIsInMem(uintptr_t a) {
83   return AddrIsInLowMem(a) || AddrIsInHighMem(a);
84 }
85 
MemToShadow(uintptr_t p)86 static inline uintptr_t MemToShadow(uintptr_t p) {
87   CHECK(AddrIsInMem(p));
88   return MEM_TO_SHADOW(p);
89 }
90 
AddrIsInHighShadow(uintptr_t a)91 static inline bool AddrIsInHighShadow(uintptr_t a) {
92   return a >= kHighShadowBeg && a <=  kHighMemEnd;
93 }
94 
AddrIsInShadow(uintptr_t a)95 static inline bool AddrIsInShadow(uintptr_t a) {
96   return AddrIsInLowShadow(a) || AddrIsInHighShadow(a);
97 }
98 
AddrIsAlignedByGranularity(uintptr_t a)99 static inline bool AddrIsAlignedByGranularity(uintptr_t a) {
100   return (a & (SHADOW_GRANULARITY - 1)) == 0;
101 }
102 
AddressIsPoisoned(uintptr_t a)103 static inline bool AddressIsPoisoned(uintptr_t a) {
104   const size_t kAccessSize = 1;
105   uint8_t *shadow_address = (uint8_t*)MemToShadow(a);
106   int8_t shadow_value = *shadow_address;
107   if (shadow_value) {
108     uint8_t last_accessed_byte = (a & (SHADOW_GRANULARITY - 1))
109                                  + kAccessSize - 1;
110     return (last_accessed_byte >= shadow_value);
111   }
112   return false;
113 }
114 
115 }  // namespace __asan
116 
117 #endif  // ASAN_MAPPING_H
118