From 6a7a87b8ecb7902de5640cad00028997825024d2 Mon Sep 17 00:00:00 2001 From: Gabriel Schneider Date: Wed, 11 Nov 2026 10:38:30 -0300 Subject: trace: a function's basic blocks; ASan's shadow memory For a graph of a function's blocks: at the first stop inside it a function's blocks are recorded once (bounds, how each ends, where its edges go), with every instruction's text up to 2048 of them; telescope.blocks reads them, and a branch event ties a run to the edge it took. Stripped libraries get blocks from their unwind bounds. For a plain build seen against an ASan one: record --asan keeps the shadow of every region it records, the offset found from the process; telescope.shadow gives an address's shadow byte. The report functions are named and the chain holds from inside the report to main. Tested on an overflowing target built plain, with gcc's ASan and with clang's. Co-Authored-By: Claude Fable 5.2 Claude-Session: https://claude.ai/code/session_016Y9MNCKgYCGiANnEwcEQxa --- src/cli/record.zig | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) (limited to 'src/cli/record.zig') diff --git a/src/cli/record.zig b/src/cli/record.zig index 6743ec4..5d3726c 100644 --- a/src/cli/record.zig +++ b/src/cli/record.zig @@ -10,7 +10,7 @@ pub const usage = \\usage: ztrace record [-o FILE] [--by insn|line|syscall|break] [-b WHERE]... [-w WHAT[,LEN]]... \\ [--from WHERE] [--stack BYTES] [-s BYTES] [--telescope DEPTH] \\ [--around BYTES] [--disasm ROWS] [--steps N] - \\ [--max-size BYTES] [--strace] [--heap] + \\ [--max-size BYTES] [--strace] [--heap] [--asan] \\ [--ring] [--preload LIB] [--alloc] -- PROGRAM [ARGS] \\ \\Runs PROGRAM under ptrace into a recording (default out.ztrace). --by @@ -44,6 +44,10 @@ pub const usage = \\tcache, and the links of every chunk on a list; at most 4096 chunks and \\256 entries a list. glibc 2.40 and 2.44 only, linked dynamically or \\statically; the main arena's heap, the stopped thread's tcache. + \\--asan also records AddressSanitizer's shadow of all memory recorded (a + \\byte for 8: 00 addressable, f1 f2 f3 a stack frame's redzones, fa a + \\heap's, fd freed), where the runtime puts it; for a program built with + \\-fsanitize=address only. \\--steps ends the recording after N moments (N stops from the --from \\place, when there is one), and --max-size when the \\file is that large (default 1G). Sizes take a suffix: 64K, 4G. @@ -74,6 +78,7 @@ pub fn run(init: std.process.Init, args: []const [:0]const u8) !void { var max_bytes: ?u64 = null; var strace = false; var heap = false; + var asan = false; var from: ?[]const u8 = null; var breaks: std.ArrayList([]const u8) = .empty; var watches: std.ArrayList([]const u8) = .empty; @@ -94,6 +99,8 @@ pub fn run(init: std.process.Init, args: []const [:0]const u8) !void { strace = true; } else if (std.mem.eql(u8, arg, "--heap")) { heap = true; + } else if (std.mem.eql(u8, arg, "--asan")) { + asan = true; } else if (producer.take(args, &i)) { // Its own argument, when it has one, is taken. } else if (value == null) { @@ -157,6 +164,9 @@ pub fn run(init: std.process.Init, args: []const [:0]const u8) !void { d.syscalls = if (by == .syscall) .stop else .record; d.stack_watch = stack_bytes; if (heap) d.heap = .{}; + // Built with ASan, the program has its runtime or calls it (`__asan_init`, from a constructor). + if (asan and d.symbol("__asan_init") == null and !d.syms.imported.contains("__asan_init")) fail("--asan: {s} has no ASan runtime: build it with -fsanitize=address\n", .{args[i]}); + d.asan = asan; if (capture) |n| d.capture = n; for (breaks.items) |where| { if (std.mem.startsWith(u8, where, "usdt:")) { @@ -234,6 +244,7 @@ pub fn run(init: std.process.Init, args: []const [:0]const u8) !void { .no_layout => std.debug.print("heap: not recorded: no layout known for glibc {d}.{d} (known: 2.40, 2.44)\n", .{ h.version[0], h.version[1] }), .no_arena => std.debug.print("heap: not recorded: more than one place in glibc looks like the main arena\n", .{}), }; + if (asan) if (d.asan_shadow) |offset| std.debug.print("asan: shadow at {x}\n", .{offset}) else std.debug.print("asan: not recorded: the runtime never set its shadow\n", .{}); std.debug.print("{s}: {d} moments, {d} events, {d} bytes ({d} bytes/moment), {d:.1} ms, {d:.0} moments/s, exit {d}\n", .{ out, moments, events, size, -- cgit v1.3