Skip to content

Commit 55546fe

Browse files
authored
Merge pull request #14205 from rdmarsh2/rdmarsh2/swift/unify-array-collection-content
Swift: Unify `ArrayContent` and `CollectionContent`
2 parents c0e600c + 62953cb commit 55546fe

File tree

12 files changed

+84
-109
lines changed

12 files changed

+84
-109
lines changed
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
category: deprecated
3+
---
4+
5+
* The `ArrayContent` type in the data flow library has been deprecated and made an alias for the `CollectionContent` type, to better reflect the hierarchy of the Swift standard library. Uses of `ArrayElement` in model files will be interpreted as referring to `CollectionContent`.

swift/ql/lib/codeql/swift/dataflow/ExternalFlow.qll

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -497,8 +497,9 @@ predicate parseContent(AccessPathToken component, Content content) {
497497
or
498498
parseEnum(component, content)
499499
or
500+
// map legacy "ArrayElement" specification components to `CollectionContent`
500501
component.getName() = "ArrayElement" and
501-
content instanceof Content::ArrayContent
502+
content instanceof Content::CollectionContent
502503
or
503504
component.getName() = "CollectionElement" and
504505
content instanceof Content::CollectionContent

swift/ql/lib/codeql/swift/dataflow/internal/DataFlowPrivate.qll

Lines changed: 5 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -297,7 +297,6 @@ private module Cached {
297297
TFieldContent(FieldDecl f) or
298298
TTupleContent(int index) { exists(any(TupleExpr te).getElement(index)) } or
299299
TEnumContent(ParamDecl f) { exists(EnumElementDecl d | d.getAParam() = f) } or
300-
TArrayContent() or
301300
TCollectionContent()
302301
}
303302

@@ -842,7 +841,7 @@ predicate storeStep(Node node1, ContentSet c, Node node2) {
842841
exists(ArrayExpr arr |
843842
node1.asExpr() = arr.getAnElement() and
844843
node2.asExpr() = arr and
845-
c.isSingleton(any(Content::ArrayContent ac))
844+
c.isSingleton(any(Content::CollectionContent ac))
846845
)
847846
or
848847
// array assignment `a[n] = x`
@@ -851,7 +850,7 @@ predicate storeStep(Node node1, ContentSet c, Node node2) {
851850
node2.(PostUpdateNode).getPreUpdateNode().asExpr() = subscript.getBase() and
852851
subscript = assign.getDest() and
853852
subscript.getBase().getType() instanceof ArrayType and
854-
c.isSingleton(any(Content::ArrayContent ac))
853+
c.isSingleton(any(Content::CollectionContent ac))
855854
)
856855
or
857856
// creation of an optional via implicit wrapping keypath component
@@ -948,7 +947,7 @@ predicate readStep(Node node1, ContentSet c, Node node2) {
948947
(
949948
c.isSingleton(any(Content::FieldContent ct | ct.getField() = component.getDeclRef()))
950949
or
951-
c.isSingleton(any(Content::ArrayContent ac)) and
950+
c.isSingleton(any(Content::CollectionContent ac)) and
952951
component.isSubscript()
953952
or
954953
c instanceof OptionalSomeContentSet and
@@ -971,12 +970,7 @@ predicate readStep(Node node1, ContentSet c, Node node2) {
971970
exists(SubscriptExpr subscript |
972971
subscript.getBase() = node1.asExpr() and
973972
subscript = node2.asExpr() and
974-
(
975-
subscript.getBase().getType() instanceof ArrayType and
976-
c.isSingleton(any(Content::ArrayContent ac))
977-
or
978-
c.isSingleton(any(Content::CollectionContent ac))
979-
)
973+
c.isSingleton(any(Content::CollectionContent ac))
980974
)
981975
or
982976
// read of a dictionary value via subscript operator
@@ -1098,9 +1092,7 @@ class DataFlowExpr = Expr;
10981092
* Holds if access paths with `c` at their head always should be tracked at high
10991093
* precision. This disables adaptive access path precision for such access paths.
11001094
*/
1101-
predicate forceHighPrecision(Content c) {
1102-
c instanceof Content::ArrayContent or c instanceof Content::CollectionContent
1103-
}
1095+
predicate forceHighPrecision(Content c) { c instanceof Content::CollectionContent }
11041096

11051097
/**
11061098
* Holds if the node `n` is unreachable when the call context is `call`.

swift/ql/lib/codeql/swift/dataflow/internal/DataFlowPublic.qll

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -220,11 +220,6 @@ module Content {
220220
override string toString() { result = this.getSignature() }
221221
}
222222

223-
/** An element of an array at an unknown index */
224-
class ArrayContent extends Content, TArrayContent {
225-
override string toString() { result = "Array element" }
226-
}
227-
228223
/**
229224
* An element of a collection. This is a broad class including:
230225
* - elements of collections, such as `Set<Element>`.
@@ -234,6 +229,11 @@ module Content {
234229
class CollectionContent extends Content, TCollectionContent {
235230
override string toString() { result = "Collection element" }
236231
}
232+
233+
/**
234+
* DEPRECATED: An element of a collection. This is an alias for the general CollectionContent.
235+
*/
236+
deprecated class ArrayContent = CollectionContent;
237237
}
238238

239239
/**

swift/ql/lib/codeql/swift/dataflow/internal/FlowSummaryImplSpecific.qll

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -129,11 +129,6 @@ private string getContentSpecific(ContentSet cs) {
129129
result = "EnumElement[" + c.getSignature() + "]"
130130
)
131131
or
132-
exists(Content::ArrayContent c |
133-
cs.isSingleton(c) and
134-
result = "ArrayElement"
135-
)
136-
or
137132
exists(Content::CollectionContent c |
138133
cs.isSingleton(c) and
139134
result = "CollectionElement"

swift/ql/lib/codeql/swift/frameworks/StandardLibrary/Array.qll

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,20 +19,20 @@ private class ArraySummaries extends SummaryModelCsv {
1919
override predicate row(string row) {
2020
row =
2121
[
22-
";Array;true;insert(_:at:);;;Argument[0];Argument[-1].ArrayElement;value",
22+
";Array;true;insert(_:at:);;;Argument[0];Argument[-1].CollectionElement;value",
2323
";Array;true;insert(_:at:);;;Argument[1];Argument[-1];taint",
2424
";Array;true;withUnsafeBufferPointer(_:);;;Argument[-1];Argument[0].Parameter[0].CollectionElement;taint",
25-
";Array;true;withUnsafeBufferPointer(_:);;;Argument[-1].ArrayElement;Argument[0].Parameter[0].CollectionElement;value",
25+
";Array;true;withUnsafeBufferPointer(_:);;;Argument[-1].CollectionElement;Argument[0].Parameter[0].CollectionElement;value",
2626
";Array;true;withUnsafeBufferPointer(_:);;;Argument[0].ReturnValue;ReturnValue;value",
2727
";Array;true;withUnsafeMutableBufferPointer(_:);;;Argument[-1];Argument[0].Parameter[0].CollectionElement;taint",
28-
";Array;true;withUnsafeMutableBufferPointer(_:);;;Argument[-1].ArrayElement;Argument[0].Parameter[0].CollectionElement;value",
28+
";Array;true;withUnsafeMutableBufferPointer(_:);;;Argument[-1].CollectionElement;Argument[0].Parameter[0].CollectionElement;value",
2929
";Array;true;withUnsafeMutableBufferPointer(_:);;;Argument[0].Parameter[0].CollectionElement;Argument[-1].CollectionElement;value",
3030
";Array;true;withUnsafeMutableBufferPointer(_:);;;Argument[0].ReturnValue;ReturnValue;value",
3131
";Array;true;withUnsafeBytes(_:);;;Argument[-1];Argument[0].Parameter[0].CollectionElement;taint",
32-
";Array;true;withUnsafeBytes(_:);;;Argument[-1].ArrayElement;Argument[0].Parameter[0].CollectionElement;taint",
32+
";Array;true;withUnsafeBytes(_:);;;Argument[-1].CollectionElement;Argument[0].Parameter[0].CollectionElement;taint",
3333
";Array;true;withUnsafeBytes(_:);;;Argument[0].ReturnValue;ReturnValue;value",
3434
";Array;true;withUnsafeMutableBytes(_:);;;Argument[-1];Argument[0].Parameter[0].CollectionElement;taint",
35-
";Array;true;withUnsafeMutableBytes(_:);;;Argument[-1].ArrayElement;Argument[0].Parameter[0].CollectionElement;taint",
35+
";Array;true;withUnsafeMutableBytes(_:);;;Argument[-1].CollectionElement;Argument[0].Parameter[0].CollectionElement;taint",
3636
";Array;true;withUnsafeMutableBytes(_:);;;Argument[0].Parameter[0].CollectionElement;Argument[-1].CollectionElement;value",
3737
";Array;true;withUnsafeMutableBytes(_:);;;Argument[0].ReturnValue;ReturnValue;value",
3838
";ContiguousArray;true;withUnsafeBufferPointer(_:);;;Argument[-1];Argument[0].Parameter[0].CollectionElement;taint",

swift/ql/lib/codeql/swift/frameworks/StandardLibrary/Collection.qll

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,6 @@ private class CollectionSummaries extends SummaryModelCsv {
2727
";Collection;true;removeFirst();;;Argument[-1];ReturnValue;taint",
2828
";Collection;true;popFirst();;;Argument[-1];ReturnValue;taint",
2929
";Collection;true;randomElement();;;Argument[-1].CollectionElement;ReturnValue.OptionalSome;value",
30-
";Collection;true;randomElement();;;Argument[-1].ArrayElement;ReturnValue.OptionalSome;value",
3130
";RangeReplaceableCollection;true;append(_:);;;Argument[0];Argument[-1];taint",
3231
";RangeReplaceableCollection;true;append(contentsOf:);;;Argument[0];Argument[-1];taint",
3332
";RangeReplaceableCollection;true;remove(at:);;;Argument[-1];ReturnValue;taint",
@@ -38,7 +37,6 @@ private class CollectionSummaries extends SummaryModelCsv {
3837
";BidirectionalCollection;true;last(where:);;;Argument[-1];ReturnValue;taint",
3938
";BidirectionalCollection;true;popLast();;;Argument[-1];ReturnValue;taint",
4039
";MutableCollection;true;withContiguousMutableStorageIfAvailable(_:);;;Argument[-1];Argument[0].Parameter[0].CollectionElement;taint",
41-
";MutableCollection;true;withContiguousMutableStorageIfAvailable(_:);;;Argument[-1].ArrayElement;Argument[0].Parameter[0].CollectionElement;value",
4240
";MutableCollection;true;withContiguousMutableStorageIfAvailable(_:);;;Argument[-1].CollectionElement;Argument[0].Parameter[0].CollectionElement;value",
4341
";MutableCollection;true;withContiguousMutableStorageIfAvailable(_:);;;Argument[0].Parameter[0].CollectionElement;Argument[-1].CollectionElement;value",
4442
";MutableCollection;true;withContiguousMutableStorageIfAvailable(_:);;;Argument[0].ReturnValue;ReturnValue.OptionalSome;value",

swift/ql/lib/codeql/swift/frameworks/StandardLibrary/Sequence.qll

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@ private class SequenceSummaries extends SummaryModelCsv {
2626
";Sequence;true;joined(separator:);;;Argument[-1..0];ReturnValue;taint",
2727
";Sequence;true;first(where:);;;Argument[-1];ReturnValue;taint",
2828
";Sequence;true;withContiguousStorageIfAvailable(_:);;;Argument[-1];Argument[0].Parameter[0].CollectionElement;taint",
29-
";Sequence;true;withContiguousStorageIfAvailable(_:);;;Argument[-1].ArrayElement;Argument[0].Parameter[0].CollectionElement;value",
3029
";Sequence;true;withContiguousStorageIfAvailable(_:);;;Argument[-1].CollectionElement;Argument[0].Parameter[0].CollectionElement;value",
3130
";Sequence;true;withContiguousStorageIfAvailable(_:);;;Argument[0].ReturnValue;ReturnValue.OptionalSome;value",
3231
]

swift/ql/lib/codeql/swift/frameworks/StandardLibrary/Set.qll

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ private class SetSummaries extends SummaryModelCsv {
1414
";Set;true;insert(_:);;;Argument[-1].CollectionElement;ReturnValue.TupleElement[1];value",
1515
";Set;true;insert(_:);;;Argument[0];Argument[-1].CollectionElement;value",
1616
";Set;true;insert(_:);;;Argument[0];ReturnValue.TupleElement[1];value",
17-
";Set;true;init(_:);;;Argument[0].ArrayElement;ReturnValue.CollectionElement;value"
17+
";Set;true;init(_:);;;Argument[0].CollectionElement;ReturnValue.CollectionElement;value"
1818
]
1919
}
2020
}

swift/ql/lib/codeql/swift/frameworks/StandardLibrary/String.qll

Lines changed: 8 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,6 @@ private class StringSummaries extends SummaryModelCsv {
3434
row =
3535
[
3636
";StringProtocol;true;init(cString:);;;Argument[0];ReturnValue;taint",
37-
";StringProtocol;true;init(cString:);;;Argument[0].ArrayElement;ReturnValue;taint",
3837
";StringProtocol;true;init(cString:);;;Argument[0].CollectionElement;ReturnValue;taint",
3938
";StringProtocol;true;init(decoding:as:);;;Argument[0];ReturnValue;taint",
4039
";StringProtocol;true;init(decodingCString:as:);;;Argument[0].OptionalSome.CollectionElement;ReturnValue.OptionalSome.TupleElement[0];taint",
@@ -46,12 +45,12 @@ private class StringSummaries extends SummaryModelCsv {
4645
";StringProtocol;true;cString(using:);;;Argument[-1];ReturnValue;taint",
4746
";StringProtocol;true;capitalized(with:);;;Argument[-1];ReturnValue;taint",
4847
";StringProtocol;true;completePath(into:caseSensitive:matchesInto:filterTypes:);;;Argument[-1];Argument[0].OptionalSome.CollectionElement;taint",
49-
";StringProtocol;true;completePath(into:caseSensitive:matchesInto:filterTypes:);;;Argument[-1];Argument[2].OptionalSome.CollectionElement.ArrayElement;taint",
48+
";StringProtocol;true;completePath(into:caseSensitive:matchesInto:filterTypes:);;;Argument[-1];Argument[2].OptionalSome.CollectionElement.CollectionElement;taint",
5049
";StringProtocol;true;components(separatedBy:);;;Argument[-1];ReturnValue;taint",
5150
";StringProtocol;true;data(using:allowLossyConversion:);;;Argument[-1];ReturnValue;taint",
5251
";StringProtocol;true;folding(options:locale:);;;Argument[-1];ReturnValue;taint",
53-
";StringProtocol;true;getBytes(_:maxLength:usedLength:encoding:options:range:remaining:);;;Argument[-1];Argument[0].ArrayElement;taint",
54-
";StringProtocol;true;getCString(_:maxLength:encoding:);;;Argument[-1];Argument[0].ArrayElement;taint",
52+
";StringProtocol;true;getBytes(_:maxLength:usedLength:encoding:options:range:remaining:);;;Argument[-1];Argument[0].CollectionElement;taint",
53+
";StringProtocol;true;getCString(_:maxLength:encoding:);;;Argument[-1];Argument[0].CollectionElement;taint",
5554
";StringProtocol;true;lowercased();;;Argument[-1];ReturnValue;taint",
5655
";StringProtocol;true;lowercased(with:);;;Argument[-1];ReturnValue;taint",
5756
";StringProtocol;true;padding(toLength:withPad:startingAt:);;;Argument[-1];ReturnValue;taint",
@@ -74,20 +73,18 @@ private class StringSummaries extends SummaryModelCsv {
7473
";String;true;init(repeating:count:);;;Argument[0];ReturnValue;taint",
7574
";String;true;init(data:encoding:);;;Argument[0];ReturnValue.OptionalSome;taint",
7675
";String;true;init(validatingUTF8:);;;Argument[0];ReturnValue.OptionalSome;taint",
77-
";String;true;init(validatingUTF8:);;;Argument[0].ArrayElement;ReturnValue.OptionalSome;taint",
7876
";String;true;init(validatingUTF8:);;;Argument[0].CollectionElement;ReturnValue.OptionalSome;taint",
7977
";String;true;init(utf16CodeUnits:count:);;;Argument[0].CollectionElement;ReturnValue;taint",
8078
";String;true;init(utf16CodeUnitsNoCopy:count:freeWhenDone:);;;Argument[0].CollectionElement;ReturnValue;taint",
8179
";String;true;init(format:_:);;;Argument[0];ReturnValue;taint",
82-
";String;true;init(format:_:);;;Argument[1].ArrayElement;ReturnValue;taint",
80+
";String;true;init(format:_:);;;Argument[1].CollectionElement;ReturnValue;taint",
8381
";String;true;init(format:arguments:);;;Argument[0];ReturnValue;taint",
84-
";String;true;init(format:arguments:);;;Argument[1].ArrayElement;ReturnValue;taint",
82+
";String;true;init(format:arguments:);;;Argument[1].CollectionElement;ReturnValue;taint",
8583
";String;true;init(format:locale:_:);;;Argument[0];ReturnValue;taint",
86-
";String;true;init(format:locale:_:);;;Argument[2].ArrayElement;ReturnValue;taint",
84+
";String;true;init(format:locale:_:);;;Argument[2].CollectionElement;ReturnValue;taint",
8785
";String;true;init(format:locale:arguments:);;;Argument[0];ReturnValue;taint",
88-
";String;true;init(format:locale:arguments:);;;Argument[2].ArrayElement;ReturnValue;taint",
86+
";String;true;init(format:locale:arguments:);;;Argument[2].CollectionElement;ReturnValue;taint",
8987
";String;true;init(_:radix:uppercase:);;;Argument[0];ReturnValue;taint",
90-
";String;true;init(bytes:encoding:);;;Argument[0].ArrayElement;ReturnValue.OptionalSome;taint",
9188
";String;true;init(bytes:encoding:);;;Argument[0].CollectionElement;ReturnValue.OptionalSome;taint",
9289
";String;true;init(bytesNoCopy:length:encoding:freeWhenDone:);;;Argument[0].CollectionElement;ReturnValue.OptionalSome;taint",
9390
";String;true;init(describing:);;;Argument[0];ReturnValue;taint",
@@ -104,20 +101,16 @@ private class StringSummaries extends SummaryModelCsv {
104101
";String;true;init(unicodeScalarLiteral:);;;Argument[0];ReturnValue;taint",
105102
";String;true;init(extendedGraphemeClusterLiteral:);;;Argument[0];ReturnValue;taint",
106103
";String;true;init(cString:encoding:);;;Argument[0];ReturnValue.OptionalSome;taint",
107-
";String;true;init(cString:encoding:);;;Argument[0].ArrayElement;ReturnValue.OptionalSome;taint",
108104
";String;true;init(cString:encoding:);;;Argument[0].CollectionElement;ReturnValue.OptionalSome;taint",
109105
";String;true;init(platformString:);;;Argument[0];ReturnValue;taint",
110-
";String;true;init(platformString:);;;Argument[0].ArrayElement;ReturnValue;taint",
111106
";String;true;init(platformString:);;;Argument[0].CollectionElement;ReturnValue;taint",
112107
";String;true;init(utf8String:);;;Argument[0];ReturnValue.OptionalSome;taint",
113-
";String;true;init(utf8String:);;;Argument[0].ArrayElement;ReturnValue.OptionalSome;taint",
114108
";String;true;init(utf8String:);;;Argument[0].CollectionElement;ReturnValue.OptionalSome;taint",
115109
";String;true;init(validating:);;;Argument[0];ReturnValue.OptionalSome;taint",
116110
";String;true;init(validatingPlatformString:);;;Argument[0];ReturnValue.OptionalSome;taint",
117-
";String;true;init(validatingPlatformString:);;;Argument[0].ArrayElement;ReturnValue.OptionalSome;taint",
118111
";String;true;init(validatingPlatformString:);;;Argument[0].CollectionElement;ReturnValue.OptionalSome;taint",
119112
";String;true;localizedStringWithFormat(_:_:);;;Argument[0];ReturnValue;taint",
120-
";String;true;localizedStringWithFormat(_:_:);;;Argument[1].ArrayContent;ReturnValue;taint",
113+
";String;true;localizedStringWithFormat(_:_:);;;Argument[1].CollectionElement;ReturnValue;taint",
121114
";String;true;write(_:);;;Argument[0];Argument[-1];taint",
122115
";String;true;write(to:);;;Argument[-1];Argument[0];taint",
123116
";String;true;insert(contentsOf:at:);;;Argument[0];Argument[-1];taint",

0 commit comments

Comments
 (0)