1 // SPDX-License-Identifier: GPL-2.0
4 * Test that MAP_FIXED_NOREPLACE works.
6 * Copyright 2018, Jann Horn <jannh@google.com>
7 * Copyright 2018, Michael Ellerman, IBM Corporation.
16 #ifndef MAP_FIXED_NOREPLACE
17 #define MAP_FIXED_NOREPLACE 0x100000
20 static void dump_maps(void)
24 snprintf(cmd, sizeof(cmd), "cat /proc/%d/maps", getpid());
28 static unsigned long find_base_addr(unsigned long size)
33 flags = MAP_PRIVATE | MAP_ANONYMOUS;
34 addr = mmap(NULL, size, PROT_NONE, flags, -1, 0);
35 if (addr == MAP_FAILED) {
36 printf("Error: couldn't map the space we need for the test\n");
40 if (munmap(addr, size) != 0) {
41 printf("Error: couldn't map the space we need for the test\n");
44 return (unsigned long)addr;
49 unsigned long base_addr;
50 unsigned long flags, addr, size, page_size;
53 page_size = sysconf(_SC_PAGE_SIZE);
55 //let's find a base addr that is free before we start the tests
57 base_addr = find_base_addr(size);
59 printf("Error: couldn't map the space we need for the test\n");
63 flags = MAP_PRIVATE | MAP_ANONYMOUS | MAP_FIXED_NOREPLACE;
65 // Check we can map all the areas we need below
69 p = mmap((void *)addr, size, PROT_NONE, flags, -1, 0);
71 printf("mmap() @ 0x%lx-0x%lx p=%p result=%m\n", addr, addr + size, p);
73 if (p == MAP_FAILED) {
75 printf("Error: couldn't map the space we need for the test\n");
80 if (munmap((void *)addr, 5 * page_size) != 0) {
82 printf("Error: munmap failed!?\n");
85 printf("unmap() successful\n");
88 addr = base_addr + page_size;
90 p = mmap((void *)addr, size, PROT_NONE, flags, -1, 0);
91 printf("mmap() @ 0x%lx-0x%lx p=%p result=%m\n", addr, addr + size, p);
93 if (p == MAP_FAILED) {
95 printf("Error: first mmap() failed unexpectedly\n");
100 * Exact same mapping again:
109 size = 5 * page_size;
110 p = mmap((void *)addr, size, PROT_NONE, flags, -1, 0);
111 printf("mmap() @ 0x%lx-0x%lx p=%p result=%m\n", addr, addr + size, p);
113 if (p != MAP_FAILED) {
115 printf("Error:1: mmap() succeeded when it shouldn't have\n");
120 * Second mapping contained within first:
129 addr = base_addr + (2 * page_size);
131 p = mmap((void *)addr, size, PROT_NONE, flags, -1, 0);
132 printf("mmap() @ 0x%lx-0x%lx p=%p result=%m\n", addr, addr + size, p);
134 if (p != MAP_FAILED) {
136 printf("Error:2: mmap() succeeded when it shouldn't have\n");
141 * Overlap end of existing mapping:
149 addr = base_addr + (3 * page_size);
150 size = 2 * page_size;
151 p = mmap((void *)addr, size, PROT_NONE, flags, -1, 0);
152 printf("mmap() @ 0x%lx-0x%lx p=%p result=%m\n", addr, addr + size, p);
154 if (p != MAP_FAILED) {
156 printf("Error:3: mmap() succeeded when it shouldn't have\n");
161 * Overlap start of existing mapping:
170 size = 2 * page_size;
171 p = mmap((void *)addr, size, PROT_NONE, flags, -1, 0);
172 printf("mmap() @ 0x%lx-0x%lx p=%p result=%m\n", addr, addr + size, p);
174 if (p != MAP_FAILED) {
176 printf("Error:4: mmap() succeeded when it shouldn't have\n");
181 * Adjacent to start of existing mapping:
191 p = mmap((void *)addr, size, PROT_NONE, flags, -1, 0);
192 printf("mmap() @ 0x%lx-0x%lx p=%p result=%m\n", addr, addr + size, p);
194 if (p == MAP_FAILED) {
196 printf("Error:5: mmap() failed when it shouldn't have\n");
201 * Adjacent to end of existing mapping:
209 addr = base_addr + (4 * page_size);
211 p = mmap((void *)addr, size, PROT_NONE, flags, -1, 0);
212 printf("mmap() @ 0x%lx-0x%lx p=%p result=%m\n", addr, addr + size, p);
214 if (p == MAP_FAILED) {
216 printf("Error:6: mmap() failed when it shouldn't have\n");
221 size = 5 * page_size;
222 if (munmap((void *)addr, size) != 0) {
224 printf("Error: munmap failed!?\n");
227 printf("unmap() successful\n");