Skip to content

@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

Open

Description

@nicolas-abril

What you did

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.

The file

import Base

@unsafe def twice(+f: Nat -> Nat) -> Nat:
  Nat.add(f(1n), f(2n))

def main() -> IO(Unit):
  do IO<Unit>:
    args : List<String> <- IO.args()
    +k : Nat = List.length(&1, String, args)
    IO.print(Nat.show(twice(x => Nat.add(x, k))))

bend version

bend 2.0.40 (main 8c329c1b)

uname -sm

Darwin arm64

clang --version (the first line)

Apple clang version 17.0.0 (clang-1700.6.4.2)

Found while testing #2180.

Activity

  1. added
    bug
    on Nov 16, 2026
  2. YidaWeng commented on Nov 22, 2026

    @YidaWeng
    Contributor

    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);
        return t;
      }

      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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    bug

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions