| 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 | "go/token" |
| 9 | "go/types" |
| 10 | "math" |
| 11 | "reflect" |
| 12 | "slices" |
| 13 | "sort" |
| 14 | "strings" |
| 15 | "testing" |
| 16 | |
| 17 | "golang.org/x/tools/go/ssa" |
| 18 | "golang.org/x/tools/go/ssa/ssautil" |
| 19 | "golang.org/x/tools/internal/testfiles" |
| 20 | "golang.org/x/tools/txtar" |
| 21 | |
| 22 | "golang.org/x/tools/go/types/typeutil" |
| 23 | ) |
| 24 | |
| 25 | // val is a test data structure for creating ssa.Value |
| 26 | // outside of the ssa package. Needed for manual creation |
| 27 | // of vta graph nodes in testing. |
| 28 | type val struct { |
| 29 | name string |
| 30 | typ types.Type |
| 31 | } |
| 32 | |
| 33 | func (v val) String() string { |
| 34 | return v.name |
| 35 | } |
| 36 | |
| 37 | func (v val) Name() string { |
| 38 | return v.name |
| 39 | } |
| 40 | |
| 41 | func (v val) Type() types.Type { |
| 42 | return v.typ |
| 43 | } |
| 44 | |
| 45 | func (v val) Parent() *ssa.Function { |
| 46 | return nil |
| 47 | } |
| 48 | |
| 49 | func (v val) Referrers() *[]ssa.Instruction { |
| 50 | return nil |
| 51 | } |
| 52 | |
| 53 | func (v val) Pos() token.Pos { |
| 54 | return token.NoPos |
| 55 | } |
| 56 | |
| 57 | // newLocal creates a new local node with ssa.Value |
| 58 | // named `name` and type `t`. |
| 59 | func newLocal(name string, t types.Type) local { |
| 60 | return local{val: val{name: name, typ: t}} |
| 61 | } |
| 62 | |
| 63 | // sccString is a utility for stringifying `nodeToScc`. Every |
| 64 | // scc is represented as a string where string representation |
| 65 | // of scc nodes are sorted and concatenated using `;`. |
| 66 | func sccString(sccs [][]idx, g *vtaGraph) []string { |
| 67 | var sccsStr []string |
| 68 | for _, scc := range sccs { |
| 69 | var nodesStr []string |
| 70 | for _, idx := range scc { |
| 71 | nodesStr = append(nodesStr, g.node[idx].String()) |
| 72 | } |
| 73 | sort.Strings(nodesStr) |
| 74 | sccsStr = append(sccsStr, strings.Join(nodesStr, ";")) |
| 75 | } |
| 76 | return sccsStr |
| 77 | } |
| 78 | |
| 79 | // nodeToTypeString is testing utility for stringifying results |
| 80 | // of type propagation: propTypeMap `pMap` is converted to a map |
| 81 | // from node strings to a string consisting of type stringifications |
| 82 | // concatenated with `;`. We stringify reachable type information |
| 83 | // that also has an accompanying function by the function name. |
| 84 | func nodeToTypeString(pMap propTypeMap) map[string]string { |
| 85 | // Convert propType to a string. If propType has |
| 86 | // an attached function, return the function name. |
| 87 | // Otherwise, return the type name. |
| 88 | propTypeString := func(p propType) string { |
| 89 | if p.f != nil { |
| 90 | return p.f.Name() |
| 91 | } |
| 92 | return p.typ.String() |
| 93 | } |
| 94 | |
| 95 | nodeToTypeStr := make(map[string]string) |
| 96 | for node := range pMap { |
| 97 | var propStrings []string |
| 98 | for prop := range pMap.propTypes(node) { |
| 99 | s := propTypeString(prop) |
| 100 | s = strings.ReplaceAll(s, "example.com.", "") |
| 101 | propStrings = append(propStrings, s) |
| 102 | } |
| 103 | sort.Strings(propStrings) |
| 104 | nodeToTypeStr[node.String()] = strings.Join(propStrings, ";") |
| 105 | } |
| 106 | |
| 107 | return nodeToTypeStr |
| 108 | } |
| 109 | |
| 110 | // sccEqual compares two sets of SCC stringifications. |
| 111 | func sccEqual(sccs1 []string, sccs2 []string) bool { |
| 112 | if len(sccs1) != len(sccs2) { |
| 113 | return false |
| 114 | } |
| 115 | sort.Strings(sccs1) |
| 116 | sort.Strings(sccs2) |
| 117 | return reflect.DeepEqual(sccs1, sccs2) |
| 118 | } |
| 119 | |
| 120 | // isRevTopSorted checks if sccs of `g` are sorted in reverse |
| 121 | // topological order: |
| 122 | // |
| 123 | // for every edge x -> y in g, nodeToScc[x] > nodeToScc[y] |
| 124 | func isRevTopSorted(g *vtaGraph, idxToScc []int) bool { |
| 125 | for n := range idxToScc { |
| 126 | for s := range g.successors(idx(n)) { |
| 127 | if idxToScc[n] < idxToScc[s] { |
| 128 | return false |
| 129 | } |
| 130 | } |
| 131 | } |
| 132 | return true |
| 133 | } |
| 134 | |
| 135 | func sccMapsConsistent(sccs [][]idx, idxToSccID []int) bool { |
| 136 | for id, scc := range sccs { |
| 137 | for _, idx := range scc { |
| 138 | if idxToSccID[idx] != id { |
| 139 | return false |
| 140 | } |
| 141 | } |
| 142 | } |
| 143 | for i, id := range idxToSccID { |
| 144 | if !slices.Contains(sccs[id], idx(i)) { |
| 145 | return false |
| 146 | } |
| 147 | } |
| 148 | return true |
| 149 | } |
| 150 | |
| 151 | // testSuite produces a named set of graphs as follows, where |
| 152 | // parentheses contain node types and F nodes stand for function |
| 153 | // nodes whose content is function named F: |
| 154 | // |
| 155 | // no-cycles: |
| 156 | // t0 (A) -> t1 (B) -> t2 (C) |
| 157 | // |
| 158 | // trivial-cycle: |
| 159 | // <-------- <-------- |
| 160 | // | | | | |
| 161 | // t0 (A) -> t1 (B) -> |
| 162 | // |
| 163 | // circle-cycle: |
| 164 | // t0 (A) -> t1 (A) -> t2 (B) |
| 165 | // | | |
| 166 | // <-------------------- |
| 167 | // |
| 168 | // fully-connected: |
| 169 | // t0 (A) <-> t1 (B) |
| 170 | // \ / |
| 171 | // t2(C) |
| 172 | // |
| 173 | // subsumed-scc: |
| 174 | // t0 (A) -> t1 (B) -> t2(B) -> t3 (A) |
| 175 | // | | | | |
| 176 | // | <--------- | |
| 177 | // <----------------------------- |
| 178 | // |
| 179 | // more-realistic: |
| 180 | // <-------- |
| 181 | // | | |
| 182 | // t0 (A) --> |
| 183 | // ----------> |
| 184 | // | | |
| 185 | // t1 (A) -> t2 (B) -> F1 -> F2 -> F3 -> F4 |
| 186 | // | | | | |
| 187 | // <------- <------------ |
| 188 | func testSuite(t *testing.T) map[string]*vtaGraph { |
| 189 | ar := txtar.Parse([]byte(`-- go.mod -- |
| 190 | module example.com |
| 191 | go 1.24 |
| 192 | |
| 193 | -- p.go -- |
| 194 | package p |
| 195 | type A struct{} |
| 196 | type B struct{} |
| 197 | type C struct{} |
| 198 | func F1() |
| 199 | func F2() |
| 200 | func F3() |
| 201 | func F4() |
| 202 | `)) |
| 203 | ppkgs := testfiles.LoadPackages(t, ar, ".") |
| 204 | if len(ppkgs) != 1 { |
| 205 | t.Fatalf("LoadPackages returned %d packages, want 1", len(ppkgs)) |
| 206 | } |
| 207 | _, ssapkgs := ssautil.Packages(ppkgs, ssa.BuilderMode(0)) |
| 208 | pkg := ssapkgs[0] |
| 209 | |
| 210 | a := pkg.Type("A").Type().(*types.Named) |
| 211 | b := pkg.Type("B").Type().(*types.Named) |
| 212 | c := pkg.Type("C").Type().(*types.Named) |
| 213 | f1 := pkg.Func("F1") |
| 214 | f2 := pkg.Func("F2") |
| 215 | f3 := pkg.Func("F3") |
| 216 | f4 := pkg.Func("F4") |
| 217 | |
| 218 | graphs := make(map[string]*vtaGraph) |
| 219 | v := &vtaGraph{} |
| 220 | graphs["no-cycles"] = v |
| 221 | v.addEdge(newLocal("t0", a), newLocal("t1", b)) |
| 222 | v.addEdge(newLocal("t1", b), newLocal("t2", c)) |
| 223 | |
| 224 | v = &vtaGraph{} |
| 225 | graphs["trivial-cycle"] = v |
| 226 | v.addEdge(newLocal("t0", a), newLocal("t0", a)) |
| 227 | v.addEdge(newLocal("t1", b), newLocal("t1", b)) |
| 228 | |
| 229 | v = &vtaGraph{} |
| 230 | graphs["circle-cycle"] = v |
| 231 | v.addEdge(newLocal("t0", a), newLocal("t1", a)) |
| 232 | v.addEdge(newLocal("t1", a), newLocal("t2", b)) |
| 233 | v.addEdge(newLocal("t2", b), newLocal("t0", a)) |
| 234 | |
| 235 | v = &vtaGraph{} |
| 236 | graphs["fully-connected"] = v |
| 237 | v.addEdge(newLocal("t0", a), newLocal("t1", b)) |
| 238 | v.addEdge(newLocal("t0", a), newLocal("t2", c)) |
| 239 | v.addEdge(newLocal("t1", b), newLocal("t0", a)) |
| 240 | v.addEdge(newLocal("t1", b), newLocal("t2", c)) |
| 241 | v.addEdge(newLocal("t2", c), newLocal("t0", a)) |
| 242 | v.addEdge(newLocal("t2", c), newLocal("t1", b)) |
| 243 | |
| 244 | v = &vtaGraph{} |
| 245 | graphs["subsumed-scc"] = v |
| 246 | v.addEdge(newLocal("t0", a), newLocal("t1", b)) |
| 247 | v.addEdge(newLocal("t1", b), newLocal("t2", b)) |
| 248 | v.addEdge(newLocal("t2", b), newLocal("t1", b)) |
| 249 | v.addEdge(newLocal("t2", b), newLocal("t3", a)) |
| 250 | v.addEdge(newLocal("t3", a), newLocal("t0", a)) |
| 251 | |
| 252 | v = &vtaGraph{} |
| 253 | graphs["more-realistic"] = v |
| 254 | v.addEdge(newLocal("t0", a), newLocal("t0", a)) |
| 255 | v.addEdge(newLocal("t1", a), newLocal("t2", b)) |
| 256 | v.addEdge(newLocal("t2", b), newLocal("t1", a)) |
| 257 | v.addEdge(newLocal("t2", b), function{f1}) |
| 258 | v.addEdge(function{f1}, function{f2}) |
| 259 | v.addEdge(function{f1}, function{f3}) |
| 260 | v.addEdge(function{f2}, function{f3}) |
| 261 | v.addEdge(function{f3}, function{f1}) |
| 262 | v.addEdge(function{f3}, function{f4}) |
| 263 | |
| 264 | return graphs |
| 265 | } |
| 266 | |
| 267 | func TestSCC(t *testing.T) { |
| 268 | suite := testSuite(t) |
| 269 | for _, test := range []struct { |
| 270 | name string |
| 271 | graph *vtaGraph |
| 272 | want []string |
| 273 | }{ |
| 274 | // No cycles results in three separate SCCs: {t0} {t1} {t2} |
| 275 | {name: "no-cycles", graph: suite["no-cycles"], want: []string{"Local(t0)", "Local(t1)", "Local(t2)"}}, |
| 276 | // The two trivial self-loop cycles results in: {t0} {t1} |
| 277 | {name: "trivial-cycle", graph: suite["trivial-cycle"], want: []string{"Local(t0)", "Local(t1)"}}, |
| 278 | // The circle cycle produce a single SCC: {t0, t1, t2} |
| 279 | {name: "circle-cycle", graph: suite["circle-cycle"], want: []string{"Local(t0);Local(t1);Local(t2)"}}, |
| 280 | // Similar holds for fully connected SCC: {t0, t1, t2} |
| 281 | {name: "fully-connected", graph: suite["fully-connected"], want: []string{"Local(t0);Local(t1);Local(t2)"}}, |
| 282 | // Subsumed SCC also has a single SCC: {t0, t1, t2, t3} |
| 283 | {name: "subsumed-scc", graph: suite["subsumed-scc"], want: []string{"Local(t0);Local(t1);Local(t2);Local(t3)"}}, |
| 284 | // The more realistic example has the following SCCs: {t0} {t1, t2} {F1, F2, F3} {F4} |
| 285 | {name: "more-realistic", graph: suite["more-realistic"], want: []string{"Local(t0)", "Local(t1);Local(t2)", "Function(F1);Function(F2);Function(F3)", "Function(F4)"}}, |
| 286 | } { |
| 287 | sccs, idxToSccID := scc(test.graph) |
| 288 | if got := sccString(sccs, test.graph); !sccEqual(test.want, got) { |
| 289 | t.Errorf("want %v for graph %v; got %v", test.want, test.name, got) |
| 290 | } |
| 291 | if !isRevTopSorted(test.graph, idxToSccID) { |
| 292 | t.Errorf("%v not topologically sorted", test.name) |
| 293 | } |
| 294 | if !sccMapsConsistent(sccs, idxToSccID) { |
| 295 | t.Errorf("%v: scc maps not consistent", test.name) |
| 296 | } |
| 297 | break |
| 298 | } |
| 299 | } |
| 300 | |
| 301 | func TestPropagation(t *testing.T) { |
| 302 | suite := testSuite(t) |
| 303 | var canon typeutil.Map |
| 304 | for _, test := range []struct { |
| 305 | name string |
| 306 | graph *vtaGraph |
| 307 | want map[string]string |
| 308 | }{ |
| 309 | // No cycles graph pushes type information forward. |
| 310 | {name: "no-cycles", graph: suite["no-cycles"], |
| 311 | want: map[string]string{ |
| 312 | "Local(t0)": "A", |
| 313 | "Local(t1)": "A;B", |
| 314 | "Local(t2)": "A;B;C", |
| 315 | }, |
| 316 | }, |
| 317 | // No interesting type flow in trivial cycle graph. |
| 318 | {name: "trivial-cycle", graph: suite["trivial-cycle"], |
| 319 | want: map[string]string{ |
| 320 | "Local(t0)": "A", |
| 321 | "Local(t1)": "B", |
| 322 | }, |
| 323 | }, |
| 324 | // Circle cycle makes type A and B get propagated everywhere. |
| 325 | {name: "circle-cycle", graph: suite["circle-cycle"], |
| 326 | want: map[string]string{ |
| 327 | "Local(t0)": "A;B", |
| 328 | "Local(t1)": "A;B", |
| 329 | "Local(t2)": "A;B", |
| 330 | }, |
| 331 | }, |
| 332 | // Similarly for fully connected graph. |
| 333 | {name: "fully-connected", graph: suite["fully-connected"], |
| 334 | want: map[string]string{ |
| 335 | "Local(t0)": "A;B;C", |
| 336 | "Local(t1)": "A;B;C", |
| 337 | "Local(t2)": "A;B;C", |
| 338 | }, |
| 339 | }, |
| 340 | // The outer loop of subsumed-scc pushes A and B through the graph. |
| 341 | {name: "subsumed-scc", graph: suite["subsumed-scc"], |
| 342 | want: map[string]string{ |
| 343 | "Local(t0)": "A;B", |
| 344 | "Local(t1)": "A;B", |
| 345 | "Local(t2)": "A;B", |
| 346 | "Local(t3)": "A;B", |
| 347 | }, |
| 348 | }, |
| 349 | // More realistic graph has a more fine grained flow. |
| 350 | {name: "more-realistic", graph: suite["more-realistic"], |
| 351 | want: map[string]string{ |
| 352 | "Local(t0)": "A", |
| 353 | "Local(t1)": "A;B", |
| 354 | "Local(t2)": "A;B", |
| 355 | "Function(F1)": "A;B;F1;F2;F3", |
| 356 | "Function(F2)": "A;B;F1;F2;F3", |
| 357 | "Function(F3)": "A;B;F1;F2;F3", |
| 358 | "Function(F4)": "A;B;F1;F2;F3;F4", |
| 359 | }, |
| 360 | }, |
| 361 | } { |
| 362 | if got := nodeToTypeString(propagate(test.graph, &canon)); !reflect.DeepEqual(got, test.want) { |
| 363 | t.Errorf("want %v for graph %v; got %v", test.want, test.name, got) |
| 364 | } |
| 365 | } |
| 366 | } |
| 367 | |
| 368 | func testLastIndex[S ~[]E, E comparable](t *testing.T, s S, e E, want int) { |
| 369 | if got := slicesLastIndex(s, e); got != want { |
| 370 | t.Errorf("LastIndex(%v, %v): got %v want %v", s, e, got, want) |
| 371 | } |
| 372 | } |
| 373 | |
| 374 | func TestLastIndex(t *testing.T) { |
| 375 | testLastIndex(t, []int{10, 20, 30}, 10, 0) |
| 376 | testLastIndex(t, []int{10, 20, 30}, 20, 1) |
| 377 | testLastIndex(t, []int{10, 20, 30}, 30, 2) |
| 378 | testLastIndex(t, []int{10, 20, 30}, 42, -1) |
| 379 | testLastIndex(t, []int{10, 20, 10}, 10, 2) |
| 380 | testLastIndex(t, []int{20, 10, 10}, 10, 2) |
| 381 | testLastIndex(t, []int{10, 10, 20}, 10, 1) |
| 382 | type foo struct { |
| 383 | i int |
| 384 | s string |
| 385 | } |
| 386 | testLastIndex(t, []foo{{1, "abc"}, {2, "abc"}, {1, "xyz"}}, foo{1, "abc"}, 0) |
| 387 | // Test that LastIndex doesn't use bitwise comparisons for floats. |
| 388 | neg0 := 1 / math.Inf(-1) |
| 389 | nan := math.NaN() |
| 390 | testLastIndex(t, []float64{0, neg0}, 0, 1) |
| 391 | testLastIndex(t, []float64{0, neg0}, neg0, 1) |
| 392 | testLastIndex(t, []float64{neg0, 0}, 0, 1) |
| 393 | testLastIndex(t, []float64{neg0, 0}, neg0, 1) |
| 394 | testLastIndex(t, []float64{0, nan}, 0, 0) |
| 395 | testLastIndex(t, []float64{0, nan}, nan, -1) |
| 396 | testLastIndex(t, []float64{0, nan}, 1, -1) |
| 397 | } |