diff options
| -rw-r--r-- | src/walk.zig | 46 | ||||
| -rw-r--r-- | test/cross_replay.py | 11 |
2 files changed, 53 insertions, 4 deletions
diff --git a/src/walk.zig b/src/walk.zig index c5a6e7a2..14e1929e 100644 --- a/src/walk.zig +++ b/src/walk.zig @@ -302,12 +302,12 @@ pub fn hash(comptime T: type, v: *const T, h: *Hasher) void { .bool => h.update(&.{@intFromBool(v.*)}), .int => hashInt(T, v.*, h), .float => h.update(std.mem.asBytes(v)), - .@"enum" => |e| hashInt(e.tag_type, @backingInt(v.*), h), + .@"enum" => hashEnum(T, v.*, h), .optional => |o| if (v.*) |*payload| { h.update(&.{1}); hash(o.child, payload, h); } else h.update(&.{0}), - .array => |a| if (comptime flat(a.child)) h.update(std.mem.sliceAsBytes(v)) else for (v) |*e| hash(a.child, e, h), + .array => |a| if (comptime flatHash(a.child)) h.update(std.mem.sliceAsBytes(v)) else for (v) |*e| hash(a.child, e, h), .pointer => |p| switch (p.size) { .one => hash(p.child, v.*, h), .slice => hashSlice(p.child, v.*, h), @@ -361,6 +361,35 @@ pub fn hash(comptime T: type, v: *const T, h: *Hasher) void { } } +/// Bytes a digest takes whole: an enum's are its build's number, not it. +fn flatHash(comptime T: type) bool { + return flat(T) and @typeInfo(T) != .@"enum"; +} + +/// An enum by its name, as a dump reads it, not by its number: an enum +/// made at compile time numbers its words as its build has them +/// (`registry.Builtin` has fewer in a FreeBSD build than a Linux one, which +/// has Tty9p), so a window client here of a session on the box loaded a +/// +LocationsConfig to another digest. A name's word (its hash, fixed +/// width, so that two names in a row are not one), looked up by number +/// where the words are numbered 0 up; one numbered by hand, or a value no +/// word has, by its number. +fn hashEnum(comptime T: type, v: T, h: *Hasher) void { + const e = @typeInfo(T).@"enum"; + const words: ?[e.field_names.len]u64 = comptime words: { + @setEvalBranchQuota(100_000); + var out: [e.field_names.len]u64 = undefined; + for (e.field_names, e.field_values, 0..) |name, n, i| { + if (n != i) break :words null; + out[i] = std.hash.Wyhash.hash(0, name); + } + break :words out; + }; + const n = @backingInt(v); + if (words) |table| if (n >= 0 and n < table.len) return h.update(std.mem.asBytes(&table[@intCast(n)])); + hashInt(e.tag_type, n, h); +} + /// An integer by its value: one narrower than its bytes (a u21, an i2) has /// bits in memory that are no part of it. fn hashInt(comptime I: type, n: I, h: *Hasher) void { @@ -372,7 +401,7 @@ fn hashInt(comptime I: type, n: I, h: *Hasher) void { fn hashSlice(comptime E: type, items: []const E, h: *Hasher) void { if (in_blob and items.len * @sizeOf(E) >= blob_min) return hashBlob(E, items, h); h.update(std.mem.asBytes(&items.len)); - if (comptime flat(E)) return h.update(std.mem.sliceAsBytes(items)); + if (comptime flatHash(E)) return h.update(std.mem.sliceAsBytes(items)); for (items) |*e| hash(E, e, h); } @@ -652,6 +681,17 @@ test "an error is taped by its name, which another build reads back as its own" try std.testing.expectEqual(error.FileNotFound, (try decode(Report, &r, arena.allocator())).err); } +test "an enum is digested by its name, which another build may number otherwise" { + // registry.Builtin as a Linux build makes it (Tty9p in it) and as a + // FreeBSD one does: LocationsConfig is another number in each. + const Linux = enum { Look, Tty9p, LocationsConfig }; + const FreeBSD = enum { Look, LocationsConfig }; + const S = struct { from: Linux, also: [2]Linux }; + const T = struct { from: FreeBSD, also: [2]FreeBSD }; + try std.testing.expectEqual(digest(S, &.{ .from = .LocationsConfig, .also = .{ .Look, .LocationsConfig } }), digest(T, &.{ .from = .LocationsConfig, .also = .{ .Look, .LocationsConfig } })); + try std.testing.expect(digest(S, &.{ .from = .Look, .also = .{ .Look, .Look } }) != digest(S, &.{ .from = .Tty9p, .also = .{ .Look, .Look } })); +} + test "a digest is of values: the same text at two addresses is one digest" { const S = struct { name: []const u8, n: u32, next: ?*const u32 }; const a = "same text"; diff --git a/test/cross_replay.py b/test/cross_replay.py index 26304d9a..6c5a1e4d 100644 --- a/test/cross_replay.py +++ b/test/cross_replay.py @@ -17,6 +17,11 @@ The session meets the host's failures, whose errors cross by name: a Look of a fifo (a read the host refuses) and of a directory, a stat and a read under /os over 9P, a Save into a file as if it were a directory and a Restore of a dump that is not there (errors the host reports, anyerrors). +And it holds what a digest must name the same in every build: a ++LocationsConfig, whose origin is a Builtin, and a file pane renamed (H19: +no Linux client could join a FreeBSD session so). ponytail: no Grep or +Find, which a 9P write runs on the loop, its reads no tape site (a replay +of it panics, by any build). """ import os import shutil @@ -70,12 +75,16 @@ def record(binary, out): started.append(int(status['pid'])) pane = next(int(row.split()[0]) for row in c.read('/index').splitlines() if row.split()[1] == b'text') for line in (f'Look {root / "fifo"}', f'Look {root / "dir"}', f'Save {root / "a.txt" / "x"}', - f'Restore {root / "nothing.dump"}'): + f'Restore {root / "nothing.dump"}', 'LocationsConfig'): try: c.write(f'/pane/{pane}/exec', line.encode() + b'\n') except Exception: pass # said as the write's error: a failure is what is wanted time.sleep(.3) + # A pane a word made (+LocationsConfig: its origin a Builtin, + # which a FreeBSD build numbers otherwise), and a file renamed. + c.write(f'/pane/{pane}/ctl', f'name {root / "renamed.txt"}\n'.encode()) + time.sleep(.3) c.stat(f'/os{root / "a.txt"}') assert c.read(f'/os{root / "a.txt"}') == b'hello\n' for _ in range(2): # the failed Save left the text edited: Exit asks again |
