xref: /freebsd/tools/test/stress2/misc/mlockall3.sh (revision 79ac3c12a714bcd3f2354c52d948aed9575c46d6)
1#!/bin/sh
2
3#
4# Copyright (c) 2013 Peter Holm <pho@FreeBSD.org>
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# mlockall(2) test scenario.
30# "panic: vm_page_unwire: page xxx's wire count is zero" seen.
31# http://people.freebsd.org/~pho/stress/log/mlockall.txt
32
33[ `id -u ` -ne 0 ] && echo "Must be root!" && exit 1
34
35. ../default.cfg
36
37dir=/tmp
38odir=`pwd`
39cd $dir
40sed '1,/^EOF/d' < $odir/$0 > $dir/mlockall3.c
41mycc -o mlockall3 -Wall -Wextra mlockall3.c -lpthread || exit 1
42rm -f mlockall3.c
43cd $odir
44
45/tmp/mlockall3
46
47killall mlockall3 > /dev/null 2>&1
48rm -f /tmp/mlockall3
49exit
50
51EOF
52#include <sys/types.h>
53#include <err.h>
54#include <errno.h>
55#include <fcntl.h>
56#include <pthread.h>
57#include <pwd.h>
58#include <signal.h>
59#include <sys/mman.h>
60#include <stdint.h>
61#include <stdio.h>
62#include <stdlib.h>
63#include <string.h>
64#include <sys/param.h>
65#include <sys/stat.h>
66#include <sys/syscall.h>
67#include <sys/wait.h>
68#include <unistd.h>
69
70#define N (128 * 1024 / (int)sizeof(u_int32_t))
71u_int32_t r[N];
72
73static void
74hand(int i __unused) {	/* handler */
75	_exit(1);
76}
77
78unsigned long
79makearg(void)
80{
81	unsigned int i;
82	unsigned long val;
83
84	val = arc4random();
85	i   = arc4random() % 100;
86	if (i < 20)
87		val = val & 0xff;
88	if (i >= 20 && i < 40)
89		val = val & 0xffff;
90	if (i >= 40 && i < 60)
91		val = (unsigned long)(r) | (val & 0xffff);
92#if defined(__LP64__)
93	if (i >= 60) {
94		val = (val << 32) | arc4random();
95		if (i > 80)
96			val = val & 0x00007fffffffffffUL;
97	}
98#endif
99
100	return(val);
101}
102
103void *
104calls(void *arg __unused)
105{
106	int i;
107	unsigned long arg1, arg2, arg3;
108
109	usleep(1000);
110	for (i = 0; i < 500; i++) {
111		arg1 = makearg();
112		arg2 = makearg();
113		arg3 = makearg();
114
115		alarm(1);
116		syscall(SYS_mlockall, arg1, arg2, arg3);
117	}
118
119	return (0);
120}
121
122int
123main(void)
124{
125	struct passwd *pw;
126	pid_t pid;
127	pthread_t cp[50];
128	int e, i, j;
129
130	if ((pw = getpwnam("nobody")) == NULL)
131		err(1, "no such user: nobody");
132
133	if (setgroups(1, &pw->pw_gid) ||
134	    setegid(pw->pw_gid) || setgid(pw->pw_gid) ||
135	    seteuid(pw->pw_uid) || setuid(pw->pw_uid))
136		err(1, "Can't drop privileges to \"nobody\"");
137	endpwent();
138
139	signal(SIGALRM, hand);
140	signal(SIGILL,  hand);
141	signal(SIGFPE,  hand);
142	signal(SIGSEGV, hand);
143	signal(SIGBUS,  hand);
144	signal(SIGURG,  hand);
145	signal(SIGSYS,  hand);
146	signal(SIGTRAP, hand);
147
148	alarm(180);
149	for (i = 0; i < 8000; i++) {
150		if ((pid = fork()) == 0) {
151			for (j = 0; j < N; j++)
152				r[j] = arc4random();
153			for (j = 0; j < 50; j++)
154				if ((e = pthread_create(&cp[j], NULL, calls, NULL)) != 0)
155					errc(1, e, "pthread_create");
156
157			for (j = 0; j < 50; j++)
158				pthread_join(cp[j], NULL);
159			_exit(0);
160		}
161		if (pid == -1)
162			err(1, "fork()");
163		wait(NULL);
164	}
165
166	return (0);
167}
168