xref: /netbsd-src/tests/lib/libc/setjmp/t_sigstack.c (revision 3026ea4883c7a850c56e4a9f2e8e47d56c4fc44e)
1 /*	$NetBSD: t_sigstack.c,v 1.3 2024/02/19 12:29:48 riastradh Exp $	*/
2 
3 /*-
4  * Copyright (c) 2024 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 <sys/cdefs.h>
30 __RCSID("$NetBSD: t_sigstack.c,v 1.3 2024/02/19 12:29:48 riastradh Exp $");
31 
32 #include <setjmp.h>
33 #include <signal.h>
34 #include <stddef.h>
35 #include <stdlib.h>
36 #include <ucontext.h>
37 
38 #include "h_macros.h"
39 
40 struct sigaltstack ss;
41 jmp_buf jmp;
42 sigjmp_buf sigjmp;
43 unsigned nentries;
44 const char *bailname;
45 void (*bailfn)(void) __dead;
46 
47 static void
48 on_sigusr1(int signo, siginfo_t *si, void *ctx)
49 {
50 	ucontext_t *uc = ctx;
51 	void *sp = (void *)(uintptr_t)_UC_MACHINE_SP(uc);
52 	void *fp = __builtin_frame_address(0);
53 
54 	/*
55 	 * Ensure that the signal handler was called in the alternate
56 	 * signal stack.
57 	 */
58 	ATF_REQUIRE_MSG(fp >= ss.ss_sp,
59 	    "sigaltstack failed to take effect --"
60 	    " signal handler's frame pointer %p doesn't lie in sigaltstack"
61 	    " [%p, %p), size 0x%zx",
62 	    fp, ss.ss_sp, (char *)ss.ss_sp + ss.ss_size, ss.ss_size);
63 	ATF_REQUIRE_MSG(fp < (void *)((char *)ss.ss_sp + ss.ss_size),
64 	    "sigaltstack failed to take effect --"
65 	    " signal handler's frame pointer %p doesn't lie in sigaltstack"
66 	    " [%p, %p), size 0x%zx",
67 	    fp, ss.ss_sp, (char *)ss.ss_sp + ss.ss_size, ss.ss_size);
68 
69 	/*
70 	 * Ensure that if we enter the signal handler, we are entering
71 	 * it from the original stack, not from the alternate signal
72 	 * stack.
73 	 *
74 	 * On some architectures, this is broken.  Those that appear to
75 	 * get this right are:
76 	 *
77 	 *	alpha, m68k, or1k, powerpc, powerpc64, riscv, vax
78 	 */
79 #if defined __arm__ || defined __hppa__ || defined __i386__ || \
80     defined __ia64__ || defined __mips__ || defined __sh3__ || \
81     defined __sparc__ || defined __sparc64__ || defined __x86_64__
82 	if (nentries > 0)
83 		atf_tc_expect_fail("PR lib/57946");
84 #endif
85 	ATF_REQUIRE_MSG((sp < ss.ss_sp ||
86 		sp > (void *)((char *)ss.ss_sp + ss.ss_size)),
87 	    "%s failed to restore stack before allowing signal --"
88 	    " interrupted stack pointer %p lies in sigaltstack"
89 	    " [%p, %p), size 0x%zx",
90 	    bailname, sp, ss.ss_sp, (char *)ss.ss_sp + ss.ss_size, ss.ss_size);
91 
92 	/*
93 	 * First time through, we want to test whether longjmp restores
94 	 * the signal mask first, or restores the stack pointer first.
95 	 * The signal should be blocked at this point, so we re-raise
96 	 * the signal to queue it up for delivery as soon as it is
97 	 * unmasked -- which should wait until the stack pointer has
98 	 * been restored in longjmp.
99 	 */
100 	if (nentries++ == 0)
101 		RL(raise(SIGUSR1));
102 
103 	/*
104 	 * Jump back to the original context.
105 	 */
106 	(*bailfn)();
107 }
108 
109 static void
110 go(const char *name, void (*fn)(void) __dead)
111 {
112 	struct sigaction sa;
113 
114 	bailname = name;
115 	bailfn = fn;
116 
117 	/*
118 	 * Allocate a stack for the signal handler to run in, and
119 	 * configure the system to use it.
120 	 *
121 	 * XXX Should maybe use a guard page but this is simpler.
122 	 */
123 	ss.ss_size = SIGSTKSZ;
124 	REQUIRE_LIBC(ss.ss_sp = malloc(ss.ss_size), NULL);
125 	RL(sigaltstack(&ss, NULL));
126 
127 	/*
128 	 * Set up a test signal handler for SIGUSR1.  Allow all
129 	 * signals, except SIGUSR1 (which is masked by default) -- that
130 	 * way we don't inadvertently obscure weird crashes in the
131 	 * signal handler.
132 	 *
133 	 * Set SA_SIGINFO so the system will pass siginfo -- and, more
134 	 * to the point, ucontext, so the signal handler can determine
135 	 * the stack pointer of the logic it interrupted.
136 	 *
137 	 * Set SA_ONSTACK so the system will use the alternate signal
138 	 * stack to call the signal handler -- that way, it can tell
139 	 * whether the stack was restored before the second time
140 	 * around.
141 	 */
142 	memset(&sa, 0, sizeof(sa));
143 	sa.sa_sigaction = &on_sigusr1;
144 	RL(sigemptyset(&sa.sa_mask));
145 	sa.sa_flags = SA_SIGINFO|SA_ONSTACK;
146 	RL(sigaction(SIGUSR1, &sa, NULL));
147 
148 	/*
149 	 * Raise the signal to enter the signal handler the first time.
150 	 */
151 	RL(raise(SIGUSR1));
152 
153 	/*
154 	 * If we ever reach this point, something went seriously wrong.
155 	 */
156 	atf_tc_fail("unreachable");
157 }
158 
159 static void __dead
160 bail_longjmp(void)
161 {
162 
163 	longjmp(jmp, 1);
164 }
165 
166 ATF_TC(setjmp);
167 ATF_TC_HEAD(setjmp, tc)
168 {
169 	atf_tc_set_md_var(tc, "descr",
170 	    "Test longjmp restores stack first, then signal mask");
171 }
172 ATF_TC_BODY(setjmp, tc)
173 {
174 
175 	/*
176 	 * Set up a return point for the signal handler: when the
177 	 * signal handler does longjmp(jmp, 1), it comes flying out of
178 	 * here.
179 	 */
180 	if (setjmp(jmp) == 1)
181 		return;
182 
183 	/*
184 	 * Run the test with longjmp.
185 	 */
186 	go("longjmp", &bail_longjmp);
187 }
188 
189 static void __dead
190 bail_siglongjmp(void)
191 {
192 
193 	siglongjmp(sigjmp, 1);
194 }
195 
196 ATF_TC(sigsetjmp);
197 ATF_TC_HEAD(sigsetjmp, tc)
198 {
199 	atf_tc_set_md_var(tc, "descr",
200 	    "Test siglongjmp restores stack first, then signal mask");
201 }
202 ATF_TC_BODY(sigsetjmp, tc)
203 {
204 
205 	/*
206 	 * Set up a return point for the signal handler: when the
207 	 * signal handler does siglongjmp(sigjmp, 1), it comes flying
208 	 * out of here.
209 	 */
210 	if (sigsetjmp(sigjmp, /*savesigmask*/1) == 1)
211 		return;
212 
213 	/*
214 	 * Run the test with siglongjmp.
215 	 */
216 	go("siglongjmp", &bail_siglongjmp);
217 }
218 
219 ATF_TP_ADD_TCS(tp)
220 {
221 
222 	ATF_TP_ADD_TC(tp, setjmp);
223 	ATF_TP_ADD_TC(tp, sigsetjmp);
224 
225 	return atf_no_error();
226 }
227