verifier: max_value_size and checked size accounting (Q-020, DD-015)
- spec 08 §7.1: platform-independent accounted size table shared by max_value_size and max_local_frame; enum tag is the smallest of 1/2/4/8 bytes; borrows 8, slices 16; checked arithmetic, overflow is E5101 with found = "overflow" - every materializable type in a module must be at most 1 MiB, checked before functions are verified; each oversized type reported once - E5101 fields no longer truncate huge sizes - conformance: frame_too_large, value_too_large, size_overflow - Q-020 resolved; Phase 1 closed
This commit is contained in:
1 parent
e3dc0fb7bb
commit
2ddf424c14
8 files changed
+165
-34
No files matched your search
@@ -81,3 +81,9 @@ Reason: timeouts make test outcomes depend on machine speed; a trap code for exh
|
||||
AIR format 0 uses the mandatory `features [...]` header as an explicit semantic compatibility contract. Every post-baseline AIR 0 extension belongs to a registered feature (`reference/air-features.md`). Modules MUST declare every feature they use and its dependency closure. Implementations MUST validate the feature set before validating the module body. Unknown, unsupported, undeclared, duplicated or dependency-incomplete features are reported as `E0006` with a structured `reason`. Feature semantics are immutable once published. `aji features` reports what a toolchain supports.
|
||||
|
||||
Reason: format 0 is frozen; features make it an extensible protocol without an older implementation half-parsing a module it cannot understand. One code with a reason keeps the §8.7 exception for `E0006` intact.
|
||||
|
||||
## DD-015 — Maximum AIR value size
|
||||
|
||||
AIR format 0 defines `max_value_size` = 1 048 576 bytes. Every materializable type in a module has an accounted size (spec 08 §7.1, a platform-independent metric, not a layout) of at most this value; violation, including overflow while computing a size, is `E5101`, checked during module validation. `max_local_frame` (1 MiB per function) is a separate guarantee. Neither depends on host memory.
|
||||
|
||||
Reason: a verified IR needs its own deterministic validity limits instead of inheriting host behavior; and large data should live in storage reached through borrows, not travel as SSA values. Total allocation is bounded separately in Phase 2, as fuel bounds execution.
|
||||
@@ -25,7 +25,7 @@ Status values: `open`, `resolved`.
|
||||
| Q-017 | open | P5 | Replay file format |
|
||||
| Q-018 | resolved | P2 | Runtime diagnostics (traps) vs verifier diagnostics |
|
||||
| Q-019 | resolved | P3 | Non-terminating programs in test tooling |
|
||||
| Q-020 | open | P2 | Size bound for SSA array values |
|
||||
| Q-020 | resolved | P2 | Size bound for SSA array values |
|
||||
|
||||
## Q-001 — Text AIR grammar
|
||||
|
||||
@@ -134,3 +134,5 @@ A program may legitimately loop forever, so the language cannot bound execution.
|
||||
## Q-020 — Size bound for SSA array values
|
||||
|
||||
Frame storage is bounded (`max_local_frame`, spec 08 §11.5), but an SSA value of type `array<T, N>` (for example from `array.splat`) is bounded only by `N ≤ 2^32`. A module can therefore ask the interpreter for gigabytes in one instruction, and the outcome then depends on host memory. Options: apply the same accounted-size limit to every type in the module (E5101), or a separate `max_value_bytes`. Needs an owner decision; changing it later would turn accepted modules into rejected ones, so it should be settled before format 0 is released.
|
||||
|
||||
**Resolved (owner decision, DD-015):** `max_value_size` = 1 048 576 bytes bounds the accounted size of every materializable type in a module (spec 08 §7.1); `max_local_frame` stays a separate limit. Both are normative, deterministic, independent of host memory and reported as `E5101`. Accounted sizes use checked arithmetic; overflow is `E5101`. Total allocation is a Phase 2 concern (allocation capabilities and runtime limits).
|
||||
@@ -4,6 +4,7 @@ Status: Accepted (revision 2, after owner review items 1–5)
|
||||
Scope: AIR format 0, features `ownership` and `arrays`
|
||||
Language version: 0.1
|
||||
Erratum (during integration): the array type is spelled `array<T, N>` in text AIR, because `;` starts a comment; revision 2 wrote `[T; N]`. Source syntax may keep `[T; N]`.
|
||||
Later change: the accounted-size table moved to spec 08 §7.1 and gained `max_value_size` and the enum tag rule (DD-015, Q-020).
|
||||
Integrated into: spec 03 §6, §4, §8.1; spec 07 §8.8; spec 07a; spec 08 §11–§14; `reference/air-text.ebnf`, `reference/air-features.md`, `reference/error-codes.md`
|
||||
Supersedes: —
|
||||
|
||||
|
||||
+28
-14
@@ -98,8 +98,34 @@ Bounds are fixed by this specification, not configurable: a configurable bound w
|
||||
| blocks per function | 16 384 |
|
||||
| instructions per function | 262 144 |
|
||||
| cases per `switch` | 4 096 |
|
||||
| `max_value_size`: accounted size of any materializable type in the module (§7.1) | 1 048 576 bytes |
|
||||
| `max_local_frame`: accounted frame storage per function (§11.5) | 1 048 576 bytes |
|
||||
|
||||
The `E5101` diagnostic carries the fields `limit` (name of the limit), `max` and `found`.
|
||||
The `E5101` diagnostic carries the fields `limit` (name of the limit), `max` and `found`; when a size computation overflows, `found` is the string `"overflow"`.
|
||||
|
||||
### 7.1 Accounted size
|
||||
|
||||
The **accounted size** of a type is a normative, platform-independent metric of AIR. It is not an ABI layout and does not depend on host memory. All additions and multiplications are checked; an overflow while computing a size is `E5101`, never a program trap.
|
||||
|
||||
| Type | Accounted bytes |
|
||||
|---|---|
|
||||
| `u8`, `i8`, `bool` | 1 |
|
||||
| `u16`, `i16` | 2 |
|
||||
| `u32`, `i32`, `f32` (reserved) | 4 |
|
||||
| `u64`, `i64`, `f64` (reserved) | 8 |
|
||||
| `u128`, `i128` | 16 |
|
||||
| `usize`, `isize` | 8 (accounting convention of AIR 0, independent of the target) |
|
||||
| `array<T, N>` | `N ×` accounted size of `T` |
|
||||
| struct | sum of the accounted sizes of its fields |
|
||||
| enum | `tag + ` the largest sum over one variant's payload, where `tag` is the smallest of 1, 2, 4, 8 bytes that can hold the index of every variant |
|
||||
| `ref<T>`, `mut<T>` | 8 |
|
||||
| `slice<T>`, `slice_mut<T>` | 16 |
|
||||
|
||||
`void`, `never` and storage types (`local<T>`) are not materializable values and have no accounted size; the `T` inside a storage type does.
|
||||
|
||||
**Maximum value size.** Every materializable type that appears in a module — in declarations, signatures, block parameters, instruction type operands, and inside compound types — MUST have an accounted size of at most `max_value_size` (`E5101`, limit `value size bytes`). The check runs during module validation, before functions are verified or executed.
|
||||
|
||||
Large data does not travel as SSA values: buffers beyond `max_value_size` belong in storage reached through borrows (regions and heap allocations from Phase 2). `max_value_size` bounds single values only; the total memory a program allocates is bounded separately, by allocation capabilities and runtime limits (Phase 2), just as fuel bounds execution (spec 11 §7).
|
||||
|
||||
## 8. Contracts
|
||||
|
||||
@@ -177,19 +203,7 @@ Only the set of accepted programs is normative. Because states must match exactl
|
||||
|
||||
### 11.5 Frame size
|
||||
|
||||
Each function's frame storage is the sum of the accounted sizes of its `local.alloc` types. It MUST NOT exceed `max_local_frame` = 1 048 576 bytes (`E5101`, limit `local frame bytes`). Accounted size is a normative counting rule, not a memory layout:
|
||||
|
||||
| Type | Accounted bytes |
|
||||
|---|---|
|
||||
| `u8`/`i8`/`bool` | 1 |
|
||||
| `u16`/`i16` | 2 |
|
||||
| `u32`/`i32` | 4 |
|
||||
| `u64`/`i64` | 8 |
|
||||
| `u128`/`i128` | 16 |
|
||||
| `usize`/`isize` | 8 (the largest supported pointer width, so the result is target-independent) |
|
||||
| struct | sum of its fields |
|
||||
| enum | 4 + the largest sum over a variant's payload |
|
||||
| `array<T, N>` | `N ×` accounted size of `T` |
|
||||
Each function's frame storage is the sum of the accounted sizes (§7.1) of its `local.alloc` types, computed with checked arithmetic. It MUST NOT exceed `max_local_frame` = 1 048 576 bytes (`E5101`, limit `local frame bytes`). `max_value_size` and `max_local_frame` are separate guarantees and both are checked: ten locals of 200 KiB each satisfy the first and violate the second.
|
||||
|
||||
### 11.6 Borrow-typed block parameters
|
||||
|
||||
|
||||
+97
-17
@@ -16,37 +16,51 @@ pub const Limits = struct {
|
||||
pub const insts = 262_144;
|
||||
pub const cases = 4_096;
|
||||
pub const local_frame = 1_048_576; // bytes, spec 08 §11.5
|
||||
pub const value_size = 1_048_576; // bytes, spec 08 §7.1
|
||||
};
|
||||
|
||||
/// Accounted size in bytes (spec 08 §11.5). A counting rule, not a layout.
|
||||
/// Recursive aggregates are reported later (E1003); they count as 0 here.
|
||||
pub fn accountedSize(m: air.Module, t: air.Type, depth: usize) u64 {
|
||||
/// Accounted size in bytes (spec 08 §7.1): a platform-independent metric,
|
||||
/// not a layout. Null if the computation overflows. Recursive aggregates are
|
||||
/// reported later (E1003) and count as 0 here; `void`, `never` and storage
|
||||
/// types are not materializable and count as 0.
|
||||
pub fn accountedSize(m: air.Module, t: air.Type, depth: usize) ?u64 {
|
||||
if (depth > m.types.len + 1) return 0;
|
||||
return switch (t) {
|
||||
.int => |it| it.bits() / 8,
|
||||
.bool => 1,
|
||||
.array => |a| std.math.mul(u64, a.len, accountedSize(m, a.elem, depth + 1)) catch std.math.maxInt(u64),
|
||||
.ref, .mut => 8,
|
||||
.slice, .slice_mut => 16,
|
||||
.array => |a| std.math.mul(u64, a.len, accountedSize(m, a.elem, depth + 1) orelse return null) catch null,
|
||||
.named => |i| switch (m.types[i].kind) {
|
||||
.@"struct" => |fs| blk: {
|
||||
var sum: u64 = 0;
|
||||
for (fs) |f| sum +|= accountedSize(m, f.ty, depth + 1);
|
||||
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;
|
||||
for (vs) |v| {
|
||||
var sum: u64 = 0;
|
||||
for (v.payload) |p| sum +|= accountedSize(m, p, depth + 1);
|
||||
for (v.payload) |p| sum = std.math.add(u64, sum, accountedSize(m, p, depth + 1) orelse return null) catch return null;
|
||||
max = @max(max, sum);
|
||||
}
|
||||
break :blk 4 +| max;
|
||||
break :blk std.math.add(u64, tagSize(vs.len), max) catch null;
|
||||
},
|
||||
},
|
||||
else => 0,
|
||||
.void, .never, .local => 0,
|
||||
};
|
||||
}
|
||||
|
||||
/// Smallest of 1, 2, 4, 8 bytes that holds every variant index.
|
||||
fn tagSize(variants: usize) u64 {
|
||||
if (variants <= 1 << 8) return 1;
|
||||
if (variants <= 1 << 16) return 2;
|
||||
if (variants <= 1 << 32) return 4;
|
||||
return 8;
|
||||
}
|
||||
|
||||
pub fn check(c: *verifier.Context) !void {
|
||||
try checkValueSizes(c);
|
||||
const m = c.module;
|
||||
const origin: air.Span = .{ .line = 1, .col = 1 };
|
||||
try limit(c, origin, "functions per module", Limits.functions, m.functions.len);
|
||||
@@ -74,23 +88,70 @@ pub fn check(c: *verifier.Context) !void {
|
||||
};
|
||||
}
|
||||
try limit(c, f.span, "instructions per function", Limits.insts, n);
|
||||
var frame: u64 = 0;
|
||||
var frame: ?u64 = 0;
|
||||
for (f.blocks) |b| for (b.insts) |inst| if (inst.op == .local_alloc) {
|
||||
frame +|= accountedSize(m, inst.op.local_alloc, 0);
|
||||
const size = accountedSize(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);
|
||||
}
|
||||
}
|
||||
|
||||
fn limit(c: *verifier.Context, span: air.Span, name: []const u8, max: u64, found: u64) !void {
|
||||
if (found <= max) return;
|
||||
/// `found == null` means the size computation overflowed.
|
||||
fn limit(c: *verifier.Context, span: air.Span, name: []const u8, max: u64, found: ?u64) !void {
|
||||
if (found) |n| if (n <= max) return;
|
||||
try c.report("E5101", span, "deterministic verifier resource limit exceeded", &.{
|
||||
.{ .name = "limit", .value = .{ .str = name } },
|
||||
.{ .name = "max", .value = .{ .int = @intCast(max) } },
|
||||
.{ .name = "found", .value = .{ .int = @intCast(found) } },
|
||||
if (found) |n| .{ .name = "found", .value = .{ .int = std.math.cast(i64, n) orelse std.math.maxInt(i64) } } else .{ .name = "found", .value = .{ .str = "overflow" } },
|
||||
});
|
||||
}
|
||||
|
||||
/// Every materializable type in the module is at most `max_value_size`
|
||||
/// (spec 08 §7.1). Each distinct oversized type is reported once, at its
|
||||
/// first occurrence.
|
||||
const ValueSizes = struct {
|
||||
c: *verifier.Context,
|
||||
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);
|
||||
if (size == null or size.? > Limits.value_size) {
|
||||
const name = try v.c.typeName(t);
|
||||
const gop = try v.reported.getOrPut(v.c.arena, name);
|
||||
if (!gop.found_existing) try v.c.report("E5101", span, "value type exceeds max_value_size", &.{
|
||||
.{ .name = "limit", .value = .{ .str = "value size bytes" } },
|
||||
.{ .name = "max", .value = .{ .int = Limits.value_size } },
|
||||
.{ .name = "type", .value = .{ .str = name } },
|
||||
if (size) |n| .{ .name = "found", .value = .{ .int = std.math.cast(i64, n) orelse std.math.maxInt(i64) } } else .{ .name = "found", .value = .{ .str = "overflow" } },
|
||||
});
|
||||
return; // inner types of an oversized type are not reported again
|
||||
}
|
||||
}
|
||||
if (t.inner()) |inner| try v.visit(span, inner);
|
||||
}
|
||||
};
|
||||
|
||||
fn checkValueSizes(c: *verifier.Context) !void {
|
||||
var v: ValueSizes = .{ .c = c };
|
||||
const m = c.module;
|
||||
for (m.types, 0..) |d, i| try v.visit(d.span, .{ .named = @intCast(i) });
|
||||
for (m.functions) |f| {
|
||||
for (f.params) |p| try v.visit(f.span, p.ty);
|
||||
try v.visit(f.span, f.ret);
|
||||
for (f.blocks) |b| {
|
||||
for (b.params) |p| try v.visit(b.span, p.ty);
|
||||
for (b.insts) |inst| switch (inst.op) {
|
||||
.local_alloc => |t| try v.visit(inst.span, t),
|
||||
.array_splat => |x| try v.visit(inst.span, x.ty),
|
||||
else => {},
|
||||
};
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
test "too many parameters" {
|
||||
var src: std.ArrayList(u8) = .empty;
|
||||
defer src.deinit(std.testing.allocator);
|
||||
@@ -104,12 +165,31 @@ test "local frame limit" {
|
||||
const h = "module t\nair 0\nlang 0.1\nfeatures [arrays, ownership]\n";
|
||||
try verifier.expectCodes(h ++ "fn @f() -> void\neffects []\nentry:\n %a = local.alloc array<u8, 1048576>\n ret\n", &.{});
|
||||
try verifier.expectCodes(h ++ "fn @f() -> void\neffects []\nentry:\n %a = local.alloc array<u8, 1048576>\n %b = local.alloc u8\n ret\n", &.{"E5101"});
|
||||
try verifier.expectCodes(h ++ "fn @f() -> void\neffects []\nentry:\n %a = local.alloc array<usize, 131073>\n ret\n", &.{"E5101"});
|
||||
// Ten locals of 200 KiB: each value fits, the frame does not.
|
||||
var src: std.ArrayList(u8) = .empty;
|
||||
defer src.deinit(std.testing.allocator);
|
||||
try src.appendSlice(std.testing.allocator, h ++ "fn @f() -> void\neffects []\nentry:\n");
|
||||
for (0..10) |i| try src.print(std.testing.allocator, " %a{d} = local.alloc array<u8, 204800>\n", .{i});
|
||||
try src.appendSlice(std.testing.allocator, " ret\n");
|
||||
try verifier.expectCodes(src.items, &.{"E5101"});
|
||||
}
|
||||
|
||||
test "maximum value size" {
|
||||
const h = "module t\nair 0\nlang 0.1\nfeatures [arrays, ownership]\n";
|
||||
const ok = "fn @f(%a: array<u8, 1048576>) -> void\neffects []\nentry:\n ret\n";
|
||||
try verifier.expectCodes(h ++ ok, &.{});
|
||||
try verifier.expectCodes(h ++ "fn @f(%a: array<u8, 1048577>) -> void\neffects []\nentry:\n ret\n", &.{"E5101"});
|
||||
// Struct sums count; reported once although the type appears twice.
|
||||
try verifier.expectCodes(h ++ "type @B = struct { a: array<u8, 600000>, b: array<u8, 600000> }\nfn @f(%x: @B) -> @B\neffects []\nentry:\n ret %x\n", &.{"E5101"});
|
||||
// Overflow while computing the size is E5101, not a crash.
|
||||
try verifier.expectCodes(h ++ "fn @f(%a: array<array<u64, 4294967296>, 4294967296>) -> void\neffects []\nentry:\n ret\n", &.{"E5101"});
|
||||
}
|
||||
|
||||
test "accounted sizes" {
|
||||
const m: air.Module = .{ .name = "t", .format_version = 0, .lang = .{ 0, 1 }, .features = &.{}, .types = &.{}, .functions = &.{} };
|
||||
try std.testing.expectEqual(@as(u64, 8), accountedSize(m, .{ .int = .isize }, 0));
|
||||
try std.testing.expectEqual(@as(u64, 16), accountedSize(m, .{ .int = .u128 }, 0));
|
||||
try std.testing.expectEqual(@as(u64, 1), accountedSize(m, .bool, 0));
|
||||
try std.testing.expectEqual(@as(?u64, 8), accountedSize(m, .{ .int = .isize }, 0));
|
||||
try std.testing.expectEqual(@as(?u64, 16), accountedSize(m, .{ .int = .u128 }, 0));
|
||||
try std.testing.expectEqual(@as(?u64, 1), accountedSize(m, .bool, 0));
|
||||
try std.testing.expectEqual(@as(u64, 1), tagSize(256));
|
||||
try std.testing.expectEqual(@as(u64, 2), tagSize(257));
|
||||
}
|
||||
@@ -1,4 +1,4 @@
|
||||
; 2 MiB of frame storage exceeds max_local_frame.
|
||||
; Three locals of 400 KiB: each value fits max_value_size, the frame exceeds max_local_frame.
|
||||
; expect: reject E5101
|
||||
; phase: 1
|
||||
; spec: docs/spec/08-verifier.md
|
||||
@@ -10,5 +10,7 @@ features [arrays, ownership]
|
||||
fn @f() -> void
|
||||
effects []
|
||||
entry:
|
||||
%x = local.alloc array<u8, 2097152>
|
||||
%a = local.alloc array<u8, 409600>
|
||||
%b = local.alloc array<u8, 409600>
|
||||
%c = local.alloc array<u8, 409600>
|
||||
ret
|
||||
@@ -0,0 +1,13 @@
|
||||
; Computing the accounted size overflows; the verifier reports E5101 instead of trapping or crashing.
|
||||
; expect: reject E5101
|
||||
; phase: 1
|
||||
; spec: docs/spec/08-verifier.md
|
||||
module test.size_overflow
|
||||
air 0
|
||||
lang 0.1
|
||||
features [arrays, ownership]
|
||||
|
||||
fn @f(%a: array<array<u64, 4294967296>, 4294967296>) -> void
|
||||
effects []
|
||||
entry:
|
||||
ret
|
||||
@@ -0,0 +1,13 @@
|
||||
; array<u8, 1048577> exceeds max_value_size even as a parameter type.
|
||||
; expect: reject E5101
|
||||
; phase: 1
|
||||
; spec: docs/spec/08-verifier.md
|
||||
module test.value_too_large
|
||||
air 0
|
||||
lang 0.1
|
||||
features [arrays, ownership]
|
||||
|
||||
fn @f(%a: array<u8, 1048577>) -> void
|
||||
effects []
|
||||
entry:
|
||||
ret
|
||||
Reference in new issue
Block a user