Skip to content

Soundness: pointer-borrow share chains (&x in composites/calls, auto-addressed *T params) and O8 block-exit store-backs bypass move freeze (HYB-E3003 / HYB-E3004) #131

Closed

Description

@ocakciomursel-pixel

Summary

Under SPEC-LANGUAGE.md §9.3 O5 (O5.1 / O5.2, rule A1b) and §9.1 O8, move x at a task spawn or channel send must freeze the moved binding and every live alias or sharing peer, and O8 index/field loans (slot := &m[k], f := &h.field) lower to a copy-read plus a synthetic block-exit store-back (m[k] = slot, h.field = f) at the end of their declaring block.

Four interconnected pointer-borrow (&x) and O8 block-exit store-back seams in hy-check + hy-syntax currently allow programs to pass hyb check with OK (0 diagnostics) and crash at runtime (fatal error: concurrent map writes) or trigger go run -race data races:

  1. Pointer-borrow + composite-literal / may-share composition (p := &h; xs := []*holder{p}; task worker(move xs, done); h.m["k"] = i and direct xs := []*holder{&h}):
    • record_local_borrow_aliases (loans.rs) recorded p -> h only in st.aliases (not st.may_share), while mark_move and check_non_owned_move (freeze.rs) expanded st.aliases only once before st.share_group(name) and never took the transitive closure of aliases ∪ may_share. Moving xs therefore froze xs and p via may_share, but left the underlying pointee h unfrozen.
    • Furthermore, record_literal_ref and record_may_share had no arm for as_addr_of(...) (&h), so xs := []*holder{&h} recorded no sharing edge to h at all.
  2. Pointer-borrow argument passed to a call (p := id_ptr(&h); task worker(move p, done); h.m["k"] = i):
    • Expr::root_ident() (compiler/crates/hy-syntax/src/ast.rs:745-754) did not recurse through Expr::AddrOf or Expr::Unary { op: UnOp::Addr }, returning None on &h. Consequently, record_alias and record_may_share ignored &h arguments in Expr::Call, leaving p unlinked to h.
  3. Scalar-struct borrow via explicit &s or hy-emit auto-addressed pointer parameter (s := stats{count: 0}; p := id_ptr(s); task worker(move p, done); s.count = i):
    • hy-emit/src/expr.rs:626-647 automatically rewrites a value argument s to &s when the callee parameter is a pointer (fn id_ptr(p *stats) *stats). Meanwhile, record_alias and record_may_share checked b.shape.is_reference_alias() / binding_may_share(st, src) on s (TypeShape::Struct with scalar fields -> false), ignoring that taking &s (explicitly or via pointer-parameter auto-addressing) creates shared mutable *stats storage.
    • Similarly, record_local_borrow_aliases (p := &param) did not propagate param's non_owned_origin onto p, allowing task worker(move p, done) on a borrowed value parameter param to bypass HYB-E3004.
  4. O8 slot/field-loan block-exit store-back after move (if true { slot := &m["k"]; slot.n = 1; task worker(move m, start, done) }):
    • hy-emit/src/stmt.rs:221-235, 898-901 lowers slot := &m["k"] (and f := &h.field) to a copy-read slot := m["k"] plus a synthetic store-back m["k"] = slot emitted at the end of the declaring block — i.e., after go worker(m, start, done). hy-check ended the loan window at last_use["slot"] (slot.n = 1) and never checked whether m (or h) was moved before the declaring block exited, emitting an unsynchronized write m["k"] = slot to the moved map concurrently with worker.
  • Tested commit (main): f99d6d0
  • Violated guarantees:
    • GUARANTEES.md Row 1 (No value reaches a spawned task or channel send through an unproven path) & Row 6 (move / freeze: a moved value cannot be used by the sender afterwards)
    • SPEC-LANGUAGE.md §9.3 O5.1 & O5.2 (HYB-E3003 / HYB-E3004, rule A1b) and §9.1 O8

Minimal Reproducers

Reproducer 1 — Pointer borrow embedded in composite literal (repro1_ptr_borrow_composite.hy)

struct holder {
    m map[string]int
}

fn worker(xs []*holder, done chan<- bool) {
    for i in 0..10000 {
        xs[0].m["k"] = i
    }
    done <- true
}

fn main() {
    h := holder{m: map[string]int{"k": 0}}
    p := &h
    xs := []*holder{p}
    done := make(chan bool, 1)
    task worker(move xs, done)
    for i in 0..10000 {
        h.m["k"] = i
    }
    <-done
}

Reproducer 2 — Pointer borrow passed through a call (repro2_ptr_borrow_call.hy)

struct holder {
    m map[string]int
}

fn id_ptr(p *holder) *holder {
    return move p
}

fn worker(p *holder, done chan<- bool) {
    for i in 0..10000 {
        p.m["k"] = i
    }
    done <- true
}

fn main() {
    h := holder{m: map[string]int{"k": 0}}
    p := id_ptr(&h)
    done := make(chan bool, 1)
    task worker(move p, done)
    for i in 0..10000 {
        h.m["k"] = i
    }
    <-done
}

Reproducer 3 — Auto-addressed pointer parameter on a scalar struct (repro3_auto_addr_ptr_param.hy)

struct stats {
    count int
}

fn id_ptr(p *stats) *stats {
    return move p
}

fn worker(p *stats, done chan<- bool) {
    for i in 0..10000 {
        p.count = i
    }
    done <- true
}

fn main() {
    s := stats{count: 0}
    p := id_ptr(s)
    done := make(chan bool, 1)
    task worker(move p, done)
    for i in 0..10000 {
        s.count = i
    }
    <-done
}

Reproducer 4 — O8 map-slot loan block-exit store-back after move m (repro4_slot_storeback_after_move.hy)

struct item {
    n int
}

fn worker(m map[string]item, start <-chan bool, done chan<- bool) {
    <-start
    for i in 0..10000 {
        m["k"] = item{n: i}
    }
    done <- true
}

fn main() {
    m := map[string]item{"k": item{n: 0}}
    start := make(chan bool, 1)
    done := make(chan bool, 1)
    if true {
        slot := &m["k"]
        slot.n = 1
        task worker(move m, start, done)
        start <- true
        for i in 0..10000 {
            slot.n = i
        }
    }
    <-done
}

Actual vs. Expected Behavior

Actual (main @ f99d6d0)

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

$ hyb run repro1_ptr_borrow_composite.hy
fatal error: concurrent map writes

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

$ hyb run repro2_ptr_borrow_call.hy
fatal error: concurrent map writes

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

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

(In repro4, hyb build --keep-go emits m["k"] = slot at the end of the if true block after go worker(m, start, done), racing with worker's map writes.)

Expected

All four programs must be rejected by hyb check with HYB-E3003 (tier0_rule: "A1b"), and moving a pointer borrow of a non-owned parameter (p := &param; task worker(move p, done)) must be rejected with HYB-E3004 (tier0_rule: "A1b").

Activity

  1. gelismarvel commented on Oct 17, 2026

    @gelismarvel
    Contributor

    /attempt #131

    Implemented and verified the fix for #131 across hy-syntax (Expr::root_ident) and hy-check (freeze.rs, shape.rs, loans.rs), with 7 integration tests in compiler/crates/hy-check/tests/pointer_borrow_chains_issue_131.rs (7/7 PASS, 25/25 pointer_borrow_composites PASS, 129/129 hy-check --lib PASS).

  2. ocakciomursel-pixel commented on Oct 17, 2026

    @ocakciomursel-pixel
    ContributorAuthor

    Note for maintainers: As the original reporter of #131 (filed 2026-10-15), companion fix PR #132 (#132) was already triaged and queued in Wave 1/Wave 2 with bounty standing preserved, and is rebased cleanly on latest main (compiler + quick green).

  3. added a commit that references this issue on Oct 17, 2026
    721eef7
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