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