summaryrefslogtreecommitdiff
path: root/README.md
diff options
context:
space:
mode:
Diffstat (limited to 'README.md')
-rw-r--r--README.md45
1 files changed, 45 insertions, 0 deletions
diff --git a/README.md b/README.md
index b960593..05a8823 100644
--- a/README.md
+++ b/README.md
@@ -316,6 +316,51 @@ before. Capstone is built from source for x86
(`build/capstone.zig`), into the command only: the library takes a
disassembler as a function (`Debugger.disasm`) and links none.
+## The heap
+
+`ztrace record --heap` records glibc's malloc at every stop as pwndbg's
+`heap` and `bins` show it, and `zt.heap.glibc(arena, moment, tid)` reads it
+back: the main heap's chunks in address order (size, the P, M, A bits, free
+or in use, the list that holds it), the thread's tcache (each bin's count
+and chain), the fastbins, the unsorted, small and large bins, the top, the
+arena's fields. Each link is given as stored and as the pointer it holds
+(safe-linking undone), each with the address of the word it was read from;
+a list ends where the recording has no memory, and says so. `ztrace show`
+prints it. The debugger runs the reader's own walk on the live target,
+every word it reads recorded as changed words are (`Debugger.Recorded`):
+the arena and the tcache whole, each chunk's size word, the two words of
+each chunk on a list. At most 4096 chunks and 256 entries a list a stop.
+
+Where to begin is found once: the version from the banner in libc's file,
+the main arena by its symbol where there is one (a static binary, an
+unstripped libc) and else in libc's writable data as the struct whose
+`next` is itself, the tcache as the word below the thread pointer that
+points at a chunk of the tcache's size. Known: glibc 2.44 (this machine's,
+stripped: no fastbins, 76 tcache bins counted down, 16 a bin, the tcache
+made at the first free) and 2.40 (pwndbg's own, the classic layout);
+another version is refused by name. A static glibc has no version in it:
+its layout is told by the size of `main_arena`. Not read: a stripped static
+glibc, musl, the arenas of other threads, mmapped chunks (`--alloc` has
+them). `zig build test-heap` records `targets/heap.c` under both and checks
+each stop against the live process.
+
+```
+$ ztrace record --by break -b heap_step --heap -- zig-out/targets/heap && ztrace show out.ztrace -1
+glibc 2.44 heap: main_arena 7f59cd219ac0, top 18fcab60, last remainder 0, system mem 21000, flags 0
+ 000018fca000 30 P-- x16 free (tcache 30)
+ 000018fca300 540 P-- free (large bin 69)
+ 000018fca840 20 --- in use
+ 000018fca860 300 P-- in use
+ 000018fcab60 204a0 P-- top
+tcache 18fca870
+ tcache 30 [16, 0 left]: 18fca2d0 (18fd2d7a) —▸ 18fca2a0 (18fd2d4a) —▸ 18fca270 (18fd2d9a) —▸ 18fca240 (18fd2dea) —▸ ... 12 more 0
+ large bin 69: 18fca300 —▸ the bin
+```
+
+By instruction from `main` it costs a quarter of the moments a second
+and 2 bytes a moment: `targets/heap.c` 55 000 to 41 000 and 112 to 114
+bytes, the list of `anim/tools` 48 000 to 41 000 and 170 to 173.
+
## The debugger
`ztrace dbg -- PROGRAM` starts a program stopped and reads commands (from a