Skip to content

validate-ownership rejects vacated payloads and moves through references; variant-payload moves are reset-on-move; a downcast place has no type (#1394, #1381, #1415) - #1425

Merged
ehartford merged 6 commits into
fix-1396-anon-file-scope-structfrom
fix-1394-variant-payload-moves
Sep 23, 2026
Merged

ehartford merged 6 commits into
fix-1396-anon-file-scope-structfrom
fix-1394-variant-payload-moves

Conversation

@ehartford

Copy link
Copy Markdown
Contributor

Fixes #1394, Fixes #1381, Fixes #1415.

Two tools stayed silent over real double frees, and one audit failed on every ??/unwrap_or because MIR and codegen disagreed about a place's type. This PR makes the tools report the defects, then fixes the defects they report.

Root causes

#1394, tool. validate_ownership_body (src/MirCore.w) checked a Drop only for Uninit/MaybeGarbage. It also could not have seen #1363's shape: MirDropStateMap.mark_place joined Moved into the base local on a sub-place move. After _8 = move _6<as v0>.f0 on one arm, both _6 and _6<as v0>.f0 read Maybe at the join, and nothing told a partial move apart from a conditional whole move.

  • A sub-place move now leaves the base's state alone.
  • The validator rejects a drop of a place whose drop-bearing sub-place is more moved than the place: Moved under Init or Maybe, or Maybe under Init.
  • MirCore has no Sema. Lowering records every drop-bearing type a body moves out of a sub-place (MirModule.sema_moved_drop_types).

#1394, lowering. consume_moved_operand (src/MirLower.w) registered moves only for a whole local or a pure field path. place_field_projection_count returns -1 through PK_DOWNCAST, so a payload move registered no reset-on-move (§2.5.1). A payload move now queues its blank, tagged with its block. The blank lands before the moving block's terminator (it is carried across a call into the call's continuation), and before any earlier drop or overwrite of the same base. It never lands at a statement flush that may sit after a join: a payload slot overlaps the other variants' payloads.

#1381. Most producers typed a downcast place as the whole enum (new_downcast_place(value_place, idx, value_ty)). Match passed 0, and codegen projects PK_DOWNCAST to the variant's payload struct. The one rule now: a downcast place has no type. new_downcast_place(base, idx) takes none, and the typed walk yields none for a place that ends in a downcast. The audit message printed only kinds ("kind 10 but ... kind 10"). It now prints both LLVM types.

#1415. Nothing rejected _4 = move _1.*.p through &self. The validator now rejects an OK_MOVE, in any statement or terminator, of a drop-bearing place whose projections dereference a TY_REF. A reference never owns its pointee. A mut fn receiver's vacate is _1.p, with no deref, and stays legal. So do a raw pointer's pointee and a Box's.

What the stricter validator found (corpus: test/, examples/, lib/std; origin/main lowering)

It flagged 24 files, and each one is a real defect.

Two more defects in the same class turned up while fixing these. The validator cannot see either one: the drop lattice cannot yet tell maybe-moved from maybe-uninit (#1414).

Validator verdicts

program before after
#1363 repro, origin/main lowering validate-all: ok drop of _6 frees _6<as v0>.f0, which a path reaching it moved out (Maybe) and nothing reset (§2.5.1)
#1363 repro, validator + #1398 (fix-1363-unwrap-or-str merged on the validator commit) — validate-all: ok
#1363 repro, this branch — validate-all: ok; MIR _8 = move _6<as v0>.f0; _6<as v0>.f0 = const zst on the success arm
#1415 repro, origin/main lowering validate-all: ok (debug allocator: DOUBLE FREE) moves out of _1.*.f769 through a reference, which does not own it (§2.2 D32, D22)
#1415 repro, this branch + #1416 — rejected by Sema (cannot take ownership of a non-Copy field through a borrow)
o ?? 0, analyze audit:all violation: place 6 ... kind 10 but ... kind 10 violations=0 ok

Evidence (iterate tier; stage2 built from this branch, rebased on f008ef3)

This is an ownership and drop-scheduling change, so batch it alone, with :drop-audit and :move-audit. Full battery not run: this PR stays a draft until it is.

Filed along the way

@ehartford

Copy link
Copy Markdown
Contributor Author

Batch battery X2 (seed-driven, NEW seed, with :drop-audit/:move-audit) on main 977e667 + #1404 → #1418 → #1419 → #1425 (head ee4e2e4, including the #1404/#1418 join-arm merge resolution): build ok, :fixpoint ok, :drop-audit ok, :move-audit ok, :test ok (no reds), :test-green ok, :last-green ok. #1424's four repros run clean on this build. Branches hold exactly the tested chain; merge in order #1404, #1418, #1419, #1425 — then batch Y (#1405 … #1360), then #1428.

@ehartford
ehartford marked this pull request as ready for review September 23, 2026 04:37
@ehartford
ehartford marked this pull request as draft September 23, 2026 04:38
@ehartford
ehartford force-pushed the fix-1394-variant-payload-moves branch from ee4e2e4 to 5259626 Compare September 23, 2026 04:38
@ehartford

Copy link
Copy Markdown
Contributor Author

Main moved (#1397 merged, 04e4934) after X2's green and conflicted in MirLower (both added a lowering-state field next to each other: pattern_bind_mut / pattern_move_log). Rebased the X2 chain onto main keeping both; because #1397 changes let-else pattern lowering and this PR changes pattern move tracking, re-battery with audits now (with #1428's fixture on top). Draft until green.

@ehartford
ehartford force-pushed the fix-1394-variant-payload-moves branch from 5259626 to ace3b38 Compare September 23, 2026 05:16
ehartford pushed a commit that referenced this pull request Sep 23, 2026
…1424)

Fails (DOUBLE FREE) on #1398's build, passes on #1425's reset-on-move
(batch X2). Lands after X2.
@ehartford
ehartford changed the base branch from main to fix-1396-anon-file-scope-struct September 23, 2026 05:17
@ehartford
ehartford added this pull request to stack #1437 September 23, 2026 05:17
@ehartford

Copy link
Copy Markdown
Contributor Author

Battery (seed-driven, new seed, with :drop-audit/:move-audit) on main 05b674e (incl. batch Y) + #1418 → #1419 → #1425 → #1428 (head eea9017): build ok, :fixpoint ok, :drop-audit ok, :move-audit ok, :test ok (no reds — including #1422's da_option_filter_observes_payload, so this clears main's #1424 red), :test-green ok, :last-green ok. Merges cleanly into main 1c76062. Now GitHub stack #1437 (gh stack link); merge the whole stack: gh stack merge 1428 --squash.

@ehartford
ehartford marked this pull request as ready for review September 23, 2026 05:17
Eric Hartford added 6 commits September 23, 2026 04:43
… reset is rejected (#1394)

The ownership validator passed #1363's MIR: `_8 = move _6<as v0>.f0` on
the success arm, then `drop(_6)` at the join, where the enum drop glue
freed the payload the result owned. Two causes:

- mark_place joined Moved into the base on a sub-place move, so the base
  read Maybe and the vacated payload read the same Maybe as its parent;
  nothing distinguished a partial move from a conditional whole move.
  A sub-place move now leaves the base's state alone.
- validate_ownership_body checked a Drop only for Uninit/MaybeGarbage.
  It now rejects a drop of a place with a drop-bearing sub-place more
  moved than the place itself (Moved under Init/Maybe, Maybe under Init):
  a path reaching the drop vacated it and nothing reset it (§2.5.1).

MirCore has no Sema, so lowering records in the module every type a body
moves out of a sub-place that has drop glue (sema_moved_drop_types);
the validator looks the vacated sub-place up by the typed validator's own
place type.
…places (#1394)

Two producers lowered an observation as a move. The stricter
validate-ownership rejected both, each where an owner drops whole after
the "move" of its field:

- `v.text == s` with `s: &str` fell outside the str-vs-str observer
  path and lowered the owned side as a value: `binop(eq, move _2.text,
  copy _6)` (ComptimeEvaluator.match_pattern in the compiler itself).
  An owned side compared with a `&str` now observes too.
- An f-string interpolant `{u.name}` lowered as `move _1.name` into
  fmt_buf_write_str, which (like fmt_to_str and every FmtBuffer writer)
  reads the value and leaves it to its owner (#761). A moved place
  operand becomes a copy: formatting observes.

Nothing reset either "moved" field, so the MIR claimed the owner's drop
freed a value it no longer owned. validate-all: 11 corpus files
(idiomatic, service x3, email, ephemerality x2, issue65 x2, named
arguments, dump_typed_top_level_string_sidecar_key) and src/main.w
now pass.
…h LLVM types (#1381)

`with analyze audit:all` failed on every `??`/`unwrap_or`:

  violation: place 6 in main: projection lowers to LLVM type kind 10
  but its MIR type 339 (Option[i32]) is kind 10; ...

lower_double_question, lower_unwrap_or_method and most Option/Result
combinators built new_downcast_place(value_place, idx, value_ty),
typing the downcast place as the whole enum, while codegen
(mir_place_projected_type) projects PK_DOWNCAST to the variant's payload
struct; match lowering passed 0. One rule now: a downcast place has no
type. It is only ever the base of a payload field place, which carries
the payload's type. new_downcast_place takes no type (every producer
gets the rule), and the typed validator's place walk yields none for a
place that ends in a downcast, so a downcast used as a value is
reported instead of read as the enum.

The audit message compared type identity while printing only kinds;
it now prints both LLVM types (wl_print_type, LLVMPrintTypeToString).
…match guard gives its bindings back (#1394)

consume_moved_operand registered a move only for a whole local or a pure
field path: place_field_projection_count is -1 through PK_DOWNCAST, so a
move out of a variant payload registered no reset-on-move (§2.5.1). Every
carrier eliminator had to hand-roll the subject's ownership, and the ones
that did not left the enum's drop glue to free the payload the result
owned. The stricter validate-ownership found them across the corpus:
unwrap_or (#1363), Result.map/map_err/or_else's passthrough arm,
.context/.with_context, ?., custom Try, sequence/traverse. Under
WITH_DEBUG_ALLOC=1 (origin/main stage2) `bad().map((x) => x + 1)` on
an Err(Vec) reports DOUBLE FREE first_drop=__drop_enum second_drop=
__drop_struct_96.

A payload move now queues its blank with the block it happened in. A
payload slot overlaps the other variants' payloads, so the blank has to
land on the moving path while the enum still holds that variant, never at
a statement flush that may sit after a join: it is emitted before the
moving block's terminator (carried across a call into the call's
continuation), and before an earlier drop or overwrite of the same base
in that block. ok()/err() drop their hand-rolled blank for it;
lower_context_error_operand now consumes the moved source.

A failed match guard dropped the arm's bindings, freeing the payload the
next arm then bound again (Some(v) if .. => .., Some(v) => ..: DOUBLE
FREE on origin/main). With payloads blanked the next arm would bind the
blank instead. Pattern bindings now log what they move out of the
subject, and the guard-fail path moves it back and blanks the binding.

Evidence (stage2 from this branch):
- #1363 repro: validate-all ok; `_8 = move _6<as v0>.f0; _6<as v0>.f0 =
  const zst` on the success arm, then drop(_6) frees only an Err payload.
- validate-all over test/, examples/, lib/std (2138 files): all 24 files
  the new rule flagged pass; no file newly fails; src/main.w ok.
- test/debug_alloc/da_variant_payload_move_blanked.w: map, err(), ?.,
  guarded payload and whole bindings — DOUBLE FREE on origin/main for
  map, ?., both guards; leak count=0 here.
- build.w deep-debug-tool-tests: audit:all on a ??/unwrap_or/map fixture
  must report violations=0.
`r.context(msg)` lowered the message as a statement temp and, on the Err
arm, moved it into the ContextError; the statement flush then dropped the
temp as well. With #1398's debug-allocator fix (the str free path asks the
ledger), `root().context(heap_message())?` on an Err reports

  DOUBLE FREE addr=... size=64 first_drop=drop#struct __drop_struct_16
  second_drop=drop#struct __drop_struct_434

The call now takes the message into its own local before the branch: the
Err arm moves it into the ContextError, the Ok arm drops it
(`context-message-unused`). with_context is unchanged (its message is
built inside the Err arm).

test/debug_alloc/da_variant_payload_move_blanked.w covers both arms: the
Ok arm leaks if the drop is missing (leak count), the Err arm double-frees
under a runtime that reports str double frees.
…ce is rejected (#1415)

Before #1395's Sema fix, `let v = if c: self.p else: Vec.new()` in a
`&self` method lowered the join arm as `_4 = move _1.*.p`, a move out of
the shared receiver's pointee; `--validate-all` said ok and the debug
allocator reported the double free (rc=134).

A reference never owns its pointee: the frame can neither own the moved
value nor reset its source, so the referent's owner and the destination
both free it (§2.2 D32: a field vacates only through a `var` base or a
`mut fn` receiver, whose place carries no deref — `_2 = move _1.p`;
D22: a view never becomes an owner). validate_ownership_body now rejects
an OK_MOVE, in any statement or terminator, of a drop-bearing place whose
projections dereference a TY_REF. A raw pointer's pointee (the unsafe
tier) and a Box's are owned through the pointer and stay legal.

Evidence (stage2 from this branch, repro from #1415, origin/main lowering):
  error: validate-ownership failed: fn sym770 stmt3 span=72: moves out of
  _1.*.f769 through a reference, which does not own it (§2.2 D32, D22)
The parent commit's stage2 says `validate-all: ok` on the same MIR;
`run --debug-alloc` on it: DOUBLE FREE ... second_drop=drop#struct
__drop_struct_135. src/main.w: validate-all ok.
test/internals/mir_move_through_reference_test.w pins the rule (move
through &T rejected; copy through &T, move through *T, and a type with
no drop glue accepted).
@ehartford
ehartford force-pushed the fix-1394-variant-payload-moves branch from ace3b38 to d3ceefc Compare September 23, 2026 09:57
ehartford pushed a commit that referenced this pull request Sep 23, 2026
…1424)

Fails (DOUBLE FREE) on #1398's build, passes on #1425's reset-on-move
(batch X2). Lands after X2.
@ehartford
ehartford removed this pull request from stack #1437 September 23, 2026 09:58
@ehartford
ehartford added this pull request to stack #1466 September 23, 2026 09:58
@ehartford

Copy link
Copy Markdown
Contributor Author

Stack #1466 battery (seed-driven, new seed, cold c_import cache, with :drop-audit/:move-audit) on main 1c76062 + #1418 → #1419 → #1425 → #1428 → #1449 → #1377 → #1459 → #1460 → #1461 → #1462 (head cf36211, including the #1449/#1462 Codegen resolution and the fixed #1460): build ok, :fixpoint ok, :drop-audit ok, :move-audit ok, :test ok (no reds), :test-green ok, :last-green ok. Each branch holds exactly its tested commits on the layer below. Merge the whole stack: gh stack merge 1462 --squash.

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