Guest User

Untitled

a guest
Apr 21st, 2014
91
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.43 KB | None | 0 0
  1. #include <stdlib.h>
  2. #include <stdio.h>
  3. #include <pthread.h>
  4. #include <pthread_np.h>
  5. #include <signal.h>
  6. #include <sys/ucontext.h>
  7.  
  8. void segfault_handler (int sig, siginfo_t *info, ucontext_t *ctx)
  9. {
  10.     pthread_t th = pthread_self();
  11.     printf ("Sigfault in thread %p\n", &th);
  12.     pthread_attr_t attr;
  13.     pthread_attr_get_np (th, &attr);
  14.     void *stack = (void*)0x800000000000;
  15.     size_t stacksize;
  16.     pthread_attr_init (&attr);
  17.     pthread_attr_getstacksize (&attr, &stacksize); // pthread_attr_getstack does not work
  18.     size_t stack_boundary = (size_t)stack - stacksize;
  19.  
  20.     size_t rsp = ctx->uc_mcontext.mc_rsp;
  21.     printf ("Stack = %p, Stack size = 0x%08lx\n", stack, stacksize);
  22.     printf ("RSP = 0x%08lx, Stack boundary = 0x%08lx\n", rsp, stack_boundary);
  23.     if (rsp < stack_boundary) fprintf (stderr, "Stack overflow\n");
  24. }
  25.  
  26. void stack_over()
  27. {
  28.     int arr[10000000];
  29.     arr[9] = 0;
  30.     arr[0] = 0;
  31.     printf ("%i\n", arr[0]);
  32. //    int *myaddr = 0x75845587967;
  33. //    myaddr[0] = 546;
  34. }
  35.  
  36. int main ()
  37. {
  38.     stack_t sigstack;
  39.     sigstack.ss_sp = malloc (SIGSTKSZ);
  40.     sigstack.ss_size = SIGSTKSZ;
  41.     sigstack.ss_flags = 0;
  42.     sigaltstack (&sigstack, NULL);
  43.    
  44.     struct sigaction sa;
  45.     sa.sa_sigaction = segfault_handler;
  46.     sa.sa_flags = SA_SIGINFO | SA_RESETHAND | SA_ONSTACK;
  47.     sigemptyset (&sa.sa_mask);
  48.     sigaction (SIGSEGV, &sa, NULL);
  49.  
  50.     stack_over();
  51.     return 0;
  52. }
Advertisement
Add Comment
Please, Sign In to add comment