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
@@ -0,0 +1,242 @@
|
||||
#!/usr/bin/env python3
|
||||
"""Hosted-profile smoke tests (spec 11 §8): run AIR programs against the real OS.
|
||||
|
||||
Unlike the conformance suite these runs are not deterministic (real files,
|
||||
real sockets, real clock), so they check observable effects instead of run
|
||||
records. Usage: python3 tests/hosted/smoke.py [path/to/aji]
|
||||
"""
|
||||
|
||||
import json
|
||||
import os
|
||||
import socket
|
||||
import subprocess
|
||||
import sys
|
||||
import tempfile
|
||||
|
||||
AJI = sys.argv[1] if len(sys.argv) > 1 else "zig-out/bin/aji"
|
||||
HEADER = "module smoke\nair 0\nlang 0.1\nfeatures [arrays, capabilities, effects, ownership, resources]\n\n"
|
||||
|
||||
|
||||
def text(name, s):
|
||||
"""Instructions that put the bytes of `s` into local %name (entry block)."""
|
||||
b = s.encode()
|
||||
out = [f" %{name} = local.alloc array<u8, {len(b)}>", f" local.zero %{name}", f" %{name}_m = borrow.mut %{name}"]
|
||||
for i, c in enumerate(b):
|
||||
out += [f" %{name}_i{i} = const.usize {i}", f" %{name}_c{i} = const.u8 {c}",
|
||||
f" %{name}_e{i} = borrow.index %{name}_m, %{name}_i{i}", f" store %{name}_e{i}, %{name}_c{i}",
|
||||
f" borrow.end %{name}_e{i}"]
|
||||
out += [f" borrow.end %{name}_m", f" %{name}_r = borrow.ref %{name}", f" %{name}_s = slice.make %{name}_r"]
|
||||
return "\n".join(out)
|
||||
|
||||
|
||||
def end(name):
|
||||
return f" borrow.end %{name}_s\n borrow.end %{name}_r"
|
||||
|
||||
|
||||
def run(tmp, name, program, grants, stdin=b""):
|
||||
path = os.path.join(tmp, name + ".air")
|
||||
with open(path, "w") as f:
|
||||
f.write(HEADER + program)
|
||||
result = os.path.join(tmp, name + ".json")
|
||||
cmd = [AJI, "run", "--profile", "hosted", "--result-file", result, "--fuel", "10000000"]
|
||||
for g in grants:
|
||||
cmd += ["--grant", g]
|
||||
p = subprocess.run(cmd + [path], input=stdin, capture_output=True, timeout=60)
|
||||
with open(result) as f:
|
||||
record = json.load(f)
|
||||
return p, record
|
||||
|
||||
|
||||
def check(cond, what):
|
||||
if not cond:
|
||||
print("FAIL", what)
|
||||
sys.exit(1)
|
||||
print("ok ", what)
|
||||
|
||||
|
||||
def main():
|
||||
with tempfile.TemporaryDirectory() as tmp:
|
||||
tmp = os.path.realpath(tmp)
|
||||
src_dir, dst_dir = os.path.join(tmp, "in"), os.path.join(tmp, "out")
|
||||
os.makedirs(src_dir)
|
||||
os.makedirs(dst_dir)
|
||||
payload = b"hosted Aji copies real files.\n" * 40
|
||||
with open(os.path.join(src_dir, "data.bin"), "wb") as f:
|
||||
f.write(payload)
|
||||
|
||||
# File copy through scoped capabilities.
|
||||
prog = f"""fn @main(%src: cap<fs.read>, %dst: cap<fs.write>) -> void
|
||||
effects [fs.read, fs.write]
|
||||
entry:
|
||||
{text('ip', src_dir + '/data.bin')}
|
||||
{text('op', dst_dir + '/copy.bin')}
|
||||
%buf = local.alloc array<u8, 64>
|
||||
local.zero %buf
|
||||
intrinsic fs.open_read(%src, %ip_s), ok -> @opened_in, err -> @fail
|
||||
opened_in(%in: file<read>):
|
||||
intrinsic fs.create(%dst, %op_s), ok -> @opened_out, err -> @fail_in
|
||||
opened_out(%out: file<write>):
|
||||
%bm = borrow.mut %buf
|
||||
%bs = slice.make %bm
|
||||
%fi = borrow.mut %in
|
||||
%fo = borrow.mut %out
|
||||
br @loop()
|
||||
loop:
|
||||
intrinsic fs.read(%fi, %bs), ok -> @got, err -> @fail_loop
|
||||
got(%n: usize):
|
||||
%z = const.usize 0
|
||||
%eof = eq.usize %n, %z
|
||||
cbr %eof, @finish(), @write()
|
||||
write:
|
||||
%chunk = slice.sub %bs, %z, %n
|
||||
%cr = borrow.ref %chunk
|
||||
intrinsic fs.write_all(%fo, %cr), ok -> @wrote, err -> @fail_write
|
||||
wrote:
|
||||
borrow.end %cr
|
||||
borrow.end %chunk
|
||||
br @loop()
|
||||
finish:
|
||||
borrow.end %fo
|
||||
borrow.end %fi
|
||||
borrow.end %bs
|
||||
borrow.end %bm
|
||||
intrinsic fs.close(%in), ok -> @closed_in, err -> @fail_close_in
|
||||
closed_in:
|
||||
intrinsic fs.close(%out), ok -> @done, err -> @fail_close_out
|
||||
done:
|
||||
{end('op')}
|
||||
{end('ip')}
|
||||
ret
|
||||
fail(%e: io_error):
|
||||
trap
|
||||
fail_in(%e2: io_error):
|
||||
trap
|
||||
fail_loop(%e3: io_error):
|
||||
trap
|
||||
fail_write(%e4: io_error):
|
||||
trap
|
||||
fail_close_in(%e5: io_error):
|
||||
trap
|
||||
fail_close_out(%e6: io_error):
|
||||
trap
|
||||
"""
|
||||
p, rec = run(tmp, "copy", prog, [f"src=fs.read:{src_dir}", f"dst=fs.write:{dst_dir}"])
|
||||
check(rec["status"] == "returned", f"file copy returned ({rec})")
|
||||
with open(os.path.join(dst_dir, "copy.bin"), "rb") as f:
|
||||
check(f.read() == payload, "copied bytes are identical")
|
||||
|
||||
# A path outside the scope never reaches the OS: permission_denied.
|
||||
prog = f"""fn @main(%src: cap<fs.read>) -> u8
|
||||
effects [fs.read]
|
||||
entry:
|
||||
{text('p', src_dir + '/data.bin')}
|
||||
intrinsic fs.open_read(%src, %p_s), ok -> @opened, err -> @failed
|
||||
opened(%f: file<read>):
|
||||
trap
|
||||
failed(%e: io_error):
|
||||
{end('p')}
|
||||
enum.switch io_error %e, permission_denied -> @denied, default -> @other()
|
||||
denied:
|
||||
%one = const.u8 1
|
||||
ret %one
|
||||
other:
|
||||
%two = const.u8 2
|
||||
ret %two
|
||||
"""
|
||||
p, rec = run(tmp, "scope", prog, [f"src=fs.read:{dst_dir}"])
|
||||
check(rec.get("value") == 1, f"out-of-scope path is permission_denied ({rec})")
|
||||
|
||||
# stdin -> stdout through the real streams.
|
||||
prog = """fn @main(%in: cap<io.read>, %out: cap<io.write>) -> void
|
||||
effects [io.read, io.write]
|
||||
entry:
|
||||
%b = local.alloc array<u8, 32>
|
||||
local.zero %b
|
||||
%m = borrow.mut %b
|
||||
%s = slice.make %m
|
||||
intrinsic io.stdin_read(%in, %s), ok -> @got, err -> @fail
|
||||
got(%n: usize):
|
||||
%z = const.usize 0
|
||||
%part = slice.sub %s, %z, %n
|
||||
%pr = borrow.ref %part
|
||||
intrinsic io.stdout_write(%out, %pr), ok -> @done, err -> @fail2
|
||||
done:
|
||||
borrow.end %pr
|
||||
borrow.end %part
|
||||
borrow.end %s
|
||||
borrow.end %m
|
||||
ret
|
||||
fail(%e: io_error):
|
||||
trap
|
||||
fail2(%e2: io_error):
|
||||
trap
|
||||
"""
|
||||
p, rec = run(tmp, "echo", prog, ["in=io.read", "out=io.write"], stdin=b"through the host")
|
||||
check(p.stdout == b"through the host", f"stdin echoed to stdout ({p.stdout!r})")
|
||||
|
||||
# TCP echo over real sockets on a free port.
|
||||
with socket.socket() as probe:
|
||||
probe.bind(("127.0.0.1", 0))
|
||||
port = probe.getsockname()[1]
|
||||
addr = f"127.0.0.1:{port}"
|
||||
prog = f"""fn @main(%listen: cap<net.listen>, %connect: cap<net.connect>) -> array<u8, 4>
|
||||
effects [net.listen, net.connect, net.read, net.write]
|
||||
entry:
|
||||
{text('addr', addr)}
|
||||
{text('ping', 'ping')}
|
||||
%buf = local.alloc array<u8, 4>
|
||||
local.zero %buf
|
||||
%back = local.alloc array<u8, 4>
|
||||
local.zero %back
|
||||
intrinsic net.listen(%listen, %addr_s), ok -> @listening, err -> @fail
|
||||
listening(%l: listener):
|
||||
intrinsic net.connect(%connect, %addr_s), ok -> @connected, err -> @fail1
|
||||
connected(%client: stream):
|
||||
%lm = borrow.mut %l
|
||||
intrinsic net.accept(%lm), ok -> @accepted, err -> @fail2
|
||||
accepted(%server: stream):
|
||||
borrow.end %lm
|
||||
%cm = borrow.mut %client
|
||||
intrinsic net.send_all(%cm, %ping_s), ok -> @sent, err -> @fail3
|
||||
sent:
|
||||
%sm = borrow.mut %server
|
||||
%bm = borrow.mut %buf
|
||||
%bs = slice.make %bm
|
||||
intrinsic net.recv(%sm, %bs), ok -> @received, err -> @fail4
|
||||
received(%n: usize):
|
||||
%z = const.usize 0
|
||||
%got = slice.sub %bs, %z, %n
|
||||
%gr = borrow.ref %got
|
||||
intrinsic net.send_all(%sm, %gr), ok -> @echoed, err -> @fail5
|
||||
echoed:
|
||||
borrow.end %gr
|
||||
borrow.end %got
|
||||
borrow.end %bs
|
||||
borrow.end %bm
|
||||
borrow.end %sm
|
||||
%km = borrow.mut %back
|
||||
%ks = slice.make %km
|
||||
intrinsic net.recv(%cm, %ks), ok -> @answer, err -> @fail6
|
||||
answer(%m: usize):
|
||||
borrow.end %ks
|
||||
borrow.end %km
|
||||
borrow.end %cm
|
||||
intrinsic net.close_stream(%server), ok -> @c1, err -> @fail7
|
||||
c1:
|
||||
intrinsic net.close_stream(%client), ok -> @c2, err -> @fail8
|
||||
c2:
|
||||
intrinsic net.close_listener(%l), ok -> @c3, err -> @fail9
|
||||
c3:
|
||||
{end('ping')}
|
||||
{end('addr')}
|
||||
%r = local.load %back
|
||||
ret %r
|
||||
""" + "".join(f"fail{s}(%e{s}: io_error):\n trap\n" for s in ["", "1", "2", "3", "4", "5", "6", "7", "8", "9"])
|
||||
p, rec = run(tmp, "tcp", prog, ["listen=net.listen", "connect=net.connect"])
|
||||
check(rec.get("value") == [112, 105, 110, 103], f"TCP echo over real sockets ({rec})")
|
||||
|
||||
print("hosted smoke tests passed")
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
Reference in new issue
Block a user