Repository navigation
Map traversal and snapshots never make a second owner (D44 step 1; #1187, #1158) - #1190
Merged
Merged
Conversation
for (k, v) in map moved the map into a temporary that was dropped after the loop, leaving the binding blank: m.len() printed 0 and m.get crashed, while with check said ok (#1187). lower_for_hashmap now reads a place receiver through its own place, as lower_for_vec does, and walks the table in place through four new intrinsics over the runtime's existing slot API (MAP_CAPACITY, MAP_SLOT_OCCUPIED, MAP_KEY_AT, MAP_VALUE_AT) instead of a byte-copied items() Vec. One element rule with Vec (Sema.traversal_binding_type): a Drop-class key or value binds as a view into the slot, a Copy-class one by value. 'for (k, v) in &m' and a &HashMap parameter now bind their variables; infer_for_element_type returned 0 for a borrowed map. keys(), values() and items() byte-copied their elements, so for a non-Copy element the Vec and the map owned the same buffers: the compiler's own sema_clone_str_str_hashmap double-freed through .keys() building the OpenSSL project (#1158; allocator: DOUBLE FREE size=64, second free with_hashmap_free from Zcu.compile_source_frontend_mode), and a plain run passed while WITH_DEBUG_ALLOC_SCRIBBLE=1 showed the map corrupted in all three modes. Sema now requires Clone for a non-Copy element and records the clone contract; MirLower builds the snapshot by walking the table and cloning. Copy elements keep the byte copy, which is a Copy. The C backend fails loudly for the table walk, as it already does for values()/items(). test/behavior/behav_d44_map_ownership.w runs both case files under --debug-alloc with scribble, with and without block reuse, and requires zero leaks. HashMap.remove leaking its stored key is separate (#1189).
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Closes #1187. Fixes the defect behind #1158 (full OpenSSL proof rides on the
with getbranch, which carries the c_import fixesmaindoes not have). First step of D44's non-compliance list (docs/decisions.md); ownership change, alone in its batch.for (k, v) in map(#1187)lower_for_hashmaplowered the receiver withlower_expr+materialize_operand— a move into a temp that was dropped after the loop, leaving the binding blank.lower_for_vecalready reads a place receiver through its own place (its comment cites §13); the map path now does the same.items()Vec whose elements shared the map's buffers. It now walks the table in place through four intrinsics over the runtime's existing slot API (with_hashmap_capacity/slot_occupied/key_ptr_at/value_ptr_at— already used by the element drop glue):MAP_CAPACITY,MAP_SLOT_OCCUPIED,MAP_KEY_AT,MAP_VALUE_AT. The last two yield the slot's address when the destination is a view and load through it when it is a value, asVEC_GET_REF/VEC_GETdo.Vec(Sema.traversal_binding_type): a Drop-class key or value binds as&T, a Copy-class one by value.for (k, v) in &mand a&HashMapparameter leftkandv"undefined variable":infer_for_element_typereturned 0 for&HashMap, andlower_forhad no route for it. Both fixed beside the existing&Veccases.keys()/values()/items()(#1158)They byte-copied elements through
with_hashmap_{keys,values,items}_out. For a non-Copy element the Vec and the map owned the same buffers: the compiler's ownsema_clone_str_str_hashmapdouble-freed through.keys()(allocator:DOUBLE FREE size=64; lldb: second freewith_hashmap_freefromZcu.compile_source_frontend_mode), andmap_snapshots.wprintedokon a plain run whileWITH_DEBUG_ALLOC_SCRIBBLE=1showed the map corrupted in all three modes.Spec §2.3 (D44): transport is not duplication. Sema now requires
Clonefor a non-Copy element and records the clone contract (key on the call node, value on its callee — one contract per node); MirLower builds the snapshot by walking the table and cloning throughclone_or_copy_place. Copy elements keep the byte copy, which is aCopy. Signatures are unchanged here; the view iterators, the stdlib map anditems()retirement are D44's next step, in one commit.The C backend fails loudly for the table walk, as it already does for
values()/items().Evidence
test/behavior/behav_d44_map_ownership.wrunslib/map_traversal_cases.wandlib/map_snapshot_cases.wunderrun --debug-allocwithWITH_DEBUG_ALLOC_SCRIBBLE=1, with and without block reuse, requiringleak count=0and no double free: Copy × Drop-class keys and values in all four combinations, owned and borrowed maps,&m,break,continue, earlyreturn, the empty map, the map intact and mutable afterwards, snapshots that outlive their map, a map that outlives its snapshots.test/compile_errors/err_d44_map_snapshot_needs_clone.wcovers the new diagnostic.Every existing test that iterates a map (8 files) or calls a snapshot method (11 files) gives the same result as on the installed compiler; the behaviour ones also pass under scribble with zero leaks.
With the release compiler from this branch,
with buildof the fetched OpenSSL project no longer dies withinvalid free; it reports the underlying c_import error (OSSL_DEPRECATEDIN_4_0), whichmainstill has and thewith getbranch fixes.Battery (pinned seed v0.15.2.0,
WITH_CODEGEN_EMIT_WIDTH=16)build272 s ·:fixpoint107 s ·:test-with-audits840 s (1042 behaviour, 769 compile-error;drop-audit123 s andmove-audit3 s inside it) ·:test-green·:last-green— all rc=0, ~21 min end to end.Found on the way, not fixed here
#1189 —
HashMap.removeleaks the stored key (onmaintoday;with_hashmap_removeclears the slot without dropping the key, and codegen emits no drop).removeis left out of the new test so it can assert zero leaks.