Source file src/cmd/internal/testdir/testdir_test.go

     1  // Copyright 2012 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 testdir_test runs tests in the GOROOT/test directory.
     6  package testdir_test
     7  
     8  import (
     9  	"bytes"
    10  	"encoding/json"
    11  	"errors"
    12  	"flag"
    13  	"fmt"
    14  	"go/build"
    15  	"go/build/constraint"
    16  	"hash/fnv"
    17  	"internal/testenv"
    18  	"io"
    19  	"io/fs"
    20  	"log"
    21  	"os"
    22  	"os/exec"
    23  	"path"
    24  	"path/filepath"
    25  	"regexp"
    26  	"runtime"
    27  	"slices"
    28  	"sort"
    29  	"strconv"
    30  	"strings"
    31  	"sync"
    32  	"testing"
    33  	"time"
    34  	"unicode"
    35  )
    36  
    37  var (
    38  	allCodegen     = flag.Bool("all_codegen", defaultAllCodeGen(), "run all goos/goarch for codegen")
    39  	runSkips       = flag.Bool("run_skips", false, "run skipped tests (ignore skip and build tags)")
    40  	linkshared     = flag.Bool("linkshared", false, "")
    41  	updateErrors   = flag.Bool("update_errors", false, "update error messages in test file based on compiler output")
    42  	runoutputLimit = flag.Int("l", defaultRunOutputLimit(), "number of parallel runoutput tests to run")
    43  	force          = flag.Bool("f", false, "ignore expected-failure test lists")
    44  	target         = flag.String("target", "", "cross-compile tests for `goos/goarch`")
    45  
    46  	shard  = flag.Int("shard", 0, "shard index to run. Only applicable if -shards is non-zero.")
    47  	shards = flag.Int("shards", 0, "number of shards. If 0, all tests are run. This is used by the continuous build.")
    48  )
    49  
    50  // defaultAllCodeGen returns the default value of the -all_codegen
    51  // flag. By default, we prefer to be fast (returning false), except on
    52  // the linux-amd64 builder that's already very fast, so we get more
    53  // test coverage on trybots. See https://go.dev/issue/34297.
    54  func defaultAllCodeGen() bool {
    55  	return testenv.Builder() == "gotip-linux-amd64"
    56  }
    57  
    58  var (
    59  	// Package-scoped variables that are initialized at the start of Test.
    60  	goTool       string
    61  	goos         string // Target GOOS
    62  	goarch       string // Target GOARCH
    63  	cgoEnabled   bool
    64  	goExperiment string
    65  	goDebug      string
    66  	tmpDir       string
    67  
    68  	// dirs are the directories to look for *.go files in.
    69  	// TODO(bradfitz): just use all directories?
    70  	dirs = []string{".", "ken", "chan", "interface", "internal/runtime/sys", "syntax", "dwarf", "fixedbugs", "codegen", "abi", "typeparam", "typeparam/mdempsky", "arenas", "simd"}
    71  )
    72  
    73  // Test is the main entrypoint that runs tests in the GOROOT/test directory.
    74  //
    75  // Each .go file test case in GOROOT/test is registered as a subtest with
    76  // a full name like "Test/fixedbugs/bug000.go" ('/'-separated relative path).
    77  func Test(t *testing.T) {
    78  	if *target != "" {
    79  		// When -target is set, propagate it to GOOS/GOARCH in our environment
    80  		// so that all commands run with the target GOOS/GOARCH.
    81  		//
    82  		// We do this before even calling "go env", because GOOS/GOARCH can
    83  		// affect other settings we get from go env (notably CGO_ENABLED).
    84  		goos, goarch, ok := strings.Cut(*target, "/")
    85  		if !ok {
    86  			t.Fatalf("bad -target flag %q, expected goos/goarch", *target)
    87  		}
    88  		t.Setenv("GOOS", goos)
    89  		t.Setenv("GOARCH", goarch)
    90  	}
    91  
    92  	goTool = testenv.GoToolPath(t)
    93  	cmd := exec.Command(goTool, "env", "-json")
    94  	stdout, err := cmd.StdoutPipe()
    95  	if err != nil {
    96  		t.Fatal("StdoutPipe:", err)
    97  	}
    98  	if err := cmd.Start(); err != nil {
    99  		t.Fatal("Start:", err)
   100  	}
   101  	var env struct {
   102  		GOOS         string
   103  		GOARCH       string
   104  		GOEXPERIMENT string
   105  		GODEBUG      string
   106  		CGO_ENABLED  string
   107  	}
   108  	if err := json.NewDecoder(stdout).Decode(&env); err != nil {
   109  		t.Fatal("Decode:", err)
   110  	}
   111  	if err := cmd.Wait(); err != nil {
   112  		t.Fatal("Wait:", err)
   113  	}
   114  	goos = env.GOOS
   115  	goarch = env.GOARCH
   116  	cgoEnabled, _ = strconv.ParseBool(env.CGO_ENABLED)
   117  	goExperiment = env.GOEXPERIMENT
   118  	goDebug = env.GODEBUG
   119  	tmpDir = t.TempDir()
   120  
   121  	common := testCommon{
   122  		gorootTestDir: filepath.Join(testenv.GOROOT(t), "test"),
   123  		runoutputGate: make(chan bool, *runoutputLimit),
   124  	}
   125  
   126  	// cmd/distpack deletes GOROOT/test, so skip the test if it isn't present.
   127  	// cmd/distpack also requires GOROOT/VERSION to exist, so use that to
   128  	// suppress false-positive skips.
   129  	if _, err := os.Stat(common.gorootTestDir); os.IsNotExist(err) {
   130  		if _, err := os.Stat(filepath.Join(testenv.GOROOT(t), "VERSION")); err == nil {
   131  			t.Skipf("skipping: GOROOT/test not present")
   132  		}
   133  	}
   134  
   135  	for _, dir := range dirs {
   136  		for _, goFile := range goFiles(t, dir) {
   137  			test := test{testCommon: common, dir: dir, goFile: goFile}
   138  			t.Run(path.Join(dir, goFile), func(t *testing.T) {
   139  				t.Parallel()
   140  				test.T = t
   141  				testError := test.run()
   142  				wantError := test.expectFail() && !*force
   143  				if testError != nil {
   144  					if wantError {
   145  						t.Log(testError.Error() + " (expected)")
   146  					} else {
   147  						t.Fatal(testError)
   148  					}
   149  				} else if wantError {
   150  					t.Fatal("unexpected success")
   151  				}
   152  			})
   153  		}
   154  	}
   155  }
   156  
   157  func shardMatch(name string) bool {
   158  	if *shards <= 1 {
   159  		return true
   160  	}
   161  	h := fnv.New32()
   162  	io.WriteString(h, name)
   163  	return int(h.Sum32()%uint32(*shards)) == *shard
   164  }
   165  
   166  func goFiles(t *testing.T, dir string) []string {
   167  	files, err := os.ReadDir(filepath.Join(testenv.GOROOT(t), "test", dir))
   168  	if err != nil {
   169  		t.Fatal(err)
   170  	}
   171  	names := []string{}
   172  	for _, file := range files {
   173  		name := file.Name()
   174  		if !strings.HasPrefix(name, ".") && strings.HasSuffix(name, ".go") && shardMatch(name) {
   175  			names = append(names, name)
   176  		}
   177  	}
   178  	return names
   179  }
   180  
   181  type runCmd func(...string) ([]byte, error)
   182  
   183  func compileFile(runcmd runCmd, longname string, flags []string) (out []byte, err error) {
   184  	cmd := []string{goTool, "tool", "compile", "-e", "-p=p", "-importcfg=" + stdlibImportcfgFile()}
   185  	cmd = append(cmd, flags...)
   186  	if *linkshared {
   187  		cmd = append(cmd, "-dynlink", "-installsuffix=dynlink")
   188  	}
   189  	cmd = append(cmd, longname)
   190  	return runcmd(cmd...)
   191  }
   192  
   193  func compileInDir(runcmd runCmd, dir string, flags []string, importcfg string, pkgname string, names ...string) (out []byte, err error) {
   194  	if importcfg == "" {
   195  		importcfg = stdlibImportcfgFile()
   196  	}
   197  	cmd := []string{goTool, "tool", "compile", "-e", "-D", "test", "-importcfg=" + importcfg}
   198  	if pkgname == "main" {
   199  		cmd = append(cmd, "-p=main")
   200  	} else {
   201  		pkgname = path.Join("test", strings.TrimSuffix(names[0], ".go"))
   202  		cmd = append(cmd, "-o", pkgname+".a", "-p", pkgname)
   203  	}
   204  	cmd = append(cmd, flags...)
   205  	if *linkshared {
   206  		cmd = append(cmd, "-dynlink", "-installsuffix=dynlink")
   207  	}
   208  	for _, name := range names {
   209  		cmd = append(cmd, filepath.Join(dir, name))
   210  	}
   211  	return runcmd(cmd...)
   212  }
   213  
   214  var stdlibImportcfg = sync.OnceValue(func() string {
   215  	cmd := exec.Command(goTool, "list", "-export", "-f", "{{if .Export}}packagefile {{.ImportPath}}={{.Export}}{{end}}", "std")
   216  	cmd.Env = append(os.Environ(), "GOENV=off", "GOFLAGS=")
   217  	output, err := cmd.Output()
   218  	if err, ok := err.(*exec.ExitError); ok && len(err.Stderr) != 0 {
   219  		log.Fatalf("'go list' failed: %v: %s", err, err.Stderr)
   220  	}
   221  	if err != nil {
   222  		log.Fatalf("'go list' failed: %v", err)
   223  	}
   224  	return string(output)
   225  })
   226  
   227  var stdlibImportcfgFile = sync.OnceValue(func() string {
   228  	filename := filepath.Join(tmpDir, "importcfg")
   229  	err := os.WriteFile(filename, []byte(stdlibImportcfg()), 0644)
   230  	if err != nil {
   231  		log.Fatal(err)
   232  	}
   233  	return filename
   234  })
   235  
   236  // linkFile links infile with the given importcfg and ldflags, writes to outfile.
   237  // infile can be the name of an object file or a go source file.
   238  func linkFile(runcmd runCmd, outfile, infile string, importcfg string, ldflags []string) (err error) {
   239  	if importcfg == "" {
   240  		importcfg = stdlibImportcfgFile()
   241  	}
   242  	if strings.HasSuffix(infile, ".go") {
   243  		infile = infile[:len(infile)-3] + ".o"
   244  	}
   245  	cmd := []string{goTool, "tool", "link", "-s", "-w", "-buildid=test", "-o", outfile, "-importcfg=" + importcfg}
   246  	if *linkshared {
   247  		cmd = append(cmd, "-linkshared", "-installsuffix=dynlink")
   248  	}
   249  	if ldflags != nil {
   250  		cmd = append(cmd, ldflags...)
   251  	}
   252  	cmd = append(cmd, infile)
   253  	_, err = runcmd(cmd...)
   254  	return
   255  }
   256  
   257  type testCommon struct {
   258  	// gorootTestDir is the GOROOT/test directory path.
   259  	gorootTestDir string
   260  
   261  	// runoutputGate controls the max number of runoutput tests
   262  	// executed in parallel as they can each consume a lot of memory.
   263  	runoutputGate chan bool
   264  }
   265  
   266  // test is a single test case in the GOROOT/test directory.
   267  type test struct {
   268  	testCommon
   269  	*testing.T
   270  	// dir and goFile identify the test case.
   271  	// For example, "fixedbugs", "bug000.go".
   272  	dir, goFile string
   273  }
   274  
   275  // expectFail reports whether the (overall) test recipe is
   276  // expected to fail under the current build+test configuration.
   277  func (t test) expectFail() bool {
   278  	failureSets := []map[string]bool{types2Failures}
   279  
   280  	// Note: gccgo supports more 32-bit architectures than this, but
   281  	// hopefully the 32-bit failures are fixed before this matters.
   282  	switch goarch {
   283  	case "386", "arm", "mips", "mipsle":
   284  		failureSets = append(failureSets, types2Failures32Bit)
   285  	}
   286  
   287  	testName := path.Join(t.dir, t.goFile) // Test name is '/'-separated.
   288  
   289  	for _, set := range failureSets {
   290  		if set[testName] {
   291  			return true
   292  		}
   293  	}
   294  	return false
   295  }
   296  
   297  func (t test) goFileName() string {
   298  	return filepath.Join(t.dir, t.goFile)
   299  }
   300  
   301  func (t test) goDirName() string {
   302  	return filepath.Join(t.dir, strings.ReplaceAll(t.goFile, ".go", ".dir"))
   303  }
   304  
   305  // goDirFiles returns .go files in dir.
   306  func goDirFiles(dir string) (filter []fs.DirEntry, _ error) {
   307  	files, err := os.ReadDir(dir)
   308  	if err != nil {
   309  		return nil, err
   310  	}
   311  	for _, goFile := range files {
   312  		if filepath.Ext(goFile.Name()) == ".go" {
   313  			filter = append(filter, goFile)
   314  		}
   315  	}
   316  	return filter, nil
   317  }
   318  
   319  var packageRE = regexp.MustCompile(`(?m)^package ([\p{Lu}\p{Ll}\w]+)`)
   320  
   321  func getPackageNameFromSource(fn string) (string, error) {
   322  	data, err := os.ReadFile(fn)
   323  	if err != nil {
   324  		return "", err
   325  	}
   326  	pkgname := packageRE.FindStringSubmatch(string(data))
   327  	if pkgname == nil {
   328  		return "", fmt.Errorf("cannot find package name in %s", fn)
   329  	}
   330  	return pkgname[1], nil
   331  }
   332  
   333  // goDirPkg represents a Go package in some directory.
   334  type goDirPkg struct {
   335  	name  string
   336  	files []string
   337  }
   338  
   339  // goDirPackages returns distinct Go packages in dir.
   340  // If singlefilepkgs is set, each file is considered a separate package
   341  // even if the package names are the same.
   342  func goDirPackages(t *testing.T, dir string, singlefilepkgs bool) []*goDirPkg {
   343  	files, err := goDirFiles(dir)
   344  	if err != nil {
   345  		t.Fatal(err)
   346  	}
   347  	var pkgs []*goDirPkg
   348  	m := make(map[string]*goDirPkg)
   349  	for _, file := range files {
   350  		name := file.Name()
   351  		pkgname, err := getPackageNameFromSource(filepath.Join(dir, name))
   352  		if err != nil {
   353  			t.Fatal(err)
   354  		}
   355  		p, ok := m[pkgname]
   356  		if singlefilepkgs || !ok {
   357  			p = &goDirPkg{name: pkgname}
   358  			pkgs = append(pkgs, p)
   359  			m[pkgname] = p
   360  		}
   361  		p.files = append(p.files, name)
   362  	}
   363  	return pkgs
   364  }
   365  
   366  type context struct {
   367  	GOOS       string
   368  	GOARCH     string
   369  	cgoEnabled bool
   370  	noOptEnv   bool
   371  }
   372  
   373  // shouldTest looks for build tags in a source file and returns
   374  // whether the file should be used according to the tags.
   375  func shouldTest(src string, goos, goarch string) (ok bool, whyNot string) {
   376  	if *runSkips {
   377  		return true, ""
   378  	}
   379  	for _, line := range strings.Split(src, "\n") {
   380  		if strings.HasPrefix(line, "package ") {
   381  			break
   382  		}
   383  
   384  		if expr, err := constraint.Parse(line); err == nil {
   385  			gcFlags := os.Getenv("GO_GCFLAGS")
   386  			ctxt := &context{
   387  				GOOS:       goos,
   388  				GOARCH:     goarch,
   389  				cgoEnabled: cgoEnabled,
   390  				noOptEnv:   strings.Contains(gcFlags, "-N") || strings.Contains(gcFlags, "-l"),
   391  			}
   392  
   393  			if !expr.Eval(ctxt.match) {
   394  				return false, line
   395  			}
   396  		}
   397  	}
   398  	return true, ""
   399  }
   400  
   401  func (ctxt *context) match(name string) bool {
   402  	if name == "" {
   403  		return false
   404  	}
   405  
   406  	// Tags must be letters, digits, underscores or dots.
   407  	// Unlike in Go identifiers, all digits are fine (e.g., "386").
   408  	for _, c := range name {
   409  		if !unicode.IsLetter(c) && !unicode.IsDigit(c) && c != '_' && c != '.' {
   410  			return false
   411  		}
   412  	}
   413  
   414  	if slices.Contains(build.Default.ReleaseTags, name) {
   415  		return true
   416  	}
   417  
   418  	if strings.HasPrefix(name, "goexperiment.") {
   419  		return slices.Contains(build.Default.ToolTags, name)
   420  	}
   421  
   422  	if name == "cgo" && ctxt.cgoEnabled {
   423  		return true
   424  	}
   425  
   426  	if name == ctxt.GOOS || name == ctxt.GOARCH || name == "gc" {
   427  		return true
   428  	}
   429  
   430  	if ctxt.noOptEnv && name == "gcflags_noopt" {
   431  		return true
   432  	}
   433  
   434  	if name == "test_run" {
   435  		return true
   436  	}
   437  
   438  	return false
   439  }
   440  
   441  // goGcflags returns the -gcflags argument to use with go build / go run.
   442  // This must match the flags used for building the standard library,
   443  // or else the commands will rebuild any needed packages (like runtime)
   444  // over and over.
   445  func (test) goGcflags() string {
   446  	return "-gcflags=all=" + os.Getenv("GO_GCFLAGS")
   447  }
   448  
   449  func (test) goGcflagsIsEmpty() bool {
   450  	return "" == os.Getenv("GO_GCFLAGS")
   451  }
   452  
   453  var errTimeout = errors.New("command exceeded time limit")
   454  
   455  // run runs the test case.
   456  //
   457  // When there is a problem, run uses t.Fatal to signify that it's an unskippable
   458  // infrastructure error (such as failing to read an input file or the test recipe
   459  // being malformed), or it returns a non-nil error to signify a test case error.
   460  //
   461  // t.Error isn't used here to give the caller the opportunity to decide whether
   462  // the test case failing is expected before promoting it to a real test failure.
   463  // See expectFail and -f flag.
   464  func (t test) run() error {
   465  	srcBytes, err := os.ReadFile(filepath.Join(t.gorootTestDir, t.goFileName()))
   466  	if err != nil {
   467  		t.Fatal("reading test case .go file:", err)
   468  	} else if bytes.HasPrefix(srcBytes, []byte{'\n'}) {
   469  		t.Fatal(".go file source starts with a newline")
   470  	}
   471  	src := string(srcBytes)
   472  
   473  	// Execution recipe is contained in a comment in
   474  	// the first non-empty line that is not a build constraint.
   475  	var action string
   476  	for actionSrc := src; action == "" && actionSrc != ""; {
   477  		var line string
   478  		line, actionSrc, _ = strings.Cut(actionSrc, "\n")
   479  		if constraint.IsGoBuild(line) || constraint.IsPlusBuild(line) {
   480  			continue
   481  		}
   482  		action = strings.TrimSpace(strings.TrimPrefix(line, "//"))
   483  	}
   484  	if action == "" {
   485  		t.Fatalf("execution recipe not found in GOROOT/test/%s", t.goFileName())
   486  	}
   487  
   488  	// Check for build constraints only up to the actual code.
   489  	header, _, ok := strings.Cut(src, "\npackage")
   490  	if !ok {
   491  		header = action // some files are intentionally malformed
   492  	}
   493  	if ok, why := shouldTest(header, goos, goarch); !ok {
   494  		t.Skip(why)
   495  	}
   496  
   497  	var args, flags, runenv []string
   498  	var tim int
   499  	wantError := false
   500  	wantAuto := false
   501  	singlefilepkgs := false
   502  	f, err := splitQuoted(action)
   503  	if err != nil {
   504  		t.Fatal("invalid test recipe:", err)
   505  	}
   506  	if len(f) > 0 {
   507  		action = f[0]
   508  		args = f[1:]
   509  	}
   510  
   511  	// TODO: Clean up/simplify this switch statement.
   512  	switch action {
   513  	case "compile", "compiledir", "build", "builddir", "buildrundir", "run", "buildrun", "runoutput", "rundir", "runindir", "asmcheck":
   514  		// nothing to do
   515  	case "errorcheckandrundir":
   516  		wantError = false // should be no error if also will run
   517  	case "errorcheckwithauto":
   518  		action = "errorcheck"
   519  		wantAuto = true
   520  		wantError = true
   521  	case "errorcheck", "errorcheckdir", "errorcheckoutput":
   522  		wantError = true
   523  	case "skip":
   524  		if *runSkips {
   525  			break
   526  		}
   527  		t.Skip("skip")
   528  	default:
   529  		t.Fatalf("unknown pattern: %q", action)
   530  	}
   531  
   532  	goexp := goExperiment
   533  	godebug := goDebug
   534  	gomodvers := ""
   535  
   536  	// collect flags
   537  	for len(args) > 0 && strings.HasPrefix(args[0], "-") {
   538  		switch args[0] {
   539  		case "-1":
   540  			wantError = true
   541  		case "-0":
   542  			wantError = false
   543  		case "-s":
   544  			singlefilepkgs = true
   545  		case "-t": // timeout in seconds
   546  			args = args[1:]
   547  			var err error
   548  			tim, err = strconv.Atoi(args[0])
   549  			if err != nil {
   550  				t.Fatalf("need number of seconds for -t timeout, got %s instead", args[0])
   551  			}
   552  			if s := os.Getenv("GO_TEST_TIMEOUT_SCALE"); s != "" {
   553  				timeoutScale, err := strconv.Atoi(s)
   554  				if err != nil {
   555  					t.Fatalf("failed to parse $GO_TEST_TIMEOUT_SCALE = %q as integer: %v", s, err)
   556  				}
   557  				tim *= timeoutScale
   558  			}
   559  		case "-goexperiment": // set GOEXPERIMENT environment
   560  			args = args[1:]
   561  			if goexp != "" {
   562  				goexp += ","
   563  			}
   564  			goexp += args[0]
   565  			runenv = append(runenv, "GOEXPERIMENT="+goexp)
   566  
   567  		case "-godebug": // set GODEBUG environment
   568  			args = args[1:]
   569  			if godebug != "" {
   570  				godebug += ","
   571  			}
   572  			godebug += args[0]
   573  			runenv = append(runenv, "GODEBUG="+godebug)
   574  
   575  		case "-gomodversion": // set the GoVersion in generated go.mod files (just runindir ATM)
   576  			args = args[1:]
   577  			gomodvers = args[0]
   578  
   579  		default:
   580  			flags = append(flags, args[0])
   581  		}
   582  		args = args[1:]
   583  	}
   584  	if action == "errorcheck" {
   585  		found := false
   586  		for i, f := range flags {
   587  			if strings.HasPrefix(f, "-d=") {
   588  				flags[i] = f + ",ssa/check/on"
   589  				found = true
   590  				break
   591  			}
   592  		}
   593  		if !found {
   594  			flags = append(flags, "-d=ssa/check/on")
   595  		}
   596  	}
   597  
   598  	tempDir := t.TempDir()
   599  	err = os.Mkdir(filepath.Join(tempDir, "test"), 0755)
   600  	if err != nil {
   601  		t.Fatal(err)
   602  	}
   603  
   604  	err = os.WriteFile(filepath.Join(tempDir, t.goFile), srcBytes, 0644)
   605  	if err != nil {
   606  		t.Fatal(err)
   607  	}
   608  
   609  	var (
   610  		runInDir        = tempDir
   611  		tempDirIsGOPATH = false
   612  	)
   613  	runcmd := func(args ...string) ([]byte, error) {
   614  		cmd := exec.Command(args[0], args[1:]...)
   615  		var buf bytes.Buffer
   616  		cmd.Stdout = &buf
   617  		cmd.Stderr = &buf
   618  		cmd.Env = append(os.Environ(), "GOENV=off", "GOFLAGS=")
   619  		if runInDir != "" {
   620  			cmd.Dir = runInDir
   621  			// Set PWD to match Dir to speed up os.Getwd in the child process.
   622  			cmd.Env = append(cmd.Env, "PWD="+cmd.Dir)
   623  		} else {
   624  			// Default to running in the GOROOT/test directory.
   625  			cmd.Dir = t.gorootTestDir
   626  			// Set PWD to match Dir to speed up os.Getwd in the child process.
   627  			cmd.Env = append(cmd.Env, "PWD="+cmd.Dir)
   628  		}
   629  		if tempDirIsGOPATH {
   630  			cmd.Env = append(cmd.Env, "GOPATH="+tempDir)
   631  		}
   632  		cmd.Env = append(cmd.Env, "STDLIB_IMPORTCFG="+stdlibImportcfgFile())
   633  		cmd.Env = append(cmd.Env, runenv...)
   634  
   635  		var err error
   636  
   637  		if tim != 0 {
   638  			err = cmd.Start()
   639  			// This command-timeout code adapted from cmd/go/test.go
   640  			// Note: the Go command uses a more sophisticated timeout
   641  			// strategy, first sending SIGQUIT (if appropriate for the
   642  			// OS in question) to try to trigger a stack trace, then
   643  			// finally much later SIGKILL. If timeouts prove to be a
   644  			// common problem here, it would be worth porting over
   645  			// that code as well. See https://do.dev/issue/50973
   646  			// for more discussion.
   647  			if err == nil {
   648  				tick := time.NewTimer(time.Duration(tim) * time.Second)
   649  				done := make(chan error)
   650  				go func() {
   651  					done <- cmd.Wait()
   652  				}()
   653  				select {
   654  				case err = <-done:
   655  					// ok
   656  				case <-tick.C:
   657  					cmd.Process.Signal(os.Interrupt)
   658  					time.Sleep(1 * time.Second)
   659  					cmd.Process.Kill()
   660  					<-done
   661  					err = errTimeout
   662  				}
   663  				tick.Stop()
   664  			}
   665  		} else {
   666  			err = cmd.Run()
   667  		}
   668  		if err != nil && err != errTimeout {
   669  			err = fmt.Errorf("%s\n%s", err, buf.Bytes())
   670  		}
   671  		return buf.Bytes(), err
   672  	}
   673  
   674  	importcfg := func(pkgs []*goDirPkg) string {
   675  		cfg := stdlibImportcfg()
   676  		for _, pkg := range pkgs {
   677  			pkgpath := path.Join("test", strings.TrimSuffix(pkg.files[0], ".go"))
   678  			cfg += "\npackagefile " + pkgpath + "=" + filepath.Join(tempDir, pkgpath+".a")
   679  		}
   680  		filename := filepath.Join(tempDir, "importcfg")
   681  		err := os.WriteFile(filename, []byte(cfg), 0644)
   682  		if err != nil {
   683  			t.Fatal(err)
   684  		}
   685  		return filename
   686  	}
   687  
   688  	long := filepath.Join(t.gorootTestDir, t.goFileName())
   689  	switch action {
   690  	default:
   691  		t.Fatalf("unimplemented action %q", action)
   692  		panic("unreachable")
   693  
   694  	case "asmcheck":
   695  		// Compile Go file and match the generated assembly
   696  		// against a set of regexps in comments.
   697  		ops := t.wantedAsmOpcodes(long)
   698  		self := runtime.GOOS + "/" + runtime.GOARCH
   699  		var lastErr error
   700  		for _, env := range ops.Envs() {
   701  			// Only run checks relevant to the current GOOS/GOARCH,
   702  			// to avoid triggering a cross-compile of the runtime.
   703  			if string(env) != self && !strings.HasPrefix(string(env), self+"/") && !*allCodegen {
   704  				continue
   705  			}
   706  			// -S=2 forces outermost line numbers when disassembling inlined code.
   707  			cmdline := []string{"build", "-gcflags", "-S=2"}
   708  
   709  			// Append flags, but don't override -gcflags=-S=2; add to it instead.
   710  			for i := 0; i < len(flags); i++ {
   711  				flag := flags[i]
   712  				switch {
   713  				case strings.HasPrefix(flag, "-gcflags="):
   714  					cmdline[2] += " " + strings.TrimPrefix(flag, "-gcflags=")
   715  				case strings.HasPrefix(flag, "--gcflags="):
   716  					cmdline[2] += " " + strings.TrimPrefix(flag, "--gcflags=")
   717  				case flag == "-gcflags", flag == "--gcflags":
   718  					i++
   719  					if i < len(flags) {
   720  						cmdline[2] += " " + flags[i]
   721  					}
   722  				default:
   723  					cmdline = append(cmdline, flag)
   724  				}
   725  			}
   726  
   727  			cmdline = append(cmdline, long)
   728  			cmd := exec.Command(goTool, cmdline...)
   729  			cmd.Env = append(os.Environ(), env.Environ()...)
   730  			if len(flags) > 0 && flags[0] == "-race" {
   731  				cmd.Env = append(cmd.Env, "CGO_ENABLED=1")
   732  			}
   733  
   734  			var buf bytes.Buffer
   735  			cmd.Stdout, cmd.Stderr = &buf, &buf
   736  			if err := cmd.Run(); err != nil {
   737  				lastErr = err
   738  				t.Log(env, "\n", cmd.Stderr)
   739  			}
   740  
   741  			err := t.asmCheck(buf.String(), long, env, ops[env])
   742  			if err != nil {
   743  				lastErr = err
   744  				t.Log(err)
   745  			}
   746  		}
   747  		// The error(s) have been logged earlier. Pass up a generic one.
   748  		if lastErr != nil {
   749  			return errors.New("One or more asmcheck tests failed. Check log for failure details.")
   750  		}
   751  		return nil
   752  
   753  	case "errorcheck":
   754  		// Compile Go file.
   755  		// Fail if wantError is true and compilation was successful and vice versa.
   756  		// Match errors produced by gc against errors in comments.
   757  		// TODO(gri) remove need for -C (disable printing of columns in error messages)
   758  		cmdline := []string{goTool, "tool", "compile", "-p=p", "-d=panic", "-C", "-e", "-importcfg=" + stdlibImportcfgFile(), "-o", "a.o"}
   759  		// No need to add -dynlink even if linkshared if we're just checking for errors...
   760  		cmdline = append(cmdline, flags...)
   761  		cmdline = append(cmdline, long)
   762  		out, err := runcmd(cmdline...)
   763  		if wantError {
   764  			if err == nil {
   765  				return fmt.Errorf("compilation succeeded unexpectedly\n%s", out)
   766  			}
   767  			if err == errTimeout {
   768  				return fmt.Errorf("compilation timed out")
   769  			}
   770  		} else {
   771  			if err != nil {
   772  				return err
   773  			}
   774  		}
   775  		if *updateErrors {
   776  			t.updateErrors(string(out), long)
   777  		}
   778  		return t.errorCheck(string(out), wantAuto, long, t.goFile)
   779  
   780  	case "compile":
   781  		// Compile Go file.
   782  		_, err := compileFile(runcmd, long, flags)
   783  		return err
   784  
   785  	case "compiledir":
   786  		// Compile all files in the directory as packages in lexicographic order.
   787  		longdir := filepath.Join(t.gorootTestDir, t.goDirName())
   788  		pkgs := goDirPackages(t.T, longdir, singlefilepkgs)
   789  		importcfgfile := importcfg(pkgs)
   790  
   791  		for _, pkg := range pkgs {
   792  			_, err := compileInDir(runcmd, longdir, flags, importcfgfile, pkg.name, pkg.files...)
   793  			if err != nil {
   794  				return err
   795  			}
   796  		}
   797  		return nil
   798  
   799  	case "errorcheckdir", "errorcheckandrundir":
   800  		flags = append(flags, "-d=panic")
   801  		// Compile and errorCheck all files in the directory as packages in lexicographic order.
   802  		// If errorcheckdir and wantError, compilation of the last package must fail.
   803  		// If errorcheckandrundir and wantError, compilation of the package prior the last must fail.
   804  		longdir := filepath.Join(t.gorootTestDir, t.goDirName())
   805  		pkgs := goDirPackages(t.T, longdir, singlefilepkgs)
   806  		errPkg := len(pkgs) - 1
   807  		if wantError && action == "errorcheckandrundir" {
   808  			// The last pkg should compiled successfully and will be run in next case.
   809  			// Preceding pkg must return an error from compileInDir.
   810  			errPkg--
   811  		}
   812  		importcfgfile := importcfg(pkgs)
   813  		for i, pkg := range pkgs {
   814  			out, err := compileInDir(runcmd, longdir, flags, importcfgfile, pkg.name, pkg.files...)
   815  			if i == errPkg {
   816  				if wantError && err == nil {
   817  					return fmt.Errorf("compilation succeeded unexpectedly\n%s", out)
   818  				} else if !wantError && err != nil {
   819  					return err
   820  				}
   821  			} else if err != nil {
   822  				return err
   823  			}
   824  			var fullshort []string
   825  			for _, name := range pkg.files {
   826  				fullshort = append(fullshort, filepath.Join(longdir, name), name)
   827  			}
   828  			err = t.errorCheck(string(out), wantAuto, fullshort...)
   829  			if err != nil {
   830  				return err
   831  			}
   832  		}
   833  		if action == "errorcheckdir" {
   834  			return nil
   835  		}
   836  		fallthrough
   837  
   838  	case "rundir":
   839  		// Compile all files in the directory as packages in lexicographic order.
   840  		// In case of errorcheckandrundir, ignore failed compilation of the package before the last.
   841  		// Link as if the last file is the main package, run it.
   842  		// Verify the expected output.
   843  		longdir := filepath.Join(t.gorootTestDir, t.goDirName())
   844  		pkgs := goDirPackages(t.T, longdir, singlefilepkgs)
   845  		// Split flags into gcflags and ldflags
   846  		ldflags := []string{}
   847  		for i, fl := range flags {
   848  			if fl == "-ldflags" {
   849  				ldflags = flags[i+1:]
   850  				flags = flags[0:i]
   851  				break
   852  			}
   853  		}
   854  
   855  		importcfgfile := importcfg(pkgs)
   856  
   857  		for i, pkg := range pkgs {
   858  			_, err := compileInDir(runcmd, longdir, flags, importcfgfile, pkg.name, pkg.files...)
   859  			// Allow this package compilation fail based on conditions below;
   860  			// its errors were checked in previous case.
   861  			if err != nil && !(wantError && action == "errorcheckandrundir" && i == len(pkgs)-2) {
   862  				return err
   863  			}
   864  
   865  			if i == len(pkgs)-1 {
   866  				err = linkFile(runcmd, "a.exe", pkg.files[0], importcfgfile, ldflags)
   867  				if err != nil {
   868  					return err
   869  				}
   870  				var cmd []string
   871  				cmd = append(cmd, findExecCmd()...)
   872  				cmd = append(cmd, filepath.Join(tempDir, "a.exe"))
   873  				cmd = append(cmd, args...)
   874  				out, err := runcmd(cmd...)
   875  				if err != nil {
   876  					return err
   877  				}
   878  				t.checkExpectedOutput(out)
   879  			}
   880  		}
   881  		return nil
   882  
   883  	case "runindir":
   884  		// Make a shallow copy of t.goDirName() in its own module and GOPATH, and
   885  		// run "go run ." in it. The module path (and hence import path prefix) of
   886  		// the copy is equal to the basename of the source directory.
   887  		//
   888  		// It's used when test a requires a full 'go build' in order to compile
   889  		// the sources, such as when importing multiple packages (issue29612.dir)
   890  		// or compiling a package containing assembly files (see issue15609.dir),
   891  		// but still needs to be run to verify the expected output.
   892  		tempDirIsGOPATH = true
   893  		srcDir := filepath.Join(t.gorootTestDir, t.goDirName())
   894  		modName := filepath.Base(srcDir)
   895  		gopathSrcDir := filepath.Join(tempDir, "src", modName)
   896  		runInDir = gopathSrcDir
   897  
   898  		if err := overlayDir(gopathSrcDir, srcDir); err != nil {
   899  			t.Fatal(err)
   900  		}
   901  
   902  		modVersion := gomodvers
   903  		if modVersion == "" {
   904  			modVersion = "1.14"
   905  		}
   906  		modFile := fmt.Sprintf("module %s\ngo %s\n", modName, modVersion)
   907  		if err := os.WriteFile(filepath.Join(gopathSrcDir, "go.mod"), []byte(modFile), 0666); err != nil {
   908  			t.Fatal(err)
   909  		}
   910  
   911  		cmd := []string{goTool, "run", t.goGcflags()}
   912  		if *linkshared {
   913  			cmd = append(cmd, "-linkshared")
   914  		}
   915  		cmd = append(cmd, flags...)
   916  		cmd = append(cmd, ".")
   917  		out, err := runcmd(cmd...)
   918  		if err != nil {
   919  			return err
   920  		}
   921  		return t.checkExpectedOutput(out)
   922  
   923  	case "build":
   924  		// Build Go file.
   925  		cmd := []string{goTool, "build", t.goGcflags()}
   926  		cmd = append(cmd, flags...)
   927  		cmd = append(cmd, "-o", "a.exe", long)
   928  		_, err := runcmd(cmd...)
   929  		return err
   930  
   931  	case "builddir", "buildrundir":
   932  		// Build an executable from all the .go and .s files in a subdirectory.
   933  		// Run it and verify its output in the buildrundir case.
   934  		longdir := filepath.Join(t.gorootTestDir, t.goDirName())
   935  		files, err := os.ReadDir(longdir)
   936  		if err != nil {
   937  			t.Fatal(err)
   938  		}
   939  		var gos []string
   940  		var asms []string
   941  		for _, file := range files {
   942  			switch filepath.Ext(file.Name()) {
   943  			case ".go":
   944  				gos = append(gos, filepath.Join(longdir, file.Name()))
   945  			case ".s":
   946  				asms = append(asms, filepath.Join(longdir, file.Name()))
   947  			}
   948  		}
   949  		if len(asms) > 0 {
   950  			emptyHdrFile := filepath.Join(tempDir, "go_asm.h")
   951  			if err := os.WriteFile(emptyHdrFile, nil, 0666); err != nil {
   952  				t.Fatalf("write empty go_asm.h: %v", err)
   953  			}
   954  			cmd := []string{goTool, "tool", "asm", "-p=main", "-gensymabis", "-o", "symabis"}
   955  			cmd = append(cmd, asms...)
   956  			_, err = runcmd(cmd...)
   957  			if err != nil {
   958  				return err
   959  			}
   960  		}
   961  		var objs []string
   962  		cmd := []string{goTool, "tool", "compile", "-p=main", "-e", "-D", ".", "-importcfg=" + stdlibImportcfgFile(), "-o", "go.o"}
   963  		if len(asms) > 0 {
   964  			cmd = append(cmd, "-asmhdr", "go_asm.h", "-symabis", "symabis")
   965  		}
   966  		cmd = append(cmd, gos...)
   967  		_, err = runcmd(cmd...)
   968  		if err != nil {
   969  			return err
   970  		}
   971  		objs = append(objs, "go.o")
   972  		if len(asms) > 0 {
   973  			cmd = []string{goTool, "tool", "asm", "-p=main", "-e", "-I", ".", "-o", "asm.o"}
   974  			cmd = append(cmd, asms...)
   975  			_, err = runcmd(cmd...)
   976  			if err != nil {
   977  				return err
   978  			}
   979  			objs = append(objs, "asm.o")
   980  		}
   981  		cmd = []string{goTool, "tool", "pack", "c", "all.a"}
   982  		cmd = append(cmd, objs...)
   983  		_, err = runcmd(cmd...)
   984  		if err != nil {
   985  			return err
   986  		}
   987  		err = linkFile(runcmd, "a.exe", "all.a", stdlibImportcfgFile(), nil)
   988  		if err != nil {
   989  			return err
   990  		}
   991  
   992  		if action == "builddir" {
   993  			return nil
   994  		}
   995  		cmd = append(findExecCmd(), filepath.Join(tempDir, "a.exe"))
   996  		out, err := runcmd(cmd...)
   997  		if err != nil {
   998  			return err
   999  		}
  1000  		return t.checkExpectedOutput(out)
  1001  
  1002  	case "buildrun":
  1003  		// Build an executable from Go file, then run it, verify its output.
  1004  		// Useful for timeout tests where failure mode is infinite loop.
  1005  		// TODO: not supported on NaCl
  1006  		cmd := []string{goTool, "build", t.goGcflags(), "-o", "a.exe"}
  1007  		if *linkshared {
  1008  			cmd = append(cmd, "-linkshared")
  1009  		}
  1010  		longDirGoFile := filepath.Join(filepath.Join(t.gorootTestDir, t.dir), t.goFile)
  1011  		cmd = append(cmd, flags...)
  1012  		cmd = append(cmd, longDirGoFile)
  1013  		_, err := runcmd(cmd...)
  1014  		if err != nil {
  1015  			return err
  1016  		}
  1017  		cmd = []string{"./a.exe"}
  1018  		out, err := runcmd(append(cmd, args...)...)
  1019  		if err != nil {
  1020  			return err
  1021  		}
  1022  
  1023  		return t.checkExpectedOutput(out)
  1024  
  1025  	case "run":
  1026  		// Run Go file if no special go command flags are provided;
  1027  		// otherwise build an executable and run it.
  1028  		// Verify the output.
  1029  		runInDir = ""
  1030  		var out []byte
  1031  		var err error
  1032  		if len(flags)+len(args) == 0 && t.goGcflagsIsEmpty() && !*linkshared && goarch == runtime.GOARCH && goos == runtime.GOOS && goexp == goExperiment && godebug == goDebug {
  1033  			// If we're not using special go command flags,
  1034  			// skip all the go command machinery.
  1035  			// This avoids any time the go command would
  1036  			// spend checking whether, for example, the installed
  1037  			// package runtime is up to date.
  1038  			// Because we run lots of trivial test programs,
  1039  			// the time adds up.
  1040  			pkg := filepath.Join(tempDir, "pkg.a")
  1041  			if _, err := runcmd(goTool, "tool", "compile", "-p=main", "-importcfg="+stdlibImportcfgFile(), "-o", pkg, t.goFileName()); err != nil {
  1042  				return err
  1043  			}
  1044  			exe := filepath.Join(tempDir, "test.exe")
  1045  			if err := linkFile(runcmd, exe, pkg, stdlibImportcfgFile(), nil); err != nil {
  1046  				return err
  1047  			}
  1048  			out, err = runcmd(append([]string{exe}, args...)...)
  1049  		} else {
  1050  			cmd := []string{goTool, "run", t.goGcflags()}
  1051  			if *linkshared {
  1052  				cmd = append(cmd, "-linkshared")
  1053  			}
  1054  			cmd = append(cmd, flags...)
  1055  			cmd = append(cmd, t.goFileName())
  1056  			out, err = runcmd(append(cmd, args...)...)
  1057  		}
  1058  		if err != nil {
  1059  			return err
  1060  		}
  1061  		return t.checkExpectedOutput(out)
  1062  
  1063  	case "runoutput":
  1064  		// Run Go file and write its output into temporary Go file.
  1065  		// Run generated Go file and verify its output.
  1066  		t.runoutputGate <- true
  1067  		defer func() {
  1068  			<-t.runoutputGate
  1069  		}()
  1070  		runInDir = ""
  1071  		cmd := []string{goTool, "run", t.goGcflags()}
  1072  		if *linkshared {
  1073  			cmd = append(cmd, "-linkshared")
  1074  		}
  1075  		cmd = append(cmd, t.goFileName())
  1076  		out, err := runcmd(append(cmd, args...)...)
  1077  		if err != nil {
  1078  			return err
  1079  		}
  1080  		tfile := filepath.Join(tempDir, "tmp__.go")
  1081  		if err := os.WriteFile(tfile, out, 0666); err != nil {
  1082  			t.Fatalf("write tempfile: %v", err)
  1083  		}
  1084  		cmd = []string{goTool, "run", t.goGcflags()}
  1085  		if *linkshared {
  1086  			cmd = append(cmd, "-linkshared")
  1087  		}
  1088  		cmd = append(cmd, tfile)
  1089  		out, err = runcmd(cmd...)
  1090  		if err != nil {
  1091  			return err
  1092  		}
  1093  		return t.checkExpectedOutput(out)
  1094  
  1095  	case "errorcheckoutput":
  1096  		// Run Go file and write its output into temporary Go file.
  1097  		// Compile and errorCheck generated Go file.
  1098  		runInDir = ""
  1099  		cmd := []string{goTool, "run", t.goGcflags()}
  1100  		if *linkshared {
  1101  			cmd = append(cmd, "-linkshared")
  1102  		}
  1103  		cmd = append(cmd, t.goFileName())
  1104  		out, err := runcmd(append(cmd, args...)...)
  1105  		if err != nil {
  1106  			return err
  1107  		}
  1108  		tfile := filepath.Join(tempDir, "tmp__.go")
  1109  		err = os.WriteFile(tfile, out, 0666)
  1110  		if err != nil {
  1111  			t.Fatalf("write tempfile: %v", err)
  1112  		}
  1113  		cmdline := []string{goTool, "tool", "compile", "-importcfg=" + stdlibImportcfgFile(), "-p=p", "-d=panic", "-e", "-o", "a.o"}
  1114  		cmdline = append(cmdline, flags...)
  1115  		cmdline = append(cmdline, tfile)
  1116  		out, err = runcmd(cmdline...)
  1117  		if wantError {
  1118  			if err == nil {
  1119  				return fmt.Errorf("compilation succeeded unexpectedly\n%s", out)
  1120  			}
  1121  		} else {
  1122  			if err != nil {
  1123  				return err
  1124  			}
  1125  		}
  1126  		return t.errorCheck(string(out), false, tfile, "tmp__.go")
  1127  	}
  1128  }
  1129  
  1130  var findExecCmd = sync.OnceValue(func() (execCmd []string) {
  1131  	if goos == runtime.GOOS && goarch == runtime.GOARCH {
  1132  		return nil
  1133  	}
  1134  	if path, err := exec.LookPath(fmt.Sprintf("go_%s_%s_exec", goos, goarch)); err == nil {
  1135  		execCmd = []string{path}
  1136  	}
  1137  	return execCmd
  1138  })
  1139  
  1140  // checkExpectedOutput compares the output from compiling and/or running with the contents
  1141  // of the corresponding reference output file, if any (replace ".go" with ".out").
  1142  // If they don't match, fail with an informative message.
  1143  func (t test) checkExpectedOutput(gotBytes []byte) error {
  1144  	got := string(gotBytes)
  1145  	filename := filepath.Join(t.dir, t.goFile)
  1146  	filename = filename[:len(filename)-len(".go")]
  1147  	filename += ".out"
  1148  	b, err := os.ReadFile(filepath.Join(t.gorootTestDir, filename))
  1149  	if errors.Is(err, fs.ErrNotExist) {
  1150  		// File is allowed to be missing, in which case output should be empty.
  1151  		b = nil
  1152  	} else if err != nil {
  1153  		return err
  1154  	}
  1155  	got = strings.ReplaceAll(got, "\r\n", "\n")
  1156  	if got != string(b) {
  1157  		if err == nil {
  1158  			return fmt.Errorf("output does not match expected in %s. Instead saw\n%s", filename, got)
  1159  		} else {
  1160  			return fmt.Errorf("output should be empty when (optional) expected-output file %s is not present. Instead saw\n%s", filename, got)
  1161  		}
  1162  	}
  1163  	return nil
  1164  }
  1165  
  1166  func splitOutput(out string, wantAuto bool) []string {
  1167  	// gc error messages continue onto additional lines with leading tabs.
  1168  	// Split the output at the beginning of each line that doesn't begin with a tab.
  1169  	// <autogenerated> lines are impossible to match so those are filtered out.
  1170  	var res []string
  1171  	for _, line := range strings.Split(out, "\n") {
  1172  		if strings.HasSuffix(line, "\r") { // remove '\r', output by compiler on windows
  1173  			line = line[:len(line)-1]
  1174  		}
  1175  		if strings.HasPrefix(line, "\t") {
  1176  			res[len(res)-1] += "\n" + line
  1177  		} else if strings.HasPrefix(line, "go tool") || strings.HasPrefix(line, "#") || !wantAuto && strings.HasPrefix(line, "<autogenerated>") {
  1178  			continue
  1179  		} else if strings.TrimSpace(line) != "" {
  1180  			res = append(res, line)
  1181  		}
  1182  	}
  1183  	return res
  1184  }
  1185  
  1186  // errorCheck matches errors in outStr against comments in source files.
  1187  // For each line of the source files which should generate an error,
  1188  // there should be a comment of the form // ERROR "regexp".
  1189  // If outStr has an error for a line which has no such comment,
  1190  // this function will report an error.
  1191  // Likewise if outStr does not have an error for a line which has a comment,
  1192  // or if the error message does not match the <regexp>.
  1193  // The <regexp> syntax is Perl but it's best to stick to egrep.
  1194  //
  1195  // Sources files are supplied as fullshort slice.
  1196  // It consists of pairs: full path to source file and its base name.
  1197  func (t test) errorCheck(outStr string, wantAuto bool, fullshort ...string) (err error) {
  1198  	defer func() {
  1199  		if testing.Verbose() && err != nil {
  1200  			t.Logf("gc output:\n%s", outStr)
  1201  		}
  1202  	}()
  1203  	var errs []error
  1204  	out := splitOutput(outStr, wantAuto)
  1205  
  1206  	// Cut directory name.
  1207  	for i := range out {
  1208  		for j := 0; j < len(fullshort); j += 2 {
  1209  			full, short := fullshort[j], fullshort[j+1]
  1210  			out[i] = replacePrefix(out[i], full, short)
  1211  		}
  1212  	}
  1213  
  1214  	var want []wantedError
  1215  	for j := 0; j < len(fullshort); j += 2 {
  1216  		full, short := fullshort[j], fullshort[j+1]
  1217  		want = append(want, t.wantedErrors(full, short)...)
  1218  	}
  1219  
  1220  	for _, we := range want {
  1221  		var errmsgs []string
  1222  		if we.auto {
  1223  			errmsgs, out = partitionStrings("<autogenerated>", out)
  1224  		} else {
  1225  			errmsgs, out = partitionStrings(we.prefix, out)
  1226  		}
  1227  		if len(errmsgs) == 0 {
  1228  			errs = append(errs, fmt.Errorf("%s:%d: missing error %q", we.file, we.lineNum, we.reStr))
  1229  			continue
  1230  		}
  1231  		matched := false
  1232  		n := len(out)
  1233  		for _, errmsg := range errmsgs {
  1234  			// Assume errmsg says "file:line: foo".
  1235  			// Cut leading "file:line: " to avoid accidental matching of file name instead of message.
  1236  			text := errmsg
  1237  			if _, suffix, ok := strings.Cut(text, " "); ok {
  1238  				text = suffix
  1239  			}
  1240  			if we.re.MatchString(text) {
  1241  				matched = true
  1242  			} else {
  1243  				out = append(out, errmsg)
  1244  			}
  1245  		}
  1246  		if !matched {
  1247  			errs = append(errs, fmt.Errorf("%s:%d: no match for %#q in:\n\t%s", we.file, we.lineNum, we.reStr, strings.Join(out[n:], "\n\t")))
  1248  			continue
  1249  		}
  1250  	}
  1251  
  1252  	if len(out) > 0 {
  1253  		// If a test uses -m and instantiates an imported generic function,
  1254  		// the errors will include messages for the instantiated function
  1255  		// with locations in the other package. Filter those out.
  1256  		localOut := make([]string, 0, len(out))
  1257  	outLoop:
  1258  		for _, errLine := range out {
  1259  			for j := 0; j < len(fullshort); j += 2 {
  1260  				full, short := fullshort[j], fullshort[j+1]
  1261  				if strings.HasPrefix(errLine, full+":") || strings.HasPrefix(errLine, short+":") {
  1262  					localOut = append(localOut, errLine)
  1263  					continue outLoop
  1264  				}
  1265  			}
  1266  		}
  1267  		out = localOut
  1268  	}
  1269  
  1270  	if len(out) > 0 {
  1271  		errs = append(errs, fmt.Errorf("Unmatched Errors:"))
  1272  		for _, errLine := range out {
  1273  			errs = append(errs, fmt.Errorf("%s", errLine))
  1274  		}
  1275  	}
  1276  
  1277  	if len(errs) == 0 {
  1278  		return nil
  1279  	}
  1280  	if len(errs) == 1 {
  1281  		return errs[0]
  1282  	}
  1283  	var buf bytes.Buffer
  1284  	fmt.Fprintf(&buf, "\n")
  1285  	for _, err := range errs {
  1286  		fmt.Fprintf(&buf, "%s\n", err.Error())
  1287  	}
  1288  	return errors.New(buf.String())
  1289  }
  1290  
  1291  func (test) updateErrors(out, file string) {
  1292  	base := path.Base(file)
  1293  	// Read in source file.
  1294  	src, err := os.ReadFile(file)
  1295  	if err != nil {
  1296  		fmt.Fprintln(os.Stderr, err)
  1297  		return
  1298  	}
  1299  	lines := strings.Split(string(src), "\n")
  1300  	// Remove old errors.
  1301  	for i := range lines {
  1302  		lines[i], _, _ = strings.Cut(lines[i], " // ERROR ")
  1303  	}
  1304  	// Parse new errors.
  1305  	errors := make(map[int]map[string]bool)
  1306  	tmpRe := regexp.MustCompile(`autotmp_\d+`)
  1307  	fileRe := regexp.MustCompile(`(\.go):\d+:`)
  1308  	for _, errStr := range splitOutput(out, false) {
  1309  		m := fileRe.FindStringSubmatchIndex(errStr)
  1310  		if len(m) != 4 {
  1311  			continue
  1312  		}
  1313  		// The end of the file is the end of the first and only submatch.
  1314  		errFile := errStr[:m[3]]
  1315  		rest := errStr[m[3]+1:]
  1316  		if errFile != file {
  1317  			continue
  1318  		}
  1319  		lineStr, msg, ok := strings.Cut(rest, ":")
  1320  		if !ok {
  1321  			continue
  1322  		}
  1323  		line, err := strconv.Atoi(lineStr)
  1324  		line--
  1325  		if err != nil || line < 0 || line >= len(lines) {
  1326  			continue
  1327  		}
  1328  		msg = strings.ReplaceAll(msg, file, base) // normalize file mentions in error itself
  1329  		msg = strings.TrimLeft(msg, " \t")
  1330  		for _, r := range []string{`\`, `*`, `+`, `?`, `[`, `]`, `(`, `)`} {
  1331  			msg = strings.ReplaceAll(msg, r, `\`+r)
  1332  		}
  1333  		msg = strings.ReplaceAll(msg, `"`, `.`)
  1334  		msg = tmpRe.ReplaceAllLiteralString(msg, `autotmp_[0-9]+`)
  1335  		if errors[line] == nil {
  1336  			errors[line] = make(map[string]bool)
  1337  		}
  1338  		errors[line][msg] = true
  1339  	}
  1340  	// Add new errors.
  1341  	for line, errs := range errors {
  1342  		var sorted []string
  1343  		for e := range errs {
  1344  			sorted = append(sorted, e)
  1345  		}
  1346  		sort.Strings(sorted)
  1347  		lines[line] += " // ERROR"
  1348  		for _, e := range sorted {
  1349  			lines[line] += fmt.Sprintf(` "%s$"`, e)
  1350  		}
  1351  	}
  1352  	// Write new file.
  1353  	err = os.WriteFile(file, []byte(strings.Join(lines, "\n")), 0640)
  1354  	if err != nil {
  1355  		fmt.Fprintln(os.Stderr, err)
  1356  		return
  1357  	}
  1358  	// Polish.
  1359  	exec.Command(goTool, "fmt", file).CombinedOutput()
  1360  }
  1361  
  1362  // matchPrefix reports whether s is of the form ^(.*/)?prefix(:|[),
  1363  // That is, it needs the file name prefix followed by a : or a [,
  1364  // and possibly preceded by a directory name.
  1365  func matchPrefix(s, prefix string) bool {
  1366  	i := strings.Index(s, ":")
  1367  	if i < 0 {
  1368  		return false
  1369  	}
  1370  	j := strings.LastIndex(s[:i], "/")
  1371  	s = s[j+1:]
  1372  	if len(s) <= len(prefix) || s[:len(prefix)] != prefix {
  1373  		return false
  1374  	}
  1375  	switch s[len(prefix)] {
  1376  	case '[', ':':
  1377  		return true
  1378  	}
  1379  	return false
  1380  }
  1381  
  1382  func partitionStrings(prefix string, strs []string) (matched, unmatched []string) {
  1383  	for _, s := range strs {
  1384  		if matchPrefix(s, prefix) {
  1385  			matched = append(matched, s)
  1386  		} else {
  1387  			unmatched = append(unmatched, s)
  1388  		}
  1389  	}
  1390  	return
  1391  }
  1392  
  1393  type wantedError struct {
  1394  	reStr   string
  1395  	re      *regexp.Regexp
  1396  	lineNum int
  1397  	auto    bool // match <autogenerated> line
  1398  	file    string
  1399  	prefix  string
  1400  }
  1401  
  1402  var (
  1403  	errRx            = regexp.MustCompile(`// (?:GC_)?ERROR (.*)`)
  1404  	errAutoRx        = regexp.MustCompile(`// (?:GC_)?ERRORAUTO (.*)`)
  1405  	errQuotesRx      = regexp.MustCompile(`"([^"]*)"`)
  1406  	lineRx           = regexp.MustCompile(`LINE(([+-])(\d+))?`)
  1407  	possibleOpcodeRx = regexp.MustCompile(`([A-Z][A-Z]|[IF](32|64))`) // two caps, or a wasm prefix
  1408  )
  1409  
  1410  func (t test) wantedErrors(file, short string) (errs []wantedError) {
  1411  	cache := make(map[string]*regexp.Regexp)
  1412  
  1413  	src, _ := os.ReadFile(file)
  1414  	for i, line := range strings.Split(string(src), "\n") {
  1415  		lineNum := i + 1
  1416  		if strings.Contains(line, "////") {
  1417  			// double comment disables ERROR
  1418  			continue
  1419  		}
  1420  		var auto bool
  1421  		m := errAutoRx.FindStringSubmatch(line)
  1422  		if m != nil {
  1423  			auto = true
  1424  		} else {
  1425  			m = errRx.FindStringSubmatch(line)
  1426  		}
  1427  		if m == nil {
  1428  			continue
  1429  		}
  1430  		all := m[1]
  1431  		mm := errQuotesRx.FindAllStringSubmatch(all, -1)
  1432  		if mm == nil {
  1433  			t.Fatalf("%s:%d: invalid errchk line: %s", t.goFileName(), lineNum, line)
  1434  		}
  1435  		for _, m := range mm {
  1436  			rx := lineRx.ReplaceAllStringFunc(m[1], func(m string) string {
  1437  				n := lineNum
  1438  				if strings.HasPrefix(m, "LINE+") {
  1439  					delta, _ := strconv.Atoi(m[5:])
  1440  					n += delta
  1441  				} else if strings.HasPrefix(m, "LINE-") {
  1442  					delta, _ := strconv.Atoi(m[5:])
  1443  					n -= delta
  1444  				}
  1445  				return fmt.Sprintf("%s:%d", short, n)
  1446  			})
  1447  			re := cache[rx]
  1448  			if re == nil {
  1449  				var err error
  1450  				re, err = regexp.Compile(rx)
  1451  				if err != nil {
  1452  					t.Fatalf("%s:%d: invalid regexp \"%s\" in ERROR line: %v", t.goFileName(), lineNum, rx, err)
  1453  				}
  1454  				cache[rx] = re
  1455  			}
  1456  			prefix := fmt.Sprintf("%s:%d", short, lineNum)
  1457  			errs = append(errs, wantedError{
  1458  				reStr:   rx,
  1459  				re:      re,
  1460  				prefix:  prefix,
  1461  				auto:    auto,
  1462  				lineNum: lineNum,
  1463  				file:    short,
  1464  			})
  1465  		}
  1466  	}
  1467  
  1468  	return
  1469  }
  1470  
  1471  const (
  1472  	// Regexp to match a single opcode check: optionally begin with "-" (to indicate
  1473  	// a negative check) or a positive number (to specify the expected number of
  1474  	// matches), followed by a string literal enclosed in "" or ``. For "",
  1475  	// backslashes must be handled.
  1476  	reMatchCheck = `(-|[1-9]\d*)?(?:\x60[^\x60]*\x60|"(?:[^"\\]|\\.)*")`
  1477  )
  1478  
  1479  var (
  1480  	// Regexp to split a line in code and comment, trimming spaces
  1481  	rxAsmComment = regexp.MustCompile(`^\s*(.*?)\s*(?://\s*(.+)\s*)?$`)
  1482  
  1483  	// Regexp to extract an architecture check: architecture name (or triplet),
  1484  	// followed by semi-colon, followed by a comma-separated list of opcode checks.
  1485  	// Extraneous spaces are ignored.
  1486  	//
  1487  	// An example: arm64/v8.1 : -`ADD` `SUB`
  1488  	//	"(\w+)" matches "arm64" (architecture name)
  1489  	//	"(/[\w.]+)?" matches "v8.1" (architecture version)
  1490  	//	"(/\w*)?" doesn't match anything here (it's an optional part of the triplet)
  1491  	//	"\s*:\s*" matches " : " (semi-colon)
  1492  	//	"(" starts a capturing group
  1493  	//      first reMatchCheck matches "-`ADD`"
  1494  	//	`(?:" starts a non-capturing group
  1495  	//	"[\s,]+" matches " "
  1496  	//	second reMatchCheck matches "`SUB`"
  1497  	//	")*)" closes started groups; "*" means that there might be other elements in the space-separated list
  1498  	rxAsmPlatform = regexp.MustCompile(`(\w+)(/[\w.]+)?(/\w*)?\s*:\s*(` + reMatchCheck + `(?:[\s,]+` + reMatchCheck + `)*)`)
  1499  
  1500  	// Regexp to extract a single opcoded check
  1501  	rxAsmCheck = regexp.MustCompile(reMatchCheck)
  1502  
  1503  	// List of all architecture variants. Key is the GOARCH architecture,
  1504  	// value[0] is the variant-changing environment variable, and values[1:]
  1505  	// are the supported variants.
  1506  	archVariants = map[string][]string{
  1507  		"386":     {"GO386", "sse2", "softfloat"},
  1508  		"amd64":   {"GOAMD64", "v1", "v2", "v3", "v4"},
  1509  		"arm":     {"GOARM", "5", "6", "7", "7,softfloat"},
  1510  		"arm64":   {"GOARM64", "v8.0", "v8.1"},
  1511  		"loong64": {},
  1512  		"mips":    {"GOMIPS", "hardfloat", "softfloat"},
  1513  		"mips64":  {"GOMIPS64", "hardfloat", "softfloat"},
  1514  		"ppc64":   {"GOPPC64", "power8", "power9", "power10"},
  1515  		"ppc64le": {"GOPPC64", "power8", "power9", "power10"},
  1516  		"ppc64x":  {}, // A pseudo-arch representing both ppc64 and ppc64le
  1517  		"s390x":   {},
  1518  		"wasm":    {},
  1519  		"riscv64": {"GORISCV64", "rva20u64", "rva22u64", "rva23u64"},
  1520  	}
  1521  )
  1522  
  1523  // wantedAsmOpcode is a single asmcheck check
  1524  type wantedAsmOpcode struct {
  1525  	fileline string         // original source file/line (eg: "/path/foo.go:45")
  1526  	line     int            // original source line
  1527  	opcode   *regexp.Regexp // opcode check to be performed on assembly output
  1528  	expected int            // expected number of matches
  1529  	actual   int            // actual number that matched
  1530  	negative bool           // true if the check is supposed to fail rather than pass
  1531  	found    bool           // true if the opcode check matched at least one in the output
  1532  }
  1533  
  1534  // A build environment triplet separated by slashes (eg: linux/386/sse2).
  1535  // The third field can be empty if the arch does not support variants (eg: "plan9/amd64/")
  1536  type buildEnv string
  1537  
  1538  // Environ returns the environment it represents in cmd.Environ() "key=val" format
  1539  // For instance, "linux/386/sse2".Environ() returns {"GOOS=linux", "GOARCH=386", "GO386=sse2"}
  1540  func (b buildEnv) Environ() []string {
  1541  	fields := strings.Split(string(b), "/")
  1542  	if len(fields) != 3 {
  1543  		panic("invalid buildEnv string: " + string(b))
  1544  	}
  1545  	env := []string{"GOOS=" + fields[0], "GOARCH=" + fields[1]}
  1546  	if fields[2] != "" {
  1547  		env = append(env, archVariants[fields[1]][0]+"="+fields[2])
  1548  	}
  1549  	return env
  1550  }
  1551  
  1552  // asmChecks represents all the asmcheck checks present in a test file
  1553  // The outer map key is the build triplet in which the checks must be performed.
  1554  // The inner map key represent the source file line ("filename.go:1234") at which the
  1555  // checks must be performed.
  1556  type asmChecks map[buildEnv]map[string][]wantedAsmOpcode
  1557  
  1558  // Envs returns all the buildEnv in which at least one check is present
  1559  func (a asmChecks) Envs() []buildEnv {
  1560  	var envs []buildEnv
  1561  	for e := range a {
  1562  		envs = append(envs, e)
  1563  	}
  1564  	sort.Slice(envs, func(i, j int) bool {
  1565  		return string(envs[i]) < string(envs[j])
  1566  	})
  1567  	return envs
  1568  }
  1569  
  1570  func (t test) wantedAsmOpcodes(fn string) asmChecks {
  1571  	ops := make(asmChecks)
  1572  
  1573  	comment := ""
  1574  	src, err := os.ReadFile(fn)
  1575  	if err != nil {
  1576  		t.Fatal(err)
  1577  	}
  1578  	for i, line := range strings.Split(string(src), "\n") {
  1579  		matches := rxAsmComment.FindStringSubmatch(line)
  1580  		code, cmt := matches[1], matches[2]
  1581  
  1582  		// Keep comments pending in the comment variable until
  1583  		// we find a line that contains some code.
  1584  		comment += " " + cmt
  1585  		if code == "" {
  1586  			continue
  1587  		}
  1588  
  1589  		// Parse and extract any architecture check from comments,
  1590  		// made by one architecture name and multiple checks.
  1591  		lnum := fn + ":" + strconv.Itoa(i+1)
  1592  		lastUsed := 0
  1593  		for _, ac := range rxAsmPlatform.FindAllStringSubmatch(comment, -1) {
  1594  			archspec, allchecks := ac[1:4], ac[4]
  1595  			lastUsed = strings.LastIndex(comment, allchecks) + len(allchecks)
  1596  			var arch, subarch, os string
  1597  			switch {
  1598  			case archspec[2] != "": // 3 components: "linux/386/sse2"
  1599  				os, arch, subarch = archspec[0], archspec[1][1:], archspec[2][1:]
  1600  			case archspec[1] != "": // 2 components: "386/sse2"
  1601  				os, arch, subarch = "linux", archspec[0], archspec[1][1:]
  1602  			default: // 1 component: "386"
  1603  				os, arch, subarch = "linux", archspec[0], ""
  1604  				if arch == "wasm" {
  1605  					os = "js"
  1606  				}
  1607  			}
  1608  
  1609  			if _, ok := archVariants[arch]; !ok {
  1610  				t.Fatalf("%s:%d: unsupported architecture: %v", t.goFileName(), i+1, arch)
  1611  			}
  1612  
  1613  			// Create the build environments corresponding the above specifiers
  1614  			envs := make([]buildEnv, 0, 4)
  1615  			arches := []string{arch}
  1616  			// ppc64x is a pseudo-arch, generate tests for both endian variants.
  1617  			if arch == "ppc64x" {
  1618  				arches = []string{"ppc64", "ppc64le"}
  1619  			}
  1620  			for _, arch := range arches {
  1621  				if subarch != "" {
  1622  					envs = append(envs, buildEnv(os+"/"+arch+"/"+subarch))
  1623  				} else {
  1624  					subarchs := archVariants[arch]
  1625  					if len(subarchs) == 0 {
  1626  						envs = append(envs, buildEnv(os+"/"+arch+"/"))
  1627  					} else {
  1628  						for _, sa := range archVariants[arch][1:] {
  1629  							envs = append(envs, buildEnv(os+"/"+arch+"/"+sa))
  1630  						}
  1631  					}
  1632  				}
  1633  			}
  1634  
  1635  			for _, m := range rxAsmCheck.FindAllString(allchecks, -1) {
  1636  				negative := false
  1637  				expected := 0
  1638  				if m[0] == '-' {
  1639  					negative = true
  1640  					m = m[1:]
  1641  				} else if '1' <= m[0] && m[0] <= '9' {
  1642  					for '0' <= m[0] && m[0] <= '9' {
  1643  						expected *= 10
  1644  						expected += int(m[0] - '0')
  1645  						m = m[1:]
  1646  					}
  1647  				}
  1648  
  1649  				rxsrc, err := strconv.Unquote(m)
  1650  				if err != nil {
  1651  					t.Fatalf("%s:%d: error unquoting string: %v", t.goFileName(), i+1, err)
  1652  				}
  1653  
  1654  				// Compile the checks as regular expressions. Notice that we
  1655  				// consider checks as matching from the beginning of the actual
  1656  				// assembler source (that is, what is left on each line of the
  1657  				// compile -S output after we strip file/line info) to avoid
  1658  				// trivial bugs such as "ADD" matching "FADD". This
  1659  				// doesn't remove genericity: it's still possible to write
  1660  				// something like "F?ADD", but we make common cases simpler
  1661  				// to get right.
  1662  				oprx, err := regexp.Compile("^" + rxsrc)
  1663  				if err != nil {
  1664  					t.Fatalf("%s:%d: %v", t.goFileName(), i+1, err)
  1665  				}
  1666  
  1667  				for _, env := range envs {
  1668  					if ops[env] == nil {
  1669  						ops[env] = make(map[string][]wantedAsmOpcode)
  1670  					}
  1671  					ops[env][lnum] = append(ops[env][lnum], wantedAsmOpcode{
  1672  						expected: expected,
  1673  						negative: negative,
  1674  						fileline: lnum,
  1675  						line:     i + 1,
  1676  						opcode:   oprx,
  1677  					})
  1678  				}
  1679  			}
  1680  		}
  1681  		if lastUsed > 0 {
  1682  			// There was an asm spec in this comment. Check for possible syntax
  1683  			// errors, which would leave some asm patterns unused. We want
  1684  			//  to allow some tail, for example for English comments. The
  1685  			// heuristic we use here is we look for two consecutive capital
  1686  			// letters (or a wasm prefix). Those are probably assembly mnemonics
  1687  			// that weren't used.
  1688  			tail := comment[lastUsed:]
  1689  			if possibleOpcodeRx.MatchString(tail) {
  1690  				t.Errorf("%s:%d: possible unused assembly pattern: %v", t.goFileName(), i+1, tail)
  1691  			} else if strings.Count(comment, "\"")%2 != 0 || strings.Count(comment, "`")%2 != 0 {
  1692  				t.Errorf("%s:%d: unbalanced quotes: %v", t.goFileName(), i+1, comment)
  1693  			} else if strings.Contains(comment, "\",") || strings.Contains(comment, "`,") {
  1694  				t.Errorf("%s:%d: comma separator - use space instead: %v", t.goFileName(), i+1, comment)
  1695  			}
  1696  		}
  1697  		comment = ""
  1698  	}
  1699  
  1700  	return ops
  1701  }
  1702  
  1703  func (t test) asmCheck(outStr string, fn string, env buildEnv, fullops map[string][]wantedAsmOpcode) error {
  1704  	// The assembly output contains the concatenated dump of multiple functions.
  1705  	// the first line of each function begins at column 0, while the rest is
  1706  	// indented by a tabulation. These data structures help us index the
  1707  	// output by function.
  1708  	functionMarkers := make([]int, 1)
  1709  	lineFuncMap := make(map[string]int)
  1710  
  1711  	lines := strings.Split(outStr, "\n")
  1712  	rxLine := regexp.MustCompile(fmt.Sprintf(`\((%s:\d+)\)\s+(.*)`, regexp.QuoteMeta(fn)))
  1713  
  1714  	for nl, line := range lines {
  1715  		// Check if this line begins a function
  1716  		if len(line) > 0 && line[0] != '\t' {
  1717  			functionMarkers = append(functionMarkers, nl)
  1718  		}
  1719  
  1720  		// Search if this line contains a assembly opcode (which is prefixed by the
  1721  		// original source file/line in parenthesis)
  1722  		matches := rxLine.FindStringSubmatch(line)
  1723  		if len(matches) == 0 {
  1724  			continue
  1725  		}
  1726  		srcFileLine, asm := matches[1], matches[2]
  1727  
  1728  		// Replace tabs with single spaces to make matches easier to write.
  1729  		asm = strings.ReplaceAll(asm, "\t", " ")
  1730  
  1731  		// Associate the original file/line information to the current
  1732  		// function in the output; it will be useful to dump it in case
  1733  		// of error.
  1734  		lineFuncMap[srcFileLine] = len(functionMarkers) - 1
  1735  
  1736  		// If there are opcode checks associated to this source file/line,
  1737  		// run the checks.
  1738  		if ops, found := fullops[srcFileLine]; found {
  1739  			for i := range ops {
  1740  				if (!ops[i].found || ops[i].expected > 0) && ops[i].opcode.FindString(asm) != "" {
  1741  					ops[i].actual++
  1742  					ops[i].found = true
  1743  				}
  1744  			}
  1745  		}
  1746  	}
  1747  	functionMarkers = append(functionMarkers, len(lines))
  1748  
  1749  	var failed []wantedAsmOpcode
  1750  	for _, ops := range fullops {
  1751  		for _, o := range ops {
  1752  			// There's a failure if a negative match was found,
  1753  			// or a positive match was not found.
  1754  			if o.negative == o.found {
  1755  				failed = append(failed, o)
  1756  			}
  1757  			if o.expected > 0 && o.expected != o.actual {
  1758  				failed = append(failed, o)
  1759  			}
  1760  		}
  1761  	}
  1762  	if len(failed) == 0 {
  1763  		return nil
  1764  	}
  1765  
  1766  	// At least one asmcheck failed; report them.
  1767  	lastFunction := -1
  1768  	var errbuf bytes.Buffer
  1769  	fmt.Fprintln(&errbuf)
  1770  	sort.Slice(failed, func(i, j int) bool { return failed[i].line < failed[j].line })
  1771  	for _, o := range failed {
  1772  		// Dump the function in which this opcode check was supposed to
  1773  		// pass but failed.
  1774  		funcIdx := lineFuncMap[o.fileline]
  1775  		if funcIdx != 0 && funcIdx != lastFunction {
  1776  			funcLines := lines[functionMarkers[funcIdx]:functionMarkers[funcIdx+1]]
  1777  			t.Log(strings.Join(funcLines, "\n"))
  1778  			lastFunction = funcIdx // avoid printing same function twice
  1779  		}
  1780  
  1781  		if o.negative {
  1782  			fmt.Fprintf(&errbuf, "%s:%d: %s: wrong opcode found: %#q\n", t.goFileName(), o.line, env, o.opcode.String())
  1783  		} else if o.expected > 0 {
  1784  			fmt.Fprintf(&errbuf, "%s:%d: %s: wrong number of opcodes: %#q\n", t.goFileName(), o.line, env, o.opcode.String())
  1785  		} else {
  1786  			fmt.Fprintf(&errbuf, "%s:%d: %s: opcode not found: %#q\n", t.goFileName(), o.line, env, o.opcode.String())
  1787  		}
  1788  	}
  1789  	return errors.New(errbuf.String())
  1790  }
  1791  
  1792  // defaultRunOutputLimit returns the number of runoutput tests that
  1793  // can be executed in parallel.
  1794  func defaultRunOutputLimit() int {
  1795  	const maxArmCPU = 2
  1796  
  1797  	cpu := runtime.NumCPU()
  1798  	if runtime.GOARCH == "arm" && cpu > maxArmCPU {
  1799  		cpu = maxArmCPU
  1800  	}
  1801  	return cpu
  1802  }
  1803  
  1804  func TestShouldTest(t *testing.T) {
  1805  	if *shard != 0 {
  1806  		t.Skipf("nothing to test on shard index %d", *shard)
  1807  	}
  1808  
  1809  	assert := func(ok bool, _ string) {
  1810  		t.Helper()
  1811  		if !ok {
  1812  			t.Error("test case failed")
  1813  		}
  1814  	}
  1815  	assertNot := func(ok bool, _ string) { t.Helper(); assert(!ok, "") }
  1816  
  1817  	// Simple tests.
  1818  	assert(shouldTest("// +build linux", "linux", "arm"))
  1819  	assert(shouldTest("// +build !windows", "linux", "arm"))
  1820  	assertNot(shouldTest("// +build !windows", "windows", "amd64"))
  1821  
  1822  	// A file with no build tags will always be tested.
  1823  	assert(shouldTest("// This is a test.", "os", "arch"))
  1824  
  1825  	// Build tags separated by a space are OR-ed together.
  1826  	assertNot(shouldTest("// +build arm 386", "linux", "amd64"))
  1827  
  1828  	// Build tags separated by a comma are AND-ed together.
  1829  	assertNot(shouldTest("// +build !windows,!plan9", "windows", "amd64"))
  1830  	assertNot(shouldTest("// +build !windows,!plan9", "plan9", "386"))
  1831  
  1832  	// Build tags on multiple lines are AND-ed together.
  1833  	assert(shouldTest("// +build !windows\n// +build amd64", "linux", "amd64"))
  1834  	assertNot(shouldTest("// +build !windows\n// +build amd64", "windows", "amd64"))
  1835  
  1836  	// Test that (!a OR !b) matches anything.
  1837  	assert(shouldTest("// +build !windows !plan9", "windows", "amd64"))
  1838  
  1839  	// Test that //go:build tag match.
  1840  	assert(shouldTest("//go:build go1.4", "linux", "amd64"))
  1841  }
  1842  
  1843  // overlayDir makes a minimal-overhead copy of srcRoot in which new files may be added.
  1844  func overlayDir(dstRoot, srcRoot string) error {
  1845  	dstRoot = filepath.Clean(dstRoot)
  1846  	if err := os.MkdirAll(dstRoot, 0777); err != nil {
  1847  		return err
  1848  	}
  1849  
  1850  	srcRoot, err := filepath.Abs(srcRoot)
  1851  	if err != nil {
  1852  		return err
  1853  	}
  1854  
  1855  	return filepath.WalkDir(srcRoot, func(srcPath string, d fs.DirEntry, err error) error {
  1856  		if err != nil || srcPath == srcRoot {
  1857  			return err
  1858  		}
  1859  
  1860  		suffix := strings.TrimPrefix(srcPath, srcRoot)
  1861  		for len(suffix) > 0 && suffix[0] == filepath.Separator {
  1862  			suffix = suffix[1:]
  1863  		}
  1864  		dstPath := filepath.Join(dstRoot, suffix)
  1865  
  1866  		var info fs.FileInfo
  1867  		if d.Type()&os.ModeSymlink != 0 {
  1868  			info, err = os.Stat(srcPath)
  1869  		} else {
  1870  			info, err = d.Info()
  1871  		}
  1872  		if err != nil {
  1873  			return err
  1874  		}
  1875  		perm := info.Mode() & os.ModePerm
  1876  
  1877  		// Always copy directories (don't symlink them).
  1878  		// If we add a file in the overlay, we don't want to add it in the original.
  1879  		if info.IsDir() {
  1880  			return os.MkdirAll(dstPath, perm|0200)
  1881  		}
  1882  
  1883  		// If the OS supports symlinks, use them instead of copying bytes.
  1884  		if err := os.Symlink(srcPath, dstPath); err == nil {
  1885  			return nil
  1886  		}
  1887  
  1888  		// Otherwise, copy the bytes.
  1889  		src, err := os.Open(srcPath)
  1890  		if err != nil {
  1891  			return err
  1892  		}
  1893  		defer src.Close()
  1894  
  1895  		dst, err := os.OpenFile(dstPath, os.O_WRONLY|os.O_CREATE|os.O_EXCL, perm)
  1896  		if err != nil {
  1897  			return err
  1898  		}
  1899  
  1900  		_, err = io.Copy(dst, src)
  1901  		if closeErr := dst.Close(); err == nil {
  1902  			err = closeErr
  1903  		}
  1904  		return err
  1905  	})
  1906  }
  1907  
  1908  // The following sets of files are excluded from testing depending on configuration.
  1909  // The types2Failures(32Bit) files pass with the 1.17 compiler but don't pass with
  1910  // the 1.18 compiler using the new types2 type checker, or pass with sub-optimal
  1911  // error(s).
  1912  
  1913  // List of files that the compiler cannot errorcheck with the new typechecker (types2).
  1914  var types2Failures = setOf(
  1915  	"shift1.go",               // types2 reports two new errors which are probably not right
  1916  	"fixedbugs/issue10700.go", // types2 should give hint about ptr to interface
  1917  	"fixedbugs/issue18331.go", // missing error about misuse of //go:noescape (irgen needs code from noder)
  1918  	"fixedbugs/issue18419.go", // types2 reports no field or method member, but should say unexported
  1919  	"fixedbugs/issue20233.go", // types2 reports two instead of one error (preference: 1.17 compiler)
  1920  	"fixedbugs/issue20245.go", // types2 reports two instead of one error (preference: 1.17 compiler)
  1921  	"fixedbugs/issue31053.go", // types2 reports "unknown field" instead of "cannot refer to unexported field"
  1922  )
  1923  
  1924  var types2Failures32Bit = setOf(
  1925  	"printbig.go",             // large untyped int passed to print (32-bit)
  1926  	"fixedbugs/bug114.go",     // large untyped int passed to println (32-bit)
  1927  	"fixedbugs/issue23305.go", // large untyped int passed to println (32-bit)
  1928  )
  1929  
  1930  // In all of these cases, the 1.17 compiler reports reasonable errors, but either the
  1931  // 1.17 or 1.18 compiler report extra errors, so we can't match correctly on both. We
  1932  // now set the patterns to match correctly on all the 1.18 errors.
  1933  // This list remains here just as a reference and for comparison - these files all pass.
  1934  var _ = setOf(
  1935  	"import1.go",      // types2 reports extra errors
  1936  	"initializerr.go", // types2 reports extra error
  1937  	"typecheck.go",    // types2 reports extra error at function call
  1938  
  1939  	"fixedbugs/bug176.go", // types2 reports all errors (pref: types2)
  1940  	"fixedbugs/bug195.go", // types2 reports slight different errors, and an extra error
  1941  	"fixedbugs/bug412.go", // types2 produces a follow-on error
  1942  
  1943  	"fixedbugs/issue11614.go", // types2 reports an extra error
  1944  	"fixedbugs/issue17038.go", // types2 doesn't report a follow-on error (pref: types2)
  1945  	"fixedbugs/issue23732.go", // types2 reports different (but ok) line numbers
  1946  	"fixedbugs/issue4510.go",  // types2 reports different (but ok) line numbers
  1947  	"fixedbugs/issue7525b.go", // types2 reports init cycle error on different line - ok otherwise
  1948  	"fixedbugs/issue7525c.go", // types2 reports init cycle error on different line - ok otherwise
  1949  	"fixedbugs/issue7525d.go", // types2 reports init cycle error on different line - ok otherwise
  1950  	"fixedbugs/issue7525e.go", // types2 reports init cycle error on different line - ok otherwise
  1951  	"fixedbugs/issue7525.go",  // types2 reports init cycle error on different line - ok otherwise
  1952  )
  1953  
  1954  func setOf(keys ...string) map[string]bool {
  1955  	m := make(map[string]bool, len(keys))
  1956  	for _, key := range keys {
  1957  		m[key] = true
  1958  	}
  1959  	return m
  1960  }
  1961  
  1962  // splitQuoted splits the string s around each instance of one or more consecutive
  1963  // white space characters while taking into account quotes and escaping, and
  1964  // returns an array of substrings of s or an empty list if s contains only white space.
  1965  // Single quotes and double quotes are recognized to prevent splitting within the
  1966  // quoted region, and are removed from the resulting substrings. If a quote in s
  1967  // isn't closed err will be set and r will have the unclosed argument as the
  1968  // last element. The backslash is used for escaping.
  1969  //
  1970  // For example, the following string:
  1971  //
  1972  //	a b:"c d" 'e''f'  "g\""
  1973  //
  1974  // Would be parsed as:
  1975  //
  1976  //	[]string{"a", "b:c d", "ef", `g"`}
  1977  //
  1978  // [copied from src/go/build/build.go]
  1979  func splitQuoted(s string) (r []string, err error) {
  1980  	var args []string
  1981  	arg := make([]rune, len(s))
  1982  	escaped := false
  1983  	quoted := false
  1984  	quote := '\x00'
  1985  	i := 0
  1986  	for _, rune := range s {
  1987  		switch {
  1988  		case escaped:
  1989  			escaped = false
  1990  		case rune == '\\':
  1991  			escaped = true
  1992  			continue
  1993  		case quote != '\x00':
  1994  			if rune == quote {
  1995  				quote = '\x00'
  1996  				continue
  1997  			}
  1998  		case rune == '"' || rune == '\'':
  1999  			quoted = true
  2000  			quote = rune
  2001  			continue
  2002  		case unicode.IsSpace(rune):
  2003  			if quoted || i > 0 {
  2004  				quoted = false
  2005  				args = append(args, string(arg[:i]))
  2006  				i = 0
  2007  			}
  2008  			continue
  2009  		}
  2010  		arg[i] = rune
  2011  		i++
  2012  	}
  2013  	if quoted || i > 0 {
  2014  		args = append(args, string(arg[:i]))
  2015  	}
  2016  	if quote != 0 {
  2017  		err = errors.New("unclosed quote")
  2018  	} else if escaped {
  2019  		err = errors.New("unfinished escaping")
  2020  	}
  2021  	return args, err
  2022  }
  2023  
  2024  // replacePrefix is like strings.ReplaceAll, but only replaces instances of old
  2025  // that are preceded by ' ', '\t', or appear at the beginning of a line.
  2026  //
  2027  // This does the same kind of filename string replacement as cmd/go.
  2028  // Pilfered from src/cmd/go/internal/work/shell.go .
  2029  func replacePrefix(s, old, new string) string {
  2030  	n := strings.Count(s, old)
  2031  	if n == 0 {
  2032  		return s
  2033  	}
  2034  
  2035  	s = strings.ReplaceAll(s, " "+old, " "+new)
  2036  	s = strings.ReplaceAll(s, "\n"+old, "\n"+new)
  2037  	s = strings.ReplaceAll(s, "\n\t"+old, "\n\t"+new)
  2038  	if strings.HasPrefix(s, old) {
  2039  		s = new + s[len(old):]
  2040  	}
  2041  	return s
  2042  }
  2043  

View as plain text