xref: /freebsd/tools/test/stress2/misc/fork2.sh (revision 271171e0)
1#!/bin/sh
2
3#
4# SPDX-License-Identifier: BSD-2-Clause-FreeBSD
5#
6# Copyright (c) 2022 Peter Holm
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 D36069
31# thread_create(): call cpu_copy_thread() after td_pflags is zeroed
32
33# Seen before fix:
34# cc: error: unable to execute command: Segmentation fault (core dumped)
35# cc: error: linker command failed due to signal (use -v to see invocation)
36# Aug  9 18:27:47 freebsd-vm kernel: pid 32094 (ld.lld), jid 0, uid 0: exited on signal 11 (core dumped)
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/fork2.c
45mycc -o fork2 -Wall -Wextra -O0 -g fork2.c || exit 1
46cd $odir
47
48set -e
49mount | grep "on $mntpoint " | grep -q /dev/md && umount -f $mntpoint
50[ -c /dev/md$mdstart ] &&  mdconfig -d -u $mdstart
51mdconfig -a -t swap -s 2g -u $mdstart
52bsdlabel -w md$mdstart auto
53newfs $newfs_flags md${mdstart}$part > /dev/null
54mount /dev/md${mdstart}$part $mntpoint
55set +e
56
57cd $mntpoint
58$dir/fork2
59s=$?
60pkill fork2
61[ -f fork2.core -a $s -eq 0 ] &&
62    { ls -l fork2.core; mv fork2.core $dir; s=1; }
63cd $odir
64
65for i in `jot 6`; do
66	mount | grep -q "on $mntpoint " || break
67	umount $mntpoint && break || sleep 10
68	[ $i -eq 6 ] &&
69	    { echo FATAL; fstat -mf $mntpoint; exit 1; }
70done
71mdconfig -d -u $mdstart
72cd $dir
73mycc -o $dir/fork2 -Wall -Wextra -O0 -g $dir/fork2.c; s=$?
74rm -rf $dir/fork2 $dir/fork2.c
75exit $s
76
77EOF
78#include <sys/param.h>
79#include <sys/mman.h>
80#include <sys/stat.h>
81#include <sys/wait.h>
82
83#include <machine/atomic.h>
84
85#include <err.h>
86#include <errno.h>
87#include <fcntl.h>
88#include <signal.h>
89#include <stdio.h>
90#include <stdlib.h>
91#include <time.h>
92#include <unistd.h>
93
94static volatile u_int *share;
95
96#define MX 5000
97#define RUNTIME (1 * 60)
98#define SYNC 0
99
100int
101main(void)
102{
103	pid_t pid;
104	size_t len;
105	time_t start;
106	int n;
107
108	len = PAGE_SIZE;
109	if ((share = mmap(NULL, len, PROT_READ | PROT_WRITE,
110	    MAP_ANON | MAP_SHARED, -1, 0)) == MAP_FAILED)
111		err(1, "mmap");
112
113	n = 0;
114	signal(SIGCHLD, SIG_IGN);
115	start = time(NULL);
116	while ((time(NULL) - start) < RUNTIME) {
117		while ((atomic_load_int(&share[SYNC])) > MX)
118			usleep(100);
119		n++;
120		pid = fork();
121		if (pid == -1)
122			err(1, "fork)");
123		if (pid == 0) {
124			atomic_add_int(&share[SYNC], 1);
125			while (atomic_load_int(&share[SYNC]) <= MX)
126				usleep(10000);
127			usleep(arc4random() % 1000);
128			atomic_add_int(&share[SYNC], -1);
129			raise(SIGHUP);
130			_exit(0);
131		}
132	}
133	atomic_add_int(&share[SYNC], MX * 2);
134	fprintf(stderr, "%d fork() calls\n", n);
135}
136