Skip to content

Commit 7e64377

Browse files
cmd/compile: only support -race and -msan where they work
Consolidate decision about whether -race and -msan options are supported in cmd/internal/sys. Use consolidated functions in cmd/compile and cmd/go. Use a copy of them in cmd/dist; cmd/dist can't import cmd/internal/sys because Go 1.4 doesn't have it. Fixes #24315 Change-Id: I9cecaed4895eb1a2a49379b4848db40de66d32a9 Reviewed-on: https://go-review.googlesource.com/121816 Run-TryBot: Ian Lance Taylor <[email protected]> TryBot-Result: Gobot Gobot <[email protected]> Reviewed-by: Brad Fitzpatrick <[email protected]>
1 parent d58f3e6 commit 7e64377

File tree

10 files changed

+89
-24
lines changed

10 files changed

+89
-24
lines changed

src/cmd/compile/internal/gc/main.go

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -216,14 +216,18 @@ func Main(archInit func(*Arch)) {
216216
flag.StringVar(&linkobj, "linkobj", "", "write linker-specific object to `file`")
217217
objabi.Flagcount("live", "debug liveness analysis", &debuglive)
218218
objabi.Flagcount("m", "print optimization decisions", &Debug['m'])
219-
flag.BoolVar(&flag_msan, "msan", false, "build code compatible with C/C++ memory sanitizer")
219+
if sys.MSanSupported(objabi.GOOS, objabi.GOARCH) {
220+
flag.BoolVar(&flag_msan, "msan", false, "build code compatible with C/C++ memory sanitizer")
221+
}
220222
flag.BoolVar(&dolinkobj, "dolinkobj", true, "generate linker-specific objects; if false, some invalid code may compile")
221223
flag.BoolVar(&nolocalimports, "nolocalimports", false, "reject local (relative) imports")
222224
flag.StringVar(&outfile, "o", "", "write output to `file`")
223225
flag.StringVar(&myimportpath, "p", "", "set expected package import `path`")
224226
flag.BoolVar(&writearchive, "pack", false, "write to file.a instead of file.o")
225227
objabi.Flagcount("r", "debug generated wrappers", &Debug['r'])
226-
flag.BoolVar(&flag_race, "race", false, "enable race detector")
228+
if sys.RaceDetectorSupported(objabi.GOOS, objabi.GOARCH) {
229+
flag.BoolVar(&flag_race, "race", false, "enable race detector")
230+
}
227231
objabi.Flagcount("s", "warn about composite literals that can be simplified", &Debug['s'])
228232
flag.StringVar(&pathPrefix, "trimpath", "", "remove `prefix` from recorded source file paths")
229233
flag.BoolVar(&safemode, "u", false, "reject unsafe code")

src/cmd/dist/test.go

Lines changed: 38 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -705,7 +705,7 @@ func (t *tester) registerTests() {
705705
if gohostos == "linux" && goarch == "amd64" {
706706
t.registerTest("testasan", "../misc/cgo/testasan", "go", "run", "main.go")
707707
}
708-
if goos == "linux" && (goarch == "amd64" || goarch == "arm64") {
708+
if mSanSupported(goos, goarch) {
709709
t.registerHostTest("testsanitizers/msan", "../misc/cgo/testsanitizers", "misc/cgo/testsanitizers", ".")
710710
}
711711
if t.hasBash() && goos != "android" && !t.iOS() && gohostos != "windows" {
@@ -1329,13 +1329,21 @@ func (t *tester) hasSwig() bool {
13291329
}
13301330

13311331
func (t *tester) raceDetectorSupported() bool {
1332-
switch gohostos {
1333-
case "linux", "darwin", "freebsd", "windows":
1334-
// The race detector doesn't work on Alpine Linux:
1335-
// golang.org/issue/14481
1336-
return t.cgoEnabled && (goarch == "amd64" || goarch == "ppc64le") && gohostos == goos && !isAlpineLinux()
1332+
if gohostos != goos {
1333+
return false
13371334
}
1338-
return false
1335+
if !t.cgoEnabled {
1336+
return false
1337+
}
1338+
if !raceDetectorSupported(goos, goarch) {
1339+
return false
1340+
}
1341+
// The race detector doesn't work on Alpine Linux:
1342+
// golang.org/issue/14481
1343+
if isAlpineLinux() {
1344+
return false
1345+
}
1346+
return true
13391347
}
13401348

13411349
func isAlpineLinux() bool {
@@ -1450,3 +1458,26 @@ func (t *tester) packageHasBenchmarks(pkg string) bool {
14501458
}
14511459
return false
14521460
}
1461+
1462+
// raceDetectorSupported is a copy of the function
1463+
// cmd/internal/sys.RaceDetectorSupported, which can't be used here
1464+
// because cmd/dist has to be buildable by Go 1.4.
1465+
func raceDetectorSupported(goos, goarch string) bool {
1466+
switch goos {
1467+
case "linux", "darwin", "freebsd", "netbsd", "windows":
1468+
return goarch == "amd64" || goarch == "ppc64le"
1469+
default:
1470+
return false
1471+
}
1472+
}
1473+
1474+
// mSanSupported is a copy of the function cmd/internal/sys.MSanSupported,
1475+
// which can't be used here because cmd/dist has to be buildable by Go 1.4.
1476+
func mSanSupported(goos, goarch string) bool {
1477+
switch goos {
1478+
case "linux":
1479+
return goarch == "amd64" || goarch == "arm64"
1480+
default:
1481+
return false
1482+
}
1483+
}

src/cmd/go/go_test.go

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ package main_test
66

77
import (
88
"bytes"
9+
"cmd/internal/sys"
910
"debug/elf"
1011
"debug/macho"
1112
"flag"
@@ -209,15 +210,13 @@ func TestMain(m *testing.M) {
209210
}
210211
testGOCACHE = strings.TrimSpace(string(out))
211212

212-
// As of Sept 2017, MSan is only supported on linux/amd64.
213-
// https://github.com/google/sanitizers/wiki/MemorySanitizer#getting-memorysanitizer
214-
canMSan = canCgo && runtime.GOOS == "linux" && runtime.GOARCH == "amd64"
215-
216-
switch runtime.GOOS {
217-
case "linux", "darwin", "freebsd", "windows":
218-
// The race detector doesn't work on Alpine Linux:
219-
// golang.org/issue/14481
220-
canRace = canCgo && runtime.GOARCH == "amd64" && !isAlpineLinux() && runtime.Compiler != "gccgo"
213+
canMSan = canCgo && sys.MSanSupported(runtime.GOOS, runtime.GOARCH)
214+
canRace = canCgo && sys.RaceDetectorSupported(runtime.GOOS, runtime.GOARCH)
215+
// The race detector doesn't work on Alpine Linux:
216+
// golang.org/issue/14481
217+
// gccgo does not support the race detector.
218+
if isAlpineLinux() || runtime.Compiler == "gccgo" {
219+
canRace = false
221220
}
222221
}
223222
// Don't let these environment variables confuse the test.

src/cmd/go/internal/work/init.go

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import (
1010
"cmd/go/internal/base"
1111
"cmd/go/internal/cfg"
1212
"cmd/go/internal/load"
13+
"cmd/internal/sys"
1314
"flag"
1415
"fmt"
1516
"os"
@@ -42,18 +43,14 @@ func instrumentInit() {
4243
fmt.Fprintf(os.Stderr, "go %s: may not use -race and -msan simultaneously\n", flag.Args()[0])
4344
os.Exit(2)
4445
}
45-
if cfg.BuildMSan && (cfg.Goos != "linux" || cfg.Goarch != "amd64" && cfg.Goarch != "arm64") {
46+
if cfg.BuildMSan && !sys.MSanSupported(cfg.Goos, cfg.Goarch) {
4647
fmt.Fprintf(os.Stderr, "-msan is not supported on %s/%s\n", cfg.Goos, cfg.Goarch)
4748
os.Exit(2)
4849
}
4950
if cfg.BuildRace {
50-
platform := cfg.Goos + "/" + cfg.Goarch
51-
switch platform {
52-
default:
51+
if !sys.RaceDetectorSupported(cfg.Goos, cfg.Goarch) {
5352
fmt.Fprintf(os.Stderr, "go %s: -race is only supported on linux/amd64, linux/ppc64le, freebsd/amd64, netbsd/amd64, darwin/amd64 and windows/amd64\n", flag.Args()[0])
5453
os.Exit(2)
55-
case "linux/amd64", "linux/ppc64le", "freebsd/amd64", "netbsd/amd64", "darwin/amd64", "windows/amd64":
56-
// race supported on these platforms
5754
}
5855
}
5956
mode := "race"

src/cmd/internal/sys/supported.go

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
// Copyright 2018 The Go Authors. All rights reserved.
2+
// Use of this source code is governed by a BSD-style
3+
// license that can be found in the LICENSE file.
4+
5+
package sys
6+
7+
// RaceDetectorSupported reports whether goos/goarch supports the race
8+
// detector. There is a copy of this function in cmd/dist/test.go.
9+
func RaceDetectorSupported(goos, goarch string) bool {
10+
switch goos {
11+
case "linux":
12+
return goarch == "amd64" || goarch == "ppc64le"
13+
case "darwin", "freebsd", "netbsd", "windows":
14+
return goarch == "amd64"
15+
default:
16+
return false
17+
}
18+
}
19+
20+
// MSanSupported reports whether goos/goarch supports the memory
21+
// sanitizer option. There is a copy of this function in cmd/dist/test.go.
22+
func MSanSupported(goos, goarch string) bool {
23+
switch goos {
24+
case "linux":
25+
return goarch == "amd64" || goarch == "arm64"
26+
default:
27+
return false
28+
}
29+
}

test/fixedbugs/issue13265.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
// errorcheck -0 -race
2+
// +build linux,amd64 linux,ppc64le darwin,amd64 freebsd,amd64 netbsd,amd64 windows,amd64
23

34
// Copyright 2017 The Go Authors. All rights reserved.
45
// Use of this source code is governed by a BSD-style

test/fixedbugs/issue15091.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
// errorcheck -0 -race
2+
// +build linux,amd64 linux,ppc64le darwin,amd64 freebsd,amd64 netbsd,amd64 windows,amd64
23

34
// Copyright 2016 The Go Authors. All rights reserved.
45
// Use of this source code is governed by a BSD-style

test/fixedbugs/issue16008.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
// errorcheck -0 -race
2+
// +build linux,amd64 linux,ppc64le darwin,amd64 freebsd,amd64 netbsd,amd64 windows,amd64
23

34
// Copyright 2016 The Go Authors. All rights reserved.
45
// Use of this source code is governed by a BSD-style

test/fixedbugs/issue17449.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
// errorcheck -0 -race
2+
// +build linux,amd64 linux,ppc64le darwin,amd64 freebsd,amd64 netbsd,amd64 windows,amd64
23

34
// Copyright 2016 The Go Authors. All rights reserved.
45
// Use of this source code is governed by a BSD-style

test/fixedbugs/issue24651a.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
//errorcheck -0 -race -m -m
2+
// +build linux,amd64 linux,ppc64le darwin,amd64 freebsd,amd64 netbsd,amd64 windows,amd64
23

34
// Copyright 2018 The Go Authors. All rights reserved.
45
// Use of this source code is governed by a BSD-style

0 commit comments

Comments
 (0)