~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~

TOMOYO Linux Cross Reference
Linux/tools/testing/selftests/x86/sigaltstack.c

Version: ~ [ linux-6.11-rc3 ] ~ [ linux-6.10.4 ] ~ [ linux-6.9.12 ] ~ [ linux-6.8.12 ] ~ [ linux-6.7.12 ] ~ [ linux-6.6.45 ] ~ [ linux-6.5.13 ] ~ [ linux-6.4.16 ] ~ [ linux-6.3.13 ] ~ [ linux-6.2.16 ] ~ [ linux-6.1.104 ] ~ [ linux-6.0.19 ] ~ [ linux-5.19.17 ] ~ [ linux-5.18.19 ] ~ [ linux-5.17.15 ] ~ [ linux-5.16.20 ] ~ [ linux-5.15.164 ] ~ [ linux-5.14.21 ] ~ [ linux-5.13.19 ] ~ [ linux-5.12.19 ] ~ [ linux-5.11.22 ] ~ [ linux-5.10.223 ] ~ [ linux-5.9.16 ] ~ [ linux-5.8.18 ] ~ [ linux-5.7.19 ] ~ [ linux-5.6.19 ] ~ [ linux-5.5.19 ] ~ [ linux-5.4.281 ] ~ [ linux-5.3.18 ] ~ [ linux-5.2.21 ] ~ [ linux-5.1.21 ] ~ [ linux-5.0.21 ] ~ [ linux-4.20.17 ] ~ [ linux-4.19.319 ] ~ [ linux-4.18.20 ] ~ [ linux-4.17.19 ] ~ [ linux-4.16.18 ] ~ [ linux-4.15.18 ] ~ [ linux-4.14.336 ] ~ [ linux-4.13.16 ] ~ [ linux-4.12.14 ] ~ [ linux-4.11.12 ] ~ [ linux-4.10.17 ] ~ [ linux-4.9.337 ] ~ [ linux-4.4.302 ] ~ [ linux-3.10.108 ] ~ [ linux-2.6.32.71 ] ~ [ linux-2.6.0 ] ~ [ linux-2.4.37.11 ] ~ [ unix-v6-master ] ~ [ ccs-tools-1.8.9 ] ~ [ policy-sample ] ~
Architecture: ~ [ i386 ] ~ [ alpha ] ~ [ m68k ] ~ [ mips ] ~ [ ppc ] ~ [ sparc ] ~ [ sparc64 ] ~

  1 // SPDX-License-Identifier: GPL-2.0-only
  2 
  3 #define _GNU_SOURCE
  4 #include <signal.h>
  5 #include <stdio.h>
  6 #include <stdbool.h>
  7 #include <string.h>
  8 #include <err.h>
  9 #include <errno.h>
 10 #include <limits.h>
 11 #include <sys/mman.h>
 12 #include <sys/auxv.h>
 13 #include <sys/prctl.h>
 14 #include <sys/resource.h>
 15 #include <setjmp.h>
 16 
 17 /* sigaltstack()-enforced minimum stack */
 18 #define ENFORCED_MINSIGSTKSZ    2048
 19 
 20 #ifndef AT_MINSIGSTKSZ
 21 #  define AT_MINSIGSTKSZ        51
 22 #endif
 23 
 24 static int nerrs;
 25 
 26 static bool sigalrm_expected;
 27 
 28 static unsigned long at_minstack_size;
 29 
 30 static void sethandler(int sig, void (*handler)(int, siginfo_t *, void *),
 31                        int flags)
 32 {
 33         struct sigaction sa;
 34 
 35         memset(&sa, 0, sizeof(sa));
 36         sa.sa_sigaction = handler;
 37         sa.sa_flags = SA_SIGINFO | flags;
 38         sigemptyset(&sa.sa_mask);
 39         if (sigaction(sig, &sa, 0))
 40                 err(1, "sigaction");
 41 }
 42 
 43 static void clearhandler(int sig)
 44 {
 45         struct sigaction sa;
 46 
 47         memset(&sa, 0, sizeof(sa));
 48         sa.sa_handler = SIG_DFL;
 49         sigemptyset(&sa.sa_mask);
 50         if (sigaction(sig, &sa, 0))
 51                 err(1, "sigaction");
 52 }
 53 
 54 static int setup_altstack(void *start, unsigned long size)
 55 {
 56         stack_t ss;
 57 
 58         memset(&ss, 0, sizeof(ss));
 59         ss.ss_size = size;
 60         ss.ss_sp = start;
 61 
 62         return sigaltstack(&ss, NULL);
 63 }
 64 
 65 static jmp_buf jmpbuf;
 66 
 67 static void sigsegv(int sig, siginfo_t *info, void *ctx_void)
 68 {
 69         if (sigalrm_expected) {
 70                 printf("[FAIL]\tWrong signal delivered: SIGSEGV (expected SIGALRM).");
 71                 nerrs++;
 72         } else {
 73                 printf("[OK]\tSIGSEGV signal delivered.\n");
 74         }
 75 
 76         siglongjmp(jmpbuf, 1);
 77 }
 78 
 79 static void sigalrm(int sig, siginfo_t *info, void *ctx_void)
 80 {
 81         if (!sigalrm_expected) {
 82                 printf("[FAIL]\tWrong signal delivered: SIGALRM (expected SIGSEGV).");
 83                 nerrs++;
 84         } else {
 85                 printf("[OK]\tSIGALRM signal delivered.\n");
 86         }
 87 }
 88 
 89 static void test_sigaltstack(void *altstack, unsigned long size)
 90 {
 91         if (setup_altstack(altstack, size))
 92                 err(1, "sigaltstack()");
 93 
 94         sigalrm_expected = (size > at_minstack_size) ? true : false;
 95 
 96         sethandler(SIGSEGV, sigsegv, 0);
 97         sethandler(SIGALRM, sigalrm, SA_ONSTACK);
 98 
 99         if (!sigsetjmp(jmpbuf, 1)) {
100                 printf("[RUN]\tTest an alternate signal stack of %ssufficient size.\n",
101                        sigalrm_expected ? "" : "in");
102                 printf("\tRaise SIGALRM. %s is expected to be delivered.\n",
103                        sigalrm_expected ? "It" : "SIGSEGV");
104                 raise(SIGALRM);
105         }
106 
107         clearhandler(SIGALRM);
108         clearhandler(SIGSEGV);
109 }
110 
111 int main(void)
112 {
113         void *altstack;
114 
115         at_minstack_size = getauxval(AT_MINSIGSTKSZ);
116 
117         altstack = mmap(NULL, at_minstack_size + SIGSTKSZ, PROT_READ | PROT_WRITE,
118                         MAP_PRIVATE | MAP_ANONYMOUS | MAP_STACK, -1, 0);
119         if (altstack == MAP_FAILED)
120                 err(1, "mmap()");
121 
122         if ((ENFORCED_MINSIGSTKSZ + 1) < at_minstack_size)
123                 test_sigaltstack(altstack, ENFORCED_MINSIGSTKSZ + 1);
124 
125         test_sigaltstack(altstack, at_minstack_size + SIGSTKSZ);
126 
127         return nerrs == 0 ? 0 : 1;
128 }
129 

~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~

kernel.org | git.kernel.org | LWN.net | Project Home | SVN repository | Mail admin

Linux® is a registered trademark of Linus Torvalds in the United States and other countries.
TOMOYO® is a registered trademark of NTT DATA CORPORATION.

sflogo.php