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

View as plain text