- 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
8.0 KiB
RFC 0009: Unsafe Operations and Trusted Boundaries
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.
Motivation
Safe AIR proves or checks every safety condition. Systems code sometimes needs to rely on a fact the verifier cannot prove — and agents need to see exactly where that happens. Spec 13 requires unsafe operations to appear only inside explicit unsafe code, with documented proof obligations and an audit report. Spec 05 lists an unsafe effect. What is missing is (a) the first unsafe operations, (b) how unsafe code can be offered behind a safe interface, and (c) the verifier rule tying them together.
Invariants
- Visible unsafety. Every unsafe operation is either inside a function that declares the effect
unsafeor inside atrustedfunction. Nowhere else. - Contagion until a boundary. The effect
unsafepropagates to callers like any flat effect (spec 05 §9.3). The only construct that stops it is atrustedfunction — the single effect-discharging boundary of format 0, as foreseen by RFC 0006 §1.3. - Documented obligations. A
trustedfunction states, in its header, the proof obligation it relies on. The text has no semantics; it is part of the audit surface. - 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.
- Auditability. Every unsafe operation and every
trustedfunction appears in the unsafe report.
1. Feature unsafe
Depends on arrays, effects, ownership.
1.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.
1.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.
1.3 Trusted functions
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
trustedline follows theeffectsline and carries one string literal: the proof obligation. An empty string isE0001. - A
trustedfunction may contain unsafe operations and callunsafefunctions without declaringunsafe; to its callers it is an ordinary safe function. - A
trustedfunction MUST NOT listunsafein its effects (E6001): it is the boundary, not part of the unsafe region. - Without the feature
unsafe, atrustedline isE0006(feature_not_declared).
1.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.
1.5 Syntax
String literals are added to the lexer for this one position: " … " with the escapes \", \\, \n; no other escapes, no line breaks inside.
function = "fn" global "(" … ")" "->" type [ "from" local ] NL
"effects" "[" … "]" NL
[ "trusted" string NL ]
block { block } ;
2. Unsafe report
aji check --report unsafe FILE prints, after the diagnostics, one JSON object per line and in source order:
{"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.
3. 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.
Valid example
The @get function above, called from safe code:
fn @sum(%s: slice<u8>) -> u8
effects []
entry:
...
%v = call @get(%s, %i) ; safe call into a trusted boundary
Rejected examples
fn @f(%s: slice<u8>, %i: usize) -> u8
effects []
entry:
%e = borrow.index.unchecked %s, %i ; E6001: no unsafe effect, not trusted
fn @g() -> void
effects []
entry:
call @raw() ; E6001: @raw declares unsafe
ret
fn @raw() -> void
effects [unsafe]
entry:
...
fn @h() -> void
effects [unsafe]
trusted "…" ; E6001: a boundary cannot be unsafe itself
Diagnostics
| Code | Use |
|---|---|
| E6001 | unsafe operation or call to an unsafe function outside an unsafe or trusted function; trusted function declaring unsafe (field reason: outside_boundary, trusted_declares_unsafe) |
| E0001 | empty obligation string |
| E0006 | trusted or an unsafe operation without the feature unsafe |
| T0009 | unsafe precondition violated (detected) |
Compatibility
Additive under spec 07 §8.7: new feature unsafe, new instructions, the trusted header line, string literals in that position, new trap code T0009.
Spec integration
On acceptance: spec 13 §2–§5 (operations, boundaries, report), spec 05 §9.3 (the one discharging boundary), spec 07 §8 (syntax), spec 07a (instructions), spec 08 (stage: unsafe after effects), spec 01 §6 / error codes (T0009), reference/air-text.ebnf, reference/air-features.md, reference/error-codes.md.
Alternatives considered
- Unsafe blocks inside safe functions: AIR has no lexical blocks; a function is the natural unit, and agents can extract the unsafe part into a small
trustedfunction. - No contagion (unsafe never propagates): callers of unsafe code would look safe without any visible boundary. Rejected.
- Contagion without a boundary: every program using one unchecked access would carry
unsafeup to@main. Thetrustedfunction is the explicit, auditable place where responsibility is taken. - Obligations as structured contracts: belongs to the contract system (spec 13 §1); a string is enough to make the obligation visible now and can later be paired with a contract.
- Trapping only in debug builds: the reference interpreter has no build modes (no flags change semantics); it always detects.
Unresolved questions
- Whether
trustedfunctions should be limited to module-private use until contracts can state their preconditions formally (proposed: no restriction in format 0).