Skip to content

Enum/Result drop glue ignores discriminant — drops inactive Drop variant (memory corruption) #601

Closed

Description

@ehartford

Summary

Dropping an enum/Result value in one variant incorrectly drops a different, inactive variant's payload when that payload type has a Drop impl. The drop glue does not respect the discriminant, so it runs Drop on uninitialized/garbage payload bytes — corrupting the allocator. This affects every Result[T, E] (and any enum) where a Drop-bearing type is one of the variants.

Repro (clean, no FFI)

use std.box
fn mk_err() -> Result[Box[i32], i32]:
    Err(7)
fn main:
    match mk_err():            // Err — the Ok(Box) variant is NOT active
        Ok(b) => print("unexpected")
        Err(e) => print(f"err={e}")
    let b2 = Box.new(99)       // a real, valid heap allocation
    print(f"b2={b2.into_inner()}")
    print("done")

Output:

err=7
b2=99
done
panic: invalid free: pointer is not an allocated payload start

The Err(7) result carries no Box, yet dropping it frees a garbage pointer (the inactive Ok variant's Box.ptr), corrupting the freelist; the next genuine free then panics.

Where to look

  • Codegen.mir_emit_drop_ptr / mir_emit_drop_fields_ptr (src/CodegenDispatch.w:3568-3619) drop a value's fields, but there is no discriminant-aware enum payload drop anywhere in src/CodegenDispatch.w / src/Codegen.w / src/MirLower.w (grep for discriminant/variant.*drop finds nothing). An enum's drop must read the tag and drop only the active variant's payload; today it appears to drop a fixed/declared payload type unconditionally.

Impact

Critical and general. Any Result[OwnedThing, E] — to_cstring() -> Result[CString, E] (#427), Result[Box[T], E], Result[Vec[T], E], etc. — corrupts memory whenever an Err (or any non-largest-Drop variant) value is dropped. Blocks #427's to_cstring API.

Fix direction

Generate enum drop glue that switches on the discriminant and drops only the active variant's payload (the same shape match-lowering already uses to read variant payloads). A non-Drop variant (e.g. the i32 in Err(i32)) drops to nothing.

Activity

  1. ehartford commented on Jun 17, 2026

    @ehartford
    ContributorAuthor

    Fixed in 903c9b6.

    Root cause was in MirLower's match lowering, not the drop glue: a match-arm pattern binding's drop was scheduled in the enclosing scope, so it ran on every path leaving the match. When a different arm was taken, the binding was uninitialized — for a Drop-typed payload the Err path ran the Ok payload's drop on garbage → panic: invalid free.

    lower_match now wraps each arm in a drop scope (push_scope before the pattern, pop_scope_with_goto at the arm exit), so a binding is dropped only on the path that bound it; the guard-fail edge drops the just-bound variables before falling through.

    Verified: the Result[Box[i32], i32] repro and all match variants run clean; behavior-tests 689 (+regression behav_match_arm_binding_drop_scope), native-spec-tests 170, native-compile-error-tests 598, fixpoint holds.

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