Phase 2: hosted profile (P2-8a)
- src/interp/host.zig: real console, files (scope checked before any host call, positional reads/writes, append from file size) and TCP through std.Io; monotonic clock and host CSPRNG; host errors mapped to the closed io_error set; open handles released at the end of the instance - interpreter dispatches intrinsics to the host or the deterministic world; never reports blocked in the hosted profile - CLI: --profile hosted; the program owns stdout/stderr, the run record goes to --result-file, otherwise a human summary on stderr - spec 11 §8.0: host error mapping table - tests/hosted/smoke.py (file copy, scope denial, stdin->stdout, TCP echo on a free port) runs in CI - works around Zig 0.17 net.Stream.read not compiling by using readWithControl
This commit is contained in:
1 parent
008654999e
commit
36e91be84c
10 files changed
+530
-51
No files matched your search
+66
-35
@@ -12,6 +12,7 @@ const std = @import("std");
|
||||
const air = @import("../air.zig");
|
||||
const diag = @import("../diag.zig");
|
||||
const world_mod = @import("world.zig");
|
||||
const host_mod = @import("host.zig");
|
||||
const budget = @import("../verifier/budget.zig");
|
||||
|
||||
pub const WorldOptions = world_mod.Options;
|
||||
@@ -117,8 +118,10 @@ pub const Options = struct {
|
||||
grants: []const Grant = &.{},
|
||||
/// Inputs of the deterministic world (spec 11 §8).
|
||||
world: WorldOptions = .{},
|
||||
/// Receives the run's observable output.
|
||||
/// Receives the run's observable output (deterministic profile).
|
||||
output: ?*Output = null,
|
||||
/// Non-null selects the `hosted` profile (spec 11 §8): real I/O through `io`.
|
||||
hosted: ?std.Io = null,
|
||||
};
|
||||
|
||||
/// Observable output of a run in the deterministic profile.
|
||||
@@ -138,6 +141,8 @@ pub fn run(arena: std.mem.Allocator, module: air.Module, entry: []const u8, opti
|
||||
const f = module.functions[idx];
|
||||
for (f.params) |p| if (p.ty != .cap) return error.EntryNotFound;
|
||||
var m: Machine = .{ .arena = arena, .module = module, .fuel = options.fuel, .world = try .init(arena, options.world) };
|
||||
if (options.hosted) |io| m.host = .{ .arena = arena, .io = io };
|
||||
defer if (m.host) |*hs| hs.deinit();
|
||||
defer if (options.output) |o| {
|
||||
o.* = m.output() catch .{ .stdout = "", .stderr = "", .files = &.{} };
|
||||
};
|
||||
@@ -229,6 +234,7 @@ const Machine = struct {
|
||||
/// Host authority table; capabilities index into it.
|
||||
authorities: std.ArrayList(Authority) = .empty,
|
||||
world: world_mod.World,
|
||||
host: ?host_mod.Host = null,
|
||||
owners: std.AutoHashMapUnmanaged(*Value.Cell, OwnInfo) = .empty,
|
||||
regions: std.ArrayList(Region) = .empty,
|
||||
/// Instructions executed so far.
|
||||
@@ -645,81 +651,106 @@ const Machine = struct {
|
||||
return v[xx.args[i]];
|
||||
}
|
||||
}.at;
|
||||
const W = world_mod.World;
|
||||
var results: [1]Value = undefined;
|
||||
var n_results: usize = 0;
|
||||
var result: ?Value = null;
|
||||
var failure: ?world_mod.Error = null;
|
||||
const is = struct {
|
||||
fn eq(a: []const u8, b: []const u8) bool {
|
||||
return std.mem.eql(u8, a, b);
|
||||
}
|
||||
}.eq;
|
||||
// `m.host` is the hosted profile; otherwise the deterministic world.
|
||||
if (is(name, "clock.monotonic")) {
|
||||
return bind(vals, inst.result, .{ .int = .{ .ty = .u64, .bits = m.steps } });
|
||||
const now = if (m.host) |*hs| hs.monotonic() else m.steps;
|
||||
return bind(vals, inst.result, .{ .int = .{ .ty = .u64, .bits = now } });
|
||||
} else if (is(name, "random.fill")) {
|
||||
const buf = try m.arena.alloc(u8, arg(vals, x, 1).slice.len);
|
||||
m.world.fill(buf);
|
||||
if (m.host) |*hs| hs.fill(buf) else m.world.fill(buf);
|
||||
fillSlice(arg(vals, x, 1), buf);
|
||||
return .next;
|
||||
} else if (is(name, "io.stdout_write") or is(name, "io.stderr_write")) {
|
||||
try m.world.writeOut(is(name, "io.stderr_write"), try m.bytesOf(arg(vals, x, 1)));
|
||||
const bytes = try m.bytesOf(arg(vals, x, 1));
|
||||
const err_stream = is(name, "io.stderr_write");
|
||||
if (m.host) |*hs| failure = hs.writeOut(err_stream, bytes) else try m.world.writeOut(err_stream, bytes);
|
||||
} else if (is(name, "io.stdin_read")) {
|
||||
const buf = try m.arena.alloc(u8, arg(vals, x, 1).slice.len);
|
||||
const n = m.world.readIn(buf);
|
||||
const n: usize = if (m.host) |*hs| switch (hs.readIn(buf)) {
|
||||
.ok => |k| k,
|
||||
.err => |e| blk: {
|
||||
failure = e;
|
||||
break :blk 0;
|
||||
},
|
||||
} else m.world.readIn(buf);
|
||||
fillSlice(arg(vals, x, 1), buf[0..n]);
|
||||
results[0] = .{ .int = .{ .ty = .usize, .bits = n } };
|
||||
n_results = 1;
|
||||
result = .{ .int = .{ .ty = .usize, .bits = n } };
|
||||
} else if (is(name, "fs.open_read") or is(name, "fs.create") or is(name, "fs.open_append")) {
|
||||
const scope = m.authorities.items[arg(vals, x, 0).cap].scope;
|
||||
const mode: air.authority.Resource = if (is(name, "fs.open_read")) .file_read else if (is(name, "fs.create")) .file_write else .file_append;
|
||||
switch (try m.world.open(scope, try m.bytesOf(arg(vals, x, 1)), mode)) {
|
||||
.ok => |hd| {
|
||||
results[0] = .{ .resource = hd };
|
||||
n_results = 1;
|
||||
},
|
||||
const path = try m.bytesOf(arg(vals, x, 1));
|
||||
switch (if (m.host) |*hs| try hs.open(scope, path, mode) else try m.world.open(scope, path, mode)) {
|
||||
.ok => |hd| result = .{ .resource = hd },
|
||||
.err => |e| failure = e,
|
||||
}
|
||||
} else if (is(name, "fs.read")) {
|
||||
const buf = try m.arena.alloc(u8, arg(vals, x, 1).slice.len);
|
||||
const n = m.world.fileRead(resourceOf(arg(vals, x, 0)), buf);
|
||||
const hd = resourceOf(arg(vals, x, 0));
|
||||
const n: usize = if (m.host) |*hs| switch (hs.fileRead(hd, buf)) {
|
||||
.ok => |k| k,
|
||||
.err => |e| blk: {
|
||||
failure = e;
|
||||
break :blk 0;
|
||||
},
|
||||
} else m.world.fileRead(hd, buf);
|
||||
fillSlice(arg(vals, x, 1), buf[0..n]);
|
||||
results[0] = .{ .int = .{ .ty = .usize, .bits = n } };
|
||||
n_results = 1;
|
||||
result = .{ .int = .{ .ty = .usize, .bits = n } };
|
||||
} else if (is(name, "fs.write_all")) {
|
||||
try m.world.fileWrite(resourceOf(arg(vals, x, 0)), try m.bytesOf(arg(vals, x, 1)));
|
||||
const hd = resourceOf(arg(vals, x, 0));
|
||||
const bytes = try m.bytesOf(arg(vals, x, 1));
|
||||
if (m.host) |*hs| failure = hs.fileWrite(hd, bytes) else try m.world.fileWrite(hd, bytes);
|
||||
} else if (is(name, "fs.sync")) {
|
||||
// Durable by construction in the in-memory file system.
|
||||
// The in-memory file system is durable by construction.
|
||||
if (m.host) |*hs| failure = hs.sync(resourceOf(arg(vals, x, 0)));
|
||||
} else if (is(name, "fs.close") or is(name, "net.close_stream") or is(name, "net.close_listener")) {
|
||||
m.world.close(arg(vals, x, 0).resource);
|
||||
if (m.host) |*hs| hs.close(arg(vals, x, 0).resource) else m.world.close(arg(vals, x, 0).resource);
|
||||
} else if (is(name, "net.listen") or is(name, "net.connect")) {
|
||||
const addr = try m.bytesOf(arg(vals, x, 1));
|
||||
switch (if (is(name, "net.listen")) try m.world.listen(addr) else try m.world.connect(addr)) {
|
||||
.ok => |hd| {
|
||||
results[0] = .{ .resource = hd };
|
||||
n_results = 1;
|
||||
},
|
||||
const listen = is(name, "net.listen");
|
||||
const r = if (m.host) |*hs|
|
||||
(if (listen) try hs.listen(addr) else try hs.connect(addr))
|
||||
else
|
||||
(if (listen) try m.world.listen(addr) else try m.world.connect(addr));
|
||||
switch (r) {
|
||||
.ok => |hd| result = .{ .resource = hd },
|
||||
.err => |e| failure = e,
|
||||
}
|
||||
} else if (is(name, "net.accept")) {
|
||||
const s = m.world.accept(resourceOf(arg(vals, x, 0))) orelse return .blocked;
|
||||
results[0] = .{ .resource = s };
|
||||
n_results = 1;
|
||||
const hd = resourceOf(arg(vals, x, 0));
|
||||
if (m.host) |*hs| switch (try hs.accept(hd)) {
|
||||
.ok => |s| result = .{ .resource = s },
|
||||
.err => |e| failure = e,
|
||||
} else result = .{ .resource = m.world.accept(hd) orelse return .blocked };
|
||||
} else if (is(name, "net.recv")) {
|
||||
const buf = try m.arena.alloc(u8, arg(vals, x, 1).slice.len);
|
||||
const n = m.world.recv(resourceOf(arg(vals, x, 0)), buf) orelse return .blocked;
|
||||
const hd = resourceOf(arg(vals, x, 0));
|
||||
const n: usize = if (m.host) |*hs| switch (hs.recv(hd, buf)) {
|
||||
.ok => |k| k,
|
||||
.err => |e| blk: {
|
||||
failure = e;
|
||||
break :blk 0;
|
||||
},
|
||||
} else m.world.recv(hd, buf) orelse return .blocked;
|
||||
fillSlice(arg(vals, x, 1), buf[0..n]);
|
||||
results[0] = .{ .int = .{ .ty = .usize, .bits = n } };
|
||||
n_results = 1;
|
||||
result = .{ .int = .{ .ty = .usize, .bits = n } };
|
||||
} else if (is(name, "net.send_all")) {
|
||||
failure = try m.world.send(resourceOf(arg(vals, x, 0)), try m.bytesOf(arg(vals, x, 1)));
|
||||
const hd = resourceOf(arg(vals, x, 0));
|
||||
const bytes = try m.bytesOf(arg(vals, x, 1));
|
||||
failure = if (m.host) |*hs| hs.send(hd, bytes) else try m.world.send(hd, bytes);
|
||||
} else unreachable; // every registered intrinsic is handled above
|
||||
_ = W;
|
||||
|
||||
if (failure) |e| {
|
||||
vals[f.blocks[x.err.?].params[0].value] = .{ .builtin = .{ .which = .io_error, .variant = @backingInt(e) } };
|
||||
return .{ .jump = x.err.? };
|
||||
}
|
||||
for (f.blocks[x.ok.?].params, results[0..n_results]) |p, r| vals[p.value] = r;
|
||||
if (result) |r| vals[f.blocks[x.ok.?].params[0].value] = r;
|
||||
return .{ .jump = x.ok.? };
|
||||
}
|
||||
|
||||
|
||||
Reference in new issue
Block a user