summaryrefslogtreecommitdiff
path: root/src/trace/heap.zig
blob: 8f0dc68f1a9bfb8969319ea80c6a4fdf85d932f2 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
//! glibc's malloc (ptmalloc) as a debugger shows it, read from a moment of a
//! recording: the main heap's chunks in address order, the tcache, the
//! fastbins, the unsorted, small and large bins, the top. Nothing is
//! guessed: a list goes as far as the recording has the memory it passes
//! through, and says where it stopped.
//!
//! The recording side is `Watch`: at each stop the debugger runs the same
//! walk on the live target, every word it reads sent as the changed words
//! are (`Debugger.Recorded`), so a recording holds what this reader reads and
//! no more: the arena's fields and bin heads, the tcache's counts and heads,
//! each chunk's size word, and the links of each chunk on a list. Where to
//! begin is found once and recorded as two strings: the version and the
//! main arena (`glibc_id`), and where a thread keeps its tcache pointer
//! (`tcache_id`).
const std = @import("std");
const event = @import("event.zig");
const store = @import("store.zig");
const Debugger = @import("Debugger.zig");

/// Where a glibc keeps what is read here: offsets into `struct
/// malloc_state` (the arena) and `tcache_perthread_struct`.
pub const Layout = struct {
    version: [2]u16,
    arena_size: u64,
    top: u64,
    last_remainder: u64,
    /// bins[0]: bin i's fd is at bins + 16 (i - 1), its bk a word on; the bin
    /// itself, as a chunk the list comes back to, is 16 bytes before its fd.
    bins: u64,
    next: u64,
    attached_threads: u64,
    /// system_mem; max_system_mem is the word after it.
    system_mem: u64,
    /// fastbinsY, and how many there are.
    fastbins: u64 = 0,
    fastbin_count: u8 = 0,
    /// The tcache: a u16 a bin from its start, a pointer a bin from
    /// `tcache_entries`, in a chunk of `tcache_chunk` bytes.
    tcache_bins: u8,
    tcache_entries: u64,
    tcache_chunk: u64,
    /// The u16s count the slots left in a bin, not the chunks in it.
    tcache_slots: bool,
    /// A tcache entry's next and a fastbin's fd are stored xor their own
    /// address shifted right by 12 (`PROTECT_PTR`).
    safe_linking: bool,
};

/// The glibcs this reader knows, each tested here.
pub const layouts = [_]Layout{
    // pwndbg's own (/usr/local/lib/pwndbg-gdb/lib), the classic layout.
    .{ .version = .{ 2, 40 }, .arena_size = 0x898, .fastbins = 0x10, .fastbin_count = 10, .top = 0x60, .last_remainder = 0x68, .bins = 0x70, .next = 0x870, .attached_threads = 0x880, .system_mem = 0x888, .tcache_bins = 64, .tcache_entries = 0x80, .tcache_chunk = 0x290, .tcache_slots = false, .safe_linking = true },
    // This machine's: no fastbins, 76 tcache bins counted down (16 slots each by default).
    .{ .version = .{ 2, 44 }, .arena_size = 0x840, .top = 0x8, .last_remainder = 0x10, .bins = 0x18, .next = 0x818, .attached_threads = 0x828, .system_mem = 0x830, .tcache_bins = 76, .tcache_entries = 0x98, .tcache_chunk = 0x300, .tcache_slots = true, .safe_linking = true },
};

pub fn layoutOf(version: [2]u16) ?Layout {
    for (layouts) |l| if (std.mem.eql(u16, &l.version, &version)) return l;
    return null;
}

/// The string `glibc_id`: the target's glibc and its main arena. `by_size`:
/// the binary says no version (glibc linked in statically), the layout was
/// told by the size of `main_arena` in its symbols.
pub const Glibc = extern struct { major: u16, minor: u16, by_size: u8 = 0, pad: [3]u8 = @splat(0), arena: u64 };
pub const glibc_id = event.stringId("glibc malloc");
/// The string `tcache_id`, an i64: where a thread's `tcache` pointer is
/// from its thread pointer (fs_base). The same in every thread.
pub const tcache_id = event.stringId("glibc tcache");

/// The most chunks walked, and entries followed on one list: what a stop
/// costs at most, and the reader stops where the recorder did.
pub const max_chunks = 4096;
pub const max_chain = 256;

/// Which of malloc's lists holds a chunk.
pub const Where = union(enum) {
    none,
    /// The tcache's bin of this index.
    tcache: u8,
    fastbin: u8,
    /// 1 the unsorted bin, 2 to 63 the small bins, 64 on the large.
    bin: u8,
    top,

    /// The size of the chunks the list holds: 0 for a range (a large bin's).
    pub fn size(in: Where) u64 {
        return switch (in) {
            .tcache, .fastbin => |i| if (i < 64) 0x20 + 0x10 * @as(u64, i) else 0,
            .bin => |i| if (i >= 2 and i < 64) 0x10 * @as(u64, i) else 0,
            else => 0,
        };
    }

    /// "tcache 0x30", "fastbin 0x30", "unsorted", "small 0x90", "large bin 70".
    pub fn format(in: Where, w: *std.Io.Writer) std.Io.Writer.Error!void {
        const size_ = in.size();
        switch (in) {
            .none => {},
            .top => try w.writeAll("top"),
            .tcache => |i| if (size_ != 0) try w.print("tcache {x}", .{size_}) else try w.print("tcache bin {d}", .{i}),
            .fastbin => try w.print("fastbin {x}", .{size_}),
            .bin => |i| if (i == 1) try w.writeAll("unsorted") else if (size_ != 0) try w.print("small {x}", .{size_}) else try w.print("large bin {d}", .{i}),
        }
    }
};

/// Why a walk ended.
pub const End = enum {
    /// Where the list ends (a null, or back at its bin), or at the top.
    end,
    /// At memory the recording does not have.
    unknown,
    /// At a pointer that is no chunk's (misaligned), or a size no chunk has.
    bad,
    /// After `max_chain` entries (a loop?) or `max_chunks` chunks.
    limit,
};

pub const Chunk = struct {
    /// Its first word (prev_size); its size word is at addr + 8.
    addr: u64,
    /// As stored: the size, and the bits P (1: the chunk before is in
    /// use), M (2: mmapped), A (4: not the main arena's).
    size_word: u64,
    in: Where = .none,
    /// On a list, or the chunk after it says it is not in use.
    free: bool = false,

    pub fn size(c: Chunk) u64 {
        return c.size_word & ~@as(u64, 7);
    }
};

/// An entry of a list: a chunk, and the link read from it.
pub const Entry = struct {
    chunk: u64,
    /// The word that links it to the next, at chunk + 16 (fd; a tcache
    /// entry's next), as stored, and the pointer it holds: `stored` itself,
    /// or unprotected (`stored ^ link_at >> 12`) under safe-linking. A
    /// tcache's pointers are to a chunk's memory, 16 bytes into it.
    link_at: u64,
    stored: u64,
    next: u64,
    /// The word after the link: a bin's bk, a tcache entry's key (0 for a fastbin: not read).
    second: u64 = 0,
};

pub const Chain = struct {
    in: Where,
    /// Where the list's head is kept (a tcache entries slot, a fastbinsY
    /// slot, a bin's fd) and what is there.
    head_at: u64,
    head: u64,
    /// A tcache bin's u16 and where it is: the chunks in the bin, or (with
    /// `Layout.tcache_slots`) the slots left in it.
    count_at: u64 = 0,
    count: ?u16 = null,
    entries: []Entry = &.{},
    end: End = .end,
    /// The address the walk could not go on from, when it did not end.
    end_at: u64 = 0,
};

pub const Arena = struct {
    /// Each field is the word at its offset in the `Layout` (`flags` the
    /// high half of the first word, the mutex its low half).
    addr: u64,
    flags: u32,
    top: u64,
    last_remainder: u64,
    next: u64,
    system_mem: u64,
    max_system_mem: u64,
};

pub const Tcache = struct {
    /// The tcache_perthread_struct, and the thread's pointer to it.
    addr: u64,
    slot: u64,
    /// The bins that hold anything.
    bins: []Chain,
};

pub const Heap = struct {
    glibc: Glibc,
    layout: Layout,
    arena: Arena,
    /// The main heap's chunks in address order, the top last.
    chunks: []Chunk,
    /// Why the walk of the chunks stopped before the top, and where.
    cut: End = .end,
    cut_at: u64 = 0,
    /// Null: the thread has none yet (glibc 2.42 on makes it at the first
    /// free), or its pointer is not known.
    tcache: ?Tcache = null,
    /// The fastbins and the bins that hold anything.
    fastbins: []Chain = &.{},
    bins: []Chain = &.{},
};

/// The heap at a moment as thread `tid` sees it (its tcache), from what the
/// recording holds: null when it holds none (`ztrace record --heap`), or not
/// yet at this moment. Allocates from `arena`.
pub fn glibc(arena: std.mem.Allocator, m: store.Moment, tid: u32) !?Heap {
    const text = m.trace.strings.get(glibc_id) orelse return null;
    if (text.len != @sizeOf(Glibc)) return null;
    const g = std.mem.bytesToValue(Glibc, text[0..@sizeOf(Glibc)]);
    var slot: ?u64 = null;
    if (m.trace.strings.get(tcache_id)) |off| if (off.len == 8) if (try m.regs(tid)) |regs| {
        slot = regs.fs_base +% @as(u64, @bitCast(std.mem.readInt(i64, off[0..8], .little)));
    };
    return walk(arena, Known{ .m = m }, g, slot);
}

/// The most read at once: an arena, a tcache, each read whole.
const max_struct = 0x900;

/// A moment's memory as the walk reads it: what the recording has, all of
/// it, or nothing.
const Known = struct {
    m: store.Moment,
    pub fn read(k: Known, addr: u64, buf: []u8) usize {
        var known: [max_struct]bool = undefined;
        k.m.read(addr, buf, known[0..buf.len]) catch return 0;
        return if (std.mem.indexOfScalar(bool, known[0..buf.len], false) == null) buf.len else 0;
    }
};

fn word(mem: anytype, addr: u64) ?u64 {
    var bytes: [8]u8 = undefined;
    return if (mem.read(addr, &bytes) == 8) std.mem.readInt(u64, &bytes, .little) else null;
}

fn field(bytes: []const u8, off: u64) u64 {
    return std.mem.readInt(u64, bytes[off..][0..8], .little);
}

/// The walk, over `mem` (anything with `read(addr, buf) usize`: a moment, the
/// debugger, the debugger recording what it reads). `slot` is the thread's
/// tcache pointer's address. Null when the arena cannot be read. In this
/// order, the same every time: the arena (whole), the tcache (whole), the
/// tcache's lists, the fastbins, the bins, the chunks.
pub fn walk(arena: std.mem.Allocator, mem: anytype, g: Glibc, slot: ?u64) !?Heap {
    const l = layoutOf(.{ g.major, g.minor }) orelse return null;
    const a = g.arena;
    var arena_buf: [max_struct]u8 = undefined;
    const ab = arena_buf[0..l.arena_size];
    if (mem.read(a, ab) != ab.len) return null;
    var h: Heap = .{ .glibc = g, .layout = l, .chunks = &.{}, .arena = .{
        .addr = a,
        .flags = @truncate(field(ab, 0) >> 32),
        .top = field(ab, l.top),
        .last_remainder = field(ab, l.last_remainder),
        .next = field(ab, l.next),
        .system_mem = field(ab, l.system_mem),
        .max_system_mem = field(ab, l.system_mem + 8),
    } };

    // The tcache, when the thread's pointer leads to a chunk of its size.
    if (slot) |at| if (word(mem, at)) |t| if (t % 16 == 0) if (word(mem, t -% 8)) |size| if (size & ~@as(u64, 7) == l.tcache_chunk) {
        var tcache_buf: [max_struct]u8 = undefined;
        const tb = tcache_buf[0 .. l.tcache_entries + 8 * @as(u64, l.tcache_bins)];
        if (mem.read(t, tb) == tb.len) {
            var bins: std.ArrayList(Chain) = .empty;
            // shortcut: 2.44's large bins (64 on, used only when the tunable glibc.malloc.tcache_max is raised) are walked as the small ones are, untested; upgrade with a target that raises it.
            for (0..l.tcache_bins) |i| {
                const count = std.mem.readInt(u16, tb[2 * i ..][0..2], .little);
                const head = field(tb, l.tcache_entries + 8 * i);
                if (head == 0 and (l.tcache_slots or count == 0)) continue;
                var c: Chain = .{ .in = .{ .tcache = @intCast(i) }, .head_at = t + l.tcache_entries + 8 * i, .head = head, .count_at = t + 2 * i, .count = count };
                try follow(arena, mem, &c, head, l, 0);
                try bins.append(arena, c);
            }
            h.tcache = .{ .addr = t, .slot = at, .bins = bins.items };
        }
    };

    var fastbins: std.ArrayList(Chain) = .empty;
    for (0..l.fastbin_count) |i| {
        const head_at = a + l.fastbins + 8 * i;
        const head = field(ab, l.fastbins + 8 * i);
        if (head == 0) continue;
        var c: Chain = .{ .in = .{ .fastbin = @intCast(i) }, .head_at = head_at, .head = head };
        try follow(arena, mem, &c, head, l, 0);
        try fastbins.append(arena, c);
    }
    h.fastbins = fastbins.items;

    var bins: std.ArrayList(Chain) = .empty;
    for (1..128) |i| {
        const head_at = a + l.bins + 16 * (i - 1);
        const head = field(ab, l.bins + 16 * (i - 1));
        // An empty bin points at itself.
        if (head == head_at - 16) continue;
        var c: Chain = .{ .in = .{ .bin = @intCast(i) }, .head_at = head_at, .head = head };
        try follow(arena, mem, &c, head, l, head_at - 16);
        try bins.append(arena, c);
    }
    h.bins = bins.items;

    // The chunks, from where the heap begins to the top: the main arena's
    // memory is one run from sbrk's first, `system_mem` long.
    if (h.arena.system_mem == 0) return h;
    var chunks: std.ArrayList(Chunk) = .empty;
    const top = h.arena.top;
    if (word(mem, top + 8)) |top_size| {
        var c = top +% (top_size & ~@as(u64, 7)) -% h.arena.system_mem;
        if (c > top or c % 16 != 0) {
            h.cut = .bad;
            h.cut_at = c;
        } else while (c < top) {
            if (chunks.items.len >= max_chunks) {
                h.cut = .limit;
                h.cut_at = c;
                break;
            }
            const sw = word(mem, c + 8) orelse {
                h.cut = .unknown;
                h.cut_at = c + 8;
                break;
            };
            const size = sw & ~@as(u64, 7);
            if (size < 32 or size % 16 != 0 or size > top - c) {
                h.cut = .bad;
                h.cut_at = c;
                break;
            }
            try chunks.append(arena, .{ .addr = c, .size_word = sw });
            c += size;
        }
        try chunks.append(arena, .{ .addr = top, .size_word = top_size, .in = .top });
    } else {
        h.cut = .unknown;
        h.cut_at = top + 8;
    }
    h.chunks = chunks.items;

    // Which list holds each chunk, and which are free.
    var held: std.AutoHashMapUnmanaged(u64, Where) = .empty;
    if (h.tcache) |t| for (t.bins) |c| for (c.entries) |e| try held.put(arena, e.chunk, c.in);
    for (h.fastbins) |c| for (c.entries) |e| try held.put(arena, e.chunk, c.in);
    for (h.bins) |c| for (c.entries) |e| try held.put(arena, e.chunk, c.in);
    for (h.chunks, 0..) |*c, i| {
        if (c.in == .none) c.in = held.get(c.addr) orelse .none;
        const after = if (i + 1 < h.chunks.len and h.chunks[i + 1].addr == c.addr + c.size()) h.chunks[i + 1] else null;
        c.free = c.in != .none and c.in != .top or (after != null and after.?.size_word & 1 == 0);
    }
    return h;
}

/// Follows a list from `first` (a chunk, or a tcache's pointer into one)
/// until `end`, recording each entry.
fn follow(arena: std.mem.Allocator, mem: anytype, c: *Chain, first: u64, l: Layout, end: u64) !void {
    const tcache = c.in == .tcache;
    const protected = l.safe_linking and c.in != .bin;
    var entries: std.ArrayList(Entry) = .empty;
    var p = first;
    while (p != end) {
        if (entries.items.len >= max_chain) {
            c.end = .limit;
            c.end_at = p;
            break;
        }
        const chunk = if (tcache) p -% 16 else p;
        if (chunk % 16 != 0) {
            c.end = .bad;
            c.end_at = p;
            break;
        }
        const link_at = chunk + 16;
        const stored = word(mem, link_at) orelse {
            c.end = .unknown;
            c.end_at = link_at;
            break;
        };
        const second = if (c.in == .fastbin) 0 else word(mem, link_at + 8) orelse {
            c.end = .unknown;
            c.end_at = link_at + 8;
            break;
        };
        const next = if (protected) stored ^ link_at >> 12 else stored;
        try entries.append(arena, .{ .chunk = chunk, .link_at = link_at, .stored = stored, .next = next, .second = second });
        p = next;
    }
    c.entries = entries.items;
}

/// The heap as `ztrace show` prints it, pwndbg's `heap` and `bins` in a
/// few lines each: runs of alike chunks are one line.
pub fn print(h: Heap, w: *std.Io.Writer) !void {
    const a = h.arena;
    try w.print("\nglibc {d}.{d}{s} heap: main_arena {x}, top {x}, last remainder {x}, system mem {x}, flags {x}\n", .{ h.glibc.major, h.glibc.minor, if (h.glibc.by_size != 0) " (by layout)" else "", a.addr, a.top, a.last_remainder, a.system_mem, a.flags });
    var i: usize = 0;
    while (i < h.chunks.len) {
        const c = h.chunks[i];
        var n: usize = 1;
        while (i + n < h.chunks.len and alike(h.chunks[i + n], c)) n += 1;
        try w.print("  {x:0>12} {x:>6} {s}{s}{s} ", .{ c.addr, c.size(), if (c.size_word & 1 != 0) "P" else "-", if (c.size_word & 2 != 0) "M" else "-", if (c.size_word & 4 != 0) "A" else "-" });
        if (n > 1) try w.print("x{d} ", .{n});
        try w.writeAll(if (c.in == .top) "top" else if (c.free) "free" else "in use");
        if (c.in != .none and c.in != .top) try w.print(" ({f})", .{c.in});
        try w.writeAll("\n");
        i += n;
    }
    if (h.cut != .end) try w.print("  ... {t} at {x}: no further\n", .{ h.cut, h.cut_at });
    if (h.tcache) |t| {
        try w.print("tcache {x}\n", .{t.addr});
        for (t.bins) |c| try printChain(w, c, h.layout);
    }
    for (h.fastbins) |c| try printChain(w, c, h.layout);
    for (h.bins) |c| try printChain(w, c, h.layout);
}

fn alike(x: Chunk, y: Chunk) bool {
    return x.size_word == y.size_word and x.free == y.free and x.in != .top and std.meta.eql(x.in, y.in);
}

fn printChain(w: *std.Io.Writer, c: Chain, l: Layout) !void {
    try w.print("  {f}", .{c.in});
    if (c.count) |n| {
        if (l.tcache_slots) try w.print(" [{d}, {d} left]", .{ c.entries.len, n }) else try w.print(" [{d}]", .{n});
    }
    try w.writeAll(":");
    const shown = @min(c.entries.len, 4);
    for (c.entries[0..shown]) |e| {
        try w.print(" {x}", .{e.chunk});
        if (e.stored != e.next) try w.print(" ({x})", .{e.stored});
        try w.writeAll(" —▸");
    }
    if (c.entries.len > shown) try w.print(" ... {d} more", .{c.entries.len - shown});
    switch (c.end) {
        .end => if (c.in == .bin) try w.writeAll(" the bin\n") else try w.writeAll(" 0\n"),
        else => |e| try w.print(" {t} at {x}\n", .{ e, c.end_at }),
    }
}

// The recording side.

/// What a debugger keeps to record the heap at each stop (`Debugger.heap`).
pub const Watch = struct {
    /// Found once: the version, the layout, the main arena.
    glibc: ?Glibc = null,
    /// Where the thread's tcache pointer is from its thread pointer, once a tcache is there.
    tcache: ?i64 = null,
    /// What stopped the looking, when it stopped without a heap.
    trouble: Trouble = .none,
    version: [2]u16 = .{ 0, 0 },
    /// Until the arena is found: how many modules were looked through, and
    /// the stripped C library whose data holds it (looked through at each
    /// stop: its loader may not have relocated it yet).
    modules: usize = 0,
    stripped: ?struct { path: []const u8, g: Glibc } = null,

    pub const Trouble = enum { none, no_version, no_layout, no_arena };

    /// At a stop: find what is not found yet, then walk the heap, the words read recorded.
    pub fn atStop(w: *Watch, d: *Debugger) !void {
        if (w.glibc == null) {
            if (w.trouble != .none) return;
            w.glibc = (try w.find(d)) orelse return;
            try emitString(d, glibc_id, std.mem.asBytes(&w.glibc.?));
        }
        const g = w.glibc.?;
        const l = layoutOf(.{ g.major, g.minor }).?;
        if (w.tcache == null) if (try findTcache(d, g, l)) |off| {
            w.tcache = off;
            try emitString(d, tcache_id, std.mem.asBytes(&off));
        };
        var arena: std.heap.ArenaAllocator = .init(d.gpa);
        defer arena.deinit();
        var rec: Debugger.Recorded = .{ .d = d };
        _ = try walk(arena.allocator(), &rec, g, if (w.tcache) |off| d.regs.fs_base +% @as(u64, @bitCast(off)) else null);
        if (rec.failed) |err| return err;
    }

    /// The glibc of the target and its main arena: by the symbol where
    /// there is one, else by what the arena holds from the start (its next
    /// is itself). Null to look again at the next stop (the C library is not
    /// mapped yet, or not relocated); `trouble` set to give up.
    fn find(w: *Watch, d: *Debugger) error{OutOfMemory}!?Glibc {
        const lib = w.stripped orelse lib: {
            // Looked for again only when a module was mapped since.
            if (d.syms.modules.items.len == w.modules) return null;
            w.modules = d.syms.modules.items.len;
            const sym = d.syms.find("main_arena");
            const libc = if (sym) |s| d.syms.moduleAt(s.addr) else for (d.syms.modules.items) |*m| {
                const name = std.fs.path.basename(m.path);
                if (m.end != 0 and (std.mem.startsWith(u8, name, "libc.so") or std.mem.startsWith(u8, name, "libc-2."))) break m;
            } else null;
            const module = libc orelse return null;
            const version = if (module.file) |file| banner(file.mapped_file) else null;
            // A static binary says no version: the size of its main_arena tells the layout.
            const l = (if (version) |v| layoutOf(v) else if (sym) |s| bySize(s.size) else null) orelse {
                w.version = version orelse .{ 0, 0 };
                w.trouble = if (version != null or sym != null) .no_layout else .no_version;
                return null;
            };
            var g: Glibc = .{ .major = l.version[0], .minor = l.version[1], .by_size = @intFromBool(version == null), .arena = 0 };
            if (sym) |s| {
                g.arena = s.addr;
                return g;
            }
            w.stripped = .{ .path = module.path, .g = g };
            break :lib w.stripped.?;
        };
        var g = lib.g;
        const l = layoutOf(.{ g.major, g.minor }).?;
        // Stripped: in the library's writable data, the struct whose next is
        // itself (main_arena's, until a second arena is made) with threads attached.
        for (d.syms.maps.list) |m| {
            if (m.prot & 3 != 3 or !std.mem.eql(u8, m.name, lib.path)) continue;
            const bytes = try d.gpa.alloc(u8, m.end - m.start);
            defer d.gpa.free(bytes);
            const n = d.read(m.start, bytes) & ~@as(usize, 7);
            var off: usize = 0;
            while (off + l.arena_size <= n) : (off += 8) {
                const at = m.start + off;
                if (std.mem.readInt(u64, bytes[off + l.next ..][0..8], .little) != at) continue;
                if (std.mem.readInt(u64, bytes[off + l.attached_threads ..][0..8], .little) == 0) continue;
                if (g.arena != 0) {
                    w.trouble = .no_arena;
                    return null;
                }
                g.arena = at;
            }
        }
        // shortcut: a process that made a second arena before the first look is not found, upgrade by following next round to the start.
        return if (g.arena != 0) g else null;
    }
};

/// The one layout whose arena is `size` bytes, when only one is.
fn bySize(size: u64) ?Layout {
    var found: ?Layout = null;
    for (layouts) |l| if (l.arena_size == size) {
        if (found != null) return null;
        found = l;
    };
    return found;
}

/// "GNU C Library (GNU libc) stable release version 2.44." in the library's file.
fn banner(bytes: []const u8) ?[2]u16 {
    const at = std.mem.indexOf(u8, bytes, "GNU C Library") orelse return null;
    const text = bytes[at..@min(bytes.len, at + 256)];
    const v = std.mem.indexOf(u8, text, "version ") orelse return null;
    var parts = std.mem.tokenizeAny(u8, text[v + 8 ..], ".\x00\n ");
    const major = std.fmt.parseInt(u16, parts.next() orelse return null, 10) catch return null;
    const minor = std.fmt.parseInt(u16, parts.next() orelse return null, 10) catch return null;
    return .{ major, minor };
}

/// Where the stopped thread keeps its tcache pointer, from its thread
/// pointer: in the static TLS below it (64 KiB of it at most: a library
/// preloaded has its block between the program's and libc's), the word
/// that points at a chunk of the tcache's size in the main heap. Null until
/// the tcache is made.
fn findTcache(d: *Debugger, g: Glibc, l: Layout) !?i64 {
    const tp = d.regs.fs_base;
    var w: [8]u8 = undefined;
    const top = if (d.read(g.arena + l.top, &w) == 8) std.mem.readInt(u64, &w, .little) else return null;
    const system_mem = if (d.read(g.arena + l.system_mem, &w) == 8) std.mem.readInt(u64, &w, .little) else return null;
    if (system_mem == 0 or top < system_mem) return null;
    const m = d.syms.mappingAt(tp -% 8) orelse return null;
    const from = @max(m.start, tp -| (64 << 10)) & ~@as(u64, 7);
    const tls = try d.gpa.alloc(u64, (tp - from) / 8);
    defer d.gpa.free(tls);
    const n = d.read(from, std.mem.sliceAsBytes(tls)) / 8;
    for (tls[0..n], 0..) |p, i| {
        if (p % 16 != 0 or p < top -| system_mem or p >= top) continue;
        if (d.read(p - 8, &w) != 8 or std.mem.readInt(u64, &w, .little) & ~@as(u64, 7) != l.tcache_chunk) continue;
        return @as(i64, @intCast(from + 8 * i)) - @as(i64, @intCast(tp));
    }
    return null;
}

fn emitString(d: *Debugger, id: u64, bytes: []const u8) !void {
    const sink = d.sink orelse return;
    try sink.emit(.{ .ts = event.now(), .tid = @intCast(d.proc.tid), .kind = .string, .a = .{ id, 0, 0, 0, 0, 0 } }, bytes);
}

test "a heap is walked from a recording, as far as it was recorded" {
    const gpa = std.testing.allocator;
    var tmp = std.testing.tmpDir(.{});
    defer tmp.cleanup();
    var dir_buf: [std.Io.Dir.max_path_bytes]u8 = undefined;
    const dir = dir_buf[0..try tmp.dir.realPath(std.testing.io, &dir_buf)];
    const path = try std.fs.path.join(gpa, &.{ dir, "t.ztrace" });
    defer gpa.free(path);

    // A glibc 2.40 arena at 0x10000 and its heap at 0x20000: the tcache
    // struct first, then a chunk in its 0x30 bin, one in the 0x30 fastbin,
    // one in the unsorted bin, one in use, the top.
    const l = layoutOf(.{ 2, 40 }).?;
    var rec = try store.Recorder.create(gpa, std.testing.io, path);
    const put = struct {
        fn at(r: *store.Recorder, addr: u64, value: u64) !void {
            try r.emit(.{ .kind = .mem_write, .tid = 1, .a = .{ addr, 8, 0, 0, 0, 0 } }, std.mem.asBytes(&value));
        }
    }.at;
    const a = 0x10000;
    const heap = 0x20000;
    const g: Glibc = .{ .major = 2, .minor = 40, .arena = a };
    try rec.emit(.{ .kind = .string, .a = .{ glibc_id, 0, 0, 0, 0, 0 } }, std.mem.asBytes(&g));
    try rec.emit(.{ .kind = .string, .a = .{ tcache_id, 0, 0, 0, 0, 0 } }, std.mem.asBytes(&@as(i64, -0x10)));
    try rec.emit(.{ .kind = .step, .tid = 1 }, std.mem.asBytes(&event.Regs{ .fs_base = 0x9010 }));
    try put(&rec, 0x9000, heap + 0x10);
    const tc = heap + 0x10;
    const t_chunk = heap + 0x290;
    const f_chunk = t_chunk + 0x30;
    const u_chunk = f_chunk + 0x30;
    const used = u_chunk + 0x100;
    const top = used + 0x20;
    // The arena and the tcache, as the recorder sends them: whole.
    var words: [0x898 / 8]u64 = @splat(0);
    words[l.top / 8] = top;
    words[l.next / 8] = a;
    words[l.attached_threads / 8] = 1;
    words[l.system_mem / 8] = 0x21000;
    words[l.system_mem / 8 + 1] = 0x21000;
    words[l.fastbins / 8 + 1] = f_chunk;
    for (1..128) |i| words[(l.bins + 16 * (i - 1)) / 8] = if (i == 1) u_chunk else a + l.bins + 16 * (i - 1) - 16;
    try rec.emit(.{ .kind = .mem_write, .tid = 1, .a = .{ a, l.arena_size, 0, 0, 0, 0 } }, std.mem.sliceAsBytes(&words));
    var tcache: [0x280 / 8]u64 = @splat(0);
    tcache[0] = 1 << 16;
    tcache[l.tcache_entries / 8 + 1] = t_chunk + 16;
    try rec.emit(.{ .kind = .mem_write, .tid = 1, .a = .{ tc, 0x280, 0, 0, 0, 0 } }, std.mem.sliceAsBytes(&tcache));
    // Chunk sizes, and the links: the tcache's and the fastbin's protected, the unsorted bin's not.
    for ([_]u64{ heap, t_chunk, f_chunk, u_chunk, used, top }, [_]u64{ 0x291, 0x31, 0x31, 0x101, 0x20, (0x21000 - (top - heap)) | 1 }) |c, s| try put(&rec, c + 8, s);
    try put(&rec, t_chunk + 16, (t_chunk + 16) >> 12);
    try put(&rec, t_chunk + 24, 0x1234);
    try put(&rec, f_chunk + 16, ((f_chunk + 16) >> 12) ^ 0x777770);
    try put(&rec, u_chunk + 16, a + l.bins - 16);
    try put(&rec, u_chunk + 24, a + l.bins - 16);
    try rec.emit(.{ .kind = .mark, .tid = 1 }, "");
    try rec.close();

    var trace = try store.Trace.open(gpa, std.testing.io, path);
    defer trace.close();
    var arena: std.heap.ArenaAllocator = .init(gpa);
    defer arena.deinit();
    const h = (try glibc(arena.allocator(), try trace.moment(1), 1)).?;
    try std.testing.expectEqual(top, h.arena.top);
    // The tcache's 0x30 bin: one entry, its next a protected null.
    const t = h.tcache.?;
    try std.testing.expectEqual(@as(u64, tc), t.addr);
    try std.testing.expectEqual(@as(usize, 1), t.bins.len);
    const tb = t.bins[0];
    try std.testing.expectEqual(@as(?u16, 1), tb.count);
    try std.testing.expectEqual(@as(u64, tc + 2), tb.count_at);
    try std.testing.expectEqual(@as(u64, tc + l.tcache_entries + 8), tb.head_at);
    try std.testing.expectEqual(End.end, tb.end);
    try std.testing.expectEqual(Entry{ .chunk = t_chunk, .link_at = t_chunk + 16, .stored = (t_chunk + 16) >> 12, .next = 0, .second = 0x1234 }, tb.entries[0]);
    // The fastbin's: its fd unprotected leads to memory never recorded.
    try std.testing.expectEqual(@as(usize, 1), h.fastbins.len);
    try std.testing.expectEqual(@as(u64, 0x777770), h.fastbins[0].entries[0].next);
    try std.testing.expectEqual(End.unknown, h.fastbins[0].end);
    try std.testing.expectEqual(@as(u64, 0x777770 + 16), h.fastbins[0].end_at);
    // The unsorted bin: one chunk, back to the bin.
    try std.testing.expectEqual(@as(usize, 1), h.bins.len);
    try std.testing.expectEqual(Where{ .bin = 1 }, h.bins[0].in);
    try std.testing.expectEqual(@as(u64, u_chunk), h.bins[0].entries[0].chunk);
    try std.testing.expectEqual(End.end, h.bins[0].end);
    // The chunks in order, each on its list.
    const want = [_]struct { u64, Where, bool }{ .{ heap, .none, false }, .{ t_chunk, .{ .tcache = 1 }, true }, .{ f_chunk, .{ .fastbin = 1 }, true }, .{ u_chunk, .{ .bin = 1 }, true }, .{ used, .none, false }, .{ top, .top, false } };
    try std.testing.expectEqual(want.len, h.chunks.len);
    for (want, h.chunks) |wc, c| {
        try std.testing.expectEqual(wc[0], c.addr);
        try std.testing.expectEqual(wc[1], c.in);
        try std.testing.expectEqual(wc[2], c.free);
    }
    try std.testing.expectEqual(End.end, h.cut);
    // Before the recording had the arena there is no heap to show.
    try std.testing.expect(try glibc(arena.allocator(), try trace.moment(0), 1) == null);
}