xref: /freebsd/tools/test/stress2/misc/syscall4.sh (revision dc318a4ffabcbfa23bb56a33403aad36e6de30af)
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_sigsuspend,
145	SYS_mac_syscall,
146	SYS_sigtimedwait,
147	SYS_sigwaitinfo,
148};
149
150static int fd[900], fds[2], kq, socketpr[2];
151#ifndef nitems
152#define nitems(x) (sizeof((x)) / sizeof((x)[0]))
153#endif
154#define N 4096
155#define MAGIC 1664
156#define RUNTIME 120
157#define THREADS 50
158
159static uint32_t r[N];
160static int magic1, syscallno, magic2;
161
162static int
163random_int(int mi, int ma)
164{
165        return (arc4random()  % (ma - mi + 1) + mi);
166}
167
168static void
169hand(int i __unused) {	/* handler */
170	exit(1);
171}
172
173static unsigned long
174makearg(void)
175{
176	unsigned int i;
177	unsigned long val;
178
179	val = arc4random();
180	i   = arc4random() % 100;
181	if (i < 20)
182		val = val & 0xff;
183	if (i >= 20 && i < 40)
184		val = val & 0xffff;
185	if (i >= 40 && i < 60)
186		val = (unsigned long)(r) | (val & 0xffff);
187#if defined(__LP64__)
188	if (i >= 60) {
189		val = (val << 32) | arc4random();
190		if (i > 80)
191			val = val & 0x00007fffffffffffUL;
192	}
193#endif
194
195	return(val);
196}
197
198static void *
199test(void *arg __unused)
200{
201	FTS *fts;
202	FTSENT *p;
203	time_t start;
204	int ftsoptions, i, numfiles;
205	char *args[] = {
206	    "/dev",
207	    "/proc",
208	    "MNTPOINT",
209	    "mnt2",
210	    ".",
211	    NULL,
212	};
213
214#ifdef __NP__
215	pthread_set_name_np(pthread_self(), __func__);
216#endif
217	numfiles = 0;
218	ftsoptions = FTS_PHYSICAL;
219	start = time(NULL);
220	while (time(NULL) - start < 2) {
221		for (i = 0; i < N; i++)
222			r[i] = arc4random();
223
224		if (pipe(fds) == -1)
225			err(1, "pipe()");
226		if (socketpair(PF_UNIX, SOCK_SEQPACKET, 0, socketpr) == -1)
227			err(1, "socketpair()");
228		kq = kqueue();
229
230		if ((fts = fts_open(args, ftsoptions, NULL)) == NULL)
231			err(1, "fts_open");
232
233		i = 0;
234		while ((p = fts_read(fts)) != NULL) {
235			if (fd[i] > 0)
236				close(fd[i]);
237			if ((fd[i] = open(p->fts_path, O_RDWR)) == -1)
238				if ((fd[i] = open(p->fts_path, O_WRONLY)) ==
239				    -1)
240					if ((fd[i] = open(p->fts_path,
241					    O_RDONLY)) == -1)
242						continue;
243			i++;
244			i = i % nitems(fd);
245			if (numfiles++ < 10) {
246				fprintf(stderr, "%d: pts_path = %s\n",
247				    numfiles, p->fts_path);
248			}
249		}
250
251		if (fts_close(fts) == -1)
252			warn("fts_close()");
253		sleep(1);
254		close(socketpr[0]);
255		close(socketpr[1]);
256		close(fds[0]);
257		close(fds[1]);
258		close(kq);
259	}
260	return(NULL);
261}
262
263static void *
264calls(void *arg __unused)
265{
266	time_t start;
267	int i, j, num;
268	unsigned long arg1, arg2, arg3, arg4, arg5, arg6, arg7;
269
270#ifdef __NP__
271	pthread_set_name_np(pthread_self(), __func__);
272#endif
273	start = time(NULL);
274	for (i = 0; time(NULL) - start < 10; i++) {
275		num = syscallno;
276		while (num == 0) {
277			num = random_int(0, SYS_MAXSYSCALL);
278			for (j = 0; j < (int)nitems(ignore); j++)
279				if (num == ignore[j]) {
280					num = 0;
281					break;
282				}
283		}
284		arg1 = makearg();
285		arg2 = makearg();
286		arg3 = makearg();
287		arg4 = makearg();
288		arg5 = makearg();
289		arg6 = makearg();
290		arg7 = makearg();
291
292#if 0		/* Debug mode */
293		fprintf(stderr, "%2d : syscall(%3d, %lx, %lx, %lx, %lx, %lx,"
294		   " %lx, %lx)\n",
295			i, num, arg1, arg2, arg3, arg4, arg5, arg6, arg7);
296		sleep(2);
297#endif
298		alarm(1);
299		syscall(num, arg1, arg2, arg3, arg4, arg5, arg6, arg7);
300		num = 0;
301		if (magic1 != MAGIC || magic2 != MAGIC)
302			exit(1);
303	}
304
305	return (NULL);
306}
307
308int
309main(int argc, char **argv)
310{
311	struct passwd *pw;
312	struct rlimit limit;
313	pthread_t rp, cp[THREADS];
314	time_t start;
315	int e, j;
316
317	magic1 = magic2 = MAGIC;
318	if ((pw = getpwnam("nobody")) == NULL)
319		err(1, "failed to resolve nobody");
320
321	if (getenv("USE_ROOT") && argc == 2)
322		fprintf(stderr, "Running syscall4 as root for %s.\n",
323				argv[1]);
324	else {
325		if (setgroups(1, &pw->pw_gid) ||
326		    setegid(pw->pw_gid) || setgid(pw->pw_gid) ||
327		    seteuid(pw->pw_uid) || setuid(pw->pw_uid))
328			err(1, "Can't drop privileges to \"nobody\"");
329		endpwent();
330	}
331
332	limit.rlim_cur = limit.rlim_max = 1000;
333#if defined(RLIMIT_NPTS)
334	if (setrlimit(RLIMIT_NPTS, &limit) < 0)
335		err(1, "setrlimit");
336#endif
337
338	signal(SIGALRM, hand);
339	signal(SIGILL,  hand);
340	signal(SIGFPE,  hand);
341	signal(SIGSEGV, hand);
342	signal(SIGBUS,  hand);
343	signal(SIGURG,  hand);
344	signal(SIGSYS,  hand);
345	signal(SIGTRAP, hand);
346
347	if (argc > 2) {
348		fprintf(stderr, "usage: %s [syscall-num]\n", argv[0]);
349		exit(1);
350	}
351	if (argc == 2) {
352		syscallno = atoi(argv[1]);
353		for (j = 0; j < (int)nitems(ignore); j++)
354			if (syscallno == ignore[j])
355				errx(0, "syscall #%d is on the ignore list.",
356				    syscallno);
357	}
358
359	if (daemon(1, 1) == -1)
360		err(1, "daemon()");
361
362	system("touch aaa bbb ccc; mkdir -p ddd");
363	start = time(NULL);
364	while ((time(NULL) - start) < RUNTIME) {
365		if (fork() == 0) {
366			if ((e = pthread_create(&rp, NULL, test, NULL)) != 0)
367				errc(1, e, "pthread_create");
368			usleep(1000);
369			for (j = 0; j < THREADS; j++)
370				if ((e = pthread_create(&cp[j], NULL, calls,
371				    NULL)) != 0)
372					errc(1, e, "pthread_create");
373			for (j = 0; j < THREADS; j++)
374				pthread_join(cp[j], NULL);
375
376			if ((e = pthread_kill(rp, SIGINT)) != 0)
377				errc(1, e, "pthread_kill");
378			exit(0);
379		}
380		wait(NULL);
381		usleep(10000);
382	}
383
384	return (0);
385}
386