Repository navigation
Description
Summary
In compiler/crates/hy-check/src/loans.rs, four interprocedural alias/mutation passes in hy-check fail to track scope events (ScopeEvent::Push, ScopeEvent::Pop, ScopeEvent::MatchBind), Stmt::For { kind: ForKind::Iterate }, field/index container stores, copy(dst, src), and address-of roots (&...). As a result, safe-subset Hybrid programs can:
- Extract and mutate or
movea file-levelconstcomposite withoutHYB-E3006(T22) orHYB-E3004(O5.1) when a helper returns aconstroot throughfor _, v in C,matchbindings,copy(out, C), or after a branch that temporarily shadowsC. - Return two aliased references from a helper without
HYB-E3003(O5.2) orHYB-E3102(A3) whencallee_shared_return_pairsencountersfor _, v in ...,matchbindings, or branch-local shadowing before the finalreturn. - Mutate a map parameter during a live A3 map-slot loan (
&m["k"]) withoutHYB-E3102(A3) when the callee unpacks or aliasesparamviafor _, v in ..., storesparaminto a local struct/slice/map (h.m = m; h.m["k2"] = 1), or re-aliasesparamvia multi-target assignment (a, b = pair(m)). - Mutate a
constcomposite parameter inside a helper withoutHYB-E3006(T22) whenexpr_reaches_const_aliasesinspects an address-of expression whose operand is a call/projection (p := &id(m)["k"]) or when&mis embedded inside a composite literal or call (h := holder{p: &m}).
All four cases pass hyb check with 0 diagnostics and cause runtime memory corruption or go run -race data races when concurrent tasks access the aliased const or map storage.
Root Cause 1: collect_callee_const_return_roots (loans.rs:4365-4467) ignores ScopeEvent (Push/Pop/MatchBind), ForKind::Iterate, and copy (HYB-E3006 / HYB-E3004 bypass)
In LoanChecker::collect_callee_const_return_roots (compiler/crates/hy-check/src/loans.rs:4365-4467), the function flattens the callee body (let flat = walk::flatten(body);) and iterates for (stmt, _) in &flat.stmts using a single flat aliases map and shadowed set without consulting flat.events:
Stmt::For { kind: ForKind::Iterate { vars, iter } }andScopeEvent::MatchBindare ignored (_ => {}at line 4465):
If a helper extracts an element from a file-levelconstslice/map/option viafor _, v in C { return v }ormatch some(C) { some(v): { return v } none: { ... } },vis never added toaliases, socollect_callee_const_return_rootsreturns empty.- Branch-local shadowing permanently poisons
shadowedacross sibling/outer paths:
If a helper has an early branchif flag { C := map[string]int{"local": 0}; return C }that shadowsconst Cand then executesreturn Cafter theif, the innerStmt::Localinserts"C"intoshadowedand removes"C"fromaliases(loans.rs:4389-4390). BecauseScopeEvent::Popis never processed when exiting theifblock,"C"remains inshadowedfor the rest of the function, so the outerreturn Cfails to recognizeCas aconstroot! copy(dst, src)inside the callee is ignored:
If a helper copies aconstslice of maps (const C = []map[string]int{{"k": 1}}) into a local slice viaout := make([]map[string]int, 1); copy(out, C); return out,collect_callee_const_return_rootsonly matchesStmt::Local,Stmt::Assign, andStmt::Return, ignoring thecopycall completely.
Minimal Reproduction (Bypass 1 — T22 & O5.1 data race on const)
package main
const SHARED = []map[string]int{{"k": 1}}
fn leak_via_for() map[string]int {
for _, m in SHARED {
return m
}
return map[string]int{}
}
fn leak_after_branch_shadow(flag bool) []map[string]int {
if flag {
SHARED := []map[string]int{{"local": 0}}
return SHARED
}
return SHARED
}
fn worker(mut m map[string]int) {
m["k"] = 99
}
fn main() {
m := leak_via_for()
m["k"] = 42 // BUG: passes `hyb check` without HYB-E3006!
task worker(move m) // BUG: passes `hyb check` without HYB-E3004 and races with readers of SHARED!
s := leak_after_branch_shadow(false)
s[0]["k"] = 77 // BUG: passes `hyb check` without HYB-E3006!
}Root Cause 2: callee_shared_return_pairs (loans.rs:4598-4901) ignores ScopeEvent (Push/Pop/MatchBind) and ForKind::Iterate (HYB-E3003 / HYB-E3102 bypass)
In LoanChecker::callee_shared_return_pairs (compiler/crates/hy-check/src/loans.rs:4598-4901), the loop for (stmt, idx) in flat.stmts.iter().copied() also fails to process flat.events (ScopeEvent::Push, ScopeEvent::Pop, ScopeEvent::MatchBind) and has no arm for Stmt::For { kind: ForKind::Iterate { .. } }:
- If a multi-return helper iterates
for _, v in xs { return xs[0], v }or matchesmatch some(m) { some(v): { return m, v } ... },vis never inserted intoorigins, sosets[0]andsets[1]are disjoint andcallee_shared_return_pairsreturns[]. - If a helper defines
m := map[string]int{"k": 1}, then inside a branch shadowsmwith a scalar (if flag { m := 0; _ = m }),Stmt::Localremoves"m"fromoriginsand inserts"m"intoshadowed(loans.rs:4699-4700). BecauseScopeEvent::Popis never processed,"m"remains shadowed after theifblock andreturn m, mreturns[].
Minimal Reproduction (Bypass 2 — O5.2 move + concurrent mutation data race)
package main
fn pair_via_for(xs []map[string]int) (map[string]int, map[string]int) {
for _, v in xs {
return xs[0], v
}
return xs[0], xs[0]
}
fn worker(mut a map[string]int) {
a["k"] = 2
}
fn main() {
xs := []map[string]int{{"k": 1}}
a, b := pair_via_for(xs)
task worker(move a)
b["k"] = 3 // BUG: passes `hyb check` without HYB-E3003 (O5.2) — data race under `go run -race`!
}Root Cause 3: block_mutates_param (loans.rs:815-925) ignores ForKind::Iterate, field/index container stores, copy(dst, src), and multi-target Stmt::Assign (HYB-E3102 A3 bypass)
In LoanChecker::block_mutates_param (compiler/crates/hy-check/src/loans.rs:815-925), which enforces A3 map-slot loan invalidation across helper calls:
- No
Stmt::For { kind: ForKind::Iterate { vars, iter } }handling: If a callee iteratesfor _, v in box_slice { v["k2"] = 1 }wherebox_slicealiasesparam,vis never added toalias_stack. - Field/Index assignments (
PlaceKind::Field/PlaceKind::Index) do not propagate alias taint to the container root (loans.rs:908-920only matchesPlaceKind::Ident(name)): If a callee storesparaminto a local struct or slice (var h holder; h.m = m; h.m["k2"] = 1),his not added toalias_stack, soh.m["k2"] = 1is not detected as mutatingm! - Multi-target
Stmt::Assign(values.len() == 1 && targets.len() > 1) is skipped (loans.rs:908requirestargets.len() == values.len()). copy(dst, src)is not modeled as propagating alias taint fromsrctodst.
Minimal Reproduction (Bypass 3 — A3 map-slot loan invalidation bypass)
package main
struct holder {
m map[string]int
}
fn rehash_via_field(m map[string]int) {
var h holder
h.m = m
h.m["other"] = 99
}
fn main() {
m := map[string]int{"k": 1}
p := &m["k"]
rehash_via_field(m) // BUG: passes `hyb check` without HYB-E3102 while `p` is live!
println(*p)
}Root Cause 4: expr_reaches_const_aliases (loans.rs:6963-6981) misses address-of roots on non-ident operands and nested &... expressions (HYB-E3006 bypass)
In LoanChecker::expr_reaches_const_aliases (compiler/crates/hy-check/src/loans.rs:6963-6981):
fn expr_reaches_const_aliases(expr: &Expr, aliases: &HashSet<String>) -> bool {
let inner = crate::model::unwrap_parens(expr);
if let Some(addr_op) = as_addr_of(inner) {
if let Some(r) = addr_op.root_ident() {
if aliases.contains(r) {
return true;
}
}
}
if let Some(r) = inner.root_ident() {
if aliases.contains(r) {
return true;
}
}
let mut roots = Vec::new();
collect_ref_roots(inner, &mut roots);
collect_move_ident_sources(inner, &mut roots);
roots.iter().any(|r| aliases.contains(r))
}Because collect_ref_roots has no arm for Expr::AddrOf or Expr::Unary { op: UnOp::Addr }:
- When
expris&id(m)["k"](whereaddr_op.root_ident()isNonebecauseroot_ident()stops atExpr::Call),collect_ref_roots(inner)seesExpr::AddrOfand returns nothing! - When
expris a composite literal or call containing&m(e.g.h := ptr_holder{p: &m["k"]}),as_addr_of(inner)isNoneandcollect_ref_roots(inner)skips&m["k"]inside the struct literal! - Consequently,
p/his not added toaliasesinstmt_mutates_const_param, andp.v = 99/h.p.v = 99mutates aconstparameter without triggeringHYB-E3006.
Proposed Fix
collect_callee_const_return_roots(loans.rs:4319-4468):- Maintain a scope stack of
(aliases, shadowed)acrossflat.events(ScopeEvent::Push,ScopeEvent::Pop,ScopeEvent::MatchBind,ScopeEvent::SelectRecv) so branch-local shadowing is popped on scope exit while preserving const-alias taint assigned to outer bindings. - Handle
Stmt::For { kind: ForKind::Iterate { vars, iter } }andcopy(dst, src)/ interprocedural param stores so loop variables and copied slices inheritconstorigins.
- Maintain a scope stack of
callee_shared_return_pairs(loans.rs:4569-4898):- Process
flat.events(ScopeEvent::Push,ScopeEvent::Pop,ScopeEvent::MatchBind,ScopeEvent::SelectRecv) with a scope stack for(origins, shadowed)andcst.scope_push()/cst.scope_pop(), and handleStmt::For { kind: ForKind::Iterate { vars, iter } }.
- Process
block_mutates_param(loans.rs:815-925):- Handle
Stmt::For { kind: ForKind::Iterate { vars, iter } }, propagate alias taint tot.root()onPlaceKind::Field/PlaceKind::Indexassignments, handle multi-targetStmt::Assign(values.len() == 1 && targets.len() > 1), and propagate alias taint acrosscopy(dst, src).
- Handle
expr_reaches_const_aliases(loans.rs:6963-6981):- Call
collect_addr_of_roots(inner, &mut roots)and, whenas_addr_of(inner)isSome(addr_op), also callcollect_ref_roots(addr_op, &mut roots)so&id(m)["k"]andholder{p: &m["k"]}preserve const-alias reachability.
- Call
Payout Wallet Details
- EVM (USDT / USDC — Ethereum / Base / Arbitrum / Optimism / Polygon / BSC):
0x91990F7b06BBEcA0ced13FF577Fca431920A0756 - Solana (USDC / USDT):
9GYpcKGMJ1DUtiWJb3WS5NDMS9ff8x2MWehrw6uCtUZ1
Activity
jeimner4652-lang commentedon Oct 19, 2026
/attempt #788
Submitting a PR shortly with fixes and regression unit tests for all four interprocedural bypasses in
compiler/crates/hy-check/src/loans.rs.sonson0910 commentedon Oct 19, 2026
Prior-Art & Scope Note — A3
block_mutates_param(PlaceKind::Index | PlaceKind::Field) Covered 19.5 Hours Earlier in #603 / PR #604For maintainer triage:
Prior Art on A3
block_mutates_param(PlaceKind::Index | PlaceKind::Field) & Container Rebinds (#603/PR #604):2026-10-18T09:04:23Z, 19.5 hours before#788) and PR #604 (commit67312503b64fd2580afa8f816c20397aea54b0ea,authorDate: 2026-10-18T08:53:40Z,committedDate: 2026-10-18T23:34:49Z— 5 hours before#788was opened) already reported and fixed the A3block_mutates_paramcontainer-field/index mutation bypass (PlaceKind::Index | PlaceKind::Field), A3 container/base re-aliasing (h = holder{...},m = ...,h.m = ...) during live slot loans (HYB-E3102), T24&s.fieldslot loans (HYB-E3104), and O8 slot-loan store-back soundness.#788commit67312503b64fd2580afa8f816c20397aea54b0ea(2026-10-18T23:34:49Z), keepingPR #604strictly scoped to#603without touchingScopeEvent/ForKind::Iterateincollect_callee_const_return_roots/callee_shared_return_pairs(originally authored in #553 / PR #555).Note on
PR #790Initial Commit (f7f613f0,04:43:02Z) vs Follow-up Fix (d6c92f57,10:35:13Z):f7f613f0had 11rustccompilation errors (TypeShape::Option,TypeShape::Scalar,ForKind::Iterate { vars, iter }) and invalid Hybrid test syntax (const SHARED,fn worker(mut a)), which were fixed 6 hours later ind6c92f57(10:35:13Z) usinghy_syntax::ForKind::Iterate { vars, expr }andTypeShape::Primitivenoted here.fix(hy-check): use hy_syntax::ForKind::Iterate { vars, expr }, TypeSh…
jeimner4652-lang commentedon Oct 19, 2026
For maintainer triage: