#!/usr/bin/env python3 """The column: a second way to compose a scene (an experiment). The panes a beat is about in one column, the narration as bubbles in the other, each level with what it speaks of; the panes the beat leaves out folded to their tags on a shelf. python3 -B column.py # 49 to 53: five beats of `open` followed down; 54: the present frame and the column # at one instant; 55: a wide subject, sort's rings, its bubble set on the drawing The data: anim/tools/hello.c built as the syscalls scene builds it (clang -O0 -g -no-pie -fstack-protector-strong -Wl,-z,now), `env -i setarch -R ztrace record --by insn --from main --steps 132 --stack 1024 --around 256 --disasm 69 -s 64`: moments 37 (main at its call), 38 (in the stub), 61 to 65 (libc's open, its last three and the syscall), 73 to 76 (leave, ret, back in main, f stored); the stack's words past the eight `ztrace show` prints from `ztrace dbg` at the same places (`x`), the canary the recording's; libc's rows from `objdump -d` of the machine's libc.so.6 (open at 110ec0). 55 is mock 25's stop (frame.py, graph.py). """ import subprocess from PIL import Image, ImageDraw, ImageFont import gen import context as C import map as M import frame as F import graph as G import touch as T from context import (PAGE, RAISED, RULE, VELLUM, BRIGHT, DIM, FAINT, GOLD, HEAP, STACK, CODE, DATA, A, S, ROW, TAG, MONO, BOLD, b, ph, mix, Svg, tw) HERE = gen.HERE READ, VERM = gen.READ, gen.VERM R_ = VELLUM SRC = open(HERE + "/../../anim/tools/hello.c").read().split("\n") L = 0x7ffff7c00000 CANARY = 0x4434195f31f30300 # ---- the program -------------------------------------------------------------------------------------------- MAIN = [(0x401180, 4, "push", "rbp"), (0x401181, 4, "mov", "rbp, rsp"), (0x401184, 4, "sub", "rsp, 0x50"), (0x401188, 4, "mov", "rax, qword ptr fs:[0x28]"), (0x401191, 4, "mov", "qword ptr [rbp - 8], rax"), (0x401195, 4, "mov", "dword ptr [rbp - 0x34], 0"), (0x40119c, 6, "lea", "rdi, [rip + 0xe61]"), (0x4011a3, 6, "xor", "esi, esi"), (0x4011a5, 6, "mov", "al, sil"), (0x4011a8, 6, "call", (0x401070, "open@plt")), (0x4011ad, 6, "mov", "dword ptr [rbp - 0x38], eax"), (0x4011b0, 7, "mov", "edi, dword ptr [rbp - 0x38]"), (0x4011b3, 7, "lea", "rsi, [rbp - 0x30]"), (0x4011b7, 7, "mov", "qword ptr [rbp - 0x48], rsi"), (0x4011bb, 7, "mov", "edx, 0x20"), (0x4011c0, 7, "call", (0x401060, "read@plt")), (0x4011c5, 7, "mov", "dword ptr [rbp - 0x3c], eax"), (0x4011c8, 8, "mov", "edi, dword ptr [rbp - 0x38]"), (0x4011cb, 8, "call", (0x401050, "close@plt"))] STUB = [(0x401070, None, "jmp", "qword ptr [rip + 0x2f62]"), (0x401076, None, "push", "4"), (0x40107b, None, "jmp", 0x401020)] OPEN = [(L + o, None, mn, ops) for o, mn, ops in [ (0x110ec0, "endbr64", ""), (0x110ec4, "push", "rbp"), (0x110ec5, "mov", "rbp, rsp"), (0x110ec8, "sub", "rsp, 0x70"), (0x110ecc, "mov", "qword ptr [rbp - 0x30], rdx"), (0x110ed0, "mov", "rax, qword ptr fs:[0x28]"), (0x110ed9, "mov", "qword ptr [rbp - 0x48], rax"), (0x110edd, "xor", "eax, eax"), (0x110edf, "test", "sil, 0x40"), (0x110ee3, "jne", L + 0x110f90), (0x110ee9, "mov", "eax, esi"), (0x110eeb, "xor", "r10d, r10d"), (0x110eee, "not", "eax"), (0x110ef0, "test", "eax, 0x410000"), (0x110ef5, "je", L + 0x110f90), (0x110efb, "mov", "r11, qword ptr fs:[0x10]"), (0x110f04, "movsxd", "rdx, esi"), (0x110f07, "mov", "eax, dword ptr [r11 + 0x308]"), (0x110f0e, "cmp", "byte ptr [rip + 0x11018b], 0"), (0x110f15, "jne", L + 0x110fb0), (0x110fb0, "mov", "rsi, rdi"), (0x110fb3, "xor", "r9d, r9d"), (0x110fb6, "xor", "r8d, r8d"), (0x110fb9, "mov", "rdi, 0xffffffffffffff9c"), (0x110fc0, "mov", "eax, 0x101"), (0x110fc5, "syscall", ""), (0x110fc7, "mov", "rdx, rax"), (0x110fca, "cmp", "rax, -0x1000"), (0x110fd0, "ja", L + 0x110fe8), (0x110fd2, "mov", "eax, edx"), (0x110fd4, "mov", "rdx, qword ptr [rbp - 0x48]"), (0x110fd8, "sub", "rdx, qword ptr fs:[0x28]"), (0x110fe1, "jne", L + 0x110fec), (0x110fe3, "leave", ""), (0x110fe4, "ret", "")]] def rows_of(table, lo, hi, states, notes): out = [] for a, ln, mn, ops in table: if lo <= a <= hi: parts = [(ops, CODE)] if isinstance(ops, tuple) else ([(ops, CODE)] if isinstance(ops, int) else ([(ops, VELLUM)] if ops else [])) out.append((a, ln, mn, parts, states.get(a), notes.get(a))) return out # ---- the registers and the stack --------------------------------------------------------------------------- QUIET = ("rbx", "r12", "r13", "r14", "r15") # the scene moves the other twelve FIXED = {"rbx": 0, "r12": 0x7fffffffee38, "r13": 1, "r14": 0x7ffff7ffd000, "r15": 0x403da8} REGS = { # rax rcx rdx rsi rdi rbp rsp r8 r9 r10 r11, rip 37: (CANARY, 0x403da8, 0x7fffffffee48, 0, 0x402004, 0x7fffffffed00, 0x7fffffffecb0, 0x7ffff7e19680, 0x7ffff7e1afa0, 0x7fffffffea60, 0x7ffff7ffca90), 38: (CANARY, 0x403da8, 0x7fffffffee48, 0, 0x402004, 0x7fffffffed00, 0x7fffffffeca8, 0x7ffff7e19680, 0x7ffff7e1afa0, 0x7fffffffea60, 0x7ffff7ffca90), 64: (0x101, 0x403da8, 0, 0x402004, (1 << 64) - 100, 0x7fffffffeca0, 0x7fffffffec30, 0, 0, 0, 0x7ffff7f70740), 65: (3, L + 0x110fc7, 0, 0x402004, (1 << 64) - 100, 0x7fffffffeca0, 0x7fffffffec30, 0, 0, 0, 0x346), 75: (3, L + 0x110fc7, 0, 0x402004, (1 << 64) - 100, 0x7fffffffed00, 0x7fffffffecb0, 0, 0, 0, 0x346), } NAMES = ("rax", "rcx", "rdx", "rsi", "rdi", "rbp", "rsp", "r8", "r9", "r10", "r11") MEM = {0x7fffffffecf8: CANARY, 0x7fffffffed00: 0x7fffffffedb0, 0x7fffffffed08: L + 0x27781, 0x7fffffffed10: 0x7ffff7fe0c60, 0x7fffffffed18: 0x7fffffffee38, 0x7fffffffed20: 0x1f7fbe000, 0x7fffffffed28: 0x401180, 0x7fffffffed30: 0x7fffffffed70, 0x7fffffffed38: 0x7ffff7fdf260, 0x7fffffffed40: 0, 0x7fffffffed48: 0xb95794861e82af9d, 0x7fffffffed50: 0x7fffffffee38, 0x7fffffffed58: 1, 0x7fffffffedb0: 0x7fffffffee10, 0x7fffffffee10: 0, 0x7fffffffee38: 0x7fffffffefe8, 0x7fffffffee48: 0, 0x7ffff7e19680: 0x7ffff7e1afa0, 0x7ffff7e1afa0: 0, 0x7fffffffea60: 0, 0x7ffff7ffd000: 0x7ffff7ffe3c0, 0x7ffff7ffe3c0: 0, 0x403da8: 0x401140, 0x7ffff7f70740: 0x7ffff7f70740} for a in range(0x7fffffffec30, 0x7fffffffecf8, 8): MEM.setdefault(a, 0) OPEN_FRAME = {0x7fffffffec30: 6, 0x7fffffffec58: CANARY, 0x7fffffffec68: 0x7ffff7fbc000, 0x7fffffffec70: 0x7fffffffee48, 0x7fffffffeca0: 0x7fffffffed00, 0x7fffffffeca8: 0x4011ad} STRINGS = {0x7fffffffefe8: "./hello", 0x402004: "note"} def region(v): for lo, hi, ink in [(0x7ffffffde000, 0x7ffffffff000, STACK), (0x405000, 0x426000, HEAP), (0x401000, 0x402000, CODE), (0x402000, 0x403000, R_), (0x403000, 0x405000, DATA), (L + 0x24000, L + 0x19f000, CODE), (L + 0x215000, L + 0x223000, DATA), (0x7ffff7f70000, 0x7ffff7f75000, DATA), (0x7ffff7fbc000, 0x7ffff7fbe000, CODE), (0x7ffff7fbf000, 0x7ffff7fec000, CODE), (0x7ffff7ffb000, 0x7ffff7fff000, DATA)]: if lo <= v < hi: return ink return None SYMS = {0x401020: "_init+32", 0x401060: "read@plt", 0x401070: "open@plt", 0x401140: "__do_global_dtors_aux", 0x401180: "main"} def name(v): if L <= v < L + 0x223000: return "libc+%x" % (v - L) if not (L + 0x110ec0 <= v < L + 0x110ff0) else "open+%d" % (v - L - 0x110ec0) if 0x7ffff7fbc000 <= v < 0x7ffff7fbe000: return "vdso+%x" % (v - 0x7ffff7fbc000) if 0x7ffff7fbe000 <= v < 0x7ffff7fff000: return "ld.so+%x" % (v - 0x7ffff7fbe000) f = max(a for a in SYMS if a <= v) return SYMS[f] if f == v else "%s+%d" % (SYMS[f].split("+")[0], v - f) def follow(v, m): links = [] while True: ink = region(v) if ink is None: return links, ("int", v) if ink == CODE: return links + [(v, CODE, name(v))], None links.append((v, ink)) if v in STRINGS: return links, ("str", STRINGS[v]) if v not in m or len(links) > 4: return links, None w = m[v] if w == v: # a word that points at itself: the chain goes on, and says so return links + [(w, ink)], None if region(w): v = w continue return links, ("int", w) def mem(k): m = dict(MEM) if k >= 38: m[0x7fffffffeca8] = 0x4011ad if 40 <= k < 75: m.update(OPEN_FRAME) if k >= 76: m[0x7fffffffecc8] = 3 return m def regs(k, pc): m = mem(k) vals = dict(FIXED, **dict(zip(NAMES, REGS[k]))) out = [] for r in ("rax", "rbx", "rcx", "rdx", "rsi", "rdi", "rbp", "rsp", "r8", "r9", "r10", "r11", "r12", "r13", "r14", "r15"): v = vals[r] links, end = follow(v, m) if region(v) else ([], ("int", v)) out.append((r, links, end)) out.append(("rip", [(pc, CODE, name(pc))], None)) return out FLAGS = [("CF", 0, 0), ("PF", 1, 0), ("AF", 0, 0), ("ZF", 1, 0), ("SF", 0, 0), ("IF", 1, 0), ("DF", 0, 0), ("OF", 0, 0)] ROLES = {0x7fffffffecc0: "n, high half", 0x7fffffffecc8: "f, low half", 0x7fffffffecd0: "buf", 0x7fffffffecd8: "buf+8", 0x7fffffffece0: "buf+10", 0x7fffffffece8: "buf+18", 0x7fffffffed00: "saved rbp", 0x7fffffffeca0: "saved rbp"} def stack_rows(k, top, n, rsp, rbp, marks=None): m = mem(k) rows = [] for i in range(n): a = top + 8 * i pins = [r for r, v in (("rbp", rbp), ("rsp", rsp)) if v == a] w = m.get(a, 0) links, end = follow(w, m) if region(w) else ([], ("int", w)) mk = (marks or {}).get(a) if mk and 1 in mk and not links: # a number written: the mark is on the value the chain ends in mk = dict(mk) mk["end"] = mk.pop(1) rows.append((a, pins, links, end, mk) if mk else (a, pins, links, end)) return rows def frames(top, n, newest): ends = newest + [("main", 0x7fffffffed08), ("libc+27781", 0x7fffffffed98)] out, first = [], top for nm, last in ends: out.append((nm, (first - top) // 8, (last - top) // 8)) first = last + 8 return out # ---- the column's blocks: the shelf, a bubble ------------------------------------------------------------ PX, TX, TW = 64, 976, 880 # the panes' column, the text column SHELF_Y = 48 def shelf(g, tags): """The panes a beat leaves out, folded to their tags, side by side in the frame's order. tags: (title, note runs) where a run is (text, ink) or (text, "struck").""" x = PX for title, runs in tags: w = 28 + tw(title, 20, "sansm") + 14 + sum(len(s_) * A if ink in ("struck", "mono") else tw(s_, 18, "sans") for s_, ink in runs) + 10 g.rect(x, SHELF_Y, w, TAG, RAISED) g.frame(x, SHELF_Y, w, TAG, RULE, 2) g.frame(x + 8, SHELF_Y + 6, 12, 12, FAINT, 2) g.t(x + 28, SHELF_Y + 18, title, 20, DIM, "sansm") xx = x + 28 + tw(title, 20, "sansm") + 14 for s_, ink in runs: if ink == "struck": g.rect(xx - 4, SHELF_Y + 2, len(s_) * A + 8, 20, C.SOFT) g.t(xx, SHELF_Y + 18, s_, 20, BRIGHT, BOLD) xx += len(s_) * A elif ink == "mono": g.t(xx, SHELF_Y + 18, s_, 20, VELLUM, MONO) xx += len(s_) * A else: g.t(xx, SHELF_Y + 18, s_, 18, ink, "sans") xx += tw(s_, 18, "sans") x += w + 12 return SHELF_Y + TAG + 12 BW = 812 # a bubble's measure: 40 characters of Go 40, and its paper's padding def wrap(parts, width=BW - 32): """Lines of words (text, font, a space after) laid in Go 40 / mono 35 within width.""" stream = [(ch, font) for text, font in parts for ch in text] words, cur = [], None for i, (ch, font) in enumerate(stream): if ch == " ": if cur: words.append((cur[0], cur[1], True)) cur = None continue if cur and cur[1] != font: words.append((cur[0], cur[1], False)) cur = None cur = (cur[0] + ch, font) if cur else (ch, font) if cur: words.append((cur[0], cur[1], False)) lines, line, x = [], [], 0 for word, font, sp in words: w = len(word) * 21 if font == "mono" else tw(word, 40, "sans") if x + w > width and line: lines.append(line) line, x = [], 0 line.append((word, font, sp)) x += w + (tw(" ", 40, "sans") if sp else 0) lines.append(line) return lines def bubble(g, top, lines, newest, stem=None): """A bubble: lines of runs, Go 40 on 54, code mono 35; the newest on raised paper, primary; an older one bare and dim. stem: the y of the row it points at, on the panes' right rule (drawn in the rest before a step).""" h = len(lines) * 54 + 26 if newest: g.rect(TX + 8, top, BW, h, RAISED) ink = VELLUM if newest else DIM y = top + 13 + 40 for runs in lines: x = TX + 8 + 16 parts = [] for word, font, sp in runs: parts.append((word + (" " if sp else ""), ink, "mono" if font == "mono" else "sans")) g.rich(x, y, parts, 40) y += 54 if stem is not None: # from the bubble's first line, across the gutter, onto the pane's rule yb = top + 13 + 27 pts = [(TX + 8, yb), (TX - 14, yb), (TX - 14, stem + 12), (PX + 880 + 1, stem + 12)] if abs(yb - stem - 12) < 2: # level: straight across pts = [(TX + 8, stem + 12), (PX + 880 + 1, stem + 12)] gen.arrow(g, pts, DIM, 2, r=6, hl=7, hw=8) return h def place_bubbles(levels, heights, floor=888, ceil=48): """Each bubble's top: the newest (last) with its first line's centre on its level; an older one at its own unless that overlaps one placed after it, then just above that one, or just below it where there is no room.""" n = len(levels) tops = [None] * n tops[-1] = min(max(levels[-1] - 40, ceil), floor - heights[-1]) for j in range(n - 2, -1, -1): t = min(max(levels[j] - 40, ceil), floor - heights[j]) for k in range(j + 1, n): if t < tops[k] + heights[k] + 12 and t + heights[j] + 12 > tops[k]: t = tops[k] - heights[j] - 12 if t < ceil: t = tops[k] + heights[k] + 12 tops[j] = t return tops def column_clock(g, moment, total): g.rect(TX, 899, 880, 2, RULE) g.rect(TX, 898, 880 * moment / total, 4, GOLD) g.t(1856, 934, "moment %d of %d" % (moment, total), S, FAINT, MONO, "end") def say(g, bubbles): """bubbles: (level, parts, stem) oldest first; the last is the newest.""" ls = [wrap(p) for _, p, _ in bubbles] hs = [len(l) * 54 + 26 for l in ls] tops = place_bubbles([lv for lv, _, _ in bubbles], hs) for i, ((lv, p, stem), lines, top) in enumerate(zip(bubbles, ls, tops)): bubble(g, top, lines, i == len(bubbles) - 1, stem) # ---- the beats ------------------------------------------------------------------------------------------------ def src_panel(g, y, n, first, cur, waiting=None): C.LX = PX return C.source(g, y, n, SRC, first, cur, [], "hello.c", 11, waiting=waiting) def stack_panel(g, y, n, k, top, rsp, rbp, marks, newest, base=None): """base: what the offsets count from, the newest frame's (rsp at a function's entry, rbp in its body).""" C.RX = PX roles = {i: (r, VELLUM if r not in ("saved rbp",) else DIM) for i in range(n) for r in [ROLES.get(top + 8 * i)] if r} base = base or ("rbp", rbp) return C.stack(g, y, stack_rows(k, top, n, rsp, rbp, marks), frames(top, n, newest), note="%d words · offsets from %s" % (n, base[0]), rsp=base[1], roles=roles) ARGS = {0x40119c: [("text", "rdi ", DIM), ("addr", 0x402004, R_), ("text", ' "note"', gen.LIT)], 0x4011a3: [("text", "rsi ", DIM), ("int", 0)], 0x4011a5: [("text", "al ", DIM), ("int", 0)]} CALL_C = [("text", "open", BRIGHT), ("text", '("note", O_RDONLY)', VELLUM)] def beat_call(g): """The call (37 to 38), mid-step: the return address struck, the touch down the gutter to it.""" M.stage(False) y = shelf(g, [("system calls", [("none yet", FAINT)]), ("its bytes", [("no call crossing", FAINT)]), ("registers", [("rsp ", "mono"), ("7fffffffeca8", "struck")])]) M.strip(False) ys = src_panel(g, y, 3, 5, 6) yd = y + ph(3) + 12 rows = rows_of(MAIN, 0x401184, 0x4011b7, {0x40119c: "trail2", 0x4011a3: "trail1", 0x4011a5: "trail0", 0x4011a8: "cur"}, {}) C.LX = PX at = C.disasm(g, yd, rows, "main", 6) for a, p in list(ARGS.items()) + [(0x4011a8, CALL_C)]: T.note(g, PX + C.DX_NOTE, at[a], p) ys_ = yd + ph(len(rows)) + 12 n = (1032 - ys_ - 32) // ROW sy = stack_panel(g, ys_, n, 38, 0x7fffffffeca8, 0x7fffffffeca8, 0x7fffffffed00, {0x7fffffffeca8: {1: "strike"}}, [("open@plt", 0x7fffffffeca8)], base=("rsp", 0x7fffffffeca8)) M.strip(False) C.LX = PX C.thread(g, ys, [(at[0x40119c], at[0x4011a8] + ROW)]) # the touch, in the column: out of the call row's right rule, down the gutter, onto the stack's rule at its word gen.arrow(g, [(944, at[0x4011a8] + 12), (956, at[0x4011a8] + 12), (956, sy[0] + 12), (946, sy[0] + 12)], VERM, 2, r=6, hl=7, hw=8) say(g, [(at[0x40119c] + 12, [("main puts open's arguments in registers: ", "sans"), ('"note"', "mono"), (" in ", "sans"), ("rdi", "mono"), (", O_RDONLY in ", "sans"), ("rsi", "mono"), (".", "sans")], None), (at[0x4011a8] + 12, [("The call pushes the way back, ", "sans"), ("main+45", "mono"), (", and goes down to the stub.", "sans")], None)]) column_clock(g, 7, 46) def descent_windows(main_lo, main_hi, main_states, main_notes, stub_rows, open_rows, kernel=None): w = [("main", None, rows_of(MAIN, main_lo, main_hi, main_states, main_notes))] if stub_rows: w.append(("open@plt", "the PLT · its jmp reads open@got, 403fd8", stub_rows)) if open_rows: w.append(("open", "libc.so.6 · no source here", open_rows)) if kernel: w.append(("kernel", "openat, system call 257", kernel)) return w def beat_stub(g): """In the stub (38), the rest before its jmp's step: the bubble's stem on the jmp row; the stack kept under the descent, the return address on its top, since what the jmp does not do there is the point.""" M.stage(False) y = shelf(g, [("system calls", [("none yet", FAINT)]), ("its bytes", [("no call crossing", FAINT)]), ("registers", [])]) M.strip(False) ys = src_panel(g, y, 3, 5, 6, waiting="main waits at line 6 · the pc is in the PLT") yd = y + ph(3) + 12 stub = rows_of(STUB, 0x401070, 0x40107b, {0x401070: "cur"}, {0x401070: [("", "arrow"), (L + 0x110ec0, CODE), (" open", VELLUM)], 0x401076: ("text", "lazy binding: unused", FAINT)}) stub = [r[:5] + ((("runs", r[5]),) if isinstance(r[5], list) else (r[5],)) for r in stub] win = descent_windows(0x401188, 0x4011ad, {0x40119c: "trail2", 0x4011a3: "trail1", 0x4011a5: "trail0", 0x4011a8: "wait"}, {0x4011a8: ("runs", [("open", BRIGHT), ('("note", O_RDONLY)', VELLUM)])}, stub, None) C.LX = PX at = C.descent(g, yd, win, note="open@plt · hello") for a, p in ARGS.items(): T.note(g, PX + C.DX_NOTE, at[a], p) T.note(g, PX + C.DX_NOTE, at[0x401188], [("text", "the stack's canary", DIM)]) ys_ = yd + ph(sum(len(w[2]) for w in win) + len(win) - 1) + 12 n = (1032 - ys_ - 32) // ROW stack_panel(g, ys_, n, 38, 0x7fffffffeca8, 0x7fffffffeca8, 0x7fffffffed00, {0x7fffffffeca8: {1: "tick"}}, [("open@plt", 0x7fffffffeca8)], base=("rsp", 0x7fffffffeca8)) C.thread(g, ys, [(at[0x40119c], at[0x4011a8] + ROW)], VELLUM, 2) say(g, [(at[0x4011a8] + 12, [("The call pushes the way back, ", "sans"), ("main+45", "mono"), (", and goes down to the stub.", "sans")], None), (at[0x401070] + 12, [("The stub is one jump through ", "sans"), ("open@got", "mono"), (": it pushes nothing.", "sans")], at[0x401070])]) column_clock(g, 8, 46) def open_rows(lo, hi, states, notes): return rows_of(OPEN, lo, hi, states, notes) def beat_args(g): """libc's open, its last three walked (61 to 64): mid-step of `mov eax, 0x101`, the registers unfolded.""" M.stage(False) y = shelf(g, [("system calls", [("none yet", FAINT)]), ("its bytes", [('"note" ', "mono"), ("handed in", FAINT)]), ("stack", [("open's frame, 16 words", FAINT)])]) M.strip(False) ys = src_panel(g, y, 2, 5, 6, waiting="main waits at line 6 · the pc is in libc's open") yd = y + ph(2) + 12 notes = {L + 0x110fb6: ("runs", [("r8 ", DIM), ("0", VELLUM)]), L + 0x110fb9: ("runs", [("rdi ", DIM), ("-100", VELLUM), (" AT_FDCWD", FAINT)]), L + 0x110fc0: ("runs", [("rax ", DIM), ("0x101 ", VELLUM), ("257", FAINT)])} orow = open_rows(L + 0x110fb3, L + 0x110fc7, {L + 0x110fb3: "trail2", L + 0x110fb6: "trail1", L + 0x110fb9: "trail0", L + 0x110fc0: "cur"}, notes) win = descent_windows(0x4011a5, 0x4011ad, {0x4011a8: "wait"}, {0x4011a8: ("runs", [("open", BRIGHT), ('("note", O_RDONLY)', VELLUM)])}, [(0x401070, None, "jmp", [("qword ptr [rip + 0x2f62]", VELLUM)], "pass", ("runs", [("", "arrow"), (L + 0x110ec0, CODE), (" open", VELLUM)]))], orow) C.LX = PX at = C.descent(g, yd, win, note="open · libc.so.6") yr = yd + ph(sum(len(w[2]) for w in win) + len(win) - 1) + 12 C.RX = PX C.registers(g, yr, regs(64, L + 0x110fc0), FLAGS, {"rax": {0: "strike"}, "rdi": {0: "tick"}, "r8": {0: "tick"}, "rsi": {0: "tick"}}, QUIET) M.strip(False) C.thread(g, ys, [(at[0x4011a8], at[0x4011a8] + ROW)], VELLUM, 2) say(g, [(at["open@plt"] + 12, [("The stub is one jump through ", "sans"), ("open@got", "mono"), (": it pushes nothing.", "sans")], None), (at[L + 0x110fb6] + 12, [("Its last three set the kernel's arguments, and openat's number, 257, in ", "sans"), ("rax", "mono"), (".", "sans")], None)]) column_clock(g, 31, 46) KERNEL = [[("openat(AT_FDCWD, \"note\", O_RDONLY, 0)", PAGE)], [("returns ", gen.KERNEL_DIM), ("3", "struck"), (' "note"', PAGE), (" rcx, r11: clobbered", gen.KERNEL_DIM)]] def crossing_windows(): orow = open_rows(L + 0x110f0e, L + 0x110fd4, {L + 0x110fb6: "trail2", L + 0x110fb9: "trail1", L + 0x110fc0: "trail0", L + 0x110fc5: "cur"}, {L + 0x110f15: ("runs", [("taken", VELLUM), (" · reads ", FAINT), ("zf", DIM)]), L + 0x110fb6: ("runs", [("r8 ", DIM), ("0", VELLUM)]), L + 0x110fb9: ("runs", [("rdi ", DIM), ("-100", VELLUM), (" AT_FDCWD", FAINT)]), L + 0x110fc0: ("runs", [("rax ", DIM), ("0x101 ", VELLUM), ("257", FAINT)]), L + 0x110fc5: ("runs", [("openat", BRIGHT), (" nr 257", FAINT)])}) return descent_windows(0x40119c, 0x4011ad, {0x4011a8: "wait"}, {0x4011a8: ("runs", [("open", BRIGHT), ('("note", O_RDONLY)', VELLUM)])}, [(0x401070, None, "jmp", [("qword ptr [rip + 0x2f62]", VELLUM)], "pass", ("runs", [("", "arrow"), (L + 0x110ec0, CODE), (" open", VELLUM)]))], orow, KERNEL) def beat_cross(g, bubbles=True): """The crossing (64 to 65), mid-step: the kernel answered, rax struck (on the registers' tag), the log's row in.""" M.stage(False) y = shelf(g, [("system calls", [("openat(…) = 3", "mono")]), ("its bytes", [('"note"', "mono")]), ("registers", [("rax ", "mono"), ("3", "struck"), (" rcx r11", "mono")]), ("stack", [])]) M.strip(False) ys = src_panel(g, y, 3, 5, 6, waiting="main waits at line 6 · the pc is in libc's open") yd = y + ph(3) + 12 win = crossing_windows() C.LX = PX at = C.descent(g, yd, win, note="open · libc.so.6 · the kernel") M.strip(False) C.thread(g, ys, [(at[0x4011a8], at[0x4011a8] + ROW)], VELLUM, 2) if bubbles: say(g, [(at[L + 0x110fb6] + 12, [("Its last three set the kernel's arguments, and openat's number, 257, in ", "sans"), ("rax", "mono"), (".", "sans")], None), (at["kernel"] + 12, [("syscall", "mono"), (" crosses down; the kernel opens ", "sans"), ('"note"', "mono"), (" and answers 3 in ", "sans"), ("rax", "mono"), (".", "sans")], None)]) column_clock(g, 35, 46) return at def beat_back(g): """Back up (74 to 75, then the store at 76): main's window whole again, the call ran, f stored; the stack's frame of open gone.""" M.stage(False) y = shelf(g, [("system calls", [("openat(…) = 3", "mono")]), ("its bytes", []), ("registers", [("rax 3", "mono")])]) M.strip(False) ys = src_panel(g, y, 3, 5, 6) yd = y + ph(3) + 12 rows = rows_of(MAIN, 0x401188, 0x4011c0, {0x4011a8: "trail1", 0x4011ad: "cur"}, {0x4011a8: ("runs", [("open", VELLUM), ('("note", O_RDONLY) = ', DIM), ("3", VELLUM)])}) C.LX = PX at = C.disasm(g, yd, rows, "main", 6) T.note(g, PX + C.DX_NOTE, at[0x4011ad], [("name", "f", STACK), ("holds",), ("int", 3, "strike")]) T.note(g, PX + C.DX_NOTE, at[0x401188], [("text", "the stack's canary", DIM)]) ys_ = yd + ph(len(rows)) + 12 n = (1032 - ys_ - 32) // ROW sy = stack_panel(g, ys_, n, 76, 0x7fffffffecb0, 0x7fffffffecb0, 0x7fffffffed00, {0x7fffffffecc8: {1: "strike"}}, []) M.strip(False) C.thread(g, ys, [(at[0x40119c], at[0x4011ad] + ROW)]) gen.arrow(g, [(944, at[0x4011ad] + 12), (956, at[0x4011ad] + 12), (956, sy[3] + 12), (946, sy[3] + 12)], VERM, 2, r=6, hl=7, hw=8) say(g, [(at[0x4011ad] + 12 + 400, [("syscall", "mono"), (" crosses down; the kernel opens ", "sans"), ('"note"', "mono"), (" and answers 3 in ", "sans"), ("rax", "mono"), (".", "sans")], None), (at[0x4011a8] + 12, [("open", "mono"), ("'s ", "sans"), ("ret", "mono"), (" comes straight back to main: the stub was a jump, not a call. main keeps the answer in ", "sans"), ("f", "mono"), (".", "sans")], None)]) column_clock(g, 38, 46) # ---- 54: the present frame and the column at one instant --------------------------------------------------- def present_cross(g): """The crossing in the frame as the scene has it now: `beside`, the log over the program, the descent 17 rows.""" M.stage(False) M.strip(False) C.LX, C.RX = 64, 976 ys = C.source(g, 212, 8, SRC, 3, 6, [], "hello.c", 11, waiting="main waits at line 6 · the pc is in libc's open") orow = open_rows(L + 0x110fb3, L + 0x110fd0, {L + 0x110fb6: "trail2", L + 0x110fb9: "trail1", L + 0x110fc0: "trail0", L + 0x110fc5: "cur"}, {L + 0x110fb6: ("runs", [("r8 ", DIM), ("0", VELLUM)]), L + 0x110fb9: ("runs", [("rdi ", DIM), ("-100", VELLUM), (" AT_FDCWD", FAINT)]), L + 0x110fc0: ("runs", [("rax ", DIM), ("0x101 ", VELLUM), ("257", FAINT)]), L + 0x110fc5: ("runs", [("openat", BRIGHT), (" nr 257", FAINT)])}) win = descent_windows(0x4011a5, 0x4011ad, {0x4011a8: "wait"}, {0x4011a8: ("runs", [("open", BRIGHT), ('("note", O_RDONLY)', VELLUM)])}, [(0x401070, None, "jmp", [("qword ptr [rip + 0x2f62]", VELLUM)], "pass", ("runs", [("", "arrow"), (L + 0x110ec0, CODE), (" open", VELLUM)]))], orow, KERNEL) at = C.descent(g, 448, win, note="open · libc.so.6 · the kernel") M.strip(False) C.thread(g, ys, [(at[0x4011a8], at[0x4011a8] + ROW)], VELLUM, 2) C.RX = 976 C.registers(g, 212, regs(65, L + 0x110fc5), FLAGS, {"rax": {0: "strike"}, "rcx": {0: "strike"}, "r11": {0: "strike"}}, QUIET) M.strip(False) C.RX = 976 roles = {i: (r, DIM) for i in range(11) for r in [ROLES.get(0x7fffffffec30 + 8 * i)] if r} C.stack(g, 592, stack_rows(65, 0x7fffffffec30, 11, 0x7fffffffec30, 0x7fffffffeca0), [("open", 0, 15)], note="eleven words · offsets from rbp", rsp=0x7fffffffeca0, roles=roles) M.strip(False) C.LX = 64 C.calls_log(g, 48, 152, [[("openat", VELLUM), (" dirfd ", DIM), ("AT_FDCWD", VELLUM), (" path ", DIM), ('"note"', gen.LIT), (" flags ", DIM), ("O_RDONLY", VELLUM), (" = ", DIM), ("3", "held")]]) C.RX = 976 C.fields(g, 48, 152, "the path openat was handed", 0x402000, 16, [(4, 5, "text", False, None)], {0x402004 + i: c for i, c in enumerate(b"note\0")} | {0x402009 + i: 0 for i in range(7)}, R_, region) M.clock(g, 35, 46) gen.caption(g, [[("syscall", "mono"), (" crosses down. The kernel opens the file", )], [("and answers in rax: 3, the descriptor.", )]]) def comparison(out): parts = [] for nm, draw in (("tmp-present", present_cross), ("tmp-column", beat_cross)): save(nm, draw) parts.append(Image.open(HERE + "/" + nm + ".png").convert("RGB")) for ext in ("svg", "png"): subprocess.run(["rm", "-f", "%s/%s.%s" % (HERE, nm, ext)], check=True) head = 76 s = Image.new("RGB", (1920, 2 * (1080 + head)), "#0a1030") d = ImageDraw.Draw(s) labels = [("the present frame", "beside: the log and its bytes over the columns; source 8, the descent 17 rows, " "registers 14, stack 11; the caption under the stage"), ("the column", "the panes the crossing is about in one column (source 3, the descent 24 rows), the rest " "on the shelf; the narration level with what it speaks of")] for i, (im, (t, sub)) in enumerate(zip(parts, labels)): y = i * (1080 + head) d.text((64, y + 18), t, font=T.FONT("Go-Medium", 34), fill="#e8c46a" if i else "#eee6d2") d.text((64 + d.textlength(t, font=T.FONT("Go-Medium", 34)) + 28, y + 30), sub, font=T.FONT("Go-Regular", 22), fill="#9aa6d9") s.paste(im, (0, y + head)) s.save(HERE + "/" + out) # ---- 55: a wide subject, its bubble on it ----------------------------------------------------------------- def graph_on(g): """Sort's rings as the beat's whole subject, a size and a half up, the program and the state on the shelf, the bubble set in the drawing's free space and tied to the block it speaks of.""" M.stage(False) y = shelf(g, [("sort.c", [("line 12", "mono")]), ("disassembly", [("jne 4011af", "mono")]), ("registers", []), ("stack", [])]) k = 2.0 w, h = 880 * k, 344 * k x0, y0 = (1920 - w) / 2, y st = G.sort_state(G.UPTO, G.PC, G.TRAIL_ROWS, G.NOTES) C.tag(g, 64, y0, 1792, h + 24, "the blocks of sort", focus=True, note="its blocks · the visits so far · the path · the loops' turns") g.o.append('' % (x0, y0 + 24, k)) G.rings(g, 0, -24, 880, 344, G.SORT_BL, G.SORT_BASE, st, G.SORT_RINGS, "", tag=False) g.o.append("") # the free space: over the left ring, left of its top block; the bubble there, its stem to +56 kept outside rings lines = wrap([("The sixth pass, its third compare: ", "sans"), ("17 > 5", "mono"), (", so ", "sans"), ("jne", "mono"), (" takes the swap.", "sans")], 470) bx, by, bw = 104, y0 + 60, 500 bh = len(lines) * 54 + 26 g.rect(bx, by, bw, bh, RAISED) yy = by + 53 for runs in lines: g.rich(bx + 16, yy, [(wd + (" " if sp else ""), VELLUM, "mono" if f == "mono" else "sans") for wd, f, sp in runs], 40) yy += 54 # +56: on the inner ring at its 3 o'clock (graph.py places it); the stem keeps above the rings and outside them cx, cy, r = x0 + (720 - 64) * k, y0 + 24 + (230 - 48 - 24) * k, 84 * k tx = cx + r + 40 gen.arrow(g, [(bx + bw - 30, by), (bx + bw - 30, y0 + 34), (tx, y0 + 34), (tx, cy - 22)], DIM, 2, r=8, hl=7, hw=8) M.clock(g, 434, 1905) def save(name_, draw, *a, **kw): gen.W, gen.H = 1920, 1080 g = Svg() draw(g, *a, **kw) g.save(name_) subprocess.run(["resvg", HERE + "/" + name_ + ".svg", HERE + "/" + name_ + ".png"], check=True) if not name_.startswith("tmp"): subprocess.run(["resvg", "-w", "480", HERE + "/" + name_ + ".svg", HERE + "/" + name_ + "-480.png"], check=True) if __name__ == "__main__": save("49-column-call", beat_call) save("50-column-stub", beat_stub) save("51-column-args", beat_args) save("52-column-cross", beat_cross) save("53-column-back", beat_back) comparison("54-column-present.png") save("55-column-graph", graph_on)