From 2c84c0c7cf133d93ff6705efc2a1b7a12cd187c4 Mon Sep 17 00:00:00 2001 From: "A.Shakhmatov" Date: Sun, 4 Oct 2026 13:07:32 +0300 Subject: [PATCH] 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 --- .../rfcs/0005-frame-storage-borrows-arrays.md | 106 +++++++++++++++--- 1 file changed, 90 insertions(+), 16 deletions(-) diff --git a/docs/rfcs/0005-frame-storage-borrows-arrays.md b/docs/rfcs/0005-frame-storage-borrows-arrays.md index 29c53e5..22f1751 100644 --- a/docs/rfcs/0005-frame-storage-borrows-arrays.md +++ b/docs/rfcs/0005-frame-storage-borrows-arrays.md @@ -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` | storage is uninitialized | allocates frame storage, freed at return | +| `%s = local.alloc T` | → `local` | 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` | no live borrow of `%s` (`E2101`); initializes `%s` | writes `%v` | | `local.zero %s` | `local` | `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` | 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`/`slice_mut` | child of `%b` | whole array | | `%n = slice.len %s` | `slice`/`slice_mut` → `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`, `slice`/`slice_mut` | 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`, `slice`/`slice_mut` | 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 k`, or if a fact `%i ref ; E2102: borrow return without `from` ``` +```text +fn @escape(%p: ref) -> ref 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