xref: /netbsd-src/tests/lib/libc/gen/t_siginfo.c (revision 5bbd2a12505d72a8177929a37b5cee489d0a1cfd)
1 /* $NetBSD: t_siginfo.c,v 1.18 2012/06/13 11:45:17 njoly 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 #include <atf-c/config.h>
31 
32 #include <sys/inttypes.h>
33 #include <sys/resource.h>
34 #include <sys/sysctl.h>
35 #include <sys/time.h>
36 #include <sys/ucontext.h>
37 #include <sys/wait.h>
38 
39 #include <assert.h>
40 #include <signal.h>
41 #include <stdio.h>
42 #include <stdlib.h>
43 #include <string.h>
44 #include <unistd.h>
45 #include <setjmp.h>
46 #include <float.h>
47 
48 #ifdef _FLOAT_IEEE754
49 #include <ieeefp.h>
50 #endif
51 
52 /* for sigbus */
53 volatile char *addr;
54 
55 /* for sigchild */
56 pid_t child;
57 int code;
58 int status;
59 
60 /* for sigfpe */
61 sig_atomic_t fltdiv_signalled = 0;
62 sig_atomic_t intdiv_signalled = 0;
63 
64 static void
65 sig_debug(int signo, siginfo_t *info, ucontext_t *ctx)
66 {
67 	unsigned int i;
68 
69 	printf("%d %p %p\n", signo, info, ctx);
70 	if (info != NULL) {
71 		printf("si_signo=%d\n", info->si_signo);
72 		printf("si_errno=%d\n", info->si_errno);
73 		printf("si_code=%d\n", info->si_code);
74 		printf("si_value.sival_int=%d\n", info->si_value.sival_int);
75 	}
76 	if (ctx != NULL) {
77 		printf("uc_flags 0x%x\n", ctx->uc_flags);
78 		printf("uc_link %p\n", ctx->uc_link);
79 		for (i = 0; i < __arraycount(ctx->uc_sigmask.__bits); i++)
80 			printf("uc_sigmask[%d] 0x%x\n", i,
81 			    ctx->uc_sigmask.__bits[i]);
82 		printf("uc_stack %p %lu 0x%x\n", ctx->uc_stack.ss_sp,
83 		    (unsigned long)ctx->uc_stack.ss_size,
84 		    ctx->uc_stack.ss_flags);
85 		for (i = 0; i < __arraycount(ctx->uc_mcontext.__gregs); i++)
86 			printf("uc_mcontext.greg[%d] 0x%lx\n", i,
87 			    (long)ctx->uc_mcontext.__gregs[i]);
88 	}
89 }
90 
91 static void
92 sigalrm_action(int signo, siginfo_t *info, void *ptr)
93 {
94 
95 	sig_debug(signo, info, (ucontext_t *)ptr);
96 
97 	ATF_REQUIRE_EQ(info->si_signo, SIGALRM);
98 	ATF_REQUIRE_EQ(info->si_code, SI_TIMER);
99 	ATF_REQUIRE_EQ(info->si_value.sival_int, ITIMER_REAL);
100 
101 	atf_tc_pass();
102 	/* NOTREACHED */
103 }
104 
105 ATF_TC(sigalarm);
106 
107 ATF_TC_HEAD(sigalarm, tc)
108 {
109 
110 	atf_tc_set_md_var(tc, "descr",
111 	    "Checks that signal trampoline correctly calls SIGALRM handler");
112 }
113 
114 ATF_TC_BODY(sigalarm, tc)
115 {
116 	struct sigaction sa;
117 	sa.sa_flags = SA_SIGINFO;
118 	sa.sa_sigaction = sigalrm_action;
119 	sigemptyset(&sa.sa_mask);
120 	sigaction(SIGALRM, &sa, NULL);
121 	for (;;) {
122 		alarm(1);
123 		sleep(1);
124 	}
125 	atf_tc_fail("SIGALRM handler wasn't called");
126 }
127 
128 static void
129 sigchild_action(int signo, siginfo_t *info, void *ptr)
130 {
131 	if (info != NULL) {
132 		printf("info=%p\n", info);
133 		printf("ptr=%p\n", ptr);
134 		printf("si_signo=%d\n", info->si_signo);
135 		printf("si_errno=%d\n", info->si_errno);
136 		printf("si_code=%d\n", info->si_code);
137 		printf("si_uid=%d\n", info->si_uid);
138 		printf("si_pid=%d\n", info->si_pid);
139 		printf("si_status=%d\n", info->si_status);
140 		printf("si_utime=%lu\n", (unsigned long int)info->si_utime);
141 		printf("si_stime=%lu\n", (unsigned long int)info->si_stime);
142 	}
143 	ATF_REQUIRE_EQ(info->si_code, code);
144 	ATF_REQUIRE_EQ(info->si_signo, SIGCHLD);
145 	ATF_REQUIRE_EQ(info->si_uid, getuid());
146 	ATF_REQUIRE_EQ(info->si_pid, child);
147 	if (WIFEXITED(info->si_status))
148 		ATF_REQUIRE_EQ(WEXITSTATUS(info->si_status), status);
149 	else if (WIFSTOPPED(info->si_status))
150 		ATF_REQUIRE_EQ(WSTOPSIG(info->si_status), status);
151 	else if (WIFSIGNALED(info->si_status))
152 		ATF_REQUIRE_EQ(WTERMSIG(info->si_status), status);
153 }
154 
155 static void
156 setchildhandler(void (*action)(int, siginfo_t *, void *))
157 {
158 	struct sigaction sa;
159 	sa.sa_flags = SA_SIGINFO;
160 	sa.sa_sigaction = action;
161 	sigemptyset(&sa.sa_mask);
162 	sigaction(SIGCHLD, &sa, NULL);
163 }
164 
165 static void
166 sigchild_setup(void)
167 {
168 	sigset_t set;
169 	struct rlimit rlim;
170 
171 	(void)getrlimit(RLIMIT_CORE, &rlim);
172 	rlim.rlim_cur = rlim.rlim_max;
173 	(void)setrlimit(RLIMIT_CORE, &rlim);
174 
175 	setchildhandler(sigchild_action);
176 	sigemptyset(&set);
177 	sigaddset(&set, SIGCHLD);
178 	sigprocmask(SIG_BLOCK, &set, NULL);
179 }
180 
181 ATF_TC(sigchild_normal);
182 ATF_TC_HEAD(sigchild_normal, tc)
183 {
184 
185 	atf_tc_set_md_var(tc, "descr",
186 	    "Checks that signal trampoline correctly calls SIGCHLD handler "
187 	    "when child exits normally");
188 }
189 
190 ATF_TC_BODY(sigchild_normal, tc)
191 {
192 	sigset_t set;
193 
194 	sigchild_setup();
195 
196 	status = 25;
197 	code = CLD_EXITED;
198 
199 	switch ((child = fork())) {
200 	case 0:
201 		sleep(1);
202 		exit(status);
203 	case -1:
204 		atf_tc_fail("fork failed");
205 	default:
206 		sigemptyset(&set);
207 		sigsuspend(&set);
208 	}
209 }
210 
211 ATF_TC(sigchild_dump);
212 ATF_TC_HEAD(sigchild_dump, tc)
213 {
214 
215 	atf_tc_set_md_var(tc, "descr",
216 	    "Checks that signal trampoline correctly calls SIGCHLD handler "
217 	    "when child segfaults");
218 }
219 
220 ATF_TC_BODY(sigchild_dump, tc)
221 {
222 	sigset_t set;
223 
224 	sigchild_setup();
225 
226 	status = SIGSEGV;
227 	code = CLD_DUMPED;
228 
229 	switch ((child = fork())) {
230 	case 0:
231 		sleep(1);
232 		*(volatile long *)0 = 0;
233 		atf_tc_fail("Child did not segfault");
234 		/* NOTREACHED */
235 	case -1:
236 		atf_tc_fail("fork failed");
237 	default:
238 		sigemptyset(&set);
239 		sigsuspend(&set);
240 	}
241 }
242 
243 ATF_TC(sigchild_kill);
244 ATF_TC_HEAD(sigchild_kill, tc)
245 {
246 
247 	atf_tc_set_md_var(tc, "descr",
248 	    "Checks that signal trampoline correctly calls SIGCHLD handler "
249 	    "when child is killed");
250 }
251 
252 ATF_TC_BODY(sigchild_kill, tc)
253 {
254 	sigset_t set;
255 
256 	sigchild_setup();
257 
258 	status = SIGPIPE;
259 	code = CLD_KILLED;
260 
261 	switch ((child = fork())) {
262 	case 0:
263 		sigemptyset(&set);
264 		sigsuspend(&set);
265 		break;
266 	case -1:
267 		atf_tc_fail("fork failed");
268 	default:
269 		kill(child, SIGPIPE);
270 		sigemptyset(&set);
271 		sigsuspend(&set);
272 	}
273 }
274 
275 static sigjmp_buf sigfpe_flt_env;
276 static void
277 sigfpe_flt_action(int signo, siginfo_t *info, void *ptr)
278 {
279 
280 	sig_debug(signo, info, (ucontext_t *)ptr);
281 
282 	if (fltdiv_signalled++ != 0)
283 		atf_tc_fail("FPE handler called more than once");
284 
285 	ATF_REQUIRE_EQ(info->si_signo, SIGFPE);
286 	ATF_REQUIRE_EQ(info->si_code, FPE_FLTDIV);
287 	ATF_REQUIRE_EQ(info->si_errno, 0);
288 
289 	siglongjmp(sigfpe_flt_env, 1);
290 }
291 
292 ATF_TC(sigfpe_flt);
293 ATF_TC_HEAD(sigfpe_flt, tc)
294 {
295 
296 	atf_tc_set_md_var(tc, "descr",
297 	    "Checks that signal trampoline correctly calls SIGFPE handler "
298 	    "for floating div-by-zero");
299 }
300 
301 ATF_TC_BODY(sigfpe_flt, tc)
302 {
303 	struct sigaction sa;
304 	double d = strtod("0", NULL);
305 
306 	if (system("cpuctl identify 0 | grep -q QEMU") == 0)
307 		atf_tc_skip("Test does not run correctly under qemu");
308 	if (system("cpuctl identify 0 | grep -q "
309 	    "'cpu0: Intel Pentium II (Klamath) (686-class), id 0x633'") == 0)
310 		atf_tc_skip("Test does not run correctly under qemu "
311 		    "(heuristic match)");
312 	if (strcmp(atf_config_get("atf_arch"),"powerpc") == 0)
313 		atf_tc_skip("Test not valid on powerpc");
314 	if (sigsetjmp(sigfpe_flt_env, 0) == 0) {
315 		sa.sa_flags = SA_SIGINFO;
316 		sa.sa_sigaction = sigfpe_flt_action;
317 		sigemptyset(&sa.sa_mask);
318 		sigaction(SIGFPE, &sa, NULL);
319 #ifdef _FLOAT_IEEE754
320 		fpsetmask(FP_X_INV|FP_X_DZ|FP_X_OFL|FP_X_UFL|FP_X_IMP);
321 #endif
322 		printf("%g\n", 1 / d);
323 	}
324 	if (fltdiv_signalled == 0)
325 		atf_tc_fail("FPE signal handler was not invoked");
326 }
327 
328 static sigjmp_buf sigfpe_int_env;
329 static void
330 sigfpe_int_action(int signo, siginfo_t *info, void *ptr)
331 {
332 
333 	sig_debug(signo, info, (ucontext_t *)ptr);
334 
335 	if (intdiv_signalled++ != 0)
336 		atf_tc_fail("INTDIV handler called more than once");
337 
338 	ATF_REQUIRE_EQ(info->si_signo, SIGFPE);
339 	ATF_REQUIRE_EQ(info->si_code, FPE_INTDIV);
340 	atf_tc_expect_pass();
341 	ATF_REQUIRE_EQ(info->si_errno, 0);
342 
343 	siglongjmp(sigfpe_int_env, 1);
344 }
345 
346 ATF_TC(sigfpe_int);
347 ATF_TC_HEAD(sigfpe_int, tc)
348 {
349 
350 	atf_tc_set_md_var(tc, "descr",
351 	    "Checks that signal trampoline correctly calls SIGFPE handler "
352 	    "for integer div-by-zero (PR port-i386/43655)");
353 }
354 
355 ATF_TC_BODY(sigfpe_int, tc)
356 {
357 	struct sigaction sa;
358 	long l = strtol("0", NULL, 10);
359 
360 	if (strcmp(atf_config_get("atf_arch"),"powerpc") == 0)
361 		atf_tc_skip("Test not valid on powerpc");
362 	if (sigsetjmp(sigfpe_int_env, 0) == 0) {
363 		sa.sa_flags = SA_SIGINFO;
364 		sa.sa_sigaction = sigfpe_int_action;
365 		sigemptyset(&sa.sa_mask);
366 		sigaction(SIGFPE, &sa, NULL);
367 #ifdef _FLOAT_IEEE754
368 		fpsetmask(FP_X_INV|FP_X_DZ|FP_X_OFL|FP_X_UFL|FP_X_IMP);
369 #endif
370 		printf("%ld\n", 1 / l);
371 	}
372 	if (intdiv_signalled == 0)
373 		atf_tc_fail("FPE signal handler was not invoked");
374 }
375 
376 static void
377 sigsegv_action(int signo, siginfo_t *info, void *ptr)
378 {
379 
380 	sig_debug(signo, info, (ucontext_t *)ptr);
381 
382 	ATF_REQUIRE_EQ(info->si_signo, SIGSEGV);
383 	ATF_REQUIRE_EQ(info->si_errno, 0);
384 	ATF_REQUIRE_EQ(info->si_code, SEGV_MAPERR);
385 	ATF_REQUIRE_EQ(info->si_addr, (void *)0);
386 
387 	atf_tc_pass();
388 	/* NOTREACHED */
389 }
390 
391 ATF_TC(sigsegv);
392 ATF_TC_HEAD(sigsegv, tc)
393 {
394 
395 	atf_tc_set_md_var(tc, "descr",
396 	    "Checks that signal trampoline correctly calls SIGSEGV handler");
397 }
398 
399 ATF_TC_BODY(sigsegv, tc)
400 {
401 	struct sigaction sa;
402 
403 	sa.sa_flags = SA_SIGINFO;
404 	sa.sa_sigaction = sigsegv_action;
405 	sigemptyset(&sa.sa_mask);
406 	sigaction(SIGSEGV, &sa, NULL);
407 
408 	*(volatile long *)0 = 0;
409 	atf_tc_fail("Test did not fault as expected");
410 }
411 
412 static void
413 sigbus_action(int signo, siginfo_t *info, void *ptr)
414 {
415 
416 	printf("si_addr = %p\n", info->si_addr);
417 	sig_debug(signo, info, (ucontext_t *)ptr);
418 
419 	ATF_REQUIRE_EQ(info->si_signo, SIGBUS);
420 	ATF_REQUIRE_EQ(info->si_errno, 0);
421 	ATF_REQUIRE_EQ(info->si_code, BUS_ADRALN);
422 
423 	if (strcmp(atf_config_get("atf_arch"), "i386") == 0 ||
424 	    strcmp(atf_config_get("atf_arch"), "x86_64") == 0) {
425 		atf_tc_expect_fail("x86 architecture does not correctly "
426 		    "report the address where the unaligned access occured");
427 	}
428 	ATF_REQUIRE_EQ(info->si_addr, (volatile void *)addr);
429 
430 	atf_tc_pass();
431 	/* NOTREACHED */
432 }
433 
434 ATF_TC(sigbus_adraln);
435 ATF_TC_HEAD(sigbus_adraln, tc)
436 {
437 
438 	atf_tc_set_md_var(tc, "descr",
439 	    "Checks that signal trampoline correctly calls SIGBUS handler "
440 	    "for invalid address alignment");
441 }
442 
443 ATF_TC_BODY(sigbus_adraln, tc)
444 {
445 	const char *arch = atf_config_get("atf_arch");
446 	struct sigaction sa;
447 
448 	if (strcmp(arch, "alpha") == 0) {
449 		int rv, val;
450 		size_t len = sizeof(val);
451 		rv = sysctlbyname("machdep.unaligned_sigbus", &val, &len,
452 			NULL, 0);
453 		ATF_REQUIRE(rv == 0);
454 		if (val == 0)
455 			atf_tc_skip("SIGBUS signal not enabled for"
456 				    " unaligned accesses");
457 
458 	}
459 
460 	sa.sa_flags = SA_SIGINFO;
461 	sa.sa_sigaction = sigbus_action;
462 	sigemptyset(&sa.sa_mask);
463 	sigaction(SIGBUS, &sa, NULL);
464 
465 	/* Enable alignement checks for x86. 0x40000 is PSL_AC. */
466 #if defined(__i386__)
467 	__asm__("pushf; orl $0x40000, (%esp); popf");
468 #elif defined(__amd64__)
469 	__asm__("pushf; orl $0x40000, (%rsp); popf");
470 #endif
471 
472 	addr = calloc(2, sizeof(int));
473 	ATF_REQUIRE(addr != NULL);
474 
475 	if (strcmp(arch, "i386") == 0 || strcmp(arch, "x86_64") == 0) {
476 		if (system("cpuctl identify 0 | grep -q QEMU") == 0) {
477 			atf_tc_expect_fail("QEMU fails to trap unaligned "
478 			    "accesses");
479 		}
480 	}
481 
482 	/* Force an unaligned access */
483 	addr++;
484 	printf("now trying to access unaligned address %p\n", addr);
485 	ATF_REQUIRE_EQ(*(volatile int *)addr, 0);
486 
487 	atf_tc_fail("Test did not fault as expected");
488 }
489 
490 ATF_TP_ADD_TCS(tp)
491 {
492 
493 	ATF_TP_ADD_TC(tp, sigalarm);
494 	ATF_TP_ADD_TC(tp, sigchild_normal);
495 	ATF_TP_ADD_TC(tp, sigchild_dump);
496 	ATF_TP_ADD_TC(tp, sigchild_kill);
497 	ATF_TP_ADD_TC(tp, sigfpe_flt);
498 	ATF_TP_ADD_TC(tp, sigfpe_int);
499 	ATF_TP_ADD_TC(tp, sigsegv);
500 	ATF_TP_ADD_TC(tp, sigbus_adraln);
501 
502 	return atf_no_error();
503 }
504