Files
aji/docs/spec/13-contracts-and-unsafe.md
dev 2bbbd2135c rfc: 0015 scalar atomics and the Aji memory model accepted (revision 2) and integrated into the spec
The Aji RC11 profile (RC11 with NO-THIN-AIR; stronger than ISO C11/C++20) as
normative axioms in spec 04 §20; DRF-SC; linear atomic<T> for AtomicScalar
with atomic.new/atomic.take; ref<atomic<T>> operands; ordering tables incl.
cmpxchg failure table and two results; effects [atomic] / [atomic, atomic.weak];
target atomic_widths and E4203; E4202; enumerator as oracle (observed ⊆ allowed,
exact equality on the normative litmus corpus). Feature atomics stays reserved
until P5-2a. Q-015 resolved; record/replay and Q-017 moved to Phase 8.
RFC 0003 superseded. AIR bundle +1.9 %.
2026-10-06 00:30:35 +03:00

6.6 KiB

13. Contracts and Unsafe Boundaries

1. Contracts

Functions may declare preconditions and postconditions. Types/modules may declare invariants.

fn append(log: mut<Log>, data: slice<u8>) -> Result<u64, Error>
requires {
    len(data) <= 16 MiB
}
ensures {
    result.is_ok() => log.offset >= old(log.offset)
}
effects [fs.write]
{
    ...
}

The verifier reports each contract as one of:

PROVEN_STATICALLY
CHECKED_AT_RUNTIME
ASSUMED_UNSAFE
UNPROVEN

A high-assurance profile may reject runtime-only or unproven obligations.

2. Assertions and guards

assert condition is a runtime-visible correctness check unless statically proven. Failure traps.

guard condition both checks the condition as required by its form and introduces a path fact to the verifier.

assume condition is unsafe-only because it introduces a proof fact without checking it.

3. Unsafe

Unsafe operations occur only in an explicit unsafe block/function/module.

Unsafe does not disable the type system globally. It enables specific operations whose safety obligations cannot be established by the normal verifier.

Typical unsafe facilities:

addr<T> dereference
pointer arithmetic
FFI
direct syscalls
inline assembly
manual Send/Share declarations
custom synchronization primitives

4. Unsafe proof obligations

Unsafe code must document applicable obligations, including:

  • valid allocation/provenance;
  • alignment;
  • initialization;
  • lifetime;
  • aliasing;
  • synchronization;
  • ABI assumptions;
  • resource-state assumptions.

Violation of an unsafe precondition may cause Undefined Aji Behavior.

Synchronization obligations are stated against the memory model (spec 04 §20–§21): an unsafe operation must not create a data race. In particular, an operation that mixes atomic and ordinary access to one location must keep every such pair ordered by happens-before. Format 0 has no unsafe operation on atomic cells (spec 04 §28).

5. Unsafe dependency report

The toolchain should produce a machine-readable report containing:

application unsafe blocks
unsafe standard-library modules
raw dereference sites
inline assembly sites
FFI boundaries
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

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.

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:

{"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.