Skip to content

Field move through share-place receiver skips reset-on-move: double free in safe code #697

Closed

Description

@ehartford

Repro (current seed, pre-#691, safe code)

type R { id: i32 }
impl Drop for R:
    move fn drop(): print(f"drop R {self.id}")

type H { items: Vec[R], n: i32 }

fn eat(v: Vec[R]) -> i64:
    let w = v
    w.len()

extend H:
    mut fn feed():
        var tmp = self.items
        let n = eat(move tmp)
        print(f"t4 ate {n}")
        // no restore of self.items

fn t4:
    var h = H { items: Vec.new(), n: 4 }
    h.items.push(R { id: 40 })
    h.feed()
    print("t4 done")

t4()

Actual

drop R 40
t4 ate 1
t4 done
drop R 10666320        <- garbage id: Drop body reading freed memory
debug-alloc: DOUBLE FREE addr=... size=32 first_drop=<untagged> second_drop=drop#struct __drop_struct_108

Compiles with no diagnostic. h.items's elements drop twice: once via eat's callee drop, again via h's scope-exit drop in the caller.

Expected (spec §2.5.1)

"The reset is unconditional (every move blanks its source)." Moving self.items out through the mut self receiver must blank the caller's field through the share-place pointer, so h's later drop sees the reset sentinel and no-ops. Either that, or the checker must reject the move — today it does neither.

Root cause area

#607's field-move machinery blanks moved fields of locally-owned structs (conditional moves blank on the moving path; unconditional moves rely on the owner's static partial drop). A share-place receiver (PassMode::IndirectPlace, D5/D6) is a pointer to the caller's place — the owner's drop runs in the caller, which cannot statically know about the callee's field move, so the static-partial-drop strategy is unsound there and a runtime blank through the pointer is required.

The effect system normally masks the call-argument form of this (consume-through-projection promotes the receiver to move fn via enforce_receiver_modes), but the let-binding form (var tmp = self.items) records only mark_field_moved with no effect promotion, so it reaches MIR unguarded. Same asymmetry, observed at src/SemaCheck.w:20887-20938 vs src/SemaCheck.w:8814-8860.

Found while grounding the #691 wide-flip design; the flip widens type_needs_drop, which makes this same pattern the compiler's own phase-threading idiom (Compilation.w:1503), so the fix is a prerequisite for #691.

Activity

  1. ehartford commented on Jul 21, 2026

    @ehartford
    ContributorAuthor

    Scope sharpened by three more probes (all current seed, safe code, pre-#691):

    t5 — user-Drop field, share-place base, unconditional move (no blank emitted):

    close F 5
    t5 done
    close F 5      <- user Drop body runs twice on the same fd (double-close class)
    

    t6b — conditional field move (blank IS emitted), user-Drop field, non-zero sibling field:

    close F 6
    t6 branch done
    close F 0      <- user Drop body runs against the reset sentinel (fd=0)
    

    t7b — conditional field move (blank IS emitted), Box field, non-zero sibling field:

    close F 7
    t7 branch done
    <exit 139>     <- SIGSEGV: pointee-drop dereferences the blanked (null) Box
    

    t6/t7 variants with a zero sibling field pass by accident: blanking the only other field makes the whole struct all-zero, so the whole-value guard at the drop site skips everything. With any non-zero sibling the guard passes and the unguarded field drops run.

    Root: the sentinel guard exists only at whole-value granularity (current_drop_needs_guard, Stage-4 elided per drop site), and ensure_structural_drop_fn bodies run fully unguarded ("the caller's guard already decided") — but a per-field blank (from #607's conditional flush, or from the missing share-place blank above) can leave a sentinel inside a live struct, which no whole-value guard can see. §2.5.1 requires the guard to be part of the destructor for exactly this reason.

    Fix shape (two halves, one invariant — reset-on-move holds end-to-end for fields):

    1. MirLower: blank unconditional field moves whenever the base local has no scheduled value drop in the current function (share-place params / borrowed receivers — the static moved-fields exclusion can never reach the caller's drop).
    2. Codegen: in structural field-drop recursion, guard sentinel-sensitive per-field drops — user Drop impls (rt_value_is_zero), Box/Rc/Arc pointee paths (pointer null-check), Drop-bearing enum fields (rt_value_is_zero). Container frees (Vec/str/HashMap) stay unguarded — inherently null-safe. Whole-value guard elision at drop sites is unchanged.
  2. ehartford commented on Jul 21, 2026

    @ehartford
    ContributorAuthor

    Fixed in two battery-gated commits, reseed complete (seed + ~/.local/bin/with at v0.15.1-gc04c14417):

    • c427e2e — Codegen: sentinel-guard member drops (struct fields, tuple/array elements, enum payloads, vec elements) via member_drop_depth; outlined per-type drop bodies reset it so their top-level stays caller-guarded while their member recursion re-raises it; runtime null guards in mir_emit_box_drop_place / mir_emit_refcount_drop_place under the same Stage-4 elision rule. Fixes the sentinel-run (t6b), the Box-field SIGSEGV (t7b), and the whole-Box/Rc conditional-move SIGSEGVs (t9).
    • c04c144 — MirLower: consume_moved_operand also blanks an UNCONDITIONAL field move whenever the base local has no scheduled value drop in the current function (share-place params, borrowed receivers, stmt temps) — reset-on-move now holds through share-place pointers. Fixes the t4 double-free / t5 user-drop double-run.

    Six new pins in test/debug_alloc/ (each verified failing on the pre-fix seed: exit 139 or invalid-free panic) plus the previously-vacuous da_conditional_field_move.w made real (its ()-bodied callee inferred no effects → share-place → the arg never moved; single-field holder masked the guard hole via the all-zero accident).

    Whole-value guard elision for never-moved locals is unchanged — the common case still drops straight-line. Gates: build, fixpoint, analyze audit:all (2,198,202 facts, 0 violations), :move-audit, :drop-audit, full :test — all green on both commits.

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