From 58643dab07a803dbb579d84f68419911f5a2a16d Mon Sep 17 00:00:00 2001 From: Gabriel Schneider Date: Tue, 10 Nov 2026 23:04:17 -0300 Subject: trace: glibc's heap, read from a recording at any moment record --heap keeps the arena, the tcache and, of every chunk, its size word and the links of those on a list: free chunks stay readable, which --alloc dropped at the free. zt.heap.glibc gives the chunks in address order, the tcache's and the bins' chains (safe-linking as the stored value and the pointer it protects), the top; each with the address of the word it was read from, each chain saying how it ended. ztrace show prints it. Layouts for glibc 2.44 (no fastbins; a tcache made at the first free, counting the slots left) and 2.40; the arena found by symbol or by the struct whose next points at itself. Against pwndbg on 2.40: the same at every stop. Three bytes a moment and a sixth slower on the list by instruction. Co-Authored-By: Claude Fable 5.2 Claude-Session: https://claude.ai/code/session_016Y9MNCKgYCGiANnEwcEQxa --- README.md | 45 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 45 insertions(+) (limited to 'README.md') 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 -- cgit v1.3