linux/tools/testing/selftests/vm/map_fixed_noreplace.c
Aneesh Kumar K.V f39c58008d selftest/vm: fix map_fixed_noreplace test failure
On the latest RHEL the test fails due to executable mapped at 256MB
address

     # ./map_fixed_noreplace
    mmap() @ 0x10000000-0x10050000 p=0xffffffffffffffff result=File exists
    10000000-10010000 r-xp 00000000 fd:04 34905657                           /root/rpmbuild/BUILD/kernel-5.14.0-56.el9/linux-5.14.0-56.el9.ppc64le/tools/testing/selftests/vm/map_fixed_noreplace
    10010000-10020000 r--p 00000000 fd:04 34905657                           /root/rpmbuild/BUILD/kernel-5.14.0-56.el9/linux-5.14.0-56.el9.ppc64le/tools/testing/selftests/vm/map_fixed_noreplace
    10020000-10030000 rw-p 00010000 fd:04 34905657                           /root/rpmbuild/BUILD/kernel-5.14.0-56.el9/linux-5.14.0-56.el9.ppc64le/tools/testing/selftests/vm/map_fixed_noreplace
    10029b90000-10029bc0000 rw-p 00000000 00:00 0                            [heap]
    7fffbb510000-7fffbb750000 r-xp 00000000 fd:04 24534                      /usr/lib64/libc.so.6
    7fffbb750000-7fffbb760000 r--p 00230000 fd:04 24534                      /usr/lib64/libc.so.6
    7fffbb760000-7fffbb770000 rw-p 00240000 fd:04 24534                      /usr/lib64/libc.so.6
    7fffbb780000-7fffbb7a0000 r--p 00000000 00:00 0                          [vvar]
    7fffbb7a0000-7fffbb7b0000 r-xp 00000000 00:00 0                          [vdso]
    7fffbb7b0000-7fffbb800000 r-xp 00000000 fd:04 24514                      /usr/lib64/ld64.so.2
    7fffbb800000-7fffbb810000 r--p 00040000 fd:04 24514                      /usr/lib64/ld64.so.2
    7fffbb810000-7fffbb820000 rw-p 00050000 fd:04 24514                      /usr/lib64/ld64.so.2
    7fffd93f0000-7fffd9420000 rw-p 00000000 00:00 0                          [stack]
    Error: couldn't map the space we need for the test

Fix this by finding a free address using mmap instead of hardcoding
BASE_ADDRESS.

Link: https://lkml.kernel.org/r/20220217083417.373823-1-aneesh.kumar@linux.ibm.com
Signed-off-by: Aneesh Kumar K.V <aneesh.kumar@linux.ibm.com>
Cc: Michael Ellerman <mpe@ellerman.id.au>
Cc: Jann Horn <jannh@google.com>
Cc: Shuah Khan <shuah@kernel.org>
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
2022-02-26 09:51:17 -08:00

232 lines
5.1 KiB
C

// SPDX-License-Identifier: GPL-2.0
/*
* Test that MAP_FIXED_NOREPLACE works.
*
* Copyright 2018, Jann Horn <jannh@google.com>
* Copyright 2018, Michael Ellerman, IBM Corporation.
*/
#include <sys/mman.h>
#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#ifndef MAP_FIXED_NOREPLACE
#define MAP_FIXED_NOREPLACE 0x100000
#endif
static void dump_maps(void)
{
char cmd[32];
snprintf(cmd, sizeof(cmd), "cat /proc/%d/maps", getpid());
system(cmd);
}
static unsigned long find_base_addr(unsigned long size)
{
void *addr;
unsigned long flags;
flags = MAP_PRIVATE | MAP_ANONYMOUS;
addr = mmap(NULL, size, PROT_NONE, flags, -1, 0);
if (addr == MAP_FAILED) {
printf("Error: couldn't map the space we need for the test\n");
return 0;
}
if (munmap(addr, size) != 0) {
printf("Error: couldn't map the space we need for the test\n");
return 0;
}
return (unsigned long)addr;
}
int main(void)
{
unsigned long base_addr;
unsigned long flags, addr, size, page_size;
char *p;
page_size = sysconf(_SC_PAGE_SIZE);
//let's find a base addr that is free before we start the tests
size = 5 * page_size;
base_addr = find_base_addr(size);
if (!base_addr) {
printf("Error: couldn't map the space we need for the test\n");
return 1;
}
flags = MAP_PRIVATE | MAP_ANONYMOUS | MAP_FIXED_NOREPLACE;
// Check we can map all the areas we need below
errno = 0;
addr = base_addr;
size = 5 * page_size;
p = mmap((void *)addr, size, PROT_NONE, flags, -1, 0);
printf("mmap() @ 0x%lx-0x%lx p=%p result=%m\n", addr, addr + size, p);
if (p == MAP_FAILED) {
dump_maps();
printf("Error: couldn't map the space we need for the test\n");
return 1;
}
errno = 0;
if (munmap((void *)addr, 5 * page_size) != 0) {
dump_maps();
printf("Error: munmap failed!?\n");
return 1;
}
printf("unmap() successful\n");
errno = 0;
addr = base_addr + page_size;
size = 3 * page_size;
p = mmap((void *)addr, size, PROT_NONE, flags, -1, 0);
printf("mmap() @ 0x%lx-0x%lx p=%p result=%m\n", addr, addr + size, p);
if (p == MAP_FAILED) {
dump_maps();
printf("Error: first mmap() failed unexpectedly\n");
return 1;
}
/*
* Exact same mapping again:
* base | free | new
* +1 | mapped | new
* +2 | mapped | new
* +3 | mapped | new
* +4 | free | new
*/
errno = 0;
addr = base_addr;
size = 5 * page_size;
p = mmap((void *)addr, size, PROT_NONE, flags, -1, 0);
printf("mmap() @ 0x%lx-0x%lx p=%p result=%m\n", addr, addr + size, p);
if (p != MAP_FAILED) {
dump_maps();
printf("Error:1: mmap() succeeded when it shouldn't have\n");
return 1;
}
/*
* Second mapping contained within first:
*
* base | free |
* +1 | mapped |
* +2 | mapped | new
* +3 | mapped |
* +4 | free |
*/
errno = 0;
addr = base_addr + (2 * page_size);
size = page_size;
p = mmap((void *)addr, size, PROT_NONE, flags, -1, 0);
printf("mmap() @ 0x%lx-0x%lx p=%p result=%m\n", addr, addr + size, p);
if (p != MAP_FAILED) {
dump_maps();
printf("Error:2: mmap() succeeded when it shouldn't have\n");
return 1;
}
/*
* Overlap end of existing mapping:
* base | free |
* +1 | mapped |
* +2 | mapped |
* +3 | mapped | new
* +4 | free | new
*/
errno = 0;
addr = base_addr + (3 * page_size);
size = 2 * page_size;
p = mmap((void *)addr, size, PROT_NONE, flags, -1, 0);
printf("mmap() @ 0x%lx-0x%lx p=%p result=%m\n", addr, addr + size, p);
if (p != MAP_FAILED) {
dump_maps();
printf("Error:3: mmap() succeeded when it shouldn't have\n");
return 1;
}
/*
* Overlap start of existing mapping:
* base | free | new
* +1 | mapped | new
* +2 | mapped |
* +3 | mapped |
* +4 | free |
*/
errno = 0;
addr = base_addr;
size = 2 * page_size;
p = mmap((void *)addr, size, PROT_NONE, flags, -1, 0);
printf("mmap() @ 0x%lx-0x%lx p=%p result=%m\n", addr, addr + size, p);
if (p != MAP_FAILED) {
dump_maps();
printf("Error:4: mmap() succeeded when it shouldn't have\n");
return 1;
}
/*
* Adjacent to start of existing mapping:
* base | free | new
* +1 | mapped |
* +2 | mapped |
* +3 | mapped |
* +4 | free |
*/
errno = 0;
addr = base_addr;
size = page_size;
p = mmap((void *)addr, size, PROT_NONE, flags, -1, 0);
printf("mmap() @ 0x%lx-0x%lx p=%p result=%m\n", addr, addr + size, p);
if (p == MAP_FAILED) {
dump_maps();
printf("Error:5: mmap() failed when it shouldn't have\n");
return 1;
}
/*
* Adjacent to end of existing mapping:
* base | free |
* +1 | mapped |
* +2 | mapped |
* +3 | mapped |
* +4 | free | new
*/
errno = 0;
addr = base_addr + (4 * page_size);
size = page_size;
p = mmap((void *)addr, size, PROT_NONE, flags, -1, 0);
printf("mmap() @ 0x%lx-0x%lx p=%p result=%m\n", addr, addr + size, p);
if (p == MAP_FAILED) {
dump_maps();
printf("Error:6: mmap() failed when it shouldn't have\n");
return 1;
}
addr = base_addr;
size = 5 * page_size;
if (munmap((void *)addr, size) != 0) {
dump_maps();
printf("Error: munmap failed!?\n");
return 1;
}
printf("unmap() successful\n");
printf("OK\n");
return 0;
}