Skip to content

[soundness] Helper-frame out-param store leaves a caller alias unfrozen: accepted program races on the moved map #455

Closed

Description

@ApelKristian

Severity: P0 — accepted-but-unsound program (data race reachable in the checked Tier-0 subset).

The smallest .hy program that demonstrates it

// callee_store_share.hy — commit e2a5f7d5eb90becb210df274765adfa50c99b21b (main, 2026-10-17)
// hyb check callee_store_share.hy   → OK (0 diagnostics)
// hyb build callee_store_share.hy -o callee_store_share && ./callee_store_share
//   → fatal error: concurrent map writes (5/5 runs, exit 2)

fn set(dst []map[string]int, v map[string]int) {
    dst[0] = v
}

fn worker(m map[string]int) {
    for i in 1..=200000 {
        m["task"]++
    }
}

fn main() {
    shared := map[string]int{}
    dst := []map[string]int{map[string]int{}}
    set(dst, shared)
    task worker(move shared)
    for i in 1..=200000 {
        dst[0]["main"]++
    }
}

Environment: Linux x86_64, hyb built from e2a5f7d5eb90becb210df274765adfa50c99b21b
with cargo build --release (Rust pinned 1.93.1), emitted Go built with go1.26.8.

What the checker should say

HYB-E3003 at the parent's dst[0]["main"]++: the helper's assignment dst[0] = v makes
dst[0] a live alias of shared in the caller, so after task worker(move shared) the
parent may not keep writing through it (O5.2, "every live alias … is frozen").

The same program with the store written directly in main is correctly rejected:

dst[0] = shared        // in main
task worker(move shared)
dst[0]["main"]++       // HYB-E3003: write through a live alias of a moved value

What it actually said

$ hyb check callee_store_share.hy
callee_store_share.hy: OK (0 diagnostics)

At run time dst[0] and shared are the same map after the call; the two tasks write it
concurrently and the Go runtime reports fatal error: concurrent map writes in 5/5
runs (exit 2). Two controls with the same binary: the direct-store form rejects with
HYB-E3003; a helper taking the same two arguments but not assigning ({ _ = v }) is
accepted — so the store inside the helper frame is the relevant difference.

Anything else

Rule: O5.2 (freeze-after-move); GUARANTEES row 6 ("a moved value cannot be used by the
sender afterwards"). The program is pure Tier-0: no managed, const, pointers, closures,
imports, or foreign calls.

Mechanism: FnState.may_share is populated from the analysed function's own assignments
(loans.rs::process_assign); the Resolved::Fn branch of process_call records
copy/delete/const effects and A3 loan events across a callee (check_a3_call_conflict,
#111/#117), but has no may-share effect for "callee stores parameter j into a place rooted
at parameter i". The call site therefore records no edge between the caller's dst and
shared, and mark_move cannot freeze the helper-created alias.

Related, not duplicate (checked against the open+closed issue/PR texts): #92/#93 and
#296/#398 are the same-function field/index store gap (the direct-store control here now
rejects); #73 and #120 are call-result aliasing (the value must be returned and bound);
#111/#117 are the A3 loan-window precedent across a callee frame; #131 covers pointer
borrows; #75/#410 cover moving a may-share peer of a parameter. None describes a helper
store that retains one argument through another.

First report of this mechanism. Credit: the GitHub account filing this issue.

Activity

  1. added a commit that references this issue on Oct 17, 2026
  2. dezzy-music commented on Oct 17, 2026

    @dezzy-music
    Contributor

    I'm on it. Senior systems & compiler engineer here — reproduced the helper-frame out-param store race locally. Working on the interprocedural parameter store aliasing tracking in \process_call\ right now. Will deliver the complete fix and regression test today.

  3. dezzy-music commented on Oct 17, 2026

    @dezzy-music
    Contributor

    Submitted verified fix in PR #470.

    Cause

    In loans.rs, process_call handled copy, delete, and A3 loan conflicts, but did not analyze interprocedural interior stores between parameters (e.g. dst[0] = v). Consequently, st.may_share never recorded an edge between caller arguments when a callee stored one parameter into another. When shared was moved, mark_move traversed only existing share group peers, leaving dst unfrozen in the caller frame and enabling concurrent writes at runtime.

    Fix

    1. In compiler/crates/hy-check/src/loans.rs:
      • Added bounded interprocedural analysis fn_stores_param_into_param traversing statement bodies, block scopes, local aliases, and nested callee frames to identify interior stores between parameters (t.root() == dst_param && !t.is_ident(dst_param) where RHS reaches src_param).
      • Added record_callee_stores_may_share and record_method_stores_may_share to establish bidirectional st.share_edge(d, s) between caller argument roots.
    2. In compiler/crates/hy-check/src/lib.rs:
      • Added regression test helper_frame_out_param_store_freezes_caller_alias_issue_455 testing both the P0 reproducer (rejects HYB-E3003) and a positive control (no_store helper without interior assignment, accepts cleanly with 0 diagnostics).
      • Entire test suite passes without regressions.
  4. gelismarvel commented on Oct 17, 2026

    @gelismarvel
    Contributor

    Fixes #455

    Summary

    When a helper function or user-defined method stores a reference-bearing parameter or receiver (or a value derived from it) into an indexed element or field of another parameter/receiver — or copies reference-bearing slice elements via copy(dst, src) — the caller's destination and source bindings share mutable GC storage after the call returns. Previously, hy-check only recorded may_share edges for in-frame assignments (dst[0] = shared), copy(dst, src) directly in the caller frame, and return-value projections, leaving caller-side out-parameters and receiver containers unlinked from the stored argument.

    Root Cause & Soundness Coverage

    1. Top-level function & user-defined struct method out-param / receiver stores (loans.rs::record_callee_param_stores):
      • Analyzes helper functions (self.fns) and user-defined struct methods (self.method_decls, unifying the receiver slot and parameter slots) up to bounded interprocedural depth (depth <= 4).
      • Tracks direct element/field stores (dst[0] = v, dst["k"] = v, h.m = v, b.items[0] = v, dst[0] = h.m), local alias and container chains inside the helper (tmp := dst; val := v; tmp[0] = val, multi-value unpacks, branch/loop/match/select paths), built-in copy(dst, src) on reference-element slices inside the helper, and transitive helper-to-helper calls (outer_set(dst, v) -> inner_set(dst, v)).
      • Guards value and target shapes via shape_may_share so primitive value copies across helpers (dst["k"] = src["k"] on map[string]int or copy(dst, src) on []int) do not introduce false-positive may_share edges.
    2. Caller-side may_share & non_owned_origin / const propagation:
      • Links caller destination roots and source roots via st.share_edge(dst_root, src_root) and propagates non_owned_origin so:
        • Moving dst freezes src and vice versa (HYB-E3003, O5.2).
        • Moving a local container dst after storing a non-owned caller parameter/capture p into it via set(dst, p) is rejected with HYB-E3004 (O5.1 / O5.4).
        • Mutating a const composite through an out-parameter container populated via set(dst, const_m) is rejected with HYB-E3006 (T22).
    3. Closure-body parity (closures.rs::check_stmt_boundaries & loans.rs::scan_block_const_writes):
      • Invokes record_callee_param_stores inside closure bodies as well as the top-level Pass B walk so helper-frame out-parameter stores inside closures obey the same freeze and immutability rules.

    Verification

  5. added a commit that references this issue on Oct 17, 2026
    f0a8f7f
  6. dezzy-music commented on Oct 17, 2026

    @dezzy-music
    Contributor

    Hey maintainer! Senior systems & compiler engineer here. I've already resolved this helper-frame out-param store race condition in PR #470 with clean test suites passing 100% green. If you have any specific requirements, extra test cases, or adjustments you'd like made, I'm online right now and can turn them around in 20 minutes or less (maximum 1 hour). Ready to get this merged today!

    Payout details:

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