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/trace/debugger_test.zig | 240 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 240 insertions(+) (limited to 'src/trace/debugger_test.zig') 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| { -- cgit v1.3