|
| 1 | +// Copyright 2021 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 ssa_test |
| 6 | + |
| 7 | +import ( |
| 8 | + "bufio" |
| 9 | + "bytes" |
| 10 | + "flag" |
| 11 | + "runtime" |
| 12 | + "sort" |
| 13 | + |
| 14 | + // "flag" |
| 15 | + "fmt" |
| 16 | + "internal/testenv" |
| 17 | + "io/ioutil" |
| 18 | + "os" |
| 19 | + "os/exec" |
| 20 | + "path/filepath" |
| 21 | + "reflect" |
| 22 | + "regexp" |
| 23 | + "strconv" |
| 24 | + "testing" |
| 25 | +) |
| 26 | + |
| 27 | +// Matches lines in genssa output that are marked "isstmt", and the parenthesized plus-prefixed line number is a submatch |
| 28 | +var asmLine *regexp.Regexp = regexp.MustCompile(`^\s[vb][0-9]+\s+[0-9]+\s\(\+([0-9]+)\)`) |
| 29 | + |
| 30 | +// this matches e.g. ` v123456789 000007 (+9876654310) MOVUPS X15, ""..autotmp_2-32(SP)` |
| 31 | + |
| 32 | +// Matches lines in genssa output that describe an inlined file (on a Unix filesystem). Note it expects an unadventurous choice of basename. |
| 33 | +var inlineLine *regexp.Regexp = regexp.MustCompile(`^#\s/.*/[-a-zA-Z0-9_]+\.go:([0-9]+)`) |
| 34 | + |
| 35 | +// this matches e.g. # /pa/inline-dumpxxxx.go:6 |
| 36 | + |
| 37 | +var testGoArchFlag = flag.String("arch", "", "run test for specified architecture") |
| 38 | + |
| 39 | +func testGoArch() string { |
| 40 | + if *testGoArchFlag == "" { |
| 41 | + return runtime.GOARCH |
| 42 | + } |
| 43 | + return *testGoArchFlag |
| 44 | +} |
| 45 | + |
| 46 | +func TestDebugLines(t *testing.T) { |
| 47 | + if runtime.GOOS == "windows" { |
| 48 | + t.Skip("Windows lacks $HOME which complicates workaround for 'missing $GOPATH'") // $HOME needed to work around #43938 |
| 49 | + } |
| 50 | + // This test is potentially fragile, the goal is that debugging should step properly through "sayhi" |
| 51 | + // If the blocks are reordered in a way that changes the statement order but execution flows correctly, |
| 52 | + // then rearrange the expected numbers. Register abi and not-register-abi also have different sequences, |
| 53 | + // at least for now. |
| 54 | + |
| 55 | + switch testGoArch() { |
| 56 | + case "arm64", "amd64": // register ABI |
| 57 | + testDebugLines(t, "sayhi.go", "sayhi", []int{8, 9, 10, 11}) |
| 58 | + |
| 59 | + case "arm", "386": // probably not register ABI for a while |
| 60 | + testDebugLines(t, "sayhi.go", "sayhi", []int{9, 10, 11}) |
| 61 | + |
| 62 | + default: // expect ppc64le and riscv will pick up register ABI soonish, not sure about others |
| 63 | + t.Skip("skipped for many architectures, also changes w/ register ABI") |
| 64 | + } |
| 65 | +} |
| 66 | + |
| 67 | +func TestInlineLines(t *testing.T) { |
| 68 | + if runtime.GOOS == "windows" { |
| 69 | + t.Skip("Windows lacks $HOME which complicates workaround for 'missing $GOPATH'") // $HOME needed to work around #43938 |
| 70 | + } |
| 71 | + if runtime.GOARCH != "amd64" && *testGoArchFlag == "" { |
| 72 | + // As of september 2021, works for everything except mips64, but still potentially fragile |
| 73 | + t.Skip("only runs for amd64 unless -arch explicitly supplied") |
| 74 | + } |
| 75 | + |
| 76 | + want := [][]int{{3}, {4, 10}, {4, 10, 16}, {4, 10}, {4, 11, 16}, {4, 11}, {4}, {5, 10}, {5, 10, 16}, {5, 10}, {5, 11, 16}, {5, 11}, {5}} |
| 77 | + testInlineStack(t, "inline-dump.go", "f", want) |
| 78 | +} |
| 79 | + |
| 80 | +func compileAndDump(t *testing.T, file, function, moreGCFlags string) []byte { |
| 81 | + testenv.MustHaveGoBuild(t) |
| 82 | + |
| 83 | + tmpdir, err := ioutil.TempDir("", "debug_lines_test") |
| 84 | + if err != nil { |
| 85 | + panic(fmt.Sprintf("Problem creating TempDir, error %v", err)) |
| 86 | + } |
| 87 | + if testing.Verbose() { |
| 88 | + fmt.Printf("Preserving temporary directory %s\n", tmpdir) |
| 89 | + } else { |
| 90 | + defer os.RemoveAll(tmpdir) |
| 91 | + } |
| 92 | + |
| 93 | + source, err := filepath.Abs(filepath.Join("testdata", file)) |
| 94 | + if err != nil { |
| 95 | + panic(fmt.Sprintf("Could not get abspath of testdata directory and file, %v", err)) |
| 96 | + } |
| 97 | + |
| 98 | + cmd := exec.Command(testenv.GoToolPath(t), "build", "-o", "foo.o", "-gcflags=-d=ssa/genssa/dump="+function+" "+moreGCFlags, source) |
| 99 | + cmd.Dir = tmpdir |
| 100 | + cmd.Env = replaceEnv(cmd.Env, "GOSSADIR", tmpdir) |
| 101 | + cmd.Env = replaceEnv(cmd.Env, "HOME", os.Getenv("HOME")) // workaround for #43938 |
| 102 | + testGoos := "linux" // default to linux |
| 103 | + if testGoArch() == "wasm" { |
| 104 | + testGoos = "js" |
| 105 | + } |
| 106 | + cmd.Env = replaceEnv(cmd.Env, "GOOS", testGoos) |
| 107 | + cmd.Env = replaceEnv(cmd.Env, "GOARCH", testGoArch()) |
| 108 | + |
| 109 | + if testing.Verbose() { |
| 110 | + fmt.Printf("About to run %s\n", asCommandLine("", cmd)) |
| 111 | + } |
| 112 | + |
| 113 | + var stdout, stderr bytes.Buffer |
| 114 | + cmd.Stdout = &stdout |
| 115 | + cmd.Stderr = &stderr |
| 116 | + |
| 117 | + if err := cmd.Run(); err != nil { |
| 118 | + t.Fatalf("error running cmd %s: %v\nstdout:\n%sstderr:\n%s\n", asCommandLine("", cmd), err, stdout.String(), stderr.String()) |
| 119 | + } |
| 120 | + |
| 121 | + if s := stderr.String(); s != "" { |
| 122 | + t.Fatalf("Wanted empty stderr, instead got:\n%s\n", s) |
| 123 | + } |
| 124 | + |
| 125 | + dumpFile := filepath.Join(tmpdir, function+"_01__genssa.dump") |
| 126 | + dumpBytes, err := os.ReadFile(dumpFile) |
| 127 | + if err != nil { |
| 128 | + t.Fatalf("Could not read dump file %s, err=%v", dumpFile, err) |
| 129 | + } |
| 130 | + return dumpBytes |
| 131 | +} |
| 132 | + |
| 133 | +func sortInlineStacks(x [][]int) { |
| 134 | + sort.Slice(x, func(i, j int) bool { |
| 135 | + if len(x[i]) != len(x[j]) { |
| 136 | + return len(x[i]) < len(x[j]) |
| 137 | + } |
| 138 | + for k := range x[i] { |
| 139 | + if x[i][k] != x[j][k] { |
| 140 | + return x[i][k] < x[j][k] |
| 141 | + } |
| 142 | + } |
| 143 | + return false |
| 144 | + }) |
| 145 | +} |
| 146 | + |
| 147 | +// testInlineStack ensures that inlining is described properly in the comments in the dump file |
| 148 | +func testInlineStack(t *testing.T, file, function string, wantStacks [][]int) { |
| 149 | + // this is an inlining reporting test, not an optimization test. -N makes it less fragile |
| 150 | + dumpBytes := compileAndDump(t, file, function, "-N") |
| 151 | + dump := bufio.NewScanner(bytes.NewReader(dumpBytes)) |
| 152 | + dumpLineNum := 0 |
| 153 | + var gotStmts []int |
| 154 | + var gotStacks [][]int |
| 155 | + for dump.Scan() { |
| 156 | + line := dump.Text() |
| 157 | + dumpLineNum++ |
| 158 | + matches := inlineLine.FindStringSubmatch(line) |
| 159 | + if len(matches) == 2 { |
| 160 | + stmt, err := strconv.ParseInt(matches[1], 10, 32) |
| 161 | + if err != nil { |
| 162 | + t.Fatalf("Expected to parse a line number but saw %s instead on dump line #%d, error %v", matches[1], dumpLineNum, err) |
| 163 | + } |
| 164 | + if testing.Verbose() { |
| 165 | + fmt.Printf("Saw stmt# %d for submatch '%s' on dump line #%d = '%s'\n", stmt, matches[1], dumpLineNum, line) |
| 166 | + } |
| 167 | + gotStmts = append(gotStmts, int(stmt)) |
| 168 | + } else if len(gotStmts) > 0 { |
| 169 | + gotStacks = append(gotStacks, gotStmts) |
| 170 | + gotStmts = nil |
| 171 | + } |
| 172 | + } |
| 173 | + if len(gotStmts) > 0 { |
| 174 | + gotStacks = append(gotStacks, gotStmts) |
| 175 | + gotStmts = nil |
| 176 | + } |
| 177 | + sortInlineStacks(gotStacks) |
| 178 | + sortInlineStacks(wantStacks) |
| 179 | + if !reflect.DeepEqual(wantStacks, gotStacks) { |
| 180 | + t.Errorf("wanted inlines %+v but got %+v", wantStacks, gotStacks) |
| 181 | + } |
| 182 | + |
| 183 | +} |
| 184 | + |
| 185 | +// testDebugLines compiles testdata/<file> with flags -N -l and -d=ssa/genssa/dump=<function> |
| 186 | +// then verifies that the statement-marked lines in that file are the same as those in wantStmts |
| 187 | +// These files must all be short because this is super-fragile. |
| 188 | +// "go build" is run in a temporary directory that is normally deleted, unless -test.v |
| 189 | +func testDebugLines(t *testing.T, file, function string, wantStmts []int) { |
| 190 | + dumpBytes := compileAndDump(t, file, function, "-N -l") |
| 191 | + dump := bufio.NewScanner(bytes.NewReader(dumpBytes)) |
| 192 | + var gotStmts []int |
| 193 | + dumpLineNum := 0 |
| 194 | + for dump.Scan() { |
| 195 | + line := dump.Text() |
| 196 | + dumpLineNum++ |
| 197 | + matches := asmLine.FindStringSubmatch(line) |
| 198 | + if len(matches) == 2 { |
| 199 | + stmt, err := strconv.ParseInt(matches[1], 10, 32) |
| 200 | + if err != nil { |
| 201 | + t.Fatalf("Expected to parse a line number but saw %s instead on dump line #%d, error %v", matches[1], dumpLineNum, err) |
| 202 | + } |
| 203 | + if testing.Verbose() { |
| 204 | + fmt.Printf("Saw stmt# %d for submatch '%s' on dump line #%d = '%s'\n", stmt, matches[1], dumpLineNum, line) |
| 205 | + } |
| 206 | + gotStmts = append(gotStmts, int(stmt)) |
| 207 | + } |
| 208 | + } |
| 209 | + if !reflect.DeepEqual(wantStmts, gotStmts) { |
| 210 | + t.Errorf("wanted stmts %v but got %v", wantStmts, gotStmts) |
| 211 | + } |
| 212 | + |
| 213 | +} |
0 commit comments