]> Cypherpunks.ru repositories - gostls13.git/blob - src/cmd/dist/test.go
2e0072f44fbbc0056f594ebb44a45a49724975fd
[gostls13.git] / src / cmd / dist / test.go
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.
4
5 package main
6
7 import (
8         "bytes"
9         "flag"
10         "fmt"
11         "io/fs"
12         "log"
13         "os"
14         "os/exec"
15         "path/filepath"
16         "reflect"
17         "regexp"
18         "runtime"
19         "strconv"
20         "strings"
21         "time"
22 )
23
24 func cmdtest() {
25         gogcflags = os.Getenv("GO_GCFLAGS")
26         setNoOpt()
27
28         var t tester
29
30         var noRebuild bool
31         flag.BoolVar(&t.listMode, "list", false, "list available tests")
32         flag.BoolVar(&t.rebuild, "rebuild", false, "rebuild everything first")
33         flag.BoolVar(&noRebuild, "no-rebuild", false, "overrides -rebuild (historical dreg)")
34         flag.BoolVar(&t.keepGoing, "k", false, "keep going even when error occurred")
35         flag.BoolVar(&t.race, "race", false, "run in race builder mode (different set of tests)")
36         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.")
37         flag.StringVar(&t.banner, "banner", "##### ", "banner prefix; blank means no section banners")
38         flag.StringVar(&t.runRxStr, "run", "",
39                 "run only those tests matching the regular expression; empty means to run all. "+
40                         "Special exception: if the string begins with '!', the match is inverted.")
41         flag.BoolVar(&t.msan, "msan", false, "run in memory sanitizer builder mode")
42         flag.BoolVar(&t.asan, "asan", false, "run in address sanitizer builder mode")
43
44         xflagparse(-1) // any number of args
45         if noRebuild {
46                 t.rebuild = false
47         }
48
49         t.run()
50 }
51
52 // tester executes cmdtest.
53 type tester struct {
54         race        bool
55         msan        bool
56         asan        bool
57         listMode    bool
58         rebuild     bool
59         failed      bool
60         keepGoing   bool
61         compileOnly bool // just try to compile all tests, but no need to run
62         runRxStr    string
63         runRx       *regexp.Regexp
64         runRxWant   bool     // want runRx to match (true) or not match (false)
65         runNames    []string // tests to run, exclusive with runRx; empty means all
66         banner      string   // prefix, or "" for none
67         lastHeading string   // last dir heading printed
68
69         short      bool
70         cgoEnabled bool
71         partial    bool
72
73         goExe    string // For host tests
74         goTmpDir string // For host tests
75
76         tests        []distTest
77         timeoutScale int
78
79         worklist []*work
80 }
81
82 type work struct {
83         dt    *distTest
84         cmd   *exec.Cmd
85         start chan bool
86         out   []byte
87         err   error
88         end   chan bool
89 }
90
91 // A distTest is a test run by dist test.
92 // Each test has a unique name and belongs to a group (heading)
93 type distTest struct {
94         name    string // unique test name; may be filtered with -run flag
95         heading string // group section; this header is printed before the test is run.
96         fn      func(*distTest) error
97 }
98
99 func (t *tester) run() {
100         timelog("start", "dist test")
101
102         os.Setenv("PATH", fmt.Sprintf("%s%c%s", gorootBin, os.PathListSeparator, os.Getenv("PATH")))
103
104         t.short = true
105         if v := os.Getenv("GO_TEST_SHORT"); v != "" {
106                 short, err := strconv.ParseBool(v)
107                 if err != nil {
108                         fatalf("invalid GO_TEST_SHORT %q: %v", v, err)
109                 }
110                 t.short = short
111         }
112
113         cmd := exec.Command(gorootBinGo, "env", "CGO_ENABLED", "GOEXE", "GOTMPDIR")
114         cmd.Stderr = new(bytes.Buffer)
115         slurp, err := cmd.Output()
116         if err != nil {
117                 fatalf("Error running %s: %v\n%s", cmd, err, cmd.Stderr)
118         }
119         parts := strings.Split(string(slurp), "\n")
120         if len(parts) < 3 {
121                 fatalf("Error running %s: output contains <3 lines\n%s", cmd, cmd.Stderr)
122         }
123         t.cgoEnabled, _ = strconv.ParseBool(parts[0])
124         t.goExe = parts[1]
125         t.goTmpDir = parts[2]
126
127         if flag.NArg() > 0 && t.runRxStr != "" {
128                 fatalf("the -run regular expression flag is mutually exclusive with test name arguments")
129         }
130
131         t.runNames = flag.Args()
132
133         // Set GOTRACEBACK to system if the user didn't set a level explicitly.
134         // Since we're running tests for Go, we want as much detail as possible
135         // if something goes wrong.
136         //
137         // Set it before running any commands just in case something goes wrong.
138         if ok := isEnvSet("GOTRACEBACK"); !ok {
139                 if err := os.Setenv("GOTRACEBACK", "system"); err != nil {
140                         if t.keepGoing {
141                                 log.Printf("Failed to set GOTRACEBACK: %v", err)
142                         } else {
143                                 fatalf("Failed to set GOTRACEBACK: %v", err)
144                         }
145                 }
146         }
147
148         if t.rebuild {
149                 t.out("Building packages and commands.")
150                 // Force rebuild the whole toolchain.
151                 goInstall(toolenv(), gorootBinGo, append([]string{"-a"}, toolchain...)...)
152         }
153
154         if !t.listMode {
155                 if builder := os.Getenv("GO_BUILDER_NAME"); builder == "" {
156                         // Ensure that installed commands are up to date, even with -no-rebuild,
157                         // so that tests that run commands end up testing what's actually on disk.
158                         // If everything is up-to-date, this is a no-op.
159                         // We first build the toolchain twice to allow it to converge,
160                         // as when we first bootstrap.
161                         // See cmdbootstrap for a description of the overall process.
162                         //
163                         // On the builders, we skip this step: we assume that 'dist test' is
164                         // already using the result of a clean build, and because of test sharding
165                         // and virtualization we usually start with a clean GOCACHE, so we would
166                         // end up rebuilding large parts of the standard library that aren't
167                         // otherwise relevant to the actual set of packages under test.
168                         goInstall(toolenv(), gorootBinGo, toolchain...)
169                         goInstall(toolenv(), gorootBinGo, toolchain...)
170                         goInstall(toolenv(), gorootBinGo, "cmd")
171                 }
172         }
173
174         t.timeoutScale = 1
175         if s := os.Getenv("GO_TEST_TIMEOUT_SCALE"); s != "" {
176                 t.timeoutScale, err = strconv.Atoi(s)
177                 if err != nil {
178                         fatalf("failed to parse $GO_TEST_TIMEOUT_SCALE = %q as integer: %v", s, err)
179                 }
180         }
181
182         if t.runRxStr != "" {
183                 if t.runRxStr[0] == '!' {
184                         t.runRxWant = false
185                         t.runRxStr = t.runRxStr[1:]
186                 } else {
187                         t.runRxWant = true
188                 }
189                 t.runRx = regexp.MustCompile(t.runRxStr)
190         }
191
192         t.registerTests()
193         if t.listMode {
194                 for _, tt := range t.tests {
195                         fmt.Println(tt.name)
196                 }
197                 return
198         }
199
200         for _, name := range t.runNames {
201                 if !t.isRegisteredTestName(name) {
202                         fatalf("unknown test %q", name)
203                 }
204         }
205
206         // On a few builders, make GOROOT unwritable to catch tests writing to it.
207         if strings.HasPrefix(os.Getenv("GO_BUILDER_NAME"), "linux-") {
208                 if os.Getuid() == 0 {
209                         // Don't bother making GOROOT unwritable:
210                         // we're running as root, so permissions would have no effect.
211                 } else {
212                         xatexit(t.makeGOROOTUnwritable())
213                 }
214         }
215
216         if err := t.maybeLogMetadata(); err != nil {
217                 t.failed = true
218                 if t.keepGoing {
219                         log.Printf("Failed logging metadata: %v", err)
220                 } else {
221                         fatalf("Failed logging metadata: %v", err)
222                 }
223         }
224
225         for _, dt := range t.tests {
226                 if !t.shouldRunTest(dt.name) {
227                         t.partial = true
228                         continue
229                 }
230                 dt := dt // dt used in background after this iteration
231                 if err := dt.fn(&dt); err != nil {
232                         t.runPending(&dt) // in case that hasn't been done yet
233                         t.failed = true
234                         if t.keepGoing {
235                                 log.Printf("Failed: %v", err)
236                         } else {
237                                 fatalf("Failed: %v", err)
238                         }
239                 }
240         }
241         t.runPending(nil)
242         timelog("end", "dist test")
243
244         if t.failed {
245                 fmt.Println("\nFAILED")
246                 xexit(1)
247         } else if t.partial {
248                 fmt.Println("\nALL TESTS PASSED (some were excluded)")
249         } else {
250                 fmt.Println("\nALL TESTS PASSED")
251         }
252 }
253
254 func (t *tester) shouldRunTest(name string) bool {
255         if t.runRx != nil {
256                 return t.runRx.MatchString(name) == t.runRxWant
257         }
258         if len(t.runNames) == 0 {
259                 return true
260         }
261         for _, runName := range t.runNames {
262                 if runName == name {
263                         return true
264                 }
265         }
266         return false
267 }
268
269 func (t *tester) maybeLogMetadata() error {
270         if t.compileOnly {
271                 // We need to run a subprocess to log metadata. Don't do that
272                 // on compile-only runs.
273                 return nil
274         }
275         t.out("Test execution environment.")
276         // Helper binary to print system metadata (CPU model, etc). This is a
277         // separate binary from dist so it need not build with the bootstrap
278         // toolchain.
279         //
280         // TODO(prattmic): If we split dist bootstrap and dist test then this
281         // could be simplified to directly use internal/sysinfo here.
282         return t.dirCmd(filepath.Join(goroot, "src/cmd/internal/metadata"), gorootBinGo, []string{"run", "main.go"}).Run()
283 }
284
285 // goTest represents all options to a "go test" command. The final command will
286 // combine configuration from goTest and tester flags.
287 type goTest struct {
288         timeout  time.Duration // If non-zero, override timeout
289         short    bool          // If true, force -short
290         tags     []string      // Build tags
291         race     bool          // Force -race
292         bench    bool          // Run benchmarks (briefly), not tests.
293         runTests string        // Regexp of tests to run
294         cpu      string        // If non-empty, -cpu flag
295         goroot   string        // If non-empty, use alternate goroot for go command
296
297         gcflags   string // If non-empty, build with -gcflags=all=X
298         ldflags   string // If non-empty, build with -ldflags=X
299         buildmode string // If non-empty, -buildmode flag
300
301         dir string   // If non-empty, run in GOROOT/src-relative directory dir
302         env []string // Environment variables to add, as KEY=VAL. KEY= unsets a variable
303
304         // We have both pkg and pkgs as a convenience. Both may be set, in which
305         // case they will be combined. If both are empty, the default is ".".
306         pkgs []string // Multiple packages to test
307         pkg  string   // A single package to test
308
309         testFlags []string // Additional flags accepted by this test
310 }
311
312 // bgCommand returns a go test Cmd. The result has Stdout and Stderr set to nil
313 // and is intended to be added to the work queue.
314 func (opts *goTest) bgCommand(t *tester) *exec.Cmd {
315         goCmd, build, run, pkgs, setupCmd := opts.buildArgs(t)
316
317         // Combine the flags.
318         args := append([]string{"test"}, build...)
319         if t.compileOnly {
320                 args = append(args, "-c", "-o", os.DevNull)
321         } else {
322                 args = append(args, run...)
323         }
324         args = append(args, pkgs...)
325         if !t.compileOnly {
326                 args = append(args, opts.testFlags...)
327         }
328
329         cmd := exec.Command(goCmd, args...)
330         setupCmd(cmd)
331
332         return cmd
333 }
334
335 // command returns a go test Cmd intended to be run immediately.
336 func (opts *goTest) command(t *tester) *exec.Cmd {
337         cmd := opts.bgCommand(t)
338         cmd.Stdout = os.Stdout
339         cmd.Stderr = os.Stderr
340         return cmd
341 }
342
343 func (opts *goTest) run(t *tester) error {
344         return opts.command(t).Run()
345 }
346
347 // runHostTest runs a test that should be built and run on the host GOOS/GOARCH,
348 // but run with GOOS/GOARCH set to the target GOOS/GOARCH. This is for tests
349 // that do nothing but compile and run other binaries. If the host and target
350 // are different, then the assumption is that the target is running in an
351 // emulator and does not have a Go toolchain at all, so the test needs to run on
352 // the host, but its resulting binaries will be run through a go_exec wrapper
353 // that runs them on the target.
354 func (opts *goTest) runHostTest(t *tester) error {
355         goCmd, build, run, pkgs, setupCmd := opts.buildArgs(t)
356
357         // Build the host test binary
358         if len(pkgs) != 1 {
359                 // We can't compile more than one package.
360                 panic("host tests must have a single test package")
361         }
362         if len(opts.env) != 0 {
363                 // It's not clear if these are for the host or the target.
364                 panic("host tests must not have environment variables")
365         }
366
367         f, err := os.CreateTemp(t.goTmpDir, "test.test-*"+t.goExe)
368         if err != nil {
369                 fatalf("failed to create temporary file: %s", err)
370         }
371         bin := f.Name()
372         f.Close()
373         xatexit(func() { os.Remove(bin) })
374
375         args := append([]string{"test", "-c", "-o", bin}, build...)
376         args = append(args, pkgs...)
377         cmd := exec.Command(goCmd, args...)
378         setupCmd(cmd)
379         cmd.Stdout = os.Stdout
380         cmd.Stderr = os.Stderr
381         setEnv(cmd, "GOARCH", gohostarch)
382         setEnv(cmd, "GOOS", gohostos)
383         if vflag > 1 {
384                 errprintf("%s\n", cmd)
385         }
386         if err := cmd.Run(); err != nil {
387                 return err
388         }
389
390         if t.compileOnly {
391                 return nil
392         }
393
394         // Transform run flags to be passed directly to a test binary.
395         for i, f := range run {
396                 if !strings.HasPrefix(f, "-") {
397                         panic("run flag does not start with -: " + f)
398                 }
399                 run[i] = "-test." + f[1:]
400         }
401
402         // Run the test
403         args = append(run, opts.testFlags...)
404         cmd = exec.Command(bin, args...)
405         setupCmd(cmd)
406         cmd.Stdout = os.Stdout
407         cmd.Stderr = os.Stderr
408         if vflag > 1 {
409                 errprintf("%s\n", cmd)
410         }
411         return cmd.Run()
412 }
413
414 // buildArgs is in internal helper for goTest that constructs the elements of
415 // the "go test" command line. goCmd is the path to the go command to use. build
416 // is the flags for building the test. run is the flags for running the test.
417 // pkgs is the list of packages to build and run.
418 //
419 // The caller is responsible for adding opts.testFlags, and must call setupCmd
420 // on the resulting exec.Cmd to set its directory and environment.
421 func (opts *goTest) buildArgs(t *tester) (goCmd string, build, run, pkgs []string, setupCmd func(*exec.Cmd)) {
422         goCmd = gorootBinGo
423         if opts.goroot != "" {
424                 goCmd = filepath.Join(opts.goroot, "bin", "go")
425         }
426
427         run = append(run, "-count=1") // Disallow caching
428         if opts.timeout != 0 {
429                 d := opts.timeout * time.Duration(t.timeoutScale)
430                 run = append(run, "-timeout="+d.String())
431         }
432         if opts.short || t.short {
433                 run = append(run, "-short")
434         }
435         var tags []string
436         if t.iOS() {
437                 tags = append(tags, "lldb")
438         }
439         if noOpt {
440                 tags = append(tags, "noopt")
441         }
442         tags = append(tags, opts.tags...)
443         if len(tags) > 0 {
444                 build = append(build, "-tags="+strings.Join(tags, ","))
445         }
446         if t.race || opts.race {
447                 build = append(build, "-race")
448         }
449         if t.msan {
450                 build = append(build, "-msan")
451         }
452         if t.asan {
453                 build = append(build, "-asan")
454         }
455         if opts.bench {
456                 // Run no tests.
457                 run = append(run, "-run=^$")
458                 // Run benchmarks as a smoke test
459                 run = append(run, "-bench=.*", "-benchtime=.1s")
460         } else if opts.runTests != "" {
461                 run = append(run, "-run="+opts.runTests)
462         }
463         if opts.cpu != "" {
464                 run = append(run, "-cpu="+opts.cpu)
465         }
466
467         if opts.gcflags != "" {
468                 build = append(build, "-gcflags=all="+opts.gcflags)
469         }
470         if opts.ldflags != "" {
471                 build = append(build, "-ldflags="+opts.ldflags)
472         }
473         if opts.buildmode != "" {
474                 build = append(build, "-buildmode="+opts.buildmode)
475         }
476
477         pkgs = opts.pkgs
478         if opts.pkg != "" {
479                 pkgs = append(pkgs[:len(pkgs):len(pkgs)], opts.pkg)
480         }
481         if len(pkgs) == 0 {
482                 pkgs = []string{"."}
483         }
484
485         thisGoroot := goroot
486         if opts.goroot != "" {
487                 thisGoroot = opts.goroot
488         }
489         var dir string
490         if opts.dir != "" {
491                 if filepath.IsAbs(opts.dir) {
492                         panic("dir must be relative, got: " + opts.dir)
493                 }
494                 dir = filepath.Join(thisGoroot, "src", opts.dir)
495         } else {
496                 dir = filepath.Join(thisGoroot, "src")
497         }
498         setupCmd = func(cmd *exec.Cmd) {
499                 setDir(cmd, dir)
500                 if len(opts.env) != 0 {
501                         for _, kv := range opts.env {
502                                 if i := strings.Index(kv, "="); i < 0 {
503                                         unsetEnv(cmd, kv[:len(kv)-1])
504                                 } else {
505                                         setEnv(cmd, kv[:i], kv[i+1:])
506                                 }
507                         }
508                 }
509         }
510
511         return
512 }
513
514 // ranGoTest and stdMatches are state closed over by the stdlib
515 // testing func in registerStdTest below. The tests are run
516 // sequentially, so there's no need for locks.
517 //
518 // ranGoBench and benchMatches are the same, but are only used
519 // in -race mode.
520 var (
521         ranGoTest  bool
522         stdMatches []string
523
524         ranGoBench   bool
525         benchMatches []string
526 )
527
528 func (t *tester) registerStdTest(pkg string) {
529         heading := "Testing packages."
530         testPrefix := "go_test:"
531         gcflags := gogcflags
532
533         testName := testPrefix + pkg
534         if t.runRx == nil || t.runRx.MatchString(testName) == t.runRxWant {
535                 stdMatches = append(stdMatches, pkg)
536         }
537
538         t.tests = append(t.tests, distTest{
539                 name:    testName,
540                 heading: heading,
541                 fn: func(dt *distTest) error {
542                         if ranGoTest {
543                                 return nil
544                         }
545                         t.runPending(dt)
546                         timelog("start", dt.name)
547                         defer timelog("end", dt.name)
548                         ranGoTest = true
549
550                         timeoutSec := 180 * time.Second
551                         for _, pkg := range stdMatches {
552                                 if pkg == "cmd/go" {
553                                         timeoutSec *= 3
554                                         break
555                                 }
556                         }
557                         return (&goTest{
558                                 timeout: timeoutSec,
559                                 gcflags: gcflags,
560                                 pkgs:    stdMatches,
561                         }).run(t)
562                 },
563         })
564 }
565
566 func (t *tester) registerRaceBenchTest(pkg string) {
567         testName := "go_test_bench:" + pkg
568         if t.runRx == nil || t.runRx.MatchString(testName) == t.runRxWant {
569                 benchMatches = append(benchMatches, pkg)
570         }
571         t.tests = append(t.tests, distTest{
572                 name:    testName,
573                 heading: "Running benchmarks briefly.",
574                 fn: func(dt *distTest) error {
575                         if ranGoBench {
576                                 return nil
577                         }
578                         t.runPending(dt)
579                         timelog("start", dt.name)
580                         defer timelog("end", dt.name)
581                         ranGoBench = true
582                         return (&goTest{
583                                 timeout: 1200 * time.Second, // longer timeout for race with benchmarks
584                                 race:    true,
585                                 bench:   true,
586                                 cpu:     "4",
587                                 pkgs:    benchMatches,
588                         }).run(t)
589                 },
590         })
591 }
592
593 func (t *tester) registerTests() {
594         // registerStdTestSpecially tracks import paths in the standard library
595         // whose test registration happens in a special way.
596         registerStdTestSpecially := map[string]bool{
597                 "internal/testdir": true, // Registered at the bottom with sharding.
598                 // cgo tests are registered specially because they involve unusual build
599                 // conditions and flags.
600                 "cmd/cgo/internal/teststdio":      true,
601                 "cmd/cgo/internal/testlife":       true,
602                 "cmd/cgo/internal/testfortran":    true,
603                 "cmd/cgo/internal/test":           true,
604                 "cmd/cgo/internal/testnocgo":      true,
605                 "cmd/cgo/internal/testtls":        true,
606                 "cmd/cgo/internal/testgodefs":     true,
607                 "cmd/cgo/internal/testso":         true,
608                 "cmd/cgo/internal/testsovar":      true,
609                 "cmd/cgo/internal/testcarchive":   true,
610                 "cmd/cgo/internal/testcshared":    true,
611                 "cmd/cgo/internal/testshared":     true,
612                 "cmd/cgo/internal/testplugin":     true,
613                 "cmd/cgo/internal/testsanitizers": true,
614                 "cmd/cgo/internal/testerrors":     true,
615         }
616
617         // Fast path to avoid the ~1 second of `go list std cmd` when
618         // the caller lists specific tests to run. (as the continuous
619         // build coordinator does).
620         if len(t.runNames) > 0 {
621                 for _, name := range t.runNames {
622                         if strings.HasPrefix(name, "go_test:") {
623                                 t.registerStdTest(strings.TrimPrefix(name, "go_test:"))
624                         }
625                         if strings.HasPrefix(name, "go_test_bench:") {
626                                 t.registerRaceBenchTest(strings.TrimPrefix(name, "go_test_bench:"))
627                         }
628                 }
629         } else {
630                 // Use a format string to only list packages and commands that have tests.
631                 const format = "{{if (or .TestGoFiles .XTestGoFiles)}}{{.ImportPath}}{{end}}"
632                 cmd := exec.Command(gorootBinGo, "list", "-f", format)
633                 if t.race {
634                         cmd.Args = append(cmd.Args, "-tags=race")
635                 }
636                 cmd.Args = append(cmd.Args, "std", "cmd")
637                 cmd.Stderr = new(bytes.Buffer)
638                 all, err := cmd.Output()
639                 if err != nil {
640                         fatalf("Error running go list std cmd: %v:\n%s", err, cmd.Stderr)
641                 }
642                 pkgs := strings.Fields(string(all))
643                 for _, pkg := range pkgs {
644                         if registerStdTestSpecially[pkg] {
645                                 continue
646                         }
647                         t.registerStdTest(pkg)
648                 }
649                 if t.race {
650                         for _, pkg := range pkgs {
651                                 if registerStdTestSpecially[pkg] {
652                                         continue
653                                 }
654                                 if t.packageHasBenchmarks(pkg) {
655                                         t.registerRaceBenchTest(pkg)
656                                 }
657                         }
658                 }
659         }
660
661         if t.race {
662                 return
663         }
664
665         // Test the os/user package in the pure-Go mode too.
666         if !t.compileOnly {
667                 t.registerTest("osusergo", "os/user with tag osusergo",
668                         &goTest{
669                                 timeout: 300 * time.Second,
670                                 tags:    []string{"osusergo"},
671                                 pkg:     "os/user",
672                         })
673                 t.registerTest("purego:hash/maphash", "hash/maphash purego implementation",
674                         &goTest{
675                                 timeout: 300 * time.Second,
676                                 tags:    []string{"purego"},
677                                 pkg:     "hash/maphash",
678                         })
679         }
680
681         // Test ios/amd64 for the iOS simulator.
682         if goos == "darwin" && goarch == "amd64" && t.cgoEnabled {
683                 t.registerTest("amd64ios", "GOOS=ios on darwin/amd64",
684                         &goTest{
685                                 timeout:  300 * time.Second,
686                                 runTests: "SystemRoots",
687                                 env:      []string{"GOOS=ios", "CGO_ENABLED=1"},
688                                 pkg:      "crypto/x509",
689                         })
690         }
691
692         // Runtime CPU tests.
693         if !t.compileOnly && t.hasParallelism() {
694                 t.registerTest("runtime:cpu124", "GOMAXPROCS=2 runtime -cpu=1,2,4 -quick",
695                         &goTest{
696                                 timeout:   300 * time.Second,
697                                 cpu:       "1,2,4",
698                                 short:     true,
699                                 testFlags: []string{"-quick"},
700                                 // We set GOMAXPROCS=2 in addition to -cpu=1,2,4 in order to test runtime bootstrap code,
701                                 // creation of first goroutines and first garbage collections in the parallel setting.
702                                 env: []string{"GOMAXPROCS=2"},
703                                 pkg: "runtime",
704                         })
705         }
706
707         // morestack tests. We only run these on in long-test mode
708         // (with GO_TEST_SHORT=false) because the runtime test is
709         // already quite long and mayMoreStackMove makes it about
710         // twice as slow.
711         if !t.compileOnly && !t.short {
712                 // hooks is the set of maymorestack hooks to test with.
713                 hooks := []string{"mayMoreStackPreempt", "mayMoreStackMove"}
714                 // pkgs is the set of test packages to run.
715                 pkgs := []string{"runtime", "reflect", "sync"}
716                 // hookPkgs is the set of package patterns to apply
717                 // the maymorestack hook to.
718                 hookPkgs := []string{"runtime/...", "reflect", "sync"}
719                 // unhookPkgs is the set of package patterns to
720                 // exclude from hookPkgs.
721                 unhookPkgs := []string{"runtime/testdata/..."}
722                 for _, hook := range hooks {
723                         // Construct the build flags to use the
724                         // maymorestack hook in the compiler and
725                         // assembler. We pass this via the GOFLAGS
726                         // environment variable so that it applies to
727                         // both the test itself and to binaries built
728                         // by the test.
729                         goFlagsList := []string{}
730                         for _, flag := range []string{"-gcflags", "-asmflags"} {
731                                 for _, hookPkg := range hookPkgs {
732                                         goFlagsList = append(goFlagsList, flag+"="+hookPkg+"=-d=maymorestack=runtime."+hook)
733                                 }
734                                 for _, unhookPkg := range unhookPkgs {
735                                         goFlagsList = append(goFlagsList, flag+"="+unhookPkg+"=")
736                                 }
737                         }
738                         goFlags := strings.Join(goFlagsList, " ")
739
740                         for _, pkg := range pkgs {
741                                 t.registerTest(hook+":"+pkg, "maymorestack="+hook,
742                                         &goTest{
743                                                 timeout: 600 * time.Second,
744                                                 short:   true,
745                                                 env:     []string{"GOFLAGS=" + goFlags},
746                                                 pkg:     pkg,
747                                         })
748                         }
749                 }
750         }
751
752         // On the builders only, test that a moved GOROOT still works.
753         // Fails on iOS because CC_FOR_TARGET refers to clangwrap.sh
754         // in the unmoved GOROOT.
755         // Fails on Android, js/wasm and wasip1/wasm with an exec format error.
756         // Fails on plan9 with "cannot find GOROOT" (issue #21016).
757         if os.Getenv("GO_BUILDER_NAME") != "" && goos != "android" && !t.iOS() && goos != "plan9" && goos != "js" && goos != "wasip1" {
758                 t.tests = append(t.tests, distTest{
759                         name:    "moved_goroot",
760                         heading: "moved GOROOT",
761                         fn: func(dt *distTest) error {
762                                 t.runPending(dt)
763                                 timelog("start", dt.name)
764                                 defer timelog("end", dt.name)
765                                 moved := goroot + "-moved"
766                                 if err := os.Rename(goroot, moved); err != nil {
767                                         if goos == "windows" {
768                                                 // Fails on Windows (with "Access is denied") if a process
769                                                 // or binary is in this directory. For instance, using all.bat
770                                                 // when run from c:\workdir\go\src fails here
771                                                 // if GO_BUILDER_NAME is set. Our builders invoke tests
772                                                 // a different way which happens to work when sharding
773                                                 // tests, but we should be tolerant of the non-sharded
774                                                 // all.bat case.
775                                                 log.Printf("skipping test on Windows")
776                                                 return nil
777                                         }
778                                         return err
779                                 }
780
781                                 // Run `go test fmt` in the moved GOROOT, without explicitly setting
782                                 // GOROOT in the environment. The 'go' command should find itself.
783                                 cmd := (&goTest{
784                                         goroot: moved,
785                                         pkg:    "fmt",
786                                 }).command(t)
787                                 unsetEnv(cmd, "GOROOT")
788                                 err := cmd.Run()
789
790                                 if rerr := os.Rename(moved, goroot); rerr != nil {
791                                         fatalf("failed to restore GOROOT: %v", rerr)
792                                 }
793                                 return err
794                         },
795                 })
796         }
797
798         // Test that internal linking of standard packages does not
799         // require libgcc. This ensures that we can install a Go
800         // release on a system that does not have a C compiler
801         // installed and still build Go programs (that don't use cgo).
802         for _, pkg := range cgoPackages {
803                 if !t.internalLink() {
804                         break
805                 }
806
807                 // ARM libgcc may be Thumb, which internal linking does not support.
808                 if goarch == "arm" {
809                         break
810                 }
811
812                 // What matters is that the tests build and start up.
813                 // Skip expensive tests, especially x509 TestSystemRoots.
814                 run := "^Test[^CS]"
815                 if pkg == "net" {
816                         run = "TestTCPStress"
817                 }
818                 t.registerTest("nolibgcc:"+pkg, "Testing without libgcc.",
819                         &goTest{
820                                 ldflags:  "-linkmode=internal -libgcc=none",
821                                 runTests: run,
822                                 pkg:      pkg,
823                         })
824         }
825
826         // Stub out following test on alpine until 54354 resolved.
827         builderName := os.Getenv("GO_BUILDER_NAME")
828         disablePIE := strings.HasSuffix(builderName, "-alpine")
829
830         // Test internal linking of PIE binaries where it is supported.
831         if t.internalLinkPIE() && !disablePIE {
832                 t.registerTest("pie_internal", "internal linking of -buildmode=pie",
833                         &goTest{
834                                 timeout:   60 * time.Second,
835                                 buildmode: "pie",
836                                 ldflags:   "-linkmode=internal",
837                                 env:       []string{"CGO_ENABLED=0"},
838                                 pkg:       "reflect",
839                         })
840                 // Also test a cgo package.
841                 if t.cgoEnabled && t.internalLink() && !disablePIE {
842                         t.registerTest("pie_internal_cgo", "internal linking of -buildmode=pie",
843                                 &goTest{
844                                         timeout:   60 * time.Second,
845                                         buildmode: "pie",
846                                         ldflags:   "-linkmode=internal",
847                                         pkg:       "os/user",
848                                 })
849                 }
850         }
851
852         // sync tests
853         if t.hasParallelism() {
854                 t.registerTest("sync_cpu", "sync -cpu=10",
855                         &goTest{
856                                 timeout: 120 * time.Second,
857                                 cpu:     "10",
858                                 pkg:     "sync",
859                         })
860         }
861
862         if t.raceDetectorSupported() {
863                 t.registerRaceTests()
864         }
865
866         if t.cgoEnabled && !t.iOS() {
867                 // Disabled on iOS. golang.org/issue/15919
868                 t.registerTest("cgo_teststdio", "", &goTest{dir: "cmd/cgo/internal/teststdio", timeout: 5 * time.Minute}, rtHostTest{})
869                 t.registerTest("cgo_testlife", "", &goTest{dir: "cmd/cgo/internal/testlife", timeout: 5 * time.Minute}, rtHostTest{})
870                 if goos != "android" {
871                         t.registerTest("cgo_testfortran", "", &goTest{dir: "cmd/cgo/internal/testfortran", timeout: 5 * time.Minute}, rtHostTest{})
872                 }
873                 if t.hasSwig() && goos != "android" {
874                         t.registerTest("swig_stdio", "", &goTest{dir: "../misc/swig/stdio"})
875                         if t.hasCxx() {
876                                 t.registerTest("swig_callback", "", &goTest{dir: "../misc/swig/callback"})
877                                 const cflags = "-flto -Wno-lto-type-mismatch -Wno-unknown-warning-option"
878                                 t.registerTest("swig_callback_lto", "",
879                                         &goTest{
880                                                 dir: "../misc/swig/callback",
881                                                 env: []string{
882                                                         "CGO_CFLAGS=" + cflags,
883                                                         "CGO_CXXFLAGS=" + cflags,
884                                                         "CGO_LDFLAGS=" + cflags,
885                                                 },
886                                         })
887                         }
888                 }
889         }
890         if t.cgoEnabled {
891                 t.registerCgoTests()
892         }
893
894         // Don't run these tests with $GO_GCFLAGS because most of them
895         // assume that they can run "go install" with no -gcflags and not
896         // recompile the entire standard library. If make.bash ran with
897         // special -gcflags, that's not true.
898         if t.cgoEnabled && gogcflags == "" {
899                 t.registerTest("cgo_testgodefs", "", &goTest{dir: "cmd/cgo/internal/testgodefs", timeout: 5 * time.Minute}, rtHostTest{})
900
901                 t.registerTest("cgo_testso", "", &goTest{dir: "cmd/cgo/internal/testso", timeout: 600 * time.Second})
902                 t.registerTest("cgo_testsovar", "", &goTest{dir: "cmd/cgo/internal/testsovar", timeout: 600 * time.Second})
903                 if t.supportedBuildmode("c-archive") {
904                         t.registerTest("cgo_testcarchive", "", &goTest{dir: "cmd/cgo/internal/testcarchive", timeout: 5 * time.Minute}, rtHostTest{})
905                 }
906                 if t.supportedBuildmode("c-shared") {
907                         t.registerTest("cgo_testcshared", "", &goTest{dir: "cmd/cgo/internal/testcshared", timeout: 5 * time.Minute}, rtHostTest{})
908                 }
909                 if t.supportedBuildmode("shared") {
910                         t.registerTest("cgo_testshared", "", &goTest{dir: "cmd/cgo/internal/testshared", timeout: 600 * time.Second})
911                 }
912                 if t.supportedBuildmode("plugin") {
913                         t.registerTest("cgo_testplugin", "", &goTest{dir: "cmd/cgo/internal/testplugin", timeout: 600 * time.Second})
914                 }
915                 if goos == "linux" || (goos == "freebsd" && goarch == "amd64") {
916                         // because Pdeathsig of syscall.SysProcAttr struct used in cmd/cgo/internal/testsanitizers is only
917                         // supported on Linux and FreeBSD.
918                         t.registerTest("cgo_testsanitizers", "", &goTest{dir: "cmd/cgo/internal/testsanitizers", timeout: 5 * time.Minute}, rtHostTest{})
919                 }
920                 if t.hasBash() && goos != "android" && !t.iOS() && gohostos != "windows" {
921                         t.registerTest("cgo_errors", "", &goTest{dir: "cmd/cgo/internal/testerrors", timeout: 5 * time.Minute}, rtHostTest{})
922                 }
923         }
924
925         if goos != "android" && !t.iOS() {
926                 // There are no tests in this directory, only benchmarks.
927                 // Check that the test binary builds.
928                 t.registerTest("bench_go1", "", &goTest{dir: "../test/bench/go1"})
929         }
930         if goos != "android" && !t.iOS() {
931                 // Only start multiple test dir shards on builders,
932                 // where they get distributed to multiple machines.
933                 // See issues 20141 and 31834.
934                 nShards := 1
935                 if os.Getenv("GO_BUILDER_NAME") != "" {
936                         nShards = 10
937                 }
938                 if n, err := strconv.Atoi(os.Getenv("GO_TEST_SHARDS")); err == nil {
939                         nShards = n
940                 }
941                 for shard := 0; shard < nShards; shard++ {
942                         t.registerTest(
943                                 fmt.Sprintf("test:%d_%d", shard, nShards),
944                                 "../test",
945                                 &goTest{
946                                         dir:       "internal/testdir",
947                                         testFlags: []string{fmt.Sprintf("-shard=%d", shard), fmt.Sprintf("-shards=%d", nShards)},
948                                 },
949                                 rtHostTest{},
950                         )
951                 }
952         }
953         // Only run the API check on fast development platforms.
954         // Every platform checks the API on every GOOS/GOARCH/CGO_ENABLED combination anyway,
955         // so we really only need to run this check once anywhere to get adequate coverage.
956         // To help developers avoid trybot-only failures, we try to run on typical developer machines
957         // which is darwin,linux,windows/amd64 and darwin/arm64.
958         if goos == "darwin" || ((goos == "linux" || goos == "windows") && goarch == "amd64") {
959                 t.registerTest("api", "", &goTest{dir: "cmd/api", timeout: 5 * time.Minute, testFlags: []string{"-check"}})
960         }
961
962         // Ensure that the toolchain can bootstrap itself.
963         // This test adds another ~45s to all.bash if run sequentially, so run it only on the builders.
964         if os.Getenv("GO_BUILDER_NAME") != "" && goos != "android" && !t.iOS() {
965                 t.registerTest("reboot", "", &goTest{dir: "../misc/reboot", timeout: 5 * time.Minute}, rtHostTest{})
966         }
967 }
968
969 // isRegisteredTestName reports whether a test named testName has already
970 // been registered.
971 func (t *tester) isRegisteredTestName(testName string) bool {
972         for _, tt := range t.tests {
973                 if tt.name == testName {
974                         return true
975                 }
976         }
977         return false
978 }
979
980 type registerTestOpt interface {
981         isRegisterTestOpt()
982 }
983
984 // rtSequential is a registerTest option that causes the registered test to run
985 // sequentially.
986 type rtSequential struct{}
987
988 func (rtSequential) isRegisterTestOpt() {}
989
990 // rtPreFunc is a registerTest option that runs a pre function before running
991 // the test.
992 type rtPreFunc struct {
993         pre func(*distTest) bool // Return false to skip the test
994 }
995
996 func (rtPreFunc) isRegisterTestOpt() {}
997
998 // rtHostTest is a registerTest option that indicates this is a host test that
999 // should be run using goTest.runHostTest. It implies rtSequential.
1000 type rtHostTest struct{}
1001
1002 func (rtHostTest) isRegisterTestOpt() {}
1003
1004 // registerTest registers a test that runs the given goTest.
1005 //
1006 // If heading is "", it uses test.dir as the heading.
1007 func (t *tester) registerTest(name, heading string, test *goTest, opts ...registerTestOpt) {
1008         seq := false
1009         hostTest := false
1010         var preFunc func(*distTest) bool
1011         for _, opt := range opts {
1012                 switch opt := opt.(type) {
1013                 case rtSequential:
1014                         seq = true
1015                 case rtPreFunc:
1016                         preFunc = opt.pre
1017                 case rtHostTest:
1018                         seq, hostTest = true, true
1019                 }
1020         }
1021         if t.isRegisteredTestName(name) {
1022                 panic("duplicate registered test name " + name)
1023         }
1024         if heading == "" {
1025                 heading = test.dir
1026         }
1027         t.tests = append(t.tests, distTest{
1028                 name:    name,
1029                 heading: heading,
1030                 fn: func(dt *distTest) error {
1031                         if preFunc != nil && !preFunc(dt) {
1032                                 return nil
1033                         }
1034                         if seq {
1035                                 t.runPending(dt)
1036                                 if hostTest {
1037                                         return test.runHostTest(t)
1038                                 }
1039                                 return test.run(t)
1040                         }
1041                         w := &work{
1042                                 dt:  dt,
1043                                 cmd: test.bgCommand(t),
1044                         }
1045                         t.worklist = append(t.worklist, w)
1046                         return nil
1047                 },
1048         })
1049 }
1050
1051 // bgDirCmd constructs a Cmd intended to be run in the background as
1052 // part of the worklist. The worklist runner will buffer its output
1053 // and replay it sequentially. The command will be run in dir.
1054 func (t *tester) bgDirCmd(dir, bin string, args ...string) *exec.Cmd {
1055         cmd := exec.Command(bin, args...)
1056         if filepath.IsAbs(dir) {
1057                 setDir(cmd, dir)
1058         } else {
1059                 setDir(cmd, filepath.Join(goroot, dir))
1060         }
1061         return cmd
1062 }
1063
1064 // dirCmd constructs a Cmd intended to be run in the foreground.
1065 // The command will be run in dir, and Stdout and Stderr will go to os.Stdout
1066 // and os.Stderr.
1067 func (t *tester) dirCmd(dir string, cmdline ...interface{}) *exec.Cmd {
1068         bin, args := flattenCmdline(cmdline)
1069         cmd := t.bgDirCmd(dir, bin, args...)
1070         cmd.Stdout = os.Stdout
1071         cmd.Stderr = os.Stderr
1072         if vflag > 1 {
1073                 errprintf("%s\n", strings.Join(cmd.Args, " "))
1074         }
1075         return cmd
1076 }
1077
1078 // flattenCmdline flattens a mixture of string and []string as single list
1079 // and then interprets it as a command line: first element is binary, then args.
1080 func flattenCmdline(cmdline []interface{}) (bin string, args []string) {
1081         var list []string
1082         for _, x := range cmdline {
1083                 switch x := x.(type) {
1084                 case string:
1085                         list = append(list, x)
1086                 case []string:
1087                         list = append(list, x...)
1088                 default:
1089                         panic("invalid addCmd argument type: " + reflect.TypeOf(x).String())
1090                 }
1091         }
1092
1093         bin = list[0]
1094         if !filepath.IsAbs(bin) {
1095                 panic("command is not absolute: " + bin)
1096         }
1097         return bin, list[1:]
1098 }
1099
1100 // addCmd adds a command to the worklist. Commands can be run in
1101 // parallel, but their output will be buffered and replayed in the
1102 // order they were added to worklist.
1103 func (t *tester) addCmd(dt *distTest, dir string, cmdline ...interface{}) *exec.Cmd {
1104         bin, args := flattenCmdline(cmdline)
1105         w := &work{
1106                 dt:  dt,
1107                 cmd: t.bgDirCmd(dir, bin, args...),
1108         }
1109         t.worklist = append(t.worklist, w)
1110         return w.cmd
1111 }
1112
1113 func (t *tester) iOS() bool {
1114         return goos == "ios"
1115 }
1116
1117 func (t *tester) out(v string) {
1118         if t.banner == "" {
1119                 return
1120         }
1121         fmt.Println("\n" + t.banner + v)
1122 }
1123
1124 // extLink reports whether the current goos/goarch supports
1125 // external linking. This should match the test in determineLinkMode
1126 // in cmd/link/internal/ld/config.go.
1127 func (t *tester) extLink() bool {
1128         if goarch == "ppc64" && goos != "aix" {
1129                 return false
1130         }
1131         return true
1132 }
1133
1134 func (t *tester) internalLink() bool {
1135         if gohostos == "dragonfly" {
1136                 // linkmode=internal fails on dragonfly since errno is a TLS relocation.
1137                 return false
1138         }
1139         if goos == "android" {
1140                 return false
1141         }
1142         if goos == "ios" {
1143                 return false
1144         }
1145         if goos == "windows" && goarch == "arm64" {
1146                 return false
1147         }
1148         // Internally linking cgo is incomplete on some architectures.
1149         // https://golang.org/issue/10373
1150         // https://golang.org/issue/14449
1151         if goarch == "loong64" || goarch == "mips64" || goarch == "mips64le" || goarch == "mips" || goarch == "mipsle" || goarch == "riscv64" {
1152                 return false
1153         }
1154         if goos == "aix" {
1155                 // linkmode=internal isn't supported.
1156                 return false
1157         }
1158         return true
1159 }
1160
1161 func (t *tester) internalLinkPIE() bool {
1162         switch goos + "-" + goarch {
1163         case "darwin-amd64", "darwin-arm64",
1164                 "linux-amd64", "linux-arm64", "linux-ppc64le",
1165                 "android-arm64",
1166                 "windows-amd64", "windows-386", "windows-arm":
1167                 return true
1168         }
1169         return false
1170 }
1171
1172 // supportedBuildMode reports whether the given build mode is supported.
1173 func (t *tester) supportedBuildmode(mode string) bool {
1174         switch mode {
1175         case "c-archive", "c-shared", "shared", "plugin", "pie":
1176         default:
1177                 fatalf("internal error: unknown buildmode %s", mode)
1178                 return false
1179         }
1180
1181         return buildModeSupported("gc", mode, goos, goarch)
1182 }
1183
1184 func (t *tester) registerCgoTests() {
1185         cgoTest := func(name string, subdir, linkmode, buildmode string, opts ...registerTestOpt) *goTest {
1186                 gt := &goTest{
1187                         dir:       "cmd/cgo/internal/" + subdir,
1188                         buildmode: buildmode,
1189                         ldflags:   "-linkmode=" + linkmode,
1190                 }
1191
1192                 if linkmode == "internal" {
1193                         gt.tags = append(gt.tags, "internal")
1194                         if buildmode == "pie" {
1195                                 gt.tags = append(gt.tags, "internal_pie")
1196                         }
1197                 }
1198                 if buildmode == "static" {
1199                         // This isn't actually a Go buildmode, just a convenient way to tell
1200                         // cgoTest we want static linking.
1201                         gt.buildmode = ""
1202                         if linkmode == "external" {
1203                                 gt.ldflags += ` -extldflags "-static -pthread"`
1204                         } else if linkmode == "auto" {
1205                                 gt.env = append(gt.env, "CGO_LDFLAGS=-static -pthread")
1206                         } else {
1207                                 panic("unknown linkmode with static build: " + linkmode)
1208                         }
1209                         gt.tags = append(gt.tags, "static")
1210                 }
1211
1212                 t.registerTest("cgo:"+name, "cmd/cgo/internal/test", gt, opts...)
1213                 return gt
1214         }
1215
1216         cgoTest("test-auto", "test", "auto", "")
1217
1218         // Stub out various buildmode=pie tests  on alpine until 54354 resolved.
1219         builderName := os.Getenv("GO_BUILDER_NAME")
1220         disablePIE := strings.HasSuffix(builderName, "-alpine")
1221
1222         if t.internalLink() {
1223                 cgoTest("test-internal", "test", "internal", "")
1224         }
1225
1226         os := gohostos
1227         p := gohostos + "/" + goarch
1228         switch {
1229         case os == "darwin", os == "windows":
1230                 if !t.extLink() {
1231                         break
1232                 }
1233                 // test linkmode=external, but __thread not supported, so skip testtls.
1234                 cgoTest("test-external", "test", "external", "")
1235
1236                 gt := cgoTest("test-external-s", "test", "external", "")
1237                 gt.ldflags += " -s"
1238
1239                 if t.supportedBuildmode("pie") && !disablePIE {
1240                         cgoTest("test-auto-pie", "test", "auto", "pie")
1241                         if t.internalLink() && t.internalLinkPIE() {
1242                                 cgoTest("test-internal-pie", "test", "internal", "pie")
1243                         }
1244                 }
1245
1246         case os == "aix", os == "android", os == "dragonfly", os == "freebsd", os == "linux", os == "netbsd", os == "openbsd":
1247                 gt := cgoTest("test-external-g0", "test", "external", "")
1248                 gt.env = append(gt.env, "CGO_CFLAGS=-g0 -fdiagnostics-color")
1249
1250                 cgoTest("testtls-auto", "testtls", "auto", "")
1251                 cgoTest("testtls-external", "testtls", "external", "")
1252                 switch {
1253                 case os == "aix":
1254                         // no static linking
1255                 case p == "freebsd/arm":
1256                         // -fPIC compiled tls code will use __tls_get_addr instead
1257                         // of __aeabi_read_tp, however, on FreeBSD/ARM, __tls_get_addr
1258                         // is implemented in rtld-elf, so -fPIC isn't compatible with
1259                         // static linking on FreeBSD/ARM with clang. (cgo depends on
1260                         // -fPIC fundamentally.)
1261                 default:
1262                         // Check for static linking support
1263                         var staticCheck rtPreFunc
1264                         ccName := compilerEnvLookup("CC", defaultcc, goos, goarch)
1265                         cc, err := exec.LookPath(ccName)
1266                         if err != nil {
1267                                 staticCheck.pre = func(*distTest) bool {
1268                                         fmt.Printf("$CC (%q) not found, skip cgo static linking test.\n", ccName)
1269                                         return false
1270                                 }
1271                         } else {
1272                                 cmd := t.dirCmd("src/cmd/cgo/internal/test", cc, "-xc", "-o", "/dev/null", "-static", "-")
1273                                 cmd.Stdin = strings.NewReader("int main() {}")
1274                                 cmd.Stdout, cmd.Stderr = nil, nil // Discard output
1275                                 if err := cmd.Run(); err != nil {
1276                                         // Skip these tests
1277                                         staticCheck.pre = func(*distTest) bool {
1278                                                 fmt.Println("No support for static linking found (lacks libc.a?), skip cgo static linking test.")
1279                                                 return false
1280                                         }
1281                                 }
1282                         }
1283
1284                         // Doing a static link with boringcrypto gets
1285                         // a C linker warning on Linux.
1286                         // in function `bio_ip_and_port_to_socket_and_addr':
1287                         // warning: Using 'getaddrinfo' in statically linked applications requires at runtime the shared libraries from the glibc version used for linking
1288                         if staticCheck.pre == nil && goos == "linux" && strings.Contains(goexperiment, "boringcrypto") {
1289                                 staticCheck.pre = func(*distTest) bool {
1290                                         fmt.Println("skipping static linking check on Linux when using boringcrypto to avoid C linker warning about getaddrinfo")
1291                                         return false
1292                                 }
1293                         }
1294
1295                         // Static linking tests
1296                         if goos != "android" && p != "netbsd/arm" {
1297                                 // TODO(#56629): Why does this fail on netbsd-arm?
1298                                 cgoTest("testtls-static", "testtls", "external", "static", staticCheck)
1299                         }
1300                         cgoTest("nocgo-auto", "testnocgo", "auto", "", staticCheck)
1301                         cgoTest("nocgo-external", "testnocgo", "external", "", staticCheck)
1302                         if goos != "android" {
1303                                 cgoTest("nocgo-static", "testnocgo", "external", "static", staticCheck)
1304                                 cgoTest("test-static", "test", "external", "static", staticCheck)
1305                                 // -static in CGO_LDFLAGS triggers a different code path
1306                                 // than -static in -extldflags, so test both.
1307                                 // See issue #16651.
1308                                 if goarch != "loong64" {
1309                                         // TODO(#56623): Why does this fail on loong64?
1310                                         cgoTest("test-static-env", "test", "auto", "static", staticCheck)
1311                                 }
1312                         }
1313
1314                         // PIE linking tests
1315                         if t.supportedBuildmode("pie") && !disablePIE {
1316                                 cgoTest("test-pie", "test", "auto", "pie")
1317                                 if t.internalLink() && t.internalLinkPIE() {
1318                                         cgoTest("test-pie-internal", "test", "internal", "pie")
1319                                 }
1320                                 cgoTest("testtls-pie", "testtls", "auto", "pie")
1321                                 cgoTest("nocgo-pie", "testnocgo", "auto", "pie")
1322                         }
1323                 }
1324         }
1325 }
1326
1327 // run pending test commands, in parallel, emitting headers as appropriate.
1328 // When finished, emit header for nextTest, which is going to run after the
1329 // pending commands are done (and runPending returns).
1330 // A test should call runPending if it wants to make sure that it is not
1331 // running in parallel with earlier tests, or if it has some other reason
1332 // for needing the earlier tests to be done.
1333 func (t *tester) runPending(nextTest *distTest) {
1334         worklist := t.worklist
1335         t.worklist = nil
1336         for _, w := range worklist {
1337                 w.start = make(chan bool)
1338                 w.end = make(chan bool)
1339                 go func(w *work) {
1340                         if !<-w.start {
1341                                 timelog("skip", w.dt.name)
1342                                 w.out = []byte(fmt.Sprintf("skipped due to earlier error\n"))
1343                         } else {
1344                                 timelog("start", w.dt.name)
1345                                 w.out, w.err = w.cmd.CombinedOutput()
1346                                 if w.err != nil {
1347                                         if isUnsupportedVMASize(w) {
1348                                                 timelog("skip", w.dt.name)
1349                                                 w.out = []byte(fmt.Sprintf("skipped due to unsupported VMA\n"))
1350                                                 w.err = nil
1351                                         }
1352                                 }
1353                         }
1354                         timelog("end", w.dt.name)
1355                         w.end <- true
1356                 }(w)
1357         }
1358
1359         started := 0
1360         ended := 0
1361         var last *distTest
1362         for ended < len(worklist) {
1363                 for started < len(worklist) && started-ended < maxbg {
1364                         w := worklist[started]
1365                         started++
1366                         w.start <- !t.failed || t.keepGoing
1367                 }
1368                 w := worklist[ended]
1369                 dt := w.dt
1370                 if dt.heading != "" && t.lastHeading != dt.heading {
1371                         t.lastHeading = dt.heading
1372                         t.out(dt.heading)
1373                 }
1374                 if dt != last {
1375                         // Assumes all the entries for a single dt are in one worklist.
1376                         last = w.dt
1377                         if vflag > 0 {
1378                                 fmt.Printf("# go tool dist test -run=^%s$\n", dt.name)
1379                         }
1380                 }
1381                 if vflag > 1 {
1382                         errprintf("%s\n", strings.Join(w.cmd.Args, " "))
1383                 }
1384                 ended++
1385                 <-w.end
1386                 os.Stdout.Write(w.out)
1387                 if w.err != nil {
1388                         log.Printf("Failed: %v", w.err)
1389                         t.failed = true
1390                 }
1391         }
1392         if t.failed && !t.keepGoing {
1393                 fatalf("FAILED")
1394         }
1395
1396         if dt := nextTest; dt != nil {
1397                 if dt.heading != "" && t.lastHeading != dt.heading {
1398                         t.lastHeading = dt.heading
1399                         t.out(dt.heading)
1400                 }
1401                 if vflag > 0 {
1402                         fmt.Printf("# go tool dist test -run=^%s$\n", dt.name)
1403                 }
1404         }
1405 }
1406
1407 func (t *tester) hasBash() bool {
1408         switch gohostos {
1409         case "windows", "plan9":
1410                 return false
1411         }
1412         return true
1413 }
1414
1415 func (t *tester) hasCxx() bool {
1416         cxx, _ := exec.LookPath(compilerEnvLookup("CXX", defaultcxx, goos, goarch))
1417         return cxx != ""
1418 }
1419
1420 func (t *tester) hasSwig() bool {
1421         swig, err := exec.LookPath("swig")
1422         if err != nil {
1423                 return false
1424         }
1425
1426         // Check that swig was installed with Go support by checking
1427         // that a go directory exists inside the swiglib directory.
1428         // See https://golang.org/issue/23469.
1429         output, err := exec.Command(swig, "-go", "-swiglib").Output()
1430         if err != nil {
1431                 return false
1432         }
1433         swigDir := strings.TrimSpace(string(output))
1434
1435         _, err = os.Stat(filepath.Join(swigDir, "go"))
1436         if err != nil {
1437                 return false
1438         }
1439
1440         // Check that swig has a new enough version.
1441         // See https://golang.org/issue/22858.
1442         out, err := exec.Command(swig, "-version").CombinedOutput()
1443         if err != nil {
1444                 return false
1445         }
1446
1447         re := regexp.MustCompile(`[vV]ersion +(\d+)([.]\d+)?([.]\d+)?`)
1448         matches := re.FindSubmatch(out)
1449         if matches == nil {
1450                 // Can't find version number; hope for the best.
1451                 return true
1452         }
1453
1454         major, err := strconv.Atoi(string(matches[1]))
1455         if err != nil {
1456                 // Can't find version number; hope for the best.
1457                 return true
1458         }
1459         if major < 3 {
1460                 return false
1461         }
1462         if major > 3 {
1463                 // 4.0 or later
1464                 return true
1465         }
1466
1467         // We have SWIG version 3.x.
1468         if len(matches[2]) > 0 {
1469                 minor, err := strconv.Atoi(string(matches[2][1:]))
1470                 if err != nil {
1471                         return true
1472                 }
1473                 if minor > 0 {
1474                         // 3.1 or later
1475                         return true
1476                 }
1477         }
1478
1479         // We have SWIG version 3.0.x.
1480         if len(matches[3]) > 0 {
1481                 patch, err := strconv.Atoi(string(matches[3][1:]))
1482                 if err != nil {
1483                         return true
1484                 }
1485                 if patch < 6 {
1486                         // Before 3.0.6.
1487                         return false
1488                 }
1489         }
1490
1491         return true
1492 }
1493
1494 // hasParallelism is a copy of the function
1495 // internal/testenv.HasParallelism, which can't be used here
1496 // because cmd/dist can not import internal packages during bootstrap.
1497 func (t *tester) hasParallelism() bool {
1498         switch goos {
1499         case "js", "wasip1":
1500                 return false
1501         }
1502         return true
1503 }
1504
1505 func (t *tester) raceDetectorSupported() bool {
1506         if gohostos != goos {
1507                 return false
1508         }
1509         if !t.cgoEnabled {
1510                 return false
1511         }
1512         if !raceDetectorSupported(goos, goarch) {
1513                 return false
1514         }
1515         // The race detector doesn't work on Alpine Linux:
1516         // golang.org/issue/14481
1517         if isAlpineLinux() {
1518                 return false
1519         }
1520         // NetBSD support is unfinished.
1521         // golang.org/issue/26403
1522         if goos == "netbsd" {
1523                 return false
1524         }
1525         return true
1526 }
1527
1528 func isAlpineLinux() bool {
1529         if runtime.GOOS != "linux" {
1530                 return false
1531         }
1532         fi, err := os.Lstat("/etc/alpine-release")
1533         return err == nil && fi.Mode().IsRegular()
1534 }
1535
1536 func (t *tester) registerRaceTests() {
1537         hdr := "Testing race detector"
1538         t.registerTest("race:runtime/race", hdr,
1539                 &goTest{
1540                         race:     true,
1541                         runTests: "Output",
1542                         pkg:      "runtime/race",
1543                 })
1544         t.registerTest("race", hdr,
1545                 &goTest{
1546                         race:     true,
1547                         runTests: "TestParse|TestEcho|TestStdinCloseRace|TestClosedPipeRace|TestTypeRace|TestFdRace|TestFdReadRace|TestFileCloseRace",
1548                         pkgs:     []string{"flag", "net", "os", "os/exec", "encoding/gob"},
1549                 })
1550         // We don't want the following line, because it
1551         // slows down all.bash (by 10 seconds on my laptop).
1552         // The race builder should catch any error here, but doesn't.
1553         // TODO(iant): Figure out how to catch this.
1554         // t.registerTest("race:cmd/go", hdr, &goTest{race: true, runTests: "TestParallelTest", pkg: "cmd/go"})
1555         if t.cgoEnabled {
1556                 // Building cmd/cgo/internal/test takes a long time.
1557                 // There are already cgo-enabled packages being tested with the race detector.
1558                 // We shouldn't need to redo all of cmd/cgo/internal/test too.
1559                 // The race buildler will take care of this.
1560                 // t.registerTest("race:cmd/cgo/internal/test", hdr, &goTest{dir: "cmd/cgo/internal/test", race: true, env: []string{"GOTRACEBACK=2"}})
1561         }
1562         if t.extLink() {
1563                 // Test with external linking; see issue 9133.
1564                 t.registerTest("race:external", hdr,
1565                         &goTest{
1566                                 race:     true,
1567                                 ldflags:  "-linkmode=external",
1568                                 runTests: "TestParse|TestEcho|TestStdinCloseRace",
1569                                 pkgs:     []string{"flag", "os/exec"},
1570                         })
1571         }
1572 }
1573
1574 // cgoPackages is the standard packages that use cgo.
1575 var cgoPackages = []string{
1576         "net",
1577         "os/user",
1578 }
1579
1580 var funcBenchmark = []byte("\nfunc Benchmark")
1581
1582 // packageHasBenchmarks reports whether pkg has benchmarks.
1583 // On any error, it conservatively returns true.
1584 //
1585 // This exists just to eliminate work on the builders, since compiling
1586 // a test in race mode just to discover it has no benchmarks costs a
1587 // second or two per package, and this function returns false for
1588 // about 100 packages.
1589 func (t *tester) packageHasBenchmarks(pkg string) bool {
1590         pkgDir := filepath.Join(goroot, "src", pkg)
1591         d, err := os.Open(pkgDir)
1592         if err != nil {
1593                 return true // conservatively
1594         }
1595         defer d.Close()
1596         names, err := d.Readdirnames(-1)
1597         if err != nil {
1598                 return true // conservatively
1599         }
1600         for _, name := range names {
1601                 if !strings.HasSuffix(name, "_test.go") {
1602                         continue
1603                 }
1604                 slurp, err := os.ReadFile(filepath.Join(pkgDir, name))
1605                 if err != nil {
1606                         return true // conservatively
1607                 }
1608                 if bytes.Contains(slurp, funcBenchmark) {
1609                         return true
1610                 }
1611         }
1612         return false
1613 }
1614
1615 // makeGOROOTUnwritable makes all $GOROOT files & directories non-writable to
1616 // check that no tests accidentally write to $GOROOT.
1617 func (t *tester) makeGOROOTUnwritable() (undo func()) {
1618         dir := os.Getenv("GOROOT")
1619         if dir == "" {
1620                 panic("GOROOT not set")
1621         }
1622
1623         type pathMode struct {
1624                 path string
1625                 mode os.FileMode
1626         }
1627         var dirs []pathMode // in lexical order
1628
1629         undo = func() {
1630                 for i := range dirs {
1631                         os.Chmod(dirs[i].path, dirs[i].mode) // best effort
1632                 }
1633         }
1634
1635         filepath.WalkDir(dir, func(path string, d fs.DirEntry, err error) error {
1636                 if suffix := strings.TrimPrefix(path, dir+string(filepath.Separator)); suffix != "" {
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
1642                         }
1643                 }
1644                 if err != nil {
1645                         return nil
1646                 }
1647
1648                 info, err := d.Info()
1649                 if err != nil {
1650                         return nil
1651                 }
1652
1653                 mode := info.Mode()
1654                 if mode&0222 != 0 && (mode.IsDir() || mode.IsRegular()) {
1655                         dirs = append(dirs, pathMode{path, mode})
1656                 }
1657                 return nil
1658         })
1659
1660         // Run over list backward to chmod children before parents.
1661         for i := len(dirs) - 1; i >= 0; i-- {
1662                 err := os.Chmod(dirs[i].path, dirs[i].mode&^0222)
1663                 if err != nil {
1664                         dirs = dirs[i:] // Only undo what we did so far.
1665                         undo()
1666                         fatalf("failed to make GOROOT read-only: %v", err)
1667                 }
1668         }
1669
1670         return undo
1671 }
1672
1673 // raceDetectorSupported is a copy of the function
1674 // internal/platform.RaceDetectorSupported, which can't be used here
1675 // because cmd/dist can not import internal packages during bootstrap.
1676 // The race detector only supports 48-bit VMA on arm64. But we don't have
1677 // a good solution to check VMA size(See https://golang.org/issue/29948)
1678 // raceDetectorSupported will always return true for arm64. But race
1679 // detector tests may abort on non 48-bit VMA configuration, the tests
1680 // will be marked as "skipped" in this case.
1681 func raceDetectorSupported(goos, goarch string) bool {
1682         switch goos {
1683         case "linux":
1684                 return goarch == "amd64" || goarch == "ppc64le" || goarch == "arm64" || goarch == "s390x"
1685         case "darwin":
1686                 return goarch == "amd64" || goarch == "arm64"
1687         case "freebsd", "netbsd", "openbsd", "windows":
1688                 return goarch == "amd64"
1689         default:
1690                 return false
1691         }
1692 }
1693
1694 // buildModeSupports is a copy of the function
1695 // internal/platform.BuildModeSupported, which can't be used here
1696 // because cmd/dist can not import internal packages during bootstrap.
1697 func buildModeSupported(compiler, buildmode, goos, goarch string) bool {
1698         if compiler == "gccgo" {
1699                 return true
1700         }
1701
1702         platform := goos + "/" + goarch
1703
1704         switch buildmode {
1705         case "archive":
1706                 return true
1707
1708         case "c-archive":
1709                 switch goos {
1710                 case "aix", "darwin", "ios", "windows":
1711                         return true
1712                 case "linux":
1713                         switch goarch {
1714                         case "386", "amd64", "arm", "armbe", "arm64", "arm64be", "loong64", "ppc64le", "riscv64", "s390x":
1715                                 // linux/ppc64 not supported because it does
1716                                 // not support external linking mode yet.
1717                                 return true
1718                         default:
1719                                 // Other targets do not support -shared,
1720                                 // per ParseFlags in
1721                                 // cmd/compile/internal/base/flag.go.
1722                                 // For c-archive the Go tool passes -shared,
1723                                 // so that the result is suitable for inclusion
1724                                 // in a PIE or shared library.
1725                                 return false
1726                         }
1727                 case "freebsd":
1728                         return goarch == "amd64"
1729                 }
1730                 return false
1731
1732         case "c-shared":
1733                 switch platform {
1734                 case "linux/amd64", "linux/arm", "linux/arm64", "linux/loong64", "linux/386", "linux/ppc64le", "linux/riscv64", "linux/s390x",
1735                         "android/amd64", "android/arm", "android/arm64", "android/386",
1736                         "freebsd/amd64",
1737                         "darwin/amd64", "darwin/arm64",
1738                         "windows/amd64", "windows/386", "windows/arm64":
1739                         return true
1740                 }
1741                 return false
1742
1743         case "default":
1744                 return true
1745
1746         case "exe":
1747                 return true
1748
1749         case "pie":
1750                 switch platform {
1751                 case "linux/386", "linux/amd64", "linux/arm", "linux/arm64", "linux/loong64", "linux/ppc64le", "linux/riscv64", "linux/s390x",
1752                         "android/amd64", "android/arm", "android/arm64", "android/386",
1753                         "freebsd/amd64",
1754                         "darwin/amd64", "darwin/arm64",
1755                         "ios/amd64", "ios/arm64",
1756                         "aix/ppc64",
1757                         "windows/386", "windows/amd64", "windows/arm", "windows/arm64":
1758                         return true
1759                 }
1760                 return false
1761
1762         case "shared":
1763                 switch platform {
1764                 case "linux/386", "linux/amd64", "linux/arm", "linux/arm64", "linux/ppc64le", "linux/s390x":
1765                         return true
1766                 }
1767                 return false
1768
1769         case "plugin":
1770                 switch platform {
1771                 case "linux/amd64", "linux/arm", "linux/arm64", "linux/386", "linux/s390x", "linux/ppc64le",
1772                         "android/amd64", "android/386",
1773                         "darwin/amd64", "darwin/arm64",
1774                         "freebsd/amd64":
1775                         return true
1776                 }
1777                 return false
1778
1779         default:
1780                 return false
1781         }
1782 }
1783
1784 // isUnsupportedVMASize reports whether the failure is caused by an unsupported
1785 // VMA for the race detector (for example, running the race detector on an
1786 // arm64 machine configured with 39-bit VMA)
1787 func isUnsupportedVMASize(w *work) bool {
1788         unsupportedVMA := []byte("unsupported VMA range")
1789         return w.dt.name == "race" && bytes.Contains(w.out, unsupportedVMA)
1790 }
1791
1792 // isEnvSet reports whether the environment variable evar is
1793 // set in the environment.
1794 func isEnvSet(evar string) bool {
1795         evarEq := evar + "="
1796         for _, e := range os.Environ() {
1797                 if strings.HasPrefix(e, evarEq) {
1798                         return true
1799                 }
1800         }
1801         return false
1802 }