2 * Stas Sergeev <stsp@users.sourceforge.net>
4 * test sigaltstack(SS_ONSTACK | SS_AUTODISARM)
5 * If that succeeds, then swapcontext() can be used inside sighandler safely.
20 #include "../kselftest.h"
23 #define SS_AUTODISARM (1U << 31)
26 static void *sstack, *ustack;
27 static ucontext_t uc, sc;
28 static const char *msg = "[OK]\tStack preserved";
29 static const char *msg2 = "[FAIL]\tStack corrupted";
35 void my_usr1(int sig, siginfo_t *si, void *u)
42 register unsigned long sp asm("sp");
44 if (sp < (unsigned long)sstack ||
45 sp >= (unsigned long)sstack + SIGSTKSZ) {
46 ksft_exit_fail_msg("SP is not on sigaltstack\n");
48 /* put some data on stack. other sighandler will try to overwrite it */
51 p = (struct stk_data *)(aa + 512);
54 ksft_print_msg("[RUN]\tsignal USR1\n");
55 err = sigaltstack(NULL, &stk);
57 ksft_exit_fail_msg("sigaltstack() - %s\n", strerror(errno));
60 if (stk.ss_flags != SS_DISABLE)
61 ksft_test_result_fail("tss_flags=%x, should be SS_DISABLE\n",
64 ksft_test_result_pass(
65 "sigaltstack is disabled in sighandler\n");
66 swapcontext(&sc, &uc);
67 ksft_print_msg("%s\n", p->msg);
69 ksft_exit_skip("[RUN]\tAborting\n");
74 void my_usr2(int sig, siginfo_t *si, void *u)
79 ksft_print_msg("[RUN]\tsignal USR2\n");
81 /* dont run valgrind on this */
82 /* try to find the data stored by previous sighandler */
83 p = memmem(aa, 1024, msg, strlen(msg));
85 ksft_test_result_fail("sigaltstack re-used\n");
86 /* corrupt the data */
88 /* tell other sighandler that his data is corrupted */
93 static void switch_fn(void)
95 ksft_print_msg("[RUN]\tswitched to user ctx\n");
102 struct sigaction act;
108 sigemptyset(&act.sa_mask);
109 act.sa_flags = SA_ONSTACK | SA_SIGINFO;
110 act.sa_sigaction = my_usr1;
111 sigaction(SIGUSR1, &act, NULL);
112 act.sa_sigaction = my_usr2;
113 sigaction(SIGUSR2, &act, NULL);
114 sstack = mmap(NULL, SIGSTKSZ, PROT_READ | PROT_WRITE,
115 MAP_PRIVATE | MAP_ANONYMOUS | MAP_STACK, -1, 0);
116 if (sstack == MAP_FAILED) {
117 ksft_exit_fail_msg("mmap() - %s\n", strerror(errno));
121 err = sigaltstack(NULL, &stk);
123 ksft_exit_fail_msg("sigaltstack() - %s\n", strerror(errno));
126 if (stk.ss_flags == SS_DISABLE) {
127 ksft_test_result_pass(
128 "Initial sigaltstack state was SS_DISABLE\n");
130 ksft_exit_fail_msg("Initial sigaltstack state was %x; "
131 "should have been SS_DISABLE\n", stk.ss_flags);
136 stk.ss_size = SIGSTKSZ;
137 stk.ss_flags = SS_ONSTACK | SS_AUTODISARM;
138 err = sigaltstack(&stk, NULL);
140 if (errno == EINVAL) {
142 "[NOTE]\tThe running kernel doesn't support SS_AUTODISARM\n");
144 * If test cases for the !SS_AUTODISARM variant were
145 * added, we could still run them. We don't have any
146 * test cases like that yet, so just exit and report
152 "sigaltstack(SS_ONSTACK | SS_AUTODISARM) %s\n",
158 ustack = mmap(NULL, SIGSTKSZ, PROT_READ | PROT_WRITE,
159 MAP_PRIVATE | MAP_ANONYMOUS | MAP_STACK, -1, 0);
160 if (ustack == MAP_FAILED) {
161 ksft_exit_fail_msg("mmap() - %s\n", strerror(errno));
166 uc.uc_stack.ss_sp = ustack;
167 uc.uc_stack.ss_size = SIGSTKSZ;
168 makecontext(&uc, switch_fn, 0);
171 err = sigaltstack(NULL, &stk);
173 ksft_exit_fail_msg("sigaltstack() - %s\n", strerror(errno));
176 if (stk.ss_flags != SS_AUTODISARM) {
177 ksft_exit_fail_msg("ss_flags=%x, should be SS_AUTODISARM\n",
181 ksft_test_result_pass(
182 "sigaltstack is still SS_AUTODISARM after signal\n");