Files
aji/docs/design/runtime-value-depth-audit.md
T

6.8 KiB
Raw Blame History

Runtime value-depth audit

Non-normative evidence at 53030ef, separate from Q-059's compiler type traversals. No acceptance limit or runtime rule is proposed here.

Generate TYPE as "array<" * 32768 + "u8" + ",1>" * 32768:

module depth.zero
air 0
lang 0.1
features [arrays, ownership]
fn @main() -> void
effects []
entry:
 %a = local.alloc TYPE
 local.zero %a
 ret

Every array length is one. The accounted value/frame size is one byte, and aji check accepts with no output. aji run --fuel 8 aborts with exit -6, no JSON run record and a symbolized traceback containing 9,997 zeroValue frames. The input has three instructions; the failure occurs inside materialization, not because fuel is exhausted. Tests use the ordinary 8-MiB native stack; only child core capture is disabled. No timeout ends the run.

This confirms native recursion in zeroValue on a verified input. An iterative fix must retain zero initialization, value shape, heap/root lifetimes, allocation failure handling, collection safety and instruction/fuel accounting. A new accepted-depth limit or trap would require a normative decision; none is added by compiler-side repairs.

At the initial baseline, deepCopy/copyAll, valueBytes and collection/serialization were inspection candidates only: the zero-materialization failure prevented probing them. The executed results below supersede that status for copying and collection. valueBytes and output serialization still need depth probes before claiming complete bounded native-stack behavior. Q-060 remains open.

Zero-materialization repair

The implementation now fills zeroable arrays and named structs in the original preorder, using explicit continuations only for pending siblings. Single-child edges need no continuation. Result payloads remain in the execution heap; temporary traversal metadata uses a separate host arena and is released before the next instruction safe point. A partially built value is not published to the local cell if an allocation fails.

The preserved invariant is complete initialization without aliasing independent array/struct payloads. Spec 07a's local.zero verifier rule (zeroable type, no live borrow) and spec 08 §13's whole-local initialization remain unchanged. All leaves have the same integer type/zero bits or false boolean, and aggregate declaration/variant identities and field/element order are unchanged. No runtime rule, Aji allocation authority, opcode, effect, feature, diagnostic code, trap, accepted-depth limit or fuel/scheduler event is introduced. Existing enum/capability zero rejects remain E1001 in AIR and E7006/zero_not_zeroable in source; the public zero_enum/capability_not_zeroable cases and their source analogues remain rejection examples.

Unit tests traverse 32,768 single-element arrays and a 32,768-declaration chain with heterogeneous pending siblings, empty-record leaves and three-byte arrays. A constrained payload allocator fails after allocating the root and proves that the old local value survives the failed instruction. The new zero_structure_siblings AIR/source pair checks complete mixed zeroing followed by a write and unchanged independent fields. Forty generated shallow mixed-type runs produce byte-identical old/new records.

An additional verified deep-program unit test pins fuel 0/1/2 as exact limit records and fuel 3 as successful completion, with matching instruction counts at the ordinary collection threshold.

The original 32,768-level input now returns {"status":"returned","value":null} under fuel 8 at the ordinary collection threshold. This is not a claim that all deep runtime paths work. Two follow-up probes are independently confirmed:

  • With collection before every instruction, the same program aborts without JSON inside Copier.value/items (4,997/4,998 symbolized frames). Its ordinary run succeeds, isolating the collector's recursive payload copy from zero construction.
  • Adding %v = local.load %a immediately after zeroing aborts without JSON inside deepCopy/copyAll (4,998 frames each), under the same fuel 8. Storage-to-SSA copying still requires its own iterative traversal.

Both probes retain the ordinary native stack limit and disable only child core capture, with no wall-clock termination. Evidence comes from the runner exit status and Zig's symbolized stderr, not a saved core dump. Q-060 stays open for copying, sharing-preserving collection/result copying, accounted-value queries and output serialization.

Storage/value-copy repair

DeepCopy now visits aggregate/array payloads in the original preorder using explicit pending-sibling continuations. CopyAll keeps its root-list order and invokes that traversal for each root. Every occurrence gets a fresh payload, even when the input reuses one payload more than once. Aggregate declaration/variant identity and scalar bits/types are preserved; owner, mutex, graph and other identity leaves remain shallow, just as before. This is distinct from the collector, which must preserve sharing. Temporary traversal storage is released before the next instruction safe point; allocation failure does not publish a partial local.store value.

The invariant is unchanged storage/SSA value independence, alongside unchanged ownership identity and explicit loan rules. Spec 07a's local.store/local.load/load/store rules and spec 08's type/initialization/borrow checks are unchanged. The mixed Copy-record/enum/array AIR/source pair copies into and out of local storage, changes one repeated row without affecting the other, restores from a snapshot, and checks that a later store does not mutate the snapshot. Existing read_uninitialized and copy_overlapping AIR cases remain rejected with E1010/E2101; there is no new rejection or diagnostic code. Aji authority/effects, deterministic instruction/fuel counts, accepted syntax/types and runtime traps remain unchanged. No new limit or bundle text is introduced.

Unit tests copy 32,768 alternating array/aggregate levels with pending siblings, empty payloads, variant identities and signed integer bits. They mutate one of two complete copies, independently test reused source payloads within one value, preserve explicit owner/mutex/graph identities, and force a mid-copy payload allocation failure while retaining complete old local storage. A verified deep programme pins exact fuel 0–4 boundaries: local.load still consumes one instruction. Forty-two mixed-type old/new runs have identical records.

The previously crashing 32,768-level zero/local.load/ret programme now returns normally under fuel 8 at the ordinary collection threshold. Adding local.store back to the same local forces an ordinary threshold collection and aborts without JSON in Copier.value/items (4,997/4,998 symbolized frames, zero deepCopy frames). This confirms the next failure without collection instrumentation. The collector/result copier, accounted-value queries and output serialization remain Q-060 work.