Skip to content

D111: a str materialized from an interior &str view shares bytes it does not own (silent use-after-free) #2307

Closed

Description

@ehartford

Regression since f7ba5ba (D110/D111), found by the D118 step (c) battery: the first battery whose build layer is compiled by a seed that has D111.

Repro (heap text; the old seed nightly-20261005 prints [alpha]):

fn main:
    let text = "  asset: " ++ "alpha\n  other\n  sha: x"
    let lines = text.split("\n")
    var pending = ""
    for i in 0..lines.len():
        let line = lines[i]
        let at = line.index_of("asset:")
        if at >= 0: pending = line.slice(at + 6, line.len()).trim()
    print(f"[{pending}]")

WITH_DEBUG_ALLOC_SCRIBBLE=1 with run --debug-alloc prints freed bytes.

Exact line: MIR lowers the store as _5 = copy+retain _24.* where _24 is the &str from trim() over the temporary str .slice() returned (_21), then drop(_21). The hold comes from MirLower.retain_consumed_copy (MIR_HOLD_RETAIN on a consumed copy of a Copy type with drop glue). rt_core.with_str_retain is a no-op for a pointer that is not the start of an owned payload ("views (interior pointers)"), so the new str shares a buffer it does not hold and dangles when _21 is freed. explain:origin reports no view origin for pending, so Sema believes it owns a value.

Silent tools: the checker accepted it, explain:origin saw nothing, the debug allocator needs SCRIBBLE to show it.

Fix: materializing a str from a view whose pointer is not the start of an owned payload copies the bytes (a prefix view of an owned buffer may retain; a literal's view may share). Ships with the D118 step (d) seed; build/seed.w's two .trim().to_owned() workarounds come out then.

Impact seen: seed-driver flagged every workflow pin as drifted (pending_asset read freed memory).

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