From 8677baf024fa8ec82adebf1b34ed9e6fbb4e2ca4 Mon Sep 17 00:00:00 2001 From: Jay Foad Date: Thu, 25 Jun 2015 06:22:31 +0000 Subject: [PATCH] Enable memory sanitizer for PPC64 Summary: This patch adds basic memory sanitizer support for PPC64. PR23219. I have further patches ready to enable it in LLVM and Clang, and to fix most of the many failing tests in check-msan. Reviewers: kcc, willschm, samsonov, wschmidt, eugenis Reviewed By: eugenis Subscribers: wschmidt, llvm-commits Differential Revision: http://reviews.llvm.org/D10648 llvm-svn: 240623 --- compiler-rt/cmake/config-ix.cmake | 3 ++- compiler-rt/lib/msan/msan.h | 19 +++++++++++++++++++ compiler-rt/lib/msan/msan_allocator.cc | 9 +++++++++ .../sanitizer_common/sanitizer_stacktrace.cc | 2 ++ compiler-rt/test/msan/mmap.cc | 2 ++ compiler-rt/test/msan/mmap_below_shadow.cc | 3 +++ compiler-rt/test/msan/strlen_of_shadow.cc | 4 ++++ 7 files changed, 41 insertions(+), 1 deletion(-) diff --git a/compiler-rt/cmake/config-ix.cmake b/compiler-rt/cmake/config-ix.cmake index b6fe8ffb08c0..02b9db4d33f1 100644 --- a/compiler-rt/cmake/config-ix.cmake +++ b/compiler-rt/cmake/config-ix.cmake @@ -251,7 +251,8 @@ filter_available_targets(ASAN_SUPPORTED_ARCH x86_64 i386 i686 powerpc64 powerpc64le arm mips mipsel mips64 mips64el) filter_available_targets(DFSAN_SUPPORTED_ARCH x86_64 mips64 mips64el) filter_available_targets(LSAN_SUPPORTED_ARCH x86_64 mips64 mips64el) -filter_available_targets(MSAN_SUPPORTED_ARCH x86_64 mips64 mips64el) +filter_available_targets(MSAN_SUPPORTED_ARCH x86_64 mips64 mips64el + powerpc64 powerpc64le) filter_available_targets(PROFILE_SUPPORTED_ARCH x86_64 i386 i686 arm mips mips64 mipsel mips64el aarch64 powerpc64 powerpc64le) filter_available_targets(TSAN_SUPPORTED_ARCH x86_64 mips64 mips64el) diff --git a/compiler-rt/lib/msan/msan.h b/compiler-rt/lib/msan/msan.h index 0adbeb68f67e..cd8bc19f51ef 100644 --- a/compiler-rt/lib/msan/msan.h +++ b/compiler-rt/lib/msan/msan.h @@ -52,6 +52,25 @@ const MappingDesc kMemoryLayout[] = { #define MEM_TO_SHADOW(mem) (((uptr)(mem)) & ~0x4000000000ULL) #define SHADOW_TO_ORIGIN(shadow) (((uptr)(shadow)) + 0x002000000000) +#elif SANITIZER_LINUX && defined(__powerpc64__) + +const MappingDesc kMemoryLayout[] = { + {0x000000000000ULL, 0x000100000000ULL, MappingDesc::APP, "low memory"}, + {0x000100000000ULL, 0x080000000000ULL, MappingDesc::INVALID, "invalid"}, + {0x080000000000ULL, 0x180100000000ULL, MappingDesc::SHADOW, "shadow"}, + {0x180100000000ULL, 0x1C0000000000ULL, MappingDesc::INVALID, "invalid"}, + {0x1C0000000000ULL, 0x2C0100000000ULL, MappingDesc::ORIGIN, "origin"}, + {0x2C0100000000ULL, 0x300000000000ULL, MappingDesc::INVALID, "invalid"}, + {0x300000000000ULL, 0x400000000000ULL, MappingDesc::APP, "high memory"}}; + +// Maps low and high app ranges to contiguous space with zero base: +// Low: 0000 0000 0000 - 0000 ffff ffff -> 1000 0000 0000 - 1000 ffff ffff +// High: 3000 0000 0000 - 3fff ffff ffff -> 0000 0000 0000 - 0fff ffff ffff +#define LINEARIZE_MEM(mem) \ + (((uptr)(mem) & ~0x200000000000ULL) ^ 0x100000000000ULL) +#define MEM_TO_SHADOW(mem) (LINEARIZE_MEM((mem)) + 0x080000000000ULL) +#define SHADOW_TO_ORIGIN(shadow) (((uptr)(shadow)) + 0x140000000000ULL) + #elif SANITIZER_FREEBSD && SANITIZER_WORDSIZE == 64 // Low memory: main binary, MAP_32BIT mappings and modules diff --git a/compiler-rt/lib/msan/msan_allocator.cc b/compiler-rt/lib/msan/msan_allocator.cc index 698b6cddd30b..6df35664279f 100644 --- a/compiler-rt/lib/msan/msan_allocator.cc +++ b/compiler-rt/lib/msan/msan_allocator.cc @@ -55,6 +55,15 @@ struct MsanMapUnmapCallback { static const uptr kMetadataSize = sizeof(Metadata); static const uptr kMaxAllowedMallocSize = 8UL << 30; + typedef SizeClassAllocator64 PrimaryAllocator; +#elif defined(__powerpc64__) + static const uptr kAllocatorSpace = 0x300000000000; + static const uptr kAllocatorSize = 0x020000000000; // 2T + static const uptr kMetadataSize = sizeof(Metadata); + static const uptr kMaxAllowedMallocSize = 2UL << 30; // 2G + typedef SizeClassAllocator64 PrimaryAllocator; diff --git a/compiler-rt/lib/sanitizer_common/sanitizer_stacktrace.cc b/compiler-rt/lib/sanitizer_common/sanitizer_stacktrace.cc index 2deadb6e3560..84ff9d9d9e3b 100644 --- a/compiler-rt/lib/sanitizer_common/sanitizer_stacktrace.cc +++ b/compiler-rt/lib/sanitizer_common/sanitizer_stacktrace.cc @@ -20,6 +20,8 @@ namespace __sanitizer { uptr StackTrace::GetNextInstructionPc(uptr pc) { #if defined(__mips__) return pc + 8; +#elif defined(__powerpc__) + return pc + 4; #else return pc + 1; #endif diff --git a/compiler-rt/test/msan/mmap.cc b/compiler-rt/test/msan/mmap.cc index cd7b93d2251b..c09fcb76a827 100644 --- a/compiler-rt/test/msan/mmap.cc +++ b/compiler-rt/test/msan/mmap.cc @@ -16,6 +16,8 @@ bool AddrIsApp(void *p) { return addr >= 0x600000000000ULL; #elif defined(__mips64) return addr >= 0x00e000000000ULL; +#elif defined(__powerpc64__) + return addr < 0x000100000000ULL || addr >= 0x300000000000ULL; #endif } diff --git a/compiler-rt/test/msan/mmap_below_shadow.cc b/compiler-rt/test/msan/mmap_below_shadow.cc index 5f25a9b62992..563d8774f525 100644 --- a/compiler-rt/test/msan/mmap_below_shadow.cc +++ b/compiler-rt/test/msan/mmap_below_shadow.cc @@ -24,6 +24,9 @@ int main(void) { #elif defined (__mips64) uintptr_t hint = 0x4f00000000ULL; const uintptr_t app_start = 0x6000000000ULL; +#elif defined (__powerpc64__) + uintptr_t hint = 0x2f0000000000ULL; + const uintptr_t app_start = 0x300000000000ULL; #endif uintptr_t p = (uintptr_t)mmap( (void *)hint, 4096, PROT_WRITE, diff --git a/compiler-rt/test/msan/strlen_of_shadow.cc b/compiler-rt/test/msan/strlen_of_shadow.cc index d6f8be70bf91..763b3a1c73c0 100644 --- a/compiler-rt/test/msan/strlen_of_shadow.cc +++ b/compiler-rt/test/msan/strlen_of_shadow.cc @@ -13,6 +13,10 @@ const char *mem_to_shadow(const char *p) { return (char *)((uintptr_t)p & ~0x400000000000ULL); #elif defined (__mips64) return (char *)((uintptr_t)p & ~0x4000000000ULL); +#elif defined(__powerpc64__) +#define LINEARIZE_MEM(mem) \ + (((uintptr_t)(mem) & ~0x200000000000ULL) ^ 0x100000000000ULL) + return (char *)(LINEARIZE_MEM(p) + 0x080000000000ULL); #endif }