/*
* Copyright (c) 2024 The NetBSD Foundation, Inc.
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
* ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
* TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
* BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*/
static void
test_execsp(const struct atf_tc *tc, const char *prog)
{
#ifdef STACK_ALIGNBYTES
char h_execsp[PATH_MAX];
struct execsp execsp;
int fd[2];
pid_t pid;
struct pollfd pollfd;
int nfds;
ssize_t nread;
int status;
/*
* Determine the full path to the helper program.
*/
RL(snprintf(h_execsp, sizeof(h_execsp), "%s/%s",
atf_tc_get_config_var(tc, "srcdir"), prog));
/*
* Create a pipe to read a bundle of stack pointer samples from
* the child, and fork the child.
*/
RL(pipe(fd));
RL(pid = vfork());
if (pid == 0) { /* child */
char *const argv[] = {h_execsp, NULL};
if (dup2(fd[1], STDOUT_FILENO) == -1)
_exit(1);
if (closefrom(STDERR_FILENO + 1) == -1)
_exit(2);
if (execve(argv[0], argv, NULL) == -1)
_exit(3);
_exit(4);
}
/*
* Close the writing end so, if something goes wrong in the
* child, we don't hang indefinitely waiting for output.
*/
RL(close(fd[1]));
/*
* Wait up to 5sec for the child to return an answer. Any more
* than that, and we kill it. The child is mostly hand-written
* assembly routines where lots can go wrong, so don't bother
* waiting if it gets stuck in a loop.
*/
pollfd.fd = fd[0];
pollfd.events = POLLIN;
RL(nfds = poll(&pollfd, 1, 5*1000/*ms*/));
if (nfds == 0) {
fprintf(stderr, "child hung, killing\n");
RL(kill(pid, SIGKILL));
}
/*
* Read a bundle of stack pointer samples from the child.
*/
RL(nread = read(fd[0], &execsp, sizeof(execsp)));
ATF_CHECK_MSG((size_t)nread == sizeof(execsp),
"nread=%zu sizeof(execsp)=%zu",
(size_t)nread, sizeof(execsp));
/*
* Wait for the child to terminate and report failure if it
* didn't exit cleanly.
*/
RL(waitpid(pid, &status, 0));
if (WIFSIGNALED(status)) {
atf_tc_fail_nonfatal("child exited on signal %d (%s)",
WTERMSIG(status), strsignal(WTERMSIG(status)));
} else if (!WIFEXITED(status)) {
atf_tc_fail_nonfatal("child exited status=0x%x", status);
} else {
ATF_CHECK_MSG(WEXITSTATUS(status) == 0,
"child exited with code %d",
WEXITSTATUS(status));
}
/*
* Now that we have reaped the child, stop here if the stack
* pointer samples are bogus; otherwise verify they are all
* aligned.
*/
if ((size_t)nread != sizeof(execsp))
return; /* failed already */
ATF_CHECK_MSG(((uintptr_t)execsp.startsp & STACK_ALIGNBYTES) == 0,
"elf entry point was called with misaligned sp: %p",
execsp.startsp);
ATF_CHECK_MSG(((uintptr_t)execsp.ctorsp & STACK_ALIGNBYTES) == 0,
"elf constructor was called with misaligned sp: %p",
execsp.ctorsp);
ATF_CHECK_MSG(((uintptr_t)execsp.mainsp & STACK_ALIGNBYTES) == 0,
"main function was called with misaligned sp: %p",
execsp.mainsp);
ATF_CHECK_MSG(((uintptr_t)execsp.dtorsp & STACK_ALIGNBYTES) == 0,
"elf destructor was called with misaligned sp: %p",
execsp.dtorsp);
/*
* Leave a reminder on architectures for which we haven't
* implemented execsp_start.S.
*/
if (execsp.startsp == NULL ||
execsp.ctorsp == NULL ||
execsp.mainsp == NULL ||
execsp.dtorsp == NULL)
atf_tc_skip("Not fully supported on this architecture");
#else
atf_tc_skip("Unknown STACK_ALIGNBYTES on this architecture");
#endif
}
ATF_TC(execsp_dynamic);
ATF_TC_HEAD(execsp_dynamic, tc)
{
atf_tc_set_md_var(tc, "descr",
"Verify stack pointer is aligned on dynamic program start");
}
ATF_TC_BODY(execsp_dynamic, tc)
{
test_execsp(tc, "h_execsp_dynamic");
}
ATF_TC(execsp_static);
ATF_TC_HEAD(execsp_static, tc)
{
atf_tc_set_md_var(tc, "descr",
"Verify stack pointer is aligned on static program start");
}
ATF_TC_BODY(execsp_static, tc)
{
test_execsp(tc, "h_execsp_static");
}
#if defined STACK_ALIGNBYTES && defined HAVE_CONTEXTSPFUNC
void *volatile contextsp; /* set by contextspfunc.S */
static ucontext_t return_context;
static volatile bool test_context_done;
ATF_CHECK(contextsp != NULL);
ATF_CHECK_MSG((uintptr_t)stack2 + i <= (uintptr_t)contextsp &&
(uintptr_t)contextsp <= (uintptr_t)stack2 + SIGSTKSZ + i,
"contextsp=%p", contextsp);
ATF_CHECK_MSG(((uintptr_t)contextsp & STACK_ALIGNBYTES) == 0,
"[%u] makecontext function called with misaligned sp %p",
i, contextsp);
}
#else
atf_tc_skip("Not implemented on this platform");
#endif
}
ATF_TC(signalsp);
ATF_TC_HEAD(signalsp, tc)
{
atf_tc_set_md_var(tc, "descr",
"Verify stack pointer is aligned on entry to signal handler");
}
ATF_TC_BODY(signalsp, tc)
{
#if defined STACK_ALIGNBYTES && defined HAVE_SIGNALSPHANDLER
struct sigaction sa;
ATF_CHECK_MSG(((uintptr_t)signalsp & STACK_ALIGNBYTES) == 0,
"signal handler was called with a misaligned sp: %p",
signalsp);
#else
atf_tc_skip("Not implemented on this platform");
#endif
}
ATF_TC(signalsp_sigaltstack);
ATF_TC_HEAD(signalsp_sigaltstack, tc)
{
atf_tc_set_md_var(tc, "descr",
"Verify stack pointer is aligned on entry to signal handler"
" with maximally misaligned sigaltstack");
}
ATF_TC_BODY(signalsp_sigaltstack, tc)
{
#if defined STACK_ALIGNBYTES && HAVE_SIGNALSPHANDLER
#if defined(__sh__)
atf_tc_expect_fail(PR_59327);
#endif
char *stack;
struct sigaction sa;
struct sigaltstack ss;
unsigned i;
/*
* Allocate a signal stack with enough slop to try all possible
* misalignments of the stack pointer. Print it to stderr so
* it always appears in atf output before shenanigans happen.
*/
REQUIRE_LIBC(stack = malloc(SIGSTKSZ + STACK_ALIGNBYTES), NULL);
fprintf(stderr, "stack @ [%p, %p)\n",
stack, stack + SIGSTKSZ + STACK_ALIGNBYTES);
/*
* Try with all alignments of high addresses.
*/
for (i = 0; i <= STACK_ALIGNBYTES; i++) {
ss.ss_sp = stack;
ss.ss_size = SIGSTKSZ + i;
ss.ss_flags = 0;
RL(sigaltstack(&ss, NULL));
signalsp = NULL;
RL(raise(SIGUSR1));
ATF_CHECK(signalsp != NULL);
ATF_CHECK_MSG((uintptr_t)stack <= (uintptr_t)signalsp &&
(uintptr_t)signalsp <= (uintptr_t)stack + SIGSTKSZ + i,
"signalsp=%p", signalsp);
ATF_CHECK_MSG(((uintptr_t)signalsp & STACK_ALIGNBYTES) == 0,
"[%u] signal handler was called with a misaligned sp: %p",
i, signalsp);
}
/*
* Try with all alignments of low addresses.
*/
for (i = 0; i <= STACK_ALIGNBYTES; i++) {
ss.ss_sp = stack + i;
ss.ss_size = SIGSTKSZ;
ss.ss_flags = 0;
RL(sigaltstack(&ss, NULL));
signalsp = NULL;
RL(raise(SIGUSR1));
ATF_CHECK(signalsp != NULL);
ATF_CHECK_MSG((uintptr_t)stack + i <= (uintptr_t)signalsp &&
(uintptr_t)signalsp <= (uintptr_t)stack + i + SIGSTKSZ,
"signalsp=%p", signalsp);
ATF_CHECK_MSG(((uintptr_t)signalsp & STACK_ALIGNBYTES) == 0,
"[%u] signal handler was called with a misaligned sp: %p",
i, signalsp);
}
#else
atf_tc_skip("Not implemented on this platform");
#endif
}
#if defined STACK_ALIGNBYTES && defined HAVE_THREADSPFUNC
void *threadspfunc(void *); /* threadspfunc.S assembly routine */
#endif
ATF_TC(threadsp);
ATF_TC_HEAD(threadsp, tc)
{
atf_tc_set_md_var(tc, "descr",
"Verify stack pointer is aligned on thread start");
}
ATF_TC_BODY(threadsp, tc)
{
#if defined STACK_ALIGNBYTES && defined HAVE_THREADSPFUNC
pthread_t t;
void *sp;
char *stack;
unsigned i;
ATF_CHECK(sp != NULL);
ATF_CHECK_MSG((uintptr_t)stack + i <= (uintptr_t)sp &&
(uintptr_t)sp <= (uintptr_t)stack + i + SIGSTKSZ,
"sp=%p", sp);
ATF_CHECK_MSG(((uintptr_t)sp & STACK_ALIGNBYTES) == 0,
"[%u] thread called with misaligned sp: %p", i, sp);
}
#else
atf_tc_skip("Not implemented on this platform");
#endif
}
ATF_TC(misaligned_sp_and_signal);
ATF_TC_HEAD(misaligned_sp_and_signal, tc)
{
atf_tc_set_md_var(tc, "descr", "process can return from a signal"
" handler even if the stack pointer is misaligned when a signal"
" arrives");
}
ATF_TC_BODY(misaligned_sp_and_signal, tc)
{
#if defined STACK_ALIGNBYTES && defined HAVE_STACK_POINTER_H
#if defined(__sh__)
atf_tc_expect_fail(PR_59327);
#endif
/*
* Set up a handler for SIGALRM.
*/
struct sigaction sa;
memset(&sa, 0, sizeof(sa));
sa.sa_handler = &signalsphandler;
RL(sigaction(SIGALRM, &sa, NULL));
/*
* Set up an interval timer so that we receive SIGALRM after 50 ms.
*/
struct itimerval itv;
memset(&itv, 0, sizeof(itv));
itv.it_value.tv_usec = 1000 * 50;
RL(setitimer(ITIMER_MONOTONIC, &itv, NULL));
/*
* Now misalign the SP. Wait for the signal to arrive and see what
* happens. This should be fine as long as we don't use it to
* access memory.
*/
MISALIGN_SP;
while (signalsp == NULL) {
/*
* Make sure the compiler does not optimize this busy loop
* away.
*/
__asm__("" ::: "memory");
}
/*
* We could successfully return from a signal handler. Now we
* should fix the SP before calling any functions.
*/
FIX_SP;
/*
* But was the stack pointer aligned when we were on the signal
* handler?
*/
ATF_CHECK_MSG(((uintptr_t)signalsp & STACK_ALIGNBYTES) == 0,
"signal handler was called with a misaligned sp: %p",
signalsp);
#else
atf_tc_skip("Not implemented for this platform");
#endif
}