1
2
3
4
5
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
51
52
53
54 func defaultAllCodeGen() bool {
55
56
57
58
59 return strings.HasPrefix(testenv.Builder(), "gotip-linux-amd64")
60 }
61
62 var (
63
64 goTool string
65 goos string
66 goarch string
67 cgoEnabled bool
68 goExperiment string
69 goDebug string
70 tmpDir string
71
72
73
74 dirs = []string{".", "ken", "chan", "interface", "internal/runtime/sys", "syntax", "dwarf", "fixedbugs", "codegen", "abi", "typeparam", "typeparam/mdempsky", "arenas", "simd"}
75 )
76
77
78
79
80
81 func Test(t *testing.T) {
82 if *target != "" {
83
84
85
86
87
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
131
132
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
241
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
263 gorootTestDir string
264
265
266
267 runoutputGate chan bool
268 }
269
270
271 type test struct {
272 testCommon
273 *testing.T
274
275
276 dir, goFile string
277 }
278
279
280
281 func (t test) expectFail() bool {
282 failureSets := []map[string]bool{types2Failures}
283
284
285
286 switch goarch {
287 case "386", "arm", "mips", "mipsle":
288 failureSets = append(failureSets, types2Failures32Bit)
289 }
290
291 testName := path.Join(t.dir, t.goFile)
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
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
338 type goDirPkg struct {
339 name string
340 files []string
341 }
342
343
344
345
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
379
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
393
394
395
396
397
398
399
400
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
427
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
471
472
473
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
485
486
487
488
489
490
491
492
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
503
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
518 header, _, ok := strings.Cut(src, "\npackage")
519 if !ok {
520 header = action
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
541 switch action {
542 case "compile", "compiledir", "build", "builddir", "buildrundir", "run", "buildrun", "runoutput", "rundir", "runindir", "asmcheck":
543
544 case "errorcheckandrundir":
545 wantError = false
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
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":
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":
589 args = args[1:]
590 if goexp != "" {
591 goexp += ","
592 }
593 goexp += args[0]
594 runenv = append(runenv, "GOEXPERIMENT="+goexp)
595
596 case "-godebug":
597 args = args[1:]
598 if godebug != "" {
599 godebug += ","
600 }
601 godebug += args[0]
602 runenv = append(runenv, "GODEBUG="+godebug)
603
604 case "-gomodversion":
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
651 cmd.Env = append(cmd.Env, "PWD="+cmd.Dir)
652 } else {
653
654 cmd.Dir = t.gorootTestDir
655
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
669
670
671
672
673
674
675
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
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
725
726 ops := t.wantedAsmOpcodes(long)
727 self := runtime.GOOS + "/" + runtime.GOARCH
728 var lastErr error
729 for _, env := range ops.Envs() {
730
731
732 if string(env) != self && !strings.HasPrefix(string(env), self+"/") && !*allCodegen {
733 continue
734 }
735
736 cmdline := []string{"build", "-gcflags", "-S=2"}
737
738
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
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
784
785
786
787 cmdline := []string{goTool, "tool", "compile", "-p=p", "-d=panic", "-C", "-e", "-importcfg=" + stdlibImportcfgFile(), "-o", "a.o"}
788
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
811 _, err := compileFile(runcmd, long, flags)
812 return err
813
814 case "compiledir":
815
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
831
832
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
838
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
869
870
871
872 longdir := filepath.Join(t.gorootTestDir, t.goDirName())
873 pkgs := goDirPackages(t.T, longdir, singlefilepkgs)
874
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
889
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
914
915
916
917
918
919
920
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
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
962
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
1033
1034
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
1056
1057
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
1063
1064
1065
1066
1067
1068
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
1094
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
1126
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
1170
1171
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
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
1197
1198
1199 var res []string
1200 for _, line := range strings.Split(out, "\n") {
1201 if strings.HasSuffix(line, "\r") {
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
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
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
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
1264
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
1283
1284
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
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
1330 for i := range lines {
1331 lines[i], _, _ = strings.Cut(lines[i], " // ERROR ")
1332 }
1333
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
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)
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
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
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
1388 exec.Command(goTool, "fmt", file).CombinedOutput()
1389 }
1390
1391
1392
1393
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
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))`)
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
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
1503
1504
1505
1506 reMatchCheck = `(-|[1-9]\d*)?(?:\x60[^\x60]*\x60|"(?:[^"\\]|\\.)*")`
1507 )
1508
1509 var (
1510
1511 rxAsmComment = regexp.MustCompile(`^\s*(.*?)\s*(?://\s*(.+)\s*)?$`)
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528 rxAsmPlatform = regexp.MustCompile(`(\w+)(/[\w.]+)?(/\w*)?\s*:\s*(` + reMatchCheck + `(?:[\s,]+` + reMatchCheck + `)*)`)
1529
1530
1531 rxAsmCheck = regexp.MustCompile(reMatchCheck)
1532
1533
1534
1535
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": {},
1547 "s390x": {},
1548 "wasm": {},
1549 "riscv64": {"GORISCV64", "rva20u64", "rva22u64", "rva23u64"},
1550 }
1551 )
1552
1553
1554 type wantedAsmOpcode struct {
1555 fileline string
1556 line int
1557 opcode *regexp.Regexp
1558 expected int
1559 actual int
1560 negative bool
1561 found bool
1562 }
1563
1564
1565
1566 type buildEnv string
1567
1568
1569
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
1583
1584
1585
1586 type asmChecks map[buildEnv]map[string][]wantedAsmOpcode
1587
1588
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
1613
1614 comment += " " + cmt
1615 if code == "" {
1616 continue
1617 }
1618
1619
1620
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] != "":
1629 os, arch, subarch = archspec[0], archspec[1][1:], archspec[2][1:]
1630 case archspec[1] != "":
1631 os, arch, subarch = "linux", archspec[0], archspec[1][1:]
1632 default:
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
1644 envs := make([]buildEnv, 0, 4)
1645 arches := []string{arch}
1646
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
1685
1686
1687
1688
1689
1690
1691
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
1713
1714
1715
1716
1717
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
1735
1736
1737
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
1746 if len(line) > 0 && line[0] != '\t' {
1747 functionMarkers = append(functionMarkers, nl)
1748 }
1749
1750
1751
1752 matches := rxLine.FindStringSubmatch(line)
1753 if len(matches) == 0 {
1754 continue
1755 }
1756 srcFileLine, asm := matches[1], matches[2]
1757
1758
1759 asm = strings.ReplaceAll(asm, "\t", " ")
1760
1761
1762
1763
1764 lineFuncMap[srcFileLine] = len(functionMarkers) - 1
1765
1766
1767
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
1783
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
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
1803
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
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
1823
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
1848 assert(shouldTest("// +build linux", "linux", "arm"))
1849 assert(shouldTest("// +build !windows", "linux", "arm"))
1850 assertNot(shouldTest("// +build !windows", "windows", "amd64"))
1851
1852
1853 assert(shouldTest("// This is a test.", "os", "arch"))
1854
1855
1856 assertNot(shouldTest("// +build arm 386", "linux", "amd64"))
1857
1858
1859 assertNot(shouldTest("// +build !windows,!plan9", "windows", "amd64"))
1860 assertNot(shouldTest("// +build !windows,!plan9", "plan9", "386"))
1861
1862
1863 assert(shouldTest("// +build !windows\n// +build amd64", "linux", "amd64"))
1864 assertNot(shouldTest("// +build !windows\n// +build amd64", "windows", "amd64"))
1865
1866
1867 assert(shouldTest("// +build !windows !plan9", "windows", "amd64"))
1868
1869
1870 assert(shouldTest("//go:build go1.4", "linux", "amd64"))
1871 }
1872
1873
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
1908
1909 if info.IsDir() {
1910 return os.MkdirAll(dstPath, perm|0200)
1911 }
1912
1913
1914 if err := os.Symlink(srcPath, dstPath); err == nil {
1915 return nil
1916 }
1917
1918
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
1939
1940
1941
1942
1943
1944 var types2Failures = setOf(
1945 "shift1.go",
1946 "fixedbugs/issue10700.go",
1947 "fixedbugs/issue18331.go",
1948 "fixedbugs/issue18419.go",
1949 "fixedbugs/issue20233.go",
1950 "fixedbugs/issue20245.go",
1951 "fixedbugs/issue31053.go",
1952 )
1953
1954 var types2Failures32Bit = setOf(
1955 "printbig.go",
1956 "fixedbugs/bug114.go",
1957 "fixedbugs/issue23305.go",
1958 )
1959
1960
1961
1962
1963
1964 var _ = setOf(
1965 "import1.go",
1966 "initializerr.go",
1967 "typecheck.go",
1968
1969 "fixedbugs/bug176.go",
1970 "fixedbugs/bug195.go",
1971 "fixedbugs/bug412.go",
1972
1973 "fixedbugs/issue11614.go",
1974 "fixedbugs/issue17038.go",
1975 "fixedbugs/issue23732.go",
1976 "fixedbugs/issue4510.go",
1977 "fixedbugs/issue7525b.go",
1978 "fixedbugs/issue7525c.go",
1979 "fixedbugs/issue7525d.go",
1980 "fixedbugs/issue7525e.go",
1981 "fixedbugs/issue7525.go",
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
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
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
2055
2056
2057
2058
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