forked from OSchip/llvm-project
[Msan] Generalize mapping facilities to add FreeBSD support
Differential Revision: http://reviews.llvm.org/D6387 llvm-svn: 222919
This commit is contained in:
parent
a0b6899234
commit
30bd345613
|
@ -383,8 +383,7 @@ void __msan_init() {
|
|||
__msan_clear_on_return();
|
||||
if (__msan_get_track_origins())
|
||||
VPrintf(1, "msan_track_origins\n");
|
||||
if (!InitShadow(/* prot1 */ true, /* prot2 */ true,
|
||||
/* map_shadow */ true, __msan_get_track_origins())) {
|
||||
if (!InitShadow(/* map_shadow */ true, __msan_get_track_origins())) {
|
||||
Printf("FATAL: MemorySanitizer can not mmap the shadow memory.\n");
|
||||
Printf("FATAL: Make sure to compile with -fPIE and to link with -pie.\n");
|
||||
Printf("FATAL: Disabling ASLR is known to cause this error.\n");
|
||||
|
|
|
@ -25,22 +25,91 @@
|
|||
# define MSAN_REPLACE_OPERATORS_NEW_AND_DELETE 1
|
||||
#endif
|
||||
|
||||
#if defined(__mips64)
|
||||
#define MEM_TO_SHADOW(mem) (((uptr)mem) & ~0x4000000000ULL)
|
||||
#define SHADOW_TO_ORIGIN(shadow) (((uptr)shadow) + 0x2000000000ULL)
|
||||
#define MEM_TO_ORIGIN(mem) (SHADOW_TO_ORIGIN(MEM_TO_SHADOW(mem)))
|
||||
#define MEM_IS_APP(mem) ((uptr)mem >= 0xe000000000ULL)
|
||||
#define MEM_IS_SHADOW(mem) \
|
||||
((uptr)mem >= 0xa000000000ULL && (uptr)mem <= 0xc000000000ULL)
|
||||
#elif defined(__x86_64__)
|
||||
#define MEM_TO_SHADOW(mem) (((uptr)mem) & ~0x400000000000ULL)
|
||||
#define SHADOW_TO_ORIGIN(shadow) (((uptr)shadow) + 0x200000000000ULL)
|
||||
#define MEM_TO_ORIGIN(mem) (SHADOW_TO_ORIGIN(MEM_TO_SHADOW(mem)))
|
||||
#define MEM_IS_APP(mem) ((uptr)mem >= 0x600000000000ULL)
|
||||
#define MEM_IS_SHADOW(mem) \
|
||||
((uptr)mem >= 0x200000000000ULL && (uptr)mem <= 0x400000000000ULL)
|
||||
/*
|
||||
C/C++ on FreeBSD
|
||||
0000 0000 0000 - 00ff ffff ffff: Low memory: main binary, MAP_32BIT mappings and modules
|
||||
0100 0000 0000 - 0fff ffff ffff: Bad1
|
||||
1000 0000 0000 - 30ff ffff ffff: Shadow
|
||||
3100 0000 0000 - 37ff ffff ffff: Bad2
|
||||
3800 0000 0000 - 58ff ffff ffff: Origins
|
||||
5900 0000 0000 - 5fff ffff ffff: Bad3
|
||||
6000 0000 0000 - 7fff ffff ffff: High memory: heap, modules and main thread stack
|
||||
|
||||
C/C++ on Linux/PIE
|
||||
0000 0000 0000 - 1fff ffff ffff: Bad1
|
||||
2000 0000 0000 - 3fff ffff ffff: Shadow
|
||||
4000 0000 0000 - 5fff ffff ffff: Origins
|
||||
6000 0000 0000 - 7fff ffff ffff: Main memory
|
||||
|
||||
C/C++ on Mips
|
||||
0000 0000 0000 - 009f ffff ffff: Bad1
|
||||
00a0 0000 0000 - 00bf ffff ffff: Shadow
|
||||
00c0 0000 0000 - 00df ffff ffff: Origins
|
||||
00e0 0000 0000 - 00ff ffff ffff: Main memory
|
||||
*/
|
||||
|
||||
#if SANITIZER_LINUX && defined(__mips64)
|
||||
const uptr kLowMemBeg = 0;
|
||||
const uptr kLowMemSize = 0;
|
||||
const uptr kHighMemBeg = 0x00e000000000;
|
||||
const uptr kHighMemSize = 0x002000000000;
|
||||
const uptr kShadowBeg = 0x00a000000000;
|
||||
const uptr kShadowSize = 0x002000000000;
|
||||
const uptr kOriginsBeg = 0x00c000000000;
|
||||
# define MEM_TO_SHADOW(mem) (((uptr)(mem)) & ~0x4000000000ULL)
|
||||
#elif SANITIZER_FREEBSD && SANITIZER_WORDSIZE == 64
|
||||
const uptr kLowMemBeg = 0x000000000000;
|
||||
const uptr kLowMemSize = 0x010000000000;
|
||||
const uptr kHighMemBeg = 0x600000000000;
|
||||
const uptr kHighMemSize = 0x200000000000;
|
||||
const uptr kShadowBeg = 0x100000000000;
|
||||
const uptr kShadowSize = 0x210000000000;
|
||||
const uptr kOriginsBeg = 0x380000000000;
|
||||
// Maps low and high app ranges to contiguous space with zero base:
|
||||
// Low: 0000 0000 0000 - 00ff ffff ffff -> 2000 0000 0000 - 20ff ffff ffff
|
||||
// High: 6000 0000 0000 - 7fff ffff ffff -> 0000 0000 0000 - 1fff ffff ffff
|
||||
# define LINEARIZE_MEM(mem) \
|
||||
(((uptr)(mem) & ~0xc00000000000ULL) ^ 0x200000000000ULL)
|
||||
# define MEM_TO_SHADOW(mem) (LINEARIZE_MEM((mem)) + 0x100000000000ULL)
|
||||
#elif SANITIZER_LINUX && SANITIZER_WORDSIZE == 64
|
||||
const uptr kLowMemBeg = 0;
|
||||
const uptr kLowMemSize = 0;
|
||||
const uptr kHighMemBeg = 0x600000000000;
|
||||
const uptr kHighMemSize = 0x200000000000;
|
||||
const uptr kShadowBeg = 0x200000000000;
|
||||
const uptr kShadowSize = 0x200000000000;
|
||||
const uptr kOriginsBeg = 0x400000000000;
|
||||
# define MEM_TO_SHADOW(mem) (((uptr)(mem)) & ~0x400000000000ULL)
|
||||
#else
|
||||
#error "Unsupported platform"
|
||||
#endif
|
||||
|
||||
const uptr kBad1Beg = kLowMemBeg + kLowMemSize;
|
||||
const uptr kBad1Size = kShadowBeg - kBad1Beg;
|
||||
|
||||
const uptr kBad2Beg = kShadowBeg + kShadowSize;
|
||||
const uptr kBad2Size = kOriginsBeg - kBad2Beg;
|
||||
|
||||
const uptr kOriginsSize = kShadowSize;
|
||||
|
||||
const uptr kBad3Beg = kOriginsBeg + kOriginsSize;
|
||||
const uptr kBad3Size = kHighMemBeg - kBad3Beg;
|
||||
|
||||
#define SHADOW_TO_ORIGIN(shadow) \
|
||||
(((uptr)(shadow)) + (kOriginsBeg - kShadowBeg))
|
||||
|
||||
#define MEM_TO_ORIGIN(mem) (SHADOW_TO_ORIGIN(MEM_TO_SHADOW((mem))))
|
||||
|
||||
#define MEM_IS_APP(mem) \
|
||||
((kLowMemSize > 0 && (uptr)(mem) < kLowMemSize) || \
|
||||
(uptr)(mem) >= kHighMemBeg)
|
||||
|
||||
#define MEM_IS_SHADOW(mem) \
|
||||
((uptr)(mem) >= kShadowBeg && (uptr)(mem) < kShadowBeg + kShadowSize)
|
||||
|
||||
#define MEM_IS_ORIGIN(mem) \
|
||||
((uptr)(mem) >= kOriginsBeg && (uptr)(mem) < kOriginsBeg + kOriginsSize)
|
||||
|
||||
// These constants must be kept in sync with the ones in MemorySanitizer.cc.
|
||||
const int kMsanParamTlsSize = 800;
|
||||
const int kMsanRetvalTlsSize = 800;
|
||||
|
@ -51,7 +120,7 @@ extern bool msan_init_is_running;
|
|||
extern int msan_report_count;
|
||||
|
||||
bool ProtectRange(uptr beg, uptr end);
|
||||
bool InitShadow(bool prot1, bool prot2, bool map_shadow, bool init_origins);
|
||||
bool InitShadow(bool map_shadow, bool init_origins);
|
||||
char *GetProcSelfMaps();
|
||||
void InitializeInterceptors();
|
||||
|
||||
|
|
|
@ -1450,7 +1450,7 @@ u32 GetOriginIfPoisoned(uptr addr, uptr size) {
|
|||
unsigned char *s = (unsigned char *)MEM_TO_SHADOW(addr);
|
||||
for (uptr i = 0; i < size; ++i)
|
||||
if (s[i])
|
||||
return *(u32 *)SHADOW_TO_ORIGIN((s + i) & ~3UL);
|
||||
return *(u32 *)SHADOW_TO_ORIGIN(((uptr)s + i) & ~3UL);
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
|
|
@ -9,11 +9,11 @@
|
|||
//
|
||||
// This file is a part of MemorySanitizer.
|
||||
//
|
||||
// Linux-specific code.
|
||||
// Linux- and FreeBSD-specific code.
|
||||
//===----------------------------------------------------------------------===//
|
||||
|
||||
#include "sanitizer_common/sanitizer_platform.h"
|
||||
#if SANITIZER_LINUX
|
||||
#if SANITIZER_FREEBSD || SANITIZER_LINUX
|
||||
|
||||
#include "msan.h"
|
||||
#include "msan_thread.h"
|
||||
|
@ -35,55 +35,98 @@
|
|||
|
||||
namespace __msan {
|
||||
|
||||
#if defined(__mips64)
|
||||
static const uptr kMemBeg = 0xe000000000;
|
||||
static const uptr kMemEnd = 0xffffffffff;
|
||||
#elif defined(__x86_64__)
|
||||
static const uptr kMemBeg = 0x600000000000;
|
||||
static const uptr kMemEnd = 0x7fffffffffff;
|
||||
#endif
|
||||
|
||||
static const uptr kShadowBeg = MEM_TO_SHADOW(kMemBeg);
|
||||
static const uptr kShadowEnd = MEM_TO_SHADOW(kMemEnd);
|
||||
static const uptr kBad1Beg = 0;
|
||||
static const uptr kBad1End = kShadowBeg - 1;
|
||||
static const uptr kBad2Beg = kShadowEnd + 1;
|
||||
static const uptr kBad2End = kMemBeg - 1;
|
||||
static const uptr kOriginsBeg = kBad2Beg;
|
||||
static const uptr kOriginsEnd = kBad2End;
|
||||
|
||||
bool InitShadow(bool prot1, bool prot2, bool map_shadow, bool init_origins) {
|
||||
if ((uptr) & InitShadow < kMemBeg) {
|
||||
Printf("FATAL: Code below application range: %p < %p. Non-PIE build?\n",
|
||||
&InitShadow, (void *)kMemBeg);
|
||||
return false;
|
||||
void ReportMapRange(const char *descr, uptr beg, uptr size) {
|
||||
if (size > 0) {
|
||||
uptr end = beg + size - 1;
|
||||
VPrintf(1, "%s : %p - %p\n", descr, beg, end);
|
||||
}
|
||||
}
|
||||
|
||||
static bool CheckMemoryRangeAvailability(uptr beg, uptr size) {
|
||||
if (size > 0) {
|
||||
uptr end = beg + size - 1;
|
||||
if (!MemoryRangeIsAvailable(beg, end)) {
|
||||
Printf("FATAL: Memory range %p - %p is not available.\n", beg, end);
|
||||
return false;
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
static bool ProtectMemoryRange(uptr beg, uptr size) {
|
||||
if (size > 0) {
|
||||
uptr end = beg + size - 1;
|
||||
if (!Mprotect(beg, size)) {
|
||||
Printf("FATAL: Cannot protect memory range %p - %p.\n", beg, end);
|
||||
return false;
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
bool InitShadow(bool map_shadow, bool init_origins) {
|
||||
// Let user know mapping parameters first.
|
||||
VPrintf(1, "__msan_init %p\n", &__msan_init);
|
||||
VPrintf(1, "Memory : %p %p\n", kMemBeg, kMemEnd);
|
||||
VPrintf(1, "Bad2 : %p %p\n", kBad2Beg, kBad2End);
|
||||
VPrintf(1, "Origins : %p %p\n", kOriginsBeg, kOriginsEnd);
|
||||
VPrintf(1, "Shadow : %p %p\n", kShadowBeg, kShadowEnd);
|
||||
VPrintf(1, "Bad1 : %p %p\n", kBad1Beg, kBad1End);
|
||||
ReportMapRange("Low Memory ", kLowMemBeg, kLowMemSize);
|
||||
ReportMapRange("Bad1 ", kBad1Beg, kBad1Size);
|
||||
ReportMapRange("Shadow ", kShadowBeg, kShadowSize);
|
||||
ReportMapRange("Bad2 ", kBad2Beg, kBad2Size);
|
||||
ReportMapRange("Origins ", kOriginsBeg, kOriginsSize);
|
||||
ReportMapRange("Bad3 ", kBad3Beg, kBad3Size);
|
||||
ReportMapRange("High Memory", kHighMemBeg, kHighMemSize);
|
||||
|
||||
if (!MemoryRangeIsAvailable(kShadowBeg,
|
||||
init_origins ? kOriginsEnd : kShadowEnd) ||
|
||||
(prot1 && !MemoryRangeIsAvailable(kBad1Beg, kBad1End)) ||
|
||||
(prot2 && !MemoryRangeIsAvailable(kBad2Beg, kBad2End))) {
|
||||
Printf("FATAL: Shadow memory range is not available.\n");
|
||||
// Check mapping sanity (the invariant).
|
||||
CHECK_EQ(kLowMemBeg, 0);
|
||||
CHECK_EQ(kBad1Beg, kLowMemBeg + kLowMemSize);
|
||||
CHECK_EQ(kShadowBeg, kBad1Beg + kBad1Size);
|
||||
CHECK_GT(kShadowSize, 0);
|
||||
CHECK_GE(kShadowSize, kLowMemSize + kHighMemSize);
|
||||
CHECK_EQ(kBad2Beg, kShadowBeg + kShadowSize);
|
||||
CHECK_EQ(kOriginsBeg, kBad2Beg + kBad2Size);
|
||||
CHECK_EQ(kOriginsSize, kShadowSize);
|
||||
CHECK_EQ(kBad3Beg, kOriginsBeg + kOriginsSize);
|
||||
CHECK_EQ(kHighMemBeg, kBad3Beg + kBad3Size);
|
||||
CHECK_GT(kHighMemSize, 0);
|
||||
CHECK_GE(kHighMemBeg + kHighMemSize, kHighMemBeg); // Tests for no overflow.
|
||||
|
||||
if (kLowMemSize > 0) {
|
||||
CHECK(MEM_IS_SHADOW(MEM_TO_SHADOW(kLowMemBeg)));
|
||||
CHECK(MEM_IS_SHADOW(MEM_TO_SHADOW(kLowMemBeg + kLowMemSize - 1)));
|
||||
CHECK(MEM_IS_ORIGIN(MEM_TO_ORIGIN(kLowMemBeg)));
|
||||
CHECK(MEM_IS_ORIGIN(MEM_TO_ORIGIN(kLowMemBeg + kLowMemSize - 1)));
|
||||
}
|
||||
CHECK(MEM_IS_SHADOW(MEM_TO_SHADOW(kHighMemBeg)));
|
||||
CHECK(MEM_IS_SHADOW(MEM_TO_SHADOW(kHighMemBeg + kHighMemSize - 1)));
|
||||
CHECK(MEM_IS_ORIGIN(MEM_TO_ORIGIN(kHighMemBeg)));
|
||||
CHECK(MEM_IS_ORIGIN(MEM_TO_ORIGIN(kHighMemBeg + kHighMemSize - 1)));
|
||||
|
||||
if (!MEM_IS_APP(&__msan_init)) {
|
||||
Printf("FATAL: Code %p is out of application range. Non-PIE build?\n",
|
||||
(uptr)&__msan_init);
|
||||
return false;
|
||||
}
|
||||
|
||||
if (prot1 && !Mprotect(kBad1Beg, kBad1End - kBad1Beg))
|
||||
if (!CheckMemoryRangeAvailability(kShadowBeg, kShadowSize) ||
|
||||
(init_origins &&
|
||||
!CheckMemoryRangeAvailability(kOriginsBeg, kOriginsSize)) ||
|
||||
!CheckMemoryRangeAvailability(kBad1Beg, kBad1Size) ||
|
||||
!CheckMemoryRangeAvailability(kBad2Beg, kBad2Size) ||
|
||||
!CheckMemoryRangeAvailability(kBad3Beg, kBad3Size)) {
|
||||
return false;
|
||||
if (prot2 && !Mprotect(kBad2Beg, kBad2End - kBad2Beg))
|
||||
}
|
||||
|
||||
if (!ProtectMemoryRange(kBad1Beg, kBad1Size) ||
|
||||
!ProtectMemoryRange(kBad2Beg, kBad2Size) ||
|
||||
!ProtectMemoryRange(kBad3Beg, kBad3Size)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if (map_shadow) {
|
||||
void *shadow = MmapFixedNoReserve(kShadowBeg, kShadowEnd - kShadowBeg);
|
||||
void *shadow = MmapFixedNoReserve(kShadowBeg, kShadowSize);
|
||||
if (shadow != (void*)kShadowBeg) return false;
|
||||
}
|
||||
if (init_origins) {
|
||||
void *origins = MmapFixedNoReserve(kOriginsBeg, kOriginsEnd - kOriginsBeg);
|
||||
void *origins = MmapFixedNoReserve(kOriginsBeg, kOriginsSize);
|
||||
if (origins != (void*)kOriginsBeg) return false;
|
||||
}
|
||||
return true;
|
||||
|
@ -140,4 +183,4 @@ void MsanTSDDtor(void *tsd) {
|
|||
|
||||
} // namespace __msan
|
||||
|
||||
#endif // __linux__
|
||||
#endif // SANITIZER_FREEBSD || SANITIZER_LINUX
|
||||
|
|
Loading…
Reference in New Issue