diff options
| author | Gabriel Schneider <[email protected]> | 2026-11-10 17:16:50 -0300 |
|---|---|---|
| committer | Gabriel Schneider <[email protected]> | 2026-11-10 17:56:05 -0300 |
| commit | 74c45474f7e3e1b3539eb88484e6749082c6addf (patch) | |
| tree | 29be105369df0b9f5bb92adb50360fd99fd664b8 /design/mock/context.py | |
| parent | 47f43f0f26b08901c1ddd16aacbe50c2c843ab8a (diff) | |
| download | computeranim-74c45474f7e3e1b3539eb88484e6749082c6addf.tar.gz computeranim-74c45474f7e3e1b3539eb88484e6749082c6addf.zip | |
design: the band is two panels; what the first videos taught
An independent review of the first videos, frame by frame, is placed in
the catalogue. A lens is a drawing over the program column and its view
over the state column (the dump; the backtrace for calls), which leaves
the stack six slots as subject: the frame whole, from rsp to past rbp.
Operands are whole. A mark covers the bytes that changed. A pin travels
and keeps its side. A call's node is a projection of its return address.
Fast passes are veiled. Captions have numbers a scene can be checked by,
and a scene's first step comes by four seconds. Effects before causes
and panels from different moments are forbidden by name. Mock 12 draws
fib(4) calling fib(2) from the recording.
Co-Authored-By: Claude Fable 5.2 <[email protected]>
Diffstat (limited to 'design/mock/context.py')
| -rw-r--r-- | design/mock/context.py | 267 |
1 files changed, 236 insertions, 31 deletions
diff --git a/design/mock/context.py b/design/mock/context.py index 886372f..96d4484 100644 --- a/design/mock/context.py +++ b/design/mock/context.py @@ -1,7 +1,7 @@ #!/usr/bin/env python3 """The frame: the machine's whole context, pwndbg's way, a lens over it, and the ties between them. - python3 context.py # writes 09-context.svg, 10-context-step.svg, 11-context-lens.svg + python3 -B context.py # writes 09-context.svg, 10-context-step.svg, 11-context-lens.svg, 12-calls-subject.svg Every function here is a block of design/README.md, under the block's name. @@ -11,6 +11,9 @@ of `reverse` with their lines (objdump -dl). The dump's node bytes are what thos and the program say the heap holds. Its header rows are drawn the way glibc lays a chunk out, with the word no one reads shown as never read: a recording made under ptrace has seen neither. + +12 is the fib scene's recording (anim/zig-out/recordings/steps.ztrace), moments 752 and 753: +`fib(4)` calling `fib(2)`; its lines from the binary by addr2line. """ import re @@ -159,6 +162,8 @@ def chain(g, x, y, links, end, marks=None, room=None, lead=0): if end is None: return where x = value_arrow(g, x, y) if links else x + if marks.get("end") == "tick": # the value it holds changed within three steps + g.rect(x - 7, y + 3, 4, ROW - 6, VERM) if end[0] == "int": integer(g, x, y, end[1]) else: @@ -185,7 +190,7 @@ def pin_up(g, cx, y, names, ink, size, link=None): gen.arrow(g, (link or []) + [(cx, y + rise), (cx, y + 1)], ink, 3, r=8, hl=10, hw=10) face = BOLD by = y + 8 + size * 0.86 - if names: + if names and names[0]: # names keep their side: None holds a side that was left g.t(cx - 9, by, names[0], size, VELLUM, face, "end") if len(names) > 1: g.t(cx + 9, by, " ".join(names[1:]), size, VELLUM, face) @@ -234,16 +239,20 @@ def registers(g, y, regs, flags, marks): def stack(g, y, rows, frames): - """rows: (slot, pins, links, end). frames: (name, first row, last row), newest first: the backtrace, - drawn on the words that are each frame.""" + """rows: (slot, pins, links, end[, marks]). frames: (name, first row, last row), newest first: the + backtrace, drawn on the words that are each frame. The window is the newest frame's, from rsp. + Returns each row's y.""" y0 = panel(g, RX, y, CW, len(rows), "stack", note="%s words from rsp · the frames they belong to" % - {4: "four", 8: "eight"}[len(rows)]) + {6: "six", 8: "eight"}[len(rows)]) lead = shared([r[0] for r in rows]) - for i, (slot, pins, links, end) in enumerate(rows): + ys = [] + for i, (slot, pins, links, end, *marks) in enumerate(rows): yy = y0 + i * ROW + ys.append(yy) if pins: pin(g, RX + 10, yy, pins, STACK) - chain(g, RX + 122, yy, [(slot, STACK)] + links, end, room=RX + CW - 136, lead=lead) + chain(g, RX + 122, yy, [(slot, STACK)] + links, end, marks[0] if marks else None, room=RX + CW - 136, + lead=lead) for i, (name, first, last) in enumerate(frames): if first >= len(rows): break @@ -256,6 +265,7 @@ def stack(g, y, rows, frames): if not open_: g.rect(x - 7, yb - 2, 8, 2, ink) g.t(x - 14, (ya + yb) / 2 + 6, name, 18, BRIGHT if i == 0 else DIM, "sansm", "end") + return ys # ---- the program column ------------------------------------------------------------- @@ -341,7 +351,7 @@ def disasm(g, y, rows, func, cur_line, jump=None): x += len(part) * A if not note: continue - x = LX + DX_NOTE + x = max(LX + DX_NOTE, x + A) # operands are never cut where the row has room if note[0] == "text": g.t(x, b(yy), note[1], S, note[2] if len(note) > 2 else DIM, MONO) elif note[0] == "set": # what an instruction that ran left in a register: a register row, small @@ -403,17 +413,19 @@ def dump_row(g, y, a, runs, lead, mark=None, node=False): g.t(x + 3 * A + 8 + 30, b(y), "NULL", S, DIM, MONO) -def lens(g, y, h, nodes, pins, marks, first, nrows, link_from=None, subject=False): - """The band: the list drawn, and the heap bytes it is drawn from, in one panel across both columns. +def lens(g, y, h, hv, nodes, pins, marks, first, nrows, link_from=None, subject=False): + """The band: the list drawn, over the program column, and the heap bytes it is drawn from, over the state + column; two panels, h and hv high, the gutter between them the ties'. nodes: (address, value, next address or 0), in the list's order. pins: {address: [registers]}. marks: {"read": address of a word, "tick": [addresses of words]}. link_from: the y of the register row the step's link comes from, and the register. As the subject the drawing is a size up; the dump stays on the context's grid and shows more rows.""" - tag(g, LX, y, W - 2 * LX, h, "the list", focus=True, - note=[("value", VELLUM, False), ("next", HEAP, False), ("malloc's", DIM, False), ("never read", FAINT, False)]) + tag(g, LX, y, CW, h, "the list", focus=True) last = first + (nrows - 1) * 16 - g.t(RX + 12, y + 18, "its bytes", 20, VELLUM, "sansm") - g.t(RX + 12 + tw("its bytes", 20, "sansm") + 12, y + 18, "the heap, %x to %x" % (first, last + 15), 18, FAINT, "sans") + tag(g, RX, y, CW, hv, "its bytes", + note=[("value", VELLUM, False), ("next", HEAP, False), ("malloc's", DIM, False), ("never read", FAINT, False)]) + g.t(RX + 28 + tw("its bytes", 20, "sansm") + 12, y + 18, "the heap, %x to %x" % (first, last + 15), 18, FAINT, + "sans") # the dump y0 = y + TAG + PAD @@ -436,12 +448,13 @@ def lens(g, y, h, nodes, pins, marks, first, nrows, link_from=None, subject=Fals else: dump_row(g, yy, a, [(0, 8, "unread", None), (8, 8, "malloc", 0x21)], lead) - # the drawing + # the drawing, centred between its lanes NW, NH, GAP, VS, LS = (124, 72, 22, 45, 25) if subject else (116, 44, 28, 25, 20) VW = NW - 48 # the value's half; the pointer's is 48 x0 = LX + (CW - len(nodes) * NW - (len(nodes) - 1) * GAP) / 2 lane_top, lane_bot = y + TAG + 4, y + h - 8 - ly = y + TAG + (44 if subject else 16) # the row of addresses over the nodes + block = LS * 1.2 + 4 + NH + LS + 10 + 8 # address, node, the pins' stub and names + ly = y + TAG + (16 if not subject else round((h - TAG - block) / 2)) # the row of addresses over the nodes ny = ly + LS * 1.2 + 4 xs = {a: x0 + i * (NW + GAP) for i, (a, v, nx) in enumerate(nodes)} for a, v, nx in nodes: @@ -464,7 +477,6 @@ def lens(g, y, h, nodes, pins, marks, first, nrows, link_from=None, subject=Fals if names: via = None if link_from and link_from[1] in names: # the step's link ends in this pin - names = [link_from[1]] + [n for n in names if n != link_from[1]] via = [(RX + 2, link_from[0] + 12), (LANE_LINK, link_from[0] + 12), (LANE_LINK, lane_bot), (x + NW / 2, lane_bot)] pin_up(g, x + NW / 2, ny + NH, names, HEAP, LS, via) @@ -550,37 +562,50 @@ DIS = [ # address, line, mnemonic, operands, state, annotation ] +# The splits with a lens: the drawing's and its view's heights, then source lines, instructions and stack slots. +BESIDE = dict(band=152, view=152, src=8, dis=17, words=8, dump=5) +SUBJECT = dict(band=296, view=200, src=10, dis=9, words=6, dump=7) + + +def clock(g, moment, total): + """The scene's clock: how far through the recording, on the rule over the caption. Still through a step.""" + g.rect(LX, 899, W - 2 * LX, 2, RULE) + g.rect(LX, 898, (W - 2 * LX) * moment / total, 4, GOLD) + g.t(W - LX, 934, "moment %d of %d" % (moment, total), S, FAINT, MONO, "end") + + def frame(name, step, subject=False): """step: mid-step, the load done. subject: the lens is what the scene is about, and the band is taller.""" g = Svg() + sp = SUBJECT if subject else BESIDE rdi = (0x28b41070, 9) if step else (0x28b41090, 4) # what the last three steps changed, and (mid-step) what this one has reg_marks = {"rax": {0: "tick"}, "rcx": {0: "tick"}, "rdi": {0: "strike" if step else "tick"}} lens_marks = {"tick": [0x28b410b8]} + # names keep their side: rax stays left of its stub when rdi goes, and rdi takes the free side where it lands pins = {0x28b410b0: ["rcx"], 0x28b41090: ["rax"] + ([] if step else ["rdi"]), 0x28b41070: ["rdi"] if step else []} if step: lens_marks["read"] = 0x28b41098 + k = [r[0] for r in DIS].index(0x1001926) + half = sp["dis"] // 2 # the pc's row, and as many above as below + dis = list(DIS[k - half:k + half + 1]) + dump_first = 0x28b41050 if subject else 0x28b41070 - band, src_rows, src_first, dis, words, dump_first, dump_rows = ( - (248, 10, 18, DIS[3:14], 4, 0x28b41030, 9) if subject else (152, 8, 19, DIS, 8, 0x28b41070, 5)) - y = TOP + band + 12 - ys = source(g, y, src_rows, SRC, src_first, 21, [20, 22], "list.c", len(SRC) - 1) - yd = y + ph(src_rows) + 12 - dis = list(dis) + y = TOP + sp["band"] + 12 + ys = source(g, y, sp["src"], SRC, 18 if subject else 19, 21, [20, 22], "list.c", len(SRC) - 1) + yd = y + ph(sp["src"]) + 12 i = [r[0] for r in dis].index(0x1001926) dis[i] = dis[i][:5] + (("chain", [(0x28b41098, H_), (0x28b41070, H_)], ("int", 9), {0: "read"} if step else None),) at = disasm(g, yd, dis, "reverse", 21, jump=(0x1001931, 0x1001920)) thread(g, ys, [(at[0x1001926], at[0x1001926] + ROW)]) - where = registers(g, y, regs_at(rdi), FLAGS, reg_marks) - stack(g, y + ph(17) + 12, STACK_ROWS[:words], FRAMES) - lens(g, TOP, band, NODES, pins, lens_marks, dump_first, dump_rows, + yr = TOP + sp["view"] + 12 + where = registers(g, yr, regs_at(rdi), FLAGS, reg_marks) + stack(g, yr + ph(17) + 12, STACK_ROWS[:sp["words"]], FRAMES) + lens(g, TOP, sp["band"], sp["view"], NODES, pins, lens_marks, dump_first, sp["dump"], link_from=(where["rdi"], "rdi") if step else None, subject=subject) - # the scene's clock: how far through the recording, on the rule over the caption - g.rect(LX, 899, W - 2 * LX, 2, RULE) - g.rect(LX, 898, (W - 2 * LX) * (71.5 if step else 71) / 95, 4, GOLD) - g.t(W - LX, 934, "moment %d of 95" % (72 if step else 71), S, FAINT, MONO, "end") + clock(g, 71, 95) # mid-step the clock has not moved: it moves when the mark has landed if step: gen.caption(g, [[("The load read the word at ",), ("28b41098", "mono"), (": the address of the next node.",)], [("rdi", "mono"), (" holds it now, and points at the node whose value is 9.",)]]) @@ -590,10 +615,190 @@ def frame(name, step, subject=False): g.save(name) +# ---- the calls lens: a tree of every call, and the backtrace it is a view of ------------- +def fib_calls(n): + """Every call of fib(n), in the order they are made: (argument, parent index, level, result).""" + out = [] + + def call(k, parent, level): + i = len(out) + out.append([k, parent, level, None]) + out[i][3] = k if k < 2 else call(k - 1, i, level + 1) + call(k - 2, i, level + 1) + return out[i][3] + call(n, None, 0) + return out + + +def tree_x(calls): + """In leaf slots: leaves in call order, a parent over the middle of its children.""" + kids = {i: [j for j, c in enumerate(calls) if c[1] == i] for i in range(len(calls))} + xs, leaf = {}, [0] + + def place(i): + if not kids[i]: + xs[i] = leaf[0] + leaf[0] += 1 + else: + for j in kids[i]: + place(j) + xs[i] = (xs[kids[i][0]] + xs[kids[i][-1]]) / 2 + place(0) + return xs, leaf[0] + + +def calls(g, y, h, tree, made, caller, project_from=None): + """The drawing: tree is fib_calls(); made: how many calls have been made (the last is running). + project_from: the y of the stack row whose return address the newest node is drawn from, mid-step. + Returns the newest node's centre.""" + tag(g, LX, y, CW, h, "calls of fib", focus=True, note="each call a node · its result on its shoulder") + D, LEVEL, BADGE = 48, 46, 30 + xs, leaves = tree_x(tree) + left, right = LX + 44, LX + CW - 12 - 48 # the last leaf's badge stays inside + pitch = (right - left) / (leaves - 1) + top = y + TAG + 48 + at = lambda i: (left + xs[i] * pitch, top + tree[i][2] * LEVEL) + newest = made - 1 + waiting = set() + p = tree[newest][1] + while p is not None: + waiting.add(p) + p = tree[p][1] + # the caller, at the drawing's left on the root's level, clear of the tree: waiting, with an edge into the root + rx, ry = at(0) + bx = LX + 20 + g.path(f"M{bx + 64},{ry} H{rx - D / 2}", VELLUM, 3) + g.rect(bx, ry - 14, 64, 28, PAGE) + g.frame(bx, ry - 14, 64, 28, VELLUM, 3) + g.t(bx + 32, b(ry - 12), caller, S, VELLUM, MONO, "middle") + # edges under the nodes: the waiting path in primary + for i in range(1, made): + (px, py), (cx, cy) = at(tree[i][1]), at(i) + live = i in waiting or i == newest + g.line(px, py, cx, cy, VELLUM if live else RULE, 3) + for i in range(made): + cx, cy = at(i) + k, _, _, result = tree[i] + if i == newest: + g.circle(cx, cy, D / 2, GOLD) + g.t(cx, cy + 0.36 * 25, str(k), 25, INK, BOLD, "middle") + elif i in waiting: + g.circle(cx, cy, D / 2 - 1.5, PAGE, VELLUM, 3) + g.t(cx, cy + 0.36 * 25, str(k), 25, VELLUM, MONO, "middle") + else: + g.circle(cx, cy, D / 2, RAISED) + g.t(cx, cy + 0.36 * 25, str(k), 25, VELLUM, MONO, "middle") + g.rect(cx + 12, cy - 42, BADGE, BADGE, PAGE) + g.frame(cx + 12, cy - 42, BADGE, BADGE, FAINT, 2) + g.t(cx + 12 + BADGE / 2, cy - 42 + BADGE / 2 + 0.36 * 25, str(result), 25, VELLUM, MONO, "middle") + cx, cy = at(newest) + if project_from is not None: # the frame's return address is the node: a projection, from below + lane_bot = y + h - 8 + bracket_y = cy + D / 2 + 5 + gen.arrow(g, [(RX + 3, project_from + 12), (LANE_LINK, project_from + 12), (LANE_LINK, lane_bot), + (cx, lane_bot), (cx, bracket_y + 3)], VERM, 2, r=6, head=False) + g.rect(cx - D / 2, bracket_y, D, 3, VERM) + g.rect(cx - D / 2, bracket_y - 5, 3, 8, VERM) + g.rect(cx + D / 2 - 3, bracket_y - 5, 3, 8, VERM) + bracket(g, RX + 4, project_from + 2, project_from + ROW - 2, VERM, 3, 5) + return cx, cy + + +def backtrace(g, y, h, frames): + """The view of the calls: the call chain as rows, newest on top. frames: (name, args, file, line).""" + y0 = panel(g, RX, y, CW, (h - TAG - 2 * PAD) // ROW, "backtrace", note="the calls waiting · newest on top") + for i, (name, args, file, line) in enumerate(frames): + yy = y0 + i * ROW + if i == 0: + g.rect(RX + 2, yy, CW - 4, ROW, EMBER[GOLD]) + g.rect(RX + 2, yy, 40, ROW, GOLD) + g.t(RX + 12, b(yy), "#0", S, INK, BOLD) + else: + g.t(RX + 12, b(yy), "#%d" % i, S, FAINT, MONO) + g.t(RX + 60, b(yy), name, S, VELLUM, MONO) + if args: + g.t(RX + 384, b(yy), args, S, VELLUM, MONO) + g.t(RX + CW - 12, b(yy), "%s:%d" % (file, line), S, DIM, MONO, "end") + + +# ---- the data: moments 752 and 753 of the fib scene (anim/zig-out/recordings/steps.ztrace) ----- +FIB_SRC = open(__file__.replace("context.py", "fib.c")).read().split("\n") +FIB_RBP = [(0x7fffffffeda0, S_), (0x7fffffffedd0, S_), (0x7fffffffedf0, S_), (0x7fffffffee20, S_)] +FIB_ARGV = [(0x7fffffffee38, S_), (0x7fffffffefe8, S_)] +FIB_REGS = [ + ("rax", [], ("int", 2)), + ("rbx", FIB_ARGV, ("str", "./steps")), + ("rcx", [], ("int", 1)), + ("rdx", [(0x7fffffffee48, S_)], ("int", 0)), + ("rsi", FIB_ARGV, ("str", "./steps")), + ("rdi", [], ("int", 2)), + ("rbp", FIB_RBP, ("int", 0)), + ("rsp", [(0x7fffffffed78, S_), (0x10133ab, C_, "fib+59")], None), + ("r8", [], ("int", 0)), + ("r9", [], ("int", 0x800000)), + ("r10", [], ("int", 0x800000)), + ("r11", [], ("int", 0x346)), + ("r12", [(0x7fffffffee48, S_)], ("int", 0)), + ("r13", [], ("int", 0)), + ("r14", [(0x10133d0, C_, "main+0")], None), + ("r15", [], ("int", 1)), + ("rip", [(0x10133a6, C_, "fib+54")], None), +] +FIB_FLAGS = [("CF", 0, 0), ("PF", 0, 0), ("AF", 0, 0), ("ZF", 0, 0), ("SF", 0, 0), ("IF", 1, 0), ("DF", 0, 0), ("OF", 0, 0)] +FIB_STACK = [ # the window from rsp: the new frame's return address, then fib(4)'s words down to its saved rbp + (0x7fffffffed78, ["rsp"], [(0x10133ab, C_, "fib+59")], None, {1: "strike"}), + (0x7fffffffed80, [], [], ("int", 0)), + (0x7fffffffed88, [], [], ("int", 2), {"end": "tick"}), + (0x7fffffffed90, [], [], ("int", 4)), + (0x7fffffffed98, [], [(0x7ffff7ffd000, C_, "vdso+0")], None), + (0x7fffffffeda0, ["rbp"], FIB_RBP[1:], ("int", 0)), +] +FIB_FRAMES = [("fib(2)", 0, 0), ("fib(4)", 1, 9)] +FIB_DIS = [ # fib(4) at its second call: line 6, one run + (0x1013395, 6, "call", [(0x1013370, C_)], None, ("text", "fib", VELLUM)), + (0x101339a, 6, "mov", [("qword ptr [rbp - 0x18], rax", P)], "trail2", + ("chain", [(0x7fffffffed88, S_)], ("int", 2), None)), + (0x101339e, 6, "mov", [("rdi, qword ptr [rbp - 0x10]", P)], "trail1", + ("chain", [(0x7fffffffed90, S_)], ("int", 4), None)), + (0x10133a2, 6, "sub", [("rdi, 2", P)], "trail0", ("set", "rdi", [], ("int", 2))), + (0x10133a6, 6, "call", [(0x1013370, C_)], "cur", ("text", "fib", BRIGHT)), + (0x10133ab, 6, "mov", [("rcx, rax", P)], None, ("set", "rdi", [], ("int", 2))), # the call's argument hangs here + (0x10133ae, 6, "mov", [("rax, qword ptr [rbp - 0x18]", P)], None, None), + (0x10133b2, 6, "add", [("rax, rcx", P)], None, None), + (0x10133b5, 6, "mov", [("qword ptr [rbp - 8], rax", P)], None, None), +] +FIB_BACKTRACE = [("fib+0", "n=2", "fib.c", 3), ("fib+59", "n=4", "fib.c", 6), ("fib+42", "n=5", "fib.c", 6), + ("main+25", "", "fib.c", 10), ("libc_start_main_stage2+45", "", "__libc_start_main.c", 95), + ("_start_c+0", "", "crt1.c", 15)] + + +def frame_calls(name): + """fib(4) has called fib(2), mid-step: rsp and the return address struck, and the new node drawn from it.""" + g = Svg() + sp = SUBJECT + y = TOP + sp["band"] + 12 + ys = source(g, y, sp["src"], FIB_SRC, 3, 6, [7, 5, 4], "fib.c", len(FIB_SRC) - 1) + at = disasm(g, y + ph(sp["src"]) + 12, FIB_DIS, "fib", 6) + thread(g, ys, [(at[0x1013395], at[0x10133b5] + ROW)]) + + yr = TOP + sp["view"] + 12 + registers(g, yr, FIB_REGS, FIB_FLAGS, {"rsp": {0: "strike"}, "rdi": {0: "tick"}}) + rows = stack(g, yr + ph(17) + 12, FIB_STACK, FIB_FRAMES) + backtrace(g, TOP, sp["view"], FIB_BACKTRACE) + calls(g, TOP, sp["band"], fib_calls(5), 8, "main", project_from=rows[0]) + + clock(g, 752, 1999) + gen.caption(g, [[("The call pushed its return address, ",), ("fib+59", "mono"), (", on the stack.",)], + [("That word is the new frame, and the frame is the node ",), ("fib(2)", "mono"), (".",)]]) + g.save(name) + + if __name__ == "__main__": assert ph(8) + 12 + ph(17) == ph(17) + 12 + ph(8) == BOTTOM - TOP - 152 - 12 # lens beside - assert ph(10) + 12 + ph(11) == ph(17) + 12 + ph(4) == BOTTOM - TOP - 248 - 12 # lens as subject + assert ph(10) + 12 + ph(9) == BOTTOM - TOP - 296 - 12 # lens as subject: the drawing + assert ph(17) + 12 + ph(6) == BOTTOM - TOP - 200 - 12 and ph(7) == 200 # its view, and the stack under it + assert ph(5) == 152 assert shared([0x1001910, 0x1001937]) == 5 and shared([0x28b41070]) == 6 frame("09-context", False) frame("10-context-step", True) frame("11-context-lens", True, subject=True) + frame_calls("12-calls-subject") |
