Skip to content

[soundness] User-defined method calls, struct-to-reference extractions, and ?-unwrapped calls bypass A1b freeze #120

Closed

Description

@sonson0910

Summary

In hy-check (compiler/crates/hy-check/src/lib.rs, shape.rs, and loans.rs), user-defined methods (fn (r T) method(...)) are discarded during table construction and method receivers (callee = Expr::Field { base, .. }) / reference-bearing composite arguments (holder{m: map[string]int}) / ?-unwrapped calls (Expr::Try) are ignored by record_may_share and record_alias.

As a result, extracting or returning a reference-bearing value via a user-defined method (m = h.data(), var m map[string]int = h.data(), n, m := h.pair(), m := h.try_data()?) or via a top-level function taking a reference-bearing struct (m := extract(h)) leaves zero may_share or aliases edge between m and h (and fails to propagate non_owned_origin when h is a method receiver or function parameter). Moving m into a spawned task (task worker(move m, done)) and concurrently mutating h.m in the parent task is accepted by hyb check with 0 diagnostics and 0 managed blocks, producing an immediate Go -race data race (exit status 66).


Minimal Reproducer (method_repro.hy, 26 lines, 0 managed blocks)

import "fmt"
import "time"

struct holder {
    m map[string]int
}

fn (h holder) data() map[string]int {
    return h.m
}

fn worker(m map[string]int, done chan<- bool) {
    m["worker"] = 1
    done <- true
}

fn main() {
    h := holder{m: map[string]int{"init": 0}}
    m := map[string]int{}
    m = h.data()
    done := make(chan bool, 1)
    task worker(move m, done)
    h.m["main"] = 2
    <-done
    time.sleep(10 * time.millisecond)
    fmt.println(h.m["worker"], h.m["main"])
}

Additional Bypass Variants

  1. Ascribed define from user-defined method (var m map[string]int = h.data()):
    • h.data() resolves to TypeShape::Unknown because build_tables discarded fn (h holder) data(), and the var m map[string]int ascription supplies TypeShape::Map(Str, Primitive).
    • Neither record_may_share nor record_alias inspects the method receiver h inside callee = Expr::Field { base: h, name: "data" }.
  2. Top-level function extracting a map/slice from a struct (m := extract(h)):
    • fn extract(h holder) map[string]int { return h.m }
    • In record_alias (loans.rs line 530), the argument loop checks if b.shape.is_reference_alias(), which is false for h (TypeShape::Struct(Some("holder"))), even though self.binding_may_share(st, "h") is true!
  3. Non-owned receiver/parameter extraction (fn (h holder) leak(...) { m := h.data(); task worker(move m, done) }):
    • Because m is not linked to h and does not inherit h.non_owned_origin, move m bypasses HYB-E3004 (check_non_owned_move) and moves caller-owned storage across a task boundary.
  4. ?-unwrapped calls (m := h.try_data()? / m2 := same_ok(m1)?) and multi-return method calls (n, m := h.pair()):
    • record_may_share and record_alias only peeled Expr::Paren (unwrap_parens) and not Expr::Try (?), so any ?-unwrapped call hit _ => {} with zero alias/may-share tracking.

Proof of Acceptance & Go -race Failure on main (651dc42)

1. hyb check accepts with 0 diagnostics

$ hyb check method_repro.hy
OK (0 diagnostics)

2. go run -race reports a fatal data race (exit status 66)

$ HYB_ALLOW_TOOLCHAIN_DRIFT=1 CC=gcc CGO_ENABLED=1 go run -race main.go
==================
WARNING: DATA RACE
Write at 0x00c0000741b0 by goroutine 7:
  runtime.mapassign_faststr()
      /usr/local/go/src/runtime/map_faststr.go:223 +0x0
  main.worker()
      main.go:19 +0x50
  main.main.gowrap1()
      main.go:29 +0x58

Previous write at 0x00c0000741b0 by main goroutine:
  runtime.mapassign_faststr()
      /usr/local/go/src/runtime/map_faststr.go:223 +0x0
  main.main()
      main.go:30 +0x1d8

Goroutine 7 (running) created at:
  main.main()
      main.go:29 +0x1b8
==================
1 2
Found 2 data race(s)
exit status 66

Root Cause in hy-check

  1. build_tables (compiler/crates/hy-check/src/lib.rs, line 170) discards all user-defined methods:
    • if f.receiver.is_none() { ck.fns.insert(f.name.clone(), f); } prevented methods from overwriting top-level functions in ck.fns, but never stored f.receiver.is_some() declarations in a method table.
    • Consequently, call_shape, ty_of_expr, multi_value_shapes, and multi_value_tys (shape.rs) could only look up foreign methods via foreign::method_result, resolving every user-defined method call to TypeShape::Unknown.
  2. record_may_share and record_alias (compiler/crates/hy-check/src/loans.rs) ignore method receivers, struct-to-reference arguments, multi-value unpacks, and Expr::Try (?):
    • record_alias only inspected args of Expr::Call (never the receiver base in callee = Expr::Field { base, .. }) and required b.shape.is_reference_alias() on the source binding (rejecting Struct/Array bindings with binding_may_share == true).
    • record_may_share had no Expr::Call arm and was not called in the multi-value define/assign branches (l.values.len() == 1 && l.names.len() > 1), and neither function unwrapped Expr::Try (?).

Expected Behavior

hyb check method_repro.hy must reject the program with HYB-E3003 (tier0_rule: "A1b"):

error[HYB-E3003]: write through a live alias of a moved value
  --> method_repro.hy:23:9

Payout / Wallet Addresses (Bounty Track #5)

  • USDT (TRC20): TCQwxbpYVdRWYNDs1JbnKyVHNRVaRJg1io
  • USDT / USDC (ERC20 / BEP20): 0x76d1c4283a2ff662ccf85c1f57655c29b4256756
  • Self-Custody EVM Wallet (ETH / USDC / USDT / any EVM chain or token): 0x6Cf7Fc00857f2bbd12fBF2E11BC5AacEF0e97985
  • USDC (Solana): 6dX8oqvvHj6Q936PVyTjVYKVk45soNpJ234aVqMm6ZJm

Activity

  1. sonson0910 commented on Oct 15, 2026

    @sonson0910
    ContributorAuthor

    Update: Rebased the fix onto main (c09e992 / pr-162) and opened replacement PR #197 (#197), since PR #121 was accidentally auto-closed by GitHub when commit 6344d496 (Merge pr-122) included fixes #121 in its commit message.

  2. limitlessprojects commented on Oct 18, 2026

    @limitlessprojects
    Owner

    Verified against main @ 0706813 — the full scenario set from this report (shared with PR #197's test block) now rejects with the exact expected codes:

    Scenario Main behavior
    user-defined method receiver extraction (obj.field callee) HYB-E3003 (A1b)
    ascribed define through method value HYB-E3003 (A1b)
    multi-return pair through method call HYB-E3003 (A1b)
    ?-unwrapped call sharing HYB-E3003 (A1b)
    top-level fn extraction HYB-E3003 (A1b)
    non-owned receiver leak HYB-E3004 (A1b)

    Method/receiver tracking, struct-to-reference extraction edges, and ?-unwrapped call sharing are all populated into record_may_share/record_alias by successor landings. Note: PR #197's own model.rs hunk was stale (it deletes the collect_ref_roots FnLit arm the current machinery needs) and was credit-closed rather than merged — the behavior this issue asks for is enforced on main.

    Closing as fixed — thanks @sonson0910 (who also authored much of the successor work).

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