xref: /freebsd/tools/test/stress2/misc/mmap39.sh (revision c7a063741720ef81d4caa4613242579d12f1d605)
1#!/bin/sh
2
3#
4# SPDX-License-Identifier: BSD-2-Clause-FreeBSD
5#
6# Copyright (c) 2020 Peter Holm <pho@FreeBSD.org>
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# Attempt to reproduce
31# "panic: pmap_release: pmap 0xfffffe012fb61b08 resident count 1 != 0"
32# No problems seen.
33
34# Test scenario suggestion by kib@
35
36. ../default.cfg
37[ `id -u` -ne 0 ] && echo "Must be root!" && exit 1
38[ `uname -p` != "amd64" ] && exit 0
39
40dir=/tmp
41odir=`pwd`
42cd $dir
43sed '1,/^EOF/d' < $odir/$0 > $dir/mmap39.c
44mycc -o mmap39 -Wall -Wextra -O0 -g mmap39.c -static || exit 1
45rm -f mmap39.c
46cd $odir
47
48set -e
49mount | grep "on $mntpoint " | grep -q /dev/md && umount -f $mntpoint
50[ -c /dev/md$mdstart ] &&  mdconfig -d -u $mdstart
51mdconfig -a -t swap -s 2g -u $mdstart
52newfs $newfs_flags md$mdstart > /dev/null
53mount /dev/md$mdstart $mntpoint
54set +e
55
56for i in `jot 128`; do
57	proccontrol -m aslr -s disable $dir/mmap39 &
58	pids="$pids $!"
59done
60s=0
61for pid in $pids; do
62	wait $pid
63	r=$?
64	[ $s -ne 0 ] && s=$r
65done
66
67[ -f mmap39.core -a $s -eq 0 ] &&
68    { ls -l mmap39.core; mv mmap39.core $dir; s=1; }
69cd $odir
70
71for i in `jot 6`; do
72	mount | grep -q "on $mntpoint " || break
73	umount $mntpoint && break || sleep 10
74	[ $i -eq 6 ] &&
75	    { echo FATAL; fstat -mf $mntpoint; exit 1; }
76done
77mdconfig -d -u $mdstart
78rm -rf $dir/mmap39
79exit $s
80
81EOF
82#include <sys/param.h>
83#include <sys/mman.h>
84#include <sys/wait.h>
85
86#include <err.h>
87#include <stdio.h>
88#include <time.h>
89#include <unistd.h>
90
91void
92test(void)
93{
94	size_t i, len;
95	void *p;
96
97	p = (void *)0x8000000000; /* 512G */
98	len = 0x200000;		  /*   2M */
99	if ((p = mmap(p, len, PROT_READ | PROT_WRITE, MAP_ANON, -1, 0)) ==
100	    MAP_FAILED)
101		err(1, "mmap");
102
103	if (p != (void *)0x8000000000)
104		errx(1, "mmap address fail");
105
106	for (i = 0; i < len; i += PAGE_SIZE)
107		*(char *)(p + i) = 1;
108
109	if (munmap(p, len) == -1)
110		err(1, "munmap()");
111
112	_exit(0);
113}
114
115int
116main(void)
117{
118	time_t start;
119	pid_t pid;
120
121	sleep(5);
122	start = time(NULL);
123	while (time(NULL) - start < 120) {
124		if ((pid = fork()) == 0)
125			test();
126		if (waitpid(pid, NULL, 0) != pid)
127			err(1, "waitpid");
128	}
129
130	return (0);
131}
132