verifier: cache structural properties and nominal size queries

This commit is contained in:
dev committed 2026-10-08 13:33:24 +03:00
1 parent 41f3afbf05
commit 48b9b42fed
31 files changed
+1399 -166

No files matched your search

+72 -10
View File
@@ -5,6 +5,7 @@
const std = @import("std");
const air = @import("../air.zig");
const air_module = @import("../air/module.zig");
const verifier = @import("../verifier.zig");
pub const Limits = struct {
@@ -24,30 +25,63 @@ pub const Limits = struct {
/// 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 {
// Read-only cache lookup never allocates; the uncached path remains an oracle.
return sizeQuery(m, t, depth, null) catch unreachable;
}
pub const SizeMemo = struct {
arena: std.mem.Allocator,
values: air_module.SizeCache = .{},
evaluations: usize = 0,
// Preserve the legacy depth cutoff exactly: at most V * (V + 2)
// nominal/depth states, rather than exponentially many shared paths.
pub fn size(cache: *SizeMemo, m: air.Module, t: air.Type, depth: usize) std.mem.Allocator.Error!?u64 {
return sizeQuery(m, t, depth, cache);
}
};
fn sizeQuery(m: air.Module, t: air.Type, depth: usize, memo: ?*SizeMemo) std.mem.Allocator.Error!?u64 {
if (depth > m.types.len + 1) return 0;
const key: air_module.SizeKey = .{ .index = if (t == .named) t.named else 0, .depth = depth };
if (t == .named) {
if (memo) |cache| {
if (cache.values.get(key)) |value| return value;
cache.evaluations += 1;
} else if (m.accounted_sizes) |cache| {
if (cache.get(key)) |value| return value;
}
}
const result = try sizeValue(m, t, depth, memo);
if (t == .named) if (memo) |cache| try cache.values.put(cache.arena, key, result);
return result;
}
fn sizeValue(m: air.Module, t: air.Type, depth: usize, memo: ?*SizeMemo) std.mem.Allocator.Error!?u64 {
return switch (t) {
.int => |it| it.bits() / 8,
.bool => 1,
.ref, .mut, .cap, .resource, .own, .region, .scope, .task, .shield, .sender, .receiver, .guard, .island, .frozen, .arc => 8,
.iref => 16,
.atomic => |inner| accountedSize(m, inner.*, depth + 1),
.mutex => |inner| std.math.add(u64, 8, accountedSize(m, inner.*, depth + 1) orelse return null) catch null,
.atomic => |inner| (try sizeQuery(m, inner.*, depth + 1, memo)),
.mutex => |inner| std.math.add(u64, 8, (try sizeQuery(m, inner.*, depth + 1, memo)) orelse return null) catch null,
.own_array => 16,
.buffer => 24,
.slice, .slice_mut => 16,
.builtin => |b| tagSize(b.variants().len),
.array => |a| std.math.mul(u64, a.len, accountedSize(m, a.elem, depth + 1) orelse return null) catch null,
.array => |a| std.math.mul(u64, a.len, (try sizeQuery(m, a.elem, depth + 1, memo)) orelse return null) catch null,
.named => |i| switch (m.types[i].kind) {
.@"struct" => |fs| blk: {
var sum: u64 = 0;
for (fs) |f| sum = std.math.add(u64, sum, accountedSize(m, f.ty, depth + 1) orelse return null) catch return null;
for (fs) |f| sum = std.math.add(u64, sum, (try sizeQuery(m, f.ty, depth + 1, memo)) 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 = std.math.add(u64, sum, accountedSize(m, p, depth + 1) orelse return null) catch return null;
for (v.payload) |p| sum = std.math.add(u64, sum, (try sizeQuery(m, p, depth + 1, memo)) orelse return null) catch return null;
max = @max(max, sum);
}
break :blk std.math.add(u64, tagSize(vs.len), max) catch null;
@@ -66,7 +100,9 @@ fn tagSize(variants: usize) u64 {
}
pub fn check(c: *verifier.Context) !void {
try checkValueSizes(c);
c.module.accounted_sizes = null;
var memo: SizeMemo = .{ .arena = c.arena };
try checkValueSizes(c, &memo);
const m = c.module;
const origin: air.Span = .{ .line = 1, .col = 1 };
try limit(c, origin, "functions per module", Limits.functions, m.functions.len);
@@ -97,11 +133,14 @@ pub fn check(c: *verifier.Context) !void {
try limit(c, f.span, "instructions per function", Limits.insts, n);
var frame: ?u64 = 0;
for (f.blocks) |b| for (b.insts) |inst| if (inst.op == .local_alloc) {
const size = accountedSize(m, inst.op.local_alloc, 0);
const size = try memo.size(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);
}
const cached = try c.arena.create(air_module.SizeCache);
cached.* = memo.values;
c.module.accounted_sizes = cached;
}
/// `found == null` means the size computation overflowed.
@@ -119,12 +158,13 @@ fn limit(c: *verifier.Context, span: air.Span, name: []const u8, max: u64, found
/// first occurrence.
const ValueSizes = struct {
c: *verifier.Context,
memo: *SizeMemo,
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);
const size = try v.memo.size(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);
@@ -141,8 +181,8 @@ const ValueSizes = struct {
}
};
fn checkValueSizes(c: *verifier.Context) !void {
var v: ValueSizes = .{ .c = c };
fn checkValueSizes(c: *verifier.Context, memo: *SizeMemo) !void {
var v: ValueSizes = .{ .c = c, .memo = memo };
const m = c.module;
for (m.types, 0..) |d, i| try v.visit(d.span, .{ .named = @intCast(i) });
for (m.functions) |f| {
@@ -168,6 +208,28 @@ test "too many parameters" {
try verifier.expectCodes(src.items, &.{"E5101"});
}
test "accounted sizes memoize shared diamonds and cyclic depth states" {
var scratch = std.heap.ArenaAllocator.init(std.testing.allocator);
defer scratch.deinit();
const a = scratch.allocator();
const ds = try a.alloc(air.TypeDecl, 65);
for (ds, 0..) |*d, i| d.* = .{ .name = "N", .span = .{ .line = 1, .col = 1 }, .kind = .{ .@"struct" = if (i == 64) &.{} else try a.dupe(air_module.Field, &.{ .{ .name = "a", .ty = .{ .named = @intCast(i + 1) } }, .{ .name = "b", .ty = .{ .named = @intCast(i + 1) } } }) } };
var m: air.Module = .{ .name = "test", .format_version = 0, .lang = .{ 0, 1 }, .features = &.{}, .types = ds, .functions = &.{} };
var memo: SizeMemo = .{ .arena = a };
for (0..65) |i| try std.testing.expectEqual(@as(?u64, 0), try memo.size(m, .{ .named = @intCast(i) }, 0));
try std.testing.expect(memo.evaluations <= 65 * 67);
const before = memo.evaluations;
m.accounted_sizes = &memo.values;
for (0..65) |i| try std.testing.expectEqual(@as(?u64, 0), accountedSize(m, .{ .named = @intCast(i) }, 0));
try std.testing.expectEqual(before, memo.evaluations);
// A cyclic, branching size query keeps the legacy depth cutoff exactly.
ds[64].kind = .{ .@"struct" = try a.dupe(air_module.Field, &.{ .{ .name = "a", .ty = .{ .named = 0 } }, .{ .name = "b", .ty = .{ .named = 0 } } }) };
m.accounted_sizes = null;
var cyclic: SizeMemo = .{ .arena = a };
try std.testing.expectEqual(@as(?u64, 0), try cyclic.size(m, .{ .named = 0 }, 0));
try std.testing.expect(cyclic.evaluations <= 65 * 67);
}
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", &.{});