4 # SPDX-License-Identifier: BSD-2-Clause-FreeBSD
6 # Copyright (c) 2019 Dell EMC Isilon
8 # Redistribution and use in source and binary forms, with or without
9 # modification, are permitted provided that the following conditions
11 # 1. Redistributions of source code must retain the above copyright
12 # notice, this list of conditions and the following disclaimer.
13 # 2. Redistributions in binary form must reproduce the above copyright
14 # notice, this list of conditions and the following disclaimer in the
15 # documentation and/or other materials provided with the distribution.
17 # THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
18 # ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 # IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 # ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
21 # FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 # DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 # OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 # HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 # LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 # OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30 # "panic: caller failed to provide space -546873344 at address 0x20a00000"
32 # https://people.freebsd.org/~pho/stress/log/indir_trunc.txt
35 # Test scenario inspired by:
36 # syzbot+6532e9aab8911f58beeb@syzkaller.appspotmail.com
39 [ `id -u` -ne 0 ] && echo "Must be root!" && exit 1
44 sed '1,/^EOF/d' < $odir/$0 > $dir/mmap34.c
45 mycc -o mmap34 -Wall -Wextra -O0 -g mmap34.c -lpthread || exit 1
49 mount | grep "on $mntpoint " | grep -q /dev/md && umount -f $mntpoint
50 [ -c /dev/md$mdstart ] && mdconfig -d -u $mdstart
51 mdconfig -a -t swap -s 4g -u $mdstart
52 newfs $newfs_flags -n md$mdstart > /dev/null
53 mount /dev/md$mdstart $mntpoint
56 (cd ../testcases/swap; ./swap -t 5m -i 20) &
60 while pkill swap; do :; done
63 { ls -l mmap34.core; mv mmap34.core $dir; s=1; }
67 mount | grep -q "on $mntpoint " || break
68 umount $mntpoint && break || sleep 10
70 { echo FATAL; fstat -mf $mntpoint; exit 1; }
72 mdconfig -d -u $mdstart
74 [ $s -eq 0 ] && rm -f /tmp/mmap34.c
78 #include <sys/param.h>
83 #include <machine/atomic.h>
94 static volatile u_int *share;
97 #define MASK 0x7ffffffffffffULL
99 #define MASK 0xffffffffULL
102 #define RUNTIME (5 * 60)
108 t1(void *data __unused)
114 while (time(NULL) - start < 30) {
115 offset = arc4random();
116 offset = (offset << 32) | arc4random();
118 if (lseek(fd, offset, SEEK_SET) == -1)
119 err(1, "lseek(%jd)", offset);
120 if (write(fd, "a", 1) != 1)
129 t2(void *data __unused)
138 while (time(NULL) - start < 30) {
141 offset = arc4random();
142 offset = (offset << 32) | arc4random();
144 if (ftruncate(fd, offset) == -1)
145 err(1, "ftruncate(%jd)", offset);
147 p = mmap(NULL, offset, PROT_READ | PROT_WRITE, MAP_SHARED,
170 atomic_add_int(&share[SYNC], 1);
171 while (share[SYNC] != PARALLEL)
174 snprintf(file, sizeof(file), "file.%d", getpid());
175 if ((fd = open(file, O_RDWR | O_CREAT, DEFFILEMODE)) == -1)
176 err(1, "open(%s)", file);
178 if ((r = pthread_create(&tid[0], NULL, t1, NULL)) != 0)
179 errc(1, r, "pthread_create");
180 if ((r = pthread_create(&tid[1], NULL, t2, NULL)) != 0)
181 errc(1, r, "pthread_create");
183 if ((r = pthread_join(tid[0], NULL)) != 0)
184 errc(1, r, "pthread_join");
185 if ((r = pthread_join(tid[1], NULL)) != 0)
186 errc(1, r, "pthread_join");
196 pid_t pids[PARALLEL];
203 if ((share = mmap(NULL, len, PROT_READ | PROT_WRITE,
204 MAP_ANON | MAP_SHARED, -1, 0)) == MAP_FAILED)
208 while ((time(NULL) - start) < RUNTIME && e == 0) {
210 for (i = 0; i < PARALLEL; i++) {
211 if ((pids[i] = fork()) == 0)
216 for (i = 0; i < PARALLEL; i++) {
217 if (waitpid(pids[i], &status, 0) == -1)
218 err(1, "waitpid(%d)", pids[i]);
220 if (WIFSIGNALED(status))
222 "pid %d exit signal %d\n",
223 pids[i], WTERMSIG(status));
225 e += status == 0 ? 0 : 1;