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 #define BASE_ADDRESS (256ul * 1024 * 1024)
23 static void dump_maps(void)
27 snprintf(cmd, sizeof(cmd), "cat /proc/%d/maps", getpid());
33 unsigned long flags, addr, size, page_size;
36 page_size = sysconf(_SC_PAGE_SIZE);
38 flags = MAP_PRIVATE | MAP_ANONYMOUS | MAP_FIXED_NOREPLACE;
40 // Check we can map all the areas we need below
44 p = mmap((void *)addr, size, PROT_NONE, flags, -1, 0);
46 printf("mmap() @ 0x%lx-0x%lx p=%p result=%m\n", addr, addr + size, p);
48 if (p == MAP_FAILED) {
50 printf("Error: couldn't map the space we need for the test\n");
55 if (munmap((void *)addr, 5 * page_size) != 0) {
57 printf("Error: munmap failed!?\n");
60 printf("unmap() successful\n");
63 addr = BASE_ADDRESS + page_size;
65 p = mmap((void *)addr, size, PROT_NONE, flags, -1, 0);
66 printf("mmap() @ 0x%lx-0x%lx p=%p result=%m\n", addr, addr + size, p);
68 if (p == MAP_FAILED) {
70 printf("Error: first mmap() failed unexpectedly\n");
75 * Exact same mapping again:
85 p = mmap((void *)addr, size, PROT_NONE, flags, -1, 0);
86 printf("mmap() @ 0x%lx-0x%lx p=%p result=%m\n", addr, addr + size, p);
88 if (p != MAP_FAILED) {
90 printf("Error:1: mmap() succeeded when it shouldn't have\n");
95 * Second mapping contained within first:
104 addr = BASE_ADDRESS + (2 * page_size);
106 p = mmap((void *)addr, size, PROT_NONE, flags, -1, 0);
107 printf("mmap() @ 0x%lx-0x%lx p=%p result=%m\n", addr, addr + size, p);
109 if (p != MAP_FAILED) {
111 printf("Error:2: mmap() succeeded when it shouldn't have\n");
116 * Overlap end of existing mapping:
124 addr = BASE_ADDRESS + (3 * page_size);
125 size = 2 * page_size;
126 p = mmap((void *)addr, size, PROT_NONE, flags, -1, 0);
127 printf("mmap() @ 0x%lx-0x%lx p=%p result=%m\n", addr, addr + size, p);
129 if (p != MAP_FAILED) {
131 printf("Error:3: mmap() succeeded when it shouldn't have\n");
136 * Overlap start of existing mapping:
145 size = 2 * page_size;
146 p = mmap((void *)addr, size, PROT_NONE, flags, -1, 0);
147 printf("mmap() @ 0x%lx-0x%lx p=%p result=%m\n", addr, addr + size, p);
149 if (p != MAP_FAILED) {
151 printf("Error:4: mmap() succeeded when it shouldn't have\n");
156 * Adjacent to start of existing mapping:
166 p = mmap((void *)addr, size, PROT_NONE, flags, -1, 0);
167 printf("mmap() @ 0x%lx-0x%lx p=%p result=%m\n", addr, addr + size, p);
169 if (p == MAP_FAILED) {
171 printf("Error:5: mmap() failed when it shouldn't have\n");
176 * Adjacent to end of existing mapping:
184 addr = BASE_ADDRESS + (4 * page_size);
186 p = mmap((void *)addr, size, PROT_NONE, flags, -1, 0);
187 printf("mmap() @ 0x%lx-0x%lx p=%p result=%m\n", addr, addr + size, p);
189 if (p == MAP_FAILED) {
191 printf("Error:6: mmap() failed when it shouldn't have\n");
196 size = 5 * page_size;
197 if (munmap((void *)addr, size) != 0) {
199 printf("Error: munmap failed!?\n");
202 printf("unmap() successful\n");