Skip to content

Ownership audit accepts shallow owning HashMap keys/values/items snapshots #1159

Closed

Description

@ehartford

with analyze ... audit:all reports zero violations for a HashMap extraction
that creates two owners of the same string payload. The native allocator's
scribble mode makes the stale read deterministic.

fn inspect_keys(map: &HashMap[str, str]):
    let keys = map.keys()
    assert(keys.len() == 1)

fn main:
    var map: HashMap[str, str] = HashMap.new()
    map.insert("owned key".to_owned(), "owned value".to_owned())
    inspect_keys(map)
    assert(map.get("owned key").unwrap() == "owned value")

At 69bf4e70, an ordinary run succeeds; WITH_DEBUG_ALLOC_SCRIBBLE=1 with run --debug-alloc repro.w fails. The equivalent values() case reads a freed
value; items() also frees map-owned strings. audit:all passes all three.

Instruction-level evidence from #1158: sema_clone_str_str_hashmap calls
with_hashmap_keys_out, then its generated Vec drop frees the source map key
at helper +404 (0x1004ece58 in the diagnostic Darwin binary). That address is
reused for a trait HashMap allocation and later freed through the stale source
map key. Native address-trap backtraces prove the full lifetime. The extraction
intrinsics raw-copy non-Copy elements into an owning Vec without a clone or
view contract. The audit currently does not validate this intrinsic ownership
boundary.

Expected: ownership auditing must reject an intrinsic whose declared result
owns payloads that its implementation only borrows/shallow-copies, or record
and validate a correct independent-ownership/view contract. Spec §§3.8, 21.1
and the owning-map contract require valid ownership and origin preservation.

No workaround applied. The collection implementation fix is tracked with
#1158; this issue records the silent validator required by AGENTS.md.

Activity

  1. joshhickson commented on Sep 17, 2026

    @joshhickson
    Contributor

    Three more places with the same shape, in case they're useful for the audit's coverage: SlotMapSlot.get(), VecSlot.get(), and Option[str].unwrap_or. Each returns an owning copy of a non-Copy element that shares the source's heap payload, and audit:all passes all three.

    Checked with a source build of main at 69bf4e70 on Windows x86_64. Your repro above behaves as described there (plain run ok, scribble panics called unwrap on None, audit:all → violations=0), so the setup matches.

    SlotMapSlot.get()

    use std.collections.SlotMap
    type Courier { name: str, n: i32 }
    fn main:
        var m: SlotMap[Courier] = SlotMap[Courier].new()
        let src: &str = "bob"
        let h = m.insert(Courier { name: src.clone(), n: 0 })
        with m.slot(h) as mut s:
            let c = s.get()
            print(f"inside: {c.name}")
        let noise = f"{m.len()}-noise"
        print(f"after: name={m.get(h).unwrap().name} {noise}")
    

    VecSlot.get() → set() (the get / modify / set pattern)

    type Courier { name: str, n: i32 }
    fn main:
        var v: Vec[Courier] = Vec.new()
        let src: &str = "bob"
        v.push(Courier { name: src.clone(), n: 0 })
        with v.slot(0) as mut s:
            var c = s.get()
            c.n += 1
            s.set(c)
        let noise = f"{v.len()}-noise"
        print(f"after: name={v[0].name} n={v[0].n} {noise}")
    

    Option[str].unwrap_or

    fn lookup(names: &Vec[str], i: i64) -> Option[str]:
        if i < names.len():
            Some(names[i].clone())
        else:
            None
    
    fn main:
        var names: Vec[str] = Vec.new()
        let src: &str = "player"
        names.push(src.clone())
        for i in 0..2:
            let name = lookup(names, i)
            let label: str = name.unwrap_or("?")
            print(f"  {label} <- typed str")
    

    Results, identical pattern for all three:

    run --debug-alloc + WITH_DEBUG_ALLOC_SCRIBBLE=1 analyze … audit:all
    SlotMapSlot.get() exit 0, name= prints stale bytes, leak count=0 exit 0, name=���, leak count=0 violations=0 ok
    VecSlot.get()/set() exit 0, name= prints stale bytes, leak count=0 exit 0, name=���, leak count=0 violations=0 ok
    Option[str].unwrap_or exit 0, correct output, leak count=0 exit 0, ������ <- typed str, leak count=0 violations=0 ok

    Unlike the keys() case these don't panic under scribble; they just print freed memory and exit 0, so the only signal is wrong output. The replace-and-swap-back form (m.replace(h, …) instead of get()) stays correct under scribble, which may be a useful control.

  2. ehartford commented on Oct 18, 2026

    @ehartford
    ContributorAuthor

    Fixed by #2451 (main 809561f): slot/range/entry extraction refuses a non-Copy element in Sema, runs copy glue in codegen, and analyze audit:all fails on an extraction intrinsic whose element is not Copy (src/Analysis.w). Pins: test/compile_errors/err_slot_get_non_copy.w, test/behavior/behav_slot_get_copy_glue.w.

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