xref: /freebsd/tools/test/stress2/misc/sem_wait.sh (revision 5ca8e32633c4ffbbcd6762e5888b6a4ba0708c6c)
1#!/bin/sh
2
3#
4# SPDX-License-Identifier: BSD-2-Clause
5#
6# Copyright (c) 2019 Dell EMC Isilon
7#
8# Redistribution and use in source and binary forms, with or without
9# modification, are permitted provided that the following conditions
10# are met:
11# 1. Redistributions of source code must retain the above copyright
12#    notice, this list of conditions and the following disclaimer.
13# 2. Redistributions in binary form must reproduce the above copyright
14#    notice, this list of conditions and the following disclaimer in the
15#    documentation and/or other materials provided with the distribution.
16#
17# THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
18# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20# ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
21# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23# OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26# OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27# SUCH DAMAGE.
28#
29
30# "panic: vm_page_free_prep: freeing mapped page 0x657936c" seen.
31# https://people.freebsd.org/~pho/stress/log/sem_wait.txt
32# Fixed by r350005
33
34. ../default.cfg
35
36cat > /tmp/sem_wait.c <<EOF
37#include <sys/param.h>
38#include <sys/mman.h>
39#include <sys/stat.h>
40#include <sys/wait.h>
41
42#include <err.h>
43#include <pthread.h>
44#include <semaphore.h>
45#include <signal.h>
46#include <stdio.h>
47#include <stdlib.h>
48#include <string.h>
49#include <time.h>
50#include <unistd.h>
51
52static sem_t *semaphore;
53
54static void *
55test(void) {
56
57	setproctitle("%s", __func__);
58	alarm(300);
59	for (;;) {
60		if (sem_wait(semaphore) == -1)
61			err(1, "sem_wait");
62		if (sem_post(semaphore) == -1)
63			err(1, "sem_post");
64	}
65}
66
67int
68main(void) {
69	pid_t pid;
70	size_t len;
71	time_t start;
72
73	setproctitle("%s", __func__);
74	alarm(300);
75	len = PAGE_SIZE;
76	if ((semaphore = mmap(NULL, len, PROT_READ | PROT_WRITE,
77	    MAP_ANON | MAP_SHARED, -1, 0)) == MAP_FAILED)
78		err(1, "mmap");
79
80	// initialize semaphore and set value to 1
81	if (sem_init(semaphore, 1, 1) == -1)
82		err(1, "sem_init");
83
84	if ((pid = fork()) == 0)
85		test();
86	if (pid == -1)
87		err(1, "fork()");
88
89	usleep(50);
90	alarm(300);
91	start = time(NULL);
92	while (time(NULL) - start < 60) {
93		if (sem_wait(semaphore) == -1)
94			err(1, "sem_wait");
95		if (sem_post(semaphore) == -1)
96			err(1, "sem_post");
97	}
98	kill(pid, SIGHUP);
99	if (waitpid(pid, NULL, 0) != pid)
100		err(1, "waitpid()");
101
102	return (0);
103}
104EOF
105mycc -o /tmp/sem_wait -Wall -Wextra -O2 /tmp/sem_wait.c || exit 1
106timeout 6m /tmp/sem_wait; s=$?
107[ $s -eq 124 ] && echo "Timed out"
108rm -f /tmp/sem_wait /tmp/sem_wait.c
109exit $s
110