1*64f92395SPeter Holm#!/bin/sh 2*64f92395SPeter Holm 3*64f92395SPeter Holm# 4*64f92395SPeter Holm# SPDX-License-Identifier: BSD-2-Clause-FreeBSD 5*64f92395SPeter Holm# 6*64f92395SPeter Holm# Copyright (c) 2022 Peter Holm 7*64f92395SPeter Holm# 8*64f92395SPeter Holm# Redistribution and use in source and binary forms, with or without 9*64f92395SPeter Holm# modification, are permitted provided that the following conditions 10*64f92395SPeter Holm# are met: 11*64f92395SPeter Holm# 1. Redistributions of source code must retain the above copyright 12*64f92395SPeter Holm# notice, this list of conditions and the following disclaimer. 13*64f92395SPeter Holm# 2. Redistributions in binary form must reproduce the above copyright 14*64f92395SPeter Holm# notice, this list of conditions and the following disclaimer in the 15*64f92395SPeter Holm# documentation and/or other materials provided with the distribution. 16*64f92395SPeter Holm# 17*64f92395SPeter Holm# THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 18*64f92395SPeter Holm# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 19*64f92395SPeter Holm# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 20*64f92395SPeter Holm# ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 21*64f92395SPeter Holm# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 22*64f92395SPeter Holm# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 23*64f92395SPeter Holm# OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 24*64f92395SPeter Holm# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 25*64f92395SPeter Holm# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 26*64f92395SPeter Holm# OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 27*64f92395SPeter Holm# SUCH DAMAGE. 28*64f92395SPeter Holm# 29*64f92395SPeter Holm 30*64f92395SPeter Holm# Regression test for D36069 31*64f92395SPeter Holm# thread_create(): call cpu_copy_thread() after td_pflags is zeroed 32*64f92395SPeter Holm 33*64f92395SPeter Holm# Seen before fix: 34*64f92395SPeter Holm# cc: error: unable to execute command: Segmentation fault (core dumped) 35*64f92395SPeter Holm# cc: error: linker command failed due to signal (use -v to see invocation) 36*64f92395SPeter Holm# Aug 9 18:27:47 freebsd-vm kernel: pid 32094 (ld.lld), jid 0, uid 0: exited on signal 11 (core dumped) 37*64f92395SPeter Holm 38*64f92395SPeter Holm. ../default.cfg 39*64f92395SPeter Holm[ `id -u` -ne 0 ] && echo "Must be root!" && exit 1 40*64f92395SPeter Holm 41*64f92395SPeter Holmdir=/tmp 42*64f92395SPeter Holmodir=`pwd` 43*64f92395SPeter Holmcd $dir 44*64f92395SPeter Holmsed '1,/^EOF/d' < $odir/$0 > $dir/fork2.c 45*64f92395SPeter Holmmycc -o fork2 -Wall -Wextra -O0 -g fork2.c || exit 1 46*64f92395SPeter Holmcd $odir 47*64f92395SPeter Holm 48*64f92395SPeter Holmset -e 49*64f92395SPeter Holmmount | grep "on $mntpoint " | grep -q /dev/md && umount -f $mntpoint 50*64f92395SPeter Holm[ -c /dev/md$mdstart ] && mdconfig -d -u $mdstart 51*64f92395SPeter Holmmdconfig -a -t swap -s 2g -u $mdstart 52*64f92395SPeter Holmbsdlabel -w md$mdstart auto 53*64f92395SPeter Holmnewfs $newfs_flags md${mdstart}$part > /dev/null 54*64f92395SPeter Holmmount /dev/md${mdstart}$part $mntpoint 55*64f92395SPeter Holmset +e 56*64f92395SPeter Holm 57*64f92395SPeter Holmcd $mntpoint 58*64f92395SPeter Holm$dir/fork2 59*64f92395SPeter Holms=$? 60*64f92395SPeter Holmpkill fork2 61*64f92395SPeter Holm[ -f fork2.core -a $s -eq 0 ] && 62*64f92395SPeter Holm { ls -l fork2.core; mv fork2.core $dir; s=1; } 63*64f92395SPeter Holmcd $odir 64*64f92395SPeter Holm 65*64f92395SPeter Holmfor i in `jot 6`; do 66*64f92395SPeter Holm mount | grep -q "on $mntpoint " || break 67*64f92395SPeter Holm umount $mntpoint && break || sleep 10 68*64f92395SPeter Holm [ $i -eq 6 ] && 69*64f92395SPeter Holm { echo FATAL; fstat -mf $mntpoint; exit 1; } 70*64f92395SPeter Holmdone 71*64f92395SPeter Holmmdconfig -d -u $mdstart 72*64f92395SPeter Holmcd $dir 73*64f92395SPeter Holmmycc -o $dir/fork2 -Wall -Wextra -O0 -g $dir/fork2.c; s=$? 74*64f92395SPeter Holmrm -rf $dir/fork2 $dir/fork2.c 75*64f92395SPeter Holmexit $s 76*64f92395SPeter Holm 77*64f92395SPeter HolmEOF 78*64f92395SPeter Holm#include <sys/param.h> 79*64f92395SPeter Holm#include <sys/mman.h> 80*64f92395SPeter Holm#include <sys/stat.h> 81*64f92395SPeter Holm#include <sys/wait.h> 82*64f92395SPeter Holm 83*64f92395SPeter Holm#include <machine/atomic.h> 84*64f92395SPeter Holm 85*64f92395SPeter Holm#include <err.h> 86*64f92395SPeter Holm#include <errno.h> 87*64f92395SPeter Holm#include <fcntl.h> 88*64f92395SPeter Holm#include <signal.h> 89*64f92395SPeter Holm#include <stdio.h> 90*64f92395SPeter Holm#include <stdlib.h> 91*64f92395SPeter Holm#include <time.h> 92*64f92395SPeter Holm#include <unistd.h> 93*64f92395SPeter Holm 94*64f92395SPeter Holmstatic volatile u_int *share; 95*64f92395SPeter Holm 96*64f92395SPeter Holm#define MX 5000 97*64f92395SPeter Holm#define RUNTIME (1 * 60) 98*64f92395SPeter Holm#define SYNC 0 99*64f92395SPeter Holm 100*64f92395SPeter Holmint 101*64f92395SPeter Holmmain(void) 102*64f92395SPeter Holm{ 103*64f92395SPeter Holm pid_t pid; 104*64f92395SPeter Holm size_t len; 105*64f92395SPeter Holm time_t start; 106*64f92395SPeter Holm int n; 107*64f92395SPeter Holm 108*64f92395SPeter Holm len = PAGE_SIZE; 109*64f92395SPeter Holm if ((share = mmap(NULL, len, PROT_READ | PROT_WRITE, 110*64f92395SPeter Holm MAP_ANON | MAP_SHARED, -1, 0)) == MAP_FAILED) 111*64f92395SPeter Holm err(1, "mmap"); 112*64f92395SPeter Holm 113*64f92395SPeter Holm n = 0; 114*64f92395SPeter Holm signal(SIGCHLD, SIG_IGN); 115*64f92395SPeter Holm start = time(NULL); 116*64f92395SPeter Holm while ((time(NULL) - start) < RUNTIME) { 117*64f92395SPeter Holm while ((atomic_load_int(&share[SYNC])) > MX) 118*64f92395SPeter Holm usleep(100); 119*64f92395SPeter Holm n++; 120*64f92395SPeter Holm pid = fork(); 121*64f92395SPeter Holm if (pid == -1) 122*64f92395SPeter Holm err(1, "fork)"); 123*64f92395SPeter Holm if (pid == 0) { 124*64f92395SPeter Holm atomic_add_int(&share[SYNC], 1); 125*64f92395SPeter Holm while (atomic_load_int(&share[SYNC]) <= MX) 126*64f92395SPeter Holm usleep(10000); 127*64f92395SPeter Holm usleep(arc4random() % 1000); 128*64f92395SPeter Holm atomic_add_int(&share[SYNC], -1); 129*64f92395SPeter Holm raise(SIGHUP); 130*64f92395SPeter Holm _exit(0); 131*64f92395SPeter Holm } 132*64f92395SPeter Holm } 133*64f92395SPeter Holm atomic_add_int(&share[SYNC], MX * 2); 134*64f92395SPeter Holm fprintf(stderr, "%d fork() calls\n", n); 135*64f92395SPeter Holm} 136