Skip to content

#1991: read-after-drop MIR validator; raw-pointer block tails read before the drop - #2050

Merged
ehartford merged 2 commits into
mainfrom
fix-1991
Oct 3, 2026
Merged

ehartford merged 2 commits into
mainfrom
fix-1991

Conversation

@ehartford

Copy link
Copy Markdown
Contributor

Closes #1991.

Root cause (#1991). The stage1-only panic was the pre-#1968 lowering in the old pinned seed (nightly-20260929, v0.15.3.0-g8f84428e5, digest 8c5a151d…). 9a73f74 changed math_fn_arity to let t = math_fn_table(); t[id].arity and fixed the lowering in the same commit, but the seed that built stage1 still had the bug. Under that seed the MIR is drop(_2); _0 = copy _2[_4].f. In the stage1 it built (impl-1930's stage1, 04:46), math_fn_arity's machine code frees the Vec and then calls with_panic_ref on every path: +164 bl with_vec_free_buffer_drop_origin, then +196 bl with_panic_ref / brk. There is no path that returns. Stage2 and the release are built by stage1's fixed lowering, so they are correct. With the current seed (nightly-20261003, c884c1c, which has 9a73f74), stage1's math_fn_arity checks the bounds, reads, drops and returns. All four fixtures named in the issue pass under it. No source change was needed for #1991 itself.

Why no tool caught it. The old seed's check --validate-all reported ok on exactly that MIR. The drop-state lattice marks a dropped place Uninit, the same as StorageDead. Its only read rule judges whole-local reads of Maybe places and never looks at a projection. Separately, no lane runs a stage1, the seed-built compiler, over a corpus. Stage1 only has to compile stage2, and fixpoint compares stage2 with stage3.

This PR

  1. validate_read_after_drop (MirCore, part of validate-ownership/validate-all/audit). It runs a forward must-analysis over locals that every path has whole-dropped and that nothing rewrote. A value read of such a local, whole or through a projection, is refused. test/internals/mir_read_after_drop_test.w builds the MIR directly and fails when the validator is removed. Sweeping stage1 check --validate-all over test/behavior + test/phase + test/spec (2049 files) found one hit, and it is a real bug (2).
  2. MirLower materialize_tail_read_of_dropped_local left a TY_PTR tail lazy, the same as a &T view. A raw pointer is a value the place stores, not a view of the place, so let t = rows(); t[0].p read the freed Vec. This panics with the release compiler. In behav_c_facade_resource_drop_once, scope()/early() lowered to drop(_1); _0 = copy _1.repr. New fixture: test/behavior/behav_1991_tail_pointer_field_read_before_drop.w (panics on the installed release, passes on this tree's stage1).

Evidence: src/main build :gate green; :deep-debug-tool-tests --no-deps green; :drop-audit 254 cells, 0 regressions; the 215 behavior fixtures that use raw pointers pass under stage1.

Eric Hartford added 2 commits October 3, 2026 10:22
… a use after drop (#1991)

#1991's stage1-only "index out of bounds" in math_fn_arity was the pre-#1968
lowering in the old pinned seed (nightly-20260929, 8f84428): `let t =
math_fn_table(); t[id].arity` lowered to `drop(_2); _0 = copy _2[_4].f`.
The seed-built stage1's machine code frees the Vec and then panics on every
path (the optimizer folded the cleared length into the bounds check), and
the seed's own `check --validate-all` said ok: the drop-state lattice marks
a dropped place Uninit, like StorageDead, and judges only whole-local reads
of Maybe places, never a projection.

validate_read_after_drop: a forward must-analysis of the locals every path
has whole-dropped (Drop, drop-and-goto) and nothing rewrote (whole Assign,
StorageLive, call destination). A value read of such a local, whole or
through a projection, in a statement or a terminator operand is refused.
Swept with stage1 `check --validate-all` over test/behavior, test/phase and
test/spec (2049 files): one hit, a real one (behav_c_facade_resource_drop_once:
`drop(_1); _0 = copy _1.repr`), fixed in the next commit.

test/internals/mir_read_after_drop_test.w builds the MIR directly (fails
with the call removed): read after drop, after a drop-and-goto, as a call
argument; clean for read-before-drop, drop-rewrite-read, and a drop on one
arm only.
…ck drops reads before the drop (#1991)

The read-after-drop validator named `fn scope() -> *mut db: let d =
Database.new(1).unwrap(); d.repr` in behav_c_facade_resource_drop_once:
`drop(_1); _0 = copy _1.repr`. materialize_tail_read_of_dropped_local (#1968)
left a TY_PTR tail lazy beside a TY_REF one, but a raw pointer is a value the
place stores, not a view of the place: `let t = rows(); t[0].p` read the
freed Vec and panicked "index out of bounds" with the release compiler.
Only a view-typed tail stays lazy now.

test/behavior/behav_1991_tail_pointer_field_read_before_drop.w panics on the
installed release compiler and passes under the stage1 built from this tree;
the facade fixture now passes validate-all.
@ehartford
ehartford marked this pull request as ready for review October 3, 2026 17:46
@ehartford
ehartford merged commit a7a30da into main Oct 3, 2026
@ehartford
ehartford deleted the fix-1991 branch October 3, 2026 17:46
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

1 participant