summaryrefslogtreecommitdiff
path: root/core/replica.zig
diff options
context:
space:
mode:
authorGabriel Schneider <[email protected]>2026-11-23 22:53:42 -0300
committerGabriel Schneider <[email protected]>2026-11-23 23:27:56 -0300
commit263b7d2e24f5bdad6a6a62b4a13bc74b8be68cb3 (patch)
treef6f288c54222bd8b525a00a08ac6a3527ff17597 /core/replica.zig
parentdde58f770995dcbc929708048a6e0f390c70f8af (diff)
downloadpardes-263b7d2e24f5bdad6a6a62b4a13bc74b8be68cb3.tar.gz
pardes-263b7d2e24f5bdad6a6a62b4a13bc74b8be68cb3.zip
walk: a length past what its buffer holds is refused where a tape, a dump or an image is decoded, its field named, never walked: merge65's core-test crashed in core/fuzz.zig's own dump fuzz (`index out of bounds: index 4503599627370496, len 64` at core/Text.zig:130, `Text.deinit` slicing `ed_redo[0..ed_redo_len]`, from views.zig `freeView`): a damaged dump was loaded with a length past its buffer, and the core's own deinit panicked (a digest, a copy and every use slice the same way); the layer's new fields moved the dump's layout so the fuzz's seeds reach it now, but the fault is the decoders': each read a `replica.len` pair's length as just a number. THE RULE, ONCE, for every pair of every type (the comptime walk lists them: `T.replica.len`, what the digest's `hash`, `fieldDigests`, the dump writer and rollback.zig already go by): `pastHeld` (the first length of a value past what its buffer holds: a slice's its own length, a fixed array's how many were read back, a dump writing one only as far as its length said, else whole), called where a struct's fields are decoded: `decode` (the tape: BadTape), `Loader.fields` (the byte dump, both of its paths: `heldTo`, BadDump) and `Get.fields` (the image: BadDump), the count read kept by each reader's `elements`/`listInto` (`filled`); the refusal's words name the field (`Text.ed_redo_len says N, past the 64 its buffer holds: refused`) and reach the user: `walk.load` and dump.zig `loadKnowing`/`loadImage` hand a refusal's notes out as they hand out a load's, and replica.zig `fromDump` says them (`the session's dump would not load: BadDump: <field> says ...`; `Replica.made` copies them out of the epoch); TESTS FIRST, each failing with the check out (run: the walk test loads `ring_len = 200`, the replica test panics `index out of bounds: index 7, len 1`): walk.zig "a length past what its buffer holds is refused where it is read back (a tape, a dump, an image), its field named, never walked" (a ring a length cuts and a room a length cuts, each through `encode`/`decode`, `save`/`load` with its length byte set past the array and past what the dump held, and `Put`/`Get`), replica.zig "a join's dump whose length says more than its buffer holds is refused, the field named, never loaded" (a column tag's `nsel` set to 7 in a byte dump's bytes, `fromDump`'s words); the fuzz as merged passes on it; the user's case: a Restore, an Upgrade or a join of a damaged dump is refused in words, the session going on (it ended at the first deinit or digest)
Co-Authored-By: Claude Opus <[email protected]> Claude-Session: https://claude.ai/code/session_016Y9MNBECMWcJD5pdS4SR6T
Diffstat (limited to 'core/replica.zig')
-rw-r--r--core/replica.zig53
1 files changed, 46 insertions, 7 deletions
diff --git a/core/replica.zig b/core/replica.zig
index 57dd4eed..72662df6 100644
--- a/core/replica.zig
+++ b/core/replica.zig
@@ -524,7 +524,7 @@ pub const Replica = struct {
/// an image a frame late at worst... or reads freed pixels, which no test
/// session has.
pub fn initDump(gpa: Allocator, id: u16, view: u8, bytes: []const u8, opts: pardes.Options) !*Replica {
- return made(gpa, id, view, opts, bytes);
+ return made(gpa, id, view, opts, bytes, null);
}
/// A pair's first client's replica (Options.pair): its core made here
@@ -534,11 +534,12 @@ pub const Replica = struct {
/// join, which the welcome confirms by its digest (`confirmBoot`). Its
/// id the session's first client's.
pub fn initBoot(gpa: Allocator, opts: pardes.Options) !*Replica {
- return made(gpa, 1, null, opts, null);
+ return made(gpa, 1, null, opts, null, null);
}
- /// `initDump`'s, or with no dump `initBoot`'s.
- fn made(gpa: Allocator, id: u16, view: ?u8, opts: pardes.Options, dump: ?[]const u8) !*Replica {
+ /// `initDump`'s, or with no dump `initBoot`'s; why a dump was refused
+ /// in `why` when given (its loader's words).
+ fn made(gpa: Allocator, id: u16, view: ?u8, opts: pardes.Options, dump: ?[]const u8, why: ?*std.ArrayList(u8)) !*Replica {
const r = try gpa.create(Replica);
r.* = .{ .gpa = gpa, .id = id, .view = view orelse 0, .t = tape.memory(gpa), .guess = true };
r.t.skips_render = true;
@@ -560,7 +561,13 @@ pub const Replica = struct {
// replica's: deltas write them while a copy is held.
pardes.dump.terminal_gpa = pardes.panes.emulator_gpa;
defer pardes.dump.terminal_gpa = null;
- const c = if (dump) |bytes| try pardes.dump.load(epoch, o, bytes, null) else boot: {
+ // The loader's words, the epoch's: why a refusal is copied out.
+ var words: std.ArrayList(u8) = .empty;
+ defer words.deinit(epoch);
+ const c = if (dump) |bytes| pardes.dump.load(epoch, o, bytes, &words) catch |err| {
+ if (why) |w| w.appendSlice(gpa, words.items) catch {};
+ return err;
+ } else boot: {
// The addons the session's start finds (runtime/session.zig).
o.addons = pardes.Addon.scan();
// On no tape, as the session's start is (one recording to a
@@ -3055,8 +3062,12 @@ pub fn join(gpa: Allocator, j: *Joined, path: []const u8, session: []const u8, o
fn fromDump(gpa: Allocator, j: *Joined, o: pardes.Options, have: ?*Have, why: []u8, t0: u64, session: []const u8) ?*Replica {
var twice = false;
while (true) {
- const r = Replica.initDump(gpa, j.client, j.view, j.dump, o) catch |err| {
- _ = std.fmt.bufPrint(why, "the session's dump would not load: {t}\x00", .{err}) catch {};
+ var refused: std.ArrayList(u8) = .empty;
+ defer refused.deinit(gpa);
+ const r = Replica.made(gpa, j.client, j.view, o, j.dump, &refused) catch |err| {
+ // The loader's first words name the field it refused.
+ const first = refused.items[0..@min(400, std.mem.indexOfScalar(u8, refused.items, '\n') orelse refused.items.len)];
+ _ = std.fmt.bufPrint(why, "the session's dump would not load: {t}{s}{s}\x00", .{ err, if (first.len > 0) ": " else "", first }) catch {};
return null;
};
if (r.loaded(j.digest)) {
@@ -5096,6 +5107,34 @@ test "bytes: a kitty image shown by a file, a temporary file or shared memory is
try std.testing.expect(shm.shm_open(object, @bitCast(std.c.O{ .ACCMODE = .RDONLY }), 0) < 0);
}
+test "a join's dump whose length says more than its buffer holds is refused, the field named, never loaded" {
+ const gpa = std.testing.allocator;
+ const a = try Authority.init(gpa, .{ .cols = 80, .rows = 24, .file = "mise.toml" }, TestHost.host());
+ defer a.deinit();
+ // A column tag's room for one range, that range written as a dump
+ // writes its numbers, then how many it uses: one.
+ const tag = &a.core.view().col_tags[0];
+ tag.sels = try a.core.gpa.alloc(pardes.panes.Text.SelRange, 1);
+ tag.sels[0] = .{ .row = 0x1234567, .col = 0x2345678, .arow = 0x3456789, .acol = 0x456789a, .sticky = 0x56789ab };
+ tag.nsel = 1;
+ const was = Stepping.begin(&a.t, null);
+ const dumped = try pardes.dump.byteDump(a.core);
+ tape.resync(&a.t);
+ was.end();
+ defer a.core.gpa.free(dumped);
+ const range = std.mem.sliceAsBytes(&[_]i32{ 0x1234567, 0x2345678, 0x3456789, 0x456789a, 0x56789ab });
+ const at = (std.mem.indexOf(u8, dumped, range) orelse return error.NoRange) + range.len;
+ try std.testing.expectEqual(@as(u16, 1), std.mem.readInt(u16, dumped[at..][0..2], .little));
+ // As a damaged dump has it: seven, in the room for one.
+ std.mem.writeInt(u16, dumped[at..][0..2], 7, .little);
+ var j: Joined = .{ .fd = -1, .pid = 0, .client = 1, .view = 0, .gmtoff = 0, .dump = dumped };
+ var why: [600]u8 = @splat(0);
+ try std.testing.expect(fromDump(gpa, &j, .{ .cols = 80, .rows = 24 }, null, &why, 0, "") == null);
+ const words = std.mem.sliceTo(&why, 0);
+ try std.testing.expect(std.mem.startsWith(u8, words, "the session's dump would not load: BadDump: "));
+ try std.testing.expect(std.mem.indexOf(u8, words, "nsel says 7, past the 1 its buffer holds") != null);
+}
+
test "a replica joined from the session's dump replays the units after it" {
const gpa = std.testing.allocator;
const a = try Authority.init(gpa, .{ .cols = 80, .rows = 24, .file = "mise.toml" }, TestHost.host());