From 97ce7f485fc0419df693fdf16d634e9e9a6085fc Mon Sep 17 00:00:00 2001 From: Gabriel Schneider Date: Tue, 10 Nov 2026 14:35:56 -0300 Subject: context: what an instruction is about to touch, and the call chain At a stop the instruction at the pc is decoded against the registers it will run with (capstone's operands): a touch event for each memory operand (address, size, read or write), what is there recorded with it, and what a write left there read back at the next stop. An instruction-level recording so has every memory write of the stepped thread, nothing watched. The frames of the call chain are recorded and their return addresses named; telescope.backtrace walks them from the recording and ztrace show prints them. Process.read no longer panics on address 0. --- src/cli/disasm.c | 65 ++++++++++++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 63 insertions(+), 2 deletions(-) (limited to 'src/cli/disasm.c') diff --git a/src/cli/disasm.c b/src/cli/disasm.c index db19059..4144986 100644 --- a/src/cli/disasm.c +++ b/src/cli/disasm.c @@ -4,17 +4,78 @@ 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 `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. -size_t zt_disasm(const uint8_t *code, size_t len, uint64_t addr, char *text, size_t cap) { +// 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) { 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); + // 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; } -- cgit v1.3