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
31 # Test scenario inspired by:
32 # syzbot+6532e9aab8911f58beeb@syzkaller.appspotmail.com
35 [ `id -u` -ne 0 ] && echo "Must be root!" && exit 1
36 [ `uname -m` = "i386" ] && exit 0 # OOM killing
41 sed '1,/^EOF/d' < $odir/$0 > $dir/indir_trunc.c
42 mycc -o indir_trunc -Wall -Wextra -O0 -g indir_trunc.c -lpthread || exit 1
47 mount | grep "on $mntpoint " | grep -q /dev/md && umount -f $mntpoint
48 [ -c /dev/md$mdstart ] && mdconfig -d -u $mdstart
49 mdconfig -a -t swap -s 40g -u $mdstart
50 newfs $newfs_flags -n md$mdstart > /dev/null
51 mount /dev/md$mdstart $mntpoint
54 ###(cd ../testcases/swap; ./swap -t 5m -i 20) &
58 while pkill swap; do :; done
60 [ -f indir_trunc.core -a $s -eq 0 ] &&
61 { ls -l indir_trunc.core; mv indir_trunc.core $dir; s=1; }
65 mount | grep -q "on $mntpoint " || break
66 umount $mntpoint && break || sleep 10
68 { echo FATAL; fstat -mf $mntpoint; exit 1; }
70 mdconfig -d -u $mdstart
71 rm -rf $dir/indir_trunc
75 #include <sys/param.h>
80 #include <machine/atomic.h>
91 static volatile u_int *share;
94 #define MASK 0x7ffffffffffffULL
96 #define MASK 0xffffffff
99 #define RUNTIME (5 * 60)
105 t1(void *data __unused)
109 char *cmdline[] = { "/usr/bin/true", NULL };
112 while (time(NULL) - start < 60) {
113 offset = arc4random();
114 offset = (offset << 32) | arc4random();
116 if (lseek(fd, offset, SEEK_SET) == -1)
117 err(1, "lseek(%jd)", offset);
118 if (write(fd, "a", 1) != 1)
121 if (arc4random() % 1000 < 2) {
122 if (execve(cmdline[0], cmdline, NULL) == -1)
130 t2(void *data __unused)
139 while (time(NULL) - start < 60) {
142 offset = arc4random();
143 offset = (offset << 32) | arc4random();
145 if (ftruncate(fd, offset) == -1)
146 err(1, "ftruncate(%jd)", offset);
148 p = mmap(NULL, offset, PROT_READ | PROT_WRITE, MAP_SHARED,
171 atomic_add_int(&share[SYNC], 1);
172 while (share[SYNC] != PARALLEL)
175 snprintf(file, sizeof(file), "file.%d", getpid());
176 if ((fd = open(file, O_RDWR | O_CREAT, DEFFILEMODE)) == -1)
177 err(1, "open(%s)", file);
179 if ((r = pthread_create(&tid[0], NULL, t1, NULL)) != 0)
180 errc(1, r, "pthread_create");
181 if ((r = pthread_create(&tid[1], NULL, t2, NULL)) != 0)
182 errc(1, r, "pthread_create");
184 if ((r = pthread_join(tid[0], NULL)) != 0)
185 errc(1, r, "pthread_join");
186 if ((r = pthread_join(tid[1], NULL)) != 0)
187 errc(1, r, "pthread_join");
197 pid_t pids[PARALLEL];
204 if ((share = mmap(NULL, len, PROT_READ | PROT_WRITE,
205 MAP_ANON | MAP_SHARED, -1, 0)) == MAP_FAILED)
209 while ((time(NULL) - start) < RUNTIME && e == 0) {
211 for (i = 0; i < PARALLEL; i++) {
212 if ((pids[i] = fork()) == 0)
217 for (i = 0; i < PARALLEL; i++) {
218 if (waitpid(pids[i], &status, 0) == -1)
219 err(1, "waitpid(%d)", pids[i]);
221 if (WIFSIGNALED(status))
223 "pid %d exit signal %d\n",
224 pids[i], WTERMSIG(status));
226 e += status == 0 ? 0 : 1;