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.
21 #define SS_AUTODISARM (1U << 31)
24 static void *sstack
, *ustack
;
25 static ucontext_t uc
, sc
;
26 static const char *msg
= "[OK]\tStack preserved";
27 static const char *msg2
= "[FAIL]\tStack corrupted";
33 void my_usr1(int sig
, siginfo_t
*si
, void *u
)
40 register unsigned long sp
asm("sp");
42 if (sp
< (unsigned long)sstack
||
43 sp
>= (unsigned long)sstack
+ SIGSTKSZ
) {
44 printf("[FAIL]\tSP is not on sigaltstack\n");
47 /* put some data on stack. other sighandler will try to overwrite it */
50 p
= (struct stk_data
*)(aa
+ 512);
53 printf("[RUN]\tsignal USR1\n");
54 err
= sigaltstack(NULL
, &stk
);
56 perror("[FAIL]\tsigaltstack()");
59 if (stk
.ss_flags
!= SS_DISABLE
)
60 printf("[FAIL]\tss_flags=%i, should be SS_DISABLE\n",
63 printf("[OK]\tsigaltstack is disabled in sighandler\n");
64 swapcontext(&sc
, &uc
);
65 printf("%s\n", p
->msg
);
67 printf("[RUN]\tAborting\n");
72 void my_usr2(int sig
, siginfo_t
*si
, void *u
)
77 printf("[RUN]\tsignal USR2\n");
79 /* dont run valgrind on this */
80 /* try to find the data stored by previous sighandler */
81 p
= memmem(aa
, 1024, msg
, strlen(msg
));
83 printf("[FAIL]\tsigaltstack re-used\n");
84 /* corrupt the data */
86 /* tell other sighandler that his data is corrupted */
91 static void switch_fn(void)
93 printf("[RUN]\tswitched to user ctx\n");
100 struct sigaction act
;
104 sigemptyset(&act
.sa_mask
);
105 act
.sa_flags
= SA_ONSTACK
| SA_SIGINFO
;
106 act
.sa_sigaction
= my_usr1
;
107 sigaction(SIGUSR1
, &act
, NULL
);
108 act
.sa_sigaction
= my_usr2
;
109 sigaction(SIGUSR2
, &act
, NULL
);
110 sstack
= mmap(NULL
, SIGSTKSZ
, PROT_READ
| PROT_WRITE
,
111 MAP_PRIVATE
| MAP_ANONYMOUS
| MAP_STACK
, -1, 0);
112 if (sstack
== MAP_FAILED
) {
117 err
= sigaltstack(NULL
, &stk
);
119 perror("[FAIL]\tsigaltstack()");
122 if (stk
.ss_flags
== SS_DISABLE
) {
123 printf("[OK]\tInitial sigaltstack state was SS_DISABLE\n");
125 printf("[FAIL]\tInitial sigaltstack state was %i; should have been SS_DISABLE\n", stk
.ss_flags
);
130 stk
.ss_size
= SIGSTKSZ
;
131 stk
.ss_flags
= SS_ONSTACK
| SS_AUTODISARM
;
132 err
= sigaltstack(&stk
, NULL
);
134 if (errno
== EINVAL
) {
135 printf("[NOTE]\tThe running kernel doesn't support SS_AUTODISARM\n");
137 * If test cases for the !SS_AUTODISARM variant were
138 * added, we could still run them. We don't have any
139 * test cases like that yet, so just exit and report
144 perror("[FAIL]\tsigaltstack(SS_ONSTACK | SS_AUTODISARM)");
149 ustack
= mmap(NULL
, SIGSTKSZ
, PROT_READ
| PROT_WRITE
,
150 MAP_PRIVATE
| MAP_ANONYMOUS
| MAP_STACK
, -1, 0);
151 if (ustack
== MAP_FAILED
) {
157 uc
.uc_stack
.ss_sp
= ustack
;
158 uc
.uc_stack
.ss_size
= SIGSTKSZ
;
159 makecontext(&uc
, switch_fn
, 0);
162 err
= sigaltstack(NULL
, &stk
);
164 perror("[FAIL]\tsigaltstack()");
167 if (stk
.ss_flags
!= SS_AUTODISARM
) {
168 printf("[FAIL]\tss_flags=%i, should be SS_AUTODISARM\n",
172 printf("[OK]\tsigaltstack is still SS_AUTODISARM after signal\n");
174 printf("[OK]\tTest passed\n");