2011-11-30 09:07:02 +08:00
|
|
|
//===-- asan_mapping.h ------------------------------------------*- C++ -*-===//
|
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
|
|
|
// 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
|
|
|
|
|
|
|
|
#if ASAN_FLEXIBLE_MAPPING_AND_OFFSET == 1
|
2012-05-31 22:35:53 +08:00
|
|
|
extern __attribute__((visibility("default"))) uptr __asan_mapping_scale;
|
|
|
|
extern __attribute__((visibility("default"))) uptr __asan_mapping_offset;
|
2012-05-23 19:52:37 +08:00
|
|
|
# define SHADOW_SCALE (__asan_mapping_scale)
|
|
|
|
# define SHADOW_OFFSET (__asan_mapping_offset)
|
2011-11-30 09:07:02 +08:00
|
|
|
#else
|
2012-05-23 19:52:37 +08:00
|
|
|
# ifdef ANDROID
|
|
|
|
# define SHADOW_SCALE (3)
|
|
|
|
# define SHADOW_OFFSET (0)
|
|
|
|
# else
|
|
|
|
# define SHADOW_SCALE (3)
|
|
|
|
# if __WORDSIZE == 32
|
|
|
|
# define SHADOW_OFFSET (1 << 29)
|
|
|
|
# else
|
|
|
|
# define SHADOW_OFFSET (1ULL << 44)
|
|
|
|
# endif
|
|
|
|
# endif
|
2011-11-30 09:07:02 +08:00
|
|
|
#endif // ASAN_FLEXIBLE_MAPPING_AND_OFFSET
|
|
|
|
|
|
|
|
#define SHADOW_GRANULARITY (1ULL << SHADOW_SCALE)
|
|
|
|
#define MEM_TO_SHADOW(mem) (((mem) >> SHADOW_SCALE) | (SHADOW_OFFSET))
|
2012-05-12 20:33:10 +08:00
|
|
|
#define SHADOW_TO_MEM(shadow) (((shadow) - SHADOW_OFFSET) << SHADOW_SCALE)
|
2011-11-30 09:07:02 +08:00
|
|
|
|
|
|
|
#if __WORDSIZE == 64
|
2012-05-31 22:35:53 +08:00
|
|
|
static const uptr kHighMemEnd = 0x00007fffffffffffUL;
|
2011-11-30 09:07:02 +08:00
|
|
|
#else // __WORDSIZE == 32
|
2012-05-31 22:35:53 +08:00
|
|
|
static const uptr kHighMemEnd = 0xffffffff;
|
2011-11-30 09:07:02 +08:00
|
|
|
#endif // __WORDSIZE
|
|
|
|
|
|
|
|
|
|
|
|
#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)
|
|
|
|
|
|
|
|
#define kShadowGapBeg (kLowShadowEnd ? kLowShadowEnd + 1 : 16 * kPageSize)
|
|
|
|
#define kShadowGapEnd (kHighShadowBeg - 1)
|
|
|
|
|
|
|
|
#define kGlobalAndStackRedzone \
|
|
|
|
(SHADOW_GRANULARITY < 32 ? 32 : SHADOW_GRANULARITY)
|
|
|
|
|
|
|
|
namespace __asan {
|
|
|
|
|
2012-05-31 22:35:53 +08:00
|
|
|
static inline bool AddrIsInLowMem(uptr a) {
|
2011-11-30 09:07:02 +08:00
|
|
|
return a < kLowMemEnd;
|
|
|
|
}
|
|
|
|
|
2012-05-31 22:35:53 +08:00
|
|
|
static inline bool AddrIsInLowShadow(uptr a) {
|
2011-11-30 09:07:02 +08:00
|
|
|
return a >= kLowShadowBeg && a <= kLowShadowEnd;
|
|
|
|
}
|
|
|
|
|
2012-05-31 22:35:53 +08:00
|
|
|
static inline bool AddrIsInHighMem(uptr a) {
|
2011-11-30 09:07:02 +08:00
|
|
|
return a >= kHighMemBeg && a <= kHighMemEnd;
|
|
|
|
}
|
|
|
|
|
2012-05-31 22:35:53 +08:00
|
|
|
static inline bool AddrIsInMem(uptr a) {
|
2011-11-30 09:07:02 +08:00
|
|
|
return AddrIsInLowMem(a) || AddrIsInHighMem(a);
|
|
|
|
}
|
|
|
|
|
2012-05-31 22:35:53 +08:00
|
|
|
static inline uptr MemToShadow(uptr p) {
|
2011-11-30 09:07:02 +08:00
|
|
|
CHECK(AddrIsInMem(p));
|
|
|
|
return MEM_TO_SHADOW(p);
|
|
|
|
}
|
|
|
|
|
2012-05-31 22:35:53 +08:00
|
|
|
static inline bool AddrIsInHighShadow(uptr a) {
|
2011-11-30 09:07:02 +08:00
|
|
|
return a >= kHighShadowBeg && a <= kHighMemEnd;
|
|
|
|
}
|
|
|
|
|
2012-05-31 22:35:53 +08:00
|
|
|
static inline bool AddrIsInShadow(uptr a) {
|
2011-11-30 09:07:02 +08:00
|
|
|
return AddrIsInLowShadow(a) || AddrIsInHighShadow(a);
|
|
|
|
}
|
|
|
|
|
2012-07-23 16:22:27 +08:00
|
|
|
static inline bool AddrIsInShadowGap(uptr a) {
|
|
|
|
return a >= kShadowGapBeg && a <= kShadowGapEnd;
|
|
|
|
}
|
|
|
|
|
2012-05-31 22:35:53 +08:00
|
|
|
static inline bool AddrIsAlignedByGranularity(uptr a) {
|
2011-12-01 02:50:23 +08:00
|
|
|
return (a & (SHADOW_GRANULARITY - 1)) == 0;
|
|
|
|
}
|
|
|
|
|
2012-05-31 22:35:53 +08:00
|
|
|
static inline bool AddressIsPoisoned(uptr a) {
|
|
|
|
const uptr kAccessSize = 1;
|
2012-05-31 23:02:07 +08:00
|
|
|
u8 *shadow_address = (u8*)MemToShadow(a);
|
|
|
|
s8 shadow_value = *shadow_address;
|
2012-03-15 09:18:06 +08:00
|
|
|
if (shadow_value) {
|
2012-05-31 23:02:07 +08:00
|
|
|
u8 last_accessed_byte = (a & (SHADOW_GRANULARITY - 1))
|
2012-03-15 09:18:06 +08:00
|
|
|
+ kAccessSize - 1;
|
|
|
|
return (last_accessed_byte >= shadow_value);
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2011-11-30 09:07:02 +08:00
|
|
|
} // namespace __asan
|
|
|
|
|
|
|
|
#endif // ASAN_MAPPING_H
|