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