Skip to content

Commit 820d0a5

Browse files
authored
Merge pull request #23316 from hvitved/unified/dataflow-paramex
Unified: Use `ParameterEx` for all parameters in data flow
2 parents 49b4b67 + 2ccd2ab commit 820d0a5

9 files changed

Lines changed: 118 additions & 72 deletions

File tree

‎unified/ql/lib/codeql/unified/internal/ParameterEx.qll‎

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ private newtype TParameterEx =
1111
TImplicitReceiverParameter(Callable c) {
1212
exists(LocalVariable v | v.isImplicitReceiverParameter(c))
1313
} or
14+
TImplicitCalleeParameter(Callable c) or
1415
TImplicitFunctionExprParameter(FunctionExpr fe, int i) {
1516
i = [0 .. fe.getNumberOfImplicitParameters() - 1]
1617
} or
@@ -40,6 +41,13 @@ abstract private class ParameterExImpl extends TParameterEx {
4041
*/
4142
predicate isImplicitReceiverParameter(Callable c) { this = TImplicitReceiverParameter(c) }
4243

44+
/**
45+
* Holds if this entity represents an implicit callee parameter of `c`.
46+
*
47+
* Only used to model variable capture.
48+
*/
49+
predicate isImplicitCalleeParameter(Callable c) { this = TImplicitCalleeParameter(c) }
50+
4351
/** Holds if this entity represents implicit parameter `i` of `fe`. */
4452
predicate isImplicitFunctionExprParameter(FunctionExpr fe, int i) {
4553
this = TImplicitFunctionExprParameter(fe, i)
@@ -75,6 +83,8 @@ abstract private class ParameterExImpl extends TParameterEx {
7583
this.isImplicitFunctionExprParameter(result, _)
7684
or
7785
this.isDefaultConstructorParameter(result, _, _)
86+
or
87+
this.isImplicitCalleeParameter(result)
7888
}
7989

8090
/** Gets the external name of this parameter, if any. */
@@ -86,6 +96,11 @@ abstract private class ParameterExImpl extends TParameterEx {
8696
/** Gets the type declaration of this parameter, if any. */
8797
abstract AstNode getType();
8898

99+
/**
100+
* Gets the control flow node associated with this parameter.
101+
*/
102+
abstract ControlFlowNode getControlFlowNode();
103+
89104
/** Gets a textual representation of this parameter. */
90105
abstract string toString();
91106

@@ -104,6 +119,13 @@ private class AstParameterEx extends ParameterExImpl, TAstParameter {
104119

105120
override AstNode getType() { result = p.getType() }
106121

122+
override ControlFlowNode getControlFlowNode() {
123+
result.injects(p.getPattern())
124+
or
125+
not exists(p.getPattern()) and
126+
result.injects(p)
127+
}
128+
107129
override string toString() { result = p.toString() }
108130

109131
override Location getLocation() { result = p.getLocation() }
@@ -120,6 +142,10 @@ private class ImplicitReceiverParameterEx extends ParameterExImpl, TImplicitRece
120142

121143
override AstNode getType() { none() }
122144

145+
override ControlFlowNode getControlFlowNode() {
146+
result.(ControlFlow::EntryNode).getEnclosingCallable() = c
147+
}
148+
123149
override string toString() {
124150
exists(LocalVariable v |
125151
v.isImplicitReceiverParameter(c) and
@@ -130,6 +156,26 @@ private class ImplicitReceiverParameterEx extends ParameterExImpl, TImplicitRece
130156
override Location getLocation() { result = c.getLocation() }
131157
}
132158

159+
private class ImplicitCalleeParameter extends ParameterExImpl, TImplicitCalleeParameter {
160+
Callable c;
161+
162+
ImplicitCalleeParameter() { this = TImplicitCalleeParameter(c) }
163+
164+
override string getExternalName() { none() }
165+
166+
override predicate hasDefault() { none() }
167+
168+
override AstNode getType() { none() }
169+
170+
override ControlFlowNode getControlFlowNode() {
171+
result.(ControlFlow::EntryNode).getEnclosingCallable() = c
172+
}
173+
174+
override string toString() { result = "implicit callee parameter" }
175+
176+
override Location getLocation() { result = c.getLocation() }
177+
}
178+
133179
/**
134180
* An implicit parameter of a function expression inferred from usage of
135181
* implicit parameters inside its body.
@@ -148,6 +194,10 @@ private class ImplicitFunctionExprParameterEx extends ParameterExImpl,
148194

149195
override AstNode getType() { none() }
150196

197+
override ControlFlowNode getControlFlowNode() {
198+
result.(ControlFlow::EntryNode).getEnclosingCallable() = fe
199+
}
200+
151201
override string toString() { result = "implicit parameter " + i }
152202

153203
override Location getLocation() { result = fe.getLocation() }
@@ -167,6 +217,10 @@ private class DefaultConstructorParameterEx extends ParameterExImpl, TDefaultCon
167217

168218
override predicate hasDefault() { hasDefault = true }
169219

220+
override ControlFlowNode getControlFlowNode() {
221+
result.(ControlFlow::EntryNode).getEnclosingCallable() = cd
222+
}
223+
170224
override string toString() { result = name + " [" + cd.getName() + " default constructor]" }
171225

172226
override Location getLocation() { result = cd.getLocation() }

‎unified/ql/lib/codeql/unified/internal/dataflow/CaptureSsa.qll‎

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -137,9 +137,7 @@ Node getNodeFromCaptureSsaNode(CaptureSsaOutput::ClosureNode n) {
137137
exists(CaptureSsaOutput::ThisParameterNode thisParam, Callable callable |
138138
n = thisParam and
139139
callable = thisParam.getCallable() and
140-
result
141-
.(BuilderNode)
142-
.isImplicitParameter(toDataFlowCallable(callable), any(ParameterPosition p | p.isCallee()))
140+
result.(BuilderNode).asParameterEx().isImplicitCalleeParameter(callable)
143141
)
144142
}
145143

‎unified/ql/lib/codeql/unified/internal/dataflow/DataFlowGraph.qll‎

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,14 +19,14 @@ predicate step(BuilderNode node1, Step step, BuilderNode node2) {
1919
)
2020
or
2121
exists(Callable callable |
22-
node1.isReceiverParameter(callable) and
22+
node1.asParameterEx().isImplicitReceiverParameter(callable) and
2323
step.value() and
2424
node2.isLocalVariableWrite(callable, getImplicitReceiverVariable(callable))
2525
)
2626
or
27-
exists(FunctionExpr fe, LocalVariable var, ParameterPosition pos |
28-
var.isImplicitFunctionExprParameter(fe, pos.asPositional()) and
29-
node1.isImplicitParameter(toDataFlowCallable(fe), pos) and
27+
exists(FunctionExpr fe, LocalVariable var, int pos |
28+
var.isImplicitFunctionExprParameter(fe, pos) and
29+
node1.asParameterEx().isImplicitFunctionExprParameter(fe, pos) and
3030
step.value() and
3131
node2.isLocalVariableWrite(fe, var)
3232
)
@@ -239,7 +239,11 @@ predicate step(BuilderNode node1, Step step, BuilderNode node2) {
239239
node2.isResultValue(expr)
240240
)
241241
or
242-
none() // Temporarily disable compilation errors from unsatisfiable types
242+
exists(Parameter p |
243+
p = node1.asParameterEx().asParameter() and
244+
node2.isIncomingValue(p.getPattern()) and
245+
if exists(p.getDefault()) then step.value() else step.valueMustFlow()
246+
)
243247
}
244248

245249
/** Holds if `node` should be included in the debug view. */

‎unified/ql/lib/codeql/unified/internal/dataflow/DataFlowInstantiation.qll‎

Lines changed: 23 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ private import unified
22
private import AllDataFlow
33
private import AllDataFlow as D
44
private import codeql.dataflow.DataFlow
5+
private import codeql.unified.internal.ParameterEx
56
private import codeql.util.Void
67
private import codeql.util.Unit
78

@@ -74,20 +75,31 @@ module DataFlowInput implements InputSig<Location> {
7475

7576
DataFlowCallable nodeGetEnclosingCallable(Node node) { result = node.getEnclosingCallableEx() }
7677

77-
private predicate isParameterNodeImpl(Node p, DataFlowCallable c, ParameterPosition pos) {
78-
exists(Parameter param |
79-
p.asExpr() = param.getPattern() and
80-
c.asSourceCallable() = param.getEnclosingCallable()
78+
private predicate isParameterNodeImpl(Node n, DataFlowCallable c, ParameterPosition pos) {
79+
exists(ParameterEx p, Callable callable |
80+
p = n.(BuilderNode).asParameterEx() and
81+
callable = c.asSourceCallable()
8182
|
82-
pos.asPositional() = param.getPositionalIndex()
83+
exists(Parameter param |
84+
param = p.asParameter() and
85+
callable = param.getDeclaringCallable()
86+
|
87+
pos.asPositional() = param.getPositionalIndex()
88+
or
89+
pos.asNamed() = param.getExternalName()
90+
)
8391
or
84-
pos.asNamed() = param.getExternalName()
92+
p.isImplicitReceiverParameter(callable) and
93+
pos.isReceiver()
94+
or
95+
p.isImplicitCalleeParameter(callable) and
96+
pos.isCallee()
97+
or
98+
p.isImplicitFunctionExprParameter(callable, pos.asPositional())
8599
)
86100
or
87-
p.(BuilderNode).isImplicitParameter(c, pos)
88-
or
89-
FlowSummaryOutput::summaryParameterNode(getSummaryNode(p), pos) and
90-
c = p.getEnclosingCallableEx()
101+
FlowSummaryOutput::summaryParameterNode(getSummaryNode(n), pos) and
102+
c = n.getEnclosingCallableEx()
91103
}
92104

93105
class ParameterNode extends Node {
@@ -280,7 +292,7 @@ module DataFlowInput implements InputSig<Location> {
280292
predicate isUnreachableInCall(NodeRegion nr, DataFlowCall call) { none() } // TODO
281293

282294
predicate allowParameterReturnInSelf(ParameterNode p) {
283-
p.isReceiverParameter(_)
295+
p.(BuilderNode).asParameterEx().isImplicitReceiverParameter(_)
284296
or
285297
captureSsaAllowParameterReturnInSelf(p)
286298
or

‎unified/ql/lib/codeql/unified/internal/dataflow/DataFlowNode.qll‎

Lines changed: 20 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ private import unified
22
private import AllDataFlow
33
private import codeql.unified.internal.ExprPositions
44
private import codeql.unified.internal.LocalNameBinding
5+
private import codeql.unified.internal.ParameterEx
56
private import codeql.util.Boolean
67

78
private predicate hasIncomingValueAtCfgNode(Expr expr, ControlFlowNode cfgNode) {
@@ -122,15 +123,7 @@ newtype TDataFlowNode =
122123
not exists(callable.asSummarizedCallable()) // represented by TSummaryNode
123124
} or
124125
TCallableNode(DataFlowCallable callable) or
125-
TImplicitParameterNode(DataFlowCallable callable, ParameterPosition pos) {
126-
not exists(callable.asSummarizedCallable()) and // represented by TSummaryNode
127-
(
128-
pos instanceof ImplicitParameterPosition
129-
or
130-
pos.asPositional() =
131-
[0 .. callable.asSourceCallable().(FunctionExpr).getNumberOfImplicitParameters() - 1]
132-
)
133-
} or
126+
TParameterExNode(ParameterEx p) { exists(toDataFlowCallable(p.getCallable())) } or
134127
TImplicitArgumentNode(DataFlowCall call, ImplicitArgumentPosition pos, Boolean isPost) {
135128
not call.isSummaryCall(_, _) // represented by TSummaryNode
136129
} or
@@ -140,7 +133,7 @@ newtype TDataFlowNode =
140133

141134
class TDataFlowNodeStage1 =
142135
TValueNode or TStrictlyIncomingValue or TExprPostUpdateNode or TLocalVariableRefNode or
143-
TReturnNode or TCallableNode or TImplicitParameterNode or TImplicitArgumentNode;
136+
TReturnNode or TCallableNode or TParameterExNode or TImplicitArgumentNode;
144137

145138
class TDataFlowNodeStage2 = TDataFlowNodeStage1 or TLocalSsaNode;
146139

@@ -214,26 +207,14 @@ class BuilderNode extends TDataFlowNodeStage1 {
214207
*/
215208
predicate isReturnNodeEx(DataFlowCallable callable) { this = TReturnNode(callable) }
216209

217-
/** Holds if this node represents an implicit parameter of `callable`. */
218-
predicate isImplicitParameter(DataFlowCallable callable, ParameterPosition pos) {
219-
this = TImplicitParameterNode(callable, pos)
220-
}
210+
/** Gets the `ParameterEx` associated with this node, if any. */
211+
ParameterEx asParameterEx() { this = TParameterExNode(result) }
221212

222213
/** Holds if this node represents an implicit argument to `call` (or its post-update). */
223214
predicate isImplicitArgument(DataFlowCall call, ImplicitArgumentPosition pos, boolean isPost) {
224215
this = TImplicitArgumentNode(call, pos, isPost)
225216
}
226217

227-
/**
228-
* Holds if this represents the receiver passed to the given callable.
229-
*
230-
* Note that for non-methods and closures that capture the receiver from the enclosing method,
231-
* this node still exists but will typically not flow anywhere.
232-
*/
233-
predicate isReceiverParameter(Callable callable) {
234-
this.isImplicitParameter(toDataFlowCallable(callable), any(ParameterPosition p | p.isReceiver()))
235-
}
236-
237218
/** Holds if this node represents the receiver argument passed to `call`. */
238219
predicate isReceiverArgumentEx(DataFlowCall call) {
239220
this.isImplicitArgument(call, any(ArgumentPosition p | p.isReceiver()), false)
@@ -318,12 +299,12 @@ class BuilderNode extends TDataFlowNodeStage1 {
318299
result = "[variable " + kind + "] " + v.toString()
319300
)
320301
or
302+
exists(ParameterEx p |
303+
this = TParameterExNode(p) and
304+
result = p.toString()
305+
)
306+
or
321307
exists(DataFlowCallable callable |
322-
exists(ParameterPosition pos |
323-
this.isImplicitParameter(callable, pos) and
324-
result = "[" + pos + " param] " + callable.toString()
325-
)
326-
or
327308
this.isCallableEx(callable) and
328309
result = "[callable] " + callable.toString()
329310
)
@@ -351,11 +332,13 @@ class BuilderNode extends TDataFlowNodeStage1 {
351332
result = repr.getLocation()
352333
)
353334
or
335+
exists(ParameterEx p |
336+
this = TParameterExNode(p) and
337+
result = p.getLocation()
338+
)
339+
or
354340
exists(DataFlowCallable callable |
355-
this.isImplicitParameter(callable, _)
356-
or
357-
this.isCallableEx(callable)
358-
|
341+
this.isCallableEx(callable) and
359342
result = callable.getLocation()
360343
)
361344
or
@@ -394,10 +377,9 @@ class BuilderNode extends TDataFlowNodeStage1 {
394377
performsVariableAccess(repr, var, kind, cfgNode)
395378
)
396379
or
380+
cfgNode = this.asParameterEx().getControlFlowNode()
381+
or
397382
exists(DataFlowCallable callable |
398-
this.isImplicitParameter(callable, _) and
399-
cfgNode.(ControlFlow::EntryNode).getEnclosingCallable() = callable.asSourceCallable()
400-
or
401383
this.isCallableEx(callable) and
402384
cfgNode.injects(callable.asSourceCallable())
403385
)
@@ -504,7 +486,7 @@ class Node extends TDataFlowNode {
504486
* this node still exists but will typically not flow anywhere.
505487
*/
506488
predicate isReceiverParameter(Callable callable) {
507-
this.(BuilderNode).isReceiverParameter(callable)
489+
this.(BuilderNode).asParameterEx().isImplicitReceiverParameter(callable)
508490
}
509491

510492
/** Holds if this node represents the receiver argument passed to `call`. */
@@ -575,7 +557,7 @@ class Node extends TDataFlowNode {
575557
result.asSourceCallable() = node.getEnclosingCallable()
576558
)
577559
or
578-
this.(BuilderNode).isImplicitParameter(result, _)
560+
result.asSourceCallable() = this.(BuilderNode).asParameterEx().getCallable()
579561
or
580562
exists(DataFlowCall call |
581563
this.(BuilderNode).isImplicitArgument(call, _, _) and

‎unified/ql/test/library-tests/dataflow/test.expected‎

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -108,8 +108,7 @@ edges
108108
| calls.swift:49:32:49:32 | y | calls.swift:51:14:51:14 | y | provenance | |
109109
| calls.swift:53:12:53:25 | source(...) | calls.swift:49:19:49:19 | x | provenance | |
110110
| calls.swift:53:28:53:41 | source(...) | calls.swift:49:32:49:32 | y | provenance | |
111-
| calls.swift:61:19:61:19 | x [Return] [field] | calls.swift:66:12:66:12 | [post] b [field] | provenance | |
112-
| calls.swift:62:9:62:9 | [post] x [field] | calls.swift:61:19:61:19 | x [Return] [field] | provenance | |
111+
| calls.swift:62:9:62:9 | [post] x [field] | calls.swift:66:12:66:12 | [post] b [field] | provenance | |
113112
| calls.swift:62:9:62:15 | ... .field | calls.swift:62:9:62:9 | [post] x [field] | provenance | |
114113
| calls.swift:62:19:62:32 | source(...) | calls.swift:62:9:62:15 | ... .field | provenance | |
115114
| calls.swift:66:12:66:12 | [post] b [field] | calls.swift:67:10:67:10 | b [field] | provenance | |
@@ -580,8 +579,7 @@ edges
580579
| subscripts.swift:143:26:143:40 | source(...) | subscripts.swift:143:5:143:22 | ... .subscript(...) | provenance | |
581580
| subscripts.swift:144:10:144:10 | t [field] | subscripts.swift:144:10:144:21 | ... .getField(...) | provenance | |
582581
| subscripts.swift:154:24:154:31 | newValue [field] | subscripts.swift:154:24:154:37 | ... .field | provenance | |
583-
| subscripts.swift:157:19:157:19 | s [Return] [field] | subscripts.swift:159:12:159:16 | [post] & ... [field] | provenance | |
584-
| subscripts.swift:157:33:157:33 | [post] s [field] | subscripts.swift:157:19:157:19 | s [Return] [field] | provenance | |
582+
| subscripts.swift:157:33:157:33 | [post] s [field] | subscripts.swift:159:12:159:16 | [post] & ... [field] | provenance | |
585583
| subscripts.swift:157:33:157:39 | ... .field | subscripts.swift:157:33:157:33 | [post] s [field] | provenance | |
586584
| subscripts.swift:157:43:157:57 | source(...) | subscripts.swift:157:33:157:39 | ... .field | provenance | |
587585
| subscripts.swift:159:12:159:16 | [post] & ... [field] | subscripts.swift:159:13:159:16 | [post] ... .subscript(...) [field] | provenance | |
@@ -917,7 +915,6 @@ nodes
917915
| calls.swift:51:14:51:14 | y | semmle.label | y |
918916
| calls.swift:53:12:53:25 | source(...) | semmle.label | source(...) |
919917
| calls.swift:53:28:53:41 | source(...) | semmle.label | source(...) |
920-
| calls.swift:61:19:61:19 | x [Return] [field] | semmle.label | x [Return] [field] |
921918
| calls.swift:62:9:62:9 | [post] x [field] | semmle.label | [post] x [field] |
922919
| calls.swift:62:9:62:15 | ... .field | semmle.label | ... .field |
923920
| calls.swift:62:19:62:32 | source(...) | semmle.label | source(...) |
@@ -1461,7 +1458,6 @@ nodes
14611458
| subscripts.swift:144:10:144:21 | ... .getField(...) | semmle.label | ... .getField(...) |
14621459
| subscripts.swift:154:24:154:31 | newValue [field] | semmle.label | newValue [field] |
14631460
| subscripts.swift:154:24:154:37 | ... .field | semmle.label | ... .field |
1464-
| subscripts.swift:157:19:157:19 | s [Return] [field] | semmle.label | s [Return] [field] |
14651461
| subscripts.swift:157:33:157:33 | [post] s [field] | semmle.label | [post] s [field] |
14661462
| subscripts.swift:157:33:157:39 | ... .field | semmle.label | ... .field |
14671463
| subscripts.swift:157:43:157:57 | source(...) | semmle.label | source(...) |

‎unified/ql/test/library-tests/mad/test.expected‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ isSource
5757
| test.swift:89:9:89:24 | ... .text | local |
5858
| test.swift:176:13:176:16 | text | local |
5959
| test.swift:177:13:177:21 | ... .text | local |
60-
| test.swift:209:5:213:5 | [self param] func ShorthandTestSub.sourceParams | shorthand-source |
60+
| test.swift:209:5:213:5 | self | shorthand-source |
6161
| test.swift:210:11:210:11 | x | shorthand-source |
6262
| test.swift:232:13:232:13 | v | subscript-setter-source |
6363
| test.swift:253:11:253:11 | x | optional-source |

0 commit comments

Comments
 (0)