// The one thing asked of capstone, in C so that its structs stay its own. #include #include static csh handle; // A memory operand of an instruction, worked out against the registers. struct zt_touch { uint64_t addr; uint8_t size; // 1 the instruction reads it, 2 it writes it, 3 both. 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; // A jump on a condition. uint8_t conditional; }; // 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) { switch (reg) { case X86_REG_R15: return 0; case X86_REG_R14: return 1; case X86_REG_R13: return 2; case X86_REG_R12: return 3; case X86_REG_RBP: return 4; case X86_REG_RBX: return 5; case X86_REG_R11: return 6; case X86_REG_R10: return 7; case X86_REG_R9: return 8; case X86_REG_R8: return 9; case X86_REG_RAX: return 10; case X86_REG_RCX: return 11; case X86_REG_RDX: return 12; case X86_REG_RSI: return 13; case X86_REG_RDI: return 14; case X86_REG_RSP: return 19; default: return -1; } } // The instruction at `code`, which is at `addr`: its text into `text`, its // 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`. A branch is told // without them too, its target where the instruction alone says it. 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); cs_option(handle, CS_OPT_DETAIL, CS_OPT_ON); } size_t room = count ? *count : 0; if (count) *count = 0; cs_insn *insn; 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 (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 = regs ? holds(insn->id, regs[18], regs[11]) : 1; branch->indirect = 0; branch->target = 0; branch->conditional = branch->kind == 2 && insn->id != X86_INS_JMP && insn->id != X86_INS_LJMP; 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 ? regs[slot(op->reg)] : 0; 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; for (int i = 0; i < x->op_count && *count < room; i++) { const cs_x86_op *op = &x->operands[i]; if (op->type != X86_OP_MEM || !(op->access & (CS_AC_READ | CS_AC_WRITE))) continue; uint64_t at = (uint64_t)op->mem.disp; if (op->mem.base == X86_REG_RIP) at += addr + size; else if (op->mem.base != X86_REG_INVALID) { if (slot(op->mem.base) < 0) continue; at += regs[slot(op->mem.base)]; } if (op->mem.index != X86_REG_INVALID) { if (slot(op->mem.index) < 0) continue; at += regs[slot(op->mem.index)] * (uint64_t)op->mem.scale; } if (op->mem.segment == X86_REG_FS) at += regs[21]; if (op->mem.segment == X86_REG_GS) at += regs[22]; touches[*count].addr = at; touches[*count].size = op->size; touches[*count].how = (op->access & CS_AC_READ ? 1 : 0) | (op->access & CS_AC_WRITE ? 2 : 0); ++*count; } } cs_free(insn, 1); return size; }