air: parse anonymous type constructors iteratively
This commit is contained in:
1 parent
ba99ad6727
commit
c94e5a0868
4 files changed
+169
-64
No files matched your search
@@ -0,0 +1,38 @@
|
||||
# AIR anonymous type-depth audit
|
||||
|
||||
Non-normative evidence for Q-059, initially at ba99ad6. This audit introduces no syntax-nesting limit, code, feature or acceptance rule. Parser repair preserves format-0 syntax and diagnostics; the verifier and other type traversals remain a separate open obligation.
|
||||
|
||||
## Reproducer and observations
|
||||
|
||||
Generate a parameter type with depth d using `"array<" * d + "u8" + ",1>" * d`:
|
||||
|
||||
```air
|
||||
module type.depth
|
||||
air 0
|
||||
lang 0.1
|
||||
features [arrays, ownership]
|
||||
fn @f(%x: TYPE) -> void
|
||||
effects []
|
||||
entry:
|
||||
ret
|
||||
```
|
||||
|
||||
Replace TYPE with the generated type and call `aji check FILE.air`. Each array has length one; accounted size is one byte. No runtime execution, model experiment or held-out input is involved. Tests used the ordinary 8-MiB native stack; only core-dump capture was disabled for the child. No wall-clock timeout ended a probe.
|
||||
|
||||
| Depth | Input bytes | Before parser repair | After parser repair |
|
||||
|---|---:|---|---|
|
||||
| 64 | 683 | accepted, exit 0 | not rerun |
|
||||
| 256 | 2,411 | accepted, exit 0 | not rerun |
|
||||
| 1,024 | 9,323 | accepted, exit 0 | accepted, exit 0 |
|
||||
| 4,096 | 36,971 | aborted, exit -6 | accepted, exit 0 |
|
||||
| 16,384 | 147,563 | not run | aborted, exit -6 |
|
||||
|
||||
The old 4,096-depth probe emitted no diagnostic JSON and a Zig segmentation-fault traceback before SIGABRT. It contains 3,529 parseType and 3,528 parseCompoundType frames. The new 16,384-depth failure instead contains 5,000 sizeQuery and 5,000 sizeValue frames in verifier/budget.zig. These repeated native frames and stack-dependent depth are evidence of native stack exhaustion, rather than a specified E5101 rejection. No core was captured; diagnosis uses Zig's symbolized stderr traceback. The observation confirms two recursive failure sites, not bounded behavior of every downstream traversal.
|
||||
|
||||
## Parser repair and validation
|
||||
|
||||
Compound syntax is a unary chain. The parser now stores pending constructors and their inner spans in an arena-backed list, then closes/builds them from the innermost outward. File modes and capability kinds remain leaves. Feature checks still precede child parsing; member gates still precede the closing delimiter. The same constructor names, array-length checks, unknown-name behavior and error positions remain. Memory grows with the explicit input depth; no program heap allocation, ABI or language semantics changes.
|
||||
|
||||
Unit tests parse and inspect 32,768 array levels without recursive equality/formatting in the test. Separate 8,192-depth probes retain a single E0004 at the unknown leaf and a single E0001 at the missing outer delimiter. All unit tests, Zig format checks and 1,765/1,765 conformance cases pass. An old/new comparison over all 364 public conformance AIR files preserves exit status, stdout diagnostics and stderr exactly. A further 232 constructor/feature/error probes cover all nineteen unary/array constructors, reserved/unknown leaves, invalid file/capability names and invalid array lengths. These constrain parser compatibility; they are not proof of all input compatibility or a complete Q-059 fix.
|
||||
|
||||
The subsequent budget failure is verified and must be repaired without silently limiting previously permitted syntax. Iterative size evaluation, inner-type budget visiting, property/validity/equality traversals and diagnostic/canonical formatting require separate inspection. Q-059 stays open; resolving parser recursion alone does not resolve verifier processing.
|
||||
@@ -457,4 +457,6 @@ Compatibility impact is explicit: oversized values/frames formerly accepted now
|
||||
|
||||
Source inspection during Q-058 finds recursive `parseType` in `src/air/parse.zig` without an explicit syntax-nesting bound. Size/property traversals also use native recursion over finite anonymous type syntax. **Unverified:** no parser stack-exhaustion reproducer has been run; syntactic finiteness alone does not establish a bound on native stack use.
|
||||
|
||||
2026-10-09 [executed audit](air-type-depth-audit.md): a 36,971-byte input with 4,096 length-one nested arrays aborts with a traceback containing thousands of parseType/parseCompoundType frames, rather than a diagnostic. The iterative parser repair accepts that input and unit-tests 32,768 levels, preserving old/new output on 364 public AIR files and constructor/error probes. A subsequent 16,384-depth check still aborts in verifier sizeQuery/sizeValue; that remaining failure is now verified. The original unverified note describes the state before these probes. No syntax-depth acceptance limit is introduced; Q-059 remains open for size, property, validity, equality and formatting traversal audits.
|
||||
|
||||
Audit parser and subsequent type traversals before claiming bounded processing for arbitrary AIR type syntax. Iterative traversal could preserve accepted syntax; imposing a new acceptance limit instead needs normative spec 08 resource rules and fixed diagnostic facts. Q-058 does not add such a limit or claim to resolve this separate issue.
|
||||
@@ -339,6 +339,8 @@ The final size row is now explicitly classified as `source_early`, retaining its
|
||||
|
||||
## P6-1 source specification consistency (2026-10-09)
|
||||
|
||||
The [Q-059 type-depth audit](../design/air-type-depth-audit.md) confirms a parser native-stack failure on a 37-KB, 4,096-depth AIR parameter type. Pending anonymous constructors now use an explicit arena-backed stack, preserving grammar, feature/member-gate order and diagnostic positions. Unit tests parse 32,768 levels and check deep unknown/malformed leaves; all unit/format checks and 1,765/1,765 conformance cases pass, and 364 public AIR checks preserve exact old/new outputs. The old crashing input now accepts. A 16,384-depth check still fails in the verifier's recursive size evaluation, so Q-059 remains open. No normative/bundle/feature/code change or new acceptance limit; no fresh Claude review was available.
|
||||
|
||||
Spec 15's initial Phase 6A limits now point to its existing extensions. Its introductory diagnostic rule follows §10 and construct-specific codes; widening reports source E7005 while AIR retains E1002, including the corrected Quick Reference entry. Multi-result bindings include complete unpacking, byte escapes agree with the later exact list, and grammar comments locate graph access in §8. Old incremental limits link buffers/borrow returns to §17/§19. No grammar production, semantic rule or implementation changes. Six existing public fixtures confirm the reported code and deterministic successful AIR; Claude independently audited the corrections. The reference bundle rebuilds; source growth is +50/+47/+58 tokens across the three tokenizers, with unchanged Quick Reference token counts, AIR and std (docs/agent/README.md). P6-1/P6-5 remain open for their unimplemented surfaces.
|
||||
|
||||
## P6-6 contract spec gate (2026-10-09)
|
||||
|
||||
+127
-64
@@ -486,8 +486,33 @@ const Parser = struct {
|
||||
return decl;
|
||||
}
|
||||
|
||||
// Anonymous type syntax is a unary chain. Keep its pending delimiters
|
||||
// in arena storage rather than one native call frame per constructor.
|
||||
fn parseType(p: *Parser) Error!air.Type {
|
||||
const t = p.next();
|
||||
var pending: std.ArrayList(TypeFrame) = .empty;
|
||||
var result: air.Type = undefined;
|
||||
while (true) {
|
||||
const t = p.next();
|
||||
if (t.tag == .word and p.peek().tag == .lt) {
|
||||
switch (try p.beginCompoundType(t)) {
|
||||
.leaf => |ty| {
|
||||
result = ty;
|
||||
break;
|
||||
},
|
||||
.wrapper => |frame| {
|
||||
try pending.append(p.arena, frame);
|
||||
continue;
|
||||
},
|
||||
}
|
||||
}
|
||||
result = try p.parseLeafType(t);
|
||||
break;
|
||||
}
|
||||
while (pending.pop()) |frame| result = try p.finishCompoundType(frame, result);
|
||||
return result;
|
||||
}
|
||||
|
||||
fn parseLeafType(p: *Parser, t: Token) Error!air.Type {
|
||||
switch (t.tag) {
|
||||
.global => {
|
||||
if (indexOf(p.type_names.items, t.text)) |i| return .{ .named = @intCast(i) };
|
||||
@@ -495,7 +520,6 @@ const Parser = struct {
|
||||
return .void;
|
||||
},
|
||||
.word => {
|
||||
if (p.peek().tag == .lt) return p.parseCompoundType(t);
|
||||
if (std.meta.stringToEnum(air.IntType, t.text)) |it| return .{ .int = it };
|
||||
if (std.mem.eql(u8, t.text, "bool")) return .bool;
|
||||
if (std.mem.eql(u8, t.text, "void")) return .void;
|
||||
@@ -534,62 +558,42 @@ const Parser = struct {
|
||||
}
|
||||
}
|
||||
|
||||
/// `local<T>`, `ref<T>`, `mut<T>`, `slice<T>`, `slice_mut<T>`, `array<T, N>` (spec 07 §8.8).
|
||||
fn parseCompoundType(p: *Parser, t: Token) Error!air.Type {
|
||||
const TypeWrapper = enum {
|
||||
local,
|
||||
ref,
|
||||
mut,
|
||||
slice,
|
||||
slice_mut,
|
||||
array,
|
||||
own,
|
||||
own_array,
|
||||
task,
|
||||
sender,
|
||||
receiver,
|
||||
mutex,
|
||||
guard,
|
||||
island,
|
||||
frozen,
|
||||
arc,
|
||||
iref,
|
||||
atomic,
|
||||
buffer,
|
||||
};
|
||||
const TypeFrame = struct { kind: TypeWrapper, inner_span: Span };
|
||||
const CompoundStart = union(enum) { leaf: air.Type, wrapper: TypeFrame };
|
||||
|
||||
fn beginCompoundType(p: *Parser, t: Token) Error!CompoundStart {
|
||||
if (eql(t.text, "file")) {
|
||||
try p.requireFeature("resources", t.span, "file");
|
||||
_ = try p.expect(.lt, "`<`");
|
||||
const mode = try p.expect(.word, "`read`, `write` or `append`");
|
||||
_ = try p.expect(.gt, "`>`");
|
||||
const Mode = enum { read, write, append };
|
||||
return .{ .resource = switch (std.meta.stringToEnum(Mode, mode.text) orelse return p.fail(mode, "`read`, `write` or `append`")) {
|
||||
return .{ .leaf = .{ .resource = switch (std.meta.stringToEnum(Mode, mode.text) orelse return p.fail(mode, "`read`, `write` or `append`")) {
|
||||
.read => .file_read,
|
||||
.write => .file_write,
|
||||
.append => .file_append,
|
||||
} };
|
||||
}
|
||||
if (eql(t.text, "own") or eql(t.text, "own_array")) {
|
||||
try p.requireFeature("heap", t.span, t.text);
|
||||
_ = try p.expect(.lt, "`<`");
|
||||
const inner_span = p.peek().span;
|
||||
const inner = try p.parseType();
|
||||
// Nested linear values need `linear_aggregates` (spec 03 §19.2).
|
||||
try p.gateLinearMember(inner, inner_span);
|
||||
_ = try p.expect(.gt, "`>`");
|
||||
const boxed = try p.arena.create(air.Type);
|
||||
boxed.* = inner;
|
||||
return if (eql(t.text, "own")) .{ .own = boxed } else .{ .own_array = boxed };
|
||||
}
|
||||
const Wrapper = enum { task, sender, receiver, mutex, guard, island, frozen, arc, iref, atomic, buffer };
|
||||
if (std.meta.stringToEnum(Wrapper, t.text)) |which| {
|
||||
try p.requireFeature(switch (which) {
|
||||
.task => "tasks",
|
||||
.sender, .receiver => "channels",
|
||||
.mutex, .guard => "sync",
|
||||
.island, .frozen, .iref => "islands",
|
||||
.arc => "arc",
|
||||
.atomic => "atomics",
|
||||
.buffer => "linear_buffers",
|
||||
}, t.span, t.text);
|
||||
_ = try p.expect(.lt, "`<`");
|
||||
// Element rules (spec 04 §38.1) are checked by the type stage.
|
||||
const inner = try p.parseType();
|
||||
_ = try p.expect(.gt, "`>`");
|
||||
const boxed = try p.arena.create(air.Type);
|
||||
boxed.* = inner;
|
||||
return switch (which) {
|
||||
.task => .{ .task = boxed },
|
||||
.sender => .{ .sender = boxed },
|
||||
.receiver => .{ .receiver = boxed },
|
||||
.mutex => .{ .mutex = boxed },
|
||||
.guard => .{ .guard = boxed },
|
||||
.island => .{ .island = boxed },
|
||||
.frozen => .{ .frozen = boxed },
|
||||
.arc => .{ .arc = boxed },
|
||||
.iref => .{ .iref = boxed },
|
||||
.atomic => .{ .atomic = boxed },
|
||||
.buffer => .{ .buffer = boxed },
|
||||
};
|
||||
} } };
|
||||
}
|
||||
if (eql(t.text, "cap")) {
|
||||
try p.requireFeature("capabilities", t.span, "cap");
|
||||
@@ -600,24 +604,36 @@ const Parser = struct {
|
||||
try p.report("E0004", k.span, "unknown capability kind", &.{.{ .name = "name", .value = .{ .str = k.text } }});
|
||||
break :blk 0;
|
||||
};
|
||||
return .{ .cap = idx };
|
||||
return .{ .leaf = .{ .cap = idx } };
|
||||
}
|
||||
const Kind = enum { local, ref, mut, slice, slice_mut, array };
|
||||
const kind = std.meta.stringToEnum(Kind, t.text) orelse {
|
||||
const kind = std.meta.stringToEnum(TypeWrapper, t.text) orelse {
|
||||
try p.report("E0004", t.span, "unknown type", &.{.{ .name = "name", .value = .{ .str = t.text } }});
|
||||
return error.SyntaxError;
|
||||
};
|
||||
try p.requireFeature(switch (kind) {
|
||||
.local, .ref, .mut => "ownership",
|
||||
.slice, .slice_mut, .array => "arrays",
|
||||
.own, .own_array => "heap",
|
||||
.task => "tasks",
|
||||
.sender, .receiver => "channels",
|
||||
.mutex, .guard => "sync",
|
||||
.island, .frozen, .iref => "islands",
|
||||
.arc => "arc",
|
||||
.atomic => "atomics",
|
||||
.buffer => "linear_buffers",
|
||||
}, t.span, t.text);
|
||||
_ = try p.expect(.lt, "`<`");
|
||||
const inner_span = p.peek().span;
|
||||
const inner = try p.parseType();
|
||||
// Storage and arrays of linear values need `linear_aggregates`; a
|
||||
// borrow of a linear value (`mut<file<read>>`) is fine.
|
||||
if (kind != .ref and kind != .mut) try p.gateLinearMember(inner, inner_span);
|
||||
if (kind == .array) {
|
||||
return .{ .wrapper = .{ .kind = kind, .inner_span = p.peek().span } };
|
||||
}
|
||||
|
||||
fn finishCompoundType(p: *Parser, frame: TypeFrame, inner: air.Type) Error!air.Type {
|
||||
// Preserve the old gate/diagnostic order: member gates precede the
|
||||
// closing delimiter, while general wrappers add no member gate here.
|
||||
switch (frame.kind) {
|
||||
.own, .own_array, .local, .slice, .slice_mut, .array => try p.gateLinearMember(inner, frame.inner_span),
|
||||
else => {},
|
||||
}
|
||||
if (frame.kind == .array) {
|
||||
_ = try p.expect(.comma, "`,`");
|
||||
const n_tok = try p.expect(.number, "array length");
|
||||
const n = std.fmt.parseInt(u64, n_tok.text, 0) catch 0;
|
||||
@@ -632,13 +648,9 @@ const Parser = struct {
|
||||
_ = try p.expect(.gt, "`>`");
|
||||
const boxed = try p.arena.create(air.Type);
|
||||
boxed.* = inner;
|
||||
return switch (kind) {
|
||||
.local => .{ .local = boxed },
|
||||
.ref => .{ .ref = boxed },
|
||||
.mut => .{ .mut = boxed },
|
||||
.slice => .{ .slice = boxed },
|
||||
.slice_mut => .{ .slice_mut = boxed },
|
||||
return switch (frame.kind) {
|
||||
.array => unreachable,
|
||||
inline else => |kind| @unionInit(air.Type, @tagName(kind), boxed),
|
||||
};
|
||||
}
|
||||
|
||||
@@ -1777,6 +1789,57 @@ test "parse types, aggregates and calls" {
|
||||
try std.testing.expectEqual(@as(u32, 1), m.functions[0].blocks[2].insts[1].op.call.callee);
|
||||
}
|
||||
|
||||
fn deepTypeSource(arena: Allocator, depth: usize, leaf: []const u8, closed: usize) ![]const u8 {
|
||||
var src: std.ArrayList(u8) = .empty;
|
||||
try src.appendSlice(arena, "module t\nair 0\nlang 0.1\nfeatures [arrays, ownership]\nfn @f(%x: ");
|
||||
for (0..depth) |_| try src.appendSlice(arena, "array<");
|
||||
try src.appendSlice(arena, leaf);
|
||||
for (0..closed) |_| try src.appendSlice(arena, ",1>");
|
||||
try src.appendSlice(arena, ") -> void\neffects []\nentry:\n ret\n");
|
||||
return src.items;
|
||||
}
|
||||
|
||||
test "deep anonymous types parse without native recursion" {
|
||||
var scratch = std.heap.ArenaAllocator.init(std.testing.allocator);
|
||||
defer scratch.deinit();
|
||||
const a = scratch.allocator();
|
||||
const depth = 32_768;
|
||||
const m = try parseOk(a, try deepTypeSource(a, depth, "u8", depth));
|
||||
var t = m.functions[0].params[0].ty;
|
||||
for (0..depth) |_| {
|
||||
try std.testing.expect(t == .array);
|
||||
try std.testing.expectEqual(@as(u64, 1), t.array.len);
|
||||
t = t.array.elem;
|
||||
}
|
||||
try std.testing.expectEqual(air.IntType.u8, t.int);
|
||||
}
|
||||
|
||||
test "deep unknown leaf retains one resolution diagnostic and its position" {
|
||||
var scratch = std.heap.ArenaAllocator.init(std.testing.allocator);
|
||||
defer scratch.deinit();
|
||||
const a = scratch.allocator();
|
||||
const depth = 8_192;
|
||||
const r = try parse(a, "deep.air", try deepTypeSource(a, depth, "unknown_type", depth));
|
||||
try std.testing.expect(r.module != null);
|
||||
try std.testing.expectEqual(@as(usize, 1), r.diagnostics.len);
|
||||
try std.testing.expectEqualStrings("E0004", r.diagnostics[0].code.id);
|
||||
try std.testing.expectEqual(@as(u32, 5), r.diagnostics[0].span.?.line);
|
||||
try std.testing.expectEqual(@as(u32, 11 + 6 * depth), r.diagnostics[0].span.?.col);
|
||||
}
|
||||
|
||||
test "deep missing outer array delimiter retains one syntax error" {
|
||||
var scratch = std.heap.ArenaAllocator.init(std.testing.allocator);
|
||||
defer scratch.deinit();
|
||||
const a = scratch.allocator();
|
||||
const depth = 8_192;
|
||||
const r = try parse(a, "deep.air", try deepTypeSource(a, depth, "u8", depth - 1));
|
||||
try std.testing.expect(r.module == null);
|
||||
try std.testing.expectEqual(@as(usize, 1), r.diagnostics.len);
|
||||
try std.testing.expectEqualStrings("E0001", r.diagnostics[0].code.id);
|
||||
try std.testing.expectEqual(@as(u32, 5), r.diagnostics[0].span.?.line);
|
||||
try std.testing.expectEqual(@as(u32, 11 + 6 * depth + 2 + 3 * (depth - 1)), r.diagnostics[0].span.?.col);
|
||||
}
|
||||
|
||||
test "resolution diagnostics" {
|
||||
try expectCodes(header ++ "fn @f() -> u8\neffects []\nentry:\n ret %x\n", &.{"E5001"});
|
||||
try expectCodes(header ++ "fn @f() -> u8\neffects []\nentry:\n br @nowhere()\n", &.{"E5002"});
|
||||
|
||||
Reference in new issue
Block a user