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