summaryrefslogtreecommitdiff
path: root/design/mock/pair.py
blob: 3adb60d398573aa2273e2e5ffe3dbd7f5c34c05d (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
#!/usr/bin/env python3
"""A `pair`: two builds of one program against each other. overflow.c, built plain and with clang's
AddressSanitizer (-O0 -g -fno-omit-frame-pointer -no-pie -fno-stack-protector), each recorded by instruction from
`fill` with ztrace; the frames, the shadow and the report are those recordings'.

    python3 -B pair.py     # 26-pair-rest, 27-pair-store, 28-pair-part (.svg)
"""
import gen
import context as C
from context import (PAGE, RAISED, RULE, VELLUM, BRIGHT, DIM, FAINT, GOLD, KW, CODE, STACK, DATA, HEAP, LIT, READ,
                     A, S, ROW, TAG, PAD, MONO, BOLD, b, ph, mix, Svg)

HERE = __file__.rsplit("/", 1)[0]
SRC = open(HERE + "/overflow.c").read().split("\n")
S_, D_, C_ = STACK, DATA, CODE
LEFT, RIGHT = 64, 976
P = VELLUM

PLAIN = [(0x401144, 4, "mov", "dword ptr [rbp - 0x14], 0"), (0x40114b, 4, "cmp", "dword ptr [rbp - 0x14], 0x20"),
         (0x40114f, 4, "jge", 0x401165), (0x401151, 4, "movsxd", "rax, dword ptr [rbp - 0x14]"),
         (0x401155, 4, "mov", "byte ptr [rbp + rax - 0x10], 0x41"), (0x40115a, 4, "mov", "eax, dword ptr [rbp - 0x14]"),
         (0x40115d, 4, "add", "eax, 1"), (0x401160, 4, "mov", "dword ptr [rbp - 0x14], eax"), (0x401163, 4, "jmp", 0x40114b),
         (0x401165, 5, "pop", "rbp"), (0x401166, 5, "ret", "")]
ASAN = [(0x523d1e, 4, "cmp", "dword ptr [rbx + 0x64], 0x20"), (0x523d22, 4, "jl", 0x523d26), (0x523d24, 4, "jmp", 0x523d6c),
        (0x523d26, 4, "mov", "rax, qword ptr [rbx + 0x18]"), (0x523d2a, 4, "movsxd", "rcx, dword ptr [rbx + 0x64]"),
        (0x523d2e, 4, "add", "rax, rcx"), (0x523d31, 4, "mov", "qword ptr [rbx], rax"), (0x523d34, 4, "shr", "rax, 3"),
        (0x523d38, 4, "mov", "al, byte ptr [rax + 0x7fff8000]"), (0x523d3e, 4, "mov", "byte ptr [rbx + 0xf], al"),
        (0x523d41, 4, "cmp", "al, 0"), (0x523d43, 4, "je", 0x523d5b), (0x523d45, 4, "mov", "cl, byte ptr [rbx + 0xf]"),
        (0x523d48, 4, "mov", "rax, qword ptr [rbx]"), (0x523d4b, 4, "and", "rax, 7"), (0x523d4f, 4, "cmp", "al, cl"),
        (0x523d51, 4, "jl", 0x523d5b), (0x523d53, 4, "mov", "rdi, qword ptr [rbx]"), (0x523d56, 4, "call", 0x4e8450),
        (0x523d5b, 4, "mov", "rax, qword ptr [rbx]"), (0x523d5e, 4, "mov", "byte ptr [rax], 0x41"),
        (0x523d61, 4, "mov", "eax, dword ptr [rbx + 0x64]"), (0x523d64, 4, "add", "eax, 1"),
        (0x523d67, 4, "mov", "dword ptr [rbx + 0x64], eax"), (0x523d6a, 4, "jmp", 0x523d1e)]
ONLY = {0x523d24, 0x523d26, 0x523d2e, 0x523d31, 0x523d34, 0x523d38, 0x523d3e, 0x523d41, 0x523d43, 0x523d45, 0x523d48,
        0x523d4b, 0x523d4f, 0x523d51, 0x523d53, 0x523d56, 0x523d5b}       # rows the plain build has no counterpart for
NAMES = {0x4e8450: "__asan_report_store1"}
FAKE = 0x7bfff58f0000


def rows_of(code, first, n, states, notes):
    out = []
    for a, ln, mn, ops in code:
        if a < first or len(out) == n:
            continue
        parts = [((ops, NAMES[ops]) if ops in NAMES else ops, C_)] if isinstance(ops, int) else ([(ops, P)] if ops else [])
        out.append((a, ln, mn, parts, states.get(a), notes.get(a)))
    return out


def bar(g, x, y, ink, w=4, h=ROW):
    """`only here` (dim) and `differs` (bright): a bar on the row's edge that faces the seam."""
    g.rect(x, y + 1, w, h - 2, ink)


def at_side(x):
    C.LX = x
    C.RX = x


def regs(g, x, y, title, rows, marks=None):
    y0 = C.panel(g, x, y, C.CW, len(rows), title, note="the line's registers, and rip")
    marks = marks or {}
    for i, (name, links, end) in enumerate(rows):
        yy = y0 + i * ROW
        if name == "rip":
            g.t(x + 12, b(yy), name, S, C.current_row(g, x + 2, yy, C.CW - 4, 54), BOLD)
        else:
            g.t(x + 12, b(yy), name, S, DIM, MONO)
        if links:
            C.chain(g, x + 72, yy, links, end, marks.get(name), room=x + C.CW - 12)
        elif marks.get(name, {}).get(0) == "strike":
            s_ = "0x%x" % end[1]
            g.t(x + 72, b(yy), s_, S, C.struck(g, x + 67, yy, len(s_) * A + 10), BOLD)
        else:
            C.integer(g, x + 72, yy, end[1])


def shadow_cell(g, x, y, v, read=False):
    """A shadow byte on the row of the eight bytes it stands for, in the offset's place."""
    if v >= 0xf0:                        # a redzone: poisoned, on the freed hatch
        g.rect(x - 3, y + 2, 30, ROW - 4, "url(#hatch)")
        g.t(x, b(y), "%02x" % v, S, BRIGHT, BOLD)
    else:
        g.t(x, b(y), "%02x" % v, S, FAINT, MONO)
    if read:
        g.frame(x - 6, y, 36, ROW, READ, 2)


def source_pair(g, cur_l, cur_r, waiting_r=False):
    """The source shared across both columns; the left run's mark on the number, the right run's at the right end."""
    C.LX, C.CW = 64, 1792
    ys = C.source(g, 48, 5, SRC, 2, cur_l, [], "overflow.c · both builds", 10)
    y_r = 48 + TAG + PAD + (cur_r - 2) * ROW
    xr = 64 + 1792 - 2 - 24
    if cur_r != cur_l:
        g.rect(64 + 2, y_r, 1792 - 4, ROW, C.WASH)
    g.rect(xr, y_r, 24, ROW, GOLD)
    g.path(f"M{xr + 8},{y_r + 12} l8,-6 v12 Z", fill=gen.INK)
    g.t(xr - 10, b(y_r), "asan", 18, DIM, "sans", "end")
    g.t(64 + 46 + 60, b(ys) if False else b(ys), "", 18, DIM, "sans")
    C.CW = 880
    return ys


# ---- the frame as bytes: `hexdump` `eight`, by the frame's own fields --------------------------------------------
PX, AX, BX8, RD = 12, 118, 274, 522     # a row: the pin lane, the address, eight bytes 30 apart, the reading
SHX = 62                                # in a run with ASan, the row's shadow byte, in the pin lane's end


def plain_region(v):
    """Where a word of the plain build points, by its mappings; None: nowhere (a number)."""
    if 0x7ffffffde000 <= v < 0x7ffffffff000:
        return S_
    if 0x401000 <= v < 0x402000:
        return C_
    if 0x404000 <= v < 0x405000:
        return D_
    return None


def asan_region(v):
    if 0x7ffffffde000 <= v < 0x7ffffffff000:
        return S_
    if 0x7bfff58e7000 <= v < 0x7bfff6400000:
        return D_
    if 0x42c000 <= v < 0x524000:
        return C_
    if 0x524000 <= v < 0x55f000:
        return VELLUM
    return None


CODE_NAMES = {0x401120: "main", 0x401140: "fill", 0x523c40: "main", 0x523c60: "fill"}


def code_name(v):
    f = max((a for a in CODE_NAMES if a <= v and v - a < 0x100), default=None)
    return None if f is None else (CODE_NAMES[f] + ("+%d" % (v - f) if v > f else ""))


def plain_mem(stored, i):
    """The plain build's stack from 7fffffffecb0 to 7fffffffed00 after `stored` of the loop's stores, i in its slot."""
    m = {a: 0 for a in range(0x7fffffffecb0, 0x7fffffffed00)}
    for k, v in enumerate([0x00, 0xed, 0xff, 0xff, 0xff, 0x7f, 0, 0, 0x34, 0x11, 0x40, 0, 0, 0, 0, 0]):
        m[0x7fffffffece0 + k] = v
    for k in range(4):
        m[0x7fffffffeccc + k] = i >> (8 * k) & 0xff
    for k in range(stored):
        m[0x7fffffffecd0 + k] = 0x41
    return m


def asan_mem(stored):
    """ASan's fake frame (the header as its prologue wrote it, buf, the redzone) and the real stack's way back."""
    m = {}
    for off, v in [(0, 0x41b58ab3), (8, 0x5387a7), (0x10, 0x523c60), (0x18, 0x7fffffffec10), (0x38, 0x7bfff58e8000)]:
        for k in range(8):
            m[FAKE + off + k] = v >> (8 * k) & 0xff
    for k in range(0x20, 0x38):
        m[FAKE + k] = 0x41 if 0x20 <= k < 0x20 + stored else 0
    for a, v in [(0x7fffffffecc0, 0x7fffffffece0), (0x7fffffffecc8, 0x523c54)]:
        for k in range(8):
            m[a + k] = v >> (8 * k) & 0xff
    return m


DESCR = {0x5387a7: "1 32 16 5 buf:3"}


def frame_dump(g, x, y, title, note, rows, mem, region, pins=None, shadow=None, struck=None, held=(), letters=(),
               read_shadow=None, span=None, rule_after=None, differs=(), seam=None):
    """`hexdump` `eight` by the frame's fields: a row a word of the stack (eight bytes), its pins and address, the
    bytes on their field's paper, the reading: the field's name (Go 18, dim) and what its type makes of its bytes.
    rows: (address, [(offset, length, reads, name)]); reads in text | pointer | number | bytes; what no field
    covers is loose. letters: bytes stored past the field the store aimed at, which keep its ink (text) wherever
    they land. shadow: {row: byte}. span: (name, first row, last row). Returns the rows' y."""
    pins = pins or {}
    y0 = C.panel(g, x, y, C.CW, len(rows) + (0 if rule_after is None else 0), title, note=note)
    lead = C.shared([a for a, _ in rows if region(a) == region(rows[0][0])])
    ys = []
    for r, (a0, flist) in enumerate(rows):
        yy = y0 + r * ROW
        ys.append(yy)
        if rule_after is not None and r == rule_after + 1:   # another region (the real stack): a rule before it
            g.rect(x + 2, yy - 1, C.CW - 4, 2, RULE)
        if r in pins:
            C.pin(g, x + PX, yy, pins[r], S_)
        if shadow and r in shadow:
            shadow_cell(g, x + SHX, yy, shadow[r], read=read_shadow == r)
        ink_a = region(a0) or VELLUM
        C.addr(g, x + AX, yy, a0, ink_a, lead=lead if region(a0) == region(rows[0][0]) else 0)
        bx = lambda k: x + BX8 + 30 * k
        covered = {}
        for f in flist:
            for k in range(f[0], f[0] + f[1]):
                covered[k] = f
        rx = x + RD
        for f in flist:
            off, n, reads, name = f
            vals = [mem[a0 + off + k] for k in range(n)]
            word = sum(v << (8 * k) for k, v in enumerate(vals))
            g.rect(bx(off) - 4, yy + 1, 30 * n - 2, ROW - 2, RAISED)
            ink = {"text": LIT, "number": VELLUM, "bytes": VELLUM, "pointer": region(word) or VELLUM}[reads]
            nz = [k for k, v in enumerate(vals) if v]
            for k, v in enumerate(vals):
                a = a0 + off + k
                xx = bx(off + k)
                if a == struck:
                    g.t(xx, b(yy), "%02x" % v, S, C.struck(g, xx - 5, yy, 34), BOLD)
                    continue
                if a in held:
                    C.held(g, xx, yy, 24)
                padded = reads in ("number", "pointer") and nz and k > nz[-1]
                kink = LIT if a in letters else ink
                g.t(xx, b(yy), "%02x" % v, S, mix(kink, PAGE, 0.45) if padded else kink, MONO)
            # the reading: the field's name, then its value
            g.t(rx, b(yy) - 1, name, 18, DIM, "sans")
            vx = rx + gen.tw(name, 18, "sans") + 10
            if reads == "text":
                s_ = '"' + bytes(v for v in vals).split(b"\0")[0].decode("latin1") + '"'
                g.t(vx, b(yy), s_, S, LIT, MONO)
            elif reads == "number":              # a count: in decimal, as the source writes it
                g.t(vx, b(yy), str(word), S, VELLUM, MONO)
            elif reads == "pointer":
                ink_w = region(word)
                if ink_w:
                    vx = C.addr(g, C.link_arrow(g, vx, yy), yy, word, ink_w)
                    nm = code_name(word) if ink_w == C_ else None
                    if nm:
                        g.t(vx + 10, b(yy), nm, S, VELLUM, MONO)
                    elif word in DESCR:
                        room = int((x + C.CW - 24 - vx - 10) // A)
                        t_ = '"%s"' % DESCR[word]
                        g.t(vx + 10, b(yy), t_ if len(t_) <= room else t_[:room - 1] + "…", S, LIT, MONO)
                else:                    # no longer an address: a number, no arrow, no name
                    g.t(vx, b(yy), "0x%x" % word, S, VELLUM, MONO)
            rx = vx + 200
        for k in range(8):               # what no field covers: loose
            if k not in covered:
                v = mem.get(a0 + k)
                g.t(bx(k), b(yy), "%02x" % v, S, mix(DIM, PAGE, 0.6), MONO)
        if r in differs:
            bar(g, seam, yy, BRIGHT)
    if span:                             # the frame's span, as the stack's: a bracket 14 in, its name left of it
        name, r0, r1 = span
        bx_, ya, yb = x + C.CW - 14, ys[r0] + 3, ys[r1] + ROW - 3
        g.rect(bx_ - 1, ya, 2, yb - ya, GOLD)
        g.rect(bx_ - 7, ya, 8, 2, GOLD)
        g.rect(bx_ - 7, yb - 2, 8, 2, GOLD)
        g.t(bx_ - 14, (ya + yb) / 2 + 6, name, 18, BRIGHT, "sansm", "end")
    return ys


PLAIN_ROWS = [(0x7fffffffecb0, []), (0x7fffffffecb8, []), (0x7fffffffecc0, []),
              (0x7fffffffecc8, [(4, 4, "number", "i")]), (0x7fffffffecd0, [(0, 8, "text", "buf[0..7]")]),
              (0x7fffffffecd8, [(0, 8, "text", "buf[8..15]")]), (0x7fffffffece0, [(0, 8, "pointer", "saved rbp")]),
              (0x7fffffffece8, [(0, 8, "pointer", "return")]), (0x7fffffffecf0, []), (0x7fffffffecf8, [])]
ASAN_ROWS = [(FAKE, [(0, 8, "bytes", "magic")]), (FAKE + 8, [(0, 8, "pointer", "descr")]),
             (FAKE + 0x10, [(0, 8, "pointer", "pc")]), (FAKE + 0x18, [(0, 8, "pointer", "real stack")]),
             (FAKE + 0x20, [(0, 8, "text", "buf[0..7]")]), (FAKE + 0x28, [(0, 8, "text", "buf[8..15]")]),
             (FAKE + 0x30, [(0, 8, "bytes", "redzone")]), (FAKE + 0x38, [(0, 8, "bytes", "redzone")]),
             (0x7fffffffecc0, [(0, 8, "pointer", "saved rbp")]), (0x7fffffffecc8, [(0, 8, "pointer", "return")])]
SHADOW = {0: 0xf1, 1: 0xf1, 2: 0xf1, 3: 0xf1, 4: 0, 5: 0, 6: 0xf3, 7: 0xf3}


def plain_dump(g, i, stored, struck=None, held=(), note="fill's frame as bytes, eight a row"):
    letters = {0x7fffffffecd0 + k for k in range(16, stored)}
    return frame_dump(g, LEFT, 612, "plain · stack", note, PLAIN_ROWS, plain_mem(stored, i), plain_region,
                      pins={6: ["rbp", "rsp"]}, struck=struck, held=held, letters=letters, span=("fill", 3, 7),
                      differs=(6, 7), seam=LEFT + 880 - 6)


def asan_dump(g, stored, read=False):
    return frame_dump(g, RIGHT, 612, "asan · fake frame", "its shadow at addr >> 3 + 7fff8000 · then the real "
                      "stack's way back", ASAN_ROWS, asan_mem(stored), asan_region, pins={8: ["rbp"]}, shadow=SHADOW,
                      read_shadow=6 if read else None, span=("fill", 0, 9), rule_after=7, differs=(6, 7),
                      seam=RIGHT + 2)


def frame(name, i):
    """The pair at the loop, the frame as bytes in each column. i = 15: both builds at the line's store (plain)
    and its check (asan). i = 16: the step in which they part: plain's store struck into the saved rbp; asan's
    check read f3."""
    gen.W, gen.H = 1920, 1080
    g = Svg()
    part = i == 16
    source_pair(g, 4, 4)
    # ---- the left column: plain ----
    at_side(LEFT)
    pn = {0x401155: ("chain", [(0x7fffffffece0 if part else 0x7fffffffecdf, S_)],
                     ("int", 0x41) if part else ("int", 0), {"end": "strike"} if part else None)}
    ps = {0x401155: "cur", 0x401151: "trail0", 0x40114f: "trail1", 0x40114b: "trail2"}
    C.disasm(g, 212, rows_of(PLAIN, 0x401144, 9, ps, pn), "plain · fill", 4)
    regs(g, LEFT, 472, "plain · registers",
         [("rax", [], ("int", i)), ("rbp", [(0x7fffffffece0, S_), (0x7fffffffed41 if part else 0x7fffffffed00, S_)], None),
          ("rsp", [(0x7fffffffece0, S_)], None), ("rip", [(0x401155, C_, "fill+21")], None)],
         {"rax": {0: "tick"}})
    plain_dump(g, i, i + 1 if part else i, struck=0x7fffffffece0 if part else None,
               held={0x7fffffffecdf, 0x7fffffffeccc} if part else {0x7fffffffecde, 0x7fffffffeccc})
    # ---- the right column: asan ----
    at_side(RIGHT)
    an = {0x523d38: ("chain", [(0xf807eb16006 if part else 0xf807eb16005, D_)], ("int", 0xf3 if part else 0),
                     {0: "read"} if part else None),
          0x523d34: ("set", "rax", [], ("int", (FAKE + 0x20 + i) >> 3))}
    ast = {0x523d38: "cur", 0x523d34: "trail0", 0x523d31: "trail1", 0x523d2e: "trail2"}
    if part:
        an[0x523d41] = ("runs", [("f3", VELLUM), (" : ", FAINT), ("0", VELLUM), ("  ", FAINT), ("zf", DIM)])
        an[0x523d43] = ("runs", [("not taken", DIM)])
    at = C.disasm(g, 212, rows_of(ASAN, 0x523d26, 9, ast, an), "asan · fill", 4)
    for a, yy in at.items():
        if a in ONLY:
            bar(g, RIGHT + 2, yy, DIM)          # its seam is on its left
    regs(g, RIGHT, 472, "asan · registers",
         [("rax", [], ("int", (FAKE + 0x20 + i) >> 3)), ("rbx", [(0x7fffffffec20, S_), (FAKE + 0x20 + i, D_)], None),
          ("rcx", [], ("int", i)), ("rip", [(0x523d38, C_, "fill+216")], None)], {"rax": {0: "tick"}})
    asan_dump(g, 16 if part else 15, read=part)
    clock(g, 17 if part else 16, "i %d · moments %d / %d" % (i, 164 if part else 156, 770 if part else 753))
    if part:
        gen.caption(g, [[("i = 16, one past ",), ("buf", "mono"), (": plain writes ",), ("41", "mono"),
                         (", an ",), ("'A'", "mono"), (", into the saved rbp.",)],
                        [("ASan reads the shadow first: ",), ("f3", "mono"), (", a redzone. It does not store.",)]])
    else:
        gen.caption(g, [[("Each turn is one more byte: ",), ("41", "mono"), (", the letter ",), ("A", "mono"),
                         (". Fifteen in ",), ("buf", "mono"), (",",)],
                        [("on both sides; ASan read ",), ("00", "mono"), (" in the shadow before each one.",)]])
    g.save(name)


def clock(g, steps, text):
    g.rect(64, 899, 1792, 2, RULE)
    g.rect(64, 898, 1792 * steps / 33, 4, GOLD)
    g.t(1648, 934, text, S, FAINT, MONO, "end")


REPORT = [("==1438870==ERROR: AddressSanitizer: stack-buffer-overflow on address 0x7bfff58f0030 at pc 0x000000523d5b",
           "stack-buffer-overflow"),
          ("WRITE of size 1 at 0x7bfff58f0030 thread T0", "WRITE of size 1"),
          ("Address 0x7bfff58f0030 is located in stack of thread T0 at offset 48 in frame", None),
          ("  This frame has 1 object(s):", None),
          ("    [32, 48) 'buf' (line 3) <== Memory access at offset 48 overflows this variable", "[32, 48) 'buf'"),
          ("=>0x7bfff58f0000: f1 f1 f1 f1 00 00[f3]f3 00 00 00 00 00 00 00 00", "[f3]")]


def report(g, x, y, lines):
    """A run's stderr, as written: a panel of its lines, the ones the narration names framed; cut at its edge."""
    y0 = C.panel(g, x, y, C.CW, len(lines), "asan · stderr", note="its report, as written · exit 1")
    room = (C.CW - 24) // A
    for k, (t, mark) in enumerate(lines):
        yy = y0 + k * ROW
        s_ = t if len(t) <= room else t[:room - 1] + "…"
        g.t(x + 12, b(yy), s_, S, VELLUM, MONO)
        if mark and mark in s_:
            c = s_.index(mark)
            g.frame(x + 12 + c * A, yy, len(mark) * A, ROW, BRIGHT, 2)


def frame_ret(name):
    """Mock 38: i = 27, plain alone: its store struck into the return address's fourth byte, which now reads
    0x41414141, no longer an address; ASan stopped at i = 16, its report in place of its registers."""
    gen.W, gen.H = 1920, 1080
    g = Svg()
    source_pair(g, 4, 4)
    at_side(LEFT)
    pn = {0x401155: ("chain", [(0x7fffffffeceb, S_)], ("int", 0x41), {"end": "strike"})}
    ps = {0x401155: "cur", 0x401151: "trail0", 0x40114f: "trail1", 0x40114b: "trail2"}
    C.disasm(g, 212, rows_of(PLAIN, 0x401144, 9, ps, pn), "plain · fill", 4)
    regs(g, LEFT, 472, "plain · registers",
         [("rax", [], ("int", 27)), ("rbp", [(0x7fffffffece0, S_)], ("int", 0x4141414141414141)),
          ("rsp", [(0x7fffffffece0, S_)], None), ("rip", [(0x401155, C_, "fill+21")], None)],
         {"rax": {0: "tick"}})
    plain_dump(g, 27, 28, struck=0x7fffffffeceb, held={0x7fffffffece8, 0x7fffffffece9, 0x7fffffffecea, 0x7fffffffeccc})
    # ---- asan: stopped at i = 16. Its disassembly's upper rows, down to the call; its report in place of the rest
    at_side(RIGHT)
    ar = rows_of(ASAN, 0x523d4b, 5, {0x523d56: "trail0", 0x523d53: "trail1", 0x523d51: "trail2"},
                 {0x523d56: ("text", "never returned", DIM)})
    at = C.disasm(g, 212, ar, "asan · fill · stopped", 4)
    for a, yy in at.items():
        if a in ONLY:
            bar(g, RIGHT + 2, yy, DIM)
    report(g, RIGHT, 376, REPORT)
    asan_dump(g, 16)
    clock(g, 28, "i 27 · moment 252 · asan exited")
    gen.caption(g, [[("i = 27: the return address is half ",), ("41", "mono"), ("s, and reads",)],
                    [("41414141", "mono"), (", no longer an address. ASan stopped at i = 16.",)]])
    g.save(name)


def sheet_store(name):
    """Mock 27: one turn of the loop, row by row, the plain build's rows facing their counterparts in the ASan
    build's; the rows only ASan has barred on the seam side, the plain side's page facing them. i = 3. Under
    each, its buf as bytes: the turn's one byte struck, the letter it is."""
    gen.W, gen.H = 1920, 1080
    g = Svg()
    face = {0x40114b: 0x523d1e, 0x40114f: 0x523d22, 0x401151: 0x523d2a, 0x401155: 0x523d5e, 0x40115a: 0x523d61,
            0x40115d: 0x523d64, 0x401160: 0x523d67, 0x401163: 0x523d6a}
    order = [a for a, *_ in ASAN]
    notes = {0x523d26: ("chain", [(FAKE + 0x20, D_)], None, None),
             0x523d2e: ("set", "rax", [(FAKE + 0x23, D_)], ("str", "AAA")),
             0x523d34: ("set", "rax", [], ("int", (FAKE + 0x23) >> 3)),
             0x523d38: ("chain", [(0xf807eb16004, D_)], ("int", 0), None),
             0x523d41: ("runs", [("0", VELLUM), (" : ", FAINT), ("0", VELLUM), ("  ", FAINT), ("ZF", "chip")]),
             0x523d43: ("runs", [("taken", VELLUM), (" · reads ZF", FAINT)]),
             0x523d5e: ("chain", [(FAKE + 0x23, D_)], ("int", 0x41), None)}
    skipped = {0x523d45, 0x523d48, 0x523d4b, 0x523d4f, 0x523d51, 0x523d53, 0x523d56}   # the partial check: not run at 00
    ar = [(a, ln, mn, [((ops, NAMES[ops]) if ops in NAMES else ops, C_)] if isinstance(ops, int) else ([(ops, P)] if ops else []),
           "skip" if a in skipped else None, notes.get(a)) for a, ln, mn, ops in ASAN]
    at_side(RIGHT)
    y = 96
    at = C.disasm(g, y, ar, "asan · line 4, one turn · 17 instructions", 4)
    for a, yy in at.items():
        if a in ONLY:
            bar(g, RIGHT + 2, yy, DIM)
    # the plain side: each row at its counterpart's height; page where it has none
    at_side(LEFT)
    C.tag(g, LEFT, y, 880, ph(len(ASAN)), "plain · line 4, one turn · 8 instructions",
          note="each row facing its counterpart")
    pnotes = {0x401151: ("set", "rax", [], ("int", 3)),
              0x401155: ("chain", [(0x7fffffffecd3, S_)], ("int", 0x41), None)}
    for a, ln, mn, ops in PLAIN:
        if a not in face:
            continue
        parts = [(ops, C_)] if isinstance(ops, int) else [(ops, P)]
        C.insn_rows(g, at[face[a]], [(a, ln if a == 0x40114b else None, mn, parts, None, pnotes.get(a))], 4, lead=4)
    g.t(LEFT + 400, at[0x523d34] + 18, "the check: plain has nothing here", 18, FAINT, "sans")
    # the turn's one byte, in each build's buf: the store struck, the reading one letter longer
    frame_dump(g, LEFT, 740, "plain · buf", "the turn's byte", PLAIN_ROWS[4:6], plain_mem(4, 3), plain_region,
               struck=0x7fffffffecd3)
    frame_dump(g, RIGHT, 740, "asan · buf", "its shadow, read before the store", ASAN_ROWS[4:6], asan_mem(4),
               asan_region, shadow={0: 0, 1: 0}, read_shadow=0, struck=FAKE + 0x23)
    gen.caption(g, [[("Plain: one store. ASan: the address, its shadow byte (",), ("addr >> 3", "mono"),
                     (" + ",), ("7fff8000", "mono"), ("),", )],
                    [("a compare with 0, a branch; the partial check and the report only if it is not 0.",)]])
    g.save(name)


if __name__ == "__main__":
    frame("26-pair-rest", 15)
    sheet_store("27-pair-store")
    frame("28-pair-part", 16)
    frame_ret("38-pair-ret")