aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDerek Parker <parkerderek86@gmail.com>2026-02-23 11:53:35 -0800
committerGopher Robot <gobot@golang.org>2026-03-26 07:20:39 -0700
commit68f42c591d81b0a142deb24cdb392c1f4253da80 (patch)
treef2ff2b2f2f610d77d226d7e655b5abc89156c105
parent9fe1db7e7e4b51b594f32d4ef34b2ddfabe14a68 (diff)
downloadgo-68f42c591d81b0a142deb24cdb392c1f4253da80.tar.xz
[release-branch.go1.26] 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 #77799 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> (cherry picked from commit 76222756d9674a41b01a11d123ce39a7b26eb7fc) Reviewed-on: https://go-review.googlesource.com/c/go/+/752360 Reviewed-by: Derek Parker <parkerderek86@gmail.com> Reviewed-by: Mark Freeman <markfreeman@google.com> Auto-Submit: Dmitri Shuralyov <dmitshur@google.com>
-rw-r--r--src/cmd/compile/internal/ssagen/ssa.go6
-rw-r--r--src/cmd/compile/internal/test/fixedbugs_test.go39
2 files changed, 42 insertions, 3 deletions
diff --git a/src/cmd/compile/internal/ssagen/ssa.go b/src/cmd/compile/internal/ssagen/ssa.go
index 17feb90df7..50b4e11f5e 100644
--- a/src/cmd/compile/internal/ssagen/ssa.go
+++ b/src/cmd/compile/internal/ssagen/ssa.go
@@ -340,9 +340,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
+ }
}
}
diff --git a/src/cmd/compile/internal/test/fixedbugs_test.go b/src/cmd/compile/internal/test/fixedbugs_test.go
index b6d3e248ad..4730a21f34 100644
--- a/src/cmd/compile/internal/test/fixedbugs_test.go
+++ b/src/cmd/compile/internal/test/fixedbugs_test.go
@@ -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")
+}
+`