Skip to content

IO foreign defs are trusted invisibly: a forged Rfl silently transports Unit{} into F32 arithmetic — the verdict should count foreign defs like it counts ~ instances #874

Closed

Description

@Cypoe

What you did

Two files: ffi_forge_rwt.bend + its JS twin ffi_forge_rwt.js (below; also at Cypoe/bend-probe). bun bend2/main.ts ffi_forge_rwt.bend

What happened

> nan
> exit 0

The checker demands the {Unit == F32 : Type} evidence live (Rwt checks it) but erases it in codegen ([t.f]), so the forged Rfl is never inspected — Unit{} flows into F32.add unchecked. All terms check. is printed with no indication the book's consistency rests on a foreign promise.

Sibling repro (ffi-empty-trap): a foreign def forge() -> IO(Empty) checks clean (ffi_forge_verdict.bend → All terms check.) and fail-stops the runtime when run — IO(Empty) is a payload no honest foreign impl can ever deliver, since Empty has zero constructors.

The ask: #841 already counts ~ instances as unsafe annotations (t.u === true || k.includes("~")). Foreign defs (tld.i set) are the bigger trust boundary and invisible to the same verdict — All terms check, with N foreign defs is the same disclosure applied to FFI. Optionally, IO(E) for ctorless E is statically refusable. (Neither demo is a kernel inconsistency — every lie arrives IO-headed.)

bend.ts's header claims "no closed live inhabitant of Empty" and carves out
only @unsafe. Foreign books aren't carved out, so the stated invariant is
falsified as written. IO(Empty) is also the one foreign payload no honest
implementation can ever deliver to a continuation — provably a lie, not merely
unverified.

JS emitter path only; C build fails earlier at emit-time layout check.
comp.ts:1737 (val_word) — the C emitter tracks value layouts and catches Unit{} (0 words) being read as F32 (1 word) at compile time. Plus the C backend requires a .c twin per foreign def (comp.ts:2733), which this repro doesn't ship — it would die on that too.

The file

function forge_eql(kont) {
  return { $: "Rfl" };
}


import Base
 
def forge_eql() -> IO({Unit == F32 : Type}):
  import "./ffi_forge_rwt.js"
 
def main() -> IO(Unit):
  do IO<Unit>:
    p : {Unit == F32 : Type} <- forge_eql()
    x : F32 = %r@p : _; Unit{}
    IO.print(F32.show(F32.add(x, 2.5)))

bend --version

bend 2.0.16

uname -sm

Linux x86_64

clang --version (the first line)

clang version 18.1.8 (Target: x86_64-pc-windows-msvc)

Activity

  1. added
    bug
    on Sep 19, 2026
  2. Cypoe commented on Sep 19, 2026

    @Cypoe
    Author

    Also, just reposting from Discord as it was asked, the harness that discovered this as well as #852 indipendently is here: https://github.com/Cypoe/bend-probe/tree/master/harness

    This was the only Issue that was left to make (kind of) was this one #874 - regarding FFI, just basically a "neat to look at maybe" (I filed it as "bug" tho. hope that doesn't overstep)

  3. VictorTaelin commented on Sep 19, 2026

    @VictorTaelin
    Contributor

    Agreed: a foreign def should not be trusted silently. Shipped in 2.0.18.

    The checker reads a foreign def's type, never its code, which is the same promise @unsafe makes. The verdict now seeds its walk with both, so a foreign def and everything that relies on it are named. Your file, unchanged:

    $ bend ffi_forge_rwt.bend
    All terms check, but 2 defs rely on unsafe or foreign code:
    - forge_eql
    - main
    nan
    

    Base's own effects keep quiet: the mark is on the file's own claims, not on IO.print.

    Refusing IO(E) for a constructor-less E is a separate question and is not in this release.

    Note: this reply was written by an AI after it reported the issue to me and I made the decision. If anything here is wrong, reply and I will review it myself.

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