xref: /freebsd/tools/test/stress2/misc/mmap15.sh (revision 2e3507c25e42292b45a5482e116d278f5515d04d)
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# Missing wakeup in the bufobj_wwait().
30
31# Snapshot of WiP work.
32# http://people.freebsd.org/~pho/stress/log/mmap15.txt
33# Not fixed
34
35# panic: invalid size
36# http://people.freebsd.org/~pho/stress/log/kostik738.txt
37# Fixed in r274878
38
39[ `id -u ` -ne 0 ] && echo "Must be root!" && exit 1
40
41. ../default.cfg
42
43here=`pwd`
44cd /tmp
45sed '1,/^EOF/d' < $here/$0 > mmap15.c
46mycc -o mmap15 -Wall -Wextra -O2 -g mmap15.c -lpthread || exit 1
47rm -f mmap15.c
48
49for i in `jot 2`; do
50	su $testuser -c /tmp/mmap15 &
51done
52sleep 300
53while pgrep -q mmap15; do
54	pkill -9 mmap15
55	sleep 2
56done
57wait
58
59rm -f /tmp/mmap15 /tmp/mmap15.core
60exit 0
61EOF
62#include <sys/types.h>
63#include <sys/mman.h>
64#include <sys/stat.h>
65#include <sys/time.h>
66#include <sys/wait.h>
67
68#include <err.h>
69#include <errno.h>
70#include <fcntl.h>
71#include <pthread.h>
72#include <pthread_np.h>
73#include <signal.h>
74#include <stdio.h>
75#include <stdlib.h>
76#include <string.h>
77#include <time.h>
78#include <unistd.h>
79
80#define LOOPS 2
81#define MMSIZE (256 * 1024)
82#define N (128 * 1024 / (int)sizeof(u_int32_t))
83#define PARALLEL 50
84
85void *p;
86u_int32_t r[N];
87
88unsigned long
89makearg(void)
90{
91	unsigned long val;
92	unsigned int i;
93
94	val = arc4random();
95	i   = arc4random() % 100;
96	if (i < 20)
97		val = val & 0xff;
98	if (i >= 20 && i < 40)
99		val = val & 0xffff;
100	if (i >= 40 && i < 60)
101		val = (unsigned long)(r) | (val & 0xffff);
102#if defined(__LP64__)
103	if (i >= 60) {
104		val = (val << 32) | arc4random();
105		if (i > 80)
106			val = val & 0x00007fffffffffffUL;
107	}
108#endif
109
110	return(val);
111}
112
113void *
114makeptr(void)
115{
116	unsigned long val;
117
118	if (p != MAP_FAILED && p != NULL)
119		val = (unsigned long)p + arc4random();
120	else
121		val = makearg();
122	val = trunc_page(val);
123
124	return ((void *)val);
125}
126
127void *
128tmmap(void *arg __unused)
129{
130	size_t len;
131	int i, j, fd;
132
133	pthread_set_name_np(pthread_self(), __func__);
134	len = MMSIZE;
135
136	if ((fd = open("/dev/zero", O_RDWR)) == -1)
137		err(1,"open()");
138	for (i = 0, j = 0; i < 100; i++) {
139		p = mmap(NULL, len, PROT_READ | PROT_WRITE, MAP_PRIVATE, fd, 0);
140		if (p != MAP_FAILED)
141			j++;
142		p = mmap(NULL, len, PROT_READ | PROT_WRITE, MAP_ANON, -1, 0);
143		if (p != MAP_FAILED)
144			j++;
145	}
146	if (j == 0)
147		fprintf(stderr, "FAIL: all mmap(2) calls failed.\n");
148	close(fd);
149
150	return (NULL);
151}
152
153void *
154tmlock(void *arg __unused)
155{
156	size_t len;
157	int i, n;
158
159	pthread_set_name_np(pthread_self(), __func__);
160	n = 0;
161	for (i = 0; i < 200; i++) {
162		len = trunc_page(makearg());
163		if (mlock(makeptr(), len) == 0)
164			n++;
165		len = trunc_page(makearg());
166		if (arc4random() % 100 < 50)
167			if (munlock(makeptr(), len) == 0)
168				n++;
169	}
170	if (n < 10)
171		fprintf(stderr, "Note: tmlock() only succeeded %d times.\n",
172		    n);
173
174	return (NULL);
175}
176
177void
178test(void)
179{
180	pthread_t tid[2];
181	int i, rc;
182
183	if ((rc = pthread_create(&tid[0], NULL, tmmap, NULL)) != 0)
184		errc(1, rc, "tmmap()");
185	if ((rc = pthread_create(&tid[1], NULL, tmlock, NULL)) != 0)
186		errc(1, rc, "tmlock()");
187
188	for (i = 0; i < 100; i++) {
189		if (fork() == 0) {
190			usleep(10000);
191			_exit(0);
192		}
193		wait(NULL);
194	}
195
196	raise(SIGSEGV);
197
198	for (i = 0; i < 2; i++)
199		if ((rc = pthread_join(tid[i], NULL)) != 0)
200			errc(1, rc, "pthread_join(%d)", i);
201	_exit(0);
202}
203
204int
205main(void)
206{
207	int i, j;
208
209	for (i = 0; i < N; i++)
210		r[i] = arc4random();
211
212	for (i = 0; i < LOOPS; i++) {
213		for (j = 0; j < PARALLEL; j++) {
214			if (fork() == 0)
215				test();
216		}
217
218		for (j = 0; j < PARALLEL; j++)
219			wait(NULL);
220	}
221
222	return (0);
223}
224