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
This commit is contained in:
dev committed 2026-10-04 14:41:30 +03:00
1 parent c510a37cf9
commit 008654999e
24 files changed
+422 -18

No files matched your search

+3 -1
View File
@@ -37,10 +37,11 @@ pub const registry = [_]Feature{
.{ .name = "resources", .depends = &.{ "capabilities", "effects", "ownership" }, .status = .published, .phase = 2 },
.{ .name = "sync", .depends = &.{"ownership"}, .status = .reserved, .phase = 3 },
.{ .name = "tasks", .depends = &.{"ownership"}, .status = .reserved, .phase = 3 },
.{ .name = "unsafe", .depends = &.{ "arrays", "effects", "ownership" }, .status = .published, .phase = 2 },
};
/// Features implemented by this toolchain, sorted.
pub const supported = [_][]const u8{ "arrays", "capabilities", "effects", "heap", "ownership", "regions", "resources" };
pub const supported = [_][]const u8{ "arrays", "capabilities", "effects", "heap", "ownership", "regions", "resources", "unsafe" };
pub const Reason = enum {
unknown_feature,
@@ -118,6 +119,7 @@ const test_registry = [_]Feature{
.{ .name = "regions", .depends = &.{"ownership"}, .status = .published, .phase = 1 },
.{ .name = "islands", .depends = &.{ "ownership", "regions" }, .status = .published, .phase = 4 },
.{ .name = "tasks", .depends = &.{"ownership"}, .status = .reserved, .phase = 3 },
.{ .name = "unsafe", .depends = &.{ "arrays", "effects", "ownership" }, .status = .published, .phase = 2 },
};
fn expectProblems(declared: []const []const u8, sup: []const []const u8, want: []const Reason) !void {
+6 -2
View File
@@ -221,7 +221,7 @@ pub const Op = union(enum) {
store: struct { borrow: ValueId, value: ValueId },
// Feature `arrays`.
array_splat: struct { ty: Type, value: ValueId },
borrow_index: struct { borrow: ValueId, index: ValueId, proven: bool },
borrow_index: struct { borrow: ValueId, index: ValueId, proven: bool, unchecked: bool = false },
slice_make: ValueId,
slice_len: ValueId,
slice_sub: struct { slice: ValueId, start: ValueId, len: ValueId },
@@ -229,6 +229,8 @@ pub const Op = union(enum) {
slice_split: struct { slice: ValueId, at: ValueId },
slice_copy: struct { dst: ValueId, src: ValueId },
guard: ValueId,
// Feature `unsafe` (spec 13 §6).
assume: ValueId,
@"const": struct { ty: Type, bits: u128 },
copy: ValueId,
@@ -302,6 +304,8 @@ pub const Function = struct {
ret: Type,
/// Index into `params` of the `from` parameter of a borrow return.
ret_from: ?u32 = null,
/// Proof obligation of a `trusted` boundary (feature `unsafe`).
trusted: ?[]const u8 = null,
effects: []const []const u8,
blocks: []const Block,
values: []const ValueInfo,
@@ -427,7 +431,7 @@ pub fn forEachUse(op: Op, ctx: anytype, comptime f: fn (@TypeOf(ctx), ValueId) v
f(ctx, x.local);
f(ctx, x.value);
},
.local_zero, .local_load, .borrow_ref, .borrow_mut, .borrow_end, .load, .slice_make, .slice_len, .guard => |v| f(ctx, v),
.local_zero, .local_load, .borrow_ref, .borrow_mut, .borrow_end, .load, .slice_make, .slice_len, .guard, .assume => |v| f(ctx, v),
.borrow_field => |x| f(ctx, x.borrow),
.store => |x| {
f(ctx, x.borrow);
+50 -2
View File
@@ -60,6 +60,8 @@ const Tag = enum {
global,
/// Raw numeric text, including an optional leading `-` and any `.`.
number,
/// `"…"`; text excludes the quotes, escapes not yet decoded.
string,
invalid,
eof,
};
@@ -145,6 +147,18 @@ fn tokenize(arena: Allocator, src: []const u8) Allocator.Error![]const Token {
else => .r_brace,
};
},
'"' => {
i += 1;
while (i < src.len and src[i] != '"' and src[i] != '\n') : (i += 1) {
if (src[i] == '\\' and i + 1 < src.len) i += 1;
}
if (i < src.len and src[i] == '"') {
i += 1;
try tokens.append(arena, .{ .tag = .string, .text = src[start + 1 .. i - 1], .span = span });
continue;
}
tag = .invalid;
},
'%', '@' => {
i += 1;
while (i < src.len and isNameChar(src[i])) i += 1;
@@ -643,6 +657,16 @@ const Parser = struct {
const effects_tok = try p.expectWord("effects");
const effects = try p.parsePathList();
_ = try p.expect(.nl, "end of line");
var trusted: ?[]const u8 = null;
p.skipNewlines();
if (p.atWord("trusted")) {
const tok = p.next();
try p.requireFeature("unsafe", tok.span, "trusted");
const str = try p.expect(.string, "obligation string");
trusted = unescapeString(p.arena, str.text) catch return p.fail(str, "string with escapes \\\", \\\\ or \\n");
if (trusted.?.len == 0) return p.fail(str, "non-empty obligation string");
_ = try p.expect(.nl, "end of line");
}
// Effect names are checked against the registry when the module
// declares `effects` (spec 05 §9.1).
if (p.declares("effects")) for (effects) |e| {
@@ -671,6 +695,7 @@ const Parser = struct {
.params = params.items,
.ret = ret,
.ret_from = ret_from,
.trusted = trusted,
.effects = effects,
.blocks = blocks.items,
.values = p.values.items,
@@ -1046,6 +1071,8 @@ const Parser = struct {
@"array.splat",
@"borrow.index",
@"borrow.index.proven",
@"borrow.index.unchecked",
assume,
@"slice.make",
@"slice.len",
@"slice.sub",
@@ -1056,6 +1083,7 @@ const Parser = struct {
const name = std.meta.stringToEnum(Name, op_tok.text) orelse return null;
const feature: []const u8 = switch (name) {
.@"local.alloc", .@"local.store", .@"local.zero", .@"local.load", .@"borrow.ref", .@"borrow.mut", .@"borrow.field", .@"borrow.end", .load, .store => "ownership",
.@"borrow.index.unchecked", .assume => "unsafe",
else => "arrays",
};
try p.requireFeature(feature, op_tok.span, op_tok.text);
@@ -1090,10 +1118,11 @@ const Parser = struct {
const ty = try p.parseType();
break :blk .{ .array_splat = .{ .ty = ty, .value = try p.useValue() } };
},
.@"borrow.index", .@"borrow.index.proven" => blk: {
.@"borrow.index", .@"borrow.index.proven", .@"borrow.index.unchecked" => blk: {
const b, const i = try p.twoOperands();
break :blk .{ .borrow_index = .{ .borrow = b, .index = i, .proven = name == .@"borrow.index.proven" } };
break :blk .{ .borrow_index = .{ .borrow = b, .index = i, .proven = name == .@"borrow.index.proven", .unchecked = name == .@"borrow.index.unchecked" } };
},
.assume => .{ .assume = try p.useValue() },
.@"slice.make" => .{ .slice_make = try p.useValue() },
.@"slice.len" => .{ .slice_len = try p.useValue() },
.@"slice.sub" => blk: {
@@ -1217,6 +1246,25 @@ pub fn literalBits(text: []const u8, ty: air.IntType) ?u128 {
return if (n == 128) raw else raw & ((@as(u128, 1) << @intCast(n)) - 1);
}
fn unescapeString(arena: Allocator, s: []const u8) ![]const u8 {
var out: std.ArrayList(u8) = .empty;
var i: usize = 0;
while (i < s.len) : (i += 1) {
if (s[i] != '\\') {
try out.append(arena, s[i]);
continue;
}
i += 1;
try out.append(arena, switch (s[i]) {
'"' => '"',
'\\' => '\\',
'n' => '\n',
else => return error.BadEscape,
});
}
return out.items;
}
fn parseScalarType(text: []const u8) ?air.Type {
if (eql(text, "bool")) return .bool;
if (std.meta.stringToEnum(air.IntType, text)) |it| return .{ .int = it };
+12 -1
View File
@@ -51,6 +51,16 @@ pub fn print(m: air.Module, w: *Writer) Writer.Error!void {
try w.writeAll("\neffects [");
try list(w, f.effects);
try w.writeAll("]\n");
if (f.trusted) |t| {
try w.writeAll("trusted \"");
for (t) |ch| switch (ch) {
'"' => try w.writeAll("\\\""),
'\\' => try w.writeAll("\\\\"),
'\n' => try w.writeAll("\\n"),
else => try w.writeByte(ch),
};
try w.writeAll("\"\n");
}
for (f.blocks) |b| {
try w.writeAll(b.name);
if (b.params.len != 0) {
@@ -276,7 +286,8 @@ fn printOp(m: air.Module, f: air.Function, op: air.Op, w: *Writer) Writer.Error!
try w.writeByte(' ');
try value(f, x.value, w);
},
.borrow_index => |x| try two(f, w, if (x.proven) "borrow.index.proven" else "borrow.index", x.borrow, x.index),
.borrow_index => |x| try two(f, w, if (x.unchecked) "borrow.index.unchecked" else if (x.proven) "borrow.index.proven" else "borrow.index", x.borrow, x.index),
.assume => |v| try one(f, w, "assume", v),
.slice_make => |v| try one(f, w, "slice.make", v),
.slice_len => |v| try one(f, w, "slice.len", v),
.slice_sub => |x| {