mirror of
git://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
synced 2025-08-05 16:54:27 +00:00

The x86 selftests frequently register and clean up signal handlers, but the sethandler() and clearhandler() functions have been redundantly copied across multiple .c files. Move these functions to helpers.h to enable reuse across tests, eliminating around 250 lines of duplicate code. Converge the error handling by using ksft_exit_fail_msg(), which is functionally equivalent with err() within the selftest framework. This change is a prerequisite for the upcoming xstate selftest, which requires signal handling for registering and cleaning up handlers. Signed-off-by: Chang S. Bae <chang.seok.bae@intel.com> Signed-off-by: Ingo Molnar <mingo@kernel.org> Link: https://lore.kernel.org/r/20250226010731.2456-2-chang.seok.bae@intel.com
106 lines
2.3 KiB
C
106 lines
2.3 KiB
C
// SPDX-License-Identifier: GPL-2.0-only
|
|
|
|
#define _GNU_SOURCE
|
|
#include <signal.h>
|
|
#include <stdio.h>
|
|
#include <stdbool.h>
|
|
#include <string.h>
|
|
#include <err.h>
|
|
#include <errno.h>
|
|
#include <limits.h>
|
|
#include <sys/mman.h>
|
|
#include <sys/auxv.h>
|
|
#include <sys/prctl.h>
|
|
#include <sys/resource.h>
|
|
#include <setjmp.h>
|
|
|
|
#include "helpers.h"
|
|
|
|
/* sigaltstack()-enforced minimum stack */
|
|
#define ENFORCED_MINSIGSTKSZ 2048
|
|
|
|
#ifndef AT_MINSIGSTKSZ
|
|
# define AT_MINSIGSTKSZ 51
|
|
#endif
|
|
|
|
static int nerrs;
|
|
|
|
static bool sigalrm_expected;
|
|
|
|
static unsigned long at_minstack_size;
|
|
|
|
static int setup_altstack(void *start, unsigned long size)
|
|
{
|
|
stack_t ss;
|
|
|
|
memset(&ss, 0, sizeof(ss));
|
|
ss.ss_size = size;
|
|
ss.ss_sp = start;
|
|
|
|
return sigaltstack(&ss, NULL);
|
|
}
|
|
|
|
static jmp_buf jmpbuf;
|
|
|
|
static void sigsegv(int sig, siginfo_t *info, void *ctx_void)
|
|
{
|
|
if (sigalrm_expected) {
|
|
printf("[FAIL]\tWrong signal delivered: SIGSEGV (expected SIGALRM).");
|
|
nerrs++;
|
|
} else {
|
|
printf("[OK]\tSIGSEGV signal delivered.\n");
|
|
}
|
|
|
|
siglongjmp(jmpbuf, 1);
|
|
}
|
|
|
|
static void sigalrm(int sig, siginfo_t *info, void *ctx_void)
|
|
{
|
|
if (!sigalrm_expected) {
|
|
printf("[FAIL]\tWrong signal delivered: SIGALRM (expected SIGSEGV).");
|
|
nerrs++;
|
|
} else {
|
|
printf("[OK]\tSIGALRM signal delivered.\n");
|
|
}
|
|
}
|
|
|
|
static void test_sigaltstack(void *altstack, unsigned long size)
|
|
{
|
|
if (setup_altstack(altstack, size))
|
|
err(1, "sigaltstack()");
|
|
|
|
sigalrm_expected = (size > at_minstack_size) ? true : false;
|
|
|
|
sethandler(SIGSEGV, sigsegv, 0);
|
|
sethandler(SIGALRM, sigalrm, SA_ONSTACK);
|
|
|
|
if (!sigsetjmp(jmpbuf, 1)) {
|
|
printf("[RUN]\tTest an alternate signal stack of %ssufficient size.\n",
|
|
sigalrm_expected ? "" : "in");
|
|
printf("\tRaise SIGALRM. %s is expected to be delivered.\n",
|
|
sigalrm_expected ? "It" : "SIGSEGV");
|
|
raise(SIGALRM);
|
|
}
|
|
|
|
clearhandler(SIGALRM);
|
|
clearhandler(SIGSEGV);
|
|
}
|
|
|
|
int main(void)
|
|
{
|
|
void *altstack;
|
|
|
|
at_minstack_size = getauxval(AT_MINSIGSTKSZ);
|
|
|
|
altstack = mmap(NULL, at_minstack_size + SIGSTKSZ, PROT_READ | PROT_WRITE,
|
|
MAP_PRIVATE | MAP_ANONYMOUS | MAP_STACK, -1, 0);
|
|
if (altstack == MAP_FAILED)
|
|
err(1, "mmap()");
|
|
|
|
if ((ENFORCED_MINSIGSTKSZ + 1) < at_minstack_size)
|
|
test_sigaltstack(altstack, ENFORCED_MINSIGSTKSZ + 1);
|
|
|
|
test_sigaltstack(altstack, at_minstack_size + SIGSTKSZ);
|
|
|
|
return nerrs == 0 ? 0 : 1;
|
|
}
|