~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~

TOMOYO Linux Cross Reference
Linux/tools/testing/selftests/x86/test_mremap_vdso.c

Version: ~ [ linux-6.11.5 ] ~ [ linux-6.10.14 ] ~ [ linux-6.9.12 ] ~ [ linux-6.8.12 ] ~ [ linux-6.7.12 ] ~ [ linux-6.6.58 ] ~ [ linux-6.5.13 ] ~ [ linux-6.4.16 ] ~ [ linux-6.3.13 ] ~ [ linux-6.2.16 ] ~ [ linux-6.1.114 ] ~ [ linux-6.0.19 ] ~ [ linux-5.19.17 ] ~ [ linux-5.18.19 ] ~ [ linux-5.17.15 ] ~ [ linux-5.16.20 ] ~ [ linux-5.15.169 ] ~ [ linux-5.14.21 ] ~ [ linux-5.13.19 ] ~ [ linux-5.12.19 ] ~ [ linux-5.11.22 ] ~ [ linux-5.10.228 ] ~ [ linux-5.9.16 ] ~ [ linux-5.8.18 ] ~ [ linux-5.7.19 ] ~ [ linux-5.6.19 ] ~ [ linux-5.5.19 ] ~ [ linux-5.4.284 ] ~ [ linux-5.3.18 ] ~ [ linux-5.2.21 ] ~ [ linux-5.1.21 ] ~ [ linux-5.0.21 ] ~ [ linux-4.20.17 ] ~ [ linux-4.19.322 ] ~ [ linux-4.18.20 ] ~ [ linux-4.17.19 ] ~ [ linux-4.16.18 ] ~ [ linux-4.15.18 ] ~ [ linux-4.14.336 ] ~ [ linux-4.13.16 ] ~ [ linux-4.12.14 ] ~ [ linux-4.11.12 ] ~ [ linux-4.10.17 ] ~ [ linux-4.9.337 ] ~ [ linux-4.4.302 ] ~ [ linux-3.10.108 ] ~ [ linux-2.6.32.71 ] ~ [ linux-2.6.0 ] ~ [ linux-2.4.37.11 ] ~ [ unix-v6-master ] ~ [ ccs-tools-1.8.9 ] ~ [ policy-sample ] ~
Architecture: ~ [ i386 ] ~ [ alpha ] ~ [ m68k ] ~ [ mips ] ~ [ ppc ] ~ [ sparc ] ~ [ sparc64 ] ~

  1 // SPDX-License-Identifier: GPL-2.0-only
  2 /*
  3  * 32-bit test to check vDSO mremap.
  4  *
  5  * Copyright (c) 2016 Dmitry Safonov
  6  * Suggested-by: Andrew Lutomirski
  7  */
  8 /*
  9  * Can be built statically:
 10  * gcc -Os -Wall -static -m32 test_mremap_vdso.c
 11  */
 12 #define _GNU_SOURCE
 13 #include <stdio.h>
 14 #include <errno.h>
 15 #include <unistd.h>
 16 #include <string.h>
 17 
 18 #include <sys/mman.h>
 19 #include <sys/auxv.h>
 20 #include <sys/syscall.h>
 21 #include <sys/wait.h>
 22 #include "../kselftest.h"
 23 
 24 #define PAGE_SIZE       4096
 25 
 26 static int try_to_remap(void *vdso_addr, unsigned long size)
 27 {
 28         void *dest_addr, *new_addr;
 29 
 30         /* Searching for memory location where to remap */
 31         dest_addr = mmap(0, size, PROT_NONE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0);
 32         if (dest_addr == MAP_FAILED) {
 33                 ksft_print_msg("WARN: mmap failed (%d): %m\n", errno);
 34                 return 0;
 35         }
 36 
 37         ksft_print_msg("Moving vDSO: [%p, %#lx] -> [%p, %#lx]\n",
 38                        vdso_addr, (unsigned long)vdso_addr + size,
 39                        dest_addr, (unsigned long)dest_addr + size);
 40         fflush(stdout);
 41 
 42         new_addr = mremap(vdso_addr, size, size,
 43                         MREMAP_FIXED|MREMAP_MAYMOVE, dest_addr);
 44         if ((unsigned long)new_addr == (unsigned long)-1) {
 45                 munmap(dest_addr, size);
 46                 if (errno == EINVAL) {
 47                         ksft_print_msg("vDSO partial move failed, will try with bigger size\n");
 48                         return -1; /* Retry with larger */
 49                 }
 50                 ksft_print_msg("[FAIL]\tmremap failed (%d): %m\n", errno);
 51                 return 1;
 52         }
 53 
 54         return 0;
 55 
 56 }
 57 
 58 int main(int argc, char **argv, char **envp)
 59 {
 60         pid_t child;
 61 
 62         ksft_print_header();
 63         ksft_set_plan(1);
 64 
 65         child = fork();
 66         if (child == -1)
 67                 ksft_exit_fail_msg("failed to fork (%d): %m\n", errno);
 68 
 69         if (child == 0) {
 70                 unsigned long vdso_size = PAGE_SIZE;
 71                 unsigned long auxval;
 72                 int ret = -1;
 73 
 74                 auxval = getauxval(AT_SYSINFO_EHDR);
 75                 ksft_print_msg("AT_SYSINFO_EHDR is %#lx\n", auxval);
 76                 if (!auxval || auxval == -ENOENT) {
 77                         ksft_print_msg("WARN: getauxval failed\n");
 78                         return 0;
 79                 }
 80 
 81                 /* Simpler than parsing ELF header */
 82                 while (ret < 0) {
 83                         ret = try_to_remap((void *)auxval, vdso_size);
 84                         vdso_size += PAGE_SIZE;
 85                 }
 86 
 87 #ifdef __i386__
 88                 /* Glibc is likely to explode now - exit with raw syscall */
 89                 asm volatile ("int $0x80" : : "a" (__NR_exit), "b" (!!ret));
 90 #else /* __x86_64__ */
 91                 syscall(SYS_exit, ret);
 92 #endif
 93         } else {
 94                 int status;
 95 
 96                 if (waitpid(child, &status, 0) != child ||
 97                         !WIFEXITED(status))
 98                         ksft_test_result_fail("mremap() of the vDSO does not work on this kernel!\n");
 99                 else if (WEXITSTATUS(status) != 0)
100                         ksft_test_result_fail("Child failed with %d\n", WEXITSTATUS(status));
101                 else
102                         ksft_test_result_pass("%s\n", __func__);
103         }
104 
105         ksft_finished();
106 }
107 

~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~

kernel.org | git.kernel.org | LWN.net | Project Home | SVN repository | Mail admin

Linux® is a registered trademark of Linus Torvalds in the United States and other countries.
TOMOYO® is a registered trademark of NTT DATA CORPORATION.

sflogo.php