1 // Copyright 2015 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.
27 gogcflags = os.Getenv("GO_GCFLAGS")
33 flag.BoolVar(&t.listMode, "list", false, "list available tests")
34 flag.BoolVar(&t.rebuild, "rebuild", false, "rebuild everything first")
35 flag.BoolVar(&noRebuild, "no-rebuild", false, "overrides -rebuild (historical dreg)")
36 flag.BoolVar(&t.keepGoing, "k", false, "keep going even when error occurred")
37 flag.BoolVar(&t.race, "race", false, "run in race builder mode (different set of tests)")
38 flag.BoolVar(&t.compileOnly, "compile-only", false, "compile tests, but don't run them. This is for some builders. Not all dist tests respect this flag, but most do.")
39 flag.StringVar(&t.banner, "banner", "##### ", "banner prefix; blank means no section banners")
40 flag.StringVar(&t.runRxStr, "run", os.Getenv("GOTESTONLY"),
41 "run only those tests matching the regular expression; empty means to run all. "+
42 "Special exception: if the string begins with '!', the match is inverted.")
43 flag.BoolVar(&t.msan, "msan", false, "run in memory sanitizer builder mode")
44 flag.BoolVar(&t.asan, "asan", false, "run in address sanitizer builder mode")
46 xflagparse(-1) // any number of args
54 // tester executes cmdtest.
63 compileOnly bool // just try to compile all tests, but no need to run
66 runRxWant bool // want runRx to match (true) or not match (false)
67 runNames []string // tests to run, exclusive with runRx; empty means all
68 banner string // prefix, or "" for none
69 lastHeading string // last dir heading printed
73 haveTime bool // the 'time' binary is available
90 // A distTest is a test run by dist test.
91 // Each test has a unique name and belongs to a group (heading)
92 type distTest struct {
93 name string // unique test name; may be filtered with -run flag
94 heading string // group section; this header is printed before the test is run.
95 fn func(*distTest) error
98 func (t *tester) run() {
99 timelog("start", "dist test")
101 os.Setenv("PATH", fmt.Sprintf("%s%c%s", gorootBin, os.PathListSeparator, os.Getenv("PATH")))
103 cmd := exec.Command(gorootBinGo, "env", "CGO_ENABLED")
104 cmd.Stderr = new(bytes.Buffer)
105 slurp, err := cmd.Output()
107 fatalf("Error running go env CGO_ENABLED: %v\n%s", err, cmd.Stderr)
109 t.cgoEnabled, _ = strconv.ParseBool(strings.TrimSpace(string(slurp)))
110 if flag.NArg() > 0 && t.runRxStr != "" {
111 fatalf("the -run regular expression flag is mutually exclusive with test name arguments")
114 t.runNames = flag.Args()
117 if _, err := exec.LookPath("time"); err == nil {
122 // Set GOTRACEBACK to system if the user didn't set a level explicitly.
123 // Since we're running tests for Go, we want as much detail as possible
124 // if something goes wrong.
126 // Set it before running any commands just in case something goes wrong.
127 if ok := isEnvSet("GOTRACEBACK"); !ok {
128 if err := os.Setenv("GOTRACEBACK", "system"); err != nil {
130 log.Printf("Failed to set GOTRACEBACK: %v", err)
132 fatalf("Failed to set GOTRACEBACK: %v", err)
138 t.out("Building packages and commands.")
139 // Force rebuild the whole toolchain.
140 goInstall("go", append([]string{"-a", "-i"}, toolchain...)...)
144 if os.Getenv("GO_BUILDER_NAME") == "" {
145 // Complete rebuild bootstrap, even with -no-rebuild.
146 // If everything is up-to-date, this is a no-op.
147 // If everything is not up-to-date, the first checkNotStale
148 // during the test process will kill the tests, so we might
149 // as well install the world.
150 // Now that for example "go install cmd/compile" does not
151 // also install runtime (you need "go install -i cmd/compile"
152 // for that), it's easy for previous workflows like
153 // "rebuild the compiler and then run run.bash"
154 // to break if we don't automatically refresh things here.
155 // Rebuilding is a shortened bootstrap.
156 // See cmdbootstrap for a description of the overall process.
157 goInstall("go", append([]string{"-i"}, toolchain...)...)
158 goInstall("go", append([]string{"-i"}, toolchain...)...)
159 goInstall("go", "std", "cmd")
161 // The Go builder infrastructure should always begin running tests from a
162 // clean, non-stale state, so there is no need to rebuild the world.
163 // Instead, we can just check that it is not stale, which may be less
164 // expensive (and is also more likely to catch bugs in the builder
166 willTest := []string{"std"}
167 if t.shouldTestCmd() {
168 willTest = append(willTest, "cmd")
170 checkNotStale("go", willTest...)
178 case "mips", "mipsle", "mips64", "mips64le":
181 if s := os.Getenv("GO_TEST_TIMEOUT_SCALE"); s != "" {
182 t.timeoutScale, err = strconv.Atoi(s)
184 fatalf("failed to parse $GO_TEST_TIMEOUT_SCALE = %q as integer: %v", s, err)
188 if t.runRxStr != "" {
189 if t.runRxStr[0] == '!' {
191 t.runRxStr = t.runRxStr[1:]
195 t.runRx = regexp.MustCompile(t.runRxStr)
200 for _, tt := range t.tests {
206 for _, name := range t.runNames {
207 if !t.isRegisteredTestName(name) {
208 fatalf("unknown test %q", name)
212 // On a few builders, make GOROOT unwritable to catch tests writing to it.
213 if strings.HasPrefix(os.Getenv("GO_BUILDER_NAME"), "linux-") {
214 if os.Getuid() == 0 {
215 // Don't bother making GOROOT unwritable:
216 // we're running as root, so permissions would have no effect.
218 xatexit(t.makeGOROOTUnwritable())
222 if err := t.maybeLogMetadata(); err != nil {
225 log.Printf("Failed logging metadata: %v", err)
227 fatalf("Failed logging metadata: %v", err)
231 for _, dt := range t.tests {
232 if !t.shouldRunTest(dt.name) {
236 dt := dt // dt used in background after this iteration
237 if err := dt.fn(&dt); err != nil {
238 t.runPending(&dt) // in case that hasn't been done yet
241 log.Printf("Failed: %v", err)
243 fatalf("Failed: %v", err)
248 timelog("end", "dist test")
251 fmt.Println("\nFAILED")
253 } else if incomplete[goos+"/"+goarch] {
254 // The test succeeded, but consider it as failed so we don't
255 // forget to remove the port from the incomplete map once the
257 fmt.Println("\nFAILED (incomplete port)")
259 } else if t.partial {
260 fmt.Println("\nALL TESTS PASSED (some were excluded)")
262 fmt.Println("\nALL TESTS PASSED")
266 func (t *tester) shouldRunTest(name string) bool {
268 return t.runRx.MatchString(name) == t.runRxWant
270 if len(t.runNames) == 0 {
273 for _, runName := range t.runNames {
281 func (t *tester) maybeLogMetadata() error {
283 // We need to run a subprocess to log metadata. Don't do that
284 // on compile-only runs.
287 t.out("Test execution environment.")
288 // Helper binary to print system metadata (CPU model, etc). This is a
289 // separate binary from dist so it need not build with the bootstrap
292 // TODO(prattmic): If we split dist bootstrap and dist test then this
293 // could be simplified to directly use internal/sysinfo here.
294 return t.dirCmd(filepath.Join(goroot, "src/cmd/internal/metadata"), "go", []string{"run", "main.go"}).Run()
297 // short returns a -short flag value to use with 'go test'
298 // or a test binary for tests intended to run in short mode.
299 // It returns "true", unless the environment variable
300 // GO_TEST_SHORT is set to a non-empty, false-ish string.
302 // This environment variable is meant to be an internal
303 // detail between the Go build system and cmd/dist for
304 // the purpose of longtest builders, and is not intended
305 // for use by users. See golang.org/issue/12508.
306 func short() string {
307 if v := os.Getenv("GO_TEST_SHORT"); v != "" {
308 short, err := strconv.ParseBool(v)
310 fatalf("invalid GO_TEST_SHORT %q: %v", v, err)
319 // goTest returns the beginning of the go test command line.
320 // Callers should use goTest and then pass flags overriding these
321 // defaults as later arguments in the command line.
322 func (t *tester) goTest() []string {
324 "go", "test", "-short=" + short(), "-count=1", t.tags(), t.runFlag(""),
328 func (t *tester) tags() string {
332 return "-tags=lldb,noopt"
342 // timeoutDuration converts the provided number of seconds into a
343 // time.Duration, scaled by the t.timeoutScale factor.
344 func (t *tester) timeoutDuration(sec int) time.Duration {
345 return time.Duration(sec) * time.Second * time.Duration(t.timeoutScale)
348 // timeout returns the "-timeout=" string argument to "go test" given
349 // the number of seconds of timeout. It scales it by the
350 // t.timeoutScale factor.
351 func (t *tester) timeout(sec int) string {
352 return "-timeout=" + t.timeoutDuration(sec).String()
355 // ranGoTest and stdMatches are state closed over by the stdlib
356 // testing func in registerStdTest below. The tests are run
357 // sequentially, so there's no need for locks.
359 // ranGoBench and benchMatches are the same, but are only used
366 benchMatches []string
369 func (t *tester) registerStdTest(pkg string) {
370 heading := "Testing packages."
371 testPrefix := "go_test:"
374 testName := testPrefix + pkg
375 if t.runRx == nil || t.runRx.MatchString(testName) == t.runRxWant {
376 stdMatches = append(stdMatches, pkg)
379 t.tests = append(t.tests, distTest{
382 fn: func(dt *distTest) error {
387 timelog("start", dt.name)
388 defer timelog("end", dt.name)
392 for _, pkg := range stdMatches {
398 // Special case for our slow cross-compiled
400 if t.shouldUsePrecompiledStdTest() {
401 return t.runPrecompiledStdTest(t.timeoutDuration(timeoutSec))
407 t.timeout(timeoutSec),
410 args = append(args, "-gcflags=all="+gcflags)
413 args = append(args, "-race")
416 args = append(args, "-msan")
419 args = append(args, "-asan")
422 args = append(args, "-run=^$")
424 args = append(args, stdMatches...)
425 cmd := exec.Command(gorootBinGo, args...)
426 cmd.Stdout = os.Stdout
427 cmd.Stderr = os.Stderr
433 func (t *tester) registerRaceBenchTest(pkg string) {
434 testName := "go_test_bench:" + pkg
435 if t.runRx == nil || t.runRx.MatchString(testName) == t.runRxWant {
436 benchMatches = append(benchMatches, pkg)
438 t.tests = append(t.tests, distTest{
440 heading: "Running benchmarks briefly.",
441 fn: func(dt *distTest) error {
446 timelog("start", dt.name)
447 defer timelog("end", dt.name)
453 t.timeout(1200), // longer timeout for race with benchmarks
454 "-run=^$", // nothing. only benchmarks.
459 args = append(args, "-bench=.*")
461 args = append(args, benchMatches...)
462 cmd := exec.Command(gorootBinGo, args...)
463 cmd.Stdout = os.Stdout
464 cmd.Stderr = os.Stderr
470 // stdOutErrAreTerminals is defined in test_linux.go, to report
471 // whether stdout & stderr are terminals.
472 var stdOutErrAreTerminals func() bool
474 func (t *tester) registerTests() {
475 // Fast path to avoid the ~1 second of `go list std cmd` when
476 // the caller lists specific tests to run. (as the continuous
477 // build coordinator does).
478 if len(t.runNames) > 0 {
479 for _, name := range t.runNames {
480 if strings.HasPrefix(name, "go_test:") {
481 t.registerStdTest(strings.TrimPrefix(name, "go_test:"))
483 if strings.HasPrefix(name, "go_test_bench:") {
484 t.registerRaceBenchTest(strings.TrimPrefix(name, "go_test_bench:"))
488 // Use a format string to only list packages and commands that have tests.
489 const format = "{{if (or .TestGoFiles .XTestGoFiles)}}{{.ImportPath}}{{end}}"
490 cmd := exec.Command(gorootBinGo, "list", "-f", format)
492 cmd.Args = append(cmd.Args, "-tags=race")
494 cmd.Args = append(cmd.Args, "std")
495 if t.shouldTestCmd() {
496 cmd.Args = append(cmd.Args, "cmd")
498 cmd.Stderr = new(bytes.Buffer)
499 all, err := cmd.Output()
501 fatalf("Error running go list std cmd: %v:\n%s", err, cmd.Stderr)
503 pkgs := strings.Fields(string(all))
504 for _, pkg := range pkgs {
505 t.registerStdTest(pkg)
508 for _, pkg := range pkgs {
509 if t.packageHasBenchmarks(pkg) {
510 t.registerRaceBenchTest(pkg)
516 // Test the os/user package in the pure-Go mode too.
518 t.tests = append(t.tests, distTest{
520 heading: "os/user with tag osusergo",
521 fn: func(dt *distTest) error {
522 t.addCmd(dt, "src", t.goTest(), t.timeout(300), "-tags=osusergo", "os/user")
528 // Test ios/amd64 for the iOS simulator.
529 if goos == "darwin" && goarch == "amd64" && t.cgoEnabled {
530 t.tests = append(t.tests, distTest{
532 heading: "GOOS=ios on darwin/amd64",
533 fn: func(dt *distTest) error {
534 cmd := t.addCmd(dt, "src", t.goTest(), t.timeout(300), "-run=SystemRoots", "crypto/x509")
535 setEnv(cmd, "GOOS", "ios")
536 setEnv(cmd, "CGO_ENABLED", "1")
546 // Runtime CPU tests.
547 if !t.compileOnly && goos != "js" { // js can't handle -cpu != 1
548 testName := "runtime:cpu124"
549 t.tests = append(t.tests, distTest{
551 heading: "GOMAXPROCS=2 runtime -cpu=1,2,4 -quick",
552 fn: func(dt *distTest) error {
553 cmd := t.addCmd(dt, "src", t.goTest(), "-short=true", t.timeout(300), "runtime", "-cpu=1,2,4", "-quick")
554 // We set GOMAXPROCS=2 in addition to -cpu=1,2,4 in order to test runtime bootstrap code,
555 // creation of first goroutines and first garbage collections in the parallel setting.
556 setEnv(cmd, "GOMAXPROCS", "2")
562 // morestack tests. We only run these on in long-test mode
563 // (with GO_TEST_SHORT=false) because the runtime test is
564 // already quite long and mayMoreStackMove makes it about
566 if !t.compileOnly && short() == "false" {
567 // hooks is the set of maymorestack hooks to test with.
568 hooks := []string{"mayMoreStackPreempt", "mayMoreStackMove"}
569 // pkgs is the set of test packages to run.
570 pkgs := []string{"runtime", "reflect", "sync"}
571 // hookPkgs is the set of package patterns to apply
572 // the maymorestack hook to.
573 hookPkgs := []string{"runtime/...", "reflect", "sync"}
574 // unhookPkgs is the set of package patterns to
575 // exclude from hookPkgs.
576 unhookPkgs := []string{"runtime/testdata/..."}
577 for _, hook := range hooks {
578 // Construct the build flags to use the
579 // maymorestack hook in the compiler and
580 // assembler. We pass this via the GOFLAGS
581 // environment variable so that it applies to
582 // both the test itself and to binaries built
584 goFlagsList := []string{}
585 for _, flag := range []string{"-gcflags", "-asmflags"} {
586 for _, hookPkg := range hookPkgs {
587 goFlagsList = append(goFlagsList, flag+"="+hookPkg+"=-d=maymorestack=runtime."+hook)
589 for _, unhookPkg := range unhookPkgs {
590 goFlagsList = append(goFlagsList, flag+"="+unhookPkg+"=")
593 goFlags := strings.Join(goFlagsList, " ")
595 for _, pkg := range pkgs {
597 testName := hook + ":" + pkg
598 t.tests = append(t.tests, distTest{
600 heading: "maymorestack=" + hook,
601 fn: func(dt *distTest) error {
602 cmd := t.addCmd(dt, "src", t.goTest(), t.timeout(600), pkg, "-short")
603 setEnv(cmd, "GOFLAGS", goFlags)
611 // This test needs its stdout/stderr to be terminals, so we don't run it from cmd/go's tests.
614 t.tests = append(t.tests, distTest{
615 name: "cmd_go_test_terminal",
616 heading: "cmd/go terminal test",
617 fn: func(dt *distTest) error {
619 timelog("start", dt.name)
620 defer timelog("end", dt.name)
621 if !stdOutErrAreTerminals() {
622 fmt.Println("skipping terminal test; stdout/stderr not terminals")
625 cmd := exec.Command(gorootBinGo, "test")
626 setDir(cmd, filepath.Join(os.Getenv("GOROOT"), "src/cmd/go/testdata/testterminal18153"))
627 cmd.Stdout = os.Stdout
628 cmd.Stderr = os.Stderr
634 // On the builders only, test that a moved GOROOT still works.
635 // Fails on iOS because CC_FOR_TARGET refers to clangwrap.sh
636 // in the unmoved GOROOT.
637 // Fails on Android and js/wasm with an exec format error.
638 // Fails on plan9 with "cannot find GOROOT" (issue #21016).
639 if os.Getenv("GO_BUILDER_NAME") != "" && goos != "android" && !t.iOS() && goos != "plan9" && goos != "js" {
640 t.tests = append(t.tests, distTest{
641 name: "moved_goroot",
642 heading: "moved GOROOT",
643 fn: func(dt *distTest) error {
645 timelog("start", dt.name)
646 defer timelog("end", dt.name)
647 moved := goroot + "-moved"
648 if err := os.Rename(goroot, moved); err != nil {
649 if goos == "windows" {
650 // Fails on Windows (with "Access is denied") if a process
651 // or binary is in this directory. For instance, using all.bat
652 // when run from c:\workdir\go\src fails here
653 // if GO_BUILDER_NAME is set. Our builders invoke tests
654 // a different way which happens to work when sharding
655 // tests, but we should be tolerant of the non-sharded
657 log.Printf("skipping test on Windows")
663 // Run `go test fmt` in the moved GOROOT, without explicitly setting
664 // GOROOT in the environment. The 'go' command should find itself.
665 cmd := exec.Command(filepath.Join(moved, "bin", "go"), "test", "fmt")
666 cmd.Stdout = os.Stdout
667 cmd.Stderr = os.Stderr
668 unsetEnv(cmd, "GOROOT")
669 unsetEnv(cmd, "GOCACHE") // TODO(bcmills): ...why‽
672 if rerr := os.Rename(moved, goroot); rerr != nil {
673 fatalf("failed to restore GOROOT: %v", rerr)
680 // Test that internal linking of standard packages does not
681 // require libgcc. This ensures that we can install a Go
682 // release on a system that does not have a C compiler
683 // installed and still build Go programs (that don't use cgo).
684 for _, pkg := range cgoPackages {
685 if !t.internalLink() {
689 // ARM libgcc may be Thumb, which internal linking does not support.
697 run = "TestTCPStress"
699 t.tests = append(t.tests, distTest{
700 name: "nolibgcc:" + pkg,
701 heading: "Testing without libgcc.",
702 fn: func(dt *distTest) error {
703 // What matters is that the tests build and start up.
704 // Skip expensive tests, especially x509 TestSystemRoots.
705 t.addCmd(dt, "src", t.goTest(), "-ldflags=-linkmode=internal -libgcc=none", "-run=^Test[^CS]", pkg, t.runFlag(run))
711 // Stub out following test on alpine until 54354 resolved.
712 builderName := os.Getenv("GO_BUILDER_NAME")
713 disablePIE := strings.HasSuffix(builderName, "-alpine")
715 // Test internal linking of PIE binaries where it is supported.
716 if t.internalLinkPIE() && !disablePIE {
717 t.tests = append(t.tests, distTest{
718 name: "pie_internal",
719 heading: "internal linking of -buildmode=pie",
720 fn: func(dt *distTest) error {
721 t.addCmd(dt, "src", t.goTest(), "reflect", "-buildmode=pie", "-ldflags=-linkmode=internal", t.timeout(60))
725 // Also test a cgo package.
726 if t.cgoEnabled && t.internalLink() && !disablePIE {
727 t.tests = append(t.tests, distTest{
728 name: "pie_internal_cgo",
729 heading: "internal linking of -buildmode=pie",
730 fn: func(dt *distTest) error {
731 t.addCmd(dt, "src", t.goTest(), "os/user", "-buildmode=pie", "-ldflags=-linkmode=internal", t.timeout(60))
739 if goos != "js" { // js doesn't support -cpu=10
740 t.tests = append(t.tests, distTest{
742 heading: "sync -cpu=10",
743 fn: func(dt *distTest) error {
744 t.addCmd(dt, "src", t.goTest(), "sync", t.timeout(120), "-cpu=10", t.runFlag(""))
750 if t.raceDetectorSupported() {
751 t.tests = append(t.tests, distTest{
753 heading: "Testing race detector",
758 if t.cgoEnabled && !t.iOS() {
759 // Disabled on iOS. golang.org/issue/15919
760 t.registerHostTest("cgo_stdio", "../misc/cgo/stdio", "misc/cgo/stdio", ".")
761 t.registerHostTest("cgo_life", "../misc/cgo/life", "misc/cgo/life", ".")
762 fortran := os.Getenv("FC")
764 fortran, _ = exec.LookPath("gfortran")
766 if t.hasBash() && goos != "android" && fortran != "" {
767 t.tests = append(t.tests, distTest{
769 heading: "../misc/cgo/fortran",
770 fn: func(dt *distTest) error {
771 t.addCmd(dt, "misc/cgo/fortran", "./test.bash", fortran)
776 if t.hasSwig() && goos != "android" {
777 t.tests = append(t.tests, distTest{
779 heading: "../misc/swig/stdio",
780 fn: func(dt *distTest) error {
781 t.addCmd(dt, "misc/swig/stdio", t.goTest(), ".")
786 t.tests = append(t.tests,
788 name: "swig_callback",
789 heading: "../misc/swig/callback",
790 fn: func(dt *distTest) error {
791 t.addCmd(dt, "misc/swig/callback", t.goTest(), ".")
796 name: "swig_callback_lto",
797 heading: "../misc/swig/callback",
798 fn: func(dt *distTest) error {
799 cmd := t.addCmd(dt, "misc/swig/callback", t.goTest(), ".")
800 setEnv(cmd, "CGO_CFLAGS", "-flto -Wno-lto-type-mismatch -Wno-unknown-warning-option")
801 setEnv(cmd, "CGO_CXXFLAGS", "-flto -Wno-lto-type-mismatch -Wno-unknown-warning-option")
802 setEnv(cmd, "CGO_LDFLAGS", "-flto -Wno-lto-type-mismatch -Wno-unknown-warning-option")
811 t.tests = append(t.tests, distTest{
813 heading: "../misc/cgo/test",
818 // Don't run these tests with $GO_GCFLAGS because most of them
819 // assume that they can run "go install" with no -gcflags and not
820 // recompile the entire standard library. If make.bash ran with
821 // special -gcflags, that's not true.
822 if t.cgoEnabled && gogcflags == "" {
823 t.registerHostTest("testgodefs", "../misc/cgo/testgodefs", "misc/cgo/testgodefs", ".")
825 t.registerTest("testso", "../misc/cgo/testso", t.goTest(), t.timeout(600), ".")
826 t.registerTest("testsovar", "../misc/cgo/testsovar", t.goTest(), t.timeout(600), ".")
827 if t.supportedBuildmode("c-archive") {
828 t.registerHostTest("testcarchive", "../misc/cgo/testcarchive", "misc/cgo/testcarchive", ".")
830 if t.supportedBuildmode("c-shared") {
831 t.registerHostTest("testcshared", "../misc/cgo/testcshared", "misc/cgo/testcshared", ".")
833 if t.supportedBuildmode("shared") {
834 t.registerTest("testshared", "../misc/cgo/testshared", t.goTest(), t.timeout(600), ".")
836 if t.supportedBuildmode("plugin") {
837 t.registerTest("testplugin", "../misc/cgo/testplugin", t.goTest(), t.timeout(600), ".")
839 if gohostos == "linux" && (goarch == "amd64" || goarch == "ppc64le") {
840 t.registerTest("testasan", "../misc/cgo/testasan", "go", "run", ".")
843 // because syscall.SysProcAttr struct used in misc/cgo/testsanitizers is only built on linux.
844 t.registerHostTest("testsanitizers", "../misc/cgo/testsanitizers", "misc/cgo/testsanitizers", ".")
846 if t.hasBash() && goos != "android" && !t.iOS() && gohostos != "windows" {
847 t.registerHostTest("cgo_errors", "../misc/cgo/errors", "misc/cgo/errors", ".")
849 if gohostos == "linux" && t.extLink() {
850 t.registerTest("testsigfwd", "../misc/cgo/testsigfwd", "go", "run", ".")
854 if goos != "android" && !t.iOS() {
855 // There are no tests in this directory, only benchmarks.
856 // Check that the test binary builds but don't bother running it.
857 // (It has init-time work to set up for the benchmarks that is not worth doing unnecessarily.)
858 t.registerTest("bench_go1", "../test/bench/go1", t.goTest(), "-c", "-o="+os.DevNull)
860 if goos != "android" && !t.iOS() {
861 // Only start multiple test dir shards on builders,
862 // where they get distributed to multiple machines.
863 // See issues 20141 and 31834.
865 if os.Getenv("GO_BUILDER_NAME") != "" {
868 if n, err := strconv.Atoi(os.Getenv("GO_TEST_SHARDS")); err == nil {
871 for shard := 0; shard < nShards; shard++ {
873 t.tests = append(t.tests, distTest{
874 name: fmt.Sprintf("test:%d_%d", shard, nShards),
876 fn: func(dt *distTest) error { return t.testDirTest(dt, shard, nShards) },
880 // Only run the API check on fast development platforms.
881 // Every platform checks the API on every GOOS/GOARCH/CGO_ENABLED combination anyway,
882 // so we really only need to run this check once anywhere to get adequate coverage.
883 // To help developers avoid trybot-only failures, we try to run on typical developer machines
884 // which is darwin/linux/windows and amd64/arm64.
885 if (goos == "darwin" || goos == "linux" || goos == "windows") && (goarch == "amd64" || goarch == "arm64") {
886 t.tests = append(t.tests, distTest{
888 heading: "API check",
889 fn: func(dt *distTest) error {
891 t.addCmd(dt, "src", "go", "build", "-o", os.DevNull, filepath.Join(goroot, "src/cmd/api/run.go"))
894 t.addCmd(dt, "src", "go", "run", filepath.Join(goroot, "src/cmd/api/run.go"))
900 // Ensure that the toolchain can bootstrap itself.
901 // This test adds another ~45s to all.bash if run sequentially, so run it only on the builders.
902 if os.Getenv("GO_BUILDER_NAME") != "" && goos != "android" && !t.iOS() {
903 t.registerHostTest("reboot", "../misc/reboot", "misc/reboot", ".")
907 // isRegisteredTestName reports whether a test named testName has already
909 func (t *tester) isRegisteredTestName(testName string) bool {
910 for _, tt := range t.tests {
911 if tt.name == testName {
918 func (t *tester) registerTest1(seq bool, name, dirBanner string, cmdline ...interface{}) {
919 bin, args := flattenCmdline(cmdline)
920 if bin == "time" && !t.haveTime {
921 bin, args = args[0], args[1:]
923 if t.isRegisteredTestName(name) {
924 panic("duplicate registered test name " + name)
926 t.tests = append(t.tests, distTest{
929 fn: func(dt *distTest) error {
932 timelog("start", name)
933 defer timelog("end", name)
934 return t.dirCmd(filepath.Join(goroot, "src", dirBanner), bin, args).Run()
936 t.addCmd(dt, filepath.Join(goroot, "src", dirBanner), bin, args)
942 func (t *tester) registerTest(name, dirBanner string, cmdline ...interface{}) {
943 t.registerTest1(false, name, dirBanner, cmdline...)
946 func (t *tester) registerSeqTest(name, dirBanner string, cmdline ...interface{}) {
947 t.registerTest1(true, name, dirBanner, cmdline...)
950 func (t *tester) bgDirCmd(dir, bin string, args ...string) *exec.Cmd {
951 cmd := exec.Command(bin, args...)
952 if filepath.IsAbs(dir) {
955 setDir(cmd, filepath.Join(goroot, dir))
960 func (t *tester) dirCmd(dir string, cmdline ...interface{}) *exec.Cmd {
961 bin, args := flattenCmdline(cmdline)
962 cmd := t.bgDirCmd(dir, bin, args...)
963 cmd.Stdout = os.Stdout
964 cmd.Stderr = os.Stderr
966 errprintf("%s\n", strings.Join(cmd.Args, " "))
971 // flattenCmdline flattens a mixture of string and []string as single list
972 // and then interprets it as a command line: first element is binary, then args.
973 func flattenCmdline(cmdline []interface{}) (bin string, args []string) {
975 for _, x := range cmdline {
976 switch x := x.(type) {
978 list = append(list, x)
980 list = append(list, x...)
982 panic("invalid addCmd argument type: " + reflect.TypeOf(x).String())
986 // The go command is too picky about duplicated flags.
987 // Drop all but the last of the allowed duplicated flags.
988 drop := make([]bool, len(list))
989 have := map[string]int{}
990 for i := 1; i < len(list); i++ {
991 j := strings.Index(list[i], "=")
997 case "-run", "-tags":
999 drop[have[flag]] = true
1005 for i, x := range list {
1007 out = append(out, x)
1016 return bin, list[1:]
1019 func (t *tester) addCmd(dt *distTest, dir string, cmdline ...interface{}) *exec.Cmd {
1020 bin, args := flattenCmdline(cmdline)
1023 cmd: t.bgDirCmd(dir, bin, args...),
1025 t.worklist = append(t.worklist, w)
1029 func (t *tester) iOS() bool {
1030 return goos == "ios"
1033 func (t *tester) out(v string) {
1037 fmt.Println("\n" + t.banner + v)
1040 func (t *tester) extLink() bool {
1041 pair := gohostos + "-" + goarch
1044 "android-arm", "android-arm64",
1045 "darwin-amd64", "darwin-arm64",
1047 "freebsd-386", "freebsd-amd64", "freebsd-arm",
1048 "linux-386", "linux-amd64", "linux-arm", "linux-arm64", "linux-loong64", "linux-ppc64le", "linux-mips64", "linux-mips64le", "linux-mips", "linux-mipsle", "linux-riscv64", "linux-s390x",
1049 "netbsd-386", "netbsd-amd64",
1050 "openbsd-386", "openbsd-amd64",
1051 "windows-386", "windows-amd64":
1057 func (t *tester) internalLink() bool {
1058 if gohostos == "dragonfly" {
1059 // linkmode=internal fails on dragonfly since errno is a TLS relocation.
1062 if goos == "android" {
1068 if goos == "windows" && goarch == "arm64" {
1071 // Internally linking cgo is incomplete on some architectures.
1072 // https://golang.org/issue/10373
1073 // https://golang.org/issue/14449
1074 if goarch == "loong64" || goarch == "mips64" || goarch == "mips64le" || goarch == "mips" || goarch == "mipsle" || goarch == "riscv64" {
1078 // linkmode=internal isn't supported.
1084 func (t *tester) internalLinkPIE() bool {
1085 switch goos + "-" + goarch {
1086 case "darwin-amd64", "darwin-arm64",
1087 "linux-amd64", "linux-arm64", "linux-ppc64le",
1089 "windows-amd64", "windows-386", "windows-arm":
1095 func (t *tester) supportedBuildmode(mode string) bool {
1096 pair := goos + "-" + goarch
1104 "darwin-amd64", "darwin-arm64", "ios-arm64",
1105 "linux-amd64", "linux-386", "linux-ppc64le", "linux-riscv64", "linux-s390x",
1107 "windows-amd64", "windows-386":
1113 case "linux-386", "linux-amd64", "linux-arm", "linux-arm64", "linux-ppc64le", "linux-riscv64", "linux-s390x",
1114 "darwin-amd64", "darwin-arm64",
1116 "android-arm", "android-arm64", "android-386",
1117 "windows-amd64", "windows-386", "windows-arm64":
1123 case "linux-386", "linux-amd64", "linux-arm", "linux-arm64", "linux-ppc64le", "linux-s390x":
1129 case "linux-386", "linux-amd64", "linux-arm", "linux-arm64", "linux-s390x", "linux-ppc64le":
1131 case "darwin-amd64", "darwin-arm64":
1133 case "freebsd-amd64":
1140 "linux-386", "linux-amd64", "linux-arm", "linux-arm64", "linux-ppc64le", "linux-riscv64", "linux-s390x",
1141 "android-amd64", "android-arm", "android-arm64", "android-386":
1143 case "darwin-amd64", "darwin-arm64":
1145 case "windows-amd64", "windows-386", "windows-arm":
1151 fatalf("internal error: unknown buildmode %s", mode)
1156 func (t *tester) registerHostTest(name, heading, dir, pkg string) {
1157 t.tests = append(t.tests, distTest{
1160 fn: func(dt *distTest) error {
1162 timelog("start", name)
1163 defer timelog("end", name)
1164 return t.runHostTest(dir, pkg)
1169 func (t *tester) runHostTest(dir, pkg string) error {
1170 out, err := exec.Command(gorootBinGo, "env", "GOEXE", "GOTMPDIR").Output()
1175 parts := strings.Split(string(out), "\n")
1177 return fmt.Errorf("'go env GOEXE GOTMPDIR' output contains <2 lines")
1179 GOEXE := strings.TrimSpace(parts[0])
1180 GOTMPDIR := strings.TrimSpace(parts[1])
1182 f, err := ioutil.TempFile(GOTMPDIR, "test.test-*"+GOEXE)
1187 defer os.Remove(f.Name())
1189 cmd := t.dirCmd(dir, t.goTest(), "-c", "-o", f.Name(), pkg)
1190 setEnv(cmd, "GOARCH", gohostarch)
1191 setEnv(cmd, "GOOS", gohostos)
1192 if err := cmd.Run(); err != nil {
1195 return t.dirCmd(dir, f.Name(), "-test.short="+short(), "-test.timeout="+t.timeoutDuration(300).String()).Run()
1198 func (t *tester) cgoTest(dt *distTest) error {
1199 cmd := t.addCmd(dt, "misc/cgo/test", t.goTest(), ".")
1200 setEnv(cmd, "GOFLAGS", "-ldflags=-linkmode=auto")
1202 // Stub out various buildmode=pie tests on alpine until 54354 resolved.
1203 builderName := os.Getenv("GO_BUILDER_NAME")
1204 disablePIE := strings.HasSuffix(builderName, "-alpine")
1206 if t.internalLink() {
1207 cmd := t.addCmd(dt, "misc/cgo/test", t.goTest(), "-tags=internal", ".")
1208 setEnv(cmd, "GOFLAGS", "-ldflags=-linkmode=internal")
1211 pair := gohostos + "-" + goarch
1213 case "darwin-amd64", "darwin-arm64",
1214 "windows-386", "windows-amd64":
1215 // test linkmode=external, but __thread not supported, so skip testtls.
1219 cmd := t.addCmd(dt, "misc/cgo/test", t.goTest(), ".")
1220 setEnv(cmd, "GOFLAGS", "-ldflags=-linkmode=external")
1222 t.addCmd(dt, "misc/cgo/test", t.goTest(), "-ldflags", "-linkmode=external -s", ".")
1224 if t.supportedBuildmode("pie") && !disablePIE {
1226 t.addCmd(dt, "misc/cgo/test", t.goTest(), "-buildmode=pie", ".")
1227 if t.internalLink() && t.internalLinkPIE() {
1228 t.addCmd(dt, "misc/cgo/test", t.goTest(), "-buildmode=pie", "-ldflags=-linkmode=internal", "-tags=internal,internal_pie", ".")
1233 "android-arm", "android-arm64",
1235 "freebsd-386", "freebsd-amd64", "freebsd-arm",
1236 "linux-386", "linux-amd64", "linux-arm", "linux-arm64", "linux-ppc64le", "linux-riscv64", "linux-s390x",
1237 "netbsd-386", "netbsd-amd64",
1238 "openbsd-386", "openbsd-amd64", "openbsd-arm", "openbsd-arm64", "openbsd-mips64":
1240 cmd := t.addCmd(dt, "misc/cgo/test", t.goTest(), ".")
1241 setEnv(cmd, "GOFLAGS", "-ldflags=-linkmode=external")
1242 // cgo should be able to cope with both -g arguments and colored
1244 setEnv(cmd, "CGO_CFLAGS", "-g0 -fdiagnostics-color")
1246 t.addCmd(dt, "misc/cgo/testtls", t.goTest(), "-ldflags", "-linkmode=auto", ".")
1247 t.addCmd(dt, "misc/cgo/testtls", t.goTest(), "-ldflags", "-linkmode=external", ".")
1250 case "aix-ppc64", "netbsd-386", "netbsd-amd64":
1251 // no static linking
1253 // -fPIC compiled tls code will use __tls_get_addr instead
1254 // of __aeabi_read_tp, however, on FreeBSD/ARM, __tls_get_addr
1255 // is implemented in rtld-elf, so -fPIC isn't compatible with
1256 // static linking on FreeBSD/ARM with clang. (cgo depends on
1257 // -fPIC fundamentally.)
1259 cmd := t.dirCmd("misc/cgo/test",
1260 compilerEnvLookup(defaultcc, goos, goarch), "-xc", "-o", "/dev/null", "-static", "-")
1261 cmd.Stdin = strings.NewReader("int main() {}")
1262 if err := cmd.Run(); err != nil {
1263 fmt.Println("No support for static linking found (lacks libc.a?), skip cgo static linking test.")
1265 if goos != "android" {
1266 t.addCmd(dt, "misc/cgo/testtls", t.goTest(), "-ldflags", `-linkmode=external -extldflags "-static -pthread"`, ".")
1268 t.addCmd(dt, "misc/cgo/nocgo", t.goTest(), ".")
1269 t.addCmd(dt, "misc/cgo/nocgo", t.goTest(), "-ldflags", `-linkmode=external`, ".")
1270 if goos != "android" {
1271 t.addCmd(dt, "misc/cgo/nocgo", t.goTest(), "-ldflags", `-linkmode=external -extldflags "-static -pthread"`, ".")
1272 t.addCmd(dt, "misc/cgo/test", t.goTest(), "-tags=static", "-ldflags", `-linkmode=external -extldflags "-static -pthread"`, ".")
1273 // -static in CGO_LDFLAGS triggers a different code path
1274 // than -static in -extldflags, so test both.
1275 // See issue #16651.
1276 cmd := t.addCmd(dt, "misc/cgo/test", t.goTest(), "-tags=static", ".")
1277 setEnv(cmd, "CGO_LDFLAGS", "-static -pthread")
1281 if t.supportedBuildmode("pie") && !disablePIE {
1282 t.addCmd(dt, "misc/cgo/test", t.goTest(), "-buildmode=pie", ".")
1283 if t.internalLink() && t.internalLinkPIE() {
1284 t.addCmd(dt, "misc/cgo/test", t.goTest(), "-buildmode=pie", "-ldflags=-linkmode=internal", "-tags=internal,internal_pie", ".")
1286 t.addCmd(dt, "misc/cgo/testtls", t.goTest(), "-buildmode=pie", ".")
1287 t.addCmd(dt, "misc/cgo/nocgo", t.goTest(), "-buildmode=pie", ".")
1295 // run pending test commands, in parallel, emitting headers as appropriate.
1296 // When finished, emit header for nextTest, which is going to run after the
1297 // pending commands are done (and runPending returns).
1298 // A test should call runPending if it wants to make sure that it is not
1299 // running in parallel with earlier tests, or if it has some other reason
1300 // for needing the earlier tests to be done.
1301 func (t *tester) runPending(nextTest *distTest) {
1302 checkNotStale("go", "std")
1303 worklist := t.worklist
1305 for _, w := range worklist {
1306 w.start = make(chan bool)
1307 w.end = make(chan bool)
1310 timelog("skip", w.dt.name)
1311 w.out = []byte(fmt.Sprintf("skipped due to earlier error\n"))
1313 timelog("start", w.dt.name)
1314 w.out, w.err = w.cmd.CombinedOutput()
1316 if isUnsupportedVMASize(w) {
1317 timelog("skip", w.dt.name)
1318 w.out = []byte(fmt.Sprintf("skipped due to unsupported VMA\n"))
1323 timelog("end", w.dt.name)
1331 for ended < len(worklist) {
1332 for started < len(worklist) && started-ended < maxbg {
1333 w := worklist[started]
1335 w.start <- !t.failed || t.keepGoing
1337 w := worklist[ended]
1339 if dt.heading != "" && t.lastHeading != dt.heading {
1340 t.lastHeading = dt.heading
1344 // Assumes all the entries for a single dt are in one worklist.
1347 fmt.Printf("# go tool dist test -run=^%s$\n", dt.name)
1351 errprintf("%s\n", strings.Join(w.cmd.Args, " "))
1355 os.Stdout.Write(w.out)
1357 log.Printf("Failed: %v", w.err)
1360 checkNotStale("go", "std")
1362 if t.failed && !t.keepGoing {
1366 if dt := nextTest; dt != nil {
1367 if dt.heading != "" && t.lastHeading != dt.heading {
1368 t.lastHeading = dt.heading
1372 fmt.Printf("# go tool dist test -run=^%s$\n", dt.name)
1377 func (t *tester) hasBash() bool {
1379 case "windows", "plan9":
1385 func (t *tester) hasCxx() bool {
1386 cxx, _ := exec.LookPath(compilerEnvLookup(defaultcxx, goos, goarch))
1390 func (t *tester) hasSwig() bool {
1391 swig, err := exec.LookPath("swig")
1396 // Check that swig was installed with Go support by checking
1397 // that a go directory exists inside the swiglib directory.
1398 // See https://golang.org/issue/23469.
1399 output, err := exec.Command(swig, "-go", "-swiglib").Output()
1403 swigDir := strings.TrimSpace(string(output))
1405 _, err = os.Stat(filepath.Join(swigDir, "go"))
1410 // Check that swig has a new enough version.
1411 // See https://golang.org/issue/22858.
1412 out, err := exec.Command(swig, "-version").CombinedOutput()
1417 re := regexp.MustCompile(`[vV]ersion +([\d]+)([.][\d]+)?([.][\d]+)?`)
1418 matches := re.FindSubmatch(out)
1420 // Can't find version number; hope for the best.
1424 major, err := strconv.Atoi(string(matches[1]))
1426 // Can't find version number; hope for the best.
1437 // We have SWIG version 3.x.
1438 if len(matches[2]) > 0 {
1439 minor, err := strconv.Atoi(string(matches[2][1:]))
1449 // We have SWIG version 3.0.x.
1450 if len(matches[3]) > 0 {
1451 patch, err := strconv.Atoi(string(matches[3][1:]))
1464 func (t *tester) raceDetectorSupported() bool {
1465 if gohostos != goos {
1471 if !raceDetectorSupported(goos, goarch) {
1474 // The race detector doesn't work on Alpine Linux:
1475 // golang.org/issue/14481
1476 if isAlpineLinux() {
1479 // NetBSD support is unfinished.
1480 // golang.org/issue/26403
1481 if goos == "netbsd" {
1487 func isAlpineLinux() bool {
1488 if runtime.GOOS != "linux" {
1491 fi, err := os.Lstat("/etc/alpine-release")
1492 return err == nil && fi.Mode().IsRegular()
1495 func (t *tester) runFlag(rx string) string {
1502 func (t *tester) raceTest(dt *distTest) error {
1503 t.addCmd(dt, "src", t.goTest(), "-race", t.runFlag("Output"), "runtime/race")
1504 t.addCmd(dt, "src", t.goTest(), "-race", t.runFlag("TestParse|TestEcho|TestStdinCloseRace|TestClosedPipeRace|TestTypeRace|TestFdRace|TestFdReadRace|TestFileCloseRace"), "flag", "net", "os", "os/exec", "encoding/gob")
1505 // We don't want the following line, because it
1506 // slows down all.bash (by 10 seconds on my laptop).
1507 // The race builder should catch any error here, but doesn't.
1508 // TODO(iant): Figure out how to catch this.
1509 // t.addCmd(dt, "src", t.goTest(), "-race", "-run=TestParallelTest", "cmd/go")
1511 // Building misc/cgo/test takes a long time.
1512 // There are already cgo-enabled packages being tested with the race detector.
1513 // We shouldn't need to redo all of misc/cgo/test too.
1514 // The race buildler will take care of this.
1515 // cmd := t.addCmd(dt, "misc/cgo/test", t.goTest(), "-race")
1516 // setEnv(cmd, "GOTRACEBACK", "2")
1519 // Test with external linking; see issue 9133.
1520 t.addCmd(dt, "src", t.goTest(), "-race", "-ldflags=-linkmode=external", t.runFlag("TestParse|TestEcho|TestStdinCloseRace"), "flag", "os/exec")
1525 var runtest struct {
1531 func (t *tester) testDirTest(dt *distTest, shard, shards int) error {
1533 f, err := ioutil.TempFile("", "runtest-*.exe") // named exe for Windows, but harmless elsewhere
1540 runtest.exe = f.Name()
1542 os.Remove(runtest.exe)
1545 cmd := t.dirCmd("test", "go", "build", "-o", runtest.exe, "run.go")
1546 setEnv(cmd, "GOOS", gohostos)
1547 setEnv(cmd, "GOARCH", gohostarch)
1548 runtest.err = cmd.Run()
1550 if runtest.err != nil {
1556 t.addCmd(dt, "test", runtest.exe,
1557 fmt.Sprintf("--shard=%d", shard),
1558 fmt.Sprintf("--shards=%d", shards),
1563 // cgoPackages is the standard packages that use cgo.
1564 var cgoPackages = []string{
1569 var funcBenchmark = []byte("\nfunc Benchmark")
1571 // packageHasBenchmarks reports whether pkg has benchmarks.
1572 // On any error, it conservatively returns true.
1574 // This exists just to eliminate work on the builders, since compiling
1575 // a test in race mode just to discover it has no benchmarks costs a
1576 // second or two per package, and this function returns false for
1577 // about 100 packages.
1578 func (t *tester) packageHasBenchmarks(pkg string) bool {
1579 pkgDir := filepath.Join(goroot, "src", pkg)
1580 d, err := os.Open(pkgDir)
1582 return true // conservatively
1585 names, err := d.Readdirnames(-1)
1587 return true // conservatively
1589 for _, name := range names {
1590 if !strings.HasSuffix(name, "_test.go") {
1593 slurp, err := ioutil.ReadFile(filepath.Join(pkgDir, name))
1595 return true // conservatively
1597 if bytes.Contains(slurp, funcBenchmark) {
1604 // makeGOROOTUnwritable makes all $GOROOT files & directories non-writable to
1605 // check that no tests accidentally write to $GOROOT.
1606 func (t *tester) makeGOROOTUnwritable() (undo func()) {
1607 dir := os.Getenv("GOROOT")
1609 panic("GOROOT not set")
1612 type pathMode struct {
1616 var dirs []pathMode // in lexical order
1619 for i := range dirs {
1620 os.Chmod(dirs[i].path, dirs[i].mode) // best effort
1624 gocache := os.Getenv("GOCACHE")
1626 panic("GOCACHE not set")
1628 gocacheSubdir, _ := filepath.Rel(dir, gocache)
1630 // Note: Can't use WalkDir here, because this has to compile with Go 1.4.
1631 filepath.Walk(dir, func(path string, info os.FileInfo, err error) error {
1632 if suffix := strings.TrimPrefix(path, dir+string(filepath.Separator)); suffix != "" {
1633 if suffix == gocacheSubdir {
1634 // Leave GOCACHE writable: we may need to write test binaries into it.
1635 return filepath.SkipDir
1637 if suffix == ".git" {
1638 // Leave Git metadata in whatever state it was in. It may contain a lot
1639 // of files, and it is highly unlikely that a test will try to modify
1640 // anything within that directory.
1641 return filepath.SkipDir
1646 if mode&0222 != 0 && (mode.IsDir() || mode.IsRegular()) {
1647 dirs = append(dirs, pathMode{path, mode})
1653 // Run over list backward to chmod children before parents.
1654 for i := len(dirs) - 1; i >= 0; i-- {
1655 err := os.Chmod(dirs[i].path, dirs[i].mode&^0222)
1657 dirs = dirs[i:] // Only undo what we did so far.
1659 fatalf("failed to make GOROOT read-only: %v", err)
1666 // shouldUsePrecompiledStdTest reports whether "dist test" should use
1667 // a pre-compiled go test binary on disk rather than running "go test"
1668 // and compiling it again. This is used by our slow qemu-based builder
1669 // that do full processor emulation where we cross-compile the
1670 // make.bash step as well as pre-compile each std test binary.
1672 // This only reports true if dist is run with an single go_test:foo
1673 // argument (as the build coordinator does with our slow qemu-based
1674 // builders), we're in a builder environment ("GO_BUILDER_NAME" is set),
1675 // and the pre-built test binary exists.
1676 func (t *tester) shouldUsePrecompiledStdTest() bool {
1677 bin := t.prebuiltGoPackageTestBinary()
1681 _, err := os.Stat(bin)
1685 func (t *tester) shouldTestCmd() bool {
1686 if goos == "js" && goarch == "wasm" {
1687 // Issues 25911, 35220
1693 // prebuiltGoPackageTestBinary returns the path where we'd expect
1694 // the pre-built go test binary to be on disk when dist test is run with
1695 // a single argument.
1696 // It returns an empty string if a pre-built binary should not be used.
1697 func (t *tester) prebuiltGoPackageTestBinary() string {
1698 if len(stdMatches) != 1 || t.race || t.compileOnly || os.Getenv("GO_BUILDER_NAME") == "" {
1701 pkg := stdMatches[0]
1702 return filepath.Join(os.Getenv("GOROOT"), "src", pkg, path.Base(pkg)+".test")
1705 // runPrecompiledStdTest runs the pre-compiled standard library package test binary.
1706 // See shouldUsePrecompiledStdTest above; it must return true for this to be called.
1707 func (t *tester) runPrecompiledStdTest(timeout time.Duration) error {
1708 bin := t.prebuiltGoPackageTestBinary()
1709 fmt.Fprintf(os.Stderr, "# %s: using pre-built %s...\n", stdMatches[0], bin)
1710 cmd := exec.Command(bin, "-test.short="+short(), "-test.timeout="+timeout.String())
1711 setDir(cmd, filepath.Dir(bin))
1712 cmd.Stdout = os.Stdout
1713 cmd.Stderr = os.Stderr
1714 if err := cmd.Start(); err != nil {
1717 // And start a timer to kill the process if it doesn't kill
1718 // itself in the prescribed timeout.
1719 const backupKillFactor = 1.05 // add 5%
1720 timer := time.AfterFunc(time.Duration(float64(timeout)*backupKillFactor), func() {
1721 fmt.Fprintf(os.Stderr, "# %s: timeout running %s; killing...\n", stdMatches[0], bin)
1728 // raceDetectorSupported is a copy of the function
1729 // cmd/internal/sys.RaceDetectorSupported, which can't be used here
1730 // because cmd/dist has to be buildable by Go 1.4.
1731 // The race detector only supports 48-bit VMA on arm64. But we don't have
1732 // a good solution to check VMA size(See https://golang.org/issue/29948)
1733 // raceDetectorSupported will always return true for arm64. But race
1734 // detector tests may abort on non 48-bit VMA configuration, the tests
1735 // will be marked as "skipped" in this case.
1736 func raceDetectorSupported(goos, goarch string) bool {
1739 return goarch == "amd64" || goarch == "ppc64le" || goarch == "arm64" || goarch == "s390x"
1741 return goarch == "amd64" || goarch == "arm64"
1742 case "freebsd", "netbsd", "openbsd", "windows":
1743 return goarch == "amd64"
1749 // isUnsupportedVMASize reports whether the failure is caused by an unsupported
1750 // VMA for the race detector (for example, running the race detector on an
1751 // arm64 machine configured with 39-bit VMA)
1752 func isUnsupportedVMASize(w *work) bool {
1753 unsupportedVMA := []byte("unsupported VMA range")
1754 return w.dt.name == "race" && bytes.Contains(w.out, unsupportedVMA)
1757 // isEnvSet reports whether the environment variable evar is
1758 // set in the environment.
1759 func isEnvSet(evar string) bool {
1760 evarEq := evar + "="
1761 for _, e := range os.Environ() {
1762 if strings.HasPrefix(e, evarEq) {