t_siginfo.c revision 1.2 1 1.1 pgoyette /* $NetBSD */
2 1.1 pgoyette
3 1.1 pgoyette /*-
4 1.1 pgoyette * Copyright (c) 2010 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 #include <atf-c.h>
30 1.1 pgoyette
31 1.2 pgoyette #include <sys/inttypes.h>
32 1.1 pgoyette #include <sys/resource.h>
33 1.1 pgoyette #include <sys/time.h>
34 1.1 pgoyette #include <sys/ucontext.h>
35 1.1 pgoyette #include <sys/wait.h>
36 1.1 pgoyette
37 1.1 pgoyette #include <assert.h>
38 1.1 pgoyette #include <signal.h>
39 1.1 pgoyette #include <stdio.h>
40 1.1 pgoyette #include <stdlib.h>
41 1.1 pgoyette #include <unistd.h>
42 1.1 pgoyette
43 1.1 pgoyette #ifndef __vax__
44 1.1 pgoyette #include <ieeefp.h>
45 1.1 pgoyette #endif
46 1.1 pgoyette
47 1.1 pgoyette /* for sigchild */
48 1.1 pgoyette pid_t child;
49 1.1 pgoyette int code;
50 1.1 pgoyette int status;
51 1.1 pgoyette
52 1.1 pgoyette /* for sigfpe */
53 1.1 pgoyette sig_atomic_t fltdiv_signalled = 0;
54 1.1 pgoyette sig_atomic_t intdiv_signalled = 0;
55 1.1 pgoyette
56 1.1 pgoyette static void
57 1.1 pgoyette sig_debug(int signo, siginfo_t *info, ucontext_t *ctx)
58 1.1 pgoyette {
59 1.1 pgoyette unsigned int i;
60 1.1 pgoyette
61 1.1 pgoyette printf("%d %p %p\n", signo, info, ctx);
62 1.1 pgoyette if (info != NULL) {
63 1.1 pgoyette printf("si_signo=%d\n", info->si_signo);
64 1.1 pgoyette printf("si_errno=%d\n", info->si_errno);
65 1.1 pgoyette printf("si_code=%d\n", info->si_code);
66 1.1 pgoyette printf("si_value.sival_int=%d\n", info->si_value.sival_int);
67 1.1 pgoyette }
68 1.1 pgoyette if (ctx != NULL) {
69 1.1 pgoyette printf("uc_flags 0x%x\n", ctx->uc_flags);
70 1.1 pgoyette printf("uc_link %p\n", ctx->uc_link);
71 1.1 pgoyette for (i = 0; i < __arraycount(ctx->uc_sigmask.__bits); i++)
72 1.1 pgoyette printf("uc_sigmask[%d] 0x%x\n", i,
73 1.1 pgoyette ctx->uc_sigmask.__bits[i]);
74 1.1 pgoyette printf("uc_stack %p %lu 0x%x\n", ctx->uc_stack.ss_sp,
75 1.1 pgoyette (unsigned long)ctx->uc_stack.ss_size,
76 1.1 pgoyette ctx->uc_stack.ss_flags);
77 1.2 pgoyette /*
78 1.2 pgoyette * XXX Don't try to print MD __gregs since we don't
79 1.2 pgoyette * XXX know what format to use
80 1.2 pgoyette *
81 1.1 pgoyette for (i = 0; i < __arraycount(mc->__gregs); i++)
82 1.2 pgoyette printf("uc_mcontext.greg[%d] 0x%x\n", i,
83 1.2 pgoyette uc->uc_mcontext.__gregs[i]);
84 1.2 pgoyette */
85 1.1 pgoyette }
86 1.1 pgoyette }
87 1.1 pgoyette
88 1.1 pgoyette static void
89 1.1 pgoyette sigalrm_action(int signo, siginfo_t *info, void *ptr)
90 1.1 pgoyette {
91 1.1 pgoyette
92 1.1 pgoyette sig_debug(signo, info, (ucontext_t *)ptr);
93 1.1 pgoyette
94 1.1 pgoyette ATF_REQUIRE_EQ(info->si_signo, SIGALRM);
95 1.1 pgoyette ATF_REQUIRE_EQ(info->si_code, SI_TIMER);
96 1.1 pgoyette ATF_REQUIRE_EQ(info->si_value.sival_int, ITIMER_REAL);
97 1.1 pgoyette
98 1.1 pgoyette atf_tc_pass();
99 1.1 pgoyette /* NOTREACHED */
100 1.1 pgoyette }
101 1.1 pgoyette
102 1.1 pgoyette ATF_TC(sigalarm);
103 1.1 pgoyette
104 1.1 pgoyette ATF_TC_HEAD(sigalarm, tc)
105 1.1 pgoyette {
106 1.1 pgoyette
107 1.1 pgoyette atf_tc_set_md_var(tc, "descr",
108 1.1 pgoyette "Checks that signal trampoline correctly calls SIGALRM handler");
109 1.1 pgoyette }
110 1.1 pgoyette
111 1.1 pgoyette ATF_TC_BODY(sigalarm, tc)
112 1.1 pgoyette {
113 1.1 pgoyette struct sigaction sa;
114 1.1 pgoyette sa.sa_flags = SA_SIGINFO;
115 1.1 pgoyette sa.sa_sigaction = sigalrm_action;
116 1.1 pgoyette sigemptyset(&sa.sa_mask);
117 1.1 pgoyette sigaction(SIGALRM, &sa, NULL);
118 1.1 pgoyette for (;;) {
119 1.1 pgoyette alarm(1);
120 1.1 pgoyette sleep(1);
121 1.1 pgoyette }
122 1.1 pgoyette atf_tc_fail("SIGALRM handler wasn't called");
123 1.1 pgoyette }
124 1.1 pgoyette
125 1.1 pgoyette static void
126 1.1 pgoyette sigchild_action(int signo, siginfo_t *info, void *ptr)
127 1.1 pgoyette {
128 1.1 pgoyette if (info != NULL) {
129 1.1 pgoyette printf("info=%p\n", info);
130 1.1 pgoyette printf("ptr=%p\n", ptr);
131 1.1 pgoyette printf("si_signo=%d\n", info->si_signo);
132 1.1 pgoyette printf("si_errno=%d\n", info->si_errno);
133 1.1 pgoyette printf("si_code=%d\n", info->si_code);
134 1.1 pgoyette printf("si_uid=%d\n", info->si_uid);
135 1.1 pgoyette printf("si_pid=%d\n", info->si_pid);
136 1.1 pgoyette printf("si_status=%d\n", info->si_status);
137 1.2 pgoyette /*
138 1.2 pgoyette * XXX don't print these until we figure out the
139 1.2 pgoyette * XXX correct machine-independant format specifier
140 1.2 pgoyette *
141 1.2 pgoyette printf("si_utime=%u\n", info->si_utime);
142 1.2 pgoyette printf("si_stime=%u\n", info->si_stime);
143 1.2 pgoyette */
144 1.1 pgoyette }
145 1.1 pgoyette ATF_REQUIRE_EQ(info->si_code, code);
146 1.1 pgoyette ATF_REQUIRE_EQ(info->si_signo, SIGCHLD);
147 1.1 pgoyette ATF_REQUIRE_EQ(info->si_uid, getuid());
148 1.1 pgoyette ATF_REQUIRE_EQ(info->si_pid, child);
149 1.1 pgoyette if (WIFEXITED(info->si_status))
150 1.1 pgoyette ATF_REQUIRE_EQ(WEXITSTATUS(info->si_status), status);
151 1.1 pgoyette else if (WIFSTOPPED(info->si_status))
152 1.1 pgoyette ATF_REQUIRE_EQ(WSTOPSIG(info->si_status), status);
153 1.1 pgoyette else if (WIFSIGNALED(info->si_status))
154 1.1 pgoyette ATF_REQUIRE_EQ(WTERMSIG(info->si_status), status);
155 1.1 pgoyette }
156 1.1 pgoyette
157 1.1 pgoyette static void
158 1.1 pgoyette setchildhandler(void (*action)(int, siginfo_t *, void *))
159 1.1 pgoyette {
160 1.1 pgoyette struct sigaction sa;
161 1.1 pgoyette sa.sa_flags = SA_SIGINFO;
162 1.1 pgoyette sa.sa_sigaction = action;
163 1.1 pgoyette sigemptyset(&sa.sa_mask);
164 1.1 pgoyette sigaction(SIGCHLD, &sa, NULL);
165 1.1 pgoyette }
166 1.1 pgoyette
167 1.1 pgoyette static void
168 1.1 pgoyette sigchild_setup(void)
169 1.1 pgoyette {
170 1.1 pgoyette sigset_t set;
171 1.1 pgoyette struct rlimit rlim;
172 1.1 pgoyette
173 1.1 pgoyette (void)getrlimit(RLIMIT_CORE, &rlim);
174 1.1 pgoyette rlim.rlim_cur = rlim.rlim_max;
175 1.1 pgoyette (void)setrlimit(RLIMIT_CORE, &rlim);
176 1.1 pgoyette
177 1.1 pgoyette setchildhandler(sigchild_action);
178 1.1 pgoyette sigemptyset(&set);
179 1.1 pgoyette sigaddset(&set, SIGCHLD);
180 1.1 pgoyette sigprocmask(SIG_BLOCK, &set, NULL);
181 1.1 pgoyette }
182 1.1 pgoyette
183 1.1 pgoyette ATF_TC(sigchild_normal);
184 1.1 pgoyette ATF_TC_HEAD(sigchild_normal, tc)
185 1.1 pgoyette {
186 1.1 pgoyette
187 1.1 pgoyette atf_tc_set_md_var(tc, "descr",
188 1.1 pgoyette "Checks that signal trampoline correctly calls SIGCHLD handler "
189 1.1 pgoyette "when child exits normally");
190 1.1 pgoyette }
191 1.1 pgoyette
192 1.1 pgoyette ATF_TC_BODY(sigchild_normal, tc)
193 1.1 pgoyette {
194 1.1 pgoyette sigset_t set;
195 1.1 pgoyette
196 1.1 pgoyette sigchild_setup();
197 1.1 pgoyette
198 1.1 pgoyette status = 25;
199 1.1 pgoyette code = CLD_EXITED;
200 1.1 pgoyette
201 1.1 pgoyette switch ((child = fork())) {
202 1.1 pgoyette case 0:
203 1.1 pgoyette sleep(1);
204 1.1 pgoyette exit(status);
205 1.1 pgoyette case -1:
206 1.1 pgoyette atf_tc_fail("fork failed");
207 1.1 pgoyette default:
208 1.1 pgoyette sigemptyset(&set);
209 1.1 pgoyette sigsuspend(&set);
210 1.1 pgoyette }
211 1.1 pgoyette }
212 1.1 pgoyette
213 1.1 pgoyette ATF_TC(sigchild_dump);
214 1.1 pgoyette ATF_TC_HEAD(sigchild_dump, tc)
215 1.1 pgoyette {
216 1.1 pgoyette
217 1.1 pgoyette atf_tc_set_md_var(tc, "descr",
218 1.1 pgoyette "Checks that signal trampoline correctly calls SIGCHLD handler "
219 1.1 pgoyette "when child segfaults");
220 1.1 pgoyette }
221 1.1 pgoyette
222 1.1 pgoyette ATF_TC_BODY(sigchild_dump, tc)
223 1.1 pgoyette {
224 1.1 pgoyette sigset_t set;
225 1.1 pgoyette
226 1.1 pgoyette sigchild_setup();
227 1.1 pgoyette
228 1.1 pgoyette status = SIGSEGV;
229 1.1 pgoyette code = CLD_DUMPED;
230 1.1 pgoyette
231 1.1 pgoyette switch ((child = fork())) {
232 1.1 pgoyette case 0:
233 1.1 pgoyette sleep(1);
234 1.1 pgoyette *(long *)0 = 0;
235 1.1 pgoyette atf_tc_fail("Child did not segfault");
236 1.1 pgoyette /* NOTREACHED */
237 1.1 pgoyette case -1:
238 1.1 pgoyette atf_tc_fail("fork failed");
239 1.1 pgoyette default:
240 1.1 pgoyette sigemptyset(&set);
241 1.1 pgoyette sigsuspend(&set);
242 1.1 pgoyette }
243 1.1 pgoyette }
244 1.1 pgoyette
245 1.1 pgoyette ATF_TC(sigchild_kill);
246 1.1 pgoyette ATF_TC_HEAD(sigchild_kill, tc)
247 1.1 pgoyette {
248 1.1 pgoyette
249 1.1 pgoyette atf_tc_set_md_var(tc, "descr",
250 1.1 pgoyette "Checks that signal trampoline correctly calls SIGCHLD handler "
251 1.1 pgoyette "when child is killed");
252 1.1 pgoyette }
253 1.1 pgoyette
254 1.1 pgoyette ATF_TC_BODY(sigchild_kill, tc)
255 1.1 pgoyette {
256 1.1 pgoyette sigset_t set;
257 1.1 pgoyette
258 1.1 pgoyette sigchild_setup();
259 1.1 pgoyette
260 1.1 pgoyette status = SIGPIPE;
261 1.1 pgoyette code = CLD_KILLED;
262 1.1 pgoyette
263 1.1 pgoyette switch ((child = fork())) {
264 1.1 pgoyette case 0:
265 1.1 pgoyette sigemptyset(&set);
266 1.1 pgoyette sigsuspend(&set);
267 1.1 pgoyette break;
268 1.1 pgoyette case -1:
269 1.1 pgoyette atf_tc_fail("fork failed");
270 1.1 pgoyette default:
271 1.1 pgoyette kill(child, SIGPIPE);
272 1.1 pgoyette sigemptyset(&set);
273 1.1 pgoyette sigsuspend(&set);
274 1.1 pgoyette }
275 1.1 pgoyette }
276 1.1 pgoyette
277 1.1 pgoyette static void
278 1.1 pgoyette sigfpe_flt_action(int signo, siginfo_t *info, void *ptr)
279 1.1 pgoyette {
280 1.1 pgoyette
281 1.1 pgoyette sig_debug(signo, info, (ucontext_t *)ptr);
282 1.1 pgoyette
283 1.1 pgoyette if (fltdiv_signalled++ != 0)
284 1.1 pgoyette atf_tc_fail("FPE handler called more than once");
285 1.1 pgoyette
286 1.1 pgoyette ATF_REQUIRE_EQ(info->si_signo, SIGFPE);
287 1.1 pgoyette ATF_REQUIRE_EQ(info->si_code, FPE_FLTDIV);
288 1.1 pgoyette ATF_REQUIRE_EQ(info->si_errno, 0);
289 1.1 pgoyette }
290 1.1 pgoyette
291 1.1 pgoyette ATF_TC(sigfpe_flt);
292 1.1 pgoyette ATF_TC_HEAD(sigfpe_flt, tc)
293 1.1 pgoyette {
294 1.1 pgoyette
295 1.1 pgoyette atf_tc_set_md_var(tc, "descr",
296 1.1 pgoyette "Checks that signal trampoline correctly calls SIGFPE handler "
297 1.1 pgoyette "for floating div-by-zero");
298 1.1 pgoyette }
299 1.1 pgoyette
300 1.1 pgoyette ATF_TC_BODY(sigfpe_flt, tc)
301 1.1 pgoyette {
302 1.1 pgoyette struct sigaction sa;
303 1.1 pgoyette double d = strtod("0", NULL);
304 1.1 pgoyette
305 1.1 pgoyette sa.sa_flags = SA_SIGINFO;
306 1.1 pgoyette sa.sa_sigaction = sigfpe_flt_action;
307 1.1 pgoyette sigemptyset(&sa.sa_mask);
308 1.1 pgoyette sigaction(SIGFPE, &sa, NULL);
309 1.1 pgoyette #ifndef __vax__
310 1.1 pgoyette fpsetmask(FP_X_INV|FP_X_DZ|FP_X_OFL|FP_X_UFL|FP_X_IMP);
311 1.1 pgoyette #endif
312 1.1 pgoyette printf("%g\n", 1 / d);
313 1.1 pgoyette if (fltdiv_signalled == 0)
314 1.1 pgoyette atf_tc_fail("FPE signal handler was not invoked");
315 1.1 pgoyette }
316 1.1 pgoyette
317 1.1 pgoyette static void
318 1.1 pgoyette sigfpe_int_action(int signo, siginfo_t *info, void *ptr)
319 1.1 pgoyette {
320 1.1 pgoyette
321 1.1 pgoyette sig_debug(signo, info, (ucontext_t *)ptr);
322 1.1 pgoyette
323 1.1 pgoyette if (intdiv_signalled++ != 0)
324 1.1 pgoyette atf_tc_fail("INTDIV handler called more than once");
325 1.1 pgoyette
326 1.1 pgoyette ATF_REQUIRE_EQ(info->si_signo, SIGFPE);
327 1.1 pgoyette if (info->si_code == FPE_FLTDIV)
328 1.1 pgoyette atf_tc_expect_fail("PR port-i386/43655 : integer div-by-zero "
329 1.1 pgoyette "reports FPE_FLTDIV instead of FPE_INTDIV");
330 1.1 pgoyette ATF_REQUIRE_EQ(info->si_code, FPE_INTDIV);
331 1.1 pgoyette ATF_REQUIRE_EQ(info->si_errno, 0);
332 1.1 pgoyette }
333 1.1 pgoyette
334 1.1 pgoyette ATF_TC(sigfpe_int);
335 1.1 pgoyette ATF_TC_HEAD(sigfpe_int, tc)
336 1.1 pgoyette {
337 1.1 pgoyette
338 1.1 pgoyette atf_tc_set_md_var(tc, "descr",
339 1.1 pgoyette "Checks that signal trampoline correctly calls SIGFPE handler "
340 1.1 pgoyette "for integer div-by-zero");
341 1.1 pgoyette }
342 1.1 pgoyette
343 1.1 pgoyette ATF_TC_BODY(sigfpe_int, tc)
344 1.1 pgoyette {
345 1.1 pgoyette struct sigaction sa;
346 1.1 pgoyette double d = strtod("0", NULL);
347 1.1 pgoyette
348 1.1 pgoyette sa.sa_flags = SA_SIGINFO;
349 1.1 pgoyette sa.sa_sigaction = sigfpe_int_action;
350 1.1 pgoyette sigemptyset(&sa.sa_mask);
351 1.1 pgoyette sigaction(SIGFPE, &sa, NULL);
352 1.1 pgoyette #ifndef __vax__
353 1.1 pgoyette fpsetmask(FP_X_INV|FP_X_DZ|FP_X_OFL|FP_X_UFL|FP_X_IMP);
354 1.1 pgoyette #endif
355 1.1 pgoyette printf("%g\n", 1 / d);
356 1.1 pgoyette if (intdiv_signalled == 0)
357 1.1 pgoyette atf_tc_fail("FPE signal handler was not invoked");
358 1.1 pgoyette }
359 1.1 pgoyette
360 1.1 pgoyette static void
361 1.1 pgoyette sigsegv_action(int signo, siginfo_t *info, void *ptr)
362 1.1 pgoyette {
363 1.1 pgoyette
364 1.1 pgoyette sig_debug(signo, info, (ucontext_t *)ptr);
365 1.1 pgoyette
366 1.1 pgoyette ATF_REQUIRE_EQ(info->si_signo, SIGSEGV);
367 1.1 pgoyette ATF_REQUIRE_EQ(info->si_errno, 0);
368 1.1 pgoyette ATF_REQUIRE_EQ(info->si_code, SEGV_MAPERR);
369 1.1 pgoyette ATF_REQUIRE_EQ(info->si_addr, (void *)0);
370 1.1 pgoyette
371 1.1 pgoyette atf_tc_pass();
372 1.1 pgoyette /* NOTREACHED */
373 1.1 pgoyette }
374 1.1 pgoyette
375 1.1 pgoyette ATF_TC(sigsegv);
376 1.1 pgoyette ATF_TC_HEAD(sigsegv, tc)
377 1.1 pgoyette {
378 1.1 pgoyette
379 1.1 pgoyette atf_tc_set_md_var(tc, "descr",
380 1.1 pgoyette "Checks that signal trampoline correctly calls SIGSEGV handler");
381 1.1 pgoyette }
382 1.1 pgoyette
383 1.1 pgoyette ATF_TC_BODY(sigsegv, tc)
384 1.1 pgoyette {
385 1.1 pgoyette struct sigaction sa;
386 1.1 pgoyette
387 1.1 pgoyette sa.sa_flags = SA_SIGINFO;
388 1.1 pgoyette sa.sa_sigaction = sigsegv_action;
389 1.1 pgoyette sigemptyset(&sa.sa_mask);
390 1.1 pgoyette sigaction(SIGSEGV, &sa, NULL);
391 1.1 pgoyette
392 1.1 pgoyette *(long *)0 = 0;
393 1.1 pgoyette atf_tc_fail("Test did not fault as expected");
394 1.1 pgoyette }
395 1.1 pgoyette
396 1.1 pgoyette ATF_TP_ADD_TCS(tp)
397 1.1 pgoyette {
398 1.1 pgoyette
399 1.1 pgoyette ATF_TP_ADD_TC(tp, sigalarm);
400 1.1 pgoyette ATF_TP_ADD_TC(tp, sigchild_normal);
401 1.1 pgoyette ATF_TP_ADD_TC(tp, sigchild_dump);
402 1.1 pgoyette ATF_TP_ADD_TC(tp, sigchild_kill);
403 1.1 pgoyette ATF_TP_ADD_TC(tp, sigfpe_flt);
404 1.1 pgoyette ATF_TP_ADD_TC(tp, sigfpe_int);
405 1.1 pgoyette ATF_TP_ADD_TC(tp, sigsegv);
406 1.1 pgoyette
407 1.1 pgoyette return atf_no_error();
408 1.1 pgoyette }
409