Skip to content

[soundness] HYB-E3003 bypass: an alias made at the loop tail is not frozen when the value it points to is moved on the next iteration #2317

Closed

Description

@mrglebx

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 with HYB-E3003 at 16:5, with the note "keep aliases t" (O5.2).
  • Unrolling the two iterations into two { } blocks with the same five statements each is rejected with HYB-E3003 at the second block's write and rebind (24:9 and 25:9).
  • Moving the alias to the top of the loop body (keep = nxt before t := nxt) is rejected with HYB-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 of task
  • 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 if at 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)), and main keeps writing m after 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:

Activity

  1. jpereyra243-byte commented on Oct 30, 2026

    @jpereyra243-byte
    Contributor

    /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

  2. Dimank92 commented on Oct 30, 2026

    @Dimank92
    Contributor

    Resolved in #2328

    Root cause:
    In freeze.rs, check_loop_backedge only checked b.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)), keep was not frozen under the end-of-body alias graph, permitting concurrent data races (keep["race"] = i) without emitting HYB-E3003.

    Fix:

    • Added check_loop_backedge_replay in freeze.rs to replay statement local definitions, assignments, task spawns, sends, and check_uses along the loop body with outer scopes populated from the tail state whenever loop-carried alias/share/origin/move state is present.
    • Correctly marks moves and freezes all live aliases, emitting HYB-E3003 upon writes/reads through frozen aliases on subsequent iterations.
    • Added is_single_iteration_loop guard for statically single-iteration constant ranges (such as 0..1) and trailing unconditional break/return so non-repeating loops remain accepted without false positives.
    • Added full regression test suite in compiler/crates/hy-check/tests/issue_2317_loop_carried_alias_freeze.rs covering task moves, channel sends, slice values, condition loops, if-tail aliases, helper functions, and controls.

    Payout Address:
    0xC345C07229307d4f1c7935aD2cAe48e390215808

  3. added a commit that references this issue on Oct 30, 2026
    b62d1a2
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions