xref: /netbsd-src/tests/lib/libc/setjmp/t_sigstack.c (revision a7b033762e81321652cf991086affed5de2826fc)
1 /*	$NetBSD: t_sigstack.c,v 1.1 2024/02/19 04:30:39 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.1 2024/02/19 04:30:39 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 unsigned nentries;
43 
44 static void
45 on_sigusr1(int signo, siginfo_t *si, void *ctx)
46 {
47 	ucontext_t *uc = ctx;
48 	void *sp = (void *)(uintptr_t)_UC_MACHINE_SP(uc);
49 	void *fp = __builtin_frame_address(0);
50 
51 	/*
52 	 * Ensure that the signal handler was called in the alternate
53 	 * signal stack.
54 	 */
55 	ATF_REQUIRE_MSG(fp >= ss.ss_sp,
56 	    "sigaltstack failed to take effect --"
57 	    " signal handler's frame pointer %p doesn't lie in sigaltstack"
58 	    " [%p, %p), size 0x%zx",
59 	    fp, ss.ss_sp, (char *)ss.ss_sp + ss.ss_size, ss.ss_size);
60 	ATF_REQUIRE_MSG(fp < (void *)((char *)ss.ss_sp + ss.ss_size),
61 	    "sigaltstack failed to take effect --"
62 	    " signal handler's frame pointer %p doesn't lie in sigaltstack"
63 	    " [%p, %p), size 0x%zx",
64 	    fp, ss.ss_sp, (char *)ss.ss_sp + ss.ss_size, ss.ss_size);
65 
66 	/*
67 	 * Ensure that if we enter the signal handler, we are entering
68 	 * it from the original stack, not from the alternate signal
69 	 * stack.
70 	 *
71 	 * On some architectures, this is broken.  Those that appear to
72 	 * get this right are:
73 	 *
74 	 *	alpha, m68k, or1k, powerpc, powerpc64, riscv, vax
75 	 */
76 #if defined __arm__ || defined __hppa__ || defined __i386__ || \
77     defined __ia64__ || defined __mips__ || defined __sh3__ || \
78     defined __sparc__ || defined __sparc64__ || defined __x86_64__
79 	if (nentries > 0)
80 		atf_tc_expect_fail("PR lib/57946");
81 #endif
82 	ATF_REQUIRE_MSG((sp < ss.ss_sp ||
83 		sp > (void *)((char *)ss.ss_sp + ss.ss_size)),
84 	    "longjmp failed to restore stack before allowing signal --"
85 	    " interrupted stack pointer %p lies in sigaltstack"
86 	    " [%p, %p), size 0x%zx",
87 	    sp, ss.ss_sp, (char *)ss.ss_sp + ss.ss_size, ss.ss_size);
88 
89 	/*
90 	 * First time through, we want to test whether longjmp restores
91 	 * the signal mask first, or restores the stack pointer first.
92 	 * The signal should be blocked at this point, so we re-raise
93 	 * the signal to queue it up for delivery as soon as it is
94 	 * unmasked -- which should wait until the stack pointer has
95 	 * been restored in longjmp.
96 	 */
97 	if (nentries++ == 0)
98 		RL(raise(SIGUSR1));
99 
100 	/*
101 	 * Jump back to the original context.
102 	 */
103 	longjmp(jmp, 1);
104 }
105 
106 ATF_TC(setjmp);
107 ATF_TC_HEAD(setjmp, tc)
108 {
109 	atf_tc_set_md_var(tc, "descr",
110 	    "Test longjmp restores stack first, then signal mask");
111 }
112 ATF_TC_BODY(setjmp, tc)
113 {
114 	struct sigaction sa;
115 
116 	/*
117 	 * Allocate a stack for the signal handler to run in, and
118 	 * configure the system to use it.
119 	 *
120 	 * XXX Should maybe use a guard page but this is simpler.
121 	 */
122 	ss.ss_size = SIGSTKSZ;
123 	REQUIRE_LIBC(ss.ss_sp = malloc(ss.ss_size), NULL);
124 	RL(sigaltstack(&ss, NULL));
125 
126 	/*
127 	 * Set up a test signal handler for SIGUSR1.  Allow all
128 	 * signals, except SIGUSR1 (which is masked by default) -- that
129 	 * way we don't inadvertently obscure weird crashes in the
130 	 * signal handler.
131 	 *
132 	 * Set SA_SIGINFO so the system will pass siginfo -- and, more
133 	 * to the point, ucontext, so the signal handler can determine
134 	 * the stack pointer of the logic it interrupted.
135 	 *
136 	 * Set SA_ONSTACK so the system will use the alternate signal
137 	 * stack to call the signal handler -- that way, it can tell
138 	 * whether the stack was restored before the second time
139 	 * around.
140 	 */
141 	memset(&sa, 0, sizeof(sa));
142 	sa.sa_sigaction = &on_sigusr1;
143 	sigemptyset(&sa.sa_mask);
144 	sa.sa_flags = SA_SIGINFO|SA_ONSTACK;
145 	RL(sigaction(SIGUSR1, &sa, NULL));
146 
147 	/*
148 	 * Set up a return point for the signal handler: when the
149 	 * signal handler does longjmp(jmp, 1), it comes flying out of
150 	 * here.
151 	 */
152 	if (setjmp(jmp) == 1)
153 		return;
154 
155 	/*
156 	 * Raise the signal to enter the signal handler the first time.
157 	 */
158 	raise(SIGUSR1);
159 
160 	/*
161 	 * If we ever reach this point, something went seriously wrong.
162 	 */
163 	atf_tc_fail("unreachable");
164 }
165 
166 ATF_TP_ADD_TCS(tp)
167 {
168 
169 	ATF_TP_ADD_TC(tp, setjmp);
170 //	ATF_TP_ADD_TC(tp, sigsetjmp);
171 
172 	return atf_no_error();
173 }
174