Skip to content

A non-Copy value cast into or out of a distinct wrapper is never transferred: s as Name leaves the buffer with s, a Name drop is a no-op, and n as str manufactures a second owner (double free) #1802

Closed

Description

@ehartford

Spec

§4.5 (distinct types are zero-cost wrappers), §2.3 (transport is not duplication: the compiler never produces a second live value from one unless the type is Copy), §2.5.1 (leaking takes a deliberate act).

Repro (fix-lang branch, after #1782's read-through fix; the same on main for the owned shapes)

type Name = distinct str
fn show(n: &Name): print(n as str)
fn main:
    let s = "abc".clone()
    let n = s as Name
    print(n as str)

with run --debug-alloc: prints abc, then DOUBLE FREE addr=... size=16 first_drop=drop#struct __drop_struct_16 second_drop=drop#struct __drop_struct_16.

Matrix (each cell one program, --debug-alloc):

shape verdict
let s = "abc".clone(); let n = s as Name; print("x") leak 0, no double free: s is dropped, the Name drop is a no-op
let n = "abc".clone() as Name; print("x") clean by luck: the clone's statement temp is dropped at the statement's end, n dangles
let s = ...clone(); let n = s as Name; print(n as str) DOUBLE FREE
let n = "abc".clone() as Name; print(n as str) DOUBLE FREE (reads freed memory first)
let n = ...clone() as Name; show(n) (through &Name) DOUBLE FREE
let n = ...clone() as Name; let t = n as str; print(t) DOUBLE FREE

--dump-mir: _5 = cast(copy _4 as Name); _1 = copy _5; drop(_4) — the cast READS its source (#1719) and the wrapper is a bitwise alias; _6 = cast(copy _1 as str); _7 = copy _6; ... drop(_7) — the cast back is materialized for the &str argument and that materialization is dropped as an owner.

Cause

Three facts, no owner:

  1. Sema records no move for a cast, so s as Name on an owned non-Copy place is a read that leaves the buffer with s; codegen gives a distinct type no drop glue (its LLVM type is the inner type but its drop is a no-op), so the wrapper never frees and dangles once its source dies.
  2. n as str (owned n, or r as str through &Name) reads a non-Copy value into an owned str temp: a second owner (§2.3), dropped by whoever materializes it.

Question for the ruling (spec is silent past "zero-cost wrappers")

What does x as T mean for a non-Copy distinct relabel?

  • Recommended: an owned place cast into or out of its distinct wrapper is a MOVE (Sema marks the source consumed; MIR consumes with reset), the wrapper carries its underlying type's drop glue; a cast THROUGH a reference (r as str, r: &Name) yields a &str view with r's origins (D22: a &T stays a reference until an owned-value demand; §2.3: never a second live value). print(n as str) then works on an owned n without consuming it only as print(&n as &str) / print(n as &str) — worth a ruling on the spelling.
  • Alternative: the reference case is an error naming .clone().

Found while fixing #1782 (the read-through cast); its test uses literals so the base bug does not fire.

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