Skip to content

[soundness] option[T] TypeShape erasure allows &option[chan T] / &option[atomic] borrows across tasks (HYB-E3001/E3103/E3104 data race) and send/recv/range on option[T] (HYB-E3007/E2008/E2009) #753

Open

Description

@aazminu

Summary

In hy-check/src/shape.rs, Checker::shape_of_type folds option[T] (TypeExpr::Generic { name: "option", args: [T] }) to shape_of_type(T). Several expression and container shape checks rely on shape_of_type or expr_is_option without preserving the outer option[...] wrapper:

  1. Indexed, Dereferenced, Projected, Channel-Received, select-Bound, for-Range-Bound, and Type-Aliased option[T] Operands:
    • expr_is_option does not handle Expr::Index (xs[0], m["k"]), Expr::Deref (*p), Expr::Field (h.opt), Expr::Paren, or Expr::Move, and checks TypeExpr syntactically without resolving type aliases (type opt_ch = option[chan int]).
    • ty_of_expr and multi_value_tys do not synthesize TypeExpr for Expr::Index, Expr::Deref, Expr::Receive (opt := <-ch, opt, ok := <-ch), or Expr::Field.
    • SelectRecv (select { opt := <-ch: ... }) and ForKind::Iterate (for _, opt in xs) pass None as ty to define_binding, losing the option[...] wrapper on the bound variable.
    • Reassigning opt = none overwrites a specific b.ty (such as option[option[chan int]]) with option[any], erasing nested option wrappers on subsequent match unwraps.
    • As a result, xs[0], m["k"], *p, h.opt, get_alias(), <-ch, select { opt := <-ch: ... }, and for _, opt in xs of type option[chan int] or opt_ch are treated as bare chan int (bypassing HYB-E3007 on <- and HYB-E2008 on for v in ...) and as Sendable (bypassing HYB-E3001 on &xs[0], p, &h.opt, &opt).
  2. Containers, Pointers, new(alias), and Structs Holding option[T]:
    • shape_of_type for Slice, Array, Map, Chan, and Pointer recursively calls shape_of_type(inner), turning []option[int] into Slice(Primitive) and *option[atomic.int64] (or new(opt_atomic)) into Pointer(Atomic) (which is Sendable!).
    • struct_is_sendable checks field shapes via shape_of_type(&f.ty) without checking whether f.ty is option[...], allowing a struct with an option[int] or option[chan int] field to be shared across task spawn without move (HYB-E3001 bypassed).

Minimal Reproducer

import "sync/atomic"

struct opt_box {
    v option[int]
}

fn worker_ptr(p *option[atomic.int64]) {}
fn worker_box(b opt_box) {}

fn main() {
    xs := []option[chan int]{some(make(chan int, 1))}
    xs[0] <- 1
    v := <-xs[0]

    var opt option[atomic.int64] = none
    task worker_ptr(&opt)

    b := opt_box{v: some(1)}
    task worker_box(b)
}

Expected Behavior

  • option[T] values obtained via indexing, dereferencing, field projection, channel receive, select receive arms, for range loops, or type aliases must not be usable as bare T in channel send/receive (HYB-E3007), for ... in channel range (HYB-E2008), or address-of/pointer/struct task spawn (HYB-E3001).

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