From 0ec2376cae2fdfc50e90d930ebc86c7a5b7f5594 Mon Sep 17 00:00:00 2001 From: Gabriel Schneider Date: Wed, 11 Nov 2026 20:08:00 -0300 Subject: trace: what push, call and ret touch; names for data; the stack's roles, sharper For a disassembly that points at the stack word it touched. The stack's own instructions have touches (push, call and enter write at rsp-8; pop and ret read at rsp; leave at rbp), and a rep string instruction its whole range. An address a global or static object holds is named (head+0, libc.so.6!main_arena+96). The saved rbp has its role at the push that makes it; every local in a shared word is given; the stack protector's copy is found by seeing its store; ztrace show labels each stack word. Six bytes a moment and 3% slower on the list by instruction. Co-Authored-By: Claude Fable 5.2 Claude-Session: https://claude.ai/code/session_016Y9MNCKgYCGiANnEwcEQxa --- src/cli/disasm.c | 32 +++++++++++++++++++++++++++++--- src/cli/show.zig | 40 ++++++++++++++++++++++++++++++++++++---- 2 files changed, 65 insertions(+), 7 deletions(-) (limited to 'src/cli') diff --git a/src/cli/disasm.c b/src/cli/disasm.c index 6f2ffa4..7f9f5e2 100644 --- a/src/cli/disasm.c +++ b/src/cli/disasm.c @@ -7,7 +7,8 @@ static csh handle; // A memory operand of an instruction, worked out against the registers. struct zt_touch { uint64_t addr; - uint8_t size; + // A rep string instruction's is the whole range its count covers. + uint32_t size; // 1 the instruction reads it, 2 it writes it, 3 both. uint8_t how; }; @@ -113,7 +114,13 @@ size_t zt_disasm(const uint8_t *code, size_t len, uint64_t addr, char *text, siz // 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++) { + // A string instruction (movs, cmps, stos, lods, scas: the one-byte + // opcodes a4 to af but test's a8 and a9) repeated touches its count's + // elements from rsi or rdi on, or down to them with the direction flag set. + uint8_t code0 = x->opcode[0]; + int string = x->opcode[1] == 0 && code0 >= 0xa4 && code0 <= 0xaf && code0 != 0xa8 && code0 != 0xa9; + uint64_t times = string && (x->prefix[0] == X86_PREFIX_REP || x->prefix[0] == X86_PREFIX_REPNE) ? regs[11] : 1; + for (int i = 0; i < x->op_count && *count < room && times > 0; 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; @@ -128,11 +135,30 @@ size_t zt_disasm(const uint8_t *code, size_t len, uint64_t addr, char *text, siz } if (op->mem.segment == X86_REG_FS) at += regs[21]; if (op->mem.segment == X86_REG_GS) at += regs[22]; + uint64_t size = op->size * times; + if (regs[18] & 0x400) at -= (times - 1) * op->size; touches[*count].addr = at; - touches[*count].size = op->size; + touches[*count].size = size > UINT32_MAX ? UINT32_MAX : (uint32_t)size; touches[*count].how = (op->access & CS_AC_READ ? 1 : 0) | (op->access & CS_AC_WRITE ? 2 : 0); ++*count; } + // The stack's own instructions touch memory no operand names, after + // what their operands touch: a push writes the word under rsp, a + // call its return address there, enter the saved rbp; a pop and a + // return read the word at rsp, leave the saved rbp at rbp. A word is + // 8 bytes, 2 with the operand-size prefix (pushf, push ax). + uint64_t rsp = regs[19]; + uint32_t word = x->prefix[2] == 0x66 ? 2 : 8; + struct zt_touch t = { 0, 0, 0 }; + switch (insn->id) { + case X86_INS_PUSH: case X86_INS_PUSHF: case X86_INS_PUSHFQ: t = (struct zt_touch){ rsp - word, word, 2 }; break; + case X86_INS_CALL: case X86_INS_ENTER: t = (struct zt_touch){ rsp - 8, 8, 2 }; break; + case X86_INS_POP: case X86_INS_POPF: case X86_INS_POPFQ: t = (struct zt_touch){ rsp, word, 1 }; break; + case X86_INS_RET: t = (struct zt_touch){ rsp, 8, 1 }; break; + case X86_INS_LEAVE: t = (struct zt_touch){ regs[4], 8, 1 }; break; + default: break; + } + if (t.size && *count < room) touches[(*count)++] = t; } cs_free(insn, 1); return size; diff --git a/src/cli/show.zig b/src/cli/show.zig index 032538f..e7c552c 100644 --- a/src/cli/show.zig +++ b/src/cli/show.zig @@ -10,7 +10,9 @@ pub const usage = \\With one (negative counts from the end), the context as of that moment, \\pwndbg's way: each thread's registers, every one followed as far as it \\points (a name for code, a string where there is one), the instructions - \\at the pc, the top of the stack followed likewise, the source line and + \\at the pc, the top of the stack followed likewise, each word with what + \\it is where the recording says (saved rbp, return address, a local by + \\name, the canary's copy, argv[1]), the source line and \\the open calls; then glibc's heap when it was recorded (record --heap: \\its chunks, the tcache and the bins, each chunk on a list followed by its \\link as stored, in brackets, where safe-linking changed it), the live @@ -82,6 +84,34 @@ fn telescope(m: zt.store.Moment, w: *std.Io.Writer, value: u64, colour: bool) !v } } +/// What a stack word is (`zt.telescope.slots`), after its chain: `saved +/// rbp`, `return address`, a local by name (`buf+8` a word into it, `i[0..4], +/// n[4..8]` where it fills part of the word), `argv[1]`; nothing where the +/// recording does not say. `word` is what it holds: 0 ends argv and envp. +fn role(w: *std.Io.Writer, slot: zt.telescope.Slot, word: u64, colour: bool) !void { + if (slot.role == .none) return; + try w.writeAll(if (colour) " \x1b[2m" else " "); + switch (slot.role) { + .none => {}, + .saved => |reg| try w.print("saved {s}", .{reg}), + .return_address => try w.writeAll("return address"), + .local => |first| for ([_]?zt.telescope.Local{first} ++ slot.also, 0..) |each, i| { + const l = each orelse break; + if (i > 0) try w.writeAll(", "); + try w.writeAll(l.name); + if (l.offset > 0) try w.print("+{x}", .{l.offset}); + const from: i64 = @max(0, -l.offset); + const to: i64 = if (l.size == 0) 8 else @min(8, @as(i64, l.size) - l.offset); + if (from > 0 or to < 8) try w.print("[{d}..{d}]", .{ from, to }); + }, + .argc => try w.writeAll("argc"), + .argv => |i| if (word == 0) try w.writeAll("argv end") else try w.print("argv[{d}]", .{i}), + .envp => |i| if (word == 0) try w.writeAll("envp end") else try w.print("envp[{d}]", .{i}), + .auxv => |i| try w.print("auxv[{d}]", .{i}), + } + if (colour) try w.writeAll("\x1b[0m"); +} + pub fn state(arena: std.mem.Allocator, trace: *zt.Trace, index: u64, w: *std.Io.Writer, colour: bool) !void { const m = try trace.moment(index); try w.print("moment {d} of {d}, {d} events so far\n", .{ index, trace.len, try m.count() }); @@ -114,16 +144,18 @@ pub fn state(arena: std.mem.Allocator, trace: *zt.Trace, index: u64, w: *std.Io. if (pc >= regs.rip) below += 1; pc += insn.len; } - // The top of the stack, a word a row. + // The top of the stack, a word a row, and what each word is. var words: [8]u64 = undefined; var known: [64]bool = undefined; try m.read(regs.rsp, std.mem.sliceAsBytes(&words), &known); + var slots: [8]zt.telescope.Slot = undefined; + _ = try zt.telescope.slots(m, ®s, regs.rsp, &slots); if (known[0]) try w.writeAll("\n"); - for (words, 0..) |word, i| { + for (words, slots, 0..) |word, slot, i| { if (!known[i * 8]) break; try w.print(" {x:0>2}:{x:0>4}│ {s} ", .{ i, i * 8, if (i == 0) "rsp" else " " }); try telescope(m, w, regs.rsp + i * 8, colour); - _ = word; + try role(w, slot, word, colour); try w.writeAll("\n"); } } -- cgit v1.3