xref: /freebsd/tools/test/stress2/misc/vm_map.sh (revision 4d846d260e2b9a3d4d0a701462568268cbfe7a5b)
1#!/bin/sh
2
3#
4# SPDX-License-Identifier: BSD-2-Clause
5#
6# Copyright (c) 2019 Dell EMC Isilon
7#
8# Redistribution and use in source and binary forms, with or without
9# modification, are permitted provided that the following conditions
10# are met:
11# 1. Redistributions of source code must retain the above copyright
12#    notice, this list of conditions and the following disclaimer.
13# 2. Redistributions in binary form must reproduce the above copyright
14#    notice, this list of conditions and the following disclaimer in the
15#    documentation and/or other materials provided with the distribution.
16#
17# THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
18# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20# ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
21# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23# OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26# OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27# SUCH DAMAGE.
28#
29
30# Test scenario based on alc@'s suggestion for D19826
31# No problems seen.
32
33[ `id -u ` -ne 0 ] && echo "Must be root!" && exit 1
34
35. ../default.cfg
36
37dir=/tmp
38odir=`pwd`
39cd $dir
40sed '1,/^EOF/d' < $odir/$0 > $dir/vm_map.c
41mycc -o vm_map -Wall -Wextra -O2 vm_map.c || exit 1
42rm -f vm_map.c
43
44pages=$((`sysctl -n hw.usermem` / `sysctl -n hw.pagesize`))
45[ `sysctl -n vm.swap_total` -eq 0 ] &&
46    pages=$((pages / 10 * 8))
47proccontrol -m aslr -s disable /tmp/vm_map $pages
48
49rm -f /tmp/vm_map
50exit $s
51EOF
52#include <sys/param.h>
53#include <sys/mman.h>
54#include <sys/time.h>
55#include <sys/wait.h>
56
57#include <err.h>
58#include <stdio.h>
59#include <stdlib.h>
60#include <unistd.h>
61
62static long n;
63
64#define RUNTIME (2 * 60)
65#define INCARNATIONS 32
66
67int
68test(void)
69{
70	size_t len;
71	time_t start;
72	void *a;
73	char **c;
74	int i;
75
76	a = NULL;
77	c = calloc(n, sizeof(char *));
78	len = PAGE_SIZE;
79        start = time(NULL);
80        while ((time(NULL) - start) < RUNTIME) {
81		for (i = 0; i < n; i++) {
82			if ((a = mmap((void *)a, len, PROT_READ | PROT_WRITE,
83			    MAP_ANON | MAP_SHARED, -1, 0)) == MAP_FAILED)
84				err(1, "mmap");
85			c[i] = a;
86			*c[i] = 1;
87			a += PAGE_SIZE;
88		}
89		for (i = 0; i < n; i++) {
90			if (munmap(c[i], PAGE_SIZE) == -1)
91				err(1, "munmap(%p)", c[i]);
92		}
93		a = NULL;
94	}
95
96	_exit(0);
97}
98
99int
100main(int argc, char *argv[])
101{
102	int i;
103
104	if (argc != 2) {
105		fprintf(stderr, "Usage %s <pages>\n", argv[0]);
106		exit(1);
107	}
108	n = atol(argv[1]) / INCARNATIONS;
109	for (i = 0; i < INCARNATIONS; i++)
110		if (fork() == 0)
111			test();
112
113	for (i = 0; i < INCARNATIONS; i++)
114		wait(NULL);
115
116	return (0);
117}
118