xref: /freebsd/tools/test/stress2/misc/mmap25.sh (revision 6be3386466ab79a84b48429ae66244f21526d3df)
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_page_unwire: page 0xc36cce48's wire count is zero" seen.
30# Fixed by r285878.
31
32# Test scenario by kib@:
33# 1. We mapped and mlocked the file.
34# 2. The file is truncated
35# 3. The file is extended again to cover the whole mapped area.
36# 4. The program accesses the mapping past the point of truncation.
37
38. ../default.cfg
39
40dir=/tmp
41odir=`pwd`
42cd $dir
43sed '1,/^EOF/d' < $odir/$0 > $dir/mmap25.c
44mycc -o mmap25 -Wall -Wextra mmap25.c || exit 1
45rm -f mmap25.c
46cd $odir
47
48cp /tmp/mmap25 /tmp/mmap25.inputfile
49daemon sh -c '(cd ../testcases/swap; ./swap -t 1m -i 2)' > \
50    /dev/null 2>&1
51sleep 1
52
53/tmp/mmap25 /tmp/mmap25.inputfile
54
55while pgrep -q swap; do
56	pkill -9 swap
57done
58rm -f /tmp/mmap25 /tmp/mmap25.inputfile mmap25.core
59exit
60
61EOF
62#include <sys/fcntl.h>
63#include <sys/mman.h>
64#include <sys/param.h>
65#include <sys/stat.h>
66#include <sys/types.h>
67#include <sys/wait.h>
68
69#include <err.h>
70#include <errno.h>
71#include <stdlib.h>
72#include <unistd.h>
73
74const char *file;
75volatile char c;
76
77void
78test(void)
79{
80	struct stat st;
81	char *p;
82	size_t len;
83	off_t pos;
84	int error, fd;
85
86	if ((fd = open(file, O_RDWR)) == -1)
87		err(1, "open %s", file);
88	if ((error = fstat(fd, &st)) == -1)
89		err(1, "stat(%s)", file);
90	len = round_page(st.st_size);
91	pos = len - getpagesize();
92	if ((p = mmap(NULL, len, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0))
93	    == MAP_FAILED)
94		err(1, "mmap");
95	if ((error = mlock(p, len)) == -1)
96		err(1, "mlock");
97
98	if (ftruncate(fd, pos) == -1)
99		err(1, "ftruncate 1");
100	if (ftruncate(fd, len) == -1)
101		err(1, "ftruncate 2");
102
103	c = p[pos];	/* Program received signal SIGSEGV, Segmentation fault. */
104
105	if (munmap(p, len) == -1)
106		err(1, "unmap()");
107	close(fd);
108}
109
110int
111main(int argc, char *argv[])
112{
113	if (argc != 2)
114		errx(1, "Usage: %s <file>", argv[0]);
115	file = argv[1];
116
117	test();
118
119	return (0);
120}
121