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

View as plain text