xref: /openbsd-src/regress/lib/libc/sys/t_sigaltstack.c (revision ffcef06798eb7b98532e76a80212f0772bebc4f6)
1 /*	$OpenBSD: t_sigaltstack.c,v 1.1 2021/09/02 12:40:44 mbuhl Exp $	*/
2 /* $NetBSD: t_sigaltstack.c,v 1.2 2020/05/01 21:35:30 christos Exp $ */
3 
4 /*-
5  * Copyright (c) 2020 The NetBSD Foundation, Inc.
6  * All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  * 1. Redistributions of source code must retain the above copyright
12  *    notice, this list of conditions and the following disclaimer.
13  * 2. Redistributions in binary form must reproduce the above copyright
14  *    notice, this list of conditions and the following disclaimer in the
15  *    documentation and/or other materials provided with the distribution.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
18  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
19  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
20  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
21  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
22  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
23  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
24  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
25  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
26  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
27  * POSSIBILITY OF SUCH DAMAGE.
28  */
29 #include "macros.h"
30 
31 #include <sys/cdefs.h>
32 __RCSID("$NetBSD: t_sigaltstack.c,v 1.2 2020/05/01 21:35:30 christos Exp $");
33 
34 #include <signal.h>
35 #include <stdbool.h>
36 
37 #include "atf-c.h"
38 
39 #include "h_macros.h"
40 
41 static stack_t sigstk;
42 static bool handler_called;
43 static bool handler_use_altstack;
44 
45 static void
46 handler(int signo __unused)
47 {
48 	char sp[128];
49 
50 	handler_called = true;
51 
52 	/* checking if the stack pointer is within the range of altstack */
53 	if ((char *)sigstk.ss_sp <= sp &&
54 	    ((char *)sigstk.ss_sp + sigstk.ss_size) > sp)
55 		handler_use_altstack = true;
56 	else
57 		handler_use_altstack = false;
58 }
59 
60 ATF_TC(sigaltstack_onstack);
61 ATF_TC_HEAD(sigaltstack_onstack, tc)
62 {
63 	atf_tc_set_md_var(tc, "descr",
64 	    "Checks for using signal stack with SA_ONSTACK");
65 }
66 
67 ATF_TC_BODY(sigaltstack_onstack, tc)
68 {
69 	struct sigaction sa;
70 	int i;
71 
72 	/* set a signal handler use alternative stack */
73 	memset(&sigstk, 0, sizeof(sigstk));
74 	sigstk.ss_sp = malloc(SIGSTKSZ);
75 	ATF_REQUIRE(sigstk.ss_sp != NULL);
76 	sigstk.ss_size = SIGSTKSZ;
77 	sigstk.ss_flags = 0;
78 	ATF_REQUIRE(sigaltstack(&sigstk, 0) == 0);
79 
80 	sigemptyset(&sa.sa_mask);
81 	sa.sa_handler = handler;
82 	sa.sa_flags = SA_ONSTACK;
83 	sigaction(SIGUSR1, &sa, NULL);
84 
85 	/* test several times */
86 	for (i = 1; i <= 5; i++) {
87 		handler_called = false;
88 		kill(getpid(), SIGUSR1);
89 
90 		if (!handler_called)
91 			atf_tc_fail("signal handler wasn't called (count=%d)", i);
92 		if (!handler_use_altstack)
93 			atf_tc_fail("alternative stack wasn't used (count=%d)", i);
94 	}
95 }
96 
97 ATF_TP_ADD_TCS(tp)
98 {
99 	ATF_TP_ADD_TC(tp, sigaltstack_onstack);
100 
101 	return atf_no_error();
102 }
103