Skip to content

Commit f2d0538

Browse files
committed
runtime: perform write barriers on direct channel receive
Currently we have write barriers for direct channel sends, where the receiver is blocked and the sender is writing directly to the receiver's stack; but not for direct channel receives, where the sender is blocked and the receiver is reading directly from the sender's stack. This was okay with the old write barrier because either 1) the receiver would write the received pointer into the heap (causing it to be shaded), 2) the pointer would still be on the receiver's stack at mark termination and we would rescan it, or 3) the receiver dropped the pointer so it wasn't necessarily reachable anyway. This is not okay with the write barrier because it lets a grey stack send a white pointer to a black stack and then remove it from its own stack. If the grey stack was the sole grey-protector of this pointer, this hides the object from the garbage collector. Fix this by making direct receives perform a stack-to-stack write barrier just like direct sends do. Fixes #17694. Change-Id: I1a4cb904e4138d2ac22f96a3e986635534a5ae41 Reviewed-on: https://go-review.googlesource.com/32450 Reviewed-by: Rick Hudson <[email protected]>
1 parent 9a8bf2d commit f2d0538

File tree

1 file changed

+21
-12
lines changed

1 file changed

+21
-12
lines changed

src/runtime/chan.go

Lines changed: 21 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -287,16 +287,18 @@ func send(c *hchan, sg *sudog, ep unsafe.Pointer, unlockf func()) {
287287
goready(gp, 4)
288288
}
289289

290+
// Sends and receives on unbuffered or empty-buffered channels are the
291+
// only operations where one running goroutine writes to the stack of
292+
// another running goroutine. The GC assumes that stack writes only
293+
// happen when the goroutine is running and are only done by that
294+
// goroutine. Using a write barrier is sufficient to make up for
295+
// violating that assumption, but the write barrier has to work.
296+
// typedmemmove will call bulkBarrierPreWrite, but the target bytes
297+
// are not in the heap, so that will not help. We arrange to call
298+
// memmove and typeBitsBulkBarrier instead.
299+
290300
func sendDirect(t *_type, sg *sudog, src unsafe.Pointer) {
291-
// Send on an unbuffered or empty-buffered channel is the only operation
292-
// in the entire runtime where one goroutine
293-
// writes to the stack of another goroutine. The GC assumes that
294-
// stack writes only happen when the goroutine is running and are
295-
// only done by that goroutine. Using a write barrier is sufficient to
296-
// make up for violating that assumption, but the write barrier has to work.
297-
// typedmemmove will call bulkBarrierPreWrite, but the target bytes
298-
// are not in the heap, so that will not help. We arrange to call
299-
// memmove and typeBitsBulkBarrier instead.
301+
// src is on our stack, dst is a slot on another stack.
300302

301303
// Once we read sg.elem out of sg, it will no longer
302304
// be updated if the destination's stack gets copied (shrunk).
@@ -306,6 +308,15 @@ func sendDirect(t *_type, sg *sudog, src unsafe.Pointer) {
306308
memmove(dst, src, t.size)
307309
}
308310

311+
func recvDirect(t *_type, sg *sudog, dst unsafe.Pointer) {
312+
// dst is on our stack or the heap, src is on another stack.
313+
// The channel is locked, so src will not move during this
314+
// operation.
315+
src := sg.elem
316+
typeBitsBulkBarrier(t, uintptr(dst), uintptr(src), t.size)
317+
memmove(dst, src, t.size)
318+
}
319+
309320
func closechan(c *hchan) {
310321
if c == nil {
311322
panic(plainError("close of nil channel"))
@@ -536,9 +547,7 @@ func recv(c *hchan, sg *sudog, ep unsafe.Pointer, unlockf func()) {
536547
}
537548
if ep != nil {
538549
// copy data from sender
539-
// ep points to our own stack or heap, so nothing
540-
// special (ala sendDirect) needed here.
541-
typedmemmove(c.elemtype, ep, sg.elem)
550+
recvDirect(c.elemtype, sg, ep)
542551
}
543552
} else {
544553
// Queue is full. Take the item at the

0 commit comments

Comments
 (0)