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