Skip to content

[Soundness] call_shape, call_result_tys, ty_of_expr, and multi_value_tys drop return shapes and TypeExprs on function-value calls and container projections, bypassing HYB-E3003 / HYB-E3004 #1042

Open

Description

@michellehumes

Track

Track 1 — Soundness Hole (#5)

Summary

In compiler/crates/hy-check/src/shape.rs, four shape/type inference helpers (call_shape, call_result_tys, ty_of_expr, and multi_value_tys) silently drop return shapes (TypeShape::Unknown) or source types (None) across indirect function-value calls and container projections:

  1. call_shape (shape.rs:321-330, 446-474) & call_result_tys (shape.rs:1104-1151):

    • call_shape's Expr::Ident arm matches &b.ty directly against Some(TypeExpr::Fn { .. }) without calling self.resolve_type_alias(ty) (unlike ty_of_expr and call_result_tys), so calling a local binding ascribed with a function type alias (type getter = fn(x map[string]int) map[string]int) degrades to TypeShape::Unknown.
    • Neither call_shape nor call_result_tys falls back to self.ty_of_expr(st, callee) -> self.resolve_type_alias(&callee_ty) -> TypeExpr::Fn { results, .. } in the Expr::Field and _ arms (unlike ty_of_expr lines 782-795 and sendability.rs::dir_ty_of_expr lines 3038-3054). Consequently, calling a struct field of function type (h.cb(m) or p.cb(m)), an indexed function table (cbs[0](m)), or a channel-received function value degrades all return shapes to TypeShape::Unknown.
  2. ty_of_expr (shape.rs:584-817) & multi_value_tys (shape.rs:1204-1239):

    • Unlike sendability.rs::dir_ty_of_expr, ty_of_expr has no arms for Expr::Slice (s[:]), Expr::Receive (<-ch), Expr::AddrOf / Expr::Unary { op: UnOp::Addr } (&box), "append" (append(s, f)), or "make" on a type-alias identifier (make(fn_map)), and Expr::Field does not fall back to self.shape_of_expr(st, base) when base is a pointer/struct whose TypeShape is known.
    • multi_value_tys matches self.ty_of_expr(st, base) against Some(TypeExpr::Map { .. }) without self.resolve_type_alias(&base_ty) and has no Expr::Receive arm for v, ok := <-ch.

Why This Causes a Soundness Bypass (HYB-E3003 & HYB-E3004)

Because shape_may_share(&TypeShape::Unknown) is false (shape.rs:112), when alias := g(m), alias := h.cb(m), alias := sub2[0](m), or alias, ok := p.cb(m) degrades alias's shape to TypeShape::Unknown:

  • Copying copy_of_alias := alias records no may_share edge from alias to copy_of_alias (loans.rs:1577). Moving m into a concurrent task (task worker(move m, done)) freezes m and alias, while leaving copy_of_alias completely unfrozen to mutate the same underlying map concurrently (HYB-E3003 bypass -> concurrent map write race).
  • Reassigning escaped = alias (where var escaped map[string]int has a declared Map shape) skips non_owned_origin propagation when alias's shape is Unknown, allowing a borrowed parameter map to be moved into a task via task worker(move escaped, done) (HYB-E3004 bypass).

Minimal Reproducer

package main

struct holder {
    cb fn(x map[string]int) map[string]int
}

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

fn main() {
    m := map[string]int{"a": 1}
    h := holder{
        cb: fn(x map[string]int) map[string]int {
            return x
        },
    }
    alias := h.cb(m)
    copy_of_alias := alias
    done := make(chan bool, 1)
    task worker(move m, done)
    copy_of_alias["k"] = 99
    <-done
}
  • Expected: Verdict::Reject (HYB-E3003 on copy_of_alias["k"] = 99 after move m).
  • Actual (on main): Verdict::Accept with 0 diagnostics; concurrent map write race at runtime.

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