1 /* $NetBSD: t_threadjmp.c,v 1.1 2011/04/21 18:58:20 martin Exp $ */
4 * Copyright (c) 2008 The NetBSD Foundation, Inc.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
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.
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.
30 * Copyright (c) 1994 Christopher G. Demetriou
31 * All rights reserved.
33 * Redistribution and use in source and binary forms, with or without
34 * modification, are permitted provided that the following conditions
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.
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.
60 * <<Id: LICENSE,v 1.2 2000/06/14 15:57:33 cgd Exp>>
63 #include <sys/cdefs.h>
64 __COPYRIGHT("@(#) Copyright (c) 2008\
65 The NetBSD Foundation, inc. All rights reserved.");
66 __RCSID("$NetBSD: t_threadjmp.c,v 1.1 2011/04/21 18:58:20 martin Exp $");
68 #include <sys/types.h>
82 #define REQUIRE_ERRNO(x) ATF_REQUIRE_MSG(x, "%s", strerror(errno))
85 #define TEST_U_SETJMP 1
86 #define TEST_SIGSETJMP_SAVE 2
87 #define TEST_SIGSETJMP_NOSAVE 3
89 static pthread_t myself
= NULL
;
91 static int expectsignal
;
94 aborthandler(int signo
)
96 ATF_REQUIRE(myself
== pthread_self());
97 ATF_REQUIRE_MSG(expectsignal
, "kill(SIGABRT) succeeded");
110 myself
= pthread_self();
113 if (test
== TEST_SETJMP
|| test
== TEST_SIGSETJMP_SAVE
)
115 else if (test
== TEST_U_SETJMP
|| test
== TEST_SIGSETJMP_NOSAVE
)
118 atf_tc_fail("unknown test");
120 sa
.sa_handler
= aborthandler
;
121 sigemptyset(&sa
.sa_mask
);
123 REQUIRE_ERRNO(sigaction(SIGABRT
, &sa
, NULL
) != -1);
124 REQUIRE_ERRNO(sigemptyset(&ss
) != -1);
125 REQUIRE_ERRNO(sigaddset(&ss
, SIGABRT
) != -1);
126 REQUIRE_ERRNO(sigprocmask(SIG_BLOCK
, &ss
, NULL
) != -1);
127 ATF_REQUIRE(myself
== pthread_self());
129 if (test
== TEST_SETJMP
)
131 else if (test
== TEST_U_SETJMP
)
134 x
= sigsetjmp(sjb
, !expectsignal
);
137 ATF_REQUIRE(myself
== pthread_self());
138 ATF_REQUIRE_MSG(x
== i
, "setjmp returned wrong value");
140 ATF_REQUIRE_MSG(!expectsignal
, "kill(SIGABRT) failed");
141 ATF_REQUIRE(myself
== pthread_self());
145 ATF_REQUIRE(myself
== pthread_self());
146 REQUIRE_ERRNO(sigprocmask(SIG_UNBLOCK
, &ss
, NULL
) != -1);
148 if (test
== TEST_SETJMP
)
150 else if (test
== TEST_U_SETJMP
)
155 atf_tc_fail("jmp failed");
159 ATF_TC_HEAD(setjmp
, tc
)
161 atf_tc_set_md_var(tc
, "descr",
162 "Checks pthread_self() and setjmp(3)");
164 ATF_TC_BODY(setjmp
, tc
)
166 h_check(TEST_SETJMP
);
170 ATF_TC_HEAD(_setjmp
, tc
)
172 atf_tc_set_md_var(tc
, "descr",
173 "Checks pthread_self() and _setjmp(3)");
175 ATF_TC_BODY(_setjmp
, tc
)
177 h_check(TEST_U_SETJMP
);
180 ATF_TC(sigsetjmp_save
);
181 ATF_TC_HEAD(sigsetjmp_save
, tc
)
183 atf_tc_set_md_var(tc
, "descr",
184 "Checks pthread_self() and sigsetjmp(3) with savemask enabled");
186 ATF_TC_BODY(sigsetjmp_save
, tc
)
188 h_check(TEST_SIGSETJMP_SAVE
);
191 ATF_TC(sigsetjmp_nosave
);
192 ATF_TC_HEAD(sigsetjmp_nosave
, tc
)
194 atf_tc_set_md_var(tc
, "descr",
195 "Checks pthread_self() and sigsetjmp(3) with savemask disabled");
197 ATF_TC_BODY(sigsetjmp_nosave
, tc
)
199 h_check(TEST_SIGSETJMP_NOSAVE
);
206 * These test cases try to verify setjmp and friends in a program
207 * linked with pthreads, and verify that pthread_self() stays
208 * consistent throughout the program. A setcontext() call invoked
209 * by *setjmp() might clobber the TLS special register used to
210 * implement pthread_self().
212 ATF_TP_ADD_TC(tp
, setjmp
);
213 ATF_TP_ADD_TC(tp
, _setjmp
);
214 ATF_TP_ADD_TC(tp
, sigsetjmp_save
);
215 ATF_TP_ADD_TC(tp
, sigsetjmp_nosave
);
217 return atf_no_error();