1
0
mirror of https://github.com/golang/go synced 2024-11-11 22:50:22 -07:00

runtime: fix data race in abi finalizer test

Fix a buglet in TestFinalizerRegisterABI that was causing problems
when run with GOEXPERIMENT=regabi.

Updates #40724.

Change-Id: I950c4aa3df69eada23e600f01e6804eb136ce806
Reviewed-on: https://go-review.googlesource.com/c/go/+/310077
Trust: Than McIntosh <thanm@google.com>
Run-TryBot: Than McIntosh <thanm@google.com>
Reviewed-by: Michael Knyszek <mknyszek@google.com>
This commit is contained in:
Than McIntosh 2021-04-14 15:40:01 -04:00
parent a89ace106f
commit 1a8f0a7961

View File

@ -17,21 +17,36 @@ import (
"os/exec" "os/exec"
"runtime" "runtime"
"strings" "strings"
"sync"
"testing" "testing"
"time" "time"
) )
var regConfirmRun int var regConfirmRunIface int
var regConfirmRunPtr int
var regConfirmMU sync.Mutex
func guardedRead(p *int) int {
regConfirmMU.Lock()
defer regConfirmMU.Unlock()
return *p
}
func guardedWrite(p *int, v int) {
regConfirmMU.Lock()
defer regConfirmMU.Unlock()
*p = v
}
//go:registerparams //go:registerparams
func regFinalizerPointer(v *Tint) (int, float32, [10]byte) { func regFinalizerPointer(v *Tint) (int, float32, [10]byte) {
regConfirmRun = *(*int)(v) guardedWrite(&regConfirmRunPtr, *(*int)(v))
return 5151, 4.0, [10]byte{1, 2, 3, 4, 5, 6, 7, 8, 9, 10} return 5151, 4.0, [10]byte{1, 2, 3, 4, 5, 6, 7, 8, 9, 10}
} }
//go:registerparams //go:registerparams
func regFinalizerIface(v Tinter) (int, float32, [10]byte) { func regFinalizerIface(v Tinter) (int, float32, [10]byte) {
regConfirmRun = *(*int)(v.(*Tint)) guardedWrite(&regConfirmRunIface, *(*int)(v.(*Tint)))
return 5151, 4.0, [10]byte{1, 2, 3, 4, 5, 6, 7, 8, 9, 10} return 5151, 4.0, [10]byte{1, 2, 3, 4, 5, 6, 7, 8, 9, 10}
} }
@ -81,14 +96,15 @@ func TestFinalizerRegisterABI(t *testing.T) {
name string name string
fin interface{} fin interface{}
confirmValue int confirmValue int
confirmRun *int
}{ }{
{"Pointer", regFinalizerPointer, -1}, {"Pointer", regFinalizerPointer, -1, &regConfirmRunPtr},
{"Interface", regFinalizerIface, -2}, {"Interface", regFinalizerIface, -2, &regConfirmRunIface},
} }
for i := range tests { for i := range tests {
test := &tests[i] test := &tests[i]
t.Run(test.name, func(t *testing.T) { t.Run(test.name, func(t *testing.T) {
regConfirmRun = 0 guardedWrite(test.confirmRun, 0)
x := new(Tint) x := new(Tint)
*x = (Tint)(test.confirmValue) *x = (Tint)(test.confirmValue)
@ -102,14 +118,15 @@ func TestFinalizerRegisterABI(t *testing.T) {
for i := 0; i < 100; i++ { for i := 0; i < 100; i++ {
time.Sleep(10 * time.Millisecond) time.Sleep(10 * time.Millisecond)
if regConfirmRun != 0 { if guardedRead(test.confirmRun) != 0 {
break break
} }
} }
if regConfirmRun == 0 { v := guardedRead(test.confirmRun)
if v == 0 {
t.Fatal("finalizer failed to execute") t.Fatal("finalizer failed to execute")
} else if regConfirmRun != test.confirmValue { } else if v != test.confirmValue {
t.Fatalf("wrong finalizer executed? regConfirmRun = %d", regConfirmRun) t.Fatalf("wrong finalizer executed? regConfirmRun = %d", v)
} }
}) })
} }