2015-05-24 10:47:59 +08:00
|
|
|
// 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>
|
2015-09-16 23:12:25 +08:00
|
|
|
#include <stdlib.h>
|
|
|
|
#include "test.h"
|
2015-05-24 10:47:59 +08:00
|
|
|
|
|
|
|
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;
|
2015-06-25 14:22:31 +08:00
|
|
|
#elif defined(__powerpc64__)
|
|
|
|
return addr < 0x000100000000ULL || addr >= 0x300000000000ULL;
|
2015-09-16 23:12:25 +08:00
|
|
|
#elif defined(__aarch64__)
|
|
|
|
unsigned long vma = SystemVMA();
|
|
|
|
if (vma == 39)
|
|
|
|
return (addr >= 0x5500000000ULL && addr < 0x5600000000ULL) ||
|
|
|
|
(addr > 0x7000000000ULL);
|
|
|
|
else if (vma == 42)
|
|
|
|
return (addr >= 0x2aa00000000ULL && addr < 0x2ab00000000ULL) ||
|
|
|
|
(addr > 0x3f000000000ULL);
|
|
|
|
else {
|
|
|
|
fprintf(stderr, "unsupported vma: %lu\n", vma);
|
|
|
|
exit(1);
|
|
|
|
}
|
2015-05-24 10:47:59 +08:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
int main() {
|
|
|
|
// Large enough to quickly exhaust the entire address space.
|
2015-09-16 23:12:25 +08:00
|
|
|
#if defined(__mips64) || defined(__aarch64__)
|
2015-05-24 10:47:59 +08:00
|
|
|
const size_t kMapSize = 0x100000000ULL;
|
|
|
|
#else
|
|
|
|
const size_t kMapSize = 0x1000000000ULL;
|
|
|
|
#endif
|
|
|
|
int success_count = 0;
|
|
|
|
while (true) {
|
2015-05-27 02:02:54 +08:00
|
|
|
void *p = mmap(0, kMapSize, PROT_WRITE,
|
|
|
|
MAP_PRIVATE | MAP_ANONYMOUS | MAP_NORESERVE, -1, 0);
|
2015-05-24 10:47:59 +08:00
|
|
|
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);
|
|
|
|
}
|