xref: /freebsd/tools/test/stress2/misc/fork2.sh (revision aa1a8ff2d6dbc51ef058f46f3db5a8bb77967145)
1#!/bin/sh
2
3#
4# SPDX-License-Identifier: BSD-2-Clause
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
52newfs $newfs_flags md$mdstart > /dev/null
53mount /dev/md$mdstart $mntpoint
54set +e
55
56cd $mntpoint
57$dir/fork2
58s=$?
59pkill fork2
60[ -f fork2.core -a $s -eq 0 ] &&
61    { ls -l fork2.core; mv fork2.core $dir; s=1; }
62cd $odir
63
64for i in `jot 6`; do
65	mount | grep -q "on $mntpoint " || break
66	umount $mntpoint && break || sleep 10
67	[ $i -eq 6 ] &&
68	    { echo FATAL; fstat -mf $mntpoint; exit 1; }
69done
70mdconfig -d -u $mdstart
71cd $dir
72mycc -o $dir/fork2 -Wall -Wextra -O0 -g $dir/fork2.c; s=$?
73rm -rf $dir/fork2 $dir/fork2.c
74exit $s
75
76EOF
77#include <sys/param.h>
78#include <sys/mman.h>
79#include <sys/stat.h>
80#include <sys/wait.h>
81
82#include <machine/atomic.h>
83
84#include <err.h>
85#include <errno.h>
86#include <fcntl.h>
87#include <signal.h>
88#include <stdio.h>
89#include <stdlib.h>
90#include <time.h>
91#include <unistd.h>
92
93static volatile u_int *share;
94
95#define MX 5000
96#define RUNTIME (1 * 60)
97#define SYNC 0
98
99int
100main(void)
101{
102	pid_t pid;
103	size_t len;
104	time_t start;
105	int n;
106
107	len = PAGE_SIZE;
108	if ((share = mmap(NULL, len, PROT_READ | PROT_WRITE,
109	    MAP_ANON | MAP_SHARED, -1, 0)) == MAP_FAILED)
110		err(1, "mmap");
111
112	n = 0;
113	signal(SIGCHLD, SIG_IGN);
114	start = time(NULL);
115	while ((time(NULL) - start) < RUNTIME) {
116		while ((atomic_load_int(&share[SYNC])) > MX)
117			usleep(100);
118		n++;
119		pid = fork();
120		if (pid == -1)
121			err(1, "fork)");
122		if (pid == 0) {
123			atomic_add_int(&share[SYNC], 1);
124			while (atomic_load_int(&share[SYNC]) <= MX)
125				usleep(10000);
126			usleep(arc4random() % 1000);
127			atomic_add_int(&share[SYNC], -1);
128			raise(SIGHUP);
129			_exit(0);
130		}
131	}
132	atomic_add_int(&share[SYNC], MX * 2);
133	fprintf(stderr, "%d fork() calls\n", n);
134}
135