1314818Sngie/* $NetBSD: t_threadjmp.c,v 1.2 2017/01/14 21:08:17 christos Exp $ */
2272343Sngie
3272343Sngie/*-
4272343Sngie * Copyright (c) 2008 The NetBSD Foundation, Inc.
5272343Sngie * All rights reserved.
6272343Sngie *
7272343Sngie * Redistribution and use in source and binary forms, with or without
8272343Sngie * modification, are permitted provided that the following conditions
9272343Sngie * are met:
10272343Sngie * 1. Redistributions of source code must retain the above copyright
11272343Sngie *    notice, this list of conditions and the following disclaimer.
12272343Sngie * 2. Redistributions in binary form must reproduce the above copyright
13272343Sngie *    notice, this list of conditions and the following disclaimer in the
14272343Sngie *    documentation and/or other materials provided with the distribution.
15272343Sngie *
16272343Sngie * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
17272343Sngie * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
18272343Sngie * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
19272343Sngie * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
20272343Sngie * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21272343Sngie * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22272343Sngie * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23272343Sngie * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24272343Sngie * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25272343Sngie * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26272343Sngie * POSSIBILITY OF SUCH DAMAGE.
27272343Sngie */
28272343Sngie
29272343Sngie/*
30272343Sngie * Copyright (c) 1994 Christopher G. Demetriou
31272343Sngie * All rights reserved.
32272343Sngie *
33272343Sngie * Redistribution and use in source and binary forms, with or without
34272343Sngie * modification, are permitted provided that the following conditions
35272343Sngie * are met:
36272343Sngie * 1. Redistributions of source code must retain the above copyright
37272343Sngie *    notice, this list of conditions and the following disclaimer.
38272343Sngie * 2. Redistributions in binary form must reproduce the above copyright
39272343Sngie *    notice, this list of conditions and the following disclaimer in the
40272343Sngie *    documentation and/or other materials provided with the distribution.
41272343Sngie * 3. All advertising materials mentioning features or use of this software
42272343Sngie *    must display the following acknowledgement:
43272343Sngie *          This product includes software developed for the
44272343Sngie *          NetBSD Project.  See http://www.NetBSD.org/ for
45272343Sngie *          information about NetBSD.
46272343Sngie * 4. The name of the author may not be used to endorse or promote products
47272343Sngie *    derived from this software without specific prior written permission.
48272343Sngie *
49272343Sngie * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
50272343Sngie * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
51272343Sngie * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
52272343Sngie * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
53272343Sngie * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
54272343Sngie * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
55272343Sngie * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
56272343Sngie * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
57272343Sngie * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
58272343Sngie * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
59272343Sngie *
60272343Sngie * <<Id: LICENSE,v 1.2 2000/06/14 15:57:33 cgd Exp>>
61272343Sngie */
62272343Sngie
63272343Sngie#include <sys/cdefs.h>
64272343Sngie__COPYRIGHT("@(#) Copyright (c) 2008\
65272343Sngie The NetBSD Foundation, inc. All rights reserved.");
66314818Sngie__RCSID("$NetBSD: t_threadjmp.c,v 1.2 2017/01/14 21:08:17 christos Exp $");
67272343Sngie
68272343Sngie#include <sys/types.h>
69272343Sngie
70272343Sngie#include <errno.h>
71272343Sngie#include <setjmp.h>
72272343Sngie#include <signal.h>
73272343Sngie#include <stdio.h>
74272343Sngie#include <stdlib.h>
75272343Sngie#include <string.h>
76272343Sngie#include <unistd.h>
77272343Sngie
78272343Sngie#include <pthread.h>
79272343Sngie
80272343Sngie#include <atf-c.h>
81272343Sngie
82272343Sngie#define REQUIRE_ERRNO(x) ATF_REQUIRE_MSG(x, "%s", strerror(errno))
83272343Sngie
84272343Sngie#define TEST_SETJMP 0
85272343Sngie#define TEST_U_SETJMP 1
86272343Sngie#define TEST_SIGSETJMP_SAVE 2
87272343Sngie#define TEST_SIGSETJMP_NOSAVE 3
88272343Sngie
89272343Sngiestatic pthread_t myself = NULL;
90272343Sngie
91272343Sngiestatic int expectsignal;
92272343Sngie
93272343Sngiestatic void
94296586Sbdreweryaborthandler(int signo __unused)
95272343Sngie{
96272343Sngie	ATF_REQUIRE(myself == pthread_self());
97272343Sngie	ATF_REQUIRE_MSG(expectsignal, "kill(SIGABRT) succeeded");
98272343Sngie	atf_tc_pass();
99272343Sngie}
100272343Sngie
101272343Sngiestatic void
102272343Sngieh_check(int test)
103272343Sngie{
104272343Sngie	struct sigaction sa;
105272343Sngie	jmp_buf jb;
106272343Sngie	sigjmp_buf sjb;
107272343Sngie	sigset_t ss;
108272343Sngie	int i, x;
109272343Sngie
110272343Sngie	myself = pthread_self();
111272343Sngie	i = getpid();
112272343Sngie
113272343Sngie	if (test == TEST_SETJMP || test == TEST_SIGSETJMP_SAVE)
114272343Sngie		expectsignal = 0;
115272343Sngie	else if (test == TEST_U_SETJMP || test == TEST_SIGSETJMP_NOSAVE)
116272343Sngie		expectsignal = 1;
117272343Sngie	else
118272343Sngie		atf_tc_fail("unknown test");
119272343Sngie
120272343Sngie	sa.sa_handler = aborthandler;
121272343Sngie	sigemptyset(&sa.sa_mask);
122272343Sngie	sa.sa_flags = 0;
123272343Sngie	REQUIRE_ERRNO(sigaction(SIGABRT, &sa, NULL) != -1);
124272343Sngie	REQUIRE_ERRNO(sigemptyset(&ss) != -1);
125272343Sngie	REQUIRE_ERRNO(sigaddset(&ss, SIGABRT) != -1);
126272343Sngie	REQUIRE_ERRNO(sigprocmask(SIG_BLOCK, &ss, NULL) != -1);
127272343Sngie	ATF_REQUIRE(myself == pthread_self());
128272343Sngie
129272343Sngie	if (test == TEST_SETJMP)
130272343Sngie		x = setjmp(jb);
131272343Sngie	else if (test == TEST_U_SETJMP)
132272343Sngie		x = _setjmp(jb);
133272343Sngie	else
134272343Sngie		x = sigsetjmp(sjb, !expectsignal);
135272343Sngie
136272343Sngie	if (x != 0) {
137272343Sngie		ATF_REQUIRE(myself == pthread_self());
138272343Sngie		ATF_REQUIRE_MSG(x == i, "setjmp returned wrong value");
139272343Sngie		kill(i, SIGABRT);
140272343Sngie		ATF_REQUIRE_MSG(!expectsignal, "kill(SIGABRT) failed");
141272343Sngie		ATF_REQUIRE(myself == pthread_self());
142272343Sngie		atf_tc_pass();
143272343Sngie	}
144272343Sngie
145272343Sngie	ATF_REQUIRE(myself == pthread_self());
146272343Sngie	REQUIRE_ERRNO(sigprocmask(SIG_UNBLOCK, &ss, NULL) != -1);
147272343Sngie
148272343Sngie	if (test == TEST_SETJMP)
149272343Sngie		longjmp(jb, i);
150272343Sngie	else if (test == TEST_U_SETJMP)
151272343Sngie		_longjmp(jb, i);
152272343Sngie	else
153272343Sngie		siglongjmp(sjb, i);
154272343Sngie
155272343Sngie	atf_tc_fail("jmp failed");
156272343Sngie}
157272343Sngie
158272343SngieATF_TC(setjmp);
159272343SngieATF_TC_HEAD(setjmp, tc)
160272343Sngie{
161272343Sngie	atf_tc_set_md_var(tc, "descr",
162272343Sngie	    "Checks pthread_self() and setjmp(3)");
163272343Sngie}
164272343SngieATF_TC_BODY(setjmp, tc)
165272343Sngie{
166272343Sngie	h_check(TEST_SETJMP);
167272343Sngie}
168272343Sngie
169272343SngieATF_TC(_setjmp);
170272343SngieATF_TC_HEAD(_setjmp, tc)
171272343Sngie{
172272343Sngie	atf_tc_set_md_var(tc, "descr",
173272343Sngie	    "Checks pthread_self() and _setjmp(3)");
174272343Sngie}
175272343SngieATF_TC_BODY(_setjmp, tc)
176272343Sngie{
177272343Sngie	h_check(TEST_U_SETJMP);
178272343Sngie}
179272343Sngie
180272343SngieATF_TC(sigsetjmp_save);
181272343SngieATF_TC_HEAD(sigsetjmp_save, tc)
182272343Sngie{
183272343Sngie	atf_tc_set_md_var(tc, "descr",
184272343Sngie	    "Checks pthread_self() and sigsetjmp(3) with savemask enabled");
185272343Sngie}
186272343SngieATF_TC_BODY(sigsetjmp_save, tc)
187272343Sngie{
188272343Sngie	h_check(TEST_SIGSETJMP_SAVE);
189272343Sngie}
190272343Sngie
191272343SngieATF_TC(sigsetjmp_nosave);
192272343SngieATF_TC_HEAD(sigsetjmp_nosave, tc)
193272343Sngie{
194272343Sngie	atf_tc_set_md_var(tc, "descr",
195272343Sngie	    "Checks pthread_self() and sigsetjmp(3) with savemask disabled");
196272343Sngie}
197272343SngieATF_TC_BODY(sigsetjmp_nosave, tc)
198272343Sngie{
199272343Sngie	h_check(TEST_SIGSETJMP_NOSAVE);
200272343Sngie}
201272343Sngie
202272343SngieATF_TP_ADD_TCS(tp)
203272343Sngie{
204272343Sngie
205272343Sngie	/*
206272343Sngie	 * These test cases try to verify setjmp and friends in a program
207272343Sngie	 * linked with pthreads, and verify that pthread_self() stays
208272343Sngie	 * consistent throughout the program. A setcontext() call invoked
209272343Sngie	 * by *setjmp() might clobber the TLS special register used to
210272343Sngie	 * implement pthread_self().
211272343Sngie	 */
212272343Sngie	ATF_TP_ADD_TC(tp, setjmp);
213272343Sngie	ATF_TP_ADD_TC(tp, _setjmp);
214272343Sngie	ATF_TP_ADD_TC(tp, sigsetjmp_save);
215272343Sngie	ATF_TP_ADD_TC(tp, sigsetjmp_nosave);
216272343Sngie
217272343Sngie	return atf_no_error();
218272343Sngie}
219