Skip to content

Codegen: a Box dropped as a Box drops its pointee by type — Box[L] of a recursive enum no longer leaks the Box inside the L (#1456) - #1546

Merged
ehartford merged 1 commit into
mainfrom
fix-1456-box-recursive-enum-drop
Sep 23, 2026
Merged

ehartford merged 1 commit into
mainfrom
fix-1456-box-recursive-enum-drop

Conversation

@ehartford

Copy link
Copy Markdown
Contributor

let b = Box.new(L.Cons(5, Box.new(L.Nil))) leaked 32 bytes (one box).
Route: debug allocator, then the drop's IR, then lldb on the emitter.

Evidence. with run --debug-alloc: LEAK size=32 origin=with_alloc, leak
count=1. Under lldb (ASLR off) WITH_DEBUG_ALLOC_TRAP_FREE on that
address: one trap-alloc hit=1, no free — the inner box. with ir:
main's scope-exit drop of b is call @with_free_drop_origin(ptr %26)
and nothing else; L's drop glue is never called. lldb on stage1 with
breakpoints on Codegen.mir_emit_box_drop_place (x3 = sema type) and
Codegen.mir_emit_drop_ptr: for x3 = 249 (Box[L]; the IR names
Box.new__receiver__249_235_235, L = 235) the box drop calls
mir_emit_drop_ptr — CodegenDispatch.w mir_emit_box_drop_place,
self.mir_emit_drop_ptr(heap_ptr, payload_ty). That drops by LLVM type
only: %L resolves to the enum's symbol, which has no Drop impl, and
the struct-field glue walks no fields of an enum, so the pointee's
payload was never dropped. A Box inside an enum payload drops through
Box's own Drop (by type), which is why one level of L did not leak.

Fix. The Box place drop drops its pointee with
mir_emit_drop_ptr_for_sema_type (the payload's sema type) before the
free; the LLVM-type path stays for a pointee with no sema type (a
dyn box). Same null guard as before.

Matrix (da_box_pointee_dropped_by_type, checked with tools/debug_drop.w,
the debug-alloc lane's driver): the issue's Box of a two-level list, a
four-level list, a list moved into a match binding by value, a Box moved
into fn take(b: Box[L]), Box[str], Box[Vec[str]], a Box of a struct
with str and Vec[str] fields, Box[Option[str]], a Box of a tree enum
holding Vec[Box[T]], a Vec[Box[L]], a reassigned var Box. Base: 23
leaks, the lane FAILs the fixture. Now: leak count 0; also 0 with
WITH_ALLOC_NO_REUSE=1 and no double-free report. Filed while here: a Box
in an Option payload is never freed at all (Option[Box[str]] leaks the
box and the str; the payload path drops Box only through a pre-frozen
concrete Drop, #1535).


Layer of the enums/discriminants stack (#1455 → #1451 → #1452 → #1453 → #1454 → #1441 → #1442 → #1439 → #1456). Draft until the stack's seed-driven battery is green (D56).

@ehartford

Copy link
Copy Markdown
Contributor Author

Stack #1547 battery (seed-driven, new seed, cold c_import cache, with :drop-audit/:move-audit) on main ce7508c + #1537 → … → #1546 (head bee5c56; every PR head is a tested commit; includes the #1451 same-named-enum fix and the render.w rebase over #1513): build ok, :fixpoint ok, :drop-audit ok, :move-audit ok, :test ok (no reds), :test-green ok, :last-green ok. Merges cleanly into main 02301f8. Merge the stack: gh stack merge 1546 --squash.

@ehartford
ehartford marked this pull request as ready for review September 23, 2026 21:34
@ehartford
ehartford force-pushed the fix-1456-box-recursive-enum-drop branch from bee5c56 to f4290fa Compare September 23, 2026 23:51
Base automatically changed from fix-1439-generic-value-cycle to main September 23, 2026 23:54
… a recursive enum no longer leaks the Box inside the L (#1456)

`let b = Box.new(L.Cons(5, Box.new(L.Nil)))` leaked 32 bytes (one box).
Route: debug allocator, then the drop's IR, then lldb on the emitter.

Evidence. `with run --debug-alloc`: LEAK size=32 origin=with_alloc, leak
count=1. Under lldb (ASLR off) WITH_DEBUG_ALLOC_TRAP_FREE on that
address: one `trap-alloc hit=1`, no free — the inner box. `with ir`:
main's scope-exit drop of `b` is `call @with_free_drop_origin(ptr %26)`
and nothing else; L's drop glue is never called. lldb on stage1 with
breakpoints on Codegen.mir_emit_box_drop_place (x3 = sema type) and
Codegen.mir_emit_drop_ptr: for x3 = 249 (Box[L]; the IR names
`Box.new__receiver__249_235_235`, L = 235) the box drop calls
mir_emit_drop_ptr — CodegenDispatch.w mir_emit_box_drop_place,
`self.mir_emit_drop_ptr(heap_ptr, payload_ty)`. That drops by LLVM type
only: `%L` resolves to the enum's symbol, which has no Drop impl, and
the struct-field glue walks no fields of an enum, so the pointee's
payload was never dropped. A Box inside an enum payload drops through
Box's own Drop (by type), which is why one level of L did not leak.

Fix. The Box place drop drops its pointee with
mir_emit_drop_ptr_for_sema_type (the payload's sema type) before the
free; the LLVM-type path stays for a pointee with no sema type (a
`dyn` box). Same null guard as before.

Matrix (da_box_pointee_dropped_by_type, checked with tools/debug_drop.w,
the debug-alloc lane's driver): the issue's Box of a two-level list, a
four-level list, a list moved into a match binding by value, a Box moved
into `fn take(b: Box[L])`, Box[str], Box[Vec[str]], a Box of a struct
with str and Vec[str] fields, Box[Option[str]], a Box of a tree enum
holding Vec[Box[T]], a Vec[Box[L]], a reassigned `var` Box. Base: 23
leaks, the lane FAILs the fixture. Now: leak count 0; also 0 with
WITH_ALLOC_NO_REUSE=1 and no double-free report. Filed while here: a Box
in an Option payload is never freed at all (`Option[Box[str]]` leaks the
box and the str; the payload path drops Box only through a pre-frozen
concrete Drop, #1535).
@ehartford
ehartford force-pushed the fix-1456-box-recursive-enum-drop branch from f4290fa to 1322ade Compare September 23, 2026 23:54
@ehartford
ehartford merged commit 408eb49 into main Sep 23, 2026
@ehartford
ehartford deleted the fix-1456-box-recursive-enum-drop branch September 23, 2026 23:54
ehartford added a commit that referenced this pull request Sep 24, 2026
…uilt Vec[i32]; main red at stage2 after #1546 + #1581 (#1596)

* MIR: the three switch tables built from Vec[i32] take Vec[i64] — discriminants are i64 end to end (#1541) and the width check (#1368) now rejects the narrowed table; main was red at stage2

* drop-audit: the field_join_view match cell indents its arms under the match — the seed parsed the flush arms by accident; §29.13 (#1391) rejects them

* MirValidationTests: the last switch table built from Vec[i32] takes Vec[i64] — the internals tests compile this file, src/main.w does not

---------

Co-authored-by: Eric Hartford <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant