/
intraprocess-visualize.go
144 lines (123 loc) · 3.16 KB
/
intraprocess-visualize.go
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
package absint
import (
"fmt"
"strconv"
"github.com/cs-au-dk/goat/analysis/cfg"
"github.com/cs-au-dk/goat/analysis/defs"
L "github.com/cs-au-dk/goat/analysis/lattice"
"github.com/cs-au-dk/goat/utils"
"github.com/cs-au-dk/goat/utils/dot"
"golang.org/x/tools/go/ssa"
)
// Debugging utility I made to visualize results of intraprocedural abstract interpretation
// If -verbose is specified the SSA-nodes will be annotated with abstract values for operands.
func VisualizeIntraprocess(
g defs.Goro,
edges map[defs.CtrLoc][]defs.CtrLoc,
analysis map[defs.CtrLoc]L.AnalysisState,
) {
G := &dot.DotGraph{
Options: map[string]string{
"minlen": fmt.Sprint(opts.Minlen()),
"nodesep": fmt.Sprint(opts.Nodesep()),
"rankdir": "TB",
"label": "Debug",
},
}
funToDotCluster := map[*ssa.Function]*dot.DotCluster{}
getCluster := func(fun *ssa.Function) *dot.DotCluster {
cluster, found := funToDotCluster[fun]
if !found {
funId := utils.SSAFunString(fun)
cluster = dot.NewDotCluster(funId)
cluster.Attrs["label"] = funId
cluster.Attrs["bgcolor"] = "#e6ffff"
funToDotCluster[fun] = cluster
G.Clusters = append(G.Clusters, cluster)
}
return cluster
}
clToDotNode := map[defs.CtrLoc]*dot.DotNode{}
getNode := func(cl defs.CtrLoc) *dot.DotNode {
node, found := clToDotNode[cl]
if !found {
label := cl.String()
if !cl.Panicked() {
label += "\n" + StringifyNodeArguments(g, analysis[cl].Memory(), cl.Node())
}
node = &dot.DotNode{
ID: strconv.Itoa(int(cl.Hash())),
Attrs: dot.DotAttrs{
"label": label,
},
}
if cl.Node().IsDeferred() {
node.Attrs["fillcolor"] = "#a0ecfa"
}
clToDotNode[cl] = node
C := getCluster(cl.Node().Function())
C.Nodes = append(C.Nodes, node)
}
return node
}
for cl, eds := range edges {
for _, ncl := range eds {
edge := &dot.DotEdge{
From: getNode(cl),
To: getNode(ncl),
Attrs: dot.DotAttrs{},
}
if !cl.Panicked() && ncl.Panicked() {
// Drop panic-edges for moderately sized graphs.
if len(edges) > 100 {
continue
}
edge.Attrs["color"] = "red"
}
G.Edges = append(G.Edges, edge)
}
// Edge for post-call node
if cr := cl.Node().CallRelation(); cr != nil {
switch cr.(type) {
case *cfg.CallNodeRelation:
pcl := cl.Derive(cl.Node().CallRelationNode())
if _, found := edges[pcl]; found {
G.Edges = append(G.Edges, &dot.DotEdge{
From: getNode(cl),
To: getNode(pcl),
Attrs: dot.DotAttrs{
"style": "dashed",
"constraint": "false",
},
})
}
}
}
}
G.ShowDot()
}
func StringifyNodeArguments(g defs.Goro, mem L.Memory, node cfg.Node) (label string) {
var ops []ssa.Value
if n, ok := node.(*cfg.SSANode); ok {
for _, pval := range n.Instruction().Operands(nil) {
ops = append(ops, *pval)
}
} else if n, ok := node.(*cfg.BuiltinCall); ok {
ops = n.Args()
}
for _, op := range ops {
if op != nil {
label += fmt.Sprintf("%s = ", op.Name())
func() {
defer func() {
if err := recover(); err != nil {
label += "Not found"
}
}()
label += evaluateSSA(g, mem, op).String()
}()
label += "\n"
}
}
return label
}