Advertisement
Guest User

Untitled

a guest
Dec 25th, 2019
117
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.68 KB | None | 0 0
  1. #include <iostream>
  2. #include <signal.h>
  3. #include <ucontext.h>
  4.  
  5. #ifdef REG_EIP
  6. #define REG_PC REG_EIP
  7. #else
  8. #define REG_PC REG_RIP
  9. #endif
  10.  
  11. void sigHandler(int signum, siginfo_t* siginfo, void* context_)
  12. {
  13.     ucontext_t* context=static_cast<ucontext_t*>(context_);
  14.     auto& pc=context->uc_mcontext.gregs[REG_PC];
  15.     pc+=2; // skip the instruction leading to trap ("idiv reg" is 2 bytes long)
  16. }
  17.  
  18. int main()
  19. {
  20.     struct sigaction act={0};
  21.     act.sa_sigaction=sigHandler;
  22.     act.sa_flags=SA_SIGINFO;
  23.     sigaction(SIGFPE, &act, NULL);
  24.     std::cerr << "Dividing by zero...\n";
  25.     int zero=0;
  26.     __asm__ __volatile__("idiv %0"::"r"(zero));
  27.     std::cerr << "Still alive\n";
  28. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement