1*11be35a1SLionel Sambuc /* $NetBSD: t_threadjmp.c,v 1.1 2011/04/21 18:58:20 martin Exp $ */
2*11be35a1SLionel Sambuc
3*11be35a1SLionel Sambuc /*-
4*11be35a1SLionel Sambuc * Copyright (c) 2008 The NetBSD Foundation, Inc.
5*11be35a1SLionel Sambuc * All rights reserved.
6*11be35a1SLionel Sambuc *
7*11be35a1SLionel Sambuc * Redistribution and use in source and binary forms, with or without
8*11be35a1SLionel Sambuc * modification, are permitted provided that the following conditions
9*11be35a1SLionel Sambuc * are met:
10*11be35a1SLionel Sambuc * 1. Redistributions of source code must retain the above copyright
11*11be35a1SLionel Sambuc * notice, this list of conditions and the following disclaimer.
12*11be35a1SLionel Sambuc * 2. Redistributions in binary form must reproduce the above copyright
13*11be35a1SLionel Sambuc * notice, this list of conditions and the following disclaimer in the
14*11be35a1SLionel Sambuc * documentation and/or other materials provided with the distribution.
15*11be35a1SLionel Sambuc *
16*11be35a1SLionel Sambuc * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
17*11be35a1SLionel Sambuc * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
18*11be35a1SLionel Sambuc * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
19*11be35a1SLionel Sambuc * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
20*11be35a1SLionel Sambuc * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21*11be35a1SLionel Sambuc * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22*11be35a1SLionel Sambuc * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23*11be35a1SLionel Sambuc * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24*11be35a1SLionel Sambuc * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25*11be35a1SLionel Sambuc * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26*11be35a1SLionel Sambuc * POSSIBILITY OF SUCH DAMAGE.
27*11be35a1SLionel Sambuc */
28*11be35a1SLionel Sambuc
29*11be35a1SLionel Sambuc /*
30*11be35a1SLionel Sambuc * Copyright (c) 1994 Christopher G. Demetriou
31*11be35a1SLionel Sambuc * All rights reserved.
32*11be35a1SLionel Sambuc *
33*11be35a1SLionel Sambuc * Redistribution and use in source and binary forms, with or without
34*11be35a1SLionel Sambuc * modification, are permitted provided that the following conditions
35*11be35a1SLionel Sambuc * are met:
36*11be35a1SLionel Sambuc * 1. Redistributions of source code must retain the above copyright
37*11be35a1SLionel Sambuc * notice, this list of conditions and the following disclaimer.
38*11be35a1SLionel Sambuc * 2. Redistributions in binary form must reproduce the above copyright
39*11be35a1SLionel Sambuc * notice, this list of conditions and the following disclaimer in the
40*11be35a1SLionel Sambuc * documentation and/or other materials provided with the distribution.
41*11be35a1SLionel Sambuc * 3. All advertising materials mentioning features or use of this software
42*11be35a1SLionel Sambuc * must display the following acknowledgement:
43*11be35a1SLionel Sambuc * This product includes software developed for the
44*11be35a1SLionel Sambuc * NetBSD Project. See http://www.NetBSD.org/ for
45*11be35a1SLionel Sambuc * information about NetBSD.
46*11be35a1SLionel Sambuc * 4. The name of the author may not be used to endorse or promote products
47*11be35a1SLionel Sambuc * derived from this software without specific prior written permission.
48*11be35a1SLionel Sambuc *
49*11be35a1SLionel Sambuc * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
50*11be35a1SLionel Sambuc * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
51*11be35a1SLionel Sambuc * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
52*11be35a1SLionel Sambuc * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
53*11be35a1SLionel Sambuc * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
54*11be35a1SLionel Sambuc * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
55*11be35a1SLionel Sambuc * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
56*11be35a1SLionel Sambuc * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
57*11be35a1SLionel Sambuc * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
58*11be35a1SLionel Sambuc * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
59*11be35a1SLionel Sambuc *
60*11be35a1SLionel Sambuc * <<Id: LICENSE,v 1.2 2000/06/14 15:57:33 cgd Exp>>
61*11be35a1SLionel Sambuc */
62*11be35a1SLionel Sambuc
63*11be35a1SLionel Sambuc #include <sys/cdefs.h>
64*11be35a1SLionel Sambuc __COPYRIGHT("@(#) Copyright (c) 2008\
65*11be35a1SLionel Sambuc The NetBSD Foundation, inc. All rights reserved.");
66*11be35a1SLionel Sambuc __RCSID("$NetBSD: t_threadjmp.c,v 1.1 2011/04/21 18:58:20 martin Exp $");
67*11be35a1SLionel Sambuc
68*11be35a1SLionel Sambuc #include <sys/types.h>
69*11be35a1SLionel Sambuc
70*11be35a1SLionel Sambuc #include <errno.h>
71*11be35a1SLionel Sambuc #include <setjmp.h>
72*11be35a1SLionel Sambuc #include <signal.h>
73*11be35a1SLionel Sambuc #include <stdio.h>
74*11be35a1SLionel Sambuc #include <stdlib.h>
75*11be35a1SLionel Sambuc #include <string.h>
76*11be35a1SLionel Sambuc #include <unistd.h>
77*11be35a1SLionel Sambuc
78*11be35a1SLionel Sambuc #include <pthread.h>
79*11be35a1SLionel Sambuc
80*11be35a1SLionel Sambuc #include <atf-c.h>
81*11be35a1SLionel Sambuc
82*11be35a1SLionel Sambuc #define REQUIRE_ERRNO(x) ATF_REQUIRE_MSG(x, "%s", strerror(errno))
83*11be35a1SLionel Sambuc
84*11be35a1SLionel Sambuc #define TEST_SETJMP 0
85*11be35a1SLionel Sambuc #define TEST_U_SETJMP 1
86*11be35a1SLionel Sambuc #define TEST_SIGSETJMP_SAVE 2
87*11be35a1SLionel Sambuc #define TEST_SIGSETJMP_NOSAVE 3
88*11be35a1SLionel Sambuc
89*11be35a1SLionel Sambuc static pthread_t myself = NULL;
90*11be35a1SLionel Sambuc
91*11be35a1SLionel Sambuc static int expectsignal;
92*11be35a1SLionel Sambuc
93*11be35a1SLionel Sambuc static void
aborthandler(int signo)94*11be35a1SLionel Sambuc aborthandler(int signo)
95*11be35a1SLionel Sambuc {
96*11be35a1SLionel Sambuc ATF_REQUIRE(myself == pthread_self());
97*11be35a1SLionel Sambuc ATF_REQUIRE_MSG(expectsignal, "kill(SIGABRT) succeeded");
98*11be35a1SLionel Sambuc atf_tc_pass();
99*11be35a1SLionel Sambuc }
100*11be35a1SLionel Sambuc
101*11be35a1SLionel Sambuc static void
h_check(int test)102*11be35a1SLionel Sambuc h_check(int test)
103*11be35a1SLionel Sambuc {
104*11be35a1SLionel Sambuc struct sigaction sa;
105*11be35a1SLionel Sambuc jmp_buf jb;
106*11be35a1SLionel Sambuc sigjmp_buf sjb;
107*11be35a1SLionel Sambuc sigset_t ss;
108*11be35a1SLionel Sambuc int i, x;
109*11be35a1SLionel Sambuc
110*11be35a1SLionel Sambuc myself = pthread_self();
111*11be35a1SLionel Sambuc i = getpid();
112*11be35a1SLionel Sambuc
113*11be35a1SLionel Sambuc if (test == TEST_SETJMP || test == TEST_SIGSETJMP_SAVE)
114*11be35a1SLionel Sambuc expectsignal = 0;
115*11be35a1SLionel Sambuc else if (test == TEST_U_SETJMP || test == TEST_SIGSETJMP_NOSAVE)
116*11be35a1SLionel Sambuc expectsignal = 1;
117*11be35a1SLionel Sambuc else
118*11be35a1SLionel Sambuc atf_tc_fail("unknown test");
119*11be35a1SLionel Sambuc
120*11be35a1SLionel Sambuc sa.sa_handler = aborthandler;
121*11be35a1SLionel Sambuc sigemptyset(&sa.sa_mask);
122*11be35a1SLionel Sambuc sa.sa_flags = 0;
123*11be35a1SLionel Sambuc REQUIRE_ERRNO(sigaction(SIGABRT, &sa, NULL) != -1);
124*11be35a1SLionel Sambuc REQUIRE_ERRNO(sigemptyset(&ss) != -1);
125*11be35a1SLionel Sambuc REQUIRE_ERRNO(sigaddset(&ss, SIGABRT) != -1);
126*11be35a1SLionel Sambuc REQUIRE_ERRNO(sigprocmask(SIG_BLOCK, &ss, NULL) != -1);
127*11be35a1SLionel Sambuc ATF_REQUIRE(myself == pthread_self());
128*11be35a1SLionel Sambuc
129*11be35a1SLionel Sambuc if (test == TEST_SETJMP)
130*11be35a1SLionel Sambuc x = setjmp(jb);
131*11be35a1SLionel Sambuc else if (test == TEST_U_SETJMP)
132*11be35a1SLionel Sambuc x = _setjmp(jb);
133*11be35a1SLionel Sambuc else
134*11be35a1SLionel Sambuc x = sigsetjmp(sjb, !expectsignal);
135*11be35a1SLionel Sambuc
136*11be35a1SLionel Sambuc if (x != 0) {
137*11be35a1SLionel Sambuc ATF_REQUIRE(myself == pthread_self());
138*11be35a1SLionel Sambuc ATF_REQUIRE_MSG(x == i, "setjmp returned wrong value");
139*11be35a1SLionel Sambuc kill(i, SIGABRT);
140*11be35a1SLionel Sambuc ATF_REQUIRE_MSG(!expectsignal, "kill(SIGABRT) failed");
141*11be35a1SLionel Sambuc ATF_REQUIRE(myself == pthread_self());
142*11be35a1SLionel Sambuc atf_tc_pass();
143*11be35a1SLionel Sambuc }
144*11be35a1SLionel Sambuc
145*11be35a1SLionel Sambuc ATF_REQUIRE(myself == pthread_self());
146*11be35a1SLionel Sambuc REQUIRE_ERRNO(sigprocmask(SIG_UNBLOCK, &ss, NULL) != -1);
147*11be35a1SLionel Sambuc
148*11be35a1SLionel Sambuc if (test == TEST_SETJMP)
149*11be35a1SLionel Sambuc longjmp(jb, i);
150*11be35a1SLionel Sambuc else if (test == TEST_U_SETJMP)
151*11be35a1SLionel Sambuc _longjmp(jb, i);
152*11be35a1SLionel Sambuc else
153*11be35a1SLionel Sambuc siglongjmp(sjb, i);
154*11be35a1SLionel Sambuc
155*11be35a1SLionel Sambuc atf_tc_fail("jmp failed");
156*11be35a1SLionel Sambuc }
157*11be35a1SLionel Sambuc
158*11be35a1SLionel Sambuc ATF_TC(setjmp);
ATF_TC_HEAD(setjmp,tc)159*11be35a1SLionel Sambuc ATF_TC_HEAD(setjmp, tc)
160*11be35a1SLionel Sambuc {
161*11be35a1SLionel Sambuc atf_tc_set_md_var(tc, "descr",
162*11be35a1SLionel Sambuc "Checks pthread_self() and setjmp(3)");
163*11be35a1SLionel Sambuc }
ATF_TC_BODY(setjmp,tc)164*11be35a1SLionel Sambuc ATF_TC_BODY(setjmp, tc)
165*11be35a1SLionel Sambuc {
166*11be35a1SLionel Sambuc h_check(TEST_SETJMP);
167*11be35a1SLionel Sambuc }
168*11be35a1SLionel Sambuc
169*11be35a1SLionel Sambuc ATF_TC(_setjmp);
ATF_TC_HEAD(_setjmp,tc)170*11be35a1SLionel Sambuc ATF_TC_HEAD(_setjmp, tc)
171*11be35a1SLionel Sambuc {
172*11be35a1SLionel Sambuc atf_tc_set_md_var(tc, "descr",
173*11be35a1SLionel Sambuc "Checks pthread_self() and _setjmp(3)");
174*11be35a1SLionel Sambuc }
ATF_TC_BODY(_setjmp,tc)175*11be35a1SLionel Sambuc ATF_TC_BODY(_setjmp, tc)
176*11be35a1SLionel Sambuc {
177*11be35a1SLionel Sambuc h_check(TEST_U_SETJMP);
178*11be35a1SLionel Sambuc }
179*11be35a1SLionel Sambuc
180*11be35a1SLionel Sambuc ATF_TC(sigsetjmp_save);
ATF_TC_HEAD(sigsetjmp_save,tc)181*11be35a1SLionel Sambuc ATF_TC_HEAD(sigsetjmp_save, tc)
182*11be35a1SLionel Sambuc {
183*11be35a1SLionel Sambuc atf_tc_set_md_var(tc, "descr",
184*11be35a1SLionel Sambuc "Checks pthread_self() and sigsetjmp(3) with savemask enabled");
185*11be35a1SLionel Sambuc }
ATF_TC_BODY(sigsetjmp_save,tc)186*11be35a1SLionel Sambuc ATF_TC_BODY(sigsetjmp_save, tc)
187*11be35a1SLionel Sambuc {
188*11be35a1SLionel Sambuc h_check(TEST_SIGSETJMP_SAVE);
189*11be35a1SLionel Sambuc }
190*11be35a1SLionel Sambuc
191*11be35a1SLionel Sambuc ATF_TC(sigsetjmp_nosave);
ATF_TC_HEAD(sigsetjmp_nosave,tc)192*11be35a1SLionel Sambuc ATF_TC_HEAD(sigsetjmp_nosave, tc)
193*11be35a1SLionel Sambuc {
194*11be35a1SLionel Sambuc atf_tc_set_md_var(tc, "descr",
195*11be35a1SLionel Sambuc "Checks pthread_self() and sigsetjmp(3) with savemask disabled");
196*11be35a1SLionel Sambuc }
ATF_TC_BODY(sigsetjmp_nosave,tc)197*11be35a1SLionel Sambuc ATF_TC_BODY(sigsetjmp_nosave, tc)
198*11be35a1SLionel Sambuc {
199*11be35a1SLionel Sambuc h_check(TEST_SIGSETJMP_NOSAVE);
200*11be35a1SLionel Sambuc }
201*11be35a1SLionel Sambuc
ATF_TP_ADD_TCS(tp)202*11be35a1SLionel Sambuc ATF_TP_ADD_TCS(tp)
203*11be35a1SLionel Sambuc {
204*11be35a1SLionel Sambuc
205*11be35a1SLionel Sambuc /*
206*11be35a1SLionel Sambuc * These test cases try to verify setjmp and friends in a program
207*11be35a1SLionel Sambuc * linked with pthreads, and verify that pthread_self() stays
208*11be35a1SLionel Sambuc * consistent throughout the program. A setcontext() call invoked
209*11be35a1SLionel Sambuc * by *setjmp() might clobber the TLS special register used to
210*11be35a1SLionel Sambuc * implement pthread_self().
211*11be35a1SLionel Sambuc */
212*11be35a1SLionel Sambuc ATF_TP_ADD_TC(tp, setjmp);
213*11be35a1SLionel Sambuc ATF_TP_ADD_TC(tp, _setjmp);
214*11be35a1SLionel Sambuc ATF_TP_ADD_TC(tp, sigsetjmp_save);
215*11be35a1SLionel Sambuc ATF_TP_ADD_TC(tp, sigsetjmp_nosave);
216*11be35a1SLionel Sambuc
217*11be35a1SLionel Sambuc return atf_no_error();
218*11be35a1SLionel Sambuc }
219