Phase 5: atomics through every layer (RFC 0015)
AIR: linear atomic<T> (AtomicScalar), atomic.new/take/load/store/RMW/ cmpxchg (two results)/fence with explicit orderings; feature atomics published. Verifier: ordering tables (E4201), ordinary access (E4202), effects [atomic] / [atomic, atomic.weak], linearity, take while lent (E4101, also for mutex.take). Interpreter: timestamped message machine (src/interp/weak.zig); views travel through spawn/join, channels, mutexes, close and cancel; seq_cst reads the latest message and programs without weak reads make no extra draw (existing records unchanged). aji explore: exhaustive exploration that branches only before visible operations. Litmus: 20/20 tests give exactly the oracle's sets; differential test (1500 random programs) found no unsound outcome. .aji layer 10, Quick Reference atomics, source coverage 67/67, audit report atomic_operations. AIR bundle +5.9 %, QUICK.md +0.5k.
This commit is contained in:
1 parent
ce9ef03bc0
commit
4242132b2a
70 files changed
+2630
-80
No files matched your search
@@ -33,6 +33,11 @@ const usage =
|
||||
\\ print the canonical AIR a source module lowers to;
|
||||
\\ --annotate adds source spans as comments (not canonical);
|
||||
\\ --stats prints source/AIR size and expansion ratio
|
||||
\\ explore [--max-runs N] [--grant P=KIND[:SCOPE]]... <file.air | file.aji>
|
||||
\\ run @main under every schedule and weak-memory choice
|
||||
\\ (depth first, up to N runs, default 100000) and print the
|
||||
\\ distinct outcomes; "complete" is true when the whole
|
||||
\\ choice tree was explored (spec 04 §20.5)
|
||||
\\ report <file.air> verify, then print the audit report (spec 10 §6.1)
|
||||
\\ codes list diagnostic codes as JSON Lines
|
||||
\\ features print supported AIR format, language version and features
|
||||
@@ -177,6 +182,7 @@ fn run(io: std.Io, arena: std.mem.Allocator, args: []const [:0]const u8, out: *s
|
||||
try writeCode(out, c.*);
|
||||
return .accepted;
|
||||
}
|
||||
if (std.mem.eql(u8, cmd, "explore")) return explore(arena, io, args[2..], out);
|
||||
if (std.mem.eql(u8, cmd, "check") or std.mem.eql(u8, cmd, "run")) {
|
||||
var options: aji.interp.Options = .{};
|
||||
var grants: std.ArrayList(aji.interp.Grant) = .empty;
|
||||
@@ -319,6 +325,97 @@ fn run(io: std.Io, arena: std.mem.Allocator, args: []const [:0]const u8, out: *s
|
||||
return usageError("unknown command");
|
||||
}
|
||||
|
||||
/// `aji explore`: exhaustive depth-first exploration of the choice tree
|
||||
/// (scheduler, `select` and weak-memory choices). Instructions other than
|
||||
/// scheduling points are not interleaved: the quantum is unbounded, so the
|
||||
/// tree branches only at synchronization and atomic operations.
|
||||
fn explore(arena: std.mem.Allocator, io: std.Io, argv: []const []const u8, out: *std.Io.Writer) !ExitCode {
|
||||
var max_runs: u64 = 100_000;
|
||||
var grants: std.ArrayList(aji.interp.Grant) = .empty;
|
||||
var rest = argv;
|
||||
while (rest.len >= 2) {
|
||||
if (std.mem.eql(u8, rest[0], "--max-runs")) {
|
||||
max_runs = std.fmt.parseInt(u64, rest[1], 10) catch return usageError("--max-runs takes a positive integer");
|
||||
} else if (std.mem.eql(u8, rest[0], "--grant")) {
|
||||
try grants.append(arena, parseGrant(rest[1]) orelse return usageError("--grant takes <parameter>=<kind>[:<scope>]"));
|
||||
} else break;
|
||||
rest = rest[2..];
|
||||
}
|
||||
if (rest.len != 1) return usageError("explore takes exactly one file");
|
||||
const path = rest[0];
|
||||
const source = std.Io.Dir.cwd().readFileAlloc(io, path, arena, .limited(64 << 20)) catch |err| {
|
||||
std.debug.print("aji: cannot read {s}: {t}\n", .{ path, err });
|
||||
return .usage;
|
||||
};
|
||||
const report = try aji.check.checkFile(arena, path, source);
|
||||
for (report.diagnostics) |d| {
|
||||
try d.writeJson(out);
|
||||
try out.writeByte('\n');
|
||||
}
|
||||
if (report.status != .accepted) return .rejected;
|
||||
const module = report.module.?;
|
||||
|
||||
var counts: std.StringArrayHashMapUnmanaged(u64) = .empty;
|
||||
var prefix: std.ArrayList(u32) = .empty;
|
||||
var runs: u64 = 0;
|
||||
var complete = false;
|
||||
while (runs < max_runs) {
|
||||
var run_arena: std.heap.ArenaAllocator = .init(std.heap.page_allocator);
|
||||
defer run_arena.deinit();
|
||||
var ex: aji.interp.Explore = .{ .prefix = prefix.items };
|
||||
const outcome = aji.interp.run(run_arena.allocator(), module, "main", .{
|
||||
.grants = grants.items,
|
||||
.quantum = std.math.maxInt(u64),
|
||||
.explore = &ex,
|
||||
}) catch |err| switch (err) {
|
||||
error.EntryNotFound => return usageError("module has no @main whose parameters are all capabilities"),
|
||||
error.DuplicateGrant => return usageError("a parameter is granted more than once"),
|
||||
else => |e| return e,
|
||||
};
|
||||
runs += 1;
|
||||
var rec: std.Io.Writer.Allocating = .init(run_arena.allocator());
|
||||
try aji.interp.writeOutcome(module, outcome, null, &rec.writer);
|
||||
const gop = try counts.getOrPut(arena, rec.written());
|
||||
if (!gop.found_existing) {
|
||||
gop.key_ptr.* = try arena.dupe(u8, rec.written());
|
||||
gop.value_ptr.* = 0;
|
||||
}
|
||||
gop.value_ptr.* += 1;
|
||||
// The next path: increment the deepest choice that has an untried
|
||||
// option, and drop everything after it.
|
||||
const widths = ex.widths.items;
|
||||
var i = widths.len;
|
||||
const next = while (i > 0) {
|
||||
i -= 1;
|
||||
const taken: u32 = if (i < prefix.items.len) prefix.items[i] else 0;
|
||||
if (taken + 1 < widths[i]) break i;
|
||||
} else null;
|
||||
const at = next orelse {
|
||||
complete = true;
|
||||
break;
|
||||
};
|
||||
var np: std.ArrayList(u32) = .empty;
|
||||
for (0..at) |k| try np.append(arena, if (k < prefix.items.len) prefix.items[k] else 0);
|
||||
try np.append(arena, (if (at < prefix.items.len) prefix.items[at] else 0) + 1);
|
||||
prefix = np;
|
||||
}
|
||||
const keys = counts.keys();
|
||||
const order = try arena.alloc(usize, keys.len);
|
||||
for (order, 0..) |*o, k| o.* = k;
|
||||
std.mem.sort(usize, order, keys, struct {
|
||||
fn lt(ks: []const []const u8, a: usize, b: usize) bool {
|
||||
return std.mem.lessThan(u8, ks[a], ks[b]);
|
||||
}
|
||||
}.lt);
|
||||
try out.print("{{\"runs\":{d},\"complete\":{},\"outcomes\":[", .{ runs, complete });
|
||||
for (order, 0..) |k, n| {
|
||||
if (n != 0) try out.writeByte(',');
|
||||
try out.print("{{\"outcome\":{s},\"count\":{d}}}", .{ keys[k], counts.values()[k] });
|
||||
}
|
||||
try out.writeAll("]}\n");
|
||||
return if (complete) .accepted else .limit_exceeded;
|
||||
}
|
||||
|
||||
/// The run record; for a `.aji` module a trap also carries its
|
||||
/// `source_location` (spec 15 §10).
|
||||
fn writeRecord(arena: std.mem.Allocator, module: aji.air.Module, outcome: aji.interp.Outcome, captured: ?aji.interp.Output, report: aji.check.Report, path: []const u8, out: *std.Io.Writer) !void {
|
||||
|
||||
Reference in new issue
Block a user