verifier: cache structural properties and nominal size queries
This commit is contained in:
1 parent
41f3afbf05
commit
48b9b42fed
31 files changed
+1399
-166
No files matched your search
@@ -36,6 +36,7 @@ pub const WaitTarget = module.WaitTarget;
|
||||
pub const parse = @import("air/parse.zig");
|
||||
pub const print = @import("air/print.zig");
|
||||
pub const features = @import("air/features.zig");
|
||||
pub const properties = @import("air/properties.zig");
|
||||
pub const authority = module.authority;
|
||||
|
||||
test {
|
||||
@@ -43,5 +44,6 @@ test {
|
||||
_ = parse;
|
||||
_ = print;
|
||||
_ = features;
|
||||
_ = properties;
|
||||
_ = authority;
|
||||
}
|
||||
+26
-1
@@ -473,6 +473,27 @@ pub const Function = struct {
|
||||
values: []const ValueInfo,
|
||||
};
|
||||
|
||||
/// Implementation cache only; neither printed nor part of AIR format 0.
|
||||
pub const NominalProperties = packed struct(u32) {
|
||||
linear: bool = false,
|
||||
copy: bool = true,
|
||||
send: bool = true,
|
||||
share: bool = true,
|
||||
island_safe: bool = true,
|
||||
local: bool = false,
|
||||
forbidden: bool = false,
|
||||
endpoint: bool = false,
|
||||
zeroable: bool = true,
|
||||
/// Maximum visited-node count, saturated at depth limit + 1.
|
||||
height: u7 = 1,
|
||||
/// Nearest forbidden source-element leaf, saturated past depth 32.
|
||||
source_forbidden_depth: u6 = 34,
|
||||
padding: u10 = 0,
|
||||
};
|
||||
|
||||
pub const SizeKey = struct { index: u32, depth: usize };
|
||||
pub const SizeCache = std.AutoHashMapUnmanaged(SizeKey, ?u64);
|
||||
|
||||
pub const Module = struct {
|
||||
name: []const u8,
|
||||
format_version: u32,
|
||||
@@ -480,6 +501,10 @@ pub const Module = struct {
|
||||
features: []const []const u8,
|
||||
types: []const TypeDecl,
|
||||
functions: []const Function,
|
||||
/// Immutable implementation caches. Replacing types invalidates both;
|
||||
/// verification always rebuilds them, regardless of caller contents.
|
||||
nominal_properties: ?[]const NominalProperties = null,
|
||||
accounted_sizes: ?*const SizeCache = null,
|
||||
|
||||
pub fn declares(m: Module, feature: []const u8) bool {
|
||||
for (m.features) |f| if (std.mem.eql(u8, f, feature)) return true;
|
||||
@@ -491,7 +516,7 @@ pub const Module = struct {
|
||||
if (t.isLinear()) return true;
|
||||
return switch (t) {
|
||||
.array => |a| m.isLinearType(a.elem),
|
||||
.named => |i| switch (m.types[i].kind) {
|
||||
.named => |i| if (m.nominal_properties) |p| p[i].linear else switch (m.types[i].kind) {
|
||||
.@"struct" => |fs| for (fs) |f| {
|
||||
if (m.isLinearType(f.ty)) break true;
|
||||
} else false,
|
||||
|
||||
@@ -0,0 +1,347 @@
|
||||
//! Cached structural properties of resolved AIR declarations (spec 02/03/04).
|
||||
const std = @import("std");
|
||||
const air = @import("module.zig");
|
||||
const Type = air.Type;
|
||||
|
||||
pub const Stats = struct { evaluations: usize = 0, fields: usize = 0 };
|
||||
|
||||
/// Resolve shared nominal subgraphs once per changed fact, including cycles.
|
||||
/// All predicates are monotone: existential facts grow from false, universal
|
||||
/// facts shrink from true. Height grows to 65, preserving the depth-64 guard.
|
||||
/// No caller-supplied cache is trusted. Declarations are immutable thereafter.
|
||||
pub fn prepare(a: std.mem.Allocator, input: air.Module, stats: ?*Stats) !air.Module {
|
||||
const entries = try a.alloc(air.NominalProperties, input.types.len);
|
||||
@memset(entries, .{});
|
||||
var m = input;
|
||||
m.nominal_properties = entries;
|
||||
const reverse = try a.alloc(std.ArrayList(u32), input.types.len);
|
||||
@memset(reverse, .empty);
|
||||
for (m.types, 0..) |d, i| switch (d.kind) {
|
||||
.@"struct" => |fs| for (fs) |f| try dependencies(a, reverse, @intCast(i), f.ty),
|
||||
.@"enum" => |vs| for (vs) |v| for (v.payload) |t| try dependencies(a, reverse, @intCast(i), t),
|
||||
};
|
||||
const queue = try a.alloc(u32, m.types.len);
|
||||
const queued = try a.alloc(bool, m.types.len);
|
||||
@memset(queued, true);
|
||||
for (queue, 0..) |*slot, i| slot.* = @intCast(queue.len - 1 - i);
|
||||
var head: usize = 0;
|
||||
var tail: usize = 0;
|
||||
var count = queue.len;
|
||||
while (count != 0) {
|
||||
const i = queue[head];
|
||||
head = (head + 1) % queue.len;
|
||||
count -= 1;
|
||||
queued[i] = false;
|
||||
if (stats) |s| s.evaluations += 1;
|
||||
var next: air.NominalProperties = .{};
|
||||
switch (m.types[i].kind) {
|
||||
.@"struct" => |fs| for (fs) |f| accumulate(m, &next, f.ty, stats),
|
||||
.@"enum" => |vs| {
|
||||
next.zeroable = false;
|
||||
for (vs) |v| for (v.payload) |t| accumulate(m, &next, t, stats);
|
||||
},
|
||||
}
|
||||
if (entries[i] == next) continue;
|
||||
entries[i] = next;
|
||||
for (reverse[i].items) |parent| {
|
||||
if (queued[parent]) continue;
|
||||
queue[tail] = parent;
|
||||
tail = (tail + 1) % queue.len;
|
||||
count += 1;
|
||||
queued[parent] = true;
|
||||
}
|
||||
}
|
||||
return m;
|
||||
}
|
||||
|
||||
// Include phantom edges for height. This is a dependency superset for the
|
||||
// boolean predicates, whose unchanged tag rules decide which inners matter.
|
||||
fn dependencies(a: std.mem.Allocator, reverse: []std.ArrayList(u32), parent: u32, t: Type) !void {
|
||||
if (t == .named) {
|
||||
try reverse[t.named].append(a, parent);
|
||||
} else if (t.inner()) |inner| try dependencies(a, reverse, parent, inner);
|
||||
}
|
||||
|
||||
fn accumulate(m: air.Module, out: *air.NominalProperties, t: Type, stats: ?*Stats) void {
|
||||
if (stats) |s| s.fields += 1;
|
||||
out.linear = out.linear or m.isLinearType(t);
|
||||
out.copy = out.copy and isCopy(m, t);
|
||||
out.send = out.send and isSend(m, t);
|
||||
out.share = out.share and isShare(m, t);
|
||||
out.island_safe = out.island_safe and isIslandSafe(m, t);
|
||||
out.local = out.local or isLocalType(m, t);
|
||||
out.forbidden = out.forbidden or contains(m, t, .forbidden);
|
||||
out.endpoint = out.endpoint or contains(m, t, .endpoint);
|
||||
out.zeroable = out.zeroable and isZeroable(m, t);
|
||||
out.height = @max(out.height, @min(65, @as(u8, height(m, t)) + 1));
|
||||
out.source_forbidden_depth = @min(out.source_forbidden_depth, @min(34, @as(u8, sourceForbiddenDepth(m, t)) + 1));
|
||||
}
|
||||
|
||||
/// Match the frontend's existing element edges and depth-32 witness guard.
|
||||
/// A nearest-leaf distance permits pruning without changing field order.
|
||||
pub fn sourceForbiddenDepth(m: air.Module, t: Type) u6 {
|
||||
return switch (t) {
|
||||
.ref, .mut, .slice, .slice_mut, .scope, .task, .shield, .guard => 0,
|
||||
.array, .own, .own_array, .buffer, .mutex => @intCast(@min(34, @as(u8, sourceForbiddenDepth(m, t.inner().?)) + 1)),
|
||||
.named => |i| m.nominal_properties.?[i].source_forbidden_depth,
|
||||
else => 34,
|
||||
};
|
||||
}
|
||||
|
||||
fn height(m: air.Module, t: Type) u7 {
|
||||
if (t == .named) return m.nominal_properties.?[t.named].height;
|
||||
return if (t.inner()) |inner| @intCast(@min(65, @as(u8, height(m, inner)) + 1)) else 1;
|
||||
}
|
||||
|
||||
/// The preceding depth guard, with cached nominal height and identical edges.
|
||||
pub fn bounded(m: air.Module, t: Type, depth: usize) bool {
|
||||
if (depth >= 64) return false;
|
||||
if (t == .named) {
|
||||
if (m.nominal_properties) |_| return height(m, t) <= 64 - depth;
|
||||
return switch (m.types[t.named].kind) {
|
||||
.@"struct" => |fs| for (fs) |f| {
|
||||
if (!bounded(m, f.ty, depth + 1)) break false;
|
||||
} else true,
|
||||
.@"enum" => |vs| blk: {
|
||||
for (vs) |v| for (v.payload) |p| if (!bounded(m, p, depth + 1)) break :blk false;
|
||||
break :blk true;
|
||||
},
|
||||
};
|
||||
}
|
||||
return if (t.inner()) |inner| bounded(m, inner, depth + 1) else true;
|
||||
}
|
||||
|
||||
pub fn isZeroable(m: air.Module, t: Type) bool {
|
||||
return switch (t) {
|
||||
.int, .bool => true,
|
||||
.array => |a| isZeroable(m, a.elem),
|
||||
.named => |i| if (m.nominal_properties) |p| p[i].zeroable else switch (m.types[i].kind) {
|
||||
.@"struct" => |fs| for (fs) |f| {
|
||||
if (!isZeroable(m, f.ty)) break false;
|
||||
} else true,
|
||||
.@"enum" => false,
|
||||
},
|
||||
else => false,
|
||||
};
|
||||
}
|
||||
|
||||
pub fn isCopy(m: air.Module, t: Type) bool {
|
||||
return switch (t) {
|
||||
.int, .bool, .builtin, .cap, .iref => true,
|
||||
.array => |a| isCopy(m, a.elem),
|
||||
.named => |i| if (m.nominal_properties) |p| p[i].copy else aggregateAll(m, i, isCopy),
|
||||
else => false,
|
||||
};
|
||||
}
|
||||
|
||||
pub fn isLocalType(m: air.Module, t: Type) bool {
|
||||
return switch (t) {
|
||||
.local, .task, .scope, .shield, .guard => true,
|
||||
.ref, .mut, .slice, .slice_mut, .own, .own_array, .buffer, .mutex => |inner| isLocalType(m, inner.*),
|
||||
.array => |a| isLocalType(m, a.elem),
|
||||
.named => |i| if (m.nominal_properties) |p| p[i].local else switch (m.types[i].kind) {
|
||||
.@"struct" => |fs| for (fs) |f| {
|
||||
if (isLocalType(m, f.ty)) break true;
|
||||
} else false,
|
||||
.@"enum" => |vs| blk: {
|
||||
for (vs) |v| for (v.payload) |p| if (isLocalType(m, p)) break :blk true;
|
||||
break :blk false;
|
||||
},
|
||||
},
|
||||
else => false,
|
||||
};
|
||||
}
|
||||
|
||||
pub fn isSend(m: air.Module, t: Type) bool {
|
||||
return switch (t) {
|
||||
.int, .bool, .builtin, .cap, .resource, .region, .sender, .receiver => true,
|
||||
// Graph contents are IslandSafe plain data (spec 03 §22–§24).
|
||||
.island, .frozen, .arc, .iref => true,
|
||||
// Every access to a cell is an atomic operation (spec 04 §23).
|
||||
.atomic => true,
|
||||
.own, .own_array, .buffer, .mutex => |inner| isSend(m, inner.*),
|
||||
.ref, .slice => |inner| isShare(m, inner.*),
|
||||
.mut, .slice_mut => |inner| isSend(m, inner.*),
|
||||
.array => |a| isSend(m, a.elem),
|
||||
.named => |i| if (m.nominal_properties) |p| p[i].send else aggregateAll(m, i, isSend),
|
||||
.scope, .task, .shield, .guard, .local, .void, .never => false,
|
||||
};
|
||||
}
|
||||
|
||||
pub fn isShare(m: air.Module, t: Type) bool {
|
||||
return switch (t) {
|
||||
.int, .bool, .builtin, .cap => true,
|
||||
.frozen, .arc, .iref, .atomic => true,
|
||||
// Its owner may mutate it: freeze to share (spec 04 §35).
|
||||
.island => false,
|
||||
.resource, .region, .mut, .slice_mut, .scope, .task, .shield, .guard, .sender, .receiver, .local, .void, .never => false,
|
||||
.own, .own_array, .buffer, .ref, .slice => |inner| isShare(m, inner.*),
|
||||
// The lock synchronizes access (spec 04 §35, §40).
|
||||
.mutex => |inner| isSend(m, inner.*),
|
||||
.array => |a| isShare(m, a.elem),
|
||||
.named => |i| if (m.nominal_properties) |p| p[i].share else aggregateAll(m, i, isShare),
|
||||
};
|
||||
}
|
||||
|
||||
pub fn isIslandSafe(m: air.Module, t: Type) bool {
|
||||
return switch (t) {
|
||||
.int, .bool, .builtin, .iref => true,
|
||||
.array => |a| isIslandSafe(m, a.elem),
|
||||
.named => |i| if (m.nominal_properties) |p| p[i].island_safe else aggregateAll(m, i, isIslandSafe),
|
||||
else => false,
|
||||
};
|
||||
}
|
||||
|
||||
pub fn aggregateAll(m: air.Module, i: u32, comptime pred: fn (air.Module, Type) bool) bool {
|
||||
return switch (m.types[i].kind) {
|
||||
.@"struct" => |fs| for (fs) |f| {
|
||||
if (!pred(m, f.ty)) break false;
|
||||
} else true,
|
||||
.@"enum" => |vs| {
|
||||
for (vs) |v| for (v.payload) |p| if (!pred(m, p)) return false;
|
||||
return true;
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
pub fn contains(m: air.Module, t: Type, comptime what: enum { endpoint, forbidden }) bool {
|
||||
const here = switch (what) {
|
||||
.endpoint => t.isEndpoint(),
|
||||
.forbidden => t.isBorrow() or t.isTaskBound() or t == .guard,
|
||||
};
|
||||
if (here) return true;
|
||||
// An `iref<T>` holds no `T`: types may recurse through it (spec 03 §22).
|
||||
if (t == .iref) return false;
|
||||
if (t.inner()) |inner| if (t != .ref and t != .mut and t != .slice and t != .slice_mut) return contains(m, inner, what);
|
||||
return switch (t) {
|
||||
.named => |i| if (m.nominal_properties) |p| @field(p[i], @tagName(what)) else switch (m.types[i].kind) {
|
||||
.@"struct" => |fs| for (fs) |f| {
|
||||
if (contains(m, f.ty, what)) break true;
|
||||
} else false,
|
||||
.@"enum" => |vs| for (vs) |v| {
|
||||
for (v.payload) |p| if (contains(m, p, what)) return true;
|
||||
} else false,
|
||||
},
|
||||
else => false,
|
||||
};
|
||||
}
|
||||
|
||||
test "shared nominal diamonds have counted bounded property queries" {
|
||||
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| {
|
||||
const fields = try a.alloc(air.Field, if (i == 64) 1 else 2);
|
||||
for (fields) |*f| f.* = .{ .name = "x", .ty = if (i == 64) .{ .int = .u64 } else .{ .named = @intCast(i + 1) } };
|
||||
d.* = .{ .name = "N", .span = .{ .line = 1, .col = 1 }, .kind = .{ .@"struct" = fields } };
|
||||
}
|
||||
const m: air.Module = .{ .name = "test", .format_version = 0, .lang = .{ 0, 1 }, .features = &.{}, .types = ds, .functions = &.{} };
|
||||
var stats: Stats = .{};
|
||||
const cached = try prepare(a, m, &stats);
|
||||
try std.testing.expectEqual(@as(usize, 65), stats.evaluations);
|
||||
try std.testing.expectEqual(@as(usize, 129), stats.fields);
|
||||
for (0..65) |i| {
|
||||
const t: Type = .{ .named = @intCast(i) };
|
||||
try std.testing.expect(!cached.isLinearType(t));
|
||||
try std.testing.expect(isCopy(cached, t) and isSend(cached, t) and isShare(cached, t) and isIslandSafe(cached, t) and isZeroable(cached, t));
|
||||
try std.testing.expect(!isLocalType(cached, t) and !contains(cached, t, .endpoint) and !contains(cached, t, .forbidden));
|
||||
}
|
||||
try std.testing.expect(!bounded(cached, .{ .named = 0 }, 0));
|
||||
try std.testing.expect(!bounded(cached, .{ .named = 1 }, 0));
|
||||
try std.testing.expect(bounded(cached, .{ .named = 2 }, 0));
|
||||
}
|
||||
|
||||
test "cyclic property work terminates and phantom handles stay Copy" {
|
||||
var scratch = std.heap.ArenaAllocator.init(std.testing.allocator);
|
||||
defer scratch.deinit();
|
||||
const a = scratch.allocator();
|
||||
const self: Type = .{ .named = 0 };
|
||||
const phantom: Type = .{ .iref = &self };
|
||||
const m: air.Module = .{ .name = "test", .format_version = 0, .lang = .{ 0, 1 }, .features = &.{}, .types = &.{.{ .name = "Node", .span = .{ .line = 1, .col = 1 }, .kind = .{ .@"struct" = &.{.{ .name = "next", .ty = phantom }} } }}, .functions = &.{} };
|
||||
var stats: Stats = .{};
|
||||
const cached = try prepare(a, m, &stats);
|
||||
try std.testing.expect(stats.evaluations <= 65);
|
||||
try std.testing.expect(isCopy(cached, self) and isIslandSafe(cached, self));
|
||||
try std.testing.expect(!cached.isLinearType(self) and !contains(cached, self, .forbidden));
|
||||
try std.testing.expect(!bounded(cached, self, 0));
|
||||
}
|
||||
|
||||
test "cached predicates match uncached derivations on mixed acyclic graphs" {
|
||||
for (0..256) |seed| {
|
||||
var scratch = std.heap.ArenaAllocator.init(std.testing.allocator);
|
||||
defer scratch.deinit();
|
||||
const a = scratch.allocator();
|
||||
const ds = try a.alloc(air.TypeDecl, 4);
|
||||
for (ds, 0..) |*d, i| {
|
||||
const child = try a.create(Type);
|
||||
child.* = if (i == 3) .{ .int = .u64 } else .{ .named = @intCast(i + 1) };
|
||||
const array = try a.create(Type.Array);
|
||||
array.* = .{ .elem = child.*, .len = 2 };
|
||||
const choices = [_]Type{ child.*, .bool, .void, .region, .scope, .{ .own = child }, .{ .mut = child }, .{ .ref = child }, .{ .mutex = child }, .{ .iref = child }, .{ .task = child }, .{ .sender = child }, .{ .array = array }, .{ .island = child }, .{ .arc = child }, .{ .buffer = child } };
|
||||
const x = choices[(seed + i * 7) % choices.len];
|
||||
const y = choices[(seed / choices.len + i * 3) % choices.len];
|
||||
const fields = try a.dupe(air.Field, &.{ .{ .name = "a", .ty = x }, .{ .name = "b", .ty = y } });
|
||||
const vs = try a.alloc(air.Variant, 2);
|
||||
vs[0] = .{ .name = "A", .payload = try a.dupe(Type, &.{x}) };
|
||||
vs[1] = .{ .name = "B", .payload = try a.dupe(Type, &.{y}) };
|
||||
d.* = .{ .name = "N", .span = .{ .line = 1, .col = 1 }, .kind = if ((seed + i) % 3 == 0) .{ .@"enum" = vs } else .{ .@"struct" = fields } };
|
||||
}
|
||||
const m: air.Module = .{ .name = "test", .format_version = 0, .lang = .{ 0, 1 }, .features = &.{}, .types = ds, .functions = &.{} };
|
||||
const cached = try prepare(a, m, null);
|
||||
for (0..4) |i| {
|
||||
const t: Type = .{ .named = @intCast(i) };
|
||||
try std.testing.expectEqual(m.isLinearType(t), cached.isLinearType(t));
|
||||
inline for (.{ isCopy, isSend, isShare, isIslandSafe, isLocalType, isZeroable }) |pred| try std.testing.expectEqual(pred(m, t), pred(cached, t));
|
||||
inline for (.{ .endpoint, .forbidden }) |what| try std.testing.expectEqual(contains(m, t, what), contains(cached, t, what));
|
||||
for ([_]usize{ 0, 31, 63, 64 }) |depth| try std.testing.expectEqual(bounded(m, t, depth), bounded(cached, t, depth));
|
||||
for ([_]u32{ 0, 30, 31, 32, 33 }) |depth| {
|
||||
const before = witness(m, t, depth, false);
|
||||
const after = witness(cached, t, depth, true);
|
||||
try std.testing.expectEqual(before == null, after == null);
|
||||
if (before) |b| try std.testing.expect(b.eql(after.?));
|
||||
}
|
||||
var memo: @import("../verifier/budget.zig").SizeMemo = .{ .arena = a };
|
||||
for ([_]usize{ 0, 1, 5, 6 }) |depth| try std.testing.expectEqual(@import("../verifier/budget.zig").accountedSize(m, t, depth), try memo.size(m, t, depth));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// The preceding frontend witness rule, independently applied to AIR types.
|
||||
fn witness(m: air.Module, t: Type, depth: u32, cached: bool) ?Type {
|
||||
if (depth > 32) return null;
|
||||
if (cached and t == .named and sourceForbiddenDepth(m, t) > 32 - depth) return null;
|
||||
return switch (t) {
|
||||
.ref, .mut, .slice, .slice_mut, .scope, .task, .shield, .guard => t,
|
||||
.array, .own, .own_array, .buffer, .mutex => witness(m, t.inner().?, depth + 1, cached),
|
||||
.named => |i| switch (m.types[i].kind) {
|
||||
.@"struct" => |fs| for (fs) |f| {
|
||||
if (witness(m, f.ty, depth + 1, cached)) |bad| break bad;
|
||||
} else null,
|
||||
.@"enum" => |vs| blk: {
|
||||
for (vs) |v| for (v.payload) |p| if (witness(m, p, depth + 1, cached)) |bad| break :blk bad;
|
||||
break :blk null;
|
||||
},
|
||||
},
|
||||
else => null,
|
||||
};
|
||||
}
|
||||
|
||||
test "cyclic existential facts reach all predecessors without a false cache" {
|
||||
var scratch = std.heap.ArenaAllocator.init(std.testing.allocator);
|
||||
defer scratch.deinit();
|
||||
const a = scratch.allocator();
|
||||
const u: Type = .{ .int = .u64 };
|
||||
const m: air.Module = .{ .name = "test", .format_version = 0, .lang = .{ 0, 1 }, .features = &.{}, .types = &.{
|
||||
.{ .name = "A", .span = .{ .line = 1, .col = 1 }, .kind = .{ .@"struct" = &.{.{ .name = "b", .ty = .{ .named = 1 } }} } },
|
||||
.{ .name = "B", .span = .{ .line = 2, .col = 1 }, .kind = .{ .@"struct" = &.{ .{ .name = "a", .ty = .{ .named = 0 } }, .{ .name = "owner", .ty = .{ .own = &u } }, .{ .name = "borrow", .ty = .{ .mut = &u } } } } },
|
||||
}, .functions = &.{} };
|
||||
var stats: Stats = .{};
|
||||
const cached = try prepare(a, m, &stats);
|
||||
try std.testing.expect(stats.evaluations < 150);
|
||||
for (0..2) |i| {
|
||||
const t: Type = .{ .named = @intCast(i) };
|
||||
try std.testing.expect(cached.isLinearType(t) and contains(cached, t, .forbidden));
|
||||
try std.testing.expect(!isCopy(cached, t) and !isShare(cached, t));
|
||||
}
|
||||
}
|
||||
+3
-3
@@ -91,9 +91,9 @@ pub fn checkAir(arena: std.mem.Allocator, file_name: []const u8, source: []const
|
||||
const parsed = try air.parse.parse(arena, file_name, source);
|
||||
if (parsed.diagnostics.len != 0) return .{ .status = .rejected, .diagnostics = parsed.diagnostics };
|
||||
const module = parsed.module.?;
|
||||
const ds = try verifier.verify(arena, file_name, module);
|
||||
if (ds.len != 0) return .{ .status = .rejected, .diagnostics = ds };
|
||||
return .{ .status = .accepted, .diagnostics = &.{}, .module = module };
|
||||
const verified = try verifier.verifyModule(arena, file_name, module);
|
||||
if (verified.diagnostics.len != 0) return .{ .status = .rejected, .diagnostics = verified.diagnostics };
|
||||
return .{ .status = .accepted, .diagnostics = &.{}, .module = verified.module };
|
||||
}
|
||||
|
||||
test "malformed lowered AIR is an internal compiler error" {
|
||||
|
||||
+51
-42
@@ -30,6 +30,7 @@ const parse_mod = @import("parse.zig");
|
||||
const authority = @import("../air/authority.zig");
|
||||
const features_mod = @import("../air/features.zig");
|
||||
const air_module = @import("../air/module.zig");
|
||||
const properties = @import("../air/properties.zig");
|
||||
const verifier_types = @import("../verifier/types.zig");
|
||||
const type_positions = @import("type_positions.zig");
|
||||
const type_properties = @import("type_properties.zig");
|
||||
@@ -328,12 +329,9 @@ const Lowerer = struct {
|
||||
linear_enabled: bool = false,
|
||||
/// Complete declaration snapshot, with finite layouts checked before bodies.
|
||||
property_module: ?air_module.Module = null,
|
||||
/// Count witness visits in unit tests, never used to accept/reject source.
|
||||
property_visits: usize = 0,
|
||||
|
||||
fn probeType(l: *Lowerer, t: ast.Type) ?Ty {
|
||||
const previous = l.problem;
|
||||
defer l.problem = previous;
|
||||
return l.typeOf(t) catch null;
|
||||
}
|
||||
fn needsLinearType(l: *Lowerer, t: Ty, depth: usize) bool {
|
||||
if (depth > l.m.types.len + 32) return false;
|
||||
if (t == .named) return l.isLinear(t);
|
||||
@@ -344,23 +342,12 @@ const Lowerer = struct {
|
||||
return false;
|
||||
}
|
||||
fn isLinear(l: *Lowerer, t: Ty) bool {
|
||||
return l.linearAt(t, 0);
|
||||
}
|
||||
|
||||
fn linearAt(l: *Lowerer, t: Ty, depth: usize) bool {
|
||||
// Signatures/declarations resolve before checkRequirements; all
|
||||
// structural ownership queries run after its immutable snapshot.
|
||||
if (t.isLinear()) return true;
|
||||
if (depth > l.m.types.len + 32) return false;
|
||||
return switch (t) {
|
||||
.array => |a| l.linearAt(a.elem, depth + 1),
|
||||
.named => |i| switch (l.m.types[i].kind) {
|
||||
.@"struct" => |fs| for (fs) |f| {
|
||||
if (l.probeType(f.ty)) |ty| if (l.linearAt(ty, depth + 1)) break true;
|
||||
} else false,
|
||||
.@"enum" => |vs| blk: {
|
||||
for (vs) |v| for (v.payload) |p| if (l.probeType(p)) |ty| if (l.linearAt(ty, depth + 1)) break :blk true;
|
||||
break :blk false;
|
||||
},
|
||||
},
|
||||
.array => |a| l.isLinear(a.elem),
|
||||
.named => |i| l.property_module.?.nominal_properties.?[i].linear,
|
||||
else => false,
|
||||
};
|
||||
}
|
||||
@@ -553,9 +540,6 @@ const Lowerer = struct {
|
||||
},
|
||||
}
|
||||
}
|
||||
for (l.m.types, 0..) |_, i| if (l.isLinear(.{ .named = @intCast(i) })) {
|
||||
l.linear_enabled = true;
|
||||
};
|
||||
const m: air_module.Module = .{ .name = l.m.name, .format_version = 0, .lang = .{ 0, 1 }, .features = &.{"linear_aggregates"}, .types = types, .functions = &.{} };
|
||||
var layout_context: @import("../verifier.zig").Context = .{ .arena = l.arena, .file = "<generic>", .module = m };
|
||||
try verifier_types.checkFiniteLayouts(&layout_context);
|
||||
@@ -563,18 +547,22 @@ const Lowerer = struct {
|
||||
const d = l.m.types[layout_context.diags.items[0].span.?.line - 1];
|
||||
return l.fail("E1003", d.span, "aggregate type contains itself by value", &.{.{ .name = "type", .value = d.display_name orelse d.name }});
|
||||
}
|
||||
l.property_module = m;
|
||||
l.property_module = try properties.prepare(l.arena, m, null);
|
||||
for (l.m.types, 0..) |_, i| if (l.isLinear(.{ .named = @intCast(i) })) {
|
||||
l.linear_enabled = true;
|
||||
};
|
||||
for (l.m.requirements) |r| {
|
||||
const ty = try l.typeOf(r.ty);
|
||||
const t = try l.airType(ty);
|
||||
if (!boundedType(m, t, 0)) return l.invalid(.implementation_limit, r.span, "constraint type traversal is cyclic or exceeds depth 64", &.{});
|
||||
if (!properties.bounded(l.property_module.?, t, 0)) return l.invalid(.implementation_limit, r.span, "constraint type traversal is cyclic or exceeds depth 64", &.{});
|
||||
if (r.property.len == 0) {
|
||||
var context: @import("../verifier.zig").Context = .{ .arena = l.arena, .file = "<generic>", .module = m };
|
||||
var context: @import("../verifier.zig").Context = .{ .arena = l.arena, .file = "<generic>", .module = l.property_module.? };
|
||||
try verifier_types.checkTypeArgument(&context, .{ .line = 1, .col = 1 }, t);
|
||||
if (context.diags.items.len != 0) return l.fail("E7005", r.span, "generic argument is not a well-formed concrete type", &.{ .{ .name = "parameter", .value = r.parameter }, .{ .name = "found", .value = l.diagnosticTyText(ty) } });
|
||||
continue;
|
||||
}
|
||||
const ok = if (std.mem.eql(u8, r.property, "Copy")) verifier_types.isCopy(m, t) else if (std.mem.eql(u8, r.property, "Send")) verifier_types.isSend(m, t) else if (std.mem.eql(u8, r.property, "Share")) verifier_types.isShare(m, t) else if (std.mem.eql(u8, r.property, "Freeze")) verifier_types.isIslandSafe(m, t) else verifier_types.isLocalType(m, t);
|
||||
const cached = l.property_module.?;
|
||||
const ok = if (std.mem.eql(u8, r.property, "Copy")) verifier_types.isCopy(cached, t) else if (std.mem.eql(u8, r.property, "Send")) verifier_types.isSend(cached, t) else if (std.mem.eql(u8, r.property, "Share")) verifier_types.isShare(cached, t) else if (std.mem.eql(u8, r.property, "Freeze")) verifier_types.isIslandSafe(cached, t) else verifier_types.isLocalType(cached, t);
|
||||
if (!ok) return l.fail("E7005", r.span, "generic argument does not satisfy structural constraint", &.{
|
||||
.{ .name = "parameter", .value = r.parameter },
|
||||
.{ .name = "constraint", .value = r.property },
|
||||
@@ -3123,7 +3111,9 @@ const Lowerer = struct {
|
||||
}
|
||||
|
||||
fn forbiddenIn(l: *Lowerer, t: Ty, depth: u32) Error!?Ty {
|
||||
if (@import("builtin").is_test) l.property_visits += 1;
|
||||
if (depth > 32) return null;
|
||||
if (t == .named and properties.sourceForbiddenDepth(l.property_module.?, .{ .named = t.named }) > 32 - depth) return null;
|
||||
return switch (t) {
|
||||
.ref, .mut, .slice, .slice_mut, .scope, .task, .shield, .guard => t,
|
||||
.array => |a| l.forbiddenIn(a.elem, depth + 1),
|
||||
@@ -4026,21 +4016,6 @@ fn expressionPath(arena: Allocator, e: ast.Expr) Allocator.Error!?[]const u8 {
|
||||
};
|
||||
}
|
||||
|
||||
// Bound property queries before invoking recursive verifier derivations.
|
||||
fn boundedType(m: air_module.Module, t: air_module.Type, depth: usize) bool {
|
||||
if (depth >= 64) return false;
|
||||
if (t == .named) return switch (m.types[t.named].kind) {
|
||||
.@"struct" => |fs| for (fs) |f| {
|
||||
if (!boundedType(m, f.ty, depth + 1)) break false;
|
||||
} else true,
|
||||
.@"enum" => |vs| blk: {
|
||||
for (vs) |v| for (v.payload) |p| if (!boundedType(m, p, depth + 1)) break :blk false;
|
||||
break :blk true;
|
||||
},
|
||||
};
|
||||
return if (t.inner()) |inner| boundedType(m, inner, depth + 1) else true;
|
||||
}
|
||||
|
||||
test "terminated blocks reject further emission with a source diagnostic" {
|
||||
var scratch = std.heap.ArenaAllocator.init(std.testing.allocator);
|
||||
defer scratch.deinit();
|
||||
@@ -4055,3 +4030,37 @@ test "terminated blocks reject further emission with a source diagnostic" {
|
||||
try std.testing.expectEqual(@as(usize, 1), l.blocks.items[0].insts.items.len);
|
||||
try std.testing.expect(l.blocks.items[0].terminated);
|
||||
}
|
||||
|
||||
test "source property queries prune deep diamonds without changing witnesses" {
|
||||
var scratch = std.heap.ArenaAllocator.init(std.testing.allocator);
|
||||
defer scratch.deinit();
|
||||
const a = scratch.allocator();
|
||||
var text: std.ArrayList(u8) = .empty;
|
||||
try text.appendSlice(a, "module t\n");
|
||||
for (0..41) |i| try text.print(a, "struct N{d} {{ a: N{d}, b: N{d} }}\n", .{ i, i + 1, i + 1 });
|
||||
try text.appendSlice(a, "struct N41 { a: ref<u64>, b: task<u64> }\nfn main() -> void effects [] {}\n");
|
||||
var l: Lowerer = .{ .arena = a, .m = (try parse_mod.parse(a, text.items)).ok };
|
||||
try l.checkRequirements();
|
||||
try std.testing.expect(l.isLinear(.{ .named = 0 }));
|
||||
try std.testing.expectEqual(@as(?Ty, null), try l.forbiddenIn(.{ .named = 0 }, 0));
|
||||
try std.testing.expectEqual(@as(usize, 1), l.property_visits);
|
||||
l.property_visits = 0;
|
||||
const bad = (try l.forbiddenIn(.{ .named = 10 }, 0)).?;
|
||||
try std.testing.expect(bad == .ref and bad.inner().? == .int);
|
||||
try std.testing.expect(l.property_visits <= 33);
|
||||
}
|
||||
|
||||
test "cyclic source diamonds reject before property construction" {
|
||||
var scratch = std.heap.ArenaAllocator.init(std.testing.allocator);
|
||||
defer scratch.deinit();
|
||||
const a = scratch.allocator();
|
||||
var text: std.ArrayList(u8) = .empty;
|
||||
try text.appendSlice(a, "module t\n");
|
||||
for (0..41) |i| try text.print(a, "struct N{d} {{ a: N{d}, b: N{d} }}\n", .{ i, (i + 1) % 41, (i + 1) % 41 });
|
||||
try text.appendSlice(a, "fn main() -> void effects [] {}\n");
|
||||
var l: Lowerer = .{ .arena = a, .m = (try parse_mod.parse(a, text.items)).ok };
|
||||
try std.testing.expectError(error.Lower, l.checkRequirements());
|
||||
try std.testing.expectEqualStrings("E1003", l.problem.?.code);
|
||||
try std.testing.expectEqual(@as(u32, 2), l.problem.?.span.start.line);
|
||||
try std.testing.expect(l.property_module == null);
|
||||
}
|
||||
+33
-2
@@ -73,16 +73,26 @@ pub const Context = struct {
|
||||
/// Runs all verifier stages. Returns the diagnostics of the first failing
|
||||
/// stage, or an empty slice if the module verifies.
|
||||
pub fn verify(arena: std.mem.Allocator, file: []const u8, module: air.Module) std.mem.Allocator.Error![]const diag.Diagnostic {
|
||||
return (try verifyModule(arena, file, module)).diagnostics;
|
||||
}
|
||||
|
||||
pub const Result = struct { diagnostics: []const diag.Diagnostic, module: air.Module };
|
||||
|
||||
/// Retain implementation caches in the checked module without trusting caches
|
||||
/// supplied by a caller. Neither cache enters AIR text or changes a stage.
|
||||
pub fn verifyModule(arena: std.mem.Allocator, file: []const u8, module: air.Module) std.mem.Allocator.Error!Result {
|
||||
var c: Context = .{ .arena = arena, .file = file, .module = module };
|
||||
c.module.nominal_properties = null;
|
||||
c.module.accounted_sizes = null;
|
||||
const stages = .{ budget.check, cfg.check, ssa.check, types.check, effects.check, unsafe_.check, init.check, borrow.check, bounds.check };
|
||||
inline for (stages) |stage| {
|
||||
try stage(&c);
|
||||
if (c.diags.items.len != 0) {
|
||||
std.mem.sort(diag.Diagnostic, c.diags.items, {}, bySpan);
|
||||
return c.diags.items;
|
||||
return .{ .diagnostics = c.diags.items, .module = c.module };
|
||||
}
|
||||
}
|
||||
return &.{};
|
||||
return .{ .diagnostics = &.{}, .module = c.module };
|
||||
}
|
||||
|
||||
fn bySpan(_: void, a: diag.Diagnostic, b: diag.Diagnostic) bool {
|
||||
@@ -127,3 +137,24 @@ pub fn expectCodes(src: []const u8, codes: []const []const u8) !void {
|
||||
}
|
||||
|
||||
pub const test_header = "module t\nair 0\nlang 0.1\nfeatures []\n";
|
||||
|
||||
test "verification rebuilds caller-supplied property and size caches" {
|
||||
var scratch = std.heap.ArenaAllocator.init(std.testing.allocator);
|
||||
defer scratch.deinit();
|
||||
const a = scratch.allocator();
|
||||
const source = test_header ++ "type @P = struct { x: u64 }\nfn @f(%p: @P) -> @P\neffects []\nentry:\n %q = copy %p\n ret %q\n";
|
||||
var m = (try air.parse.parse(a, "t.air", source)).module.?;
|
||||
const fake = try a.alloc(@import("air/module.zig").NominalProperties, 1);
|
||||
fake[0] = .{ .linear = true, .copy = false };
|
||||
m.nominal_properties = fake;
|
||||
const result = try verifyModule(a, "t.air", m);
|
||||
try std.testing.expectEqual(@as(usize, 0), result.diagnostics.len);
|
||||
try std.testing.expect(!result.module.isLinearType(.{ .named = 0 }));
|
||||
try std.testing.expect(result.module.accounted_sizes != null);
|
||||
var large = (try air.parse.parse(a, "t.air", test_header ++ "type @P = struct { x: array<u8, 1048577> }\nfn @f() -> void\neffects []\nentry:\n ret\n")).module.?;
|
||||
var sizes: @import("air/module.zig").SizeCache = .{};
|
||||
try sizes.put(a, .{ .index = 0, .depth = 0 }, 0);
|
||||
large.accounted_sizes = &sizes;
|
||||
const rejected = try verifyModule(a, "t.air", large);
|
||||
try std.testing.expectEqualStrings("E5101", rejected.diagnostics[0].code.id);
|
||||
}
|
||||
+72
-10
@@ -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", &.{});
|
||||
|
||||
+8
-108
@@ -12,6 +12,7 @@ const Context = verifier.Context;
|
||||
const Type = air.Type;
|
||||
|
||||
pub fn check(c: *Context) !void {
|
||||
c.module = try air.properties.prepare(c.arena, c.module, null);
|
||||
try checkDeclarations(c);
|
||||
if (c.diags.items.len != 0) return;
|
||||
const all = try c.arena.alloc([]const Type, c.module.functions.len);
|
||||
@@ -157,19 +158,7 @@ fn argumentLengths(c: *Context, span: air.Span, t: Type) !void {
|
||||
}
|
||||
|
||||
/// `local.alloc` results are checked separately: their own type is storage.
|
||||
fn zeroable(m: air.Module, t: Type) bool {
|
||||
return switch (t) {
|
||||
.int, .bool => true,
|
||||
.array => |a| zeroable(m, a.elem),
|
||||
.named => |i| switch (m.types[i].kind) {
|
||||
.@"struct" => |fs| for (fs) |f| {
|
||||
if (!zeroable(m, f.ty)) break false;
|
||||
} else true,
|
||||
.@"enum" => false,
|
||||
},
|
||||
else => false,
|
||||
};
|
||||
}
|
||||
const zeroable = air.properties.isZeroable;
|
||||
|
||||
// ---------------------------------------------------------------- functions
|
||||
|
||||
@@ -379,66 +368,16 @@ fn checkIntrinsic(fc: *FnCtx, inst: air.Inst, x: @FieldType(air.Op, "intrinsic")
|
||||
}
|
||||
|
||||
/// Structural Copy query for source constraints; borrows remain second-class.
|
||||
pub fn isCopy(m: air.Module, t: Type) bool {
|
||||
return switch (t) {
|
||||
.int, .bool, .builtin, .cap, .iref => true,
|
||||
.array => |a| isCopy(m, a.elem),
|
||||
.named => |i| aggregateAll(m, i, isCopy),
|
||||
else => false,
|
||||
};
|
||||
}
|
||||
pub const isCopy = air.properties.isCopy;
|
||||
|
||||
/// Local is context-bound state, not the complement of Send (spec 15 §15).
|
||||
pub fn isLocalType(m: air.Module, t: Type) bool {
|
||||
return switch (t) {
|
||||
.local, .task, .scope, .shield, .guard => true,
|
||||
.ref, .mut, .slice, .slice_mut, .own, .own_array, .buffer, .mutex => |inner| isLocalType(m, inner.*),
|
||||
.array => |a| isLocalType(m, a.elem),
|
||||
.named => |i| switch (m.types[i].kind) {
|
||||
.@"struct" => |fs| for (fs) |f| {
|
||||
if (isLocalType(m, f.ty)) break true;
|
||||
} else false,
|
||||
.@"enum" => |vs| blk: {
|
||||
for (vs) |v| for (v.payload) |p| if (isLocalType(m, p)) break :blk true;
|
||||
break :blk false;
|
||||
},
|
||||
},
|
||||
else => false,
|
||||
};
|
||||
}
|
||||
pub const isLocalType = air.properties.isLocalType;
|
||||
|
||||
/// `Send` (spec 04 §35): may move to another task.
|
||||
pub fn isSend(m: air.Module, t: Type) bool {
|
||||
return switch (t) {
|
||||
.int, .bool, .builtin, .cap, .resource, .region, .sender, .receiver => true,
|
||||
// Graph contents are IslandSafe plain data (spec 03 §22–§24).
|
||||
.island, .frozen, .arc, .iref => true,
|
||||
// Every access to a cell is an atomic operation (spec 04 §23).
|
||||
.atomic => true,
|
||||
.own, .own_array, .buffer, .mutex => |inner| isSend(m, inner.*),
|
||||
.ref, .slice => |inner| isShare(m, inner.*),
|
||||
.mut, .slice_mut => |inner| isSend(m, inner.*),
|
||||
.array => |a| isSend(m, a.elem),
|
||||
.named => |i| aggregateAll(m, i, isSend),
|
||||
.scope, .task, .shield, .guard, .local, .void, .never => false,
|
||||
};
|
||||
}
|
||||
pub const isSend = air.properties.isSend;
|
||||
|
||||
/// `Share` (spec 04 §35): may be observed concurrently through shared borrows.
|
||||
pub fn isShare(m: air.Module, t: Type) bool {
|
||||
return switch (t) {
|
||||
.int, .bool, .builtin, .cap => true,
|
||||
.frozen, .arc, .iref, .atomic => true,
|
||||
// Its owner may mutate it: freeze to share (spec 04 §35).
|
||||
.island => false,
|
||||
.resource, .region, .mut, .slice_mut, .scope, .task, .shield, .guard, .sender, .receiver, .local, .void, .never => false,
|
||||
.own, .own_array, .buffer, .ref, .slice => |inner| isShare(m, inner.*),
|
||||
// The lock synchronizes access (spec 04 §35, §40).
|
||||
.mutex => |inner| isSend(m, inner.*),
|
||||
.array => |a| isShare(m, a.elem),
|
||||
.named => |i| aggregateAll(m, i, isShare),
|
||||
};
|
||||
}
|
||||
pub const isShare = air.properties.isShare;
|
||||
|
||||
/// A borrow whose pointee is an atomic cell: reached only atomically.
|
||||
fn atomicPointee(t: Type) bool {
|
||||
@@ -479,26 +418,7 @@ pub fn isAtomicScalar(t: Type) bool {
|
||||
|
||||
/// `IslandSafe` (spec 02 §6): plain data and `iref` handles, at any depth.
|
||||
/// `ArcSafe` is the same set in format 0.
|
||||
pub fn isIslandSafe(m: air.Module, t: Type) bool {
|
||||
return switch (t) {
|
||||
.int, .bool, .builtin, .iref => true,
|
||||
.array => |a| isIslandSafe(m, a.elem),
|
||||
.named => |i| aggregateAll(m, i, isIslandSafe),
|
||||
else => false,
|
||||
};
|
||||
}
|
||||
|
||||
fn aggregateAll(m: air.Module, i: u32, comptime pred: fn (air.Module, Type) bool) bool {
|
||||
return switch (m.types[i].kind) {
|
||||
.@"struct" => |fs| for (fs) |f| {
|
||||
if (!pred(m, f.ty)) break false;
|
||||
} else true,
|
||||
.@"enum" => |vs| {
|
||||
for (vs) |v| for (v.payload) |p| if (!pred(m, p)) return false;
|
||||
return true;
|
||||
},
|
||||
};
|
||||
}
|
||||
pub const isIslandSafe = air.properties.isIslandSafe;
|
||||
|
||||
/// `task.spawn` (spec 04 §33.2, §35): arguments like a call, Send/Share
|
||||
/// rules per argument, and a borrow-free, Send result.
|
||||
@@ -558,27 +478,7 @@ fn checkElement(fc: *FnCtx, span: air.Span, t: Type) !void {
|
||||
|
||||
/// Whether `t` transitively contains an endpoint, or a borrow or
|
||||
/// function-bound value.
|
||||
fn contains(m: air.Module, t: Type, comptime what: enum { endpoint, forbidden }) bool {
|
||||
const here = switch (what) {
|
||||
.endpoint => t.isEndpoint(),
|
||||
.forbidden => t.isBorrow() or t.isTaskBound() or t == .guard,
|
||||
};
|
||||
if (here) return true;
|
||||
// An `iref<T>` holds no `T`: types may recurse through it (spec 03 §22).
|
||||
if (t == .iref) return false;
|
||||
if (t.inner()) |inner| if (t != .ref and t != .mut and t != .slice and t != .slice_mut) return contains(m, inner, what);
|
||||
return switch (t) {
|
||||
.named => |i| switch (m.types[i].kind) {
|
||||
.@"struct" => |fs| for (fs) |f| {
|
||||
if (contains(m, f.ty, what)) break true;
|
||||
} else false,
|
||||
.@"enum" => |vs| for (vs) |v| {
|
||||
for (v.payload) |p| if (contains(m, p, what)) return true;
|
||||
} else false,
|
||||
},
|
||||
else => false,
|
||||
};
|
||||
}
|
||||
const contains = air.properties.contains;
|
||||
|
||||
/// `select` (spec 04 §39): each arm is a channel operation; each target
|
||||
/// takes its own parameters, then the send values not sent on its edge.
|
||||
|
||||
Reference in new issue
Block a user