Skip to content

Commit 85f2582

Browse files
committed
Rust: Move CFG consistency logic into a library.
1 parent 5b57826 commit 85f2582

File tree

2 files changed

+42
-37
lines changed

2 files changed

+42
-37
lines changed
Lines changed: 1 addition & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -1,37 +1 @@
1-
import rust
2-
import codeql.rust.controlflow.internal.ControlFlowGraphImpl::Consistency as Consistency
3-
import Consistency
4-
import codeql.rust.controlflow.ControlFlowGraph
5-
import codeql.rust.controlflow.internal.ControlFlowGraphImpl as CfgImpl
6-
import codeql.rust.controlflow.internal.Completion
7-
8-
/**
9-
* All `Expr` nodes are `PostOrderTree`s
10-
*/
11-
query predicate nonPostOrderExpr(Expr e, string cls) {
12-
cls = e.getPrimaryQlClasses() and
13-
not e instanceof LetExpr and
14-
not e instanceof ParenExpr and
15-
exists(AstNode last, Completion c |
16-
CfgImpl::last(e, last, c) and
17-
last != e and
18-
c instanceof NormalCompletion
19-
)
20-
}
21-
22-
query predicate scopeNoFirst(CfgScope scope) {
23-
Consistency::scopeNoFirst(scope) and
24-
not scope = any(Function f | not exists(f.getBody())) and
25-
not scope = any(ClosureExpr c | not exists(c.getBody()))
26-
}
27-
28-
/** Holds if `be` is the `else` branch of a `let` statement that results in a panic. */
29-
private predicate letElsePanic(BlockExpr be) {
30-
be = any(LetStmt let).getLetElse().getBlockExpr() and
31-
exists(Completion c | CfgImpl::last(be, _, c) | completionIsNormal(c))
32-
}
33-
34-
query predicate deadEnd(CfgImpl::Node node) {
35-
Consistency::deadEnd(node) and
36-
not letElsePanic(node.getAstNode())
37-
}
1+
import codeql.rust.controlflow.internal.CfgConsistency
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
/**
2+
* Provides classes for recognizing control flow graph inconsistencies.
3+
*/
4+
5+
private import rust
6+
private import codeql.rust.controlflow.internal.ControlFlowGraphImpl::Consistency as Consistency
7+
import Consistency
8+
private import codeql.rust.controlflow.ControlFlowGraph
9+
private import codeql.rust.controlflow.internal.ControlFlowGraphImpl as CfgImpl
10+
private import codeql.rust.controlflow.internal.Completion
11+
12+
/**
13+
* All `Expr` nodes are `PostOrderTree`s
14+
*/
15+
query predicate nonPostOrderExpr(Expr e, string cls) {
16+
cls = e.getPrimaryQlClasses() and
17+
not e instanceof LetExpr and
18+
not e instanceof ParenExpr and
19+
exists(AstNode last, Completion c |
20+
CfgImpl::last(e, last, c) and
21+
last != e and
22+
c instanceof NormalCompletion
23+
)
24+
}
25+
26+
query predicate scopeNoFirst(CfgScope scope) {
27+
Consistency::scopeNoFirst(scope) and
28+
not scope = any(Function f | not exists(f.getBody())) and
29+
not scope = any(ClosureExpr c | not exists(c.getBody()))
30+
}
31+
32+
/** Holds if `be` is the `else` branch of a `let` statement that results in a panic. */
33+
private predicate letElsePanic(BlockExpr be) {
34+
be = any(LetStmt let).getLetElse().getBlockExpr() and
35+
exists(Completion c | CfgImpl::last(be, _, c) | completionIsNormal(c))
36+
}
37+
38+
query predicate deadEnd(CfgImpl::Node node) {
39+
Consistency::deadEnd(node) and
40+
not letElsePanic(node.getAstNode())
41+
}

0 commit comments

Comments
 (0)