From 33cb1481f26c6f1acca445fafa18e7ad1d49efed Mon Sep 17 00:00:00 2001 From: Emmanuel Odeke Date: Thu, 14 Sep 2017 17:16:58 -0600 Subject: [PATCH] cmd/go: correctly report that -msan needs CGO_ENABLED=1 Previously, if CGO_ENABLED=0 was set when building with -msan, the error message printed was: -race requires cgo; enable cgo by setting CGO_ENABLED=1 yet the instrumentation flag passed in was -msan. This CL fixes that message to correctly report that -msan needed CGO_ENABLED=1, and likewise if -race, report -race needed it. Fixes #21895 Change-Id: If423d520daae7847fb38cc97c3192ada5d960f9d Reviewed-on: https://go-review.googlesource.com/63930 Run-TryBot: Emmanuel Odeke TryBot-Result: Gobot Gobot Reviewed-by: Russ Cox --- src/cmd/go/go_test.go | 28 +++++++++++++++++++++++++++- src/cmd/go/internal/work/build.go | 6 +++++- 2 files changed, 32 insertions(+), 2 deletions(-) diff --git a/src/cmd/go/go_test.go b/src/cmd/go/go_test.go index 3908fe8823..e7f0668f7e 100644 --- a/src/cmd/go/go_test.go +++ b/src/cmd/go/go_test.go @@ -29,6 +29,7 @@ var ( canRun = true // whether we can run go or ./testgo canRace = false // whether we can run the race detector canCgo = false // whether we can use cgo + canMSan = false // whether we can run the memory sanitizer exeSuffix string // ".exe" on Windows @@ -120,6 +121,10 @@ func TestMain(m *testing.M) { } } + // As of Sept 2017, MSan is only supported on linux/amd64. + // https://github.com/google/sanitizers/wiki/MemorySanitizer#getting-memorysanitizer + canMSan = canCgo && runtime.GOOS == "linux" && runtime.GOARCH == "amd64" + switch runtime.GOOS { case "linux", "darwin", "freebsd", "windows": // The race detector doesn't work on Alpine Linux: @@ -127,7 +132,6 @@ func TestMain(m *testing.M) { canRace = canCgo && runtime.GOARCH == "amd64" && !isAlpineLinux() } } - // Don't let these environment variables confuse the test. os.Unsetenv("GOBIN") os.Unsetenv("GOPATH") @@ -1448,6 +1452,28 @@ func TestInstallFailsWithNoBuildableFiles(t *testing.T) { tg.grepStderr("build constraints exclude all Go files", "go install cgotest did not report 'build constraints exclude all Go files'") } +// Issue 21895 +func TestMSanAndRaceRequireCgo(t *testing.T) { + if !canMSan && !canRace { + t.Skip("skipping because both msan and the race detector are not supported") + } + + tg := testgo(t) + defer tg.cleanup() + tg.tempFile("triv.go", `package main; func main() {}`) + tg.setenv("CGO_ENABLED", "0") + if canRace { + tg.runFail("install", "-race", "triv.go") + tg.grepStderr("-race requires cgo", "did not correctly report that -race requires cgo") + tg.grepStderrNot("-msan", "reported that -msan instead of -race requires cgo") + } + if canMSan { + tg.runFail("install", "-msan", "triv.go") + tg.grepStderr("-msan requires cgo", "did not correctly report that -msan requires cgo") + tg.grepStderrNot("-race", "reported that -race instead of -msan requires cgo") + } +} + func TestRelativeGOBINFail(t *testing.T) { tg := testgo(t) defer tg.cleanup() diff --git a/src/cmd/go/internal/work/build.go b/src/cmd/go/internal/work/build.go index 22ece5b989..ab4992f077 100644 --- a/src/cmd/go/internal/work/build.go +++ b/src/cmd/go/internal/work/build.go @@ -3850,7 +3850,11 @@ func InstrumentInit() { os.Exit(2) } if !cfg.BuildContext.CgoEnabled { - fmt.Fprintf(os.Stderr, "go %s: -race requires cgo; enable cgo by setting CGO_ENABLED=1\n", flag.Args()[0]) + instrFlag := "-race" + if cfg.BuildMSan { + instrFlag = "-msan" + } + fmt.Fprintf(os.Stderr, "go %s: %s requires cgo; enable cgo by setting CGO_ENABLED=1\n", flag.Args()[0], instrFlag) os.Exit(2) } if cfg.BuildRace { -- 2.48.1