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 --- README.md | 76 +++++++++++--- build/debugger.zig | 15 ++- src/cli/disasm.c | 32 +++++- src/cli/show.zig | 40 +++++++- src/trace/Debugger.zig | 107 ++++++++++++++++++-- src/trace/Symbols.zig | 4 + src/trace/debugger_test.zig | 240 ++++++++++++++++++++++++++++++++++++++++++++ src/trace/event.zig | 4 +- src/trace/telescope.zig | 77 +++++++++++--- targets/stack.c | 36 +++++++ 10 files changed, 585 insertions(+), 46 deletions(-) create mode 100644 targets/stack.c diff --git a/README.md b/README.md index f166675..71142c3 100644 --- a/README.md +++ b/README.md @@ -67,10 +67,11 @@ is any of them, in any order: time travel is an index. | `string(id)` | `string` | | `events()` | everything, as it came | -From a debugger's recording, `zt.telescope`: `instruction(m, addr, buf)` is an instruction's text, its source `line`, and for a direct call its callee's `params` (DWARF's); `frames(m, regs, out)` is the call chain with each frame's extent on the stack, `low` to `high` (a `stack()` frame's is up to `sp + 8`), walked by the call frame information the recording holds (below), else by frame pointer; `slots(m, regs, low, out)` is what each stack word from `low` is (`out[i]` the word at `low + 8 i`), a `Slot`: its `frame`, an index into `frames` (null in none), and its `role`: `.saved` (the register a function saved for its caller there: `"rbx"`, `"rbp"`, `"r12"` to `"r15"`), `.return_address`, `.local` (a variable or parameter: `name`, `size`, and `offset`, where the word is in it), or the process's first stack, `.argc`, `.argv` i, `.envp` i (i = argc, envc: the zero that ends them) and `.auxv` i (the auxiliary vector's i-th word, two an entry); `.none` where the recording does not say; `function(m, pc)` is the function that holds a pc as the recorded walk went through it, its `start` and `len` by its call frame information (a PLT stub's 16 bytes), for every frame of every stop; `got(m, slot)` is what a GOT slot holds and whether that is still `lazy` (its name has `@plt`: the way back into the PLT, so the next call runs the loader's resolver) or bound. +From a debugger's recording, `zt.telescope`: `instruction(m, addr, buf)` is an instruction's text, its source `line`, and for a direct call its callee's `params` (DWARF's); `frames(m, regs, out)` is the call chain with each frame's extent on the stack, `low` to `high` (a `stack()` frame's is up to `sp + 8`), walked by the call frame information the recording holds (below), else by frame pointer; `slots(m, regs, low, out)` is what each stack word from `low` is (`out[i]` the word at `low + 8 i`), a `Slot`: its `frame`, an index into `frames` (null in none), and its `role`: `.saved` (the register a function saved for its caller there: `"rbx"`, `"rbp"`, `"r12"` to `"r15"`; or `"canary"`, its copy of the stack protector's value, below), `.return_address`, `.local` (a variable or parameter, a `Local`: `name`, `size`, and `offset`, where the word is in it: 0 at its first byte, 8 a word on, -4 its high half), or the process's first stack, `.argc`, `.argv` i, `.envp` i (i = argc, envc: the zero that ends them) and `.auxv` i (the auxiliary vector's i-th word, two an entry); `.none` where the recording does not say. Where small locals share a word, `role` is the one that begins first in it and `also` the others in the order they begin (two ints: `i` at offset 0 and `n` at -4, so `i, n`; a viewer writes `n, high half` from `n`'s -4 and size 4). At an instruction that saves a register (`push rbp`, the frame being made) the word it is about to write, under rsp, has the register's role already, in frame 0: the call frame information says where it goes from that instruction on (the row that begins after it); `function(m, pc)` is the function that holds a pc as the recorded walk went through it, its `start` and `len` by its call frame information (a PLT stub's 16 bytes), for every frame of every stop; `got(m, slot)` is what a GOT slot holds and whether that is still `lazy` (its name has `@plt`: the way back into the PLT, so the next call runs the loader's resolver) or bound. For a call followed down, each frame has its module (`mapping`), whether it has source (its instructions' `line`), its function's bounds (`function`), and, from a caller, three instructions round its call: the return address, the call `back` from it, and the row before the call `back` from that. A function as a graph (IDA's, Binary Ninja's): `blocks(m, pc, out)` gives the basic blocks of the function that holds `pc`, a `Graph`: its bounds, its blocks in address order, the one that holds the pc (`here`), and whether every instruction's text is recorded (`text`; then `instruction` reads each block's rows). A `Block` has its bounds, how it ends (`Debugger.End`: `fall` into the next, an unconditional `jump`, a conditional `branch`, `ret`, an `indirect` jump whose targets are not known, a `tail` call out of the function, a call that does not return, `noreturn`: the function's last instruction, or `abort`, `exit`, `__stack_chk_fail` and their kin) and its edges: `to`, where a jump, a taken branch, a tail call or a call that does not return goes, and `falls`, the block after it for `fall` and a branch not taken. A call that returns ends no block. The debugger records them once a function, at the first stop in it, decoded from its own instructions (its bounds from its symbol, else its unwind information: a stripped library's function has them too), with every instruction's text and the names of what its calls and its jumps out of it reach, up to `Debugger.function_text` instructions (2048); past that the blocks without the text (`sqlite3VdbeExec`, 11 077 instructions: 2 510 blocks). Code with no bounds (the C runtime's start, a JIT's) has none. Which edges a run took: by instruction, a stop whose instruction is a jump or a return has a `branch` event (a2 its pc, a0 where it goes, a1 1 when it will go there); its pc is the last instruction of the block that holds it, and the next stop is at `to` when a1 is 1, else at `falls` (a `ret`'s, at its caller). By line or by breakpoint there are no stops between, so a reader knows the blocks the stops are in and, at a stop on a jump, the edge it is about to take; the path between two stops it does not know. What it costs: the list of `anim/tools` by instruction, 243 bytes a moment against 224 (every function stopped in, recorded whole once) and 25 600 moments a second against 26 300; the first stop in `sqlite3BtreeInsert` (1 045 instructions) 118 KB and 2 to 4 ms, in `sqlite3VdbeExec` 80 KB (its blocks alone) and 6 ms; SQLite by breakpoint on `sqlite3BtreeInsert` 317 bytes a moment against 310. Code is named at every pc, a function's first instruction too, and a PLT stub and a GOT slot for the function they lead to: `open@plt+0`, `open@got+0` (from the relocations and the dynamic symbols; an IFUNC's in libc by the function its resolver is, `libc.so.6!strlen@plt+0`); of the names libc gives one address (`open`, `open64`, `__open64`) the one the program's relocations use. A slot is named when an instruction jumps or calls through it. +Data is named as code is, by the global or static object of the symbol tables that holds it, `+` how far into it in decimal: `head+0`, `big+4096`, `libc.so.6!main_arena+96`, `libc.so.6!_IO_2_1_stdout_+0` (libc's statics from its separate debug file). A reader gets it as it gets a code name, the string whose id is the address (`Moment.string`, `m.trace.strings.get`), for every address an instruction touches and every pointer the telescope follows into a module's data, its .bss past the file's last page included. An address no object holds (a string literal, the stack, the heap) has none; the debugger looks each new address up once. With `--alloc`, every live allocation and the 16 bytes of allocator header before it are in `read` at each stop (its first 4 KiB; what changed, a stop at a time; 256 KiB of them at most, lowest addresses first: `Debugger.allocation_watch`). Recorded `--by insn`, the instructions that ran between two moments are the `rip` of each moment between them; `--by line` does not keep them. A byte in `read` is as it was when the debugger last sent it: the stack window, the watched regions and the live allocations at every stop, a `read`'s or `write`'s buffer at that syscall (`-s` bytes of it). Memory it looked at once and not since is known and may be stale. @@ -303,11 +304,35 @@ moment against 249, 12 000 moments a second against 16 000. A stop also says what its instruction is about to do to memory, worked out from capstone's operands and the registers: a `touch` event (the -address, the size, read or write) with what is there, and after the step -what a write left there. So an instruction-level recording has every -memory write the stepped thread makes, with nothing watched and nothing -compiled in. A call, a jump or a return at the pc is a `branch` event: -where it goes, and for a conditional jump whether it will. +address, the size, read or write) with what is there (and `--around` +bytes each side), and after the step what a write left there. So an +instruction-level recording has every memory write the stepped thread +makes, with nothing watched and nothing compiled in. The stack's own +instructions touch memory no operand names, and have their touch after +their operands' (a2 the pc, as every touch): `push`, `pushfq` and `call` +write the 8 bytes at rsp - 8 (the pushed register, the flags, the return +address), `enter` the saved rbp there; `pop`, `popfq` and `ret` read the 8 +at rsp, `leave` the saved rbp at rbp (2 bytes for `pushf`, `push ax`: the +operand-size prefix). The string instructions (`movs`, `stos`, `lods`, +`cmps`, `scas`) name theirs, at rdi and rsi; repeated (`rep`), the touch is +the range the count covers, rcx elements up from there, or down to there +with the direction flag set, none when rcx is 0. Stepped by instruction a +`rep` stops after each element (rcx one less at each stop, the range one +element shorter, the pc the same); of a range only its first 4 KiB, from +where it begins, is recorded at a stop. A call, a jump or a return at the +pc is a `branch` event: where it goes, and for a conditional jump whether +it will. What the stack's touches and the names of data cost, on the list +of `anim/tools` by instruction (`--from main --stack 1024 --around 80 +--disasm 48`, -O0, 2 464 moments): 205 more touches and 135 names, 362 +bytes a moment against 356, and 11 800 moments a second against 12 100 +(the median of eight runs each). + +How far an instruction moves rsp or rbp (`push` -8, `sub rsp, 0x18` +-0x18, `leave` up to the caller's frame, `ret` +8) is no event: it is the +next stop's register less this one's, both `Moment.regs` of the same +thread. By instruction the next stop is the next moment, or the one after +the syscall's when the instruction is a `syscall`; by line or breakpoint +it is the sum of everything between. **The call chain** is walked at every stop by the unwind tables (`.eh_frame`, `.debug_frame`, through `std.debug.Dwarf.Unwind`), as gdb @@ -323,16 +348,41 @@ places by it (`DW_OP_fbreg`), each with its size and its block's range of code. `zt.telescope.frames` walks the same chain again from the recording, which needs no ELF file, and `slots` names the words: the saved registers and return addresses of every frame; the locals of -O0 and -Og code where -the frame's pc is in their block (a leaf's in the red zone too); and argc, -argv, envp and the auxiliary vector, from where the stack began (the -string `event.frameId(0)`, `Debugger.Start`, read from `/proc/pid/stat`). +the frame's pc is in their block (a leaf's in the red zone too), every one +that shares a word; the stack protector's copy; and argc, argv, envp and +the auxiliary vector, from where the stack began (the string +`event.frameId(0)`, `Debugger.Start`, read from `/proc/pid/stat`). +`ztrace show FILE MOMENT` prints each stack word's role after its chain: +`saved rbp`, `saved canary`, `return address`, `buf`, `buf+8` (a word into +it), `i[0..4], n[4..8]` (the bytes of the word each fills), `argv[1]`. + +**The canary.** A function built with the stack protector copies the +value at `fs:[0x28]` into its frame and checks it before it returns: that +word's role is `.saved = "canary"`, from the store on. It is found by +seeing the store, not by position: when the debugger records a function's +frame it decodes its first 64 instructions, once, for a `mov reg, +fs:[0x28]` and the next store of that register to the stack (`mov [rbp - +8], rax` at -O0 by gcc and clang, `mov [rsp + 0x18], rax` in libc), and +records the word as a local with no name, placed from the frame base (from +the CFA in a function without debug info). By position it would be a guess: +the canary is under the saved registers, but with padding between, and a +function with no array has none at all. It cannot be known where the store +is past the first 64 instructions (a prologue shrink-wrapped behind an +early return), where its register is neither the frame base nor the one +the CFA is kept in at the store (`mov [rsp + 8], rax` in a function that +keeps rbp, at -O2), and in a recording made without a disassembler. + What it does not say: a local in a register or a list of places (-O1 and up), or in a block whose code is in pieces (`DW_AT_ranges`); a local's name in a prologue or an epilogue where the frame base is rbp and rbp is -not yet or no longer the frame's; one of two small locals sharing a word -(the first keeps it); a frame whose CFA is an expression (a signal frame) -or that has no unwind tables, which falls back on the frame pointer; -inlined calls, which have no frame (gdb lists them). +not yet or no longer the frame's; a frame whose CFA is an expression (a +signal frame) or that has no unwind tables, which falls back on the frame +pointer; inlined calls, which have no frame (gdb lists them). In +`targets/stack.c`'s loop (gcc and clang, -O0) and in libc's `sbrk` +between its `push rbp` and its `mov rbp, rsp` (the list of `anim/tools`), +the roles are where gdb's `info frame` and `info locals` put the saved +registers and the locals (`&i`, `&n`, `&buf`); gdb names no canary, whose +word holds what `fs:[0x28]` does. What it costs, on `fib(12)` by instruction (142 000 moments): 154 000 moments a second and 64 bytes a moment with the disassembly and the diff --git a/build/debugger.zig b/build/debugger.zig index 47547e8..e50c82c 100644 --- a/build/debugger.zig +++ b/build/debugger.zig @@ -29,6 +29,10 @@ pub fn add(ctx: Ctx) void { const overflow_mod = b.createModule(.{ .target = ctx.target, .optimize = .Debug, .link_libc = true, .omit_frame_pointer = false, .stack_protector = false }); overflow_mod.addCSourceFiles(.{ .root = b.path("targets"), .files = &.{"overflow.c"}, .flags = &.{ "-O0", "-g" } }); const overflow = b.addExecutable(.{ .name = "overflow", .root_module = overflow_mod }); + // The stack's own instructions and the string ones; -O0 with a canary. + const stack_mod = b.createModule(.{ .target = ctx.target, .optimize = .Debug, .link_libc = true, .omit_frame_pointer = false, .stack_protector = true }); + stack_mod.addCSourceFiles(.{ .root = b.path("targets"), .files = &.{"stack.c"}, .flags = &.{ "-O0", "-g" } }); + const stack = b.addExecutable(.{ .name = "stack", .root_module = stack_mod }); // Zig, no libc: always a debug build, so its lines are the source's. const graph = b.addExecutable(.{ .name = "graph", .root_module = b.createModule(.{ .root_source_file = b.path("targets/graph.zig"), @@ -36,7 +40,7 @@ pub fn add(ctx: Ctx) void { .optimize = .Debug, }) }); - const targets = [_]*std.Build.Step.Compile{ fib, list, list_o0, hello, clock, overflow, threads, libc_glibc, libc_musl, graph }; + const targets = [_]*std.Build.Step.Compile{ fib, list, list_o0, hello, clock, overflow, stack, threads, libc_glibc, libc_musl, graph }; for (targets) |t| b.getInstallStep().dependOn(&b.addInstallArtifact(t, .{ .dest_dir = .{ .override = .{ .custom = "targets" } } }).step); // The compiler's own checks (UBSan, on by default in a debug build) are @@ -105,6 +109,7 @@ pub fn add(ctx: Ctx) void { paths.addOptionPath("fib_recording", recording); // The overflow built with ASan by the system's compilers and recorded by the command, in the tests. paths.addOptionPath("overflow_source", b.path("targets/overflow.c")); + paths.addOptionPath("stack_source", b.path("targets/stack.c")); paths.addOptionPath("ztrace", ctx.exe.getEmittedBin()); // A call followed down by instruction: through the PLT, the loader's @@ -125,6 +130,14 @@ pub fn add(ctx: Ctx) void { loops.addArg("3"); loops.expectStdErrMatch("exit 0"); + // The stack's own instructions, the string ones, a canary, by instruction from main to the end. + const stack_run = b.addRunArtifact(ctx.exe); + stack_run.addArgs(&.{ "record", "--by", "insn", "--from", "main", "-o" }); + paths.addOptionPath("stack_recording", stack_run.addOutputFileArg("stack.ztrace")); + stack_run.addArg("--"); + stack_run.addArtifactArg(stack); + stack_run.expectStdErrMatch("exit 0"); + // Into libc by its PLT stub, syscalls not followed: the libraries' // mappings are read when a name is wanted in them, and the callee's // first instruction is named in the recording, its caller under it. 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"); } } diff --git a/src/trace/Debugger.zig b/src/trace/Debugger.zig index 4ca7eca..729b93a 100644 --- a/src/trace/Debugger.zig +++ b/src/trace/Debugger.zig @@ -107,8 +107,8 @@ telescope_words: usize = 16, /// the 32 at it) or an instruction touches, no further than its mapping: /// the region a viewer shows round what a pointer leads to. telescope_around: usize = 0, -/// The addresses already named, and the instructions already recorded -/// with the length of each. +/// The addresses already named (and the data addresses looked up that have +/// no name), and the instructions already recorded with the length of each. described: std.AutoHashMapUnmanaged(u64, void) = .empty, code_len: std.AutoHashMapUnmanaged(u64, u8) = .empty, /// The call frame information at each pc a recorded walk met, its @@ -158,7 +158,11 @@ pub const Insn = struct { len: u8, text: []const u8, touches: []const Touch = &. /// a jump on a condition. pub const Branch = extern struct { target: u64 = 0, kind: u8 = 0, taken: u8 = 1, indirect: u8 = 0, conditional: u8 = 0 }; /// Memory an instruction touches: `how` is 1 for a read, 2 a write, 3 both. -pub const Touch = extern struct { addr: u64, size: u8, how: u8 }; +/// The memory its operands name, and what the stack's own instructions +/// touch without naming it: a push's word and a call's return address +/// under rsp, a pop's and a return's at rsp, leave's saved rbp at rbp. A +/// `rep` string instruction's is the range its count covers. +pub const Touch = extern struct { addr: u64, size: u32, how: u8 }; pub const Watch = struct { addr: u64, len: usize }; const Page = struct { @@ -537,12 +541,25 @@ fn snapshotRound(d: *Debugger, addr: u64, size: usize) !void { try d.snapshot(lo, @min(m.end, addr + size + d.telescope_around) - lo); } +/// The most of one touch a stop records: a `rep` string instruction's +/// range can be megabytes, and stepped by instruction it does one element +/// a step. +const touch_max = 4096; + +/// `snapshotRound` of a touch, `touch_max` of it at most from where a +/// `rep` begins: its start, or its end with the direction flag set. +// shortcut: of a `rep` past 4 KiB run between two stops (by line, by breakpoint) the rest of what it wrote is not recorded, upgrade by reading the range in pages at the stop after when someone records memset by line. +fn snapshotTouch(d: *Debugger, t: Touch) !void { + const n = @min(t.size, touch_max); + try d.snapshotRound(if (d.regs.eflags & 0x400 != 0) t.addr + t.size - n else t.addr, n); +} + /// The instructions at the pc and after it, each a string (`event.codeId`) /// recorded once: what a viewer shows as the disassembly. fn emitCode(d: *Debugger) !void { const insnAt = d.disasm orelse return; // What the instruction before wrote is there to read now. - for (d.written[0..d.written_count]) |t| try d.snapshotRound(t.addr, t.size); + for (d.written[0..d.written_count]) |t| try d.snapshotTouch(t); d.written_count = 0; var code: [256]u8 = undefined; var text: [192]u8 = undefined; @@ -553,7 +570,8 @@ fn emitCode(d: *Debugger) !void { var touches: [2]Touch = undefined; if (insnAt(code[0..15], d.regs.rip, text[1..], &d.regs, &touches)) |insn| { for (insn.touches) |t| { - try d.snapshotRound(t.addr, t.size); + try d.snapshotTouch(t); + try d.nameData(t.addr); try d.emit(.{ .kind = .touch, .aux = t.how, .a = .{ t.addr, t.size, d.regs.rip, 0, 0, 0 } }, ""); if (t.how & 2 != 0) { d.written[d.written_count] = t; @@ -837,6 +855,8 @@ fn chase(d: *Debugger, value: u64) !void { const m = d.syms.mappingAt(v) orelse return; if (m.prot & 1 == 0) return; if (m.prot & 4 != 0) return d.nameAddress(v); + // A file's data, or the zeroes after it; not "[stack]" nor "[heap]". + if (m.name.len == 0 or m.name[0] == '/') try d.nameData(v); const lo = @max(m.start, v -| d.telescope_around) & ~@as(u64, 7); const bytes = try d.snap(lo, @min(m.end, v + 32 + d.telescope_around) - lo); const off: usize = @intCast(v - lo); @@ -859,6 +879,16 @@ fn nameAddress(d: *Debugger, addr: u64) !void { try d.emit(.{ .kind = .string, .a = .{ addr, 0, 0, 0, 0, 0 } }, w.buffered()); } +/// `nameAddress` of a data address that a global or static object of the +/// symbol tables holds: "head+0", "libc.so.6!main_arena+96". One a module's +/// symbols do not hold (a string literal, the GOT's header) is looked up +/// once; one outside every module (the stack, the heap) never. +fn nameData(d: *Debugger, addr: u64) !void { + if (d.syms.moduleAt(addr) == null or d.described.contains(addr)) return; + if (d.syms.symbolAt(addr) != null) return d.nameAddress(addr); + try d.described.put(d.gpa, addr, {}); +} + fn emitStop(d: *Debugger, stop: Stop) !void { const r = &d.regs; const regs = std.mem.asBytes(r); @@ -1387,27 +1417,84 @@ pub const Recorded = struct { /// `Symbols.Function.base`), how many rows (2 bytes), the rows /// (`Symbols.Row`), then each local (`Symbols.Local`): its offset from the /// base, its size, the stretch of the function's code it is in, from and -/// to (4 bytes each), its name and a zero. +/// to (4 bytes each), its name and a zero. The copy of the stack +/// protector's value it keeps (`canary`) is a local with no name, from its +/// store on; a function with no debug info then has the CFA for its base. fn emitFunction(d: *Debugger, f: Symbols.Function) !void { if ((try d.described.getOrPut(d.gpa, event.frameId(f.start))).found_existing) return; + const own = d.canary(f, if (f.base == Symbols.no_base) Symbols.base_cfa else f.base); + const base = if (own != null and f.base == Symbols.no_base) Symbols.base_cfa else f.base; var text: std.Io.Writer.Allocating = .init(d.gpa); defer text.deinit(); const w = &text.writer; - try w.writeInt(u32, std.math.cast(u32, f.len) orelse 0, .little); - try w.writeByte(f.base); + const len = std.math.cast(u32, f.len) orelse 0; + try w.writeInt(u32, len, .little); + try w.writeByte(base); try w.writeInt(u16, @intCast(@min(f.rows.len, std.math.maxInt(u16))), .little); try w.writeAll(std.mem.sliceAsBytes(f.rows[0..@min(f.rows.len, std.math.maxInt(u16))])); - for (f.locals) |local| { + const kept: [1]Symbols.Local = .{.{ .name = "", .offset = if (own) |c| c.offset else 0, .size = 8, .from = if (own) |c| c.from else 0, .to = len }}; + // Locals by a base that is not the one written are no use to a reader. + for ([_][]const Symbols.Local{ if (base == f.base) f.locals else &.{}, kept[0..@intFromBool(own != null)] }) |list| for (list) |local| { try w.writeInt(i32, local.offset, .little); try w.writeInt(u32, local.size, .little); try w.writeInt(u32, local.from, .little); try w.writeInt(u32, local.to, .little); try w.writeAll(local.name); try w.writeByte(0); - } + }; try d.emit(.{ .kind = .string, .a = .{ event.frameId(f.start), 0, 0, 0, 0, 0 } }, text.written()); } +/// Where a function keeps its copy of the stack protector's value: the +/// word its prologue stores what it read at `fs:[0x28]` into (`mov rax, +/// fs:[0x28]` then `mov [rbp - 8], rax`), seen by decoding its first 64 +/// instructions, once. Its offset from the frame base `base` (a register, +/// or the CFA), and how far into the function the store is. Null without +/// one, or where a reader could not place it: stored by a register that is +/// neither the base nor, with the CFA for base, the one the CFA is kept in +/// (rsp in a function that keeps rbp). +fn canary(d: *Debugger, f: Symbols.Function, base: u8) ?struct { offset: i32, from: u32 } { + const insnAt = d.disasm orelse return null; + var code: [1024]u8 = undefined; + const n = d.read(f.start, code[0..@min(code.len, f.len)]); + // Registers whose values tell, in a touch's address, which it was made from. + const by_rbp: u64 = 1 << 44; + const by_rsp: u64 = 2 << 44; + const by_fs: u64 = 3 << 44; + const regs: Regs = .{ .rbp = by_rbp, .rsp = by_rsp, .fs_base = by_fs }; + var text: [192]u8 = undefined; + var touches: [2]Touch = undefined; + // The register the value was read into, as the text names it. + var held: [8]u8 = undefined; + var held_len: usize = 0; + var off: usize = 0; + for (0..64) |_| { + const at = off; + const insn = insnAt(code[off..n], f.start + off, &text, ®s, &touches) orelse return null; + off += insn.len; + if (held_len == 0) { + const comma = std.mem.indexOfScalar(u8, insn.text, ',') orelse continue; + for (insn.touches) |t| if (t.addr == by_fs + 0x28 and t.size == 8 and t.how == 1 and std.mem.startsWith(u8, insn.text, "mov ") and comma - 4 <= held.len) { + held_len = comma - 4; + @memcpy(held[0..held_len], insn.text[4..comma]); + }; + continue; + } + // The next store of that register. + if (insn.touches.len == 0 or insn.touches[0].how != 2 or insn.text.len < held_len + 2) continue; + const tail = insn.text[insn.text.len - held_len - 2 ..]; + if (!std.mem.eql(u8, tail[2..], held[0..held_len]) or !std.mem.eql(u8, tail[0..2], ", ")) continue; + const t = insn.touches[0]; + const from_rbp: i64 = @bitCast(t.addr -% by_rbp); + const from_rsp: i64 = @bitCast(t.addr -% by_rsp); + const reg: u8, const disp: i64 = if (@abs(from_rbp) < 1 << 32) .{ 6, from_rbp } else if (@abs(from_rsp) < 1 << 32) .{ 7, from_rsp } else return null; + const row = f.rowAt(f.start + at); + const offset = if (base == Symbols.base_cfa and row.cfa_reg == reg) disp - row.cfa else if (base == reg) disp else return null; + return .{ .offset = std.math.cast(i32, offset) orelse return null, .from = @intCast(at) }; + } + return null; +} + /// Where the process's first stack is: the stack pointer at its exec, where /// argc is, then the `argc` pointers of argv and a zero, the `envc` of envp /// and a zero, and the `auxc` entries of the auxiliary vector (two words diff --git a/src/trace/Symbols.zig b/src/trace/Symbols.zig index da13047..684dd95 100644 --- a/src/trace/Symbols.zig +++ b/src/trace/Symbols.zig @@ -271,6 +271,10 @@ pub fn refresh(s: *Symbols, pid: std.os.linux.pid_t) !Maps { mod.start = @min(mod.start, m.start); mod.end = @max(mod.end, m.end); } + // Its .bss past the file's last page is the anonymous mapping right after it. + for (s.maps.list) |m| if (m.start == mod.end and m.end > m.start and m.name.len == 0 and m.prot & 4 == 0) { + mod.end = m.end; + }; if (mod.end == 0) mod.start = 0; // unmapped since } return old; diff --git a/src/trace/debugger_test.zig b/src/trace/debugger_test.zig index ac54356..66174fc 100644 --- a/src/trace/debugger_test.zig +++ b/src/trace/debugger_test.zig @@ -874,6 +874,246 @@ test "a recording tells each stack word's role: saved registers, return addresse try std.testing.expectEqual(@as(u64, 2), std.mem.readInt(u64, &argc, .little)); } +/// A stop of a recording by instruction: the registers it stopped with, +/// and what its instruction touches as recorded there (`touch` events: +/// address, size, how), the memory its operands name first. +const Stop = struct { regs: zt.Regs, touches: [2][3]u64 = undefined, n: usize = 0 }; + +/// The stop of thread `tid` at a moment; null where the moment is none (a syscall's). +fn stopAt(m: zt.store.Moment, tid: u32) !?Stop { + var s: Stop = .{ .regs = (try m.regs(tid)) orelse return null }; + var stopped = false; + var events = try m.events(gpa); + defer events.deinit(gpa); + while (events.next()) |item| switch (item.ev.kind) { + .step, .breakpoint => stopped = true, + .touch => if (s.n < s.touches.len) { + s.touches[s.n] = .{ item.ev.a[0], item.ev.a[1], item.ev.aux }; + s.n += 1; + }, + else => {}, + }; + return if (stopped) s else null; +} + +/// The word at `addr` in a moment, where the recording has all of it. +fn wordIn(m: zt.store.Moment, addr: u64) !?u64 { + var bytes: [8]u8 = undefined; + var known: [8]bool = undefined; + try m.read(addr, &bytes, &known); + return if (std.mem.indexOfScalar(bool, &known, false) == null) std.mem.readInt(u64, &bytes, .little) else null; +} + +test "the stack's own instructions touch the words they push and pop, a repeated string instruction its count's range" { + var trace = try zt.Trace.open(gpa, io, targets.stack_recording); + defer trace.close(); + const tids = try (try trace.moment(0)).threads(gpa); + defer gpa.free(tids); + var text: [256]u8 = undefined; + const kinds = [_][]const u8{ "push", "call", "enter", "pushfq", "pop", "ret", "popfq", "leave", "rep" }; + var seen: [kinds.len]usize = @splat(0); + for (0..trace.len - 1) |i| { + const m = try trace.moment(i); + const s = (try stopAt(m, tids[0])) orelse continue; + const regs = s.regs; + const insn = (try zt.telescope.instruction(m, regs.rip, &text)) orelse continue; + const k = for (kinds, 0..) |kind, k| { + if (std.mem.startsWith(u8, insn.text, kind) and (insn.text.len == kind.len or insn.text[kind.len] == ' ')) break k; + } else continue; + seen[k] += 1; + // A stop by instruction is followed by the next: these make no syscall. + const next = try trace.moment(i + 1); + const after = (try next.regs(tids[0])).?; + try std.testing.expect(s.n >= 1); + // The stack's own touch comes after what the operands name. + const t = s.touches[s.n - 1]; + switch (k) { + // A word written under rsp; the stop after has it, and rsp on it + // (enter's rbp, its 16 bytes of locals under it). + 0...3 => { + try std.testing.expectEqual([3]u64{ regs.rsp - 8, 8, 2 }, t); + try std.testing.expectEqual(regs.rsp - 8, if (k == 2) after.rbp else after.rsp); + const written = (try wordIn(next, regs.rsp - 8)).?; + if (k == 1) try std.testing.expectEqual(regs.rip + insn.len, written); + if (k == 2 or std.mem.eql(u8, insn.text, "push rbp")) try std.testing.expectEqual(regs.rbp, written); + if (std.mem.eql(u8, insn.text, "push rbx")) try std.testing.expectEqual(regs.rbx, written); + }, + // The word at rsp read: where a return goes, what a pop takes. + 4...6 => { + try std.testing.expectEqual([3]u64{ regs.rsp, 8, 1 }, t); + try std.testing.expectEqual(regs.rsp + 8, after.rsp); + const read = (try wordIn(m, regs.rsp)).?; + if (k == 5) try std.testing.expectEqual(after.rip, read); + if (std.mem.eql(u8, insn.text, "pop rbp")) try std.testing.expectEqual(after.rbp, read); + if (std.mem.eql(u8, insn.text, "pop rbx")) try std.testing.expectEqual(after.rbx, read); + }, + // The saved rbp at rbp, rsp past it. + 7 => { + try std.testing.expectEqual([3]u64{ regs.rbp, 8, 1 }, t); + try std.testing.expectEqual((try wordIn(m, regs.rbp)).?, after.rbp); + try std.testing.expectEqual(regs.rbp + 8, after.rsp); + }, + // rcx elements from rdi (and rsi), down to them with the direction + // flag set; a step is an iteration: rcx one less after it. + else => { + const size: u64 = switch (insn.text[std.mem.indexOfScalar(u8, insn.text[4..], ' ').? + 3]) { + 'b' => 1, + 'w' => 2, + 'd' => 4, + else => 8, + }; + const back = if (regs.eflags & 0x400 != 0) (regs.rcx - 1) * size else 0; + try std.testing.expectEqual([3]u64{ regs.rdi - back, regs.rcx * size, 2 }, s.touches[0]); + if (std.mem.startsWith(u8, insn.text, "rep movs")) try std.testing.expectEqual([3]u64{ regs.rsi - back, regs.rcx * size, 1 }, s.touches[1]); + try std.testing.expectEqual(regs.rcx - 1, after.rcx); + // What the iteration wrote is in the stop after. + var byte: [1]u8 = undefined; + var known: [1]bool = undefined; + try next.read(regs.rdi, &byte, &known); + try std.testing.expect(known[0]); + if (std.mem.startsWith(u8, insn.text, "rep stosb")) try std.testing.expectEqual(@as(u8, @truncate(regs.rax)), byte[0]); + }, + } + } + // Each of them, the three repeated copies and fills an iteration a stop: 3 + 5 + 4. + for (seen, kinds) |n, kind| if (n == 0) { + std.debug.print("no {s} stepped\n", .{kind}); + return error.NotSeen; + }; + try std.testing.expectEqual(@as(usize, 12), seen[8]); +} + +test "a global is named where an instruction touches it, as code is: in the program, past its file's last page, in libc" { + var trace = try zt.Trace.open(gpa, io, targets.stack_recording); + defer trace.close(); + const tids = try (try trace.moment(0)).threads(gpa); + defer gpa.free(tids); + var text: [256]u8 = undefined; + var seen: [3]bool = @splat(false); + var in_libc = false; + for (0..trace.len) |i| { + const m = try trace.moment(i); + const s = (try stopAt(m, tids[0])) orelse continue; + const insn = (try zt.telescope.instruction(m, s.regs.rip, &text)) orelse continue; + for (s.touches[0..s.n]) |t| { + const name = m.trace.strings.get(t[0]); + // The first copy's first element, and the store into big. + if (std.mem.startsWith(u8, insn.text, "rep movsq") and s.regs.rcx == 3) { + try std.testing.expectEqualStrings(if (t[2] == 2) "dst+0" else "src+0", name.?); + seen[@intCast(t[2] - 1)] = true; + } + if (t[1] == 1 and t[2] == 2 and std.mem.startsWith(u8, name orelse "", "big+")) { + try std.testing.expectEqualStrings("big+4096", name.?); + seen[2] = true; + } + if (std.mem.startsWith(u8, name orelse "", "libc.so.6!")) in_libc = true; + } + // What the stack's own instructions touch there has no name. + if (s.n > 0 and (std.mem.startsWith(u8, insn.text, "push") or std.mem.startsWith(u8, insn.text, "call"))) try std.testing.expect(m.trace.strings.get(s.touches[s.n - 1][0]) == null); + } + try std.testing.expectEqual([3]bool{ true, true, true }, seen); + // exit's walk of its handlers and its streams: `__exit_funcs`, `_IO_2_1_stdout_`. + try std.testing.expect(in_libc); +} + +test "a stack word's role: the saved rbp as it is pushed, two ints in one word, the canary's copy by clang and gcc" { + var tmp = std.testing.tmpDir(.{}); + defer tmp.cleanup(); + var dir_buf: [std.Io.Dir.max_path_bytes]u8 = undefined; + const dir = dir_buf[0..try tmp.dir.realPath(io, &dir_buf)]; + const Slot = zt.telescope.Slot; + // By clang (Zig's), and by gcc where it is here. + const gcc_exe = try std.fs.path.join(gpa, &.{ dir, "stack-gcc" }); + defer gpa.free(gcc_exe); + const gcc_recording = try std.fs.path.join(gpa, &.{ dir, "stack-gcc.ztrace" }); + defer gpa.free(gcc_recording); + const gcc = built: { + const compiled = runQuiet(&.{ "gcc", "-O0", "-g", "-fstack-protector-all", targets.stack_source, "-o", gcc_exe }) catch break :built false; + if (exited(compiled) != 0) break :built false; + break :built exited(try runQuiet(&.{ targets.ztrace, "record", "--by", "insn", "--from", "main", "-o", gcc_recording, "--", gcc_exe })) == 0; + }; + var text: [256]u8 = undefined; + const paths = [_][]const u8{ targets.stack_recording, gcc_recording }; + for (paths[0 .. @as(usize, 1) + @intFromBool(gcc)], 0..) |path, by_gcc| { + var trace = try zt.Trace.open(gpa, io, path); + defer trace.close(); + const tids = try (try trace.moment(0)).threads(gpa); + defer gpa.free(tids); + var pushed = false; + var in_loop: ?usize = null; + for (0..trace.len) |i| { + const m = try trace.moment(i); + const s = (try stopAt(m, tids[0])) orelse continue; + const regs = s.regs; + const here = m.trace.strings.get(regs.rip) orelse continue; + // At fill's `push rbp`: the word it is about to write is its saved rbp already, its frame's. + if (std.mem.eql(u8, here, "fill+0")) { + var two: [2]Slot = undefined; + const words = try zt.telescope.slots(m, ®s, regs.rsp - 8, &two); + try std.testing.expectEqualStrings("rbp", words[0].role.saved); + try std.testing.expectEqual(@as(?u8, 0), words[0].frame); + try std.testing.expectEqual(zt.telescope.Role.return_address, words[1].role); + pushed = true; + } + // The loop's store into buf: the prologue has run, the canary stored. + if (in_loop == null and std.mem.startsWith(u8, here, "fill+") and std.mem.indexOf(u8, (try zt.telescope.instruction(m, regs.rip, &text)).?.text, "byte ptr [rbp + rax - 0x20]") != null) in_loop = i; + } + try std.testing.expect(pushed); + const m = try trace.moment(in_loop orelse return error.NoLoop); + const regs = (try m.regs(tids[0])).?; + // fill's frame, from rsp to its return address. + var buf: [16]Slot = undefined; + const words = try zt.telescope.slots(m, ®s, regs.rsp, buf[0 .. (regs.rbp + 16 - regs.rsp) / 8]); + const at = struct { + fn word(all: []const Slot, rsp: u64, addr: u64) Slot { + return all[(addr - rsp) / 8]; + } + }.word; + // The canary's copy, under the saved rbp: what fs:[0x28] holds. + const canary = at(words, regs.rsp, regs.rbp - 8); + try std.testing.expectEqualStrings("canary", canary.role.saved); + try std.testing.expectEqual(@as(?u8, 0), canary.frame); + try std.testing.expectEqual((try wordIn(m, regs.fs_base + 0x28)).?, (try wordIn(m, regs.rbp - 8)).?); + try std.testing.expectEqualStrings("rbp", at(words, regs.rsp, regs.rbp).role.saved); + // buf, a word at a time, and the two ints: clang puts i and n in one word, i first. + var found: [3]bool = @splat(false); + for (words) |w| switch (w.role) { + .local => |l| { + if (std.mem.eql(u8, l.name, "buf")) found[0] = true; + if (std.mem.eql(u8, l.name, "i") or std.mem.eql(u8, l.name, "n")) { + try std.testing.expectEqual(@as(u32, 4), l.size); + found[if (l.name[0] == 'i') 1 else 2] = true; + } + if (by_gcc == 0 and std.mem.eql(u8, l.name, "i")) { + try std.testing.expectEqual(@as(i32, 0), l.offset); + const n = w.also[0].?; + try std.testing.expectEqualStrings("n", n.name); + try std.testing.expectEqual(@as(i32, -4), n.offset); + try std.testing.expectEqual(@as(u32, 4), n.size); + try std.testing.expect(w.also[1] == null); + found[2] = true; + } + }, + else => {}, + }; + try std.testing.expectEqual([3]bool{ true, true, true }, found); + + // `ztrace show` says them beside its eight words from rsp: with + // clang's frame up to the return address; gcc's is 16 bytes deeper. + var index: [16]u8 = undefined; + const shown = try runQuiet(&.{ targets.ztrace, "show", path, try std.fmt.bufPrint(&index, "{d}", .{in_loop.?}) }); + defer gpa.free(shown.stdout); + defer gpa.free(shown.stderr); + const each = [2][3][]const u8{ .{ " i[0..4], n[4..8]\n", " saved rbp\n", " return address\n" }, .{ " i[4..8]\n", " n[4..8]\n", " buf+8\n" } }; + for ([_][]const u8{ " saved canary\n", " buf\n" } ++ each[by_gcc]) |want| { + if (std.mem.indexOf(u8, shown.stdout, want) == null) { + std.debug.print("{s}\nno \"{s}\" in it\n", .{ shown.stdout, want }); + return error.NotShown; + } + } + } +} + test "by instruction, a call into the vDSO is run through to its return: its retry loop is no stops, whatever a stop costs" { var counts: [2]usize = undefined; for ([_]usize{ 1024, 0 }, &counts) |stack, *count| { diff --git a/src/trace/event.zig b/src/trace/event.zig index 1447c8e..2900be7 100644 --- a/src/trace/event.zig +++ b/src/trace/event.zig @@ -41,7 +41,9 @@ pub const Kind = enum(u16) { /// The instruction at the pc is about to touch memory: a0=addr a1=len /// a2=pc. aux: 1 it reads there, 2 it writes there, 3 both. From a /// debugger, which sees the instruction before it runs; what is there is - /// recorded beside it, and what a write left there after the step. + /// recorded beside it, and what a write left there after the step. What + /// its operands name, then what the stack's own instructions touch + /// without naming it (`Debugger.Touch`). touch = 22, /// The instruction at the pc is a call, a jump or a return: a0=where it /// goes a1=1 when it will go there (0: a conditional jump that falls diff --git a/src/trace/telescope.zig b/src/trace/telescope.zig index f7e4a3d..02b637e 100644 --- a/src/trace/telescope.zig +++ b/src/trace/telescope.zig @@ -124,19 +124,25 @@ pub fn frames(m: Moment, regs: *const event.Regs, out: []Debugger.Frame) ![]Debu return Debugger.frames(Memory{ .m = m }, regs, try atEntry(m, regs), out); } +/// A local variable or parameter of a frame's function that the debug info +/// places on the stack: its name (the recording's, good while it is open), +/// its size (0: not known), and where the word is in it: 0 at its first +/// byte, 8 a word on, negative where it begins inside the word (-4: its +/// high half). +pub const Local = struct { name: []const u8, size: u32, offset: i32 }; + /// What a word of the stack is, where the recording says. pub const Role = union(enum) { none, /// A register its frame's function saved for its caller: "rbx", - /// "rbp", "r12" to "r15". + /// "rbp", "r12" to "r15"; or "canary", the copy of the stack + /// protector's value (`fs:[0x28]`) it checks before it returns. saved: []const u8, /// Where its frame's function returns to. return_address, - /// A local variable or parameter of its frame's function that the debug - /// info places on the stack: its name (the recording's, good while it - /// is open), its size (0: not known), and where the word is in it: 0 at - /// its first byte, 8 a word on, negative where it begins inside the word. - local: struct { name: []const u8, size: u32, offset: i32 }, + /// A local (the one that begins first in the word: `Slot.also` has + /// the others that share it). + local: Local, /// The process's first stack (`Debugger.Start`): argc; argv[i], the /// zero that ends them at i = argc; envp[i] likewise, at i = envc; the /// auxiliary vector's i-th word, two an entry (its type, its value), @@ -150,8 +156,11 @@ pub const Role = union(enum) { /// A word of the stack: what it is, and the frame of `frames` it lies in /// (0 the newest; null in none: under the stack pointer, or above the /// outermost frame, where the process's first stack is). A leaf's locals -/// under the stack pointer (the red zone, at -O0) are its frame's. -pub const Slot = struct { role: Role = .none, frame: ?u8 = null }; +/// under the stack pointer (the red zone, at -O0) are its frame's. Where +/// small locals share the word (two ints: `i, n`), `role` is the one that +/// begins first in it and `also` the others, in the order they begin; null +/// past the last. +pub const Slot = struct { role: Role = .none, frame: ?u8 = null, also: [7]?Local = @splat(null) }; /// What each word of a thread's stack from `low` (rounded down to a word) /// is at this moment: `out[i]` is the word at `low + 8 i`. The frames are @@ -162,7 +171,11 @@ pub const Slot = struct { role: Role = .none, frame: ?u8 = null }; /// none at all where it is in a register or a list of places, nor in a /// block whose code is in pieces, nor in a prologue or an epilogue where /// the frame base is a register the CFA is not yet or no longer kept in); -/// and the process's first stack pointer. +/// the stack protector's copy, from the store the debugger saw in the +/// function's prologue (`Debugger.canary`); and the process's first stack +/// pointer. At an instruction that saves a register (`push rbp`), the word +/// it is about to write already has the register's role, in the newest +/// frame: the call frame information says it from that instruction on. pub fn slots(m: Moment, regs: *const event.Regs, low: u64, out: []Slot) ![]Slot { @memset(out, .{}); const base = low & ~@as(u64, 7); @@ -210,17 +223,35 @@ pub fn slots(m: Moment, regs: *const event.Regs, low: u64, out: []Slot) ![]Slot var w = @max(at & ~@as(u64, 7), base); while (w < @min(at +% @max(size, 1), end)) : (w += 8) { const s = in(out, base, w) orelse continue; - // Two small locals in one word: the first named keeps it. - if (s.role == .none) s.role = .{ .local = .{ .name = name, .size = size, .offset = @intCast(@as(i64, @bitCast(w -% at))) } }; + const l: Local = .{ .name = name, .size = size, .offset = @intCast(@as(i64, @bitCast(w -% at))) }; + // The canary's copy is recorded as a local with no name (`Debugger.emitFunction`). + if (name.len == 0) s.* = .{ .role = .{ .saved = "canary" }, .frame = s.frame } else switch (s.role) { + .none => s.role = .{ .local = l }, + .local => share(s, l), + else => {}, + } // A leaf's locals under the stack pointer (the red zone) are its frame's too. if (s.frame == null) s.frame = @intCast(k); } }; } - for (row.saved, 0..) |at, i| { + // The newest frame is being made where its instruction saves a + // register (a `push rbp`): the word it is about to write is the + // register's already, by the row that begins after it. + var saved = row.saved; + if (k == 0) if (function(m, u.function)) |func| { + var buf: [256]u8 = undefined; + if (try instruction(m, f.pc, &buf)) |insn| { + const next = func.rowAt(f.pc + insn.len); + if (next.offset == f.pc + insn.len - func.start) for (&saved, next.saved) |*at, will| { + if (at.* == 0) at.* = will; + }; + } + }; + for (saved, 0..) |at, i| { if (at == 0 or at == Symbols.lost) continue; const s = in(out, base, cfa +% @as(u64, @bitCast(@as(i64, at) * 8))) orelse continue; - s.role = if (i == 6) .return_address else .{ .saved = Symbols.saved_names[i] }; + s.* = .{ .role = if (i == 6) .return_address else .{ .saved = Symbols.saved_names[i] }, .frame = s.frame orelse @intCast(k) }; } } const text = m.trace.strings.get(event.frameId(0)) orelse return out; @@ -233,11 +264,29 @@ pub fn slots(m: Moment, regs: *const event.Regs, low: u64, out: []Slot) ![]Slot const w = (a - start.sp) / 8; const envp = start.argc + 2; const auxv = envp + start.envc + 1; - s.role = if (w == 0) .argc else if (w < envp) .{ .argv = @intCast(w - 1) } else if (w < auxv) .{ .envp = @intCast(w - envp) } else if (w < auxv + 2 * (start.auxc + 1)) .{ .auxv = @intCast(w - auxv) } else continue; + s.* = .{ .role = if (w == 0) .argc else if (w < envp) .{ .argv = @intCast(w - 1) } else if (w < auxv) .{ .envp = @intCast(w - envp) } else if (w < auxv + 2 * (start.auxc + 1)) .{ .auxv = @intCast(w - auxv) } else continue, .frame = s.frame }; } return out; } +/// Puts `l` among the locals that share the word `s` names, in the order +/// they begin in it (a local begun in a word before begins it). +fn share(s: *Slot, l: Local) void { + var all: [8]Local = undefined; + all[0] = s.role.local; + var n: usize = 1; + for (s.also) |a| if (a) |x| { + all[n] = x; + n += 1; + }; + if (n == all.len) return; + var i = n; + while (i > 0 and all[i - 1].offset < l.offset) : (i -= 1) all[i] = all[i - 1]; + all[i] = l; + s.role = .{ .local = all[0] }; + for (&s.also, 1..) |*a, k| a.* = if (k <= n) all[k] else null; +} + /// A moment's memory as the frame walk reads it: all of it recorded, or /// none; and the call frame information the debugger recorded. const Memory = struct { diff --git a/targets/stack.c b/targets/stack.c new file mode 100644 index 0000000..6ba2735 --- /dev/null +++ b/targets/stack.c @@ -0,0 +1,36 @@ +// What a stack's own instructions and the string instructions touch, by +// instruction: pushes and pops, calls and returns, enter and leave, the +// flags pushed; copies and fills, repeated, both ways. Built at -O0 with +// the stack protector: `fill` keeps a canary, and its i and n share a word. +#include + +char dst[64], src[64] = "abcdefghijklmnopqrstuvwxyz"; +// Past the file's last page: zeroes in a mapping of their own. +char big[1 << 16]; + +__attribute__((noinline)) static void strings(void) { + void *d = dst, *s = src; + uint64_t n = 3; + __asm__ volatile("rep movsq" : "+D"(d), "+S"(s), "+c"(n) : : "memory"); + d = dst + 40; + n = 5; + __asm__ volatile("rep stosb" : "+D"(d), "+c"(n) : "a"(0x41) : "memory"); + // Down from the last byte, the direction flag set. + d = dst + 63; + s = src + 25; + n = 4; + __asm__ volatile("std; rep movsb; cld" : "+D"(d), "+S"(s), "+c"(n) : : "memory"); + __asm__ volatile("pushfq; popfq; push %%rbx; pop %%rbx; enter $16, $0; leave" : : : "memory"); +} + +__attribute__((noinline)) static int fill(int n) { + char buf[16]; + for (int i = 0; i < n; i++) buf[i] = 'A' + i; + return buf[n - 1]; +} + +int main(void) { + strings(); + big[4096] = 1; + return fill(16) + big[4096] + dst[0] - 'P' - 1 - 'a'; +} -- cgit v1.3