xref: /minix3/tests/lib/libc/setjmp/t_setjmp.c (revision 11be35a165022172ed3cea20f2b5df0307540b0e)
1*11be35a1SLionel Sambuc /* $NetBSD: t_setjmp.c,v 1.1 2010/12/27 19:35:31 pgoyette 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_setjmp.c,v 1.1 2010/12/27 19:35:31 pgoyette 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 <atf-c.h>
79*11be35a1SLionel Sambuc 
80*11be35a1SLionel Sambuc #define REQUIRE_ERRNO(x) ATF_REQUIRE_MSG(x, "%s", strerror(errno))
81*11be35a1SLionel Sambuc 
82*11be35a1SLionel Sambuc #define TEST_SETJMP 0
83*11be35a1SLionel Sambuc #define TEST_U_SETJMP 1
84*11be35a1SLionel Sambuc #define TEST_SIGSETJMP_SAVE 2
85*11be35a1SLionel Sambuc #define TEST_SIGSETJMP_NOSAVE 3
86*11be35a1SLionel Sambuc 
87*11be35a1SLionel Sambuc static int expectsignal;
88*11be35a1SLionel Sambuc 
89*11be35a1SLionel Sambuc static void
aborthandler(int signo)90*11be35a1SLionel Sambuc aborthandler(int signo)
91*11be35a1SLionel Sambuc {
92*11be35a1SLionel Sambuc 	ATF_REQUIRE_MSG(expectsignal, "kill(SIGABRT) succeeded");
93*11be35a1SLionel Sambuc 	atf_tc_pass();
94*11be35a1SLionel Sambuc }
95*11be35a1SLionel Sambuc 
96*11be35a1SLionel Sambuc static void
h_check(int test)97*11be35a1SLionel Sambuc h_check(int test)
98*11be35a1SLionel Sambuc {
99*11be35a1SLionel Sambuc 	struct sigaction sa;
100*11be35a1SLionel Sambuc 	jmp_buf jb;
101*11be35a1SLionel Sambuc 	sigjmp_buf sjb;
102*11be35a1SLionel Sambuc 	sigset_t ss;
103*11be35a1SLionel Sambuc 	int i, x;
104*11be35a1SLionel Sambuc 
105*11be35a1SLionel Sambuc 	i = getpid();
106*11be35a1SLionel Sambuc 
107*11be35a1SLionel Sambuc 	if (test == TEST_SETJMP || test == TEST_SIGSETJMP_SAVE)
108*11be35a1SLionel Sambuc 		expectsignal = 0;
109*11be35a1SLionel Sambuc 	else if (test == TEST_U_SETJMP || test == TEST_SIGSETJMP_NOSAVE)
110*11be35a1SLionel Sambuc 		expectsignal = 1;
111*11be35a1SLionel Sambuc 	else
112*11be35a1SLionel Sambuc 		atf_tc_fail("unknown test");
113*11be35a1SLionel Sambuc 
114*11be35a1SLionel Sambuc 	sa.sa_handler = aborthandler;
115*11be35a1SLionel Sambuc 	sigemptyset(&sa.sa_mask);
116*11be35a1SLionel Sambuc 	sa.sa_flags = 0;
117*11be35a1SLionel Sambuc 	REQUIRE_ERRNO(sigaction(SIGABRT, &sa, NULL) != -1);
118*11be35a1SLionel Sambuc 	REQUIRE_ERRNO(sigemptyset(&ss) != -1);
119*11be35a1SLionel Sambuc 	REQUIRE_ERRNO(sigaddset(&ss, SIGABRT) != -1);
120*11be35a1SLionel Sambuc 	REQUIRE_ERRNO(sigprocmask(SIG_BLOCK, &ss, NULL) != -1);
121*11be35a1SLionel Sambuc 
122*11be35a1SLionel Sambuc 	if (test == TEST_SETJMP)
123*11be35a1SLionel Sambuc 		x = setjmp(jb);
124*11be35a1SLionel Sambuc 	else if (test == TEST_U_SETJMP)
125*11be35a1SLionel Sambuc 		x = _setjmp(jb);
126*11be35a1SLionel Sambuc 	else
127*11be35a1SLionel Sambuc 		x = sigsetjmp(sjb, !expectsignal);
128*11be35a1SLionel Sambuc 
129*11be35a1SLionel Sambuc 	if (x != 0) {
130*11be35a1SLionel Sambuc 		ATF_REQUIRE_MSG(x == i, "setjmp returned wrong value");
131*11be35a1SLionel Sambuc 		kill(i, SIGABRT);
132*11be35a1SLionel Sambuc 		ATF_REQUIRE_MSG(!expectsignal, "kill(SIGABRT) failed");
133*11be35a1SLionel Sambuc 		atf_tc_pass();
134*11be35a1SLionel Sambuc 	}
135*11be35a1SLionel Sambuc 
136*11be35a1SLionel Sambuc 	REQUIRE_ERRNO(sigprocmask(SIG_UNBLOCK, &ss, NULL) != -1);
137*11be35a1SLionel Sambuc 
138*11be35a1SLionel Sambuc 	if (test == TEST_SETJMP)
139*11be35a1SLionel Sambuc 		longjmp(jb, i);
140*11be35a1SLionel Sambuc 	else if (test == TEST_U_SETJMP)
141*11be35a1SLionel Sambuc 		_longjmp(jb, i);
142*11be35a1SLionel Sambuc 	else
143*11be35a1SLionel Sambuc 		siglongjmp(sjb, i);
144*11be35a1SLionel Sambuc 
145*11be35a1SLionel Sambuc 	atf_tc_fail("jmp failed");
146*11be35a1SLionel Sambuc }
147*11be35a1SLionel Sambuc 
148*11be35a1SLionel Sambuc ATF_TC(setjmp);
ATF_TC_HEAD(setjmp,tc)149*11be35a1SLionel Sambuc ATF_TC_HEAD(setjmp, tc)
150*11be35a1SLionel Sambuc {
151*11be35a1SLionel Sambuc 	atf_tc_set_md_var(tc, "descr", "Checks setjmp(3)");
152*11be35a1SLionel Sambuc }
ATF_TC_BODY(setjmp,tc)153*11be35a1SLionel Sambuc ATF_TC_BODY(setjmp, tc)
154*11be35a1SLionel Sambuc {
155*11be35a1SLionel Sambuc 	h_check(TEST_SETJMP);
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", "Checks _setjmp(3)");
162*11be35a1SLionel Sambuc }
ATF_TC_BODY(_setjmp,tc)163*11be35a1SLionel Sambuc ATF_TC_BODY(_setjmp, tc)
164*11be35a1SLionel Sambuc {
165*11be35a1SLionel Sambuc 	h_check(TEST_U_SETJMP);
166*11be35a1SLionel Sambuc }
167*11be35a1SLionel Sambuc 
168*11be35a1SLionel Sambuc ATF_TC(sigsetjmp_save);
ATF_TC_HEAD(sigsetjmp_save,tc)169*11be35a1SLionel Sambuc ATF_TC_HEAD(sigsetjmp_save, tc)
170*11be35a1SLionel Sambuc {
171*11be35a1SLionel Sambuc 	atf_tc_set_md_var(tc, "descr", "Checks sigsetjmp(3) with savemask enabled");
172*11be35a1SLionel Sambuc }
ATF_TC_BODY(sigsetjmp_save,tc)173*11be35a1SLionel Sambuc ATF_TC_BODY(sigsetjmp_save, tc)
174*11be35a1SLionel Sambuc {
175*11be35a1SLionel Sambuc 	h_check(TEST_SIGSETJMP_SAVE);
176*11be35a1SLionel Sambuc }
177*11be35a1SLionel Sambuc 
178*11be35a1SLionel Sambuc ATF_TC(sigsetjmp_nosave);
ATF_TC_HEAD(sigsetjmp_nosave,tc)179*11be35a1SLionel Sambuc ATF_TC_HEAD(sigsetjmp_nosave, tc)
180*11be35a1SLionel Sambuc {
181*11be35a1SLionel Sambuc 	atf_tc_set_md_var(tc, "descr", "Checks sigsetjmp(3) with savemask disabled");
182*11be35a1SLionel Sambuc }
ATF_TC_BODY(sigsetjmp_nosave,tc)183*11be35a1SLionel Sambuc ATF_TC_BODY(sigsetjmp_nosave, tc)
184*11be35a1SLionel Sambuc {
185*11be35a1SLionel Sambuc 	h_check(TEST_SIGSETJMP_NOSAVE);
186*11be35a1SLionel Sambuc }
187*11be35a1SLionel Sambuc 
ATF_TP_ADD_TCS(tp)188*11be35a1SLionel Sambuc ATF_TP_ADD_TCS(tp)
189*11be35a1SLionel Sambuc {
190*11be35a1SLionel Sambuc 	ATF_TP_ADD_TC(tp, setjmp);
191*11be35a1SLionel Sambuc 	ATF_TP_ADD_TC(tp, _setjmp);
192*11be35a1SLionel Sambuc 	ATF_TP_ADD_TC(tp, sigsetjmp_save);
193*11be35a1SLionel Sambuc 	ATF_TP_ADD_TC(tp, sigsetjmp_nosave);
194*11be35a1SLionel Sambuc 
195*11be35a1SLionel Sambuc 	return atf_no_error();
196*11be35a1SLionel Sambuc }
197