diff options
| author | Gabriel Schneider <[email protected]> | 2026-11-10 23:04:17 -0300 |
|---|---|---|
| committer | Gabriel Schneider <[email protected]> | 2026-11-10 23:25:40 -0300 |
| commit | 58643dab07a803dbb579d84f68419911f5a2a16d (patch) | |
| tree | b97c9ab4f5ccd27448ecb609ea8879a42a8fe992 /targets/heap.c | |
| parent | b4ab1d770edb06d3eeebfd290bbf97725e55b7fa (diff) | |
| download | computeranim-58643dab07a803dbb579d84f68419911f5a2a16d.tar.gz computeranim-58643dab07a803dbb579d84f68419911f5a2a16d.zip | |
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 <[email protected]>
Claude-Session: https://claude.ai/code/session_016Y9MNCKgYCGiANnEwcEQxa
Diffstat (limited to 'targets/heap.c')
| -rw-r--r-- | targets/heap.c | 29 |
1 files changed, 29 insertions, 0 deletions
diff --git a/targets/heap.c b/targets/heap.c new file mode 100644 index 0000000..7f999e9 --- /dev/null +++ b/targets/heap.c @@ -0,0 +1,29 @@ +// glibc's malloc a step at a time, for `ztrace record --heap -b heap_step`: each +// call of `heap_step` follows one change to the heap. Seventeen chunks of one +// size fill its tcache bin (7 before glibc 2.42, 16 by default since) and +// pass the rest on, to a fastbin where there are fastbins; a chunk too large +// for the tcache, freed beside one in use, goes to the unsorted bin; a +// larger request sorts it into a large bin (2.40 consolidates the fastbins +// first) and is served from the top, to which it returns when freed. +#include <stdlib.h> + +// Volatile, or the compiler proves the allocations away. +static void *volatile small[17], *volatile big, *volatile guard, *volatile larger; + +__attribute__((noinline)) void heap_step(int n) { __asm__ volatile("" : : "r"(n) : "memory"); } + +int main(void) { + for (int i = 0; i < 17; i++) small[i] = malloc(0x28); + big = malloc(0x500); + guard = malloc(0x18); + heap_step(1); + for (int i = 0; i < 17; i++) free(small[i]); + heap_step(2); + free(big); + heap_step(3); + larger = malloc(0x1000); + heap_step(4); + free(larger); + heap_step(5); + return 0; +} |
