在 SIGILL 处理程序中,如何跳过有问题的指令?

发布于 2025-01-06 10:47:43 字数 115 浏览 2 评论 0原文

我要生成 JIT 代码,并且想将无效的操作码插入流中以执行一些元调试。一切都很好,直到它到达指令,此时,事情进入非法指令到信号处理程序并返回的无限循环。

有什么方法可以设置它来简单地跳过错误的指令吗?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

末が日狂欢 2025-01-13 10:47:43

这是非常hacky和不可移植的,但是:

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;
}

像这样安装sighandler

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

如果你知道要跳过多远,那么可以工作(并且它是Intel进程):-)

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) :-)

彼岸花ソ最美的依靠 2025-01-13 10:47:43

您还可以尝试另一种方法(如果它适用于您的情况):
您可以使用更易于管理的 SIGTRAP。

void sigtrap_handler(int sig){
    printf("Process %d received sigtrap %d.\n", getpid(),sig);
}

signal(SIGTRAP,sigtrap_handler);
asm("int3"); // causes a SIGTRAP

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
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文