summaryrefslogtreecommitdiff
path: root/core/replica.zig
diff options
context:
space:
mode:
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());