]> git.proxmox.com Git - rustc.git/blob - src/compiler-rt/test/msan/mmap.cc
New upstream version 1.19.0+dfsg3
[rustc.git] / src / compiler-rt / test / msan / mmap.cc
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
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(__aarch64__)
28
29 struct AddrMapping {
30 uintptr_t start;
31 uintptr_t end;
32 } mappings[] = {
33 {0x05000000000ULL, 0x06000000000ULL},
34 {0x07000000000ULL, 0x08000000000ULL},
35 {0x0F000000000ULL, 0x10000000000ULL},
36 {0x11000000000ULL, 0x12000000000ULL},
37 {0x20000000000ULL, 0x21000000000ULL},
38 {0x2A000000000ULL, 0x2B000000000ULL},
39 {0x2E000000000ULL, 0x2F000000000ULL},
40 {0x3B000000000ULL, 0x3C000000000ULL},
41 {0x3F000000000ULL, 0x40000000000ULL},
42 {0x0041000000000ULL, 0x0042000000000ULL},
43 {0x0050000000000ULL, 0x0051000000000ULL},
44 {0x0058000000000ULL, 0x0059000000000ULL},
45 {0x0061000000000ULL, 0x0062000000000ULL},
46 {0x0AAAAA0000000ULL, 0x0AAAB00000000ULL},
47 {0x0FFFF00000000ULL, 0x1000000000000ULL},
48 };
49 const size_t mappingsSize = sizeof (mappings) / sizeof (mappings[0]);
50
51 for (int i=0; i<mappingsSize; ++i)
52 if (addr >= mappings[i].start && addr < mappings[i].end)
53 return true;
54 return false;
55 #endif
56 }
57
58 int main() {
59 // Large enough to quickly exhaust the entire address space.
60 #if defined(__mips64) || defined(__aarch64__)
61 const size_t kMapSize = 0x100000000ULL;
62 #else
63 const size_t kMapSize = 0x1000000000ULL;
64 #endif
65 int success_count = 0;
66 while (true) {
67 void *p = mmap(0, kMapSize, PROT_WRITE,
68 MAP_PRIVATE | MAP_ANONYMOUS | MAP_NORESERVE, -1, 0);
69 printf("%p\n", p);
70 if (p == MAP_FAILED) {
71 assert(errno == ENOMEM);
72 break;
73 }
74 assert(AddrIsApp(p));
75 success_count++;
76 }
77 printf("successful mappings: %d\n", success_count);
78 assert(success_count > 5);
79 }