Skip to content

Soundness: enum Drop-payload double-free at plain scope exit (post-#606 over-propagation) #693

Closed

Description

@ehartford

Found by the new tools/drop_audit.w matrix (the repo-committed drop auditor) on its first run — cell scope_exit/enum. Reproduces on the shipped compiler.

Repro (double-frees today)

extern fn with_alloc(size: i64) -> *mut u8
extern fn with_free(ptr: *mut u8) -> Unit
type R { id: i32, ptr: *mut u8, slot: *mut i32 }
impl Drop for R:
    fn drop(move self: Self):
        unsafe:
            *self.slot = *self.slot + self.id
            with_free(self.ptr)
fn mk(id: i32, slot: *mut i32) -> R:
    unsafe { R { id: id, ptr: with_alloc(16), slot: slot } }
enum E:
    Carry(R)
    Empty
fn go(slot: *mut i32):
    let a = E.Carry(mk(1, slot))
    let _keep = 0
fn main:
    var drops: i32 = 0
    let slot = &raw mut drops
    go(slot)
    print_i32(drops)

with run --debug-alloc repro.w → DOUBLE FREE addr=… size=16 origin=with_alloc (the payload's allocation freed twice).

Scope and dating

  • Affected: EVERY control-flow shape with a live Drop-payload enum reaching scope exit — the auditor flags scope-exit, branch-taken, loop, move-out, reassign-over, move-then-reassign, early-return, normal-return (8 cells).
  • Not affected: let _ = E.Carry(mk(...)) discard and match-consume paths (the existing da_drop_enum_payload_discard.w lane fixture covers exactly the passing path — the plain-exit cell was covered nowhere until the auditor).
  • Dating: v0.15.0 prints drops=0 for the same program (payload Drop never ran — the pre-Soundness: Drop is not propagated through Option/Vec/array/tuple/enum contents → leak #606 leak this class had). Soundness: Drop is not propagated through Option/Vec/array/tuple/enum contents → leak #606's Drop-propagation fix moved the class from under-drop to over-drop: payload dropped once by the enum's payload-propagation glue and once by (presumably) the value drop of the enum place itself. Post-v0.15.0; predates today's sessions (no enum-drop-glue changes in them).
  • Struct-field / tuple / bare shapes are clean across the same 10 scenarios (auditor evidence), so the doubling is enum-payload-specific.

Next

Workflow per CLAUDE.md: --dump-drop-plan on the repro to see the two scheduled drops, then the MIR/codegen branch that emits the payload-propagation drop alongside the whole-value drop. The auditor cell pins the fix (with run tools/drop_audit.w <candidate> <baseline> — the 8 enum cells must flip DOUBLE-FREE → PASS with zero regressions elsewhere).

Activity

  1. ehartford commented on Jul 20, 2026

    @ehartford
    ContributorAuthor

    SQUASHED (4d42957, battery-green incl. the allocator lane, reseeded g4d4295743 + asset). The repro drops exactly once (7 / leak count=0), all 8 auditor enum cells flipped DOUBLE-FREE → PASS, and with build :drop-audit reads 60 cells, 0 non-PASS, 0 regressions — the gate's first fully green steady state.

    Two independent doubles were stacked (why single-layer fixes kept failing during bring-up):

    1. Root cause: the variant ctor never CONSUMED its moved payload operands (fields=[move _3]). The drop-state plan correctly marked the payload temp's scope-exit drop Moved→skip, but codegen's emission relies on the reset-on-move blank + ever-moved guard that only consume_moved_operand arms — so drop(_3) ran UNGUARDED on intact payload bytes. The ctor and assign_enum_variant_to_place now consume per field, the same discipline as every other move path.
    2. The struct-field glue also walked USER-DECLARED enums' tag+payload layout (user enums are registered in the struct tables; generic-inst enums like Option miss them — which is exactly why only user enums doubled and the auditor's option cells passed). Payload cleanup now belongs exclusively to the variant-switched enum drop.
    3. Hygiene: the ctor returned OK_COPY for non-Copy enums (two owners of one payload); now moves, Copy-gated.

    Debug trail (for the record): drop-plan dump showed the plan was right → MIR dump showed the copy-not-move → emitted-IR call-site counting found TWO @R.drop calls under different dbg locations → an instrumented Drop (printed id twice) plus the unguarded-IR read pinned layer 1. lldb was uncooperative; the IR + instrumentation route was faster.

    Pinned forever by test/debug_alloc/da_enum_payload_scope_exit.w (in every battery) and the auditor's enum row. Dating stands: introduced by the #606 payload-propagation era (v0.15.0 leaked instead of doubling — drops never ran at all).

  2. added 2 commits that reference this issue on Jul 20, 2026
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

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions