Skip to content

Commit 2cf043c

Browse files
committed
Rust: Address PR comments
1 parent 59f3f1f commit 2cf043c

File tree

3 files changed

+61
-63
lines changed

3 files changed

+61
-63
lines changed

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

Lines changed: 1 addition & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -214,7 +214,7 @@ module Ssa {
214214
* variable.
215215
*
216216
* This is either the value in a direct assignment, `x = value`, or in a
217-
* `let` statement, `let x = value`. Note that patterns on the rhs. are
217+
* `let` statement, `let x = value`. Note that patterns on the lhs. are
218218
* currently not supported.
219219
*/
220220
predicate assigns(ExprCfgNode value) {
@@ -337,37 +337,4 @@ module Ssa {
337337

338338
override Location getLocation() { result = this.getBasicBlock().getLocation() }
339339
}
340-
341-
/**
342-
* An SSA definition inserted at a call that may update the value of a captured
343-
* variable. For example, in
344-
*
345-
* ```rust
346-
* fn capture_mut() {
347-
* let mut y = 0;
348-
* (0..5).for_each(|| {
349-
* y += x
350-
* });
351-
* y
352-
* }
353-
* ```
354-
*
355-
* a definition for `y` is inserted at the call to `for_each`.
356-
*/
357-
class CapturedCallDefinition extends Definition, SsaImpl::UncertainWriteDefinition {
358-
CapturedCallDefinition() {
359-
exists(Variable v, BasicBlock bb, int i |
360-
this.definesAt(v, bb, i) and
361-
SsaImpl::capturedCallWrite(_, bb, i, v)
362-
)
363-
}
364-
365-
/**
366-
* Gets the immediately preceding definition. Since this update is uncertain,
367-
* the value from the preceding definition might still be valid.
368-
*/
369-
final Definition getPriorDefinition() { result = SsaImpl::uncertainWriteDefinitionInput(this) }
370-
371-
override string toString() { result = "<captured exit> " + this.getSourceVariable() }
372-
}
373340
}

rust/ql/lib/codeql/rust/dataflow/internal/DataFlowImpl.qll

Lines changed: 27 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -102,18 +102,18 @@ final class ParameterPosition extends TParameterPosition {
102102
predicate isSelf() { this = TSelfParameterPosition() }
103103

104104
/**
105-
* Holds if this position represents a reference to a lambda itself. Only
105+
* Holds if this position represents a reference to a closure itself. Only
106106
* used for tracking flow through captured variables.
107107
*/
108-
predicate isLambdaSelf() { this = TLambdaSelfParameterPosition() }
108+
predicate isClosureSelf() { this = TClosureSelfParameterPosition() }
109109

110110
/** Gets a textual representation of this position. */
111111
string toString() {
112112
result = this.getPosition().toString()
113113
or
114114
result = "self" and this.isSelf()
115115
or
116-
result = "lambda self" and this.isLambdaSelf()
116+
result = "closure self" and this.isClosureSelf()
117117
}
118118

119119
ParamBase getParameterIn(ParamList ps) {
@@ -276,15 +276,15 @@ module Node {
276276
* The run-time representation of a closure itself at function entry, viewed
277277
* as a node in a data flow graph.
278278
*/
279-
final class ClosureParameterNode extends ParameterNode, TLambdaSelfReferenceNode {
279+
final class ClosureParameterNode extends ParameterNode, TClosureSelfReferenceNode {
280280
private CfgScope cfgScope;
281281

282-
ClosureParameterNode() { this = TLambdaSelfReferenceNode(cfgScope) }
282+
ClosureParameterNode() { this = TClosureSelfReferenceNode(cfgScope) }
283283

284284
final override CfgScope getCfgScope() { result = cfgScope }
285285

286286
override predicate isParameterOf(DataFlowCallable c, ParameterPosition pos) {
287-
cfgScope = c.asCfgScope() and pos.isLambdaSelf()
287+
cfgScope = c.asCfgScope() and pos.isClosureSelf()
288288
}
289289

290290
override Location getLocation() { result = cfgScope.getLocation() }
@@ -331,7 +331,7 @@ module Node {
331331

332332
override predicate isArgumentOf(DataFlowCall call, RustDataFlow::ArgumentPosition pos) {
333333
call.asCallExprCfgNode() = call_ and
334-
pos.isLambdaSelf()
334+
pos.isClosureSelf()
335335
}
336336
}
337337

@@ -403,6 +403,24 @@ module Node {
403403
override DataFlowCall getCall(ReturnKind kind) { result = call and kind = kind_ }
404404
}
405405

406+
/**
407+
* A synthesized data flow node representing a closure object that tracks
408+
* captured variables.
409+
*/
410+
class CaptureNode extends Node, TCaptureNode {
411+
private VariableCapture::Flow::SynthesizedCaptureNode cn;
412+
413+
CaptureNode() { this = TCaptureNode(cn) }
414+
415+
VariableCapture::Flow::SynthesizedCaptureNode getSynthesizedCaptureNode() { result = cn }
416+
417+
override CfgScope getCfgScope() { result = cn.getEnclosingCallable() }
418+
419+
override Location getLocation() { result = cn.getLocation() }
420+
421+
override string toString() { result = cn.toString() }
422+
}
423+
406424
/**
407425
* A node associated with an object after an operation that might have
408426
* changed its state.
@@ -458,24 +476,6 @@ module Node {
458476
final override string toString() { result = PostUpdateNode.super.toString() }
459477
}
460478

461-
/**
462-
* A synthesized data flow node representing a closure object that tracks
463-
* captured variables.
464-
*/
465-
class CaptureNode extends Node, TCaptureNode {
466-
private VariableCapture::Flow::SynthesizedCaptureNode cn;
467-
468-
CaptureNode() { this = TCaptureNode(cn) }
469-
470-
VariableCapture::Flow::SynthesizedCaptureNode getSynthesizedCaptureNode() { result = cn }
471-
472-
override CfgScope getCfgScope() { result = cn.getEnclosingCallable() }
473-
474-
override Location getLocation() { result = cn.getLocation() }
475-
476-
override string toString() { result = cn.toString() }
477-
}
478-
479479
final class CastNode = NaNode;
480480
}
481481

@@ -847,8 +847,6 @@ module RustDataFlow implements InputSig<Location> {
847847
node instanceof Node::CaptureNode
848848
or
849849
node instanceof Node::ClosureParameterNode
850-
or
851-
node instanceof Node::ClosureArgumentNode
852850
}
853851

854852
class DataFlowExpr = ExprCfgNode;
@@ -1383,7 +1381,7 @@ private module Cached {
13831381
} or
13841382
TSsaNode(SsaImpl::DataFlowIntegration::SsaNode node) or
13851383
TFlowSummaryNode(FlowSummaryImpl::Private::SummaryNode sn) or
1386-
TLambdaSelfReferenceNode(CfgScope c) { lambdaCreationExpr(c, _) } or
1384+
TClosureSelfReferenceNode(CfgScope c) { lambdaCreationExpr(c, _) } or
13871385
TCaptureNode(VariableCapture::Flow::SynthesizedCaptureNode cn)
13881386

13891387
cached
@@ -1421,7 +1419,7 @@ private module Cached {
14211419
or
14221420
FlowSummaryImpl::ParsePositions::isParsedParameterPosition(_, i)
14231421
} or
1424-
TLambdaSelfParameterPosition() or
1422+
TClosureSelfParameterPosition() or
14251423
TSelfParameterPosition()
14261424

14271425
cached

rust/ql/lib/codeql/rust/dataflow/internal/SsaImpl.qll

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -467,6 +467,39 @@ class PhiReadNode extends DefinitionExt, Impl::PhiReadNode {
467467
override Location getLocation() { result = Impl::PhiReadNode.super.getLocation() }
468468
}
469469

470+
/**
471+
* An SSA definition inserted at a call that may update the value of a captured
472+
* variable. For example, in
473+
*
474+
* ```rust
475+
* fn capture_mut() {
476+
* let mut y = 0;
477+
* (0..5).for_each(|x| {
478+
* y += x
479+
* });
480+
* y
481+
* }
482+
* ```
483+
*
484+
* a definition for `y` is inserted at the call to `for_each`.
485+
*/
486+
class CapturedCallDefinition extends Definition, Impl::UncertainWriteDefinition {
487+
CapturedCallDefinition() {
488+
exists(Variable v, BasicBlock bb, int i |
489+
this.definesAt(v, bb, i) and
490+
capturedCallWrite(_, bb, i, v)
491+
)
492+
}
493+
494+
/**
495+
* Gets the immediately preceding definition. Since this update is uncertain,
496+
* the value from the preceding definition might still be valid.
497+
*/
498+
final Definition getPriorDefinition() { result = uncertainWriteDefinitionInput(this) }
499+
500+
override string toString() { result = "<captured exit> " + this.getSourceVariable() }
501+
}
502+
470503
private module DataFlowIntegrationInput implements Impl::DataFlowIntegrationInputSig {
471504
class Expr extends CfgNodes::AstCfgNode {
472505
predicate hasCfgNode(SsaInput::BasicBlock bb, int i) { this = bb.getNode(i) }

0 commit comments

Comments
 (0)