cmd/compile: gate instrumentEnterExit on NoRaceFunc check

The NoRaceFunc flag is meant to suppress racefuncenter/racefuncexit
instrumentation for packages like internal/runtime/atomic. However,
instrumentEnterExit was set unconditionally when -race was enabled,
outside the NoRaceFunc guard. This caused generic functions from
NoRaceFunc packages (e.g. atomic.(*Pointer[T]).Store) to receive
racefuncenter calls when instantiated in other packages, leading to
a segfault during early runtime init before the race runtime is ready.

Move the instrumentEnterExit assignment inside the NoRaceFunc check
so both memory and enter/exit instrumentation are suppressed together.

Fixes #77597

Change-Id: Id03bb9c422d36e2e88ecdf165ad3b1a4700a935c
Reviewed-on: https://go-review.googlesource.com/c/go/+/748260
Auto-Submit: Keith Randall <khr@golang.org>
Reviewed-by: David Chase <drchase@google.com>
Reviewed-by: Keith Randall <khr@golang.org>
Reviewed-by: Keith Randall <khr@google.com>
LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com>
This commit is contained in:
Derek Parker
2026-02-23 11:53:35 -08:00
committed by Gopher Robot
parent f2633386e0
commit 76222756d9
2 changed files with 42 additions and 3 deletions

View File

@@ -341,9 +341,9 @@ func buildssa(fn *ir.Func, worker int, isPgoHot bool) *ssa.Func {
if base.Flag.Cfg.Instrumenting && fn.Pragma&ir.Norace == 0 && !fn.Linksym().ABIWrapper() {
if !base.Flag.Race || !objabi.LookupPkgSpecial(fn.Sym().Pkg.Path).NoRaceFunc {
s.instrumentMemory = true
}
if base.Flag.Race {
s.instrumentEnterExit = true
if base.Flag.Race {
s.instrumentEnterExit = true
}
}
}

View File

@@ -5,9 +5,11 @@
package test
import (
"internal/platform"
"internal/testenv"
"os"
"path/filepath"
"runtime"
"strings"
"testing"
)
@@ -84,3 +86,40 @@ func Mod32(x uint32) uint32 {
return x % 3 // frontend rewrites it as HMUL with 2863311531, the LITERAL node has unknown Pos
}
`
// Test that building and running a program with -race -gcflags='all=-N -l'
// does not crash. This is a regression test for #77597 where generic functions
// from NoRaceFunc packages (like internal/runtime/atomic) were incorrectly
// getting racefuncenter/racefuncexit instrumentation when instantiated in
// other packages with optimizations disabled.
func TestIssue77597(t *testing.T) {
if !platform.RaceDetectorSupported(runtime.GOOS, runtime.GOARCH) {
t.Skipf("race detector not supported on %s/%s", runtime.GOOS, runtime.GOARCH)
}
testenv.MustHaveGoBuild(t)
testenv.MustHaveGoRun(t)
testenv.MustHaveCGO(t)
dir := t.TempDir()
src := filepath.Join(dir, "main.go")
err := os.WriteFile(src, []byte(issue77597src), 0644)
if err != nil {
t.Fatalf("could not write file: %v", err)
}
cmd := testenv.Command(t, testenv.GoToolPath(t), "run", "-race", "-gcflags=all=-N -l", src)
out, err := cmd.CombinedOutput()
if err != nil {
t.Fatalf("program failed: %v\n%s", err, out)
}
}
var issue77597src = `
package main
import "fmt"
func main() {
fmt.Println("OK")
}
`