1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338
|
// Copyright 2021 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package vta
import (
"go/token"
"go/types"
"reflect"
"sort"
"strings"
"testing"
"unsafe"
"golang.org/x/tools/go/ssa"
"golang.org/x/tools/go/types/typeutil"
)
// val is a test data structure for creating ssa.Value
// outside of the ssa package. Needed for manual creation
// of vta graph nodes in testing.
type val struct {
name string
typ types.Type
}
func (v val) String() string {
return v.name
}
func (v val) Name() string {
return v.name
}
func (v val) Type() types.Type {
return v.typ
}
func (v val) Parent() *ssa.Function {
return nil
}
func (v val) Referrers() *[]ssa.Instruction {
return nil
}
func (v val) Pos() token.Pos {
return token.NoPos
}
// newLocal creates a new local node with ssa.Value
// named `name` and type `t`.
func newLocal(name string, t types.Type) local {
return local{val: val{name: name, typ: t}}
}
// newNamedType creates a bogus type named `name`.
func newNamedType(name string) *types.Named {
return types.NewNamed(types.NewTypeName(token.NoPos, nil, name, nil), types.Universe.Lookup("int").Type(), nil)
}
// sccString is a utility for stringifying `nodeToScc`. Every
// scc is represented as a string where string representation
// of scc nodes are sorted and concatenated using `;`.
func sccString(nodeToScc map[node]int) []string {
sccs := make(map[int][]node)
for n, id := range nodeToScc {
sccs[id] = append(sccs[id], n)
}
var sccsStr []string
for _, scc := range sccs {
var nodesStr []string
for _, node := range scc {
nodesStr = append(nodesStr, node.String())
}
sort.Strings(nodesStr)
sccsStr = append(sccsStr, strings.Join(nodesStr, ";"))
}
return sccsStr
}
// nodeToTypeString is testing utility for stringifying results
// of type propagation: propTypeMap `pMap` is converted to a map
// from node strings to a string consisting of type stringifications
// concatenated with `;`. We stringify reachable type information
// that also has an accompanying function by the function name.
func nodeToTypeString(pMap propTypeMap) map[string]string {
// Convert propType to a string. If propType has
// an attached function, return the function name.
// Otherwise, return the type name.
propTypeString := func(p propType) string {
if p.f != nil {
return p.f.Name()
}
return p.typ.String()
}
nodeToTypeStr := make(map[string]string)
for node := range pMap.nodeToScc {
var propStrings []string
pMap.propTypes(node)(func(prop propType) bool {
propStrings = append(propStrings, propTypeString(prop))
return true
})
sort.Strings(propStrings)
nodeToTypeStr[node.String()] = strings.Join(propStrings, ";")
}
return nodeToTypeStr
}
// sccEqual compares two sets of SCC stringifications.
func sccEqual(sccs1 []string, sccs2 []string) bool {
if len(sccs1) != len(sccs2) {
return false
}
sort.Strings(sccs1)
sort.Strings(sccs2)
return reflect.DeepEqual(sccs1, sccs2)
}
// isRevTopSorted checks if sccs of `g` are sorted in reverse
// topological order:
//
// for every edge x -> y in g, nodeToScc[x] > nodeToScc[y]
func isRevTopSorted(g vtaGraph, nodeToScc map[node]int) bool {
for n, succs := range g {
for s := range succs {
if nodeToScc[n] < nodeToScc[s] {
return false
}
}
}
return true
}
// setName sets name of the function `f` to `name`
// using reflection since setting the name otherwise
// is only possible within the ssa package.
func setName(f *ssa.Function, name string) {
fi := reflect.ValueOf(f).Elem().FieldByName("name")
fi = reflect.NewAt(fi.Type(), unsafe.Pointer(fi.UnsafeAddr())).Elem()
fi.SetString(name)
}
// testSuite produces a named set of graphs as follows, where
// parentheses contain node types and F nodes stand for function
// nodes whose content is function named F:
//
// no-cycles:
// t0 (A) -> t1 (B) -> t2 (C)
//
// trivial-cycle:
// <-------- <--------
// | | | |
// t0 (A) -> t1 (B) ->
//
// circle-cycle:
// t0 (A) -> t1 (A) -> t2 (B)
// | |
// <--------------------
//
// fully-connected:
// t0 (A) <-> t1 (B)
// \ /
// t2(C)
//
// subsumed-scc:
// t0 (A) -> t1 (B) -> t2(B) -> t3 (A)
// | | | |
// | <--------- |
// <-----------------------------
//
// more-realistic:
// <--------
// | |
// t0 (A) -->
// ---------->
// | |
// t1 (A) -> t2 (B) -> F1 -> F2 -> F3 -> F4
// | | | |
// <------- <------------
func testSuite() map[string]vtaGraph {
a := newNamedType("A")
b := newNamedType("B")
c := newNamedType("C")
sig := types.NewSignature(nil, types.NewTuple(), types.NewTuple(), false)
f1 := &ssa.Function{Signature: sig}
setName(f1, "F1")
f2 := &ssa.Function{Signature: sig}
setName(f2, "F2")
f3 := &ssa.Function{Signature: sig}
setName(f3, "F3")
f4 := &ssa.Function{Signature: sig}
setName(f4, "F4")
graphs := make(map[string]vtaGraph)
graphs["no-cycles"] = map[node]map[node]empty{
newLocal("t0", a): {newLocal("t1", b): empty{}},
newLocal("t1", b): {newLocal("t2", c): empty{}},
}
graphs["trivial-cycle"] = map[node]map[node]empty{
newLocal("t0", a): {newLocal("t0", a): empty{}},
newLocal("t1", b): {newLocal("t1", b): empty{}},
}
graphs["circle-cycle"] = map[node]map[node]empty{
newLocal("t0", a): {newLocal("t1", a): empty{}},
newLocal("t1", a): {newLocal("t2", b): empty{}},
newLocal("t2", b): {newLocal("t0", a): empty{}},
}
graphs["fully-connected"] = map[node]map[node]empty{
newLocal("t0", a): {newLocal("t1", b): empty{}, newLocal("t2", c): empty{}},
newLocal("t1", b): {newLocal("t0", a): empty{}, newLocal("t2", c): empty{}},
newLocal("t2", c): {newLocal("t0", a): empty{}, newLocal("t1", b): empty{}},
}
graphs["subsumed-scc"] = map[node]map[node]empty{
newLocal("t0", a): {newLocal("t1", b): empty{}},
newLocal("t1", b): {newLocal("t2", b): empty{}},
newLocal("t2", b): {newLocal("t1", b): empty{}, newLocal("t3", a): empty{}},
newLocal("t3", a): {newLocal("t0", a): empty{}},
}
graphs["more-realistic"] = map[node]map[node]empty{
newLocal("t0", a): {newLocal("t0", a): empty{}},
newLocal("t1", a): {newLocal("t2", b): empty{}},
newLocal("t2", b): {newLocal("t1", a): empty{}, function{f1}: empty{}},
function{f1}: {function{f2}: empty{}, function{f3}: empty{}},
function{f2}: {function{f3}: empty{}},
function{f3}: {function{f1}: empty{}, function{f4}: empty{}},
}
return graphs
}
func TestSCC(t *testing.T) {
suite := testSuite()
for _, test := range []struct {
name string
graph vtaGraph
want []string
}{
// No cycles results in three separate SCCs: {t0} {t1} {t2}
{name: "no-cycles", graph: suite["no-cycles"], want: []string{"Local(t0)", "Local(t1)", "Local(t2)"}},
// The two trivial self-loop cycles results in: {t0} {t1}
{name: "trivial-cycle", graph: suite["trivial-cycle"], want: []string{"Local(t0)", "Local(t1)"}},
// The circle cycle produce a single SCC: {t0, t1, t2}
{name: "circle-cycle", graph: suite["circle-cycle"], want: []string{"Local(t0);Local(t1);Local(t2)"}},
// Similar holds for fully connected SCC: {t0, t1, t2}
{name: "fully-connected", graph: suite["fully-connected"], want: []string{"Local(t0);Local(t1);Local(t2)"}},
// Subsumed SCC also has a single SCC: {t0, t1, t2, t3}
{name: "subsumed-scc", graph: suite["subsumed-scc"], want: []string{"Local(t0);Local(t1);Local(t2);Local(t3)"}},
// The more realistic example has the following SCCs: {t0} {t1, t2} {F1, F2, F3} {F4}
{name: "more-realistic", graph: suite["more-realistic"], want: []string{"Local(t0)", "Local(t1);Local(t2)", "Function(F1);Function(F2);Function(F3)", "Function(F4)"}},
} {
sccs, _ := scc(test.graph)
if got := sccString(sccs); !sccEqual(test.want, got) {
t.Errorf("want %v for graph %v; got %v", test.want, test.name, got)
}
if !isRevTopSorted(test.graph, sccs) {
t.Errorf("%v not topologically sorted", test.name)
}
}
}
func TestPropagation(t *testing.T) {
suite := testSuite()
var canon typeutil.Map
for _, test := range []struct {
name string
graph vtaGraph
want map[string]string
}{
// No cycles graph pushes type information forward.
{name: "no-cycles", graph: suite["no-cycles"],
want: map[string]string{
"Local(t0)": "A",
"Local(t1)": "A;B",
"Local(t2)": "A;B;C",
},
},
// No interesting type flow in trivial cycle graph.
{name: "trivial-cycle", graph: suite["trivial-cycle"],
want: map[string]string{
"Local(t0)": "A",
"Local(t1)": "B",
},
},
// Circle cycle makes type A and B get propagated everywhere.
{name: "circle-cycle", graph: suite["circle-cycle"],
want: map[string]string{
"Local(t0)": "A;B",
"Local(t1)": "A;B",
"Local(t2)": "A;B",
},
},
// Similarly for fully connected graph.
{name: "fully-connected", graph: suite["fully-connected"],
want: map[string]string{
"Local(t0)": "A;B;C",
"Local(t1)": "A;B;C",
"Local(t2)": "A;B;C",
},
},
// The outer loop of subsumed-scc pushes A an B through the graph.
{name: "subsumed-scc", graph: suite["subsumed-scc"],
want: map[string]string{
"Local(t0)": "A;B",
"Local(t1)": "A;B",
"Local(t2)": "A;B",
"Local(t3)": "A;B",
},
},
// More realistic graph has a more fine grained flow.
{name: "more-realistic", graph: suite["more-realistic"],
want: map[string]string{
"Local(t0)": "A",
"Local(t1)": "A;B",
"Local(t2)": "A;B",
"Function(F1)": "A;B;F1;F2;F3",
"Function(F2)": "A;B;F1;F2;F3",
"Function(F3)": "A;B;F1;F2;F3",
"Function(F4)": "A;B;F1;F2;F3;F4",
},
},
} {
if got := nodeToTypeString(propagate(test.graph, &canon)); !reflect.DeepEqual(got, test.want) {
t.Errorf("want %v for graph %v; got %v", test.want, test.name, got)
}
}
}
|