verifier: max_value_size and checked size accounting (Q-020, DD-015)

- spec 08 §7.1: platform-independent accounted size table shared by
  max_value_size and max_local_frame; enum tag is the smallest of
  1/2/4/8 bytes; borrows 8, slices 16; checked arithmetic, overflow is
  E5101 with found = "overflow"
- every materializable type in a module must be at most 1 MiB,
  checked before functions are verified; each oversized type reported
  once
- E5101 fields no longer truncate huge sizes
- conformance: frame_too_large, value_too_large, size_overflow
- Q-020 resolved; Phase 1 closed
This commit is contained in:
dev committed 2026-10-04 13:29:54 +03:00
1 parent e3dc0fb7bb
commit 2ddf424c14
8 files changed
+165 -34

No files matched your search

+97 -17
View File
@@ -16,37 +16,51 @@ pub const Limits = struct {
pub const insts = 262_144;
pub const cases = 4_096;
pub const local_frame = 1_048_576; // bytes, spec 08 §11.5
pub const value_size = 1_048_576; // bytes, spec 08 §7.1
};
/// Accounted size in bytes (spec 08 §11.5). A counting rule, not a layout.
/// Recursive aggregates are reported later (E1003); they count as 0 here.
pub fn accountedSize(m: air.Module, t: air.Type, depth: usize) u64 {
/// Accounted size in bytes (spec 08 §7.1): a platform-independent metric,
/// not a layout. Null if the computation overflows. Recursive aggregates are
/// reported later (E1003) and count as 0 here; `void`, `never` and storage
/// types are not materializable and count as 0.
pub fn accountedSize(m: air.Module, t: air.Type, depth: usize) ?u64 {
if (depth > m.types.len + 1) return 0;
return switch (t) {
.int => |it| it.bits() / 8,
.bool => 1,
.array => |a| std.math.mul(u64, a.len, accountedSize(m, a.elem, depth + 1)) catch std.math.maxInt(u64),
.ref, .mut => 8,
.slice, .slice_mut => 16,
.array => |a| std.math.mul(u64, a.len, accountedSize(m, a.elem, depth + 1) orelse return null) catch null,
.named => |i| switch (m.types[i].kind) {
.@"struct" => |fs| blk: {
var sum: u64 = 0;
for (fs) |f| sum +|= accountedSize(m, f.ty, depth + 1);
for (fs) |f| sum = std.math.add(u64, sum, accountedSize(m, f.ty, depth + 1) orelse return null) catch return null;
break :blk sum;
},
.@"enum" => |vs| blk: {
var max: u64 = 0;
for (vs) |v| {
var sum: u64 = 0;
for (v.payload) |p| sum +|= accountedSize(m, p, depth + 1);
for (v.payload) |p| sum = std.math.add(u64, sum, accountedSize(m, p, depth + 1) orelse return null) catch return null;
max = @max(max, sum);
}
break :blk 4 +| max;
break :blk std.math.add(u64, tagSize(vs.len), max) catch null;
},
},
else => 0,
.void, .never, .local => 0,
};
}
/// Smallest of 1, 2, 4, 8 bytes that holds every variant index.
fn tagSize(variants: usize) u64 {
if (variants <= 1 << 8) return 1;
if (variants <= 1 << 16) return 2;
if (variants <= 1 << 32) return 4;
return 8;
}
pub fn check(c: *verifier.Context) !void {
try checkValueSizes(c);
const m = c.module;
const origin: air.Span = .{ .line = 1, .col = 1 };
try limit(c, origin, "functions per module", Limits.functions, m.functions.len);
@@ -74,23 +88,70 @@ pub fn check(c: *verifier.Context) !void {
};
}
try limit(c, f.span, "instructions per function", Limits.insts, n);
var frame: u64 = 0;
var frame: ?u64 = 0;
for (f.blocks) |b| for (b.insts) |inst| if (inst.op == .local_alloc) {
frame +|= accountedSize(m, inst.op.local_alloc, 0);
const size = accountedSize(m, inst.op.local_alloc, 0);
frame = if (frame != null and size != null) std.math.add(u64, frame.?, size.?) catch null else null;
};
try limit(c, f.span, "local frame bytes", Limits.local_frame, frame);
}
}
fn limit(c: *verifier.Context, span: air.Span, name: []const u8, max: u64, found: u64) !void {
if (found <= max) return;
/// `found == null` means the size computation overflowed.
fn limit(c: *verifier.Context, span: air.Span, name: []const u8, max: u64, found: ?u64) !void {
if (found) |n| if (n <= max) return;
try c.report("E5101", span, "deterministic verifier resource limit exceeded", &.{
.{ .name = "limit", .value = .{ .str = name } },
.{ .name = "max", .value = .{ .int = @intCast(max) } },
.{ .name = "found", .value = .{ .int = @intCast(found) } },
if (found) |n| .{ .name = "found", .value = .{ .int = std.math.cast(i64, n) orelse std.math.maxInt(i64) } } else .{ .name = "found", .value = .{ .str = "overflow" } },
});
}
/// Every materializable type in the module is at most `max_value_size`
/// (spec 08 §7.1). Each distinct oversized type is reported once, at its
/// first occurrence.
const ValueSizes = struct {
c: *verifier.Context,
reported: std.StringHashMapUnmanaged(void) = .empty,
fn visit(v: *ValueSizes, span: air.Span, t: air.Type) !void {
const m = v.c.module;
if (t != .local and t != .void and t != .never) {
const size = accountedSize(m, t, 0);
if (size == null or size.? > Limits.value_size) {
const name = try v.c.typeName(t);
const gop = try v.reported.getOrPut(v.c.arena, name);
if (!gop.found_existing) try v.c.report("E5101", span, "value type exceeds max_value_size", &.{
.{ .name = "limit", .value = .{ .str = "value size bytes" } },
.{ .name = "max", .value = .{ .int = Limits.value_size } },
.{ .name = "type", .value = .{ .str = name } },
if (size) |n| .{ .name = "found", .value = .{ .int = std.math.cast(i64, n) orelse std.math.maxInt(i64) } } else .{ .name = "found", .value = .{ .str = "overflow" } },
});
return; // inner types of an oversized type are not reported again
}
}
if (t.inner()) |inner| try v.visit(span, inner);
}
};
fn checkValueSizes(c: *verifier.Context) !void {
var v: ValueSizes = .{ .c = c };
const m = c.module;
for (m.types, 0..) |d, i| try v.visit(d.span, .{ .named = @intCast(i) });
for (m.functions) |f| {
for (f.params) |p| try v.visit(f.span, p.ty);
try v.visit(f.span, f.ret);
for (f.blocks) |b| {
for (b.params) |p| try v.visit(b.span, p.ty);
for (b.insts) |inst| switch (inst.op) {
.local_alloc => |t| try v.visit(inst.span, t),
.array_splat => |x| try v.visit(inst.span, x.ty),
else => {},
};
}
}
}
test "too many parameters" {
var src: std.ArrayList(u8) = .empty;
defer src.deinit(std.testing.allocator);
@@ -104,12 +165,31 @@ test "local frame limit" {
const h = "module t\nair 0\nlang 0.1\nfeatures [arrays, ownership]\n";
try verifier.expectCodes(h ++ "fn @f() -> void\neffects []\nentry:\n %a = local.alloc array<u8, 1048576>\n ret\n", &.{});
try verifier.expectCodes(h ++ "fn @f() -> void\neffects []\nentry:\n %a = local.alloc array<u8, 1048576>\n %b = local.alloc u8\n ret\n", &.{"E5101"});
try verifier.expectCodes(h ++ "fn @f() -> void\neffects []\nentry:\n %a = local.alloc array<usize, 131073>\n ret\n", &.{"E5101"});
// Ten locals of 200 KiB: each value fits, the frame does not.
var src: std.ArrayList(u8) = .empty;
defer src.deinit(std.testing.allocator);
try src.appendSlice(std.testing.allocator, h ++ "fn @f() -> void\neffects []\nentry:\n");
for (0..10) |i| try src.print(std.testing.allocator, " %a{d} = local.alloc array<u8, 204800>\n", .{i});
try src.appendSlice(std.testing.allocator, " ret\n");
try verifier.expectCodes(src.items, &.{"E5101"});
}
test "maximum value size" {
const h = "module t\nair 0\nlang 0.1\nfeatures [arrays, ownership]\n";
const ok = "fn @f(%a: array<u8, 1048576>) -> void\neffects []\nentry:\n ret\n";
try verifier.expectCodes(h ++ ok, &.{});
try verifier.expectCodes(h ++ "fn @f(%a: array<u8, 1048577>) -> void\neffects []\nentry:\n ret\n", &.{"E5101"});
// Struct sums count; reported once although the type appears twice.
try verifier.expectCodes(h ++ "type @B = struct { a: array<u8, 600000>, b: array<u8, 600000> }\nfn @f(%x: @B) -> @B\neffects []\nentry:\n ret %x\n", &.{"E5101"});
// Overflow while computing the size is E5101, not a crash.
try verifier.expectCodes(h ++ "fn @f(%a: array<array<u64, 4294967296>, 4294967296>) -> void\neffects []\nentry:\n ret\n", &.{"E5101"});
}
test "accounted sizes" {
const m: air.Module = .{ .name = "t", .format_version = 0, .lang = .{ 0, 1 }, .features = &.{}, .types = &.{}, .functions = &.{} };
try std.testing.expectEqual(@as(u64, 8), accountedSize(m, .{ .int = .isize }, 0));
try std.testing.expectEqual(@as(u64, 16), accountedSize(m, .{ .int = .u128 }, 0));
try std.testing.expectEqual(@as(u64, 1), accountedSize(m, .bool, 0));
try std.testing.expectEqual(@as(?u64, 8), accountedSize(m, .{ .int = .isize }, 0));
try std.testing.expectEqual(@as(?u64, 16), accountedSize(m, .{ .int = .u128 }, 0));
try std.testing.expectEqual(@as(?u64, 1), accountedSize(m, .bool, 0));
try std.testing.expectEqual(@as(u64, 1), tagSize(256));
try std.testing.expectEqual(@as(u64, 2), tagSize(257));
}