From eab50d1c040774351f332a1568fa332c377860bd Mon Sep 17 00:00:00 2001 From: Gabriel Schneider Date: Thu, 12 Nov 2026 11:32:59 -0300 Subject: canim: the introduction as one video, with a chapter on registers From the user. One scene, intro, in four chapters: memory, registers, the stack, code and meaning (210 s). The registers chapter builds the panel before the stack needs it: sixteen rows with their chains, rip pinned on the code band and rsp and rbp on the stack's, rdi opened into its parts as mov edi changes one byte, a call to malloc stepped over with its result in rax, the registers a scene never changes folded. The chapters join at seams that say their title and keep it as the clock's label; one recording (from push(9), no agent, the heap from --heap) and one clock. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_016Y9MNCKgYCGiANnEwcEQxa --- anim/src/Recording.zig | 57 +++++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 56 insertions(+), 1 deletion(-) (limited to 'anim/src/Recording.zig') diff --git a/anim/src/Recording.zig b/anim/src/Recording.zig index bcc8903..c6a305f 100644 --- a/anim/src/Recording.zig +++ b/anim/src/Recording.zig @@ -58,7 +58,11 @@ pub const Mark = struct { moment: u64, text: []const u8 }; /// An allocation ever made: its place in this list is its identity, the /// same in every view. -pub const Born = struct { ptr: u64, size: u64, moment: u64, event: u64 }; +/// An allocation: the agent's (`event` its event), or, in a recording with +/// none and glibc's heap at each stop (`--heap`), a chunk in use there, its +/// body (`ptr` 16 into the chunk, `size` the chunk's less its 16), from the +/// first stop that has it to the first that does not (`gone`). +pub const Born = struct { ptr: u64, size: u64, moment: u64, event: u64, gone: u64 = std.math.maxInt(u64) }; /// A run of bytes a moment changed, or read. pub const Span = struct { addr: u64, len: u64 }; @@ -125,6 +129,8 @@ calls: []Call = &.{}, syscalls: []Syscall = &.{}, marks: []Mark = &.{}, allocations: []Born = &.{}, +/// Whether `allocations` were read off the heap (`allocationsFromHeap`). +heap_read: bool = false, /// The registers of `tid` after each moment (the last known, before any). registers: []zt.Regs = &.{}, write_spans: Spans = .{}, @@ -607,6 +613,55 @@ fn index(r: *Recording) !void { r.globals = globals.items; } +/// A scene that draws the program's allocations from glibc's heap +/// (`--heap`) where no agent told them (the introduction's list nodes): +/// `allocations` read off the heap, once. Not by default: a scene of the +/// heap's own (`heap`) times its steps by what it draws, not by these. +pub fn allocationsFromHeap(r: *Recording) void { + if (r.allocations.len > 0 or r.heap_read) return; + r.heap_read = true; + r.allocations = heapBorn(r, r.arena.allocator()) catch &.{}; +} + +/// The program's allocations read off glibc's heap at each stop (`--heap`), +/// where no agent told them: each chunk in use but the tcache's own, from +/// the first stop that has it; `gone` at the first that does not. In the +/// order they were first seen, by address within one stop. +fn heapBorn(r: *const Recording, arena: std.mem.Allocator) ![]Born { + var out: std.ArrayList(Born) = .empty; + var scratch: std.heap.ArenaAllocator = .init(gpa); + defer scratch.deinit(); + var i: u64 = 0; + while (i <= r.end()) : (i += 1) { + _ = scratch.reset(.retain_capacity); + const h = (zt.heap.glibc(scratch.allocator(), r.moment(i), r.tid) catch null) orelse continue; + const own: u64 = if (h.tcache) |tc| tc.addr else 0; + for (h.chunks) |c| { + if (c.free or c.in != .none or c.addr + 16 == own or c.size() < 32) continue; + const ptr = c.addr + 16; + const known = for (out.items) |b| { + if (b.ptr == ptr and b.gone > i) break true; + } else false; + if (!known) try out.append(arena, .{ .ptr = ptr, .size = c.size() - 16, .moment = i, .event = std.math.maxInt(u64) }); + } + // What was in use and is not now has gone. + for (out.items) |*b| if (b.gone > i and b.moment < i) { + const still = for (h.chunks) |c| { + if (c.addr + 16 == b.ptr and !c.free and c.in == .none) break true; + } else false; + if (!still) b.gone = i; + }; + } + return out.items; +} + +/// The allocation live at `m` that holds `addr`: the agent's (`Moment.allocation`) or one read off the heap. +pub fn allocated(r: *const Recording, m: Moment, addr: u64) bool { + if ((m.allocation(addr) catch null) != null) return true; + for (r.allocations) |b| if (b.event == std.math.maxInt(u64) and addr >= b.ptr and addr - b.ptr < @max(b.size, 1) and b.moment <= m.index and m.index < b.gone) return true; + return false; +} + /// A string id for `text`, for names the recording did not define itself. fn intern(r: *Recording, text: []const u8) !u64 { const id = std.hash.Wyhash.hash(0, text) | 1 << 63; -- cgit v1.3