From 8c39bbf9c93f773ab351bbddb4c3dd93e4fddc76 Mon Sep 17 00:00:00 2001 From: Keith Randall Date: Tue, 11 Aug 2020 13:19:57 -0700 Subject: [PATCH] cmd/compile: stop race instrumentation from clobbering frame pointer There is an optimization rule that removes calls to racefuncenter and racefuncexit, if there are no other race calls in the function. The rule removes the call to racefuncenter, but it does *not* remove the store of its argument to the outargs section of the frame. If the outargs section is now size 0 (because the calls to racefuncenter/exit were the only calls), then that argument store clobbers the frame pointer instead. The fix is to remove the argument store when removing the call to racefuncenter. (Racefuncexit doesn't have an argument.) Change-Id: I183ec4d92bbb4920200e1be27b7b8f66b89a2a0a Reviewed-on: https://go-review.googlesource.com/c/go/+/248262 Reviewed-by: Robert Griesemer Run-TryBot: Keith Randall TryBot-Result: Gobot Gobot --- src/cmd/compile/internal/gc/racewalk.go | 2 +- src/cmd/compile/internal/ssa/rewrite.go | 9 +++++++++ 2 files changed, 10 insertions(+), 1 deletion(-) diff --git a/src/cmd/compile/internal/gc/racewalk.go b/src/cmd/compile/internal/gc/racewalk.go index 6f251377c9..3552617401 100644 --- a/src/cmd/compile/internal/gc/racewalk.go +++ b/src/cmd/compile/internal/gc/racewalk.go @@ -42,7 +42,7 @@ var omit_pkgs = []string{ "internal/cpu", } -// Only insert racefuncenterfp/racefuncexit into the following packages. +// Don't insert racefuncenterfp/racefuncexit into the following packages. // Memory accesses in the packages are either uninteresting or will cause false positives. var norace_inst_pkgs = []string{"sync", "sync/atomic"} diff --git a/src/cmd/compile/internal/ssa/rewrite.go b/src/cmd/compile/internal/ssa/rewrite.go index 2152b1675a..e082bb1dfa 100644 --- a/src/cmd/compile/internal/ssa/rewrite.go +++ b/src/cmd/compile/internal/ssa/rewrite.go @@ -1379,6 +1379,15 @@ func needRaceCleanup(sym Sym, v *Value) bool { } } } + if symNamed(sym, "runtime.racefuncenter") { + // If we're removing racefuncenter, remove its argument as well. + if v.Args[0].Op != OpStore { + return false + } + mem := v.Args[0].Args[2] + v.Args[0].reset(OpCopy) + v.Args[0].AddArg(mem) + } return true } -- 2.50.0