spec: bit-exact PRNG, canonical run record, global quiescence
Owner review of the RFC 0007 implementation: - spec 11 §8.1: xoshiro256** with SplitMix64 seeding specified bit for bit, fill consumes ceil(n/8) outputs LSB first; golden vector; own implementation replaces std.Random (whose seeding differs) - spec 07a: canonical run record; stdout/stderr as UTF-8 strings or base64 objects, files always base64 objects - spec 11 §8: blocked only at global quiescence (no runnable unit, no known future event, unfinished execution) - reserved feature resource_joins; test names are permanent and rule-descriptive (renamed resource_*/borrow_* tests) - plan: remaining Phase 2 order heap/regions -> unsafe -> hosted -> reports -> aji.toml
This commit is contained in:
1 parent
d405d4ea50
commit
7a74be3cf7
18 files changed
+203
-38
No files matched your search
@@ -33,6 +33,7 @@ pub const registry = [_]Feature{
|
||||
.{ .name = "linear_aggregates", .depends = &.{ "capabilities", "effects", "ownership", "resources" }, .status = .reserved, .phase = null },
|
||||
.{ .name = "ownership", .depends = &.{}, .status = .published, .phase = 1 },
|
||||
.{ .name = "regions", .depends = &.{ "capabilities", "effects", "ownership" }, .status = .reserved, .phase = 2 },
|
||||
.{ .name = "resource_joins", .depends = &.{ "capabilities", "effects", "ownership", "resources" }, .status = .reserved, .phase = null },
|
||||
.{ .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 },
|
||||
|
||||
+41
-21
@@ -830,18 +830,32 @@ pub fn writeValue(module: air.Module, v: Value, s: *std.json.Stringify) std.Io.W
|
||||
}
|
||||
}
|
||||
|
||||
/// Writes bytes as a JSON string: printable ASCII as is, every other byte
|
||||
/// as `\u00XX`, so arbitrary output is valid, deterministic JSON.
|
||||
fn writeBytes(w: *std.Io.Writer, bytes: []const u8) std.Io.Writer.Error!void {
|
||||
try w.writeByte('"');
|
||||
for (bytes) |b| switch (b) {
|
||||
'"' => try w.writeAll("\\\""),
|
||||
'\\' => try w.writeAll("\\\\"),
|
||||
'\n' => try w.writeAll("\\n"),
|
||||
0x20...0x21, 0x23...0x5b, 0x5d...0x7e => try w.writeByte(b),
|
||||
else => try w.print("\\u{x:0>4}", .{b}),
|
||||
};
|
||||
try w.writeByte('"');
|
||||
/// Byte strings in the run record (spec 07a "Run result"): a JSON string
|
||||
/// when the bytes are valid UTF-8, otherwise {"encoding":"base64","data":…}.
|
||||
fn writeText(s: *std.json.Stringify, bytes: []const u8) std.Io.Writer.Error!void {
|
||||
if (std.unicode.utf8ValidateSlice(bytes)) return s.write(bytes);
|
||||
try writeBase64(s, bytes);
|
||||
}
|
||||
|
||||
/// Binary data: always {"encoding":"base64","data":…} (RFC 4648, padded).
|
||||
fn writeBase64(s: *std.json.Stringify, bytes: []const u8) std.Io.Writer.Error!void {
|
||||
try s.beginObject();
|
||||
try s.objectField("encoding");
|
||||
try s.write("base64");
|
||||
try s.objectField("data");
|
||||
var buf: [4096]u8 = undefined;
|
||||
try s.beginWriteRaw();
|
||||
try s.writer.writeByte('"');
|
||||
var i: usize = 0;
|
||||
// Encode in chunks of 3-byte groups so no padding appears mid-string.
|
||||
while (i < bytes.len) {
|
||||
const n = @min(bytes.len - i, 3 * 1024);
|
||||
try s.writer.writeAll(std.base64.standard.Encoder.encode(&buf, bytes[i..][0..n]));
|
||||
i += n;
|
||||
}
|
||||
try s.writer.writeByte('"');
|
||||
s.endWriteRaw();
|
||||
try s.endObject();
|
||||
}
|
||||
|
||||
/// Writes the run record for `outcome` as one JSON object (no newline).
|
||||
@@ -911,20 +925,14 @@ pub fn writeOutcome(module: air.Module, outcome: Outcome, out: ?Output, w: *std.
|
||||
}
|
||||
if (out) |o| if (module.declares("capabilities") and outcome != .not_instantiated) {
|
||||
try s.objectField("stdout");
|
||||
try s.beginWriteRaw();
|
||||
try writeBytes(w, o.stdout);
|
||||
s.endWriteRaw();
|
||||
try writeText(&s, o.stdout);
|
||||
try s.objectField("stderr");
|
||||
try s.beginWriteRaw();
|
||||
try writeBytes(w, o.stderr);
|
||||
s.endWriteRaw();
|
||||
try writeText(&s, o.stderr);
|
||||
try s.objectField("files");
|
||||
try s.beginObject();
|
||||
for (o.files) |file| {
|
||||
try s.objectField(file[0]);
|
||||
try s.beginWriteRaw();
|
||||
try writeBytes(w, file[1]);
|
||||
s.endWriteRaw();
|
||||
try writeBase64(&s, file[1]);
|
||||
}
|
||||
try s.endObject();
|
||||
};
|
||||
@@ -1088,3 +1096,15 @@ test "a trap at the last unit of fuel is still a trap" {
|
||||
// const, const, add (traps) = the add begins with the third unit.
|
||||
try expectRunFuel(comptime main1("u8", " %a = const.u8 255\n %b = const.u8 1\n %c = add.checked.u8 %a, %b\n ret %c\n"), "{\"status\":\"trapped\",\"code\":\"T0003\",\"kind\":\"trap.overflow\",\"function\":\"main\",\"block\":\"entry\",\"inst\":2}", 3);
|
||||
}
|
||||
|
||||
test "canonical byte strings in the run record" {
|
||||
var buf: [256]u8 = undefined;
|
||||
var w: std.Io.Writer = .fixed(&buf);
|
||||
var s: std.json.Stringify = .{ .writer = &w };
|
||||
try s.beginArray();
|
||||
try writeText(&s, "h\u{e9}\n");
|
||||
try writeText(&s, "\xff");
|
||||
try writeBase64(&s, "Aji");
|
||||
try s.endArray();
|
||||
try std.testing.expectEqualStrings("[\"h\u{e9}\\n\",{\"encoding\":\"base64\",\"data\":\"/w==\"},{\"encoding\":\"base64\",\"data\":\"QWpp\"}]", w.buffered());
|
||||
}
|
||||
+67
-4
@@ -47,6 +47,53 @@ pub const Options = struct {
|
||||
seed: u64 = 0,
|
||||
};
|
||||
|
||||
/// xoshiro256** with SplitMix64 seeding, specified bit for bit in spec 11 §8.1.
|
||||
/// Implemented here rather than taken from `std.Random`, so that neither a
|
||||
/// toolchain upgrade nor library details can change deterministic output.
|
||||
pub const Prng = struct {
|
||||
s: [4]u64,
|
||||
|
||||
pub fn init(seed: u64) Prng {
|
||||
var x = seed;
|
||||
var p: Prng = undefined;
|
||||
for (&p.s) |*w| {
|
||||
x +%= 0x9e3779b97f4a7c15;
|
||||
var z = x;
|
||||
z = (z ^ (z >> 30)) *% 0xbf58476d1ce4e5b9;
|
||||
z = (z ^ (z >> 27)) *% 0x94d049bb133111eb;
|
||||
w.* = z ^ (z >> 31);
|
||||
}
|
||||
return p;
|
||||
}
|
||||
|
||||
pub fn next(p: *Prng) u64 {
|
||||
const s = &p.s;
|
||||
const result = std.math.rotl(u64, s[1] *% 5, 7) *% 9;
|
||||
const t = s[1] << 17;
|
||||
s[2] ^= s[0];
|
||||
s[3] ^= s[1];
|
||||
s[1] ^= s[2];
|
||||
s[0] ^= s[3];
|
||||
s[2] ^= t;
|
||||
s[3] = std.math.rotl(u64, s[3], 45);
|
||||
return result;
|
||||
}
|
||||
|
||||
/// Each call consumes ceil(len / 8) outputs; bytes are taken least
|
||||
/// significant first; unused bytes of the last output are discarded.
|
||||
pub fn fill(p: *Prng, buf: []u8) void {
|
||||
var i: usize = 0;
|
||||
while (i < buf.len) {
|
||||
const v = p.next();
|
||||
var k: u6 = 0;
|
||||
while (k < 8 and i < buf.len) : ({
|
||||
k += 1;
|
||||
i += 1;
|
||||
}) buf[i] = @truncate(v >> (@as(u6, k) * 8));
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
pub const World = struct {
|
||||
arena: std.mem.Allocator,
|
||||
stdout: std.ArrayList(u8) = .empty,
|
||||
@@ -57,9 +104,7 @@ pub const World = struct {
|
||||
written: std.StringArrayHashMapUnmanaged(void) = .empty,
|
||||
handles: std.ArrayList(Handle) = .empty,
|
||||
listeners: std.StringHashMapUnmanaged(u32) = .empty,
|
||||
/// xoshiro256** seeded by SplitMix64 from `--seed` (spec 11 §8); pinned
|
||||
/// explicitly so a toolchain upgrade cannot change deterministic output.
|
||||
prng: std.Random.Xoshiro256,
|
||||
prng: Prng,
|
||||
|
||||
pub fn init(arena: std.mem.Allocator, o: Options) !World {
|
||||
var w: World = .{ .arena = arena, .stdin = o.stdin, .prng = .init(o.seed) };
|
||||
@@ -221,7 +266,7 @@ pub const World = struct {
|
||||
// ------------------------------------------------------------ random
|
||||
|
||||
pub fn fill(w: *World, buf: []u8) void {
|
||||
w.prng.random().bytes(buf);
|
||||
w.prng.fill(buf);
|
||||
}
|
||||
};
|
||||
|
||||
@@ -240,3 +285,21 @@ test "addresses" {
|
||||
try std.testing.expect(!World.validAddr("localhost:80"));
|
||||
try std.testing.expect(!World.validAddr("127.0.0.1:99999"));
|
||||
}
|
||||
|
||||
test "PRNG golden vectors (spec 11 §8.1)" {
|
||||
var p: Prng = .init(0);
|
||||
// SplitMix64(0) first output, a published reference value.
|
||||
try std.testing.expectEqual(@as(u64, 0xe220a8397b1dcdaf), Prng.init(0).s[0]);
|
||||
var b1: [1]u8 = undefined;
|
||||
var b7: [7]u8 = undefined;
|
||||
var b8: [8]u8 = undefined;
|
||||
var b17: [17]u8 = undefined;
|
||||
p.fill(&b1);
|
||||
p.fill(&b7);
|
||||
p.fill(&b8);
|
||||
p.fill(&b17);
|
||||
try std.testing.expectEqualSlices(u8, &.{180}, &b1);
|
||||
try std.testing.expectEqualSlices(u8, &.{ 42, 69, 86, 73, 120, 31, 110 }, &b7);
|
||||
try std.testing.expectEqualSlices(u8, &.{ 224, 230, 51, 73, 157, 132, 95, 26 }, &b8);
|
||||
try std.testing.expectEqualSlices(u8, &.{ 44, 45, 45, 38, 241, 148, 165, 106, 89, 46, 132, 31, 74, 173, 165, 187, 202 }, &b17);
|
||||
}
|
||||
Reference in new issue
Block a user