Add the error_values feature and error.const through specification, AIR, verification, interpretation and source lowering. Error values are Copy data with no allocation, effects or authority. Keep enum.make semantics frozen. Cover all registered tags, feature negotiation, payload/type errors and authority rejection in AIR/source; verify canonical and hosted CLI behavior. Record reference-bundle costs and the map preflight-validation prerequisite.
250 lines
13 KiB
Markdown
250 lines
13 KiB
Markdown
# 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:
|
||
|
||
```text
|
||
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
|
||
|
||
For `error.const` (feature `error_values`, spec 05 §14), verification introduces one available Copy value of the selected built-in error type. It MUST NOT alter authority, resource, memory, borrow or concurrency state. The static family/tag and ordinary result-use typing MUST be checked.
|
||
|
||
Representative states:
|
||
|
||
```text
|
||
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:
|
||
|
||
```aji
|
||
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 |
|
||
| `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`; 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 |
|
||
| `buffer<T>` (linear_buffers) | 24 |
|
||
|
||
`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
|
||
|
||
Contracts may be classified as:
|
||
|
||
```text
|
||
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, at the two results of one `slice.split`, or at two different region allocations (spec 03 §20.2) (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 (§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
|
||
|
||
Borrow-typed block parameters are reserved for the feature `borrow_joins`, with one exception: the single parameter of the `ok` target of `region.new`/`region.array` (spec 03 §20), provided every edge into that block is such an `ok` edge. 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`, `guard %c`, or `assume %c` (feature `unsafe`) | `%i < %n` |
|
||
| `%c = gt.usize %n, %i` | true edge of `cbr %c`, `guard %c`, or `assume %c` (feature `unsafe`) | `%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 value and frame size), CFG structure (including `local.alloc` placement), SSA, types (including capability kinds and narrowing forms), effects, unsafe boundaries (spec 13 §6.4), initialization, borrows (including task loans and scope structure, spec 04 §33–§34; shield stacks, guards and the critical-section rule, spec 04 §37–§41), bounds.
|