2012-11-12 16:53:47 +01:00
|
|
|
//===-- asan_mapping.h ------------------------------------------*- C++ -*-===//
|
|
|
|
//
|
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// This file is a part of AddressSanitizer, an address sanity checker.
|
|
|
|
//
|
|
|
|
// Defines ASan memory mapping.
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
#ifndef ASAN_MAPPING_H
|
|
|
|
#define ASAN_MAPPING_H
|
|
|
|
|
|
|
|
#include "asan_internal.h"
|
|
|
|
|
|
|
|
// The full explanation of the memory mapping could be found here:
|
|
|
|
// http://code.google.com/p/address-sanitizer/wiki/AddressSanitizerAlgorithm
|
2013-02-21 11:57:10 +01:00
|
|
|
//
|
|
|
|
// Typical shadow mapping on Linux/x86_64 with SHADOW_OFFSET == 0x00007fff8000:
|
|
|
|
// || `[0x10007fff8000, 0x7fffffffffff]` || HighMem ||
|
|
|
|
// || `[0x02008fff7000, 0x10007fff7fff]` || HighShadow ||
|
|
|
|
// || `[0x00008fff7000, 0x02008fff6fff]` || ShadowGap ||
|
|
|
|
// || `[0x00007fff8000, 0x00008fff6fff]` || LowShadow ||
|
|
|
|
// || `[0x000000000000, 0x00007fff7fff]` || LowMem ||
|
|
|
|
//
|
|
|
|
// When SHADOW_OFFSET is zero (-pie):
|
|
|
|
// || `[0x100000000000, 0x7fffffffffff]` || HighMem ||
|
|
|
|
// || `[0x020000000000, 0x0fffffffffff]` || HighShadow ||
|
|
|
|
// || `[0x000000040000, 0x01ffffffffff]` || ShadowGap ||
|
|
|
|
//
|
|
|
|
// Special case when something is already mapped between
|
|
|
|
// 0x003000000000 and 0x004000000000 (e.g. when prelink is installed):
|
|
|
|
// || `[0x10007fff8000, 0x7fffffffffff]` || HighMem ||
|
|
|
|
// || `[0x02008fff7000, 0x10007fff7fff]` || HighShadow ||
|
|
|
|
// || `[0x004000000000, 0x02008fff6fff]` || ShadowGap3 ||
|
|
|
|
// || `[0x003000000000, 0x003fffffffff]` || MidMem ||
|
|
|
|
// || `[0x00087fff8000, 0x002fffffffff]` || ShadowGap2 ||
|
|
|
|
// || `[0x00067fff8000, 0x00087fff7fff]` || MidShadow ||
|
|
|
|
// || `[0x00008fff7000, 0x00067fff7fff]` || ShadowGap ||
|
|
|
|
// || `[0x00007fff8000, 0x00008fff6fff]` || LowShadow ||
|
|
|
|
// || `[0x000000000000, 0x00007fff7fff]` || LowMem ||
|
|
|
|
//
|
|
|
|
// Default Linux/i386 mapping:
|
|
|
|
// || `[0x40000000, 0xffffffff]` || HighMem ||
|
|
|
|
// || `[0x28000000, 0x3fffffff]` || HighShadow ||
|
|
|
|
// || `[0x24000000, 0x27ffffff]` || ShadowGap ||
|
|
|
|
// || `[0x20000000, 0x23ffffff]` || LowShadow ||
|
|
|
|
// || `[0x00000000, 0x1fffffff]` || LowMem ||
|
2012-11-12 16:53:47 +01:00
|
|
|
|
|
|
|
#if ASAN_FLEXIBLE_MAPPING_AND_OFFSET == 1
|
2013-01-10 13:44:08 +01:00
|
|
|
extern SANITIZER_INTERFACE_ATTRIBUTE uptr __asan_mapping_scale;
|
|
|
|
extern SANITIZER_INTERFACE_ATTRIBUTE uptr __asan_mapping_offset;
|
2012-11-12 16:53:47 +01:00
|
|
|
# define SHADOW_SCALE (__asan_mapping_scale)
|
|
|
|
# define SHADOW_OFFSET (__asan_mapping_offset)
|
|
|
|
#else
|
|
|
|
# if ASAN_ANDROID
|
|
|
|
# define SHADOW_SCALE (3)
|
|
|
|
# define SHADOW_OFFSET (0)
|
|
|
|
# else
|
|
|
|
# define SHADOW_SCALE (3)
|
2012-11-23 15:46:25 +01:00
|
|
|
# if SANITIZER_WORDSIZE == 32
|
2012-11-12 16:53:47 +01:00
|
|
|
# define SHADOW_OFFSET (1 << 29)
|
|
|
|
# else
|
2012-11-23 15:46:25 +01:00
|
|
|
# if defined(__powerpc64__)
|
|
|
|
# define SHADOW_OFFSET (1ULL << 41)
|
|
|
|
# else
|
2013-02-21 11:57:10 +01:00
|
|
|
# if ASAN_MAC
|
|
|
|
# define SHADOW_OFFSET (1ULL << 44)
|
|
|
|
# else
|
|
|
|
# define SHADOW_OFFSET 0x7fff8000ULL
|
|
|
|
# endif
|
2012-11-23 15:46:25 +01:00
|
|
|
# endif
|
2012-11-12 16:53:47 +01:00
|
|
|
# endif
|
|
|
|
# endif
|
|
|
|
#endif // ASAN_FLEXIBLE_MAPPING_AND_OFFSET
|
|
|
|
|
|
|
|
#define SHADOW_GRANULARITY (1ULL << SHADOW_SCALE)
|
2013-02-13 11:46:01 +01:00
|
|
|
#define MEM_TO_SHADOW(mem) (((mem) >> SHADOW_SCALE) + (SHADOW_OFFSET))
|
2012-11-12 16:53:47 +01:00
|
|
|
#define SHADOW_TO_MEM(shadow) (((shadow) - SHADOW_OFFSET) << SHADOW_SCALE)
|
|
|
|
|
|
|
|
#define kLowMemBeg 0
|
|
|
|
#define kLowMemEnd (SHADOW_OFFSET ? SHADOW_OFFSET - 1 : 0)
|
|
|
|
|
|
|
|
#define kLowShadowBeg SHADOW_OFFSET
|
|
|
|
#define kLowShadowEnd MEM_TO_SHADOW(kLowMemEnd)
|
|
|
|
|
|
|
|
#define kHighMemBeg (MEM_TO_SHADOW(kHighMemEnd) + 1)
|
|
|
|
|
|
|
|
#define kHighShadowBeg MEM_TO_SHADOW(kHighMemBeg)
|
|
|
|
#define kHighShadowEnd MEM_TO_SHADOW(kHighMemEnd)
|
|
|
|
|
2013-02-21 11:57:10 +01:00
|
|
|
# define kMidShadowBeg MEM_TO_SHADOW(kMidMemBeg)
|
|
|
|
# define kMidShadowEnd MEM_TO_SHADOW(kMidMemEnd)
|
|
|
|
|
2012-11-27 15:01:46 +01:00
|
|
|
// With the zero shadow base we can not actually map pages starting from 0.
|
|
|
|
// This constant is somewhat arbitrary.
|
|
|
|
#define kZeroBaseShadowStart (1 << 18)
|
|
|
|
|
|
|
|
#define kShadowGapBeg (kLowShadowEnd ? kLowShadowEnd + 1 \
|
|
|
|
: kZeroBaseShadowStart)
|
2013-02-21 11:57:10 +01:00
|
|
|
#define kShadowGapEnd ((kMidMemBeg ? kMidShadowBeg : kHighShadowBeg) - 1)
|
|
|
|
|
|
|
|
#define kShadowGap2Beg (kMidMemBeg ? kMidShadowEnd + 1 : 0)
|
|
|
|
#define kShadowGap2End (kMidMemBeg ? kMidMemBeg - 1 : 0)
|
|
|
|
|
|
|
|
#define kShadowGap3Beg (kMidMemBeg ? kMidMemEnd + 1 : 0)
|
|
|
|
#define kShadowGap3End (kMidMemBeg ? kHighShadowBeg - 1 : 0)
|
|
|
|
|
|
|
|
#define DO_ASAN_MAPPING_PROFILE 0 // Set to 1 to profile the functions below.
|
|
|
|
|
|
|
|
#if DO_ASAN_MAPPING_PROFILE
|
|
|
|
# define PROFILE_ASAN_MAPPING() AsanMappingProfile[__LINE__]++;
|
|
|
|
#else
|
|
|
|
# define PROFILE_ASAN_MAPPING()
|
|
|
|
#endif
|
|
|
|
|
|
|
|
// If 1, all shadow boundaries are constants.
|
|
|
|
// Don't set to 1 other than for testing.
|
|
|
|
#define ASAN_FIXED_MAPPING 0
|
2012-11-12 16:53:47 +01:00
|
|
|
|
|
|
|
namespace __asan {
|
|
|
|
|
2013-02-21 11:57:10 +01:00
|
|
|
extern uptr AsanMappingProfile[];
|
|
|
|
|
|
|
|
#if ASAN_FIXED_MAPPING
|
|
|
|
// Fixed mapping for 64-bit Linux. Mostly used for performance comparison
|
|
|
|
// with non-fixed mapping. As of r175253 (Feb 2013) the performance
|
|
|
|
// difference between fixed and non-fixed mapping is below the noise level.
|
|
|
|
static uptr kHighMemEnd = 0x7fffffffffffULL;
|
|
|
|
static uptr kMidMemBeg = 0x3000000000ULL;
|
|
|
|
static uptr kMidMemEnd = 0x3fffffffffULL;
|
|
|
|
#else
|
2013-02-13 11:46:01 +01:00
|
|
|
SANITIZER_INTERFACE_ATTRIBUTE
|
2013-02-21 11:57:10 +01:00
|
|
|
extern uptr kHighMemEnd, kMidMemBeg, kMidMemEnd; // Initialized in __asan_init.
|
|
|
|
#endif
|
2013-02-13 11:46:01 +01:00
|
|
|
|
2012-11-12 16:53:47 +01:00
|
|
|
static inline bool AddrIsInLowMem(uptr a) {
|
2013-02-21 11:57:10 +01:00
|
|
|
PROFILE_ASAN_MAPPING();
|
2012-11-12 16:53:47 +01:00
|
|
|
return a < kLowMemEnd;
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline bool AddrIsInLowShadow(uptr a) {
|
2013-02-21 11:57:10 +01:00
|
|
|
PROFILE_ASAN_MAPPING();
|
2012-11-12 16:53:47 +01:00
|
|
|
return a >= kLowShadowBeg && a <= kLowShadowEnd;
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline bool AddrIsInHighMem(uptr a) {
|
2013-02-21 11:57:10 +01:00
|
|
|
PROFILE_ASAN_MAPPING();
|
2012-11-12 16:53:47 +01:00
|
|
|
return a >= kHighMemBeg && a <= kHighMemEnd;
|
|
|
|
}
|
|
|
|
|
2013-02-21 11:57:10 +01:00
|
|
|
static inline bool AddrIsInMidMem(uptr a) {
|
|
|
|
PROFILE_ASAN_MAPPING();
|
|
|
|
return kMidMemBeg && a >= kMidMemBeg && a <= kMidMemEnd;
|
|
|
|
}
|
|
|
|
|
2012-11-12 16:53:47 +01:00
|
|
|
static inline bool AddrIsInMem(uptr a) {
|
2013-02-21 11:57:10 +01:00
|
|
|
PROFILE_ASAN_MAPPING();
|
|
|
|
return AddrIsInLowMem(a) || AddrIsInMidMem(a) || AddrIsInHighMem(a);
|
2012-11-12 16:53:47 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
static inline uptr MemToShadow(uptr p) {
|
2013-02-21 11:57:10 +01:00
|
|
|
PROFILE_ASAN_MAPPING();
|
2012-11-12 16:53:47 +01:00
|
|
|
CHECK(AddrIsInMem(p));
|
|
|
|
return MEM_TO_SHADOW(p);
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline bool AddrIsInHighShadow(uptr a) {
|
2013-02-21 11:57:10 +01:00
|
|
|
PROFILE_ASAN_MAPPING();
|
|
|
|
return a >= kHighShadowBeg && a <= kHighMemEnd;
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline bool AddrIsInMidShadow(uptr a) {
|
|
|
|
PROFILE_ASAN_MAPPING();
|
|
|
|
return kMidMemBeg && a >= kMidShadowBeg && a <= kMidMemEnd;
|
2012-11-12 16:53:47 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
static inline bool AddrIsInShadow(uptr a) {
|
2013-02-21 11:57:10 +01:00
|
|
|
PROFILE_ASAN_MAPPING();
|
|
|
|
return AddrIsInLowShadow(a) || AddrIsInMidShadow(a) || AddrIsInHighShadow(a);
|
2012-11-12 16:53:47 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
static inline bool AddrIsInShadowGap(uptr a) {
|
2013-02-21 11:57:10 +01:00
|
|
|
PROFILE_ASAN_MAPPING();
|
|
|
|
if (kMidMemBeg) {
|
|
|
|
if (a <= kShadowGapEnd)
|
|
|
|
return SHADOW_OFFSET == 0 || a >= kShadowGapBeg;
|
|
|
|
return (a >= kShadowGap2Beg && a <= kShadowGap2End) ||
|
|
|
|
(a >= kShadowGap3Beg && a <= kShadowGap3End);
|
|
|
|
}
|
2013-01-23 12:41:33 +01:00
|
|
|
// In zero-based shadow mode we treat addresses near zero as addresses
|
|
|
|
// in shadow gap as well.
|
|
|
|
if (SHADOW_OFFSET == 0)
|
|
|
|
return a <= kShadowGapEnd;
|
2012-11-12 16:53:47 +01:00
|
|
|
return a >= kShadowGapBeg && a <= kShadowGapEnd;
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline bool AddrIsAlignedByGranularity(uptr a) {
|
2013-02-21 11:57:10 +01:00
|
|
|
PROFILE_ASAN_MAPPING();
|
2012-11-12 16:53:47 +01:00
|
|
|
return (a & (SHADOW_GRANULARITY - 1)) == 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline bool AddressIsPoisoned(uptr a) {
|
2013-02-21 11:57:10 +01:00
|
|
|
PROFILE_ASAN_MAPPING();
|
2012-11-12 16:53:47 +01:00
|
|
|
const uptr kAccessSize = 1;
|
2013-02-21 11:57:10 +01:00
|
|
|
u8 *shadow_address = (u8*)MEM_TO_SHADOW(a);
|
2012-11-12 16:53:47 +01:00
|
|
|
s8 shadow_value = *shadow_address;
|
|
|
|
if (shadow_value) {
|
|
|
|
u8 last_accessed_byte = (a & (SHADOW_GRANULARITY - 1))
|
|
|
|
+ kAccessSize - 1;
|
|
|
|
return (last_accessed_byte >= shadow_value);
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2013-02-21 11:57:10 +01:00
|
|
|
// Must be after all calls to PROFILE_ASAN_MAPPING().
|
|
|
|
static const uptr kAsanMappingProfileSize = __LINE__;
|
|
|
|
|
2012-11-12 16:53:47 +01:00
|
|
|
} // namespace __asan
|
|
|
|
|
|
|
|
#endif // ASAN_MAPPING_H
|