Home | History | Annotate | Line # | Download | only in setjmp
t_sigstack.c revision 1.10
      1  1.10     skrll /*	$NetBSD: t_sigstack.c,v 1.10 2024/05/06 06:58:00 skrll Exp $	*/
      2   1.1  riastrad 
      3   1.1  riastrad /*-
      4   1.1  riastrad  * Copyright (c) 2024 The NetBSD Foundation, Inc.
      5   1.1  riastrad  * All rights reserved.
      6   1.1  riastrad  *
      7   1.1  riastrad  * Redistribution and use in source and binary forms, with or without
      8   1.1  riastrad  * modification, are permitted provided that the following conditions
      9   1.1  riastrad  * are met:
     10   1.1  riastrad  * 1. Redistributions of source code must retain the above copyright
     11   1.1  riastrad  *    notice, this list of conditions and the following disclaimer.
     12   1.1  riastrad  * 2. Redistributions in binary form must reproduce the above copyright
     13   1.1  riastrad  *    notice, this list of conditions and the following disclaimer in the
     14   1.1  riastrad  *    documentation and/or other materials provided with the distribution.
     15   1.1  riastrad  *
     16   1.1  riastrad  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     17   1.1  riastrad  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     18   1.1  riastrad  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     19   1.1  riastrad  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     20   1.1  riastrad  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     21   1.1  riastrad  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     22   1.1  riastrad  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     23   1.1  riastrad  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     24   1.1  riastrad  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     25   1.1  riastrad  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     26   1.1  riastrad  * POSSIBILITY OF SUCH DAMAGE.
     27   1.1  riastrad  */
     28   1.1  riastrad 
     29   1.1  riastrad #include <sys/cdefs.h>
     30  1.10     skrll __RCSID("$NetBSD: t_sigstack.c,v 1.10 2024/05/06 06:58:00 skrll Exp $");
     31   1.1  riastrad 
     32   1.1  riastrad #include <setjmp.h>
     33   1.1  riastrad #include <signal.h>
     34   1.1  riastrad #include <stddef.h>
     35   1.1  riastrad #include <stdlib.h>
     36   1.1  riastrad #include <ucontext.h>
     37   1.1  riastrad 
     38   1.1  riastrad #include "h_macros.h"
     39   1.1  riastrad 
     40   1.6  riastrad struct sigaltstack ss[3];
     41   1.1  riastrad jmp_buf jmp;
     42   1.3  riastrad sigjmp_buf sigjmp;
     43   1.1  riastrad unsigned nentries;
     44   1.3  riastrad const char *bailname;
     45   1.3  riastrad void (*bailfn)(void) __dead;
     46   1.1  riastrad 
     47   1.1  riastrad static void
     48   1.1  riastrad on_sigusr1(int signo, siginfo_t *si, void *ctx)
     49   1.1  riastrad {
     50   1.1  riastrad 	ucontext_t *uc = ctx;
     51   1.1  riastrad 	void *sp = (void *)(uintptr_t)_UC_MACHINE_SP(uc);
     52   1.1  riastrad 	void *fp = __builtin_frame_address(0);
     53   1.6  riastrad 	struct sigaltstack *ssp;
     54   1.6  riastrad 
     55   1.6  riastrad 	/*
     56   1.6  riastrad 	 * Ensure we haven't re-entered the signal handler too many
     57   1.6  riastrad 	 * times.  We should enter only twice.
     58   1.6  riastrad 	 */
     59   1.6  riastrad 	ATF_REQUIRE_MSG(nentries < 2,
     60   1.6  riastrad 	    "%u recursive signal handler entries is too many in this test",
     61   1.6  riastrad 	    nentries + 1);
     62   1.1  riastrad 
     63   1.1  riastrad 	/*
     64   1.1  riastrad 	 * Ensure that the signal handler was called in the alternate
     65   1.1  riastrad 	 * signal stack.
     66   1.1  riastrad 	 */
     67   1.6  riastrad 	ssp = &ss[nentries];
     68   1.6  riastrad 	ATF_REQUIRE_MSG((fp >= ssp->ss_sp &&
     69   1.6  riastrad 		fp < (void *)((char *)ssp->ss_sp + ssp->ss_size)),
     70   1.4  riastrad 	    "sigaltstack failed to take effect on entry %u --"
     71   1.1  riastrad 	    " signal handler's frame pointer %p doesn't lie in sigaltstack"
     72   1.1  riastrad 	    " [%p, %p), size 0x%zx",
     73   1.4  riastrad 	    nentries,
     74   1.6  riastrad 	    fp, ssp->ss_sp, (char *)ssp->ss_sp + ssp->ss_size, ssp->ss_size);
     75   1.1  riastrad 
     76   1.1  riastrad 	/*
     77   1.1  riastrad 	 * Ensure that if we enter the signal handler, we are entering
     78   1.6  riastrad 	 * it from the original stack, not from any of the alternate
     79   1.6  riastrad 	 * signal stacks.
     80   1.1  riastrad 	 *
     81   1.1  riastrad 	 * On some architectures, this is broken.  Those that appear to
     82   1.1  riastrad 	 * get this right are:
     83   1.1  riastrad 	 *
     84   1.9  riastrad 	 *	aarch64
     85   1.9  riastrad 	 *	alpha
     86   1.9  riastrad 	 *	i386
     87   1.9  riastrad 	 *	m68k
     88   1.9  riastrad 	 *	or1k
     89   1.9  riastrad 	 *	powerpc
     90   1.9  riastrad 	 *	powerpc64
     91   1.9  riastrad 	 *	riscv
     92   1.9  riastrad 	 *	vax
     93   1.9  riastrad 	 *	x86_64
     94   1.1  riastrad 	 */
     95  1.10     skrll #if defined __hppa__ || \
     96   1.1  riastrad     defined __ia64__ || defined __mips__ || defined __sh3__ || \
     97   1.8  riastrad     defined __sparc__ || defined __sparc64__
     98   1.1  riastrad 	if (nentries > 0)
     99   1.1  riastrad 		atf_tc_expect_fail("PR lib/57946");
    100   1.1  riastrad #endif
    101   1.6  riastrad 	for (ssp = &ss[0]; ssp < &ss[__arraycount(ss)]; ssp++) {
    102   1.6  riastrad 		ATF_REQUIRE_MSG((sp < ssp->ss_sp ||
    103   1.7  riastrad 			sp >= (void *)((char *)ssp->ss_sp + ssp->ss_size)),
    104   1.6  riastrad 		    "%s failed to restore stack"
    105   1.6  riastrad 		    " before allowing signal on entry %u --"
    106   1.6  riastrad 		    " interrupted stack pointer %p lies in sigaltstack %zd"
    107   1.6  riastrad 		    " [%p, %p), size 0x%zx",
    108   1.6  riastrad 		    bailname,
    109   1.6  riastrad 		    nentries,
    110   1.6  riastrad 		    sp, ssp - ss,
    111   1.6  riastrad 		    ssp->ss_sp, (char *)ssp->ss_sp + ssp->ss_size,
    112   1.6  riastrad 		    ssp->ss_size);
    113   1.6  riastrad 	}
    114   1.1  riastrad 
    115   1.1  riastrad 	/*
    116   1.1  riastrad 	 * First time through, we want to test whether longjmp restores
    117   1.1  riastrad 	 * the signal mask first, or restores the stack pointer first.
    118   1.1  riastrad 	 * The signal should be blocked at this point, so we re-raise
    119   1.1  riastrad 	 * the signal to queue it up for delivery as soon as it is
    120   1.1  riastrad 	 * unmasked -- which should wait until the stack pointer has
    121   1.1  riastrad 	 * been restored in longjmp.
    122   1.1  riastrad 	 */
    123   1.1  riastrad 	if (nentries++ == 0)
    124   1.1  riastrad 		RL(raise(SIGUSR1));
    125   1.1  riastrad 
    126   1.1  riastrad 	/*
    127   1.6  riastrad 	 * Set up the next sigaltstack.  We can't reuse the current one
    128   1.6  riastrad 	 * for the next signal handler re-entry until the system clears
    129   1.6  riastrad 	 * the SS_ONSTACK process state -- which normal return from
    130   1.6  riastrad 	 * signal handler does, but which longjmp does not.  So to keep
    131   1.6  riastrad 	 * it simple (ha), we just use another sigaltstack.
    132   1.6  riastrad 	 */
    133   1.6  riastrad 	RL(sigaltstack(&ss[nentries], NULL));
    134   1.6  riastrad 
    135   1.6  riastrad 	/*
    136   1.1  riastrad 	 * Jump back to the original context.
    137   1.1  riastrad 	 */
    138   1.3  riastrad 	(*bailfn)();
    139   1.1  riastrad }
    140   1.1  riastrad 
    141   1.3  riastrad static void
    142   1.3  riastrad go(const char *name, void (*fn)(void) __dead)
    143   1.1  riastrad {
    144   1.1  riastrad 	struct sigaction sa;
    145   1.6  riastrad 	unsigned i;
    146   1.1  riastrad 
    147   1.3  riastrad 	bailname = name;
    148   1.3  riastrad 	bailfn = fn;
    149   1.3  riastrad 
    150   1.1  riastrad 	/*
    151   1.1  riastrad 	 * Allocate a stack for the signal handler to run in, and
    152   1.6  riastrad 	 * configure the system to use the first one.
    153   1.1  riastrad 	 *
    154   1.1  riastrad 	 * XXX Should maybe use a guard page but this is simpler.
    155   1.1  riastrad 	 */
    156   1.6  riastrad 	for (i = 0; i < __arraycount(ss); i++) {
    157   1.6  riastrad 		ss[i].ss_size = SIGSTKSZ;
    158   1.6  riastrad 		REQUIRE_LIBC(ss[i].ss_sp = malloc(ss[i].ss_size), NULL);
    159   1.6  riastrad 	}
    160   1.6  riastrad 	RL(sigaltstack(&ss[0], NULL));
    161   1.1  riastrad 
    162   1.1  riastrad 	/*
    163   1.1  riastrad 	 * Set up a test signal handler for SIGUSR1.  Allow all
    164   1.1  riastrad 	 * signals, except SIGUSR1 (which is masked by default) -- that
    165   1.1  riastrad 	 * way we don't inadvertently obscure weird crashes in the
    166   1.1  riastrad 	 * signal handler.
    167   1.1  riastrad 	 *
    168   1.1  riastrad 	 * Set SA_SIGINFO so the system will pass siginfo -- and, more
    169   1.1  riastrad 	 * to the point, ucontext, so the signal handler can determine
    170   1.1  riastrad 	 * the stack pointer of the logic it interrupted.
    171   1.1  riastrad 	 *
    172   1.1  riastrad 	 * Set SA_ONSTACK so the system will use the alternate signal
    173   1.1  riastrad 	 * stack to call the signal handler -- that way, it can tell
    174   1.1  riastrad 	 * whether the stack was restored before the second time
    175   1.1  riastrad 	 * around.
    176   1.1  riastrad 	 */
    177   1.1  riastrad 	memset(&sa, 0, sizeof(sa));
    178   1.1  riastrad 	sa.sa_sigaction = &on_sigusr1;
    179   1.2  riastrad 	RL(sigemptyset(&sa.sa_mask));
    180   1.1  riastrad 	sa.sa_flags = SA_SIGINFO|SA_ONSTACK;
    181   1.1  riastrad 	RL(sigaction(SIGUSR1, &sa, NULL));
    182   1.1  riastrad 
    183   1.1  riastrad 	/*
    184   1.3  riastrad 	 * Raise the signal to enter the signal handler the first time.
    185   1.3  riastrad 	 */
    186   1.3  riastrad 	RL(raise(SIGUSR1));
    187   1.3  riastrad 
    188   1.3  riastrad 	/*
    189   1.3  riastrad 	 * If we ever reach this point, something went seriously wrong.
    190   1.3  riastrad 	 */
    191   1.3  riastrad 	atf_tc_fail("unreachable");
    192   1.3  riastrad }
    193   1.3  riastrad 
    194   1.3  riastrad static void __dead
    195   1.3  riastrad bail_longjmp(void)
    196   1.3  riastrad {
    197   1.3  riastrad 
    198   1.3  riastrad 	longjmp(jmp, 1);
    199   1.3  riastrad }
    200   1.3  riastrad 
    201   1.3  riastrad ATF_TC(setjmp);
    202   1.3  riastrad ATF_TC_HEAD(setjmp, tc)
    203   1.3  riastrad {
    204   1.3  riastrad 	atf_tc_set_md_var(tc, "descr",
    205   1.3  riastrad 	    "Test longjmp restores stack first, then signal mask");
    206   1.3  riastrad }
    207   1.3  riastrad ATF_TC_BODY(setjmp, tc)
    208   1.3  riastrad {
    209   1.3  riastrad 
    210   1.3  riastrad 	/*
    211   1.1  riastrad 	 * Set up a return point for the signal handler: when the
    212   1.1  riastrad 	 * signal handler does longjmp(jmp, 1), it comes flying out of
    213   1.1  riastrad 	 * here.
    214   1.1  riastrad 	 */
    215   1.1  riastrad 	if (setjmp(jmp) == 1)
    216   1.1  riastrad 		return;
    217   1.1  riastrad 
    218   1.1  riastrad 	/*
    219   1.3  riastrad 	 * Run the test with longjmp.
    220   1.3  riastrad 	 */
    221   1.3  riastrad 	go("longjmp", &bail_longjmp);
    222   1.3  riastrad }
    223   1.3  riastrad 
    224   1.3  riastrad static void __dead
    225   1.3  riastrad bail_siglongjmp(void)
    226   1.3  riastrad {
    227   1.3  riastrad 
    228   1.3  riastrad 	siglongjmp(sigjmp, 1);
    229   1.3  riastrad }
    230   1.3  riastrad 
    231   1.3  riastrad ATF_TC(sigsetjmp);
    232   1.3  riastrad ATF_TC_HEAD(sigsetjmp, tc)
    233   1.3  riastrad {
    234   1.3  riastrad 	atf_tc_set_md_var(tc, "descr",
    235   1.3  riastrad 	    "Test siglongjmp restores stack first, then signal mask");
    236   1.3  riastrad }
    237   1.3  riastrad ATF_TC_BODY(sigsetjmp, tc)
    238   1.3  riastrad {
    239   1.3  riastrad 
    240   1.3  riastrad 	/*
    241   1.3  riastrad 	 * Set up a return point for the signal handler: when the
    242   1.3  riastrad 	 * signal handler does siglongjmp(sigjmp, 1), it comes flying
    243   1.3  riastrad 	 * out of here.
    244   1.1  riastrad 	 */
    245   1.3  riastrad 	if (sigsetjmp(sigjmp, /*savesigmask*/1) == 1)
    246   1.3  riastrad 		return;
    247   1.1  riastrad 
    248   1.1  riastrad 	/*
    249   1.3  riastrad 	 * Run the test with siglongjmp.
    250   1.1  riastrad 	 */
    251   1.3  riastrad 	go("siglongjmp", &bail_siglongjmp);
    252   1.1  riastrad }
    253   1.1  riastrad 
    254   1.1  riastrad ATF_TP_ADD_TCS(tp)
    255   1.1  riastrad {
    256   1.1  riastrad 
    257   1.1  riastrad 	ATF_TP_ADD_TC(tp, setjmp);
    258   1.3  riastrad 	ATF_TP_ADD_TC(tp, sigsetjmp);
    259   1.1  riastrad 
    260   1.1  riastrad 	return atf_no_error();
    261   1.1  riastrad }
    262