diff options
Diffstat (limited to 'design/mock/pair.py')
| -rw-r--r-- | design/mock/pair.py | 311 |
1 files changed, 260 insertions, 51 deletions
diff --git a/design/mock/pair.py b/design/mock/pair.py index 1311b95..3adb60d 100644 --- a/design/mock/pair.py +++ b/design/mock/pair.py @@ -102,12 +102,188 @@ def source_pair(g, cur_l, cur_r, waiting_r=False): return ys -def frame(name, part): - """The pair at the loop. part=False: i = 15, both builds at the line's store (plain) and its check (asan). - part=True: i = 16, the step in which they part: plain's store struck into the saved rbp; asan's check read f3.""" +# ---- 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() - i = 16 if part else 15 + part = i == 16 source_pair(g, 4, 4) # ---- the left column: plain ---- at_side(LEFT) @@ -119,16 +295,8 @@ def frame(name, part): [("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"}}) - sv = 0x7fffffffed41 if part else 0x7fffffffed00 - prow = [(0x7fffffffecb0, [], [], ("int", 0)), (0x7fffffffecb8, [], [], ("int", 0)), - (0x7fffffffecc0, [], [], ("int", 0)), (0x7fffffffecc8, [], [], ("int", i << 32)), - (0x7fffffffecd0, [], [], ("str", "AAAAAAAA")), (0x7fffffffecd8, [], [], ("str", "AAAAAAA" if not part else "AAAAAAAA")), - (0x7fffffffece0, ["rbp", "rsp"], [(sv, S_)], None) + (({1: "strike"},) if part else ()), - (0x7fffffffece8, [], [(0x401134, C_, "main+20")], None), - (0x7fffffffecf0, [], [], ("int", 0)), (0x7fffffffecf8, [], [], ("int", 0))] - py = C.stack(g, 612, prow, [("fill", 3, 7)], note="red zone, buf, the frame · rsp at +0", rsp=0x7fffffffece0, - roles={3: ("i, high half", VELLUM), 4: ("buf[0..7]", VELLUM), 5: ("buf[8..15]", VELLUM), - 6: ("saved rbp", DIM), 7: ("return", DIM)}, title="plain · stack") + 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), @@ -138,53 +306,89 @@ def frame(name, part): if part: an[0x523d41] = ("runs", [("f3", VELLUM), (" : ", FAINT), ("0", VELLUM), (" ", FAINT), ("zf", DIM)]) an[0x523d43] = ("runs", [("not taken", DIM)]) - ar = rows_of(ASAN, 0x523d26, 9, ast, an) - at = C.disasm(g, 212, ar, "asan · fill", 4) + 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"}}) - arow = [(FAKE + 0x00, [], [], ("int", 0x41b58ab3)), (FAKE + 0x08, [], [(0x5387a7, VELLUM)], ("str", "1 32 16 5 buf:3")), - (FAKE + 0x10, [], [(0x523c60, C_, "fill")], None), (FAKE + 0x18, [], [(0x7fffffffec10, S_)], None), - (FAKE + 0x20, [], [], ("str", "AAAAAAAA")), (FAKE + 0x28, [], [], ("str", "AAAAAAA" if not part else "AAAAAAAA")), - (FAKE + 0x30, [], [], ("int", 0)), (FAKE + 0x38, [], [(0x7bfff58e8000, D_)], None)] - ay = C.stack(g, 612, arow + [(0x7fffffffecc0, ["rbp"], [(0x7fffffffece0, S_)], None), - (0x7fffffffecc8, [], [(0x523c54, C_, "main+20")], None)], - [("fill", 0, 9)], note="the fake frame, its shadow · the real stack's way back", rsp=None, - roles={0: ("magic", DIM), 1: ("descr", DIM), 2: ("pc", DIM), 3: ("real stack", DIM), - 4: ("buf[0..7]", VELLUM), 5: ("buf[8..15]", VELLUM), 6: ("redzone", DIM), 7: ("redzone", DIM), - 8: ("saved rbp", DIM), 9: ("return", DIM)}, title="asan · fake stack", - ink_of=lambda a: S_ if a >= 0x7ffffffde000 else D_) - sh = [0xf1, 0xf1, 0xf1, 0xf1, 0, 0, 0xf3, 0xf3] - ys = sorted(ay.values()) if isinstance(ay, dict) else ay - base_y = 612 + TAG + PAD - for k, v in enumerate(sh): - shadow_cell(g, RIGHT + 88, base_y + k * ROW, v, read=part and k == 6) - # the real stack is another region: a rule before its two rows - g.rect(RIGHT + 2, base_y + 8 * ROW, C.CW - 4, 2, RULE) - # `differs`: the rows that face each other across the seam and hold different things (buf's end: saved rbp vs redzone) - for k in (6, 7): - bar(g, LEFT + 880 - 6, base_y + k * ROW, BRIGHT if part or k == 6 else BRIGHT) - bar(g, RIGHT + 2, base_y + k * ROW, BRIGHT) - # the clock: one rule, each run's moment - g.rect(64, 899, 1792, 2, RULE) - g.rect(64, 898, 1792 * (17 if part else 16) / 33, 4, GOLD) - g.t(1648, 934, "i %d · moments %d / %d" % (i, 164 if part else 156, 770 if part else 753), S, FAINT, MONO, "end") + 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 ",), ("'A'", "mono"), - (" into the saved rbp. ASan reads",)], - [("the shadow first: ",), ("f3", "mono"), (", a redzone. Its branch to the store is not taken.",)]]) + 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, [[("The same line, two builds: plain stores the byte; ASan first",)], - [("reads the byte of shadow that stands for it: ",), ("00", "mono"), (", so it may.",)]]) + 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.""" + 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, @@ -217,8 +421,12 @@ def sheet_store(name): 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) - gaps = [a for a in order if a in ONLY and a not in skipped] 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.",)]]) @@ -226,6 +434,7 @@ def sheet_store(name): if __name__ == "__main__": - frame("26-pair-rest", False) + frame("26-pair-rest", 15) sheet_store("27-pair-store") - frame("28-pair-part", True) + frame("28-pair-part", 16) + frame_ret("38-pair-ret") |
