Repository navigation
Expand file tree
/
Copy pathiterators.go
More file actions
122 lines (113 loc) · 4.12 KB
/
Copy pathiterators.go
File metadata and controls
122 lines (113 loc) · 4.12 KB
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
package contracttrace
import (
"go/types"
"golang.org/x/tools/go/ssa"
)
type iteratorSummary struct {
name string
source ssa.Value
call *ssa.Call
}
type iteratorYield struct {
value flowValue
typ types.Type
}
func iteratorFactory(common *ssa.CallCommon) string {
target := common.StaticCallee()
if target == nil || target.Object() == nil || target.Object().Pkg() == nil || len(common.Args) != 1 {
return ""
}
name := target.Object().Pkg().Path() + "." + target.Object().Name()
switch name {
case "slices.Values", "slices.All", "slices.Backward", "maps.Keys", "maps.Values", "maps.All":
return name
}
return ""
}
func (a *flowAnalysis) modelIterator(call *ssa.Call, ix *index, result *flowValue) {
name := iteratorFactory(call.Common())
if name == "" {
return
}
if a.iterators == nil {
a.iterators = map[string]iteratorSummary{}
}
id := ix.allocationID("iterator", call.Parent(), call)
a.iterators[id] = iteratorSummary{name, call.Common().Args[0], call}
result.effects[id] = true
}
func (a *flowAnalysis) iteratorValues(summary iteratorSummary) []iteratorYield {
switch source := summary.source.Type().Underlying().(type) {
case *types.Slice:
value := iteratorYield{a.sequenceElements(summary.source), source.Elem()}
if summary.name == "slices.Values" {
return []iteratorYield{value}
}
return []iteratorYield{{emptyFlow(), types.Typ[types.Int]}, value}
case *types.Map:
key := iteratorYield{a.mapIterationValue(summary.source, 1, source.Key()), source.Key()}
value := iteratorYield{a.mapIterationValue(summary.source, 2, source.Elem()), source.Elem()}
switch summary.name {
case "maps.Keys":
return []iteratorYield{key}
case "maps.Values":
return []iteratorYield{value}
case "maps.All":
return []iteratorYield{key, value}
}
}
return nil
}
func (a *flowAnalysis) invokeIterator(common *ssa.CallCommon, ix *index) bool {
if len(common.Args) != 1 {
return false
}
changed := false
for _, effect := range sortedKeys(a.get(common.Value).effects) {
summary, ok := a.iterators[effect]
if !ok {
continue
}
for _, target := range sortedFunctions(a.get(common.Args[0]).functions) {
if ix.owner(target) == "" {
continue
}
for slot, candidate := range a.iteratorValues(summary) {
if slot < len(target.Params) && a.put(target.Params[slot], candidate.value) {
changed = true
}
}
}
}
return changed
}
func (a *flowAnalysis) iteratorRelationships(ix *index) {
for _, effect := range sortedKeys(a.iterators) {
summary := a.iterators[effect]
evidence := ix.callEvidence(summary.call)
ix.funcs[effect] = &function{node: Node{ID: effect, Name: summary.name, Kind: "iterator", Evidence: evidence}}
ix.edges = append(ix.edges, Relationship{From: ix.owner(summary.call.Parent()), To: effect, Kind: "iterator_create", Certainty: "fact", Evidence: evidence})
ix.boundaries = append(ix.boundaries, Boundary{Node: effect, Kind: "iterator_model", Reason: "standard slices/maps iterator retains yielded candidate slots through modeled local consumers; index numbers, key/value pairing, order, early termination, panic and cleanup timing are not proved", Evidence: evidence})
}
}
func (a *flowAnalysis) iteratorUse(call ssa.CallInstruction, ix *index) {
for _, effect := range sortedKeys(a.get(call.Common().Value).effects) {
_, ok := a.iterators[effect]
if !ok {
continue
}
ix.edges = append(ix.edges, Relationship{From: ix.owner(call.Parent()), To: effect, Kind: "iterator_invoke", Certainty: "possible", Evidence: ix.callEvidence(call)})
found := false
if len(call.Common().Args) == 1 {
for _, target := range sortedFunctions(a.get(call.Common().Args[0]).functions) {
if owner := ix.owner(target); owner != "" {
found = true
ix.edges = append(ix.edges, Relationship{From: effect, To: owner, Kind: "iterator_yield", Certainty: "possible", Evidence: ix.callEvidence(call)})
}
}
}
if !found {
ix.boundaries = append(ix.boundaries, Boundary{Node: effect, Kind: "unresolved_iterator_yield", Reason: "iterator invocation has no modeled local yield consumer; outside consumers or unsupported function flow remain open", Evidence: ix.callEvidence(call)})
}
}
}