Phase 2: unsafe operations and trusted boundaries (RFC 0009)

- air: string literals (trusted line only), trusted obligation on
  functions, assume, borrow.index.unchecked
- verifier: unsafe stage (E6001 outside_boundary /
  trusted_declares_unsafe); effect stage leaves unsafe to it, so trusted
  functions discharge it; assume establishes bounds facts
- interp: T0009 for detected unsafe precondition violations
- aji check --report unsafe: trusted functions with obligations, unsafe
  functions, unsafe operations with their boundary
- feature unsafe published and supported; 10 conformance tests;
  3000 mutants without a crash
This commit is contained in:
dev committed 2026-10-04 14:41:30 +03:00
1 parent c510a37cf9
commit 008654999e
24 files changed
+422 -18

No files matched your search

@@ -0,0 +1,19 @@
; assume establishes a bounds fact like guard, so borrow.index.proven is accepted.
; expect: accept
; phase: 2
; spec: docs/spec/13-contracts-and-unsafe.md
module test.unsafe_assume_fact
air 0
lang 0.1
features [arrays, effects, ownership, unsafe]
fn @f(%s: slice<u8>, %i: usize) -> u8
effects [unsafe]
entry:
%n = slice.len %s
%c = lt.usize %i, %n
assume %c
%e = borrow.index.proven %s, %i
%v = load %e
borrow.end %e
ret %v
@@ -0,0 +1,32 @@
; A trusted function wraps an unchecked access; its callers stay safe.
; expect: accept
; phase: 2
; spec: docs/spec/13-contracts-and-unsafe.md
module test.unsafe_trusted_boundary
air 0
lang 0.1
features [arrays, effects, ownership, unsafe]
fn @get(%s: slice<u8>, %i: usize) -> u8
effects []
trusted "callers pass %i < slice.len(%s)"
entry:
%e = borrow.index.unchecked %s, %i
%v = load %e
borrow.end %e
ret %v
fn @main() -> u8
effects []
entry:
%x = local.alloc array<u8, 4>
%nine = const.u8 9
%a = array.splat array<u8, 4> %nine
local.store %x, %a
%r = borrow.ref %x
%s = slice.make %r
%i = const.usize 1
%v = call @get(%s, %i)
borrow.end %s
borrow.end %r
ret %v
@@ -0,0 +1,19 @@
; A safe function calls an unsafe one without a trusted boundary.
; expect: reject E6001
; phase: 2
; spec: docs/spec/13-contracts-and-unsafe.md
module test.unsafe_call_from_safe
air 0
lang 0.1
features [arrays, effects, ownership, unsafe]
fn @f() -> void
effects []
entry:
call @raw()
ret
fn @raw() -> void
effects [unsafe]
entry:
ret
@@ -0,0 +1,14 @@
; The obligation of a trusted function must not be empty.
; expect: reject E0001
; phase: 2
; spec: docs/spec/13-contracts-and-unsafe.md
module test.unsafe_empty_obligation
air 0
lang 0.1
features [arrays, effects, ownership, unsafe]
fn @f() -> void
effects []
trusted ""
entry:
ret
@@ -0,0 +1,16 @@
; An unchecked access in a function that is neither unsafe nor trusted.
; expect: reject E6001
; phase: 2
; spec: docs/spec/13-contracts-and-unsafe.md
module test.unsafe_outside_boundary
air 0
lang 0.1
features [arrays, effects, ownership, unsafe]
fn @f(%s: slice<u8>, %i: usize) -> u8
effects []
entry:
%e = borrow.index.unchecked %s, %i
%v = load %e
borrow.end %e
ret %v
@@ -0,0 +1,14 @@
; A trusted boundary cannot itself declare the effect unsafe.
; expect: reject E6001
; phase: 2
; spec: docs/spec/13-contracts-and-unsafe.md
module test.unsafe_trusted_declares_unsafe
air 0
lang 0.1
features [arrays, effects, ownership, unsafe]
fn @f() -> void
effects [unsafe]
trusted "nothing to rely on"
entry:
ret
@@ -0,0 +1,14 @@
; trusted belongs to the feature unsafe.
; expect: reject E0006
; phase: 2
; spec: docs/spec/13-contracts-and-unsafe.md
module test.unsafe_trusted_without_feature
air 0
lang 0.1
features [effects]
fn @f() -> void
effects []
trusted "x"
entry:
ret
@@ -0,0 +1,16 @@
; A false assume is a detected unsafe precondition violation.
; expect: run
; trap: T0009
; phase: 2
; spec: docs/spec/13-contracts-and-unsafe.md
module test.unsafe_assume_violated
air 0
lang 0.1
features [arrays, effects, ownership, unsafe]
fn @main() -> void
effects [unsafe]
entry:
%f = const.bool false
assume %f
ret
@@ -0,0 +1,33 @@
; The reference interpreter detects a violated unsafe precondition (T0009), not a bounds trap.
; expect: run
; trap: T0009
; phase: 2
; spec: docs/spec/13-contracts-and-unsafe.md
module test.unsafe_precondition_violated
air 0
lang 0.1
features [arrays, effects, ownership, unsafe]
fn @get(%s: slice<u8>, %i: usize) -> u8
effects []
trusted "callers pass %i < slice.len(%s)"
entry:
%e = borrow.index.unchecked %s, %i
%v = load %e
borrow.end %e
ret %v
fn @main() -> u8
effects []
entry:
%x = local.alloc array<u8, 4>
%nine = const.u8 9
%a = array.splat array<u8, 4> %nine
local.store %x, %a
%r = borrow.ref %x
%s = slice.make %r
%i = const.usize 4
%v = call @get(%s, %i)
borrow.end %s
borrow.end %r
ret %v
@@ -0,0 +1,33 @@
; Calling a trusted boundary within its obligation works.
; expect: run
; result: 9
; phase: 2
; spec: docs/spec/13-contracts-and-unsafe.md
module test.unsafe_trusted_call
air 0
lang 0.1
features [arrays, effects, ownership, unsafe]
fn @get(%s: slice<u8>, %i: usize) -> u8
effects []
trusted "callers pass %i < slice.len(%s)"
entry:
%e = borrow.index.unchecked %s, %i
%v = load %e
borrow.end %e
ret %v
fn @main() -> u8
effects []
entry:
%x = local.alloc array<u8, 4>
%nine = const.u8 9
%a = array.splat array<u8, 4> %nine
local.store %x, %a
%r = borrow.ref %x
%s = slice.make %r
%i = const.usize 3
%v = call @get(%s, %i)
borrow.end %s
borrow.end %r
ret %v