In a SIGILL handler, how can I skip the offending instruction?

我们两清 提交于 2019-12-21 20:01:06

问题


I'm going JIT code generation, and I want to insert invalid opcodes into the stream in order to perform some meta-debugging. Everything is fine and good until it hits the instruction, at which point the thing goes into an infinite loop of illegal instruction to signal handler and back.

Is there any way I can set the thing to simply skip the bad instruction?


回答1:


It's very hacky 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 the sighandler like that:

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

That could work if you know how far to skip (and it's a Intel proc) :-)




回答2:


You can also try another approach (if it applies to your case): you can use a 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


来源:https://stackoverflow.com/questions/9314755/in-a-sigill-handler-how-can-i-skip-the-offending-instruction

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!