aboutsummaryrefslogtreecommitdiff
path: root/tools/test/stress2/misc/mmap3.sh
blob: bc5905dd920edca838f0246c3f3921805fe8694f (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
#!/bin/sh

#
# Copyright (c) 2009 Peter Holm <pho@FreeBSD.org>
# All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions
# are met:
# 1. Redistributions of source code must retain the above copyright
#    notice, this list of conditions and the following disclaimer.
# 2. Redistributions in binary form must reproduce the above copyright
#    notice, this list of conditions and the following disclaimer in the
#    documentation and/or other materials provided with the distribution.
#
# THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
# ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
# OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
# OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
# SUCH DAMAGE.
#

# Variation of mmap2.sh with focus on random arguments for mprotect()
# https://people.freebsd.org/~pho/stress/log/kostik209.txt

. ../default.cfg

odir=`pwd`
cd /tmp
sed '1,/^EOF/d' < $odir/$0 > mmap3.c
mycc -o mmap3 -Wall mmap3.c -lpthread || exit 1
rm -f mmap3.c

start=`date '+%s'`
while [ `date '+%s'` -lt $((start + 5 * 60)) ]; do
	./mmap3
done
echo "Expect Segmentation faults"
start=`date '+%s'`
while [ `date '+%s'` -lt $((start + 5 * 60)) ]; do
	./mmap3 random
done
rm -f mmap3 mmap3.core /tmp/mmap.0*
exit

EOF
/*
   Stress mmap by having max 100 threads mapping random areas within
   a 100 Mb range.
 */
#include <sys/types.h>
#include <err.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <fcntl.h>
#include <pthread.h>
#include <sys/mman.h>
#include <sys/param.h>
#include <string.h>
#include <unistd.h>
#include <errno.h>

#define THREADS 100
#define STARTADDR 0x50000000U
#define ADRSPACE  0x06400000U /* 100 Mb */

static int ra;

void
trash(void *p)
{
	unsigned long v;

	mprotect(p, 0x570e3d38, 0x2c8fd54f);
	if (ra) {
		v = arc4random();
#if defined(__LP64__)
		v = v << 32 | arc4random();
#endif
		madvise((void *)v, arc4random(), arc4random());
		mprotect((void *)v, arc4random(), arc4random());
		msync((void *)v, arc4random(), arc4random());
	}

}

void
work(int nr)
{
	int fd, m;
	void *p;
	size_t len;
	char path[128];

	p = (void *)STARTADDR;
	len = ADRSPACE;

	sprintf(path, "/tmp/mmap.%06d.%04d", getpid(), nr);
	if ((fd = open(path, O_CREAT | O_TRUNC | O_RDWR, 0622)) == -1)
		err(1,"open()");
	if (ftruncate(fd, len) == -1)
		err(1, "ftruncate");
	if ((p = mmap(p, len, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0)) ==
			MAP_FAILED) {
		if (errno == ENOMEM)
			return;
		err(1, "mmap()");
	}
	if (unlink(path) == -1)
		err(1, "unlink(%s)", path);

	trash(p);

	m = arc4random() % 10;
	if (madvise(p, len, m) == -1)
		warn("madvise(%p, %zd, %d)", p, len, m);
	if (mprotect(p, trunc_page(arc4random() % len), PROT_READ) == -1 )
		err(1, "mprotect failed with error:");
	if (msync(p, 0, MS_SYNC) == -1)
		err(1, "msync(%p)", p);
	if (munmap(p, len) == -1)
		err(1, "munmap(%p)", p);
	close(fd);
}

void *
thr(void *arg)
{
	int i;

	for (i = 0; i < 512; i++) {
		work(*(int *)arg);
	}
	return (0);
}

int
main(int argc, char **argv)
{
	pthread_t threads[THREADS];
	int nr[THREADS];
	int i, n, r;

	n  = arc4random() % 14 + 5;
	ra = argc != 1;
//	printf("Address start 0x%x, address end 0x%x, pages %d, n %d\n",
//		STARTADDR, STARTADDR + ADRSPACE, ADRSPACE>>PAGE_SHIFT, n);
	for (i = 0; i < n; i++) {
		nr[i] = i;
		if ((r = pthread_create(&threads[i], NULL, thr, (void *)&nr[i])) != 0)
			errc(1, r, "pthread_create()");
	}

	for (i = 0; i < n; i++) {
		if ((r = pthread_join(threads[i], NULL)) != 0)
			errc(1, r, "pthread_join(%d)", i);
	}

	return (0);
}