Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdlib.h>
- #include <stdio.h>
- #include <pthread.h>
- #include <signal.h>
- #include <sys/ucontext.h>
- #include <sys/mman.h>
- struct my_thread
- {
- void *stack;
- ssize_t stack_size;
- void* (*init_func) ();
- };
- __thread struct my_thread *curthr;
- void segfault_handler (int sig, siginfo_t *info, ucontext_t *ctx)
- {
- ssize_t stack_boundary = (ssize_t)curthr->stack - curthr->stack_size;
- size_t rsp = ctx->uc_mcontext.mc_rsp;
- if (rsp < stack_boundary)
- {
- fprintf (stderr, "Stack overflow\n");
- printf ("Stack boundary: 0x%08lx, RSP: 0x%08lx\n", stack_boundary, rsp);
- }
- }
- void* new_thread_trampoline (struct my_thread *th)
- {
- curthr = th;
- stack_t sigstack;
- sigstack.ss_sp = malloc (SIGSTKSZ);
- sigstack.ss_size = SIGSTKSZ;
- sigstack.ss_flags = 0;
- sigaltstack (&sigstack, NULL);
- return th->init_func();
- }
- void* stack_over()
- {
- /* int *iknowthepath = 0x477777777f;
- iknowthepath[0] = 445;
- printf ("Here\n");*/
- int arr[10000000];
- arr[9] = 0;
- arr[0] = 0;
- printf ("%i\n", arr[0]);
- return NULL;
- }
- int main ()
- {
- pthread_t new_thr;
- struct my_thread *my_new_thr = malloc (sizeof (struct my_thread));
- my_new_thr->stack_size = 2000000;
- my_new_thr->stack = mmap (NULL, my_new_thr->stack_size,
- PROT_READ|PROT_WRITE,
- MAP_ANON|MAP_PRIVATE, -1, 0);
- my_new_thr->init_func = stack_over;
- 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);
- pthread_attr_t attr;
- pthread_attr_init (&attr);
- pthread_attr_setstack (&attr, my_new_thr->stack, my_new_thr->stack_size);
- pthread_create (&new_thr, &attr, (void * (*) (void*))new_thread_trampoline, my_new_thr);
- pthread_join (new_thr, NULL);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment