//! Watched memory: regions whose changes become `mem_write` events. Each //! region has a shadow copy in the agent's own memory; `diff` compares the //! two and emits the runs that changed. Plain memory compares in the target's //! own address space: no syscall, no page protection, any granularity. The //! agent calls `diff` before every event that ends a moment, so a moment's //! memory is what the target had when that event happened. const std = @import("std"); const linux = std.os.linux; const agent = @import("agent.zig"); const Region = struct { addr: usize, len: usize, shadow: [*]u8, /// The thread that asked, and how many hooked calls it had open: the /// region is forgotten when that call returns, as the recorder forgets /// its `variable`. Depth 0 lives forever. tid: u32, depth: u32, }; // shortcut: 64 regions, each compared whole at every moment; when a target // watches megabytes, find the dirty pages first (PAGEMAP_SCAN) and compare those. var table: [64]Region = undefined; var count: usize = 0; var busy: std.atomic.Value(bool) = .init(false); /// The memory must stay mapped for as long as it is watched. pub fn add(memory: []const u8, tid: u32, depth: u32) void { if (memory.len == 0) return; const shadow = linux.mmap(null, memory.len, .{ .READ = true, .WRITE = true }, .{ .TYPE = .PRIVATE, .ANONYMOUS = true }, -1, 0); if (linux.errno(shadow) != .SUCCESS) return; while (busy.swap(true, .acquire)) std.atomic.spinLoopHint(); defer busy.store(false, .release); if (count == table.len) { _ = linux.munmap(@ptrFromInt(shadow), memory.len); return; } table[count] = .{ .addr = @intFromPtr(memory.ptr), .len = memory.len, .shadow = @ptrFromInt(shadow), .tid = tid, .depth = depth }; @memcpy(table[count].shadow, memory); count += 1; } /// Forgets what `tid` watched from inside calls deeper than `depth`. pub fn drop(tid: u32, depth: u32) void { if (count == 0) return; while (busy.swap(true, .acquire)) std.atomic.spinLoopHint(); defer busy.store(false, .release); var i: usize = 0; while (i < count) { const r = table[i]; if (r.tid != tid or r.depth <= depth) { i += 1; continue; } _ = linux.munmap(r.shadow, r.len); count -= 1; table[i] = table[count]; } } /// Emits what changed in every region since the last time. pub fn diff() void { if (count == 0) return; // Busy: another thread is at it, or this one is, below this frame. if (busy.swap(true, .acquire)) return; defer busy.store(false, .release); for (table[0..count]) |r| { var changes: Changes = .{ .old = r.shadow[0..r.len], .new = @as([*]const u8, @ptrFromInt(r.addr))[0..r.len] }; while (changes.next()) |run| agent.write(r.addr + run.at, run.bytes); } } /// The runs of bytes where `new` differs from `old`, each brought into `old` /// as it is returned. Changes less than 8 bytes apart travel as one run. pub const Changes = struct { old: []u8, new: []const u8, at: usize = 0, pub const Run = struct { at: usize, bytes: []const u8 }; pub fn next(c: *Changes) ?Run { const from = c.at + same(c.old[c.at..], c.new[c.at..]); if (from == c.new.len) { c.at = from; return null; } var to = from + 1; var i = to; while (i < c.new.len and i - to < 8) : (i += 1) { if (c.old[i] != c.new[i]) to = i + 1; } @memcpy(c.old[from..to], c.new[from..to]); c.at = to; // From the copy: the target may be writing there right now. return .{ .at = from, .bytes = c.old[from..to] }; } /// How many bytes at the start of `a` and `b` are equal. fn same(a: []const u8, b: []const u8) usize { const V = @Vector(32, u8); var i: usize = 0; while (i + 32 <= a.len) : (i += 32) { const differ: u32 = @bitCast(@as(V, a[i..][0..32].*) != @as(V, b[i..][0..32].*)); if (differ != 0) return i + @ctz(differ); } while (i < a.len and a[i] == b[i]) i += 1; return i; } }; test "changes are found as runs and folded into the old copy" { var old: [200]u8 = @splat(0); var new: [200]u8 = @splat(0); new[3] = 1; new[9] = 2; // 5 equal bytes after the first: one run new[100] = 3; new[199] = 4; var changes: Changes = .{ .old = &old, .new = &new }; const a = changes.next().?; try std.testing.expectEqual(@as(usize, 3), a.at); try std.testing.expectEqualSlices(u8, &.{ 1, 0, 0, 0, 0, 0, 2 }, a.bytes); try std.testing.expectEqual(@as(usize, 100), changes.next().?.at); try std.testing.expectEqual(@as(usize, 199), changes.next().?.at); try std.testing.expect(changes.next() == null); try std.testing.expectEqualSlices(u8, &new, &old); changes.at = 0; try std.testing.expect(changes.next() == null); }