1#!/bin/sh 2 3# 4# SPDX-License-Identifier: BSD-2-Clause 5# 6# Copyright (c) 2019 Dell EMC Isilon 7# 8# Redistribution and use in source and binary forms, with or without 9# modification, are permitted provided that the following conditions 10# are met: 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. 16# 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 27# SUCH DAMAGE. 28# 29 30# Regression test for https://reviews.freebsd.org/D20784 31# "Fix mutual exclusion in pipe_direct_write()" 32# https://people.freebsd.org/~pho/stress/log/mkfifo8.txt 33 34# Reported by syzbot+21811cc0a89b2a87a9e7@syzkaller.appspotmail.com 35# Test scenario suggestion by markj@ 36# Fixed by r349546 37 38. ../default.cfg 39[ `id -u ` -ne 0 ] && echo "Must be root!" && exit 1 40 41dir=/tmp 42odir=`pwd` 43cd $dir 44sed '1,/^EOF/d' < $odir/$0 > $dir/mkfifo8.c 45mycc -o mkfifo8 -Wall -Wextra -O0 -g mkfifo8.c || exit 1 46rm -f mkfifo8.c 47cd $odir 48 49set -e 50mount | grep -q "on $mntpoint " && umount -f $mntpoint 51[ -c /dev/md$mdstart ] && mdconfig -d -u $mdstart 52mdconfig -a -t swap -s 1g -u $mdstart 53newfs $newfs_flags /dev/md$mdstart > /dev/null 54mount /dev/md$mdstart $mntpoint 55set +e 56 57cd $mntpoint 58$dir/mkfifo8; s=$? 59cd $odir 60 61while mount | grep -q "on $mntpoint "; do 62 umount $mntpoint || sleep 1 63done 64mdconfig -d -u $mdstart 65rm -rf /tmp/mkfifo8 66exit $s 67 68EOF 69#include <sys/param.h> 70#include <sys/mman.h> 71#include <sys/stat.h> 72#include <sys/wait.h> 73 74#include <machine/atomic.h> 75 76#include <err.h> 77#include <errno.h> 78#include <fcntl.h> 79#include <signal.h> 80#include <stdio.h> 81#include <stdlib.h> 82#include <time.h> 83#include <unistd.h> 84 85#define ACT 1 86#define NPROCS 64 87#define PARALLEL 4 88#define RUNTIME 60 89#define SIZ 8192 90#define SYNC 0 91 92static volatile u_int *share; 93static int fd; 94static char cp[SIZ]; 95 96static void 97tw(void) 98{ 99 int r; 100 101 atomic_add_int(&share[ACT], 1); 102 r = write(fd, cp, SIZ); 103 if (r == -1) 104 warn("write"); 105 106 _exit(0); 107} 108 109static void 110tr(void) 111{ 112 int i, r; 113 char cp[SIZ]; 114 115 while (share[ACT] < NPROCS / 2) 116 usleep(10); 117 for (i = 0; i < NPROCS; i++) { 118 r = read(fd, cp, SIZ); 119 if (r == -1) 120 warn("read"); 121 } 122 123 _exit(0); 124} 125 126static void 127test(void) 128{ 129 pid_t pid[NPROCS + 1]; 130 int i; 131 char file[80]; 132 133 atomic_add_int(&share[SYNC], 1); 134 while (share[SYNC] != PARALLEL) 135 ; 136 137 snprintf(file, sizeof(file), "fifo.%d", getpid()); 138 if (mkfifo(file, DEFFILEMODE) == -1) 139 err(1, "mkfifo(%s)", file); 140 if ((fd = open(file, O_RDWR)) == -1) 141 err(1, "open(%s)", file); 142 143 for (i = 0; i < NPROCS; i++) { 144 if ((pid[i] = fork()) == 0) 145 tw(); 146 } 147 if ((pid[NPROCS] = fork()) == 0) 148 tr(); 149 150 for (i = 0; i < NPROCS + 1; i++) { 151 if (waitpid(pid[i], NULL, 0) != pid[i]) 152 err(1, "waitpid"); 153 } 154 close(fd); 155 unlink(file); 156 157 _exit(0); 158} 159 160int 161main(void) 162{ 163 pid_t pids[PARALLEL]; 164 size_t len; 165 time_t start; 166 int e, i, status; 167 168 e = 0; 169 len = PAGE_SIZE; 170 if ((share = mmap(NULL, len, PROT_READ | PROT_WRITE, 171 MAP_ANON | MAP_SHARED, -1, 0)) == MAP_FAILED) 172 err(1, "mmap"); 173 174 for (i = 0; i < SIZ; i += PAGE_SIZE) 175 cp[i] = 1; 176 start = time(NULL); 177 while ((time(NULL) - start) < RUNTIME && e == 0) { 178 share[SYNC] = 0; 179 share[ACT] = 0; 180 for (i = 0; i < PARALLEL; i++) { 181 if ((pids[i] = fork()) == 0) 182 test(); 183 if (pids[i] == -1) 184 err(1, "fork()"); 185 } 186 for (i = 0; i < PARALLEL; i++) { 187 if (waitpid(pids[i], &status, 0) == -1) 188 err(1, "waitpid(%d)", pids[i]); 189 if (status != 0) { 190 if (WIFSIGNALED(status)) 191 fprintf(stderr, 192 "pid %d exit signal %d\n", 193 pids[i], WTERMSIG(status)); 194 } 195 e += status == 0 ? 0 : 1; 196 } 197 } 198 199 return (e); 200} 201