xref: /freebsd/tools/test/stress2/misc/forkbomb.sh (revision 924226fba12cc9a228c73b956e1b7fa24c60b055)
1#!/bin/sh
2
3#
4# Copyright (c) 2015 EMC Corp.
5# All rights reserved.
6#
7# Redistribution and use in source and binary forms, with or without
8# modification, are permitted provided that the following conditions
9# are met:
10# 1. Redistributions of source code must retain the above copyright
11#    notice, this list of conditions and the following disclaimer.
12# 2. Redistributions in binary form must reproduce the above copyright
13#    notice, this list of conditions and the following disclaimer in the
14#    documentation and/or other materials provided with the distribution.
15#
16# THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19# ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22# OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25# OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26# SUCH DAMAGE.
27#
28
29# Fork bomb memory leak test scenario.
30# https://en.wikipedia.org/wiki/Fork_bomb
31
32# OO memory seen:
33# https://people.freebsd.org/~pho/stress/log/forkbomb.txt
34# Fixed by r289026.
35
36. ../default.cfg
37[ `id -u ` -ne 0 ] && echo "Must be root!" && exit 1
38
39dir=/tmp
40odir=`pwd`
41cd $dir
42sed '1,/^EOF/d' < $odir/$0 > $dir/forkbomb.c
43mycc -o forkbomb -Wall -Wextra -O0 -g forkbomb.c || exit 1
44rm -f forkbomb.c
45cd $odir
46
47mount | grep "on $mntpoint " | grep -q /dev/md && umount -f $mntpoint
48mdconfig -l | grep -q md$mdstart &&  mdconfig -d -u $mdstart
49mdconfig -a -t swap -s 512m -u $mdstart || exit 1
50newfs $newfs_flags md$mdstart > /dev/null
51mount /dev/md$mdstart $mntpoint
52
53su $testuser -c /tmp/forkbomb
54
55while mount | grep "on $mntpoint " | grep -q /dev/md; do
56	umount $mntpoint || sleep 1
57done
58mdconfig -d -u $mdstart
59rm -rf /tmp/forkbomb
60exit 0
61
62EOF
63#include <sys/param.h>
64#include <sys/mman.h>
65
66#include <machine/atomic.h>
67
68#include <err.h>
69#include <errno.h>
70#include <fcntl.h>
71#include <signal.h>
72#include <stdio.h>
73#include <stdlib.h>
74#include <unistd.h>
75
76volatile u_int *share;
77
78#define R1 1 /* sync start  */
79#define R2 2 /* forks       */
80#define R3 3 /* exits       */
81#define R4 4 /* fork failed */
82
83//#define DEBUG
84#define MXFAIL 100
85#define MAXPROC 40000	/* Arbitrary cap */
86#define PARALLEL 200
87
88void
89test(void)
90{
91	int r;
92
93	alarm(1200);
94	atomic_add_int(&share[R1], 1);
95	while (share[R1] != PARALLEL)
96		;
97	atomic_add_int(&share[R2], 1);
98
99	for (;;) {
100		if (share[R2] >= MAXPROC || share[R4] > MXFAIL)
101			break;
102		atomic_add_int(&share[R2], 1);
103		if ((r = fork()) == -1) {
104			atomic_add_int(&share[R4], 1);
105			atomic_add_int(&share[R2], -1);
106			break;
107		}
108	}
109
110	atomic_add_int(&share[R3], 1);
111	_exit(0);
112}
113
114int
115main(void)
116{
117	struct sigaction sa;
118	size_t len;
119	int i;
120
121	alarm(1200);
122	len = PAGE_SIZE;
123	if ((share = mmap(NULL, len, PROT_READ | PROT_WRITE,
124	    MAP_ANON | MAP_SHARED, -1, 0)) == MAP_FAILED)
125		err(1, "mmap");
126
127	sa.sa_handler = SIG_IGN;
128	sigemptyset(&sa.sa_mask);
129	sa.sa_flags = 0;
130	if (sigaction(SIGCHLD, &sa, 0) == -1)
131		err(1, "sigaction");
132
133	for (i = 0; i < PARALLEL; i++) {
134		if (fork() == 0)
135			test();
136	}
137
138	while (share[R2] == 0 || share[R3] < share[R2])
139		sleep(1);
140
141#if defined(DEBUG)
142	fprintf(stderr, "MAXPROC: %d. forks: %u / exits: %u / fails: %u\n",
143	    MAXPROC, share[R2], share[R3], share[R4]);
144#endif
145
146	return (0);
147}
148