This source file includes following definitions.
- hind_addr
- validate_addr
- validate_lower_address_hint
- main
1
2
3
4
5
6
7
8 #include <stdio.h>
9 #include <stdlib.h>
10 #include <string.h>
11 #include <unistd.h>
12 #include <errno.h>
13 #include <sys/mman.h>
14 #include <sys/time.h>
15
16
17
18
19
20
21
22 #define MAP_CHUNK_SIZE 17179869184UL
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39 #define NR_CHUNKS_128TB 8192UL
40 #define NR_CHUNKS_256TB (NR_CHUNKS_128TB * 2UL)
41 #define NR_CHUNKS_384TB (NR_CHUNKS_128TB * 3UL)
42
43 #define ADDR_MARK_128TB (1UL << 47)
44 #define ADDR_MARK_256TB (1UL << 48)
45
46 #ifdef __aarch64__
47 #define HIGH_ADDR_MARK ADDR_MARK_256TB
48 #define HIGH_ADDR_SHIFT 49
49 #define NR_CHUNKS_LOW NR_CHUNKS_256TB
50 #define NR_CHUNKS_HIGH 0
51 #else
52 #define HIGH_ADDR_MARK ADDR_MARK_128TB
53 #define HIGH_ADDR_SHIFT 48
54 #define NR_CHUNKS_LOW NR_CHUNKS_128TB
55 #define NR_CHUNKS_HIGH NR_CHUNKS_384TB
56 #endif
57
58 static char *hind_addr(void)
59 {
60 int bits = HIGH_ADDR_SHIFT + rand() % (63 - HIGH_ADDR_SHIFT);
61
62 return (char *) (1UL << bits);
63 }
64
65 static int validate_addr(char *ptr, int high_addr)
66 {
67 unsigned long addr = (unsigned long) ptr;
68
69 if (high_addr) {
70 if (addr < HIGH_ADDR_MARK) {
71 printf("Bad address %lx\n", addr);
72 return 1;
73 }
74 return 0;
75 }
76
77 if (addr > HIGH_ADDR_MARK) {
78 printf("Bad address %lx\n", addr);
79 return 1;
80 }
81 return 0;
82 }
83
84 static int validate_lower_address_hint(void)
85 {
86 char *ptr;
87
88 ptr = mmap((void *) (1UL << 45), MAP_CHUNK_SIZE, PROT_READ |
89 PROT_WRITE, MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
90
91 if (ptr == MAP_FAILED)
92 return 0;
93
94 return 1;
95 }
96
97 int main(int argc, char *argv[])
98 {
99 char *ptr[NR_CHUNKS_LOW];
100 char *hptr[NR_CHUNKS_HIGH];
101 char *hint;
102 unsigned long i, lchunks, hchunks;
103
104 for (i = 0; i < NR_CHUNKS_LOW; i++) {
105 ptr[i] = mmap(NULL, MAP_CHUNK_SIZE, PROT_READ | PROT_WRITE,
106 MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
107
108 if (ptr[i] == MAP_FAILED) {
109 if (validate_lower_address_hint())
110 return 1;
111 break;
112 }
113
114 if (validate_addr(ptr[i], 0))
115 return 1;
116 }
117 lchunks = i;
118
119 for (i = 0; i < NR_CHUNKS_HIGH; i++) {
120 hint = hind_addr();
121 hptr[i] = mmap(hint, MAP_CHUNK_SIZE, PROT_READ | PROT_WRITE,
122 MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
123
124 if (hptr[i] == MAP_FAILED)
125 break;
126
127 if (validate_addr(hptr[i], 1))
128 return 1;
129 }
130 hchunks = i;
131
132 for (i = 0; i < lchunks; i++)
133 munmap(ptr[i], MAP_CHUNK_SIZE);
134
135 for (i = 0; i < hchunks; i++)
136 munmap(hptr[i], MAP_CHUNK_SIZE);
137
138 return 0;
139 }