summaryrefslogtreecommitdiff
path: root/core/walk.zig
diff options
context:
space:
mode:
authorGabriel Schneider <[email protected]>2026-11-23 16:13:35 -0300
committerGabriel Schneider <[email protected]>2026-11-23 16:28:34 -0300
commit2aa977777fca277eba401ac0c1ebbd6e9049d6f9 (patch)
treed2180e8d48ddf0b182397962cd72745b2d5835f7 /core/walk.zig
parent8ee2339f0224000c48d23eca94e31d7c59fedd4c (diff)
downloadpardes-2aa977777fca277eba401ac0c1ebbd6e9049d6f9.tar.gz
pardes-2aa977777fca277eba401ac0c1ebbd6e9049d6f9.zip
the tape's reader holds every record to its type: a unit a client replays (from its session, a far link, a recorded tape) is cut, flipped or hostile, and each read of it is checked, the client diverging (and joining again) in words where it panicked: a record's kind byte (core/tape.zig Tape.next, peek, nextRecord, describe: `@fromBackingInt`: `invalid enum value` at tape.zig:458 on main, the fuzz's client side), a payload cut short after its head (replayUnit, finishInput: `next().?`), a host record under two bytes (finishInput, expectHost: new Tape.hostId), an input's tag (apply), and in core/walk.zig's decode, the tape's decoder for every type: an enum's bytes (new walk.enumFrom: its number held to a name, as std.enums.fromInt holds it), a run of enums copied whole (`named`), a slice's count held to the bytes left before it is allocated (`least`, the fewest bytes encode writes for a type, derived from the same walk); the one generic decoder checked, every type the tape carries is, and every type added; a user could hit it only by a corrupt or hostile peer
Co-Authored-By: Claude Opus <[email protected]> Claude-Session: https://claude.ai/code/session_016Y9MNBECMWcJD5pdS4SR6T
Diffstat (limited to 'core/walk.zig')
-rw-r--r--core/walk.zig58
1 files changed, 53 insertions, 5 deletions
diff --git a/core/walk.zig b/core/walk.zig
index d870052d..46bfadc5 100644
--- a/core/walk.zig
+++ b/core/walk.zig
@@ -457,8 +457,52 @@ pub const Reader = struct {
};
/// What an error read back is when this build has none of that name (an
-/// OS's own, a newer build's): the tape is another build's, and diverges.
-const TapeError = error{ EndOfTape, OutOfMemory, NoSuchError };
+/// OS's own, a newer build's): the tape is another build's, and diverges;
+/// BadTape, a value none of its type's (an enum's number of no name).
+const TapeError = error{ EndOfTape, OutOfMemory, NoSuchError, BadTape };
+
+/// An enum's bytes as `encode` writes them (`@sizeOf(E)` of them), read
+/// checked: null for a number none of `E`'s names has, which a tape or a
+/// dump read back may hold (cut, flipped, a hostile peer's), never cast.
+pub fn enumFrom(comptime E: type, bytes: *const [@sizeOf(E)]u8) ?E {
+ const info = @typeInfo(E).@"enum";
+ const wide = std.mem.bytesToValue(@Int(.unsigned, @sizeOf(E) * 8), bytes);
+ const tag: info.tag_type = @bitCast(@as(@Int(.unsigned, @bitSizeOf(info.tag_type)), @truncate(wide)));
+ if (info.mode == .nonexhaustive) return @fromBackingInt(tag);
+ return std.enums.fromInt(E, tag);
+}
+
+/// `bytes`, a run of `E`s copied whole, when each is one of `E`'s names (an
+/// enum's run is checked as one read alone is; another flat type's is bytes).
+fn named(comptime E: type, bytes: []const u8) error{BadTape}![]const u8 {
+ if (comptime @typeInfo(E) != .@"enum" or @typeInfo(E).@"enum".mode == .nonexhaustive) return bytes;
+ var at: usize = 0;
+ while (at < bytes.len) : (at += @sizeOf(E)) if (enumFrom(E, bytes[at..][0..@sizeOf(E)]) == null) return error.BadTape;
+ return bytes;
+}
+
+/// The fewest bytes `encode` writes for a `T`: a count read back is held to
+/// the bytes left before anything is made for it (0: no such bound).
+fn least(comptime T: type) usize {
+ return switch (@typeInfo(T)) {
+ .bool, .int, .float, .@"enum" => @sizeOf(T),
+ .optional, .error_union => 1,
+ .array => |a| a.len * least(a.child),
+ .pointer => |p| if (p.size == .slice) @sizeOf(usize) else 0,
+ .@"union" => |u| @sizeOf(u.tag_type.?),
+ .@"struct" => |s| sum: {
+ if (s.layout == .@"packed") break :sum @sizeOf(T);
+ if (machinery(T)) break :sum 0;
+ var n: usize = 0;
+ for (s.field_names, s.field_types, s.field_attrs) |name, F, attr| {
+ if (attr.@"comptime" or excluded(T, name)) continue;
+ n += least(F);
+ }
+ break :sum n;
+ },
+ else => 0,
+ };
+}
/// The inverse of `encode`; slices (and what a pointer points at) are made
/// in `a`. A field `encode` left out is its default.
@@ -466,7 +510,8 @@ pub fn decode(comptime T: type, r: *Reader, a: std.mem.Allocator) TapeError!T {
switch (@typeInfo(T)) {
.void, .null, .undefined => return {},
.bool => return (try r.take(1))[0] != 0,
- .int, .float, .@"enum" => return r.scalar(T),
+ .int, .float => return r.scalar(T),
+ .@"enum" => return enumFrom(T, (try r.take(@sizeOf(T)))[0..@sizeOf(T)]) orelse error.BadTape,
.optional => |o| {
if ((try r.take(1))[0] == 0) return null;
if (comptime machinery(o.child)) unreachable;
@@ -475,7 +520,7 @@ pub fn decode(comptime T: type, r: *Reader, a: std.mem.Allocator) TapeError!T {
.array => |a_info| {
var out: T = undefined;
if (comptime flat(a_info.child)) {
- @memcpy(std.mem.sliceAsBytes(&out), try r.take(@sizeOf(T)));
+ @memcpy(std.mem.sliceAsBytes(&out), try named(a_info.child, try r.take(@sizeOf(T))));
} else for (&out) |*e| {
e.* = try decode(a_info.child, r, a);
}
@@ -489,9 +534,12 @@ pub fn decode(comptime T: type, r: *Reader, a: std.mem.Allocator) TapeError!T {
},
.slice => {
const n = try r.scalar(usize);
+ // More than the bytes left could hold: refused before it is made.
+ const min = comptime least(p.child);
+ if (min > 0 and n > (r.bytes.len - r.at) / min) return error.EndOfTape;
const out = try a.alloc(p.child, n);
if (comptime flat(p.child)) {
- @memcpy(std.mem.sliceAsBytes(out), try r.take(n * @sizeOf(p.child)));
+ @memcpy(std.mem.sliceAsBytes(out), try named(p.child, try r.take(n * @sizeOf(p.child))));
} else for (out) |*e| {
e.* = try decode(p.child, r, a);
}