Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdlib.h>
- #include <stdio.h>
- #include <pthread.h>
- #include <pthread_np.h>
- #include <signal.h>
- #include <sys/ucontext.h>
- void segfault_handler (int sig, siginfo_t *info, ucontext_t *ctx)
- {
- pthread_t th = pthread_self();
- printf ("Sigfault in thread %p\n", &th);
- pthread_attr_t attr;
- pthread_attr_get_np (th, &attr);
- void *stack = (void*)0x800000000000;
- size_t stacksize;
- pthread_attr_init (&attr);
- pthread_attr_getstacksize (&attr, &stacksize); // pthread_attr_getstack does not work
- size_t stack_boundary = (size_t)stack - stacksize;
- size_t rsp = ctx->uc_mcontext.mc_rsp;
- printf ("Stack = %p, Stack size = 0x%08lx\n", stack, stacksize);
- printf ("RSP = 0x%08lx, Stack boundary = 0x%08lx\n", rsp, stack_boundary);
- if (rsp < stack_boundary) fprintf (stderr, "Stack overflow\n");
- }
- void stack_over()
- {
- int arr[10000000];
- arr[9] = 0;
- arr[0] = 0;
- printf ("%i\n", arr[0]);
- // int *myaddr = 0x75845587967;
- // myaddr[0] = 546;
- }
- int main ()
- {
- stack_t sigstack;
- sigstack.ss_sp = malloc (SIGSTKSZ);
- sigstack.ss_size = SIGSTKSZ;
- sigstack.ss_flags = 0;
- sigaltstack (&sigstack, NULL);
- struct sigaction sa;
- sa.sa_sigaction = segfault_handler;
- sa.sa_flags = SA_SIGINFO | SA_RESETHAND | SA_ONSTACK;
- sigemptyset (&sa.sa_mask);
- sigaction (SIGSEGV, &sa, NULL);
- stack_over();
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment