xref: /freebsd/tools/test/stress2/misc/mmap26.sh (revision ebacd8013fe5f7fdf9f6a5b286f6680dd2891036)
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# https://people.freebsd.org/~pho/stress/log/kostik820.txt
31# Fixed by r285878.
32
33# Variation of mmap25.sh:
34# Access one byte past the end of the file, which is not wrong, but
35# out of specified behaviour.
36
37. ../default.cfg
38
39dir=/tmp
40odir=`pwd`
41cd $dir
42sed '1,/^EOF/d' < $odir/$0 > $dir/mmap26.c
43mycc -o mmap26 -Wall -Wextra mmap26.c || exit 1
44rm -f mmap26.c
45cd $odir
46
47cp /tmp/mmap26 /tmp/mmap26.inputfile
48daemon sh -c '(cd ../testcases/swap; ./swap -t 1m -i 2)' > \
49    /dev/null 2>&1
50sleep 1
51
52(cd /tmp; /tmp/mmap26 /tmp/mmap26.inputfile)
53
54while pkill -9 swap; do :; done
55rm -f /tmp/mmap26 /tmp/mmap26.inputfile /tmp/mmap26.core
56exit 0
57
58EOF
59#include <sys/fcntl.h>
60#include <sys/mman.h>
61#include <sys/param.h>
62#include <sys/stat.h>
63#include <sys/types.h>
64#include <sys/wait.h>
65
66#include <err.h>
67#include <errno.h>
68#include <stdio.h>
69#include <stdlib.h>
70#include <unistd.h>
71
72const char *file;
73volatile char c;
74
75void
76test(void)
77{
78	struct stat st;
79	char *p;
80	size_t len;
81	int error, fd;
82
83	if ((fd = open(file, O_RDWR)) == -1)
84		err(1, "open %s", file);
85	if ((error = fstat(fd, &st)) == -1)
86		err(1, "stat(%s)", file);
87	len = round_page(st.st_size);
88	if ((p = mmap(NULL, len, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0))
89	    == MAP_FAILED)
90		err(1, "mmap");
91	if ((error = mlock(p, len)) == -1)
92		err(1, "mlock");
93
94	if (ftruncate(fd, (off_t)0) == -1)
95		err(1, "ftruncate 1");
96	if (ftruncate(fd, len) == -1)
97		err(1, "ftruncate 2");
98
99	p[len - 1] = 1;
100
101	/* one byte past EOF */
102	if (round_page((unsigned long)&p[len]) ==
103	    round_page((unsigned long)&p[len - 1])) {
104		fprintf(stderr, "Expect: Segmentation fault (core dumped)\n");
105		c = p[len];
106	}
107
108	if (munmap(p, len) == -1)
109		err(1, "unmap()");
110	close(fd);
111}
112
113int
114main(int argc, char *argv[])
115{
116	if (argc != 2)
117		errx(1, "Usage: %s <file>", argv[0]);
118	file = argv[1];
119
120	test();
121
122	return (0);
123}
124