rfc: 0005 revision 2 per owner review
1. frame-derived borrows never escape (new E2108); aggregate escape impossible by the second-class rule 2. local.alloc only in the entry block (E5002); frame size limit with a normative accounting table (E5101) 3. path linearity of borrows; full borrow state compared at joins; only the accepted program set is normative 4. slice.sub takes (start, len) with overflow-free checks; slice.split ranges; slice.copy copies len(src) into the prefix of dst, no overlap 5. explicit fact table for borrow.index.proven
This commit is contained in:
1 file changed
+90
-16
@@ -1,6 +1,6 @@
|
||||
# RFC 0005: Frame Storage, Second-Class Borrows and Arrays
|
||||
|
||||
Status: Proposed
|
||||
Status: Proposed (revision 2: owner review items 1–5 applied)
|
||||
Scope: AIR format 0, features `ownership` and `arrays`
|
||||
Language version: 0.1
|
||||
Supersedes: —
|
||||
@@ -19,11 +19,12 @@ Phase 1 must give AIR mutable memory, references and bounds-checked arrays while
|
||||
|
||||
For every program accepted with `ownership` and `arrays`:
|
||||
|
||||
1. **No dangling borrow.** A borrow never outlives the storage it was derived from. Frame storage dies at function return; no borrow of it survives the return.
|
||||
1. **No dangling borrow, no frame escape.** A borrow never outlives the storage it was derived from. A borrow whose provenance leads to a `local.alloc` never leaves the call that executed the `local.alloc`: not by `ret`, and not inside an aggregate (borrows cannot be stored in aggregates at all, §2).
|
||||
2. **Exclusive XOR shared.** For overlapping storage, at any program point there is either one live `mut` borrow (possibly with live descendants) or any number of live `ref` borrows, never both. Direct access to a local (`local.load`, `local.store`, `local.zero`) counts as a short access of the whole local.
|
||||
3. **No uninitialized read.** Storage is read or borrowed only after it is definitely initialized on every path.
|
||||
4. **No out-of-bounds access.** Every element access is either proven in bounds by the spec-defined fact analysis (§6) or checked at runtime and traps with `T0008`.
|
||||
5. **Explicit live ranges.** Every borrow created in a function ends at a visible point: `borrow.end`, or `ret` when it is the returned borrow.
|
||||
5. **Explicit live ranges.** On every normal path, every borrow created in a function ends at a visible point: `borrow.end`, or `ret` when it is the returned borrow.
|
||||
6. **Static frames.** The frame storage of a function is fixed before execution: `local.alloc` occurs only in the entry block, and the total accounted size is bounded.
|
||||
|
||||
## 1. Features
|
||||
|
||||
@@ -70,7 +71,7 @@ Common contract unless stated: no effects, no capabilities, no synchronization.
|
||||
|
||||
| Instruction | Operands → result | Verifier | Runtime |
|
||||
|---|---|---|---|
|
||||
| `%s = local.alloc T` | → `local<T>` | storage is uninitialized | allocates frame storage, freed at return |
|
||||
| `%s = local.alloc T` | → `local<T>` | only in the entry block (`E5002`); storage is uninitialized; counts toward the frame limit (§5.5) | allocates frame storage once per call, freed at return |
|
||||
| `local.store %s, %v` | `local<T>`, `T` | no live borrow of `%s` (`E2101`); initializes `%s` | writes `%v` |
|
||||
| `local.zero %s` | `local<T>` | `T` zeroable: integers, `bool`, arrays and structs of zeroable types (`E1001`); no live borrow (`E2101`); initializes | writes all-zero value |
|
||||
| `%v = local.load %s` | `local<T>` → `T` | initialized (`E1010`); no live `mut` borrow of `%s` (`E2101`) | reads |
|
||||
@@ -90,21 +91,24 @@ Common contract unless stated: no effects, no capabilities, no synchronization.
|
||||
| `%e = borrow.index.proven %b, %i` | as above | `%i < length` proven (§6), otherwise `E2103` | no check |
|
||||
| `%s = slice.make %b` | `ref<[T; N]>`/`mut<[T; N]>` → `slice<T>`/`slice_mut<T>` | child of `%b` | whole array |
|
||||
| `%n = slice.len %s` | `slice<T>`/`slice_mut<T>` → `usize` | — | length |
|
||||
| `%t = slice.sub %s, %lo, %hi` | slice, `usize`, `usize` → same slice type | child of `%s` | traps `T0008` unless `lo ≤ hi ≤ len` |
|
||||
| `%l, %r = slice.split %s, %mid` | slice, `usize` → two slices of the same type | two children of `%s`, disjoint | traps `T0008` unless `mid ≤ len` |
|
||||
| `slice.copy %dst, %src` | `slice_mut<T>`, `slice<T>`/`slice_mut<T>` | both usable (§5.2); they cannot overlap by §5.2 | traps `T0008` unless lengths are equal; copies elements |
|
||||
| `%t = slice.sub %s, %start, %len` | slice, `usize`, `usize` → same slice type | child of `%s` | traps `T0008` unless `start ≤ slice.len(s)` and `len ≤ slice.len(s) − start` (evaluated without overflow); result covers `[start, start + len)` |
|
||||
| `%l, %r = slice.split %s, %at` | slice, `usize` → two slices of the same type | two children of `%s`, disjoint | traps `T0008` unless `at ≤ slice.len(s)`; `l` covers `[0, at)`, `r` covers `[at, len)` |
|
||||
| `slice.copy %dst, %src` | `slice_mut<T>`, `slice<T>`/`slice_mut<T>` | both usable (§5.2); overlapping operands are rejected by the aliasing rules (§5.2), so this is never a `memmove` | traps `T0008` unless `len(dst) ≥ len(src)`; copies exactly `len(src)` elements into the start of `dst` |
|
||||
| `guard %c` | `bool` | adds a fact (§6) | traps `T0002` if false |
|
||||
|
||||
New trap code: `T0008 trap.out_of_bounds`.
|
||||
|
||||
`slice.make` creates a slice only from an existing borrow of an array, covering exactly that array and inheriting its provenance. No instruction creates a slice from an address and a length. An overlap-safe copy, if ever needed, will be a separate instruction (`slice.move`).
|
||||
|
||||
## 5. Borrow rules
|
||||
|
||||
### 5.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.
|
||||
- Every owned borrow MUST be ended exactly once on every path that reaches `ret`, either by `borrow.end` or by being the returned value of `ret … from` (§5.3); otherwise `E2105`. Paths ending in `trap` have no obligation.
|
||||
- Using a borrow after it has been ended is `E2106`.
|
||||
- **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` (§5.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`.
|
||||
|
||||
### 5.2 Places, overlap and access
|
||||
|
||||
@@ -124,24 +128,64 @@ Every borrow has a **root** (a `local` or a lent parameter), a **path** from the
|
||||
|
||||
- `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` (`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`.
|
||||
- `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 (§2), 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`).
|
||||
|
||||
### 5.4 Joins and loops
|
||||
|
||||
The set of live owned borrows MUST be identical on every edge into a block (`E2105`, reported at the block). In particular a borrow created inside a loop body is ended before the back edge. Because states must match exactly, loops need no widening and the analysis is a single pass in reverse postorder plus one check per back edge.
|
||||
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.
|
||||
|
||||
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.
|
||||
|
||||
### 5.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 |
|
||||
| `[T; N]` | `N ×` accounted size of `T` |
|
||||
|
||||
### 5.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).
|
||||
|
||||
## 6. Bounds facts
|
||||
|
||||
A fact `i <u n` (unsigned comparison of two `usize` values) holds at an instruction if one of these dominates the instruction:
|
||||
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.
|
||||
|
||||
- a `cbr` on `%c = lt.usize %i, %n` or `%c = gt.usize %n, %i`, along the true edge, into a block whose only predecessor is that `cbr`'s block;
|
||||
- a `cbr` on `%c = ge.usize %i, %n` or `%c = le.usize %n, %i`, along the false edge, under the same single-predecessor condition;
|
||||
- a `guard %c` with `%c` one of the comparisons above, for the instructions that follow it in its block and the blocks it dominates.
|
||||
| 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` |
|
||||
|
||||
`borrow.index.proven %b, %i` is proven if `%i` is a `const.usize k` and the length of `%b` is a static `N > k`, or if a fact `%i <u %n` holds where `%n` is either `slice.len %b` (the same SSA value `%b`) or a `const.usize k` with `k ≤ N` for a static length `N`. Nothing else is a proof; in particular no arithmetic reasoning is performed. This is the complete, normative analysis: an implementation MUST NOT accept more or fewer programs.
|
||||
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 `[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.
|
||||
|
||||
## 7. Runtime and interpreter
|
||||
|
||||
@@ -160,6 +204,9 @@ A fact `i <u n` (unsigned comparison of two `usize` values) holds at an instruct
|
||||
| **E2105** | borrow.not_ended | owned borrow live at `ret` or live on only some edges into a block |
|
||||
| **E2106** | borrow.use_after_end | borrow used or ended after it was ended |
|
||||
| **E2107** | borrow.not_owned | lent (parameter) borrow ended by the callee |
|
||||
| **E2108** | borrow.escapes_frame | borrow derived from frame storage returned from its function |
|
||||
| E5002 | air.invalid_cfg | (existing) `local.alloc` outside the entry block |
|
||||
| E5101 | verifier.resource_limit | (existing) frame storage above `max_local_frame` |
|
||||
| **T0008** | trap.out_of_bounds | runtime bounds check failed |
|
||||
|
||||
Structured fields: `function`, `block`, `inst`, `borrow` (SSA name), `root`, and for `E2101` the conflicting borrow `conflicts_with`.
|
||||
@@ -209,6 +256,33 @@ entry:
|
||||
fn @leak() -> ref<u8> ; E2102: borrow return without `from`
|
||||
```
|
||||
|
||||
```text
|
||||
fn @escape(%p: ref<u8>) -> ref<u8> from %p
|
||||
effects []
|
||||
entry:
|
||||
%x = local.alloc u8
|
||||
local.zero %x
|
||||
%b = borrow.ref %x
|
||||
ret %b ; E2108: frame storage escapes
|
||||
```
|
||||
|
||||
```text
|
||||
fn @f(%n: usize) -> void
|
||||
effects []
|
||||
entry:
|
||||
br @loop()
|
||||
loop:
|
||||
%x = local.alloc u8 ; E5002: local.alloc outside the entry block
|
||||
...
|
||||
```
|
||||
|
||||
```text
|
||||
%ok = le.usize %i, %len ; i <= len does not prove i < len
|
||||
cbr %ok, @use(), @out()
|
||||
use:
|
||||
%e = borrow.index.proven %s, %i ; E2103
|
||||
```
|
||||
|
||||
```text
|
||||
%x = local.alloc u8
|
||||
%v = local.load %x ; E1010: not initialized
|
||||
|
||||
Reference in new issue
Block a user