You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
@unsafe lets a function be a + parameter, and the native lane fail-stops when a closure with a run-time capture is applied twice (bend file.bend and JS print the result) #2182
bend twice.bend, bend twice.bend -o twice.js && node twice.js, and bend twice.bend -o twice && ./twice, built from main 8c329c1b.
What happened
bend file.bend and the JS lane print the result; the native executable fail-stops, at one thread and at the default count.
$ bend twice.bend
5
$ bend twice.bend -o twice.js && node twice.js
5
$ bend twice.bend -o twice && ./twice
bend: runtime fail-stop
$ echo $?
1
twice takes a function as a + parameter and applies it twice. Without @unsafe the checker refuses the def:
- expected : Data
- observed : Type
Note: +f can be used many times, so its type must be Data.
With @unsafe the file is accepted (--check-only reports only that twice and main rely on unsafe code), and the C lane then fail-stops on the second application.
What I found about when it happens:
The closure has to capture a run-time value. x => Nat.add(x, 0n), which captures nothing, prints the result on all three lanes.
A loop that applies f once a turn (loop(p, f, Nat.add(acc, f(7n)))) behaves the same way: 147 on bend file.bend and JS, fail-stop natively.
A closure built by another def (mk(c) returning x => Nat.add(x, c)) behaves the same way.
The guide says @unsafe "skips the termination check" and that "reusable variables require Data". So either @unsafe should not lift the Data requirement on a + parameter, or the C lane should run what the other two lanes run. I do not know which is intended.
I traced this to the C runtime's refcount path, not the checker. On current main (0e2f38f7):
twice takes +f, so the emitter wraps the parameter in term_keep(e, _f_0, 1) at FID_twice entry (bend2/comp.ts:1645).
term_keep calls rfc_wrap (bend2/comp.ts:3848), which rejects a closure outright:
if (term_tag(t) ==TAG_CLO||term_tag(t) ==TAG_TSK) {
err_post(e.mem, ERR_RFCS);
returnt;
}
So the fail-stop happens before either application. Even if rfc_wrap allowed TAG_CLO, Clo~apply (bend2/comp.ts:4386) does spare_free on the closure segment and moves the captures, so a second application would read a freed capture. That is why it needs a runtime change rather than a one-line predicate.
On the checker side, @unsafe lifting the Data wall on + binders looks intentional: tests/check/unsafe_many.bend documents exactly that ("@unsafe also lifts the Data wall on + binders ... a function may sit under a + binder"), and it passes today. So I did not pursue making @unsafe reject this.
I prototyped the runtime direction in a scratch tree:
allow TAG_CLO in rfc_wrap;
when a closure is first shared, seal its captures (wrap each non-trivial, non-RFC capture with rfc_wrap(..., 1)); without this the first apply frees a capture the closure segment still points at, which is a real use-after-free;
in Clo~apply, use term_peek to follow the RFC redirect, keep each capture for an RFC closure, then term_drop the closure handle instead of spare_free-ing the segment;
add a WL_KEEP(N) ladder next to WL_LOAD for the capture registers.
With that, the issue's file prints 5 on the C lane, and so do variants with a captured Duo, a closure built by another def, three applications, and higher-order closures. The existing tests/compile/closure_{record_shared,dynamic_apply,partial_app,cps,slab_drop}.bend still pass, and the sealed build is clean under -DBEND_POISON. An earlier unsealed prototype also survived 30 runs at 16 threads with a captured Duo, but it still had the use-after-free, so I would want a proper concurrent stress of the sealed version before trusting it.
The direction works, but it is not free: the prototype adds about 180 ttok to comp.ts, landing roughly 175 over the permanent 66,000 cap, and it needs review of the capture refcount path plus the Metal/CUDA lanes.
Before I write this up as a PR: is shared-closure support the intended direction here, or should @unsafe's +-binder lift be narrowed instead? If it is the runtime, does the ~180 ttok cost need a cap decision from Taelin first, and are there existing RFC/closure invariants (device sealing, rfc_bump, the task path) I should follow rather than extend? I can prepare the PR once the direction is agreed.
Activity
YidaWeng commentedon Nov 22, 2026
I traced this to the C runtime's refcount path, not the checker. On current
main(0e2f38f7):twicetakes+f, so the emitter wraps the parameter interm_keep(e, _f_0, 1)atFID_twiceentry (bend2/comp.ts:1645).term_keepcallsrfc_wrap(bend2/comp.ts:3848), which rejects a closure outright:So the fail-stop happens before either application. Even if
rfc_wrapallowedTAG_CLO,Clo~apply(bend2/comp.ts:4386) doesspare_freeon the closure segment and moves the captures, so a second application would read a freed capture. That is why it needs a runtime change rather than a one-line predicate.On the checker side,
@unsafelifting theDatawall on+binders looks intentional:tests/check/unsafe_many.benddocuments exactly that ("@unsafe also lifts the Data wall on + binders ... a function may sit under a + binder"), and it passes today. So I did not pursue making@unsafereject this.I prototyped the runtime direction in a scratch tree:
TAG_CLOinrfc_wrap;rfc_wrap(..., 1)); without this the first apply frees a capture the closure segment still points at, which is a real use-after-free;Clo~apply, useterm_peekto follow the RFC redirect, keep each capture for an RFC closure, thenterm_dropthe closure handle instead ofspare_free-ing the segment;WL_KEEP(N)ladder next toWL_LOADfor the capture registers.With that, the issue's file prints
5on the C lane, and so do variants with a capturedDuo, a closure built by another def, three applications, and higher-order closures. The existingtests/compile/closure_{record_shared,dynamic_apply,partial_app,cps,slab_drop}.bendstill pass, and the sealed build is clean under-DBEND_POISON. An earlier unsealed prototype also survived 30 runs at 16 threads with a capturedDuo, but it still had the use-after-free, so I would want a proper concurrent stress of the sealed version before trusting it.The direction works, but it is not free: the prototype adds about 180 ttok to
comp.ts, landing roughly 175 over the permanent 66,000 cap, and it needs review of the capture refcount path plus the Metal/CUDA lanes.Before I write this up as a PR: is shared-closure support the intended direction here, or should
@unsafe's+-binder lift be narrowed instead? If it is the runtime, does the ~180 ttok cost need a cap decision from Taelin first, and are there existing RFC/closure invariants (device sealing,rfc_bump, the task path) I should follow rather than extend? I can prepare the PR once the direction is agreed.