-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsigaltstack_from_sighandler.c
253 lines (209 loc) · 5.95 KB
/
sigaltstack_from_sighandler.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
// SPDX-License-Identifier: GPL-2.0
#define _GNU_SOURCE
#define __SANE_USERSPACE_TYPES__
#include <errno.h>
#include <linux/elf.h>
#include <linux/futex.h>
#include <time.h>
#include <sys/time.h>
#include <sys/syscall.h>
#include <string.h>
#include <stdio.h>
#include <stdint.h>
#include <stdbool.h>
#include <signal.h>
#include <assert.h>
#include <stdlib.h>
#include <ucontext.h>
#include <sys/mman.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <sys/ptrace.h>
#include <setjmp.h>
#include <linux/types.h>
#include "../kselftest.h"
/*
* need those definition for manually build using gcc.
* gcc -mxsave -o sigaltstack_from_sighandler -O2 -g -std=gnu99 -pthread -Wall ./sigaltstack_from_sighandler.c -lrt -ldl -lm
*/
#define FAIL_TEST_IF_FALSE(c) do {\
if (!(c)) {\
ksft_test_result_fail("%s, line:%d\n", __func__, __LINE__);\
goto test_end;\
} \
} \
while (0)
#define SKIP_TEST_IF_FALSE(c) do {\
if (!(c)) {\
ksft_test_result_skip("%s, line:%d\n", __func__, __LINE__);\
goto test_end;\
} \
} \
while (0)
#define TEST_END_CHECK() {\
ksft_test_result_pass("%s\n", __func__);\
return;\
test_end:\
return;\
}
#ifndef u16
#define u16 __u16
#endif
#ifndef u32
#define u32 __u32
#endif
#ifndef u64
#define u64 __u64
#endif
int sigaltstack_size = 2 * 1024 * 1024;
void *addr1 = (void *)0x5000000;
void *addr2;
bool sigaltstack_called = false;
void inner()
{
stack_t altstack;
void * ptr;
ptr = mmap(addr1, sigaltstack_size, PROT_READ | PROT_WRITE, MAP_ANONYMOUS | MAP_PRIVATE |MAP_FIXED, -1, 0);
assert(ptr == addr1);
altstack.ss_sp = addr1;
altstack.ss_size = sigaltstack_size;
altstack.ss_flags = SS_ONSTACK;
int ret = sigaltstack(&altstack, NULL);
if (ret != 0)
printf("sigaltstack(%lx) failed inside signal_handler\n", (unsigned long) addr1);
else
printf("sigaltstack(%lx) succeed inside signal handler\n", (unsigned long) addr1);
}
static void usr1_handler(int sig, siginfo_t *siginfo, void *ptr)
{
unsigned long sp;
int ret;
__asm__ volatile (
"mov %%rsp, %0"
: "=r"(sp)
);
if (sp > addr1 && sp <= addr1 + sigaltstack_size) {
printf("usr1_handler:sp (%lx) on altstack\n", sp);
assert(0);
}
else {
printf("usr1_handler:sp (%lx) on normal stack\n", sp);
}
if (sigaltstack_called == false) {
sigaltstack_called = true;
inner();
/*
* USR2 will be handled inside altstack
* this is because usr2_handler is called inside raise, or before
* current handler return, note: this is different han USR1 below
*/
printf("raise USR2\n");
ret = raise(SIGUSR2);
assert(ret == 0);
/*
* raise USR1, this will cause a endless loop
* the sigaltstack_called is not enough protection for reentry.
* this means the raise just put signal to some queue, then
* next usr1_handler will be called after current function return.
*/
//printf("raise USR1\n");
//ret = raise(SIGUSR1);
//assert(ret == 0);
sigaltstack_called = false;
}
printf("return from usr1_handler\n", sp);
}
static void usr2_handler(int sig, siginfo_t *siginfo, void *ptr)
{
unsigned long sp;
__asm__ volatile (
"mov %%rsp, %0"
: "=r"(sp)
);
if (sp > addr1 && sp <= addr1 + sigaltstack_size) {
printf("usr2_handler:sp (%lx) on altstack\n", sp);
assert(sigaltstack_called == true);
}
else {
printf("usr2_handler:sp (%lx) on normal stack\n", sp);
assert(sigaltstack_called == false);
}
printf("return from usr2_handler\n", sp);
}
void setup_fixed_address(int size, void *addr, void **ptrOut)
{
void *ptr;
stack_t altstack;
ptr = mmap(addr, size, PROT_READ | PROT_WRITE, MAP_ANONYMOUS | MAP_PRIVATE | MAP_FIXED, -1, 0);
assert(ptr == addr);
*ptrOut = ptr;
}
void setup_sigusr1()
{
struct sigaction sa;
int ret;
memset(&sa, 0, sizeof(sa));
sa.sa_handler = usr1_handler;
sa.sa_flags = SA_SIGINFO;
sigemptyset(&sa.sa_mask);
ret = sigaction(SIGUSR1, &sa, NULL);
assert(ret == 0);
}
void setup_sigusr2()
{
struct sigaction sa;
int ret;
memset(&sa, 0, sizeof(sa));
sa.sa_handler = usr2_handler;
sa.sa_flags = SA_SIGINFO | SA_ONSTACK;
sigemptyset(&sa.sa_mask);
ret = sigaction(SIGUSR2, &sa, NULL);
assert(ret == 0);
}
/*
* this test is used to observe how sigaltstack is used.
* usr1 is without SA_ONSTACK, this means usr1 is always handled by normal stack.
* usr2 is with SA_ONSTACK, so
* a> if sigaltstack is available, then use altstack
* b> if sigaltstack is not available, use normal stack.
* in usr1_handler, call sigaltstack to setup altstack to addr1
*/
void test_sigaltstack_from_sighandler()
{
setup_sigusr1();
setup_sigusr2();
/* USR2 will be handled in normal stack, since no sigaltstack yet */
printf("raise USR2\n");
int ret = raise(SIGUSR2);
FAIL_TEST_IF_FALSE(ret == 0);
/* USR1 will be handled in normal stack, call sigaltstack
* usr1_handler will raise SIGUSR2, SIGUSR2 will be handled in altstack
*/
printf("raise USR1\n");
ret = raise(SIGUSR1);
FAIL_TEST_IF_FALSE(ret == 0);
/* USR1 will be handled in normal stack, because sigaction for USR1 is not registered with SA_ONSTACK */
printf("raise USR1\n");
ret = raise(SIGUSR1);
FAIL_TEST_IF_FALSE(ret == 0);
/*
* USR2 will be handled in normal stack,
* even sigaltstack is called inside usr1_handler, however, the scope is only limited in-side usr1_handler,
* i.e restore_altstack during rt_sigreturn will restore the thread's altstack when usr1_handler returns.
*/
printf("raise USR2\n");
ret = raise(SIGUSR2);
FAIL_TEST_IF_FALSE(ret == 0);
TEST_END_CHECK();
}
int main(void)
{
ksft_print_header();
ksft_print_msg("pid=%d\n", getpid());
ksft_set_plan(1);
test_sigaltstack_from_sighandler();
return 0;
}