verifier: evaluate type sizes with explicit continuations
This commit is contained in:
1 parent
c94e5a0868
commit
3906ce5a9b
11 files changed
+353
-54
No files matched your search
+6
-1
@@ -491,7 +491,12 @@ pub const NominalProperties = packed struct(u32) {
|
||||
padding: u10 = 0,
|
||||
};
|
||||
|
||||
pub const SizeKey = struct { index: u32, depth: usize };
|
||||
pub const SizeKey = struct {
|
||||
index: u32 = 0,
|
||||
depth: usize,
|
||||
kind: enum { named, array, atomic, mutex } = .named,
|
||||
address: usize = 0,
|
||||
};
|
||||
pub const SizeCache = std.AutoHashMapUnmanaged(SizeKey, ?u64);
|
||||
|
||||
pub const Module = struct {
|
||||
|
||||
@@ -157,4 +157,10 @@ test "verification rebuilds caller-supplied property and size caches" {
|
||||
large.accounted_sizes = &sizes;
|
||||
const rejected = try verifyModule(a, "t.air", large);
|
||||
try std.testing.expectEqualStrings("E5101", rejected.diagnostics[0].code.id);
|
||||
var anonymous = (try air.parse.parse(a, "t.air", test_header ++ "fn @f(%x: array<u8, 1048577>) -> void\neffects []\nentry:\n ret\n")).module.?;
|
||||
const array = anonymous.functions[0].params[0].ty.array;
|
||||
try sizes.put(a, .{ .depth = 0, .kind = .array, .address = @intFromPtr(array) }, 0);
|
||||
anonymous.accounted_sizes = &sizes;
|
||||
const anonymous_rejected = try verifyModule(a, "t.air", anonymous);
|
||||
try std.testing.expectEqualStrings("E5101", anonymous_rejected.diagnostics[0].code.id);
|
||||
}
|
||||
+278
-53
@@ -21,75 +21,208 @@ pub const Limits = struct {
|
||||
};
|
||||
|
||||
/// 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
|
||||
/// not a layout. Null if the computation overflows. The nominal depth guard
|
||||
/// cuts invalid cycles at zero; earlier fields retain their contribution.
|
||||
/// Finite-layout validation reports E1003 later. `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;
|
||||
if (depth > m.types.len + 1) return 0;
|
||||
// Compose unary wrappers as a saturated affine map. A saturated factor
|
||||
// times a zero-sized leaf is still zero; do not call it an overflow.
|
||||
var current = t;
|
||||
var factor: ?u64 = 1;
|
||||
var offset: ?u64 = 0;
|
||||
var fixed: ?u64 = null;
|
||||
var base: ?u64 = undefined;
|
||||
while (true) {
|
||||
if (sizeKey(current, depth)) |key| if (m.accounted_sizes) |cache| {
|
||||
if (cache.get(key)) |value| {
|
||||
base = value;
|
||||
break;
|
||||
}
|
||||
};
|
||||
switch (current) {
|
||||
.array => |a| {
|
||||
if (a.len == 0) {
|
||||
// Even an invalid outer zero length cannot mask an
|
||||
// overflowing inner query. Check each reset segment.
|
||||
const value = offset orelse return null;
|
||||
if (fixed == null) fixed = value;
|
||||
factor = 1;
|
||||
offset = 0;
|
||||
} else factor = if (factor) |f| std.math.mul(u64, f, a.len) catch null else null;
|
||||
current = a.elem;
|
||||
},
|
||||
.atomic => |inner| current = inner.*,
|
||||
.mutex => |inner| {
|
||||
const add = if (factor) |f| std.math.mul(u64, f, 8) catch null else null;
|
||||
offset = if (offset != null and add != null) std.math.add(u64, offset.?, add.?) catch null else null;
|
||||
current = inner.*;
|
||||
},
|
||||
else => {
|
||||
base = uncachedBaseSize(m, current, depth);
|
||||
break;
|
||||
},
|
||||
}
|
||||
}
|
||||
const value = base orelse return null;
|
||||
const product = if (value == 0) 0 else std.math.mul(u64, factor orelse return null, value) catch return null;
|
||||
const result = std.math.add(u64, product, offset orelse return null) catch return null;
|
||||
return fixed orelse result;
|
||||
}
|
||||
|
||||
fn sizeKey(t: air.Type, depth: usize) ?air_module.SizeKey {
|
||||
return switch (t) {
|
||||
.named => |i| .{ .index = i, .depth = depth },
|
||||
.array => |a| .{ .depth = depth, .kind = .array, .address = @intFromPtr(a) },
|
||||
.atomic => |a| .{ .depth = depth, .kind = .atomic, .address = @intFromPtr(a) },
|
||||
.mutex => |a| .{ .depth = depth, .kind = .mutex, .address = @intFromPtr(a) },
|
||||
else => null,
|
||||
};
|
||||
}
|
||||
|
||||
const SizeFrame = struct {
|
||||
ty: air.Type,
|
||||
depth: usize,
|
||||
next: usize = 0,
|
||||
variant: usize = 0,
|
||||
sum: u64 = 0,
|
||||
maximum: u64 = 0,
|
||||
|
||||
fn child(f: *SizeFrame, m: air.Module) ?air.Type {
|
||||
return switch (m.types[f.ty.named].kind) {
|
||||
.@"struct" => |fs| if (f.next < fs.len) fs[f.next].ty else null,
|
||||
.@"enum" => |vs| blk: {
|
||||
while (f.variant < vs.len) {
|
||||
if (f.next < vs[f.variant].payload.len) break :blk vs[f.variant].payload[f.next];
|
||||
f.maximum = @max(f.maximum, f.sum);
|
||||
f.variant += 1;
|
||||
f.next = 0;
|
||||
f.sum = 0;
|
||||
}
|
||||
break :blk null;
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
fn total(f: SizeFrame, m: air.Module) ?u64 {
|
||||
return switch (m.types[f.ty.named].kind) {
|
||||
.@"struct" => f.sum,
|
||||
.@"enum" => |vs| std.math.add(u64, tagSize(vs.len), f.maximum) catch null,
|
||||
};
|
||||
}
|
||||
};
|
||||
|
||||
pub const SizeMemo = struct {
|
||||
arena: std.mem.Allocator,
|
||||
values: air_module.SizeCache = .{},
|
||||
evaluations: usize = 0,
|
||||
pending: std.ArrayList(SizeFrame) = .empty,
|
||||
|
||||
// Count nominal member edges, not finite anonymous wrappers: at most
|
||||
// V * (V + 2) nominal/depth states, rather than shared-path expansion.
|
||||
|
||||
// Nominal member edges alone advance the cycle guard. Anonymous cache
|
||||
// identities also keep repeated inner-budget/runtime queries linear.
|
||||
pub fn size(cache: *SizeMemo, m: air.Module, t: air.Type, depth: usize) std.mem.Allocator.Error!?u64 {
|
||||
return sizeQuery(m, t, depth, cache);
|
||||
cache.pending.clearRetainingCapacity();
|
||||
defer cache.pending.clearRetainingCapacity();
|
||||
var current = t;
|
||||
var current_depth = depth;
|
||||
var result: ?u64 = undefined;
|
||||
descend: while (true) {
|
||||
if (current_depth > m.types.len + 1) {
|
||||
result = 0;
|
||||
} else if (sizeKey(current, current_depth)) |key| {
|
||||
if (cache.values.get(key)) |value| {
|
||||
result = value;
|
||||
} else {
|
||||
if (current == .named) cache.evaluations += 1;
|
||||
try cache.pending.append(cache.arena, .{ .ty = current, .depth = current_depth });
|
||||
switch (current) {
|
||||
.array => |a| {
|
||||
current = a.elem;
|
||||
continue :descend;
|
||||
},
|
||||
.atomic, .mutex => |inner| {
|
||||
current = inner.*;
|
||||
continue :descend;
|
||||
},
|
||||
.named => {
|
||||
const frame = &cache.pending.items[cache.pending.items.len - 1];
|
||||
if (frame.child(m)) |child| {
|
||||
current = child;
|
||||
current_depth += 1;
|
||||
continue :descend;
|
||||
}
|
||||
result = frame.total(m);
|
||||
_ = cache.pending.pop();
|
||||
try cache.values.put(cache.arena, key, result);
|
||||
},
|
||||
else => unreachable,
|
||||
}
|
||||
}
|
||||
} else result = scalarSize(current);
|
||||
while (cache.pending.items.len != 0) {
|
||||
const frame = &cache.pending.items[cache.pending.items.len - 1];
|
||||
switch (frame.ty) {
|
||||
.array => |a| result = if (result) |value| std.math.mul(u64, a.len, value) catch null else null,
|
||||
.atomic => {},
|
||||
.mutex => result = if (result) |value| std.math.add(u64, 8, value) catch null else null,
|
||||
.named => {
|
||||
const sum = if (result) |value| std.math.add(u64, frame.sum, value) catch null else null;
|
||||
if (sum) |value| {
|
||||
frame.sum = value;
|
||||
frame.next += 1;
|
||||
if (frame.child(m)) |child| {
|
||||
current = child;
|
||||
current_depth = frame.depth + 1;
|
||||
continue :descend;
|
||||
}
|
||||
result = frame.total(m);
|
||||
} else result = null;
|
||||
},
|
||||
else => unreachable,
|
||||
}
|
||||
const completed = cache.pending.pop().?;
|
||||
try cache.values.put(cache.arena, sizeKey(completed.ty, completed.depth).?, result);
|
||||
}
|
||||
return result;
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
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 {
|
||||
fn scalarSize(t: air.Type) 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,
|
||||
.iref, .own_array, .slice, .slice_mut => 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,
|
||||
.void, .never, .local => 0,
|
||||
.array, .atomic, .mutex, .named => unreachable,
|
||||
};
|
||||
}
|
||||
|
||||
// The allocation-free fallback remains an independent arithmetic oracle.
|
||||
// Only nominal members recurse; anonymous syntax is folded by accountedSize.
|
||||
fn uncachedBaseSize(m: air.Module, t: air.Type, depth: usize) ?u64 {
|
||||
return switch (t) {
|
||||
.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;
|
||||
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;
|
||||
var maximum: 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);
|
||||
for (v.payload) |p| sum = std.math.add(u64, sum, accountedSize(m, p, depth + 1) orelse return null) catch return null;
|
||||
maximum = @max(maximum, sum);
|
||||
}
|
||||
break :blk std.math.add(u64, tagSize(vs.len), max) catch null;
|
||||
break :blk std.math.add(u64, tagSize(vs.len), maximum) catch null;
|
||||
},
|
||||
},
|
||||
.void, .never, .local => 0,
|
||||
else => scalarSize(t),
|
||||
};
|
||||
}
|
||||
|
||||
@@ -163,23 +296,26 @@ const ValueSizes = struct {
|
||||
memo: *SizeMemo,
|
||||
reported: std.StringHashMapUnmanaged(void) = .empty,
|
||||
|
||||
fn visit(v: *ValueSizes, span: air.Span, t: air.Type) !void {
|
||||
fn visit(v: *ValueSizes, span: air.Span, first: 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
|
||||
var t = first;
|
||||
while (true) {
|
||||
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
|
||||
}
|
||||
}
|
||||
t = t.inner() orelse return;
|
||||
}
|
||||
if (t.inner()) |inner| try v.visit(span, inner);
|
||||
}
|
||||
};
|
||||
|
||||
@@ -298,3 +434,92 @@ test "cyclic nominal layouts through anonymous wrappers stay bounded" {
|
||||
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));
|
||||
}
|
||||
|
||||
test "deep anonymous sizes use bounded native stack and cached inner queries" {
|
||||
var scratch = std.heap.ArenaAllocator.init(std.testing.allocator);
|
||||
defer scratch.deinit();
|
||||
const a = scratch.allocator();
|
||||
const depth = 32_768;
|
||||
const rows = try a.alloc(air.Type.Array, depth);
|
||||
var t: air.Type = .{ .int = .u8 };
|
||||
for (rows) |*row| {
|
||||
row.* = .{ .elem = t, .len = 1 };
|
||||
t = .{ .array = row };
|
||||
}
|
||||
var m: air.Module = .{ .name = "deep", .format_version = 0, .lang = .{ 0, 1 }, .features = &.{}, .types = &.{}, .functions = &.{} };
|
||||
var memo: SizeMemo = .{ .arena = a };
|
||||
try std.testing.expectEqual(@as(?u64, 1), try memo.size(m, t, 0));
|
||||
try std.testing.expectEqual(@as(usize, depth), memo.values.count());
|
||||
try std.testing.expectEqual(@as(?u64, 1), accountedSize(m, t, 0));
|
||||
m.accounted_sizes = &memo.values;
|
||||
for (rows) |*row| try std.testing.expectEqual(@as(?u64, 1), accountedSize(m, .{ .array = row }, 0));
|
||||
}
|
||||
|
||||
test "size overflow preserves zero leaves and does not hide behind outer zero" {
|
||||
var scratch = std.heap.ArenaAllocator.init(std.testing.allocator);
|
||||
defer scratch.deinit();
|
||||
const a = scratch.allocator();
|
||||
const rows = try a.alloc(air.Type.Array, 64);
|
||||
var t: air.Type = .{ .named = 0 };
|
||||
for (rows) |*row| {
|
||||
row.* = .{ .elem = t, .len = 4_294_967_296 };
|
||||
t = .{ .array = row };
|
||||
}
|
||||
const m: air.Module = .{ .name = "zero", .format_version = 0, .lang = .{ 0, 1 }, .features = &.{}, .types = &.{.{ .name = "Empty", .span = .{ .line = 1, .col = 1 }, .kind = .{ .@"struct" = &.{} } }}, .functions = &.{} };
|
||||
var memo: SizeMemo = .{ .arena = a };
|
||||
try std.testing.expectEqual(@as(?u64, 0), try memo.size(m, t, 0));
|
||||
try std.testing.expectEqual(@as(?u64, 0), accountedSize(m, t, 0));
|
||||
rows[0].elem = .{ .int = .u64 };
|
||||
rows[63].len = 0; // Invalid syntax still cannot mask an inner overflow.
|
||||
var overflow: SizeMemo = .{ .arena = a };
|
||||
try std.testing.expectEqual(@as(?u64, null), try overflow.size(m, t, 0));
|
||||
try std.testing.expectEqual(@as(?u64, null), accountedSize(m, t, 0));
|
||||
}
|
||||
|
||||
test "deep nominal size queries use explicit continuations" {
|
||||
var scratch = std.heap.ArenaAllocator.init(std.testing.allocator);
|
||||
defer scratch.deinit();
|
||||
const a = scratch.allocator();
|
||||
const count = 32_768;
|
||||
const ds = try a.alloc(air.TypeDecl, count);
|
||||
for (ds, 0..) |*d, i| d.* = .{ .name = "N", .span = .{ .line = 1, .col = 1 }, .kind = .{ .@"struct" = try a.dupe(air_module.Field, &.{.{ .name = "x", .ty = if (i + 1 == count) .{ .int = .u8 } else .{ .named = @intCast(i + 1) } }}) } };
|
||||
const m: air.Module = .{ .name = "chain", .format_version = 0, .lang = .{ 0, 1 }, .features = &.{}, .types = ds, .functions = &.{} };
|
||||
var memo: SizeMemo = .{ .arena = a };
|
||||
try std.testing.expectEqual(@as(?u64, 1), try memo.size(m, .{ .named = 0 }, 0));
|
||||
try std.testing.expectEqual(@as(usize, count), memo.evaluations);
|
||||
}
|
||||
|
||||
test "overflowing nominal continuations cache null without revisiting children" {
|
||||
var scratch = std.heap.ArenaAllocator.init(std.testing.allocator);
|
||||
defer scratch.deinit();
|
||||
const a = scratch.allocator();
|
||||
const row: air.Type.Array = .{ .elem = .{ .int = .u64 }, .len = std.math.maxInt(u64) };
|
||||
const ds = [_]air.TypeDecl{
|
||||
.{ .name = "Outer", .span = .{ .line = 1, .col = 1 }, .kind = .{ .@"enum" = &.{.{ .name = "Value", .payload = &.{.{ .named = 1 }} }} } },
|
||||
.{ .name = "Inner", .span = .{ .line = 1, .col = 1 }, .kind = .{ .@"struct" = &.{.{ .name = "x", .ty = .{ .array = &row } }} } },
|
||||
};
|
||||
const m: air.Module = .{ .name = "overflow", .format_version = 0, .lang = .{ 0, 1 }, .features = &.{}, .types = &ds, .functions = &.{} };
|
||||
var memo: SizeMemo = .{ .arena = a };
|
||||
try std.testing.expectEqual(@as(?u64, null), try memo.size(m, .{ .named = 0 }, 0));
|
||||
try std.testing.expect(memo.values.contains(.{ .index = 0, .depth = 0 }));
|
||||
try std.testing.expect(memo.values.contains(.{ .index = 1, .depth = 1 }));
|
||||
const before = memo.evaluations;
|
||||
try std.testing.expectEqual(@as(?u64, null), try memo.size(m, .{ .named = 0 }, 0));
|
||||
try std.testing.expectEqual(before, memo.evaluations);
|
||||
}
|
||||
|
||||
test "anonymous cache identities distinguish shared atomic and mutex content" {
|
||||
var scratch = std.heap.ArenaAllocator.init(std.testing.allocator);
|
||||
defer scratch.deinit();
|
||||
const a = scratch.allocator();
|
||||
const leaf: air.Type = .{ .int = .u64 };
|
||||
const atom: air.Type = .{ .atomic = &leaf };
|
||||
const lock: air.Type = .{ .mutex = &leaf };
|
||||
var m: air.Module = .{ .name = "cache", .format_version = 0, .lang = .{ 0, 1 }, .features = &.{}, .types = &.{}, .functions = &.{} };
|
||||
var memo: SizeMemo = .{ .arena = a };
|
||||
try std.testing.expectEqual(@as(?u64, 8), try memo.size(m, atom, 0));
|
||||
try std.testing.expectEqual(@as(?u64, 16), try memo.size(m, lock, 0));
|
||||
m.accounted_sizes = &memo.values;
|
||||
try std.testing.expectEqual(@as(?u64, 8), accountedSize(m, atom, 0));
|
||||
try std.testing.expectEqual(@as(?u64, 16), accountedSize(m, lock, 0));
|
||||
}
|
||||
Reference in new issue
Block a user