|
Revision 13, 1.7 kB
(checked in by haypo, 1 year ago)
|
cleanup stack.c
|
- Property svn:eol-style set to
native
|
| Line | |
|---|
| 1 |
|
|---|
| 2 |
|
|---|
| 3 |
|
|---|
| 4 |
|
|---|
| 5 |
|
|---|
| 6 |
|
|---|
| 7 |
|
|---|
| 8 |
|
|---|
| 9 |
|
|---|
| 10 |
#include <setjmp.h> |
|---|
| 11 |
#include <signal.h> |
|---|
| 12 |
#include <stdlib.h> |
|---|
| 13 |
#include <stdio.h> |
|---|
| 14 |
#include <sys/ucontext.h> |
|---|
| 15 |
|
|---|
| 16 |
jmp_buf env; |
|---|
| 17 |
char alternate_stack[10*4096]; |
|---|
| 18 |
|
|---|
| 19 |
|
|---|
| 20 |
void handler(int signum) |
|---|
| 21 |
{ |
|---|
| 22 |
longjmp(env, 1); |
|---|
| 23 |
} |
|---|
| 24 |
|
|---|
| 25 |
|
|---|
| 26 |
void overflow(int i) { |
|---|
| 27 |
int r[256]; |
|---|
| 28 |
if (i % 8 == 0) { |
|---|
| 29 |
printf("%p ", &r); |
|---|
| 30 |
if (i % 64 == 0) { |
|---|
| 31 |
printf("\n"); |
|---|
| 32 |
} |
|---|
| 33 |
fflush(stdout); |
|---|
| 34 |
} |
|---|
| 35 |
overflow(i+1); |
|---|
| 36 |
} |
|---|
| 37 |
|
|---|
| 38 |
|
|---|
| 39 |
void init_signal_stack() |
|---|
| 40 |
{ |
|---|
| 41 |
stack_t ss ; |
|---|
| 42 |
ss.ss_sp = alternate_stack; |
|---|
| 43 |
ss.ss_size = sizeof(alternate_stack); |
|---|
| 44 |
ss.ss_flags = 0; |
|---|
| 45 |
if( sigaltstack(&ss, NULL)) { |
|---|
| 46 |
|
|---|
| 47 |
fprintf(stderr, "Failed sigaltstack\n" ); |
|---|
| 48 |
exit(1); |
|---|
| 49 |
} |
|---|
| 50 |
} |
|---|
| 51 |
|
|---|
| 52 |
|
|---|
| 53 |
void init_signal_handler() |
|---|
| 54 |
{ |
|---|
| 55 |
struct sigaction newAct; |
|---|
| 56 |
sigemptyset(&newAct.sa_mask); |
|---|
| 57 |
sigaddset(&newAct.sa_mask, SIGSEGV); |
|---|
| 58 |
newAct.sa_handler = handler; |
|---|
| 59 |
newAct.sa_flags = SA_RESETHAND | SA_RESTART | SA_ONSTACK; |
|---|
| 60 |
if( sigaction( SIGSEGV, &newAct, NULL ) == -1 ){ |
|---|
| 61 |
|
|---|
| 62 |
fprintf(stderr, "Failed to set my signal handler.\n" ); |
|---|
| 63 |
exit(1); |
|---|
| 64 |
} |
|---|
| 65 |
} |
|---|
| 66 |
|
|---|
| 67 |
void code() |
|---|
| 68 |
{ |
|---|
| 69 |
if (!setjmp(env)) { |
|---|
| 70 |
overflow(1); |
|---|
| 71 |
} else { |
|---|
| 72 |
|
|---|
| 73 |
printf("sigsegv\n"); |
|---|
| 74 |
} |
|---|
| 75 |
printf("continue execution!\n"); |
|---|
| 76 |
} |
|---|
| 77 |
|
|---|
| 78 |
int main() |
|---|
| 79 |
{ |
|---|
| 80 |
init_signal_stack(); |
|---|
| 81 |
init_signal_handler(); |
|---|
| 82 |
code(); |
|---|
| 83 |
exit(0); |
|---|
| 84 |
} |
|---|
| 85 |
|
|---|