Home | History | Annotate | Line # | Download | only in kernel
t_signal_and_sp.c revision 1.3
      1  1.3  riastrad /*	$NetBSD: t_signal_and_sp.c,v 1.3 2025/04/20 22:31:25 riastradh Exp $	*/
      2  1.1       pho 
      3  1.1       pho /*
      4  1.1       pho  * Copyright (c) 2024 The NetBSD Foundation, Inc.
      5  1.1       pho  * All rights reserved.
      6  1.1       pho  *
      7  1.1       pho  * Redistribution and use in source and binary forms, with or without
      8  1.1       pho  * modification, are permitted provided that the following conditions
      9  1.1       pho  * are met:
     10  1.1       pho  * 1. Redistributions of source code must retain the above copyright
     11  1.1       pho  *    notice, this list of conditions and the following disclaimer.
     12  1.1       pho  * 2. Redistributions in binary form must reproduce the above copyright
     13  1.1       pho  *    notice, this list of conditions and the following disclaimer in the
     14  1.1       pho  *    documentation and/or other materials provided with the distribution.
     15  1.1       pho  *
     16  1.1       pho  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     17  1.1       pho  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     18  1.1       pho  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     19  1.1       pho  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     20  1.1       pho  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     21  1.1       pho  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     22  1.1       pho  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     23  1.1       pho  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     24  1.1       pho  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     25  1.1       pho  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     26  1.1       pho  * POSSIBILITY OF SUCH DAMAGE.
     27  1.1       pho  */
     28  1.1       pho 
     29  1.2  riastrad #include <sys/cdefs.h>
     30  1.3  riastrad __RCSID("$NetBSD: t_signal_and_sp.c,v 1.3 2025/04/20 22:31:25 riastradh Exp $");
     31  1.2  riastrad 
     32  1.2  riastrad #include <sys/wait.h>
     33  1.2  riastrad 
     34  1.2  riastrad #include <machine/param.h>
     35  1.2  riastrad 
     36  1.1       pho #include <atf-c.h>
     37  1.2  riastrad #include <limits.h>
     38  1.2  riastrad #include <poll.h>
     39  1.2  riastrad #include <signal.h>
     40  1.2  riastrad #include <stdint.h>
     41  1.2  riastrad #include <stdio.h>
     42  1.2  riastrad #include <string.h>
     43  1.2  riastrad #include <time.h>
     44  1.2  riastrad #include <unistd.h>
     45  1.1       pho 
     46  1.2  riastrad #include "h_execsp.h"
     47  1.2  riastrad #include "h_macros.h"
     48  1.2  riastrad 
     49  1.2  riastrad #ifdef HAVE_STACK_POINTER_H
     50  1.1       pho #  include "stack_pointer.h"
     51  1.2  riastrad #endif
     52  1.1       pho 
     53  1.2  riastrad #ifdef HAVE_SIGNALSPHANDLER
     54  1.2  riastrad void signalsphandler(int);	/* signalsphandler.S assembly routine */
     55  1.2  riastrad #endif
     56  1.2  riastrad 
     57  1.2  riastrad void *volatile signalsp;
     58  1.2  riastrad 
     59  1.2  riastrad static void
     60  1.2  riastrad test_execsp(const struct atf_tc *tc, const char *prog)
     61  1.1       pho {
     62  1.2  riastrad #ifdef STACK_ALIGNBYTES
     63  1.2  riastrad 	char h_execsp[PATH_MAX];
     64  1.2  riastrad 	struct execsp execsp;
     65  1.2  riastrad 	int fd[2];
     66  1.2  riastrad 	pid_t pid;
     67  1.2  riastrad 	struct pollfd pollfd;
     68  1.2  riastrad 	int nfds;
     69  1.2  riastrad 	ssize_t nread;
     70  1.2  riastrad 	int status;
     71  1.2  riastrad 
     72  1.1       pho 	/*
     73  1.2  riastrad 	 * Determine the full path to the helper program.
     74  1.1       pho 	 */
     75  1.2  riastrad 	RL(snprintf(h_execsp, sizeof(h_execsp), "%s/%s",
     76  1.2  riastrad 		atf_tc_get_config_var(tc, "srcdir"), prog));
     77  1.2  riastrad 
     78  1.2  riastrad 	/*
     79  1.2  riastrad 	 * Create a pipe to read a bundle of stack pointer samples from
     80  1.2  riastrad 	 * the child, and fork the child.
     81  1.2  riastrad 	 */
     82  1.2  riastrad 	RL(pipe(fd));
     83  1.2  riastrad 	RL(pid = vfork());
     84  1.2  riastrad 	if (pid == 0) {		/* child */
     85  1.2  riastrad 		char *const argv[] = {h_execsp, NULL};
     86  1.2  riastrad 
     87  1.2  riastrad 		if (dup2(fd[1], STDOUT_FILENO) == -1)
     88  1.2  riastrad 			_exit(1);
     89  1.2  riastrad 		if (closefrom(STDERR_FILENO + 1) == -1)
     90  1.2  riastrad 			_exit(2);
     91  1.2  riastrad 		if (execve(argv[0], argv, NULL) == -1)
     92  1.2  riastrad 			_exit(3);
     93  1.2  riastrad 		_exit(4);
     94  1.2  riastrad 	}
     95  1.2  riastrad 
     96  1.2  riastrad 	/*
     97  1.2  riastrad 	 * Close the writing end so, if something goes wrong in the
     98  1.2  riastrad 	 * child, we don't hang indefinitely waiting for output.
     99  1.2  riastrad 	 */
    100  1.2  riastrad 	RL(close(fd[1]));
    101  1.2  riastrad 
    102  1.2  riastrad 	/*
    103  1.2  riastrad 	 * Wait up to 5sec for the child to return an answer.  Any more
    104  1.2  riastrad 	 * than that, and we kill it.  The child is mostly hand-written
    105  1.2  riastrad 	 * assembly routines where lots can go wrong, so don't bother
    106  1.2  riastrad 	 * waiting if it gets stuck in a loop.
    107  1.2  riastrad 	 */
    108  1.2  riastrad 	pollfd.fd = fd[0];
    109  1.2  riastrad 	pollfd.events = POLLIN;
    110  1.2  riastrad 	RL(nfds = poll(&pollfd, 1, 5*1000/*ms*/));
    111  1.2  riastrad 	if (nfds == 0) {
    112  1.2  riastrad 		fprintf(stderr, "child hung, killing\n");
    113  1.2  riastrad 		RL(kill(pid, SIGKILL));
    114  1.2  riastrad 	}
    115  1.1       pho 
    116  1.1       pho 	/*
    117  1.2  riastrad 	 * Read a bundle of stack pointer samples from the child.
    118  1.1       pho 	 */
    119  1.2  riastrad 	RL(nread = read(fd[0], &execsp, sizeof(execsp)));
    120  1.2  riastrad 	ATF_CHECK_MSG((size_t)nread == sizeof(execsp),
    121  1.2  riastrad 	    "nread=%zu sizeof(execsp)=%zu",
    122  1.2  riastrad 	    (size_t)nread, sizeof(execsp));
    123  1.2  riastrad 
    124  1.2  riastrad 	/*
    125  1.2  riastrad 	 * Wait for the child to terminate and report failure if it
    126  1.2  riastrad 	 * didn't exit cleanly.
    127  1.2  riastrad 	 */
    128  1.2  riastrad 	RL(waitpid(pid, &status, 0));
    129  1.2  riastrad 	if (WIFSIGNALED(status)) {
    130  1.2  riastrad 		atf_tc_fail_nonfatal("child exited on signal %d (%s)",
    131  1.2  riastrad 		    WTERMSIG(status), strsignal(WTERMSIG(status)));
    132  1.2  riastrad 	} else if (!WIFEXITED(status)) {
    133  1.2  riastrad 		atf_tc_fail_nonfatal("child exited status=0x%x", status);
    134  1.2  riastrad 	} else {
    135  1.2  riastrad 		ATF_CHECK_MSG(WEXITSTATUS(status) == 0,
    136  1.2  riastrad 		    "child exited with code %d",
    137  1.2  riastrad 		    WEXITSTATUS(status));
    138  1.2  riastrad 	}
    139  1.2  riastrad 
    140  1.2  riastrad 	/*
    141  1.2  riastrad 	 * Now that we have reaped the child, stop here if the stack
    142  1.2  riastrad 	 * pointer samples are bogus; otherwise verify they are all
    143  1.2  riastrad 	 * aligned.
    144  1.2  riastrad 	 */
    145  1.2  riastrad 	if ((size_t)nread != sizeof(execsp))
    146  1.2  riastrad 		return;		/* failed already */
    147  1.2  riastrad 
    148  1.2  riastrad 	printf("start sp @ %p\n", execsp.startsp);
    149  1.3  riastrad 	printf("ctor sp @ %p\n", execsp.ctorsp);
    150  1.2  riastrad 	printf("main sp @ %p\n", execsp.mainsp);
    151  1.3  riastrad 	printf("dtor sp @ %p\n", execsp.dtorsp);
    152  1.2  riastrad 
    153  1.2  riastrad 	ATF_CHECK_MSG(((uintptr_t)execsp.startsp & STACK_ALIGNBYTES) == 0,
    154  1.2  riastrad 	    "elf entry point was called with misaligned sp: %p",
    155  1.2  riastrad 	    execsp.startsp);
    156  1.2  riastrad 
    157  1.3  riastrad 	ATF_CHECK_MSG(((uintptr_t)execsp.ctorsp & STACK_ALIGNBYTES) == 0,
    158  1.3  riastrad 	    "elf constructor was called with misaligned sp: %p",
    159  1.3  riastrad 	    execsp.ctorsp);
    160  1.3  riastrad 
    161  1.2  riastrad 	ATF_CHECK_MSG(((uintptr_t)execsp.mainsp & STACK_ALIGNBYTES) == 0,
    162  1.2  riastrad 	    "main function was called with misaligned sp: %p",
    163  1.2  riastrad 	    execsp.mainsp);
    164  1.2  riastrad 
    165  1.3  riastrad 	ATF_CHECK_MSG(((uintptr_t)execsp.dtorsp & STACK_ALIGNBYTES) == 0,
    166  1.3  riastrad 	    "elf destructor was called with misaligned sp: %p",
    167  1.3  riastrad 	    execsp.dtorsp);
    168  1.3  riastrad 
    169  1.2  riastrad 	/*
    170  1.2  riastrad 	 * Leave a reminder on architectures for which we haven't
    171  1.2  riastrad 	 * implemented execsp_start.S.
    172  1.2  riastrad 	 */
    173  1.3  riastrad 	if (execsp.startsp == NULL ||
    174  1.3  riastrad 	    execsp.ctorsp == NULL ||
    175  1.3  riastrad 	    execsp.mainsp == NULL ||
    176  1.3  riastrad 	    execsp.dtorsp == NULL)
    177  1.2  riastrad 		atf_tc_skip("Not fully supported on this architecture");
    178  1.2  riastrad #else
    179  1.2  riastrad 	atf_tc_skip("Unknown STACK_ALIGNBYTES on this architecture");
    180  1.2  riastrad #endif
    181  1.2  riastrad }
    182  1.2  riastrad 
    183  1.2  riastrad ATF_TC(execsp_dynamic);
    184  1.2  riastrad ATF_TC_HEAD(execsp_dynamic, tc)
    185  1.2  riastrad {
    186  1.2  riastrad 	atf_tc_set_md_var(tc, "descr",
    187  1.2  riastrad 	    "Verify stack pointer is aligned on dynamic program start");
    188  1.2  riastrad }
    189  1.2  riastrad ATF_TC_BODY(execsp_dynamic, tc)
    190  1.2  riastrad {
    191  1.2  riastrad 	test_execsp(tc, "h_execsp_dynamic");
    192  1.1       pho }
    193  1.2  riastrad 
    194  1.2  riastrad ATF_TC(execsp_static);
    195  1.2  riastrad ATF_TC_HEAD(execsp_static, tc)
    196  1.2  riastrad {
    197  1.2  riastrad 	atf_tc_set_md_var(tc, "descr",
    198  1.2  riastrad 	    "Verify stack pointer is aligned on static program start");
    199  1.2  riastrad }
    200  1.2  riastrad ATF_TC_BODY(execsp_static, tc)
    201  1.2  riastrad {
    202  1.2  riastrad 	test_execsp(tc, "h_execsp_static");
    203  1.2  riastrad }
    204  1.2  riastrad 
    205  1.2  riastrad ATF_TC(signalsp);
    206  1.2  riastrad ATF_TC_HEAD(signalsp, tc)
    207  1.2  riastrad {
    208  1.2  riastrad 	atf_tc_set_md_var(tc, "descr",
    209  1.2  riastrad 	    "Verify stack pointer is aligned on entry to signal handler");
    210  1.2  riastrad }
    211  1.2  riastrad ATF_TC_BODY(signalsp, tc)
    212  1.2  riastrad {
    213  1.2  riastrad #if defined STACK_ALIGNBYTES && defined HAVE_SIGNALSPHANDLER
    214  1.2  riastrad 	struct sigaction sa;
    215  1.2  riastrad 
    216  1.2  riastrad 	memset(&sa, 0, sizeof(sa));
    217  1.2  riastrad 	sa.sa_handler = &signalsphandler;
    218  1.2  riastrad 	RL(sigaction(SIGUSR1, &sa, NULL));
    219  1.2  riastrad 	RL(raise(SIGUSR1));
    220  1.2  riastrad 
    221  1.2  riastrad 	ATF_CHECK_MSG(((uintptr_t)signalsp & STACK_ALIGNBYTES) == 0,
    222  1.2  riastrad 	    "signal handler was called with a misaligned sp: %p",
    223  1.2  riastrad 	    signalsp);
    224  1.2  riastrad #else
    225  1.2  riastrad 	atf_tc_skip("Not implemented on this platform");
    226  1.2  riastrad #endif
    227  1.2  riastrad }
    228  1.2  riastrad 
    229  1.2  riastrad ATF_TC(signalsp_sigaltstack);
    230  1.2  riastrad ATF_TC_HEAD(signalsp_sigaltstack, tc)
    231  1.2  riastrad {
    232  1.2  riastrad 	atf_tc_set_md_var(tc, "descr",
    233  1.2  riastrad 	    "Verify stack pointer is aligned on entry to signal handler"
    234  1.2  riastrad 	    " with maximally misaligned sigaltstack");
    235  1.2  riastrad }
    236  1.2  riastrad ATF_TC_BODY(signalsp_sigaltstack, tc)
    237  1.2  riastrad {
    238  1.2  riastrad #if defined STACK_ALIGNBYTES && HAVE_SIGNALSPHANDLER
    239  1.2  riastrad 	char *stack;
    240  1.2  riastrad 	struct sigaction sa;
    241  1.2  riastrad 	struct sigaltstack ss;
    242  1.2  riastrad 	unsigned i;
    243  1.2  riastrad 
    244  1.2  riastrad 	memset(&sa, 0, sizeof(sa));
    245  1.2  riastrad 	sa.sa_handler = &signalsphandler;
    246  1.2  riastrad 	sa.sa_flags = SA_ONSTACK;
    247  1.2  riastrad 	RL(sigaction(SIGUSR1, &sa, NULL));
    248  1.2  riastrad 
    249  1.2  riastrad 	/*
    250  1.2  riastrad 	 * Allocate a signal stack with enough slop to try all possible
    251  1.2  riastrad 	 * misalignments of the stack pointer.  Print it to stderr so
    252  1.2  riastrad 	 * it always appears in atf output before shenanigans happen.
    253  1.2  riastrad 	 */
    254  1.2  riastrad 	REQUIRE_LIBC(stack = malloc(SIGSTKSZ + STACK_ALIGNBYTES), NULL);
    255  1.2  riastrad 	fprintf(stderr, "stack @ [%p, %p)",
    256  1.2  riastrad 	    stack, stack + SIGSTKSZ + STACK_ALIGNBYTES);
    257  1.2  riastrad 
    258  1.2  riastrad 	/*
    259  1.2  riastrad 	 * Try with all alignments of high addresses.
    260  1.2  riastrad 	 */
    261  1.2  riastrad 	for (i = 0; i <= STACK_ALIGNBYTES; i++) {
    262  1.2  riastrad 		ss.ss_sp = stack;
    263  1.2  riastrad 		ss.ss_size = SIGSTKSZ + i;
    264  1.2  riastrad 		ss.ss_flags = 0;
    265  1.2  riastrad 		RL(sigaltstack(&ss, NULL));
    266  1.2  riastrad 
    267  1.2  riastrad 		signalsp = NULL;
    268  1.2  riastrad 		RL(raise(SIGUSR1));
    269  1.2  riastrad 		ATF_CHECK(signalsp != NULL);
    270  1.2  riastrad 		ATF_CHECK_MSG(((uintptr_t)signalsp & STACK_ALIGNBYTES) == 0,
    271  1.2  riastrad 		    "[%u] signal handler was called with a misaligned sp: %p",
    272  1.2  riastrad 		    i, signalsp);
    273  1.2  riastrad 	}
    274  1.2  riastrad 
    275  1.2  riastrad 	/*
    276  1.2  riastrad 	 * Try with all alignments of low addresses.
    277  1.2  riastrad 	 */
    278  1.2  riastrad 	for (i = 0; i <= STACK_ALIGNBYTES; i++) {
    279  1.2  riastrad 		ss.ss_sp = stack + i;
    280  1.2  riastrad 		ss.ss_size = SIGSTKSZ;
    281  1.2  riastrad 		ss.ss_flags = 0;
    282  1.2  riastrad 		RL(sigaltstack(&ss, NULL));
    283  1.2  riastrad 
    284  1.2  riastrad 		signalsp = NULL;
    285  1.2  riastrad 		RL(raise(SIGUSR1));
    286  1.2  riastrad 		ATF_CHECK(signalsp != NULL);
    287  1.2  riastrad 		ATF_CHECK_MSG(((uintptr_t)signalsp & STACK_ALIGNBYTES) == 0,
    288  1.2  riastrad 		    "[%u] signal handler was called with a misaligned sp: %p",
    289  1.2  riastrad 		    i, signalsp);
    290  1.2  riastrad 	}
    291  1.2  riastrad #else
    292  1.2  riastrad 	atf_tc_skip("Not implemented on this platform");
    293  1.1       pho #endif
    294  1.2  riastrad }
    295  1.1       pho 
    296  1.1       pho ATF_TC(misaligned_sp_and_signal);
    297  1.1       pho ATF_TC_HEAD(misaligned_sp_and_signal, tc)
    298  1.1       pho {
    299  1.1       pho 	atf_tc_set_md_var(tc, "descr", "process can return from a signal"
    300  1.1       pho 	    " handler even if the stack pointer is misaligned when a signal"
    301  1.1       pho 	    " arrives");
    302  1.1       pho }
    303  1.1       pho ATF_TC_BODY(misaligned_sp_and_signal, tc)
    304  1.1       pho {
    305  1.2  riastrad #if defined STACK_ALIGNBYTES && defined HAVE_STACK_POINTER_H
    306  1.1       pho 	/*
    307  1.1       pho 	 * Set up a handler for SIGALRM.
    308  1.1       pho 	 */
    309  1.1       pho 	struct sigaction sa;
    310  1.1       pho 	memset(&sa, 0, sizeof(sa));
    311  1.2  riastrad 	sa.sa_handler = &signalsphandler;
    312  1.2  riastrad 	RL(sigaction(SIGALRM, &sa, NULL));
    313  1.1       pho 
    314  1.1       pho 	/*
    315  1.1       pho 	 * Set up an interval timer so that we receive SIGALRM after 50 ms.
    316  1.1       pho 	 */
    317  1.1       pho 	struct itimerval itv;
    318  1.1       pho 	memset(&itv, 0, sizeof(itv));
    319  1.1       pho 	itv.it_value.tv_usec = 1000 * 50;
    320  1.2  riastrad 	RL(setitimer(ITIMER_MONOTONIC, &itv, NULL));
    321  1.1       pho 
    322  1.1       pho 	/*
    323  1.1       pho 	 * Now misalign the SP. Wait for the signal to arrive and see what
    324  1.1       pho 	 * happens. This should be fine as long as we don't use it to
    325  1.1       pho 	 * access memory.
    326  1.1       pho 	 */
    327  1.1       pho 	MISALIGN_SP;
    328  1.2  riastrad 	while (signalsp == NULL) {
    329  1.1       pho 		/*
    330  1.1       pho 		 * Make sure the compiler does not optimize this busy loop
    331  1.1       pho 		 * away.
    332  1.1       pho 		 */
    333  1.2  riastrad 		__asm__("" ::: "memory");
    334  1.1       pho 	}
    335  1.1       pho 	/*
    336  1.1       pho 	 * We could successfully return from a signal handler. Now we
    337  1.1       pho 	 * should fix the SP before calling any functions.
    338  1.1       pho 	 */
    339  1.1       pho 	FIX_SP;
    340  1.1       pho 
    341  1.1       pho 	/*
    342  1.1       pho 	 * But was the stack pointer aligned when we were on the signal
    343  1.1       pho 	 * handler?
    344  1.1       pho 	 */
    345  1.2  riastrad 	ATF_CHECK_MSG(((uintptr_t)signalsp & STACK_ALIGNBYTES) == 0,
    346  1.1       pho 	    "signal handler was called with a misaligned sp: %p",
    347  1.2  riastrad 	    signalsp);
    348  1.1       pho #else
    349  1.1       pho 	atf_tc_skip("Not implemented for this platform");
    350  1.1       pho #endif
    351  1.1       pho }
    352  1.1       pho 
    353  1.1       pho ATF_TP_ADD_TCS(tp)
    354  1.1       pho {
    355  1.2  riastrad 
    356  1.2  riastrad 	ATF_TP_ADD_TC(tp, execsp_dynamic);
    357  1.2  riastrad 	ATF_TP_ADD_TC(tp, execsp_static);
    358  1.1       pho 	ATF_TP_ADD_TC(tp, misaligned_sp_and_signal);
    359  1.2  riastrad 	ATF_TP_ADD_TC(tp, signalsp);
    360  1.2  riastrad 	ATF_TP_ADD_TC(tp, signalsp_sigaltstack);
    361  1.1       pho 	return atf_no_error();
    362  1.1       pho }
    363