diff options
Diffstat (limited to 'design/mock/map.py')
| -rw-r--r-- | design/mock/map.py | 393 |
1 files changed, 393 insertions, 0 deletions
diff --git a/design/mock/map.py b/design/mock/map.py new file mode 100644 index 0000000..84e42fa --- /dev/null +++ b/design/mock/map.py @@ -0,0 +1,393 @@ +#!/usr/bin/env python3 +"""The map, the minimap, and the working frame they belong to, from one fresh recording of targets/list. + + python3 -B map.py # 09, 10, 11 (the working frame), 19 (the map), 20 (the dive), 21 (a loupe open) + +The working frame is the 1920 stage (context.py's blocks, as mocks 9 to 14 always drew them). The map is the same +moment unfolded on 3840 by 2160: every panel at its full length, packed into four columns by content. The minimap +is the map at a twentieth, drawn from the map itself: every run of text a stroke, the marks, the windows. The data +is `list-dbg.txt` (registers, the stack from 128 bytes under rsp, the heap) and `list.dis`, as dense.py reads them. +""" +import subprocess + +from PIL import Image, ImageDraw, ImageFont + +import gen +import dense as D +import context as C +from context import (PAGE, RAISED, RULE, VELLUM, BRIGHT, DIM, FAINT, GOLD, VERM, READ, HEAP, STACK, A, S, ROW, TAG, + PAD, MONO, b, ph, tw, mix, Svg, H_) + +MINI = (1664, 912, 192, 108) # the minimap's place in the working frame: x, y, w, h +K = 1 / 20 # map units to the minimap's +RSP, UNDER = D.RSP, 16 # the map's stack starts 16 words above rsp +ROLES = {0: "saved rbp", 3: "saved rbx", 4: "saved rbp", 26: "saved rbp", 38: "saved rbp", 42: "argc", + 43: "argv[0]", 44: "argv end", 45: "envp end", 46: "auxv"} +FRAMES = [("reverse", 0, 1), ("main", 2, 5), ("libc+27781", 6, 27), ("__libc_start_main", 28, 39), ("_start", 40, 999)] + + +def stage(map_): + """Set the blocks' globals for the map (3840) or the working frame (1920).""" + gen.W, gen.H = (3840, 2160) if map_ else (1920, 1080) + C.W = gen.W + C.TOP, C.BOTTOM = (96, 1784) if map_ else (48, 888) + C.LANE_LINK, C.LANE_VIEW = (1912, 1924) if map_ else (950, 962) + + +class Rec(Svg): + """An Svg that keeps what it drew, so the minimap can be drawn from it.""" + def __init__(self): + super().__init__() + self.log = [] + + def t(self, x, y, s, size=30, fill=VELLUM, font="mono", anchor="start"): + super().t(x, y, s, size, fill, font, anchor) + w = len(s) * 0.6 * size if font.startswith("mono") else tw(s, size, font) + x0 = x - w if anchor == "end" else x - w / 2 if anchor == "middle" else x + self.log.append(("t", x0, y, w, size, fill)) + + def rect(self, x, y, w, h, fill="none", stroke=None, sw=2, dash=None): + super().rect(x, y, w, h, fill, stroke, sw, dash) + self.log.append(("r", x, y, w, h, fill, stroke)) + + +# ---- the map -------------------------------------------------------------------------------------------- +def slot_rows(first, n, step_free=True): + """Stack rows from slot `first` (0 is 16 words above rsp): the free ones above rsp, then the stack.""" + rows = [] + for i in range(first, first + n): + a = RSP + 8 * (i - UNDER) + w = D.word(a) + if i < UNDER: + rows.append((a, [], [], ("int", w), {"free": True})) + continue + links, end = D.value(w) + rows.append((a, ["rbp", "rsp"] if a == RSP else [], links, end)) + return rows + + +def roles_from(first, n): + return {i - first: (t, DIM) for j, t in ROLES.items() for i in [j + UNDER] if first <= i < first + n} + + +def frames_from(first, n): + out = [] + for name, a, z in FRAMES: + a, z = a + UNDER - first, z + UNDER - first + if z >= 0 and a < n: + out.append((name, a if a >= 0 else -1, z)) + return out + + +def node_marks(step): + return {"tick": [0x10040b8]} | ({"read": 0x1004098} if step else {}) + + +def pins(step): + return {0x10040b0: ["rcx"], 0x1004090: ["rax"] + ([] if step else ["rdi"]), 0x1004070: ["rdi"] if step else []} + + +def draw_map(g, step, loupe, subject=False): + """The map at one moment. Returns the outlines of the working frame's windows, in map units.""" + stage(True) + sp = C.SUBJECT if subject else C.BESIDE + col = D.COL + win = {} + # A: the lens's drawing at its subject size, the file whole, the backtrace whole, the key + D.put(col[0]) + C.RX = col[2] + C.lens(g, 96, 296, 0, D.NODES, pins(step), node_marks(step), 0x1004000, 0, subject=True, view_rows={}, ties=False) + win["drawing"] = (col[0], 96, D.CW, 296) + ys = C.source(g, 404, 42, D.SRC, 1, 21, [], "list.c", 42) + first = 18 if subject else 19 + win["source"] = (col[0], 404 + TAG + PAD + (first - 1) * ROW, D.CW, sp["src"] * ROW) + D.put(col[0]) + C.backtrace(g, 1456, ph(5), D.BT, x=col[0], note="newest on top") + key(g, col[0], 1620, step) + # B: 69 instructions, the function and its neighbours + D.put(col[1]) + rows = D.window(69, step) + at = C.disasm(g, 96, rows, "reverse", 21) + D.thread_across(g, ys, [(at[0x1001926], at[0x1001926] + ROW)]) + wn = [r[0] for r in D.window(sp["dis"], step)] + win["disasm"] = (col[1], at[wn[0]], D.CW, sp["dis"] * ROW) + # C: memory, the registers, a loupe, the stack continued + D.put(col[2]) + mem = C.fields(g, 96, ph(14), "the heap, round what the step reads", 0x1004000, 512, + D.heap_fields(0x1004000, 512, 0x1004098), D.MEM, HEAP, D.region, marks=(9,) if step else (), + title="memory") + win["view"] = (col[2], mem[0x1004050 if subject else 0x1004070], D.CW, (7 if subject else 5) * ROW) + C.registers(g, 476, D.regs(step), D.FLAGS, {"rax": {0: "tick"}, "rcx": {0: "tick"}, + "rdi": {0: "strike" if step else "tick"}}) + win["registers"] = (col[2], 476, D.CW, ph(17)) + y = 476 + ph(17) + 12 + if loupe: + lys = D.loupe(g, col[2], y, [("rdi", DIM), (" → ", FAINT), ("1004070", HEAP)], "heap · struct node, 16 bytes", + 0x1004030, D.heap_fields(0x1004030, 144), [(0x1004070 - 0x1004030, 16)], 0) + win["loupe"] = (col[2], lys[0x1004050], D.CW, 5 * ROW) + y += ph(9) + 12 + n_c = (1784 - y - TAG - 2 * PAD) // ROW + # D: the stack, 16 words above rsp and on; what does not fit continues at C's foot, then `more` + D.put(col[3]) + C.stack(g, 96, slot_rows(0, 69), frames_from(0, 69), note="from rsp-80", rsp=RSP, roles=roles_from(0, 69)) + win["stack"] = (col[3], 96 + TAG + PAD + UNDER * ROW, D.CW, sp["words"] * ROW) + D.put(col[2]) + C.stack(g, y, slot_rows(69, n_c - 1), frames_from(69, n_c - 1), note="continued", rsp=RSP, + roles=roles_from(69, n_c - 1)) + left = (0x7ffffffff000 - (RSP + 8 * (69 + n_c - 1 - UNDER))) // 8 + g.rect(col[2] + 2, y + ph(n_c - 1) - 2, D.CW - 4, 2, PAGE) # one row longer: the `more` row is the panel's + g.frame(col[2], y, D.CW, ph(n_c), RULE, 2) + yy = y + TAG + PAD + (n_c - 1) * ROW + C.more(g, col[2] + 184 - 6, yy) + g.t(col[2] + 184 + 66, b(yy), "%d words, to the stack's top" % left, S, FAINT, MONO) + # the windows the working frame shows: an outline round each, 4 out + for x, y0, w, h in win.values(): + g.frame(x - 4, y0 - 4, w + 8, h + 8, DIM, 2) + return win + + +def key(g, x, y, step): + """The map's key, at the foot of a column it leaves: what this is, and its marks.""" + y0 = C.panel(g, x, y, D.CW, 5, "the map", note="targets/list · moment %d of 4" % (3 if step else 2)) + g.t(x + 12, b(y0), "every panel whole; the working frame's windows outlined", 18, DIM, "sans") + items = [("current", lambda xx, yy: C.current_row(g, xx, yy, 120, 24)), ("struck", lambda xx, yy: C.struck(g, xx, yy, 60)), + ("read", lambda xx, yy: g.frame(xx, yy, 60, ROW, READ, 2)), ("held", lambda xx, yy: C.held(g, xx + 8, yy, 40)), + ("window", lambda xx, yy: g.frame(xx, yy, 60, ROW, DIM, 2))] + for i, (name, draw) in enumerate(items): + xx, yy = x + 12 + (i % 3) * 290, y0 + (1 + i // 3) * ROW + 8 + draw(xx, yy) + g.t(xx + 136, b(yy), name, 18, DIM, "sans") + + +# ---- the minimap ------------------------------------------------------------------------------------------- +INKED = {GOLD, C.SOFT, VERM, C.WASH, RAISED, D.LIFT, D.LIFT_TAG, READ} + + +def minimap(g, log, windows, x0, y0, k=K, focus="drawing"): + """The map at k: every run of text a stroke 1 high in its ink at 45% toward the page; tags, paper and the + lift as blocks; the marks over them; the working frame's windows outlined, the one in focus 2.""" + W, H = 3840 * k, 2160 * k + g.rect(x0, y0, W, H, PAGE) + marks = [] + for e in log: + if e[0] == "r": + _, x, y, w, h, fill, stroke = e + if fill in (GOLD, C.SOFT, VERM) or stroke in (READ, VERM, BRIGHT): + marks.append(e) + elif fill in INKED and w * k >= 1: + g.rect(x0 + x * k, y0 + y * k, max(w * k, 1), max(h * k, 1), mix(fill, PAGE, 0.7 if fill == RAISED else 0.6)) + elif stroke in (RULE, DIM) and w > 200: + g.frame(x0 + x * k, y0 + y * k, w * k, h * k, mix(RULE, PAGE, 0.8), 1) + elif e[2] < 2100 and e[5] not in (None, "none"): + _, x, y, w, size, fill = e + g.rect(x0 + x * k, y0 + (y - size * 0.62) * k, max(w * k, 0.8), max(size * 0.05, 0.9), mix(fill, PAGE, 0.55)) + for _, x, y, w, h, fill, stroke in marks: # the marks: gold, a strike, a read; at least a pixel and a half + ink = fill if fill in (GOLD, C.SOFT, VERM) else stroke + if ink == C.SOFT: + ink = VERM + if ink == BRIGHT: + continue + g.rect(x0 + x * k, y0 + y * k, max(w * k, 1.5), max(h * k, 1.5), ink) + for name, (x, y, w, h) in windows.items(): + g.frame(x0 + (x - 4) * k, y0 + (y - 4) * k, (w + 8) * k, (h + 8) * k, BRIGHT if name == focus else VELLUM, + 1.5 if name == focus else 1) + g.frame(x0, y0, W, H, RULE, 1) + + +def map_log(step, loupe, subject=False): + stage(True) + m = Rec() + win = draw_map(m, step, loupe, subject) + return m, win + + +# ---- the working frame ------------------------------------------------------------------------------------- +BESIDE_VIEW = [7, 8, 9, 10, 11] # the view's rows (of the heap's 32): 1004070 to 10040b0 +SUBJECT_VIEW = [5, 6, 7, 8, 9, 10, 11] + + +def clock(g, moment, total): + g.rect(64, 899, 1792, 2, RULE) + g.rect(64, 898, 1792 * moment / total, 4, GOLD) + g.t(MINI[0] - 16, 934, "moment %d of %d" % (moment, total), S, FAINT, MONO, "end") + + +def working_panels(g, step, subject, wrap=lambda name: ("", "")): + """The working frame's panels, each between the strings wrap(name) gives (a transform, for the dive).""" + stage(False) + sp = C.SUBJECT if subject else C.BESIDE + out = {} + + def open_(name): + o, c = wrap(name) + g.o.append(o) if o else None + return c + + y = 48 + sp["band"] + 12 + c = open_("source") + D.put(64) + ys = C.source(g, y, sp["src"], D.SRC, 18 if subject else 19, 21, [], "list.c", 42) + g.o.append(c) if c else None + c = open_("disasm") + at = C.disasm(g, y + ph(sp["src"]) + 12, D.window(sp["dis"], step), "reverse", 21) + g.o.append(c) if c else None + out["thread"] = (ys, at[0x1001926]) + yr = 48 + sp["view"] + 12 + c = open_("registers") + D.put(976) + out["regs"] = C.registers(g, yr, D.regs(step), D.FLAGS, {"rax": {0: "tick"}, "rcx": {0: "tick"}, + "rdi": {0: "strike" if step else "tick"}}) + g.o.append(c) if c else None + c = open_("stack") + n = sp["words"] + C.stack(g, yr + ph(17) + 12, slot_rows(UNDER, n), frames_from(UNDER, n), rsp=RSP, roles=roles_from(UNDER, n)) + g.o.append(c) if c else None + c = open_("view") + view = C.fields(g, 48, sp["view"], "the heap, %x to %x" % (0x1004000 + 16 * (SUBJECT_VIEW if subject else BESIDE_VIEW)[0], + 0x10040bf), 0x1004000, 512, D.heap_fields(0x1004000, 512, 0x1004098), D.MEM, HEAP, D.region, + marks=(9,) if step else (), rows_shown=SUBJECT_VIEW if subject else BESIDE_VIEW, more_rows=False) + g.o.append(c) if c else None + out["view"] = view + return out, sp + + +def working(name, step, subject=False, loupe=False, caption=None): + """A working frame at 1920 by 1080, with its minimap.""" + m, win = map_log(step, loupe, subject) + stage(False) + g = Svg() + out, sp = working_panels(g, step, subject) + D.put(64) + C.thread(g, out["thread"][0], [(out["thread"][1], out["thread"][1] + ROW)]) + D.put(64) + C.RX = 976 + C.lens(g, 48, sp["band"], sp["view"], D.NODES, pins(step), node_marks(step), 0x1004000, 32, + link_from=(out["regs"]["rdi"], "rdi") if step and not loupe else None, subject=subject, view_rows=out["view"], + ties=not loupe) + if loupe: # opened under its pointer, rdi's new value: the heap round 1004070 + ry = out["regs"]["rdi"] + hop = 976 + 72 + lx, ly = 956, ry + ROW - 1 # it hangs from its pointer: its tag on the row's foot, the rows under rdi covered whole + D.loupe(g, lx, ly, [("rdi", DIM), (" → ", FAINT), ("1004070", HEAP)], "heap · struct node, 16 bytes", + 0x1004050, D.heap_fields(0x1004050, 80), [(0x1004070 - 0x1004050, 16)], 1, rows=5) + g.rect(hop - 2, ry + ROW - 3, 7 * A + 4, 4, HEAP) # the stem, short: a bracket under the pointer's digits, on the tag + minimap(g, m.log, win, *MINI[:2], focus="loupe" if loupe else "drawing") + clock(g, 3 if step else 2, 4) + gen.caption(g, caption) + g.save(name) + + +# ---- the dive: the map coming in to the working frame, four instants ------------------------------------- +def glide(t): + """The glide curve (0.45, 0, 0.55, 1), solved for t.""" + lo, hi = 0.0, 1.0 + for _ in range(40): + s = (lo + hi) / 2 + x = 3 * (1 - s) ** 2 * s * 0.45 + 3 * (1 - s) * s * s * 0.55 + s ** 3 + lo, hi = (s, hi) if x < t else (lo, s) + return 3 * (1 - s) * s * s + s ** 3 + + +FRAME_RECT = {"source": (64, 212, 880, ph(8)), "disasm": (64, 448, 880, ph(17)), "registers": (976, 212, 880, ph(17)), + "stack": (976, 664, 880, ph(8)), "view": (976, 48, 880, 152), "drawing": (64, 48, 880, 152)} + + +def dive(name, e, veil, outline, fade_frame): + """One instant of `dive`: e, the move's progress (glide applied), veil on the map, the windows' outline, how + far the working frame's own tags and minimap have come in.""" + m, win = map_log(False, False) + body = m.o[1:] # the map's drawing, its page and all, without its svg head + stage(False) + g = Svg() + sm = 0.5 ** (1 - e) * K ** e # the map: from filling the frame to the minimap, on the log of its scale + tx, ty = MINI[0] * e, MINI[1] * e + g.o.append('<g transform="translate(%.2f,%.2f) scale(%.4f)">' % (tx, ty, sm)) + g.o += body + if veil: + g.o.append('<rect width="3840" height="2160" fill="%s" opacity="%.2f"/>' % (PAGE, veil)) + for x, y, w, h in win.values(): + if outline: + g.o.append('<rect x="%.1f" y="%.1f" width="%.1f" height="%.1f" fill="none" stroke="%s" stroke-width="%.1f"/>' + % (x - 4, y - 4, w + 8, h + 8, BRIGHT, 2 + outline * 2)) + g.o.append("</g>") + # each window: the working panel, from its place on the map to its own, its scale on a logarithm + map_rect = {"source": win["source"], "disasm": win["disasm"], "registers": win["registers"], "stack": win["stack"], + "view": win["view"], "drawing": win["drawing"]} + + def wrap(nm): + if nm not in FRAME_RECT: + return "", "" + fx, fy, fw, fh = FRAME_RECT[nm] + mx, my, mw, mh = map_rect[nm] + # at e = 0 the panel's first row lies on its window's first row on the map, both at half size; at e = 1 + # the panel is at its own place and size. Scale on its logarithm, the offset with the move. + row0 = fy if nm in ("registers", "drawing") else fy + TAG + PAD + sw = 0.5 ** (1 - e) + ox, oy = (mx - fx) * 0.5 * (1 - e), (my - row0) * 0.5 * (1 - e) + return '<g transform="translate(%.2f,%.2f) scale(%.4f)">' % (ox, oy, sw), "</g>" + + if e > 0: + working_panels(g, False, False, wrap) + o, c = wrap("drawing") + g.o.append(o) + D.put(64) + C.RX = 976 + C.lens(g, 48, 152, 152, D.NODES, pins(False), node_marks(False), 0x1004000, 32, view_rows={0x1004090: 0}, + ties=False) + g.o.append(c) + if fade_frame > 0: + g.o.append('<g opacity="%.2f">' % fade_frame) + minimap(g, m.log, win, *MINI[:2]) + clock(g, 2, 4) + g.o.append("</g>") + gen.caption(g, [[("Stopped in ",), ("reverse", "mono"), (", about to load ",), ("head->next", "mono"), (".",)], + [("rdi", "mono"), (" points at a node; the word after its value is the next node.",)]]) + g.save(name) + + +def sheet(names, out, labels): + """Four 1920 by 1080 instants on one 3840 by 2160 sheet, each at its own size.""" + tiles = [] + for n in names: + subprocess.run(["resvg", n + ".svg", n + ".png"], check=True) + tiles.append(Image.open(n + ".png").convert("RGB")) + s = Image.new("RGB", (3840, 2160), "#000000") + font = ImageFont.truetype("/usr/share/fonts/TTF/JetBrainsMonoNL-Medium.ttf", 28) + d = ImageDraw.Draw(s) + for i, (t, lab) in enumerate(zip(tiles, labels)): + x, y = (i % 2) * 1920, (i // 2) * 1080 + s.paste(t, (x, y)) + d.rectangle((x + 8, y + 8, x + 8 + 18 * len(lab), y + 48), fill="#000000") + d.text((x + 16, y + 12), lab, font=font, fill="#fff8e8") + s.save(out) + + +if __name__ == "__main__": + import os + stage(True) + g = Rec() + draw_map(g, True, True) + D.clock4(g, 3, 4) + D.caption4(g, [[("The map: this moment of ",), ("targets/list", "mono"), (", every panel whole.",)], + [("The working frame's windows are outlined; a scene comes in from here.",)]]) + g.save("19-map") + cap9 = [[("Stopped in ",), ("reverse", "mono"), (", about to load ",), ("head->next", "mono"), (".",)], + [("rdi", "mono"), (" points at a node; the word after its value is the next node.",)]] + cap10 = [[("The load read the word at ",), ("1004098", "mono"), (": the address of the next node.",)], + [("rdi", "mono"), (" holds it now, and points at the node whose value is 9.",)]] + working("09-context", False, caption=cap9) + working("10-context-step", True, caption=cap10) + working("11-context-lens", True, subject=True, caption=cap10) + working("21-loupe", True, loupe=True, + caption=[[("rdi", "mono"), (" holds ",), ("1004070", "mono"), ("; under it opens the heap round that node.",)], + [("The minimap shows the same loupe at its place on the map.",)]]) + names = ["20a", "20b", "20c", "20d"] + dive("20a", 0, 0, 0, 0) + dive("20b", 0, 0.5, 1, 0) + dive("20c", glide(0.5), 0.5, 1, 0) + dive("20d", 1, 0.5, 0, 1) + sheet(names, "20-dive.png", ["frame 0: the map", "frame 24: windows bright, the rest veiled", + "frame 60: halfway", "frame 96: the working frame"]) + for n in names: + os.remove(n + ".svg") + os.remove(n + ".png") |
