Skip to content

Soundness: atomic.pointer[T] and atomic.value classified as sendable Class-2 Atomic, contradicting GUARANTEES row 11 (HYB-E3005) #51

Closed

Description

@ocakciomursel-pixel

Summary

GUARANTEES.md row 11 explicitly specifies:

sync/atomic (atomic.Pointer[T], ...): [TBD] ... Until then: unproven → rejected.

However, in compiler/crates/hy-check/src/foreign.rs, foreign_named_type matches if name.starts_with("atomic.") { return Some(TypeShape::Atomic); } for all atomic.* types — including atomic.pointer[T] and atomic.value — and method_result returns TypeShape::Primitive for every atomic.* method call (p.load(), p.swap(...), etc.).

As a result, sharing &p (where p is atomic.pointer[holder] pointing to a struct containing a mutable map[string]int) or &v (atomic.value) across spawned tasks is accepted by hyb check with 0 diagnostics (OK), allowing two goroutines to load the same *holder and concurrently mutate h.data["k"] without synchronization (fatal error: concurrent map writes).

  • Tested commit (main): 8aaa231e947d87e163cb5836e798ee6a69b58fd0
  • Violated guarantees:
    • GUARANTEES.md Row 1 (No value reaches a spawned task or channel send through an unproven path) & Row 11 (sync/atomic (atomic.Pointer[T], ...) — Until then: unproven → rejected)
    • SPEC.md §O4.9 (Fail-closed concurrency boundary (HYB-E3005))

Minimal Reproducer (f5_atomic_pointer.hy)

import "sync/atomic"

struct holder {
    data map[string]int
}

fn worker(p *atomic.pointer[holder], done chan<- bool) {
    h := p.load()
    for i in 0..10000 {
        h.data["k"] = i
    }
    done <- true
}

fn main() {
    var p atomic.pointer[holder]
    shared := holder{data: map[string]int{"k": 0}}
    p.store(&shared)
    done := make(chan bool, 1)
    task worker(&p, done)
    for i in 0..10000 {
        shared.data["k"] = i
    }
    <-done
}

Variant 2 — atomic.value (f5b_atomic_value.hy)

import "sync/atomic"

fn worker(v *atomic.value, done chan<- bool) {
    done <- true
}

fn main() {
    var v atomic.value
    done := make(chan bool, 1)
    task worker(&v, done)
    <-done
}

Actual vs. Expected Behavior

Actual (main)

$ hyb check f5_atomic_pointer.hy
OK (0 diagnostics)

Running the emitted Go code on Go 1.27.0 immediately crashes:

fatal error: concurrent map writes

Expected (and behavior with the accompanying fix PR)

$ hyb check f5_atomic_pointer.hy
  error[HYB-E3005]: cannot prove `&p` (`*atomic.pointer[holder]`) is safe to send at the spawn `task worker(...)` (fail-closed, O4.9)
   --> f5_atomic_pointer.hy:20:17
    |
 20 |     task worker(&p, done)
    |                 ^^ sendability is not proven here
    = note: why: `atomic.pointer[holder]` can smuggle arbitrary non-sendable references across task boundaries; GUARANTEES.md row 11 marks `atomic.Pointer[T]` and `atomic.Value` as unproven -> rejected until T-sendability + linear store/swap discipline is specified

Root Cause

  1. compiler/crates/hy-check/src/foreign.rs: foreign_named_type checks name.starts_with("atomic.") and unconditionally returns Some(TypeShape::Atomic) (Class 2 sendable) instead of restricting Class 2 TypeShape::Atomic to scalar atomic types (atomic.int32, atomic.int64, atomic.uint32, atomic.uint64, atomic.uintptr, atomic.bool).
  2. compiler/crates/hy-check/src/foreign.rs: method_result returns Some(TypeShape::Primitive) whenever recv_type.starts_with("atomic."), treating p.load() on an atomic.pointer[T] as a primitive copy.

Activity

  1. added a commit that references this issue on Oct 14, 2026
    99da184
  2. added a commit that references this issue on Oct 17, 2026
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