Home | History | Annotate | Line # | Download | only in setjmp
t_sigstack.c revision 1.13
      1 /*	$NetBSD: t_sigstack.c,v 1.13 2025/04/13 23:45:10 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.13 2025/04/13 23:45:10 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[3];
     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 	struct sigaltstack *ssp;
     54 
     55 	/*
     56 	 * Ensure we haven't re-entered the signal handler too many
     57 	 * times.  We should enter only twice.
     58 	 */
     59 	ATF_REQUIRE_MSG(nentries < 2,
     60 	    "%u recursive signal handler entries is too many in this test",
     61 	    nentries + 1);
     62 
     63 	/*
     64 	 * Ensure that the signal handler was called in the alternate
     65 	 * signal stack.
     66 	 */
     67 	ssp = &ss[nentries];
     68 	ATF_REQUIRE_MSG((fp >= ssp->ss_sp &&
     69 		fp < (void *)((char *)ssp->ss_sp + ssp->ss_size)),
     70 	    "sigaltstack failed to take effect on entry %u --"
     71 	    " signal handler's frame pointer %p doesn't lie in sigaltstack"
     72 	    " [%p, %p), size 0x%zx",
     73 	    nentries,
     74 	    fp, ssp->ss_sp, (char *)ssp->ss_sp + ssp->ss_size, ssp->ss_size);
     75 
     76 	/*
     77 	 * Ensure that if we enter the signal handler, we are entering
     78 	 * it from the original stack, not from any of the alternate
     79 	 * signal stacks.
     80 	 *
     81 	 * On some architectures, this is broken.  Those that appear to
     82 	 * get this right are:
     83 	 *
     84 	 *	aarch64
     85 	 *	alpha
     86 	 *	arm
     87 	 *	hppa
     88 	 *	i386
     89 	 *	m68k
     90 	 *	or1k
     91 	 *	powerpc
     92 	 *	powerpc64
     93 	 *	riscv
     94 	 *	vax
     95 	 *	x86_64
     96 	 */
     97 #if \
     98     defined __ia64__ || defined __mips__ || \
     99     defined __sparc__ || defined __sparc64__
    100 	if (nentries > 0)
    101 		atf_tc_expect_fail("PR lib/57946");
    102 #endif
    103 	for (ssp = &ss[0]; ssp < &ss[__arraycount(ss)]; ssp++) {
    104 		ATF_REQUIRE_MSG((sp < ssp->ss_sp ||
    105 			sp >= (void *)((char *)ssp->ss_sp + ssp->ss_size)),
    106 		    "%s failed to restore stack"
    107 		    " before allowing signal on entry %u --"
    108 		    " interrupted stack pointer %p lies in sigaltstack %zd"
    109 		    " [%p, %p), size 0x%zx",
    110 		    bailname,
    111 		    nentries,
    112 		    sp, ssp - ss,
    113 		    ssp->ss_sp, (char *)ssp->ss_sp + ssp->ss_size,
    114 		    ssp->ss_size);
    115 	}
    116 
    117 	/*
    118 	 * First time through, we want to test whether longjmp restores
    119 	 * the signal mask first, or restores the stack pointer first.
    120 	 * The signal should be blocked at this point, so we re-raise
    121 	 * the signal to queue it up for delivery as soon as it is
    122 	 * unmasked -- which should wait until the stack pointer has
    123 	 * been restored in longjmp.
    124 	 */
    125 	if (nentries++ == 0)
    126 		RL(raise(SIGUSR1));
    127 
    128 	/*
    129 	 * Set up the next sigaltstack.  We can't reuse the current one
    130 	 * for the next signal handler re-entry until the system clears
    131 	 * the SS_ONSTACK process state -- which normal return from
    132 	 * signal handler does, but which longjmp does not.  So to keep
    133 	 * it simple (ha), we just use another sigaltstack.
    134 	 */
    135 	RL(sigaltstack(&ss[nentries], NULL));
    136 
    137 	/*
    138 	 * Jump back to the original context.
    139 	 */
    140 	(*bailfn)();
    141 }
    142 
    143 static void
    144 go(const char *name, void (*fn)(void) __dead)
    145 {
    146 	struct sigaction sa;
    147 	unsigned i;
    148 
    149 	bailname = name;
    150 	bailfn = fn;
    151 
    152 	/*
    153 	 * Allocate a stack for the signal handler to run in, and
    154 	 * configure the system to use the first one.
    155 	 *
    156 	 * XXX Should maybe use a guard page but this is simpler.
    157 	 */
    158 	for (i = 0; i < __arraycount(ss); i++) {
    159 		ss[i].ss_size = SIGSTKSZ;
    160 		REQUIRE_LIBC(ss[i].ss_sp = malloc(ss[i].ss_size), NULL);
    161 	}
    162 	RL(sigaltstack(&ss[0], NULL));
    163 
    164 	/*
    165 	 * Set up a test signal handler for SIGUSR1.  Allow all
    166 	 * signals, except SIGUSR1 (which is masked by default) -- that
    167 	 * way we don't inadvertently obscure weird crashes in the
    168 	 * signal handler.
    169 	 *
    170 	 * Set SA_SIGINFO so the system will pass siginfo -- and, more
    171 	 * to the point, ucontext, so the signal handler can determine
    172 	 * the stack pointer of the logic it interrupted.
    173 	 *
    174 	 * Set SA_ONSTACK so the system will use the alternate signal
    175 	 * stack to call the signal handler -- that way, it can tell
    176 	 * whether the stack was restored before the second time
    177 	 * around.
    178 	 */
    179 	memset(&sa, 0, sizeof(sa));
    180 	sa.sa_sigaction = &on_sigusr1;
    181 	RL(sigemptyset(&sa.sa_mask));
    182 	sa.sa_flags = SA_SIGINFO|SA_ONSTACK;
    183 	RL(sigaction(SIGUSR1, &sa, NULL));
    184 
    185 	/*
    186 	 * Raise the signal to enter the signal handler the first time.
    187 	 */
    188 	RL(raise(SIGUSR1));
    189 
    190 	/*
    191 	 * If we ever reach this point, something went seriously wrong.
    192 	 */
    193 	atf_tc_fail("unreachable");
    194 }
    195 
    196 static void __dead
    197 bail_longjmp(void)
    198 {
    199 
    200 	longjmp(jmp, 1);
    201 }
    202 
    203 ATF_TC(setjmp);
    204 ATF_TC_HEAD(setjmp, tc)
    205 {
    206 	atf_tc_set_md_var(tc, "descr",
    207 	    "Test longjmp restores stack first, then signal mask");
    208 }
    209 ATF_TC_BODY(setjmp, tc)
    210 {
    211 
    212 	/*
    213 	 * Set up a return point for the signal handler: when the
    214 	 * signal handler does longjmp(jmp, 1), it comes flying out of
    215 	 * here.
    216 	 */
    217 	if (setjmp(jmp) == 1)
    218 		return;
    219 
    220 	/*
    221 	 * Run the test with longjmp.
    222 	 */
    223 	go("longjmp", &bail_longjmp);
    224 }
    225 
    226 static void __dead
    227 bail_siglongjmp(void)
    228 {
    229 
    230 	siglongjmp(sigjmp, 1);
    231 }
    232 
    233 ATF_TC(sigsetjmp);
    234 ATF_TC_HEAD(sigsetjmp, tc)
    235 {
    236 	atf_tc_set_md_var(tc, "descr",
    237 	    "Test siglongjmp restores stack first, then signal mask");
    238 }
    239 ATF_TC_BODY(sigsetjmp, tc)
    240 {
    241 
    242 	/*
    243 	 * Set up a return point for the signal handler: when the
    244 	 * signal handler does siglongjmp(sigjmp, 1), it comes flying
    245 	 * out of here.
    246 	 */
    247 	if (sigsetjmp(sigjmp, /*savesigmask*/1) == 1)
    248 		return;
    249 
    250 	/*
    251 	 * Run the test with siglongjmp.
    252 	 */
    253 	go("siglongjmp", &bail_siglongjmp);
    254 }
    255 
    256 ATF_TP_ADD_TCS(tp)
    257 {
    258 
    259 	ATF_TP_ADD_TC(tp, setjmp);
    260 	ATF_TP_ADD_TC(tp, sigsetjmp);
    261 
    262 	return atf_no_error();
    263 }
    264