xref: /freebsd/tools/test/stress2/misc/mmap22.sh (revision 61e21613)
1#!/bin/sh
2
3#
4# Copyright (c) 2014 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# Core dump wedge
30# Trimmed down version of mmap15.sh
31# http://people.freebsd.org/~pho/stress/log/mmap15-2.txt
32# Fixed in r272534 + r272535.
33
34# Page fault seen: http://people.freebsd.org/~pho/stress/log/kostik733.txt
35# Fixed in r274474.
36
37[ `id -u ` -ne 0 ] && echo "Must be root!" && exit 1
38
39. ../default.cfg
40
41here=`pwd`
42cd /tmp
43sed '1,/^EOF/d' < $here/$0 > mmap22.c
44mycc -o mmap22 -Wall -Wextra -O0 -g mmap22.c -lpthread || exit 1
45rm -f mmap22.c
46
47su $testuser -c /tmp/mmap22 &
48
49sleep 300
50while pgrep -q mmap22; do
51	pkill -9 mmap22
52        sleep 2
53done
54wait
55
56rm -f /tmp/mmap22 /tmp/mmap22.core
57exit 0
58EOF
59#include <sys/mman.h>
60#include <sys/param.h>
61#include <sys/stat.h>
62#include <sys/wait.h>
63
64#include <err.h>
65#include <errno.h>
66#include <fcntl.h>
67#include <pthread.h>
68#include <signal.h>
69#include <stdio.h>
70#include <stdlib.h>
71#include <unistd.h>
72
73#define MMAPS 25
74#define PARALLEL 4
75#define SIZ (64 * 1024 * 1024)
76
77void *
78tmmap(void *arg __unused)
79{
80	size_t len;
81	void *p __unused;
82	int i;
83
84	len = SIZ;
85	for (i = 0; i < MMAPS; i++)
86		p = mmap(NULL, len, PROT_READ | PROT_WRITE, MAP_ANON, -1, 0);
87	fsync(fileno(stdout));
88
89	return (NULL);
90}
91
92void
93test(void)
94{
95	pthread_t tid;
96	int i, rc;
97
98	if ((rc = pthread_create(&tid, NULL, tmmap, NULL)) != 0)
99		errc(1, rc, "test()");
100
101	for (i = 0; i < 100; i++) {
102		if (fork() == 0) {
103			usleep(10000);
104			_exit(0);
105		}
106		wait(NULL);
107	}
108
109	raise(SIGSEGV);
110
111	if ((rc = pthread_join(tid, NULL)) != 0)
112		errc(1, rc, "pthread_join(%d)", i);
113	_exit(0);
114}
115
116int
117main(void)
118{
119	int i;
120
121	for (i = 0; i < PARALLEL; i++) {
122		if (fork() == 0)
123			test();
124	}
125
126	for (i = 0; i < PARALLEL; i++)
127		wait(NULL);
128
129	return (0);
130}
131