summaryrefslogtreecommitdiff
path: root/test/msan/mmap.cc
diff options
context:
space:
mode:
Diffstat (limited to 'test/msan/mmap.cc')
-rw-r--r--test/msan/mmap.cc45
1 files changed, 45 insertions, 0 deletions
diff --git a/test/msan/mmap.cc b/test/msan/mmap.cc
new file mode 100644
index 0000000000000..c09fcb76a8279
--- /dev/null
+++ b/test/msan/mmap.cc
@@ -0,0 +1,45 @@
+// 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;
+#elif defined(__powerpc64__)
+ return addr < 0x000100000000ULL || addr >= 0x300000000000ULL;
+#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 | MAP_NORESERVE, -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);
+}