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