In the SIGILL handler, how can I skip offensive instructions?

I am going to create JIT code, and I want to insert invalid opcodes in a thread in order to do some meta debugging. All is well and good until he attacks the instruction, and at that moment the thing goes into an endless cycle of illegal signal handler instructions and back.

Is there a way I can install a thing to just skip a bad instruction?

+4
source share
2 answers

These are very hacks and UNPORTABLE, but:

void sighandler (int signo, siginfo_t si, void *data) { ucontext_t *uc = (ucontext_t *)data; int instruction_length = /* the length of the "instruction" to skip */ uc->uc_mcontext.gregs[REG_RIP] += instruction_length; } 

install sighandler as follows:

 struct sigaction sa, osa; sa.sa_flags = SA_ONSTACK | SA_RESTART | SA_SIGINFO; sa.sa_sigaction = sighandler; sigaction(SIGILL, &sa, &osa); 

This might work if you know how far to skip (and this is an Intel processor) :-)

+10
source

You can also try a different approach (if applicable to your case): you can use SIGTRAP, which is easier to manage.

 void sigtrap_handler(int sig){ printf("Process %d received sigtrap %d.\n", getpid(),sig); } signal(SIGTRAP,sigtrap_handler); asm("int3"); // causes a SIGTRAP 
0
source

Source: https://habr.com/ru/post/1396875/


All Articles