summaryrefslogtreecommitdiff
path: root/src/trace
diff options
context:
space:
mode:
Diffstat (limited to 'src/trace')
-rw-r--r--src/trace/Debugger.zig5
-rw-r--r--src/trace/heap.zig676
-rw-r--r--src/trace/heap_test.zig183
-rw-r--r--src/trace/root.zig3
4 files changed, 866 insertions, 1 deletions
diff --git a/src/trace/Debugger.zig b/src/trace/Debugger.zig
index 5310ad3..f4f945b 100644
--- a/src/trace/Debugger.zig
+++ b/src/trace/Debugger.zig
@@ -66,6 +66,8 @@ allocations: std.ArrayList(Watch) = .empty,
/// The most read of them at a stop, in bytes: a stop's cost must not grow
/// with the target's heap.
allocation_watch: usize = 256 << 10,
+/// glibc's heap, recorded at every stop as `heap.zig` reads it (`--heap`): null for not at all.
+heap: ?@import("heap.zig").Watch = null,
breakpoints: std.ArrayList(Breakpoint) = .empty,
/// The current thread's registers at its last stop.
regs: Regs = .{},
@@ -490,6 +492,7 @@ fn report(d: *Debugger, stop: Stop) !Stop {
for (d.watches.items) |w| try d.snapshot(w.addr, w.len);
for (d.declared.items) |w| try d.snapshot(w.addr, w.len);
try d.snapshotAllocations();
+ if (d.heap) |*h| try h.atStop(d);
try d.emitCode();
try d.emitTelescope();
}
@@ -1136,7 +1139,7 @@ pub fn cfi(d: *Debugger, pc: u64) ?Symbols.Cfi {
/// The target's memory as a walk of the call chain reads it, every word it
/// reads recorded, and the frame of every function it goes through
/// (`emitFunction`): so a reader walks the same chain from the recording.
-const Recorded = struct {
+pub const Recorded = struct {
d: *Debugger,
failed: ?anyerror = null,
/// The stack as last read, a page at a time: a walk goes up it.
diff --git a/src/trace/heap.zig b/src/trace/heap.zig
new file mode 100644
index 0000000..8f0dc68
--- /dev/null
+++ b/src/trace/heap.zig
@@ -0,0 +1,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);
+}
diff --git a/src/trace/heap_test.zig b/src/trace/heap_test.zig
new file mode 100644
index 0000000..2cb5996
--- /dev/null
+++ b/src/trace/heap_test.zig
@@ -0,0 +1,183 @@
+//! The heap reader against `targets/heap.c`, under this machine's glibc
+//! (2.44) and under pwndbg's own glibc (2.40) where pwndbg is installed:
+//! recorded with the heap watched, stopped after each step, and read back.
+//! At every stop the reader's answer from the recording is the answer the
+//! same walk gives on the live process, and is what the step did.
+const std = @import("std");
+const linux = std.os.linux;
+const zt = @import("ztrace");
+const targets = @import("targets");
+const heap = zt.heap;
+
+const gpa = std.testing.allocator;
+const io = std.testing.io;
+const no_env = [_:null]?[*:0]const u8{null};
+/// pwndbg's glibc, run through its own loader.
+const glibc_240 = "/usr/local/lib/pwndbg-gdb/lib";
+
+/// The heap at each of the five stops: read from the live process, and
+/// the moment of the recording it is.
+const Stop = struct { moment: u64, live: []const u8 };
+
+fn text(arena: std.mem.Allocator, h: ?heap.Heap) ![]const u8 {
+ var out: std.Io.Writer.Allocating = .init(arena);
+ if (h) |found| try heap.print(found, &out.writer);
+ return out.written();
+}
+
+/// Records the target with the heap watched, stopping at `heap_step`; argv
+/// runs it, through a loader when it is not the target.
+fn record(arena: std.mem.Allocator, path: []const u8, argv: []const []const u8) ![5]Stop {
+ var stops: [5]Stop = undefined;
+ var rec = try zt.Recorder.create(gpa, io, path);
+ errdefer rec.close() catch {};
+ var args: [8:null]?[*:0]const u8 = @splat(null);
+ for (argv, 0..) |a, i| args[i] = try arena.dupeSentinel(u8, a, 0);
+ // Its output goes nowhere: this process's own is the test runner's channel to the build.
+ const saved: linux.fd_t = @intCast(linux.dup(1));
+ const nowhere: linux.fd_t = @intCast(linux.open("/dev/null", .{ .ACCMODE = .WRONLY }, 0));
+ _ = linux.dup2(nowhere, 1);
+ const spawned = zt.Debugger.spawn(gpa, io, args[0].?, &args, &no_env, rec.sink());
+ _ = linux.dup2(saved, 1);
+ _ = linux.close(saved);
+ _ = linux.close(nowhere);
+ var dbg = try spawned;
+ defer dbg.deinit();
+ // The mappings are followed through their syscalls; a target run by its
+ // loader is mapped by it, its code where it runs once executable.
+ dbg.syscalls = .stop;
+ while (true) {
+ if (dbg.symbol("heap_step")) |s| if (dbg.syms.mappingAt(s.addr)) |m| if (m.prot & 4 != 0) break;
+ if (try dbg.cont() == .exited) return error.NoHeapStep;
+ }
+ dbg.syscalls = .record;
+ _ = try dbg.breakAt(try dbg.resolve("heap_step"), null);
+ dbg.heap = .{};
+ var n: usize = 0;
+ while (true) switch (try dbg.cont()) {
+ .breakpoint => {
+ const w = dbg.heap.?;
+ const slot = if (w.tcache) |off| dbg.regs.fs_base +% @as(u64, @bitCast(off)) else null;
+ stops[n] = .{ .moment = rec.moments - 1, .live = try text(arena, try heap.walk(arena, &dbg, w.glibc.?, slot)) };
+ n += 1;
+ },
+ .exited => |code| {
+ try std.testing.expectEqual(@as(u8, 0), code);
+ break;
+ },
+ else => {},
+ };
+ try std.testing.expectEqual(@as(usize, 5), n);
+ try rec.close();
+ return stops;
+}
+
+fn check(argv: []const []const u8) !void {
+ var arena_state: std.heap.ArenaAllocator = .init(gpa);
+ defer arena_state.deinit();
+ const arena = arena_state.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(io, &dir_buf)];
+ const path = try std.fs.path.join(arena, &.{ dir, "heap.ztrace" });
+ const stops = try record(arena, path, argv);
+
+ var trace = try zt.Trace.open(gpa, io, path);
+ defer trace.close();
+ var heaps: [5]heap.Heap = undefined;
+ for (stops, &heaps) |stop, *h| {
+ const m = try trace.moment(stop.moment);
+ const tids = try m.threads(arena);
+ h.* = (try heap.glibc(arena, m, tids[0])).?;
+ // The recording holds what the walk reads: it reads back the same.
+ try std.testing.expectEqualStrings(stop.live, try text(arena, h.*));
+ // The chunks run from the heap's start to the top, none skipped.
+ try std.testing.expectEqual(heap.End.end, h.cut);
+ for (h.chunks[0 .. h.chunks.len - 1], h.chunks[1..]) |c, after| try std.testing.expectEqual(after.addr, c.addr + c.size());
+ try std.testing.expectEqual(heap.Where.top, h.chunks[h.chunks.len - 1].in);
+ try std.testing.expectEqual(h.arena.top, h.chunks[h.chunks.len - 1].addr);
+ }
+ const l = heaps[0].layout;
+ // 7 a tcache bin before glibc 2.42, 16 since.
+ const fill: usize = if (l.tcache_slots) 16 else 7;
+
+ // 1: seventeen chunks of 0x30 in use, a 0x510, a 0x20; no list holds anything.
+ var smalls: usize = 0;
+ for (heaps[0].chunks) |c| {
+ try std.testing.expect(!c.free);
+ smalls += @intFromBool(c.size() == 0x30);
+ }
+ try std.testing.expectEqual(@as(usize, 17), smalls);
+ try std.testing.expect(heaps[0].fastbins.len == 0 and heaps[0].bins.len == 0);
+ // glibc 2.44 makes the tcache at the first free.
+ if (heaps[0].tcache) |t| try std.testing.expectEqual(@as(usize, 0), t.bins.len) else try std.testing.expect(l.tcache_slots);
+
+ // 2: the tcache's 0x30 bin full, the last freed first, each link
+ // protected by its own address; the rest in fastbin 0x30 where there are
+ // fastbins, else in a bin.
+ const t = heaps[1].tcache.?;
+ try std.testing.expectEqual(@as(usize, 1), t.bins.len);
+ const bin = t.bins[0];
+ try std.testing.expectEqual(heap.Where{ .tcache = 1 }, bin.in);
+ try std.testing.expectEqual(fill, bin.entries.len);
+ try std.testing.expectEqual(@as(?u16, if (l.tcache_slots) 0 else 7), bin.count);
+ try std.testing.expectEqual(t.addr + 2, bin.count_at);
+ try std.testing.expectEqual(t.addr + l.tcache_entries + 8, bin.head_at);
+ try std.testing.expectEqual(bin.entries[0].chunk + 16, bin.head);
+ for (bin.entries, 0..) |e, i| {
+ try std.testing.expectEqual(e.next, e.stored ^ e.link_at >> 12);
+ try std.testing.expect(e.stored != e.next);
+ try std.testing.expectEqual(bin.entries[0].second, e.second);
+ const next = if (i + 1 < bin.entries.len) bin.entries[i + 1].chunk + 16 else 0;
+ try std.testing.expectEqual(next, e.next);
+ if (i > 0) try std.testing.expectEqual(bin.entries[i - 1].chunk - 0x30, e.chunk);
+ }
+ if (l.fastbin_count > 0) {
+ try std.testing.expectEqual(@as(usize, 1), heaps[1].fastbins.len);
+ const fast = heaps[1].fastbins[0];
+ try std.testing.expectEqual(heap.Where{ .fastbin = 1 }, fast.in);
+ try std.testing.expectEqual(@as(usize, 17 - 7), fast.entries.len);
+ try std.testing.expectEqual(heap.End.end, fast.end);
+ for (fast.entries[0 .. fast.entries.len - 1], fast.entries[1..]) |e, next| {
+ try std.testing.expectEqual(next.chunk, e.next);
+ try std.testing.expectEqual(next.chunk + 0x30, e.chunk);
+ }
+ } else {
+ try std.testing.expectEqual(@as(usize, 1), heaps[1].bins.len);
+ try std.testing.expectEqual(@as(usize, 1), heaps[1].bins[0].entries.len);
+ }
+ var free_smalls: usize = 0;
+ for (heaps[1].chunks) |c| free_smalls += @intFromBool(c.size() == 0x30 and c.free);
+ try std.testing.expectEqual(@as(usize, 17), free_smalls);
+
+ // 3: the 0x510 freed beside the guard in use: in the unsorted bin (2.44
+ // merges it with the 0x30 before it, free in a bin), linked to the bin
+ // both ways.
+ const unsorted = heaps[2].bins[0];
+ try std.testing.expectEqual(heap.Where{ .bin = 1 }, unsorted.in);
+ try std.testing.expectEqual(@as(usize, 1), unsorted.entries.len);
+ const head = unsorted.head_at - 16;
+ try std.testing.expectEqual(heap.Entry{ .chunk = unsorted.head, .link_at = unsorted.head + 16, .stored = head, .next = head, .second = head }, unsorted.entries[0]);
+ for (heaps[2].chunks) |c| if (c.addr == unsorted.head) try std.testing.expectEqual(@as(u64, if (l.fastbin_count > 0) 0x510 else 0x540), c.size());
+
+ // 4: a larger request sorts it into a large bin and is served from the top.
+ try std.testing.expectEqual(@as(usize, 1), heaps[3].bins.len);
+ try std.testing.expect(heaps[3].bins[0].in.bin >= 64);
+ const larger = heaps[3].chunks[heaps[3].chunks.len - 2];
+ try std.testing.expectEqual(@as(u64, 0x1010), larger.size());
+ try std.testing.expect(!larger.free);
+
+ // 5: freed, it is the top again.
+ try std.testing.expectEqual(larger.addr, heaps[4].arena.top);
+ try std.testing.expectEqual(heaps[0].arena.system_mem, heaps[4].arena.system_mem);
+}
+
+test "glibc's heap from a recording, at each step, as the live process has it: this machine's glibc" {
+ try check(&.{targets.heap});
+}
+
+test "... and glibc 2.40, pwndbg's own, which has fastbins" {
+ std.Io.Dir.accessAbsolute(io, glibc_240 ++ "/libc.so.6", .{}) catch return error.SkipZigTest;
+ try check(&.{ glibc_240 ++ "/ld-linux-x86-64.so.2", "--library-path", glibc_240, targets.heap });
+}
diff --git a/src/trace/root.zig b/src/trace/root.zig
index 88e2935..ef684db 100644
--- a/src/trace/root.zig
+++ b/src/trace/root.zig
@@ -11,6 +11,8 @@ pub const Trace = store.Trace;
pub const ptrace = @import("ptrace.zig");
/// Pointers followed through a moment, pwndbg's way.
pub const telescope = @import("telescope.zig");
+/// glibc's malloc at a moment: chunks, tcache, bins, the top.
+pub const heap = @import("heap.zig");
pub const Debugger = @import("Debugger.zig");
pub const Symbols = @import("Symbols.zig");
pub const syscalls = @import("syscalls.zig");
@@ -31,6 +33,7 @@ test {
_ = store;
_ = ptrace;
_ = telescope;
+ _ = heap;
_ = Debugger;
_ = Symbols;
_ = syscalls;