1 // SPDX-License-Identifier: GPL-2.0
3 * Stas Sergeev <stsp@users.sourceforge.net>
5 * test sigaltstack(SS_ONSTACK | SS_AUTODISARM)
6 * If that succeeds, then swapcontext() can be used inside sighandler safely.
21 #include "../kselftest.h"
24 #define SS_AUTODISARM (1U << 31)
27 static void *sstack
, *ustack
;
28 static ucontext_t uc
, sc
;
29 static const char *msg
= "[OK]\tStack preserved";
30 static const char *msg2
= "[FAIL]\tStack corrupted";
36 void my_usr1(int sig
, siginfo_t
*si
, void *u
)
44 register unsigned long sp
asm("%15");
46 register unsigned long sp
asm("sp");
49 if (sp
< (unsigned long)sstack
||
50 sp
>= (unsigned long)sstack
+ SIGSTKSZ
) {
51 ksft_exit_fail_msg("SP is not on sigaltstack\n");
53 /* put some data on stack. other sighandler will try to overwrite it */
56 p
= (struct stk_data
*)(aa
+ 512);
59 ksft_print_msg("[RUN]\tsignal USR1\n");
60 err
= sigaltstack(NULL
, &stk
);
62 ksft_exit_fail_msg("sigaltstack() - %s\n", strerror(errno
));
65 if (stk
.ss_flags
!= SS_DISABLE
)
66 ksft_test_result_fail("tss_flags=%x, should be SS_DISABLE\n",
69 ksft_test_result_pass(
70 "sigaltstack is disabled in sighandler\n");
71 swapcontext(&sc
, &uc
);
72 ksft_print_msg("%s\n", p
->msg
);
74 ksft_exit_skip("[RUN]\tAborting\n");
79 void my_usr2(int sig
, siginfo_t
*si
, void *u
)
84 ksft_print_msg("[RUN]\tsignal USR2\n");
86 /* dont run valgrind on this */
87 /* try to find the data stored by previous sighandler */
88 p
= memmem(aa
, 1024, msg
, strlen(msg
));
90 ksft_test_result_fail("sigaltstack re-used\n");
91 /* corrupt the data */
93 /* tell other sighandler that his data is corrupted */
98 static void switch_fn(void)
100 ksft_print_msg("[RUN]\tswitched to user ctx\n");
107 struct sigaction act
;
113 sigemptyset(&act
.sa_mask
);
114 act
.sa_flags
= SA_ONSTACK
| SA_SIGINFO
;
115 act
.sa_sigaction
= my_usr1
;
116 sigaction(SIGUSR1
, &act
, NULL
);
117 act
.sa_sigaction
= my_usr2
;
118 sigaction(SIGUSR2
, &act
, NULL
);
119 sstack
= mmap(NULL
, SIGSTKSZ
, PROT_READ
| PROT_WRITE
,
120 MAP_PRIVATE
| MAP_ANONYMOUS
| MAP_STACK
, -1, 0);
121 if (sstack
== MAP_FAILED
) {
122 ksft_exit_fail_msg("mmap() - %s\n", strerror(errno
));
126 err
= sigaltstack(NULL
, &stk
);
128 ksft_exit_fail_msg("sigaltstack() - %s\n", strerror(errno
));
131 if (stk
.ss_flags
== SS_DISABLE
) {
132 ksft_test_result_pass(
133 "Initial sigaltstack state was SS_DISABLE\n");
135 ksft_exit_fail_msg("Initial sigaltstack state was %x; "
136 "should have been SS_DISABLE\n", stk
.ss_flags
);
141 stk
.ss_size
= SIGSTKSZ
;
142 stk
.ss_flags
= SS_ONSTACK
| SS_AUTODISARM
;
143 err
= sigaltstack(&stk
, NULL
);
145 if (errno
== EINVAL
) {
147 "[NOTE]\tThe running kernel doesn't support SS_AUTODISARM\n");
149 * If test cases for the !SS_AUTODISARM variant were
150 * added, we could still run them. We don't have any
151 * test cases like that yet, so just exit and report
157 "sigaltstack(SS_ONSTACK | SS_AUTODISARM) %s\n",
163 ustack
= mmap(NULL
, SIGSTKSZ
, PROT_READ
| PROT_WRITE
,
164 MAP_PRIVATE
| MAP_ANONYMOUS
| MAP_STACK
, -1, 0);
165 if (ustack
== MAP_FAILED
) {
166 ksft_exit_fail_msg("mmap() - %s\n", strerror(errno
));
171 uc
.uc_stack
.ss_sp
= ustack
;
172 uc
.uc_stack
.ss_size
= SIGSTKSZ
;
173 makecontext(&uc
, switch_fn
, 0);
176 err
= sigaltstack(NULL
, &stk
);
178 ksft_exit_fail_msg("sigaltstack() - %s\n", strerror(errno
));
181 if (stk
.ss_flags
!= SS_AUTODISARM
) {
182 ksft_exit_fail_msg("ss_flags=%x, should be SS_AUTODISARM\n",
186 ksft_test_result_pass(
187 "sigaltstack is still SS_AUTODISARM after signal\n");