]> Cypherpunks repositories - gostls13.git/commitdiff
cmd/go: correctly report that -msan needs CGO_ENABLED=1
authorEmmanuel Odeke <emm.odeke@gmail.com>
Thu, 14 Sep 2017 23:16:58 +0000 (17:16 -0600)
committerEmmanuel Odeke <emm.odeke@gmail.com>
Fri, 15 Sep 2017 00:11:18 +0000 (00:11 +0000)
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>
src/cmd/go/go_test.go
src/cmd/go/internal/work/build.go

index 3908fe8823213a740753c25480b736bfda84ee8a..e7f0668f7eb794cab370a41bcc557bf4e16cb4cc 100644 (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()
index 22ece5b9892730559032def2f0ed5debc1c8438e..ab4992f077d4cd56829d9b751294f7d7f2c22cb2 100644 (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 {