anxvonial
anxvonial

Reputation: 61

How to get instruction length

here is code for continuing when segv, I don't understand "6 bytes", why is 6?

static void sigaction_segv(int signal, siginfo_t *si, void *arg) {
    ucontext_t *ctx = (ucontext_t *) arg;
    /* We are on linux x86, the returning IP is stored in RIP (64bit) or EIP (32bit).
       In this example, the length of the offending instruction is 6 bytes.
       So we skip the offender ! */
#if __WORDSIZE == 64
    printf("Caught SIGSEGV, addr %p, RIP 0x%lx\n", si->si_addr, ctx->uc_mcontext.gregs[REG_RIP]);
    ctx->uc_mcontext.gregs[REG_RIP] += 6;
#else
    printf("Caught SIGSEGV, addr %p, EIP 0x%x\n", si->si_addr, ctx->uc_mcontext.gregs[REG_EIP]);
        ctx->uc_mcontext.gregs[REG_EIP] += 6;
#endif
}

full code is here

Upvotes: 2

Views: 338

Answers (1)

ctrl-d
ctrl-d

Reputation: 392

*(int *) NULL = 0;

will compile to (after clearing rax):

c7 00 00 00 00 00       ' movl   $0x0,(%rax)

so that's 6 bytes of machine code. Use objdump to see the assembly of your code.

Upvotes: 2

Related Questions