Skip to content

validate-ownership accepts a drop of a Moved place (moved out, not yet reset) — a double free at runtime #1539

Closed

Description

@ehartford

--validate-ownership accepts a drop of a place whose drop state is Moved (moved out, not yet reset). Drop flags are retired and codegen emits every drop statement, so that drop frees the value its new owner holds — a double free. --dump-drop-plan even labels it skip, and mir_elaborate_dead_drops (which would NOP such drops) has no caller.

Found on #1412's shape (stage1 of origin/main ba2d5d1):

gen fn words(count: i32) -> str:
    var i = 0
    while i < count:
        let w = f"w{i}"
        yield w
        i += 1
fn main:
    for s in words(3):
        print(s)

run --debug-alloc: prints w0, then DOUBLE FREE … first_drop=drop#struct __drop_struct_16. The generator's next body contains

_5 = move _10;
drop(_10) @ drop#6 scope-exit _7;
_10 = const zst(ty16);

— _10 is Moved at the drop (the reset comes after it), and --validate-ownership prints ok. (The wrong place comes from lower_generator_next_body remapping a Drop's place id through the local map; fixed in the #1412 PR. The validator should have named it.)

Expected: the ownership validator rejects a drop whose place is Moved on every path reaching it: "drop of _10 after it was moved out and before its reset; this frees the value its new owner holds". Maybe (moved on some paths, reset there) stays legal.

Needs a corpus sweep with the check before landing (companion of #1487, the reset-of-Init check).

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