Skip to content

Commit 63d84ed

Browse files
committed
Rust: Fix minor issues from PR feedback
1 parent 9f0fba1 commit 63d84ed

File tree

9 files changed

+29
-33
lines changed

9 files changed

+29
-33
lines changed

rust/ql/lib/codeql/rust/controlflow/ControlFlowGraph.qll

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,6 @@ private import BasicBlocks
99

1010
final class CfgScope = Scope::CfgScope;
1111

12-
predicate getEnclosingCfgScope = Scope::getEnclosingCfgScope/1;
13-
1412
final class SuccessorType = SuccessorTypeImpl;
1513

1614
final class NormalSuccessor = NormalSuccessorImpl;
@@ -54,6 +52,3 @@ final class CfgNode extends Node {
5452
/** Gets the basic block that this control flow node belongs to. */
5553
BasicBlock getBasicBlock() { result.getANode() = this }
5654
}
57-
58-
/** Holds if evaluating `e` jumps to the evaluation of a different CFG scope. */
59-
predicate isControlFlowJump(Expr e) { e instanceof CallExprBase or e instanceof AwaitExpr }

rust/ql/lib/codeql/rust/controlflow/internal/ControlFlowGraphImpl.qll

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ private module CfgInput implements InputSig<Location> {
2323
class CfgScope = Scope::CfgScope;
2424

2525
CfgScope getCfgScope(AstNode n) {
26-
result = Scope::getEnclosingCfgScope(n) and
26+
result = n.getEnclosingCfgScope() and
2727
Stages::CfgStage::ref()
2828
}
2929

rust/ql/lib/codeql/rust/controlflow/internal/Scope.qll

Lines changed: 4 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -16,13 +16,12 @@ abstract private class CfgScopeImpl extends AstNode {
1616

1717
final class CfgScope = CfgScopeImpl;
1818

19-
final class AsyncBlockScope extends CfgScopeImpl, AsyncBlockExpr {
20-
override predicate scopeFirst(AstNode first) {
21-
first(this.(ExprTrees::AsyncBlockExprTree).getFirstChildNode(), first)
22-
}
19+
final class AsyncBlockScope extends CfgScopeImpl, AsyncBlockExpr instanceof ExprTrees::AsyncBlockExprTree
20+
{
21+
override predicate scopeFirst(AstNode first) { first(super.getFirstChildNode(), first) }
2322

2423
override predicate scopeLast(AstNode last, Completion c) {
25-
last(this.(ExprTrees::AsyncBlockExprTree).getLastChildElement(), last, c)
24+
last(super.getLastChildElement(), last, c)
2625
}
2726
}
2827

@@ -52,13 +51,3 @@ final class CallableScope extends CfgScopeImpl, Callable {
5251
/** Holds if `scope` is exited when `last` finishes with completion `c`. */
5352
override predicate scopeLast(AstNode last, Completion c) { last(this.getBody(), last, c) }
5453
}
55-
56-
/** Gets the CFG scope that encloses `node`, if any. */
57-
CfgScope getEnclosingCfgScope(AstNode node) {
58-
exists(AstNode p | p = node.getParentNode() |
59-
result = p
60-
or
61-
not p instanceof CfgScope and
62-
result = getEnclosingCfgScope(p)
63-
)
64-
}

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -134,7 +134,7 @@ module Node {
134134

135135
ExprNode() { this = TExprNode(n) }
136136

137-
override CfgScope getCfgScope() { result = getEnclosingCfgScope(this.asExpr()) }
137+
override CfgScope getCfgScope() { result = this.asExpr().getEnclosingCfgScope() }
138138

139139
override Location getLocation() { result = n.getExpr().getLocation() }
140140

@@ -154,7 +154,7 @@ module Node {
154154

155155
ParameterNode() { this = TParameterNode(parameter) }
156156

157-
override CfgScope getCfgScope() { result = getEnclosingCfgScope(parameter) }
157+
override CfgScope getCfgScope() { result = parameter.getEnclosingCfgScope() }
158158

159159
override Location getLocation() { result = parameter.getLocation() }
160160

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

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -156,7 +156,7 @@ private predicate variableReadActual(BasicBlock bb, int i, Variable v) {
156156
*/
157157
pragma[noinline]
158158
private predicate hasCapturedWrite(Variable v, Cfg::CfgScope scope) {
159-
any(VariableWriteAccess write | write.getVariable() = v and scope = getEnclosingCfgScope+(write))
159+
any(VariableWriteAccess write | write.getVariable() = v and scope = write.getEnclosingCfgScope+())
160160
.isCapture()
161161
}
162162

@@ -168,7 +168,7 @@ pragma[noinline]
168168
private predicate variableReadActualInOuterScope(
169169
BasicBlock bb, int i, Variable v, Cfg::CfgScope scope
170170
) {
171-
variableReadActual(bb, i, v) and bb.getScope() = getEnclosingCfgScope(scope)
171+
variableReadActual(bb, i, v) and bb.getScope() = scope.getEnclosingCfgScope()
172172
}
173173

174174
pragma[noinline]
@@ -263,7 +263,7 @@ private predicate readsCapturedVariable(BasicBlock bb, Variable v) {
263263
*/
264264
pragma[noinline]
265265
private predicate hasCapturedRead(Variable v, Cfg::CfgScope scope) {
266-
any(VariableReadAccess read | read.getVariable() = v and scope = getEnclosingCfgScope+(read))
266+
any(VariableReadAccess read | read.getVariable() = v and scope = read.getEnclosingCfgScope+())
267267
.isCapture()
268268
}
269269

@@ -273,15 +273,18 @@ private predicate hasCapturedRead(Variable v, Cfg::CfgScope scope) {
273273
*/
274274
pragma[noinline]
275275
private predicate variableWriteInOuterScope(BasicBlock bb, int i, Variable v, Cfg::CfgScope scope) {
276-
SsaInput::variableWrite(bb, i, v, _) and getEnclosingCfgScope(scope) = bb.getScope()
276+
SsaInput::variableWrite(bb, i, v, _) and scope.getEnclosingCfgScope() = bb.getScope()
277277
}
278278

279+
/** Holds if evaluating `e` jumps to the evaluation of a different CFG scope. */
280+
private predicate isControlFlowJump(Expr e) { e instanceof CallExprBase or e instanceof AwaitExpr }
281+
279282
/**
280283
* Holds if the call `call` at index `i` in basic block `bb` may reach
281284
* a callable that reads captured variable `v`.
282285
*/
283286
private predicate capturedCallRead(Expr call, BasicBlock bb, int i, Variable v) {
284-
Cfg::isControlFlowJump(call) and
287+
isControlFlowJump(call) and
285288
exists(Cfg::CfgScope scope |
286289
hasCapturedRead(v, scope) and
287290
(
@@ -297,7 +300,7 @@ private predicate capturedCallRead(Expr call, BasicBlock bb, int i, Variable v)
297300
* that writes captured variable `v`.
298301
*/
299302
predicate capturedCallWrite(Expr call, BasicBlock bb, int i, Variable v) {
300-
Cfg::isControlFlowJump(call) and
303+
isControlFlowJump(call) and
301304
call = bb.getNode(i).getAstNode() and
302305
exists(Cfg::CfgScope scope |
303306
hasVariableReadWithCapturedWrite(bb, any(int j | j > i), v, scope)

rust/ql/lib/codeql/rust/elements/internal/AstNodeImpl.qll

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
*/
66

77
private import codeql.rust.elements.internal.generated.AstNode
8+
private import codeql.rust.controlflow.ControlFlowGraph
89

910
/**
1011
* INTERNAL: This module contains the customizable definition of `AstNode` and should not
@@ -44,6 +45,16 @@ module Impl {
4445
)
4546
}
4647

48+
/** Gets the CFG scope that encloses this node, if any. */
49+
CfgScope getEnclosingCfgScope() {
50+
exists(AstNode p | p = this.getParentNode() |
51+
result = p
52+
or
53+
not p instanceof CfgScope and
54+
result = p.getEnclosingCfgScope()
55+
)
56+
}
57+
4758
/** Holds if this node is inside a macro expansion. */
4859
predicate isInMacroExpansion() {
4960
this = any(MacroCall mc).getExpanded()

rust/ql/lib/codeql/rust/elements/internal/VariableImpl.qll

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -446,7 +446,7 @@ module Impl {
446446
Variable getVariable() { result = v }
447447

448448
/** Holds if this access is a capture. */
449-
predicate isCapture() { getEnclosingCfgScope(this) != getEnclosingCfgScope(v.getPat()) }
449+
predicate isCapture() { this.getEnclosingCfgScope() != v.getPat().getEnclosingCfgScope() }
450450

451451
override string toString() { result = name }
452452

rust/ql/test/library-tests/variables/variables.expected

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
11
testFailures
2-
| variables.rs:438:9:438:9 | i | Unexpected result: write_access=i |
3-
| variables.rs:438:16:438:33 | Comment | Missing result: read_access=i |
42
failures
53
variable
64
| variables.rs:3:14:3:14 | s |

rust/ql/test/library-tests/variables/variables.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -435,7 +435,7 @@ fn capture_mut() {
435435
async fn async_block_capture() {
436436
let mut i: i64 = 0; // i
437437
let block = async {
438-
i = 1; // $ read_access=i
438+
i = 1; // $ write_access=i
439439
};
440440
// The await below causes write to `i`
441441
block.await; // $ read_access=block

0 commit comments

Comments
 (0)