Commit 33cb1481 authored by Emmanuel Odeke's avatar Emmanuel Odeke

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 <emm.odeke@gmail.com>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: 's avatarRuss Cox <rsc@golang.org>
parent 95b146e8
......@@ -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()
......
......@@ -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 {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment