#!/usr/bin/env python3 """The disassembly's other form: a function as a graph of its basic blocks, with what the run has done on it by the moment shown, and nothing after. python3 -B graph.py # 22-graph-sort, 24-graph-strip, 43-graph-forms (.svg) # (25, the `blocks` lens in the working frame, is frame.py's) Two forms. `rings`, the first: each loop a ring its blocks sit on, clockwise from its header at the ring's left, an inner loop a smaller ring touching its parent at its header, a loop's way out leaving along the ring's tangent; what is outside every loop laid in layers. `layers`, the second, for a function that is a tree of branches: the blocks in layers, pills and round corners. A block a pill either way (its offset, its visits so far). Data: sort as the scene builds it (clang -O0, anim/build.zig, less the instrumentation's calls): sort-clang.dis (objdump -dl), its path from `ztrace record --by insn --from sort` (the block leaders it hit), its stop in sort-dbg.txt. reverse from anim/tools/list.c (cc -O0), reverse-O0.dis, its path from `ztrace dbg` stepped by instruction. libc's `open` from objdump of /usr/lib/libc.so.6 (libc-open.dis), its path the one mock 16's run took. Every count is the path's up to the moment shown. """ import math import re from collections import Counter import gen import dense as D import context as C from context import (PAGE, RAISED, RULE, VELLUM, BRIGHT, DIM, FAINT, GOLD, KW, CODE, HEAP, STACK, DATA, A, S, ROW, TAG, PAD, MONO, BOLD, b, ph, mix, Svg) HERE = __file__.rsplit("/", 1)[0] NORETURN = {"__stack_chk_fail", "__syscall_do_cancel"} TRAIL = gen.TRAIL def read_dis(name, lines=True): """(address, line, mnemonic, operands, target) from objdump -d[l] -M intel.""" out, line = [], None for l in open(f"{HERE}/{name}"): m = re.search(r"\.c:(\d+)", l) if m and not re.match(r"^\s+[0-9a-f]+:", l): line = int(m.group(1)) continue m = re.match(r"^\s+([0-9a-f]+):\s+(\S+)\s*(.*)$", l) if not m: continue mnem, ops = m.group(2), m.group(3) if mnem in ("addr32", "bnd", "notrack") and ops: # a prefix: the instruction is what follows it mnem, _, ops = ops.partition(" ") ops = ops.strip() tgt = re.match(r"([0-9a-f]+) <([^>]+)>", ops) callee = tgt.group(2) if tgt else None ops = re.sub(r"\s*<[^>]+>", "", re.sub(r"\s+#.*", "", ops)).strip() mn, ops = D.conv(mnem, ops) out.append((int(m.group(1), 16), line if lines else None, mn, ops, int(tgt.group(1), 16) if tgt else None, callee)) return out class Block: def __init__(self, name, rows): self.name, self.rows = name, rows self.a = rows[0][0] self.out = [] # (target name, kind): taken, fall, jmp self.ret = rows[-1][2] == "ret" def blocks(insns, base): """Basic blocks: a leader at the entry, at every jump's target and after every jump, ret or call that does not return. Padding (nops after a jump) is left out. Names are offsets: +5f.""" lo, hi = insns[0][0], insns[-1][0] lead = {lo} for i, (a, ln, mn, ops, t, callee) in enumerate(insns): ends = mn.startswith("j") or mn == "ret" or (mn == "call" and callee in NORETURN) if ends and i + 1 < len(insns): lead.add(insns[i + 1][0]) if mn.startswith("j") and t and lo <= t <= hi: lead.add(t) out, cur = {}, [] for r in insns: if r[0] in lead and cur: out[cur[0][0]] = cur cur = [] cur.append(r) out[cur[0][0]] = cur bl = {} for a, rows in out.items(): if all(r[2] in ("nop", "xchg") for r in rows): continue # padding between functions' paths: not a block bl[a] = Block("+%x" % (a - base), rows) order = sorted(bl) for i, a in enumerate(order): last = bl[a].rows[-1] nxt = bl[a].rows[-1][0] + 1 nxt = next((x for x in order if x > a), None) mn, t, callee = last[2], last[4], last[5] if mn == "jmp": bl[a].out = [(t, "jmp")] elif mn.startswith("j"): bl[a].out = [(t, "taken"), (nxt, "fall")] elif mn == "ret" or (mn == "call" and callee in NORETURN): bl[a].out = [] else: bl[a].out = [(nxt, "fall")] bl[a].out = [(x, k) for x, k in bl[a].out if x in bl] return bl def layers(bl, entry): """Back edges by a depth-first walk from the entry; layers by the longest path over the rest; a block that returns in the last layer.""" back, state = set(), {} def walk(a): state[a] = 1 for t, k in bl[a].out: if state.get(t) == 1: back.add((a, t)) elif t not in state: walk(t) state[a] = 2 walk(entry) lay = {a: 0 for a in bl} for _ in bl: for a in bl: for t, k in bl[a].out: if (a, t) not in back: lay[t] = max(lay[t], lay[a] + 1) last = max(lay.values()) for a in bl: if bl[a].ret: lay[a] = last + (0 if all(lay[x] < last or bl[x].ret for x in bl) else 1) return lay, back def arrow(g, pts, *a, **k): """gen.arrow, its repeated and straight-through points dropped.""" q = [] for p in pts: if q and abs(p[0] - q[-1][0]) < 0.5 and abs(p[1] - q[-1][1]) < 0.5: continue if len(q) >= 2 and (abs(q[-2][0] - q[-1][0]) < 0.5 and abs(q[-1][0] - p[0]) < 0.5 or abs(q[-2][1] - q[-1][1]) < 0.5 and abs(q[-1][1] - p[1]) < 0.5): q[-1] = p continue q.append(p) gen.arrow(g, q, *a, **k) def place(bl, lay, cols, widths, heights, x0, y0, pitch, gap): """Each block at its column (a scene's layout, by the rule in the catalogue) and its layer.""" nl = max(lay.values()) + 1 lh = [max([heights[a] for a in bl if lay[a] == k] or [0]) for k in range(nl)] ly = [y0] for k in range(1, nl): ly.append(ly[-1] + lh[k - 1] + gap) pos = {a: (x0 + cols[a] * pitch, ly[lay[a]]) for a in bl} return pos, lh, ly def edges(g, bl, lay, back, pos, widths, heights, lh, ly, gap, st, base, lane_x0, channel_x, small=False, radius=None): """Every edge: forward ones down from the bottom by the side their target lies on, along the gap's middle, down into the target's top; one that would cross a block runs in the right channel; a back edge leaves the bottom, goes out to its loop's lane on the left, climbs it and enters its header's head from the side: the one thing that climbs. Ink by the moment shown: not taken yet `rule`; taken `dim`; the last three the trail's golds.""" sw0 = 2 trail = st.get("edge_trail", []) counts = st.get("edge_counts", {}) backs = sorted([(a, t) for a, t in back], key=lambda e: -(lay[e[0]] - lay[e[1]])) lane = {e: lane_x0 + i * (10 if small else 16) for i, e in enumerate(backs)} inc = {} for a in bl: for t, k in bl[a].out: if (a, t) not in back: inc.setdefault(t, []).append(a) track = {} for a in sorted(bl, key=lambda a: (lay[a], pos[a][0])): x, y = pos[a] w, h = widths[a], heights[a] outs = bl[a].out for t, kind in outs: e = (a, t) n = counts.get(e, 0) ink = TRAIL[trail.index(e)] if e in trail else DIM if n else RULE sw = 3 if trail and e == trail[0] else sw0 tx, ty = pos[t] tw_ = widths[t] if e in back: lx = lane[e] k = track.get(lay[a], 0) track[lay[a]] = k + 1 by = ly[lay[a]] + lh[lay[a]] + (8 if small else 14) + k * 8 sx = x + (16 if small else 40) hy = ty + (12 if not small else 12) pts = [(sx, y + h), (sx, by), (lx, by), (lx, hy), (tx - 1, hy)] st.setdefault("paths", {})[e] = pts arrow(g, pts, ink, sw, r=radius or (4 if small else 8), hl=7 if small else 10, hw=8 if small else 10) if n: # the loop's turns, on the lane under its turn into the header if st.get("struck_count") == e: # the count is a value: written, it is struck g.rect(lx + 1, hy + 8, 10 + len(str(n)) * 11, 20, C.SOFT) g.t(lx + 5, hy + 22, "×%d" % n, 18, BRIGHT, "sansb") else: g.t(lx + 5, hy + 22, "×%d" % n, 18, FAINT, "sans") continue # the side the target lies on picks the port srcs = sorted(inc.get(t, []), key=lambda s_: pos[s_][0]) slot = srcs.index(a) tx_ = tx + tw_ / 2 + (slot - (len(srcs) - 1) / 2) * (16 if small else 48) crosses = any(lay[a] < lay[o] < lay[t] and pos[o][0] - 8 < tx_ < pos[o][0] + widths[o] + 8 for o in bl) side = 1 if crosses else (tx + tw_ / 2) - (x + w / 2) # the channel is on the right sx = x + w / 2 if abs(side) < w / 2 - 40 and not crosses else ( x + w - (16 if small else 40) if side > 0 else x + (16 if small else 40)) gy = ly[lay[a]] + lh[lay[a]] + gap / 2 + track.get(("f", lay[a]), 0) * 6 track[("f", lay[a])] = track.get(("f", lay[a]), 0) + 1 between = [o for o in bl if lay[a] < lay[o] < lay[t] and pos[o][0] - 8 < tx_ < pos[o][0] + widths[o] + 8] if between: # it would cross a block: the right channel gy2 = ly[lay[t]] - gap / 2 pts = [(sx, y + h), (sx, gy), (channel_x, gy), (channel_x, gy2), (tx_, gy2), (tx_, ty)] else: pts = [(sx, y + h), (sx, gy), (tx_, gy), (tx_, ty)] st.setdefault("paths", {})[e] = pts arrow(g, pts, ink, sw, r=radius or (4 if small else 8), hl=7 if small else 10, hw=8 if small else 10) if n and kind != "jmp" and not small and len(outs) > 1: g.t(sx + 8, y + h + 20, str(n), S, FAINT, MONO) def run_state(bl, path, upto, base): """Visits and edge counts over the path's first `upto` blocks; the last three edges taken.""" p = path[:upto] cnt = Counter(p) ec = Counter(zip(p, p[1:])) trail = list(reversed(list(zip(p, p[1:]))[-3:])) return cnt, ec, trail class Path: """An edge as drawn: straight runs and quarter circles, one family. Kept as points too, to measure it and to put the travelling mark on it.""" def __init__(self, x, y): self.d, self.pts = f"M{x:.1f},{y:.1f}", [(x, y)] def to(self, x, y): self.d += f" L{x:.1f},{y:.1f}" self.pts.append((x, y)) return self def arc(self, x, y, r, cw): """A circular arc of radius r to (x, y), clockwise on the screen when cw.""" x0, y0 = self.pts[-1] self.d += f" A{r:.1f},{r:.1f} 0 0 {1 if cw else 0} {x:.1f},{y:.1f}" cl = math.hypot(x - x0, y - y0) ux, uy = (x - x0) / cl, (y - y0) / cl hh = math.sqrt(max(r * r - cl * cl / 4, 0)) nx, ny = (-uy, ux) if cw else (uy, -ux) cx, cy = (x0 + x) / 2 + nx * hh, (y0 + y) / 2 + ny * hh a0, a1 = math.atan2(y0 - cy, x0 - cx), math.atan2(y - cy, x - cx) if cw and a1 < a0: a1 += 2 * math.pi if not cw and a1 > a0: a1 -= 2 * math.pi for k in range(1, 17): a = a0 + (a1 - a0) * k / 16 self.pts.append((cx + r * math.cos(a), cy + r * math.sin(a))) return self def length(self): return sum(math.hypot(q[0] - p[0], q[1] - p[1]) for p, q in zip(self.pts, self.pts[1:])) def at(self, f): """The point a fraction f along, and the unit direction there.""" goal, run = self.length() * f, 0 for p, q in zip(self.pts, self.pts[1:]): l_ = math.hypot(q[0] - p[0], q[1] - p[1]) if l_ and run + l_ >= goal: k = (goal - run) / l_ return (p[0] + (q[0] - p[0]) * k, p[1] + (q[1] - p[1]) * k), ((q[0] - p[0]) / l_, (q[1] - p[1]) / l_) run += l_ p, q = self.pts[-2], self.pts[-1] l_ = math.hypot(q[0] - p[0], q[1] - p[1]) return q, ((q[0] - p[0]) / l_, (q[1] - p[1]) / l_) def draw_path(g, p, ink, sw, dash=None, hl=8, hw=8): """The edge and its head on its last point; the stroke stops at the head's base (its last leg is straight).""" (x1, y1), (x2, y2) = p.pts[-2], p.pts[-1] l_ = math.hypot(x2 - x1, y2 - y1) ux, uy = (x2 - x1) / l_, (y2 - y1) / l_ d = p.d[:p.d.rfind(" L")] + f" L{x2 - ux * (hl - 1):.1f},{y2 - uy * (hl - 1):.1f}" g.path(d, ink, sw, dash=dash, join="round", cap="butt") gen.head_tri(g, x2, y2, ux, uy, ink, hl, hw) def mark(g, p, f, ink=GOLD): """The current mark travelling an edge: the gold plaque, its point the way it goes.""" (mx, my), (ux, uy) = p.at(f) g.rect(mx - 11, my - 11, 22, 22, ink) gen.head_tri(g, mx + ux * 6, my + uy * 6, ux, uy, gen.INK, 12, 10) def edge_ink(e, st): """By the moment shown: not taken yet, `rule`; taken, `dim`; the last three taken, the trail's golds, the newest 3 units. Nothing that needs the rest of the run.""" trail = st.get("edge_trail", []) if e in trail: return TRAIL[trail.index(e)], 3 if trail.index(e) == 0 else 2, None if st.get("edge_counts", {}).get(e, 0): return DIM, 2, None return RULE, 2, None def pill(g, x, y, w, fill, stroke, sw=2, dash=None): """A block: a pill 24 high, its ends half circles.""" a = (f'") def calls_of(blk): """The functions a block calls and that come back: named in its pill, after its offset.""" if hasattr(blk, "folded"): return "" names = [r[5] for r in blk.rows if r[2] == "call" and r[5] and r[5] not in NORETURN] return " ".join(names[:2]) + (" …" if len(names) > 2 else "") def pill_text(bl, a, base, st): """A block's words: its offset, the functions it calls, and its visits so far (none before its first).""" if hasattr(bl[a], "folded"): return "+%x …" % (bl[a].folded[0] - base), "· %d" % len(bl[a].folded) n = st["blocks"].get(a, {}).get("count", 0) return "+%x" % (a - base), ("×%d" % n) if n else "" def pill_w(bl, a, base, st): off, cnt = pill_text(bl, a, base, st) c = calls_of(bl[a]) return 24 + (len(off) + len(cnt) + 1 + (len(c) + 1 if c else 0)) * A + 8 def draw_pill(g, x, y, bl, a, base, st, w=None): """Reached so far: on `raised`, its offset primary, its visits. Not yet: on the page, its offset faint, no count. A fold of blocks not reached yet: dashed, its span and how many. The pc's: framed gold, 3.""" w = w or pill_w(bl, a, base, st) s0 = st["blocks"].get(a, {}) n, cur, fold = s0.get("count", 0), s0.get("cur"), hasattr(bl[a], "folded") pill(g, x, y, w, RAISED if n else PAGE, GOLD if cur else RULE, 3 if cur else 2, "12 8" if fold else None) off, cnt = pill_text(bl, a, base, st) g.t(x + 12, b(y), off, S, VELLUM if n else FAINT, MONO) c = calls_of(bl[a]) if c: # a call is not an edge: the callee named in the pill, faint g.t(x + 12 + (len(off) + 1) * A, b(y), c, S, FAINT, MONO) if cnt: g.t(x + w - 12, b(y), cnt, S, DIM if n else FAINT, MONO, "end") return w # ---- the second form: layers ------------------------------------------------------------------------------------ def layered(g, x, y, w, h, bl, entry, base, cols, st, title, pitch=300, tag=True): """`layers`: the blocks in layers by the longest path from the entry (from the code, not from the run), each under its fall-through predecessor and a taken target to its right; a block a pill; edges down from the foot, a back edge up its lane on the left; every corner round (12). Returns the pills' places and the height used.""" lay, back = layers(bl, entry) widths = {a: pill_w(bl, a, base, st) for a in bl} heights = {a: 24 for a in bl} x0 = x + 24 + len(back) * 10 + 16 pos, lh, ly = place(bl, lay, cols, widths, heights, x0 + 60, y + TAG + 14, pitch, 12) if tag: C.tag(g, x, y, w, h, title, focus=True, note="its blocks · the visits so far · the path") edges(g, bl, lay, back, pos, widths, heights, lh, ly, 12, st, base, x + 24, max(pos[a][0] + widths[a] for a in bl) + 20, small=True, radius=12) for a in bl: draw_pill(g, *pos[a], bl, a, base, st, widths[a]) return pos, max(by for _, by in pos.values()) + 24 - y def fold_not_yet(bl, reached, groups): """The blocks the run has not reached by the moment shown, folded: each group (the code beyond one way the run has not taken yet, as far as it leads, cut so that no fold leads back into another) one dashed pill in the place of its first block; an edge into or out of a member is the fold's.""" out = {a: bl[a] for a in bl if a in reached or not any(a in ms for ms in groups)} rep = {} for ms in groups: fb = Block("+%x" % ms[0], [r for m in ms for r in bl[m].rows]) fb.folded = ms out[ms[0]] = fb rep.update({m: ms[0] for m in ms}) for a, blk in list(out.items()): seen = [] for t, k in (blk.out if not hasattr(blk, "folded") else [o for m in blk.folded for o in bl[m].out]): t2 = rep.get(t, t) if t2 != a and t2 in out and t2 not in [s_ for s_, _ in seen]: seen.append((t2, k)) blk.out = seen if hasattr(blk, "folded"): blk.ret = False return out # ---- the first form: rings ---------------------------------------------------------------------------------- def rings(g, x, y, w, h, bl, base, st, spec, title, moving=None, struck=None, tag=True): """`rings`: each loop a circle its blocks sit on, upright, clockwise from its header at the ring's left (9 o'clock), where the way in arrives; an inner loop a smaller ring outside its parent, touching it at its header, which is the parent's block at 3 o'clock. An edge from a block to the next on its ring, and a loop's back edge, are arcs of the ring; an edge inside a loop that passes blocks of it is an arc 30 inside the ring; a way out leaves along the ring's tangent, out of the pill's foot, and every bend after is a quarter circle of 16. Outside every loop the blocks lie as `layers` lays them. The loop's turns so far at its centre. spec: rings {name: (dx, dy, r)} from the origin; at {offset: (ring, degrees)}; free {offset: (dx, dy)}; the origin (dx, dy) from the panel's corner; edges {(from, to): kind}, kind "line", ("arc", ring), ("skip", ring) or a function of the places giving a Path; hook {offset: (dx, side)}.""" if tag: C.tag(g, x, y, w, h, title, focus=True, note="its blocks · the visits so far · the path · the loops' turns") ox, oy = x + spec["origin"][0], y + spec["origin"][1] ring = {k: (ox + dx, oy + dy, r) for k, (dx, dy, r) in spec["rings"].items()} pos = {} for off, (k, deg) in spec["at"].items(): cx, cy, r = ring[k] pos[base + off] = (cx + r * math.cos(math.radians(deg)), cy + r * math.sin(math.radians(deg))) for off, (dx, dy) in spec["free"].items(): pos[base + off] = (ox + dx, oy + dy) pw = {a: pill_w(bl, a, base, st) for a in pos} inside = lambda a, px, py: abs(px - pos[a][0]) < pw[a] / 2 + 4 and abs(py - pos[a][1]) < 16 def arc(a, t, k, inset=0): cx, cy, r = ring[k] r -= inset a0 = math.atan2(pos[a][1] - cy, pos[a][0] - cx) a1 = math.atan2(pos[t][1] - cy, pos[t][0] - cx) while a1 <= a0: a1 += 2 * math.pi pt = lambda th: (cx + r * math.cos(th), cy + r * math.sin(th)) s, e = a0, a1 while inside(a, *pt(s)): s += 0.004 while inside(t, *pt(e)): e -= 0.004 p = Path(*pt(s)) steps = max(1, int((e - s) / (math.pi / 2)) + 1) # an arc a quarter or less at a time for k_ in range(1, steps + 1): p.arc(*pt(s + (e - s) * k_ / steps), r, True) return p paths = {} for (fo, to), kind in spec["edges"].items(): a, t = base + fo, base + to if callable(kind): p = kind(pos, pw, base) elif kind == "line": p = Path(pos[a][0] + pw[a] / 2, pos[a][1]).to(pos[t][0] - pw[t] / 2, pos[t][1]) elif kind[0] == "arc": p = arc(a, t, kind[1]) else: p = arc(a, t, kind[1], 30) paths[(a, t)] = p for e, p in sorted(paths.items(), key=lambda kv: kv[0] in st.get("edge_trail", ())): ink, sw, dash = edge_ink(e, st) (draw_arc_head if len(p.pts) > 2 and abs(p.pts[-1][0] - p.pts[-2][0]) > 0.5 and abs(p.pts[-1][1] - p.pts[-2][1]) > 0.5 else draw_path)(g, p, ink, sw, dash) for k, e in spec["turns"].items(): # each loop's turns so far, at its centre, under the line its pills are on cx, cy, _ = ring[k] cy += spec.get("turns_dy", 30) e = (base + e[0], base + e[1]) n = st.get("edge_counts", {}).get(e, 0) if not n: continue s, word = str(n), " turn" if n == 1 else " turns" x0 = cx - (len(s) * A + gen.tw(word, 18, "sans")) / 2 if struck == e: g.rect(x0 - 5, cy - 11, len(s) * A + 10, 22, C.SOFT) g.t(x0, cy + 7, s, S, BRIGHT if struck == e else VELLUM, BOLD if struck == e else MONO) g.t(x0 + len(s) * A, cy + 6, word, 18, DIM, "sans") for a in pos: s0 = dict(st["blocks"].get(a, {})) if moving: s0.pop("cur", None) draw_pill(g, pos[a][0] - pw[a] / 2, pos[a][1] - 12, bl, a, base, {"blocks": {a: s0}}, pw[a]) if moving: mark(g, paths[moving[0]], moving[1]) return paths, pos def draw_arc_head(g, p, ink, sw, dash): """An arc, its stroke ending where its head begins, the head on the arc's tangent.""" pts, run, cut = p.pts, 0, 7 k = len(pts) - 1 while k > 0 and run < cut: run += math.hypot(pts[k][0] - pts[k - 1][0], pts[k][1] - pts[k - 1][1]) k -= 1 d = "M%.1f,%.1f " % pts[0] + " ".join("L%.1f,%.1f" % q for q in pts[1:k + 1]) g.path(d, ink, sw, dash=dash, join="round") (x1, y1), (x2, y2) = pts[-2], pts[-1] l_ = math.hypot(x2 - x1, y2 - y1) gen.head_tri(g, x2, y2, (x2 - x1) / l_, (y2 - y1) / l_, ink, 8, 8) # ---- the data: sort as the scene builds it ------------------------------------------------------------------ SORTC = [r for r in read_dis("sort-clang.dis") if 0x401150 <= r[0] < 0x401200] SORT_BASE = 0x401150 SORT_BL = blocks(SORTC, SORT_BASE) SB = sorted(SORT_BL) # the block leaders the run hit, from sort's entry to its return: ztrace record --by insn --from sort, clang -O0 SORT_PATH = [SORT_BASE + int(o, 16) for o in ( "0 12 1e 24 2f 56 5d 97 99 12 1e 24 2f 56 5d 97 99 12 1e 24 2f 56 5d 97 99 12 1e 24 2f 56 5f 24 2f 56 5f 24 2f " "56 5d 97 99 12 1e 24 2f 56 5f 24 2f 56 5d 97 99 12 1e 24 2f 56 5f 24 2f 56 5f 24 2f 56 5f 24 2f 56 5f 24 2f 56 " "5f 24 2f 56 5d 97 99 12 1e 24 2f 56 5f 24 2f 56 5f 24 2f 56 5d 97 99 12 a7").split()] O = lambda off: SORT_BASE + off def sort_state(upto, cur, trail_rows, notes=None, cur_line=12): """The run's facts by the moment shown: the path's first `upto` blocks, and nothing after.""" cnt, ec, trail = run_state(SORT_BL, SORT_PATH, upto, SORT_BASE) blk = {a: {"count": cnt.get(a, 0)} for a in SORT_BL} if cur: ca = max(a for a in SORT_BL if a <= cur) blk[ca].update(cur=cur, trail=trail_rows, notes=notes or {}, cur_line=cur_line) return {"blocks": blk, "edge_counts": ec, "edge_trail": trail} # the 11th time through the compare: i = 6, j = 4, a[3] = 17 against a[4] = 5; the stop is the jne after it # (sort-dbg.txt). Five swaps so far, five turns of each loop; the function's way out not taken yet. UPTO = [i for i, a in enumerate(SORT_PATH) if a == O(0x56)][10] + 1 PC = 0x4011ab TRAIL_ROWS = [0x4011a9, 0x4011a6, 0x4011a3] NOTES = {0x4011a6: [("7fffffffecd3 ", STACK), ("◂ ", FAINT), ("1", VELLUM)], 0x4011a9: [("1", VELLUM), (" : ", FAINT), ("1", VELLUM), (" ", FAINT), ("zf", DIM)], 0x4011ab: [("taken", VELLUM), (" · reads zf", FAINT)]} def down_round(sx, sy, ty, tx, ey, r=16): """A way out: straight down from (sx, sy) to ty, round a quarter, across to tx, round a quarter, and straight to (tx, ey), up or down.""" left = tx < sx p = Path(sx, sy).to(sx, ty - r) p.arc(sx + (-r if left else r), ty, r, left) up = ey < ty p.to(tx + (r if left else -r), ty) p.arc(tx, ty + (-r if up else r), r, left if up else not left) return p.to(tx, ey) SORT_RINGS = { "origin": (434, TAG + 14 + 144), "rings": {"o": (0, 0, 140), "i": (224, 0, 84)}, "at": {0x12: ("o", 180), 0x1e: ("o", 225), 0x24: ("o", 0), 0x5d: ("o", 60), 0x97: ("o", 110), 0x99: ("o", 145), 0x2f: ("i", 270), 0x56: ("i", 0), 0x5f: ("i", 75)}, "free": {0x0: (-320, 0), 0xa7: (-180, 124)}, "edges": {(0x0, 0x12): "line", (0x12, 0x1e): ("arc", "o"), (0x1e, 0x24): ("arc", "o"), (0x24, 0x2f): ("arc", "i"), (0x2f, 0x56): ("arc", "i"), (0x56, 0x5f): ("arc", "i"), (0x5f, 0x24): ("arc", "i"), (0x24, 0x56): ("skip", "i"), (0x5d, 0x97): ("arc", "o"), (0x97, 0x99): ("arc", "o"), (0x99, 0x12): ("arc", "o"), # the inner loop's way out: down out of +56's foot, along its ring's tangent, under +5f, into +5d's end (0x56, 0x5d): lambda pos, pw, base: (Path(pos[base + 0x56][0] + 30, pos[base + 0x56][1] + 12) .to(pos[base + 0x56][0] + 30, pos[base + 0x5d][1] - 16) .arc(pos[base + 0x56][0] + 14, pos[base + 0x5d][1], 16, True) .to(pos[base + 0x5d][0] + pw[base + 0x5d] / 2, pos[base + 0x5d][1])), # the function's way out: down out of +12's foot, outside the ring, into +a7 (0x12, 0xa7): lambda pos, pw, base: Path(pos[base + 0x12][0] - 40, pos[base + 0x12][1] + 12) .to(pos[base + 0xa7][0], pos[base + 0xa7][1] - 12)}, "turns": {"o": (0x99, 0x12), "i": (0x5f, 0x24)}, } # ---- libc's open, at its syscall: a tree of branches, what is not reached yet folded -------------------------- OPEN = read_dis("libc-open.dis", lines=False) OPEN_BASE = 0x110ec0 L = 0x7ffff7c00000 def rebase(rows): return [(a + L, ln, mn, ops, (t + L) if t else None, callee) for a, ln, mn, ops, t, callee in rows] OPEN_BL = blocks(rebase(OPEN), OPEN_BASE + L) OPEN_PATH = [a + L for a in (0x110ec0, 0x110ee9, 0x110efb, 0x110fb0)] # by the syscall (mock 16's moment 4) # ---- reverse: a loop's turn ------------------------------------------------------------------------------------ REV = read_dis("reverse-O0.dis") REV_BASE = 0x401188 REV_BL = blocks(REV, REV_BASE) RB = sorted(REV_BL) R0, R1, R2, R3 = RB REV_PATH = [R0, R2, R1, R2, R1, R2, R1, R2, R3] REV_RINGS = { "origin": (441, TAG + 14 + 82), "rings": {"o": (0, 0, 70)}, "at": {R2 - REV_BASE: ("o", 180), R1 - REV_BASE: ("o", 0)}, "free": {R0 - REV_BASE: (-230, 0), R3 - REV_BASE: (-100, 100)}, "edges": {(R0 - REV_BASE, R2 - REV_BASE): "line", (R2 - REV_BASE, R1 - REV_BASE): ("arc", "o"), (R1 - REV_BASE, R2 - REV_BASE): ("arc", "o"), (R2 - REV_BASE, R3 - REV_BASE): lambda pos, pw, base: Path(pos[R2][0] - 30, pos[R2][1] + 12) .to(pos[R3][0], pos[R3][1] - 12)}, "turns": {"o": (R1 - REV_BASE, R2 - REV_BASE)}, } # ---- the disassembly beside the graph, linear, its blocks ruled --------------------------------------------------- TEXT = read_dis("sort-clang.dis") # the program's text: swap, sort, main FUNC = lambda a: "swap" if a < 0x401150 else "sort" if a < 0x401200 else "main" def sort_disasm(g, y, n, line=12): """n rows round the pc; each block's first row under a rule, its visits so far ranged right in the annotation's room; the trail's three rows and the pc's with what they did; another function's rows `outside`.""" i = [r[0] for r in TEXT].index(PC) lo = max(0, min(i - n // 2, len(TEXT) - n)) st = sort_state(UPTO, PC, TRAIL_ROWS) notes = dict(NOTES) notes[0x4011a3] = [("7fffffffecd3 ", STACK), ("◂ ", FAINT), ("1", VELLUM)] rows, last = [], None for a, ln, mn, ops, t, callee in TEXT[lo:lo + n]: f = FUNC(a) state = "cur" if a == PC else {0x4011a9: "trail0", 0x4011a6: "trail1", 0x4011a3: "trail2"}.get(a) note = ("runs", notes[a]) if a in notes else ("text", callee, VELLUM) if mn == "call" else None if f != "sort": state = "other_first" if f != last else "other" note = ("text", f, DIM) if f != last else note rows.append((a, ln if f == "sort" else None, mn, [(t, CODE)] if t and (mn.startswith("j") or mn == "call") else [(ops, VELLUM)], state, note)) last = f at = C.disasm(g, y, rows, "sort", line) for a in at: if a in SORT_BL: # a block's first row: the rule over it, its pill's count in digits yy = at[a] g.rect(C.LX + 2, yy, C.CW - 4, 2, RULE) n_ = st["blocks"][a]["count"] if n_: g.t(C.LX + C.CW - 12, b(yy), "×%d" % n_, S, FAINT, MONO, "end") line12 = [at[r[0]] for r in rows if r[1] == 12 and r[0] < 0x4011af] return at, (min(line12), max(line12) + ROW), lo # ---- the mocks ------------------------------------------------------------------------------------------------ def frame_sort_map(name): """Mock 22: the map's first two columns at the 11th compare: the drawing (rings, at its size in the frame) over the source whole, and the disassembly column linear, 69 rows round the pc, each block ruled with its visits so far; the thread across the gutter; the working frame's windows outlined.""" gen.W, gen.H = 2000, 1860 g = Svg() st = sort_state(UPTO, PC, TRAIL_ROWS, NOTES) g.t(112, 60, "the map: sort's rings over the source, the disassembly linear beside them, at the 11th compare", 22, DIM, "sans") rings(g, 112, 96, 880, 344, SORT_BL, SORT_BASE, st, SORT_RINGS, "the blocks of sort") src = open(HERE + "/../../anim/tools/sort.c").read().split("\n") C.LX = 112 ys = C.source(g, 452, 22, src, 1, 12, [14, 13], "sort.c", 22) C.LX = 1024 at, run, lo = sort_disasm(g, 96, 69) C.LX = 64 D.thread_across(g, ys, [run]) g.frame(112 - 4, 96 - 4, 888, 352, DIM, 2) # the frame's windows (25): the drawing whole for bx, by, bw, bh in ((112, 452 + TAG + PAD + 8 * ROW, 880, 6 * ROW), (1024, at[0x4011ab] - 5 * ROW, 880, 11 * ROW)): g.frame(bx - 1, by - 1, bw + 2, bh + 2, DIM, 2) # on the rows' edges, never through a line's letters g.rect(112, 1798, 1792, 2, RULE) g.t(112, 1824, "the map's stage ends at 1784; the other columns (memory, the registers whole, the stack) as in 19", 18, FAINT, "sans") g.save(name) def frame_strip(name): """Mock 24: reverse's loop as a ring, four instants: the condition; the edge taken, the mark on the ring's top; the turn, the mark coming round under it, the count at the centre struck; the way out, down the tangent.""" gen.W, gen.H = 1920, 712 g = Svg() inst = [ ("1", "the condition", "the pc on jne in the test's block, the second time; its two ways out", 4, 0x4011ce, None), ("2", "the edge taken", "taken: round the top of the ring to the body, the mark on it", 4, None, ((R2, R1), 0.5)), ("3", "the loop's turn", "back round the foot of the ring to the test; the count at its centre struck as it lands", 5, None, ((R1, R2), 0.5)), ("4", "the way out", "not taken, the fourth time: down the tangent, out of the ring, landed", 9, 0x4011d0, None), ] for k, (num, title, sub, upto, cur, moving) in enumerate(inst): x, y = 64 + (k % 2) * 912, 40 + (k // 2) * 320 g.t(x, y + 24, num, 28, GOLD, "sansb") g.t(x + 28, y + 24, title, 28, VELLUM, "sansm") g.t(x, y + 52, sub, 18, FAINT, "sans") path = REV_PATH[:upto] + ([moving[0][1]] if moving else []) cnt, ec, trail = run_state(REV_BL, path, len(path), REV_BASE) if moving: # the mark is on its edge: the target not yet entered, the edge lit cnt[moving[0][1]] -= 1 st = {"blocks": {a: {"count": cnt.get(a, 0)} for a in REV_BL}, "edge_counts": ec, "edge_trail": trail} if cur: ca = max(a for a in REV_BL if a <= cur) st["blocks"][ca].update(cur=cur) rings(g, x, y + 72, 880, 248, REV_BL, REV_BASE, st, REV_RINGS, "the blocks of reverse", moving=moving, struck=moving[0] if moving and moving[0] == (R1, R2) else None) g.save(name) def frame_forms(name): """Mock 43: the graph's two forms at the lens's scale, every count as of the moment shown: rings (the first), on sort in the pane the frame gives it; layers (the second), on sort, and on libc's open at its syscall, a tree of branches, what the run has not reached yet folded.""" gen.W, gen.H = 1920, 1260 g = Svg() st = sort_state(UPTO, PC, TRAIL_ROWS, NOTES) g.t(64, 56, "the graph's two forms", 36, BRIGHT, "sansm") g.t(64, 90, "At the lens's scale, mono 20; each in the pane the `blocks` split gives it, 880 by 344. Every number is " "the run's by the moment shown; veiled: what a pane must pan to.", 20, DIM, "sans") def label(x, y, key, title, sub): g.t(x, y, key, 28, GOLD if key == "1" else BRIGHT, "sansb") g.t(x + 30, y, title, 28, VELLUM, "sansm") g.t(x, y + 28, sub, 18, FAINT, "sans") def veil(x, y, w, h): g.o.append(f'') label(64, 152, "1", "rings: sort at the 11th compare", "each loop a ring, the inner touching the outer at its " "header; ways out down the tangent; whole in 880 by 344") rings(g, 64, 212, 880, 344, SORT_BL, SORT_BASE, st, SORT_RINGS, "the blocks of sort") cols = {a: 0 for a in SORT_BL} cols[O(0xa7)] = 1 cols[O(0x5f)] = 1 label(976, 152, "2", "layers: the same moment", "the loops as lanes; 400 high, so it pans in 344 (veiled): " "for loops, rings") _, hh = layered(Svg(), 976, 212, 880, 999, SORT_BL, SB[0], SORT_BASE, cols, st, "", pitch=330) layered(g, 976, 212, 880, hh + 16, SORT_BL, SB[0], SORT_BASE, cols, st, "the blocks of sort", pitch=330) veil(974, 212 + 344, 884, hh + 16 - 344 + 2) # libc's open at its syscall: the branches it has taken, and a fold for each run of what it has not reached yet o = lambda off: OPEN_BASE + L + off bl = fold_not_yet(OPEN_BL, set(OPEN_PATH), [[o(x) for x in (0x5b, 0x63, 0x6b, 0xad, 0xb9, 0x136, 0x149)], [o(x) for x in (0xbe, 0x10a, 0x112, 0x114, 0x123, 0x128, 0x12c, 0x130)]]) ost = {"blocks": {a: {"count": 1 if a in OPEN_PATH else 0} for a in bl}, "edge_counts": Counter(zip(OPEN_PATH, OPEN_PATH[1:])), "edge_trail": list(reversed(list(zip(OPEN_PATH, OPEN_PATH[1:]))))} ost["blocks"][OPEN_PATH[-1]]["cur"] = True ocols = {a: (0 if a in OPEN_PATH else 1) for a in bl} label(64, 670, "2", "layers: libc's open at its syscall", "a tree of branches and no loop (rings has none to " "draw, and is this); what is not reached yet folded, dashed") layered(g, 64, 730, 880, 344, bl, OPEN_PATH[0], OPEN_BASE + L, ocols, ost, "the blocks of open · libc.so.6", pitch=380) nx = 976 for i, (s, ink) in enumerate(( ("Which, and why", VELLUM), ("1 Rings, the first form. A loop is a ring and the mark going", VELLUM), (" round it is the picture of iteration; its turns at its", VELLUM), (" centre. Nested loops: rings touching at the header.", VELLUM), ("2 Layers, the second, for a function of branches: the path", DIM), (" reads down, and a fork is two columns. Rings with no", DIM), (" loop is this. For loops it is tall and pans.", DIM), (" The track is withdrawn: a ring says a loop, a layer a", DIM), (" branch; a line of blocks said neither, and its arches", DIM), (" were rings flattened to fit a band.", DIM), ("Nothing from after the moment: counts so far; an edge or", VELLUM), ("a block not reached yet is quiet (rule, page, no count);", VELLUM), ("open's folds are what it has not reached yet, and open", VELLUM), ("as the run gets there.", VELLUM))): g.t(nx, 690 + i * 30, s, 22 if i == 0 else 20, ink, "sansm" if i == 0 else "sans") g.t(64, 1120, "Ink: not reached so far, rule; taken so far, dim; the last three, the trail's golds, the newest 3. " "A fold, dashed. No count before a block's first visit.", 18, DIM, "sans") g.save(name) if __name__ == "__main__": assert len(SORT_BL) == 11 and len(REV_BL) == 4, (len(SORT_BL), len(REV_BL)) assert Counter(SORT_PATH)[O(0x56)] == 17 and Counter(SORT_PATH)[O(0x5f)] == 10 st_ = sort_state(UPTO, PC, TRAIL_ROWS) # nothing from after the moment: the way out not taken yet assert st_["blocks"][O(0xa7)]["count"] == 0 and st_["edge_counts"][(O(0x12), O(0xa7))] == 0 assert st_["blocks"][O(0x5f)]["count"] == 5 and st_["edge_counts"][(O(0x99), O(0x12))] == 5 frame_sort_map("22-graph-sort") frame_strip("24-graph-strip") frame_forms("43-graph-forms")