interp: evaluate cache-free nominal sizes without recursion
This commit is contained in:
1 parent
c9064418bd
commit
897f538840
7 files changed
+218
-43
No files matched your search
@@ -367,7 +367,7 @@ test "cached predicates match uncached derivations on mixed acyclic graphs" {
|
||||
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));
|
||||
for ([_]usize{ 0, 1, 5, 6 }) |depth| try std.testing.expectEqual(try @import("../verifier/budget.zig").accountedSize(a, m, t, depth), try memo.size(m, t, depth));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
+85
-23
@@ -1219,7 +1219,7 @@ const Machine = struct {
|
||||
switch (inst.op) {
|
||||
.channel_make => |x| {
|
||||
const n = usize_(vals[x.cap]);
|
||||
const each = budget.accountedSize(m.module, x.elem, 0);
|
||||
const each = try budget.accountedSize(m.arena, m.module, x.elem, 0);
|
||||
const bytes: ?u64 = if (each) |e| std.math.mul(u64, n, e) catch null else null;
|
||||
const a = vals[x.mem].cap;
|
||||
if (!m.charge(a, bytes)) return allocFailed(f, vals, x.err);
|
||||
@@ -1773,14 +1773,14 @@ const Machine = struct {
|
||||
while (x) |i| : (x = m.authorities.items[i].parent) m.authorities.items[i].used -= bytes;
|
||||
}
|
||||
|
||||
fn arrayBytes(m: *Machine, element: Value, n: u64) ?u64 {
|
||||
const each = m.valueBytes(element) orelse return null;
|
||||
fn arrayBytes(m: *Machine, element: Value, n: u64) std.mem.Allocator.Error!?u64 {
|
||||
const each = try m.valueBytes(element) orelse return null;
|
||||
return std.math.mul(u64, n, each) catch null;
|
||||
}
|
||||
|
||||
/// Accounted size (spec 08 §7.1) of the type of a runtime value; equal
|
||||
/// to `budget.accountedSize` of the value's static type.
|
||||
fn valueBytes(m: *Machine, initial: Value) ?u64 {
|
||||
fn valueBytes(m: *Machine, initial: Value) std.mem.Allocator.Error!?u64 {
|
||||
var v = initial;
|
||||
var factor: ?u64 = 1;
|
||||
while (v == .array) {
|
||||
@@ -1794,8 +1794,8 @@ const Machine = struct {
|
||||
const base = (switch (v) {
|
||||
.int => |x| x.ty.bits() / 8,
|
||||
.boolean => 1,
|
||||
.aggregate => |a| budget.accountedSize(m.module, .{ .named = a.decl }, 0),
|
||||
.builtin => |b| budget.accountedSize(m.module, .{ .builtin = b.which }, 0),
|
||||
.aggregate => |a| try budget.accountedSize(m.arena, m.module, .{ .named = a.decl }, 0),
|
||||
.builtin => |b| try budget.accountedSize(m.arena, m.module, .{ .builtin = b.which }, 0),
|
||||
.cap => 8,
|
||||
.iref => 16,
|
||||
else => unreachable, // only value types are allocated (spec 03 §19.2)
|
||||
@@ -1852,7 +1852,7 @@ const Machine = struct {
|
||||
inline .island_create, .arc_new => |x| {
|
||||
const v = vals[x.value];
|
||||
const a = vals[x.cap].cap;
|
||||
const bytes = if (m.valueBytes(v)) |b| (std.math.add(u64, b, graph_header) catch null) else null;
|
||||
const bytes = if (try m.valueBytes(v)) |b| (std.math.add(u64, b, graph_header) catch null) else null;
|
||||
if (!m.charge(a, bytes)) return allocFailed(f, vals, x.err);
|
||||
const g = try m.newGraph(a, bytes.?, v);
|
||||
if (inst.op == .arc_new) {
|
||||
@@ -1866,7 +1866,7 @@ const Machine = struct {
|
||||
.island_alloc => |x| {
|
||||
const g = graphOf(vals[x.island]);
|
||||
const v = vals[x.value];
|
||||
const bytes = m.valueBytes(v);
|
||||
const bytes = try m.valueBytes(v);
|
||||
if (!m.charge(g.authority, bytes)) return allocFailed(f, vals, x.err);
|
||||
g.bytes += bytes.?;
|
||||
const cell = try m.heap().create(Value.Cell);
|
||||
@@ -1930,7 +1930,7 @@ const Machine = struct {
|
||||
switch (inst.op) {
|
||||
.buffer_new => |x| {
|
||||
const capacity = usize_(vals[x.n]);
|
||||
const size = budget.accountedSize(m.module, x.ty, 0).?;
|
||||
const size = (try budget.accountedSize(m.arena, m.module, x.ty, 0)).?;
|
||||
const bytes = std.math.mul(u64, capacity, size) catch null;
|
||||
const authority_ = vals[x.cap].cap;
|
||||
if (!m.charge(authority_, bytes)) return allocFailed(f, vals, x.err);
|
||||
@@ -1957,7 +1957,7 @@ const Machine = struct {
|
||||
if (try m.raceCheck(b.cell, &.{}, true)) |r| return r;
|
||||
const capacity = usize_(vals[x.n]);
|
||||
if (capacity <= b.capacity) return .{ .jump = x.ok };
|
||||
const size = budget.accountedSize(m.module, b.elem, 0).?;
|
||||
const size = (try budget.accountedSize(m.arena, m.module, b.elem, 0)).?;
|
||||
const bytes = std.math.mul(u64, capacity, size) catch null;
|
||||
const authority_ = vals[x.cap].cap;
|
||||
if (!m.charge(authority_, bytes)) return allocFailed(f, vals, x.err);
|
||||
@@ -2016,7 +2016,7 @@ const Machine = struct {
|
||||
switch (inst.op) {
|
||||
.own_pack => |x| {
|
||||
const a = vals[x.cap].cap;
|
||||
const bytes = budget.accountedSize(m.module, x.ty, 0);
|
||||
const bytes = try budget.accountedSize(m.arena, m.module, x.ty, 0);
|
||||
if (!m.charge(a, bytes)) {
|
||||
vals[f.blocks[x.err].params[1].value] = vals[x.value];
|
||||
return allocFailed(f, vals, x.err);
|
||||
@@ -2029,7 +2029,7 @@ const Machine = struct {
|
||||
},
|
||||
.own_new => |x| {
|
||||
const v = vals[x.value];
|
||||
const bytes = m.valueBytes(vals[x.value]);
|
||||
const bytes = try m.valueBytes(vals[x.value]);
|
||||
const a = vals[x.cap].cap;
|
||||
if (!m.charge(a, bytes)) return allocFailed(f, vals, x.err);
|
||||
const cell = try m.heap().create(Value.Cell);
|
||||
@@ -2041,7 +2041,7 @@ const Machine = struct {
|
||||
.own_array => |x| {
|
||||
const n = usize_(vals[x.n]);
|
||||
const a = vals[x.cap].cap;
|
||||
const bytes = m.arrayBytes(vals[x.value], n);
|
||||
const bytes = try m.arrayBytes(vals[x.value], n);
|
||||
if (!m.charge(a, bytes)) return allocFailed(f, vals, x.err);
|
||||
const items = try m.heap().alloc(Value, n);
|
||||
for (items) |*it| it.* = try m.deepCopy(vals[x.value]);
|
||||
@@ -2076,7 +2076,7 @@ const Machine = struct {
|
||||
const t = air.allocTargets(inst.op).?;
|
||||
const r = placePtr(asPlace(vals[rb])).region;
|
||||
const region = &m.regions.items[r];
|
||||
const bytes = if (n) |count| m.arrayBytes(vals[value], count) else m.valueBytes(vals[value]);
|
||||
const bytes = if (n) |count| try m.arrayBytes(vals[value], count) else try m.valueBytes(vals[value]);
|
||||
if (!m.charge(region.authority, bytes)) return allocFailed(f, vals, t.err);
|
||||
region.used += bytes.?;
|
||||
const cell = try m.heap().create(Value.Cell);
|
||||
@@ -2997,10 +2997,10 @@ test "runtime value sizing agrees with static accounting through array wrappers"
|
||||
var ty = leaf.ty;
|
||||
var v = leaf.value;
|
||||
for (0..8) |depth| {
|
||||
const size = budget.accountedSize(module, ty, 0).?;
|
||||
try std.testing.expectEqual(@as(?u64, size), m.valueBytes(v));
|
||||
const size = (try budget.accountedSize(arena, module, ty, 0)).?;
|
||||
try std.testing.expectEqual(@as(?u64, size), try m.valueBytes(v));
|
||||
for ([_]u64{ 0, 1, 2, std.math.maxInt(u64) }) |count|
|
||||
try std.testing.expectEqual(@as(?u64, std.math.mul(u64, size, count) catch null), m.arrayBytes(v, count));
|
||||
try std.testing.expectEqual(@as(?u64, std.math.mul(u64, size, count) catch null), try m.arrayBytes(v, count));
|
||||
const len = depth % 3 + 1;
|
||||
const items = try arena.alloc(Value, len);
|
||||
@memset(items, v);
|
||||
@@ -3043,13 +3043,13 @@ test "runtime value sizing handles deep wrappers and zero-sized leaves without r
|
||||
overflowing = .{ .array = pair };
|
||||
}
|
||||
}
|
||||
try std.testing.expectEqual(@as(?u64, 1), m.valueBytes(one));
|
||||
try std.testing.expectEqual(@as(?u64, std.math.maxInt(u64)), m.arrayBytes(one, std.math.maxInt(u64)));
|
||||
try std.testing.expectEqual(@as(?u64, 0), m.valueBytes(zero));
|
||||
try std.testing.expectEqual(@as(?u64, 0), m.arrayBytes(zero, std.math.maxInt(u64)));
|
||||
try std.testing.expectEqual(@as(?u64, null), m.valueBytes(overflowing));
|
||||
try std.testing.expectEqual(@as(?u64, 1), try m.valueBytes(one));
|
||||
try std.testing.expectEqual(@as(?u64, std.math.maxInt(u64)), try m.arrayBytes(one, std.math.maxInt(u64)));
|
||||
try std.testing.expectEqual(@as(?u64, 0), try m.valueBytes(zero));
|
||||
try std.testing.expectEqual(@as(?u64, 0), try m.arrayBytes(zero, std.math.maxInt(u64)));
|
||||
try std.testing.expectEqual(@as(?u64, null), try m.valueBytes(overflowing));
|
||||
// A zero dynamic allocation count must not mask an overflowing element.
|
||||
try std.testing.expectEqual(@as(?u64, null), m.arrayBytes(overflowing, 0));
|
||||
try std.testing.expectEqual(@as(?u64, null), try m.arrayBytes(overflowing, 0));
|
||||
}
|
||||
|
||||
fn expectCollectorAllocationFailure(source: []Value, fail_index: usize) !bool {
|
||||
@@ -3431,6 +3431,68 @@ test "deep zero and local-load copying retain fuel boundaries with ordinary and
|
||||
}
|
||||
}
|
||||
|
||||
test "public verification and runtime entry preserve cache-free nominal quota outcomes" {
|
||||
var state: std.heap.ArenaAllocator = .init(std.testing.allocator);
|
||||
defer state.deinit();
|
||||
const a = state.allocator();
|
||||
const src =
|
||||
\\module cache.free
|
||||
\\air 0
|
||||
\\lang 0.1
|
||||
\\features [arrays, capabilities, effects, heap, linear_aggregates, ownership, resources]
|
||||
\\type @Empty = struct {}
|
||||
\\type @Record = struct { xs: array<u16, 3>, empty: @Empty }
|
||||
\\type @Choice = enum { none, some(@Record) }
|
||||
\\fn @main(%heap: cap<mem.alloc>) -> u8
|
||||
\\effects [mem.alloc]
|
||||
\\entry:
|
||||
\\ %p = local.alloc @Record
|
||||
\\ local.zero %p
|
||||
\\ %r = local.load %p
|
||||
\\ %v = enum.make @Choice.some(%r)
|
||||
\\ own.new %v, %heap, ok -> @have, err -> @nomem
|
||||
\\have(%o: own<@Choice>):
|
||||
\\ own.free %o
|
||||
\\ %yes = const.u8 7
|
||||
\\ ret %yes
|
||||
\\nomem(%e: alloc_error):
|
||||
\\ %no = const.u8 0
|
||||
\\ ret %no
|
||||
;
|
||||
const parsed = try air.parse.parse(a, "cache-free.air", src);
|
||||
try std.testing.expect(parsed.diagnostics.len == 0);
|
||||
const original = parsed.module.?;
|
||||
const verifier = @import("../verifier.zig");
|
||||
try std.testing.expect((try verifier.verify(a, "cache-free.air", original)).len == 0);
|
||||
try std.testing.expect(original.accounted_sizes == null and original.nominal_properties == null);
|
||||
const checked = try verifier.verifyModule(a, "cache-free.air", original);
|
||||
try std.testing.expect(checked.diagnostics.len == 0 and checked.module.accounted_sizes != null);
|
||||
for ([_]bool{ false, true }) |every_step| for ([_][]const u8{ "0", "6", "7" }) |quota| for (0..9) |fuel| {
|
||||
var records: [2][]const u8 = undefined;
|
||||
for ([_]air.Module{ original, checked.module }, 0..) |module, index| {
|
||||
var stats: MemoryStats = .{};
|
||||
const result = try run(a, module, "main", .{ .fuel = fuel, .memory = &stats, .collect_every_step = every_step, .grants = &.{.{ .parameter = "heap", .kind = "mem.alloc", .scope = quota }} });
|
||||
const success = std.mem.eql(u8, quota, "7");
|
||||
const complete: u64 = if (success) 8 else 7;
|
||||
try std.testing.expectEqual(@min(fuel, complete), stats.steps);
|
||||
if (fuel < complete) {
|
||||
try std.testing.expect(result == .limit_exceeded and result.limit_exceeded.steps == fuel);
|
||||
} else try std.testing.expect(result == .returned and result.returned.int.bits == @as(u128, if (success) 7 else 0));
|
||||
var encoded: std.Io.Writer.Allocating = .init(a);
|
||||
try writeOutcome(module, result, null, &encoded.writer);
|
||||
records[index] = encoded.written();
|
||||
}
|
||||
try std.testing.expectEqualStrings(records[0], records[1]);
|
||||
};
|
||||
var m: Machine = .{ .arena = a, .module = original, .fuel = null, .world = try .init(a, .{}) };
|
||||
var failing: std.testing.FailingAllocator = .init(std.testing.allocator, .{ .fail_index = 0 });
|
||||
m.arena = failing.allocator();
|
||||
// A nominal query reads its type declaration, not the active payload.
|
||||
const value: Value = .{ .aggregate = .{ .decl = 2, .variant = 0, .fields = &.{} } };
|
||||
try std.testing.expectError(error.OutOfMemory, m.valueBytes(value));
|
||||
try std.testing.expectEqual(failing.allocated_bytes, failing.freed_bytes);
|
||||
}
|
||||
|
||||
test "deep returned payload survives per-step collection and execution heap release" {
|
||||
var arena_state: std.heap.ArenaAllocator = .init(std.testing.allocator);
|
||||
defer arena_state.deinit();
|
||||
|
||||
+104
-19
@@ -25,7 +25,10 @@ pub const Limits = struct {
|
||||
/// 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 {
|
||||
/// Cached/anonymous reads allocate nothing. An uncached nominal query uses
|
||||
/// temporary continuations and propagates host allocation failure separately
|
||||
/// from arithmetic overflow; it releases metadata before returning.
|
||||
pub fn accountedSize(allocator: std.mem.Allocator, m: air.Module, t: air.Type, depth: usize) std.mem.Allocator.Error!?u64 {
|
||||
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.
|
||||
@@ -60,7 +63,15 @@ pub fn accountedSize(m: air.Module, t: air.Type, depth: usize) ?u64 {
|
||||
current = inner.*;
|
||||
},
|
||||
else => {
|
||||
base = uncachedBaseSize(m, current, depth);
|
||||
if (current == .named) {
|
||||
// A cache-free public module needs the same explicit
|
||||
// nominal continuations as verification. Keep temporary
|
||||
// metadata separate from caller/runtime payload storage.
|
||||
var scratch: std.heap.ArenaAllocator = .init(allocator);
|
||||
defer scratch.deinit();
|
||||
var memo: SizeMemo = .{ .arena = scratch.allocator() };
|
||||
base = try memo.size(m, current, depth);
|
||||
} else base = scalarSize(current);
|
||||
break;
|
||||
},
|
||||
}
|
||||
@@ -131,7 +142,7 @@ pub const SizeMemo = struct {
|
||||
if (current_depth > m.types.len + 1) {
|
||||
result = 0;
|
||||
} else if (sizeKey(current, current_depth)) |key| {
|
||||
if (cache.values.get(key)) |value| {
|
||||
if (cache.values.get(key) orelse if (m.accounted_sizes) |shared| shared.get(key) else null) |value| {
|
||||
result = value;
|
||||
} else {
|
||||
if (current == .named) cache.evaluations += 1;
|
||||
@@ -202,21 +213,25 @@ fn scalarSize(t: air.Type) u64 {
|
||||
};
|
||||
}
|
||||
|
||||
// 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 {
|
||||
// Independent, bounded test oracle. Production queries never recurse over
|
||||
// nominal members; this derivation is used only on shallow test graphs.
|
||||
fn referenceSize(m: air.Module, t: air.Type, depth: usize) ?u64 {
|
||||
if (depth > m.types.len + 1) return 0;
|
||||
return switch (t) {
|
||||
.array => |row| std.math.mul(u64, row.len, referenceSize(m, row.elem, depth) orelse return null) catch null,
|
||||
.atomic => |inner| referenceSize(m, inner.*, depth),
|
||||
.mutex => |inner| std.math.add(u64, 8, referenceSize(m, inner.*, depth) 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, referenceSize(m, f.ty, depth + 1) orelse return null) catch return null;
|
||||
break :blk sum;
|
||||
},
|
||||
.@"enum" => |vs| blk: {
|
||||
var maximum: 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, referenceSize(m, p, depth + 1) orelse return null) catch return null;
|
||||
maximum = @max(maximum, sum);
|
||||
}
|
||||
break :blk std.math.add(u64, tagSize(vs.len), maximum) catch null;
|
||||
@@ -337,6 +352,69 @@ fn checkValueSizes(c: *verifier.Context, memo: *SizeMemo) !void {
|
||||
}
|
||||
}
|
||||
|
||||
test "uncached nominal sizes preserve shallow independent arithmetic and cycle guards" {
|
||||
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 leaf = try a.create(air.Type);
|
||||
leaf.* = if (i == 3) .{ .int = .u64 } else .{ .named = @intCast(if (seed % 7 == 0) i else i + 1) };
|
||||
const array = try a.create(air.Type.Array);
|
||||
array.* = .{ .elem = leaf.*, .len = if (seed % 13 == 0) std.math.maxInt(u64) else (seed + i) % 4 };
|
||||
const choices = [_]air.Type{ leaf.*, .bool, .{ .array = array }, .{ .atomic = leaf }, .{ .mutex = leaf }, .{ .own = leaf }, .{ .iref = leaf } };
|
||||
const x = choices[(seed + 3 * i) % choices.len];
|
||||
const y = choices[(seed / choices.len + i) % choices.len];
|
||||
const fields = try a.dupe(air_module.Field, &.{ .{ .name = "x", .ty = x }, .{ .name = "y", .ty = y } });
|
||||
const variants = try a.dupe(air_module.Variant, &.{ .{ .name = "empty", .payload = &.{} }, .{ .name = "some", .payload = try a.dupe(air.Type, &.{ x, y }) } });
|
||||
d.* = .{ .name = "N", .span = .{ .line = 1, .col = 1 }, .kind = if ((seed + i) % 2 == 0) .{ .@"struct" = fields } else .{ .@"enum" = variants } };
|
||||
}
|
||||
const m: air.Module = .{ .name = "oracle", .format_version = 0, .lang = .{ 0, 1 }, .features = &.{}, .types = ds, .functions = &.{} };
|
||||
for (0..4) |i| for (0..7) |depth| {
|
||||
const ty: air.Type = .{ .named = @intCast(i) };
|
||||
try std.testing.expectEqual(referenceSize(m, ty, depth), try accountedSize(a, m, ty, depth));
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
test "deep cache-free nominal queries release continuations and separate allocation failure" {
|
||||
var scratch: std.heap.ArenaAllocator = .init(std.testing.allocator);
|
||||
defer scratch.deinit();
|
||||
const a = scratch.allocator();
|
||||
const ds = try a.alloc(air.TypeDecl, 32768);
|
||||
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 == ds.len) .{ .int = .u8 } else .{ .named = @intCast(i + 1) } }}) } };
|
||||
var m: air.Module = .{ .name = "deep", .format_version = 0, .lang = .{ 0, 1 }, .features = &.{}, .types = ds, .functions = &.{} };
|
||||
const ty: air.Type = .{ .named = 0 };
|
||||
try std.testing.expectEqual(@as(?u64, 1), try accountedSize(std.testing.allocator, m, ty, 0));
|
||||
try std.testing.expect(m.accounted_sizes == null);
|
||||
// Fail every underlying metadata allocation, then reach a successful
|
||||
// query. Null is reserved for arithmetic overflow, never host OOM.
|
||||
var fail_index: usize = 0;
|
||||
while (true) : (fail_index += 1) {
|
||||
try std.testing.expect(fail_index < 100);
|
||||
var failing: std.testing.FailingAllocator = .init(std.testing.allocator, .{ .fail_index = fail_index });
|
||||
const result = accountedSize(failing.allocator(), m, ty, 0);
|
||||
if (failing.has_induced_failure) {
|
||||
try std.testing.expectError(error.OutOfMemory, result);
|
||||
} else try std.testing.expectEqual(@as(?u64, 1), try result);
|
||||
try std.testing.expectEqual(failing.allocated_bytes, failing.freed_bytes);
|
||||
if (!failing.has_induced_failure) break;
|
||||
}
|
||||
var cache: air_module.SizeCache = .{};
|
||||
try cache.put(a, .{ .index = 0, .depth = 0 }, 1);
|
||||
m.accounted_sizes = &cache;
|
||||
var failing: std.testing.FailingAllocator = .init(std.testing.allocator, .{ .fail_index = 0 });
|
||||
try std.testing.expectEqual(@as(?u64, 1), try accountedSize(failing.allocator(), m, ty, 0));
|
||||
try std.testing.expect(!failing.has_induced_failure);
|
||||
// A cached overflow and a partial cache remain distinguishable from a
|
||||
// missing entry; neither the caller's table nor type storage is mutated.
|
||||
try cache.put(a, .{ .index = 0, .depth = 0 }, null);
|
||||
try std.testing.expectEqual(@as(?u64, null), try accountedSize(failing.allocator(), m, ty, 0));
|
||||
try std.testing.expectEqual(@as(?u64, 1), try accountedSize(a, m, .{ .named = 1 }, 0));
|
||||
try std.testing.expectEqual(@as(usize, 1), cache.count());
|
||||
}
|
||||
|
||||
test "too many parameters" {
|
||||
var src: std.ArrayList(u8) = .empty;
|
||||
defer src.deinit(std.testing.allocator);
|
||||
@@ -358,7 +436,7 @@ test "accounted sizes memoize shared diamonds and cyclic depth states" {
|
||||
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));
|
||||
for (0..65) |i| try std.testing.expectEqual(@as(?u64, 0), try accountedSize(std.testing.allocator, 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 } } }) };
|
||||
@@ -394,9 +472,9 @@ test "maximum value size" {
|
||||
|
||||
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), try accountedSize(std.testing.allocator, m, .{ .int = .isize }, 0));
|
||||
try std.testing.expectEqual(@as(?u64, 16), try accountedSize(std.testing.allocator, m, .{ .int = .u128 }, 0));
|
||||
try std.testing.expectEqual(@as(?u64, 1), try accountedSize(std.testing.allocator, m, .bool, 0));
|
||||
try std.testing.expectEqual(@as(u64, 1), tagSize(256));
|
||||
try std.testing.expectEqual(@as(u64, 2), tagSize(257));
|
||||
}
|
||||
@@ -418,7 +496,7 @@ test "anonymous wrapper sizes do not depend on nominal declaration count" {
|
||||
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 accountedSize(std.testing.allocator, m, t, 0));
|
||||
try std.testing.expectEqual(@as(?u64, want), try memo.size(m, t, 0));
|
||||
}
|
||||
}
|
||||
@@ -450,9 +528,9 @@ test "deep anonymous sizes use bounded native stack and cached inner queries" {
|
||||
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));
|
||||
try std.testing.expectEqual(@as(?u64, 1), try accountedSize(std.testing.allocator, m, t, 0));
|
||||
m.accounted_sizes = &memo.values;
|
||||
for (rows) |*row| try std.testing.expectEqual(@as(?u64, 1), accountedSize(m, .{ .array = row }, 0));
|
||||
for (rows) |*row| try std.testing.expectEqual(@as(?u64, 1), try accountedSize(std.testing.allocator, m, .{ .array = row }, 0));
|
||||
}
|
||||
|
||||
test "size overflow preserves zero leaves and does not hide behind outer zero" {
|
||||
@@ -468,12 +546,12 @@ test "size overflow preserves zero leaves and does not hide behind outer zero" {
|
||||
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));
|
||||
try std.testing.expectEqual(@as(?u64, 0), try accountedSize(std.testing.allocator, 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));
|
||||
try std.testing.expectEqual(@as(?u64, null), try accountedSize(std.testing.allocator, m, t, 0));
|
||||
}
|
||||
|
||||
test "deep nominal size queries use explicit continuations" {
|
||||
@@ -506,6 +584,13 @@ test "overflowing nominal continuations cache null without revisiting children"
|
||||
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);
|
||||
var shared = m;
|
||||
shared.accounted_sizes = &memo.values;
|
||||
var failing: std.testing.FailingAllocator = .init(std.testing.allocator, .{ .fail_index = 0 });
|
||||
var reader: SizeMemo = .{ .arena = failing.allocator() };
|
||||
try std.testing.expectEqual(@as(?u64, null), try reader.size(shared, .{ .named = 0 }, 0));
|
||||
try std.testing.expectEqual(@as(usize, 0), reader.evaluations);
|
||||
try std.testing.expect(!failing.has_induced_failure);
|
||||
}
|
||||
|
||||
test "anonymous cache identities distinguish shared atomic and mutex content" {
|
||||
@@ -520,6 +605,6 @@ test "anonymous cache identities distinguish shared atomic and mutex content" {
|
||||
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));
|
||||
try std.testing.expectEqual(@as(?u64, 8), try accountedSize(std.testing.allocator, m, atom, 0));
|
||||
try std.testing.expectEqual(@as(?u64, 16), try accountedSize(std.testing.allocator, m, lock, 0));
|
||||
}
|
||||
Reference in new issue
Block a user