Skip to content

Commit 089e482

Browse files
committed
runtime: reorder race detector calls in slicecopy
In rare circumstances, this helps report a race which would otherwise go undetected. Fixes #36794 Change-Id: I8a3c9bd6fc34efa51516393f7ee72531c34fb073 Reviewed-on: https://go-review.googlesource.com/c/go/+/220685 Run-TryBot: Keith Randall <[email protected]> TryBot-Result: Gobot Gobot <[email protected]> Reviewed-by: Dmitry Vyukov <[email protected]>
1 parent 0652c80 commit 089e482

File tree

2 files changed

+18
-2
lines changed

2 files changed

+18
-2
lines changed

src/runtime/race/testdata/slice_test.go

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
package race_test
66

77
import (
8+
"sync"
89
"testing"
910
)
1011

@@ -590,3 +591,18 @@ func TestRaceSlice3(t *testing.T) {
590591
_ = x[:1:i]
591592
<-done
592593
}
594+
595+
var saved string
596+
597+
func TestRaceSlice4(t *testing.T) {
598+
// See issue 36794.
599+
data := []byte("hello there")
600+
var wg sync.WaitGroup
601+
wg.Add(1)
602+
go func() {
603+
_ = string(data)
604+
wg.Done()
605+
}()
606+
copy(data, data[2:])
607+
wg.Wait()
608+
}

src/runtime/slice.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -211,12 +211,12 @@ func slicecopy(to, fm slice, width uintptr) int {
211211
if raceenabled {
212212
callerpc := getcallerpc()
213213
pc := funcPC(slicecopy)
214-
racewriterangepc(to.array, uintptr(n*int(width)), callerpc, pc)
215214
racereadrangepc(fm.array, uintptr(n*int(width)), callerpc, pc)
215+
racewriterangepc(to.array, uintptr(n*int(width)), callerpc, pc)
216216
}
217217
if msanenabled {
218-
msanwrite(to.array, uintptr(n*int(width)))
219218
msanread(fm.array, uintptr(n*int(width)))
219+
msanwrite(to.array, uintptr(n*int(width)))
220220
}
221221

222222
size := uintptr(n) * width

0 commit comments

Comments
 (0)