Skip to content

Commit 32e4c49

Browse files
authored
Swift: Dataflow through Phi nodes
1 parent 507fdef commit 32e4c49

File tree

4 files changed

+41
-3
lines changed

4 files changed

+41
-3
lines changed

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

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,4 +72,22 @@ module Ssa {
7272
)
7373
}
7474
}
75+
76+
cached class PhiDefinition extends Definition, SsaImplCommon::PhiNode {
77+
cached
78+
override Location getLocation() {
79+
exists(BasicBlock bb, int i |
80+
this.definesAt(_, bb, i) and
81+
result = bb.getLocation()
82+
)
83+
}
84+
85+
cached Definition getPhiInput(BasicBlock bb) {
86+
SsaImplCommon::phiHasInputFromBlock(this, result, bb)
87+
}
88+
89+
cached Definition getAPhiInput() {
90+
result = this.getPhiInput(_)
91+
}
92+
}
7593
}

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

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ private import DataFlowDispatch
44
private import codeql.swift.controlflow.CfgNodes
55
private import codeql.swift.dataflow.Ssa
66
private import codeql.swift.controlflow.BasicBlocks
7+
private import codeql.swift.dataflow.internal.SsaImplCommon as SsaImpl
78

89
/** Gets the callable in which this node occurs. */
910
DataFlowCallable nodeGetEnclosingCallable(NodeImpl n) { result = n.getEnclosingCallable() }
@@ -46,6 +47,13 @@ private class SsaDefinitionNodeImpl extends SsaDefinitionNode, NodeImpl {
4647
}
4748
}
4849

50+
private predicate localFlowSsaInput(Node nodeFrom, Ssa::Definition def, Ssa::Definition next) {
51+
exists(BasicBlock bb, int i | SsaImpl::lastRefRedef(def, bb, i, next) |
52+
def.definesAt(_, bb, i) and
53+
def = nodeFrom.asDefinition()
54+
)
55+
}
56+
4957
/** A collection of cached types and predicates to be evaluated in the same stage. */
5058
cached
5159
private module Cached {
@@ -66,6 +74,9 @@ private module Cached {
6674
or
6775
// use-use flow
6876
def.adjacentReadPair(nodeFrom.getCfgNode(), nodeTo.getCfgNode())
77+
// step from previous read to Phi node
78+
or
79+
localFlowSsaInput(nodeFrom, def, nodeTo.asDefinition())
6980
)
7081
}
7182

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ edges
1111
| test.swift:29:26:29:29 | ParamDecl : | test.swift:31:15:31:15 | DeclRefExpr |
1212
| test.swift:29:26:29:29 | WriteDef : | test.swift:31:15:31:15 | DeclRefExpr |
1313
| test.swift:35:12:35:19 | CallExpr : | test.swift:39:15:39:29 | CallExpr |
14+
| test.swift:43:19:43:26 | CallExpr : | test.swift:50:15:50:15 | DeclRefExpr |
1415
nodes
1516
| test.swift:6:19:6:26 | CallExpr : | semmle.label | CallExpr : |
1617
| test.swift:7:15:7:15 | DeclRefExpr | semmle.label | DeclRefExpr |
@@ -30,6 +31,8 @@ nodes
3031
| test.swift:31:15:31:15 | DeclRefExpr | semmle.label | DeclRefExpr |
3132
| test.swift:35:12:35:19 | CallExpr : | semmle.label | CallExpr : |
3233
| test.swift:39:15:39:29 | CallExpr | semmle.label | CallExpr |
34+
| test.swift:43:19:43:26 | CallExpr : | semmle.label | CallExpr : |
35+
| test.swift:50:15:50:15 | DeclRefExpr | semmle.label | DeclRefExpr |
3336
subpaths
3437
#select
3538
| test.swift:6:19:6:26 | CallExpr : | test.swift:7:15:7:15 | DeclRefExpr |
@@ -38,3 +41,4 @@ subpaths
3841
| test.swift:25:20:25:27 | CallExpr : | test.swift:30:15:30:15 | DeclRefExpr |
3942
| test.swift:26:26:26:33 | CallExpr : | test.swift:31:15:31:15 | DeclRefExpr |
4043
| test.swift:35:12:35:19 | CallExpr : | test.swift:39:15:39:29 | CallExpr |
44+
| test.swift:43:19:43:26 | CallExpr : | test.swift:50:15:50:15 | DeclRefExpr |

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

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,3 @@
1-
| file://:0:0:0:0 | Phi | test.swift:15:15:15:15 | DeclRefExpr |
2-
| file://:0:0:0:0 | Phi | test.swift:21:15:21:15 | DeclRefExpr |
3-
| file://:0:0:0:0 | Phi | test.swift:50:15:50:15 | DeclRefExpr |
41
| test.swift:6:9:6:13 | WriteDef | test.swift:7:15:7:15 | DeclRefExpr |
52
| test.swift:6:19:6:26 | CallExpr | test.swift:6:9:6:13 | WriteDef |
63
| test.swift:7:15:7:15 | DeclRefExpr | test.swift:8:10:8:10 | DeclRefExpr |
@@ -10,8 +7,13 @@
107
| test.swift:9:15:9:15 | DeclRefExpr | test.swift:11:8:11:8 | DeclRefExpr |
118
| test.swift:12:9:12:14 | WriteDef | test.swift:13:19:13:19 | DeclRefExpr |
129
| test.swift:12:14:12:14 | IntegerLiteralExpr | test.swift:12:9:12:14 | WriteDef |
10+
| test.swift:15:5:15:5 | Phi | test.swift:15:15:15:15 | DeclRefExpr |
1311
| test.swift:15:15:15:15 | DeclRefExpr | test.swift:19:14:19:14 | DeclRefExpr |
12+
| test.swift:17:5:17:10 | WriteDef | test.swift:18:11:18:11 | Phi |
1413
| test.swift:17:10:17:10 | IntegerLiteralExpr | test.swift:17:5:17:10 | WriteDef |
14+
| test.swift:18:11:18:11 | Phi | test.swift:19:9:19:14 | WriteDef |
15+
| test.swift:18:11:18:11 | Phi | test.swift:21:15:21:15 | DeclRefExpr |
16+
| test.swift:19:9:19:14 | WriteDef | test.swift:18:11:18:11 | Phi |
1517
| test.swift:19:14:19:14 | DeclRefExpr | test.swift:19:9:19:14 | WriteDef |
1618
| test.swift:19:14:19:14 | DeclRefExpr | test.swift:19:14:19:14 | DeclRefExpr |
1719
| test.swift:29:18:29:21 | ParamDecl | test.swift:30:15:30:15 | DeclRefExpr |
@@ -22,8 +24,11 @@
2224
| test.swift:42:16:42:19 | WriteDef | test.swift:45:8:45:8 | DeclRefExpr |
2325
| test.swift:43:9:43:13 | WriteDef | test.swift:46:13:46:13 | DeclRefExpr |
2426
| test.swift:43:19:43:26 | CallExpr | test.swift:43:9:43:13 | WriteDef |
27+
| test.swift:46:9:46:13 | WriteDef | test.swift:50:5:50:5 | Phi |
2528
| test.swift:46:13:46:13 | DeclRefExpr | test.swift:46:9:46:13 | WriteDef |
29+
| test.swift:48:9:48:13 | WriteDef | test.swift:50:5:50:5 | Phi |
2630
| test.swift:48:13:48:13 | IntegerLiteralExpr | test.swift:48:9:48:13 | WriteDef |
31+
| test.swift:50:5:50:5 | Phi | test.swift:50:15:50:15 | DeclRefExpr |
2732
| test.swift:58:9:58:12 | WriteDef | test.swift:59:15:59:15 | DeclRefExpr |
2833
| test.swift:58:18:58:18 | IntegerLiteralExpr | test.swift:58:9:58:12 | WriteDef |
2934
| test.swift:59:15:59:15 | DeclRefExpr | test.swift:60:23:60:23 | DeclRefExpr |

0 commit comments

Comments
 (0)