summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorGabriel Schneider <[email protected]>2026-10-28 21:54:47 -0300
committerGabriel Schneider <[email protected]>2026-10-28 21:59:19 -0300
commit2e237ef20b6a5d9f680d80a560d144c0f67dff91 (patch)
tree3be4ee40d4611e88761843473c3296be1853ede2
parent30b7fd64b4b819abafd293f4ef45bf8ece3082e2 (diff)
downloadpardes-2e237ef20b6a5d9f680d80a560d144c0f67dff91.tar.gz
pardes-2e237ef20b6a5d9f680d80a560d144c0f67dff91.zip
kitty fuzz: seeded random kitty traffic through a terminal pane (test/kitty.zig), in kitty-test and unit-test
Commands with any action, keys and values (huge, negative, zero, junk), payloads valid and broken (base64, a cut PNG), CAN, SUB and ESC mid-command, half of them loads and placements with random geometry so they reach the storage and the frame; placeholders with every diacritic, 24-bit and 256-colour ids and underline-colour placements; cursor moves, scroll regions, insert/delete lines, ED 2/3, the alternate screen; window and cell-size resizes and grid resizes; frames between; every batch split into reads anywhere. Each seed checks no crash, no leak (the testing allocator) and that every answer parses as a whole ESC _ G ... ESC \ or CSI. A bad answer shrinks to its shortest failing prefix; PARDES_KITTY_FUZZ=<seeds>, PARDES_KITTY_FUZZ_SEED and PARDES_KITTY_FUZZ_STEPS for long runs and replays. Found nothing: 900 seeds of 400 steps pass, with frames drawing up to 42 places. Co-Authored-By: Claude Opus 5.5 <[email protected]>
-rw-r--r--test/kitty.zig214
1 files changed, 214 insertions, 0 deletions
diff --git a/test/kitty.zig b/test/kitty.zig
index e760e8d6..a100e7a6 100644
--- a/test/kitty.zig
+++ b/test/kitty.zig
@@ -920,3 +920,217 @@ test "kitty: a file bigger than a screen may hold is refused, not read whole" {
try std.testing.expect(std.mem.startsWith(u8, got, "\x1b_Gi=94;E"));
try std.testing.expect(!r.hasImage(0, 94));
}
+
+// ---- Fuzz ----
+//
+// Seeded random kitty traffic, the way a confused or hostile program sends
+// it: commands with any keys and values, payloads valid and broken, CAN and
+// SUB mid-command, placeholders with any diacritics, cursor moves, ED 2/3,
+// the alternate screen, window and cell-size resizes and frames between,
+// split into reads anywhere. Each seed checks that pardes neither crashes
+// nor leaks (the testing allocator) and that every answer parses.
+//
+// PARDES_KITTY_FUZZ=<seeds> more seeds (default 24)
+// PARDES_KITTY_FUZZ_SEED=<seed> one seed, printing each step
+// PARDES_KITTY_FUZZ_STEPS=<n> steps per seed (default 400)
+//
+// A crash prints `seed S step N`: rerun that seed with fewer steps to
+// shrink it. A bad answer shrinks itself to the shortest failing prefix.
+
+fn envNumber(name: [*:0]const u8, default: u64) u64 {
+ const v = std.c.getenv(name) orelse return default;
+ return std.fmt.parseInt(u64, std.mem.span(v), 10) catch default;
+}
+
+const fuzz_keys = "aiIpfsvxywhcrXYzCUPQHVmoqtSOd";
+const fuzz_actions = "tTpdqfac";
+const fuzz_deletes = "aAiInNcCpPqQxXyYzZrRfF";
+
+fn fuzzValue(rng: std.Random, out: *std.ArrayList(u8), arena: std.mem.Allocator) !void {
+ switch (rng.uintLessThan(u8, 10)) {
+ 0 => try out.appendSlice(arena, "0"),
+ 1 => try out.print(arena, "{d}", .{rng.int(u32)}),
+ 2 => try out.print(arena, "-{d}", .{rng.uintLessThan(u32, 1 << 31)}),
+ 3 => try out.appendSlice(arena, "4294967295"),
+ 4 => try out.appendSlice(arena, "99999999999999999999"),
+ 5 => try out.appendSlice(arena, "x"),
+ else => try out.print(arena, "{d}", .{rng.uintLessThan(u32, 12)}),
+ }
+}
+
+/// A command that loads, or places what is loaded, with any geometry: it
+/// gets past the parser to the storage and the drawing.
+fn fuzzPlacement(rng: std.Random, arena: std.mem.Allocator) ![]const u8 {
+ var out: std.ArrayList(u8) = .empty;
+ const id = 1 + rng.uintLessThan(u32, 4);
+ switch (rng.uintLessThan(u8, 3)) {
+ 0 => try out.print(arena, "\x1b_Ga=T,f=24,s=4,v=2,i={d},q=2", .{id}),
+ 1 => try out.print(arena, "\x1b_Ga=p,i={d},q=2", .{id}),
+ else => try out.print(arena, "\x1b_Ga=T,f=100,i={d},q=2", .{id}),
+ }
+ const keys = "crxywhXYzCUpPQHV";
+ for (0..rng.uintLessThan(usize, 6)) |_| {
+ try out.print(arena, ",{c}=", .{keys[rng.uintLessThan(usize, keys.len)]});
+ try fuzzValue(rng, &out, arena);
+ }
+ try out.append(arena, ';');
+ if (std.mem.startsWith(u8, out.items, "\x1b_Ga=T,f=24")) try out.appendSlice(arena, rgb_b64);
+ if (std.mem.startsWith(u8, out.items, "\x1b_Ga=T,f=100")) try out.appendSlice(arena, png_b64);
+ try out.appendSlice(arena, "\x1b\\");
+ return out.items;
+}
+
+fn fuzzCommand(rng: std.Random, arena: std.mem.Allocator) ![]const u8 {
+ if (rng.boolean()) return fuzzPlacement(rng, arena);
+ var out: std.ArrayList(u8) = .empty;
+ try out.appendSlice(arena, "\x1b_G");
+ const action = fuzz_actions[rng.uintLessThan(usize, fuzz_actions.len)];
+ try out.print(arena, "a={c}", .{action});
+ if (action == 'd') try out.print(arena, ",d={c}", .{fuzz_deletes[rng.uintLessThan(usize, fuzz_deletes.len)]});
+ for (0..rng.uintLessThan(usize, 7)) |_| {
+ const key = fuzz_keys[rng.uintLessThan(usize, fuzz_keys.len)];
+ try out.print(arena, ",{c}=", .{key});
+ switch (key) {
+ 'f' => try out.appendSlice(arena, ([_][]const u8{ "24", "32", "100", "7" })[rng.uintLessThan(usize, 4)]),
+ 't' => try out.appendSlice(arena, ([_][]const u8{ "d", "f", "t", "s", "q" })[rng.uintLessThan(usize, 5)]),
+ 'o' => try out.appendSlice(arena, if (rng.boolean()) "z" else "y"),
+ else => try fuzzValue(rng, &out, arena),
+ }
+ }
+ // Mostly the small ids the rest reuse, so commands meet each other.
+ if (rng.uintLessThan(u8, 4) != 0) try out.print(arena, ",i={d}", .{1 + rng.uintLessThan(u32, 4)});
+ if (rng.boolean()) try out.print(arena, ",s={d},v={d}", .{ rng.uintLessThan(u32, 6), rng.uintLessThan(u32, 6) });
+ try out.append(arena, ';');
+ switch (rng.uintLessThan(u8, 8)) {
+ 0 => try out.appendSlice(arena, rgb_b64),
+ 1 => try out.appendSlice(arena, rgba_b64),
+ 2 => try out.appendSlice(arena, png_b64),
+ 3 => try out.appendSlice(arena, rgb_zlib_b64),
+ 4 => {}, // nothing
+ 5 => {
+ // base64, broken: random characters, wrong padding.
+ for (0..rng.uintLessThan(usize, 40)) |_| try out.append(arena, "AB/+=x!Z09"[rng.uintLessThan(usize, 10)]);
+ },
+ 6 => try out.appendSlice(arena, png_b64[0..rng.uintLessThan(usize, png_b64.len)]),
+ else => try out.appendSlice(arena, "AAAA"),
+ }
+ switch (rng.uintLessThan(u8, 12)) {
+ 0 => try out.append(arena, 0x18), // CAN
+ 1 => try out.append(arena, 0x1a), // SUB
+ 2 => try out.appendSlice(arena, "\x1b[m"), // ESC aborting it
+ else => try out.appendSlice(arena, "\x1b\\"),
+ }
+ return out.items;
+}
+
+fn fuzzOutput(rng: std.Random, arena: std.mem.Allocator) ![]const u8 {
+ return switch (rng.uintLessThan(u8, 9)) {
+ 0 => try std.fmt.allocPrint(arena, "\x1b[{d};{d}H", .{ 1 + rng.uintLessThan(u32, 50), 1 + rng.uintLessThan(u32, 150) }),
+ 1 => "line\r\n",
+ 2 => "\x1b[H\x1b[2J\x1b[3J",
+ 3 => if (rng.boolean()) "\x1b[?1049h" else "\x1b[?1049l",
+ 4 => blk: {
+ // Placeholders: an image id in the colour (24-bit or 256), any
+ // diacritics, up to three per cell.
+ var out: std.ArrayList(u8) = .empty;
+ if (rng.boolean())
+ try out.print(arena, "\x1b[38;2;{d};{d};{d}m", .{ rng.int(u8), rng.int(u8), rng.uintLessThan(u8, 5) })
+ else
+ try out.print(arena, "\x1b[38;5;{d}m", .{rng.uintLessThan(u8, 5)});
+ if (rng.boolean()) try out.print(arena, "\x1b[58;5;{d}m", .{rng.uintLessThan(u8, 4)});
+ for (0..1 + rng.uintLessThan(usize, 6)) |_| {
+ try out.appendSlice(arena, "\u{10EEEE}");
+ const marks = [_][]const u8{ "\u{0305}", "\u{030D}", "\u{030E}", "\u{0310}", "\u{0312}", "\u{033D}", "\u{0346}", "\u{1D242}" };
+ for (0..rng.uintLessThan(usize, 4)) |_| try out.appendSlice(arena, marks[rng.uintLessThan(usize, marks.len)]);
+ }
+ try out.appendSlice(arena, "\x1b[39;59m");
+ break :blk out.items;
+ },
+ 5 => "\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n",
+ 6 => "\x1b[2;8r\x1b[S\x1b[T\x1b[r",
+ 7 => "\x1b[3L\x1b[2M\x1b[4@\x1b[2P",
+ else => "text",
+ };
+}
+
+/// Every answer whole: kitty's `ESC _ G ... ESC \`, or a CSI.
+fn answersParse(got: []const u8) bool {
+ var rest = got;
+ while (rest.len > 0) {
+ if (std.mem.startsWith(u8, rest, "\x1b_G")) {
+ const end = std.mem.indexOf(u8, rest, "\x1b\\") orelse return false;
+ if (std.mem.indexOfScalar(u8, rest[3..end], 0x1b) != null) return false;
+ rest = rest[end + 2 ..];
+ } else if (std.mem.startsWith(u8, rest, "\x1b[")) {
+ var i: usize = 2;
+ while (i < rest.len and !(rest[i] >= 0x40 and rest[i] <= 0x7e)) i += 1;
+ if (i == rest.len) return false;
+ rest = rest[i + 1 ..];
+ } else return false;
+ }
+ return true;
+}
+
+/// One seed's run, `steps` long; false on a bad answer.
+fn fuzzRun(seed: u64, steps: usize, trace: bool) !bool {
+ var prng: std.Random.DefaultPrng = .init(seed);
+ const rng = prng.random();
+ var r = try Rig.init(1);
+ defer r.deinit();
+ var arena_state: std.heap.ArenaAllocator = .init(gpa);
+ defer arena_state.deinit();
+ for (0..steps) |step| {
+ _ = arena_state.reset(.retain_capacity);
+ const arena = arena_state.allocator();
+ if (trace) std.debug.print("seed {d} step {d}\n", .{ seed, step });
+ switch (rng.uintLessThan(u8, 20)) {
+ 0 => {
+ const cols = 20 + rng.uintLessThan(u16, 140);
+ const rows = 6 + rng.uintLessThan(u16, 50);
+ r.p.update(.{ .resize = .{ .cols = cols, .rows = rows, .cell_pixels = .{ .w = 1 + rng.uintLessThan(u16, 30), .h = 1 + rng.uintLessThan(u16, 60) } } });
+ r.drain();
+ },
+ 1 => terminal.resizeGrid(r.pane(0), gpa, 1 + rng.uintLessThan(u16, 120), 1 + rng.uintLessThan(u16, 40)),
+ 2, 3 => {
+ const drawn = try r.places();
+ if (trace) std.debug.print(" {d} places, {d} placements, {d} images\n", .{ drawn.len, r.placementCount(0), r.storage(0).images.count() });
+ },
+ 4, 5, 6, 7 => r.feedSplit(0, try fuzzOutput(rng, arena), 1 + rng.uintLessThan(usize, 64)),
+ else => {
+ var batch: std.ArrayList(u8) = .empty;
+ for (0..1 + rng.uintLessThan(usize, 4)) |_| try batch.appendSlice(arena, try fuzzCommand(rng, arena));
+ r.feedSplit(0, batch.items, 1 + rng.uintLessThan(usize, 200));
+ },
+ }
+ const got = try r.take(0);
+ if (!answersParse(got)) {
+ if (trace) std.debug.print("seed {d} step {d}: unparsable answer {f}\n", .{ seed, step, std.ascii.hexEscape(got, .lower) });
+ return false;
+ }
+ }
+ _ = try r.places();
+ return true;
+}
+
+test "kitty: fuzzed traffic neither crashes nor leaks, and every answer parses" {
+ const steps: usize = @intCast(envNumber("PARDES_KITTY_FUZZ_STEPS", 400));
+ if (std.c.getenv("PARDES_KITTY_FUZZ_SEED")) |one| {
+ const seed = try std.fmt.parseInt(u64, std.mem.span(one), 10);
+ try std.testing.expect(try fuzzRun(seed, steps, true));
+ return;
+ }
+ const seeds = envNumber("PARDES_KITTY_FUZZ", 24);
+ for (0..seeds) |seed| {
+ if (try fuzzRun(seed, steps, false)) continue;
+ // Shrink to the shortest failing prefix.
+ var lo: usize = 1;
+ var hi: usize = steps;
+ while (lo < hi) {
+ const mid = lo + (hi - lo) / 2;
+ if (try fuzzRun(seed, mid, false)) lo = mid + 1 else hi = mid;
+ }
+ std.debug.print("kitty fuzz: seed {d} fails in {d} steps; PARDES_KITTY_FUZZ_SEED={d} PARDES_KITTY_FUZZ_STEPS={d} traces it\n", .{ seed, lo, seed, lo });
+ return error.FuzzFailed;
+ }
+ if (seeds != 24) std.debug.print("kitty fuzz: {d} seeds of {d} steps ok\n", .{ seeds, steps });
+}