summaryrefslogtreecommitdiff
path: root/src/trace/heap_test.zig
blob: f7919e21be8d56c86c68beca8c4893eb9a8748af (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
//! 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);
    // Where the symbol main_arena is known (the C library's own, or its separate debug info's), the arena is it.
    if (dbg.syms.find("main_arena")) |sym| try std.testing.expectEqual(sym.addr, dbg.heap.?.glibc.?.arena);
    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: until then the thread's pointer is on its dummy.
    if (heaps[0].tcache) |t| try std.testing.expectEqual(@as(usize, 0), t.bins.len) else try std.testing.expect(l.tcache_slots and heaps[0].tcache_dummy);
    try std.testing.expect(!heaps[1].tcache_dummy);

    // 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);
    // Where a count is the slots left, an empty bin's is the capacity.
    try std.testing.expectEqual(if (l.tcache_slots) @as(?u16, @intCast(fill)) else null, t.capacity);
    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 });
}