File tree Expand file tree Collapse file tree 1 file changed +60
-0
lines changed Expand file tree Collapse file tree 1 file changed +60
-0
lines changed Original file line number Diff line number Diff line change
1
+ #define _GNU_SOURCE
2
+ #include <unistd.h>
3
+ #include <stdio.h>
4
+ #include <stdlib.h>
5
+ #include <string.h>
6
+ #include <signal.h>
7
+
8
+ static void sigseg_handler (int sig )
9
+ {
10
+ int x ;
11
+ printf ("\nCaught signal %d (%s)\n" , sig , strsignal (sig ));
12
+ printf ("Top of handler stack near %10p\n" , (void * )& x );
13
+ fflush (NULL );
14
+
15
+ _exit (123 ); //在段错误后不能从此处返回
16
+ }
17
+
18
+ static void overflow_stack (int call_num )
19
+ {
20
+ char a [1024 * 1024 ]; //撑大栈帧
21
+
22
+ printf ("Call %4d - top of stack near %10p\n" , call_num , & a [0 ]);
23
+
24
+ overflow_stack (call_num + 1 );
25
+ }
26
+
27
+ int main (int argc , char * argv [])
28
+ {
29
+ stack_t sigstack ;
30
+ struct sigaction sa ;
31
+ int j ;
32
+
33
+ printf ("Top of standard stack is near %10p\n" , (void * )& j );
34
+
35
+
36
+ #ifdef ALT_STACK
37
+ //分配可变栈并通知内核
38
+ sigstack .ss_sp = malloc (SIGSTKSZ );
39
+ if (sigstack .ss_sp == NULL ) {
40
+ perror ("malloc" );
41
+ exit (EXIT_FAILURE );
42
+ }
43
+ sigstack .ss_size = SIGSTKSZ ;
44
+ sigstack .ss_flags = 0 ;
45
+ if (sigaltstack (& sigstack , NULL ) == -1 ) {
46
+ perror ("sigaltstack" );
47
+ exit (EXIT_FAILURE );
48
+ }
49
+ printf ("Alternate stack is at %10p-%p\n" , sigstack .ss_sp , (char * )sbrk (0 ) - 1 );
50
+ #endif
51
+ sa .sa_handler = sigseg_handler ; //建立SIGSEGV 的处理函数
52
+ sigemptyset (& sa .sa_mask );
53
+ sa .sa_flags = SA_ONSTACK ; //处理函数使用可变栈
54
+ if (sigaction (SIGSEGV , & sa , NULL ) == -1 ) {
55
+ perror ("sigaction" );
56
+ exit (125 );
57
+ }
58
+
59
+ overflow_stack (1 );
60
+ }
You can’t perform that action at this time.
0 commit comments