summaryrefslogtreecommitdiff
path: root/test/msan
diff options
context:
space:
mode:
authorEvgeniy Stepanov <eugeni.stepanov@gmail.com>2015-05-24 02:47:59 +0000
committerEvgeniy Stepanov <eugeni.stepanov@gmail.com>2015-05-24 02:47:59 +0000
commit5236cff8bf581bb2615c787eb59ed53d0cb95586 (patch)
treef5d3f2bff7bad83103298dc5a47fd3a57ba798e8 /test/msan
parent1cc9bbb81c4e4eb09444cd233cb622e180b6cadc (diff)
[msan] Mprotect all inaccessible memory regions.
Fix 2 bugs in memory mapping setup: - the invalid region at offset 0 was not protected because mmap at address 0 fails with EPERM on most Linux systems. We did not notice this because the check condition was flipped: the code was checking that mprotect has failed. And the test that was supposed to catch this was weakened by the mitigations in the mmap interceptor. - when running without origins, the origin shadow range was left unprotected. The new test ensures that mmap w/o MAP_FIXED always returns valid application addresses. git-svn-id: https://llvm.org/svn/llvm-project/compiler-rt/trunk@238109 91177308-0d34-0410-b5e6-96231b3b80d8
Diffstat (limited to 'test/msan')
-rw-r--r--test/msan/mmap.cc42
1 files changed, 42 insertions, 0 deletions
diff --git a/test/msan/mmap.cc b/test/msan/mmap.cc
new file mode 100644
index 000000000..c657052e0
--- /dev/null
+++ b/test/msan/mmap.cc
@@ -0,0 +1,42 @@
+// Test that mmap (without MAP_FIXED) always returns valid application addresses.
+// RUN: %clangxx_msan -O0 %s -o %t && %run %t
+// RUN: %clangxx_msan -O0 -fsanitize-memory-track-origins %s -o %t && %run %t
+
+#include <assert.h>
+#include <errno.h>
+#include <stdint.h>
+#include <sys/mman.h>
+#include <stdio.h>
+
+bool AddrIsApp(void *p) {
+ uintptr_t addr = (uintptr_t)p;
+#if defined(__FreeBSD__) && defined(__x86_64__)
+ return addr < 0x010000000000ULL || addr >= 0x600000000000ULL;
+#elif defined(__x86_64__)
+ return addr >= 0x600000000000ULL;
+#elif defined(__mips64)
+ return addr >= 0x00e000000000ULL;
+#endif
+}
+
+int main() {
+ // Large enough to quickly exhaust the entire address space.
+#if defined(__mips64)
+ const size_t kMapSize = 0x100000000ULL;
+#else
+ const size_t kMapSize = 0x1000000000ULL;
+#endif
+ int success_count = 0;
+ while (true) {
+ void *p = mmap(0, kMapSize, PROT_WRITE, MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
+ printf("%p\n", p);
+ if (p == MAP_FAILED) {
+ assert(errno == ENOMEM);
+ break;
+ }
+ assert(AddrIsApp(p));
+ success_count++;
+ }
+ printf("successful mappings: %d\n", success_count);
+ assert(success_count > 5);
+}