From f8740f6996eefc235af066a6686f52847c158a6b Mon Sep 17 00:00:00 2001 From: Gabriel Schneider Date: Tue, 10 Nov 2026 14:12:42 -0300 Subject: design, revision 2: the whole context in every frame, a lens beside it The user's premise: show what pwndbg shows, hide nothing; the semantic views stay as a lens on the machine. So every frame holds the registers (all, each followed as a pointer), the stack telescope and the backtrace in a column that never changes, and the source, the disassembly and the scene's lens in the other. A value's ink says where it points (stack, heap, code, data; rwx underlined), its paper says what happened to it. JetBrains Mono NL for everything mono; the floor is mono 20, which a phone does not read: what narration depends on is also in the caption. Mocks 9 to 11 are drawn from a real recording of targets/list. --- design/mock/01-fib-recursion-480.png | Bin 29901 -> 29176 bytes design/mock/01-fib-recursion.png | Bin 130973 -> 123592 bytes design/mock/01-fib-recursion.svg | 346 ++++++++++---------- design/mock/02-list-insert-480.png | Bin 28715 -> 27754 bytes design/mock/02-list-insert.png | Bin 142848 -> 134027 bytes design/mock/02-list-insert.svg | 414 +++++++++++------------ design/mock/03-syscall-write-480.png | Bin 29669 -> 28174 bytes design/mock/03-syscall-write.png | Bin 135294 -> 131312 bytes design/mock/03-syscall-write.svg | 338 +++++++++---------- design/mock/04-malloc-glibc-musl-480.png | Bin 25492 -> 25358 bytes design/mock/04-malloc-glibc-musl.png | Bin 124884 -> 120730 bytes design/mock/04-malloc-glibc-musl.svg | 300 ++++++++--------- design/mock/05-memory-write-480.png | Bin 33443 -> 31517 bytes design/mock/05-memory-write.png | Bin 168817 -> 156153 bytes design/mock/05-memory-write.svg | 526 +++++++++++++++--------------- design/mock/06-title-legend-480.png | Bin 19214 -> 19092 bytes design/mock/06-title-legend.png | Bin 85492 -> 85774 bytes design/mock/06-title-legend.svg | 127 ++++---- design/mock/07-sort-swap-480.png | Bin 19377 -> 19040 bytes design/mock/07-sort-swap.png | Bin 88330 -> 85498 bytes design/mock/07-sort-swap.svg | 132 ++++---- design/mock/08-far-view-480.png | Bin 29949 -> 28963 bytes design/mock/08-far-view.png | Bin 157996 -> 148084 bytes design/mock/08-far-view.svg | 212 ++++++------ design/mock/09-context-480.png | Bin 0 -> 41745 bytes design/mock/09-context.png | Bin 0 -> 225572 bytes design/mock/09-context.svg | 536 ++++++++++++++++++++++++++++++ design/mock/10-context-step-480.png | Bin 0 -> 43219 bytes design/mock/10-context-step.png | Bin 0 -> 229646 bytes design/mock/10-context-step.svg | 543 +++++++++++++++++++++++++++++++ design/mock/11-context-lens-480.png | Bin 0 -> 44506 bytes design/mock/11-context-lens.png | Bin 0 -> 225033 bytes design/mock/11-context-lens.svg | 524 +++++++++++++++++++++++++++++ design/mock/context.py | 480 +++++++++++++++++++++++++++ design/mock/gen.py | 227 +++++++------ design/mock/list.c | 42 +++ 36 files changed, 3453 insertions(+), 1294 deletions(-) create mode 100644 design/mock/09-context-480.png create mode 100644 design/mock/09-context.png create mode 100644 design/mock/09-context.svg create mode 100644 design/mock/10-context-step-480.png create mode 100644 design/mock/10-context-step.png create mode 100644 design/mock/10-context-step.svg create mode 100644 design/mock/11-context-lens-480.png create mode 100644 design/mock/11-context-lens.png create mode 100644 design/mock/11-context-lens.svg create mode 100644 design/mock/context.py create mode 100644 design/mock/list.c (limited to 'design/mock') diff --git a/design/mock/01-fib-recursion-480.png b/design/mock/01-fib-recursion-480.png index 167fc50..11c845e 100644 Binary files a/design/mock/01-fib-recursion-480.png and b/design/mock/01-fib-recursion-480.png differ diff --git a/design/mock/01-fib-recursion.png b/design/mock/01-fib-recursion.png index aefa55a..a3737fa 100644 Binary files a/design/mock/01-fib-recursion.png and b/design/mock/01-fib-recursion.png differ diff --git a/design/mock/01-fib-recursion.svg b/design/mock/01-fib-recursion.svg index 1cb0378..b96f70a 100644 --- a/design/mock/01-fib-recursion.svg +++ b/design/mock/01-fib-recursion.svg @@ -2,7 +2,7 @@ Recursion -fib(5) +fib(5) kernel memory @@ -17,56 +17,56 @@ fib.c -1 -#include -<stdio.h> -2 +1 +#include +<stdio.h> +2 -3 -long -fib( -long -n) { +3 +long +fib( +long +n) { -4 -if -(n < -2 -) -5 -return -n; +4 +if +(n < +2 +) +5 +return +n; -6 -return -fib(n- -1 -) + fib(n- -2 -); -7 -} -8 -9 -int -main( -void -) { -10 -printf( -"%ld\n" -, fib( -5 -)); -11 -return -0 -; -12 -} +6 +return +fib(n- +1 +) + fib(n- +2 +); +7 +} +8 +9 +int +main( +void +) { +10 +printf( +"%ld\n" +, fib( +5 +)); +11 +return +0 +; +12 +} -n = 1 +n = 1 @@ -86,58 +86,58 @@ -5 +5 -4 +4 -3 +3 -3 +3 -3 +3 -2 +2 -2 +2 -1 +1 -2 +2 -2 +2 -1 +1 -1 +1 -1 +1 -1 +1 -1 +1 -0 +0 -0 +0 -1 +1 -1 +1 -1 +1 -0 +0 -0 +0 @@ -146,28 +146,28 @@ newest on top -fib -n = 1 -:4 +fib +n = 1 +:4 -fib -n = 2 -:6 +fib +n = 2 +:6 -fib -n = 3 -:6 +fib +n = 3 +:6 -fib -n = 5 -:6 +fib +n = 5 +:6 -main -:10 +main +:10 @@ -176,107 +176,107 @@ x86-64 -0 -0 -0 -0 -5 -5 -d -4 -a -1 -c -0 -1 -1 -4 -1 -rip +0 +0 +0 +0 +5 +5 +d +4 +a +1 +c +0 +1 +1 +4 +1 +rip -0 -0 -0 -0 -0 -0 -0 -0 -0 -0 -0 -0 -0 -0 -0 -1 -rax +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +1 +rax -0 -0 -0 -0 -0 -0 -0 -0 -0 -0 -0 -0 -0 -0 -0 -1 -rdi +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +1 +rdi -0 -0 -0 -0 -7 -f -f -d -3 -a -2 -c -1 -d -c -0 -rsp -0 -0 -0 -0 -7 -f -f -d -3 -a -2 -c -1 -d -e -0 -rbp +0 +0 +0 +0 +7 +f +f +d +3 +a +2 +c +1 +d +c +0 +rsp +0 +0 +0 +0 +7 +f +f +d +3 +a +2 +c +1 +d +e +0 +rbp -CF +CF -ZF +ZF -SF +SF -OF -Four calls to fib are still waiting, each with its own n. -The newest one just received 1 in rdi. +OF +Four calls to fib are still waiting, each with its own n. +The newest one just received 1 in rdi. diff --git a/design/mock/02-list-insert-480.png b/design/mock/02-list-insert-480.png index 8cc538f..18d09e3 100644 Binary files a/design/mock/02-list-insert-480.png and b/design/mock/02-list-insert-480.png differ diff --git a/design/mock/02-list-insert.png b/design/mock/02-list-insert.png index 6e99916..d39cd1a 100644 Binary files a/design/mock/02-list-insert.png and b/design/mock/02-list-insert.png differ diff --git a/design/mock/02-list-insert.svg b/design/mock/02-list-insert.svg index 878740d..734a161 100644 --- a/design/mock/02-list-insert.svg +++ b/design/mock/02-list-insert.svg @@ -2,7 +2,7 @@ Linked lists -insert after a node +insert after a node kernel memory @@ -17,79 +17,79 @@ list.c -3 -struct -node { -4 -long -value; -5 -struct -node *next; -6 -}; +3 +struct +node { +4 +long +value; +5 +struct +node *next; +6 +}; -21 -n = malloc( -sizeof -*n); +21 +n = malloc( +sizeof +*n); -22 -n->value = v; +22 +n->value = v; -23 -n->next = at->next; +23 +n->next = at->next; -24 -at->next = n; -25 -return -n; +24 +at->next = n; +25 +return +n; locals -at -0 -0 -0 -0 -5 -5 -d -4 -a -1 -c -0 -3 -2 -a -0 +at +0 +0 +0 +0 +5 +5 +d +4 +a +1 +c +0 +3 +2 +a +0 node 7 -n -0 -0 -0 -0 -5 -5 -d -4 -a -1 -c -0 -3 -2 -e -0 +n +0 +0 +0 +0 +5 +5 +d +4 +a +1 +c +0 +3 +2 +e +0 node 9 -v -9 +v +9 @@ -97,29 +97,29 @@ the list what the code means -head - - +head + + -7 +7 -12 +12 -9 +9 - + - - - - -at -n + + + + +at +n the end @@ -131,156 +131,156 @@ malloc's own 16 bytes -55d4 -a1c0 -32a0 +55d4 +a1c0 +32a0 -0 -0 -0 -0 -0 -0 -0 -0 -0 -0 -0 -0 -0 -0 -0 -7 -value -= 7 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +7 +value += 7 -at +at -55d4 -a1c0 -32a8 +55d4 +a1c0 +32a8 -0 -0 -0 -0 -5 -5 -d -4 -a -1 -c -0 -3 -2 -e -0 -next +0 +0 +0 +0 +5 +5 +d +4 +a +1 +c +0 +3 +2 +e +0 +next -55d4 -a1c0 -32c0 +55d4 +a1c0 +32c0 -0 -0 -0 -0 -0 -0 -0 -0 -0 -0 -0 -0 -0 -0 -0 -c -value -= 12 -55d4 -a1c0 -32c8 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +c +value += 12 +55d4 +a1c0 +32c8 -0 -0 -0 -0 -0 -0 -0 -0 -0 -0 -0 -0 -0 -0 -0 -0 -next -= NULL +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +next += NULL -55d4 -a1c0 -32e0 +55d4 +a1c0 +32e0 -0 -0 -0 -0 -0 -0 -0 -0 -0 -0 -0 -0 -0 -0 -0 -9 -value -= 9 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +9 +value += 9 -n +n -55d4 -a1c0 -32e8 +55d4 +a1c0 +32e8 -0 -0 -0 -0 -5 -5 -d -4 -a -1 -c -0 -3 -2 -c -0 -next +0 +0 +0 +0 +5 +5 +d +4 +a +1 +c +0 +3 +2 +c +0 +next - - - - - - -Inserting moves nothing. One word changes: the next field of at + + + + + + +Inserting moves nothing. One word changes: the next field of at now holds the address of the new node. diff --git a/design/mock/03-syscall-write-480.png b/design/mock/03-syscall-write-480.png index ebaf2af..9aaa0b1 100644 Binary files a/design/mock/03-syscall-write-480.png and b/design/mock/03-syscall-write-480.png differ diff --git a/design/mock/03-syscall-write.png b/design/mock/03-syscall-write.png index b56f743..fa37cd5 100644 Binary files a/design/mock/03-syscall-write.png and b/design/mock/03-syscall-write.png differ diff --git a/design/mock/03-syscall-write.svg b/design/mock/03-syscall-write.svg index 140369e..5be4d5b 100644 --- a/design/mock/03-syscall-write.svg +++ b/design/mock/03-syscall-write.svg @@ -3,7 +3,7 @@ System calls -write(1, msg, 14) +write(1, msg, 14) kernel memory @@ -20,28 +20,28 @@ hello.c -11 -write( -1 -, msg, -14 -); +11 +write( +1 +, msg, +14 +); -mov -eax, 1 -mov -edi, 1 -lea -rsi, [msg] -mov -edx, 14 +mov +eax, 1 +mov +edi, 1 +lea +rsi, [msg] +mov +edx, 14 -syscall -12 -return -0 -; +syscall +12 +return +0 +; @@ -50,99 +50,99 @@ x86-64 -0 -0 -0 -0 -5 -5 -d -4 -a -1 -c -0 -1 -1 -5 -c -rip +0 +0 +0 +0 +5 +5 +d +4 +a +1 +c +0 +1 +1 +5 +c +rip -0 -0 -0 -0 -0 -0 -0 -0 -0 -0 -0 -0 -0 -0 -0 -1 -rax +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +1 +rax -0 -0 -0 -0 -0 -0 -0 -0 -0 -0 -0 -0 -0 -0 -0 -1 -rdi +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +1 +rdi -0 -0 -0 -0 -5 -5 -d -4 -a -1 -c -0 -2 -0 -0 -4 -rsi +0 +0 +0 +0 +5 +5 +d +4 +a +1 +c +0 +2 +0 +0 +4 +rsi -0 -0 -0 -0 -0 -0 -0 -0 -0 -0 -0 -0 -0 -0 -0 -e -rdx +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +e +rdx which call 1st 2nd @@ -154,54 +154,54 @@ memory read-only data -a1c0 -2004 +a1c0 +2004 -H -48 +H +48 -e -65 +e +65 -l -6c +l +6c -l -6c +l +6c -o -6f +o +6f -, -2c +, +2c -20 +20 -w -77 -a1c0 -200c +w +77 +a1c0 +200c -o -6f +o +6f -r -72 +r +72 -l -6c +l +6c -d -64 +d +64 -! -21 +! +21 -\n -0a +\n +0a -00 +00 @@ -212,7 +212,7 @@ -syscall +syscall @@ -220,74 +220,74 @@ what it runs -tty_write +tty_write -vfs_write +vfs_write -ksys_write -write +ksys_write +write system call 1 -rdi +rdi fd -1 +1 the terminal -rsi +rsi buf -5 -5 -d -4 -a -1 -c -0 -2 -0 -0 -4 +5 +5 +d +4 +a +1 +c +0 +2 +0 +0 +4 -rdx +rdx count -14 +14 bytes -rax +rax returns not yet the terminal's buffer -H +H -e +e -l +l -l +l -o +o -, +, -w +w -o +o 9 of 14 copied The program cannot reach the terminal. It leaves four numbers -in registers, and syscall hands them to the kernel. +in registers, and syscall hands them to the kernel. diff --git a/design/mock/04-malloc-glibc-musl-480.png b/design/mock/04-malloc-glibc-musl-480.png index d69374f..fb44a6e 100644 Binary files a/design/mock/04-malloc-glibc-musl-480.png and b/design/mock/04-malloc-glibc-musl-480.png differ diff --git a/design/mock/04-malloc-glibc-musl.png b/design/mock/04-malloc-glibc-musl.png index 8d392dd..3145681 100644 Binary files a/design/mock/04-malloc-glibc-musl.png and b/design/mock/04-malloc-glibc-musl.png differ diff --git a/design/mock/04-malloc-glibc-musl.svg b/design/mock/04-malloc-glibc-musl.svg index a638d28..a2c1ca0 100644 --- a/design/mock/04-malloc-glibc-musl.svg +++ b/design/mock/04-malloc-glibc-musl.svg @@ -2,7 +2,7 @@ malloc: glibc and musl -heap.c:9 free(b); +heap.c:9 free(b); kernel memory @@ -23,18 +23,18 @@ -a +a -b +b -c +c @@ -46,100 +46,100 @@ inside b -0 -0 -0 -0 -0 -0 -0 -0 -0 -0 -0 -0 -0 -0 -2 -1 -size +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +2 +1 +size of this chunk -0 -0 -0 -0 -0 -0 -0 -5 -5 -d -4 -a -1 -c -0 -3 -next +0 +0 +0 +0 +0 +0 +0 +5 +5 +d +4 +a +1 +c +0 +3 +next free chunk: none -8 -f -3 -a -6 -1 -c -2 -d -0 -7 -b -4 -e -1 -9 -key +8 +f +3 +a +6 +1 +c +2 +d +0 +7 +b +4 +e +1 +9 +key marks it freed -0 -0 -0 -0 -0 -0 -0 -0 -0 -0 -0 -0 -0 -0 -2 -a +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +2 +a your old data -0x20 +0x20 -1 free -0x30 +1 free +0x30 -0 -0x40 +0 +0x40 -0 -0x50 +0 +0x50 -0 -0x60 +0 +0x60 -0 -0x70 +0 +0x70 -0 - - +0 + + its notes: a list for each size, kept in the heap itself @@ -151,7 +151,7 @@ -c +c @@ -160,12 +160,12 @@ -a +a -b +b @@ -177,95 +177,95 @@ -f -f -0 -0 -0 -0 -0 -0 +f +f +0 +0 +0 +0 +0 +0 its tag, cleared -0 -0 -0 -0 -0 -0 -0 -0 -0 -0 -0 -0 -0 -0 -2 -a +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +2 +a your old data, -0 -0 -0 -0 -0 -0 -0 -0 -0 -0 -0 -0 -0 -0 -0 -0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 all of it untouched can hand out -0 +0 -0 +0 -1 +1 -1 +1 -1 +1 -1 +1 -1 +1 -1 +1 -1 +1 -1 +1 freed -0 +0 -1 +1 -0 +0 -0 +0 -0 +0 -0 +0 -0 +0 -0 +0 -0 +0 -0 -a -b +0 +a +b one bit for each slot its notes: one record for each group, kept apart glibc threads its free list through the freed chunk itself. diff --git a/design/mock/05-memory-write-480.png b/design/mock/05-memory-write-480.png index 37f6caf..04a6308 100644 Binary files a/design/mock/05-memory-write-480.png and b/design/mock/05-memory-write-480.png differ diff --git a/design/mock/05-memory-write.png b/design/mock/05-memory-write.png index 370aa82..87bf68d 100644 Binary files a/design/mock/05-memory-write.png and b/design/mock/05-memory-write.png differ diff --git a/design/mock/05-memory-write.svg b/design/mock/05-memory-write.svg index 2d0d264..1f37326 100644 --- a/design/mock/05-memory-write.svg +++ b/design/mock/05-memory-write.svg @@ -2,7 +2,7 @@ Where a pointer lives -hello.c:8 +hello.c:8 kernel memory @@ -18,35 +18,35 @@ hello.c -7 -char -buf[ -16 -] = -"Hello, world!\n" -; +7 +char +buf[ +16 +] = +"Hello, world!\n" +; -8 -char -*p = buf; +8 +char +*p = buf; -lea -rax, [rbp-0x20] +lea +rax, [rbp-0x20] -mov -[rbp-0x8], rax -9 -write( -1 -, p, -14 -); -10 -return -0 -; +mov +[rbp-0x8], rax +9 +write( +1 +, p, +14 +); +10 +return +0 +; @@ -55,113 +55,113 @@ x86-64 -0 -0 -0 -0 -5 -5 -d -4 -a -1 -c -0 -1 -1 -6 -d -rip +0 +0 +0 +0 +5 +5 +d +4 +a +1 +c +0 +1 +1 +6 +d +rip -0 -0 -0 -0 -7 -f -f -d -3 -a -2 -c -1 -e -4 -0 -rax -0 -0 -0 -0 -0 -0 -0 -0 -0 -0 -0 -0 -0 -0 -0 -1 -rdi -0 -0 -0 -0 -7 -f -f -d -3 -a -2 -c -1 -e -3 -0 -rsp -0 -0 -0 -0 -7 -f -f -d -3 -a -2 -c -1 -e -6 -0 -rbp +0 +0 +0 +0 +7 +f +f +d +3 +a +2 +c +1 +e +4 +0 +rax +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +1 +rdi +0 +0 +0 +0 +7 +f +f +d +3 +a +2 +c +1 +e +3 +0 +rsp +0 +0 +0 +0 +7 +f +f +d +3 +a +2 +c +1 +e +6 +0 +rbp stack from 7ffd 3a2c 1e30 · low addresses on top -+0 -+1 -+2 -+3 -+4 -+5 -+6 -+7 ++0 ++1 ++2 ++3 ++4 ++5 ++6 ++7 as text -main +main -3a2c -1e30 +3a2c +1e30 @@ -178,8 +178,8 @@ -3a2c -1e38 +3a2c +1e38 @@ -196,206 +196,206 @@ -3a2c -1e40 +3a2c +1e40 -48 +48 -65 +65 -6c +6c -6c +6c -6f +6f -2c +2c -20 +20 -77 -H -e -l -l -o -, - -w -buf -3a2c -1e48 +77 +H +e +l +l +o +, + +w +buf +3a2c +1e48 -6f +6f -72 +72 -6c +6c -64 +64 -21 +21 -0a +0a -00 +00 -00 -o -r -l -d -! -· -· -· -3a2c -1e50 +00 +o +r +l +d +! +· +· +· +3a2c +1e50 -00 +00 -3f +3f -9a +9a -c1 +c1 -77 +77 -d2 +d2 -8e +8e -5b -· -? -· -· -w -· -· -[ +5b +· +? +· +· +w +· +· +[ canary -3a2c -1e58 +3a2c +1e58 -40 +40 -1e +1e -2c +2c -3a +3a -fd +fd -7f +7f -00 +00 -00 -@ -· -, -: -· -· -· -· -p -3a2c -1e60 +00 +@ +· +, +: +· +· +· +· +p +3a2c +1e60 -00 +00 -1f +1f -2c +2c -3a +3a -fd +fd -7f +7f -00 +00 -00 -· -· -, -: -· -· -· -· +00 +· +· +, +: +· +· +· +· saved rbp -3a2c -1e68 +3a2c +1e68 -90 +90 -9d +9d -22 +22 -5e +5e -3c +3c -7f +7f -00 +00 -00 -· -· -" -^ -< -· -· -· +00 +· +· +" +^ +< +· +· +· return to -__libc_start_call_main +__libc_start_call_main - - - + + + - -rsp - + +rsp + - -rbp - + +rbp + the same eight bytes, low byte first, as one number -0 -0 -0 -0 -7 -f -f -d -3 -a -2 -c -1 -e -4 -0 - +0 +0 +0 +0 +7 +f +f +d +3 +a +2 +c +1 +e +4 +0 + = where -buf +buf starts -The pointer p is eight ordinary bytes on the stack. -Read as one number, they are the address of buf. +The pointer p is eight ordinary bytes on the stack. +Read as one number, they are the address of buf. diff --git a/design/mock/06-title-legend-480.png b/design/mock/06-title-legend-480.png index 7daa823..6d52227 100644 Binary files a/design/mock/06-title-legend-480.png and b/design/mock/06-title-legend-480.png differ diff --git a/design/mock/06-title-legend.png b/design/mock/06-title-legend.png index 696b59a..0139b1e 100644 Binary files a/design/mock/06-title-legend.png and b/design/mock/06-title-legend.png differ diff --git a/design/mock/06-title-legend.svg b/design/mock/06-title-legend.svg index 8e57394..f975a89 100644 --- a/design/mock/06-title-legend.svg +++ b/design/mock/06-title-legend.svg @@ -6,74 +6,73 @@ part 3 Where does malloc keep its notes? -glibc and musl, one free() at a time +glibc and musl, one free() at a time legend the same marks in every picture - -rip -current -executing now - -2a - -00 -written -by this step - - -2a - - -00 -changed -a moment ago - - -2a - - -00 -read -by this step - - - - -pointer -an address - - -moving -in transit - - -allocated -handed out - - -freed -handed back - - - - -not seen -never observed - -sys -kernel -the other side - - -SEGV -fault -signal or error - - -one object -in every view + +rip +current +executing now + +2a +written +by this step + + +2a +changed +a moment ago + + +2a +read +by this step + + +moving +in transit + + +not seen +never observed + +sys +kernel +the other side + + +SEGV +fault +signal, error +7ffe +stack +points into it +7ffe +heap +points into it +7ffe +code +points into it +7ffe +data +points into it +7f3c + +rwx +write and run +"hi" +text +a string + + +allocated +handed out + + +freed +handed back diff --git a/design/mock/07-sort-swap-480.png b/design/mock/07-sort-swap-480.png index a2b75f0..cee41ba 100644 Binary files a/design/mock/07-sort-swap-480.png and b/design/mock/07-sort-swap-480.png differ diff --git a/design/mock/07-sort-swap.png b/design/mock/07-sort-swap.png index e6127b2..9ff7153 100644 Binary files a/design/mock/07-sort-swap.png and b/design/mock/07-sort-swap.png differ diff --git a/design/mock/07-sort-swap.svg b/design/mock/07-sort-swap.svg index 6e866e5..17fec50 100644 --- a/design/mock/07-sort-swap.svg +++ b/design/mock/07-sort-swap.svg @@ -2,7 +2,7 @@ Insertion sort -sort.c:6 +sort.c:6 kernel memory @@ -19,111 +19,111 @@ a int a[8] -0 -4 +0 +4 -1 -9 +1 +9 -2 -17 +2 +17 -3 +3 -j-1 -31 +j-1 +31 -4 +4 -j -12 +j +12 -5 +5 -i -26 +i +26 -6 -5 +6 +5 -7 -20 +7 +20 -31 > 12 +31 > 12 so they trade places -31 +31 -12 +12 sort.c -3 -for -( -int -i = -1 -; i < n; i++) { -4 -int -j = i; +3 +for +( +int +i = +1 +; i < n; i++) { +4 +int +j = i; -5 -while -(j > -0 -&& a[j-1] > a[j]) { +5 +while +(j > +0 +&& a[j-1] > a[j]) { -6 -swap(&a[j-1], &a[j]); +6 +swap(&a[j-1], &a[j]); -7 -j--; +7 +j--; locals of sort() -i -int -5 -j -int +i +int +5 +j +int -4 -n -int -8 -a -int * -7 -f -f -d -3 -a -2 -c -1 -e -2 -0 +4 +n +int +8 +a +int * +7 +f +f +d +3 +a +2 +c +1 +e +2 +0 where a[0] is While the one on the left is larger, they trade places, -and j follows the smaller value down. +and j follows the smaller value down. diff --git a/design/mock/08-far-view-480.png b/design/mock/08-far-view-480.png index d182a84..8bf8e27 100644 Binary files a/design/mock/08-far-view-480.png and b/design/mock/08-far-view-480.png differ diff --git a/design/mock/08-far-view.png b/design/mock/08-far-view.png index e148dd4..151d345 100644 Binary files a/design/mock/08-far-view.png and b/design/mock/08-far-view.png differ diff --git a/design/mock/08-far-view.svg b/design/mock/08-far-view.svg index d97688f..0ff910a 100644 --- a/design/mock/08-far-view.svg +++ b/design/mock/08-far-view.svg @@ -2,7 +2,7 @@ A process, from far away -hello +hello kernel memory @@ -18,50 +18,50 @@ address space one square a page of 4096 bytes · low addresses on top -55d4 -a1c0 -1000 +55d4 +a1c0 +1000 -r +r -x -hello -4K +x +hello +4K -55d4 -a1c0 -2000 +55d4 +a1c0 +2000 -r +r -hello -4K +hello +4K -55d4 -a1c0 -3000 +55d4 +a1c0 +3000 -r +r -w +w -hello -8K +hello +8K -55d4 -a2e5 -f000 +55d4 +a2e5 +f000 -r +r -w +w -heap -132K +heap +132K @@ -81,16 +81,16 @@ -7f3c -5e20 -0000 +7f3c +5e20 +0000 -r +r -x -libc.so.6 -2.1M +x +libc.so.6 +2.1M @@ -110,16 +110,16 @@ -7f3c -5e5c -2000 +7f3c +5e5c +2000 -r +r -x -ld.so -216K +x +ld.so +216K @@ -139,16 +139,16 @@ -7ffd -3a2a -2000 +7ffd +3a2a +2000 -r +r -w +w -stack -132K +stack +132K @@ -168,20 +168,20 @@ -7ffd -3a3d -1000 +7ffd +3a3d +1000 -r +r -x -vdso -8K +x +vdso +8K -ffff 8000 0000 +ffff 8000 0000 kernel the upper half of every address space @@ -194,8 +194,8 @@ 3640 bytes above -3a2c -1e38 +3a2c +1e38 @@ -212,78 +212,78 @@ -3a2c -1e40 +3a2c +1e40 -48 +48 -65 +65 -6c +6c -6c +6c -6f +6f -2c +2c -20 +20 -77 -3a2c -1e48 +77 +3a2c +1e48 -6f +6f -72 +72 -6c +6c -64 +64 -21 +21 -0a +0a -00 +00 -00 -3a2c -1e50 +00 +3a2c +1e50 -00 +00 -3f +3f -9a +9a -c1 +c1 -77 +77 -d2 +d2 -8e +8e -5b -3a2c -1e58 +5b +3a2c +1e58 -40 +40 -1e +1e -2c +2c -3a +3a -fd +fd -7f +7f -00 +00 -00 +00 @@ -300,30 +300,30 @@ user kernel -execve +execve -openat +openat -mprotect +mprotect -write +write -exit_group +exit_group the dynamic loader -main +main -Before main runs a single line, the loader has already +Before main runs a single line, the loader has already asked the kernel for memory a dozen times. diff --git a/design/mock/09-context-480.png b/design/mock/09-context-480.png new file mode 100644 index 0000000..4552f96 Binary files /dev/null and b/design/mock/09-context-480.png differ diff --git a/design/mock/09-context.png b/design/mock/09-context.png new file mode 100644 index 0000000..e328b1a Binary files /dev/null and b/design/mock/09-context.png differ diff --git a/design/mock/09-context.svg b/design/mock/09-context.svg new file mode 100644 index 0000000..9dbecb2 --- /dev/null +++ b/design/mock/09-context.svg @@ -0,0 +1,536 @@ + + + + + + +the list +each node, and under it the heap words that are that node + +1 + + + +28b4 +10b0 + + +rcx + +4 + + + + +28b4 +1090 + + +rax + + +rdi + +9 + + + + +28b4 +1070 + +16 + + + + +28b4 +1050 + +25 + + + + +28b4 +1030 + +36 + + +28b4 +1010 +address +value +next +the heap, sixteen bytes a node +28b4 +1010 +0x24 +36 +0 +NULL +28b4 +1030 +0x19 +25 +28b4 +1010 +28b4 +1050 +0x10 +16 +28b4 +1030 +28b4 +1070 +9 +28b4 +1050 + + +rax + + +rdi +28b4 +1090 +4 +28b4 +1070 + + +rcx +28b4 +10b0 +1 + +0 +NULL + + + +list.c +lines 15 to 26 of 42 +15 +return +n +; +16 +} +17 +18 +__attribute__ +( +( +noinline +) +) +static +struct +node +* +reverse +( +struct +node +… +19 +struct +node +* +prev += +NULL +; + +20 +while +( +head +) +{ + + +21 +struct +node +* +next += +head +- +> +next +; + +22 +head +- +> +next += +prev +; +23 +prev += +head +; +24 +head += +next +; +25 +} +26 +return +prev +; + + + +disassembly +reverse + +100 +1920 ++16 +mov +rcx, rax + +100 +1923 ++19 +mov +rax, rdi + + + +100 +1926 ++22 +mov +rdi, qword ptr [rdi + 8] + +rdi + 8 = +28b4 +1098 + + +28b4 +1070 + + +9 +100 +192a ++26 +mov +qword ptr [rax + 8], rcx +100 +192e ++30 +test +rdi, rdi + + + +registers +stack +heap +code +data +rwx + +read-only +rax + +28b4 +1090 + + +4 +rbx +0 +rcx + +28b4 +10b0 + + +1 +rdx +0 +rsi +28b4 +10c0 + + +0 +rdi + +28b4 +1090 + + +4 +rbp +7ffe +970f +17b0 + + +7ffe +970f +17d0 + + +7ffe +970f +1880 + + +7ffe +970f +18e0 + + +0 +rsp +7ffe +970f +17b0 + + +7ffe +970f +17d0 + + +7ffe +970f +1880 + + +7ffe +970f +18e0 + + +0 +r8 +7f2b +a301 +9ac0 + + +0 +r9 +0x20 +32 +r10 +0x10 +16 +r11 +7f2b +a301 +9ac8 + + +28b4 +10c0 + + +0 +r12 +7ffe +970f +1908 + + +7ffe +970f +2fce + + +"zig-out/targets/list" +r13 +1 +r14 +7f2b +a32a +c000 + + +7f2b +a32a +d3c0 + + +0 +r15 +100 +29c0 + + +1 + + +rip +100 +1926 +reverse+22 +flags +0x206 +cf + +PF +af + +zf +sf + +IF +df +of + + + +stack +eight words from rsp · low addresses on top + + +rsp + + +rbp +7ffe +970f +17b0 + + +7ffe +970f +17d0 + + +7ffe +970f +1880 + + +7ffe +970f +18e0 + + +0 +7ffe +970f +17b8 + + +100 +18b0 +main+80 +return address +7ffe +970f +17c0 + + +7f2b +a302 +0de8 + + +7ffe +970f +1918 + + + + + +7ffe +970f +17c8 + + +0 +7ffe +970f +17d0 + + +7ffe +970f +1880 + + +7ffe +970f +18e0 + + +0 +main's saved rbp +7ffe +970f +17d8 + + +7f2b +a2e2 +7781 +libc+27781 +return address +7ffe +970f +17e0 + + +7f2b +a328 +fc60 +ld.so+22c60 +7ffe +970f +17e8 + + +7ffe +970f +1908 + + +7ffe +970f +2fce + + +"zig-out/targets… + + + +backtrace +newest on top + + +#0 +reverse+22 +list.c:21 +#1 +main+80 +list.c:39 +#2 +libc+27781 +libc.so.6 + + +moment 71 of 95 +Stopped in reverse, about to load head->next. +rdi holds a node's address; the word after its value is the next node. + diff --git a/design/mock/10-context-step-480.png b/design/mock/10-context-step-480.png new file mode 100644 index 0000000..8f4bfed Binary files /dev/null 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 new file mode 100644 index 0000000..0faaba7 Binary files /dev/null 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 new file mode 100644 index 0000000..f13df1b --- /dev/null +++ b/design/mock/10-context-step.svg @@ -0,0 +1,543 @@ + + + + + + +the list +each node, and under it the heap words that are that node + +1 + + + +28b4 +10b0 + + +rcx + +4 + + + + + +28b4 +1090 + + +rax + +9 + + + + + +28b4 +1070 + + +rdi + +16 + + + + +28b4 +1050 + +25 + + + + +28b4 +1030 + +36 + + +28b4 +1010 +address +value +next +the heap, sixteen bytes a node +28b4 +1010 +0x24 +36 +0 +NULL +28b4 +1030 +0x19 +25 +28b4 +1010 +28b4 +1050 +0x10 +16 +28b4 +1030 + + +rdi + +28b4 +1070 +9 +28b4 +1050 + + +rax +28b4 +1090 +4 + +28b4 +1070 + + +rcx +28b4 +10b0 +1 + +0 +NULL + + + +list.c +lines 15 to 26 of 42 +15 +return +n +; +16 +} +17 +18 +__attribute__ +( +( +noinline +) +) +static +struct +node +* +reverse +( +struct +node +… +19 +struct +node +* +prev += +NULL +; + +20 +while +( +head +) +{ + + +21 +struct +node +* +next += +head +- +> +next +; + +22 +head +- +> +next += +prev +; +23 +prev += +head +; +24 +head += +next +; +25 +} +26 +return +prev +; + + + +disassembly +reverse + +100 +1920 ++16 +mov +rcx, rax + +100 +1923 ++19 +mov +rax, rdi + + + +100 +1926 ++22 +mov +rdi, qword ptr [rdi + 8] + +rdi + 8 = + +28b4 +1098 + + +28b4 +1070 + + +9 +100 +192a ++26 +mov +qword ptr [rax + 8], rcx +100 +192e ++30 +test +rdi, rdi + + + +registers +stack +heap +code +data +rwx + +read-only +rax + +28b4 +1090 + + +4 +rbx +0 +rcx + +28b4 +10b0 + + +1 +rdx +0 +rsi +28b4 +10c0 + + +0 +rdi + +28b4 +1070 + + +9 +rbp +7ffe +970f +17b0 + + +7ffe +970f +17d0 + + +7ffe +970f +1880 + + +7ffe +970f +18e0 + + +0 +rsp +7ffe +970f +17b0 + + +7ffe +970f +17d0 + + +7ffe +970f +1880 + + +7ffe +970f +18e0 + + +0 +r8 +7f2b +a301 +9ac0 + + +0 +r9 +0x20 +32 +r10 +0x10 +16 +r11 +7f2b +a301 +9ac8 + + +28b4 +10c0 + + +0 +r12 +7ffe +970f +1908 + + +7ffe +970f +2fce + + +"zig-out/targets/list" +r13 +1 +r14 +7f2b +a32a +c000 + + +7f2b +a32a +d3c0 + + +0 +r15 +100 +29c0 + + +1 + + +rip +100 +1926 +reverse+22 +flags +0x206 +cf + +PF +af + +zf +sf + +IF +df +of + + + +stack +eight words from rsp · low addresses on top + + +rsp + + +rbp +7ffe +970f +17b0 + + +7ffe +970f +17d0 + + +7ffe +970f +1880 + + +7ffe +970f +18e0 + + +0 +7ffe +970f +17b8 + + +100 +18b0 +main+80 +return address +7ffe +970f +17c0 + + +7f2b +a302 +0de8 + + +7ffe +970f +1918 + + + + + +7ffe +970f +17c8 + + +0 +7ffe +970f +17d0 + + +7ffe +970f +1880 + + +7ffe +970f +18e0 + + +0 +main's saved rbp +7ffe +970f +17d8 + + +7f2b +a2e2 +7781 +libc+27781 +return address +7ffe +970f +17e0 + + +7f2b +a328 +fc60 +ld.so+22c60 +7ffe +970f +17e8 + + +7ffe +970f +1908 + + +7ffe +970f +2fce + + +"zig-out/targets… + + + +backtrace +newest on top + + +#0 +reverse+22 +list.c:21 +#1 +main+80 +list.c:39 +#2 +libc+27781 +libc.so.6 + + + + +moment 72 of 95 +The load read the word at 28b4 1098: the address of the next node. +rdi holds it now, and follows it to the node whose value is 9. + diff --git a/design/mock/11-context-lens-480.png b/design/mock/11-context-lens-480.png new file mode 100644 index 0000000..26d5fe3 Binary files /dev/null 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 new file mode 100644 index 0000000..6bb5e5c Binary files /dev/null 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 new file mode 100644 index 0000000..f9b93d8 --- /dev/null +++ b/design/mock/11-context-lens.svg @@ -0,0 +1,524 @@ + + + + + + +the list +each node, and under it the heap words that are that node + +1 + + + +28b4 +10b0 + + +rcx + +4 + + + + + +28b4 +1090 + + +rax + +9 + + + + + +28b4 +1070 + + +rdi + +16 + + + + +28b4 +1050 + +25 + + + + +28b4 +1030 + +36 + + +28b4 +1010 +address +value +next +28b4 +1010 +0x24 +36 +0 +NULL +28b4 +1030 +0x19 +25 +28b4 +1010 +28b4 +1050 +0x10 +16 +28b4 +1030 + + +rdi + +28b4 +1070 +9 +28b4 +1050 + + +rax +28b4 +1090 +4 + +28b4 +1070 + + +rcx +28b4 +10b0 +1 + +0 +NULL + + + +list.c +lines 17 to 24 of 42 +17 +18 +__attribute__ +( +( +noinline +) +) +static +struct +node +* +reverse +( +struct +node +… +19 +struct +node +* +prev += +NULL +; + +20 +while +( +head +) +{ + + +21 +struct +node +* +next += +head +- +> +next +; + +22 +head +- +> +next += +prev +; +23 +prev += +head +; +24 +head += +next +; + + + +disassembly +reverse + +100 +1923 ++19 +mov +rax, rdi + + + +100 +1926 ++22 +mov +rdi, qword ptr [rdi + 8] + +rdi + 8 = + +28b4 +1098 + + +28b4 +1070 + + +9 +100 +192a ++26 +mov +qword ptr [rax + 8], rcx +100 +192e ++30 +test +rdi, rdi + + + +registers +stack +heap +code +data +rwx + +read-only +rax + +28b4 +1090 + + +4 +rbx +0 +rcx + +28b4 +10b0 + + +1 +rdx +0 +rsi +28b4 +10c0 + + +0 +rdi + +28b4 +1070 + + +9 +rbp +7ffe +970f +17b0 + + +7ffe +970f +17d0 + + +7ffe +970f +1880 + + +7ffe +970f +18e0 + + +0 +rsp +7ffe +970f +17b0 + + +7ffe +970f +17d0 + + +7ffe +970f +1880 + + +7ffe +970f +18e0 + + +0 +r8 +7f2b +a301 +9ac0 + + +0 +r9 +0x20 +32 +r10 +0x10 +16 +r11 +7f2b +a301 +9ac8 + + +28b4 +10c0 + + +0 +r12 +7ffe +970f +1908 + + +7ffe +970f +2fce + + +"zig-out/targets/list" +r13 +1 +r14 +7f2b +a32a +c000 + + +7f2b +a32a +d3c0 + + +0 +r15 +100 +29c0 + + +1 + + +rip +100 +1926 +reverse+22 +flags +0x206 +cf + +PF +af + +zf +sf + +IF +df +of + + + +stack +eight words from rsp · low addresses on top + + +rsp + + +rbp +7ffe +970f +17b0 + + +7ffe +970f +17d0 + + +7ffe +970f +1880 + + +7ffe +970f +18e0 + + +0 +7ffe +970f +17b8 + + +100 +18b0 +main+80 +return address +7ffe +970f +17c0 + + +7f2b +a302 +0de8 + + +7ffe +970f +1918 + + + + + +7ffe +970f +17c8 + + +0 +7ffe +970f +17d0 + + +7ffe +970f +1880 + + +7ffe +970f +18e0 + + +0 +main's saved rbp +7ffe +970f +17d8 + + +7f2b +a2e2 +7781 +libc+27781 +return address +7ffe +970f +17e0 + + +7f2b +a328 +fc60 +ld.so+22c60 +7ffe +970f +17e8 + + +7ffe +970f +1908 + + +7ffe +970f +2fce + + +"zig-out/targets… + + + +backtrace +newest on top + + +#0 +reverse+22 +list.c:21 +#1 +main+80 +list.c:39 +#2 +libc+27781 +libc.so.6 + + + + +moment 72 of 95 +The load read the word at 28b4 1098: the address of the next node. +rdi holds it now, and follows it to the node whose value is 9. + diff --git a/design/mock/context.py b/design/mock/context.py new file mode 100644 index 0000000..8a5d923 --- /dev/null +++ b/design/mock/context.py @@ -0,0 +1,480 @@ +#!/usr/bin/env python3 +"""The default frame: the machine's whole context, pwndbg's way, and the scene's lens. + + python3 context.py # writes 09-context.svg, 10-context-step.svg, 11-context-lens.svg + +The moment is real: `ztrace record --by line -- targets/list`, moments 71 and 72 +(`ztrace show FILE 71`): every register and its chain, the instructions at the pc, the +stack. The lens (the list, and the heap words that are its nodes) is what those chains +and the program say the heap holds; a recording made under ptrace has no allocations. +""" +import re + +import gen +from gen import (PAGE, RAISED, RULE, VELLUM, BRIGHT, DIM, FAINT, INK, GOLD, VERM, READ, ALLOC, EMBER, TRAIL, + STACK, HEAP, CODE, DATA, KW, LIT, COMMENT, W, H, Svg, tw) + +S = 20 # the context's mono size: an advance of 12 +A = 12 +ROW = 24 +TAG = 24 +MONO, BOLD, ITAL = "monom", "monob", "monoi" +LX, RX, CW = 64, 976, 880 # the two columns +TOP, BOTTOM = 48, 888 # the stage + + +def b(y): + """Baseline of a row at y.""" + return y + ROW / 2 + 0.36 * S + + +def panel(g, x, y, w, rows, title, focus=False, note=None, h=None): + """A one-row tag and `rows` rows under it. Returns the y of the first row.""" + h = h or TAG + 12 + rows * ROW + g.rect(x, y, w, TAG, RAISED) + g.frame(x, y, w, h, DIM if focus else RULE, 2) + if focus: + g.rect(x + 8, y + 6, 12, 12, GOLD) + else: + g.frame(x + 8, y + 6, 12, 12, FAINT, 2) + g.t(x + 28, y + 18, title, 20, VELLUM if focus else DIM, "sansm") + if isinstance(note, str): + g.t(x + w - 10, y + 18, note, 18, FAINT, "sans", "end") + elif note: # runs, each in its own ink: the legend + nx = x + w - 10 - sum(tw(s, 18, "sansm") + 14 for s, _, _ in note) + 14 + for s, ink, under in note: + g.t(nx, y + 18, s, 18, ink, "sansm") + if under: + g.rect(nx, y + 21, tw(s, 18, "sansm"), 2, ink) + nx += tw(s, 18, "sansm") + 14 + return y + TAG + 6 + + +# ---- a value, by what it is --------------------------------------------------- +def groups(n): + """Hex at its natural length, in fours from the right.""" + d = "%x" % n + head = len(d) % 4 + return ([d[:head]] if head else []) + [d[i:i + 4] for i in range(head, len(d), 4)] + + +def addr_w(n): + gs = groups(n) + return sum(len(s) for s in gs) * A + (len(gs) - 1) * (A // 2) + + +def addr(g, x, y, n, ink, face=MONO, under=False): + for s in groups(n): + g.t(x, b(y), s, S, ink, face) + x += len(s) * A + A // 2 + if under: # writable and executable + g.rect(x - addr_w(n) - A // 2, y + ROW - 3, addr_w(n), 2, ink) + return x - A // 2 + + +def link_arrow(g, x, y, ink=FAINT): + """A pointer to: 30 wide.""" + g.rect(x + 6, y + 11, 11, 2, ink) + g.path(f"M{x + 24},{y + 12} l-8,-5 v10 Z", fill=ink) + return x + 30 + + +def value_arrow(g, x, y, ink=FAINT): + """Which holds: 30 wide, pointing back.""" + g.path(f"M{x + 6},{y + 12} l8,-5 v10 Z", fill=ink) + g.rect(x + 13, y + 11, 11, 2, ink) + return x + 30 + + +def integer(g, x, y, v, ink=VELLUM): + """A number that is not an address: one digit as it is; more, hex with 0x and its decimal after, faint.""" + if v < 10: + g.t(x, b(y), str(v), S, ink, MONO) + return x + A + s = "0x%x" % v + g.t(x, b(y), s, S, ink, MONO) + x += len(s) * A + if v <= 0xffff: + g.t(x + 8, b(y), str(v), S, FAINT, MONO) + x += 8 + len(str(v)) * A + return x + + +def chain(g, x, y, links, end, marks=None, room=None): + """A telescope: links are (address, ink[, name]); end is ("int", v), ("str", s), ("more",) or None. + marks: {index: "strike" | "tick" | "read" | "land"} on a link, index len(links) being the end. + Returns each link's (x, width).""" + marks = marks or {} + where = [] + for i, link in enumerate(links): + n, ink = link[0], link[1] + if i: + x = link_arrow(g, x, y) + w = addr_w(n) + m = marks.get(i) + if m == "strike": + g.rect(x - 5, y + 1, w + 10, ROW - 2, VERM) + addr(g, x, y, n, INK, BOLD) + else: + if m == "tick": + g.rect(x - 9, y + 3, 4, ROW - 6, VERM) + if m == "read": + g.frame(x - 6, y, w + 12, ROW, READ, 2) + if m == "land": + g.frame(x - 6, y, w + 12, ROW, BRIGHT, 2) + addr(g, x, y, n, ink) + where.append((x, w)) + x += w + if len(link) > 2: # code: its name is what a reader reads + g.t(x + 10, b(y), link[2], S, VELLUM, MONO) + x += 10 + len(link[2]) * A + if end is None: + return where + if end[0] == "more": + x = link_arrow(g, x, y) + for d in range(3): + g.rect(x + 2 + d * 8, y + 14, 3, 3, FAINT) + return where + x = value_arrow(g, x, y) if links else x + m = marks.get(len(links)) + if m == "tick": + g.rect(x - 3, y + 3, 4, ROW - 6, VERM) + x += 6 + if end[0] == "int": + integer(g, x, y, end[1]) + else: + s = end[1] + if room is not None and len(s) + 2 > (room - x) // A: + s = s[:max(int((room - x) // A) - 2, 1)] + "…" + g.t(x, b(y), '"' + s, S, LIT, MONO) + else: + g.t(x, b(y), '"' + s + '"', S, LIT, MONO) + return where + + +def plaque(g, x, y, s, fill=RAISED, ink=VELLUM, stroke=None, pad=4): + """A register's name where it points: 20 tall in a 24 row.""" + w = len(s) * A + 2 * pad + g.rect(x, y + 2, w, ROW - 4, fill) + if stroke: + g.frame(x, y + 2, w, ROW - 4, stroke, 2) + g.t(x + pad, b(y) - 1, s, S, ink, MONO) + return w + + +# ---- the context's five panels --------------------------------------------------- +def registers(g, y, regs, flags, marks): + legend = [("stack", STACK, False), ("heap", HEAP, False), ("code", CODE, False), ("data", DATA, False), + ("rwx", DATA, True), ("read-only", VELLUM, False)] + y0 = panel(g, RX, y, CW, 18, "registers", note=legend) + where = {} + for i, (name, links, end) in enumerate(regs): + yy = y0 + i * ROW + if name == "rip": + g.rect(RX + 2, yy, CW - 4, ROW, EMBER[GOLD]) + g.rect(RX + 2, yy, 54, ROW, GOLD) + g.t(RX + 12, b(yy), name, S, INK, BOLD) + else: + g.t(RX + 12, b(yy), name, S, DIM, MONO) + where[name] = (yy, chain(g, RX + 72, yy, links, end, marks.get(name), room=RX + CW - 12) if links else + [(RX + 72, integer(g, RX + 72, yy, end[1]) - RX - 72)]) + if not links and marks.get(name, {}).get(0) == "tick": + g.rect(RX + 63, yy + 3, 4, ROW - 6, VERM) + yy = y0 + 17 * ROW + g.t(RX + 12, b(yy), "flags", S, DIM, MONO) + g.t(RX + 84, b(yy), "0x%x" % flags[0], S, VELLUM, MONO) + x = RX + 84 + 6 * A + 12 + for name, on, changed in flags[1]: + if changed: + g.rect(x - 7, yy + 3, 4, ROW - 6, VERM) + if on: + g.rect(x, yy + 2, 2 * A + 8, 20, RAISED) + g.t(x + 4, b(yy), name, S, BRIGHT, BOLD) + else: + g.t(x + 4, b(yy), name.lower(), S, FAINT, MONO) + x += 2 * A + 8 + 10 + return where + + +def stack(g, y, rows): + y0 = panel(g, RX, y, CW, len(rows), "stack", note="eight words from rsp · low addresses on top") + where = {} + for i, (slot, names, links, end, note) in enumerate(rows): + yy = y0 + i * ROW + x = RX + 10 + for n in names: + x += plaque(g, x, yy, n, stroke=STACK, pad=2) + 2 + where[slot] = yy + chain(g, RX + 104, yy, [(slot, STACK)] + links, end, room=RX + CW - 8) + if note: + g.t(RX + CW - 12, b(yy) - 1, note, 18, FAINT, "sans", "end") + return where + + +def backtrace(g, y, frames): + y0 = panel(g, RX, y, CW, len(frames), "backtrace", note="newest on top") + for i, (name, where_) in enumerate(frames): + yy = y0 + i * ROW + if i == 0: + g.rect(RX + 2, yy, CW - 4, ROW, EMBER[GOLD]) + g.rect(RX + 2, yy, 42, ROW, GOLD) + g.t(RX + 12, b(yy), "#%d" % i, S, INK if i == 0 else FAINT, BOLD if i == 0 else MONO) + g.t(RX + 60, b(yy), name, S, VELLUM, MONO) + g.t(RX + 60 + 18 * A, b(yy), where_, S, DIM, MONO) + + +C_WORDS = {"struct", "long", "static", "return", "while", "for", "int", "char", "sizeof", "if", "else", "void", "NULL", + "__attribute__", "noinline"} + + +def c_line(g, x, y, text, limit): + """One line of C in the four inks. Cut, with a faint mark, where the panel ends.""" + cut = len(text) > limit + text = text[:limit - 1] if cut else text + if text.lstrip().startswith("//"): + g.t(x, b(y), text, S, COMMENT, ITAL) + return + for m in re.finditer(r'"[^"]*"?|#\w+|<[\w.]+>|\w+|\s+|.', text): + s = m.group() + if not s.strip(): + continue + ink, face = VELLUM, MONO + if s in C_WORDS or s.startswith("#"): + ink, face = KW, BOLD + elif s[0] == '"' or s[0].isdigit() or (s[0] == "<" and len(s) > 2): + ink = LIT + g.t(x + m.start() * A, b(y), s, S, ink, face) + if cut: + g.t(x + (limit - 1) * A, b(y), "…", S, FAINT, MONO) + + +def source(g, y, rows, lines, first, cur, trail, name, total): + y0 = panel(g, LX, y, CW, rows, name, note=f"lines {first} to {first + rows - 1} of {total}") + for i in range(rows): + n = first + i + yy = y0 + i * ROW + if n == cur: + g.rect(LX + 2, yy, CW - 4, ROW, EMBER[GOLD]) + g.rect(LX + 2, yy, 50, ROW, GOLD) + g.t(LX + 46, b(yy), str(n), S, INK, BOLD, "end") + else: + if n in trail: + g.rect(LX + 2, yy + 1, 4, ROW - 2, TRAIL[min(trail.index(n), 2)]) + g.t(LX + 46, b(yy), str(n), S, FAINT, MONO, "end") + c_line(g, LX + 64, yy, lines[n - 1], (CW - 64 - 12) // A) + + +def disasm(g, y, rows, func): + """rows: (address, offset, mnemonic, operands, state) with state in cur | trail0 | trail1 | None, + or ("note", parts) for the row under the pc that says what its operand is.""" + y0 = panel(g, LX, y, CW, len(rows), "disassembly", note=func) + where = {} + for i, r in enumerate(rows): + yy = y0 + i * ROW + if r[0] == "note": + g.rect(LX + 2, yy, CW - 4, ROW, EMBER[GOLD]) + g.t(LX + 198, b(yy), r[1], S, DIM, MONO) + x = LX + 198 + (len(r[1]) + 1) * A + where["note"] = (yy, chain(g, x, yy, r[2], r[3], r[4] if len(r) > 4 else None)) + continue + a, off, mn, ops, st = r + if st == "cur": + g.rect(LX + 2, yy, CW - 4, ROW, EMBER[GOLD]) + g.rect(LX + 2, yy, 26, ROW, GOLD) + g.path(f"M{LX + 21},{yy + 12} l-10,-6 v12 Z", fill=INK) + elif st: + g.rect(LX + 2, yy + 1, 4, ROW - 2, TRAIL[int(st[-1])]) + addr(g, LX + 36, yy, a, CODE) + g.t(LX + 138, b(yy), "+%d" % off, S, FAINT, MONO) + g.t(LX + 198, b(yy), mn, S, KW, BOLD) + x = LX + 198 + 6 * A + for part, ink in ops: + if isinstance(part, int): + x = addr(g, x, yy, part, ink) + 0 + else: + g.t(x, b(yy), part, S, ink, MONO) + x += len(part) * A + where[a] = yy + return where + + +# ---- the lens: a list, and the heap words that are its nodes ---------------------- +def lens(g, y, nodes, pointing, marks, h=312, subject=False): + """nodes: (address, value, next address or 0), in the list's first order. pointing: {address: [registers]}. + marks: {"read": address of a word, "tick": [...], "land": address of a node}. + As the subject it is one size up: mono 25 on rows of 30.""" + global S, A, ROW + keep = (S, A, ROW) + panel(g, LX, y, CW, 0, "the list", focus=True, note="each node, and under it the heap words that are that node", h=h) + if subject: + S, A, ROW = 25, 15, 30 + NW, NH, GAP = (124, 64, 22) if subject else (116, 44, 28) + VW = NW - 48 # the value's half; the pointer's is 48 + x0 = LX + (CW - len(nodes) * NW - (len(nodes) - 1) * GAP) / 2 + ny = y + TAG + (22 if subject else 14) + xs = {a: x0 + i * (NW + GAP) for i, (a, v, nx) in enumerate(nodes)} + for a, v, nx in nodes: + x = xs[a] + g.rect(x, ny, VW - 2, NH, RAISED) + vs = 35 if subject else 25 + g.t(x + VW / 2 - 1, ny + NH / 2 + 0.36 * vs, str(v), vs, VELLUM, MONO if vs < 30 else "mono", "middle") + g.rect(x + VW, ny, NW - VW, NH, "#101a48") + if marks.get("read") == a + 8: + g.frame(x + VW - 2, ny - 2, NW - VW + 4, NH + 4, READ, 2) + if a + 8 in marks.get("tick", ()): + g.rect(x + VW, ny + NH - 4, NW - VW, 4, VERM) + if marks.get("land") == a: + g.frame(x - 4, ny - 4, NW + 8, NH + 8, BRIGHT, 2) + cx, cy = x + VW + (NW - VW) / 2, ny + NH / 2 + if nx: + g.rect(cx - 5, cy - 5, 10, 10, HEAP) + tx = xs[nx] + if tx > x: + gen.arrow(g, [(cx + 8, cy), (tx - 3, cy)], HEAP, 3, hl=12, hw=10) + else: # a link turned round: under the row + gen.arrow(g, [(cx, cy + 8), (cx, ny + NH + 12), (tx + 34, ny + NH + 12), (tx + 34, ny + NH + 3)], HEAP, 3, r=8, hl=12, hw=10) + else: + g.path(f"M{x + VW + 10},{ny + NH - 8} L{x + NW - 10},{ny + 8}", DIM, 2) + addr(g, x + (NW - addr_w(a)) / 2, ny + NH + 2, a, HEAP) + px = x + 2 + for name in pointing.get(a, ()): + px += plaque(g, px, ny + NH + ROW + 2, name, stroke=HEAP) + 4 + # the words + wy = ny + NH + 2 * ROW + (46 if subject else 34) + c0, c1, c2 = (LX + 190, LX + 380, LX + 590) if subject else (LX + 150, LX + 300, LX + 470) + g.t(c0, wy - 6, "address", 18, FAINT, "sansm") + g.t(c1, wy - 6, "value", 18, FAINT, "sansm") + g.t(c2, wy - 6, "next", 18, FAINT, "sansm") + if not subject: + g.t(LX + CW - 12, wy - 6, "the heap, sixteen bytes a node", 18, FAINT, "sans", "end") + rows = {} + for i, (a, v, nx) in enumerate(sorted(nodes)): + yy = wy + i * ROW + rows[a] = yy + px = LX + 12 + for name in pointing.get(a, ()): + px += plaque(g, px, yy, name, stroke=HEAP) + 4 + if marks.get("land") == a: + g.frame(c0 - 6, yy, c2 - c0 + addr_w(a) + 12, ROW, BRIGHT, 2) + addr(g, c0, yy, a, HEAP) + integer(g, c1, yy, v) + if a + 8 in marks.get("tick", ()): + g.rect(c2 - 9, yy + 3, 4, ROW - 6, VERM) + if marks.get("read") == a + 8: + g.frame(c2 - 6, yy, addr_w(a) + 12, ROW, READ, 2) + if nx: + addr(g, c2, yy, nx, HEAP) + else: + g.t(c2, b(yy), "0", S, VELLUM, MONO) + g.t(c2 + 2 * A, b(yy), "NULL", S, FAINT, MONO) + land_x = c2 + addr_w(nodes[0][0]) + 10 + S, A, ROW = keep + return xs, rows, land_x + + +# ---- the data: moments 71 and 72 of the recording ----------------------------------- +SRC = open(__file__.replace("context.py", "list.c")).read().split("\n") +S_, H_, C_, D_ = STACK, HEAP, CODE, DATA +RBP = [(0x7ffe970f17b0, S_), (0x7ffe970f17d0, S_), (0x7ffe970f1880, S_), (0x7ffe970f18e0, S_)] + + +def regs_at(rdi): + return [ + ("rax", [(0x28b41090, H_)], ("int", 4)), + ("rbx", [], ("int", 0)), + ("rcx", [(0x28b410b0, H_)], ("int", 1)), + ("rdx", [], ("int", 0)), + ("rsi", [(0x28b410c0, H_)], ("int", 0)), + ("rdi", [(rdi[0], H_)], ("int", rdi[1])), + ("rbp", RBP, ("int", 0)), + ("rsp", RBP, ("int", 0)), + ("r8", [(0x7f2ba3019ac0, D_)], ("int", 0)), + ("r9", [], ("int", 0x20)), + ("r10", [], ("int", 0x10)), + ("r11", [(0x7f2ba3019ac8, D_), (0x28b410c0, H_)], ("int", 0)), + ("r12", [(0x7ffe970f1908, S_), (0x7ffe970f2fce, S_)], ("str", "zig-out/targets/list")), + ("r13", [], ("int", 1)), + ("r14", [(0x7f2ba32ac000, D_), (0x7f2ba32ad3c0, D_)], ("int", 0)), + ("r15", [(0x10029c0, VELLUM)], ("int", 1)), + ("rip", [(0x1001926, C_, "reverse+22")], None), + ] + + +STACK_ROWS = [ + (0x7ffe970f17b0, ["rsp", "rbp"], RBP[1:], ("int", 0), ""), + (0x7ffe970f17b8, [], [(0x10018b0, C_, "main+80")], None, "return address"), + (0x7ffe970f17c0, [], [(0x7f2ba3020de8, D_), (0x7ffe970f1918, S_)], ("more",), ""), + (0x7ffe970f17c8, [], [], ("int", 0), ""), + (0x7ffe970f17d0, [], RBP[2:], ("int", 0), "main's saved rbp"), + (0x7ffe970f17d8, [], [(0x7f2ba2e27781, C_, "libc+27781")], None, "return address"), + (0x7ffe970f17e0, [], [(0x7f2ba328fc60, C_, "ld.so+22c60")], None, ""), + (0x7ffe970f17e8, [], [(0x7ffe970f1908, S_), (0x7ffe970f2fce, S_)], ("str", "zig-out/targets/list"), ""), +] +FRAMES = [("reverse+22", "list.c:21"), ("main+80", "list.c:39"), ("libc+27781", "libc.so.6")] +NODES = [(0x28b410b0, 1, 0), (0x28b41090, 4, 0x28b41070), (0x28b41070, 9, 0x28b41050), + (0x28b41050, 16, 0x28b41030), (0x28b41030, 25, 0x28b41010), (0x28b41010, 36, 0)] +P = VELLUM +DIS = [ + (0x1001920, 16, "mov", [("rcx, rax", P)], "trail1"), + (0x1001923, 19, "mov", [("rax, rdi", P)], "trail0"), + (0x1001926, 22, "mov", [("rdi, qword ptr [rdi + 8]", P)], "cur"), + None, # the note, filled in by each frame + (0x100192a, 26, "mov", [("qword ptr [rax + 8], rcx", P)], None), + (0x100192e, 30, "test", [("rdi, rdi", P)], None), +] + + +def frame(name, step, subject=False): + """subject: the lens is what the scene is about, and takes rows from the source and the disassembly.""" + g = Svg() + rdi = (0x28b41070, 9) if step else (0x28b41090, 4) + # what the last three steps changed, and (mid-step) what this one has + reg_marks = {"rax": {0: "tick"}, "rcx": {0: "tick"}} + lens_marks = {"tick": [0x28b410b8]} + pointing = {0x28b410b0: ["rcx"], 0x28b41090: ["rax"] + ([] if step else ["rdi"]), 0x28b41070: ["rdi"] if step else []} + note_marks = None + if step: + reg_marks["rdi"] = {0: "strike"} + lens_marks.update(read=0x28b41098, land=0x28b41070) + note_marks = {0: "read"} + else: + reg_marks["rdi"] = {0: "tick"} + + lens_h, src_rows, src_first = (432, 8, 17) if subject else (312, 12, 15) + xs, rows, land_x = lens(g, TOP, NODES, pointing, lens_marks, lens_h, subject) + y = TOP + lens_h + 12 + source(g, y, src_rows, SRC, src_first, 21, [20, 22], "list.c", len(SRC) - 1) + y += TAG + 12 + src_rows * ROW + 12 + dis = list(DIS) + dis[3] = ("note", "rdi + 8 =", [(0x28b41098, H_), (0x28b41070, H_)], ("int", 9), note_marks) + disasm(g, y, dis[1:] if subject else dis, "reverse") + + where = registers(g, TOP, regs_at(rdi), (0x206, [("CF", 0, 0), ("PF", 1, 0), ("AF", 0, 0), ("ZF", 0, 1), ("SF", 0, 0), + ("IF", 1, 0), ("DF", 0, 0), ("OF", 0, 0)]), reg_marks) + stack(g, TOP + 468 + 12, STACK_ROWS) + backtrace(g, TOP + 468 + 12 + 228 + 12, FRAMES) + + if step: + # the one link drawn: from the register just struck to the node it now holds, in heap's ink, for this step + yy, links = where["rdi"] + ty = rows[0x28b41070] + (15 if subject else 12) + gen.arrow(g, [(RX, yy + 12), (RX - 16, yy + 12), (RX - 16, ty), (land_x, ty)], HEAP, 3, r=8, hl=12, hw=10) + + # the scene's clock: how far through the recording, on the rule over the caption + g.rect(LX, 899, W - 2 * LX, 2, RULE) + at = 71.5 if step else 71 + g.rect(LX, 898, (W - 2 * LX) * at / 95, 4, GOLD) + g.t(W - LX, 934, "moment %d of 95" % (72 if step else 71), S, FAINT, MONO, "end") + if step: + gen.caption(g, [[("The load read the word at ",), ("28b4 1098", "mono"), (": the address of the next node.",)], + [("rdi", "mono"), (" holds it now, and follows it to the node whose value is 9.",)]]) + else: + gen.caption(g, [[("Stopped in ",), ("reverse", "mono"), (", about to load ",), ("head->next", "mono"), (".",)], + [("rdi", "mono"), (" holds a node's address; the word after its value is the next node.",)]]) + g.save(name) + + +if __name__ == "__main__": + frame("09-context", False) + frame("10-context-step", True) + frame("11-context-lens", True, subject=True) diff --git a/design/mock/gen.py b/design/mock/gen.py index 6a9733e..afd7a5a 100644 --- a/design/mock/gen.py +++ b/design/mock/gen.py @@ -17,12 +17,15 @@ HERE = os.path.dirname(os.path.abspath(__file__)) PAGE, RAISED, RULE = "#0a1030", "#16245f", "#3a4a8a" VELLUM, BRIGHT, DIM, FAINT = "#eee6d2", "#fff8e8", "#9aa6d9", "#6a7ab8" INK = "#050a24" # letters on a bright fill -GOLD, VERM, READ, PTR, ALLOC = "#e8c46a", "#ff6a4a", "#6fd6e6", "#d59cf5", "#7fdc9a" +GOLD, VERM, READ, ALLOC = "#e8c46a", "#ff6a4a", "#6fd6e6", "#7fdc9a" +# Where an address points, as pwndbg colours it: the ink of its digits, and of an arrow to it. +STACK, HEAP, CODE, DATA = "#f2d675", "#86b6ff", "#ff8f9a", "#d59cf5" +LINK = HEAP # the region the pointers a frame draws point into; each frame sets it # An accent's ember: its hold state. Chosen, not alpha-blended: gold over lapis blends to grey. -EMBER = {GOLD: "#4a3d1c", VERM: "#5a2218", READ: "#1c4652", PTR: "#45305a", ALLOC: "#1f4a33"} +EMBER = {GOLD: "#4a3d1c", VERM: "#5a2218", READ: "#1c4652", ALLOC: "#1f4a33"} TRAIL = ["#ae9350", "#746235", "#463b20"] # gold, cooling: newest first KERNEL, KERNEL_RULE, KERNEL_DIM, KERNEL_HELD = "#eee6d2", "#b9ad8e", "#4a5488", "#f3b9a0" -KW, LIT, COMMENT = "#c0dcff", "#c8f0d8", "#9aa6d9" +KW, LIT, COMMENT = "#c0dcff", "#b5e8c8", "#9aa6d9" OCHRE, TEAL = "#8a5e0c", "#17707c" # the first two pigments: identity MX, MY = 64, 48 # margins @@ -34,8 +37,8 @@ TAG_H = 48 CELL = 48 # byte cell pitch; drawn 46 ROW = 60 # memory row pitch LH = 48 # code line, register row -ADV = 0.6 # Go Mono advance, em -SM, DATA, VAL, HERO = 25, 30, 35, 60 # mono sizes: multiples of 5, so an advance is whole units +ADV = 0.6 # JetBrains Mono's advance, em: the same as Go Mono's +SM, DATA_SIZE, VAL, HERO = 25, 30, 35, 60 # mono sizes: multiples of 5, so an advance is whole units def colx(i): @@ -56,9 +59,10 @@ def esc(s): FONTS = { - "mono": ("Go Mono", "normal", "normal"), - "monob": ("Go Mono", "bold", "normal"), - "monoi": ("Go Mono", "normal", "italic"), + "mono": ("JetBrains Mono NL", "normal", "normal"), + "monom": ("JetBrains Mono NL", "500", "normal"), # at 20 and 25: thin strokes do not survive a stream + "monob": ("JetBrains Mono NL", "bold", "normal"), + "monoi": ("JetBrains Mono NL", "normal", "italic"), "sans": ("Go", "normal", "normal"), "sansm": ("Go Medium", "normal", "normal"), "sansb": ("Go", "bold", "normal"), @@ -127,7 +131,7 @@ class Svg: a += f' stroke="{stroke}" stroke-width="{sw}"' self.o.append(a + "/>") - def t(self, x, y, s, size=DATA, fill=VELLUM, font="mono", anchor="start"): + def t(self, x, y, s, size=DATA_SIZE, fill=VELLUM, font="mono", anchor="start"): """One run of text; y is the baseline. Mono is laid a character at a time on its whole-unit advance, as the renderer does.""" fam, wt, st = FONTS[font] @@ -149,7 +153,7 @@ class Svg: a += f' text-anchor="{anchor}"' self.o.append(f'{esc(s)}') - def tc(self, x, y, w, h, s, size=DATA, fill=VELLUM, font="mono"): + def tc(self, x, y, w, h, s, size=DATA_SIZE, fill=VELLUM, font="mono"): """Text centred in a box, on its cap height.""" self.t(x + w / 2, y + h / 2 + 0.36 * size, s, size, fill, font, "middle") @@ -158,7 +162,7 @@ class Svg: a = f'' for p in parts: fam, wt, st = FONTS[p[2] if len(p) > 2 else "sans"] - sz = f' font-size="{round(size * 0.875)}"' if fam == "Go Mono" else "" + sz = f' font-size="{round(size * 0.875)}"' if "Mono" in fam else "" a += f'{esc(p[0])}' self.o.append(a + "") @@ -179,8 +183,10 @@ def head_tri(g, x2, y2, ux, uy, color, hl, hw): g.path(f"M{x2:.1f},{y2:.1f} L{bx + nx:.1f},{by + ny:.1f} L{bx - nx:.1f},{by - ny:.1f} Z", fill=color) -def arrow(g, pts, color=PTR, sw=4, r=12, head=True, dash=None, hl=18, hw=16): - """A polyline with filleted elbows and a filled head on its last point.""" +def arrow(g, pts, color=None, sw=4, r=12, head=True, dash=None, hl=18, hw=16): + """A polyline with filleted elbows and a filled head on its last point, in the + colour of the region it points into.""" + color = color or LINK pts = [tuple(map(float, p)) for p in pts] (x1, y1), (x2, y2) = pts[-2], pts[-1] L = math.hypot(x2 - x1, y2 - y1) @@ -280,13 +286,24 @@ def elide(g, x, y, label="", h=24): # ---- numbers -------------------------------------------------------------- -def hexw(n, size=DATA): +def hexw(n, size=DATA_SIZE): adv = round(ADV * size) return n * adv + (n // 4 - 1) * round(adv / 2) -def hexgroups(g, x, y, digits, size=DATA, lead=FAINT, fill=VELLUM, font="mono", right=False, all_fill=None): - """Hex in fours, half an advance apart; leading zeros in `lead`.""" +def region(digits): + """Where a value of these mocks points: the ink of its digits. None for what is not a pointer + (and for read-only data, which pwndbg leaves plain).""" + d = digits.lstrip("0") + for prefix, ink in (("7ffd", STACK), ("55d4a1c03", HEAP), ("55d4a1c01", CODE), ("7f3c", CODE)): + if len(d) == 12 and d.startswith(prefix): + return ink + return None + + +def hexgroups(g, x, y, digits, size=DATA_SIZE, lead=FAINT, fill=None, font="mono", right=False, all_fill=None): + """Hex in fours, half an advance apart; leading zeros in `lead`; a pointer in its region's ink.""" + fill = fill or region(digits) or VELLUM adv = round(ADV * size) if right: x -= hexw(len(digits), size) @@ -335,7 +352,7 @@ def byte(g, x, y, v, state="plain"): g.rect(x, y + w - 6, w, 6, VERM) if state == "read": g.frame(x, y, w, w, READ, 3) - g.tc(x, y, w, w, v, DATA, ink, font) + g.tc(x, y, w, w, v, DATA_SIZE, ink, font) def word(g, x, y, digits, state="plain", nbytes=8): @@ -353,13 +370,13 @@ def word(g, x, y, digits, state="plain", nbytes=8): g.rect(x, y + h - 6, w, 6, VERM) if state == "read": g.frame(x, y, w, h, READ, 3) - hexgroups(g, x + (w - hexw(len(digits))) / 2, base(y, h, DATA), digits, all_fill=all_fill, font=font) + hexgroups(g, x + (w - hexw(len(digits))) / 2, base(y, h, DATA_SIZE), digits, all_fill=all_fill, font=font) return w def ptr_frame(g, x, y, nbytes=8): """These bytes are one pointer.""" - g.frame(x - 6, y - 6, nbytes * CELL - 2 + 12, CELL - 2 + 12, PTR, 4) + g.frame(x - 6, y - 6, nbytes * CELL - 2 + 12, CELL - 2 + 12, LINK, 4) def regmark(g, xr, y, name, fill=RAISED, rows=1): @@ -369,10 +386,10 @@ def regmark(g, xr, y, name, fill=RAISED, rows=1): x = xr - w - 16 g.rect(x, y + 3, w, 40, fill) if fill == RAISED: - g.frame(x, y + 3, w, 40, PTR, 2) + g.frame(x, y + 3, w, 40, LINK, 2) g.t(x + 10, base(y, CELL - 2, SM), name, SM, VELLUM, "mono") if fill == RAISED: - g.path(f"M{xr},{y + 23} l-14,-9 v18 Z", fill=PTR) + g.path(f"M{xr},{y + 23} l-14,-9 v18 Z", fill=LINK) else: g.rect(xr - 12, y, 6, (rows - 1) * ROW + CELL - 2, fill) @@ -387,34 +404,34 @@ def code(g, x, y, w, lines, cur=None, trail=(), unfold=None): gut = 3 * 18 + 16 ys = {} for num, parts in lines: - b = base(y, LH, DATA) + b = base(y, LH, DATA_SIZE) ys[num] = b if num == cur: g.rect(x, y + 2, w, LH - 4, EMBER[GOLD]) g.rect(x, y + 2, gut + 4, LH - 4, GOLD) - g.t(x + gut - 6, b, str(num), DATA, INK, "monob", "end") + g.t(x + gut - 6, b, str(num), DATA_SIZE, INK, "monob", "end") else: if num in trail: g.rect(x, y + 2, 6, LH - 4, TRAIL[min(trail.index(num), 2)]) - g.t(x + gut - 6, b, str(num), DATA, FAINT, "mono", "end") + g.t(x + gut - 6, b, str(num), DATA_SIZE, FAINT, "mono", "end") xx = x + gut + 20 for s, cls in parts: if s.strip(): - g.t(xx + (len(s) - len(s.lstrip())) * 18, b, s.lstrip(), DATA, ink[cls], fnt[cls]) - xx += mw(s, DATA) + g.t(xx + (len(s) - len(s.lstrip())) * 18, b, s.lstrip(), DATA_SIZE, ink[cls], fnt[cls]) + xx += mw(s, DATA_SIZE) y += LH if unfold and unfold[0] == num: ax = x + gut + 20 + 36 g.line(ax - 18, y + 2, ax - 18, y + len(unfold[1]) * LH - 2, RULE, 2) for i, (op, args) in enumerate(unfold[1]): - b2 = base(y, LH, DATA) + b2 = base(y, LH, DATA_SIZE) on = i == unfold[2] if on: g.rect(ax - 22, y + 3, w - (ax - 22 - x), LH - 6, EMBER[GOLD]) g.rect(ax - 22, y + 3, 8, LH - 6, GOLD) - g.t(ax, b2, op, DATA, VELLUM if on else DIM, "monob" if on else "mono") + g.t(ax, b2, op, DATA_SIZE, VELLUM if on else DIM, "monob" if on else "mono") if args: - g.t(ax + 5 * 18, b2, args, DATA, VELLUM if on else DIM, "mono") + g.t(ax + 5 * 18, b2, args, DATA_SIZE, VELLUM if on else DIM, "mono") ys[("asm", i)] = b2 y += LH return ys @@ -427,7 +444,7 @@ def regs(g, x, y, w, rows, flags=None): vx = x + w - 18 vw = hexw(16) for name, val, st in rows: - b = base(y, LH, DATA) + b = base(y, LH, DATA_SIZE) cell = (vx - vw - 10, y + 3, vw + 20, LH - 6) if st == "pc": g.rect(x, y + 3, w, LH - 6, EMBER[GOLD]) @@ -443,7 +460,7 @@ def regs(g, x, y, w, rows, flags=None): g.rect(*cell, EMBER[READ]) g.frame(*cell, READ, 3) hexgroups(g, vx, b, val, right=True) - g.t(x + 18, b, name, DATA, INK if st == "pc" else DIM, "monob" if st == "pc" else "mono") + g.t(x + 18, b, name, DATA_SIZE, INK if st == "pc" else DIM, "monob" if st == "pc" else "mono") out[name] = y y += LH if flags: @@ -474,10 +491,10 @@ def cards(g, x, y, w, items, h=56, gap=12, on_kernel=False): else: g.rect(x, y, w, h, EMBER[GOLD] if cur else RAISED) g.rect(x, y, 12, h, GOLD if cur else RULE) - b = base(y, h, DATA) - g.t(x + 28, b, name, DATA, ink, "monob" if cur else "mono") + b = base(y, h, DATA_SIZE) + g.t(x + 28, b, name, DATA_SIZE, ink, "monob" if cur else "mono") if args: - g.t(x + 28 + mw(name, DATA) + 18, b, args, DATA, ink if cur else dim, "mono") + g.t(x + 28 + mw(name, DATA_SIZE) + 18, b, args, DATA_SIZE, ink if cur else dim, "mono") if where: g.t(x + w - 14, base(y, h, SM), where, SM, dim if cur or on_kernel else FAINT, "mono", "end") y += h + gap @@ -496,7 +513,7 @@ def node(g, x, y, value, state="plain", body=RAISED, null=False, w=180, h=72): if null: g.path(f"M{nx + 16},{y + h - 14} L{nx + pw - 14},{y + 14}", DIM, 3) else: - g.rect(cx - 7, cy - 7, 14, 14, INK if state == "write" else PTR) + g.rect(cx - 7, cy - 7, 14, 14, INK if state == "write" else LINK) return cx, cy @@ -549,7 +566,7 @@ def frame1(): ] code(g, bx + 2, by + 16, bw - 4, lines, cur=4, trail=(3, 6)) # what the current frame sees, at the end of its line - plaque(g, bx + 2 + 70 + 20 + mw(" if (n < 2)", DATA) + 36, by + 16 + 3 * LH + 4, "n = 1", SM, VELLUM, "none", DIM) + plaque(g, bx + 2 + 70 + 20 + mw(" if (n < 2)", DATA_SIZE) + 36, by + 16 + 3 * LH + 4, "n = 1", SM, VELLUM, "none", DIM) # the calls so far tx, ty, tw_, th = panel(g, colx(5), STAGE_Y, span(7), 336, "calls of fib(n)", right="each result on its call's shoulder") @@ -573,13 +590,13 @@ def frame1(): y = Y[l] if s == "current": g.circle(x, y, R, GOLD) - g.t(x, y + 0.36 * DATA, str(n), DATA, INK, "monob", "middle") + g.t(x, y + 0.36 * DATA_SIZE, str(n), DATA_SIZE, INK, "monob", "middle") elif s == "active": g.circle(x, y, R - 2, PAGE, VELLUM, 4) - g.t(x, y + 0.36 * DATA, str(n), DATA, VELLUM, "mono", "middle") + g.t(x, y + 0.36 * DATA_SIZE, str(n), DATA_SIZE, VELLUM, "mono", "middle") else: g.circle(x, y, R, RAISED) - g.t(x, y + 0.36 * DATA, str(n), DATA, DIM, "mono", "middle") + g.t(x, y + 0.36 * DATA_SIZE, str(n), DATA_SIZE, DIM, "mono", "middle") g.rect(x + 14, y - 34, 28, 28, PAGE) g.frame(x + 14, y - 34, 28, 28, FAINT, 2) g.t(x + 28, y - 20 + 0.36 * SM, str(r), SM, VELLUM, "mono", "middle") @@ -610,6 +627,8 @@ def frame1(): # ---- frame 2: linked-list insert, nodes above the heap that holds them ---- def frame2(): + global LINK + LINK = HEAP g = Svg() head(g, "Linked lists", lit=("source", "memory"), focus="memory", where="insert after a node") @@ -637,12 +656,12 @@ def frame2(): y = ly + 28 + i * 52 if pig: g.rect(bx + 20, y, 12, 46, pig) - g.t(bx + 48, base(y, 46, DATA), name, DATA, VELLUM, "mono") + g.t(bx + 48, base(y, 46, DATA_SIZE), name, DATA_SIZE, VELLUM, "mono") if val: - hexgroups(g, bx + 118, base(y, 46, DATA), val) + hexgroups(g, bx + 118, base(y, 46, DATA_SIZE), val) g.t(bx + 118 + hexw(16) + 24, base(y, 46, 28) - 1, note, 28, DIM, "sans") else: - g.t(bx + 118, base(y, 46, DATA), note, DATA, VELLUM, "mono") + g.t(bx + 118, base(y, 46, DATA_SIZE), note, DATA_SIZE, VELLUM, "mono") # the list, as the program thinks of it lx, ly_, lw, lh = panel(g, colx(5), STAGE_Y, span(7), 240, "the list", right="what the code means") @@ -699,7 +718,7 @@ def frame2(): y += ROW # pointers, in the lane left of the words; the longest outermost - def ptr(src, dst, lane, color=PTR, dash=None, sw=4, head=True): + def ptr(src, dst, lane, color=None, dash=None, sw=4, head=True): ys_, yd = ry[src] + 23, ry[dst] + 23 arrow(g, [(cx - 6, ys_), (cx - lane, ys_), (cx - lane, yd), (cx - 4, yd)], color, sw, dash=dash, head=head) ptr(0x55d4a1c032a8, 0x55d4a1c032c0, 30, FAINT, "12 8", 3, False) @@ -761,7 +780,7 @@ def frame3(): if ch == " ": space_glyph(g, x + 23, y + 38) elif rd: - g.t(x + 23, y + 38, ch, DATA, VELLUM, "mono", "middle") + g.t(x + 23, y + 38, ch, DATA_SIZE, VELLUM, "mono", "middle") g.t(x + 23, y + 72, hx_, SM, DIM if rd else FAINT, "mono", "middle") y1 = my_ + 36 g.frame(cx - 5, y1 - 5, 8 * CELL + 8, TH + 10, READ, 3) @@ -797,16 +816,16 @@ def frame3(): ("rdx", "count", "14", "bytes"), ("rax", "returns", "", "not yet"))): y = BY + 146 + i * 44 g.line(kx, y, kx + span(4), y, KERNEL_RULE, 2) - b = base(y, 44, DATA) - g.t(kx, b, reg, DATA, KERNEL_DIM, "mono") + b = base(y, 44, DATA_SIZE) + g.t(kx, b, reg, DATA_SIZE, KERNEL_DIM, "mono") g.t(kx + 84, b - 1, name, 28, PAGE, "sansm") vw = 0 if len(val) > 4: hexgroups(g, kx + 220, b, val, all_fill=PAGE) vw = hexw(len(val)) elif val: - g.t(kx + 220, b, val, DATA, PAGE, "mono") - vw = mw(val, DATA) + g.t(kx + 220, b, val, DATA_SIZE, PAGE, "mono") + vw = mw(val, DATA_SIZE) if note: g.t(kx + 220 + (vw + 20 if val else 0), b - 1, note, 28, KERNEL_DIM, "sans") @@ -820,9 +839,9 @@ def frame3(): g.rect(x, y, CELL - 2, CELL - 2, VERM if j == 8 else KERNEL_HELD) g.frame(x, y, CELL - 2, CELL - 2, PAGE, 2) if chars[j] == " ": - space_glyph(g, x + 23, base(y, CELL - 2, DATA), INK) + space_glyph(g, x + 23, base(y, CELL - 2, DATA_SIZE), INK) else: - g.tc(x, y, CELL - 2, CELL - 2, chars[j], DATA, INK, "monob" if j == 8 else "mono") + g.tc(x, y, CELL - 2, CELL - 2, chars[j], DATA_SIZE, INK, "monob" if j == 8 else "mono") else: g.frame(x, y, CELL - 2, CELL - 2, KERNEL_RULE, 2) g.t(tx, BY + 176 + 78, "9 of 14 copied", 24, KERNEL_DIM, "sans") @@ -834,6 +853,8 @@ def frame3(): # ---- frame 4: glibc and musl malloc, side by side ------------------------- def frame4(): + global LINK + LINK = HEAP g = Svg() head(g, "malloc: glibc and musl", lit=("source", "memory"), focus="memory", where="heap.c:9 free(b);") S = 4 # logical units per byte, in the strips @@ -889,9 +910,9 @@ def frame4(): g.t(bx_ + 60, ty + CELL + 28, size, SM, DIM, "mono", "middle") g.rect(bx_, ty, 120, CELL - 2, VERM if cnt else RAISED) if cnt: - g.tc(bx_, ty, 120, CELL - 2, "1 free", DATA, INK, "monob") + g.tc(bx_, ty, 120, CELL - 2, "1 free", DATA_SIZE, INK, "monob") else: - g.tc(bx_, ty, 120, CELL - 2, "0", DATA, FAINT, "mono") + g.tc(bx_, ty, 120, CELL - 2, "0", DATA_SIZE, FAINT, "mono") arrow(g, [(px + 30, ty), (px + 30, rowy[1] + 23), (zx - 4, rowy[1] + 23)]) g.t(px, ty + CELL + 72, "its notes: a list for each size, kept in the heap itself", 24, DIM, "sans") @@ -935,9 +956,9 @@ def frame4(): bx_ = bx0 + i * CELL g.rect(bx_, yy, CELL - 2, CELL - 2, VERM if i == struck else RAISED) if i == struck: - g.tc(bx_, yy, CELL - 2, CELL - 2, bch, DATA, INK, "monob") + g.tc(bx_, yy, CELL - 2, CELL - 2, bch, DATA_SIZE, INK, "monob") else: - g.tc(bx_, yy, CELL - 2, CELL - 2, bch, DATA, VELLUM if bch == "1" else FAINT, "mono") + g.tc(bx_, yy, CELL - 2, CELL - 2, bch, DATA_SIZE, VELLUM if bch == "1" else FAINT, "mono") g.t(bx0 + 23, ty + CELL + 28, "a", SM, DIM, "mono", "middle") g.t(bx0 + CELL + 23, ty + CELL + 28, "b", SM, VELLUM, "mono", "middle") g.t(bx0 + 2 * CELL + 10, ty + CELL + 28, "one bit for each slot", 22, FAINT, "sans") @@ -950,6 +971,8 @@ def frame4(): # ---- frame 5: a memory grid mid-write, with a pointer --------------------- def frame5(): + global LINK + LINK = STACK g = Svg() head(g, "Where a pointer lives", lit=("source", "cpu", "memory"), focus="memory", where="hello.c:8") @@ -1003,9 +1026,9 @@ def frame5(): byte(g, cx + j * CELL, y, v, st) if asc: for j, ch in enumerate(asc): - g.t(ax + j * 18, base(y, CELL - 2, DATA), ch, DATA, FAINT if ch == "·" else VELLUM, "mono") + g.t(ax + j * 18, base(y, CELL - 2, DATA_SIZE), ch, DATA_SIZE, FAINT if ch == "·" else VELLUM, "mono") if label in ("buf", "p"): - g.t(lx, base(y, CELL - 2, DATA), label, DATA, VELLUM if st == "write" else DIM, "mono") + g.t(lx, base(y, CELL - 2, DATA_SIZE), label, DATA_SIZE, VELLUM if st == "write" else DIM, "mono") elif label: g.t(lx, base(y, CELL - 2, 28), label, 28, DIM, "sans") yb = rowy[0x7ffd3a2c1e40] @@ -1034,7 +1057,7 @@ def frame5(): word(g, cx, wy, "00007ffd3a2c1e40") ptr_frame(g, cx, wy) g.t(cx + 8 * CELL + 22, base(wy, CELL - 2, 28), "= where ", 28, DIM, "sans") - g.t(cx + 8 * CELL + 22 + tw("= where ", 28), base(wy, CELL - 2, DATA), "buf", DATA, VELLUM, "mono") + g.t(cx + 8 * CELL + 22 + tw("= where ", 28), base(wy, CELL - 2, DATA_SIZE), "buf", DATA_SIZE, VELLUM, "mono") g.t(cx + 8 * CELL + 22 + tw("= where ", 28) + 3 * 18 + 10, base(wy, CELL - 2, 28), "starts", 28, DIM, "sans") caption(g, [[("The pointer ",), ("p", "mono"), (" is eight ordinary bytes on the stack.",)], @@ -1053,51 +1076,63 @@ def frame6(): g.rich(MX, 540, [("glibc and musl, one ", DIM), ("free()", VELLUM, "mono"), (" at a time", DIM)], 44) x, y, w, h = panel(g, MX, 624, W - 2 * MX, 264, "legend", right="the same marks in every picture") - EW = (w - 40) / 6 + EW = (w - 40) / 8 + # What happened is on the paper; what a value is, is in its ink. entries = [ ("current", "executing now", "cur"), ("written", "by this step", "write"), ("changed", "a moment ago", "held"), - ("read", "by this step", "read"), ("pointer", "an address", "ptr"), ("moving", "in transit", "flow"), - ("allocated", "handed out", "alloc"), ("freed", "handed back", "free"), ("not seen", "never observed", "unk"), - ("kernel", "the other side", "kern"), ("fault", "signal or error", "err"), ("one object", "in every view", "pig"), + ("read", "by this step", "read"), ("moving", "in transit", "flow"), ("not seen", "never observed", "unk"), + ("kernel", "the other side", "kern"), ("fault", "signal, error", "err"), + ("stack", "points into it", STACK), ("heap", "points into it", HEAP), ("code", "points into it", CODE), + ("data", "points into it", DATA), ("rwx", "write and run", "rwx"), ("text", "a string", "str"), + ("allocated", "handed out", "alloc"), ("freed", "handed back", "free"), ] c = CELL - 2 + SW = 72 for i, (name, gloss, kind) in enumerate(entries): - ex = x + 24 + (i % 6) * EW - ey = y + 24 + (i // 6) * 96 + ex = x + 24 + (i % 8) * EW + ey = y + 24 + (i // 8) * 96 sx, sy = ex, ey + 10 if kind == "cur": - g.rect(sx, sy, 94, c, GOLD) - g.tc(sx, sy, 94, c, "rip", DATA, INK, "monob") - elif kind in ("write", "held", "read"): - byte(g, sx, sy, "2a", kind) - byte(g, sx + CELL, sy, "00", kind) - elif kind == "ptr": - g.rect(sx, sy, 34, c, RAISED) - g.frame(sx - 6, sy - 6, 46, c + 12, PTR, 4) - arrow(g, [(sx + 40, sy + 23), (sx + 94, sy + 23)]) + g.rect(sx, sy, SW, c, GOLD) + g.tc(sx, sy, SW, c, "rip", DATA_SIZE, INK, "monob") + elif kind == "write": + g.rect(sx, sy, SW, c, VERM) + g.tc(sx, sy, SW, c, "2a", DATA_SIZE, INK, "monob") + elif kind == "held": + g.rect(sx, sy, SW, c, EMBER[VERM]) + g.rect(sx, sy + c - 6, SW, 6, VERM) + g.tc(sx, sy, SW, c, "2a", DATA_SIZE, VELLUM, "mono") + elif kind == "read": + g.rect(sx, sy, SW, c, EMBER[READ]) + g.frame(sx, sy, SW, c, READ, 3) + g.tc(sx, sy, SW, c, "2a", DATA_SIZE, VELLUM, "mono") elif kind == "flow": - flow(g, [(sx, sy + 23), (sx + 94, sy + 23)]) - elif kind == "alloc": - g.rect(sx, sy, 94, c, EMBER[ALLOC]) - g.frame(sx, sy, 94, c, ALLOC, 3) - elif kind == "free": - g.rect(sx, sy, 94, c, "url(#hatch)") - g.frame(sx, sy, 94, c, DIM, 3, dash="12 8") + flow(g, [(sx, sy + 23), (sx + SW, sy + 23)]) elif kind == "unk": - byte(g, sx, sy, None) - byte(g, sx + CELL, sy, None) + g.rect(sx + 22, sy + 20, 6, 6, FAINT) + g.rect(sx + 44, sy + 20, 6, 6, FAINT) elif kind == "kern": - g.rect(sx, sy, 94, c, KERNEL) - g.tc(sx, sy, 94, c, "sys", DATA, PAGE, "monob") + g.rect(sx, sy, SW, c, KERNEL) + g.tc(sx, sy, SW, c, "sys", DATA_SIZE, PAGE, "monob") elif kind == "err": - g.rect(sx, sy, 94, c, "url(#hazard)") - g.rect(sx + 8, sy + 8, 78, c - 16, INK) - g.tc(sx + 8, sy + 8, 78, c - 16, "SEGV", SM, BRIGHT, "monob") - elif kind == "pig": - g.rect(sx, sy, 43, c, OCHRE) - g.rect(sx + 51, sy, 43, c, TEAL) - g.t(ex + 110, ey + 28, name, 28, VELLUM, "sansm") - g.t(ex + 110, ey + 58, gloss, 22, DIM, "sans") + g.rect(sx, sy, SW, c, "url(#hazard)") + g.rect(sx + 8, sy + 8, SW - 16, c - 16, INK) + g.tc(sx + 8, sy + 8, SW - 16, c - 16, "SEGV", 20, BRIGHT, "monob") + elif kind == "alloc": + g.rect(sx, sy, SW, c, EMBER[ALLOC]) + g.frame(sx, sy, SW, c, ALLOC, 3) + elif kind == "free": + g.rect(sx, sy, SW, c, "url(#hatch)") + g.frame(sx, sy, SW, c, DIM, 3, dash="12 8") + elif kind == "rwx": + g.t(sx, base(sy, c, DATA_SIZE), "7f3c", DATA_SIZE, DATA, "mono") + g.rect(sx, sy + c - 8, 4 * 18, 2, DATA) + elif kind == "str": + g.t(sx, base(sy, c, DATA_SIZE), '"hi"', DATA_SIZE, LIT, "mono") + else: # a region: the ink of an address that points there + g.t(sx, base(sy, c, DATA_SIZE), "7ffe", DATA_SIZE, kind, "mono") + g.t(ex + SW + 14, ey + 28, name, 26, VELLUM, "sansm") + g.t(ex + SW + 14, ey + 56, gloss, 18, DIM, "sans") g.save("06-title-legend") @@ -1130,9 +1165,9 @@ def frame7(): # the comparison that decided it xa, xb = x0 + (J - 1) * CW + CW / 2, x0 + J * CW + CW / 2 mid = (xa + xb) / 2 - cw_ = mw("31 > 12", DATA) + 32 + cw_ = mw("31 > 12", DATA_SIZE) + 32 g.frame(mid - cw_ / 2, y + 10, cw_, 46, DIM, 2) - g.t(mid, base(y + 10, 46, DATA), "31 > 12", DATA, VELLUM, "mono", "middle") + g.t(mid, base(y + 10, 46, DATA_SIZE), "31 > 12", DATA_SIZE, VELLUM, "mono", "middle") g.t(mid + cw_ / 2 + 16, base(y + 10, 46, 24), "so they trade places", 24, DIM, "sans") # the swap, in flight: one value over, one under @@ -1164,14 +1199,14 @@ def frame7(): lx, ly, lw, lh = panel(g, colx(7), by0, span(5), bh0, "locals", right="of sort()") for k, (name, typ, val) in enumerate((("i", "int", "5"), ("j", "int", "4"), ("n", "int", "8"), ("a", "int *", None))): yy = ly + 12 + k * 56 - b = base(yy, 46, DATA) - g.t(lx + 24, b, name, DATA, VELLUM, "mono") + b = base(yy, 46, DATA_SIZE) + g.t(lx + 24, b, name, DATA_SIZE, VELLUM, "mono") g.t(lx + 84, base(yy, 46, SM), typ, SM, FAINT, "mono") if val: if name == "j": g.rect(lx + 196, yy, 76, 46, EMBER[VERM]) g.rect(lx + 196, yy + 40, 76, 6, VERM) - g.t(lx + 212, b, val, DATA, VELLUM, "mono") + g.t(lx + 212, b, val, DATA_SIZE, VELLUM, "mono") else: hexgroups(g, lx + 212, b, "7ffd3a2c1e20") g.t(lx + 212 + hexw(12) + 20, base(yy, 46, 24), "where a[0] is", 24, DIM, "sans") diff --git a/design/mock/list.c b/design/mock/list.c new file mode 100644 index 0000000..e09d301 --- /dev/null +++ b/design/mock/list.c @@ -0,0 +1,42 @@ +// A linked list on the heap: built, reversed, summed. `list_done` gets the +// finished list, the place for a tracer to look at it. +#include +#include + +struct node { + long value; + struct node *next; +}; + +__attribute__((noinline)) static struct node *push(struct node *head, long value) { + struct node *n = malloc(sizeof *n); + n->value = value; + n->next = head; + return n; +} + +__attribute__((noinline)) static struct node *reverse(struct node *head) { + struct node *prev = NULL; + while (head) { + struct node *next = head->next; + head->next = prev; + prev = head; + head = next; + } + return prev; +} + +__attribute__((noinline)) long list_done(struct node *head) { + long sum = 0; + for (; head; head = head->next) sum += head->value; + return sum; +} + +int main(int argc, char **argv) { + int n = argc > 1 ? atoi(argv[1]) : 6; + struct node *head = NULL; + for (int i = n; i >= 1; i--) head = push(head, i * i); + head = reverse(head); + printf("sum = %ld\n", list_done(head)); + return 0; +} -- cgit v1.3