301 lines
15 KiB
Zig
301 lines
15 KiB
Zig
//! Deterministic verifier resource limits (spec 08 §7, E5101).
|
|
//!
|
|
//! Limits are fixed by the spec so that no flag can change which programs
|
|
//! are accepted.
|
|
|
|
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 {
|
|
pub const functions = 65_536;
|
|
pub const types = 65_536;
|
|
pub const members = 256; // fields, variants, payload values
|
|
pub const params = 64; // per function or block; arguments per call or branch
|
|
pub const blocks = 16_384;
|
|
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 §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 {
|
|
// 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,
|
|
|
|
// Count nominal member edges, not finite anonymous wrappers: at most
|
|
// V * (V + 2) nominal/depth states, rather than shared-path expansion.
|
|
|
|
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 {
|
|
// Only nominal members advance this cycle guard. Array/atomic/mutex
|
|
// nesting is finite syntax and must never truncate a valid value's size.
|
|
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| (try sizeQuery(m, inner.*, depth, memo)),
|
|
.mutex => |inner| std.math.add(u64, 8, (try sizeQuery(m, inner.*, depth, 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, (try sizeQuery(m, a.elem, depth, 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, (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, (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;
|
|
},
|
|
},
|
|
.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 {
|
|
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);
|
|
try limit(c, origin, "type declarations per module", Limits.types, m.types.len);
|
|
for (m.types) |d| switch (d.kind) {
|
|
.@"struct" => |fs| try limit(c, d.span, "fields per struct", Limits.members, fs.len),
|
|
.@"enum" => |vs| {
|
|
try limit(c, d.span, "variants per enum", Limits.members, vs.len);
|
|
for (vs) |v| try limit(c, d.span, "payload values per variant", Limits.members, v.payload.len);
|
|
},
|
|
};
|
|
for (m.functions) |f| {
|
|
try limit(c, f.span, "parameters per function", Limits.params, f.params.len);
|
|
try limit(c, f.span, "blocks per function", Limits.blocks, f.blocks.len);
|
|
var n: usize = 0;
|
|
for (f.blocks) |b| {
|
|
n += b.insts.len;
|
|
try limit(c, b.span, "parameters per block", Limits.params, b.params.len);
|
|
for (b.insts) |inst| switch (inst.op) {
|
|
.call => |x| try limit(c, inst.span, "arguments per call", Limits.params, x.args.len),
|
|
.struct_make => |x| try limit(c, inst.span, "arguments per call", Limits.params, x.args.len),
|
|
.array_make => |x| try limit(c, inst.span, "arguments per call", Limits.params, x.args.len),
|
|
.enum_make => |x| try limit(c, inst.span, "arguments per call", Limits.params, x.args.len),
|
|
.@"switch" => |x| try limit(c, inst.span, "cases per switch", Limits.cases, x.cases.len),
|
|
else => {},
|
|
};
|
|
}
|
|
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 = 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.
|
|
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) } },
|
|
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,
|
|
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 = 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);
|
|
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, 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| {
|
|
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),
|
|
inline .array_splat, .array_make, .aggregate_unpack, .own_pack, .buffer_new => |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);
|
|
try src.appendSlice(std.testing.allocator, verifier.test_header ++ "fn @f(");
|
|
for (0..65) |i| try src.print(std.testing.allocator, "{s}%p{d}: u8", .{ if (i == 0) "" else ", ", i });
|
|
try src.appendSlice(std.testing.allocator, ") -> void\neffects []\nentry:\n ret\n");
|
|
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 nominal depth guard bounded.
|
|
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", &.{});
|
|
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"});
|
|
// 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, 1), tagSize(256));
|
|
try std.testing.expectEqual(@as(u64, 2), tagSize(257));
|
|
}
|
|
|
|
test "anonymous wrapper sizes do not depend on nominal declaration count" {
|
|
var scratch = std.heap.ArenaAllocator.init(std.testing.allocator);
|
|
defer scratch.deinit();
|
|
const a = scratch.allocator();
|
|
const leaf: air.Type = .{ .int = .u64 };
|
|
const row: air.Type.Array = .{ .elem = leaf, .len = 2 };
|
|
const rows: air.Type.Array = .{ .elem = .{ .array = &row }, .len = 2 };
|
|
const matrix: air.Type = .{ .array = &rows };
|
|
const cell: air.Type = .{ .atomic = &leaf };
|
|
const cells: air.Type.Array = .{ .elem = cell, .len = 4 };
|
|
const wrapped: air.Type = .{ .mutex = &matrix };
|
|
const ds = try a.alloc(air.TypeDecl, 4);
|
|
for (ds) |*d| d.* = .{ .name = "Unused", .span = .{ .line = 1, .col = 1 }, .kind = .{ .@"struct" = &.{} } };
|
|
for (0..5) |n| {
|
|
const m: air.Module = .{ .name = "t", .format_version = 0, .lang = .{ 0, 1 }, .features = &.{}, .types = ds[0..n], .functions = &.{} };
|
|
var memo: SizeMemo = .{ .arena = a };
|
|
for ([_]air.Type{ matrix, .{ .array = &cells }, wrapped }, [_]u64{ 32, 32, 40 }) |t, want| {
|
|
try std.testing.expectEqual(@as(?u64, want), accountedSize(m, t, 0));
|
|
try std.testing.expectEqual(@as(?u64, want), try memo.size(m, t, 0));
|
|
}
|
|
}
|
|
}
|
|
|
|
test "cyclic nominal layouts through anonymous wrappers stay bounded" {
|
|
var scratch = std.heap.ArenaAllocator.init(std.testing.allocator);
|
|
defer scratch.deinit();
|
|
const a = scratch.allocator();
|
|
const row: air.Type.Array = .{ .elem = .{ .named = 0 }, .len = 2 };
|
|
const m: air.Module = .{ .name = "t", .format_version = 0, .lang = .{ 0, 1 }, .features = &.{}, .types = &.{.{ .name = "Cycle", .span = .{ .line = 1, .col = 1 }, .kind = .{ .@"struct" = &.{.{ .name = "children", .ty = .{ .array = &row } }} } }}, .functions = &.{} };
|
|
var memo: SizeMemo = .{ .arena = a };
|
|
try std.testing.expectEqual(@as(?u64, 0), try memo.size(m, .{ .named = 0 }, 0));
|
|
try std.testing.expect(memo.evaluations <= m.types.len * (m.types.len + 2));
|
|
}
|