Phase 2: effects and capabilities (RFC 0006)
- air: cap<K> and built-in enums (cap_error) as types; enum.switch over built-in enums; cap.narrow terminator with ok/err targets; intrinsic syntax (registry still empty: E0004); registries for effects, capability kinds and built-in enums, synced with reference docs - verifier: effect stage (E3101, flat, direct callees); cap.narrow form check (E3202) and ok/err parameter rules (E5002, E1001); capabilities are copyable, not zeroable, not comparable; sizes cap 8, built-in tag - interp: host authority table; @main instantiation from grants by parameter name with not_instantiated reasons; capabilities render as opaque - CLI: --grant P=KIND[:SCOPE], exit code 6; runner: grant: and not_instantiated: headers, pending only for unsupported features - conformance: 18 Phase 2 tests; 4000 mutants without a crash
This commit is contained in:
1 parent
fb4338cea6
commit
9c29a80b72
41 files changed
+908
-58
No files matched your search
+103
-6
@@ -27,6 +27,11 @@ pub const Value = union(enum) {
|
||||
place: Place,
|
||||
/// `slice<T>` / `slice_mut<T>`.
|
||||
slice: Slice,
|
||||
/// `cap<K>`: index into the host's authority table. Never observable
|
||||
/// by the program (spec 05 §10.1).
|
||||
cap: u32,
|
||||
/// Value of a built-in enum such as `cap_error`.
|
||||
builtin: struct { which: air.authority.Builtin, variant: u32 },
|
||||
|
||||
pub const Aggregate = struct {
|
||||
decl: u32,
|
||||
@@ -57,22 +62,70 @@ pub const Outcome = union(enum) {
|
||||
trapped: Trap,
|
||||
/// The runner's fuel ran out (spec 11 §7). Not a trap.
|
||||
limit_exceeded: struct { limit: u64, steps: u64 },
|
||||
/// The host did not start the program (spec 05 §10.2). Not a trap.
|
||||
not_instantiated: NotInstantiated,
|
||||
};
|
||||
|
||||
pub const NotInstantiated = struct {
|
||||
reason: []const u8,
|
||||
parameter: []const u8,
|
||||
kind: ?[]const u8 = null,
|
||||
granted: ?[]const u8 = null,
|
||||
scope: ?[]const u8 = null,
|
||||
};
|
||||
|
||||
/// One `--grant <parameter>=<kind>[:<scope>]`.
|
||||
pub const Grant = struct {
|
||||
parameter: []const u8,
|
||||
kind: []const u8,
|
||||
scope: ?[]const u8 = null,
|
||||
};
|
||||
|
||||
/// An authority object held by the host: copies of a capability share it.
|
||||
pub const Authority = struct { kind: u16, scope: ?[]const u8 };
|
||||
|
||||
pub const Options = struct {
|
||||
/// Maximum number of instructions to execute (spec 11 §7.1); null means unlimited.
|
||||
fuel: ?u64 = null,
|
||||
/// Capability grants for the parameters of the entry function.
|
||||
grants: []const Grant = &.{},
|
||||
};
|
||||
|
||||
pub const Error = std.mem.Allocator.Error || error{EntryNotFound};
|
||||
pub const Error = std.mem.Allocator.Error || error{ EntryNotFound, DuplicateGrant };
|
||||
|
||||
/// Runs `entry`, which must take no parameters. `module` must have passed
|
||||
/// verification.
|
||||
pub fn run(arena: std.mem.Allocator, module: air.Module, entry: []const u8, options: Options) Error!Outcome {
|
||||
const idx = module.functionIndex(entry) orelse return error.EntryNotFound;
|
||||
if (module.functions[idx].params.len != 0) return error.EntryNotFound;
|
||||
const f = module.functions[idx];
|
||||
for (f.params) |p| if (p.ty != .cap) return error.EntryNotFound;
|
||||
var m: Machine = .{ .arena = arena, .module = module, .fuel = options.fuel };
|
||||
return switch (try m.call(idx, &.{})) {
|
||||
|
||||
// Instantiation (spec 05 §10.2): every capability parameter gets exactly
|
||||
// one grant of its kind, and every grant names a parameter.
|
||||
for (options.grants, 0..) |g, i| {
|
||||
for (options.grants[0..i]) |prev| if (std.mem.eql(u8, prev.parameter, g.parameter)) return error.DuplicateGrant;
|
||||
}
|
||||
const args = try arena.alloc(Value, f.params.len);
|
||||
for (f.params, args) |p, *arg| {
|
||||
const pname = f.values[p.value].name;
|
||||
const kind = air.authority.kinds[p.ty.cap].name;
|
||||
const g = for (options.grants) |g| {
|
||||
if (std.mem.eql(u8, g.parameter, pname)) break g;
|
||||
} else return .{ .not_instantiated = .{ .reason = "missing_capability", .parameter = pname, .kind = kind } };
|
||||
if (!std.mem.eql(u8, g.kind, kind)) return .{ .not_instantiated = .{ .reason = "kind_mismatch", .parameter = pname, .kind = kind, .granted = g.kind } };
|
||||
if (!air.authority.validScope(p.ty.cap, g.scope)) return .{ .not_instantiated = .{ .reason = "invalid_scope", .parameter = pname, .kind = kind, .scope = g.scope } };
|
||||
try m.authorities.append(arena, .{ .kind = p.ty.cap, .scope = g.scope });
|
||||
arg.* = .{ .cap = @intCast(m.authorities.items.len - 1) };
|
||||
}
|
||||
for (options.grants) |g| {
|
||||
const known = for (f.params) |p| {
|
||||
if (std.mem.eql(u8, f.values[p.value].name, g.parameter)) break true;
|
||||
} else false;
|
||||
if (!known) return .{ .not_instantiated = .{ .reason = "unknown_parameter", .parameter = g.parameter } };
|
||||
}
|
||||
|
||||
return switch (try m.call(idx, args)) {
|
||||
.value => |v| .{ .returned = v },
|
||||
.trap => |t| .{ .trapped = t },
|
||||
.limit_exceeded => .{ .limit_exceeded = .{ .limit = m.fuel.?, .steps = m.steps } },
|
||||
@@ -122,6 +175,8 @@ const Machine = struct {
|
||||
module: air.Module,
|
||||
depth: u32 = 0,
|
||||
fuel: ?u64,
|
||||
/// Host authority table; capabilities index into it.
|
||||
authorities: std.ArrayList(Authority) = .empty,
|
||||
/// Instructions executed so far.
|
||||
steps: u64 = 0,
|
||||
|
||||
@@ -246,13 +301,20 @@ const Machine = struct {
|
||||
return m.jump(f, vals, x.default);
|
||||
},
|
||||
.enum_switch => |x| {
|
||||
const agg = vals[x.value].aggregate;
|
||||
for (x.arms) |arm| if (arm.variant == agg.variant) {
|
||||
for (f.blocks[arm.block].params, agg.fields) |p, fv| vals[p.value] = fv;
|
||||
const variant: u32, const fields: []const Value = switch (vals[x.value]) {
|
||||
.aggregate => |agg| .{ agg.variant, agg.fields },
|
||||
.builtin => |b| .{ b.variant, &.{} },
|
||||
else => unreachable,
|
||||
};
|
||||
for (x.arms) |arm| if (arm.variant == variant) {
|
||||
for (f.blocks[arm.block].params, fields) |p, fv| vals[p.value] = fv;
|
||||
return .{ .jump = arm.block };
|
||||
};
|
||||
return m.jump(f, vals, x.default.?);
|
||||
},
|
||||
// No narrowing form is registered yet, so verified AIR contains
|
||||
// no reachable cap.narrow (spec 05 §10.4; forms arrive with RFC 0007/0008).
|
||||
.cap_narrow => unreachable,
|
||||
.ret => |v| return .{ .ret = if (v) |x| vals[x] else .void },
|
||||
.trap => return .{ .trap = .explicit },
|
||||
.local_alloc, .local_store, .local_zero, .local_load, .borrow_ref, .borrow_mut, .borrow_field, .borrow_end, .load, .store, .array_splat, .borrow_index, .slice_make, .slice_len, .slice_sub, .slice_split, .slice_copy, .guard => return m.execMemory(vals, inst),
|
||||
@@ -549,6 +611,22 @@ pub fn writeValue(module: air.Module, v: Value, s: *std.json.Stringify) std.Io.W
|
||||
},
|
||||
// Storage and borrows cannot be returned from `@main` (spec 08 §11.3).
|
||||
.local, .place, .slice => try s.write(null),
|
||||
// A capability is opaque: only its kind is shown, never its identity.
|
||||
.cap => {
|
||||
try s.beginObject();
|
||||
try s.objectField("capability");
|
||||
try s.write("opaque");
|
||||
try s.endObject();
|
||||
},
|
||||
.builtin => |b| {
|
||||
try s.beginObject();
|
||||
try s.objectField("variant");
|
||||
try s.write(b.which.variants()[b.variant]);
|
||||
try s.objectField("payload");
|
||||
try s.beginArray();
|
||||
try s.endArray();
|
||||
try s.endObject();
|
||||
},
|
||||
.aggregate => |agg| {
|
||||
const d = module.types[agg.decl];
|
||||
switch (d.kind) {
|
||||
@@ -599,6 +677,25 @@ pub fn writeOutcome(module: air.Module, outcome: Outcome, w: *std.Io.Writer) std
|
||||
try s.objectField("inst");
|
||||
try s.write(t.inst);
|
||||
},
|
||||
.not_instantiated => |n| {
|
||||
try s.write("not_instantiated");
|
||||
try s.objectField("reason");
|
||||
try s.write(n.reason);
|
||||
try s.objectField("parameter");
|
||||
try s.write(n.parameter);
|
||||
if (n.kind) |k| {
|
||||
try s.objectField("kind");
|
||||
try s.write(k);
|
||||
}
|
||||
if (n.granted) |g| {
|
||||
try s.objectField("granted");
|
||||
try s.write(g);
|
||||
}
|
||||
if (n.scope) |sc| {
|
||||
try s.objectField("scope");
|
||||
try s.write(sc);
|
||||
}
|
||||
},
|
||||
.limit_exceeded => |l| {
|
||||
try s.write("limit_exceeded");
|
||||
try s.objectField("kind");
|
||||
|
||||
Reference in new issue
Block a user