Phase 3: reference programs and schedule stress mode

- program_parallel_map: four exclusive loans of disjoint quarters
- program_channel_transfer: heap-owned messages moved through a bounded
  channel; the 64-byte budget fits exactly the bounded worst case
- program_tcp_echo_tasks: server task, one echo task per connection,
  two client tasks sharing address and message by shared loans
- runner --stress N: seeds 0..N-1 under quanta 1, 2, 7 and the test's
  own; failures print the replay command; CI runs --stress 200
- plan: P3-12, P3-13 done
This commit is contained in:
dev committed 2026-10-04 17:01:05 +03:00
1 parent 929132c81d
commit 9f322cfe31
7 files changed
+453 -7

No files matched your search

@@ -0,0 +1,233 @@
; Phase 3 exit program: TCP echo with tasks over the deterministic loopback.
; A server task accepts two connections and spawns one echo task per
; connection; two client tasks share the address and the message through
; shared loans. Result: the bytes echoed to both clients, 2 × (112+105+110+103).
; expect: run
; grant: listen=net.listen
; grant: connect=net.connect
; schedules: 30
; quantum: 5
; result: 860
; phase: 3
; spec: docs/spec/04-concurrency-and-atomics.md
module test.program_tcp_echo_tasks
air 0
lang 0.1
features [arrays, capabilities, effects, ownership, resources, tasks]
fn @main(%listen: cap<net.listen>, %connect: cap<net.connect>) -> u64
effects [net.listen, net.connect, net.read, net.write, task.spawn]
entry:
%addr = local.alloc array<u8, 11>
local.zero %addr
%addr_m = borrow.mut %addr
%addr_i0 = const.usize 0
%addr_c0 = const.u8 49
%addr_e0 = borrow.index %addr_m, %addr_i0
store %addr_e0, %addr_c0
borrow.end %addr_e0
%addr_i1 = const.usize 1
%addr_c1 = const.u8 50
%addr_e1 = borrow.index %addr_m, %addr_i1
store %addr_e1, %addr_c1
borrow.end %addr_e1
%addr_i2 = const.usize 2
%addr_c2 = const.u8 55
%addr_e2 = borrow.index %addr_m, %addr_i2
store %addr_e2, %addr_c2
borrow.end %addr_e2
%addr_i3 = const.usize 3
%addr_c3 = const.u8 46
%addr_e3 = borrow.index %addr_m, %addr_i3
store %addr_e3, %addr_c3
borrow.end %addr_e3
%addr_i4 = const.usize 4
%addr_c4 = const.u8 48
%addr_e4 = borrow.index %addr_m, %addr_i4
store %addr_e4, %addr_c4
borrow.end %addr_e4
%addr_i5 = const.usize 5
%addr_c5 = const.u8 46
%addr_e5 = borrow.index %addr_m, %addr_i5
store %addr_e5, %addr_c5
borrow.end %addr_e5
%addr_i6 = const.usize 6
%addr_c6 = const.u8 48
%addr_e6 = borrow.index %addr_m, %addr_i6
store %addr_e6, %addr_c6
borrow.end %addr_e6
%addr_i7 = const.usize 7
%addr_c7 = const.u8 46
%addr_e7 = borrow.index %addr_m, %addr_i7
store %addr_e7, %addr_c7
borrow.end %addr_e7
%addr_i8 = const.usize 8
%addr_c8 = const.u8 49
%addr_e8 = borrow.index %addr_m, %addr_i8
store %addr_e8, %addr_c8
borrow.end %addr_e8
%addr_i9 = const.usize 9
%addr_c9 = const.u8 58
%addr_e9 = borrow.index %addr_m, %addr_i9
store %addr_e9, %addr_c9
borrow.end %addr_e9
%addr_i10 = const.usize 10
%addr_c10 = const.u8 55
%addr_e10 = borrow.index %addr_m, %addr_i10
store %addr_e10, %addr_c10
borrow.end %addr_e10
borrow.end %addr_m
%addr_r = borrow.ref %addr
%addr_s = slice.make %addr_r
%ping = local.alloc array<u8, 4>
local.zero %ping
%ping_m = borrow.mut %ping
%ping_i0 = const.usize 0
%ping_c0 = const.u8 112
%ping_e0 = borrow.index %ping_m, %ping_i0
store %ping_e0, %ping_c0
borrow.end %ping_e0
%ping_i1 = const.usize 1
%ping_c1 = const.u8 105
%ping_e1 = borrow.index %ping_m, %ping_i1
store %ping_e1, %ping_c1
borrow.end %ping_e1
%ping_i2 = const.usize 2
%ping_c2 = const.u8 110
%ping_e2 = borrow.index %ping_m, %ping_i2
store %ping_e2, %ping_c2
borrow.end %ping_e2
%ping_i3 = const.usize 3
%ping_c3 = const.u8 103
%ping_e3 = borrow.index %ping_m, %ping_i3
store %ping_e3, %ping_c3
borrow.end %ping_e3
borrow.end %ping_m
%ping_r = borrow.ref %ping
%ping_s = slice.make %ping_r
intrinsic net.listen(%listen, %addr_s), ok -> @listening, err -> @f0
listening(%l: listener):
%sc = task.scope.begin
%srv = task.spawn %sc, @serve(%l)
%c1 = task.spawn %sc, @client(%connect, %addr_s, %ping_s)
%c2 = task.spawn %sc, @client(%connect, %addr_s, %ping_s)
%x = task.join %srv
%a = task.join %c1
%b = task.join %c2
task.scope.end %sc
borrow.end %ping_s
borrow.end %ping_r
borrow.end %addr_s
borrow.end %addr_r
%sum = add.checked.u64 %a, %b
ret %sum
f0(%fe0: io_error):
trap
; Accepts two connections; each is moved into its own echo task.
fn @serve(%l: listener) -> u8
effects [net.listen, net.read, net.write, task.spawn]
entry:
%sc = task.scope.begin
%lm = borrow.mut %l
intrinsic net.accept(%lm), ok -> @first, err -> @g0
first(%s1: stream):
%t1 = task.spawn %sc, @echo(%s1)
intrinsic net.accept(%lm), ok -> @second, err -> @g1
second(%s2: stream):
%t2 = task.spawn %sc, @echo(%s2)
borrow.end %lm
%x1 = task.join %t1
%x2 = task.join %t2
task.scope.end %sc
intrinsic net.close_listener(%l), ok -> @done, err -> @g2
done:
%r = const.u8 0
ret %r
g0(%ge0: io_error):
trap
g1(%ge1: io_error):
trap
g2(%ge2: io_error):
trap
fn @echo(%s: stream) -> u8
effects [net.read, net.write]
entry:
%buf = local.alloc array<u8, 4>
local.zero %buf
%sm = borrow.mut %s
%bm = borrow.mut %buf
%bs = slice.make %bm
intrinsic net.recv(%sm, %bs), ok -> @received, err -> @h0
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 -> @h1
echoed:
borrow.end %gr
borrow.end %got
borrow.end %bs
borrow.end %bm
borrow.end %sm
intrinsic net.close_stream(%s), ok -> @done, err -> @h2
done:
%r = const.u8 0
ret %r
h0(%he0: io_error):
trap
h1(%he1: io_error):
trap
h2(%he2: io_error):
trap
; Connects, sends the message, and returns the sum of the bytes echoed back.
fn @client(%connect: cap<net.connect>, %addr: slice<u8>, %msg: slice<u8>) -> u64
effects [net.connect, net.read, net.write]
entry:
%back = local.alloc array<u8, 4>
local.zero %back
intrinsic net.connect(%connect, %addr), ok -> @connected, err -> @k0
connected(%c: stream):
%cm = borrow.mut %c
intrinsic net.send_all(%cm, %msg), ok -> @sent, err -> @k1
sent:
%km = borrow.mut %back
%ks = slice.make %km
intrinsic net.recv(%cm, %ks), ok -> @answer, err -> @k2
answer(%n: usize):
borrow.end %ks
borrow.end %km
borrow.end %cm
intrinsic net.close_stream(%c), ok -> @closed, err -> @k3
closed:
%br = borrow.ref %back
%bs = slice.make %br
%z = const.usize 0
%acc0 = const.u64 0
br @sum(%z, %acc0)
sum(%i: usize, %acc: u64):
%done = eq.usize %i, %n
cbr %done, @fin(), @add(%i, %acc)
add(%j: usize, %acc2: u64):
%e = borrow.index %bs, %j
%b = load %e
borrow.end %e
%w = cast.widen.u64 %b
%acc3 = add.checked.u64 %acc2, %w
%one = const.usize 1
%j2 = add.checked.usize %j, %one
br @sum(%j2, %acc3)
fin:
borrow.end %bs
borrow.end %br
ret %acc
k0(%ke0: io_error):
trap
k1(%ke1: io_error):
trap
k2(%ke2: io_error):
trap
k3(%ke3: io_error):
trap