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