Files
aji/src/verifier.zig
T
dev 008654999e 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
2026-10-04 14:41:30 +03:00

115 lines
4.2 KiB
Zig

//! AIR verifier (spec 08). Part of the language semantics, not a lint.
//!
//! Passes are added in plan order (docs/plan/README.md). Each normative
//! rejection maps to a code in `diag.registry` and has accept and reject
//! conformance tests under tests/conformance/.
//!
//! Stages run in the order of spec 07 §8.6 and verification stops after the
//! first stage that reports an error, so later stages may assume the
//! invariants established by earlier ones.
const std = @import("std");
const air = @import("air.zig");
const diag = @import("diag.zig");
pub const budget = @import("verifier/budget.zig");
pub const cfg = @import("verifier/cfg.zig");
pub const ssa = @import("verifier/ssa.zig");
pub const types = @import("verifier/types.zig");
pub const effects = @import("verifier/effects.zig");
pub const unsafe_ = @import("verifier/unsafe.zig");
pub const init = @import("verifier/init.zig");
pub const borrow = @import("verifier/borrow.zig");
pub const bounds = @import("verifier/bounds.zig");
/// Shared state for one verification run.
pub const Context = struct {
arena: std.mem.Allocator,
file: []const u8,
module: air.Module,
diags: std.ArrayList(diag.Diagnostic) = .empty,
/// Type of every value, per function; filled by the types stage and
/// available to all later stages.
value_types: []const []const air.Type = &.{},
pub fn report(
c: *Context,
id: []const u8,
span: air.Span,
message: []const u8,
fields: []const diag.Field,
) std.mem.Allocator.Error!void {
try c.diags.append(c.arena, .{
.code = diag.lookup(id).?,
.span = .{ .file = c.file, .line = span.line, .col = span.col },
.message = message,
.fields = try c.arena.dupe(diag.Field, fields),
});
}
pub fn valueName(c: *Context, f: air.Function, v: air.ValueId) std.mem.Allocator.Error![]const u8 {
return std.fmt.allocPrint(c.arena, "%{s}", .{f.values[v].name});
}
pub fn typeName(c: *Context, t: air.Type) std.mem.Allocator.Error![]const u8 {
var w: std.Io.Writer.Allocating = .init(c.arena);
air.print.printType(c.module, t, &w.writer) catch return error.OutOfMemory;
return w.written();
}
};
/// Runs all verifier stages. Returns the diagnostics of the first failing
/// stage, or an empty slice if the module verifies.
pub fn verify(arena: std.mem.Allocator, file: []const u8, module: air.Module) std.mem.Allocator.Error![]const diag.Diagnostic {
var c: Context = .{ .arena = arena, .file = file, .module = module };
const stages = .{ budget.check, cfg.check, ssa.check, types.check, effects.check, unsafe_.check, init.check, borrow.check, bounds.check };
inline for (stages) |stage| {
try stage(&c);
if (c.diags.items.len != 0) {
std.mem.sort(diag.Diagnostic, c.diags.items, {}, bySpan);
return c.diags.items;
}
}
return &.{};
}
fn bySpan(_: void, a: diag.Diagnostic, b: diag.Diagnostic) bool {
const sa = a.span.?;
const sb = b.span.?;
if (sa.line != sb.line) return sa.line < sb.line;
if (sa.col != sb.col) return sa.col < sb.col;
return std.mem.lessThan(u8, a.code.id, b.code.id);
}
test {
_ = budget;
_ = cfg;
_ = ssa;
_ = types;
_ = effects;
_ = unsafe_;
_ = init;
_ = borrow;
_ = bounds;
}
/// Test helper: parses `src` and verifies it, expecting exactly `codes`.
pub fn expectCodes(src: []const u8, codes: []const []const u8) !void {
var arena_state: std.heap.ArenaAllocator = .init(std.testing.allocator);
defer arena_state.deinit();
const arena = arena_state.allocator();
const r = try air.parse.parse(arena, "t.air", src);
if (r.diagnostics.len != 0) {
for (r.diagnostics) |d| std.debug.print("parse: {s} {s}\n", .{ d.code.id, d.message });
return error.ParseFailed;
}
const ds = try verify(arena, "t.air", r.module.?);
if (ds.len != codes.len) {
for (ds) |d| std.debug.print("got {s} {s} at {d}:{d}\n", .{ d.code.id, d.message, d.span.?.line, d.span.?.col });
return error.TestExpectedEqual;
}
for (codes, ds) |c, d| try std.testing.expectEqualStrings(c, d.code.id);
}
pub const test_header = "module t\nair 0\nlang 0.1\nfeatures []\n";