xref: /freebsd/tools/test/stress2/misc/mmap21.sh (revision 8a272653d9fbd9fc37691c9aad6a05089b4ecb4d)
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# panic: vm_reserv_populate: reserv 0xfffff807cbd3c400 is already promoted
30# http://people.freebsd.org/~pho/stress/log/mmap21.txt
31# Fixed by r280238
32
33[ `id -u ` -ne 0 ] && echo "Must be root!" && exit 1
34
35. ../default.cfg
36
37here=`pwd`
38cd /tmp
39sed '1,/^EOF/d' < $here/$0 > mmap21.c
40mycc -o mmap21 -Wall -Wextra -O2 -g mmap21.c -lpthread || exit 1
41rm -f mmap21.c
42
43su $testuser -c /tmp/mmap21
44
45rm -f /tmp/mmap21 /tmp/mmap21.core
46exit 0
47EOF
48#include <sys/types.h>
49#include <sys/mman.h>
50#include <sys/stat.h>
51#include <sys/time.h>
52#include <sys/wait.h>
53
54#include <err.h>
55#include <fcntl.h>
56#include <pthread.h>
57#include <pthread_np.h>
58#include <signal.h>
59#include <stdio.h>
60#include <stdlib.h>
61#include <unistd.h>
62
63#define LOOPS 1
64#define NMAPS 50
65#define PARALLEL 2
66
67void *p;
68
69static void *
70tmmap(void *arg __unused)
71{
72	size_t len;
73	int i;
74
75	pthread_set_name_np(pthread_self(), __func__);
76	len = 1LL * 128 * 1024 * 1024;
77
78	for (i = 0; i < NMAPS; i++)
79		p = mmap(NULL, len, PROT_READ | PROT_WRITE, MAP_ANON, -1, 0);
80
81	return (NULL);
82}
83
84static void *
85tmlock(void *arg __unused)
86{
87	size_t len;
88	int i, n;
89
90	pthread_set_name_np(pthread_self(), __func__);
91	n = 0;
92	for (i = 0; i < 200; i++) {
93		len = trunc_page(arc4random());
94		if (mlock(p, len) == 0)
95			n++;
96		len = trunc_page(arc4random());
97		if (arc4random() % 100 < 50)
98			if (munlock(p, len) == 0)
99				n++;
100	}
101	if (n < 10)
102		fprintf(stderr, "Note: tmlock() only succeeded %d times.\n",
103		    n);
104
105	return (NULL);
106}
107
108static void
109test(void)
110{
111	pid_t pid;
112	pthread_t tid[2];
113	int i, rc;
114
115	if ((rc = pthread_create(&tid[0], NULL, tmmap, NULL)) != 0)
116		errc(1, rc, "tmmap()");
117	if ((rc = pthread_create(&tid[1], NULL, tmlock, NULL)) != 0)
118		errc(1, rc, "tmlock()");
119
120	for (i = 0; i < 100; i++) {
121		if ((pid = fork()) == 0) {
122			usleep(10000);
123			_exit(0);
124		}
125		if (waitpid(pid, NULL, 0) != pid)
126			err(1, "waitpid(%d)", pid);
127	}
128
129	raise(SIGSEGV);
130
131	for (i = 0; i < 2; i++)
132		if ((rc = pthread_join(tid[i], NULL)) != 0)
133			errc(1, rc, "pthread_join(%d)", i);
134	_exit(0);
135}
136
137int
138main(void)
139{
140
141	pid_t pids[PARALLEL];
142	time_t start;
143	int e, i, j, status;
144
145	start = time(NULL);
146	for (i = 0; i < LOOPS; i++) {
147		for (j = 0; j < PARALLEL; j++) {
148			if ((pids[j] = fork()) == 0)
149				test();
150		}
151
152		e = 0;
153		for (j = 0; j < PARALLEL; j++) {
154			if (waitpid(pids[j], &status, 0) == -1)
155				err(1, "waitpid(%d)", pids[j]);
156			e += status == 0 ? 0 : 1;
157		}
158		if (time(NULL) - start > 1200) {
159			fprintf(stderr, "Timed out.");
160			break;
161		}
162	}
163
164	return (e);
165}
166