From ed46c51fe127377eb1fde8dee83da984e0f61860 Mon Sep 17 00:00:00 2001 From: Gabriel Schneider Date: Tue, 10 Nov 2026 14:39:55 -0300 Subject: context: eight instructions above the pc and eight below; where a branch goes A function is decoded once from its start, so the instructions before the pc are known: each instruction's string now says the length of the one before it, and ztrace show prints eight above and eight below, as pwndbg does. A call, a jump or a return at the pc is a branch event with its target (named) and, for a conditional jump, whether the flags take it. --- src/cli/disasm.c | 56 +++++++++++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 55 insertions(+), 1 deletion(-) (limited to 'src/cli/disasm.c') diff --git a/src/cli/disasm.c b/src/cli/disasm.c index 4144986..82fd0bb 100644 --- a/src/cli/disasm.c +++ b/src/cli/disasm.c @@ -12,6 +12,43 @@ struct zt_touch { uint8_t how; }; +// Where an instruction that is a call, a jump or a return goes. +struct zt_branch { + // The target; with `indirect`, the address the target is read from. + uint64_t target; + // 0 not a branch, 1 call, 2 jump, 3 return (the target is on the stack). + uint8_t kind; + // A conditional jump whose condition does not hold is not taken. + uint8_t taken; + uint8_t indirect; +}; + +// Whether the flags (and rcx) make a conditional jump go. +static int holds(unsigned id, uint64_t flags, uint64_t rcx) { + int cf = flags & 1, pf = flags >> 2 & 1, zf = flags >> 6 & 1, sf = flags >> 7 & 1, of = flags >> 11 & 1; + switch (id) { + case X86_INS_JO: return of; + case X86_INS_JNO: return !of; + case X86_INS_JB: return cf; + case X86_INS_JAE: return !cf; + case X86_INS_JE: return zf; + case X86_INS_JNE: return !zf; + case X86_INS_JBE: return cf || zf; + case X86_INS_JA: return !cf && !zf; + case X86_INS_JS: return sf; + case X86_INS_JNS: return !sf; + case X86_INS_JP: return pf; + case X86_INS_JNP: return !pf; + case X86_INS_JL: return sf != of; + case X86_INS_JGE: return sf == of; + case X86_INS_JLE: return zf || sf != of; + case X86_INS_JG: return !zf && sf == of; + case X86_INS_JRCXZ: return rcx == 0; + case X86_INS_JECXZ: return (uint32_t)rcx == 0; + default: return 1; + } +} + // Where `reg` is in the kernel's user_regs_struct, as 64-bit words; -1 for // a register an address is not made of here. static int slot(x86_reg reg) { @@ -40,7 +77,7 @@ static int slot(x86_reg reg) { // length returned; 0 where there is no instruction. With `regs` (the // thread's, about to run it), the memory it touches into `touches`, at // most `*count` of them, and how many into `*count`. -size_t zt_disasm(const uint8_t *code, size_t len, uint64_t addr, char *text, size_t cap, const uint64_t *regs, struct zt_touch *touches, size_t *count) { +size_t zt_disasm(const uint8_t *code, size_t len, uint64_t addr, char *text, size_t cap, const uint64_t *regs, struct zt_touch *touches, size_t *count, struct zt_branch *branch) { if (!handle) { if (cs_open(CS_ARCH_X86, CS_MODE_64, &handle) != CS_ERR_OK) return 0; cs_option(handle, CS_OPT_SYNTAX, CS_OPT_SYNTAX_INTEL); @@ -52,6 +89,23 @@ size_t zt_disasm(const uint8_t *code, size_t len, uint64_t addr, char *text, siz if (cs_disasm(handle, code, len, addr, 1, &insn) != 1) return 0; size_t size = insn->size; snprintf(text, cap, insn->op_str[0] ? "%s %s" : "%s", insn->mnemonic, insn->op_str); + if (regs && branch && insn->detail) { + const cs_x86 *x = &insn->detail->x86; + branch->kind = cs_insn_group(handle, insn, CS_GRP_RET) ? 3 : cs_insn_group(handle, insn, CS_GRP_CALL) ? 1 : cs_insn_group(handle, insn, CS_GRP_JUMP) ? 2 : 0; + branch->taken = holds(insn->id, regs[18], regs[11]); + branch->indirect = 0; + branch->target = 0; + if (branch->kind == 1 || branch->kind == 2) { + const cs_x86_op *op = &x->operands[0]; + if (x->op_count < 1) branch->kind = 0; + else if (op->type == X86_OP_IMM) branch->target = (uint64_t)op->imm; + else if (op->type == X86_OP_REG && slot(op->reg) >= 0) branch->target = regs[slot(op->reg)]; + else if (op->type == X86_OP_MEM) { + // Its address is the first touch below: the target is what is there. + branch->indirect = 1; + } else branch->kind = 0; + } + } // lea and the long nops name memory and touch none. if (regs && insn->detail && insn->id != X86_INS_LEA && insn->id != X86_INS_NOP) { const cs_x86 *x = &insn->detail->x86; -- cgit v1.3