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