Linux Audio

Check our new training course

Loading...
Note: File does not exist in v3.15.
  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
 26static 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
 58int 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}