Skip to content

Ownership audit accepts deferred reads after a temporary iterable is dropped #1175

Closed

Description

@ehartford

While fixing #1172, an intermediate (uncommitted) lowering change correctly
freed the temporary iterable on return but emitted that drop before defers
and local destructors. Runtime regression is being fixed in that same batch;
this issue tracks the analyzer that accepted the invalid lifetime.

fn observe(part: &str): assert(part == "bad")
fn stop(text: &str):
    for part in text.split(","):
        defer: observe(part)
        return
fn main: stop("bad,old")

Expected: ownership analysis rejects the read through part after its
iterable owner has been destroyed. The declared borrow and defer lifetime
must hold (spec §§2.4 and 13.2).

Actual: the retained candidate panics, also with WITH_ALLOC_NO_REUSE=1,
while analyze ... audit:all reports facts=11547 violations=0 ok on the
explicit-main probe. The retained probe adds two small allocations in
observe; these are not needed for the no-reuse failure.

Native proof: in the retained return-temporary-defer, LLDB at 0x100000694
observes element address 0x100068030 holding payload 4295393296, length 3,
immediately before with_str_free_drop_origin. At the deferred read,
0x100000744, both element words are zero. The allocator address trap
WITH_DEBUG_ALLOC_TRAP_FREE=4295393296 with hit 1 stops on this free.
Compiler LLDB for stop (sym 330) shows MirBuilder.lower_return+268
calling temporary cleanup before +276 calls emit_defers_for_return.

Evidence under local out/with-get-investigation/:
return-temps-defer-{free-read,trap,lowering}-lldb.log,
return-temps-defer-explicit-audit.log, and
return_temporary_defer_explicit.w. A local ephemeral destructor borrowing
the element reproduces the same ordering error. The repair merges temporary
drops with reverse local-drop order after defers; no audit exemption is used.

Activity

  1. ehartford commented on Oct 25, 2026

    @ehartford
    ContributorAuthor

    Triage 2026-10-25, with v0.15.3.0 (main a261b65): does not reproduce; the runtime is correct and #1791 (bbd2241) added view liveness across blocks to the validator.

    $ cat main.w
    fn observe(part: &str): assert(part == "bad")
    fn stop(text: &str):
        for part in text.split(","):
            defer: observe(part)
            return
    fn main: stop("bad,old")
    $ with run --debug-alloc main.w
    debug-alloc: leak count=0 allocations=3
    
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