- features cancellation, channels, sync supported - AIR model/parser/printer: shield, sender<T>, receiver<T>, mutex<T>, guard<T>; task.check_cancel, task.join.cancel, task.shield.*, channel.*, select (bracketed arms), mutex.* - verifier: arities (E5002), element rules (E4001/E1001, E0006 channel_transfer), guard_transfer gating, task.wait effect (gated for task.join by cancellation), per-edge consumption with values and handles returned as block parameters, loans moved to the handle on task.join.cancel's cancelled edge, shield order (E4305), guard children (E2102), critical-section rule (E4304) - interpreter: waiting-operation rule (ready, else cancelled if observable, else wait), shield depth, bounded and rendezvous channels with FIFO offers, drain-then-close receiver, budget release at the last close, select with seeded draw among ready arms, mutex wakeups; schedule record also after a select draw - report counts task.spawn, task.wait and channel allocations - spec erratum: mutex<T> is linear (format 0 has no droppable move-only class) - 30 new conformance tests (10 run with schedules, 4 accept, 16 reject); 6000-run schedule stress and 4350 mutants without failure
254 lines
11 KiB
Zig
254 lines
11 KiB
Zig
//! Audit report (spec 10 §6.1): one canonical JSON document derived from a
|
|
//! verified module. Never depends on a run.
|
|
|
|
const std = @import("std");
|
|
const air = @import("air.zig");
|
|
const unsafe_ = @import("verifier/unsafe.zig");
|
|
|
|
const Writer = std.Io.Writer;
|
|
|
|
const Checks = struct {
|
|
overflow: u32 = 0,
|
|
division: u32 = 0,
|
|
shift: u32 = 0,
|
|
cast: u32 = 0,
|
|
bounds: u32 = 0,
|
|
assert: u32 = 0,
|
|
guard: u32 = 0,
|
|
};
|
|
|
|
const FnInfo = struct {
|
|
calls: std.ArrayList([]const u8) = .empty,
|
|
intrinsics: std.ArrayList([]const u8) = .empty,
|
|
allocations: u32 = 0,
|
|
checks: Checks = .{},
|
|
proven_bounds: u32 = 0,
|
|
unsafe_ops: std.ArrayList(struct { block: []const u8, inst: u32, op: []const u8 }) = .empty,
|
|
/// Effects performed directly (intrinsics, allocation, unsafe operations).
|
|
direct: std.ArrayList([]const u8) = .empty,
|
|
/// Fixed point of `direct` over callees.
|
|
used: std.ArrayList([]const u8) = .empty,
|
|
};
|
|
|
|
fn contains(list: []const []const u8, s: []const u8) bool {
|
|
for (list) |x| if (std.mem.eql(u8, x, s)) return true;
|
|
return false;
|
|
}
|
|
|
|
fn addOnce(arena: std.mem.Allocator, list: *std.ArrayList([]const u8), s: []const u8) !bool {
|
|
if (contains(list.items, s)) return false;
|
|
try list.append(arena, s);
|
|
return true;
|
|
}
|
|
|
|
fn lessThan(_: void, a: []const u8, b: []const u8) bool {
|
|
return std.mem.lessThan(u8, a, b);
|
|
}
|
|
|
|
fn sorted(arena: std.mem.Allocator, items: []const []const u8) ![]const []const u8 {
|
|
const out = try arena.dupe([]const u8, items);
|
|
std.mem.sort([]const u8, out, {}, lessThan);
|
|
return out;
|
|
}
|
|
|
|
fn analyze(arena: std.mem.Allocator, m: air.Module, f: air.Function) !FnInfo {
|
|
var info: FnInfo = .{};
|
|
for (f.blocks) |b| for (b.insts, 0..) |inst, ii| {
|
|
switch (inst.op) {
|
|
.call => |x| _ = try addOnce(arena, &info.calls, m.functions[x.callee].name),
|
|
.intrinsic => |x| {
|
|
const reg = air.authority.intrinsics[x.index];
|
|
_ = try addOnce(arena, &info.intrinsics, reg.name);
|
|
if (reg.effect) |e| _ = try addOnce(arena, &info.direct, e);
|
|
},
|
|
.own_new, .own_array, .region_new, .region_array, .region_create, .channel_make => {
|
|
info.allocations += 1;
|
|
_ = try addOnce(arena, &info.direct, "mem.alloc");
|
|
},
|
|
.task_spawn => _ = try addOnce(arena, &info.direct, "task.spawn"),
|
|
// spec 04 §37.4: `task.join` waits in modules declaring `cancellation`.
|
|
.task_join => if (m.declares("cancellation")) {
|
|
_ = try addOnce(arena, &info.direct, "task.wait");
|
|
},
|
|
.task_join_cancel, .channel_send, .channel_recv, .select, .mutex_lock => _ = try addOnce(arena, &info.direct, "task.wait"),
|
|
.arith => |x| if (x.mode == .checked) switch (x.op) {
|
|
.add, .sub, .mul => info.checks.overflow += 1,
|
|
.div, .rem => {
|
|
info.checks.division += 1;
|
|
if (x.ty.signed()) info.checks.overflow += 1;
|
|
},
|
|
},
|
|
.bit => |x| if (x.op == .shl or x.op == .shr) {
|
|
info.checks.shift += 1;
|
|
},
|
|
.cast => |x| if (x.mode == .checked) {
|
|
info.checks.cast += 1;
|
|
},
|
|
.assert => info.checks.assert += 1,
|
|
.guard => info.checks.guard += 1,
|
|
.borrow_index => |x| {
|
|
if (x.unchecked) {} else if (x.proven) info.proven_bounds += 1 else info.checks.bounds += 1;
|
|
},
|
|
.slice_sub, .slice_split, .slice_copy => info.checks.bounds += 1,
|
|
else => {},
|
|
}
|
|
if (unsafe_.unsafeOp(m, inst.op)) |what| {
|
|
// Calls of unsafe functions propagate through `effects_used`.
|
|
if (inst.op != .call) {
|
|
try info.unsafe_ops.append(arena, .{ .block = b.name, .inst = @intCast(ii), .op = what });
|
|
_ = try addOnce(arena, &info.direct, "unsafe");
|
|
}
|
|
}
|
|
};
|
|
return info;
|
|
}
|
|
|
|
pub fn write(arena: std.mem.Allocator, m: air.Module, w: *Writer) !void {
|
|
const infos = try arena.alloc(FnInfo, m.functions.len);
|
|
for (m.functions, infos) |f, *i| i.* = try analyze(arena, m, f);
|
|
|
|
// effects_used: fixed point over the call graph; a trusted function
|
|
// does not pass `unsafe` on (spec 13 §6.3).
|
|
for (infos) |*i| for (i.direct.items) |e| {
|
|
_ = try addOnce(arena, &i.used, e);
|
|
};
|
|
var changed = true;
|
|
while (changed) {
|
|
changed = false;
|
|
for (m.functions, infos) |f, *i| {
|
|
for (f.blocks) |b| for (b.insts) |inst| if (inst.op == .call) {
|
|
const callee = inst.op.call.callee;
|
|
for (infos[callee].used.items) |e| {
|
|
// A trusted callee is the boundary: `unsafe` stops there.
|
|
if (m.functions[callee].trusted != null and std.mem.eql(u8, e, "unsafe")) continue;
|
|
if (try addOnce(arena, &i.used, e)) changed = true;
|
|
}
|
|
};
|
|
}
|
|
}
|
|
|
|
var s: std.json.Stringify = .{ .writer = w };
|
|
try s.beginObject();
|
|
try s.objectField("report");
|
|
try s.write("audit");
|
|
try s.objectField("air");
|
|
try s.write(m.format_version);
|
|
try s.objectField("module");
|
|
try s.write(m.name);
|
|
try s.objectField("features");
|
|
try s.write(m.features);
|
|
|
|
try s.objectField("entry");
|
|
if (m.functionIndex("main")) |mi| {
|
|
const f = m.functions[mi];
|
|
try s.beginObject();
|
|
try s.objectField("function");
|
|
try s.write("main");
|
|
try s.objectField("capabilities");
|
|
try s.beginArray();
|
|
for (f.params) |p| if (p.ty == .cap) {
|
|
try s.write(.{ .parameter = f.values[p.value].name, .kind = air.authority.kinds[p.ty.cap].name });
|
|
};
|
|
try s.endArray();
|
|
try s.endObject();
|
|
} else try s.write(null);
|
|
|
|
try s.objectField("functions");
|
|
try s.beginArray();
|
|
for (m.functions, infos) |f, i| {
|
|
try s.beginObject();
|
|
try s.objectField("name");
|
|
try s.write(f.name);
|
|
try s.objectField("trusted");
|
|
try s.write(f.trusted);
|
|
try s.objectField("effects_declared");
|
|
try s.write(try sorted(arena, f.effects));
|
|
try s.objectField("effects_used");
|
|
try s.write(try sorted(arena, i.used.items));
|
|
var unused: std.ArrayList([]const u8) = .empty;
|
|
for (f.effects) |e| if (!contains(i.used.items, e)) try unused.append(arena, e);
|
|
try s.objectField("effects_unused");
|
|
try s.write(try sorted(arena, unused.items));
|
|
try s.objectField("calls");
|
|
try s.write(i.calls.items);
|
|
try s.objectField("intrinsics");
|
|
try s.write(i.intrinsics.items);
|
|
try s.objectField("allocations");
|
|
try s.write(i.allocations);
|
|
try s.objectField("runtime_checks");
|
|
try s.write(i.checks);
|
|
try s.objectField("proven_checks");
|
|
try s.write(.{ .bounds = i.proven_bounds });
|
|
try s.objectField("unsafe_operations");
|
|
try s.write(i.unsafe_ops.items);
|
|
try s.endObject();
|
|
}
|
|
try s.endArray();
|
|
|
|
// Capability kinds appearing in any type of the module, sorted.
|
|
var kinds: std.ArrayList([]const u8) = .empty;
|
|
const visit = struct {
|
|
fn go(arena_: std.mem.Allocator, list: *std.ArrayList([]const u8), t: air.Type) !void {
|
|
if (t == .cap) _ = try addOnce(arena_, list, air.authority.kinds[t.cap].name);
|
|
if (t.inner()) |inner| try go(arena_, list, inner);
|
|
}
|
|
}.go;
|
|
for (m.types) |d| switch (d.kind) {
|
|
.@"struct" => |fs| for (fs) |fd| try visit(arena, &kinds, fd.ty),
|
|
.@"enum" => |vs| for (vs) |v| for (v.payload) |t| try visit(arena, &kinds, t),
|
|
};
|
|
for (m.functions) |f| {
|
|
for (f.params) |p| try visit(arena, &kinds, p.ty);
|
|
try visit(arena, &kinds, f.ret);
|
|
for (f.blocks) |b| for (b.params) |p| try visit(arena, &kinds, p.ty);
|
|
}
|
|
try s.objectField("capability_kinds");
|
|
try s.write(try sorted(arena, kinds.items));
|
|
|
|
var trusted: std.ArrayList([]const u8) = .empty;
|
|
var unsafe_fns: std.ArrayList([]const u8) = .empty;
|
|
var ops: u32 = 0;
|
|
for (m.functions, infos) |f, i| {
|
|
if (f.trusted != null) try trusted.append(arena, f.name);
|
|
if (unsafe_.declaresUnsafe(f)) try unsafe_fns.append(arena, f.name);
|
|
ops += @intCast(i.unsafe_ops.items.len);
|
|
}
|
|
try s.objectField("unsafe");
|
|
try s.write(.{ .trusted_functions = trusted.items, .unsafe_functions = unsafe_fns.items, .operations = ops });
|
|
try s.endObject();
|
|
}
|
|
|
|
test "audit report" {
|
|
var arena_state: std.heap.ArenaAllocator = .init(std.testing.allocator);
|
|
defer arena_state.deinit();
|
|
const arena = arena_state.allocator();
|
|
const src =
|
|
\\module demo
|
|
\\air 0
|
|
\\lang 0.1
|
|
\\features [arrays, capabilities, effects, ownership, resources, unsafe]
|
|
\\fn @main(%out: cap<io.write>, %fs: cap<fs.read>) -> u8
|
|
\\effects [fs.read, io.write]
|
|
\\entry:
|
|
\\ %a = const.u8 1
|
|
\\ %b = add.checked.u8 %a, %a
|
|
\\ %r = call @peek(%b)
|
|
\\ ret %r
|
|
\\fn @peek(%x: u8) -> u8
|
|
\\effects []
|
|
\\trusted "only reads"
|
|
\\entry:
|
|
\\ %c = const.bool true
|
|
\\ assume %c
|
|
\\ ret %x
|
|
\\
|
|
;
|
|
const parsed = try air.parse.parse(arena, "demo.air", src);
|
|
try std.testing.expectEqual(@as(usize, 0), parsed.diagnostics.len);
|
|
var out: Writer.Allocating = .init(arena);
|
|
try write(arena, parsed.module.?, &out.writer);
|
|
try std.testing.expectEqualStrings(
|
|
\\{"report":"audit","air":0,"module":"demo","features":["arrays","capabilities","effects","ownership","resources","unsafe"],"entry":{"function":"main","capabilities":[{"parameter":"out","kind":"io.write"},{"parameter":"fs","kind":"fs.read"}]},"functions":[{"name":"main","trusted":null,"effects_declared":["fs.read","io.write"],"effects_used":[],"effects_unused":["fs.read","io.write"],"calls":["peek"],"intrinsics":[],"allocations":0,"runtime_checks":{"overflow":1,"division":0,"shift":0,"cast":0,"bounds":0,"assert":0,"guard":0},"proven_checks":{"bounds":0},"unsafe_operations":[]},{"name":"peek","trusted":"only reads","effects_declared":[],"effects_used":["unsafe"],"effects_unused":[],"calls":[],"intrinsics":[],"allocations":0,"runtime_checks":{"overflow":0,"division":0,"shift":0,"cast":0,"bounds":0,"assert":0,"guard":0},"proven_checks":{"bounds":0},"unsafe_operations":[{"block":"entry","inst":1,"op":"assume"}]}],"capability_kinds":["fs.read","io.write"],"unsafe":{"trusted_functions":["peek"],"unsafe_functions":[],"operations":1}}
|
|
, out.written());
|
|
}
|