Skip to content

Commit d4b635d

Browse files
committed
Swift: add ArrayContent summary support
1 parent 169326f commit d4b635d

File tree

10 files changed

+40
-10
lines changed

10 files changed

+40
-10
lines changed

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -479,6 +479,9 @@ private predicate parseField(AccessPathToken c, Content::FieldContent f) {
479479
/** Holds if the specification component parses as a `Content`. */
480480
predicate parseContent(AccessPathToken component, Content content) {
481481
parseField(component, content)
482+
or
483+
component.getName() = "ArrayElement" and
484+
content instanceof Content::ArrayContent
482485
}
483486

484487
cached

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ private import codeql.swift.controlflow.BasicBlocks
88
private import codeql.swift.dataflow.FlowSummary as FlowSummary
99
private import codeql.swift.dataflow.internal.FlowSummaryImpl as FlowSummaryImpl
1010
private import codeql.swift.frameworks.StandardLibrary.PointerTypes
11-
private import codeql.swift.frameworks.StandardLibrary.ArrayType
11+
private import codeql.swift.frameworks.StandardLibrary.Array
1212

1313
/** Gets the callable in which this node occurs. */
1414
DataFlowCallable nodeGetEnclosingCallable(NodeImpl n) { result = n.getEnclosingCallable() }

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,10 @@ private string getContentSpecific(ContentSet cs) {
108108
exists(Content::FieldContent c |
109109
cs.isSingleton(c) and
110110
result = "Field[" + c.getField().getName() + "]"
111+
) or
112+
exists(Content::ArrayContent c |
113+
cs.isSingleton(c) and
114+
result = "ArrayElement"
111115
)
112116
}
113117

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
import swift
2+
private import codeql.swift.dataflow.ExternalFlow
3+
4+
class ArrayType extends BoundGenericType {
5+
ArrayType() { this.getName().matches("Array<%") }
6+
}
7+
8+
/**
9+
* A model for `Array` and related class members that permit data flow.
10+
*/
11+
private class ArraySummaries extends SummaryModelCsv {
12+
override predicate row(string row) {
13+
row = [
14+
";Array;true;insert(_:at:);;;Argument[0];Argument[-1].ArrayElement;value",
15+
";Array;true;insert(_:at:);;;Argument[1];Argument[-1];taint"
16+
]
17+
}
18+
}

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

Lines changed: 0 additions & 7 deletions
This file was deleted.

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
* This file imports all models related to the Swift standard library.
33
*/
44

5+
private import Array
56
private import Collection
67
private import CustomUrlSchemes
78
private import Data

swift/ql/test/library-tests/dataflow/dataflow/DataFlow.expected

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -292,6 +292,9 @@ edges
292292
| test.swift:647:15:647:15 | &... [Array element, Array element] | test.swift:647:15:647:23 | ...[...] [Array element] |
293293
| test.swift:647:15:647:23 | &... [Array element] | test.swift:647:15:647:26 | ...[...] |
294294
| test.swift:647:15:647:23 | ...[...] [Array element] | test.swift:647:15:647:23 | &... [Array element] |
295+
| test.swift:658:5:658:5 | [post] &... [Array element] | test.swift:659:15:659:15 | &... [Array element] |
296+
| test.swift:658:17:658:24 | call to source() | test.swift:658:5:658:5 | [post] &... [Array element] |
297+
| test.swift:659:15:659:15 | &... [Array element] | test.swift:659:15:659:21 | ...[...] |
295298
nodes
296299
| file://:0:0:0:0 | .a [x] | semmle.label | .a [x] |
297300
| file://:0:0:0:0 | .str | semmle.label | .str |
@@ -614,6 +617,10 @@ nodes
614617
| test.swift:647:15:647:23 | &... [Array element] | semmle.label | &... [Array element] |
615618
| test.swift:647:15:647:23 | ...[...] [Array element] | semmle.label | ...[...] [Array element] |
616619
| test.swift:647:15:647:26 | ...[...] | semmle.label | ...[...] |
620+
| test.swift:658:5:658:5 | [post] &... [Array element] | semmle.label | [post] &... [Array element] |
621+
| test.swift:658:17:658:24 | call to source() | semmle.label | call to source() |
622+
| test.swift:659:15:659:15 | &... [Array element] | semmle.label | &... [Array element] |
623+
| test.swift:659:15:659:21 | ...[...] | semmle.label | ...[...] |
617624
subpaths
618625
| test.swift:75:21:75:22 | &... | test.swift:65:16:65:28 | arg1 | test.swift:65:1:70:1 | arg2[return] | test.swift:75:31:75:32 | [post] &... |
619626
| test.swift:114:19:114:19 | arg | test.swift:109:9:109:14 | arg | test.swift:110:12:110:12 | arg | test.swift:114:12:114:22 | call to ... |
@@ -733,3 +740,4 @@ subpaths
733740
| test.swift:639:15:639:21 | ...[...] | test.swift:638:15:638:22 | call to source() | test.swift:639:15:639:21 | ...[...] | result |
734741
| test.swift:643:15:643:21 | ...[...] | test.swift:642:17:642:24 | call to source() | test.swift:643:15:643:21 | ...[...] | result |
735742
| test.swift:647:15:647:26 | ...[...] | test.swift:645:20:645:27 | call to source() | test.swift:647:15:647:26 | ...[...] | result |
743+
| test.swift:659:15:659:21 | ...[...] | test.swift:658:17:658:24 | call to source() | test.swift:659:15:659:21 | ...[...] | result |

swift/ql/test/library-tests/dataflow/dataflow/FlowConfig.qll

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import swift
66
import codeql.swift.dataflow.DataFlow
77
import codeql.swift.dataflow.ExternalFlow
8+
import codeql.swift.frameworks.Frameworks
89

910
module TestConfiguration implements DataFlow::ConfigSig {
1011
predicate isSource(DataFlow::Node src) {

swift/ql/test/library-tests/dataflow/dataflow/LocalFlow.expected

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -721,9 +721,11 @@
721721
| test.swift:654:9:654:9 | SSA def(arr5) | test.swift:655:15:655:15 | arr5 |
722722
| test.swift:654:9:654:9 | arr5 | test.swift:654:9:654:9 | SSA def(arr5) |
723723
| test.swift:654:16:654:51 | call to Array<Element>.init(repeating:count:) | test.swift:654:9:654:9 | arr5 |
724+
| test.swift:655:15:655:15 | arr5 | test.swift:655:15:655:15 | &... |
724725
| test.swift:657:9:657:9 | SSA def(arr6) | test.swift:658:5:658:5 | arr6 |
725726
| test.swift:657:9:657:9 | arr6 | test.swift:657:9:657:9 | SSA def(arr6) |
726727
| test.swift:657:16:657:22 | [...] | test.swift:657:9:657:9 | arr6 |
727728
| test.swift:658:5:658:5 | &... | test.swift:659:15:659:15 | arr6 |
728729
| test.swift:658:5:658:5 | [post] &... | test.swift:659:15:659:15 | arr6 |
729730
| test.swift:658:5:658:5 | arr6 | test.swift:658:5:658:5 | &... |
731+
| test.swift:659:15:659:15 | arr6 | test.swift:659:15:659:15 | &... |

swift/ql/test/library-tests/dataflow/dataflow/test.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -652,9 +652,9 @@ func testArray() {
652652
sink(arg: arr4) // $ MISSING: flow=642
653653

654654
var arr5 = Array(repeating: source(), count: 2)
655-
sink(arg: arr5) // $ MISSING: flow=654
655+
sink(arg: arr5[0]) // $ MISSING: flow=654
656656

657657
var arr6 = [1,2,3]
658658
arr6.insert(source(), at: 2)
659-
sink(arg: arr6) // $ MISSING: flow=658
659+
sink(arg: arr6[0]) // $ flow=658
660660
}

0 commit comments

Comments
 (0)