Files
aji/docs/spec/08-verifier.md
T
dev 1ae6b485d8 Phase 1: frame storage, second-class borrows, arrays (P1-3..P1-10)
- air: types local/ref/mut/array/slice/slice_mut, from-returns,
  multi-result binding, 18 new instructions; feature gating reports
  E0006 feature_not_declared; borrow-typed block parameters gated by
  borrow_joins; printer and round-trip tests for all new forms
- verifier: second-class and zeroable type rules (E1001); local.alloc
  only in entry (E5002); frame size accounting (E5101); stages init
  (E1010), borrow (E2101, E2102, E2105-E2108) and bounds facts (E2103);
  value types shared between stages
- interp: frame cells, places, slices, deep copies between storage and
  SSA values, T0008 bounds traps
- features arrays and ownership supported; implemented_phase = 1
- conformance: 5 accept, 18 reject, 11 run tests for Phase 1, including
  the file-copy core exit program; 6000 mutants without a crash
2026-10-04 13:24:09 +03:00

11 KiB
Raw Blame History

08. Verifier

1. Role

The verifier is part of Aji's language semantics, not an optional lint pass.

A safe module that fails verification is not an executable Aji program.

2. Required passes

A conforming verifier performs, directly or equivalently:

  1. type verification;
  2. SSA/control-flow verification;
  3. initialization verification;
  4. ownership verification;
  5. borrow/alias verification;
  6. region lifetime and generation verification;
  7. island closure verification;
  8. linear-resource verification;
  9. effect verification;
  10. capability verification;
  11. Send/Share verification;
  12. task-scope verification;
  13. synchronization and atomic-ordering validation;
  14. range/bounds verification;
  15. unsafe-boundary verification.

3. Per-value abstract state

The verifier may track:

type
initialization state
ownership state
borrow state
provenance
region and generation
bounds
alignment
integer range
Send/Share/Local properties
task scope
resource state

4. Ownership state machine

Representative states:

AVAILABLE
MOVED
BORROWED_SHARED(n)
BORROWED_MUT
CONSUMED

Transitions are instruction-defined.

A moved or consumed value cannot be reused unless a later operation returns a distinct new value.

5. Path sensitivity

The verifier propagates facts through branches.

Example:

if i < len(data) {
    let x = data[i];
}

Inside the branch, i < len(data) is available as a proof fact.

6. Guards

guard condition records a verifier-recognized condition on the continuing path.

It may justify elimination of later runtime checks.

7. Bounded analysis

Verifier algorithms must have deterministic resource budgets.

If verification exceeds the spec-defined complexity bounds, the verifier emits a stable failure (E5101) rather than silently weakening checks.

Bounds are fixed by this specification, not configurable: a configurable bound would let a flag change which programs are accepted. Format version 0 defines:

Limit Value
functions per module 65 536
type declarations per module 65 536
fields per struct, variants per enum, payload values per variant 256
parameters per function or block, arguments per call or branch 64
blocks per function 16 384
instructions per function 262 144
cases per switch 4 096

The E5101 diagnostic carries the fields limit (name of the limit), max and found.

8. Contracts

Contracts may be classified as:

PROVEN_STATICALLY
CHECKED_AT_RUNTIME
ASSUMED_UNSAFE
UNPROVEN

A high-assurance profile may reject UNPROVEN or ASSUMED_UNSAFE results.

9. Concurrency verification

The verifier must reject:

  • task lifetime escape;
  • transfer of non-Send values to migratable tasks/threads;
  • concurrent mutable aliases not proven disjoint;
  • sharing non-Share values through concurrent immutable access;
  • use of moved channel messages;
  • accessing async I/O buffers while an outstanding operation owns/borrows them;
  • illegal atomic memory orders.

10. Diagnostics

Every normative verifier rejection has a stable machine-readable code.

Diagnostics should include enough structured state for an agent to repair the program without parsing prose.

11. Borrow verification (feature ownership)

Normative rules for frame storage and second-class borrows. Decision record: RFC 0005.

11.1 Owned and lent borrows

  • A borrow created in the function (borrow.*, slice.make, slice.sub, slice.split, or the result of a call to a function with from) is owned by the function.
  • A borrow-typed function parameter is lent: the caller keeps it alive for the whole call. A lent borrow is never ended by the callee (E2107) and need not be ended.
  • Path linearity. On every reachable normal path from the creation of an owned borrow to a ret, the borrow MUST be either ended exactly once or transferred to the caller by a valid ret … from (§11.3); otherwise E2105. The rule is per path, not per instruction text: a borrow may have several borrow.end instructions in different branches, as long as each path executes exactly one.
  • trap ends a path abnormally. Aji has no unwinding, so paths ending in trap carry no obligation.
  • Using or ending a borrow on a path where it has already been ended is E2106.

11.2 Places, overlap and access

Every borrow has a root (a local or a lent parameter), a path from the root (field names, a slice range, or an element), a mode (ref/mut) and a parent (none for borrows taken directly from a local or a parameter).

  • Two paths overlap unless they diverge at different fields of the same struct, or they are the two results of one slice.split (and descendants of each).
  • Element borrows (borrow.index) and sub-slices of the same parent all overlap each other.
  • Creating a borrow conflicts with every live borrow whose path overlaps when either is mut (E2101).
  • A borrow with live children is frozen: it may still create further children that do not conflict, but it cannot be read, written or ended while a live child conflicts with that use (E2101, E2102 for ending).
  • A ref borrow is usable for reading; a mut borrow is usable for reading and writing, in both cases only if not frozen by a conflicting child.
  • There is no implicit conversion between borrow types: a mut/slice_mut borrow is passed where a shared one is expected only through an explicit borrow.ref (E1001 otherwise).
  • call: each borrow argument is lent to the callee for the duration of the call. Arguments are checked as if each were a new borrow of its operand at the call: a mut argument conflicts with any other argument that overlaps it (E2101).

11.3 Returning a borrow

fn @f(…, %p: slice<u8>, …) -> ref<u8> from %p declares that the result borrows from the argument passed for %p.

  • from names a borrow-typed parameter; its mode must allow the result mode (mut result needs a mut/slice_mut parameter) (E1001).
  • A borrow-typed return without from is E2102.
  • ret %b in such a function requires %b to be %p or a descendant of %p. If %b's provenance leads to a local.alloc of this function, the return is E2108 (frame escape); if it leads to another parameter, E2102. At ret, the only live owned borrows may be %b and its owned ancestors, which are transferred to the caller with it; any other live owned borrow is E2105.
  • Escape through aggregates is impossible by construction: borrow types cannot be fields, payloads or array elements (spec 07 §8.8), so struct.make, enum.make and returned aggregates never contain a borrow.
  • from may name only a borrow parameter. Later features may add other escapable sources (for example region-derived borrows in Phase 2); frame-derived borrows never escape.
  • At the call site, the result is a new owned borrow whose parent is the argument passed for %p; the argument cannot be ended before the result (E2102).

11.4 Joins and loops

The complete borrow state MUST be identical on every edge into a block (E2105, reported at the block). The state of each borrow consists of its identity (SSA value), kind (ref/mut), provenance (root), path, parent borrow, and whether it is live or ended. In particular a borrow created inside a loop body is ended before the back edge.

Sections 11–13 apply to the blocks reachable from the entry block; blocks that cannot execute are not analyzed (their SSA and types are still checked, spec 07 §8.2).

Only the set of accepted programs is normative. Because states must match exactly at joins, an implementation can verify a function in one pass over a reverse postorder with one comparison per back edge, but it is not required to work that way.

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

11.6 Borrow-typed block parameters

Borrow-typed block parameters are reserved for the feature borrow_joins. Without it they are rejected with E0006 reason feature_not_declared. Using a feature gate, rather than a permanent type error, keeps the door open without breaking the format-0 freeze (spec 07 §8.7).

12. Bounds facts (feature arrays)

A fact %i < %n is established only by the following table. %i and %n are SSA values of type usize; %c is the comparison result.

Comparison defining %c Established by Fact
%c = lt.usize %i, %n true edge of cbr %c, or guard %c %i < %n
%c = gt.usize %n, %i true edge of cbr %c, or guard %c %i < %n
%c = ge.usize %i, %n false edge of cbr %c %i < %n
%c = le.usize %n, %i false edge of cbr %c %i < %n

Nothing else establishes a fact. In particular le.usize %i, %n and ge.usize %n, %i (that is, i ≤ n) do not, since i = n is out of bounds; comparisons of other types do not; and no arithmetic is performed.

Scope of a fact:

  • from a cbr edge: the target block and every block it dominates, provided the target block's only predecessor is the block containing the cbr (so the edge is the only way in);
  • from guard %c: the instructions after the guard in its block and every block that block dominates.

borrow.index.proven %b, %i is proven exactly when one of these holds at the instruction:

Case Condition
fact on the same slice a fact %i < %n holds where %n = slice.len %b, with %i and %b the same SSA values as the operands
constant index %i = const.usize k and %b has a static length N (a borrow of array<T, N>) with k < N
fact against a constant a fact %i < %n holds where %n = const.usize k, %b has a static length N and k ≤ N

This is the complete normative analysis: an implementation MUST NOT accept more or fewer programs.

13. Initialization (feature ownership)

A local<T> is uninitialized after local.alloc and becomes initialized by local.store or local.zero. local.load, borrow.ref and borrow.mut of a local require it to be initialized on every path from the entry block (E1010). Initialization is tracked for the whole local; there is no partial initialization in format 0.

14. Stage order

Verification stages run in this order, stopping after the first stage that reports an error (spec 07 §8.6): resource limits (including frame size), CFG structure (including local.alloc placement), SSA, types, initialization, borrows, bounds.