xref: /freebsd/tools/test/stress2/misc/syscall4.sh (revision c1d255d3)
1#!/bin/sh
2
3#
4# Copyright (c) 2011-2013 Peter Holm
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# Threaded syscall(2) fuzz test inspired by the iknowthis test suite
30# by Tavis Ormandy <taviso  cmpxchg8b com>
31
32# Usage: syscall4.sh [syscall number]
33#	Without an argument random syscall numbers are tested.
34#	With an argument only the specified syscall number is tested.
35
36# Sample problems found:
37# Thread stuck in stopprof.
38# http://people.freebsd.org/~pho/stress/log/kostik732.txt
39# Fixed by r275121.
40
41# panic: td 0xcbe1ac40 is not suspended.
42# https://people.freebsd.org/~pho/stress/log/kostik807.txt
43# Fixed by r282944.
44
45[ `id -u ` -ne 0 ] && echo "Must be root!" && exit 1
46
47. ../default.cfg
48
49odir=`pwd`
50cd /tmp
51sed '1,/^EOF/d' < $odir/$0 > syscall4.c
52sed -i '' -e "s#MNTPOINT#$mntpoint#" syscall4.c
53rm -f /tmp/syscall4
54mycc -o syscall4 -Wall -Wextra -O2 -g syscall4.c -lpthread || exit 1
55rm -f syscall4.c
56
57kldstat -v | grep -q sysvmsg  || $stress2tools/kldload.sh sysvmsg
58kldstat -v | grep -q sysvsem  || $stress2tools/kldload.sh sysvsem
59kldstat -v | grep -q sysvshm  || $stress2tools/kldload.sh sysvshm
60kldstat -v | grep -q aio      || $stress2tools/kldload.sh aio
61kldstat -v | grep -q mqueuefs || $stress2tools/kldload.sh mqueuefs
62
63mount | grep -q "on $mntpoint " && umount -f $mntpoint
64[ -c /dev/md$mdstart ] && mdconfig -d -u $mdstart
65
66mdconfig -a -t swap -s 2g -u $mdstart || exit 1
67bsdlabel -w md$mdstart auto
68newfs $newfs_flags -n md${mdstart}$part > /dev/null
69mount /dev/md${mdstart}$part $mntpoint
70chmod 777 $mntpoint
71
72[ -z "$noswap" ] &&
73    daemon sh -c "(cd $odir/../testcases/swap; ./swap -t 10m -i 20 -k)" > \
74    /dev/null
75sleeptime=${sleeptime:-12}
76st=`date '+%s'`
77while [ $((`date '+%s'` - st)) -lt $((10 * sleeptime)) ]; do
78	(cd $mntpoint; /tmp/syscall4 $* 1>>stdout 2>>stderr) &
79	start=`date '+%s'`
80	while [ $((`date '+%s'` - start)) -lt $sleeptime ]; do
81		pgrep syscall4 > /dev/null || break
82		sleep .5
83	done
84	while pkill -9 syscall4; do :; done
85	wait
86	ipcs | grep nobody | awk '/^(q|m|s)/ {print " -" $1, $2}' |
87	    xargs -L 1 ipcrm
88done
89while pkill -9 swap; do :; done
90while pkill -9 syscall4; do :; done
91
92for i in `jot 10`; do
93	mount | grep -q md${mdstart}$part  && \
94		umount $mntpoint && mdconfig -d -u $mdstart && break
95	sleep 10
96done
97if mount | grep -q md${mdstart}$part; then
98	fstat $mntpoint
99	echo "umount $mntpoint failed"
100	exit 1
101fi
102rm -f /tmp/syscall4
103exit 0
104EOF
105#include <sys/types.h>
106#include <sys/event.h>
107#include <sys/resource.h>
108#include <sys/socket.h>
109#include <sys/stat.h>
110#include <sys/syscall.h>
111#include <sys/wait.h>
112
113#include <err.h>
114#include <errno.h>
115#include <fcntl.h>
116#include <fts.h>
117#include <libutil.h>
118#include <pthread.h>
119#if defined(__FreeBSD__)
120#include <pthread_np.h>
121#define	__NP__
122#endif
123#include <pwd.h>
124#include <signal.h>
125#include <stdint.h>
126#include <stdio.h>
127#include <stdlib.h>
128#include <string.h>
129#include <unistd.h>
130
131static int ignore[] = {
132	SYS_syscall,
133	SYS_exit,
134	SYS_fork,
135	11,			/* 11 is obsolete execv */
136	SYS_reboot,
137	SYS_vfork,
138	109,			/* 109 is old sigblock */
139	111,			/* 111 is old sigsuspend */
140	SYS_shutdown,
141	SYS___syscall,
142	216,			/* custom syscall */
143	SYS_rfork,
144	SYS_mac_syscall,
145};
146
147static int fd[900], fds[2], kq, socketpr[2];
148#ifndef nitems
149#define nitems(x) (sizeof((x)) / sizeof((x)[0]))
150#endif
151#define N 4096
152#define MAGIC 1664
153#define RUNTIME 120
154#define THREADS 50
155
156static uint32_t r[N];
157static int magic1, syscallno, magic2;
158
159static int
160random_int(int mi, int ma)
161{
162        return (arc4random()  % (ma - mi + 1) + mi);
163}
164
165static void
166hand(int i __unused) {	/* handler */
167	exit(1);
168}
169
170static unsigned long
171makearg(void)
172{
173	unsigned int i;
174	unsigned long val;
175
176	val = arc4random();
177	i   = arc4random() % 100;
178	if (i < 20)
179		val = val & 0xff;
180	if (i >= 20 && i < 40)
181		val = val & 0xffff;
182	if (i >= 40 && i < 60)
183		val = (unsigned long)(r) | (val & 0xffff);
184#if defined(__LP64__)
185	if (i >= 60) {
186		val = (val << 32) | arc4random();
187		if (i > 80)
188			val = val & 0x00007fffffffffffUL;
189	}
190#endif
191
192	return(val);
193}
194
195static void *
196test(void *arg __unused)
197{
198	FTS *fts;
199	FTSENT *p;
200	time_t start;
201	int ftsoptions, i, numfiles;
202	char *args[] = {
203	    "/dev",
204	    "/proc",
205	    "MNTPOINT",
206	    "mnt2",
207	    ".",
208	    NULL,
209	};
210
211#ifdef __NP__
212	pthread_set_name_np(pthread_self(), __func__);
213#endif
214	numfiles = 0;
215	ftsoptions = FTS_PHYSICAL;
216	start = time(NULL);
217	while (time(NULL) - start < 2) {
218		for (i = 0; i < N; i++)
219			r[i] = arc4random();
220
221		if (pipe(fds) == -1)
222			err(1, "pipe()");
223		if (socketpair(PF_UNIX, SOCK_SEQPACKET, 0, socketpr) == -1)
224			err(1, "socketpair()");
225		kq = kqueue();
226
227		if ((fts = fts_open(args, ftsoptions, NULL)) == NULL)
228			err(1, "fts_open");
229
230		i = 0;
231		while ((p = fts_read(fts)) != NULL) {
232			if (fd[i] > 0)
233				close(fd[i]);
234			if ((fd[i] = open(p->fts_path, O_RDWR)) == -1)
235				if ((fd[i] = open(p->fts_path, O_WRONLY)) ==
236				    -1)
237					if ((fd[i] = open(p->fts_path,
238					    O_RDONLY)) == -1)
239						continue;
240			i++;
241			i = i % nitems(fd);
242			if (numfiles++ < 10) {
243				fprintf(stderr, "%d: pts_path = %s\n",
244				    numfiles, p->fts_path);
245			}
246		}
247
248		if (fts_close(fts) == -1)
249			warn("fts_close()");
250		sleep(1);
251		close(socketpr[0]);
252		close(socketpr[1]);
253		close(fds[0]);
254		close(fds[1]);
255		close(kq);
256	}
257	return(NULL);
258}
259
260static void *
261calls(void *arg __unused)
262{
263	time_t start;
264	int i, j, num;
265	unsigned long arg1, arg2, arg3, arg4, arg5, arg6, arg7;
266
267#ifdef __NP__
268	pthread_set_name_np(pthread_self(), __func__);
269#endif
270	start = time(NULL);
271	for (i = 0; time(NULL) - start < 10; i++) {
272		num = syscallno;
273		while (num == 0) {
274			num = random_int(0, SYS_MAXSYSCALL);
275			for (j = 0; j < (int)nitems(ignore); j++)
276				if (num == ignore[j]) {
277					num = 0;
278					break;
279				}
280		}
281		arg1 = makearg();
282		arg2 = makearg();
283		arg3 = makearg();
284		arg4 = makearg();
285		arg5 = makearg();
286		arg6 = makearg();
287		arg7 = makearg();
288
289#if 0		/* Debug mode */
290		fprintf(stderr, "%2d : syscall(%3d, %lx, %lx, %lx, %lx, %lx,"
291		   " %lx, %lx)\n",
292			i, num, arg1, arg2, arg3, arg4, arg5, arg6, arg7);
293		sleep(2);
294#endif
295		alarm(1);
296		syscall(num, arg1, arg2, arg3, arg4, arg5, arg6, arg7);
297		num = 0;
298		if (magic1 != MAGIC || magic2 != MAGIC)
299			exit(1);
300	}
301
302	return (NULL);
303}
304
305int
306main(int argc, char **argv)
307{
308	struct passwd *pw;
309	struct rlimit limit;
310	pthread_t rp, cp[THREADS];
311	time_t start;
312	int e, j;
313
314	magic1 = magic2 = MAGIC;
315	if ((pw = getpwnam("nobody")) == NULL)
316		err(1, "failed to resolve nobody");
317
318	if (getenv("USE_ROOT") && argc == 2)
319		fprintf(stderr, "Running syscall4 as root for %s.\n",
320				argv[1]);
321	else {
322		if (setgroups(1, &pw->pw_gid) ||
323		    setegid(pw->pw_gid) || setgid(pw->pw_gid) ||
324		    seteuid(pw->pw_uid) || setuid(pw->pw_uid))
325			err(1, "Can't drop privileges to \"nobody\"");
326		endpwent();
327	}
328
329	limit.rlim_cur = limit.rlim_max = 1000;
330#if defined(RLIMIT_NPTS)
331	if (setrlimit(RLIMIT_NPTS, &limit) < 0)
332		err(1, "setrlimit");
333#endif
334
335	signal(SIGALRM, hand);
336	signal(SIGILL,  hand);
337	signal(SIGFPE,  hand);
338	signal(SIGSEGV, hand);
339	signal(SIGBUS,  hand);
340	signal(SIGURG,  hand);
341	signal(SIGSYS,  hand);
342	signal(SIGTRAP, hand);
343
344	if (argc > 2) {
345		fprintf(stderr, "usage: %s [syscall-num]\n", argv[0]);
346		exit(1);
347	}
348	if (argc == 2) {
349		syscallno = atoi(argv[1]);
350		for (j = 0; j < (int)nitems(ignore); j++)
351			if (syscallno == ignore[j])
352				errx(0, "syscall #%d is on the ignore list.",
353				    syscallno);
354	}
355
356	if (daemon(1, 1) == -1)
357		err(1, "daemon()");
358
359	system("touch aaa bbb ccc; mkdir -p ddd");
360	start = time(NULL);
361	while ((time(NULL) - start) < RUNTIME) {
362		if (fork() == 0) {
363			if ((e = pthread_create(&rp, NULL, test, NULL)) != 0)
364				errc(1, e, "pthread_create");
365			usleep(1000);
366			for (j = 0; j < THREADS; j++)
367				if ((e = pthread_create(&cp[j], NULL, calls,
368				    NULL)) != 0)
369					errc(1, e, "pthread_create");
370			for (j = 0; j < THREADS; j++)
371				pthread_join(cp[j], NULL);
372
373			if ((e = pthread_kill(rp, SIGINT)) != 0)
374				errc(1, e, "pthread_kill");
375			exit(0);
376		}
377		wait(NULL);
378		usleep(10000);
379	}
380
381	return (0);
382}
383