Repository navigation
Expand file tree
/
Copy pathcontainers.go
More file actions
420 lines (401 loc) · 13.1 KB
/
Copy pathcontainers.go
File metadata and controls
420 lines (401 loc) · 13.1 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
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
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
package contracttrace
import (
"fmt"
"go/ast"
"go/constant"
"go/token"
"go/types"
"strconv"
"strings"
"golang.org/x/tools/go/ssa"
)
func inlineAggregate(t types.Type) bool {
switch t.Underlying().(type) {
case *types.Struct, *types.Array:
return true
}
return false
}
func (a *flowAnalysis) sequenceElements(value ssa.Value) flowValue {
result := emptyFlow()
sequence, ok := value.Type().Underlying().(*types.Slice)
if !ok {
return result
}
for _, root := range sortedKeys(a.get(value).addresses) {
address := root + ".element"
a.merge(&result, a.memory[address])
if sequenceMayHaveElement(value) {
result.functionNil = result.functionNil || a.zeroFunctionElements[address]
result.functionUnknown = result.functionUnknown || a.unknownFunctionElements[address]
}
if inlineAggregate(sequence.Elem()) {
result.addresses[address] = true
marker := a.get(value)
if sequenceMayHaveElement(value) && exactZeroAggregateMatches(marker, sequence.Elem()) {
a.addZeroAggregate(&result, sequence.Elem())
}
if sequenceMayHaveElement(value) && marker.zeroAggregateUnknown {
a.markZeroAggregateUnknown(&result)
}
}
}
return result
}
// Element candidates are unordered and monotone. Append may reuse its backing
// array or allocate a new one; copy may move any prefix up to the shorter length.
// Preserve both append storage candidates and all modeled source elements.
func (a *flowAnalysis) sliceBuiltin(call ssa.CallInstruction, ix *index, result *flowValue) bool {
common := call.Common()
builtin, ok := common.Value.(*ssa.Builtin)
if !ok || len(common.Args) != 2 || (builtin.Name() != "append" && builtin.Name() != "copy") {
return false
}
destinations := a.get(common.Args[0])
elements := a.sequenceElements(common.Args[1])
if builtin.Name() == "append" {
value, ok := call.(*ssa.Call)
if !ok {
return false
}
a.merge(&elements, a.sequenceElements(common.Args[0]))
root := ix.allocationID("append", call.Parent(), value)
candidate := emptyFlow()
candidate.addresses[root] = true
a.merge(&destinations, candidate)
a.addressTypes[root] = value.Type()
a.merge(result, destinations)
}
changed := false
for _, root := range sortedKeys(destinations.addresses) {
if a.store(root+".element", elements) {
changed = true
}
}
return changed
}
func (a *flowAnalysis) store(key string, value flowValue) bool {
old := a.memory[key]
aliases := len(old.addresses)
changed := a.merge(&old, value)
a.memory[key] = old
if changed {
if a.memoryVersions == nil {
a.memoryVersions = map[string]uint64{}
}
a.memoryVersions[key]++
}
if len(old.addresses) != aliases {
if a.aliasVersions == nil {
a.aliasVersions = map[string]uint64{}
}
a.aliasVersions[key]++
}
return changed
}
func (a *flowAnalysis) storeMap(root, key string, value flowValue) bool {
if a.mapEntries == nil {
a.mapEntries = map[string]map[string]bool{}
}
if a.mapEntries[root] == nil {
a.mapEntries[root] = map[string]bool{}
}
address := root + ".key:" + key
a.mapEntries[root][address] = true
return a.store(address, value)
}
func isMapValue(value ssa.Value) bool {
if value == nil || value.Type() == nil {
return false
}
return isMapType(value.Type())
}
func isMapType(t types.Type) bool {
if t == nil {
return false
}
_, ok := types.Unalias(t).Underlying().(*types.Map)
return ok
}
func (a *flowAnalysis) openMap(root string) bool {
if a.mapOpen == nil {
a.mapOpen = map[string]bool{}
}
if a.mapOpen[root] {
return false
}
a.mapOpen[root] = true
return true
}
func (a *flowAnalysis) openMapValue(value ssa.Value) bool {
changed := false
for _, root := range sortedKeys(a.get(value).addresses) {
if a.openMap(root) {
changed = true
}
}
return changed
}
// Literal MapUpdates are the only writes for which the analyzer currently
// retains a closed key set. Later updates and wildcard keys invalidate absence
// claims rather than making a historical entry definite.
func (a *flowAnalysis) mapUpdateOpens(function *ssa.Function, update *ssa.MapUpdate, ix *index) bool {
if update == nil || !isMapValue(update.Map) {
return false
}
mapValue, ok := update.Map.(*ssa.MakeMap)
if !ok || !a.mapUpdateInLiteral(function, update, mapValue, ix) {
return true
}
keys := a.mapKeys(update.Key)
return len(keys) != 1 || keys[0] == "*"
}
func (a *flowAnalysis) mapUpdateInLiteral(function *ssa.Function, update *ssa.MapUpdate, allocation *ssa.MakeMap, ix *index) bool {
if function == nil || function.Syntax() == nil || update == nil || allocation == nil || ix == nil || ix.fset == nil {
return false
}
owner := ix.funcs[ix.owner(function)]
if owner == nil || owner.pkg == nil || owner.pkg.TypesInfo == nil {
return false
}
updatePos, allocationPos := update.Pos(), allocation.Pos()
if updatePos == token.NoPos || allocationPos == token.NoPos {
return false
}
matched := false
ast.Inspect(function.Syntax(), func(node ast.Node) bool {
literal, ok := node.(*ast.CompositeLit)
if !ok || literal.Pos() > updatePos || literal.End() < updatePos || literal.Pos() > allocationPos || literal.End() < allocationPos {
return true
}
typ := owner.pkg.TypesInfo.TypeOf(literal)
if typ == nil {
return true
}
_, mapLiteral := typ.Underlying().(*types.Map)
if mapLiteral && types.Identical(types.Unalias(typ), types.Unalias(allocation.Type())) {
matched = true
return false
}
return true
})
return matched
}
func (a *flowAnalysis) openMutatedMaps(call *ssa.Call) bool {
if call == nil || len(call.Common().Args) == 0 {
return false
}
builtin, ok := call.Common().Value.(*ssa.Builtin)
if !ok || (builtin.Name() != "delete" && builtin.Name() != "clear") {
return false
}
return a.openMapValue(call.Common().Args[0])
}
// Quoted strings and typed constants avoid collisions with the unknown-key marker.
func (a *flowAnalysis) mapKeys(value ssa.Value) []string {
if c, ok := value.(*ssa.Const); ok && c.Value != nil && c.Value.Kind() != constant.String {
return []string{c.Type().String() + ":" + c.Value.ExactString()}
}
keys := []string{}
candidates := a.get(value)
for _, s := range sortedKeys(candidates.strings) {
keys = append(keys, strconv.Quote(s))
}
if len(keys) == 0 {
return []string{"*"}
}
if candidates.stringUnknown {
keys = append(keys, "*")
}
return keys
}
func (a *flowAnalysis) mapLookup(value, key ssa.Value) flowValue {
result := emptyFlow()
mapFlow := a.get(value)
mapType, isMap := types.Unalias(value.Type()).Underlying().(*types.Map)
if !isMap {
return result
}
keys := a.mapKeys(key)
for _, p := range sortedKeys(mapFlow.addresses) {
prefix := p + ".key:"
for _, k := range keys {
if k == "*" {
for _, address := range sortedKeys(a.mapEntries[p]) {
a.merge(&result, a.memory[address])
}
} else {
a.merge(&result, a.memory[prefix+k])
}
a.merge(&result, a.memory[prefix+"*"])
missing := (k == "*" && !a.mapEntries[p][prefix+"*"]) ||
(k != "*" && !a.mapEntries[p][prefix+k] && !a.mapEntries[p][prefix+"*"])
zeroAlternative := a.mapClosed[p] && (missing || a.mapOpen[p])
unknownAlternative := !a.mapClosed[p] || a.mapOpen[p]
addTypedAlternative(a, &result, mapType.Elem(), zeroAlternative, unknownAlternative)
}
}
if mapFlow.mapNil {
addTypedAlternative(a, &result, mapType.Elem(), true, false)
}
if mapFlow.interfaceUnknown {
addTypedAlternative(a, &result, mapType.Elem(), false, true)
}
return result
}
func addTypedAlternative(a *flowAnalysis, value *flowValue, element types.Type, zero, unknown bool) {
if value == nil || (!zero && !unknown) {
return
}
switch {
case isFunctionType(element):
value.functionNil = value.functionNil || zero
value.functionUnknown = value.functionUnknown || unknown
case inlineAggregate(element):
if zero {
a.addZeroAggregate(value, element)
}
if unknown {
a.markZeroAggregateUnknown(value)
}
case isSQLHandleType(element):
value.sqlUnknown = true
case isMapType(element):
value.mapNil = value.mapNil || zero
value.interfaceUnknown = value.interfaceUnknown || unknown
default:
markMapElementAlternative(value, element, true)
}
}
func markMapElementAlternative(value *flowValue, element types.Type, possible bool) {
if !possible {
return
}
if isSQLHandleType(element) {
value.sqlUnknown = true
return
}
switch types.Unalias(element).Underlying().(type) {
case *types.Pointer, *types.Interface, *types.Chan, *types.Map, *types.Slice:
value.interfaceUnknown = true
}
}
// SSA Next slots are (ok, key, value). Each iteration retains an unordered
// union, with keys and values separate; pairing and iteration order are lost.
func (a *flowAnalysis) mapIterationValue(iterator ssa.Value, slot int, typ types.Type) flowValue {
result := emptyFlow()
if slot != 1 && slot != 2 {
return result
}
for _, root := range sortedKeys(a.get(iterator).addresses) {
if slot == 1 {
a.merge(&result, a.memory[root+".keys"])
continue
}
addresses := map[string]bool{root + ".key:*": true}
for address := range a.mapEntries[root] {
addresses[address] = true
}
for _, address := range sortedKeys(addresses) {
a.merge(&result, a.memory[address])
if inlineAggregate(typ) {
result.addresses[address] = true
}
}
}
return result
}
func (a *flowAnalysis) send(channel, value ssa.Value, block *ssa.BasicBlock) bool {
changed := false
sent := a.get(value)
if isScalarType(value.Type()) {
a.mergeScalar(&sent, a.scalarControlValue(block))
}
for _, p := range sortedKeys(a.get(channel).addresses) {
if a.store(p+".sent", sent) {
changed = true
}
}
return changed
}
func (a *flowAnalysis) receive(channel ssa.Value) flowValue {
result := emptyFlow()
for _, p := range sortedKeys(a.get(channel).addresses) {
a.merge(&result, a.memory[p+".sent"])
}
return result
}
func (ix *index) channelID(pos token.Pos) string {
e := ix.evidence(pos)
return resourceID("channel", "", fmt.Sprintf("%s:%d:%d", e.File, e.Line, e.Column))
}
func (a *flowAnalysis) channelUse(ins ssa.Instruction, from string, ix *index) {
link := func(value ssa.Value, kind string, pos token.Pos) {
found := false
flow := a.get(value)
if kind == "channel_receive" || kind == "channel_select_receive" {
_, closeSites, orderUncertain := a.receiveWithClosedChannelValue(value, a.channelClosures)
if len(closeSites) > 0 {
examples := make([]string, 0, len(closeSites))
for _, closePos := range closeSites {
evidence := ix.evidence(closePos)
examples = append(examples, fmt.Sprintf("close at %s:%d:%d", evidence.File, evidence.Line, evidence.Column))
}
reason := "a modeled close permits the element zero value, which can carry a nil callback through an inline aggregate, after queued values drain; send/close ordering and queue occupancy are not proved"
if orderUncertain {
reason = "a modeled close and send candidates coexist; receive ordering, queue occupancy and whether a queued element or element zero value (which can carry a nil callback through an inline aggregate) is returned are unresolved"
}
ix.boundaries = append(ix.boundaries, Boundary{Node: from, Kind: "closed_function_channel_zero", Reason: reason, Evidence: ix.evidence(pos), Examples: examples})
}
}
for _, effect := range sortedKeys(flow.effects) {
if strings.HasPrefix(effect, "nil_done:") {
found = true
if kind == "channel_receive" {
ix.boundaries = append(ix.boundaries, Boundary{Node: from, Kind: "nil_context_done_wait", Reason: "receive has a modeled nil Done channel candidate; it cannot be released by cancellation of that context", Evidence: ix.evidence(pos)})
} else if kind == "channel_select_receive" {
ix.boundaries = append(ix.boundaries, Boundary{Node: from, Kind: "nil_context_done_select", Reason: "select has a modeled nil Done channel candidate; that arm is disabled for this candidate", Evidence: ix.evidence(pos)})
}
}
}
for key := range a.get(value).addresses {
if strings.HasPrefix(key, "channel:") {
found = true
ix.edge(from, key, kind, "possible", pos)
}
}
if !found || flow.interfaceUnknown || flow.sqlUnknown || flow.boundReceiverUnknown {
reason := "channel operation has no modeled creation site; input or alias may be external"
if found {
reason = "channel operation has a modeled creation site and an unresolved receiver or alias candidate"
}
ix.boundaries = append(ix.boundaries, Boundary{Node: from, Kind: "unresolved_channel", Reason: reason, Evidence: ix.evidence(pos)})
}
}
switch v := ins.(type) {
case *ssa.MakeChan:
key := ix.channelID(v.Pos())
if ix.funcs[key] == nil {
ix.funcs[key] = &function{node: Node{ID: key, Name: key, Kind: "channel", Evidence: ix.evidence(v.Pos())}}
}
ix.edge(from, key, "channel_create", "fact", v.Pos())
case *ssa.Send:
link(v.Chan, "channel_send", v.Pos())
case *ssa.UnOp:
if v.Op == token.ARROW {
link(v.X, "channel_receive", v.Pos())
}
case *ssa.Select:
for _, state := range v.States {
kind := "channel_select_receive"
if state.Dir == types.SendOnly {
kind = "channel_select_send"
}
link(state.Chan, kind, state.Pos)
}
case ssa.CallInstruction:
if builtin, ok := v.Common().Value.(*ssa.Builtin); ok && builtin.Name() == "close" && len(v.Common().Args) == 1 {
link(v.Common().Args[0], "channel_close", v.Pos())
}
}
}