Skip to content

Commit b979df6

Browse files
committed
Rust: Handle functions correctly through scope in CFG
1 parent f73680b commit b979df6

File tree

2 files changed

+16
-19
lines changed

2 files changed

+16
-19
lines changed

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

Lines changed: 5 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -49,31 +49,17 @@ module CfgInput implements InputSig<Location> {
4949
int maxSplits() { result = 0 }
5050

5151
/** Holds if `first` is first executed when entering `scope`. */
52-
predicate scopeFirst(CfgScope scope, AstNode first) {
53-
scope.(CfgImpl::ControlFlowTree).first(first)
54-
}
52+
predicate scopeFirst(CfgScope scope, AstNode first) { scope.scopeFirst(first) }
5553

5654
/** Holds if `scope` is exited when `last` finishes with completion `c`. */
57-
predicate scopeLast(CfgScope scope, AstNode last, Completion c) {
58-
scope.(CfgImpl::ControlFlowTree).last(last, c)
59-
}
55+
predicate scopeLast(CfgScope scope, AstNode last, Completion c) { scope.scopeLast(last, c) }
6056
}
6157

6258
module CfgImpl = Make<Location, CfgInput>;
6359

6460
import CfgImpl
6561

66-
class FunctionTree extends PostOrderTree instanceof Function {
67-
override predicate propagatesAbnormal(AstNode child) { child = super.getBody() }
68-
69-
override predicate first(AstNode node) { first(super.getBody(), node) }
70-
71-
override predicate succ(AstNode pred, AstNode succ, Completion c) {
72-
last(super.getBody(), pred, c) and
73-
(completionIsNormal(c) or c instanceof ReturnCompletion) and
74-
succ = this
75-
}
76-
}
62+
class FunctionTree extends LeafTree instanceof Function { }
7763

7864
class BlockExprTree extends StandardPostOrderTree instanceof BlockExpr {
7965
override ControlFlowTree getChildNode(int i) {
@@ -233,6 +219,8 @@ class BreakExprTree extends PostOrderTree instanceof BreakExpr {
233219
}
234220
}
235221

222+
class ClosureExprTree extends LeafTree instanceof ClosureExpr { }
223+
236224
class ContinueExprTree extends LeafTree instanceof ContinueExpr { }
237225

238226
class LiteralExprTree extends LeafTree instanceof LiteralExpr { }

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

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,19 @@
11
private import rust
22
private import Completion
3+
private import ControlFlowGraphImpl
34
private import codeql.rust.generated.ParentChild
45

5-
abstract class CfgScope extends AstNode { }
6+
abstract class CfgScope extends AstNode {
7+
abstract predicate scopeFirst(AstNode first);
68

7-
class FunctionScope extends CfgScope, Function { }
9+
abstract predicate scopeLast(AstNode last, Completion c);
10+
}
11+
12+
final class FunctionScope extends CfgScope, Function {
13+
override predicate scopeFirst(AstNode node) { first(this.getBody(), node) }
14+
15+
override predicate scopeLast(AstNode node, Completion c) { last(this.getBody(), node, c) }
16+
}
817

918
/**
1019
* Gets the immediate parent of a non-`AstNode` element `e`.

0 commit comments

Comments
 (0)