Skip to content

Commit ae6d16a

Browse files
authored
Swift: flow into callees via params
1 parent 25c8b81 commit ae6d16a

File tree

6 files changed

+41
-5
lines changed

6 files changed

+41
-5
lines changed

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

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,13 @@ class ParameterPosition extends TParameterPosition {
9494
string toString() { none() }
9595
}
9696

97+
class PositionalParameterPosition extends ParameterPosition, TPositionalParameter {
98+
int getIndex() {
99+
this = TPositionalParameter(result)
100+
}
101+
}
102+
103+
97104
/** An argument position. */
98105
class ArgumentPosition extends TArgumentPosition {
99106
/** Gets a textual representation of this position. */
@@ -108,4 +115,9 @@ class PositionalArgumentPosition extends ArgumentPosition, TPositionalArgument {
108115

109116
/** Holds if arguments at position `apos` match parameters at position `ppos`. */
110117
pragma[inline]
111-
predicate parameterMatch(ParameterPosition ppos, ArgumentPosition apos) { none() }
118+
predicate parameterMatch(ParameterPosition ppos, ArgumentPosition apos) {
119+
ppos instanceof TThisParameter and
120+
apos instanceof TThisArgument
121+
or
122+
ppos.(PositionalParameterPosition).getIndex() = apos.(PositionalArgumentPosition).getIndex()
123+
}

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

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ private import DataFlowPublic
33
private import DataFlowDispatch
44
private import codeql.swift.controlflow.CfgNodes
55
private import codeql.swift.dataflow.Ssa
6+
private import codeql.swift.controlflow.BasicBlocks
67

78
/** Gets the callable in which this node occurs. */
89
DataFlowCallable nodeGetEnclosingCallable(NodeImpl n) { result = n.getEnclosingCallable() }
@@ -51,7 +52,6 @@ private module Cached {
5152
cached
5253
newtype TNode =
5354
TExprNode(ExprCfgNode e) or
54-
TNormalParameterNode(ParamDecl p) or
5555
TSsaDefinitionNode(Ssa::Definition def)
5656

5757
private predicate localFlowStepCommon(Node nodeFrom, Node nodeTo) {
@@ -99,10 +99,15 @@ private module ParameterNodes {
9999
predicate isParameterOf(DataFlowCallable c, ParameterPosition pos) { none() }
100100
}
101101

102-
class NormalParameterNode extends ParameterNodeImpl, TNormalParameterNode {
102+
class NormalParameterNode extends ParameterNodeImpl, SsaDefinitionNode {
103103
ParamDecl param;
104104

105-
NormalParameterNode() { this = TNormalParameterNode(param) }
105+
NormalParameterNode() {
106+
exists(BasicBlock bb, int i |
107+
super.asDefinition().definesAt(param, bb, i) and
108+
bb.getNode(i).getNode().asAstNode() = param
109+
)
110+
}
106111

107112
override Location getLocationImpl() { result = param.getLocation() }
108113

@@ -115,6 +120,8 @@ private module ParameterNodes {
115120
pos = TPositionalParameter(index)
116121
)
117122
}
123+
124+
override DataFlowCallable getEnclosingCallable() { isParameterOf(result, _) }
118125
}
119126
}
120127

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ class ExprNode extends Node, TExprNode {
6969
* The value of a parameter at function entry, viewed as a node in a data
7070
* flow graph.
7171
*/
72-
class ParameterNode extends Node, TNormalParameterNode instanceof ParameterNodeImpl { }
72+
class ParameterNode extends Node, SsaDefinitionNode instanceof ParameterNodeImpl { }
7373

7474
/**
7575
*/

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,10 @@ predicate variableWrite(BasicBlock bb, int i, SourceVariable v, boolean certain)
2727
v.getParentPattern() = pattern and
2828
certain = true
2929
)
30+
or
31+
v instanceof ParamDecl and
32+
bb.getNode(i).getNode().asAstNode() = v and
33+
certain = true
3034
}
3135

3236
private predicate isLValue(DeclRefExpr ref) { any(AssignExpr assign).getDest() = ref }

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

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,24 @@ edges
22
| test.swift:6:19:6:26 | CallExpr : | test.swift:7:15:7:15 | DeclRefExpr |
33
| test.swift:6:19:6:26 | CallExpr : | test.swift:9:15:9:15 | DeclRefExpr |
44
| test.swift:6:19:6:26 | CallExpr : | test.swift:10:15:10:15 | DeclRefExpr |
5+
| test.swift:25:20:25:27 | CallExpr : | test.swift:28:18:28:21 | ParamDecl : |
6+
| test.swift:25:20:25:27 | CallExpr : | test.swift:28:18:28:21 | WriteDef : |
7+
| test.swift:28:18:28:21 | ParamDecl : | test.swift:29:15:29:15 | DeclRefExpr |
8+
| test.swift:28:18:28:21 | WriteDef : | test.swift:29:15:29:15 | DeclRefExpr |
59
nodes
610
| test.swift:6:19:6:26 | CallExpr : | semmle.label | CallExpr : |
711
| test.swift:7:15:7:15 | DeclRefExpr | semmle.label | DeclRefExpr |
812
| test.swift:9:15:9:15 | DeclRefExpr | semmle.label | DeclRefExpr |
913
| test.swift:10:15:10:15 | DeclRefExpr | semmle.label | DeclRefExpr |
14+
| test.swift:25:20:25:27 | CallExpr : | semmle.label | CallExpr : |
15+
| test.swift:28:18:28:21 | ParamDecl : | semmle.label | ParamDecl : |
16+
| test.swift:28:18:28:21 | ParamDecl : | semmle.label | WriteDef : |
17+
| test.swift:28:18:28:21 | WriteDef : | semmle.label | ParamDecl : |
18+
| test.swift:28:18:28:21 | WriteDef : | semmle.label | WriteDef : |
19+
| test.swift:29:15:29:15 | DeclRefExpr | semmle.label | DeclRefExpr |
1020
subpaths
1121
#select
1222
| test.swift:6:19:6:26 | CallExpr : | test.swift:7:15:7:15 | DeclRefExpr |
1323
| test.swift:6:19:6:26 | CallExpr : | test.swift:9:15:9:15 | DeclRefExpr |
1424
| test.swift:6:19:6:26 | CallExpr : | test.swift:10:15:10:15 | DeclRefExpr |
25+
| test.swift:25:20:25:27 | CallExpr : | test.swift:29:15:29:15 | DeclRefExpr |

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,3 +13,5 @@
1313
| test.swift:17:10:17:10 | IntegerLiteralExpr | test.swift:17:5:17:10 | WriteDef |
1414
| test.swift:19:14:19:14 | DeclRefExpr | test.swift:19:9:19:14 | WriteDef |
1515
| test.swift:19:14:19:14 | DeclRefExpr | test.swift:19:14:19:14 | DeclRefExpr |
16+
| test.swift:28:18:28:21 | ParamDecl | test.swift:29:15:29:15 | DeclRefExpr |
17+
| test.swift:28:18:28:21 | WriteDef | test.swift:29:15:29:15 | DeclRefExpr |

0 commit comments

Comments
 (0)