spec: accept RFC 0009 and integrate unsafe boundaries

- spec 13 §6-§8: unsafe operations (assume, borrow.index.unchecked),
  contagious unsafe effect, trusted boundary with obligation, E6001,
  unsafe report, profiles as policy
- spec 05 §9.3: trusted is the one discharging boundary; spec 07 §8.10
  trusted syntax; 07a, 08 stage order and assume facts
- T0009, E6001 reasons; feature unsafe published
This commit is contained in:
dev committed 2026-10-04 14:38:53 +03:00
1 parent 17ddb98597
commit c510a37cf9
9 files changed
+105 -7

No files matched your search

+1
View File
@@ -45,6 +45,7 @@ Declaring `features [capabilities, effects, ownership]` grants no authority. It
| `resources` | published | 2 | `capabilities`, `effects`, `ownership` | linear resources `file<read>`, `file<write>`, `file<append>`, `listener`, `stream`; built-in `io_error`; I/O intrinsics (spec 06 §6–§7, `reference/intrinsics.md`) |
| `sync` | reserved | 3 | `ownership` | `Mutex<T>`, `RwLock<T>`, linear guards |
| `tasks` | reserved | 3 | `ownership` | structured task scopes, spawn/join/cancel, `Send`/`Share` |
| `unsafe` | published | 2 | `arrays`, `effects`, `ownership` | `assume`, `borrow.index.unchecked`, the `trusted` boundary, the effect `unsafe` (spec 13 §6–§8) |
`capabilities` depends on `effects` so that every module that can perform an external operation is also effect-checked.
+5 -1
View File
@@ -37,6 +37,7 @@ variant = ident [ "(" [ type { "," type } ] ")" ] ;
function = "fn" global "(" [ param_list ] ")" "->" type [ "from" local ] NL (* from: feature ownership *)
"effects" "[" [ path_list ] "]" NL
[ "trusted" string NL ] (* feature unsafe *)
block { block } ;
param_list = param { "," param } ;
param = local ":" type ;
@@ -81,7 +82,9 @@ arrays_op = "array.splat" type local
| "slice.sub" local "," local "," local
| "slice.split" local "," local
| "slice.copy" local "," local
| "guard" local ;
| "guard" local
| "assume" local (* feature unsafe *)
| "borrow.index.unchecked" local "," local ; (* feature unsafe *)
arith_op = ( "add" | "sub" | "mul" ) "." ( "checked" | "wrap" ) "." int_type
| ( "div" | "rem" ) ".checked." int_type
@@ -121,3 +124,4 @@ name_char = letter | digit | "_" ;
int_lit = [ "-" ] ( dec_digits | "0x" hex_digits | "0b" bin_digits ) ;
(* "_" may separate digits; it carries no meaning. *)
version_lit = digit { digit } "." digit { digit } ;
string = '"' { string_char | '\\"' | '\\\\' | '\\n' } '"' ; (* not empty; only in `trusted` *)
+2 -1
View File
@@ -73,7 +73,7 @@ Draft registry. Codes are stable once released in a tagged compiler version.
| Code | Kind | Meaning |
|---|---|---|
| E6001 | unsafe.required | Operation requires explicit unsafe boundary |
| E6001 | unsafe.required | Operation requires explicit unsafe boundary; field `reason`: `outside_boundary`, `trusted_declares_unsafe` |
| E6101 | abi.unaligned_reference | Normal reference requested from insufficiently aligned storage |
## T0xxx — Runtime traps
@@ -90,3 +90,4 @@ Reported in trap records (spec 01 §6), not by the verifier.
| T0006 | trap.cast_out_of_range | Checked cast value not representable in target type |
| T0007 | trap.call_depth | Call depth limit (1 024 frames) exceeded |
| T0008 | trap.out_of_bounds | Runtime bounds check failed |
| T0009 | trap.unsafe_precondition | Unsafe precondition violated (detected) |
+2 -1
View File
@@ -1,8 +1,9 @@
# RFC 0009: Unsafe Operations and Trusted Boundaries
Status: Proposed
Status: Accepted
Scope: AIR format 0, feature `unsafe`
Language version: 0.1
Integrated into: spec 05 §9.3; spec 07 §8.10; spec 07a; spec 08 §12, §14; spec 13 §6–§8; `reference/air-text.ebnf`, `reference/error-codes.md`, `reference/air-features.md`
Supersedes: —
Realizes spec 13 §2–§5 and spec 07 §7 for format 0. Raw addresses, FFI, syscalls and manual `Send`/`Share` declarations come later (Phase 3 and Phase 7) as further unsafe operations of this feature or of features depending on it; this RFC fixes the boundary mechanism they will all use.
+1 -1
View File
@@ -154,7 +154,7 @@ In a module that declares `effects`:
**Indirect calls (normative for future features).** When function values are introduced, the effect set MUST be part of the function type, for example `fnptr<(u8) -> void effects [io.write]>`, and an indirect call is checked against that set exactly like a direct call. Otherwise dynamic calls would break transitivity.
There is **no effect discharge** in format 0: no construct removes an effect from what a caller must declare. A later feature may introduce a discharging boundary only by specifying it explicitly (Q-011).
There is **no effect discharge** in format 0 except one: a `trusted` function (feature `unsafe`, spec 13 §6.3) discharges the effect `unsafe`. No other construct removes an effect from what a caller must declare; a later feature may introduce another discharging boundary only by specifying it explicitly (Q-011).
In a module that does not declare `effects`, `effects [...]` lists are recorded and not checked ([`../reference/air-features.md`](../reference/air-features.md)). Such a module cannot contain an effectful operation, because every intrinsic belongs to `capabilities`, which depends on `effects`.
+4
View File
@@ -262,6 +262,10 @@ Feature `resources`: `file<read>`, `file<write>`, `file<append>`, `listener`, `s
Feature `heap`: `own<T>` and `own_array<T>` (linear owners, `T` a value type) and the built-in enum `alloc_error`. Feature `regions`: `region` (linear). Rules: spec 03 §18–§21. Linear values — resources, owners and regions — follow the consumption rules of spec 06 §6.2–§6.3.
### 8.10 Syntax of the feature `unsafe`
A function header may carry, after its `effects` line, a line `trusted "<obligation>"`. String literals exist only in this position: `"` … `"` with the escapes `\"`, `\\` and `\n`, no line breaks inside, not empty (`E0001`). Rules: spec 13 §6.
### 8.8 Types and syntax of the features `ownership` and `arrays`
Feature membership: `local<T>`, `ref<T>`, `mut<T>` and the `from` clause belong to `ownership`; `array<T, N>`, `slice<T>`, `slice_mut<T>` and multi-result binding belong to `arrays`. Using them without declaring the feature is `E0006` reason `feature_not_declared`.
+9
View File
@@ -246,6 +246,15 @@ Instructions, budget accounting and verifier rules: spec 03 §18–§20. Summary
`borrow.ref`/`borrow.mut` accept owners and regions as roots: `own<T>` gives `ref<T>`/`mut<T>`, `own_array<T>` gives `slice<T>`/`slice_mut<T>`, `region` gives `ref<region>`/`mut<region>`.
## Feature `unsafe`
| Instruction | Operands → result | Precondition | Reference interpreter |
|---|---|---|---|
| `assume %c` | `bool` | `%c` is true; establishes a fact like `guard` (spec 08 §12) | traps `T0009` if false |
| `%e = borrow.index.unchecked %b, %i` | as `borrow.index` | `%i <` length of `%b` | traps `T0009` if violated |
Allowed only in functions that declare the effect `unsafe` or are `trusted` (`E6001`, spec 13 §6.4).
## Run result
`aji run` executes the function `@main`, whose parameters MUST all be capabilities, bound by name at instantiation (spec 05 §10.2); without the `capabilities` feature it takes no parameters. On return it reports `{"status":"returned","value":V}` where `V` renders the returned value as JSON:
+3 -3
View File
@@ -215,8 +215,8 @@ A **fact** `%i < %n` is established only by the following table. `%i` and `%n` a
| 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 = 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` |
@@ -243,4 +243,4 @@ A `local<T>` is uninitialized after `local.alloc` and becomes initialized by `lo
## 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, initialization, borrows, bounds.
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, bounds.
+78
View File
@@ -84,3 +84,81 @@ manual Send/Share declarations
```
This report is part of the audit surface for agent-generated systems.
## 6. Unsafe code in AIR (feature `unsafe`)
Decision record: RFC 0009.
Invariants:
1. **Visible unsafety.** Every unsafe operation is either inside a function that declares the effect `unsafe` or inside a `trusted` function. Nowhere else.
2. **Contagion until a boundary.** The effect `unsafe` propagates to callers like any flat effect (spec 05 §9.3). The only construct that stops it is a `trusted` function — the single effect-discharging boundary of format 0, as foreseen by RFC 0006 §1.3.
3. **Documented obligations.** A `trusted` function states, in its header, the proof obligation it relies on. The text has no semantics; it is part of the audit surface.
4. **Undefined Aji Behavior only from violated unsafe preconditions** (spec 00 §3). The reference interpreter detects every violation of a format-0 unsafe precondition and traps; native code is not required to.
5. **Auditability.** Every unsafe operation and every `trusted` function appears in the unsafe report.
Depends on `arrays`, `effects`, `ownership`.
### 6.1 Unsafe operations in format 0
| Instruction | Operands → result | Precondition (caller's obligation) | Reference interpreter |
|---|---|---|---|
| `assume %c` | `bool` | `%c` is true | traps `T0009` if false |
| `%e = borrow.index.unchecked %b, %i` | as `borrow.index` | `%i <` length of `%b` | traps `T0009` if violated |
`assume` establishes a bounds fact exactly like `guard` (spec 08 §12) but without a defined runtime check. `borrow.index.unchecked` behaves like `borrow.index.proven` without a proof. Both are verifier-visible: they take part in every other check (types, borrows, effects) as their safe counterparts do.
New trap code: `T0009 trap.unsafe_precondition` — an unsafe precondition was violated. It is reported only by implementations that detect the violation; the reference interpreter always does.
### 6.2 Unsafe functions
A function whose `effects` list contains `unsafe` may contain unsafe operations and call other `unsafe` functions. Its callers must declare `unsafe` too (`E3101`), unless they are `trusted`.
### 6.3 Trusted functions
```text
fn @get(%s: slice<u8>, %i: usize) -> u8
effects []
trusted "callers pass %i < slice.len(%s); every call site in this module is behind a lt.usize check"
entry:
%e = borrow.index.unchecked %s, %i
%v = load %e
borrow.end %e
ret %v
```
- The `trusted` line follows the `effects` line and carries one string literal: the proof obligation. An empty string is `E0001`.
- A `trusted` function may contain unsafe operations and call `unsafe` functions without declaring `unsafe`; to its callers it is an ordinary safe function.
- A `trusted` function MUST NOT list `unsafe` in its effects (`E6001`): it is the boundary, not part of the unsafe region.
- Without the feature `unsafe`, a `trusted` line is `E0006` (`feature_not_declared`).
### 6.4 Verifier rule
An unsafe operation, or a call to a function declaring `unsafe`, in a function that neither declares `unsafe` nor is `trusted`, is `E6001` (`unsafe.required`). The call case is reported as `E6001` rather than `E3101`, so the agent sees that a boundary, not just an effect, is missing.
### 6.5 Syntax
String literals are added to the lexer for this one position: `"` … `"` with the escapes `\"`, `\\`, `\n`; no other escapes, no line breaks inside.
```text
function = "fn" global "(" … ")" "->" type [ "from" local ] NL
"effects" "[" … "]" NL
[ "trusted" string NL ]
block { block } ;
```
## 7. Unsafe report
`aji check --report unsafe FILE` prints, after the diagnostics, one JSON object per line and in source order:
```json
{"report":"unsafe","kind":"trusted_function","function":"get","obligation":"callers pass ..."}
{"report":"unsafe","kind":"operation","function":"get","block":"entry","inst":0,"op":"borrow.index.unchecked","boundary":"get"}
{"report":"unsafe","kind":"unsafe_function","function":"raw_copy"}
```
`boundary` names the `trusted` function the operation sits in, or is `null` inside an `unsafe` function. The full audit report (spec 10 §6) incorporates this report in P2-10.
## 8. Verification profiles
A verification profile MAY reject modules that use `unsafe` at all (the `high-assurance` profile of spec 00 §4). This RFC does not add a flag for it: rejecting is a policy applied to the unsafe report, so the language accepts the same programs everywhere.