Skip to content

soundness: borrow checker accepts use-after-move, aliased &mut and a dangling ref (aggregate copy-out, nested reassign, closure capture, deref-reborrow) — T0, absent from SOUNDNESS.adoc #847

Backlog

Description

@hyperpolymath

The production borrow checker (lib/borrow.ml) accepts programs that use a value after it was moved, hold two live &mut to one place, write under a live shared borrow, and return a reference to a dead local. These are false negatives of the same class as #554: they accept wrong programs, which is the class #563 puts in its top tier (T0).

This also matters for the v1 decision. #563 words the fallback cut as "lexical checker + #554 fixed + disclosed over-rejection", which assumes the lexical checker is only incomplete. It is unsound.

Reproduce

Re-verified on main-based build 161eaaf: each program below prints Type checking passed, and each control is rejected with a borrow error.

module P;
fn pick(ref x: Int) -> ref Int { return &x; }
fn consume(own v: Int) -> Int { return v; }
fn main() -> Int { /* body from the table */ }
Id Class main body Violation
U-A2 borrow copied out of its binder into a tuple let a: Int = 7; let r = &a; let t = (r, 0); let _g = consume(a); let v = *(t.0); return v; use after move
U-B2 reference binder reassigned inside if let a: Int = 7; let b: Int = 8; let c = true; let mut r = pick(a); if c { r = pick(b); } let _g = consume(b); let v = *r; return v; use after move
U-C1 closure captures a reference binder let a: Int = 7; let r = &a; let f = fn() -> Int { *r }; let _g = consume(a); return f(); use after move
U-E3 deref-reborrow records no loan let x = 5; let r = &x; let r2 = &*r; let gone = consume(x); *r2 + gone use after move
K-2 (control) let a: Int = 7; let r = &a; let _g = consume(a); return *r; rejected, correctly
K-8 (control) let a: Int = 7; let _g = consume(a); return a; rejected, correctly

The full set has 15 unsound programs (all accepted) and 8 controls (all rejected) in five classes:

  • A: a borrow copied into a tuple or array: use after move, a write under a shared borrow, two live &mut, a dangling reference.
  • B: a reference binder reassigned inside a loop, if or match.
  • C: a closure capturing a reference binder.
  • D: the return-borrow summary misses an aggregate-wrapped borrow.
  • E: &mut *r / &*r record no loan.

The full tables and a probe kit are in issues-drafts/10-borrow-checker-false-negatives-round4.adoc and in ADR-0028 appendix A.4 (PR #845).

What is and is not new

  • Classes A and B are described in the "Still deferred" trailer of lib/borrow.ml as accepted, low-severity residuals for tracking: ADR-022 Polonius origin/region variables — implementation M1–M4 (lib/borrow_polonius/) #553.
  • Classes D and E are holes 1 and 4 of the unfiled issues-drafts/09-round3-borrow-affine-soundness-probe.adoc, and still reproduce. That draft's holes 2 and 3 are in lib/quantity.ml and need their own issue.
  • Class C and probes U-A4 to U-A6 were not recorded anywhere.
  • docs/SOUNDNESS.adoc says "There are no soundness-relevant holes outside this table". None of these has a row or a pinned xfail.

Severity

Memory corruption was not demonstrated. On core wasm, &e copies the operand into a fresh heap cell, so a stale reference reads a stale copy (that copying is itself a separate bug, filed alongside this one). The interpreter rejects most reference programs at run time. The static guarantee "no use after move, no aliased &mut" is false today.

The test-only Polonius extractor (#553 M3) rejects only U-B1–U-B3 and accepts three of the controls, so wiring it in would not close this.

Handling

Options are in ADR-0028 (PR #845). In short: close these in an origin-based checker (#553), or restrict references for v1 so these constructs cannot be written. The ADR measured the restriction's impact on the repo corpus at zero files outside test//tests/.

Refs #553, #563, #554, #515.


Generated by Claude Code

Activity

  1. added
    documentation
    on Dec 1, 2026
  2. whatproblem-ai commented on Dec 1, 2026

    @whatproblem-ai

    Each unsound program comes with a rejected control (K-2, K-8) and a pinned build (161eaaf), so every hole can be confirmed as a checker gap rather than a setup difference.

    Restated: lib/borrow.ml stops tracking a loan once the borrow leaves its original binder. The borrow can be copied into a tuple or array, reassigned inside if/loop/match, captured by a closure, or reborrowed via &*r/&mut *r. In each case the checker accepts use-after-move, two live &mut, writes under a shared borrow, and dangling returns. That makes it unsound, not merely incomplete. This breaks the #563 v1 fallback premise, and docs/SOUNDNESS.adoc currently claims no such holes exist.

    Nothing is missing for a fixer. The open question is the policy choice in ADR-0028: an origin-based checker (#553) or restricting references for v1.

    flowchart TD
        A[📱 let r = borrow of a] --> B{How the borrow escapes}
        B --> C[A: copied into tuple or array]
        B --> D[B: binder reassigned in branch]
        B --> E[C: captured by closure]
        B --> F[E: reborrow via deref]
        C --> G[⚠️ Loan no longer tracked]
        D --> G
        E --> G
        F --> G
        G --> H[consume a accepted]
        H --> I[🔴 Stale deref accepted]
    
    Loading

    🏆 Clarity Score: 96% - Great detail! 💫

    ⌨️ Commands · 💬 Have feedback? Tell us · 🎯 New here? Add WhatProblem to your repo · 🔒 Privacy

  3. added
    architecture
    bindings
    bug
    priority:p1
    scope:repo
    on Dec 1, 2026
  4. added 2 commits that reference this issue on Dec 1, 2026
  5. hyperpolymath commented on Dec 1, 2026

    @hyperpolymath
    OwnerAuthor

    Addendum: a 16th unsound program, U-G1 (copying a &mut binder)

    Found by the #553 work (ADR-0028 §2.4, PR #845, commit 829091d) while testing whether option D's rule R3 closes class E. Re-verified on a main-based build that includes #841 (8659e16):

    module P;
    fn main() -> Int { let mut x = 5; let r = &mut x; let r2 = r; *r2 = 99; *r = 100; *r2 }
    
    Program check
    U-G1 (above) Type checking passed — accepted; unsound
    K-4 control: let mut x = 5; let a = &mut x; let b = &mut x; *a + *b Borrow error — rejected, correctly

    Copying a &mut binder leaves two usable exclusive references to x. This uses no aggregate, closure, nested reassign or deref-reborrow, so it is outside classes A–E. The count of known unsound accepted programs is now 16.

    Consequences recorded in ADR-0028:

    • Option D's claim that "every known hole lies outside the second-class fragment" was false. U-G1 is inside it.
    • The ADR's own fallback criterion is therefore met, and the recommendation is now E (withdraw &/&mut expressions for v1, keep parameter modes). D, plus a new rule R4 ("exclusive references are not copyable"), remains an upgrade path only if its experiment and a further probing round pass.
    • U-G1 is a liveness failure, not a provenance one. A provenance-inclusion statement alone (ADR option G) would not catch it.

    Generated by Claude Code

  6. whatproblem-ai commented on Dec 1, 2026

    @whatproblem-ai

    U-G1 adds a sixth escape route: a plain let r2 = r copy of a &mut binder leaves two live exclusive references, with no aggregate, branch, closure or reborrow involved. This is a liveness failure rather than a provenance one, so ADR option G alone would not close it. It also settles the policy question I left open, now that ADR-0028 recommends E, with D plus R4 kept only as an upgrade path.


    ✅ Nothing further needed. Clarity: 92%

    ⌨️ Commands · 💬 Have feedback? Tell us · 🎯 New here? Add WhatProblem to your repo · 🔒 Privacy

  7. hyperpolymath commented on Dec 1, 2026

    @hyperpolymath
    OwnerAuthor

    Three more unsound programs, D-01, D-06 and D-19, are filed with repros as #857. That brings the known total to 19: the fifteen here, U-G1 (comment above) and those three. All three use a value after a move, and all three lie inside ADR-028 option D's fragment, which is why they are tracked separately. Re-verified on main 464f7c6.


    Generated by Claude Code

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

    architecturebindingsbugdocumentationpriority:p1scope:repo

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions