Skip to content

Commit d26d5af

Browse files
committed
flow: a Region that ends while a handle that allocates in it is still open is an error (region-ended-under-handle): the handle's state and the data its ops made would dangle; a shut handle (in an end state it reached by an op) does not count; neg tests handle_row_end, handle_row_end_state
1 parent 415b7fa commit d26d5af

7 files changed

Lines changed: 146 additions & 58 deletions

File tree

‎CHANGELOG.md‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ Every change to srs that lands, newest first. A project that builds with srs (sn
44

55
## Unreleased
66

7+
- flow: a Region that ends (an op into an end state) while a handle that allocates in it is still open is an error (`error[region-ended-under-handle]`); before, the handle's next op wrote into or read freed memory. A handle already shut by an op does not count.
8+
79
- types: a handler fn takes its op's parameter types: each value param after `self` must have the op's declared type (`error[mismatch]` at the fn); before, only the count was checked, so a handler body read its op's arguments under types of its own choosing.
810

911
- arena: a program past 32 MiB gets huge pages without handling `Srs.Pages`: the Arena arms the gate itself at its second reservation, so a small program pays nothing and a large one faults once per 2 MiB (snx on a NixOS system: 881k -> 356k page faults, system time 5.3 -> 3.0 s; a 64 MB list program: 15.7k -> 8.0k faults).

‎bootstrap/s2c-seed.c‎

Lines changed: 54 additions & 43 deletions
Large diffs are not rendered by default.

‎s2/flow.srs‎

Lines changed: 41 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,8 @@ enum Flow.Clash:
9494
case Flow.NoSol(line: I64, cap: Sym.Sym)
9595
// a value used after its region ended (s1 "use of value after its region ended"); cap = the region
9696
case Flow.Dead(line: I64, cap: Sym.Sym)
97+
// a Region ended (by an op into an end state) while a handle that allocates in it is still open; cap = the region
98+
case Flow.RowEnd(line: I64, cap: Sym.Sym)
9799
// a block value that lives in a capability the block closes (s1 "value escapes the scope of its region")
98100
case Flow.Escape(line: I64, cap: Sym.Sym)
99101
// a value of a type with <at(c, S)> read where its capability is not in S (s1 "value needs c in state S"); name = the value
@@ -154,6 +156,9 @@ enum Flow.Rel:
154156
case Flow.REdge
155157
case Flow.RFin
156158
case Flow.RDead
159+
// site(h, r): handle h allocates in Region r (its site); rowEnd(line, r): r reaches an end state while h is still open
160+
case Flow.RSite
161+
case Flow.RRowEnd
157162
case Flow.REscCap
158163
case Flow.REscVar
159164
case Flow.REscape
@@ -228,6 +233,8 @@ def Flow.relSym(x: Flow.Rel): Sym.Sym = x match
228233
case Flow.REdge => Sym.of("edge")
229234
case Flow.RFin => Sym.of("fin")
230235
case Flow.RDead => Sym.of("dead")
236+
case Flow.RSite => Sym.of("site")
237+
case Flow.RRowEnd => Sym.of("rowEnd")
231238
case Flow.REscCap => Sym.of("escCap")
232239
case Flow.REscVar => Sym.of("escVar")
233240
case Flow.REscape => Sym.of("escape")
@@ -257,12 +264,14 @@ enum Flow.Kind:
257264
case Flow.KExitBad
258265
case Flow.KNoSol
259266
case Flow.KDead
267+
case Flow.KRowEnd
260268
case Flow.KEscape
261269
case Flow.KStale
262270
case Flow.KMsState
263271
case Flow.KBound
264272

265273
def Flow.relOf(k: Flow.Kind): Flow.Rel = k match
274+
case Flow.KRowEnd => Flow.RRowEnd
266275
case Flow.KStuck => Flow.RStuck
267276
case Flow.KSplit => Flow.RSplitAt
268277
case Flow.KUnended => Flow.RUnendedAt
@@ -302,6 +311,7 @@ def Flow.fCapOf(cap: I64, proto: Sym.Sym): D.Fact = Flow.fact2(Flow.RCapOf, Flow
302311
def Flow.fCapName(cap: I64, name: Sym.Sym): D.Fact = Flow.fact2(Flow.RCapName, Flow.i(cap), Flow.s(name))
303312
def Flow.fCapLine(cap: I64, line: I64): D.Fact = Flow.fact2(Flow.RCapLine, Flow.i(cap), Flow.i(line))
304313
def Flow.fHandle(p: I64, cap: I64): D.Fact = Flow.fact2(Flow.RHandle, Flow.i(p), Flow.i(cap))
314+
def Flow.fSite(h: I64, r: I64): D.Fact = Flow.fact2(Flow.RSite, Flow.i(h), Flow.i(r))
305315
def Flow.fTr(proto: Sym.Sym, op: Sym.Sym, a: Sym.Sym, b: Sym.Sym): D.Fact =
306316
Flow.fact4(Flow.RTr, Flow.s(proto), Flow.s(op), Flow.s(a), Flow.s(b))
307317
def Flow.fStart(proto: Sym.Sym, s: Sym.Sym): D.Fact = Flow.fact2(Flow.RStart, Flow.s(proto), Flow.s(s))
@@ -1756,20 +1766,27 @@ def Flow.stmt(cx: Flow.Cx, st: Flow.St, p: I64, s: Ast.Stmt): Flow.At = s match
17561766
val Flow.At(st2, id) = Flow.fresh(st1)
17571767
val Flow.St(n, fs, cs, env, lp) = st2
17581768
val Flow.Cx(_, _, _, _, _, hrows, _) = cx
1759-
val st3 =
1760-
Flow.St(n, fs, Flow.withSite(hrows, h, x, line, cs, Coll.Cons(Flow.Cap(x, id, proto, line), cs)), env, lp)
1769+
val site = Flow.siteOf(hrows, h, cs)
1770+
val st3 = Flow.St(n, fs, Flow.withSite(site, x, line, Coll.Cons(Flow.Cap(x, id, proto, line), cs)), env, lp)
17611771
val st4 = Flow.add(Flow.add(st3, Flow.fCapOf(id, proto)), Flow.fCapName(id, x))
1762-
Flow.At(Flow.add(Flow.add(st4, Flow.fCapLine(id, line)), Flow.fHandle(pa, id)), pa)
1763-
1764-
// the capabilities after a handle of x by handler h: x, and when h's row has Region the pseudo capability site.x = the innermost Region
1765-
// at the handle site (where the handler builds the data its ops return)
1766-
def Flow.withSite(rows: Eff.Rows, h: Sym.Sym, x: Sym.Sym, line: I64, before: Coll.List[Flow.Cap], after: Coll.List[Flow.Cap]): Coll.List[Flow.Cap] =
1767-
if Eff.has(rows, h, Sym.of("Region")) then Flow.innermost(before, Sym.of("Region")) match
1768-
case Coll.None => after
1769-
case Coll.Some(r) =>
1770-
val Flow.Cap(_, rid, _, _) = r
1771-
Coll.Cons(Flow.Cap(Eff.dotted("site", x), rid, Sym.of("Site"), line), after)
1772-
else after
1772+
val st5 = site match
1773+
case Coll.None => st4
1774+
case Coll.Some(r) =>
1775+
val Flow.Cap(_, rid, _, _) = r
1776+
Flow.add(st4, Flow.fSite(id, rid))
1777+
Flow.At(Flow.add(Flow.add(st5, Flow.fCapLine(id, line)), Flow.fHandle(pa, id)), pa)
1778+
1779+
// the site of a handle by handler h: when h's row has Region, the innermost Region in scope at the handle (where the
1780+
// handler builds its state and the data its ops return)
1781+
def Flow.siteOf(rows: Eff.Rows, h: Sym.Sym, before: Coll.List[Flow.Cap]): Coll.Option[Flow.Cap] =
1782+
if Eff.has(rows, h, Sym.of("Region")) then Flow.innermost(before, Sym.of("Region")) else Coll.None
1783+
1784+
// the capabilities after a handle of x: x, and with a site the pseudo capability site.x = that Region
1785+
def Flow.withSite(site: Coll.Option[Flow.Cap], x: Sym.Sym, line: I64, after: Coll.List[Flow.Cap]): Coll.List[Flow.Cap] = site match
1786+
case Coll.None => after
1787+
case Coll.Some(r) =>
1788+
val Flow.Cap(_, rid, _, _) = r
1789+
Coll.Cons(Flow.Cap(Eff.dotted("site", x), rid, Sym.of("Site"), line), after)
17731790

17741791
// ── the protocols as facts: transitions, end states (an end state, or a state whose drop op reaches one), states ──
17751792
def Flow.protoFacts(ps: Coll.List[Ast.Proto], fs: Coll.List[D.Fact]): Coll.List[D.Fact] = ps match
@@ -2023,7 +2040,7 @@ def Flow.clashLN(k: Flow.Kind, a: D.Val, b: D.Val): Coll.Option[Flow.Clash] = a
20232040

20242041
// every clash relation of a solved model, read
20252042
def Flow.readAll(m: D.Model): Flow.Read =
2026-
Flow.clashes(m, Flow.KBound, Flow.clashes(m, Flow.KMsState, Flow.clashes(m, Flow.KStale, Flow.clashes(m, Flow.KStuck, Flow.clashes(m, Flow.KSplit, Flow.clashes(m, Flow.KUnended, Flow.clashes(m, Flow.KAmbig, Flow.clashes(m, Flow.KExitBad, Flow.clashes(m, Flow.KNoSol, Flow.clashes(m, Flow.KDead, Flow.clashes(m, Flow.KEscape, Flow.Got(Coll.Nil))))))))))))
2043+
Flow.clashes(m, Flow.KBound, Flow.clashes(m, Flow.KMsState, Flow.clashes(m, Flow.KStale, Flow.clashes(m, Flow.KStuck, Flow.clashes(m, Flow.KSplit, Flow.clashes(m, Flow.KUnended, Flow.clashes(m, Flow.KAmbig, Flow.clashes(m, Flow.KExitBad, Flow.clashes(m, Flow.KNoSol, Flow.clashes(m, Flow.KDead, Flow.clashes(m, Flow.KRowEnd, Flow.clashes(m, Flow.KEscape, Flow.Got(Coll.Nil)))))))))))))
20272044

20282045
def Flow.make(k: Flow.Kind, line: I64, name: Sym.Sym): Flow.Clash = k match
20292046
case Flow.KStuck => Flow.Stuck(line, name)
@@ -2033,6 +2050,7 @@ def Flow.make(k: Flow.Kind, line: I64, name: Sym.Sym): Flow.Clash = k match
20332050
case Flow.KExitBad => Flow.ExitBad(line, name)
20342051
case Flow.KNoSol => Flow.NoSol(line, name)
20352052
case Flow.KDead => Flow.Dead(line, name)
2053+
case Flow.KRowEnd => Flow.RowEnd(line, name)
20362054
case Flow.KEscape => Flow.Escape(line, name)
20372055
case Flow.KStale => Flow.Stale(line, name)
20382056
case Flow.KMsState => Flow.MsState(line, name)
@@ -2167,7 +2185,15 @@ def Flow.capsRules(): Coll.List[D.Rule] =
21672185
// a read of x at p uses every region x holds there; a region in an end state is dead
21682186
val dead =
21692187
Flow.rule(Flow.RDead, Flow.l2(8, 9), Coll.Cons(Flow.pos(Flow.RReadAt, Flow.l4(0, 14, 8, 16)), Coll.Cons(Flow.pos(Flow.RHolds, Flow.l3(0, 14, 3)), Flow.l4(Flow.pos(Flow.RAt, Flow.l4(0, 2, 3, 4)), Flow.pos(Flow.RCapOf, Flow.l2(3, 7)), Flow.pos(Flow.RFin, Flow.l2(7, 4)), Flow.pos(Flow.RCapName, Flow.l2(3, 9))))))
2170-
Coll.Cons(e1, Coll.Cons(e2, Coll.Cons(e3, Coll.Cons(h1, Coll.Cons(h2, Coll.Cons(h3, Flow.l1(dead)))))))
2188+
// an op that takes a Region r to an end state (at q) while a handle h with site r is still open (h has a state at p,
2189+
// not an end state of its protocol: a shut handle is not used again and its drop does not run): h's state and the data
2190+
// its ops made would dangle (vic-de9x.18); line 8 = the op's, name 9 = r's, h 14, its state 15, its protocol 16
2191+
val rowEnd =
2192+
Flow.rule(Flow.RRowEnd, Flow.l2(8, 9), Coll.Cons(Flow.pos(Flow.RStep, Flow.l4(0, 1, 3, 6)), Coll.Cons(Flow.pos(Flow.RAt, Flow.l4(1, 2, 3, 5)), Coll.Cons(Flow.pos(Flow.RCapOf, Flow.l2(3, 7)), Coll.Cons(Flow.pos(Flow.RFin, Flow.l2(7, 5)), Coll.Cons(Flow.pos(Flow.RSite, Flow.l2(14, 3)), Coll.Cons(Flow.pos(Flow.RAt, Flow.l4(0, 2, 14, 15)), Coll.Cons(Flow.pos(Flow.RCapOf, Flow.l2(14, 16)), Coll.Cons(Flow.neg(Flow.RFin, Flow.l2(16, 15)), Coll.Cons(Flow.pos(Flow.RLine, Flow.l2(0, 8)), Flow.l1(Flow.pos(Flow.RCapName, Flow.l2(3, 9)))))))))))))
2193+
// the same when h sits in its start state, an end state too for a protocol without states: still open, not shut
2194+
val rowEnd2 =
2195+
Flow.rule(Flow.RRowEnd, Flow.l2(8, 9), Coll.Cons(Flow.pos(Flow.RStep, Flow.l4(0, 1, 3, 6)), Coll.Cons(Flow.pos(Flow.RAt, Flow.l4(1, 2, 3, 5)), Coll.Cons(Flow.pos(Flow.RCapOf, Flow.l2(3, 7)), Coll.Cons(Flow.pos(Flow.RFin, Flow.l2(7, 5)), Coll.Cons(Flow.pos(Flow.RSite, Flow.l2(14, 3)), Coll.Cons(Flow.pos(Flow.RAt, Flow.l4(0, 2, 14, 15)), Coll.Cons(Flow.pos(Flow.RCapOf, Flow.l2(14, 16)), Coll.Cons(Flow.pos(Flow.RStart, Flow.l2(16, 15)), Coll.Cons(Flow.pos(Flow.RLine, Flow.l2(0, 8)), Flow.l1(Flow.pos(Flow.RCapName, Flow.l2(3, 9)))))))))))))
2196+
Coll.Cons(e1, Coll.Cons(e2, Coll.Cons(e3, Coll.Cons(h1, Coll.Cons(h2, Coll.Cons(h3, Coll.Cons(dead, Coll.Cons(rowEnd, Flow.l1(rowEnd2)))))))))
21712197

21722198
// a block end e closes c: a block value in c escapes; every binding that holds c there (an outer val or var: a name
21732199
// read after e is never one of the block's own bindings) holds a value whose region ended, from e on every path (also

‎s2c/main.srs‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -480,6 +480,8 @@ def S2c.showFlow(fs: Coll.List[S2c.File], cs: Coll.List[Flow.Clash]): Unit = cs
480480
S2c.diag(fs, "entry-facts-unsolvable", l, "entry facts with no solution in", S2c.Name(n), "make the entry facts satisfiable")
481481
case Flow.Dead(l, n) =>
482482
S2c.diag(fs, "use-after-free", l, "use of a value after its region ended:", S2c.Name(n), "use it before the region ends, or allocate it in a longer region")
483+
case Flow.RowEnd(l, n) =>
484+
S2c.diag(fs, "region-ended-under-handle", l, "the region ends while a handle that allocates in it is still open:", S2c.Name(n), "end the handle's block first, or open the handle in a region that outlives it")
483485
case Flow.Escape(l, n) =>
484486
S2c.diag(fs, "region-escape", l, "a value escapes the scope of its region", S2c.Name(n), "allocate it in a region that outlives the block")
485487
case Flow.Stale(l, x) =>

‎tests/s2/flow.srs‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -315,6 +315,7 @@ def FT.show(c: Flow.Clash): Unit = c match
315315
case Flow.ExitBad(line, cap) => FT.say("exitbad", line, cap)
316316
case Flow.NoSol(line, cap) => FT.say("nosol", line, cap)
317317
case Flow.Dead(line, cap) => FT.say("dead", line, cap)
318+
case Flow.RowEnd(line, cap) => FT.say("rowEnd", line, cap)
318319
case Flow.Escape(line, cap) => FT.say("escape", line, cap)
319320
case Flow.Stale(line, x) => FT.say("stale", line, x)
320321
case Flow.MsState(line, c) => FT.say("msstate", line, c)

‎tests/s2/neg/handle_row_end.srs‎

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
// format: off
2+
// expect: error[region-ended-under-handle]
3+
// the handler Hb allocates in r (the Region at its handle site); r is freed while h is still open, so the next op would
4+
// write a box into freed memory
5+
enum Bx:
6+
case Bx(v: I64)
7+
capability Bp:
8+
def mk(n: I64): Bx
9+
10+
handler Hb for Bp:
11+
def mk(n: I64): Bx = Bx(n)
12+
13+
def Bx.get(b: Bx): I64 = b match
14+
case Bx(v) => v
15+
16+
def main(): I64 =
17+
handle io = Srs.Console()
18+
handle r = Srs.Arena()
19+
handle h = Hb()
20+
val a = Bp.mk(1)
21+
r.free()
22+
val b = Bp.mk(2)
23+
Console.printI64(7)
24+
0
25+
// expect: --> tests/s2/neg/handle_row_end.srs:21
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
// format: off
2+
// expect: error[region-ended-under-handle]
3+
// the stateful handler Hs keeps its state in r; r is freed while h is still open, so the next op would read freed memory
4+
enum Sx:
5+
case Sx(v: I64)
6+
capability Sp:
7+
def get(): I64
8+
9+
handler Hs for Sp:
10+
def init(): Sx = Sx(41)
11+
def get(self: Sx): I64 = self match
12+
case Sx(v) => v + 1
13+
14+
def main(): I64 =
15+
handle io = Srs.Console()
16+
handle r = Srs.Arena()
17+
handle h = Hs()
18+
r.free()
19+
Console.printI64(Sp.get())
20+
0
21+
// expect: --> tests/s2/neg/handle_row_end_state.srs:18

0 commit comments

Comments
 (0)