Skip to content

MIR: statement temps materialized inside a zero-iteration loop body drop at the join — uninitialized stack slot freed as a str (#729 residue; release-lane c_import SEGV root cause) #771

Closed

Description

@ehartford

The defect

In Zcu.c_import_cache_key_frontend, codegen emits a str-temp drop (glue 'drop#struct __drop_struct_16') at the exit/join of the owns/borrows concat loops. The temp is materialized INSIDE the loop body; on a ZERO-ITERATION run the drop still executes, freeing whatever the never-initialized stack slot (sp+0x210 in the release binary) holds. At -O1 that slot contains the dead frame of the immediately-preceding record_frontend_tracked_input call — insert_unique's sret Vec header — so the 'str free' lands on the TRACKED-INPUT VEC'S LIVE BUFFER.

This is behav_branch_temp_drop_not_taken_path's class (#729) in its loop/zero-iteration shape, alive in the compiler itself.

Proof chain (single_tiny.w, release binary, lldb ASLR-stable addresses)

  1. Crash: str_byte_at_ref dereferencing quoted-path text as a header; caller tracked_input_str_compare (x30; bt unwind broken).
  2. Crash memory: vec element slots hold '"/Users/…' path TEXT spanning two slots; later slots hold intact stale headers — buffer freed + partially recycled by a string alloc.
  3. Time bisection: address zeroed at expand_c_imports_frontend entry; crash before Sema.
  4. Hardware watchpoint on the block header (payload-16):
  5. WITH_ALLOC_NO_REUSE=1: compile prints 'ok' then 'invalid free … origin=drop#struct __drop_struct_470' from Compilation's exit glue — the SECOND free of the same block, proving the vec still owned it when free Type resolution corruption when single source file exceeds ~14K lines #1 fired.
  6. Disassembly at the free site: add x0, sp, #0x210 → bl with_str_free_drop_origin, origin constant 'drop#struct __drop_struct_16' (str's own glue); the site sits between the owns-loop back-edge (b.vc +1228) and the borrows loop — the loop-exit drop of a body-materialized temp.

Why each lane behaves as it does

  • Release binary (flip codegen): drops are real → frees live heap through stack garbage → UAF/SEGV on every c_import (tracked-input recording is c_import's hot path).
  • Seed-compiled stage1: pre-flip Copy world emits NO temp drops — the path is structurally absent → lane green.
  • Why the drop-audit matrix missed it: cells cover branch taken/untaken and loops that RUN; the zero-iteration × -O1-slot-overlay cell isn't in the matrix, and the misfree usually hits the rt_payload_start_can_be_owned guard silently.

Fix direction

MirLower: a stmt temp materialized inside a conditional region (loop body, arm) must drop INSIDE that region or carry a drop-flag guard — extend the #729 fix's scope to loop bodies (register_stmt_temp/schedule placement). Ownership-class change → its own :move-audit/:drop-audit-bracketed batch, plus a drop-audit cell for the zero-iteration loop shape.

Blocks the reseed (this defect is why the release lane is red — #761's remaining class). Prior suspects (unspelled field captures, fixed in ee33341 and the Sema/CodegenTraits siblings) were real hygiene bugs but NOT this root; Compilation's var-zcu pattern is exonerated.

Activity

  1. ehartford commented on Aug 13, 2026

    @ehartford
    ContributorAuthor

    Fixed in c1f4985 (+ dd66426 for the formatter-temp residue it unmasked). All 12 loop-body lowerings now frame their statement temps (drop inside the body, per iteration); the four fmt helpers register their owned results. Bracket battery: fixpoint ✓ move-audit ✓ drop-audit ✓ debug-alloc 65 PASS (da_loop_body_stmt_temp_zero_and_n pins both shapes: zero-iteration misdrop + per-iteration leak — pre-fix leak count=7, post-fix 0). Release lane: behav_c_import_offsetof and the single_tiny/single_stdint repros healed. Remaining release-lane crashes are SEPARATE instances (first: UAF read at cimport_deps_sorted_unique_paths+176, fault on a freed-header data pointer; also issue114's interior-NUL panic) — tracked on #761 with the established watchpoint protocol.

  2. added a commit that references this issue on Aug 13, 2026
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