diff options
Diffstat (limited to 'src/trace/debugger_test.zig')
| -rw-r--r-- | src/trace/debugger_test.zig | 200 |
1 files changed, 200 insertions, 0 deletions
diff --git a/src/trace/debugger_test.zig b/src/trace/debugger_test.zig index e20f864..15ecc71 100644 --- a/src/trace/debugger_test.zig +++ b/src/trace/debugger_test.zig @@ -144,6 +144,185 @@ test "a recording by `ztrace record` has the line of each instruction round the try std.testing.expect(caller.line != null); } +test "PLT stubs and GOT slots are named for the function they lead to, in the program and in libc" { + var d = try spawn(targets.hello, null, null); + defer d.deinit(); + try d.toEntry(); + // Not in the program: the mappings are read again, libc's with them. + try std.testing.expect(d.symbol("open") != null); + var buf: [256]u8 = undefined; + var w: std.Io.Writer = .fixed(&buf); + for ([_][]const u8{ "open@plt", "read@plt", "close@plt", "open@got", "read@got" }) |name| { + w.end = 0; + try std.testing.expect(try d.syms.label(&w, d.symbol(name).?.addr)); + try std.testing.expectEqualStrings(name, w.buffered()[0 .. w.buffered().len - 2]); + } + // The function a caller calls, of the four names libc gives open. + w.end = 0; + _ = try d.syms.label(&w, d.symbol("__open64").?.addr); + try std.testing.expectEqualStrings("libc.so.6!open+0", w.buffered()); + // libc's own stubs are its IFUNCs', whose relocations name nothing: by the function each resolver is. + const libc = for (d.syms.modules.items) |*m| { + if (std.mem.endsWith(u8, m.path, "/libc.so.6")) break m; + } else return error.NoLibc; + var stubs: usize = 0; + for (libc.symbols) |sym| if (std.mem.endsWith(u8, sym.name, "@plt")) { + stubs += 1; + try std.testing.expect(d.symbol(sym.name[0 .. sym.name.len - "@plt".len]) != null); + w.end = 0; + _ = try d.syms.label(&w, sym.addr + libc.bias); + try std.testing.expect(std.mem.startsWith(u8, w.buffered(), "libc.so.6!") and std.mem.endsWith(u8, w.buffered(), "@plt+0")); + }; + try std.testing.expect(stubs > 10); +} + +test "a call followed down by instruction: through the PLT, the loader's resolver and libc to the kernel, and back" { + var trace = try zt.Trace.open(gpa, io, targets.hello_recording); + defer trace.close(); + const tids = try (try trace.moment(0)).threads(gpa); + defer gpa.free(tids); + const named_ = struct { + fn at(m: zt.store.Moment, addr: u64) []const u8 { + return m.trace.strings.get(addr) orelse ""; + } + }.at; + const inMain = struct { + fn at(m: zt.store.Moment, addr: u64) bool { + return std.mem.startsWith(u8, m.trace.strings.get(addr) orelse "", "main+"); + } + }.at; + // From main's first instruction to its last: the calls it makes, open, read twice and close. + var first: ?u64 = null; + var last: u64 = 0; + for (0..trace.len) |i| { + const regs = (try (try trace.moment(i)).regs(tids[0])) orelse continue; + if (!inMain(try trace.moment(i), regs.rip)) continue; + if (first == null) first = i; + last = i; + } + var stubs: [4][]const u8 = undefined; + var n_stubs: usize = 0; + var entries: [4][]const u8 = undefined; + var n_entries: usize = 0; + var crossings: [4]u16 = undefined; + var n_crossings: usize = 0; + var returns: usize = 0; + var resolver: usize = 0; + var back_to: u64 = 0; + var was_in_main = true; + var text: [256]u8 = undefined; + for (first.?..last + 1) |i| { + const m = try trace.moment(i); + const regs = (try m.regs(tids[0])).?; + const here = named_(m, regs.rip); + var chain_buf: [32]u64 = undefined; + const chain = try zt.telescope.backtrace(m, ®s, &chain_buf); + const in_main = inMain(m, regs.rip); + if (in_main) { + // After the ret: the instruction after the call. + if (!was_in_main) { + try std.testing.expectEqual(back_to, regs.rip); + returns += 1; + } + } else { + // main waits under every frame of the call, the resolver's too. + for (chain[1..]) |pc| { + if (inMain(m, pc)) break; + } else { + std.debug.print("moment {d} at {s}: main lost\n", .{ i, here }); + return error.MainLost; + } + if (std.mem.startsWith(u8, here, "ld-linux")) resolver += 1; + } + was_in_main = in_main; + // The stub's jmp, named for the function it leads to, its GOT slot + // too; lazy at a function's first call, bound at the second read's. + if (std.mem.endsWith(u8, here, "@plt+0")) { + stubs[n_stubs] = here; + n_stubs += 1; + try std.testing.expect(inMain(m, chain[1])); + const stub = zt.telescope.function(m, regs.rip).?; + try std.testing.expectEqual(regs.rip, stub.start); + try std.testing.expectEqual(@as(u64, 16), stub.len); + var events = try m.events(gpa); + defer events.deinit(gpa); + const slot = while (events.next()) |item| { + if (item.ev.kind == .touch and item.ev.a[2] == regs.rip) break item.ev.a[0]; + } else return error.NoTouch; + const function_name = here[0 .. here.len - "@plt+0".len]; + try std.testing.expectEqualStrings(try std.fmt.bufPrint(&text, "{s}@got+0", .{function_name}), named_(m, slot)); + const held = (try zt.telescope.got(m, slot)).?; + try std.testing.expectEqual(@as(?bool, n_stubs != 3), held.lazy); + } + // The callee's first instruction, by the name its caller used. + if (std.mem.startsWith(u8, here, "libc.so.6!") and std.mem.endsWith(u8, here, "+0")) { + entries[n_entries] = here; + n_entries += 1; + try std.testing.expect(inMain(m, chain[1])); + back_to = chain[1]; + // The caller's three rows: the one before the call, the call, the one it returns to. + const after = (try zt.telescope.instruction(m, back_to, &text)).?; + const call_at = back_to - after.back; + const call = (try zt.telescope.instruction(m, call_at, &text)).?; + try std.testing.expect(std.mem.startsWith(u8, call.text, "call")); + try std.testing.expect(call.back != 0 and try zt.telescope.instruction(m, call_at - call.back, &text) != null); + // main's bounds, from the recording: where its name says it begins. + const f = zt.telescope.function(m, back_to - 1).?; + try std.testing.expectEqual(back_to - try std.fmt.parseInt(u64, named_(m, back_to)["main+".len..], 10), f.start); + try std.testing.expect(back_to < f.start + f.len); + } + // At a syscall instruction: the next moment is the call, entry and + // exit, with the registers after it. + if (i + 1 < trace.len) { + const next = try trace.moment(i + 1); + var events = try next.events(gpa); + defer events.deinit(gpa); + var enter: ?zt.store.Events.Item = null; + var exit: ?zt.store.Events.Item = null; + while (events.next()) |item| switch (item.ev.kind) { + .syscall_enter => enter = item, + .syscall_exit => exit = item, + else => {}, + }; + if (enter) |e| { + const nr = e.ev.aux; + crossings[n_crossings] = nr; + n_crossings += 1; + try std.testing.expectEqual(nr, exit.?.ev.aux); + try std.testing.expectEqual(@as(u64, nr), regs.rax); + try std.testing.expect(!inMain(m, regs.rip)); + const after = (try next.regs(tids[0])).?; + // The instruction's own clobbers: rcx the return address, r11 the flags (with the step's trap flag). + try std.testing.expectEqual(regs.rip + 2, after.rcx); + try std.testing.expectEqual(regs.rip + 2, after.rip); + try std.testing.expectEqual(regs.eflags | 0x100, after.r11 | 0x100); + try std.testing.expectEqual(exit.?.ev.a[0], after.rax); + var out: std.Io.Writer = .fixed(&text); + try zt.syscalls.formatEnter(&out, &e.ev, e.payload); + try zt.syscalls.formatExit(&out, &exit.?.ev); + const call = out.buffered(); + if (std.mem.eql(u8, zt.syscalls.name(nr).?, "openat")) { + try std.testing.expect(std.mem.startsWith(u8, call, "openat(-100, \"/")); + try std.testing.expect(std.mem.endsWith(u8, call, "/hello\", 0x0, 0x0) = 3")); + } + if (std.mem.eql(u8, zt.syscalls.name(nr).?, "read")) try std.testing.expect(std.mem.startsWith(u8, call, "read(3, 0x") and std.mem.endsWith(u8, call, ", 16) = 16")); + } + } + } + const want_stubs = [_][]const u8{ "open@plt+0", "read@plt+0", "read@plt+0", "close@plt+0" }; + const want_entries = [_][]const u8{ "libc.so.6!open+0", "libc.so.6!read+0", "libc.so.6!read+0", "libc.so.6!close+0" }; + try std.testing.expectEqual(want_stubs.len, n_stubs); + for (want_stubs, stubs, want_entries, entries) |ws, s, we, e| { + try std.testing.expectEqualStrings(ws, s); + try std.testing.expectEqualStrings(we, e); + } + try std.testing.expectEqual(@as(usize, 4), n_crossings); + for ([_][]const u8{ "openat", "read", "read", "close" }, crossings) |want, nr| try std.testing.expectEqualStrings(want, zt.syscalls.name(nr).?); + try std.testing.expectEqual(@as(usize, 4), returns); + // Three first calls ran the resolver, hundreds of instructions each. + try std.testing.expect(resolver > 300); +} + test "step walks the source lines, in and out of calls, as gdb does" { var d = try spawn(targets.list, "2", null); defer d.deinit(); @@ -319,6 +498,27 @@ test "a recording tells each stack word's role: saved registers, return addresse try std.testing.expectEqual(@as(u64, 2), std.mem.readInt(u64, &argc, .little)); } +test "by instruction, a call into the vDSO is run through to its return: its retry loop is no stops, whatever a stop costs" { + var counts: [2]usize = undefined; + for ([_]usize{ 1024, 0 }, &counts) |stack, *count| { + var log: Log = .{}; + defer log.deinit(); + var d = try spawn(targets.clock, null, log.sink()); + defer d.deinit(); + d.telescope = 3; + d.stack_watch = stack; + _ = try d.breakAt(try d.resolve("main"), null); + _ = try d.cont(); + count.* = 0; + while (try d.stepi() != .exited) : (count.* += 1) { + const in = d.syms.mappingAt(d.regs.rip) orelse continue; + try std.testing.expect(!std.mem.eql(u8, in.name, "[vdso]")); + } + } + // The same stops however long a stop takes. + try std.testing.expectEqual(counts[0], counts[1]); +} + test "the telescope records the region round what a pointer leads to" { var log: Log = .{}; defer log.deinit(); |
