1*7c3d14c8STreehugger Robot // Test that mmap (without MAP_FIXED) always returns valid application addresses.
2*7c3d14c8STreehugger Robot // RUN: %clangxx_msan -O0 %s -o %t && %run %t
3*7c3d14c8STreehugger Robot // RUN: %clangxx_msan -O0 -fsanitize-memory-track-origins %s -o %t && %run %t
4*7c3d14c8STreehugger Robot
5*7c3d14c8STreehugger Robot #include <assert.h>
6*7c3d14c8STreehugger Robot #include <errno.h>
7*7c3d14c8STreehugger Robot #include <stdint.h>
8*7c3d14c8STreehugger Robot #include <sys/mman.h>
9*7c3d14c8STreehugger Robot #include <stdio.h>
10*7c3d14c8STreehugger Robot #include <stdlib.h>
11*7c3d14c8STreehugger Robot #include "test.h"
12*7c3d14c8STreehugger Robot
AddrIsApp(void * p)13*7c3d14c8STreehugger Robot bool AddrIsApp(void *p) {
14*7c3d14c8STreehugger Robot uintptr_t addr = (uintptr_t)p;
15*7c3d14c8STreehugger Robot #if defined(__FreeBSD__) && defined(__x86_64__)
16*7c3d14c8STreehugger Robot return addr < 0x010000000000ULL || addr >= 0x600000000000ULL;
17*7c3d14c8STreehugger Robot #elif defined(__x86_64__)
18*7c3d14c8STreehugger Robot return (addr >= 0x000000000000ULL && addr < 0x010000000000ULL) ||
19*7c3d14c8STreehugger Robot (addr >= 0x510000000000ULL && addr < 0x600000000000ULL) ||
20*7c3d14c8STreehugger Robot (addr >= 0x700000000000ULL && addr < 0x800000000000ULL);
21*7c3d14c8STreehugger Robot #elif defined(__mips64)
22*7c3d14c8STreehugger Robot return addr >= 0x00e000000000ULL;
23*7c3d14c8STreehugger Robot #elif defined(__powerpc64__)
24*7c3d14c8STreehugger Robot return addr < 0x000100000000ULL || addr >= 0x300000000000ULL;
25*7c3d14c8STreehugger Robot #elif defined(__aarch64__)
26*7c3d14c8STreehugger Robot
27*7c3d14c8STreehugger Robot struct AddrMapping {
28*7c3d14c8STreehugger Robot uintptr_t start;
29*7c3d14c8STreehugger Robot uintptr_t end;
30*7c3d14c8STreehugger Robot } mappings[] = {
31*7c3d14c8STreehugger Robot {0x05000000000ULL, 0x06000000000ULL},
32*7c3d14c8STreehugger Robot {0x07000000000ULL, 0x08000000000ULL},
33*7c3d14c8STreehugger Robot {0x0F000000000ULL, 0x10000000000ULL},
34*7c3d14c8STreehugger Robot {0x11000000000ULL, 0x12000000000ULL},
35*7c3d14c8STreehugger Robot {0x20000000000ULL, 0x21000000000ULL},
36*7c3d14c8STreehugger Robot {0x2A000000000ULL, 0x2B000000000ULL},
37*7c3d14c8STreehugger Robot {0x2E000000000ULL, 0x2F000000000ULL},
38*7c3d14c8STreehugger Robot {0x3B000000000ULL, 0x3C000000000ULL},
39*7c3d14c8STreehugger Robot {0x3F000000000ULL, 0x40000000000ULL},
40*7c3d14c8STreehugger Robot };
41*7c3d14c8STreehugger Robot const size_t mappingsSize = sizeof (mappings) / sizeof (mappings[0]);
42*7c3d14c8STreehugger Robot
43*7c3d14c8STreehugger Robot for (int i=0; i<mappingsSize; ++i)
44*7c3d14c8STreehugger Robot if (addr >= mappings[i].start && addr < mappings[i].end)
45*7c3d14c8STreehugger Robot return true;
46*7c3d14c8STreehugger Robot return false;
47*7c3d14c8STreehugger Robot #endif
48*7c3d14c8STreehugger Robot }
49*7c3d14c8STreehugger Robot
main()50*7c3d14c8STreehugger Robot int main() {
51*7c3d14c8STreehugger Robot // Large enough to quickly exhaust the entire address space.
52*7c3d14c8STreehugger Robot #if defined(__mips64) || defined(__aarch64__)
53*7c3d14c8STreehugger Robot const size_t kMapSize = 0x100000000ULL;
54*7c3d14c8STreehugger Robot #else
55*7c3d14c8STreehugger Robot const size_t kMapSize = 0x1000000000ULL;
56*7c3d14c8STreehugger Robot #endif
57*7c3d14c8STreehugger Robot int success_count = 0;
58*7c3d14c8STreehugger Robot while (true) {
59*7c3d14c8STreehugger Robot void *p = mmap(0, kMapSize, PROT_WRITE,
60*7c3d14c8STreehugger Robot MAP_PRIVATE | MAP_ANONYMOUS | MAP_NORESERVE, -1, 0);
61*7c3d14c8STreehugger Robot printf("%p\n", p);
62*7c3d14c8STreehugger Robot if (p == MAP_FAILED) {
63*7c3d14c8STreehugger Robot assert(errno == ENOMEM);
64*7c3d14c8STreehugger Robot break;
65*7c3d14c8STreehugger Robot }
66*7c3d14c8STreehugger Robot assert(AddrIsApp(p));
67*7c3d14c8STreehugger Robot success_count++;
68*7c3d14c8STreehugger Robot }
69*7c3d14c8STreehugger Robot printf("successful mappings: %d\n", success_count);
70*7c3d14c8STreehugger Robot assert(success_count > 5);
71*7c3d14c8STreehugger Robot }
72