xref: /netbsd-src/tests/lib/libc/setjmp/t_threadjmp.c (revision 2162d236666fd84e7614980d24814e0065336fdd)
1*2162d236Schristos /* $NetBSD: t_threadjmp.c,v 1.2 2017/01/14 21:08:17 christos Exp $ */
284bd71a2Smartin 
384bd71a2Smartin /*-
484bd71a2Smartin  * Copyright (c) 2008 The NetBSD Foundation, Inc.
584bd71a2Smartin  * All rights reserved.
684bd71a2Smartin  *
784bd71a2Smartin  * Redistribution and use in source and binary forms, with or without
884bd71a2Smartin  * modification, are permitted provided that the following conditions
984bd71a2Smartin  * are met:
1084bd71a2Smartin  * 1. Redistributions of source code must retain the above copyright
1184bd71a2Smartin  *    notice, this list of conditions and the following disclaimer.
1284bd71a2Smartin  * 2. Redistributions in binary form must reproduce the above copyright
1384bd71a2Smartin  *    notice, this list of conditions and the following disclaimer in the
1484bd71a2Smartin  *    documentation and/or other materials provided with the distribution.
1584bd71a2Smartin  *
1684bd71a2Smartin  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
1784bd71a2Smartin  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
1884bd71a2Smartin  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
1984bd71a2Smartin  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
2084bd71a2Smartin  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
2184bd71a2Smartin  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
2284bd71a2Smartin  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
2384bd71a2Smartin  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
2484bd71a2Smartin  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
2584bd71a2Smartin  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
2684bd71a2Smartin  * POSSIBILITY OF SUCH DAMAGE.
2784bd71a2Smartin  */
2884bd71a2Smartin 
2984bd71a2Smartin /*
3084bd71a2Smartin  * Copyright (c) 1994 Christopher G. Demetriou
3184bd71a2Smartin  * All rights reserved.
3284bd71a2Smartin  *
3384bd71a2Smartin  * Redistribution and use in source and binary forms, with or without
3484bd71a2Smartin  * modification, are permitted provided that the following conditions
3584bd71a2Smartin  * are met:
3684bd71a2Smartin  * 1. Redistributions of source code must retain the above copyright
3784bd71a2Smartin  *    notice, this list of conditions and the following disclaimer.
3884bd71a2Smartin  * 2. Redistributions in binary form must reproduce the above copyright
3984bd71a2Smartin  *    notice, this list of conditions and the following disclaimer in the
4084bd71a2Smartin  *    documentation and/or other materials provided with the distribution.
4184bd71a2Smartin  * 3. All advertising materials mentioning features or use of this software
4284bd71a2Smartin  *    must display the following acknowledgement:
4384bd71a2Smartin  *          This product includes software developed for the
4484bd71a2Smartin  *          NetBSD Project.  See http://www.NetBSD.org/ for
4584bd71a2Smartin  *          information about NetBSD.
4684bd71a2Smartin  * 4. The name of the author may not be used to endorse or promote products
4784bd71a2Smartin  *    derived from this software without specific prior written permission.
4884bd71a2Smartin  *
4984bd71a2Smartin  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
5084bd71a2Smartin  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
5184bd71a2Smartin  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
5284bd71a2Smartin  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
5384bd71a2Smartin  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
5484bd71a2Smartin  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
5584bd71a2Smartin  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
5684bd71a2Smartin  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
5784bd71a2Smartin  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
5884bd71a2Smartin  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
5984bd71a2Smartin  *
6084bd71a2Smartin  * <<Id: LICENSE,v 1.2 2000/06/14 15:57:33 cgd Exp>>
6184bd71a2Smartin  */
6284bd71a2Smartin 
6384bd71a2Smartin #include <sys/cdefs.h>
6484bd71a2Smartin __COPYRIGHT("@(#) Copyright (c) 2008\
6584bd71a2Smartin  The NetBSD Foundation, inc. All rights reserved.");
66*2162d236Schristos __RCSID("$NetBSD: t_threadjmp.c,v 1.2 2017/01/14 21:08:17 christos Exp $");
6784bd71a2Smartin 
6884bd71a2Smartin #include <sys/types.h>
6984bd71a2Smartin 
7084bd71a2Smartin #include <errno.h>
7184bd71a2Smartin #include <setjmp.h>
7284bd71a2Smartin #include <signal.h>
7384bd71a2Smartin #include <stdio.h>
7484bd71a2Smartin #include <stdlib.h>
7584bd71a2Smartin #include <string.h>
7684bd71a2Smartin #include <unistd.h>
7784bd71a2Smartin 
7884bd71a2Smartin #include <pthread.h>
7984bd71a2Smartin 
8084bd71a2Smartin #include <atf-c.h>
8184bd71a2Smartin 
8284bd71a2Smartin #define REQUIRE_ERRNO(x) ATF_REQUIRE_MSG(x, "%s", strerror(errno))
8384bd71a2Smartin 
8484bd71a2Smartin #define TEST_SETJMP 0
8584bd71a2Smartin #define TEST_U_SETJMP 1
8684bd71a2Smartin #define TEST_SIGSETJMP_SAVE 2
8784bd71a2Smartin #define TEST_SIGSETJMP_NOSAVE 3
8884bd71a2Smartin 
8984bd71a2Smartin static pthread_t myself = NULL;
9084bd71a2Smartin 
9184bd71a2Smartin static int expectsignal;
9284bd71a2Smartin 
9384bd71a2Smartin static void
aborthandler(int signo __unused)94*2162d236Schristos aborthandler(int signo __unused)
9584bd71a2Smartin {
9684bd71a2Smartin 	ATF_REQUIRE(myself == pthread_self());
9784bd71a2Smartin 	ATF_REQUIRE_MSG(expectsignal, "kill(SIGABRT) succeeded");
9884bd71a2Smartin 	atf_tc_pass();
9984bd71a2Smartin }
10084bd71a2Smartin 
10184bd71a2Smartin static void
h_check(int test)10284bd71a2Smartin h_check(int test)
10384bd71a2Smartin {
10484bd71a2Smartin 	struct sigaction sa;
10584bd71a2Smartin 	jmp_buf jb;
10684bd71a2Smartin 	sigjmp_buf sjb;
10784bd71a2Smartin 	sigset_t ss;
10884bd71a2Smartin 	int i, x;
10984bd71a2Smartin 
11084bd71a2Smartin 	myself = pthread_self();
11184bd71a2Smartin 	i = getpid();
11284bd71a2Smartin 
11384bd71a2Smartin 	if (test == TEST_SETJMP || test == TEST_SIGSETJMP_SAVE)
11484bd71a2Smartin 		expectsignal = 0;
11584bd71a2Smartin 	else if (test == TEST_U_SETJMP || test == TEST_SIGSETJMP_NOSAVE)
11684bd71a2Smartin 		expectsignal = 1;
11784bd71a2Smartin 	else
11884bd71a2Smartin 		atf_tc_fail("unknown test");
11984bd71a2Smartin 
12084bd71a2Smartin 	sa.sa_handler = aborthandler;
12184bd71a2Smartin 	sigemptyset(&sa.sa_mask);
12284bd71a2Smartin 	sa.sa_flags = 0;
12384bd71a2Smartin 	REQUIRE_ERRNO(sigaction(SIGABRT, &sa, NULL) != -1);
12484bd71a2Smartin 	REQUIRE_ERRNO(sigemptyset(&ss) != -1);
12584bd71a2Smartin 	REQUIRE_ERRNO(sigaddset(&ss, SIGABRT) != -1);
12684bd71a2Smartin 	REQUIRE_ERRNO(sigprocmask(SIG_BLOCK, &ss, NULL) != -1);
12784bd71a2Smartin 	ATF_REQUIRE(myself == pthread_self());
12884bd71a2Smartin 
12984bd71a2Smartin 	if (test == TEST_SETJMP)
13084bd71a2Smartin 		x = setjmp(jb);
13184bd71a2Smartin 	else if (test == TEST_U_SETJMP)
13284bd71a2Smartin 		x = _setjmp(jb);
13384bd71a2Smartin 	else
13484bd71a2Smartin 		x = sigsetjmp(sjb, !expectsignal);
13584bd71a2Smartin 
13684bd71a2Smartin 	if (x != 0) {
13784bd71a2Smartin 		ATF_REQUIRE(myself == pthread_self());
13884bd71a2Smartin 		ATF_REQUIRE_MSG(x == i, "setjmp returned wrong value");
13984bd71a2Smartin 		kill(i, SIGABRT);
14084bd71a2Smartin 		ATF_REQUIRE_MSG(!expectsignal, "kill(SIGABRT) failed");
14184bd71a2Smartin 		ATF_REQUIRE(myself == pthread_self());
14284bd71a2Smartin 		atf_tc_pass();
14384bd71a2Smartin 	}
14484bd71a2Smartin 
14584bd71a2Smartin 	ATF_REQUIRE(myself == pthread_self());
14684bd71a2Smartin 	REQUIRE_ERRNO(sigprocmask(SIG_UNBLOCK, &ss, NULL) != -1);
14784bd71a2Smartin 
14884bd71a2Smartin 	if (test == TEST_SETJMP)
14984bd71a2Smartin 		longjmp(jb, i);
15084bd71a2Smartin 	else if (test == TEST_U_SETJMP)
15184bd71a2Smartin 		_longjmp(jb, i);
15284bd71a2Smartin 	else
15384bd71a2Smartin 		siglongjmp(sjb, i);
15484bd71a2Smartin 
15584bd71a2Smartin 	atf_tc_fail("jmp failed");
15684bd71a2Smartin }
15784bd71a2Smartin 
15884bd71a2Smartin ATF_TC(setjmp);
ATF_TC_HEAD(setjmp,tc)15984bd71a2Smartin ATF_TC_HEAD(setjmp, tc)
16084bd71a2Smartin {
16184bd71a2Smartin 	atf_tc_set_md_var(tc, "descr",
16284bd71a2Smartin 	    "Checks pthread_self() and setjmp(3)");
16384bd71a2Smartin }
ATF_TC_BODY(setjmp,tc)16484bd71a2Smartin ATF_TC_BODY(setjmp, tc)
16584bd71a2Smartin {
16684bd71a2Smartin 	h_check(TEST_SETJMP);
16784bd71a2Smartin }
16884bd71a2Smartin 
16984bd71a2Smartin ATF_TC(_setjmp);
ATF_TC_HEAD(_setjmp,tc)17084bd71a2Smartin ATF_TC_HEAD(_setjmp, tc)
17184bd71a2Smartin {
17284bd71a2Smartin 	atf_tc_set_md_var(tc, "descr",
17384bd71a2Smartin 	    "Checks pthread_self() and _setjmp(3)");
17484bd71a2Smartin }
ATF_TC_BODY(_setjmp,tc)17584bd71a2Smartin ATF_TC_BODY(_setjmp, tc)
17684bd71a2Smartin {
17784bd71a2Smartin 	h_check(TEST_U_SETJMP);
17884bd71a2Smartin }
17984bd71a2Smartin 
18084bd71a2Smartin ATF_TC(sigsetjmp_save);
ATF_TC_HEAD(sigsetjmp_save,tc)18184bd71a2Smartin ATF_TC_HEAD(sigsetjmp_save, tc)
18284bd71a2Smartin {
18384bd71a2Smartin 	atf_tc_set_md_var(tc, "descr",
18484bd71a2Smartin 	    "Checks pthread_self() and sigsetjmp(3) with savemask enabled");
18584bd71a2Smartin }
ATF_TC_BODY(sigsetjmp_save,tc)18684bd71a2Smartin ATF_TC_BODY(sigsetjmp_save, tc)
18784bd71a2Smartin {
18884bd71a2Smartin 	h_check(TEST_SIGSETJMP_SAVE);
18984bd71a2Smartin }
19084bd71a2Smartin 
19184bd71a2Smartin ATF_TC(sigsetjmp_nosave);
ATF_TC_HEAD(sigsetjmp_nosave,tc)19284bd71a2Smartin ATF_TC_HEAD(sigsetjmp_nosave, tc)
19384bd71a2Smartin {
19484bd71a2Smartin 	atf_tc_set_md_var(tc, "descr",
19584bd71a2Smartin 	    "Checks pthread_self() and sigsetjmp(3) with savemask disabled");
19684bd71a2Smartin }
ATF_TC_BODY(sigsetjmp_nosave,tc)19784bd71a2Smartin ATF_TC_BODY(sigsetjmp_nosave, tc)
19884bd71a2Smartin {
19984bd71a2Smartin 	h_check(TEST_SIGSETJMP_NOSAVE);
20084bd71a2Smartin }
20184bd71a2Smartin 
ATF_TP_ADD_TCS(tp)20284bd71a2Smartin ATF_TP_ADD_TCS(tp)
20384bd71a2Smartin {
20484bd71a2Smartin 
20584bd71a2Smartin 	/*
20684bd71a2Smartin 	 * These test cases try to verify setjmp and friends in a program
20784bd71a2Smartin 	 * linked with pthreads, and verify that pthread_self() stays
20884bd71a2Smartin 	 * consistent throughout the program. A setcontext() call invoked
20984bd71a2Smartin 	 * by *setjmp() might clobber the TLS special register used to
21084bd71a2Smartin 	 * implement pthread_self().
21184bd71a2Smartin 	 */
21284bd71a2Smartin 	ATF_TP_ADD_TC(tp, setjmp);
21384bd71a2Smartin 	ATF_TP_ADD_TC(tp, _setjmp);
21484bd71a2Smartin 	ATF_TP_ADD_TC(tp, sigsetjmp_save);
21584bd71a2Smartin 	ATF_TP_ADD_TC(tp, sigsetjmp_nosave);
21684bd71a2Smartin 
21784bd71a2Smartin 	return atf_no_error();
21884bd71a2Smartin }
219