164f92395SPeter Holm#!/bin/sh 264f92395SPeter Holm 364f92395SPeter Holm# 4*4d846d26SWarner Losh# SPDX-License-Identifier: BSD-2-Clause 564f92395SPeter Holm# 664f92395SPeter Holm# Copyright (c) 2022 Peter Holm 764f92395SPeter Holm# 864f92395SPeter Holm# Redistribution and use in source and binary forms, with or without 964f92395SPeter Holm# modification, are permitted provided that the following conditions 1064f92395SPeter Holm# are met: 1164f92395SPeter Holm# 1. Redistributions of source code must retain the above copyright 1264f92395SPeter Holm# notice, this list of conditions and the following disclaimer. 1364f92395SPeter Holm# 2. Redistributions in binary form must reproduce the above copyright 1464f92395SPeter Holm# notice, this list of conditions and the following disclaimer in the 1564f92395SPeter Holm# documentation and/or other materials provided with the distribution. 1664f92395SPeter Holm# 1764f92395SPeter Holm# THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 1864f92395SPeter Holm# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 1964f92395SPeter Holm# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 2064f92395SPeter Holm# ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 2164f92395SPeter Holm# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 2264f92395SPeter Holm# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 2364f92395SPeter Holm# OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 2464f92395SPeter Holm# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 2564f92395SPeter Holm# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 2664f92395SPeter Holm# OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 2764f92395SPeter Holm# SUCH DAMAGE. 2864f92395SPeter Holm# 2964f92395SPeter Holm 3064f92395SPeter Holm# Regression test for D36069 3164f92395SPeter Holm# thread_create(): call cpu_copy_thread() after td_pflags is zeroed 3264f92395SPeter Holm 3364f92395SPeter Holm# Seen before fix: 3464f92395SPeter Holm# cc: error: unable to execute command: Segmentation fault (core dumped) 3564f92395SPeter Holm# cc: error: linker command failed due to signal (use -v to see invocation) 3664f92395SPeter Holm# Aug 9 18:27:47 freebsd-vm kernel: pid 32094 (ld.lld), jid 0, uid 0: exited on signal 11 (core dumped) 3764f92395SPeter Holm 3864f92395SPeter Holm. ../default.cfg 3964f92395SPeter Holm[ `id -u` -ne 0 ] && echo "Must be root!" && exit 1 4064f92395SPeter Holm 4164f92395SPeter Holmdir=/tmp 4264f92395SPeter Holmodir=`pwd` 4364f92395SPeter Holmcd $dir 4464f92395SPeter Holmsed '1,/^EOF/d' < $odir/$0 > $dir/fork2.c 4564f92395SPeter Holmmycc -o fork2 -Wall -Wextra -O0 -g fork2.c || exit 1 4664f92395SPeter Holmcd $odir 4764f92395SPeter Holm 4864f92395SPeter Holmset -e 4964f92395SPeter Holmmount | grep "on $mntpoint " | grep -q /dev/md && umount -f $mntpoint 5064f92395SPeter Holm[ -c /dev/md$mdstart ] && mdconfig -d -u $mdstart 5164f92395SPeter Holmmdconfig -a -t swap -s 2g -u $mdstart 5264f92395SPeter Holmbsdlabel -w md$mdstart auto 5364f92395SPeter Holmnewfs $newfs_flags md${mdstart}$part > /dev/null 5464f92395SPeter Holmmount /dev/md${mdstart}$part $mntpoint 5564f92395SPeter Holmset +e 5664f92395SPeter Holm 5764f92395SPeter Holmcd $mntpoint 5864f92395SPeter Holm$dir/fork2 5964f92395SPeter Holms=$? 6064f92395SPeter Holmpkill fork2 6164f92395SPeter Holm[ -f fork2.core -a $s -eq 0 ] && 6264f92395SPeter Holm { ls -l fork2.core; mv fork2.core $dir; s=1; } 6364f92395SPeter Holmcd $odir 6464f92395SPeter Holm 6564f92395SPeter Holmfor i in `jot 6`; do 6664f92395SPeter Holm mount | grep -q "on $mntpoint " || break 6764f92395SPeter Holm umount $mntpoint && break || sleep 10 6864f92395SPeter Holm [ $i -eq 6 ] && 6964f92395SPeter Holm { echo FATAL; fstat -mf $mntpoint; exit 1; } 7064f92395SPeter Holmdone 7164f92395SPeter Holmmdconfig -d -u $mdstart 7264f92395SPeter Holmcd $dir 7364f92395SPeter Holmmycc -o $dir/fork2 -Wall -Wextra -O0 -g $dir/fork2.c; s=$? 7464f92395SPeter Holmrm -rf $dir/fork2 $dir/fork2.c 7564f92395SPeter Holmexit $s 7664f92395SPeter Holm 7764f92395SPeter HolmEOF 7864f92395SPeter Holm#include <sys/param.h> 7964f92395SPeter Holm#include <sys/mman.h> 8064f92395SPeter Holm#include <sys/stat.h> 8164f92395SPeter Holm#include <sys/wait.h> 8264f92395SPeter Holm 8364f92395SPeter Holm#include <machine/atomic.h> 8464f92395SPeter Holm 8564f92395SPeter Holm#include <err.h> 8664f92395SPeter Holm#include <errno.h> 8764f92395SPeter Holm#include <fcntl.h> 8864f92395SPeter Holm#include <signal.h> 8964f92395SPeter Holm#include <stdio.h> 9064f92395SPeter Holm#include <stdlib.h> 9164f92395SPeter Holm#include <time.h> 9264f92395SPeter Holm#include <unistd.h> 9364f92395SPeter Holm 9464f92395SPeter Holmstatic volatile u_int *share; 9564f92395SPeter Holm 9664f92395SPeter Holm#define MX 5000 9764f92395SPeter Holm#define RUNTIME (1 * 60) 9864f92395SPeter Holm#define SYNC 0 9964f92395SPeter Holm 10064f92395SPeter Holmint 10164f92395SPeter Holmmain(void) 10264f92395SPeter Holm{ 10364f92395SPeter Holm pid_t pid; 10464f92395SPeter Holm size_t len; 10564f92395SPeter Holm time_t start; 10664f92395SPeter Holm int n; 10764f92395SPeter Holm 10864f92395SPeter Holm len = PAGE_SIZE; 10964f92395SPeter Holm if ((share = mmap(NULL, len, PROT_READ | PROT_WRITE, 11064f92395SPeter Holm MAP_ANON | MAP_SHARED, -1, 0)) == MAP_FAILED) 11164f92395SPeter Holm err(1, "mmap"); 11264f92395SPeter Holm 11364f92395SPeter Holm n = 0; 11464f92395SPeter Holm signal(SIGCHLD, SIG_IGN); 11564f92395SPeter Holm start = time(NULL); 11664f92395SPeter Holm while ((time(NULL) - start) < RUNTIME) { 11764f92395SPeter Holm while ((atomic_load_int(&share[SYNC])) > MX) 11864f92395SPeter Holm usleep(100); 11964f92395SPeter Holm n++; 12064f92395SPeter Holm pid = fork(); 12164f92395SPeter Holm if (pid == -1) 12264f92395SPeter Holm err(1, "fork)"); 12364f92395SPeter Holm if (pid == 0) { 12464f92395SPeter Holm atomic_add_int(&share[SYNC], 1); 12564f92395SPeter Holm while (atomic_load_int(&share[SYNC]) <= MX) 12664f92395SPeter Holm usleep(10000); 12764f92395SPeter Holm usleep(arc4random() % 1000); 12864f92395SPeter Holm atomic_add_int(&share[SYNC], -1); 12964f92395SPeter Holm raise(SIGHUP); 13064f92395SPeter Holm _exit(0); 13164f92395SPeter Holm } 13264f92395SPeter Holm } 13364f92395SPeter Holm atomic_add_int(&share[SYNC], MX * 2); 13464f92395SPeter Holm fprintf(stderr, "%d fork() calls\n", n); 13564f92395SPeter Holm} 136