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