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