Repository navigation
Description
The smallest .hy program that demonstrates it
package main
fn wm(x map[string]int, done chan int) {
for i in 0..1000 {
x["race"] = i
}
done <- 1
}
fn main() {
done := make(chan int, 2)
keep := map[string]int{}
nxt := map[string]int{}
for i in 0..2 {
t := nxt
nxt = map[string]int{}
task wm(move t, done)
keep["race"] = i
keep = nxt
}
<-done
<-done
}Commands (main at 1f0ff97, build pins go1.26.9 / rust 1.99.0):
hyb check loop_peer_freeze.hy
hyb build loop_peer_freeze.hy -o out/bin --emit-go
cd out && go run -race .
What the checker should say
HYB-E3003 at 17:9 (keep["race"] = i): "write through a live alias of a moved value", with ownership of t leaving at the spawn at 16:17. The unrolled form of this exact program also reports the rebind of the frozen alias, so a second HYB-E3003 at 18:9 (keep = nxt) is expected as well.
Why it is unsafe: on iteration 1, line 18 (keep = nxt) makes keep share storage with the map that nxt holds. On iteration 2, line 14 (t := nxt) copies that same map into t, line 15 rebinds nxt to a fresh map, and line 16 moves t into a new task. keep is still a live alias of the moved map, so line 17 writes it on the main task while the spawned task writes it too. SPEC-LANGUAGE O5.2 (Freeze-after-send) says that when a value is moved or sent, every live alias in the sending task is frozen, and any read or write through a frozen alias is HYB-E3003. GUARANTEES row 6 marks this as Guaranteed ("a moved value cannot be used by the sender afterwards"), and row 1 says no value reaches a spawned task through an unproven path.
Controls (all on main 1f0ff97):
- Straight-line form (
keep := nxt; t := nxt; nxt = map[string]int{}; task wm(move t, done); keep["race"] = 1) is rejected withHYB-E3003at 16:5, with the note "keepaliasest" (O5.2). - Unrolling the two iterations into two
{ }blocks with the same five statements each is rejected withHYB-E3003at the second block's write and rebind (24:9 and 25:9). - Moving the alias to the top of the loop body (
keep = nxtbeforet := nxt) is rejected withHYB-E3003(14:9 and 18:9). - Only the loop-carried version, where the alias is made after the move site, is accepted.
What it actually said
f2m_loop_carried_peer_freeze.hy: OK (0 diagnostics)
go run -race on the emitted Go reports a race in 10/10 runs (exit status 66). Excerpt from a real run (emitted main.go:17 is go wm(t, done), main.go:18 is keep["race"] = i):
==================
WARNING: DATA RACE
Write at 0x00c0000a0030 by goroutine 8:
runtime.mapassign_faststr()
$GOROOT/src/internal/runtime/maps/runtime_faststr.go:263 +0x0
main.wm()
main.go:5 +0x53
main.main.gowrap1()
main.go:17 +0x12
Previous write at 0x00c0000a0030 by main goroutine:
runtime.mapassign_faststr()
$GOROOT/src/internal/runtime/maps/runtime_faststr.go:263 +0x0
main.main()
main.go:18 +0x10d
Goroutine 8 (running) created at:
main.main()
main.go:17 +0xf0
Claimed severity
P0, data race reachable in the checked subset (no managed, no unsafe).
Anything else
Rule: O5.2 (freeze of every live alias after a move, HYB-E3003), with O5.1.
Variants, all accepted with 0 diagnostics and all racing 5/5 under -race:
- the move is a channel send (
ch <- move t) instead oftask - the shared value is a slice instead of a map
- a condition loop (
for i < 2 { ... }) instead of a range loop - the tail alias is made inside an
ifat the end of the body - the loop is in a helper function instead of
main - the write through the peer happens inside a closure, defined before the loop and called right after the move
- the tail alias comes from a helper that returns its argument (
cur = alias_of(m)), andmainkeeps writingmafter the loop
Control: a 0..1 loop with the alias at the tail (genuinely sound, no second iteration) is accepted and races 0/3.
Likely root cause (by reading plus a debug print, not certain): Pass B walks the flattened loop body once in order. At line 16, mark_move (freeze.rs:298) freezes the storage component of t (freeze.rs:43) through aliases and may_share, but at that point keep does not alias t yet; the edge keep to nxt only appears at line 18. At the loop end the state is: t moved (a loop local, popped), keep aliases nxt, and nxt was never moved, because line 15 rebinds it before the move. check_loop_backedge (freeze.rs:608, called from lib.rs around line 884 and from closures.rs) seeds only outer bindings that are still moved, finds none, and returns early. Its replay only calls emit_use_after_move; it never re-runs the move freeze or the write-through-alias check under the end-of-body alias state. So the alias carried in from line 18 is never frozen when line 16 moves the same map on iteration 2.
A local experiment (not a proposed patch) that clones the checker state at the loop end and replays the body once more, running the use check and the call/move processing at each statement and keeping only new HYB-E3003 / HYB-E3004 diagnostics, rejects the program above and all seven variants with HYB-E3003, and leaves every straight-line, unrolled, and alias-first control unchanged. cargo test --workspace passes (1800/0), the corpus and differential suites agree (127/127 verdicts; 41/41 accept, 86/86 reject), and verdicts on all 156 in-repo .hy files are unchanged. As expected from any fixpoint, it also rejects the sound 0..1 control and a few sound shapes that the straight-line rule already rejects (rebinding or deferring a use of a frozen alias), which are conservative false positives.
Related but distinct:
- [soundness] HYB-E3004 bypass: a non-owned origin assigned at the loop tail is moved on the next iteration (no back-edge origin replay) #2313 (
HYB-E3004, non-owned origin assigned at the loop tail) has the same "state made at the loop tail" shape, and its side note mentions a missed frozen-alias read in that program. The race there comes from the non-owned origin; here no parameter is involved and the race comes only from the missing freeze. The replay described in [soundness] HYB-E3004 bypass: a non-owned origin assigned at the loop tail is moved on the next iteration (no back-edge origin replay) #2313 (origin only) still accepts this program; the state that is missing here is the alias graph used bymark_move, notnon_owned_origin. - [soundness] walk_stmt_param_stores walks a loop body once, so a loop-carried param store in a helper is missed (HYB-E3003 bypass, go run -race data race) #2308 / PR hy-check: loop-carried param-store aliases reach a fixpoint; scope-aware merges (#854) #2307 are about the
walk_stmt_param_storessummary in loans.rs. PR hy-check: loop-carried param-store aliases reach a fixpoint; scope-aware merges (#854) #2307 at its current head (88007db) still accepts this program and races 3/3. - [soundness] T22: a const alias created at the end of a loop body is not carried over the back-edge, so the next iteration writes to the const (HYB-E3006 bypass) #2309 / PR hy-check: carry const aliases across loop back-edges (fixes #2309) #2314 are about
constaliases carried over the back edge (HYB-E3006). Noconsthere; PR hy-check: carry const aliases across loop back-edges (fixes #2309) #2314 (b581485) still accepts and races 3/3. - [soundness] Loop back-edge check (check_loop_backedge) fails to propagate moved alias state across iterations, allowing double-move data races #313 / fix(hy-check): propagate moved alias and may_share state across block scope exit and loop back-edges (#313, #399, #253) #401 and Soundness: move of outer binding inside loop accepted with 0 diagnostics, causing concurrent map writes (HYB-E3003) #23 / [soundness] loop-backedge freeze erased by call-local roots: m := same(agg) in a loop re-moves the same map into two tasks (follow-up gap in #23's fix) #194 / [soundness] check_loop_backedge: inner-block local shadowing erases outer loop-carried move/alias freeze, and ForKind::Range upper bound hi is unchecked on back-edge (0 diagnostics on 44a08b9) #233 / hy-check: check_loop_backedge misses loop-carried moves on shadowed pre_moved bindings and LX14 continue loop / break loop inside nested finite for loops (go run -race) #739 are about a binding that is still marked moved (or an alias that already existed before the move) across the back edge. Here the alias is created after the move site and nothing outer stays moved, so that path returns early.
- P0 soundness: inner loop header self-shadowing on back-edge, block/multi-value self-shadow share retention across scope_pop, user method return shapes, and reslice/index/append borrow-root & return loan-escape bypasses (HYB-E3003 / E3004 / E3104) #456 (item 1) moves an alias that already points at the outer value before the move; [soundness] P0: block-scoped assignment drops may_share edges - accepted program races #399 and [P0 Soundness] Fresh-copy call in loop body clears back-edge move state of outer argument root via alias resolution #372 now reject on main.
- Checked heads of open PRs hy-check: loop-carried param-store aliases reach a fixpoint; scope-aware merges (#854) #2307, hy-check: carry const aliases across loop back-edges (fixes #2309) #2314, hy-check: close conditional/closure rebind laundering, outermost closure scope blindness, and loop-continue back-edge gaps (fixes #562) #1789, hy-check: track closure whole-binding rebinds of captured outer vars and loop back-edge bare returns (fixes #839) #1750, fix(hy-check): track LX14 continue/break loop across nested for loops in check_loop_backedge (Fixes #882) #886, fix(hy-check): enforce non-owned origin and non-ident roots on CallMode::Consume (#2172) #2173, fix(hy-check): evaluate parallel assign/define RHS against pre-statement snapshot (#795) #803 and fix(hy-check): enforce HYB-E3104/E3103/E3003/E3004 on auto-addressed calls, range/match/copy/out-param borrows, index stores, and closures (Fixes #920) #921: all still accept the program.
Activity
jpereyra243-byte commentedon Oct 30, 2026
/claim #2317
Working on loop-carried peer freeze back-edge propagation fix in
compiler/crates/hy-check/src/freeze.rs.Settlement wallet (Polygon USDC):
0xC175c34c256598C7e1AD5B15503cFcED7Ed67e10
Dimank92 commentedon Oct 30, 2026
Resolved in #2328
Root cause:
In
freeze.rs,check_loop_backedgeonly checkedb.moved.is_some(). When an alias was created at the tail of a loop body (e.g.keep = nxt), that alias edge existed across the back-edge into subsequent iterations. When the value was later moved on iteration 2 (task wm(move t)),keepwas not frozen under the end-of-body alias graph, permitting concurrent data races (keep["race"] = i) without emittingHYB-E3003.Fix:
check_loop_backedge_replayinfreeze.rsto replay statement local definitions, assignments, task spawns, sends, andcheck_usesalong the loop body with outer scopes populated from the tail state whenever loop-carried alias/share/origin/move state is present.HYB-E3003upon writes/reads through frozen aliases on subsequent iterations.is_single_iteration_loopguard for statically single-iteration constant ranges (such as0..1) and trailing unconditional break/return so non-repeating loops remain accepted without false positives.compiler/crates/hy-check/tests/issue_2317_loop_carried_alias_freeze.rscovering task moves, channel sends, slice values, condition loops, if-tail aliases, helper functions, and controls.Payout Address:
0xC345C07229307d4f1c7935aD2cAe48e390215808fix(hy-check): freeze loop-carried tail aliases on back-edge moves (l…
Merge PR limitlessprojects#2328: freeze loop-carried tail aliases on …