| 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 | "fmt" |
| 9 | "go/types" |
| 10 | "reflect" |
| 11 | "sort" |
| 12 | "strings" |
| 13 | "testing" |
| 14 | |
| 15 | "golang.org/x/tools/go/callgraph/cha" |
| 16 | "golang.org/x/tools/go/ssa" |
| 17 | "golang.org/x/tools/go/ssa/ssautil" |
| 18 | ) |
| 19 | |
| 20 | func TestNodeInterface(t *testing.T) { |
| 21 | // Since ssa package does not allow explicit creation of ssa |
| 22 | // values, we use the values from the program testdata/src/simple.go: |
| 23 | // - basic type int |
| 24 | // - struct X with two int fields a and b |
| 25 | // - global variable "gl" |
| 26 | // - "foo" function |
| 27 | // - "main" function and its |
| 28 | // - first register instruction t0 := *gl |
| 29 | prog, _, err := testProg(t, "testdata/src/simple.go", ssa.BuilderMode(0)) |
| 30 | if err != nil { |
| 31 | t.Fatalf("couldn't load testdata/src/simple.go program: %v", err) |
| 32 | } |
| 33 | |
| 34 | pkg := prog.AllPackages()[0] |
| 35 | main := pkg.Func("main") |
| 36 | foo := pkg.Func("foo") |
| 37 | reg := firstRegInstr(main) // t0 := *gl |
| 38 | X := pkg.Type("X").Type() |
| 39 | gl := pkg.Var("gl") |
| 40 | glPtrType, ok := types.Unalias(gl.Type()).(*types.Pointer) |
| 41 | if !ok { |
| 42 | t.Fatalf("could not cast gl variable to pointer type") |
| 43 | } |
| 44 | bint := glPtrType.Elem() |
| 45 | |
| 46 | pint := types.NewPointer(bint) |
| 47 | i := types.NewInterface(nil, nil) |
| 48 | |
| 49 | voidFunc := main.Signature.Underlying() |
| 50 | |
| 51 | for _, test := range []struct { |
| 52 | n node |
| 53 | s string |
| 54 | t types.Type |
| 55 | }{ |
| 56 | {constant{typ: bint}, "Constant(int)", bint}, |
| 57 | {pointer{typ: pint}, "Pointer(*int)", pint}, |
| 58 | {mapKey{typ: bint}, "MapKey(int)", bint}, |
| 59 | {mapValue{typ: pint}, "MapValue(*int)", pint}, |
| 60 | {sliceElem{typ: bint}, "Slice([]int)", bint}, |
| 61 | {channelElem{typ: pint}, "Channel(chan *int)", pint}, |
| 62 | {field{StructType: X, index: 0}, "Field(testdata.X:a)", bint}, |
| 63 | {field{StructType: X, index: 1}, "Field(testdata.X:b)", bint}, |
| 64 | {global{val: gl}, "Global(gl)", gl.Type()}, |
| 65 | {local{val: reg}, "Local(t0)", bint}, |
| 66 | {indexedLocal{val: reg, typ: X, index: 0}, "Local(t0[0])", X}, |
| 67 | {function{f: main}, "Function(main)", voidFunc}, |
| 68 | {resultVar{f: foo, index: 0}, "Return(foo[r])", bint}, |
| 69 | {nestedPtrInterface{typ: i}, "PtrInterface(interface{})", i}, |
| 70 | {nestedPtrFunction{typ: voidFunc}, "PtrFunction(func())", voidFunc}, |
| 71 | {panicArg{}, "Panic", nil}, |
| 72 | {recoverReturn{}, "Recover", nil}, |
| 73 | } { |
| 74 | if removeModulePrefix(test.s) != removeModulePrefix(test.n.String()) { |
| 75 | t.Errorf("want %s; got %s", removeModulePrefix(test.s), removeModulePrefix(test.n.String())) |
| 76 | } |
| 77 | if test.t != test.n.Type() { |
| 78 | t.Errorf("want %s; got %s", test.t, test.n.Type()) |
| 79 | } |
| 80 | } |
| 81 | } |
| 82 | |
| 83 | // removeModulePrefix removes the "x.io/" module name prefix throughout s. |
| 84 | // (It is added by testProg.) |
| 85 | func removeModulePrefix(s string) string { |
| 86 | return strings.ReplaceAll(s, "x.io/", "") |
| 87 | } |
| 88 | |
| 89 | func TestVtaGraph(t *testing.T) { |
| 90 | // Get the basic type int from a real program. |
| 91 | prog, _, err := testProg(t, "testdata/src/simple.go", ssa.BuilderMode(0)) |
| 92 | if err != nil { |
| 93 | t.Fatalf("couldn't load testdata/src/simple.go program: %v", err) |
| 94 | } |
| 95 | |
| 96 | glPtrType, ok := prog.AllPackages()[0].Var("gl").Type().(*types.Pointer) |
| 97 | if !ok { |
| 98 | t.Fatalf("could not cast gl variable to pointer type") |
| 99 | } |
| 100 | bint := glPtrType.Elem() |
| 101 | |
| 102 | n1 := constant{typ: bint} |
| 103 | n2 := pointer{typ: types.NewPointer(bint)} |
| 104 | n3 := mapKey{typ: types.NewMap(bint, bint)} |
| 105 | n4 := mapValue{typ: types.NewMap(bint, bint)} |
| 106 | |
| 107 | // Create graph |
| 108 | // n1 n2 |
| 109 | // \ / / |
| 110 | // n3 / |
| 111 | // | / |
| 112 | // n4 |
| 113 | var g vtaGraph |
| 114 | g.addEdge(n1, n3) |
| 115 | g.addEdge(n2, n3) |
| 116 | g.addEdge(n3, n4) |
| 117 | g.addEdge(n2, n4) |
| 118 | // for checking duplicates |
| 119 | g.addEdge(n1, n3) |
| 120 | |
| 121 | want := vtaGraph{ |
| 122 | m: []map[idx]empty{ |
| 123 | map[idx]empty{1: empty{}}, |
| 124 | map[idx]empty{3: empty{}}, |
| 125 | map[idx]empty{1: empty{}, 3: empty{}}, |
| 126 | nil, |
| 127 | }, |
| 128 | idx: map[node]idx{ |
| 129 | n1: 0, |
| 130 | n3: 1, |
| 131 | n2: 2, |
| 132 | n4: 3, |
| 133 | }, |
| 134 | node: []node{n1, n3, n2, n4}, |
| 135 | } |
| 136 | |
| 137 | if !reflect.DeepEqual(want, g) { |
| 138 | t.Errorf("want %v; got %v", want, g) |
| 139 | } |
| 140 | |
| 141 | for _, test := range []struct { |
| 142 | n node |
| 143 | l int |
| 144 | }{ |
| 145 | {n1, 1}, |
| 146 | {n2, 2}, |
| 147 | {n3, 1}, |
| 148 | {n4, 0}, |
| 149 | } { |
| 150 | sl := 0 |
| 151 | for range g.successors(g.idx[test.n]) { |
| 152 | sl++ |
| 153 | } |
| 154 | if sl != test.l { |
| 155 | t.Errorf("want %d successors; got %d", test.l, sl) |
| 156 | } |
| 157 | } |
| 158 | } |
| 159 | |
| 160 | // vtaGraphStr stringifies vtaGraph into a list of strings |
| 161 | // where each string represents an edge set of the format |
| 162 | // node -> succ_1, ..., succ_n. succ_1, ..., succ_n are |
| 163 | // sorted in alphabetical order. |
| 164 | func vtaGraphStr(g *vtaGraph) []string { |
| 165 | var vgs []string |
| 166 | for n := 0; n < g.numNodes(); n++ { |
| 167 | var succStr []string |
| 168 | for s := range g.successors(idx(n)) { |
| 169 | succStr = append(succStr, g.node[s].String()) |
| 170 | } |
| 171 | |
| 172 | sort.Strings(succStr) |
| 173 | entry := fmt.Sprintf("%v -> %v", g.node[n].String(), strings.Join(succStr, ", ")) |
| 174 | vgs = append(vgs, removeModulePrefix(entry)) |
| 175 | } |
| 176 | return vgs |
| 177 | } |
| 178 | |
| 179 | // setdiff returns the set difference of `X-Y` or {s | s ∈ X, s ∉ Y }. |
| 180 | func setdiff(X, Y []string) []string { |
| 181 | y := make(map[string]bool) |
| 182 | var delta []string |
| 183 | for _, s := range Y { |
| 184 | y[s] = true |
| 185 | } |
| 186 | |
| 187 | for _, s := range X { |
| 188 | if _, ok := y[s]; !ok { |
| 189 | delta = append(delta, s) |
| 190 | } |
| 191 | } |
| 192 | sort.Strings(delta) |
| 193 | return delta |
| 194 | } |
| 195 | |
| 196 | func TestVTAGraphConstruction(t *testing.T) { |
| 197 | for _, file := range []string{ |
| 198 | "testdata/src/store.go", |
| 199 | "testdata/src/phi.go", |
| 200 | "testdata/src/type_conversions.go", |
| 201 | "testdata/src/type_assertions.go", |
| 202 | "testdata/src/fields.go", |
| 203 | "testdata/src/node_uniqueness.go", |
| 204 | "testdata/src/store_load_alias.go", |
| 205 | "testdata/src/phi_alias.go", |
| 206 | "testdata/src/channels.go", |
| 207 | "testdata/src/generic_channels.go", |
| 208 | "testdata/src/select.go", |
| 209 | "testdata/src/stores_arrays.go", |
| 210 | "testdata/src/maps.go", |
| 211 | "testdata/src/ranges.go", |
| 212 | "testdata/src/closures.go", |
| 213 | "testdata/src/function_alias.go", |
| 214 | "testdata/src/static_calls.go", |
| 215 | "testdata/src/dynamic_calls.go", |
| 216 | "testdata/src/returns.go", |
| 217 | "testdata/src/panic.go", |
| 218 | } { |
| 219 | t.Run(file, func(t *testing.T) { |
| 220 | prog, want, err := testProg(t, file, ssa.BuilderMode(0)) |
| 221 | if err != nil { |
| 222 | t.Fatalf("couldn't load test file '%s': %s", file, err) |
| 223 | } |
| 224 | if len(want) == 0 { |
| 225 | t.Fatalf("couldn't find want in `%s`", file) |
| 226 | } |
| 227 | |
| 228 | fs := ssautil.AllFunctions(prog) |
| 229 | |
| 230 | // First test propagation with lazy-CHA initial call graph. |
| 231 | g, _ := typePropGraph(fs, makeCalleesFunc(fs, nil)) |
| 232 | got := vtaGraphStr(g) |
| 233 | if diff := setdiff(want, got); len(diff) > 0 { |
| 234 | t.Errorf("`%s`: want superset of %v;\n got %v\ndiff: %v", file, want, got, diff) |
| 235 | } |
| 236 | |
| 237 | // Repeat the test with explicit CHA initial call graph. |
| 238 | g, _ = typePropGraph(fs, makeCalleesFunc(fs, cha.CallGraph(prog))) |
| 239 | got = vtaGraphStr(g) |
| 240 | if diff := setdiff(want, got); len(diff) > 0 { |
| 241 | t.Errorf("`%s`: want superset of %v;\n got %v\ndiff: %v", file, want, got, diff) |
| 242 | } |
| 243 | }) |
| 244 | } |
| 245 | } |