xref: /freebsd/tools/test/stress2/misc/mmap23.sh (revision 3e5d0784b9b5296bda801add034b057ad68237f7)
1#!/bin/sh
2
3#
4# Copyright (c) 2015 EMC Corp.
5# All rights reserved.
6#
7# Redistribution and use in source and binary forms, with or without
8# modification, are permitted provided that the following conditions
9# are met:
10# 1. Redistributions of source code must retain the above copyright
11#    notice, this list of conditions and the following disclaimer.
12# 2. Redistributions in binary form must reproduce the above copyright
13#    notice, this list of conditions and the following disclaimer in the
14#    documentation and/or other materials provided with the distribution.
15#
16# THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19# ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22# OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25# OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26# SUCH DAMAGE.
27#
28
29# "panic: vm_object_unwire: missing page" seen.
30# https://people.freebsd.org/~pho/stress/log/mmap23.txt
31# Test scenario by kib@.
32# Fixed by r285878.
33
34. ../default.cfg
35
36dir=/tmp
37odir=`pwd`
38cd $dir
39sed '1,/^EOF/d' < $odir/$0 > $dir/mmap23.c
40mycc -o mmap23 -Wall -Wextra mmap23.c || exit 1
41rm -f mmap23.c
42cd $odir
43
44cp /tmp/mmap23 /tmp/mmap23.inputfile
45daemon sh -c '(cd ../testcases/swap; ./swap -t 1m -i 2)' > \
46    /dev/null 2>&1
47
48/tmp/mmap23 /tmp/mmap23.inputfile &
49sleep .2
50dd if=/dev/zero of=/tmp/mmap23.inputfile bs=1k count=1 status=none
51
52while pgrep -q swap; do
53	pkill -9 swap
54done
55
56rm -f /tmp/mmap23 /tmp/mmap23.inputfile
57exit
58
59EOF
60#include <sys/fcntl.h>
61#include <sys/mman.h>
62#include <sys/param.h>
63#include <sys/stat.h>
64#include <sys/types.h>
65#include <sys/wait.h>
66
67#include <err.h>
68#include <errno.h>
69#include <stdlib.h>
70#include <unistd.h>
71
72const char *file;
73
74void
75test(void)
76{
77	struct stat st;
78	char *p;
79	size_t len;
80	int error, fd;
81
82	if ((fd = open(file, O_RDWR)) == -1)
83		err(1, "open %s", file);
84	if ((error = fstat(fd, &st)) == -1)
85		err(1, "stat(%s)", file);
86	len = round_page(st.st_size);
87	if ((p = mmap(NULL, len, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0)) == MAP_FAILED)
88		err(1, "mmap");
89	p[arc4random() % len] = 1;
90	if ((error = mlock(p, len)) == -1)
91		err(1, "mlock");
92	sleep(2);
93	if (munmap(p, len) == -1)
94		err(1, "unmap()");
95	close(fd);
96}
97
98int
99main(int argc, char *argv[])
100{
101	if (argc != 2)
102		errx(1, "Usage: %s <file>", argv[0]);
103	file = argv[1];
104
105	test();
106
107	return (0);
108}
109