xref: /freebsd/tools/test/stress2/misc/mmap25.sh (revision 8a272653d9fbd9fc37691c9aad6a05089b4ecb4d)
1*8a272653SPeter Holm#!/bin/sh
2*8a272653SPeter Holm
3*8a272653SPeter Holm#
4*8a272653SPeter Holm# Copyright (c) 2015 EMC Corp.
5*8a272653SPeter Holm# All rights reserved.
6*8a272653SPeter Holm#
7*8a272653SPeter Holm# Redistribution and use in source and binary forms, with or without
8*8a272653SPeter Holm# modification, are permitted provided that the following conditions
9*8a272653SPeter Holm# are met:
10*8a272653SPeter Holm# 1. Redistributions of source code must retain the above copyright
11*8a272653SPeter Holm#    notice, this list of conditions and the following disclaimer.
12*8a272653SPeter Holm# 2. Redistributions in binary form must reproduce the above copyright
13*8a272653SPeter Holm#    notice, this list of conditions and the following disclaimer in the
14*8a272653SPeter Holm#    documentation and/or other materials provided with the distribution.
15*8a272653SPeter Holm#
16*8a272653SPeter Holm# THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17*8a272653SPeter Holm# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18*8a272653SPeter Holm# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19*8a272653SPeter Holm# ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20*8a272653SPeter Holm# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21*8a272653SPeter Holm# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22*8a272653SPeter Holm# OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23*8a272653SPeter Holm# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24*8a272653SPeter Holm# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25*8a272653SPeter Holm# OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26*8a272653SPeter Holm# SUCH DAMAGE.
27*8a272653SPeter Holm#
28*8a272653SPeter Holm
29*8a272653SPeter Holm# "panic: vm_page_unwire: page 0xc36cce48's wire count is zero" seen.
30*8a272653SPeter Holm# Fixed by r285878.
31*8a272653SPeter Holm
32*8a272653SPeter Holm# Test scenario by kib@:
33*8a272653SPeter Holm# 1. We mapped and mlocked the file.
34*8a272653SPeter Holm# 2. The file is truncated
35*8a272653SPeter Holm# 3. The file is extended again to cover the whole mapped area.
36*8a272653SPeter Holm# 4. The program accesses the mapping past the point of truncation.
37*8a272653SPeter Holm
38*8a272653SPeter Holm. ../default.cfg
39*8a272653SPeter Holm
40*8a272653SPeter Holmdir=/tmp
41*8a272653SPeter Holmodir=`pwd`
42*8a272653SPeter Holmcd $dir
43*8a272653SPeter Holmsed '1,/^EOF/d' < $odir/$0 > $dir/mmap25.c
44*8a272653SPeter Holmmycc -o mmap25 -Wall -Wextra mmap25.c || exit 1
45*8a272653SPeter Holmrm -f mmap25.c
46*8a272653SPeter Holmcd $odir
47*8a272653SPeter Holm
48*8a272653SPeter Holmcp /tmp/mmap25 /tmp/mmap25.inputfile
49*8a272653SPeter Holmdaemon sh -c '(cd ../testcases/swap; ./swap -t 1m -i 2)' > \
50*8a272653SPeter Holm    /dev/null 2>&1
51*8a272653SPeter Holmsleep 1
52*8a272653SPeter Holm
53*8a272653SPeter Holm/tmp/mmap25 /tmp/mmap25.inputfile
54*8a272653SPeter Holm
55*8a272653SPeter Holmwhile pgrep -q swap; do
56*8a272653SPeter Holm	pkill -9 swap
57*8a272653SPeter Holmdone
58*8a272653SPeter Holmrm -f /tmp/mmap25 /tmp/mmap25.inputfile mmap25.core
59*8a272653SPeter Holmexit
60*8a272653SPeter Holm
61*8a272653SPeter HolmEOF
62*8a272653SPeter Holm#include <sys/fcntl.h>
63*8a272653SPeter Holm#include <sys/mman.h>
64*8a272653SPeter Holm#include <sys/param.h>
65*8a272653SPeter Holm#include <sys/stat.h>
66*8a272653SPeter Holm#include <sys/types.h>
67*8a272653SPeter Holm#include <sys/wait.h>
68*8a272653SPeter Holm
69*8a272653SPeter Holm#include <err.h>
70*8a272653SPeter Holm#include <errno.h>
71*8a272653SPeter Holm#include <stdlib.h>
72*8a272653SPeter Holm#include <unistd.h>
73*8a272653SPeter Holm
74*8a272653SPeter Holmconst char *file;
75*8a272653SPeter Holmvolatile char c;
76*8a272653SPeter Holm
77*8a272653SPeter Holmvoid
78*8a272653SPeter Holmtest(void)
79*8a272653SPeter Holm{
80*8a272653SPeter Holm	struct stat st;
81*8a272653SPeter Holm	char *p;
82*8a272653SPeter Holm	size_t len;
83*8a272653SPeter Holm	off_t pos;
84*8a272653SPeter Holm	int error, fd;
85*8a272653SPeter Holm
86*8a272653SPeter Holm	if ((fd = open(file, O_RDWR)) == -1)
87*8a272653SPeter Holm		err(1, "open %s", file);
88*8a272653SPeter Holm	if ((error = fstat(fd, &st)) == -1)
89*8a272653SPeter Holm		err(1, "stat(%s)", file);
90*8a272653SPeter Holm	len = round_page(st.st_size);
91*8a272653SPeter Holm	pos = len - getpagesize();
92*8a272653SPeter Holm	if ((p = mmap(NULL, len, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0))
93*8a272653SPeter Holm	    == MAP_FAILED)
94*8a272653SPeter Holm		err(1, "mmap");
95*8a272653SPeter Holm	if ((error = mlock(p, len)) == -1)
96*8a272653SPeter Holm		err(1, "mlock");
97*8a272653SPeter Holm
98*8a272653SPeter Holm	if (ftruncate(fd, pos) == -1)
99*8a272653SPeter Holm		err(1, "ftruncate 1");
100*8a272653SPeter Holm	if (ftruncate(fd, len) == -1)
101*8a272653SPeter Holm		err(1, "ftruncate 2");
102*8a272653SPeter Holm
103*8a272653SPeter Holm	c = p[pos];	/* Program received signal SIGSEGV, Segmentation fault. */
104*8a272653SPeter Holm
105*8a272653SPeter Holm	if (munmap(p, len) == -1)
106*8a272653SPeter Holm		err(1, "unmap()");
107*8a272653SPeter Holm	close(fd);
108*8a272653SPeter Holm}
109*8a272653SPeter Holm
110*8a272653SPeter Holmint
111*8a272653SPeter Holmmain(int argc, char *argv[])
112*8a272653SPeter Holm{
113*8a272653SPeter Holm	if (argc != 2)
114*8a272653SPeter Holm		errx(1, "Usage: %s <file>", argv[0]);
115*8a272653SPeter Holm	file = argv[1];
116*8a272653SPeter Holm
117*8a272653SPeter Holm	test();
118*8a272653SPeter Holm
119*8a272653SPeter Holm	return (0);
120*8a272653SPeter Holm}
121