xref: /freebsd/tools/test/stress2/misc/fsgs.sh (revision 911f0260390e18cf85f3dbf2c719b593efdc1e3c)
1#!/bin/sh
2
3#
4# Copyright (c) 2017 Dell EMC Isilon
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# Regression test for r322799:
30# Ensure that fs/gs bases are stored in pcb before copying the pcb for
31# new process or thread.
32
33# "Exit status is 139" seen.
34
35# Test scenario suggestion by kib@
36
37. ../default.cfg
38
39dir=/tmp
40odir=`pwd`
41cd $dir
42sed '1,/^EOF/d' < $odir/$0 > $dir/fsgs.c
43mycc -o fsgs -Wall -Wextra -O0 -g fsgs.c || exit 1
44rm -f fsgs.c
45cd $odir
46
47$dir/fsgs
48s=$?
49[ -f $dir/fsgs.core ] &&
50    { file $dir/fsgs.core; s=1; }
51
52rm -rf $dir/fsgs $dir/fsgs.core
53exit $s
54
55EOF
56#include <sys/param.h>
57#include <sys/mman.h>
58#include <sys/stat.h>
59#include <sys/wait.h>
60
61#include <machine/atomic.h>
62
63#include <err.h>
64#include <errno.h>
65#include <fcntl.h>
66#include <stdio.h>
67#include <stdlib.h>
68#include <time.h>
69#include <unistd.h>
70
71static volatile u_int *share;
72
73#define PARALLEL 4
74#define RUNTIME (1 * 60)
75#define SYNC 0
76
77static void
78test(void)
79{
80	pid_t pid;
81	int i, status;
82	volatile char *cp;
83
84	atomic_add_int(&share[SYNC], 1);
85	while (share[SYNC] != PARALLEL)
86		;
87
88	for (i = 0; i < 100; i++) {
89		if ((pid = fork()) == 0) {
90			cp = malloc(2);
91			_exit(cp == NULL);
92		}
93		if (waitpid(pid, &status, 0) != pid)
94			err(1, "waitpid(%d)", pid);
95		if (status != 0) {
96			fprintf(stderr, "Exit status is %d\n", status);
97			exit(1);
98		}
99	}
100
101	_exit(0);
102}
103
104int
105main(void)
106{
107	pid_t pids[PARALLEL];
108	size_t len;
109	time_t start;
110	int e, i, status;
111
112	e = 0;
113	len = PAGE_SIZE;
114	if ((share = mmap(NULL, len, PROT_READ | PROT_WRITE,
115	    MAP_ANON | MAP_SHARED, -1, 0)) == MAP_FAILED)
116		err(1, "mmap");
117
118	start = time(NULL);
119	while ((time(NULL) - start) < RUNTIME && e == 0) {
120		share[SYNC] = 0;
121		for (i = 0; i < PARALLEL; i++) {
122			if ((pids[i] = fork()) == 0)
123				test();
124			if (pids[i] == -1)
125				err(1, "fork()");
126		}
127		for (i = 0; i < PARALLEL; i++) {
128			if (waitpid(pids[i], &status, 0) == -1)
129				err(1, "waitpid(%d)", pids[i]);
130			e += status == 0 ? 0 : 1;
131		}
132	}
133
134	return (e);
135}
136