From: Keith Randall Date: Sun, 16 Feb 2020 03:23:07 +0000 (-0800) Subject: runtime: reorder race detector calls in slicecopy X-Git-Tag: go1.15beta1~1060 X-Git-Url: http://www.git.cypherpunks.su/?a=commitdiff_plain;h=089e482b3dd2026178c8ee5b90d9aadb6bf81239;p=gostls13.git 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 TryBot-Result: Gobot Gobot Reviewed-by: Dmitry Vyukov --- diff --git a/src/runtime/race/testdata/slice_test.go b/src/runtime/race/testdata/slice_test.go index 1ec52438ec..9009a9a4ea 100644 --- a/src/runtime/race/testdata/slice_test.go +++ b/src/runtime/race/testdata/slice_test.go @@ -5,6 +5,7 @@ package race_test import ( + "sync" "testing" ) @@ -590,3 +591,18 @@ func TestRaceSlice3(t *testing.T) { _ = x[:1:i] <-done } + +var saved string + +func TestRaceSlice4(t *testing.T) { + // See issue 36794. + data := []byte("hello there") + var wg sync.WaitGroup + wg.Add(1) + go func() { + _ = string(data) + wg.Done() + }() + copy(data, data[2:]) + wg.Wait() +} diff --git a/src/runtime/slice.go b/src/runtime/slice.go index 16937a2a01..9ad814a555 100644 --- a/src/runtime/slice.go +++ b/src/runtime/slice.go @@ -211,12 +211,12 @@ func slicecopy(to, fm slice, width uintptr) int { if raceenabled { callerpc := getcallerpc() pc := funcPC(slicecopy) - racewriterangepc(to.array, uintptr(n*int(width)), callerpc, pc) racereadrangepc(fm.array, uintptr(n*int(width)), callerpc, pc) + racewriterangepc(to.array, uintptr(n*int(width)), callerpc, pc) } if msanenabled { - msanwrite(to.array, uintptr(n*int(width))) msanread(fm.array, uintptr(n*int(width))) + msanwrite(to.array, uintptr(n*int(width))) } size := uintptr(n) * width