Skip to content

Map traversal and snapshots never make a second owner (D44 step 1; #1187, #1158) - #1190

Merged
ehartford merged 1 commit into
mainfrom
d44-map-defects
Sep 19, 2026
Merged

ehartford merged 1 commit into
mainfrom
d44-map-defects

Conversation

@ehartford

Copy link
Copy Markdown
Contributor

Closes #1187. Fixes the defect behind #1158 (full OpenSSL proof rides on the with get branch, which carries the c_import fixes main does 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)

var m: HashMap[i32, i32] = HashMap.new()
m.insert(1, 7)
for (k, v) in m: print(f"{k}")
print(f"{m.len()}")      // printed 0; m.get(1) crashed; `with check` said ok
  • Emptied map. lower_for_hashmap lowered the receiver with lower_expr + materialize_operand — a move into a temp that was dropped after the loop, leaving the binding blank. lower_for_vec already reads a place receiver through its own place (its comment cites §13); the map path now does the same.
  • No second owner. The loop walked a byte-copied 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, as VEC_GET_REF / VEC_GET do.
  • One element rule with Vec (Sema.traversal_binding_type): a Drop-class key or value binds as &T, a Copy-class one by value.
  • Borrowed maps. for (k, v) in &m and a &HashMap parameter left k and v "undefined variable": infer_for_element_type returned 0 for &HashMap, and lower_for had no route for it. Both fixed beside the existing &Vec cases.

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 own sema_clone_str_str_hashmap double-freed through .keys() (allocator: DOUBLE FREE size=64; lldb: second free with_hashmap_free from Zcu.compile_source_frontend_mode), and map_snapshots.w printed ok on a plain run while WITH_DEBUG_ALLOC_SCRIBBLE=1 showed the map corrupted in all three modes.

Spec §2.3 (D44): transport is not duplication. Sema now requires Clone for 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 through clone_or_copy_place. Copy elements keep the byte copy, which is a Copy. Signatures are unchanged here; the view iterators, the stdlib map and items() 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.w runs lib/map_traversal_cases.w and lib/map_snapshot_cases.w under run --debug-alloc with WITH_DEBUG_ALLOC_SCRIBBLE=1, with and without block reuse, requiring leak count=0 and no double free: Copy × Drop-class keys and values in all four combinations, owned and borrowed maps, &m, break, continue, early return, 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.w covers 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 build of the fetched OpenSSL project no longer dies with invalid free; it reports the underlying c_import error (OSSL_DEPRECATEDIN_4_0), which main still has and the with get branch fixes.

Battery (pinned seed v0.15.2.0, WITH_CODEGEN_EMIT_WIDTH=16)

build 272 s · :fixpoint 107 s · :test-with-audits 840 s (1042 behaviour, 769 compile-error; drop-audit 123 s and move-audit 3 s inside it) · :test-green · :last-green — all rc=0, ~21 min end to end.

Found on the way, not fixed here

#1189 — HashMap.remove leaks the stored key (on main today; with_hashmap_remove clears the slot without dropping the key, and codegen emits no drop). remove is left out of the new test so it can assert zero leaks.

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).
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.

Iterating a HashMap empties it: for (k, v) in m leaves m.len() == 0 and m.get segfaults (spec §13.5)

1 participant