spec: accept RFC 0006 and integrate effects and capabilities
- spec 05 §9-§12: flat effects, checking rule, capability model, instantiation by parameter name, lifetime, fallible narrowing, intrinsic calls, runtime - spec 07 §8.9, 07a (cap.narrow, intrinsic, run result), 08 stage order, 11 not_instantiated outcome - new reference/effects.md and reference/capabilities.md (kinds, scopes, built-in cap_error) - features effects and capabilities published - Q-011, Q-012 resolved
This commit is contained in:
1 parent
3fd1513c86
commit
fb4338cea6
10 files changed
+245
-13
No files matched your search
@@ -44,7 +44,7 @@ ffi
|
||||
unsafe
|
||||
```
|
||||
|
||||
A caller's effect set must cover transitive callee effects unless the effect is discharged by a verifier-recognized boundary.
|
||||
A caller's effect set must cover transitive callee effects. Effects are flat atoms and format 0 has no discharging boundary (§9).
|
||||
|
||||
## 3. Capabilities
|
||||
|
||||
@@ -103,3 +103,155 @@ The toolchain should be able to emit:
|
||||
## 8. Relation to AIR features
|
||||
|
||||
AIR features (spec 07 §8.1) are a third, separate concept. A feature says which semantics the implementation must understand; it grants no authority and adds no effect. Declaring `features [capabilities, effects, ownership]` does not let a program read a file: the function still needs `effects [fs.read]` and a `cap<fs.read>` value.
|
||||
|
||||
## 9. Effects in AIR (feature `effects`)
|
||||
|
||||
Decision record: RFC 0006.
|
||||
|
||||
Invariants of the authority model:
|
||||
|
||||
1. **No ambient authority.** An operation that acts outside the program's own memory requires a capability value as an operand. There is no global, implicit or default authority.
|
||||
2. **Unforgeability.** A capability value can only originate from the host (as an argument of the entry function) or from an existing capability by an explicit, verifier-known narrowing. No instruction creates one from integers, bytes, constants or other values, and no instruction inspects its representation.
|
||||
3. **No widening.** Narrowing never yields more authority than its input, and never changes the kind of authority.
|
||||
4. **Effect soundness.** If a function can perform effect `E`, directly or through any callee, then `E` is in its declared effect set.
|
||||
5. **Separation.** Features say what the implementation must understand; effects say what code does; capabilities say what an invocation may do. None implies another, and not every effect needs authority.
|
||||
6. **Monotone authority graph.** The only operation that transforms authority is `cap.narrow`. Authority flows from the host down through narrowings; it is never widened, combined, forged or cast.
|
||||
7. **Instance lifetime.** A capability is valid only within the execution instance that received it from the host.
|
||||
|
||||
### 9.1 Effect names
|
||||
|
||||
Effect names come from a closed registry, `reference/effects.md`, initially the list of spec 05 §2. The registry records for each effect whether exercising it requires authority, and which capability kind provides it:
|
||||
|
||||
| Effect | Authority |
|
||||
|---|---|
|
||||
| `mem.alloc`, `mem.map` | `cap<mem.alloc>`, `cap<mem.map>` |
|
||||
| `fs.read`, `fs.write` | `cap<fs.read>`, `cap<fs.write>` |
|
||||
| `io.read`, `io.write` | `cap<io.read>`, `cap<io.write>` |
|
||||
| `net.connect`, `net.listen`, `net.read`, `net.write` | `cap<net.connect>`, `cap<net.listen>`, `cap<net.read>`, `cap<net.write>` |
|
||||
| `clock.read`, `random.read` | `cap<clock.read>`, `cap<random.read>` |
|
||||
| `process.spawn`, `process.env` | `cap<process.spawn>`, `cap<process.env>` |
|
||||
| `task.spawn`, `thread.spawn`, `thread.block` | none |
|
||||
| `atomic`, `atomic.weak` | none |
|
||||
| `ffi`, `unsafe` | none (governed by unsafe boundaries, spec 13) |
|
||||
|
||||
An effect says what a function does; a capability gives power over an external resource. The relation is not one-to-one: effects such as `atomic` or `thread.block` need no authority.
|
||||
|
||||
In a module that declares `effects`, an unregistered name in an `effects [...]` list is `E0004`. New names are added to the registry by the feature that introduces their operations.
|
||||
|
||||
### 9.2 Effects are flat (Q-011)
|
||||
|
||||
Effects are atoms. The dotted spelling is organizational only: `atomic` does not imply `atomic.weak`, `io.write` does not imply `fs.write`, and no effect implies another. A function that performs two effects lists both.
|
||||
|
||||
Reason: implication rules are a second semantics agents must model; a flat set is checked by set inclusion and diagnosed by naming the missing atom. It also keeps weaker mechanisms visible across the call graph: a function declaring `[atomic]` cannot call one declaring `[atomic.weak]` without saying so.
|
||||
|
||||
### 9.3 Checking rule
|
||||
|
||||
In a module that declares `effects`:
|
||||
|
||||
- For every `call @g(…)` in function `f`: `effects(g) ⊆ effects(f)`, otherwise `E3101` for each missing effect.
|
||||
- For every `intrinsic` in `f` (§11): the intrinsic's effect is in `effects(f)`, otherwise `E3101`.
|
||||
- Because every function is checked against its direct callees, the rule holds transitively, including for recursion.
|
||||
|
||||
**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).
|
||||
|
||||
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`.
|
||||
|
||||
## 10. Capabilities in AIR (feature `capabilities`)
|
||||
|
||||
### 10.1 Type
|
||||
|
||||
`cap<K>` is the type of a capability of **kind** `K`. Kinds come from a closed registry, `reference/capabilities.md`, which lists for each kind the effects it authorizes (see §1.1) and its narrowing forms. An unregistered kind is `E0004`.
|
||||
|
||||
A capability value is an **unforgeable reference to one runtime authority object** held by the host. Copying a capability copies the reference, not the authority: all copies denote the same authority object and share its state. For example, two copies of a `cap<mem.alloc>` narrowed to a 64 MiB budget draw on one shared 64 MiB budget, not 64 MiB each (RFC 0008).
|
||||
|
||||
A capability is **opaque**:
|
||||
|
||||
- it is copyable: it may be passed, returned, stored in structs, enums and arrays, and copied;
|
||||
- it cannot be cast to or from an integer or any other type, bit-cast, compared (`eq`/`ne` do not accept it), zero-initialized, given as a constant, serialized or inspected as bytes;
|
||||
- its accounted size (spec 08 §7.1) is 8, but those 64 bits are not a number, an address or a serializable representation.
|
||||
|
||||
**Authority-bearing types.** A type is authority-bearing if it is `cap<K>` or an aggregate or array containing an authority-bearing type. Every rule above is transitive: an authority-bearing type is not zeroable, and any future instruction that exposes the bytes of a value (a `bytes`-of-value view, bit-casts, by-value FFI) MUST reject authority-bearing types. A `struct @Logger { out: cap<io.write> }` can therefore never be turned into `slice<u8>`.
|
||||
|
||||
Copyable authority keeps format 0 simple: a capability is shared by copying it, and authority is limited by narrowing, not by tracking copies. Move-only (one-shot) and revocable capabilities are not part of format 0; they can be added later as new kinds or a new feature.
|
||||
|
||||
### 10.2 Origin: the entry function (Q-012)
|
||||
|
||||
Capabilities enter a program only as parameters of the entry function `@main`:
|
||||
|
||||
```text
|
||||
fn @main(%out: cap<io.write>, %src: cap<fs.read>) -> void
|
||||
effects [fs.read, io.write]
|
||||
```
|
||||
|
||||
- `@main` parameters MUST all be capability types in format 0; the runner rejects other signatures before execution.
|
||||
- Grants are bound **by parameter name**, so two parameters of the same kind can receive different scopes:
|
||||
|
||||
```bash
|
||||
aji run app.air --grant config=fs.read:/etc/app --grant data=fs.read:/var/data
|
||||
```
|
||||
|
||||
For packages the same bindings come from `aji.toml` `[capabilities]` (spec 12).
|
||||
- Before execution the host **instantiates** the program: every capability parameter of `@main` must have exactly one grant whose kind matches, and every grant must name a parameter. Otherwise the program does not start. This is an **environment outcome**, not a trap and not a verifier error; `aji run` exits with code 6 and reports, for the first problem in parameter order:
|
||||
|
||||
```json
|
||||
{"status":"not_instantiated","reason":"missing_capability","parameter":"data","kind":"fs.read"}
|
||||
```
|
||||
|
||||
with `reason` one of `missing_capability`, `kind_mismatch` (fields `parameter`, `kind`, `granted`), `unknown_parameter` (an extra grant, field `parameter`; typos are not ignored) and `invalid_scope` (the host cannot represent the requested scope, fields `parameter`, `scope`).
|
||||
- Compiling or verifying a module never grants authority (spec 05 §6).
|
||||
|
||||
**Startup data (future).** RFC 0006 defines capability instantiation only. Future features MAY allow immutable non-authority startup values (for example `port: u16`) as `@main` parameters; such values are input data, are recorded in the execution record for replay, and grant no authority. Command-line arguments are startup data of this kind. The host environment is not: it often holds secrets, so reading it requires `cap<process.env>` and an intrinsic.
|
||||
|
||||
### 10.3 Lifetime
|
||||
|
||||
A capability obtained at instantiation, and every copy and narrowing of it, is valid from the start of the execution instance until its end. Because capabilities cannot be serialized (§10.1), none can be stored and used by a later run.
|
||||
|
||||
### 10.4 Narrowing
|
||||
|
||||
`cap.narrow` is the only instruction that transforms authority. It is fallible and is a **terminator**, delivering its result to block parameters as `enum.switch` delivers payloads, so a failed narrowing can never be mistaken for a usable capability:
|
||||
|
||||
```text
|
||||
cap.narrow.<form> %c, %arg…, ok -> @ok, err -> @err
|
||||
|
||||
ok(%n: cap<K>): ; same kind K, at most the authority of %c
|
||||
err(%e: cap_error): ; why the restriction was refused
|
||||
```
|
||||
|
||||
`cap_error` is a built-in enum with the variants `invalid_restriction` (the argument is malformed or forbidden, for example a path containing `..`), `not_subset` (the requested scope is not within the current one, for example `/etc` below `/data`) and `unsupported_restriction` (the host cannot enforce this restriction for this capability). It is used with `enum.switch cap_error %e, …`. Built-in enums are spelled without `@` and have registered variants; they are values like any enum.
|
||||
|
||||
The capability registry lists, per kind, which narrowing forms exist and what they mean; for example (specified with the kinds in RFC 0007 and RFC 0008):
|
||||
|
||||
| Kind | Form | Meaning |
|
||||
|---|---|---|
|
||||
| `fs.read`, `fs.write` | `prefix` | restrict to a path below the current scope (`slice<u8>` operand) |
|
||||
| `mem.alloc` | `budget` | restrict to at most N bytes (`usize` operand) (RFC 0008) |
|
||||
|
||||
- A form not listed for the operand's kind is `E3202` (`capability.widen`): there is no instruction that changes kind or adds authority, so any attempt is reported as widening.
|
||||
- Narrowing never traps and never produces a capability with more authority than its input. There is no "empty capability" in format 0: a refused restriction goes to the `err` target.
|
||||
- `ok` and `err` targets follow the block-parameter rules of `enum.switch`: `@ok` takes exactly one parameter of type `cap<K>`, `@err` exactly one of type `cap_error` (`E5002` for the count, `E1001` for the type).
|
||||
- There is no `cap.combine`, union, widening or kind conversion in format 0.
|
||||
|
||||
### 10.5 Using a capability
|
||||
|
||||
An effectful operation takes its capability as an explicit operand (§11). If the operand's kind differs from the kind the operation requires, the error is `E3201` (`capability.missing`) rather than a generic type mismatch, so an agent sees that authority, not data, is missing.
|
||||
|
||||
## 11. Intrinsic calls (feature `capabilities`)
|
||||
|
||||
```text
|
||||
[%r =] intrinsic <name>(%a1, …, %an)
|
||||
```
|
||||
|
||||
- `<name>` is a dotted name from the intrinsic registry (`reference/intrinsics.md`); an unknown name is `E0004`.
|
||||
- Each registry entry specifies, as for any instruction (spec 07 §4): operand types, which operand is the capability and its kind, result type, required effect, ownership and borrow effects on operands, memory read/write set, trap cases, failure model, blocking behavior, and behavior in the `deterministic` profile.
|
||||
- Verification: operand types (`E1001`), capability kind (`E3201`), effect (`E3101`), borrows as for `call` (spec 08 §11.2).
|
||||
- Execution: the interpreter forwards to the host through the capability; the capability's scope is enforced by the host, never by trusting the program.
|
||||
|
||||
This RFC registers no I/O intrinsic yet. RFC 0007 adds the catalogue (console, files, sockets, clock, random) together with the error representation.
|
||||
|
||||
## 12. Runtime
|
||||
|
||||
- A capability is represented as an index into a host-owned table of (kind, scope) entries. The index is not observable by the program.
|
||||
- Narrowing appends a new table entry. Entries are never removed during a run.
|
||||
- In the `deterministic` profile, grants refer to virtual resources (an in-memory file system, a captured stdout, a seeded clock and random source), so runs are reproducible.
|
||||
@@ -254,6 +254,10 @@ A published feature's semantics are immutable. Extending a feature with new sema
|
||||
|
||||
Any other change to format 0 — a different meaning, typing rule, trap behavior, diagnostic code or canonical spelling for an existing construct — requires a new format version (`air 1`) introduced by an RFC. An implementation that supports several format versions selects the semantics by the module's `air` line; it never mixes them.
|
||||
|
||||
### 8.9 Types of the features `effects` and `capabilities`
|
||||
|
||||
`cap<K>` (feature `capabilities`) is an opaque, copyable, authority-bearing value type; `K` is a registered capability kind ([`../reference/capabilities.md`](../reference/capabilities.md)). Built-in enums such as `cap_error` are spelled without `@`, have registered variants and are used with `enum.switch <builtin> %v, …` like declared enums. Rules: spec 05 §9–§12.
|
||||
|
||||
### 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`.
|
||||
|
||||
@@ -213,9 +213,20 @@ New trap code: `T0008 trap.out_of_bounds`.
|
||||
- The interpreter represents a borrow as (storage, path) and a slice as (storage, path, offset, length). It performs no alias or liveness checks at runtime: the verifier guarantees them. Bounds checks run exactly where §4 says.
|
||||
- Behavior is deterministic; storage addresses are never observable.
|
||||
|
||||
## Feature `effects` and feature `capabilities`
|
||||
|
||||
Rules: spec 05 §9–§12. Decision record: RFC 0006.
|
||||
|
||||
| Instruction | Operands → result | Verifier | Runtime |
|
||||
|---|---|---|---|
|
||||
| `cap.narrow.<form> %c, %arg…, ok -> @ok, err -> @err` (terminator) | `cap<K>`, form arguments | form registered for `K` (`E3202`); `@ok` has one parameter `cap<K>`, `@err` one parameter `cap_error` (`E5002` count, `E1001` type) | continues at `@ok` with a narrower capability sharing nothing wider than `%c`, or at `@err` with the reason; never traps |
|
||||
| `[%r =] intrinsic <name>(%a…)` | per registry entry | operand types (`E1001`), capability kind (`E3201`), effect (`E3101`), borrows as for `call`; unknown name `E0004` | forwards to the host through the capability |
|
||||
|
||||
No intrinsic is registered yet (RFC 0007). Effect checking applies to `call` and `intrinsic` in modules that declare `effects` (spec 05 §9.3).
|
||||
|
||||
## Run result
|
||||
|
||||
`aji run` executes the function `@main`, which MUST take no parameters. On return it reports `{"status":"returned","value":V}` where `V` renders the returned value as JSON:
|
||||
`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:
|
||||
|
||||
| Type | Rendering |
|
||||
|---|---|
|
||||
@@ -227,4 +238,4 @@ New trap code: `T0008 trap.out_of_bounds`.
|
||||
|
||||
On a trap it reports `{"status":"trapped", "code":…, "kind":…, "function":…, "block":…, "inst":…}` (spec 01 §6).
|
||||
|
||||
When the runner stops execution because an execution limit is exhausted (spec 11 §7), it reports `{"status":"limit_exceeded","kind":"fuel","limit":N,"steps":N}`. This is an outcome of the execution environment, not of the program, and is not a trap.
|
||||
When instantiation fails (spec 05 §10.2) the program does not start and the runner reports `{"status":"not_instantiated","reason":…,…}`. When the runner stops execution because an execution limit is exhausted (spec 11 §7), it reports `{"status":"limit_exceeded","kind":"fuel","limit":N,"steps":N}`. This is an outcome of the execution environment, not of the program, and is not a trap.
|
||||
@@ -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 frame size), CFG structure (including `local.alloc` placement), SSA, types, 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, initialization, borrows, bounds.
|
||||
@@ -80,12 +80,13 @@ Fuel exhaustion is a decision of the execution environment, not behavior of the
|
||||
- a program's semantics are the same with or without a fuel limit, except that a limited run may end early with `limit_exceeded`;
|
||||
- for a given module, inputs and fuel limit, the outcome is deterministic.
|
||||
|
||||
The three run outcomes are therefore:
|
||||
The run outcomes are therefore:
|
||||
|
||||
```text
|
||||
returned the program finished
|
||||
trapped the program failed (T0xxx)
|
||||
limit_exceeded the environment stopped the program
|
||||
not_instantiated the environment did not start the program (spec 05 §10.2)
|
||||
returned the program finished
|
||||
trapped the program failed (T0xxx)
|
||||
limit_exceeded the environment stopped the program
|
||||
```
|
||||
|
||||
Running without a fuel limit is permitted (for example `aji run program.air`). Conformance, fuzz and stress runs MUST use a fuel limit.
|
||||
|
||||
Reference in new issue
Block a user