From 2a97030c9743d4680d698414c28e8d8739614215 Mon Sep 17 00:00:00 2001 From: Gabriel Schneider Date: Tue, 10 Nov 2026 23:25:40 -0300 Subject: design: down is deeper; a call followed down; operands without size words; the heap as a scene From the user. Direction and nearness are rules: the idea, the source, its instructions, the callee and the kernel go down the frame, a call goes down and a return comes up, and every tie runs one of those ways. The disassembly can descend: a window a call depth, the caller keeping three rows with its call waiting, a PLT stub its jump, the kernel at the bottom on vellum, with verbs descend, cross and ascend. The kernel leaves the band, which keeps the calls so far as strace writes them; the backtrace has its newest frame at the bottom. Size words are dropped from operands and a width is shown as byte cells where nothing else says it; a compare shows its values and flags, a jump the flags it reads, an indirect call its target and the slot it was read from. libc's heap is a scene of blocks that exist (a dump by fields with a size and a protected pointer read, arcs as the free list, a loupe into the tcache, the list drawn over malloc's own), storyboarded on glibc 2.44. Mocks 16 to 18. Co-Authored-By: Claude Fable 5.2 Claude-Session: https://claude.ai/code/session_016Y9MNCKgYCGiANnEwcEQxa --- design/mock/09-context-480.png | Bin 36400 -> 36022 bytes design/mock/09-context.png | Bin 235217 -> 232404 bytes design/mock/09-context.svg | 76 +- design/mock/10-context-step-480.png | Bin 37253 -> 36905 bytes design/mock/10-context-step.png | Bin 239232 -> 236408 bytes design/mock/10-context-step.svg | 76 +- design/mock/11-context-lens-480.png | Bin 36630 -> 36592 bytes design/mock/11-context-lens.png | Bin 227838 -> 225223 bytes design/mock/11-context-lens.svg | 74 +- design/mock/12-calls-subject-480.png | Bin 33189 -> 33319 bytes design/mock/12-calls-subject.png | Bin 201794 -> 199751 bytes design/mock/12-calls-subject.svg | 66 +- design/mock/13-sqlite-row-480.png | Bin 32488 -> 33082 bytes design/mock/13-sqlite-row.png | Bin 227680 -> 228295 bytes design/mock/13-sqlite-row.svg | 36 +- design/mock/14-sqlite-page-480.png | Bin 37621 -> 34451 bytes design/mock/14-sqlite-page.png | Bin 236738 -> 226521 bytes design/mock/14-sqlite-page.svg | 162 ++-- design/mock/15-marks.png | Bin 265080 -> 261186 bytes design/mock/15-marks.svg | 24 +- design/mock/16-descent.png | Bin 0 -> 288435 bytes design/mock/16-descent.svg | 733 +++++++++++++++++ design/mock/17-operands.png | Bin 0 -> 118317 bytes design/mock/17-operands.svg | 163 ++++ design/mock/18-heap-480.png | Bin 0 -> 75720 bytes design/mock/18-heap.png | Bin 0 -> 450494 bytes design/mock/18-heap.svg | 1460 ++++++++++++++++++++++++++++++++++ design/mock/19-map-1080.png | Bin 1021707 -> 392428 bytes design/mock/19-map-480.png | Bin 45442 -> 45447 bytes design/mock/19-map.png | Bin 908493 -> 896620 bytes design/mock/19-map.svg | 66 +- design/mock/20-dive.png | Bin 1339861 -> 1320934 bytes design/mock/21-loupe-480.png | Bin 38432 -> 38096 bytes design/mock/21-loupe.png | Bin 246423 -> 243732 bytes design/mock/21-loupe.svg | 76 +- design/mock/context.py | 462 +++++++++-- design/mock/heap.c | 31 + design/mock/heap.py | 229 ++++++ design/mock/map.py | 2 +- 39 files changed, 3302 insertions(+), 434 deletions(-) create mode 100644 design/mock/16-descent.png create mode 100644 design/mock/16-descent.svg create mode 100644 design/mock/17-operands.png create mode 100644 design/mock/17-operands.svg create mode 100644 design/mock/18-heap-480.png create mode 100644 design/mock/18-heap.png create mode 100644 design/mock/18-heap.svg create mode 100644 design/mock/heap.c create mode 100644 design/mock/heap.py (limited to 'design/mock') diff --git a/design/mock/09-context-480.png b/design/mock/09-context-480.png index 3e5996b..d20bfa0 100644 Binary files a/design/mock/09-context-480.png and b/design/mock/09-context-480.png differ diff --git a/design/mock/09-context.png b/design/mock/09-context.png index 41cd918..d7ad775 100644 Binary files a/design/mock/09-context.png and b/design/mock/09-context.png differ diff --git a/design/mock/09-context.svg b/design/mock/09-context.svg index 4000a2d..a67fd37 100644 --- a/design/mock/09-context.svg +++ b/design/mock/09-context.svg @@ -85,8 +85,8 @@ eax, eax 10019 1b -nop -dword ptr [rax + rax] +nop +[rax + rax] 10019 20 mov @@ -102,7 +102,7 @@ 26 21 mov -rdi, qword ptr [rdi + 8] +rdi, [rdi + 8] 1004098 @@ -114,7 +114,7 @@ 2a 22 mov -qword ptr [rax + 8], rcx +[rax + 8], rcx 10019 2e 20 @@ -142,7 +142,7 @@ 10019 38 -endbr64 +endbr64 _init @@ -859,20 +859,20 @@ - - - - - - - - + + + + + + + + - - - - + + + + @@ -946,8 +946,8 @@ - - + + @@ -988,12 +988,12 @@ - + - + @@ -1012,8 +1012,8 @@ - - + + @@ -1037,8 +1037,8 @@ - - + + @@ -1052,7 +1052,7 @@ - + @@ -1060,7 +1060,7 @@ - + @@ -1087,7 +1087,7 @@ - + @@ -1096,7 +1096,7 @@ - + @@ -1118,7 +1118,7 @@ - + @@ -1134,20 +1134,20 @@ - + - + - - + + - + @@ -1160,7 +1160,7 @@ - + @@ -1173,7 +1173,7 @@ - + @@ -1999,7 +1999,7 @@ - + diff --git a/design/mock/10-context-step-480.png b/design/mock/10-context-step-480.png index 57090bb..8f92f8d 100644 Binary files a/design/mock/10-context-step-480.png and b/design/mock/10-context-step-480.png differ diff --git a/design/mock/10-context-step.png b/design/mock/10-context-step.png index fee3594..59d3169 100644 Binary files a/design/mock/10-context-step.png and b/design/mock/10-context-step.png differ diff --git a/design/mock/10-context-step.svg b/design/mock/10-context-step.svg index de52e22..b33a4d0 100644 --- a/design/mock/10-context-step.svg +++ b/design/mock/10-context-step.svg @@ -85,8 +85,8 @@ eax, eax 10019 1b -nop -dword ptr [rax + rax] +nop +[rax + rax] 10019 20 mov @@ -102,7 +102,7 @@ 26 21 mov -rdi, qword ptr [rdi + 8] +rdi, [rdi + 8] 1004098 @@ -115,7 +115,7 @@ 2a 22 mov -qword ptr [rax + 8], rcx +[rax + 8], rcx 10019 2e 20 @@ -143,7 +143,7 @@ 10019 38 -endbr64 +endbr64 _init @@ -871,20 +871,20 @@ - - - - - - - - + + + + + + + + - - - - + + + + @@ -958,8 +958,8 @@ - - + + @@ -1000,12 +1000,12 @@ - + - + @@ -1024,8 +1024,8 @@ - - + + @@ -1049,8 +1049,8 @@ - - + + @@ -1064,7 +1064,7 @@ - + @@ -1072,7 +1072,7 @@ - + @@ -1099,7 +1099,7 @@ - + @@ -1108,7 +1108,7 @@ - + @@ -1130,7 +1130,7 @@ - + @@ -1146,20 +1146,20 @@ - + - + - - + + - + @@ -1172,7 +1172,7 @@ - + @@ -1185,7 +1185,7 @@ - + @@ -2012,7 +2012,7 @@ - + diff --git a/design/mock/11-context-lens-480.png b/design/mock/11-context-lens-480.png index d4746ff..c5e6c0b 100644 Binary files a/design/mock/11-context-lens-480.png and b/design/mock/11-context-lens-480.png differ diff --git a/design/mock/11-context-lens.png b/design/mock/11-context-lens.png index 631e45c..c070e3e 100644 Binary files a/design/mock/11-context-lens.png and b/design/mock/11-context-lens.png differ diff --git a/design/mock/11-context-lens.svg b/design/mock/11-context-lens.svg index eae34f3..1190b7c 100644 --- a/design/mock/11-context-lens.svg +++ b/design/mock/11-context-lens.svg @@ -87,8 +87,8 @@ eax, eax 10019 1b -nop -dword ptr [rax + rax] +nop +[rax + rax] 10019 20 mov @@ -104,7 +104,7 @@ 26 21 mov -rdi, qword ptr [rdi + 8] +rdi, [rdi + 8] 1004098 @@ -117,7 +117,7 @@ 2a 22 mov -qword ptr [rax + 8], rcx +[rax + 8], rcx 10019 2e 20 @@ -886,20 +886,20 @@ - - - - - - - - + + + + + + + + - - - - + + + + @@ -973,8 +973,8 @@ - - + + @@ -1015,12 +1015,12 @@ - + - + @@ -1039,8 +1039,8 @@ - - + + @@ -1064,8 +1064,8 @@ - - + + @@ -1079,7 +1079,7 @@ - + @@ -1087,7 +1087,7 @@ - + @@ -1114,7 +1114,7 @@ - + @@ -1123,7 +1123,7 @@ - + @@ -1145,7 +1145,7 @@ - + @@ -1161,20 +1161,20 @@ - + - + - - + + - + @@ -1187,7 +1187,7 @@ - + @@ -1200,7 +1200,7 @@ - + @@ -2027,7 +2027,7 @@ - + diff --git a/design/mock/12-calls-subject-480.png b/design/mock/12-calls-subject-480.png index 2501db2..5224b43 100644 Binary files a/design/mock/12-calls-subject-480.png and b/design/mock/12-calls-subject-480.png differ diff --git a/design/mock/12-calls-subject.png b/design/mock/12-calls-subject.png index 61f349d..e2cb8f4 100644 Binary files a/design/mock/12-calls-subject.png and b/design/mock/12-calls-subject.png differ diff --git a/design/mock/12-calls-subject.svg b/design/mock/12-calls-subject.svg index 3fbf152..6e3cb1b 100644 --- a/design/mock/12-calls-subject.svg +++ b/design/mock/12-calls-subject.svg @@ -88,20 +88,20 @@ 10133 9a mov -qword ptr [rbp - 0x18], rax -7fffffffed88 - - -2 +[rbp - 0x18], rax +7fffffffed88 + + +2 10133 9e mov -rdi, qword ptr [rbp - 0x10] -7fffffffed90 - - -4 +rdi, [rbp - 0x10] +7fffffffed90 + + +4 10133 a2 @@ -126,7 +126,7 @@ 10133 ae mov -rax, qword ptr [rbp - 0x18] +rax, [rbp - 0x18] 10133 b2 add @@ -134,7 +134,7 @@ 10133 b5 mov -qword ptr [rbp - 8], rax +[rbp - 8], rax @@ -303,30 +303,30 @@ backtrace -the calls waiting · newest on top - - -#0 -fib+0 -n=2 -fib.c:3 -#1 -fib+59 -n=4 -fib.c:6 -#2 -fib+42 -n=5 -fib.c:6 +the calls waiting · newest at the bottom + + +#0 +fib+0 +n=2 +fib.c:3 +#1 +fib+59 +n=4 +fib.c:6 +#2 +fib+42 +n=5 +fib.c:6 #3 main+25 fib.c:10 -#4 -libc_start_main_stage2+45 -__libc_start_main.c:95 -#5 -_start_c+0 -crt1.c:15 +#4 +libc_start_main_stage2+45 +__libc_start_main.c:95 +#5 +_start_c+0 +crt1.c:15 diff --git a/design/mock/13-sqlite-row-480.png b/design/mock/13-sqlite-row-480.png index aacd128..a319f7d 100644 Binary files a/design/mock/13-sqlite-row-480.png and b/design/mock/13-sqlite-row-480.png differ diff --git a/design/mock/13-sqlite-row.png b/design/mock/13-sqlite-row.png index 3330d15..f66f6c5 100644 Binary files a/design/mock/13-sqlite-row.png and b/design/mock/13-sqlite-row.png differ diff --git a/design/mock/13-sqlite-row.svg b/design/mock/13-sqlite-row.svg index 101dd32..b538383 100644 --- a/design/mock/13-sqlite-row.svg +++ b/design/mock/13-sqlite-row.svg @@ -312,26 +312,26 @@ backtrace -newest on top - - -#0 -sqlite3BtreeInsert+4 -sqlite3.c:80895 -#1 -sqlite3VdbeExec+18905 -#2 -sqlite3_step+910 -#3 -sqlite3_exec+394 -#4 -main+56 +newest at the bottom + + +#0 +sqlite3BtreeInsert+4 +sqlite3.c:80895 +#1 +sqlite3VdbeExec+18905 +#2 +sqlite3_step+910 +#3 +sqlite3_exec+394 +#4 +main+56 #5 libc.so.6+27781 -#6 -__libc_start_main+137 -#7 -_start+37 +#6 +__libc_start_main+137 +#7 +_start+37 diff --git a/design/mock/14-sqlite-page-480.png b/design/mock/14-sqlite-page-480.png index aed0f9e..ab320b3 100644 Binary files a/design/mock/14-sqlite-page-480.png and b/design/mock/14-sqlite-page-480.png differ diff --git a/design/mock/14-sqlite-page.png b/design/mock/14-sqlite-page.png index 40739fd..101824d 100644 Binary files a/design/mock/14-sqlite-page.png and b/design/mock/14-sqlite-page.png differ diff --git a/design/mock/14-sqlite-page.svg b/design/mock/14-sqlite-page.svg index c22705b..5a8b676 100644 --- a/design/mock/14-sqlite-page.svg +++ b/design/mock/14-sqlite-page.svg @@ -121,10 +121,15 @@ 48 mov rcx, r14 + 4220 4b call -qword ptr [rip + 0xf1567] +[rip + 0xf1567] + + +__pwrite64 + = 4096 @@ -147,7 +152,7 @@ 4220 5e mov -ecx, dword ptr [rax] +ecx, [rax] @@ -325,121 +330,44 @@ unixWrite - - -kernel -the other side of a system call - -calls so far - - -pwrite64 -4096 - -pwrite64 -4096 - -fsync -0 - -pwrite64 -12 - -close -0 - -fsync -0 - -openat -5 - -fsync -0 -pwrite64 -system call 18 - -rdi -fd -3 -"t.db" - -rsi -buf -538b28 - -rdx -count -4096 - -r10 -offset -4096 - -rax -returns -4096 -the bytes it was given - - -0d - - -00 - - -00 - - -00 - - -03 - - -0f - - -e6 - - -00 - - -0f - - -f7 - - -0f - - -ef - - -0f - - -e6 - - -00 - - -00 - - -00 - - -00 - - -00 - - -00 + + +system calls +oldest on top · newest at the bottom +fsync +(…) = +0 +openat +(…) = +5 +fsync +(…) = +0 +close +(…) = +0 +pwrite64 +(…) = +12 +fsync +(…) = +0 +pwrite64 +(…) = +4096 +pwrite64 +(fd +3 + "t.db" +, buf +538b28 +, count +4096 +, offset +4096 +) = + +4096 diff --git a/design/mock/15-marks.png b/design/mock/15-marks.png index 5a68341..2462266 100644 Binary files a/design/mock/15-marks.png and b/design/mock/15-marks.png differ diff --git a/design/mock/15-marks.svg b/design/mock/15-marks.svg index 2c44a39..3048247 100644 --- a/design/mock/15-marks.svg +++ b/design/mock/15-marks.svg @@ -79,8 +79,8 @@ eax, eax 10019 1b -nop -dword ptr [rax + rax] +nop +[rax + rax] 10019 20 mov @@ -97,7 +97,7 @@ 26 21 mov -rdi, qword ptr [rdi + 8] +rdi, [rdi + 8] 1004098 @@ -110,7 +110,7 @@ 2a 22 mov -qword ptr [rax + 8], rcx +[rax + 8], rcx 10019 2e 20 @@ -420,8 +420,8 @@ eax, eax 10019 1b -nop -dword ptr [rax + rax] +nop +[rax + rax] 10019 20 mov @@ -438,7 +438,7 @@ 26 21 mov -rdi, qword ptr [rdi + 8] +rdi, [rdi + 8] 1004098 @@ -451,7 +451,7 @@ 2a 22 mov -qword ptr [rax + 8], rcx +[rax + 8], rcx 10019 2e 20 @@ -761,8 +761,8 @@ eax, eax 10019 1b -nop -dword ptr [rax + rax] +nop +[rax + rax] 10019 20 mov @@ -779,7 +779,7 @@ 26 21 mov -rdi, qword ptr [rdi + 8] +rdi, [rdi + 8] 1004098 @@ -792,7 +792,7 @@ 2a 22 mov -qword ptr [rax + 8], rcx +[rax + 8], rcx 10019 2e 20 diff --git a/design/mock/16-descent.png b/design/mock/16-descent.png new file mode 100644 index 0000000..36ff670 Binary files /dev/null and b/design/mock/16-descent.png differ diff --git a/design/mock/16-descent.svg b/design/mock/16-descent.svg new file mode 100644 index 0000000..c0af532 --- /dev/null +++ b/design/mock/16-descent.svg @@ -0,0 +1,733 @@ + + + + +1 +at the call +moment 1 + + + +hello.c +lines 3 to 10 of 11 +3 +4 +int +main +( +void +) +{ +5 +char +buf +[ +32 +] +; + + +6 +int +f += +open +( +"note" +, +0 +) +; +7 +int +n += +read +( +f +, +buf +, +32 +) +; +8 +close +( +f +) +; +9 +write +( +1 +, +buf +, +n +) +; +10 +return +0 +; + + + +disassembly +main +4011 +7a +4 +sub +rsp, 0x40 +4011 +7e +mov +rax, fs:[0x28] +the stack's canary +4011 +87 +mov +[rbp - 8], rax +4011 +8b +xor +eax, eax +4011 +8d +6 +lea +rax, [rip + 0xe70] += +402004 + "note" + +4011 +94 +mov +esi, 0 +rsi +0 + +4011 +99 +mov +rdi, rax +rdi +402004 + + +"note" + +4011 +9c +mov +eax, 0 +rax +0 + + + +4011 +a1 +call +401070 +open@plt +open + in libc, by the PLT +4011 +a6 +mov +[rbp - 0x38], eax +rdi +402004 + + +"note" +4011 +a9 +7 +lea +rcx, [rbp - 0x30] +rsi +O_RDONLY +4011 +ad +mov +eax, [rbp - 0x38] +4011 +b0 +mov +edx, 0x20 +4011 +b5 +mov +rsi, rcx +4011 +b8 +mov +edi, eax +4011 +ba +call +401060 +read@plt +read +4011 +bf +mov +[rbp - 0x34], eax + + + + + + + +main calls open by its stub in the PLT. The arguments +are in rdi and rsi: "note" and O_RDONLY. + + +2 +in the stub +moment 2 + + + +hello.c +main waits at line 6 · the pc is in the PLT +3 +4 +int +main +( +void +) +{ +5 +char +buf +[ +32 +] +; + +6 +int +f += +open +( +"note" +, +0 +) +; +7 +int +n += +read +( +f +, +buf +, +32 +) +; +8 +close +( +f +) +; +9 +write +( +1 +, +buf +, +n +) +; +10 +return +0 +; + + + +disassembly +open@plt · the PLT +4011 +87 +4 +mov +[rbp - 8], rax +4011 +8b +xor +eax, eax +4011 +8d +6 +lea +rax, [rip + 0xe70] += +402004 + "note" +4011 +94 +mov +esi, 0 + +4011 +99 +mov +rdi, rax + +4011 +9c +mov +eax, 0 + + +4011 +a1 +call +401070 +open@plt +open +("note", O_RDONLY) +4011 +a6 +mov +[rbp - 0x38], eax +4011 +a9 +7 +lea +rcx, [rbp - 0x30] +4011 +ad +mov +eax, [rbp - 0x38] +4011 +b0 +mov +edx, 0x20 +4011 +b5 +mov +rsi, rcx +4011 +b8 +mov +edi, eax + +open@plt +the PLT · its jmp reads open@got, 403fd8 + + + + + +4010 +70 +jmp +[rip + 0x2f62] + + +7ffff7d10ec0 + open +4010 +76 +push +4 +lazy binding: unused +4010 +7b +jmp +401020 + + + + + + + +The call pushed main+48 and went down: main's window keeps +its call, ringed; the stub's opens under it, and its jmp reads the GOT. + + +3 +the crossing +moments 4 and 5 + + + +hello.c +main waits at line 6 · the pc is in libc's open +3 +4 +int +main +( +void +) +{ +5 +char +buf +[ +32 +] +; + +6 +int +f += +open +( +"note" +, +0 +) +; +7 +int +n += +read +( +f +, +buf +, +32 +) +; +8 +close +( +f +) +; +9 +write +( +1 +, +buf +, +n +) +; +10 +return +0 +; + + + +disassembly +open · libc.so.6 + +4011 +9c +6 +mov +eax, 0 + + +4011 +a1 +call +401070 +open@plt +open +("note", O_RDONLY) +4011 +a6 +mov +[rbp - 0x38], eax + +open@plt +the PLT, jumped through + + + +4010 +70 +jmp +[rip + 0x2f62] + + +7ffff7d10ec0 + open + +open +libc.so.6 · no source + + + +7ffff7d10f +b6 +xor +r8d, r8d +r8 +0 + +7ffff7d10f +b9 +mov +rdi, 0xffffffffffffff9c +rdi +-100 + AT_FDCWD + +7ffff7d10f +c0 +mov +eax, 0x101 +rax +0x101 + 257 + + + +7ffff7d10f +c5 +syscall +openat + nr 257 +7ffff7d10f +c7 +mov +rdx, rax +rdi +-100 +7ffff7d10f +ca +cmp +rax, -0x1000 +rsi +402004 + + +"note" +7ffff7d10f +d0 +ja +7ffff7d10fe8 +rdx +O_RDONLY +7ffff7d10f +d2 +mov +eax, edx +r10 +0 + + +the kernel +openat, system call 257 + + +openat(AT_FDCWD, "note", O_RDONLY, 0) +returns + +3 + "note" + rcx, r11: clobbered + + + + + + + +libc's open is a wrapper: it puts openat's number in rax +and its arguments in rdi, rsi, rdx, r10; syscall crosses down. + + +4 +back up +moment 6 + + + +hello.c +lines 3 to 10 of 11 +3 +4 +int +main +( +void +) +{ +5 +char +buf +[ +32 +] +; + + +6 +int +f += +open +( +"note" +, +0 +) +; +7 +int +n += +read +( +f +, +buf +, +32 +) +; +8 +close +( +f +) +; +9 +write +( +1 +, +buf +, +n +) +; +10 +return +0 +; + + + +disassembly +main +4011 +7a +4 +sub +rsp, 0x40 +4011 +7e +mov +rax, fs:[0x28] +the stack's canary +4011 +87 +mov +[rbp - 8], rax +4011 +8b +xor +eax, eax +4011 +8d +6 +lea +rax, [rip + 0xe70] += +402004 + "note" +4011 +94 +mov +esi, 0 + +4011 +99 +mov +rdi, rax + +4011 +9c +mov +eax, 0 + +4011 +a1 +call +401070 +open@plt +open +("note", O_RDONLY) = +3 + + + +4011 +a6 +mov +[rbp - 0x38], eax +7fffffffecc8 + + +0 +4011 +a9 +7 +lea +rcx, [rbp - 0x30] +4011 +ad +mov +eax, [rbp - 0x38] +4011 +b0 +mov +edx, 0x20 +4011 +b5 +mov +rsi, rcx +4011 +b8 +mov +edi, eax +4011 +ba +call +401060 +read@plt +read +4011 +bf +mov +[rbp - 0x34], eax + + + + + + + +open's ret came straight back to main+48: the stub was a jump, +not a call. The windows close up, and eax holds 3, the descriptor. + + diff --git a/design/mock/17-operands.png b/design/mock/17-operands.png new file mode 100644 index 0000000..c09e6db Binary files /dev/null and b/design/mock/17-operands.png differ diff --git a/design/mock/17-operands.svg b/design/mock/17-operands.svg new file mode 100644 index 0000000..ae3c4f0 --- /dev/null +++ b/design/mock/17-operands.svg @@ -0,0 +1,163 @@ + + + + + + + +as the disassembler writes them +list, hello, libc's open +100191b +nop +dword ptr [rax + rax] +1001926 +mov +rdi, qword ptr [rdi + 8] +100192a +mov +qword ptr [rax + 8], rcx +40117e +mov +rax, qword ptr fs:[0x28] +4011a6 +mov +dword ptr [rbp - 0x38], eax +401070 +jmp +qword ptr [rip + 0x2f62] +7ffff7d10f07 +mov +eax, dword ptr [r11 + 0x308] +7ffff7d10f0e +cmp +byte ptr [rip + 0x11018b], 0 +7ffff7d10f94 +mov +dword ptr [rbp - 0x60], 0x10 +7ffff7d10f9b +movsxd +r10, dword ptr [rbp - 0x30] +7ffff7d10f85 +mov +dword ptr fs:[rax], edx +7ffff7d10fe5 +nop +dword ptr [rax] + + + + + +as canim draws them +list, hello, libc's open +100191b +nop +[rax + rax] +1001926 +mov +rdi, [rdi + 8] +100192a +mov +[rax + 8], rcx +40117e +mov +rax, fs:[0x28] +4011a6 +mov +[rbp - 0x38], eax +401070 +jmp +[rip + 0x2f62] +7ffff7d10f07 +mov +eax, [r11 + 0x308] +7ffff7d10f0e +cmp +[rip + 0x11018b] + +, 0 +7ffff7d10f94 +mov +[rbp - 0x60] + + + + +, 0x10 +7ffff7d10f9b +movsxd +r10, [rbp - 0x30] + + + + + +7ffff7d10f85 +mov +fs:[rax], edx +7ffff7d10fe5 +nop +[rax] + + + + +the row's detail +what a row says: a name for a known place, lea's sum, the slot a jump goes through, a compare's two values and the flags it leaves +40117e +mov +rax, fs:[0x28] +the stack's canary +191a6a1f012bed00 +40118d +lea +rax, [rip + 0xe70] +rip + 0xe70 = +402004 + "note" +401070 +jmp +[rip + 0x2f62] + + +7ffff7d10ec0 + open + · read from open@got, +403fd8 +7ffff7d10f0e +cmp +[rip + 0x11018b] + +, 0 +1 + : +0 + +zf + +7ffff7d10f15 +jne +7ffff7d10fb0 +taken + · reads +zf + → 7ffff7d10fb0 open+240 +7ffff7d10fca +cmp +rax, -0x1000 +3 + : +-4096 + + +CF +zf +7ffff7d10fd0 +ja +7ffff7d10fe8 +not taken + · reads + +CF +zf + diff --git a/design/mock/18-heap-480.png b/design/mock/18-heap-480.png new file mode 100644 index 0000000..2ba9c76 Binary files /dev/null and b/design/mock/18-heap-480.png differ diff --git a/design/mock/18-heap.png b/design/mock/18-heap.png new file mode 100644 index 0000000..17789ad Binary files /dev/null and b/design/mock/18-heap.png differ diff --git a/design/mock/18-heap.svg b/design/mock/18-heap.svg new file mode 100644 index 0000000..cf60bb3 --- /dev/null +++ b/design/mock/18-heap.svg @@ -0,0 +1,1460 @@ + + + + +1 +the first malloc +stop 1, back from malloc in push(7) + + + +the chunks of 0x20 +the program's list · malloc's free list +the program's +405010 + +0 + + +dummy tcache + + +top +0x20fe0 +405020 + + +rax + + + +its bytes +the heap, 405000 to 40506f + + + +4050 +00 +00 +00 +00 +00 +00 +00 +00 +00 + +21 +00 +00 +00 +00 +00 +00 +00 +0x20 +a +m + +P + + + +4050 +10 + +00 +00 +00 +00 +00 +00 +00 +00 +0 + +00 +00 +00 +00 +00 +00 +00 +00 +NULL + + + +4050 +20 +00 +00 +00 +00 +00 +00 +00 +00 + +e1 +0f +02 +00 +00 +00 +00 +00 +0x20fe0 +a +m + +P +top + + + +4050 +30 +00 +00 +00 +00 +00 +00 +00 +00 +00 +00 +00 +00 +00 +00 +00 +00 + + + +4050 +40 +00 +00 +00 +00 +00 +00 +00 +00 +00 +00 +00 +00 +00 +00 +00 +00 + + + +4050 +50 +00 +00 +00 +00 +00 +00 +00 +00 +00 +00 +00 +00 +00 +00 +00 +00 + + + +4050 +60 +00 +00 +00 +00 +00 +00 +00 +00 +00 +00 +00 +00 +00 +00 +00 +00 +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. + + +2 +three chunks +stop 6, after push(9) + + + +the chunks of 0x20 +the program's list · malloc's free list +the program's +405050 +405030 +405010 + +9 + + + + + +12 + + + + + +7 + + + +dummy tcache + + +top +0x20fa0 +405060 + + +head +head + + + +its bytes +the heap, 405000 to 40506f + + + +4050 +00 +00 +00 +00 +00 +00 +00 +00 +00 + +21 +00 +00 +00 +00 +00 +00 +00 +0x20 +a +m + +P + + + +4050 +10 + +07 +00 +00 +00 +00 +00 +00 +00 +7 + +00 +00 +00 +00 +00 +00 +00 +00 +NULL + + + +4050 +20 +00 +00 +00 +00 +00 +00 +00 +00 + +21 +00 +00 +00 +00 +00 +00 +00 +0x20 +a +m + +P + + + +4050 +30 + +0c +00 +00 +00 +00 +00 +00 +00 +12 + +10 +50 +40 +00 +00 +00 +00 +00 + + +405010 + + + +4050 +40 +00 +00 +00 +00 +00 +00 +00 +00 + +21 +00 +00 +00 +00 +00 +00 +00 +0x20 +a +m + +P + + + +4050 +50 + + +09 +00 +00 +00 +00 +00 +00 +00 +9 + + +30 +50 +40 +00 +00 +00 +00 +00 + + +405030 + + + +4050 +60 +00 +00 +00 +00 +00 +00 +00 +00 + +a1 +0f +02 +00 +00 +00 +00 +00 +0x20fa0 +a +m + +P +top + + + + +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. + + +3 +a free +stop 7, back from free in the first pop + + + +the chunks of 0x20 +the program's list · malloc's free list +the program's +405030 +405010 + +12 + + + + + +7 + + + +tcache 0x20 + + +1 + + +405050 + + +key + + + + +top +0x20ca0 +405360 + + +head +head + + +rdi + + + +its bytes +the heap, 405000 to 40506f + + + +4050 +00 +00 +00 +00 +00 +00 +00 +00 +00 + +21 +00 +00 +00 +00 +00 +00 +00 +0x20 +a +m + +P + + + +4050 +10 + +07 +00 +00 +00 +00 +00 +00 +00 +7 + +00 +00 +00 +00 +00 +00 +00 +00 +NULL + + + +4050 +20 +00 +00 +00 +00 +00 +00 +00 +00 + +21 +00 +00 +00 +00 +00 +00 +00 +0x20 +a +m + +P + + + +4050 +30 + +0c +00 +00 +00 +00 +00 +00 +00 +12 + +10 +50 +40 +00 +00 +00 +00 +00 + + +405010 + + + +4050 +40 +00 +00 +00 +00 +00 +00 +00 +00 + +21 +00 +00 +00 +00 +00 +00 +00 +0x20 +a +m + +P + + + +4050 +50 + + +05 +04 +00 +00 +00 +00 +00 +00 +NULL +^ 405 + + +53 +89 +03 +0a +cf +c5 +9f +6b +key + + + +4050 +60 +00 +00 +00 +00 +00 +00 +00 +00 + + +01 +03 +00 +00 +00 +00 +00 +00 +0x300 +a +m + +P +tcache + + + + + + +tcache + → +405070 +tcache_perthread_struct · 0x2f0 bytes + + + +405 +070 + + + +0f +00 +15 +slots left · 1 in the bin +10 +00 +10 +00 +10 +00 +10 +00 +10 +00 +10 +00 +10 +00 + + + + + +8 rows + + + +405 +100 +10 +00 +10 +00 +10 +00 +10 +00 + + + +50 +50 +40 +00 +00 +00 +00 +00 + + +405050 + + + +405 +110 +00 +00 +00 +00 +00 +00 +00 +00 +00 +00 +00 +00 +00 +00 +00 +00 + + + + + +36 rows + +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. + + +4 +a second free +stop 8, back from free in the second pop + + + +the chunks of 0x20 +the program's list · malloc's free list +the program's +405010 + +7 + + + +tcache 0x20 + + +2 + + +405030 + + +key + + + + +405050 + + +key + + + + +top +0x20ca0 +405360 + + +head +head + + +rdi + + + +its bytes +the heap, 405000 to 40506f + + + +4050 +00 +00 +00 +00 +00 +00 +00 +00 +00 + +21 +00 +00 +00 +00 +00 +00 +00 +0x20 +a +m + +P + + + +4050 +10 + +07 +00 +00 +00 +00 +00 +00 +00 +7 + +00 +00 +00 +00 +00 +00 +00 +00 +NULL + + + +4050 +20 +00 +00 +00 +00 +00 +00 +00 +00 + +21 +00 +00 +00 +00 +00 +00 +00 +0x20 +a +m + +P + + + +4050 +30 + + +55 +54 +40 +00 +00 +00 +00 +00 + + +405050 +^ 405 + + +53 +89 +03 +0a +cf +c5 +9f +6b +key + + + +4050 +40 +00 +00 +00 +00 +00 +00 +00 +00 + +21 +00 +00 +00 +00 +00 +00 +00 +0x20 +a +m + +P + + + +4050 +50 + +05 +04 +00 +00 +00 +00 +00 +00 +NULL +^ 405 + +53 +89 +03 +0a +cf +c5 +9f +6b +key + + + +4050 +60 +00 +00 +00 +00 +00 +00 +00 +00 + +01 +03 +00 +00 +00 +00 +00 +00 +0x300 +a +m + +P +tcache + + + + + + +tcache + → +405070 +tcache_perthread_struct · 0x2f0 bytes + + + +405 +070 + + + +0e +00 +14 +slots left · 2 in the bin +10 +00 +10 +00 +10 +00 +10 +00 +10 +00 +10 +00 +10 +00 + + + + + +8 rows + + + +405 +100 +10 +00 +10 +00 +10 +00 +10 +00 + + + +30 +50 +40 +00 +00 +00 +00 +00 + + +405030 + + + +405 +110 +00 +00 +00 +00 +00 +00 +00 +00 +00 +00 +00 +00 +00 +00 +00 +00 + + + + + +36 rows + +The second chains to the first: 405455 is 405050 xor 405, the +chunk's own address shifted 12. The arc is the pointer it hides. + + +5 +malloc takes it back +stop 9, back from malloc in push(25) + + + +the chunks of 0x20 +the program's list · malloc's free list +the program's +405030 +405010 + +405455 + + +7 + + + +tcache 0x20 + + +1 + + +405050 + + +key + + + + +top +0x20ca0 +405360 + + +rax + + +head +head + + + +its bytes +the heap, 405000 to 40506f + + + +4050 +00 +00 +00 +00 +00 +00 +00 +00 +00 + +21 +00 +00 +00 +00 +00 +00 +00 +0x20 +a +m + +P + + + +4050 +10 + +07 +00 +00 +00 +00 +00 +00 +00 +7 + +00 +00 +00 +00 +00 +00 +00 +00 +NULL + + + +4050 +20 +00 +00 +00 +00 +00 +00 +00 +00 + +21 +00 +00 +00 +00 +00 +00 +00 +0x20 +a +m + +P + + + +4050 +30 + +55 +54 +40 +00 +00 +00 +00 +00 +the old link + + +00 +00 +00 +00 +00 +00 +00 +00 +NULL + + + +4050 +40 +00 +00 +00 +00 +00 +00 +00 +00 + +21 +00 +00 +00 +00 +00 +00 +00 +0x20 +a +m + +P + + + +4050 +50 + +05 +04 +00 +00 +00 +00 +00 +00 +NULL +^ 405 + +53 +89 +03 +0a +cf +c5 +9f +6b +key + + + +4050 +60 +00 +00 +00 +00 +00 +00 +00 +00 + +01 +03 +00 +00 +00 +00 +00 +00 +0x300 +a +m + +P +tcache + + + + +tcache + → +405070 +tcache_perthread_struct · 0x2f0 bytes + + + +405 +070 + + + +0f +00 +15 +slots left · 1 in the bin +10 +00 +10 +00 +10 +00 +10 +00 +10 +00 +10 +00 +10 +00 + + + + + +8 rows + + + +405 +100 +10 +00 +10 +00 +10 +00 +10 +00 + + + +50 +50 +40 +00 +00 +00 +00 +00 + + +405050 + + + +405 +110 +00 +00 +00 +00 +00 +00 +00 +00 +00 +00 +00 +00 +00 +00 +00 +00 + + + + + +36 rows + +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. + + diff --git a/design/mock/19-map-1080.png b/design/mock/19-map-1080.png index 3f8ed45..65ae927 100644 Binary files a/design/mock/19-map-1080.png and b/design/mock/19-map-1080.png differ diff --git a/design/mock/19-map-480.png b/design/mock/19-map-480.png index 5c08c28..1dabf72 100644 Binary files a/design/mock/19-map-480.png and b/design/mock/19-map-480.png differ diff --git a/design/mock/19-map.png b/design/mock/19-map.png index b9313a6..3c5d6d9 100644 Binary files a/design/mock/19-map.png and b/design/mock/19-map.png differ diff --git a/design/mock/19-map.svg b/design/mock/19-map.svg index cd2def6..8dc8779 100644 --- a/design/mock/19-map.svg +++ b/design/mock/19-map.svg @@ -358,21 +358,21 @@ backtrace -newest on top - - -#0 -reverse+22 -list.c:21 -#1 -main+80 -list.c:40 +newest at the bottom + + +#0 +reverse+22 +list.c:21 +#1 +main+80 +list.c:40 #2 libc+27781 -#3 -__libc_start_main+137 -#4 -_start+37 +#3 +__libc_start_main+137 +#4 +_start+37 @@ -454,8 +454,8 @@ ret 1001 8d2 -nop -word ptr cs:[rax + rax] +nop +cs:[rax + rax] 1001 8e0 @@ -497,12 +497,12 @@ 8f7 13 mov -qword ptr [rax], rbx +[rax], rbx 1001 8fa 14 mov -qword ptr [rax + 8], r14 +[rax + 8], r14 1001 8fe 15 @@ -521,8 +521,8 @@ ret 1001 903 -nop -word ptr cs:[rax + rax] +nop +cs:[rax + rax] 1001 910 20 @@ -546,8 +546,8 @@ eax, eax 1001 91b -nop -dword ptr [rax + rax] +nop +[rax + rax] 1001 920 mov @@ -563,7 +563,7 @@ 926 21 mov -rdi, qword ptr [rdi + 8] +rdi, [rdi + 8] 1004098 @@ -576,7 +576,7 @@ 92a 22 mov -qword ptr [rax + 8], rcx +[rax + 8], rcx 1001 92e 20 @@ -604,7 +604,7 @@ 1001 938 -endbr64 +endbr64 _init 1001 93c @@ -613,7 +613,7 @@ 1001 940 mov -rax, qword ptr [rip + 0x1231] +rax, [rip + 0x1231] 1001 947 test @@ -636,7 +636,7 @@ 1001 954 -endbr64 +endbr64 _fini 1001 958 @@ -653,21 +653,21 @@ 1001 970 push -qword ptr [rip + 0x1222] +[rip + 0x1222] strtol@plt-0x10 1001 976 jmp -qword ptr [rip + 0x1224] +[rip + 0x1224] 1001 97c -nop -dword ptr [rax] +nop +[rax] 1001 980 jmp -qword ptr [rip + 0x1222] +[rip + 0x1222] strtol@plt 1001 986 @@ -681,7 +681,7 @@ 1001 990 jmp -qword ptr [rip + 0x121a] +[rip + 0x121a] printf@plt 1001 996 @@ -695,7 +695,7 @@ 1001 9a0 jmp -qword ptr [rip + 0x1212] +[rip + 0x1212] malloc@plt 1001 9a6 diff --git a/design/mock/20-dive.png b/design/mock/20-dive.png index 4f52b73..b389ac2 100644 Binary files a/design/mock/20-dive.png and b/design/mock/20-dive.png differ diff --git a/design/mock/21-loupe-480.png b/design/mock/21-loupe-480.png index 4f9f28e..684fc1a 100644 Binary files a/design/mock/21-loupe-480.png and b/design/mock/21-loupe-480.png differ diff --git a/design/mock/21-loupe.png b/design/mock/21-loupe.png index 56c85d7..1633127 100644 Binary files a/design/mock/21-loupe.png and b/design/mock/21-loupe.png differ diff --git a/design/mock/21-loupe.svg b/design/mock/21-loupe.svg index 8108808..7a40916 100644 --- a/design/mock/21-loupe.svg +++ b/design/mock/21-loupe.svg @@ -85,8 +85,8 @@ eax, eax 10019 1b -nop -dword ptr [rax + rax] +nop +[rax + rax] 10019 20 mov @@ -102,7 +102,7 @@ 26 21 mov -rdi, qword ptr [rdi + 8] +rdi, [rdi + 8] 1004098 @@ -115,7 +115,7 @@ 2a 22 mov -qword ptr [rax + 8], rcx +[rax + 8], rcx 10019 2e 20 @@ -143,7 +143,7 @@ 10019 38 -endbr64 +endbr64 _init @@ -1002,20 +1002,20 @@ - - - - - - - - + + + + + + + + - - - - + + + + @@ -1089,8 +1089,8 @@ - - + + @@ -1131,12 +1131,12 @@ - + - + @@ -1155,8 +1155,8 @@ - - + + @@ -1180,8 +1180,8 @@ - - + + @@ -1195,7 +1195,7 @@ - + @@ -1203,7 +1203,7 @@ - + @@ -1230,7 +1230,7 @@ - + @@ -1239,7 +1239,7 @@ - + @@ -1261,7 +1261,7 @@ - + @@ -1277,20 +1277,20 @@ - + - + - - + + - + @@ -1303,7 +1303,7 @@ - + @@ -1316,7 +1316,7 @@ - + @@ -2285,7 +2285,7 @@ - + diff --git a/design/mock/context.py b/design/mock/context.py index 8c99956..efae74c 100644 --- a/design/mock/context.py +++ b/design/mock/context.py @@ -436,14 +436,19 @@ def wide(total): return max(len(str(total)) - 3, 0) * A -def source(g, y, rows, lines, first, cur, trail, name, total): - """Returns the y of the current line's row. Numbers as wide as the file's last: the text moves by the extra.""" +def source(g, y, rows, lines, first, cur, trail, name, total, waiting=None): + """Returns the y of the current line's row. Numbers as wide as the file's last: the text moves by the extra. + waiting: the pc is in code with no source; cur is the line whose call it is under, ringed, and the tag says + where the pc is.""" w = wide(total) - y0 = panel(g, LX, y, CW, rows, name, note=f"lines {first} to {min(first + rows - 1, total)} of {total}") + y0 = panel(g, LX, y, CW, rows, name, note=waiting or f"lines {first} to {min(first + rows - 1, total)} of {total}") for i in range(rows): n = first + i yy = y0 + i * ROW - if n == cur: + if n == cur and waiting: + g.frame(LX + 2, yy, 50 + w, ROW, VELLUM, 2) + g.t(LX + 46 + w, b(yy), str(n), S, VELLUM, BOLD, "end") + elif n == cur: g.t(LX + 46 + w, b(yy), str(n), S, current_row(g, LX + 2, yy, CW - 4, 50 + w), BOLD, "end") else: if n in trail: @@ -454,17 +459,81 @@ def source(g, y, rows, lines, first, cur, trail, name, total): DX_ADDR, DX_LINE, DX_MN, DX_OPS, DX_NOTE = 40, 134, 180, 264, 564 - - -def disasm(g, y, rows, func, cur_line, jump=None, w=0): +SIZES = {"byte": 1, "word": 2, "dword": 4, "qword": 8, "tbyte": 10, "xmmword": 16, "ymmword": 32, "zmmword": 64} +IMPLIED = {"push", "pop", "call", "jmp", "lea", "nop", "endbr64"} # the width is the mnemonic's, or nothing is touched + + +def reg_width(r): + """The bytes a register names, or None for what is not one (a segment, a number).""" + m = re.fullmatch(r"([xyz])mm\d+", r) or re.fullmatch(r"r(\d+)([dwb]?)", r) + if m and m.re.pattern.startswith("(["): + return {"x": 16, "y": 32, "z": 64}[m.group(1)] + if m: + return {"": 8, "d": 4, "w": 2, "b": 1}[m.group(2)] + for n, pat in ((8, r"r([abcd]x|[sd]i|[sb]p|ip)"), (4, r"e([abcd]x|[sd]i|[sb]p|ip)"), (2, r"[abcd]x|[sd]i|[sb]p"), + (1, r"[abcd][lh]|[sd]il|[sb]pl")): + if re.fullmatch(pat, r): + return n + return None + + +def width(g, x, y, n): + """`width`: an access's size as byte cells after the operand it is the size of: 5 by 10, 7 apart, 3 after + the bracket, in dim; what follows the operand goes on 4 after the last. Returns where it goes on.""" + for i in range(min(n, 8)): + g.rect(x + 3 + i * 7, y + 9, 5, 10, DIM) + end = x + 3 + min(n, 8) * 7 - 2 + if n > 8: # wider than a word: the count after the cells + g.t(end + 4, b(y), str(n), S, FAINT, MONO) + end += 4 + len(str(n)) * A + return end + 4 + + +def operands(g, x, y, mn, s, ink, sizes=False): + """Operands as the disassembler writes them, less the size word (`qword ptr `). Where no register of the + same instruction has the access's size, a `width` after the bracket says it. sizes: draw them as written + (the mock's 'before'). Returns where the row's text ends.""" + m = re.search(r"\b(%s) ptr " % "|".join(SIZES), s) + if sizes or not m: + g.t(x, b(y), s, S, FAINT if mn in ("nop", "endbr64") else ink, MONO) + return x + len(s) * A + n, s = SIZES[m.group(1)], s[:m.start()] + s[m.end():] + if mn in ("nop", "endbr64"): # padding: nothing is read or written + g.t(x, b(y), s, S, FAINT, MONO) + return x + len(s) * A + outside = re.sub(r"\[.*?\]", "", s) + if mn in IMPLIED or n in [reg_width(t) for t in re.findall(r"[a-z][a-z0-9]*", outside)]: + g.t(x, b(y), s, S, ink, MONO) + return x + len(s) * A + cut = s.index("]") + 1 + g.t(x, b(y), s[:cut], S, ink, MONO) + x = width(g, x + cut * A, y, n) + g.t(x, b(y), s[cut:], S, ink, MONO) + return x + len(s[cut:]) * A + + +def disasm(g, y, rows, func, cur_line, jump=None, w=0, sizes=False): """rows: (address, line, mnemonic, operands, state, note), in address order: the pc's row and as many - above as below. state: cur | trail0..2 (it ran just now) | out (past the function's end) | None. - note, the row's annotation: ("chain", links, end, marks) | ("set", register, links, end) | ("text", s). + above as below. state: cur | trail0..2 (it ran just now) | out (past the function's end) | wait (a call + whose callee is open under it, in a descent) | None. + note, the row's annotation: ("chain", links, end, marks) | ("set", register, links, end) | ("text", s) | + ("runs", [(s, ink)]). jump: (from, to): a jump that was taken, drawn in the lane. w: how much wider than three characters the program's line numbers are; every column right of the line's moves by it. Returns each address's row y.""" y0 = panel(g, LX, y, CW, len(rows), "disassembly", note=func) - lead = shared([r[0] for r in rows if r[4] != "out"]) + where = insn_rows(g, y0, rows, cur_line, w, sizes=sizes) + if jump: # how control came here: up the lane, into the row it landed on + ya, yb = where[jump[0]] + 12, where[jump[1]] + 12 + gen.arrow(g, [(LX + 38, ya), (LX + 31, ya), (LX + 31, yb), (LX + 39, yb)], TRAIL[0], 2, r=4, hl=7, hw=8) + return where + + +def insn_rows(g, y0, rows, cur_line, w=0, lead=None, sizes=False): + """`insn` rows from y0, a row each. Addresses wider than seven digits (a library's, 7ffff7d10fc5) move every + column right of theirs by the extra, for the rows of this window only.""" + lead = shared([r[0] for r in rows if r[4] != "out"]) if lead is None else lead + w += max(max(len("%x" % r[0]) for r in rows) - 7, 0) * A where, last_line = {}, None for i, (a, line, mn, ops, st, note) in enumerate(rows): yy = y0 + i * ROW @@ -474,50 +543,115 @@ def disasm(g, y, rows, func, cur_line, jump=None, w=0): g.rect(LX + 2, yy, CW - 4, 2, RULE) if st == "cur": g.path(f"M{LX + 20},{yy + 12} l-10,-6 v12 Z", fill=current_row(g, LX + 2, yy, CW - 4, 24)) + elif st == "wait": # it called, and waits: the plaque's place ringed, as a waiting call node + g.frame(LX + 2, yy, 24, ROW, VELLUM, 2) + g.path(f"M{LX + 19},{yy + 12} l-8,-5 v10 Z", fill=VELLUM) elif st and st.startswith("trail"): g.rect(LX + 2, yy + 1, 4, ROW - 2, TRAIL[int(st[-1])]) + elif st == "pass": # a stub's jump, passed through on the way down + g.rect(LX + 2, yy + 1, 4, ROW - 2, TRAIL[1]) addr(g, LX + DX_ADDR, yy, a, mix(CODE, PAGE, 0.6) if dim else CODE, lead=0 if dim else lead) if line is not None and line != last_line and not dim: # the line this run was compiled from on = line == cur_line g.t(LX + DX_LINE + 3 * A + w, b(yy), str(line), S, GOLD if on else FAINT, BOLD if on else MONO, "end") last_line = line - g.t(LX + DX_MN + w, b(yy), mn, S, FAINT if dim else KW, MONO if dim else BOLD) + g.t(LX + DX_MN + w, b(yy), mn, S, FAINT if dim or mn in ("nop", "endbr64") else KW, + MONO if dim or mn in ("nop", "endbr64") else BOLD) x = LX + DX_OPS + w for part, ink in ops: if isinstance(part, int): x = addr(g, x, yy, part, ink) + elif isinstance(part, tuple): # a target and its name: `401070 open@plt` + x = addr(g, x, yy, part[0], ink) + g.t(x + A, b(yy), part[1], S, VELLUM, MONO) + x += A + len(part[1]) * A else: - g.t(x, b(yy), part, S, FAINT if dim else ink, MONO) - x += len(part) * A + x = operands(g, x, yy, mn, part, FAINT if dim else ink, sizes) if not note: continue x = max(LX + DX_NOTE + w, x + A) # operands are never cut where the row has room if note[0] == "text": g.t(x, b(yy), note[1], S, note[2] if len(note) > 2 else DIM, MONO) + elif note[0] == "runs": # a line of the row's own: words, values, chips + for s_, ink in note[1]: + if isinstance(s_, int): + x = addr(g, x, yy, s_, ink) + elif ink == "arrow": # `points` + x = link_arrow(g, x, yy) + elif ink == "chip": # a flag the instruction set, as rip's row draws it + g.rect(x, yy + 2, 32, 20, RAISED) + g.t(x + 16, b(yy), s_, S, BRIGHT, BOLD, "middle") + x += 32 + 6 + else: + g.t(x, b(yy), s_, S, ink, MONO) + x += len(s_) * A elif note[0] == "set": # what an instruction that ran left in a register: a register row, small g.t(x, b(yy), note[1], S, DIM, MONO) chain(g, x + (len(note[1]) + 1) * A, yy, note[2], note[3], room=LX + CW - 12) else: where["note"] = (yy, chain(g, x, yy, note[1], note[2], note[3], room=LX + CW - 12)) - if jump: # how control came here: up the lane, into the row it landed on - ya, yb = where[jump[0]] + 12, where[jump[1]] + 12 - gen.arrow(g, [(LX + 38, ya), (LX + 31, ya), (LX + 31, yb), (LX + 39, yb)], TRAIL[0], 2, r=4, hl=7, hw=8) return where -def thread(g, ys, spans): +def descent(g, y, windows, note=None, w=0, sizes=False): + """`disasm` as a descent: a window of code a call deep, the caller's first, each callee's under the row that + called it. windows: (name, about, rows) for code, rows as disasm's; ("kernel", about, lines) last, lines + being runs [(s, ink)] on vellum. A window after the first opens with a head row: a rule, the call lane + arriving from the row that called (a `wait` row) or jumped (the row before the head), the name, what it is. + Returns {address: y}, and each head's y under its name.""" + n = sum(len(wn[2]) for wn in windows) + len(windows) - 1 + y0 = panel(g, LX, y, CW, n, "disassembly", note=note) + yy, where, came = y0, {}, None + for k, (name, about, rows) in enumerate(windows): + if k: + kern = name == "kernel" + if kern: # solid vellum is the kernel + g.rect(LX + 2, yy, CW - 4, (len(rows) + 1) * ROW, gen.KERNEL) + g.rect(LX + 2, yy, CW - 4, 2, gen.KERNEL_RULE if kern else RULE) + g.t(LX + DX_ADDR, b(yy), name if not kern else "the kernel", 18, PAGE if kern else VELLUM, "sansm") + g.t(LX + CW - 12, b(yy), about, 18, gen.KERNEL_DIM if kern else FAINT, "sans", "end") + if came is not None and not kern: # the call lane: down from the row that went, into the head + gen.arrow(g, [(LX + 14, came), (LX + 14, yy + 12), (LX + 34, yy + 12)], VELLUM, 2, r=4, hl=7, hw=8) + elif came is not None: # the crossing: down from the `syscall` row onto the vellum, its head in ink + g.rect(LX + 12.5, came, 3, yy - came, VELLUM) + g.path(f"M{LX + 14},{yy + 14} l-6,-10 h12 Z", fill=PAGE) + where[name] = yy + yy += ROW + if kern: + for i, runs in enumerate(rows): + x = LX + DX_ADDR + for s_, ink in runs: + if ink == "struck": + g.rect(x - 5, yy + i * ROW + 1, len(s_) * A + 10, ROW - 2, SOFT) + g.t(x, b(yy + i * ROW), s_, S, BRIGHT, BOLD) + else: + g.t(x, b(yy + i * ROW), s_, S, ink, MONO) + x += len(s_) * A + yy += len(rows) * ROW + continue + at = insn_rows(g, yy, rows, None, w, sizes=sizes) + where.update(at) + went = [r for r in rows if r[4] in ("wait", "pass", "cur")] + came = (at[went[-1][0]] + (ROW if went[-1][4] == "wait" else 18)) if went else None + yy += len(rows) * ROW + return where + + +def thread(g, ys, spans, ink=GOLD, sw=3): """The gold thread: the source's current line and the instructions it was compiled to are one mark. - A bracket on the line, a bracket on each run of its instructions, and a line in the margin between.""" + A bracket on the line, a bracket on each run of its instructions, and a line in the margin between. + Waiting (the pc is in code with no source, under this line's call): primary, 2.""" x = LX - 10 - bracket(g, x, ys + 2, ys + ROW - 2, GOLD, 3, 7) + bracket(g, x, ys + 2, ys + ROW - 2, ink, sw, 7) for ya, yb in spans: - bracket(g, x, ya + 2, yb - 2, GOLD, 3, 7) - g.rect(x - 1.5, ys + 2, 3, spans[-1][1] - 2 - ys - 2, GOLD) + bracket(g, x, ya + 2, yb - 2, ink, sw, 7) + g.rect(x - sw / 2, ys + 2, sw, spans[-1][1] - 2 - ys - 2, ink) # ---- the lens: a list, and beside it the bytes it is drawn from ------------------------- def fields(g, y, h, about, base, length, flist, mem, region, region_of, marks=(), title="its bytes", x=None, - framed=(), bare=False, rows_shown=None, more_rows=True, touched=(), withhold=None, hot=False): + framed=(), bare=False, rows_shown=None, more_rows=True, touched=(), withhold=None, hot=False, changed=(), + held_at=()): """`hexdump` by `fields`: the region a scene names, read by the fields it gives it, in the band's view. flist: (at, len, reads, big, mark) with reads in number | pointer | text | bytes. mem: {address: byte} of what the stop shown saw (or the kernel took); a byte not in it is unread. marks: the field marks framed as read. @@ -576,19 +710,60 @@ def fields(g, y, h, about, base, length, flist, mem, region, region_of, marks=() for fa, fl in framed: # the target: framed bright, as attention if base + fa < a0 + i0 + n and base + fa + fl > a0 + i0: g.frame(x0 - 3, yy - 1, x1 - x0 + 6, ROW + 2, BRIGHT, 2) - ink = {"number": VELLUM, "bytes": VELLUM, "text": LIT, "loose": DIM, "malloc": DIM, - "pointer": (region_of(word) or VELLUM) if known else VELLUM}[reads] + ink = {"number": VELLUM, "bytes": VELLUM, "text": LIT, "loose": DIM, "malloc": DIM, "size": VELLUM, + "protected": VELLUM, "pointer": (region_of(word) or VELLUM) if known else VELLUM}[reads] nz = [k for k, v in enumerate(vals) if v] + hit = [k for k in range(n) if a0 + i0 + k in changed] + if hit: # struck: the bytes whose value changed, first to last, in the run + g.rect(bx(i0 + hit[0]) - 5, yy + 1, bx(i0 + hit[-1]) - bx(i0 + hit[0]) + 34, ROW - 2, SOFT) + if any(a0 + i0 + k in held_at for k in range(n)): + held(g, bx(i0 + min(k for k in range(n) if a0 + i0 + k in held_at)), yy, 24) for k, v in enumerate(vals): xx = bx(i0 + k) + if hit and hit[0] <= k <= hit[-1] and v is not None: + g.t(xx, b(yy), "%02x" % v, S, BRIGHT, BOLD) + continue if v is None: # not read at this stop: two dots, on the field's paper g.rect(xx + 5, yy + 11, 4, 4, FAINT) g.rect(xx + 15, yy + 11, 4, 4, FAINT) continue - padded = reads in ("number", "pointer") and nz and (k < nz[0] if f[3] else k > nz[-1]) + padded = reads in ("number", "pointer", "size", "protected") and nz and (k < nz[0] if f[3] else k > nz[-1]) g.t(xx, b(yy), "%02x" % v, S, mix(ink, PAGE, 0.45) if padded else ink, MONO) + if known and f and len(f) > 5 and reads in ("bytes", "loose"): # a field the scene names, left to the eye + g.t(x, b(yy) - 1, f[5], 18, FAINT, "sans") + x += tw(f[5], 18, "sans") + A if not known or reads in ("bytes", "loose"): continue + if reads == "size": # a chunk's size word: the size, and its three low bits as flags + s_ = "0x%x" % (word & ~7) + g.t(x, b(yy), s_, S, VELLUM, MONO) + x += (len(s_) + 1) * A + for bit, letter in ((2, "A"), (1, "M"), (0, "P")): + if word >> bit & 1: # set: on a chip, as rip's flags; clear: lower case, dim + g.rect(x - 4, yy + 2, 20, 20, RAISED) + g.t(x, b(yy), letter, S, BRIGHT, BOLD) + else: + g.t(x, b(yy), letter.lower(), S, DIM, MONO) + x += 18 + x += 6 + if len(f) > 5: + g.t(x, b(yy) - 1, f[5], 18, FAINT, "sans") + x += tw(f[5], 18, "sans") + A + continue + if reads == "protected": # safe-linking: the bytes are the pointer xor its own address >> 12 + fa = a0 + i0 + ptr = word ^ (fa >> 12) + if ptr and base <= ptr < base + length: + found.append((a0, fa, ptr, region_of(ptr) or VELLUM, None)) + if ptr: + x = addr(g, link_arrow(g, x, yy), yy, ptr, region_of(ptr) or VELLUM) + A + else: + g.t(x, b(yy), "NULL", S, DIM, MONO) + x += 5 * A + s_ = "^ %x" % (fa >> 12) + g.t(x, b(yy), s_, S, FAINT, MONO) + x += (len(s_) + 1) * A + continue if reads == "malloc": # the allocator's own: the size word of the chunk after it note = "malloc's: a chunk of %d" % (word & ~7) if word < 0x10000 else "malloc's: the top chunk" g.t(x, b(yy) - 1, note, 18, FAINT, "sans") @@ -610,6 +785,9 @@ def fields(g, y, h, about, base, length, flist, mem, region, region_of, marks=() s_ = s_[:max(int((X + CW - 12 - x) // A) - 1, 0)] + "…" g.t(x, b(yy), s_, S, sink, MONO) x += (len(s_) + 1) * A + if f and len(f) > 5: # what the scene says the number is + g.t(x, b(yy) - 1, f[5], 18, FAINT, "sans") + x += tw(f[5], 18, "sans") + A yy += ROW if more_rows and shown and shown[-1] < total - 1: # the rows after the last field: one row, and how many more(g, X + 34 - 6, yy) @@ -621,37 +799,22 @@ def fields(g, y, h, about, base, length, flist, mem, region, region_of, marks=() return ys -def kernel(g, y, h, calls, name, nr, rows, given): - """`syscalls` in the band: the kernel's side, on vellum. calls: (name, result), newest first (the first is - the call in hand, gold: where execution is); rows: (register, argument, value, note); given: the bytes it took.""" - tag(g, LX, y, CW, h, "kernel", note="the other side of a system call") - g.rect(LX + 2, y + TAG, CW - 4, h - TAG - 2, gen.KERNEL) - g.t(LX + 12, y + TAG + 20, "calls so far", 18, gen.KERNEL_DIM, "sans") - for i, (c, ret) in enumerate(calls[:(h - TAG - 40) // 28]): - yy = y + TAG + 28 + i * 28 - if i == 0: - g.rect(LX + 12, yy, 196, 24, GOLD) - g.frame(LX + 12, yy, 196, 24, PAGE, 2) - g.t(LX + 20, b(yy), c, S, PAGE, MONO) - g.t(LX + 200, b(yy), str(ret), S, PAGE, MONO, "end") - x = LX + 236 - g.t(x, y + TAG + 46, name, 35, PAGE, "monob") - g.t(x + len(name) * 21 + 16, y + TAG + 44, "system call %d" % nr, 18, gen.KERNEL_DIM, "sans") - for i, (reg, arg, val, note) in enumerate(rows): - yy = y + TAG + 60 + i * 28 - g.rect(x, yy, 300, 2, gen.KERNEL_RULE) - g.t(x, b(yy + 2), reg, S, gen.KERNEL_DIM, MONO) - g.t(x + 52, b(yy + 2), arg, 20, PAGE, "sansm") - g.t(x + 130, b(yy + 2), val, S, PAGE, MONO) - if note: - g.t(x + 130 + (len(val) + 1) * A, b(yy + 2), note, S, PAGE, MONO) - x = LX + 564 - g.t(x, y + TAG + 20, "the bytes it was given", 18, gen.KERNEL_DIM, "sans") - for i, v in enumerate(given): - xx, yy = x + (i % 10) * 28, y + TAG + 28 + (i // 10) * 30 - g.rect(xx, yy, 26, 26, gen.KERNEL_HELD) - g.frame(xx, yy, 26, 26, PAGE, 2) - g.t(xx + 1, b(yy + 1), "%02x" % v, S, PAGE, MONO) +def calls_log(g, y, h, calls): + """`syscalls`'s drawing: the calls so far, as strace writes them, oldest on top, the newest last; the kernel's + own side is under the `syscall` row while a call is made (`descent`), not here. calls: runs [(s, ink)] a row, + oldest first; the newest's result carries the turned corner.""" + rows = (h - TAG - 2 * PAD) // ROW + note = "oldest on top · newest at the bottom" + (" · %d more above" % (len(calls) - rows) if len(calls) > rows else "") + y0 = panel(g, LX, y, CW, rows, "system calls", focus=True, note=note) + shown = calls[-rows:] + for i, runs in enumerate(shown): + yy, x = y0 + i * ROW, LX + 12 + for s_, ink in runs: + if ink == "held": + held(g, x, yy, len(s_) * A) + ink = VELLUM + g.t(x, b(yy), s_, S, ink, BOLD if x == LX + 12 else MONO) + x += len(s_) * A def dump_row(g, y, a, runs, lead, mark=None, node=False): @@ -987,15 +1150,17 @@ def calls(g, y, h, tree, made, caller, project_from=None): return cx, cy -def backtrace(g, y, h, frames, x=RX, note="the calls waiting · newest on top", focus=False): - """The call chain as rows, newest on top: the view of the calls, or a lens's drawing (x = LX). frames: (name, - args, file, line), file None where the recording does not know it. More frames than rows: the oldest go, counted.""" +def backtrace(g, y, h, frames, x=RX, note="the calls waiting · newest at the bottom", focus=False): + """The call chain as rows, newest at the bottom (a call goes down): the view of the calls, or a lens's drawing + (x = LX). frames: (name, args, file, line), newest first, as numbered; file None where the recording does not + know it. More frames than rows: the oldest go from the top, counted.""" rows = (h - TAG - 2 * PAD) // ROW if len(frames) > rows: - note += " · %d more" % (len(frames) - rows) + note += " · %d more above" % (len(frames) - rows) y0 = panel(g, x, y, CW, rows, "backtrace", focus=focus, note=note) - for i, (name, args, file, line) in enumerate(frames[:rows]): - yy = y0 + i * ROW + shown = frames[:rows] + for i, (name, args, file, line) in enumerate(shown): + yy = y0 + (len(shown) - 1 - i) * ROW + (rows - len(shown)) * ROW if i == 0: g.t(x + 12, b(yy), "#0", S, current_row(g, x + 2, yy, CW - 4, 40), BOLD) else: @@ -1146,7 +1311,9 @@ SQ_DIS_120 = [(a, 80895, mn, [(ops, P)], "cur" if a == 0x448d84 else None, None) (0x448d80, "push", "rbp"), (0x448d81, "mov", "rbp, rsp"), (0x448d84, "push", "r15"), (0x448d86, "push", "r14"), (0x448d88, "push", "r13"), (0x448d8a, "push", "r12"), (0x448d8c, "push", "rbx"), (0x448d8d, "sub", "rsp, 0xa8"), (0x448d94, "mov", "r14d, ecx")]] -SQ_DIS_169 = [(a, 42290, mn, ops, "cur" if a == 0x422051 else None, None) for a, mn, ops in [ +SQ_DIS_169 = [(a, 42290, mn, ops, "cur" if a == 0x422051 else "trail0" if a == 0x42204b else None, + ("runs", [("", "arrow"), ("__pwrite64", VELLUM), (" = 4096", DIM)]) if a == 0x42204b else None) + for a, mn, ops in [ (0x422042, "mov", [("rsi, r15", P)]), (0x422045, "mov", [("rdx, r12", P)]), (0x422048, "mov", [("rcx, r14", P)]), (0x42204b, "call", [("qword ptr [rip + 0xf1567]", P)]), (0x422051, "mov", [("r13, rax", P)]), (0x422054, "test", [("r13d, r13d", P)]), (0x422057, "jns", [(0x422070, C_)]), (0x422059, "call", [(0x4021c0, C_)]), @@ -1196,7 +1363,7 @@ def frame_sqlite(name, late): registers(g, yr, SQ_REGS_120, SQ_FLAGS_120, changed) stack(g, yr + ph(17) + 12, SQ_STACK_120, [("sqlite3BtreeInsert", 0, 1), ("sqlite3VdbeExec", 2, 9)], rsp=0x7fffffffe980, roles={0: ("saved rbp", DIM)}) - backtrace(g, TOP, sp["band"], SQ_BT_120, x=LX, note="newest on top", focus=True) + backtrace(g, TOP, sp["band"], SQ_BT_120, x=LX, note="newest at the bottom", focus=True) base = 0x7fffffffea60 fields(g, TOP, sp["view"], "pX, the row: a BtreePayload, at rsi", base, 48, PAYLOAD, {base + i: v for i, v in enumerate(ROW_BYTES)}, STACK, sq_region, marks=(0, 1)) @@ -1213,11 +1380,12 @@ def frame_sqlite(name, late): roles={4: ("saved rbp", DIM)}) page = bytes(4096 - len(PAGE_END) - len(PAGE0)) # the middle of the page is not drawn: `more` page = PAGE0 + page + PAGE_END - kernel(g, TOP, sp["band"], [("pwrite64", 4096), ("pwrite64", 4096), ("fsync", 0), ("pwrite64", 12), - ("close", 0), ("fsync", 0), ("openat", 5), ("fsync", 0)], - "pwrite64", 18, [("rdi", "fd", "3", '"t.db"'), ("rsi", "buf", "538b28", None), - ("rdx", "count", "4096", None), ("r10", "offset", "4096", None), - ("rax", "returns", "4096", None)], PAGE0 + bytes(4)) + F = FAINT + older = [("fsync", 0), ("openat", 5), ("fsync", 0), ("close", 0), ("pwrite64", 12), ("fsync", 0), ("pwrite64", 4096)] + log = [[(c, VELLUM), ("(…) = ", F), (str(r), VELLUM)] for c, r in older] # their arguments: not in this mock's data + log.append([("pwrite64", VELLUM), ("(fd ", F), ("3", VELLUM), (' "t.db"', F), (", buf ", F), ("538b28", HEAP), + (", count ", F), ("4096", VELLUM), (", offset ", F), ("4096", VELLUM), (") = ", F), ("4096", "held")]) + calls_log(g, TOP, sp["band"], log) base = 0x538b28 took = {base + i: v for i, v in enumerate(PAGE0)} took.update({base + 0xfe0 + i: v for i, v in enumerate(PAGE_END)}) @@ -1230,6 +1398,160 @@ def frame_sqlite(name, late): g.save(name) +# ---- the call followed down: the syscalls scene's `open`, from main through the PLT and libc into the kernel ---- +# `ztrace dbg` on anim/tools/hello.c (cc -O0 -no-pie -Wl,-z,now; env -i setarch -R): moments 1 (at the call), +# 2 (in the stub), 4 (at libc's `syscall`), 5 (after it) and 6 (back in main). +HELLO = open(__file__.replace("design/mock/context.py", "anim/tools/hello.c")).read().split("\n") +R_ = VELLUM # read-only data: the string, and the GOT once relro has sealed it +HM = [(0x40117a, 4, "sub", "rsp, 0x40"), (0x40117e, 4, "mov", "rax, qword ptr fs:[0x28]"), + (0x401187, 4, "mov", "qword ptr [rbp - 8], rax"), (0x40118b, 4, "xor", "eax, eax"), + (0x40118d, 6, "lea", "rax, [rip + 0xe70]"), (0x401194, 6, "mov", "esi, 0"), (0x401199, 6, "mov", "rdi, rax"), + (0x40119c, 6, "mov", "eax, 0"), (0x4011a1, 6, "call", (0x401070, "open@plt")), + (0x4011a6, 6, "mov", "dword ptr [rbp - 0x38], eax"), (0x4011a9, 7, "lea", "rcx, [rbp - 0x30]"), + (0x4011ad, 7, "mov", "eax, dword ptr [rbp - 0x38]"), (0x4011b0, 7, "mov", "edx, 0x20"), + (0x4011b5, 7, "mov", "rsi, rcx"), (0x4011b8, 7, "mov", "edi, eax"), (0x4011ba, 7, "call", (0x401060, "read@plt")), + (0x4011bf, 7, "mov", "dword ptr [rbp - 0x34], eax")] +OPEN_ARGS = [("set", "rdi", [(0x402004, R_)], ("str", "note")), ("runs", [("rsi ", DIM), ("O_RDONLY", VELLUM)])] +STATIC = {0x40117e: ("runs", [("the stack's canary", DIM)]), # what a row says before it runs, and after + 0x40118d: ("runs", [("= ", FAINT), (0x402004, R_), (' "note"', LIT)]), + 0x4011ba: ("text", "read", VELLUM)} + + +def main_rows(first, n, states, notes): + out = [] + for a, line, mn, ops in HM: + if a < first or len(out) == n: + continue + parts = [(ops, CODE)] if isinstance(ops, tuple) else [(ops, P)] + out.append((a, line, mn, parts, states.get(a), notes.get(a, STATIC.get(a)))) + return out + + +LIBC_OPEN = [(0x7ffff7d10fb6, "xor", "r8d, r8d"), (0x7ffff7d10fb9, "mov", "rdi, 0xffffffffffffff9c"), + (0x7ffff7d10fc0, "mov", "eax, 0x101"), (0x7ffff7d10fc5, "syscall", ""), (0x7ffff7d10fc7, "mov", "rdx, rax"), + (0x7ffff7d10fca, "cmp", "rax, -0x1000"), (0x7ffff7d10fd0, "ja", 0x7ffff7d10fe8), + (0x7ffff7d10fd2, "mov", "eax, edx")] + + +def frame_descent(name): + """Mock 16: four instants of one call followed down, each the program column of the working frame (`beside`): + the source over the disassembly, as a descent of windows by call depth.""" + gen.W, gen.H = 3840, 1000 + g = Svg() + syscall_args = [("set", "rdi", [], ("int", -100)), ("set", "rsi", [(0x402004, R_)], ("str", "note")), + ("runs", [("rdx ", DIM), ("O_RDONLY", VELLUM)]), ("runs", [("r10 ", DIM), ("0", VELLUM)])] + call_c = ("runs", [("open", BRIGHT), ('("note", O_RDONLY)', VELLUM)]) + instants = [ + ("1", "at the call", "moment 1", None, "main", + [("main", None, main_rows(0, 17, {0x401194: "trail2", 0x401199: "trail1", 0x40119c: "trail0", 0x4011a1: "cur"}, + {0x401194: ("set", "rsi", [], ("int", 0)), + 0x401199: ("set", "rdi", [(0x402004, R_)], ("str", "note")), + 0x40119c: ("set", "rax", [], ("int", 0)), + 0x4011a1: ("runs", [("open", BRIGHT), (" in libc, by the PLT", FAINT)]), + 0x4011a6: OPEN_ARGS[0], 0x4011a9: OPEN_ARGS[1]}))], + ["main calls open by its stub in the PLT. The arguments", + 'are in rdi and rsi: "note" and O_RDONLY.']), + ("2", "in the stub", "moment 2", "main waits at line 6 · the pc is in the PLT", "open@plt · the PLT", + [("main", None, main_rows(0x401187, 13, {0x401199: "trail2", 0x40119c: "trail1", 0x4011a1: "wait"}, + {0x4011a1: call_c})), + ("open@plt", "the PLT · its jmp reads open@got, 403fd8", + [(0x401070, None, "jmp", [("qword ptr [rip + 0x2f62]", P)], "cur", + ("runs", [("", "arrow"), (0x7ffff7d10ec0, CODE), (" open", VELLUM)])), + (0x401076, None, "push", [("4", P)], None, ("text", "lazy binding: unused", FAINT)), + (0x40107b, None, "jmp", [(0x401020, CODE)], None, None)])], + ["The call pushed main+48 and went down: main's window keeps", + "its call, ringed; the stub's opens under it, and its jmp reads the GOT."]), + ("3", "the crossing", "moments 4 and 5", "main waits at line 6 · the pc is in libc's open", + "open · libc.so.6", + [("main", None, main_rows(0x40119c, 3, {0x40119c: "trail2", 0x4011a1: "wait"}, {0x4011a1: call_c})), + ("open@plt", "the PLT, jumped through", [(0x401070, None, "jmp", [("qword ptr [rip + 0x2f62]", P)], "pass", + ("runs", [("", "arrow"), (0x7ffff7d10ec0, CODE), (" open", VELLUM)]))]), + ("open", "libc.so.6 · no source", + [(a, None, mn, [(ops, CODE)] if isinstance(ops, int) else ([(ops, P)] if ops else []), + {0x7ffff7d10fb6: "trail2", 0x7ffff7d10fb9: "trail1", 0x7ffff7d10fc0: "trail0", 0x7ffff7d10fc5: "cur"}.get(a), + {0x7ffff7d10fb6: ("set", "r8", [], ("int", 0)), + 0x7ffff7d10fb9: ("runs", [("rdi ", DIM), ("-100", VELLUM), (" AT_FDCWD", FAINT)]), + 0x7ffff7d10fc0: ("runs", [("rax ", DIM), ("0x101", VELLUM), (" 257", FAINT)]), + 0x7ffff7d10fc5: ("runs", [("openat", BRIGHT), (" nr 257", FAINT)]), + 0x7ffff7d10fc7: syscall_args[0], 0x7ffff7d10fca: syscall_args[1], + 0x7ffff7d10fd0: syscall_args[2], 0x7ffff7d10fd2: syscall_args[3]}.get(a)) + for a, mn, ops in LIBC_OPEN]), + ("kernel", "openat, system call 257", + [[("openat(AT_FDCWD, \"note\", O_RDONLY, 0)", PAGE)], + [("returns ", gen.KERNEL_DIM), ("3", "struck"), (' "note"', PAGE), (" rcx, r11: clobbered", gen.KERNEL_DIM)]])], + ["libc's open is a wrapper: it puts openat's number in rax", + "and its arguments in rdi, rsi, rdx, r10; syscall crosses down."]), + ("4", "back up", "moment 6", None, "main", + [("main", None, main_rows(0, 17, {0x401199: "trail2", 0x40119c: "trail1", 0x4011a1: "trail0", 0x4011a6: "cur"}, + {0x4011a1: ("runs", [("open", VELLUM), ('("note", O_RDONLY) = ', DIM), ("3", VELLUM)]), + 0x4011a6: ("chain", [(0x7fffffffecc8, S_)], ("int", 0), None)}))], + ["open's ret came straight back to main+48: the stub was a jump,", + "not a call. The windows close up, and eax holds 3, the descriptor."]), + ] + for k, (num, title, when, waiting, note, windows, cap) in enumerate(instants): + dx = k * 928 + g.o.append(f'') + g.t(LX, 40, num, 28, GOLD, "sansb") + g.t(LX + 28, 40, title, 28, VELLUM, "sansm") + g.t(LX + CW, 40, when, 20, FAINT, "sans", "end") + ys = source(g, 64, 8, HELLO, 3, 6, [] if waiting else [], "hello.c", 11, waiting=waiting) + at = descent(g, 300, windows, note=note) + run = [a for a in (0x40118d, 0x401194, 0x401199, 0x40119c, 0x4011a1, 0x4011a6) if a in at] + thread(g, ys, [(at[run[0]], at[run[-1]] + ROW)], *((VELLUM, 2) if waiting else (GOLD, 3))) + for i, line in enumerate(cap): + g.t(LX, 800 + i * 40, line, 28, VELLUM, "sans") + g.o.append("") + g.save(name) + + +def frame_operands(name): + """Mock 17: real rows, as the disassembler writes them and as canim draws them: no size words, and a + `width` where no register of the instruction says the size; then the row's detail on rows of the strip.""" + gen.W, gen.H = 1920, 640 + g = Svg() + L = 0x7ffff7c00000 # libc's base in the recording + rows = [(0x100191b, "nop", "dword ptr [rax + rax]", "list"), (0x1001926, "mov", "rdi, qword ptr [rdi + 8]", "list"), + (0x100192a, "mov", "qword ptr [rax + 8], rcx", "list"), (0x40117e, "mov", "rax, qword ptr fs:[0x28]", "hello"), + (0x4011a6, "mov", "dword ptr [rbp - 0x38], eax", "hello"), + (0x401070, "jmp", "qword ptr [rip + 0x2f62]", "hello"), + (L + 0x110f07, "mov", "eax, dword ptr [r11 + 0x308]", "libc"), + (L + 0x110f0e, "cmp", "byte ptr [rip + 0x11018b], 0", "libc"), + (L + 0x110f94, "mov", "dword ptr [rbp - 0x60], 0x10", "libc"), + (L + 0x110f9b, "movsxd", "r10, dword ptr [rbp - 0x30]", "libc"), + (L + 0x110f85, "mov", "dword ptr fs:[rax], edx", "libc"), + (L + 0x110fe5, "nop", "dword ptr [rax]", "libc")] + for k, (title, sizes) in enumerate([("as the disassembler writes them", True), ("as canim draws them", False)]): + x0 = 64 + k * 912 + g.o.append(f'') + y0 = panel(g, LX, 48, CW, len(rows), title, focus=not sizes, note="list, hello, libc's open") + for i, (a, mn, ops, where) in enumerate(rows): + yy = y0 + i * ROW + addr(g, LX + DX_ADDR, yy, a, CODE) # libc's twelve digits move the columns after them by 60 + g.t(LX + DX_MN + 60, b(yy), mn, S, FAINT if mn == "nop" else KW, MONO if mn == "nop" else BOLD) + operands(g, LX + DX_OPS + 60, yy, mn, ops, VELLUM, sizes) + g.o.append("") + detail = [ + (0x40117e, "mov", "rax, qword ptr fs:[0x28]", ("runs", [("the stack's canary ", DIM), ("191a6a1f012bed00", VELLUM)])), + (0x40118d, "lea", "rax, [rip + 0xe70]", ("runs", [("rip + 0xe70 = ", FAINT), (0x402004, R_), (' "note"', LIT)])), + (0x401070, "jmp", "qword ptr [rip + 0x2f62]", + ("runs", [("", "arrow"), (0x7ffff7d10ec0, CODE), (" open", VELLUM), (" · read from open@got, ", FAINT), + (0x403fd8, R_)])), + (L + 0x110f0e, "cmp", "byte ptr [rip + 0x11018b], 0", + ("runs", [("1", VELLUM), (" : ", FAINT), ("0", VELLUM), (" ", FAINT), ("zf", DIM), (" ", FAINT)])), + (L + 0x110f15, "jne", L + 0x110fb0, ("runs", [("taken", VELLUM), (" · reads ", FAINT), ("zf", DIM), + (" → 7ffff7d10fb0 open+240", DIM)])), + (L + 0x110fca, "cmp", "rax, -0x1000", ("runs", [("3", VELLUM), (" : ", FAINT), ("-4096", VELLUM), (" ", FAINT), + ("CF", "chip"), ("zf", DIM)])), + (L + 0x110fd0, "ja", L + 0x110fe8, ("runs", [("not taken", DIM), (" · reads ", FAINT), ("CF", "chip"), + ("zf", DIM)])), + ] + y0 = panel(g, LX, 48 + ph(len(rows)) + 36, CW * 2 + 32, len(detail), "the row's detail", + note="what a row says: a name for a known place, lea's sum, the slot a jump goes through, a compare's two values and the flags it leaves") + insn_rows(g, y0, [(a, None, mn, [(ops, CODE)] if isinstance(ops, int) else [(ops, P)], None, note) + for a, mn, ops, note in detail], None, lead=0) + g.save(name) + + if __name__ == "__main__": assert ph(8) + 12 + ph(17) == ph(17) + 12 + ph(8) == BOTTOM - TOP - 152 - 12 # lens beside assert ph(10) + 12 + ph(9) == BOTTOM - TOP - 296 - 12 # lens as subject: the drawing @@ -1240,3 +1562,5 @@ if __name__ == "__main__": frame_calls("12-calls-subject") frame_sqlite("13-sqlite-row", False) frame_sqlite("14-sqlite-page", True) + frame_descent("16-descent") + frame_operands("17-operands") diff --git a/design/mock/heap.c b/design/mock/heap.c new file mode 100644 index 0000000..3c95f07 --- /dev/null +++ b/design/mock/heap.c @@ -0,0 +1,31 @@ +#include + +typedef struct node { + long value; + struct node *next; +} node; + +node *head; + +void push(long value) { + node *n = malloc(sizeof *n); + n->value = value; + n->next = head; + head = n; +} + +void pop(void) { + node *n = head; + head = n->next; + free(n); +} + +int main(void) { + push(7); + push(12); + push(9); + pop(); + pop(); + push(25); + return 0; +} diff --git a/design/mock/heap.py b/design/mock/heap.py new file mode 100644 index 0000000..e4c3e3a --- /dev/null +++ b/design/mock/heap.py @@ -0,0 +1,229 @@ +#!/usr/bin/env python3 +"""Mock 18: libc's heap from the inside, a storyboard of five instants, drawn with the blocks the language has. + + python3 -B heap.py # writes 18-heap.svg + +The program is heap.c (three pushes, two pops, a push). The data is a fresh recording: `ztrace dbg` on it (cc -O0 +-no-pie, glibc 2.44, env -i setarch -R), stopped after each malloc and free returns, `x 0x405000 1024` at each +stop. Each instant is the band as the scene's `subject` split draws it (the drawing, its view of seven rows) and +the loupe on the tcache under the view; the caption under it is the scene's. +""" +import gen +import dense as D +import context as C +from context import (PAGE, RAISED, RULE, VELLUM, BRIGHT, DIM, FAINT, GOLD, HEAP, DATA, A, S, ROW, TAG, PAD, + MONO, BOLD, LX, RX, CW, b, ph, mix, Svg) + +BASE = 0x405000 + + +def rows(*hexrows): + """{address: byte} from rows of 32 hex digits, from BASE.""" + out = {} + for r, h in enumerate(hexrows): + for i, v in enumerate(bytes.fromhex(h.replace(" ", ""))): + out[BASE + 16 * r + i] = v + return out + + +Z, SZ = "00" * 8, "2100000000000000" +KEY = "538903 0acfc59f6b" +# the heap's first seven rows at each stop, as `x` printed them +MEM = { + "A": rows(Z + SZ, "00" * 16, Z + "e10f020000000000", "00" * 16, "00" * 16, "00" * 16, "00" * 16), + "B": rows(Z + SZ, "0700000000000000" + Z, Z + SZ, "0c00000000000000" + "1050400000000000", Z + SZ, + "0900000000000000" + "3050400000000000", Z + "a10f020000000000"), + "C": rows(Z + SZ, "0700000000000000" + Z, Z + SZ, "0c00000000000000" + "1050400000000000", Z + SZ, + "0504000000000000" + KEY, Z + "0103000000000000"), + "D": rows(Z + SZ, "0700000000000000" + Z, Z + SZ, "5554400000000000" + KEY, Z + SZ, + "0504000000000000" + KEY, Z + "0103000000000000"), + "E": rows(Z + SZ, "0700000000000000" + Z, Z + SZ, "5554400000000000" + Z, Z + SZ, + "0504000000000000" + KEY, Z + "0103000000000000"), +} +# the tcache: num_slots[0] at 405070 (slots left for 0x20: 16 less the chunks in the bin), entries[0] at 405108 +TC = {"C": (0x0f, 0x405050), "D": (0x0e, 0x405030), "E": (0x0f, 0x405050)} + + +def region(v): + return HEAP if 0x405000 <= v < 0x426000 else DATA if 0x404000 <= v < 0x405000 else None + + +def chunk_fields(inst): + """The view's fields: each chunk's size word (`size`: the size and its flags), and its body as the program + has it (a node's value and next) or as malloc does (the protected link and the key).""" + free = {"C": {0x405050}, "D": {0x405030, 0x405050}, "E": {0x405050}}.get(inst, set()) + out = [] + for c in (0x405000, 0x405020, 0x405040): + body = c + 16 - BASE + if inst == "A" and c != 0x405000: + continue + out.append((c + 8 - BASE, 8, "size", False, None)) + if c + 16 in free: + out += [(body, 8, "protected", False, None), (body + 8, 8, "bytes", False, None, "key")] + elif inst == "E" and c == 0x405020: # malloc's again the program's, not yet written: the stale link + out += [(body, 8, "bytes", False, None, "the old link"), (body + 8, 8, "pointer", False, None)] + else: + out += [(body, 8, "number", False, None), (body + 8, 8, "pointer", False, None)] + top = 0x405020 if inst == "A" else 0x405060 + out.append((top + 8 - BASE, 8, "size", False, None, "tcache" if inst in "CDE" else "top")) + return out + + +def node(g, x, y, value=None, nxt=None, xs=None, free=False, stale=None, k=1): + """A chunk as the `nodes` drawing has it: the value's half and the pointer's. Free: the first word is the + link now (its square, its arrow) and the second the key, hatched; the outline dashed, as `freed`.""" + NW, NH, VW = 136, 44, 88 + if free: + g.rect(x, y, VW - 2, NH, C.WELL) + g.rect(x + VW, y, NW - VW, NH, "url(#hatch)") + g.t(x + VW + (NW - VW) / 2, y + 17, "key", S, FAINT, MONO, "middle") # over the link's line + g.frame(x - 3, y - 3, NW + 6, NH + 6, DIM, 2, "12 8") + cx, cy = x + VW / 2, y + NH / 2 + else: + g.rect(x, y, VW - 2, NH, RAISED) + if stale is not None: # allocated, not yet written: what the bytes still hold + g.t(x + VW / 2 - 1, b(y + 10), "%x" % stale, S, FAINT, MONO, "middle") + else: + g.t(x + VW / 2 - 1, y + NH / 2 + 0.36 * 25, str(value), 25, VELLUM, MONO, "middle") + g.rect(x + VW, y, NW - VW, NH, C.WELL) + cx, cy = x + VW + (NW - VW) / 2, y + NH / 2 + if nxt: + g.rect(cx - 5, cy - 5, 10, 10, HEAP) + gen.arrow(g, [(cx + 8, cy), (xs[nxt] - 3, cy)], HEAP, 3, hl=12, hw=10) + elif nxt == 0: + sx = x + 10 if free else x + VW + 10 + g.path(f"M{sx},{y + NH - 8} L{sx + (VW if free else NW - VW) - 20},{y + 8}", DIM, 2) + + +def drawing(g, y, h, inst): + """The heap's chunks as two lists: the program's, from `head`, over malloc's bin for 0x20, from the tcache. + A chunk moves between the rows; its bytes are the view's.""" + C.tag(g, LX, y, CW, h, "the chunks of 0x20", focus=True, note="the program's list · malloc's free list") + NW, P0, PITCH = 136, LX + 200, 160 + y1, y2 = y + TAG + 52, y + TAG + 162 + top = {"A": [0x405010], "B": [0x405050, 0x405030, 0x405010], "C": [0x405030, 0x405010], + "D": [0x405010], "E": [0x405030, 0x405010]}[inst] + tbin = {"C": [0x405050], "D": [0x405030, 0x405050], "E": [0x405050]}.get(inst, []) + vals = {0x405010: 7, 0x405030: 12, 0x405050: 9} + links = {"B": {0x405050: 0x405030, 0x405030: 0x405010, 0x405010: 0}, "C": {0x405030: 0x405010, 0x405010: 0}, + "D": {0x405010: 0}, "E": {0x405010: 0}}.get(inst, {}) + g.t(LX + 16, b(y1 + 10), "the program's", 18, FAINT, "sans") + xs = {a: P0 + i * PITCH for i, a in enumerate(top)} + for a in top: + g.t(xs[a] + NW / 2, y1 - 8, "%x" % a, 20, HEAP, MONO, "middle") + for a in top: + if inst == "A" or (inst == "E" and a == 0x405030): + node(g, xs[a], y1, stale=0 if inst == "A" else 0x405455, nxt=None, xs=xs) + else: + node(g, xs[a], y1, vals[a], links[a], xs) + # malloc's row: the bin's head, a box with the count on its shoulder, then the free chunks, then the top + bx = LX + 16 + if inst in "AB": # glibc 2.44 makes the tcache at the first free; till then a dummy in libc + g.frame(bx, y2, 160, 44, FAINT, 2, "12 8") + g.t(bx + 80, y2 + 22 + 0.36 * 20, "dummy tcache", S, FAINT, MONO, "middle") + else: + g.rect(bx, y2, 160, 44, RAISED) + g.t(bx + 80, y2 + 22 + 0.36 * 20, "tcache 0x20", S, VELLUM, MONO, "middle") + g.rect(bx + 160 - 18, y2 - 26, 30, 30, PAGE) + g.frame(bx + 160 - 18, y2 - 26, 30, 30, FAINT, 2) + g.t(bx + 160 - 3, y2 - 11 + 0.36 * 25, str(len(tbin)), 25, VELLUM, MONO, "middle") + xb = {a: P0 + i * PITCH for i, a in enumerate(tbin)} + if tbin: + gen.arrow(g, [(bx + 160, y2 + 22), (xb[tbin[0]] - 3, y2 + 22)], HEAP, 3, hl=12, hw=10) + for i, a in enumerate(tbin): + g.t(xb[a] + NW / 2, y2 - 8, "%x" % a, 20, HEAP, MONO, "middle") + node(g, xb[a], y2, free=True, nxt=tbin[i + 1] if i + 1 < len(tbin) else 0, xs=xb) + tx = LX + 680 + g.rect(tx, y2, 164, 44, RAISED) + g.frame(tx, y2, 164, 44, FAINT, 2, "12 8") + size = {"A": 0x20fe0, "B": 0x20fa0}.get(inst, 0x20ca0) + g.t(tx + 12, y2 + 22 + 0.36 * 20, "top", S, DIM, MONO) + g.t(tx + 152, y2 + 22 + 0.36 * 20, "0x%x" % size, S, VELLUM, MONO, "end") + g.t(tx + 82, y2 - 8, "%x" % (0x405020 if inst == "A" else 0x405060 if inst == "B" else 0x405360), 20, HEAP, MONO, + "middle") + # pins: a variable in the ink of where it lives (head, a global: data), a register in primary + pins = {"A": [(top, 0x405010, ["rax"])], "B": [(top, 0x405050, ["head"])], + "C": [(top, 0x405030, ["head"]), (tbin, 0x405050, ["rdi"])], + "D": [(top, 0x405010, ["head"]), (tbin, 0x405030, ["rdi"])], + "E": [(top, 0x405030, ["rax"]), (top, 0x405010, ["head"])]}[inst] + for row, a, names in pins: + x0 = (xs if row is top else xb)[a] + yy = (y1 if row is top else y2) + 44 + C.pin_up(g, x0 + NW / 2, yy, names, HEAP, 20) + if names == ["head"]: # a global's name in data's ink: redraw it over the primary one + g.t(x0 + NW / 2 - 9, yy + 8 + 20 * 0.86, "head", 20, DATA, BOLD, "end") + + +def loupe(g, x, y, inst, changed): + """The tcache, opened from the row of the chunk it is: its count for 0x20 and its head.""" + h = ph(5) + g.rect(x + 12, y + 16, CW, h, D.SHADE) + g.rect(x, y, CW, h, D.LIFT) + g.rect(x, y, CW, TAG, D.LIFT_TAG) + g.frame(x, y, CW, h, DIM, 2) + xx = x + 12 + 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") + slots, head = TC[inst] + base = 0x405070 + mem = {base + i: v for i, v in enumerate(bytes([slots, 0] + [0x10, 0] * 7))} + mem.update({base + 0x90 + i: v for i, v in enumerate(bytes([0x10, 0] * 4) + head.to_bytes(8, "little"))}) + 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, + 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 = [ + ("A", "1", "the first malloc", "stop 1, back from malloc in push(7)", + ["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."]), + ("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."]), + ("D", "4", "a second free", "stop 8, back from free in the second pop", + ["The second chains to the first: 405455 is 405050 xor 405, the", + "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."]), +] +CHANGED = { # the bytes each stop changed, of those on screen: struck + "C": {0x405050, 0x405051} | set(range(0x405058, 0x405060)) | {0x405068, 0x405069, 0x40506a}, + "D": {0x405030, 0x405031, 0x405032} | set(range(0x405038, 0x405040)), + "E": set(range(0x405038, 0x405040)), +} +LOUPE_CHANGED = {"C": {0x405070} | {0x405108, 0x405109, 0x40510a}, "D": {0x405070, 0x405108}, "E": {0x405070, 0x405108}} + + +def storyboard(name): + gen.W, gen.H = 1920, 5 * 560 + 40 + g = Svg() + for k, (inst, num, title, when, cap) in enumerate(INSTANTS): + dy = k * 560 + g.o.append(f'') + g.t(LX, 34, num, 28, GOLD, "sansb") + g.t(LX + 28, 34, title, 28, VELLUM, "sansm") + g.t(LX + 1792, 34, when, 20, FAINT, "sans", "end") + 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]) + for i, line in enumerate(cap): + g.t(LX, 440 + i * 44, line, 32, VELLUM, "sans") + g.o.append("") + g.save(name) + + +if __name__ == "__main__": + assert 0x405455 ^ (0x405030 >> 12) == 0x405050 and 0x405 ^ (0x405050 >> 12) == 0 # safe-linking, as stored + storyboard("18-heap") diff --git a/design/mock/map.py b/design/mock/map.py index 8e91ffa..aaf90e6 100644 --- a/design/mock/map.py +++ b/design/mock/map.py @@ -102,7 +102,7 @@ def draw_map(g, step, loupe, subject=False): 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") + C.backtrace(g, 1456, ph(5), D.BT, x=col[0], note="newest at the bottom") key(g, col[0], 1620, step) # B: 69 instructions, the function and its neighbours D.put(col[1]) -- cgit v1.3