Files
aji/src/verifier.zig
T

167 lines
7.0 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 {
return c.reportRelated(id, span, message, fields, &.{});
}
pub fn reportRelated(
c: *Context,
id: []const u8,
span: air.Span,
message: []const u8,
fields: []const diag.Field,
related: []const diag.RelatedLocation,
) 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),
.related_locations = try c.arena.dupe(diag.RelatedLocation, related),
});
}
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.arena, 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 {
return (try verifyModule(arena, file, module)).diagnostics;
}
pub const Result = struct { diagnostics: []const diag.Diagnostic, module: air.Module };
/// Retain implementation caches in the checked module without trusting caches
/// supplied by a caller. Neither cache enters AIR text or changes a stage.
pub fn verifyModule(arena: std.mem.Allocator, file: []const u8, module: air.Module) std.mem.Allocator.Error!Result {
var c: Context = .{ .arena = arena, .file = file, .module = module };
c.module.nominal_properties = null;
c.module.accounted_sizes = null;
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 .{ .diagnostics = c.diags.items, .module = c.module };
}
}
return .{ .diagnostics = &.{}, .module = c.module };
}
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);
try diag.expectUniqueFields(d);
}
}
pub const test_header = "module t\nair 0\nlang 0.1\nfeatures []\n";
test "verification rebuilds caller-supplied property and size caches" {
var scratch = std.heap.ArenaAllocator.init(std.testing.allocator);
defer scratch.deinit();
const a = scratch.allocator();
const source = test_header ++ "type @P = struct { x: u64 }\nfn @f(%p: @P) -> @P\neffects []\nentry:\n %q = copy %p\n ret %q\n";
var m = (try air.parse.parse(a, "t.air", source)).module.?;
const fake = try a.alloc(@import("air/module.zig").NominalProperties, 1);
fake[0] = .{ .linear = true, .copy = false };
m.nominal_properties = fake;
const result = try verifyModule(a, "t.air", m);
try std.testing.expectEqual(@as(usize, 0), result.diagnostics.len);
try std.testing.expect(!result.module.isLinearTypeCached(.{ .named = 0 }));
try std.testing.expect(result.module.accounted_sizes != null);
var large = (try air.parse.parse(a, "t.air", test_header ++ "type @P = struct { x: array<u8, 1048577> }\nfn @f() -> void\neffects []\nentry:\n ret\n")).module.?;
var sizes: @import("air/module.zig").SizeCache = .{};
try sizes.put(a, .{ .index = 0, .depth = 0 }, 0);
large.accounted_sizes = &sizes;
const rejected = try verifyModule(a, "t.air", large);
try std.testing.expectEqualStrings("E5101", rejected.diagnostics[0].code.id);
var anonymous = (try air.parse.parse(a, "t.air", test_header ++ "fn @f(%x: array<u8, 1048577>) -> void\neffects []\nentry:\n ret\n")).module.?;
const array = anonymous.functions[0].params[0].ty.array;
try sizes.put(a, .{ .depth = 0, .kind = .array, .address = @intFromPtr(array) }, 0);
anonymous.accounted_sizes = &sizes;
const anonymous_rejected = try verifyModule(a, "t.air", anonymous);
try std.testing.expectEqualStrings("E5101", anonymous_rejected.diagnostics[0].code.id);
}