Home | History | Annotate | Line # | Download | only in kernel
t_signal_and_sp.c revision 1.2
      1  1.2  riastrad /*	$NetBSD: t_signal_and_sp.c,v 1.2 2025/04/20 22:31:00 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.2  riastrad __RCSID("$NetBSD: t_signal_and_sp.c,v 1.2 2025/04/20 22:31:00 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.2  riastrad 	printf("main sp @ %p\n", execsp.mainsp);
    150  1.2  riastrad 
    151  1.2  riastrad 	ATF_CHECK_MSG(((uintptr_t)execsp.startsp & STACK_ALIGNBYTES) == 0,
    152  1.2  riastrad 	    "elf entry point was called with misaligned sp: %p",
    153  1.2  riastrad 	    execsp.startsp);
    154  1.2  riastrad 
    155  1.2  riastrad 	ATF_CHECK_MSG(((uintptr_t)execsp.mainsp & STACK_ALIGNBYTES) == 0,
    156  1.2  riastrad 	    "main function was called with misaligned sp: %p",
    157  1.2  riastrad 	    execsp.mainsp);
    158  1.2  riastrad 
    159  1.2  riastrad 	/*
    160  1.2  riastrad 	 * Leave a reminder on architectures for which we haven't
    161  1.2  riastrad 	 * implemented execsp_start.S.
    162  1.2  riastrad 	 */
    163  1.2  riastrad 	if (execsp.startsp == NULL || execsp.mainsp == NULL)
    164  1.2  riastrad 		atf_tc_skip("Not fully supported on this architecture");
    165  1.2  riastrad #else
    166  1.2  riastrad 	atf_tc_skip("Unknown STACK_ALIGNBYTES on this architecture");
    167  1.2  riastrad #endif
    168  1.2  riastrad }
    169  1.2  riastrad 
    170  1.2  riastrad ATF_TC(execsp_dynamic);
    171  1.2  riastrad ATF_TC_HEAD(execsp_dynamic, tc)
    172  1.2  riastrad {
    173  1.2  riastrad 	atf_tc_set_md_var(tc, "descr",
    174  1.2  riastrad 	    "Verify stack pointer is aligned on dynamic program start");
    175  1.2  riastrad }
    176  1.2  riastrad ATF_TC_BODY(execsp_dynamic, tc)
    177  1.2  riastrad {
    178  1.2  riastrad 	test_execsp(tc, "h_execsp_dynamic");
    179  1.1       pho }
    180  1.2  riastrad 
    181  1.2  riastrad ATF_TC(execsp_static);
    182  1.2  riastrad ATF_TC_HEAD(execsp_static, tc)
    183  1.2  riastrad {
    184  1.2  riastrad 	atf_tc_set_md_var(tc, "descr",
    185  1.2  riastrad 	    "Verify stack pointer is aligned on static program start");
    186  1.2  riastrad }
    187  1.2  riastrad ATF_TC_BODY(execsp_static, tc)
    188  1.2  riastrad {
    189  1.2  riastrad 	test_execsp(tc, "h_execsp_static");
    190  1.2  riastrad }
    191  1.2  riastrad 
    192  1.2  riastrad ATF_TC(signalsp);
    193  1.2  riastrad ATF_TC_HEAD(signalsp, tc)
    194  1.2  riastrad {
    195  1.2  riastrad 	atf_tc_set_md_var(tc, "descr",
    196  1.2  riastrad 	    "Verify stack pointer is aligned on entry to signal handler");
    197  1.2  riastrad }
    198  1.2  riastrad ATF_TC_BODY(signalsp, tc)
    199  1.2  riastrad {
    200  1.2  riastrad #if defined STACK_ALIGNBYTES && defined HAVE_SIGNALSPHANDLER
    201  1.2  riastrad 	struct sigaction sa;
    202  1.2  riastrad 
    203  1.2  riastrad 	memset(&sa, 0, sizeof(sa));
    204  1.2  riastrad 	sa.sa_handler = &signalsphandler;
    205  1.2  riastrad 	RL(sigaction(SIGUSR1, &sa, NULL));
    206  1.2  riastrad 	RL(raise(SIGUSR1));
    207  1.2  riastrad 
    208  1.2  riastrad 	ATF_CHECK_MSG(((uintptr_t)signalsp & STACK_ALIGNBYTES) == 0,
    209  1.2  riastrad 	    "signal handler was called with a misaligned sp: %p",
    210  1.2  riastrad 	    signalsp);
    211  1.2  riastrad #else
    212  1.2  riastrad 	atf_tc_skip("Not implemented on this platform");
    213  1.2  riastrad #endif
    214  1.2  riastrad }
    215  1.2  riastrad 
    216  1.2  riastrad ATF_TC(signalsp_sigaltstack);
    217  1.2  riastrad ATF_TC_HEAD(signalsp_sigaltstack, tc)
    218  1.2  riastrad {
    219  1.2  riastrad 	atf_tc_set_md_var(tc, "descr",
    220  1.2  riastrad 	    "Verify stack pointer is aligned on entry to signal handler"
    221  1.2  riastrad 	    " with maximally misaligned sigaltstack");
    222  1.2  riastrad }
    223  1.2  riastrad ATF_TC_BODY(signalsp_sigaltstack, tc)
    224  1.2  riastrad {
    225  1.2  riastrad #if defined STACK_ALIGNBYTES && HAVE_SIGNALSPHANDLER
    226  1.2  riastrad 	char *stack;
    227  1.2  riastrad 	struct sigaction sa;
    228  1.2  riastrad 	struct sigaltstack ss;
    229  1.2  riastrad 	unsigned i;
    230  1.2  riastrad 
    231  1.2  riastrad 	memset(&sa, 0, sizeof(sa));
    232  1.2  riastrad 	sa.sa_handler = &signalsphandler;
    233  1.2  riastrad 	sa.sa_flags = SA_ONSTACK;
    234  1.2  riastrad 	RL(sigaction(SIGUSR1, &sa, NULL));
    235  1.2  riastrad 
    236  1.2  riastrad 	/*
    237  1.2  riastrad 	 * Allocate a signal stack with enough slop to try all possible
    238  1.2  riastrad 	 * misalignments of the stack pointer.  Print it to stderr so
    239  1.2  riastrad 	 * it always appears in atf output before shenanigans happen.
    240  1.2  riastrad 	 */
    241  1.2  riastrad 	REQUIRE_LIBC(stack = malloc(SIGSTKSZ + STACK_ALIGNBYTES), NULL);
    242  1.2  riastrad 	fprintf(stderr, "stack @ [%p, %p)",
    243  1.2  riastrad 	    stack, stack + SIGSTKSZ + STACK_ALIGNBYTES);
    244  1.2  riastrad 
    245  1.2  riastrad 	/*
    246  1.2  riastrad 	 * Try with all alignments of high addresses.
    247  1.2  riastrad 	 */
    248  1.2  riastrad 	for (i = 0; i <= STACK_ALIGNBYTES; i++) {
    249  1.2  riastrad 		ss.ss_sp = stack;
    250  1.2  riastrad 		ss.ss_size = SIGSTKSZ + i;
    251  1.2  riastrad 		ss.ss_flags = 0;
    252  1.2  riastrad 		RL(sigaltstack(&ss, NULL));
    253  1.2  riastrad 
    254  1.2  riastrad 		signalsp = NULL;
    255  1.2  riastrad 		RL(raise(SIGUSR1));
    256  1.2  riastrad 		ATF_CHECK(signalsp != NULL);
    257  1.2  riastrad 		ATF_CHECK_MSG(((uintptr_t)signalsp & STACK_ALIGNBYTES) == 0,
    258  1.2  riastrad 		    "[%u] signal handler was called with a misaligned sp: %p",
    259  1.2  riastrad 		    i, signalsp);
    260  1.2  riastrad 	}
    261  1.2  riastrad 
    262  1.2  riastrad 	/*
    263  1.2  riastrad 	 * Try with all alignments of low addresses.
    264  1.2  riastrad 	 */
    265  1.2  riastrad 	for (i = 0; i <= STACK_ALIGNBYTES; i++) {
    266  1.2  riastrad 		ss.ss_sp = stack + i;
    267  1.2  riastrad 		ss.ss_size = SIGSTKSZ;
    268  1.2  riastrad 		ss.ss_flags = 0;
    269  1.2  riastrad 		RL(sigaltstack(&ss, NULL));
    270  1.2  riastrad 
    271  1.2  riastrad 		signalsp = NULL;
    272  1.2  riastrad 		RL(raise(SIGUSR1));
    273  1.2  riastrad 		ATF_CHECK(signalsp != NULL);
    274  1.2  riastrad 		ATF_CHECK_MSG(((uintptr_t)signalsp & STACK_ALIGNBYTES) == 0,
    275  1.2  riastrad 		    "[%u] signal handler was called with a misaligned sp: %p",
    276  1.2  riastrad 		    i, signalsp);
    277  1.2  riastrad 	}
    278  1.2  riastrad #else
    279  1.2  riastrad 	atf_tc_skip("Not implemented on this platform");
    280  1.1       pho #endif
    281  1.2  riastrad }
    282  1.1       pho 
    283  1.1       pho ATF_TC(misaligned_sp_and_signal);
    284  1.1       pho ATF_TC_HEAD(misaligned_sp_and_signal, tc)
    285  1.1       pho {
    286  1.1       pho 	atf_tc_set_md_var(tc, "descr", "process can return from a signal"
    287  1.1       pho 	    " handler even if the stack pointer is misaligned when a signal"
    288  1.1       pho 	    " arrives");
    289  1.1       pho }
    290  1.1       pho ATF_TC_BODY(misaligned_sp_and_signal, tc)
    291  1.1       pho {
    292  1.2  riastrad #if defined STACK_ALIGNBYTES && defined HAVE_STACK_POINTER_H
    293  1.1       pho 	/*
    294  1.1       pho 	 * Set up a handler for SIGALRM.
    295  1.1       pho 	 */
    296  1.1       pho 	struct sigaction sa;
    297  1.1       pho 	memset(&sa, 0, sizeof(sa));
    298  1.2  riastrad 	sa.sa_handler = &signalsphandler;
    299  1.2  riastrad 	RL(sigaction(SIGALRM, &sa, NULL));
    300  1.1       pho 
    301  1.1       pho 	/*
    302  1.1       pho 	 * Set up an interval timer so that we receive SIGALRM after 50 ms.
    303  1.1       pho 	 */
    304  1.1       pho 	struct itimerval itv;
    305  1.1       pho 	memset(&itv, 0, sizeof(itv));
    306  1.1       pho 	itv.it_value.tv_usec = 1000 * 50;
    307  1.2  riastrad 	RL(setitimer(ITIMER_MONOTONIC, &itv, NULL));
    308  1.1       pho 
    309  1.1       pho 	/*
    310  1.1       pho 	 * Now misalign the SP. Wait for the signal to arrive and see what
    311  1.1       pho 	 * happens. This should be fine as long as we don't use it to
    312  1.1       pho 	 * access memory.
    313  1.1       pho 	 */
    314  1.1       pho 	MISALIGN_SP;
    315  1.2  riastrad 	while (signalsp == NULL) {
    316  1.1       pho 		/*
    317  1.1       pho 		 * Make sure the compiler does not optimize this busy loop
    318  1.1       pho 		 * away.
    319  1.1       pho 		 */
    320  1.2  riastrad 		__asm__("" ::: "memory");
    321  1.1       pho 	}
    322  1.1       pho 	/*
    323  1.1       pho 	 * We could successfully return from a signal handler. Now we
    324  1.1       pho 	 * should fix the SP before calling any functions.
    325  1.1       pho 	 */
    326  1.1       pho 	FIX_SP;
    327  1.1       pho 
    328  1.1       pho 	/*
    329  1.1       pho 	 * But was the stack pointer aligned when we were on the signal
    330  1.1       pho 	 * handler?
    331  1.1       pho 	 */
    332  1.2  riastrad 	ATF_CHECK_MSG(((uintptr_t)signalsp & STACK_ALIGNBYTES) == 0,
    333  1.1       pho 	    "signal handler was called with a misaligned sp: %p",
    334  1.2  riastrad 	    signalsp);
    335  1.1       pho #else
    336  1.1       pho 	atf_tc_skip("Not implemented for this platform");
    337  1.1       pho #endif
    338  1.1       pho }
    339  1.1       pho 
    340  1.1       pho ATF_TP_ADD_TCS(tp)
    341  1.1       pho {
    342  1.2  riastrad 
    343  1.2  riastrad 	ATF_TP_ADD_TC(tp, execsp_dynamic);
    344  1.2  riastrad 	ATF_TP_ADD_TC(tp, execsp_static);
    345  1.1       pho 	ATF_TP_ADD_TC(tp, misaligned_sp_and_signal);
    346  1.2  riastrad 	ATF_TP_ADD_TC(tp, signalsp);
    347  1.2  riastrad 	ATF_TP_ADD_TC(tp, signalsp_sigaltstack);
    348  1.1       pho 	return atf_no_error();
    349  1.1       pho }
    350