forked from OSchip/llvm-project
129 lines
3.7 KiB
C++
129 lines
3.7 KiB
C++
//===-- 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
|
|
extern SANITIZER_INTERFACE_ATTRIBUTE uptr __asan_mapping_scale;
|
|
extern SANITIZER_INTERFACE_ATTRIBUTE uptr __asan_mapping_offset;
|
|
# 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)
|
|
# if SANITIZER_WORDSIZE == 32
|
|
# define SHADOW_OFFSET (1 << 29)
|
|
# else
|
|
# if defined(__powerpc64__)
|
|
# define SHADOW_OFFSET (1ULL << 41)
|
|
# else
|
|
# define SHADOW_OFFSET (1ULL << 44)
|
|
# endif
|
|
# endif
|
|
# endif
|
|
#endif // ASAN_FLEXIBLE_MAPPING_AND_OFFSET
|
|
|
|
#define SHADOW_GRANULARITY (1ULL << SHADOW_SCALE)
|
|
#define MEM_TO_SHADOW(mem) (((mem) >> SHADOW_SCALE) + (SHADOW_OFFSET))
|
|
#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)
|
|
|
|
// 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)
|
|
#define kShadowGapEnd (kHighShadowBeg - 1)
|
|
|
|
namespace __asan {
|
|
|
|
SANITIZER_INTERFACE_ATTRIBUTE
|
|
extern uptr kHighMemEnd; // Initialized in __asan_init.
|
|
|
|
static inline bool AddrIsInLowMem(uptr a) {
|
|
return a < kLowMemEnd;
|
|
}
|
|
|
|
static inline bool AddrIsInLowShadow(uptr a) {
|
|
return a >= kLowShadowBeg && a <= kLowShadowEnd;
|
|
}
|
|
|
|
static inline bool AddrIsInHighMem(uptr a) {
|
|
return a >= kHighMemBeg && a <= kHighMemEnd;
|
|
}
|
|
|
|
static inline bool AddrIsInMem(uptr a) {
|
|
return AddrIsInLowMem(a) || AddrIsInHighMem(a);
|
|
}
|
|
|
|
static inline uptr MemToShadow(uptr p) {
|
|
CHECK(AddrIsInMem(p));
|
|
return MEM_TO_SHADOW(p);
|
|
}
|
|
|
|
static inline bool AddrIsInHighShadow(uptr a) {
|
|
return a >= kHighShadowBeg && a <= kHighMemEnd;
|
|
}
|
|
|
|
static inline bool AddrIsInShadow(uptr a) {
|
|
return AddrIsInLowShadow(a) || AddrIsInHighShadow(a);
|
|
}
|
|
|
|
static inline bool AddrIsInShadowGap(uptr a) {
|
|
// In zero-based shadow mode we treat addresses near zero as addresses
|
|
// in shadow gap as well.
|
|
if (SHADOW_OFFSET == 0)
|
|
return a <= kShadowGapEnd;
|
|
return a >= kShadowGapBeg && a <= kShadowGapEnd;
|
|
}
|
|
|
|
static inline bool AddrIsAlignedByGranularity(uptr a) {
|
|
return (a & (SHADOW_GRANULARITY - 1)) == 0;
|
|
}
|
|
|
|
static inline bool AddressIsPoisoned(uptr a) {
|
|
const uptr kAccessSize = 1;
|
|
u8 *shadow_address = (u8*)MemToShadow(a);
|
|
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;
|
|
}
|
|
|
|
} // namespace __asan
|
|
|
|
#endif // ASAN_MAPPING_H
|