Debian Codegraph @

go/callgraph/vta/vta_test.go

raw symbols SCIP
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 vta
6
7 import (
8 "strings"
9 "testing"
10
11 "github.com/google/go-cmp/cmp"
12 "golang.org/x/tools/go/analysis"
13 "golang.org/x/tools/go/analysis/analysistest"
14 "golang.org/x/tools/go/analysis/passes/buildssa"
15 "golang.org/x/tools/go/callgraph/cha"
16 "golang.org/x/tools/go/ssa"
17 "golang.org/x/tools/go/ssa/ssautil"
18 "golang.org/x/tools/internal/testenv"
19 )
20
21 func TestVTACallGraph(t *testing.T) {
22 errDiff := func(t *testing.T, want, got, missing []string) {
23 t.Errorf("got:\n%s\n\nwant:\n%s\n\nmissing:\n%s\n\ndiff:\n%s",
24 strings.Join(got, "\n"),
25 strings.Join(want, "\n"),
26 strings.Join(missing, "\n"),
27 cmp.Diff(got, want)) // to aid debugging
28 }
29
30 files := []string{
31 "testdata/src/callgraph_static.go",
32 "testdata/src/callgraph_ho.go",
33 "testdata/src/callgraph_interfaces.go",
34 "testdata/src/callgraph_pointers.go",
35 "testdata/src/callgraph_collections.go",
36 "testdata/src/callgraph_fields.go",
37 "testdata/src/callgraph_field_funcs.go",
38 "testdata/src/callgraph_recursive_types.go",
39 "testdata/src/callgraph_issue_57756.go",
40 "testdata/src/callgraph_comma_maps.go",
41 "testdata/src/callgraph_type_aliases.go", // https://github.com/golang/go/issues/68799
42 }
43 if testenv.Go1Point() >= 23 {
44 files = append(files, "testdata/src/callgraph_range_over_func.go")
45 }
46
47 for _, file := range files {
48 t.Run(file, func(t *testing.T) {
49 prog, want, err := testProg(t, file, ssa.BuilderMode(0))
50 if err != nil {
51 t.Fatalf("couldn't load test file '%s': %s", file, err)
52 }
53 if len(want) == 0 {
54 t.Fatalf("couldn't find want in `%s`", file)
55 }
56
57 // First test VTA with lazy-CHA initial call graph.
58 g := CallGraph(ssautil.AllFunctions(prog), nil)
59 got := callGraphStr(g)
60 if missing := setdiff(want, got); len(missing) > 0 {
61 errDiff(t, want, got, missing)
62 }
63
64 // Repeat the test with explicit CHA initial call graph.
65 g = CallGraph(ssautil.AllFunctions(prog), cha.CallGraph(prog))
66 got = callGraphStr(g)
67 if missing := setdiff(want, got); len(missing) > 0 {
68 errDiff(t, want, got, missing)
69 }
70 })
71 }
72 }
73
74 // TestVTAProgVsFuncSet exemplifies and tests different possibilities
75 // enabled by having an arbitrary function set as input to CallGraph
76 // instead of the whole program (i.e., ssautil.AllFunctions(prog)).
77 func TestVTAProgVsFuncSet(t *testing.T) {
78 prog, want, err := testProg(t, "testdata/src/callgraph_nested_ptr.go", ssa.BuilderMode(0))
79 if err != nil {
80 t.Fatalf("couldn't load test `testdata/src/callgraph_nested_ptr.go`: %s", err)
81 }
82 if len(want) == 0 {
83 t.Fatal("couldn't find want in `testdata/src/callgraph_nested_ptr.go`")
84 }
85
86 allFuncs := ssautil.AllFunctions(prog)
87 g := CallGraph(allFuncs, cha.CallGraph(prog))
88 // VTA over the whole program will produce a call graph that
89 // includes Baz:(**i).Foo -> A.Foo, B.Foo.
90 got := callGraphStr(g)
91 if diff := setdiff(want, got); len(diff) > 0 {
92 t.Errorf("computed callgraph %v should contain %v (diff: %v)", got, want, diff)
93 }
94
95 // Prune the set of program functions to exclude Bar(). This should
96 // yield a call graph that includes different set of callees for Baz
97 // Baz:(**i).Foo -> A.Foo
98 //
99 // Note that the exclusion of Bar can happen, for instance, if Baz is
100 // considered an entry point of some data flow analysis and Bar is
101 // provably (e.g., using CHA forward reachability) unreachable from Baz.
102 noBarFuncs := make(map[*ssa.Function]bool)
103 for f, in := range allFuncs {
104 noBarFuncs[f] = in && (funcName(f) != "Bar")
105 }
106 want = []string{"Baz: Do(i) -> Do; invoke t2.Foo() -> A.Foo"}
107 g = CallGraph(noBarFuncs, cha.CallGraph(prog))
108 got = callGraphStr(g)
109 if diff := setdiff(want, got); len(diff) > 0 {
110 t.Errorf("pruned callgraph %v should contain %v (diff: %v)", got, want, diff)
111 }
112 }
113
114 // TestVTAPanicMissingDefinitions tests if VTA gracefully handles the case
115 // where VTA panics when a definition of a function or method is not
116 // available, which can happen when using analysis package. A successful
117 // test simply does not panic.
118 func TestVTAPanicMissingDefinitions(t *testing.T) {
119 run := func(pass *analysis.Pass) (any, error) {
120 s := pass.ResultOf[buildssa.Analyzer].(*buildssa.SSA)
121 CallGraph(ssautil.AllFunctions(s.Pkg.Prog), cha.CallGraph(s.Pkg.Prog))
122 return nil, nil
123 }
124
125 analyzer := &analysis.Analyzer{
126 Name: "test",
127 Doc: "test",
128 Run: run,
129 Requires: []*analysis.Analyzer{
130 buildssa.Analyzer,
131 },
132 }
133
134 testdata := analysistest.TestData()
135 res := analysistest.Run(t, testdata, analyzer, "t", "d")
136 if len(res) != 2 {
137 t.Errorf("want analysis results for 2 packages; got %v", len(res))
138 }
139 for _, r := range res {
140 if r.Err != nil {
141 t.Errorf("want no error for package %v; got %v", r.Action.Package.Types.Path(), r.Err)
142 }
143 }
144 }
145
146 func TestVTACallGraphGenerics(t *testing.T) {
147 // TODO(zpavlinovic): add more tests
148 files := []string{
149 "testdata/src/arrays_generics.go",
150 "testdata/src/callgraph_generics.go",
151 "testdata/src/issue63146.go",
152 }
153 for _, file := range files {
154 t.Run(file, func(t *testing.T) {
155 prog, want, err := testProg(t, file, ssa.InstantiateGenerics)
156 if err != nil {
157 t.Fatalf("couldn't load test file '%s': %s", file, err)
158 }
159 if len(want) == 0 {
160 t.Fatalf("couldn't find want in `%s`", file)
161 }
162
163 g := CallGraph(ssautil.AllFunctions(prog), cha.CallGraph(prog))
164 got := callGraphStr(g)
165 if diff := setdiff(want, got); len(diff) != 0 {
166 t.Errorf("computed callgraph %v should contain %v (diff: %v)", got, want, diff)
167 logFns(t, prog)
168 }
169 })
170 }
171 }
172
173 func TestVTACallGraphGo117(t *testing.T) {
174 file := "testdata/src/go117.go"
175 prog, want, err := testProg(t, file, ssa.BuilderMode(0))
176 if err != nil {
177 t.Fatalf("couldn't load test file '%s': %s", file, err)
178 }
179 if len(want) == 0 {
180 t.Fatalf("couldn't find want in `%s`", file)
181 }
182
183 g, _ := typePropGraph(ssautil.AllFunctions(prog), makeCalleesFunc(nil, cha.CallGraph(prog)))
184 got := vtaGraphStr(g)
185 if diff := setdiff(want, got); len(diff) != 0 {
186 t.Errorf("`%s`: want superset of %v;\n got %v", file, want, got)
187 }
188 }