]> Cypherpunks.ru repositories - gostls13.git/blob - src/cmd/dist/test.go
runtime: implement wasip1 netpoll
[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         "encoding/json"
10         "flag"
11         "fmt"
12         "io"
13         "io/fs"
14         "log"
15         "os"
16         "os/exec"
17         "path/filepath"
18         "reflect"
19         "regexp"
20         "runtime"
21         "strconv"
22         "strings"
23         "time"
24 )
25
26 func cmdtest() {
27         gogcflags = os.Getenv("GO_GCFLAGS")
28         setNoOpt()
29
30         var t tester
31
32         var noRebuild bool
33         flag.BoolVar(&t.listMode, "list", false, "list available tests")
34         flag.BoolVar(&t.rebuild, "rebuild", false, "rebuild everything first")
35         flag.BoolVar(&noRebuild, "no-rebuild", false, "overrides -rebuild (historical dreg)")
36         flag.BoolVar(&t.keepGoing, "k", false, "keep going even when error occurred")
37         flag.BoolVar(&t.race, "race", false, "run in race builder mode (different set of tests)")
38         flag.BoolVar(&t.compileOnly, "compile-only", false, "compile tests, but don't run them")
39         flag.StringVar(&t.banner, "banner", "##### ", "banner prefix; blank means no section banners")
40         flag.StringVar(&t.runRxStr, "run", "",
41                 "run only those tests matching the regular expression; empty means to run all. "+
42                         "Special exception: if the string begins with '!', the match is inverted.")
43         flag.BoolVar(&t.msan, "msan", false, "run in memory sanitizer builder mode")
44         flag.BoolVar(&t.asan, "asan", false, "run in address sanitizer builder mode")
45         flag.BoolVar(&t.json, "json", false, "report test results in JSON")
46
47         xflagparse(-1) // any number of args
48         if noRebuild {
49                 t.rebuild = false
50         }
51
52         t.run()
53 }
54
55 // tester executes cmdtest.
56 type tester struct {
57         race        bool
58         msan        bool
59         asan        bool
60         listMode    bool
61         rebuild     bool
62         failed      bool
63         keepGoing   bool
64         compileOnly bool // just try to compile all tests, but no need to run
65         runRxStr    string
66         runRx       *regexp.Regexp
67         runRxWant   bool     // want runRx to match (true) or not match (false)
68         runNames    []string // tests to run, exclusive with runRx; empty means all
69         banner      string   // prefix, or "" for none
70         lastHeading string   // last dir heading printed
71
72         short      bool
73         cgoEnabled bool
74         partial    bool
75         json       bool
76
77         tests        []distTest // use addTest to extend
78         testNames    map[string]bool
79         timeoutScale int
80
81         worklist []*work
82 }
83
84 type work struct {
85         dt    *distTest
86         cmd   *exec.Cmd // Must write stdout/stderr to work.out
87         flush func()    // If non-nil, called after cmd.Run
88         start chan bool
89         out   bytes.Buffer
90         err   error
91         end   chan bool
92 }
93
94 // A distTest is a test run by dist test.
95 // Each test has a unique name and belongs to a group (heading)
96 type distTest struct {
97         name    string // unique test name; may be filtered with -run flag
98         heading string // group section; this header is printed before the test is run.
99         fn      func(*distTest) error
100 }
101
102 func (t *tester) run() {
103         timelog("start", "dist test")
104
105         os.Setenv("PATH", fmt.Sprintf("%s%c%s", gorootBin, os.PathListSeparator, os.Getenv("PATH")))
106
107         t.short = true
108         if v := os.Getenv("GO_TEST_SHORT"); v != "" {
109                 short, err := strconv.ParseBool(v)
110                 if err != nil {
111                         fatalf("invalid GO_TEST_SHORT %q: %v", v, err)
112                 }
113                 t.short = short
114         }
115
116         cmd := exec.Command(gorootBinGo, "env", "CGO_ENABLED")
117         cmd.Stderr = new(bytes.Buffer)
118         slurp, err := cmd.Output()
119         if err != nil {
120                 fatalf("Error running %s: %v\n%s", cmd, err, cmd.Stderr)
121         }
122         parts := strings.Split(string(slurp), "\n")
123         if nlines := len(parts) - 1; nlines < 1 {
124                 fatalf("Error running %s: output contains <1 lines\n%s", cmd, cmd.Stderr)
125         }
126         t.cgoEnabled, _ = strconv.ParseBool(parts[0])
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.testNames[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 !t.json {
218                 if err := t.maybeLogMetadata(); err != nil {
219                         t.failed = true
220                         if t.keepGoing {
221                                 log.Printf("Failed logging metadata: %v", err)
222                         } else {
223                                 fatalf("Failed logging metadata: %v", err)
224                         }
225                 }
226         }
227
228         for _, dt := range t.tests {
229                 if !t.shouldRunTest(dt.name) {
230                         t.partial = true
231                         continue
232                 }
233                 dt := dt // dt used in background after this iteration
234                 if err := dt.fn(&dt); err != nil {
235                         t.runPending(&dt) // in case that hasn't been done yet
236                         t.failed = true
237                         if t.keepGoing {
238                                 log.Printf("Failed: %v", err)
239                         } else {
240                                 fatalf("Failed: %v", err)
241                         }
242                 }
243         }
244         t.runPending(nil)
245         timelog("end", "dist test")
246
247         if !t.json {
248                 if t.failed {
249                         fmt.Println("\nFAILED")
250                 } else if t.partial {
251                         fmt.Println("\nALL TESTS PASSED (some were excluded)")
252                 } else {
253                         fmt.Println("\nALL TESTS PASSED")
254                 }
255         }
256         if t.failed {
257                 xexit(1)
258         }
259 }
260
261 func (t *tester) shouldRunTest(name string) bool {
262         if t.runRx != nil {
263                 return t.runRx.MatchString(name) == t.runRxWant
264         }
265         if len(t.runNames) == 0 {
266                 return true
267         }
268         for _, runName := range t.runNames {
269                 if runName == name {
270                         return true
271                 }
272         }
273         return false
274 }
275
276 func (t *tester) maybeLogMetadata() error {
277         if t.compileOnly {
278                 // We need to run a subprocess to log metadata. Don't do that
279                 // on compile-only runs.
280                 return nil
281         }
282         t.out("Test execution environment.")
283         // Helper binary to print system metadata (CPU model, etc). This is a
284         // separate binary from dist so it need not build with the bootstrap
285         // toolchain.
286         //
287         // TODO(prattmic): If we split dist bootstrap and dist test then this
288         // could be simplified to directly use internal/sysinfo here.
289         return t.dirCmd(filepath.Join(goroot, "src/cmd/internal/metadata"), gorootBinGo, []string{"run", "main.go"}).Run()
290 }
291
292 // testName returns the dist test name for a given package and variant.
293 func testName(pkg, variant string) string {
294         name := pkg
295         if variant != "" {
296                 name += ":" + variant
297         }
298         return name
299 }
300
301 // goTest represents all options to a "go test" command. The final command will
302 // combine configuration from goTest and tester flags.
303 type goTest struct {
304         timeout  time.Duration // If non-zero, override timeout
305         short    bool          // If true, force -short
306         tags     []string      // Build tags
307         race     bool          // Force -race
308         bench    bool          // Run benchmarks (briefly), not tests.
309         runTests string        // Regexp of tests to run
310         cpu      string        // If non-empty, -cpu flag
311
312         gcflags   string // If non-empty, build with -gcflags=all=X
313         ldflags   string // If non-empty, build with -ldflags=X
314         buildmode string // If non-empty, -buildmode flag
315
316         env []string // Environment variables to add, as KEY=VAL. KEY= unsets a variable
317
318         runOnHost bool // When cross-compiling, run this test on the host instead of guest
319
320         // variant, if non-empty, is a name used to distinguish different
321         // configurations of the same test package(s). If set and omitVariant is false,
322         // the Package field in test2json output is rewritten to pkg:variant.
323         variant string
324         // omitVariant indicates that variant is used solely for the dist test name and
325         // that the set of test names run by each variant (including empty) of a package
326         // is non-overlapping.
327         omitVariant bool
328
329         // We have both pkg and pkgs as a convenience. Both may be set, in which
330         // case they will be combined. At least one must be set.
331         pkgs []string // Multiple packages to test
332         pkg  string   // A single package to test
333
334         testFlags []string // Additional flags accepted by this test
335 }
336
337 // bgCommand returns a go test Cmd and a post-Run flush function. The result
338 // will write its output to stdout and stderr. If stdout==stderr, bgCommand
339 // ensures Writes are serialized. The caller should call flush() after Cmd exits.
340 func (opts *goTest) bgCommand(t *tester, stdout, stderr io.Writer) (cmd *exec.Cmd, flush func()) {
341         build, run, pkgs, testFlags, setupCmd := opts.buildArgs(t)
342
343         // Combine the flags.
344         args := append([]string{"test"}, build...)
345         if t.compileOnly {
346                 args = append(args, "-c", "-o", os.DevNull)
347         } else {
348                 args = append(args, run...)
349         }
350         args = append(args, pkgs...)
351         if !t.compileOnly {
352                 args = append(args, testFlags...)
353         }
354
355         cmd = exec.Command(gorootBinGo, args...)
356         setupCmd(cmd)
357         if t.json && opts.variant != "" && !opts.omitVariant {
358                 // Rewrite Package in the JSON output to be pkg:variant. When omitVariant
359                 // is true, pkg.TestName is already unambiguous, so we don't need to
360                 // rewrite the Package field.
361                 //
362                 // We only want to process JSON on the child's stdout. Ideally if
363                 // stdout==stderr, we would also use the same testJSONFilter for
364                 // cmd.Stdout and cmd.Stderr in order to keep the underlying
365                 // interleaving of writes, but then it would see even partial writes
366                 // interleaved, which would corrupt the JSON. So, we only process
367                 // cmd.Stdout. This has another consequence though: if stdout==stderr,
368                 // we have to serialize Writes in case the Writer is not concurrent
369                 // safe. If we were just passing stdout/stderr through to exec, it would
370                 // do this for us, but since we're wrapping stdout, we have to do it
371                 // ourselves.
372                 if stdout == stderr {
373                         stdout = &lockedWriter{w: stdout}
374                         stderr = stdout
375                 }
376                 f := &testJSONFilter{w: stdout, variant: opts.variant}
377                 cmd.Stdout = f
378                 flush = f.Flush
379         } else {
380                 cmd.Stdout = stdout
381                 flush = func() {}
382         }
383         cmd.Stderr = stderr
384
385         return cmd, flush
386 }
387
388 // run runs a go test and returns an error if it does not succeed.
389 func (opts *goTest) run(t *tester) error {
390         cmd, flush := opts.bgCommand(t, os.Stdout, os.Stderr)
391         err := cmd.Run()
392         flush()
393         return err
394 }
395
396 // buildArgs is in internal helper for goTest that constructs the elements of
397 // the "go test" command line. build is the flags for building the test. run is
398 // the flags for running the test. pkgs is the list of packages to build and
399 // run. testFlags is the list of flags to pass to the test package.
400 //
401 // The caller must call setupCmd on the resulting exec.Cmd to set its directory
402 // and environment.
403 func (opts *goTest) buildArgs(t *tester) (build, run, pkgs, testFlags []string, setupCmd func(*exec.Cmd)) {
404         run = append(run, "-count=1") // Disallow caching
405         if opts.timeout != 0 {
406                 d := opts.timeout * time.Duration(t.timeoutScale)
407                 run = append(run, "-timeout="+d.String())
408         }
409         if opts.short || t.short {
410                 run = append(run, "-short")
411         }
412         var tags []string
413         if t.iOS() {
414                 tags = append(tags, "lldb")
415         }
416         if noOpt {
417                 tags = append(tags, "noopt")
418         }
419         tags = append(tags, opts.tags...)
420         if len(tags) > 0 {
421                 build = append(build, "-tags="+strings.Join(tags, ","))
422         }
423         if t.race || opts.race {
424                 build = append(build, "-race")
425         }
426         if t.msan {
427                 build = append(build, "-msan")
428         }
429         if t.asan {
430                 build = append(build, "-asan")
431         }
432         if opts.bench {
433                 // Run no tests.
434                 run = append(run, "-run=^$")
435                 // Run benchmarks briefly as a smoke test.
436                 run = append(run, "-bench=.*", "-benchtime=.1s")
437         } else if opts.runTests != "" {
438                 run = append(run, "-run="+opts.runTests)
439         }
440         if opts.cpu != "" {
441                 run = append(run, "-cpu="+opts.cpu)
442         }
443         if t.json {
444                 run = append(run, "-json")
445         }
446
447         if opts.gcflags != "" {
448                 build = append(build, "-gcflags=all="+opts.gcflags)
449         }
450         if opts.ldflags != "" {
451                 build = append(build, "-ldflags="+opts.ldflags)
452         }
453         if opts.buildmode != "" {
454                 build = append(build, "-buildmode="+opts.buildmode)
455         }
456
457         pkgs = opts.packages()
458
459         runOnHost := opts.runOnHost && (goarch != gohostarch || goos != gohostos)
460         needTestFlags := len(opts.testFlags) > 0 || runOnHost
461         if needTestFlags {
462                 testFlags = append([]string{"-args"}, opts.testFlags...)
463         }
464         if runOnHost {
465                 // -target is a special flag understood by tests that can run on the host
466                 testFlags = append(testFlags, "-target="+goos+"/"+goarch)
467         }
468
469         setupCmd = func(cmd *exec.Cmd) {
470                 setDir(cmd, filepath.Join(goroot, "src"))
471                 if len(opts.env) != 0 {
472                         for _, kv := range opts.env {
473                                 if i := strings.Index(kv, "="); i < 0 {
474                                         unsetEnv(cmd, kv[:len(kv)-1])
475                                 } else {
476                                         setEnv(cmd, kv[:i], kv[i+1:])
477                                 }
478                         }
479                 }
480                 if runOnHost {
481                         setEnv(cmd, "GOARCH", gohostarch)
482                         setEnv(cmd, "GOOS", gohostos)
483                 }
484         }
485
486         return
487 }
488
489 // packages returns the full list of packages to be run by this goTest. This
490 // will always include at least one package.
491 func (opts *goTest) packages() []string {
492         pkgs := opts.pkgs
493         if opts.pkg != "" {
494                 pkgs = append(pkgs[:len(pkgs):len(pkgs)], opts.pkg)
495         }
496         if len(pkgs) == 0 {
497                 panic("no packages")
498         }
499         return pkgs
500 }
501
502 // ranGoTest and stdMatches are state closed over by the stdlib
503 // testing func in registerStdTest below. The tests are run
504 // sequentially, so there's no need for locks.
505 //
506 // ranGoBench and benchMatches are the same, but are only used
507 // in -race mode.
508 var (
509         ranGoTest  bool
510         stdMatches []string
511
512         ranGoBench   bool
513         benchMatches []string
514 )
515
516 func (t *tester) registerStdTest(pkg string) {
517         const stdTestHeading = "Testing packages." // known to addTest for a safety check
518         gcflags := gogcflags
519         name := testName(pkg, "")
520         if t.runRx == nil || t.runRx.MatchString(name) == t.runRxWant {
521                 stdMatches = append(stdMatches, pkg)
522         }
523         t.addTest(name, stdTestHeading, func(dt *distTest) error {
524                 if ranGoTest {
525                         return nil
526                 }
527                 t.runPending(dt)
528                 timelog("start", dt.name)
529                 defer timelog("end", dt.name)
530                 ranGoTest = true
531
532                 timeoutSec := 180 * time.Second
533                 for _, pkg := range stdMatches {
534                         if pkg == "cmd/go" {
535                                 timeoutSec *= 3
536                                 break
537                         }
538                 }
539                 return (&goTest{
540                         timeout: timeoutSec,
541                         gcflags: gcflags,
542                         pkgs:    stdMatches,
543                 }).run(t)
544         })
545 }
546
547 func (t *tester) registerRaceBenchTest(pkg string) {
548         const raceBenchHeading = "Running benchmarks briefly." // known to addTest for a safety check
549         name := testName(pkg, "racebench")
550         if t.runRx == nil || t.runRx.MatchString(name) == t.runRxWant {
551                 benchMatches = append(benchMatches, pkg)
552         }
553         t.addTest(name, raceBenchHeading, func(dt *distTest) error {
554                 if ranGoBench {
555                         return nil
556                 }
557                 t.runPending(dt)
558                 timelog("start", dt.name)
559                 defer timelog("end", dt.name)
560                 ranGoBench = true
561                 return (&goTest{
562                         variant:     "racebench",
563                         omitVariant: true,               // The only execution of benchmarks in dist; benchmark names are guaranteed not to overlap with test names.
564                         timeout:     1200 * time.Second, // longer timeout for race with benchmarks
565                         race:        true,
566                         bench:       true,
567                         cpu:         "4",
568                         pkgs:        benchMatches,
569                 }).run(t)
570         })
571 }
572
573 func (t *tester) registerTests() {
574         // registerStdTestSpecially tracks import paths in the standard library
575         // whose test registration happens in a special way.
576         registerStdTestSpecially := map[string]bool{
577                 "runtime/internal/wasitest": true, // Registered at the bottom as a host test.
578                 "cmd/internal/testdir":      true, // Registered at the bottom with sharding.
579         }
580
581         // Fast path to avoid the ~1 second of `go list std cmd` when
582         // the caller lists specific tests to run. (as the continuous
583         // build coordinator does).
584         if len(t.runNames) > 0 {
585                 for _, name := range t.runNames {
586                         if !strings.Contains(name, ":") {
587                                 t.registerStdTest(name)
588                         } else if strings.HasSuffix(name, ":racebench") {
589                                 t.registerRaceBenchTest(strings.TrimSuffix(name, ":racebench"))
590                         }
591                 }
592         } else {
593                 // Use a format string to only list packages and commands that have tests.
594                 const format = "{{if (or .TestGoFiles .XTestGoFiles)}}{{.ImportPath}}{{end}}"
595                 cmd := exec.Command(gorootBinGo, "list", "-f", format)
596                 if t.race {
597                         cmd.Args = append(cmd.Args, "-tags=race")
598                 }
599                 cmd.Args = append(cmd.Args, "std", "cmd")
600                 cmd.Stderr = new(bytes.Buffer)
601                 all, err := cmd.Output()
602                 if err != nil {
603                         fatalf("Error running go list std cmd: %v:\n%s", err, cmd.Stderr)
604                 }
605                 pkgs := strings.Fields(string(all))
606                 for _, pkg := range pkgs {
607                         if registerStdTestSpecially[pkg] {
608                                 continue
609                         }
610                         t.registerStdTest(pkg)
611                 }
612                 if t.race {
613                         for _, pkg := range pkgs {
614                                 if t.packageHasBenchmarks(pkg) {
615                                         t.registerRaceBenchTest(pkg)
616                                 }
617                         }
618                 }
619         }
620
621         if t.race {
622                 return
623         }
624
625         // Test the os/user package in the pure-Go mode too.
626         if !t.compileOnly {
627                 t.registerTest("os/user with tag osusergo",
628                         &goTest{
629                                 variant: "osusergo",
630                                 timeout: 300 * time.Second,
631                                 tags:    []string{"osusergo"},
632                                 pkg:     "os/user",
633                         })
634                 t.registerTest("hash/maphash purego implementation",
635                         &goTest{
636                                 variant: "purego",
637                                 timeout: 300 * time.Second,
638                                 tags:    []string{"purego"},
639                                 pkg:     "hash/maphash",
640                         })
641         }
642
643         // Test ios/amd64 for the iOS simulator.
644         if goos == "darwin" && goarch == "amd64" && t.cgoEnabled {
645                 t.registerTest("GOOS=ios on darwin/amd64",
646                         &goTest{
647                                 variant:  "amd64ios",
648                                 timeout:  300 * time.Second,
649                                 runTests: "SystemRoots",
650                                 env:      []string{"GOOS=ios", "CGO_ENABLED=1"},
651                                 pkg:      "crypto/x509",
652                         })
653         }
654
655         // Runtime CPU tests.
656         if !t.compileOnly && t.hasParallelism() {
657                 t.registerTest("GOMAXPROCS=2 runtime -cpu=1,2,4 -quick",
658                         &goTest{
659                                 variant:   "cpu124",
660                                 timeout:   300 * time.Second,
661                                 cpu:       "1,2,4",
662                                 short:     true,
663                                 testFlags: []string{"-quick"},
664                                 // We set GOMAXPROCS=2 in addition to -cpu=1,2,4 in order to test runtime bootstrap code,
665                                 // creation of first goroutines and first garbage collections in the parallel setting.
666                                 env: []string{"GOMAXPROCS=2"},
667                                 pkg: "runtime",
668                         })
669         }
670
671         // morestack tests. We only run these on in long-test mode
672         // (with GO_TEST_SHORT=false) because the runtime test is
673         // already quite long and mayMoreStackMove makes it about
674         // twice as slow.
675         if !t.compileOnly && !t.short {
676                 // hooks is the set of maymorestack hooks to test with.
677                 hooks := []string{"mayMoreStackPreempt", "mayMoreStackMove"}
678                 // hookPkgs is the set of package patterns to apply
679                 // the maymorestack hook to.
680                 hookPkgs := []string{"runtime/...", "reflect", "sync"}
681                 // unhookPkgs is the set of package patterns to
682                 // exclude from hookPkgs.
683                 unhookPkgs := []string{"runtime/testdata/..."}
684                 for _, hook := range hooks {
685                         // Construct the build flags to use the
686                         // maymorestack hook in the compiler and
687                         // assembler. We pass this via the GOFLAGS
688                         // environment variable so that it applies to
689                         // both the test itself and to binaries built
690                         // by the test.
691                         goFlagsList := []string{}
692                         for _, flag := range []string{"-gcflags", "-asmflags"} {
693                                 for _, hookPkg := range hookPkgs {
694                                         goFlagsList = append(goFlagsList, flag+"="+hookPkg+"=-d=maymorestack=runtime."+hook)
695                                 }
696                                 for _, unhookPkg := range unhookPkgs {
697                                         goFlagsList = append(goFlagsList, flag+"="+unhookPkg+"=")
698                                 }
699                         }
700                         goFlags := strings.Join(goFlagsList, " ")
701
702                         t.registerTest("maymorestack="+hook,
703                                 &goTest{
704                                         variant: hook,
705                                         timeout: 600 * time.Second,
706                                         short:   true,
707                                         env:     []string{"GOFLAGS=" + goFlags},
708                                         pkgs:    []string{"runtime", "reflect", "sync"},
709                                 })
710                 }
711         }
712
713         // Test that internal linking of standard packages does not
714         // require libgcc. This ensures that we can install a Go
715         // release on a system that does not have a C compiler
716         // installed and still build Go programs (that don't use cgo).
717         for _, pkg := range cgoPackages {
718                 if !t.internalLink() {
719                         break
720                 }
721
722                 // ARM libgcc may be Thumb, which internal linking does not support.
723                 if goarch == "arm" {
724                         break
725                 }
726
727                 // What matters is that the tests build and start up.
728                 // Skip expensive tests, especially x509 TestSystemRoots.
729                 run := "^Test[^CS]"
730                 if pkg == "net" {
731                         run = "TestTCPStress"
732                 }
733                 t.registerTest("Testing without libgcc.",
734                         &goTest{
735                                 variant:  "nolibgcc",
736                                 ldflags:  "-linkmode=internal -libgcc=none",
737                                 runTests: run,
738                                 pkg:      pkg,
739                         })
740         }
741
742         // Stub out following test on alpine until 54354 resolved.
743         builderName := os.Getenv("GO_BUILDER_NAME")
744         disablePIE := strings.HasSuffix(builderName, "-alpine")
745
746         // Test internal linking of PIE binaries where it is supported.
747         if t.internalLinkPIE() && !disablePIE {
748                 t.registerTest("internal linking of -buildmode=pie",
749                         &goTest{
750                                 variant:   "pie_internal",
751                                 timeout:   60 * time.Second,
752                                 buildmode: "pie",
753                                 ldflags:   "-linkmode=internal",
754                                 env:       []string{"CGO_ENABLED=0"},
755                                 pkg:       "reflect",
756                         })
757                 // Also test a cgo package.
758                 if t.cgoEnabled && t.internalLink() && !disablePIE {
759                         t.registerTest("internal linking of -buildmode=pie",
760                                 &goTest{
761                                         variant:   "pie_internal",
762                                         timeout:   60 * time.Second,
763                                         buildmode: "pie",
764                                         ldflags:   "-linkmode=internal",
765                                         pkg:       "os/user",
766                                 })
767                 }
768         }
769
770         // sync tests
771         if t.hasParallelism() {
772                 t.registerTest("sync -cpu=10",
773                         &goTest{
774                                 variant: "cpu10",
775                                 timeout: 120 * time.Second,
776                                 cpu:     "10",
777                                 pkg:     "sync",
778                         })
779         }
780
781         if t.raceDetectorSupported() {
782                 t.registerRaceTests()
783         }
784
785         const cgoHeading = "Testing cgo"
786         if t.cgoEnabled {
787                 t.registerCgoTests(cgoHeading)
788         }
789
790         if goos == "wasip1" {
791                 t.registerTest("wasip1 host tests",
792                         &goTest{
793                                 variant:   "host",
794                                 pkg:       "runtime/internal/wasitest",
795                                 timeout:   1 * time.Minute,
796                                 runOnHost: true,
797                         })
798         }
799
800         if goos != "android" && !t.iOS() {
801                 // Only start multiple test dir shards on builders,
802                 // where they get distributed to multiple machines.
803                 // See issues 20141 and 31834.
804                 nShards := 1
805                 if os.Getenv("GO_BUILDER_NAME") != "" {
806                         nShards = 10
807                 }
808                 if n, err := strconv.Atoi(os.Getenv("GO_TEST_SHARDS")); err == nil {
809                         nShards = n
810                 }
811                 for shard := 0; shard < nShards; shard++ {
812                         id := fmt.Sprintf("%d_%d", shard, nShards)
813                         t.registerTest("../test",
814                                 &goTest{
815                                         variant:     id,
816                                         omitVariant: true, // Shards of the same Go package; tests are guaranteed not to overlap.
817                                         pkg:         "cmd/internal/testdir",
818                                         testFlags:   []string{fmt.Sprintf("-shard=%d", shard), fmt.Sprintf("-shards=%d", nShards)},
819                                         runOnHost:   true,
820                                 },
821                         )
822                 }
823         }
824         // Only run the API check on fast development platforms.
825         // Every platform checks the API on every GOOS/GOARCH/CGO_ENABLED combination anyway,
826         // so we really only need to run this check once anywhere to get adequate coverage.
827         // To help developers avoid trybot-only failures, we try to run on typical developer machines
828         // which is darwin,linux,windows/amd64 and darwin/arm64.
829         if goos == "darwin" || ((goos == "linux" || goos == "windows") && goarch == "amd64") {
830                 t.registerTest("API check", &goTest{variant: "check", pkg: "cmd/api", timeout: 5 * time.Minute, testFlags: []string{"-check"}})
831         }
832 }
833
834 // addTest adds an arbitrary test callback to the test list.
835 //
836 // name must uniquely identify the test and heading must be non-empty.
837 func (t *tester) addTest(name, heading string, fn func(*distTest) error) {
838         if t.testNames[name] {
839                 panic("duplicate registered test name " + name)
840         }
841         if heading == "" {
842                 panic("empty heading")
843         }
844         // Two simple checks for cases that would conflict with the fast path in registerTests.
845         if !strings.Contains(name, ":") && heading != "Testing packages." {
846                 panic("empty variant is reserved exclusively for registerStdTest")
847         } else if strings.HasSuffix(name, ":racebench") && heading != "Running benchmarks briefly." {
848                 panic("racebench variant is reserved exclusively for registerRaceBenchTest")
849         }
850         if t.testNames == nil {
851                 t.testNames = make(map[string]bool)
852         }
853         t.testNames[name] = true
854         t.tests = append(t.tests, distTest{
855                 name:    name,
856                 heading: heading,
857                 fn:      fn,
858         })
859 }
860
861 type registerTestOpt interface {
862         isRegisterTestOpt()
863 }
864
865 // rtSkipFunc is a registerTest option that runs a skip check function before
866 // running the test.
867 type rtSkipFunc struct {
868         skip func(*distTest) (string, bool) // Return message, true to skip the test
869 }
870
871 func (rtSkipFunc) isRegisterTestOpt() {}
872
873 // registerTest registers a test that runs the given goTest.
874 //
875 // Each Go package in goTest will have a corresponding test
876 // "<pkg>:<variant>", which must uniquely identify the test.
877 //
878 // heading and test.variant must be non-empty.
879 func (t *tester) registerTest(heading string, test *goTest, opts ...registerTestOpt) {
880         var skipFunc func(*distTest) (string, bool)
881         for _, opt := range opts {
882                 switch opt := opt.(type) {
883                 case rtSkipFunc:
884                         skipFunc = opt.skip
885                 }
886         }
887         // Register each test package as a separate test.
888         register1 := func(test *goTest) {
889                 if test.variant == "" {
890                         panic("empty variant")
891                 }
892                 name := testName(test.pkg, test.variant)
893                 t.addTest(name, heading, func(dt *distTest) error {
894                         if skipFunc != nil {
895                                 msg, skip := skipFunc(dt)
896                                 if skip {
897                                         t.printSkip(test, msg)
898                                         return nil
899                                 }
900                         }
901                         w := &work{dt: dt}
902                         w.cmd, w.flush = test.bgCommand(t, &w.out, &w.out)
903                         t.worklist = append(t.worklist, w)
904                         return nil
905                 })
906         }
907         if test.pkg != "" && len(test.pkgs) == 0 {
908                 // Common case. Avoid copying.
909                 register1(test)
910                 return
911         }
912         // TODO(dmitshur,austin): It might be better to unify the execution of 'go test pkg'
913         // invocations for the same variant to be done with a single 'go test pkg1 pkg2 pkg3'
914         // command, just like it's already done in registerStdTest and registerRaceBenchTest.
915         // Those methods accumulate matched packages in stdMatches and benchMatches slices,
916         // and we can extend that mechanism to work for all other equal variant registrations.
917         // Do the simple thing to start with.
918         for _, pkg := range test.packages() {
919                 test1 := *test
920                 test1.pkg, test1.pkgs = pkg, nil
921                 register1(&test1)
922         }
923 }
924
925 func (t *tester) printSkip(test *goTest, msg string) {
926         if !t.json {
927                 fmt.Println(msg)
928                 return
929         }
930         type event struct {
931                 Time    time.Time
932                 Action  string
933                 Package string
934                 Output  string `json:",omitempty"`
935         }
936         out := json.NewEncoder(os.Stdout)
937         for _, pkg := range test.packages() {
938                 ev := event{Time: time.Now(), Package: testName(pkg, test.variant), Action: "start"}
939                 out.Encode(ev)
940                 ev.Action = "output"
941                 ev.Output = msg
942                 out.Encode(ev)
943                 ev.Action = "skip"
944                 ev.Output = ""
945                 out.Encode(ev)
946         }
947 }
948
949 // dirCmd constructs a Cmd intended to be run in the foreground.
950 // The command will be run in dir, and Stdout and Stderr will go to os.Stdout
951 // and os.Stderr.
952 func (t *tester) dirCmd(dir string, cmdline ...interface{}) *exec.Cmd {
953         bin, args := flattenCmdline(cmdline)
954         cmd := exec.Command(bin, args...)
955         if filepath.IsAbs(dir) {
956                 setDir(cmd, dir)
957         } else {
958                 setDir(cmd, filepath.Join(goroot, dir))
959         }
960         cmd.Stdout = os.Stdout
961         cmd.Stderr = os.Stderr
962         if vflag > 1 {
963                 errprintf("%s\n", strings.Join(cmd.Args, " "))
964         }
965         return cmd
966 }
967
968 // flattenCmdline flattens a mixture of string and []string as single list
969 // and then interprets it as a command line: first element is binary, then args.
970 func flattenCmdline(cmdline []interface{}) (bin string, args []string) {
971         var list []string
972         for _, x := range cmdline {
973                 switch x := x.(type) {
974                 case string:
975                         list = append(list, x)
976                 case []string:
977                         list = append(list, x...)
978                 default:
979                         panic("invalid dirCmd argument type: " + reflect.TypeOf(x).String())
980                 }
981         }
982
983         bin = list[0]
984         if !filepath.IsAbs(bin) {
985                 panic("command is not absolute: " + bin)
986         }
987         return bin, list[1:]
988 }
989
990 func (t *tester) iOS() bool {
991         return goos == "ios"
992 }
993
994 func (t *tester) out(v string) {
995         if t.json {
996                 return
997         }
998         if t.banner == "" {
999                 return
1000         }
1001         fmt.Println("\n" + t.banner + v)
1002 }
1003
1004 // extLink reports whether the current goos/goarch supports
1005 // external linking. This should match the test in determineLinkMode
1006 // in cmd/link/internal/ld/config.go.
1007 func (t *tester) extLink() bool {
1008         if goarch == "ppc64" && goos != "aix" {
1009                 return false
1010         }
1011         return true
1012 }
1013
1014 func (t *tester) internalLink() bool {
1015         if gohostos == "dragonfly" {
1016                 // linkmode=internal fails on dragonfly since errno is a TLS relocation.
1017                 return false
1018         }
1019         if goos == "android" {
1020                 return false
1021         }
1022         if goos == "ios" {
1023                 return false
1024         }
1025         if goos == "windows" && goarch == "arm64" {
1026                 return false
1027         }
1028         // Internally linking cgo is incomplete on some architectures.
1029         // https://golang.org/issue/10373
1030         // https://golang.org/issue/14449
1031         if goarch == "loong64" || goarch == "mips64" || goarch == "mips64le" || goarch == "mips" || goarch == "mipsle" || goarch == "riscv64" {
1032                 return false
1033         }
1034         if goos == "aix" {
1035                 // linkmode=internal isn't supported.
1036                 return false
1037         }
1038         return true
1039 }
1040
1041 func (t *tester) internalLinkPIE() bool {
1042         switch goos + "-" + goarch {
1043         case "darwin-amd64", "darwin-arm64",
1044                 "linux-amd64", "linux-arm64", "linux-ppc64le",
1045                 "android-arm64",
1046                 "windows-amd64", "windows-386", "windows-arm":
1047                 return true
1048         }
1049         return false
1050 }
1051
1052 // supportedBuildMode reports whether the given build mode is supported.
1053 func (t *tester) supportedBuildmode(mode string) bool {
1054         switch mode {
1055         case "c-archive", "c-shared", "shared", "plugin", "pie":
1056         default:
1057                 fatalf("internal error: unknown buildmode %s", mode)
1058                 return false
1059         }
1060
1061         return buildModeSupported("gc", mode, goos, goarch)
1062 }
1063
1064 func (t *tester) registerCgoTests(heading string) {
1065         cgoTest := func(variant string, subdir, linkmode, buildmode string, opts ...registerTestOpt) *goTest {
1066                 gt := &goTest{
1067                         variant:   variant,
1068                         pkg:       "cmd/cgo/internal/" + subdir,
1069                         buildmode: buildmode,
1070                 }
1071                 var ldflags []string
1072                 if linkmode != "auto" {
1073                         // "auto" is the default, so avoid cluttering the command line for "auto"
1074                         ldflags = append(ldflags, "-linkmode="+linkmode)
1075                 }
1076
1077                 if linkmode == "internal" {
1078                         gt.tags = append(gt.tags, "internal")
1079                         if buildmode == "pie" {
1080                                 gt.tags = append(gt.tags, "internal_pie")
1081                         }
1082                 }
1083                 if buildmode == "static" {
1084                         // This isn't actually a Go buildmode, just a convenient way to tell
1085                         // cgoTest we want static linking.
1086                         gt.buildmode = ""
1087                         if linkmode == "external" {
1088                                 ldflags = append(ldflags, `-extldflags "-static -pthread"`)
1089                         } else if linkmode == "auto" {
1090                                 gt.env = append(gt.env, "CGO_LDFLAGS=-static -pthread")
1091                         } else {
1092                                 panic("unknown linkmode with static build: " + linkmode)
1093                         }
1094                         gt.tags = append(gt.tags, "static")
1095                 }
1096                 gt.ldflags = strings.Join(ldflags, " ")
1097
1098                 t.registerTest(heading, gt, opts...)
1099                 return gt
1100         }
1101
1102         // test, testtls, and testnocgo are run with linkmode="auto", buildmode=""
1103         // as part of go test cmd. Here we only have to register the non-default
1104         // build modes of these tests.
1105
1106         // Stub out various buildmode=pie tests  on alpine until 54354 resolved.
1107         builderName := os.Getenv("GO_BUILDER_NAME")
1108         disablePIE := strings.HasSuffix(builderName, "-alpine")
1109
1110         if t.internalLink() {
1111                 cgoTest("internal", "test", "internal", "")
1112         }
1113
1114         os := gohostos
1115         p := gohostos + "/" + goarch
1116         switch {
1117         case os == "darwin", os == "windows":
1118                 if !t.extLink() {
1119                         break
1120                 }
1121                 // test linkmode=external, but __thread not supported, so skip testtls.
1122                 cgoTest("external", "test", "external", "")
1123
1124                 gt := cgoTest("external-s", "test", "external", "")
1125                 gt.ldflags += " -s"
1126
1127                 if t.supportedBuildmode("pie") && !disablePIE {
1128                         cgoTest("auto-pie", "test", "auto", "pie")
1129                         if t.internalLink() && t.internalLinkPIE() {
1130                                 cgoTest("internal-pie", "test", "internal", "pie")
1131                         }
1132                 }
1133
1134         case os == "aix", os == "android", os == "dragonfly", os == "freebsd", os == "linux", os == "netbsd", os == "openbsd":
1135                 gt := cgoTest("external-g0", "test", "external", "")
1136                 gt.env = append(gt.env, "CGO_CFLAGS=-g0 -fdiagnostics-color")
1137
1138                 cgoTest("external", "testtls", "external", "")
1139                 switch {
1140                 case os == "aix":
1141                         // no static linking
1142                 case p == "freebsd/arm":
1143                         // -fPIC compiled tls code will use __tls_get_addr instead
1144                         // of __aeabi_read_tp, however, on FreeBSD/ARM, __tls_get_addr
1145                         // is implemented in rtld-elf, so -fPIC isn't compatible with
1146                         // static linking on FreeBSD/ARM with clang. (cgo depends on
1147                         // -fPIC fundamentally.)
1148                 default:
1149                         // Check for static linking support
1150                         var staticCheck rtSkipFunc
1151                         ccName := compilerEnvLookup("CC", defaultcc, goos, goarch)
1152                         cc, err := exec.LookPath(ccName)
1153                         if err != nil {
1154                                 staticCheck.skip = func(*distTest) (string, bool) {
1155                                         return fmt.Sprintf("$CC (%q) not found, skip cgo static linking test.", ccName), true
1156                                 }
1157                         } else {
1158                                 cmd := t.dirCmd("src/cmd/cgo/internal/test", cc, "-xc", "-o", "/dev/null", "-static", "-")
1159                                 cmd.Stdin = strings.NewReader("int main() {}")
1160                                 cmd.Stdout, cmd.Stderr = nil, nil // Discard output
1161                                 if err := cmd.Run(); err != nil {
1162                                         // Skip these tests
1163                                         staticCheck.skip = func(*distTest) (string, bool) {
1164                                                 return "No support for static linking found (lacks libc.a?), skip cgo static linking test.", true
1165                                         }
1166                                 }
1167                         }
1168
1169                         // Doing a static link with boringcrypto gets
1170                         // a C linker warning on Linux.
1171                         // in function `bio_ip_and_port_to_socket_and_addr':
1172                         // warning: Using 'getaddrinfo' in statically linked applications requires at runtime the shared libraries from the glibc version used for linking
1173                         if staticCheck.skip == nil && goos == "linux" && strings.Contains(goexperiment, "boringcrypto") {
1174                                 staticCheck.skip = func(*distTest) (string, bool) {
1175                                         return "skipping static linking check on Linux when using boringcrypto to avoid C linker warning about getaddrinfo", true
1176                                 }
1177                         }
1178
1179                         // Static linking tests
1180                         if goos != "android" && p != "netbsd/arm" {
1181                                 // TODO(#56629): Why does this fail on netbsd-arm?
1182                                 cgoTest("static", "testtls", "external", "static", staticCheck)
1183                         }
1184                         cgoTest("external", "testnocgo", "external", "", staticCheck)
1185                         if goos != "android" {
1186                                 cgoTest("static", "testnocgo", "external", "static", staticCheck)
1187                                 cgoTest("static", "test", "external", "static", staticCheck)
1188                                 // -static in CGO_LDFLAGS triggers a different code path
1189                                 // than -static in -extldflags, so test both.
1190                                 // See issue #16651.
1191                                 if goarch != "loong64" {
1192                                         // TODO(#56623): Why does this fail on loong64?
1193                                         cgoTest("auto-static", "test", "auto", "static", staticCheck)
1194                                 }
1195                         }
1196
1197                         // PIE linking tests
1198                         if t.supportedBuildmode("pie") && !disablePIE {
1199                                 cgoTest("auto-pie", "test", "auto", "pie")
1200                                 if t.internalLink() && t.internalLinkPIE() {
1201                                         cgoTest("internal-pie", "test", "internal", "pie")
1202                                 }
1203                                 cgoTest("auto-pie", "testtls", "auto", "pie")
1204                                 cgoTest("auto-pie", "testnocgo", "auto", "pie")
1205                         }
1206                 }
1207         }
1208 }
1209
1210 // run pending test commands, in parallel, emitting headers as appropriate.
1211 // When finished, emit header for nextTest, which is going to run after the
1212 // pending commands are done (and runPending returns).
1213 // A test should call runPending if it wants to make sure that it is not
1214 // running in parallel with earlier tests, or if it has some other reason
1215 // for needing the earlier tests to be done.
1216 func (t *tester) runPending(nextTest *distTest) {
1217         worklist := t.worklist
1218         t.worklist = nil
1219         for _, w := range worklist {
1220                 w.start = make(chan bool)
1221                 w.end = make(chan bool)
1222                 // w.cmd must be set up to write to w.out. We can't check that, but we
1223                 // can check for easy mistakes.
1224                 if w.cmd.Stdout == nil || w.cmd.Stdout == os.Stdout || w.cmd.Stderr == nil || w.cmd.Stderr == os.Stderr {
1225                         panic("work.cmd.Stdout/Stderr must be redirected")
1226                 }
1227                 go func(w *work) {
1228                         if !<-w.start {
1229                                 timelog("skip", w.dt.name)
1230                                 w.out.WriteString("skipped due to earlier error\n")
1231                         } else {
1232                                 timelog("start", w.dt.name)
1233                                 w.err = w.cmd.Run()
1234                                 if w.flush != nil {
1235                                         w.flush()
1236                                 }
1237                                 if w.err != nil {
1238                                         if isUnsupportedVMASize(w) {
1239                                                 timelog("skip", w.dt.name)
1240                                                 w.out.Reset()
1241                                                 w.out.WriteString("skipped due to unsupported VMA\n")
1242                                                 w.err = nil
1243                                         }
1244                                 }
1245                         }
1246                         timelog("end", w.dt.name)
1247                         w.end <- true
1248                 }(w)
1249         }
1250
1251         started := 0
1252         ended := 0
1253         var last *distTest
1254         for ended < len(worklist) {
1255                 for started < len(worklist) && started-ended < maxbg {
1256                         w := worklist[started]
1257                         started++
1258                         w.start <- !t.failed || t.keepGoing
1259                 }
1260                 w := worklist[ended]
1261                 dt := w.dt
1262                 if t.lastHeading != dt.heading {
1263                         t.lastHeading = dt.heading
1264                         t.out(dt.heading)
1265                 }
1266                 if dt != last {
1267                         // Assumes all the entries for a single dt are in one worklist.
1268                         last = w.dt
1269                         if vflag > 0 {
1270                                 fmt.Printf("# go tool dist test -run=^%s$\n", dt.name)
1271                         }
1272                 }
1273                 if vflag > 1 {
1274                         errprintf("%s\n", strings.Join(w.cmd.Args, " "))
1275                 }
1276                 ended++
1277                 <-w.end
1278                 os.Stdout.Write(w.out.Bytes())
1279                 // We no longer need the output, so drop the buffer.
1280                 w.out = bytes.Buffer{}
1281                 if w.err != nil {
1282                         log.Printf("Failed: %v", w.err)
1283                         t.failed = true
1284                 }
1285         }
1286         if t.failed && !t.keepGoing {
1287                 fatalf("FAILED")
1288         }
1289
1290         if dt := nextTest; dt != nil {
1291                 if t.lastHeading != dt.heading {
1292                         t.lastHeading = dt.heading
1293                         t.out(dt.heading)
1294                 }
1295                 if vflag > 0 {
1296                         fmt.Printf("# go tool dist test -run=^%s$\n", dt.name)
1297                 }
1298         }
1299 }
1300
1301 func (t *tester) hasBash() bool {
1302         switch gohostos {
1303         case "windows", "plan9":
1304                 return false
1305         }
1306         return true
1307 }
1308
1309 // hasParallelism is a copy of the function
1310 // internal/testenv.HasParallelism, which can't be used here
1311 // because cmd/dist can not import internal packages during bootstrap.
1312 func (t *tester) hasParallelism() bool {
1313         switch goos {
1314         case "js", "wasip1":
1315                 return false
1316         }
1317         return true
1318 }
1319
1320 func (t *tester) raceDetectorSupported() bool {
1321         if gohostos != goos {
1322                 return false
1323         }
1324         if !t.cgoEnabled {
1325                 return false
1326         }
1327         if !raceDetectorSupported(goos, goarch) {
1328                 return false
1329         }
1330         // The race detector doesn't work on Alpine Linux:
1331         // golang.org/issue/14481
1332         if isAlpineLinux() {
1333                 return false
1334         }
1335         // NetBSD support is unfinished.
1336         // golang.org/issue/26403
1337         if goos == "netbsd" {
1338                 return false
1339         }
1340         return true
1341 }
1342
1343 func isAlpineLinux() bool {
1344         if runtime.GOOS != "linux" {
1345                 return false
1346         }
1347         fi, err := os.Lstat("/etc/alpine-release")
1348         return err == nil && fi.Mode().IsRegular()
1349 }
1350
1351 func (t *tester) registerRaceTests() {
1352         hdr := "Testing race detector"
1353         t.registerTest(hdr,
1354                 &goTest{
1355                         variant:  "race",
1356                         race:     true,
1357                         runTests: "Output",
1358                         pkg:      "runtime/race",
1359                 })
1360         t.registerTest(hdr,
1361                 &goTest{
1362                         variant:  "race",
1363                         race:     true,
1364                         runTests: "TestParse|TestEcho|TestStdinCloseRace|TestClosedPipeRace|TestTypeRace|TestFdRace|TestFdReadRace|TestFileCloseRace",
1365                         pkgs:     []string{"flag", "net", "os", "os/exec", "encoding/gob"},
1366                 })
1367         // We don't want the following line, because it
1368         // slows down all.bash (by 10 seconds on my laptop).
1369         // The race builder should catch any error here, but doesn't.
1370         // TODO(iant): Figure out how to catch this.
1371         // t.registerTest(hdr, &goTest{variant: "race", race: true, runTests: "TestParallelTest", pkg: "cmd/go"})
1372         if t.cgoEnabled {
1373                 // Building cmd/cgo/internal/test takes a long time.
1374                 // There are already cgo-enabled packages being tested with the race detector.
1375                 // We shouldn't need to redo all of cmd/cgo/internal/test too.
1376                 // The race buildler will take care of this.
1377                 // t.registerTest(hdr, &goTest{variant: "race", race: true, env: []string{"GOTRACEBACK=2"}, pkg: "cmd/cgo/internal/test"})
1378         }
1379         if t.extLink() {
1380                 // Test with external linking; see issue 9133.
1381                 t.registerTest(hdr,
1382                         &goTest{
1383                                 variant:  "race-external",
1384                                 race:     true,
1385                                 ldflags:  "-linkmode=external",
1386                                 runTests: "TestParse|TestEcho|TestStdinCloseRace",
1387                                 pkgs:     []string{"flag", "os/exec"},
1388                         })
1389         }
1390 }
1391
1392 // cgoPackages is the standard packages that use cgo.
1393 var cgoPackages = []string{
1394         "net",
1395         "os/user",
1396 }
1397
1398 var funcBenchmark = []byte("\nfunc Benchmark")
1399
1400 // packageHasBenchmarks reports whether pkg has benchmarks.
1401 // On any error, it conservatively returns true.
1402 //
1403 // This exists just to eliminate work on the builders, since compiling
1404 // a test in race mode just to discover it has no benchmarks costs a
1405 // second or two per package, and this function returns false for
1406 // about 100 packages.
1407 func (t *tester) packageHasBenchmarks(pkg string) bool {
1408         pkgDir := filepath.Join(goroot, "src", pkg)
1409         d, err := os.Open(pkgDir)
1410         if err != nil {
1411                 return true // conservatively
1412         }
1413         defer d.Close()
1414         names, err := d.Readdirnames(-1)
1415         if err != nil {
1416                 return true // conservatively
1417         }
1418         for _, name := range names {
1419                 if !strings.HasSuffix(name, "_test.go") {
1420                         continue
1421                 }
1422                 slurp, err := os.ReadFile(filepath.Join(pkgDir, name))
1423                 if err != nil {
1424                         return true // conservatively
1425                 }
1426                 if bytes.Contains(slurp, funcBenchmark) {
1427                         return true
1428                 }
1429         }
1430         return false
1431 }
1432
1433 // makeGOROOTUnwritable makes all $GOROOT files & directories non-writable to
1434 // check that no tests accidentally write to $GOROOT.
1435 func (t *tester) makeGOROOTUnwritable() (undo func()) {
1436         dir := os.Getenv("GOROOT")
1437         if dir == "" {
1438                 panic("GOROOT not set")
1439         }
1440
1441         type pathMode struct {
1442                 path string
1443                 mode os.FileMode
1444         }
1445         var dirs []pathMode // in lexical order
1446
1447         undo = func() {
1448                 for i := range dirs {
1449                         os.Chmod(dirs[i].path, dirs[i].mode) // best effort
1450                 }
1451         }
1452
1453         filepath.WalkDir(dir, func(path string, d fs.DirEntry, err error) error {
1454                 if suffix := strings.TrimPrefix(path, dir+string(filepath.Separator)); suffix != "" {
1455                         if suffix == ".git" {
1456                                 // Leave Git metadata in whatever state it was in. It may contain a lot
1457                                 // of files, and it is highly unlikely that a test will try to modify
1458                                 // anything within that directory.
1459                                 return filepath.SkipDir
1460                         }
1461                 }
1462                 if err != nil {
1463                         return nil
1464                 }
1465
1466                 info, err := d.Info()
1467                 if err != nil {
1468                         return nil
1469                 }
1470
1471                 mode := info.Mode()
1472                 if mode&0222 != 0 && (mode.IsDir() || mode.IsRegular()) {
1473                         dirs = append(dirs, pathMode{path, mode})
1474                 }
1475                 return nil
1476         })
1477
1478         // Run over list backward to chmod children before parents.
1479         for i := len(dirs) - 1; i >= 0; i-- {
1480                 err := os.Chmod(dirs[i].path, dirs[i].mode&^0222)
1481                 if err != nil {
1482                         dirs = dirs[i:] // Only undo what we did so far.
1483                         undo()
1484                         fatalf("failed to make GOROOT read-only: %v", err)
1485                 }
1486         }
1487
1488         return undo
1489 }
1490
1491 // raceDetectorSupported is a copy of the function
1492 // internal/platform.RaceDetectorSupported, which can't be used here
1493 // because cmd/dist can not import internal packages during bootstrap.
1494 // The race detector only supports 48-bit VMA on arm64. But we don't have
1495 // a good solution to check VMA size(See https://golang.org/issue/29948)
1496 // raceDetectorSupported will always return true for arm64. But race
1497 // detector tests may abort on non 48-bit VMA configuration, the tests
1498 // will be marked as "skipped" in this case.
1499 func raceDetectorSupported(goos, goarch string) bool {
1500         switch goos {
1501         case "linux":
1502                 return goarch == "amd64" || goarch == "ppc64le" || goarch == "arm64" || goarch == "s390x"
1503         case "darwin":
1504                 return goarch == "amd64" || goarch == "arm64"
1505         case "freebsd", "netbsd", "openbsd", "windows":
1506                 return goarch == "amd64"
1507         default:
1508                 return false
1509         }
1510 }
1511
1512 // buildModeSupports is a copy of the function
1513 // internal/platform.BuildModeSupported, which can't be used here
1514 // because cmd/dist can not import internal packages during bootstrap.
1515 func buildModeSupported(compiler, buildmode, goos, goarch string) bool {
1516         if compiler == "gccgo" {
1517                 return true
1518         }
1519
1520         platform := goos + "/" + goarch
1521
1522         switch buildmode {
1523         case "archive":
1524                 return true
1525
1526         case "c-archive":
1527                 switch goos {
1528                 case "aix", "darwin", "ios", "windows":
1529                         return true
1530                 case "linux":
1531                         switch goarch {
1532                         case "386", "amd64", "arm", "armbe", "arm64", "arm64be", "loong64", "ppc64le", "riscv64", "s390x":
1533                                 // linux/ppc64 not supported because it does
1534                                 // not support external linking mode yet.
1535                                 return true
1536                         default:
1537                                 // Other targets do not support -shared,
1538                                 // per ParseFlags in
1539                                 // cmd/compile/internal/base/flag.go.
1540                                 // For c-archive the Go tool passes -shared,
1541                                 // so that the result is suitable for inclusion
1542                                 // in a PIE or shared library.
1543                                 return false
1544                         }
1545                 case "freebsd":
1546                         return goarch == "amd64"
1547                 }
1548                 return false
1549
1550         case "c-shared":
1551                 switch platform {
1552                 case "linux/amd64", "linux/arm", "linux/arm64", "linux/loong64", "linux/386", "linux/ppc64le", "linux/riscv64", "linux/s390x",
1553                         "android/amd64", "android/arm", "android/arm64", "android/386",
1554                         "freebsd/amd64",
1555                         "darwin/amd64", "darwin/arm64",
1556                         "windows/amd64", "windows/386", "windows/arm64":
1557                         return true
1558                 }
1559                 return false
1560
1561         case "default":
1562                 return true
1563
1564         case "exe":
1565                 return true
1566
1567         case "pie":
1568                 switch platform {
1569                 case "linux/386", "linux/amd64", "linux/arm", "linux/arm64", "linux/loong64", "linux/ppc64le", "linux/riscv64", "linux/s390x",
1570                         "android/amd64", "android/arm", "android/arm64", "android/386",
1571                         "freebsd/amd64",
1572                         "darwin/amd64", "darwin/arm64",
1573                         "ios/amd64", "ios/arm64",
1574                         "aix/ppc64",
1575                         "windows/386", "windows/amd64", "windows/arm", "windows/arm64":
1576                         return true
1577                 }
1578                 return false
1579
1580         case "shared":
1581                 switch platform {
1582                 case "linux/386", "linux/amd64", "linux/arm", "linux/arm64", "linux/ppc64le", "linux/s390x":
1583                         return true
1584                 }
1585                 return false
1586
1587         case "plugin":
1588                 switch platform {
1589                 case "linux/amd64", "linux/arm", "linux/arm64", "linux/386", "linux/s390x", "linux/ppc64le",
1590                         "android/amd64", "android/386",
1591                         "darwin/amd64", "darwin/arm64",
1592                         "freebsd/amd64":
1593                         return true
1594                 }
1595                 return false
1596
1597         default:
1598                 return false
1599         }
1600 }
1601
1602 // isUnsupportedVMASize reports whether the failure is caused by an unsupported
1603 // VMA for the race detector (for example, running the race detector on an
1604 // arm64 machine configured with 39-bit VMA)
1605 func isUnsupportedVMASize(w *work) bool {
1606         unsupportedVMA := []byte("unsupported VMA range")
1607         return w.dt.name == "race" && bytes.Contains(w.out.Bytes(), unsupportedVMA)
1608 }
1609
1610 // isEnvSet reports whether the environment variable evar is
1611 // set in the environment.
1612 func isEnvSet(evar string) bool {
1613         evarEq := evar + "="
1614         for _, e := range os.Environ() {
1615                 if strings.HasPrefix(e, evarEq) {
1616                         return true
1617                 }
1618         }
1619         return false
1620 }