1
0
mirror of https://github.com/golang/go synced 2024-11-20 11:04:56 -07:00

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: Russ Cox <rsc@golang.org>
This commit is contained in:
Emmanuel Odeke 2017-09-14 17:16:58 -06:00
parent 95b146e8eb
commit 33cb1481f2
2 changed files with 32 additions and 2 deletions

View File

@ -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()

View File

@ -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 {