diff options
Diffstat (limited to 'src/trace/heap_test.zig')
| -rw-r--r-- | src/trace/heap_test.zig | 183 |
1 files changed, 183 insertions, 0 deletions
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 }); +} |
