1f40f3adcSEnji Cooper /* $NetBSD: t_setjmp.c,v 1.2 2017/01/14 21:08:17 christos Exp $ */
257718be8SEnji Cooper
357718be8SEnji Cooper /*-
457718be8SEnji Cooper * Copyright (c) 2008 The NetBSD Foundation, Inc.
557718be8SEnji Cooper * All rights reserved.
657718be8SEnji Cooper *
757718be8SEnji Cooper * Redistribution and use in source and binary forms, with or without
857718be8SEnji Cooper * modification, are permitted provided that the following conditions
957718be8SEnji Cooper * are met:
1057718be8SEnji Cooper * 1. Redistributions of source code must retain the above copyright
1157718be8SEnji Cooper * notice, this list of conditions and the following disclaimer.
1257718be8SEnji Cooper * 2. Redistributions in binary form must reproduce the above copyright
1357718be8SEnji Cooper * notice, this list of conditions and the following disclaimer in the
1457718be8SEnji Cooper * documentation and/or other materials provided with the distribution.
1557718be8SEnji Cooper *
1657718be8SEnji Cooper * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
1757718be8SEnji Cooper * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
1857718be8SEnji Cooper * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
1957718be8SEnji Cooper * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
2057718be8SEnji Cooper * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
2157718be8SEnji Cooper * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
2257718be8SEnji Cooper * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
2357718be8SEnji Cooper * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
2457718be8SEnji Cooper * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
2557718be8SEnji Cooper * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
2657718be8SEnji Cooper * POSSIBILITY OF SUCH DAMAGE.
2757718be8SEnji Cooper */
2857718be8SEnji Cooper
2957718be8SEnji Cooper /*
3057718be8SEnji Cooper * Copyright (c) 1994 Christopher G. Demetriou
3157718be8SEnji Cooper * All rights reserved.
3257718be8SEnji Cooper *
3357718be8SEnji Cooper * Redistribution and use in source and binary forms, with or without
3457718be8SEnji Cooper * modification, are permitted provided that the following conditions
3557718be8SEnji Cooper * are met:
3657718be8SEnji Cooper * 1. Redistributions of source code must retain the above copyright
3757718be8SEnji Cooper * notice, this list of conditions and the following disclaimer.
3857718be8SEnji Cooper * 2. Redistributions in binary form must reproduce the above copyright
3957718be8SEnji Cooper * notice, this list of conditions and the following disclaimer in the
4057718be8SEnji Cooper * documentation and/or other materials provided with the distribution.
4157718be8SEnji Cooper * 3. All advertising materials mentioning features or use of this software
4257718be8SEnji Cooper * must display the following acknowledgement:
4357718be8SEnji Cooper * This product includes software developed for the
4457718be8SEnji Cooper * NetBSD Project. See http://www.NetBSD.org/ for
4557718be8SEnji Cooper * information about NetBSD.
4657718be8SEnji Cooper * 4. The name of the author may not be used to endorse or promote products
4757718be8SEnji Cooper * derived from this software without specific prior written permission.
4857718be8SEnji Cooper *
4957718be8SEnji Cooper * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
5057718be8SEnji Cooper * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
5157718be8SEnji Cooper * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
5257718be8SEnji Cooper * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
5357718be8SEnji Cooper * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
5457718be8SEnji Cooper * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
5557718be8SEnji Cooper * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
5657718be8SEnji Cooper * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
5757718be8SEnji Cooper * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
5857718be8SEnji Cooper * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
5957718be8SEnji Cooper *
6057718be8SEnji Cooper * <<Id: LICENSE,v 1.2 2000/06/14 15:57:33 cgd Exp>>
6157718be8SEnji Cooper */
6257718be8SEnji Cooper
6357718be8SEnji Cooper #include <sys/cdefs.h>
6457718be8SEnji Cooper __COPYRIGHT("@(#) Copyright (c) 2008\
6557718be8SEnji Cooper The NetBSD Foundation, inc. All rights reserved.");
66f40f3adcSEnji Cooper __RCSID("$NetBSD: t_setjmp.c,v 1.2 2017/01/14 21:08:17 christos Exp $");
6757718be8SEnji Cooper
6857718be8SEnji Cooper #include <sys/types.h>
6957718be8SEnji Cooper
7057718be8SEnji Cooper #include <errno.h>
7157718be8SEnji Cooper #include <setjmp.h>
7257718be8SEnji Cooper #include <signal.h>
73*9fb118beSJessica Clarke #include <stdbool.h>
7457718be8SEnji Cooper #include <stdio.h>
7557718be8SEnji Cooper #include <stdlib.h>
7657718be8SEnji Cooper #include <string.h>
7757718be8SEnji Cooper #include <unistd.h>
7857718be8SEnji Cooper
7957718be8SEnji Cooper #include <atf-c.h>
8057718be8SEnji Cooper
8157718be8SEnji Cooper #define REQUIRE_ERRNO(x) ATF_REQUIRE_MSG(x, "%s", strerror(errno))
8257718be8SEnji Cooper
8357718be8SEnji Cooper #define TEST_SETJMP 0
8457718be8SEnji Cooper #define TEST_U_SETJMP 1
8557718be8SEnji Cooper #define TEST_SIGSETJMP_SAVE 2
8657718be8SEnji Cooper #define TEST_SIGSETJMP_NOSAVE 3
87*9fb118beSJessica Clarke #define TEST_LONGJMP_ZERO 4
88*9fb118beSJessica Clarke #define TEST_U_LONGJMP_ZERO 5
8957718be8SEnji Cooper
9057718be8SEnji Cooper static int expectsignal;
9157718be8SEnji Cooper
9257718be8SEnji Cooper static void
aborthandler(int signo __unused)9330924962SBryan Drewery aborthandler(int signo __unused)
9457718be8SEnji Cooper {
9557718be8SEnji Cooper ATF_REQUIRE_MSG(expectsignal, "kill(SIGABRT) succeeded");
9657718be8SEnji Cooper atf_tc_pass();
9757718be8SEnji Cooper }
9857718be8SEnji Cooper
9957718be8SEnji Cooper static void
h_check(int test)10057718be8SEnji Cooper h_check(int test)
10157718be8SEnji Cooper {
10257718be8SEnji Cooper struct sigaction sa;
10357718be8SEnji Cooper jmp_buf jb;
10457718be8SEnji Cooper sigjmp_buf sjb;
10557718be8SEnji Cooper sigset_t ss;
10657718be8SEnji Cooper int i, x;
107*9fb118beSJessica Clarke volatile bool did_longjmp;
10857718be8SEnji Cooper
10957718be8SEnji Cooper i = getpid();
110*9fb118beSJessica Clarke did_longjmp = false;
11157718be8SEnji Cooper
112*9fb118beSJessica Clarke if (test == TEST_SETJMP || test == TEST_SIGSETJMP_SAVE ||
113*9fb118beSJessica Clarke test == TEST_LONGJMP_ZERO)
11457718be8SEnji Cooper expectsignal = 0;
115*9fb118beSJessica Clarke else if (test == TEST_U_SETJMP || test == TEST_SIGSETJMP_NOSAVE ||
116*9fb118beSJessica Clarke test == TEST_U_LONGJMP_ZERO)
11757718be8SEnji Cooper expectsignal = 1;
11857718be8SEnji Cooper else
11957718be8SEnji Cooper atf_tc_fail("unknown test");
12057718be8SEnji Cooper
12157718be8SEnji Cooper sa.sa_handler = aborthandler;
12257718be8SEnji Cooper sigemptyset(&sa.sa_mask);
12357718be8SEnji Cooper sa.sa_flags = 0;
12457718be8SEnji Cooper REQUIRE_ERRNO(sigaction(SIGABRT, &sa, NULL) != -1);
12557718be8SEnji Cooper REQUIRE_ERRNO(sigemptyset(&ss) != -1);
12657718be8SEnji Cooper REQUIRE_ERRNO(sigaddset(&ss, SIGABRT) != -1);
12757718be8SEnji Cooper REQUIRE_ERRNO(sigprocmask(SIG_BLOCK, &ss, NULL) != -1);
12857718be8SEnji Cooper
129*9fb118beSJessica Clarke if (test == TEST_SETJMP || test == TEST_LONGJMP_ZERO)
13057718be8SEnji Cooper x = setjmp(jb);
131*9fb118beSJessica Clarke else if (test == TEST_U_SETJMP || test == TEST_U_LONGJMP_ZERO)
13257718be8SEnji Cooper x = _setjmp(jb);
13357718be8SEnji Cooper else
13457718be8SEnji Cooper x = sigsetjmp(sjb, !expectsignal);
13557718be8SEnji Cooper
13657718be8SEnji Cooper if (x != 0) {
137*9fb118beSJessica Clarke if (test == TEST_LONGJMP_ZERO || test == TEST_U_LONGJMP_ZERO)
138*9fb118beSJessica Clarke ATF_REQUIRE_MSG(x == 1, "setjmp returned wrong value");
139*9fb118beSJessica Clarke else
14057718be8SEnji Cooper ATF_REQUIRE_MSG(x == i, "setjmp returned wrong value");
141*9fb118beSJessica Clarke
14257718be8SEnji Cooper kill(i, SIGABRT);
14357718be8SEnji Cooper ATF_REQUIRE_MSG(!expectsignal, "kill(SIGABRT) failed");
14457718be8SEnji Cooper atf_tc_pass();
145*9fb118beSJessica Clarke } else if (did_longjmp) {
146*9fb118beSJessica Clarke atf_tc_fail("setjmp returned zero after longjmp");
14757718be8SEnji Cooper }
14857718be8SEnji Cooper
14957718be8SEnji Cooper REQUIRE_ERRNO(sigprocmask(SIG_UNBLOCK, &ss, NULL) != -1);
15057718be8SEnji Cooper
151*9fb118beSJessica Clarke did_longjmp = true;
15257718be8SEnji Cooper if (test == TEST_SETJMP)
15357718be8SEnji Cooper longjmp(jb, i);
154*9fb118beSJessica Clarke else if (test == TEST_LONGJMP_ZERO)
155*9fb118beSJessica Clarke longjmp(jb, 0);
15657718be8SEnji Cooper else if (test == TEST_U_SETJMP)
15757718be8SEnji Cooper _longjmp(jb, i);
158*9fb118beSJessica Clarke else if (test == TEST_U_LONGJMP_ZERO)
159*9fb118beSJessica Clarke _longjmp(jb, 0);
16057718be8SEnji Cooper else
16157718be8SEnji Cooper siglongjmp(sjb, i);
16257718be8SEnji Cooper
16357718be8SEnji Cooper atf_tc_fail("jmp failed");
16457718be8SEnji Cooper }
16557718be8SEnji Cooper
16657718be8SEnji Cooper ATF_TC(setjmp);
ATF_TC_HEAD(setjmp,tc)16757718be8SEnji Cooper ATF_TC_HEAD(setjmp, tc)
16857718be8SEnji Cooper {
16957718be8SEnji Cooper atf_tc_set_md_var(tc, "descr", "Checks setjmp(3)");
17057718be8SEnji Cooper }
ATF_TC_BODY(setjmp,tc)17157718be8SEnji Cooper ATF_TC_BODY(setjmp, tc)
17257718be8SEnji Cooper {
17357718be8SEnji Cooper h_check(TEST_SETJMP);
17457718be8SEnji Cooper }
17557718be8SEnji Cooper
17657718be8SEnji Cooper ATF_TC(_setjmp);
ATF_TC_HEAD(_setjmp,tc)17757718be8SEnji Cooper ATF_TC_HEAD(_setjmp, tc)
17857718be8SEnji Cooper {
17957718be8SEnji Cooper atf_tc_set_md_var(tc, "descr", "Checks _setjmp(3)");
18057718be8SEnji Cooper }
ATF_TC_BODY(_setjmp,tc)18157718be8SEnji Cooper ATF_TC_BODY(_setjmp, tc)
18257718be8SEnji Cooper {
18357718be8SEnji Cooper h_check(TEST_U_SETJMP);
18457718be8SEnji Cooper }
18557718be8SEnji Cooper
18657718be8SEnji Cooper ATF_TC(sigsetjmp_save);
ATF_TC_HEAD(sigsetjmp_save,tc)18757718be8SEnji Cooper ATF_TC_HEAD(sigsetjmp_save, tc)
18857718be8SEnji Cooper {
18957718be8SEnji Cooper atf_tc_set_md_var(tc, "descr", "Checks sigsetjmp(3) with savemask enabled");
19057718be8SEnji Cooper }
ATF_TC_BODY(sigsetjmp_save,tc)19157718be8SEnji Cooper ATF_TC_BODY(sigsetjmp_save, tc)
19257718be8SEnji Cooper {
19357718be8SEnji Cooper h_check(TEST_SIGSETJMP_SAVE);
19457718be8SEnji Cooper }
19557718be8SEnji Cooper
19657718be8SEnji Cooper ATF_TC(sigsetjmp_nosave);
ATF_TC_HEAD(sigsetjmp_nosave,tc)19757718be8SEnji Cooper ATF_TC_HEAD(sigsetjmp_nosave, tc)
19857718be8SEnji Cooper {
19957718be8SEnji Cooper atf_tc_set_md_var(tc, "descr", "Checks sigsetjmp(3) with savemask disabled");
20057718be8SEnji Cooper }
ATF_TC_BODY(sigsetjmp_nosave,tc)20157718be8SEnji Cooper ATF_TC_BODY(sigsetjmp_nosave, tc)
20257718be8SEnji Cooper {
20357718be8SEnji Cooper h_check(TEST_SIGSETJMP_NOSAVE);
20457718be8SEnji Cooper }
20557718be8SEnji Cooper
206*9fb118beSJessica Clarke ATF_TC(longjmp_zero);
ATF_TC_HEAD(longjmp_zero,tc)207*9fb118beSJessica Clarke ATF_TC_HEAD(longjmp_zero, tc)
208*9fb118beSJessica Clarke {
209*9fb118beSJessica Clarke atf_tc_set_md_var(tc, "descr", "Checks longjmp(3) with a zero value");
210*9fb118beSJessica Clarke }
ATF_TC_BODY(longjmp_zero,tc)211*9fb118beSJessica Clarke ATF_TC_BODY(longjmp_zero, tc)
212*9fb118beSJessica Clarke {
213*9fb118beSJessica Clarke h_check(TEST_LONGJMP_ZERO);
214*9fb118beSJessica Clarke }
215*9fb118beSJessica Clarke
216*9fb118beSJessica Clarke ATF_TC(_longjmp_zero);
ATF_TC_HEAD(_longjmp_zero,tc)217*9fb118beSJessica Clarke ATF_TC_HEAD(_longjmp_zero, tc)
218*9fb118beSJessica Clarke {
219*9fb118beSJessica Clarke atf_tc_set_md_var(tc, "descr", "Checks _longjmp(3) with a zero value");
220*9fb118beSJessica Clarke }
ATF_TC_BODY(_longjmp_zero,tc)221*9fb118beSJessica Clarke ATF_TC_BODY(_longjmp_zero, tc)
222*9fb118beSJessica Clarke {
223*9fb118beSJessica Clarke h_check(TEST_U_LONGJMP_ZERO);
224*9fb118beSJessica Clarke }
225*9fb118beSJessica Clarke
ATF_TP_ADD_TCS(tp)22657718be8SEnji Cooper ATF_TP_ADD_TCS(tp)
22757718be8SEnji Cooper {
22857718be8SEnji Cooper ATF_TP_ADD_TC(tp, setjmp);
22957718be8SEnji Cooper ATF_TP_ADD_TC(tp, _setjmp);
23057718be8SEnji Cooper ATF_TP_ADD_TC(tp, sigsetjmp_save);
23157718be8SEnji Cooper ATF_TP_ADD_TC(tp, sigsetjmp_nosave);
232*9fb118beSJessica Clarke ATF_TP_ADD_TC(tp, longjmp_zero);
233*9fb118beSJessica Clarke ATF_TP_ADD_TC(tp, _longjmp_zero);
23457718be8SEnji Cooper
23557718be8SEnji Cooper return atf_no_error();
23657718be8SEnji Cooper }
237