You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
[soundness] builtin copy(dst, src) shares reference elements but records no may-share edge — task boundary races with 0 diagnostics #178
The builtin copy(dst, src) is not modelled by the checker at all (grep '"copy"' compiler/crates/ is empty). For a slice whose element
is a reference type ([]map[...], [][]..., []*T, or a struct/array carrying one), copy is element-wise: it duplicates the element references, so dst[i] and src[i] end up aliasing the exact same map/slice. The checker records the may-share / alias edge only on
define/rebind statements (record_may_share / record_alias, compiler/crates/hy-check/src/loans.rs), never for a copy(...) call
statement, so a later move dst does not freeze src. A pure Tier-0 .hy program (no managed, let, const, new) therefore passes hyb check with OK (0 diagnostics) and then races (fatal error: concurrent map writes) under go run -race.
appendis modelled — dst := append([]map[string]int{}, src...) on reference elements is rejected via HYB-E3003. copy is the gap.
Minimal reproducer (repro_copy_race.hy)
fn worker(d []map[string]int, done chan<- bool) {
for i in0..100000 {
d[0]["k"] = i
}
done <- true
}
fn main() {
src := []map[string]int{map[string]int{"k": 0}}
dst := make([]map[string]int, 1)
copy(dst, src) // dst[0] and src[0] are the SAME map
done := make(chan bool, 1)
task worker(move dst, done) // src is NOT frozen
for i in0..100000 {
src[0]["k"] = i // concurrent map write
}
<-done
}
Observed
$ hyb check repro_copy_race.hy
repro_copy_race.hy: OK (0 diagnostics)
The emitted Go, run under the race detector:
$ go run -race a.out.go
==================
WARNING: DATA RACE
Write at 0x00c00009e000 by goroutine 7:
runtime.mapassign_faststr()
main.worker()
a.out.go:4
...
Previous write at 0x00c00009e000 by main goroutine:
main.main()
a.out.go:16
==================
fatal error: concurrent map writes
Expected
HYB-E3003 (freeze-after-send / O5.2): copy(dst, src) shares mutable GC storage between dst and src, so moving dst must freeze src.
Root cause
record_may_share (loans.rs:883) and record_alias (loans.rs:974) are only reached from the define/rebind handlers. A copy(dst, src)
statement is an Stmt::Expr call; process_call (loans.rs:136) resolves it to Resolved::Fn { name: "copy" } but has no arm for it, so no
edge is ever recorded. There is no copy entry anywhere in hy-check.
Proposed fix
In process_call's Resolved::Fn arm, when name == "copy" with two slice arguments, record a may-share edge between the roots of the two
arguments only when the destination's element shape may share (shape_may_share(elem)), so a []int copy (by-value elements, shares
nothing) stays accepted. A move of either binding then freezes the other through the existing may-share component (freeze.rs).
Activity
Merge pr-180 (Ratanlavate, fixes issue #178): the builtin copy(dst, s…
fix(hy-check): record a may-share edge for the builtin copy (fixes li…