summaryrefslogtreecommitdiff
path: root/design/mock/heap.py
diff options
context:
space:
mode:
Diffstat (limited to 'design/mock/heap.py')
-rw-r--r--design/mock/heap.py75
1 files changed, 66 insertions, 9 deletions
diff --git a/design/mock/heap.py b/design/mock/heap.py
index e4c3e3a..57cd7e3 100644
--- a/design/mock/heap.py
+++ b/design/mock/heap.py
@@ -165,7 +165,7 @@ def loupe(g, x, y, inst, changed):
for s_, ink in [("tcache", DIM), (" → ", FAINT), ("405070", HEAP)]:
g.t(xx, y + 18, s_, S, ink, MONO)
xx += len(s_) * A
- g.t(x + CW - 10, y + 18, "tcache_perthread_struct · 0x2f0 bytes", 18, DIM, "sans", "end")
+ g.t(x + CW - 10, y + 18, "tcache_perthread_struct · 0x2f8 bytes", 18, DIM, "sans", "end")
slots, head = TC[inst]
base = 0x405070
mem = {base + i: v for i, v in enumerate(bytes([slots, 0] + [0x10, 0] * 7))}
@@ -173,9 +173,8 @@ def loupe(g, x, y, inst, changed):
mem.update({base + 0xa0 + i: 0 for i in range(16)})
fl = [(0, 2, "number", False, None, "slots left · %d in the bin" % (16 - slots)),
(0x98, 8, "pointer", False, None, "the head of 0x20")]
- C.fields(g, y, h, "", base, 0x2f0, fl, mem, HEAP, region, framed=[(0, 2), (0x98, 8)], bare=True, x=x,
+ C.fields(g, y, h, "", base, 0x2f8, fl, mem, HEAP, region, framed=[(0, 2), (0x98, 8)], bare=True, x=x,
rows_shown=[0, 9, 10], changed=changed)
- g.rect(RX + 34 - 2, y - 4, 6 * A + 4, 4, HEAP) # the stem: a bracket under the row's address, on the tag
INSTANTS = [
@@ -183,8 +182,8 @@ INSTANTS = [
["malloc(16) cut a chunk of 0x20 off the top: its size word, 0x21,",
"then the 16 bytes rax points at. The top is 0x20 smaller."]),
("B", "2", "three chunks", "stop 6, after push(9)",
- ["Three pushes, three chunks in a row, each with P: the chunk",
- "before it is in use. The list's next pointers are arcs in the bytes."]),
+ ["Two more pushes, at a run: three chunks in a row, each with P",
+ "set, the chunk before it in use. Settled: no marks, the arcs standing."]),
("C", "3", "a free", "stop 7, back from free in the first pop",
["free gave 9's chunk to the tcache, malloc's bin for 0x20. Its first",
"word is the link now: 405, which is NULL, safe-linked."]),
@@ -193,7 +192,7 @@ INSTANTS = [
"chunk's own address shifted 12. The arc is the pointer it hides."]),
("E", "5", "malloc takes it back", "stop 9, back from malloc in push(25)",
["malloc(16) took the bin's head, 405030: the tcache points at 405050",
- "again, the key is wiped, and the old link stays until push writes."]),
+ "again, the key is wiped, the old link stays."]),
]
CHANGED = { # the bytes each stop changed, of those on screen: struck
"C": {0x405050, 0x405051} | set(range(0x405058, 0x405060)) | {0x405068, 0x405069, 0x40506a},
@@ -215,15 +214,73 @@ def storyboard(name):
drawing(g, 48, 296, inst)
C.fields(g, 48, 200, "the heap, 405000 to 40506f", BASE, 0x70, chunk_fields(inst), MEM[inst], HEAP, region,
rows_shown=list(range(7)), more_rows=False, changed=CHANGED.get(inst, ()),
- held_at={0x405050, 0x405058} if inst == "B" else (), touched={0x405030} if inst == "D" else ())
- if inst in TC: # hung from the view's last row: the chunk that is the tcache
- loupe(g, RX + 34 - 92, 48 + TAG + PAD + 7 * ROW + 2, inst, LOUPE_CHANGED[inst])
+ touched={0x405030} if inst == "D" else ()) # 2 is a run's end: settled, no marks
+ # the tcache's loupe lies over the stack, never over the registers: in 18-heap-free, not on this sheet
for i, line in enumerate(cap):
g.t(LX, 440 + i * 44, line, 32, VELLUM, "sans")
g.o.append("</g>")
g.save(name)
+# ---- beat 3 whole: the first free, mid-step, in the working frame (`subject`) ---------------------------------
+H_, S_, C_, D_ = HEAP, C.STACK, C.CODE, DATA
+SRC = open(__file__.replace("heap.py", "heap.c")).read().split("\n")
+RBP = [(0x7fffffffed00, S_), (0x7fffffffedb0, S_), (0x7fffffffee10, S_)]
+REGS = [("rax", [], ("int", 0)), ("rbx", [], ("int", 0)), ("rcx", [], ("int", 0x405)), ("rdx", [], ("int", 0xf)),
+ ("rsi", [], ("int", 0xe2263c1be1246b56)), ("rdi", [(0x405050, H_)], ("int", 0x405)),
+ ("rbp", [(0x7fffffffecf0, S_)] + RBP, ("int", 0)), ("rsp", [(0x7fffffffece0, S_)], ("int", 0)),
+ ("r8", [(0x405070, H_)], ("int", 0x10001000100f)), ("r9", [], ("int", 0x20)), ("r10", [], ("int", 0x21)),
+ ("r11", [(0x7ffff7e19ac8, D_)], None), ("r12", [(0x7fffffffee38, S_), (0x7fffffffefea, S_)], ("str", "./heap")),
+ ("r13", [], ("int", 1)), ("r14", [(0x7ffff7ffd000, D_), (0x7ffff7ffe3c0, D_)], ("int", 0)),
+ ("r15", [(0x403df0, D_), (0x401110, C_, "__do_global_dtors_aux")], None),
+ ("rip", [(0x4011b6, C_, "pop+46")], None)]
+FLAGS = [("CF", 0, 0), ("PF", 1, 0), ("AF", 0, 0), ("ZF", 0, 0), ("SF", 0, 0), ("IF", 1, 0), ("DF", 0, 0), ("OF", 0, 0)]
+STACK = [(0x7fffffffece0, ["rsp"], [], ("int", 0)), (0x7fffffffece8, [], [(0x405050, H_)], ("int", 0x405)),
+ (0x7fffffffecf0, ["rbp"], RBP, ("int", 0)), (0x7fffffffecf8, [], [(0x4011e0, C_, "main+39")], None),
+ (0x7fffffffed00, [], RBP[1:], ("int", 0)), (0x7fffffffed08, [], [(0x7ffff7c27781, C_, "libc+27781")], None)]
+P = VELLUM
+DIS = [(0x40119b, 19, "mov", "rax, qword ptr [rbp - 8]", None, None),
+ (0x40119f, 19, "mov", "rax, qword ptr [rax + 8]", None, None),
+ (0x4011a3, 19, "mov", "qword ptr [rip + 0x2e7e], rax", None, ("text", "head", DATA)),
+ (0x4011aa, 20, "mov", "rax, qword ptr [rbp - 8]", "trail1", None),
+ (0x4011ae, 20, "mov", "rdi, rax", "trail0", None),
+ (0x4011b1, 20, "call", (0x401030, "free@plt"), "cur", ("runs", [("free", BRIGHT), (" in libc, stepped over", FAINT)])),
+ (0x4011b6, 21, "nop", "", None, ("set", "rdi", [(0x405050, H_)], ("int", 0x405))),
+ (0x4011b7, 21, "leave", "", None, None), (0x4011b8, 21, "ret", "", None, None)]
+
+
+def frame_free(name):
+ """The first free in the working frame: the register wave struck, the chunk's words struck on the bytes that
+ changed, the node gone down to the bin's row, and the loupe on the tcache over the stack, never the registers."""
+ gen.W, gen.H = 1920, 1080
+ g = Svg()
+ sp = C.SUBJECT
+ y = 48 + sp["band"] + 12
+ ys = C.source(g, y, sp["src"], SRC, 14, 20, [19, 18], "heap.c", 31)
+ at = C.disasm(g, y + ph(sp["src"]) + 12,
+ [(a, ln, mn, [(ops, C.CODE)] if isinstance(ops, tuple) else ([(ops, P)] if ops else []), st, note)
+ for a, ln, mn, ops, st, note in DIS], "pop", 20)
+ C.thread(g, ys, [(at[0x4011aa], at[0x4011b1] + ROW)])
+ yr = 48 + sp["view"] + 12
+ C.registers(g, yr, REGS, FLAGS, {r: {0: "strike"} for r in ("rax", "rcx", "rdx", "rsi", "r8", "r10")})
+ C.stack(g, yr + ph(17) + 12, STACK, [("pop", 0, 3), ("main", 4, 5)], rsp=0x7fffffffece0,
+ roles={1: ("n", VELLUM), 2: ("saved rbp", DIM), 4: ("saved rbp", DIM)})
+ drawing(g, 48, sp["band"], "C")
+ rows_y = C.fields(g, 48, sp["view"], "the heap, 405000 to 40506f", BASE, 0x70, chunk_fields("C"), MEM["C"], HEAP,
+ region, rows_shown=list(range(7)), more_rows=False, changed=CHANGED["C"])
+ ly = 888 - ph(5) # over the stack, clamped to the stage
+ loupe(g, RX, ly, "C", LOUPE_CHANGED["C"])
+ foot = rows_y[0x405060] + ROW + 2 # the stem: from under the row's address, down the gutter's right lane
+ g.rect(RX + 34 - 2, foot - 2, 6 * A + 4, 4, HEAP)
+ gen.arrow(g, [(RX + 32, foot), (962, foot), (962, ly + 12), (RX - 1, ly + 12)], HEAP, 3, r=8,
+ hl=10, hw=10)
+ C.clock(g, 7, 12)
+ gen.caption(g, [[("free gave 9's chunk to the tcache, malloc's bin for ",), ("0x20", "mono"), (". Its first",)],
+ [("word is the link now: ",), ("405", "mono"), (", which is NULL, safe-linked.",)]])
+ g.save(name)
+
+
if __name__ == "__main__":
assert 0x405455 ^ (0x405030 >> 12) == 0x405050 and 0x405 ^ (0x405050 >> 12) == 0 # safe-linking, as stored
storyboard("18-heap")
+ frame_free("18-heap-free")