Skip to content

Commit 90869ec

Browse files
authored
Merge pull request #17558 from hvitved/rust/cfg-consistency-queries
Rust: Enable CFG consistency checks
2 parents cc63abf + 79620c1 commit 90869ec

File tree

26 files changed

+165
-121
lines changed

26 files changed

+165
-121
lines changed

csharp/ql/consistency-queries/CfgConsistency.ql

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -61,8 +61,3 @@ query predicate preBasicBlockConsistency(ControlFlowElement cfe1, ControlFlowEle
6161
bbIntraSuccInconsistency(cfe1, cfe2) and
6262
s = "intra succ inconsistency"
6363
}
64-
65-
query predicate multipleToString(Node n, string s) {
66-
s = strictconcat(n.toString(), ",") and
67-
strictcount(n.toString()) > 1
68-
}

ruby/ql/consistency-queries/CfgConsistency.ql

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,3 @@ query predicate nonPostOrderExpr(Expr e, string cls) {
1919
c instanceof NormalCompletion
2020
)
2121
}
22-
23-
query predicate multipleToString(CfgNode n, string s) {
24-
s = strictconcat(n.toString(), ",") and
25-
strictcount(n.toString()) > 1
26-
}

rust/ql/.generated.list

Lines changed: 0 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

rust/ql/.gitattributes

Lines changed: 0 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
import rust
2+
import codeql.rust.controlflow.internal.ControlFlowGraphImpl::Consistency
3+
import codeql.rust.controlflow.internal.ControlFlowGraphImpl as CfgImpl
4+
import codeql.rust.controlflow.internal.Completion
5+
6+
/**
7+
* All `Expr` nodes are `PostOrderTree`s
8+
*/
9+
query predicate nonPostOrderExpr(Expr e, string cls) {
10+
cls = e.getPrimaryQlClasses() and
11+
not e instanceof LetExpr and
12+
not e instanceof LogicalAndExpr and // todo
13+
not e instanceof LogicalOrExpr and
14+
exists(AstNode last, Completion c |
15+
CfgImpl::last(e, last, c) and
16+
last != e and
17+
c instanceof NormalCompletion
18+
)
19+
}

rust/ql/consistency-queries/Placeholder.ql

Lines changed: 0 additions & 3 deletions
This file was deleted.

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

Lines changed: 15 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ class BecomeExprTree extends StandardPostOrderTree instanceof BecomeExpr {
7373
}
7474

7575
class BinaryOpExprTree extends StandardPostOrderTree instanceof BinaryExpr {
76-
BinaryOpExprTree() { super.getOperatorName() != "&&" and super.getOperatorName() != "||" }
76+
BinaryOpExprTree() { not this instanceof BinaryLogicalOperation }
7777

7878
override ControlFlowTree getChildNode(int i) {
7979
i = 0 and result = super.getLhs()
@@ -82,61 +82,53 @@ class BinaryOpExprTree extends StandardPostOrderTree instanceof BinaryExpr {
8282
}
8383
}
8484

85-
class LogicalOrBinaryOpExprTree extends PreOrderTree instanceof BinaryExpr {
86-
LogicalOrBinaryOpExprTree() { super.getOperatorName() = "||" }
87-
88-
final override predicate propagatesAbnormal(AstNode child) {
89-
child = [super.getRhs(), super.getLhs()]
90-
}
85+
class LogicalOrBinaryOpExprTree extends PreOrderTree, LogicalOrExpr {
86+
final override predicate propagatesAbnormal(AstNode child) { child = this.getAnOperand() }
9187

9288
override predicate succ(AstNode pred, AstNode succ, Completion c) {
9389
// Edge to the first node in the lhs
9490
pred = this and
95-
first(super.getLhs(), succ) and
91+
first(this.getLhs(), succ) and
9692
completionIsSimple(c)
9793
or
9894
// Edge from the last node in the lhs to the first node in the rhs
99-
last(super.getLhs(), pred, c) and
100-
first(super.getRhs(), succ) and
95+
last(this.getLhs(), pred, c) and
96+
first(this.getRhs(), succ) and
10197
c.(BooleanCompletion).failed()
10298
}
10399

104100
override predicate last(AstNode node, Completion c) {
105101
// Lhs. as the last node
106-
last(super.getLhs(), node, c) and
102+
last(this.getLhs(), node, c) and
107103
c.(BooleanCompletion).succeeded()
108104
or
109105
// Rhs. as the last node
110-
last(super.getRhs(), node, c)
106+
last(this.getRhs(), node, c)
111107
}
112108
}
113109

114-
class LogicalAndBinaryOpExprTree extends PreOrderTree instanceof BinaryExpr {
115-
LogicalAndBinaryOpExprTree() { super.getOperatorName() = "&&" }
116-
117-
final override predicate propagatesAbnormal(AstNode child) {
118-
child = [super.getRhs(), super.getLhs()]
119-
}
110+
class LogicalAndBinaryOpExprTree extends PreOrderTree, LogicalAndExpr {
111+
final override predicate propagatesAbnormal(AstNode child) { child = this.getAnOperand() }
120112

121113
override predicate succ(AstNode pred, AstNode succ, Completion c) {
122114
// Edge to the first node in the lhs
123115
pred = this and
124-
first(super.getLhs(), succ) and
116+
first(this.getLhs(), succ) and
125117
completionIsSimple(c)
126118
or
127119
// Edge from the last node in the lhs to the first node in the rhs
128-
last(super.getLhs(), pred, c) and
129-
first(super.getRhs(), succ) and
120+
last(this.getLhs(), pred, c) and
121+
first(this.getRhs(), succ) and
130122
c.(BooleanCompletion).succeeded()
131123
}
132124

133125
override predicate last(AstNode node, Completion c) {
134126
// Lhs. as the last node
135-
last(super.getLhs(), node, c) and
127+
last(this.getLhs(), node, c) and
136128
c.(BooleanCompletion).failed()
137129
or
138130
// Rhs. as the last node
139-
last(super.getRhs(), node, c)
131+
last(this.getRhs(), node, c)
140132
}
141133
}
142134

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
private import codeql.rust.elements.Expr
2+
private import codeql.rust.elements.BinaryExpr
3+
private import codeql.rust.elements.PrefixExpr
4+
5+
abstract private class LogicalOperationImpl extends Expr {
6+
abstract Expr getAnOperand();
7+
}
8+
9+
final class LogicalOperation = LogicalOperationImpl;
10+
11+
abstract private class BinaryLogicalOperationImpl extends BinaryExpr, LogicalOperationImpl {
12+
override Expr getAnOperand() { result = [this.getLhs(), this.getRhs()] }
13+
}
14+
15+
final class BinaryLogicalOperation = BinaryLogicalOperationImpl;
16+
17+
final class LogicalAndExpr extends BinaryLogicalOperationImpl, BinaryExpr {
18+
LogicalAndExpr() { this.getOperatorName() = "&&" }
19+
}
20+
21+
final class LogicalOrExpr extends BinaryLogicalOperationImpl {
22+
LogicalOrExpr() { this.getOperatorName() = "||" }
23+
}
24+
25+
abstract private class UnaryLogicalOperationImpl extends PrefixExpr, LogicalOperationImpl { }
26+
27+
final class UnaryLogicalOperation = UnaryLogicalOperationImpl;
28+
29+
final class LogicalNotExpr extends UnaryLogicalOperationImpl {
30+
LogicalNotExpr() { this.getOperatorName() = "!" }
31+
32+
override Expr getAnOperand() { result = this.getExpr() }
33+
}

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

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
// generated by codegen, remove this comment if you wish to edit this file
21
/**
32
* This module provides a hand-modifiable wrapper around the generated class `BinaryExpr`.
43
*
@@ -12,6 +11,7 @@ private import codeql.rust.elements.internal.generated.BinaryExpr
1211
* be referenced directly.
1312
*/
1413
module Impl {
14+
// the following QLdoc is generated: if you need to edit it, do it in the schema file
1515
/**
1616
* A binary operation expression. For example:
1717
* ```rust
@@ -22,5 +22,7 @@ module Impl {
2222
* x += y;
2323
* ```
2424
*/
25-
class BinaryExpr extends Generated::BinaryExpr { }
25+
class BinaryExpr extends Generated::BinaryExpr {
26+
override string toString() { result = "... " + this.getOperatorName() + " ..." }
27+
}
2628
}

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

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
// generated by codegen, remove this comment if you wish to edit this file
21
/**
32
* This module provides a hand-modifiable wrapper around the generated class `PrefixExpr`.
43
*
@@ -12,6 +11,7 @@ private import codeql.rust.elements.internal.generated.PrefixExpr
1211
* be referenced directly.
1312
*/
1413
module Impl {
14+
// the following QLdoc is generated: if you need to edit it, do it in the schema file
1515
/**
1616
* A unary operation expression. For example:
1717
* ```rust
@@ -20,5 +20,7 @@ module Impl {
2020
* let z = *ptr
2121
* ```
2222
*/
23-
class PrefixExpr extends Generated::PrefixExpr { }
23+
class PrefixExpr extends Generated::PrefixExpr {
24+
override string toString() { result = this.getOperatorName() + " ..." }
25+
}
2426
}

rust/ql/lib/rust.qll

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,4 @@
33
import codeql.rust.elements
44
import codeql.Locations
55
import codeql.files.FileSystem
6+
import codeql.rust.elements.LogicalOperation
Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
| gen_binary_expr.rs:5:5:5:9 | BinaryExpr | getNumberOfAttrs: | 0 | hasLhs: | yes | hasOperatorName: | yes | hasRhs: | yes |
2-
| gen_binary_expr.rs:6:5:6:10 | BinaryExpr | getNumberOfAttrs: | 0 | hasLhs: | yes | hasOperatorName: | yes | hasRhs: | yes |
3-
| gen_binary_expr.rs:7:5:7:10 | BinaryExpr | getNumberOfAttrs: | 0 | hasLhs: | yes | hasOperatorName: | yes | hasRhs: | yes |
4-
| gen_binary_expr.rs:8:5:8:9 | BinaryExpr | getNumberOfAttrs: | 0 | hasLhs: | yes | hasOperatorName: | yes | hasRhs: | yes |
5-
| gen_binary_expr.rs:9:5:9:10 | BinaryExpr | getNumberOfAttrs: | 0 | hasLhs: | yes | hasOperatorName: | yes | hasRhs: | yes |
1+
| gen_binary_expr.rs:5:5:5:9 | ... + ... | getNumberOfAttrs: | 0 | hasLhs: | yes | hasOperatorName: | yes | hasRhs: | yes |
2+
| gen_binary_expr.rs:6:5:6:10 | ... && ... | getNumberOfAttrs: | 0 | hasLhs: | yes | hasOperatorName: | yes | hasRhs: | yes |
3+
| gen_binary_expr.rs:7:5:7:10 | ... <= ... | getNumberOfAttrs: | 0 | hasLhs: | yes | hasOperatorName: | yes | hasRhs: | yes |
4+
| gen_binary_expr.rs:8:5:8:9 | ... = ... | getNumberOfAttrs: | 0 | hasLhs: | yes | hasOperatorName: | yes | hasRhs: | yes |
5+
| gen_binary_expr.rs:9:5:9:10 | ... += ... | getNumberOfAttrs: | 0 | hasLhs: | yes | hasOperatorName: | yes | hasRhs: | yes |
Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
| gen_binary_expr.rs:5:5:5:9 | BinaryExpr | gen_binary_expr.rs:5:5:5:5 | PathExpr |
2-
| gen_binary_expr.rs:6:5:6:10 | BinaryExpr | gen_binary_expr.rs:6:5:6:5 | PathExpr |
3-
| gen_binary_expr.rs:7:5:7:10 | BinaryExpr | gen_binary_expr.rs:7:5:7:5 | PathExpr |
4-
| gen_binary_expr.rs:8:5:8:9 | BinaryExpr | gen_binary_expr.rs:8:5:8:5 | PathExpr |
5-
| gen_binary_expr.rs:9:5:9:10 | BinaryExpr | gen_binary_expr.rs:9:5:9:5 | PathExpr |
1+
| gen_binary_expr.rs:5:5:5:9 | ... + ... | gen_binary_expr.rs:5:5:5:5 | PathExpr |
2+
| gen_binary_expr.rs:6:5:6:10 | ... && ... | gen_binary_expr.rs:6:5:6:5 | PathExpr |
3+
| gen_binary_expr.rs:7:5:7:10 | ... <= ... | gen_binary_expr.rs:7:5:7:5 | PathExpr |
4+
| gen_binary_expr.rs:8:5:8:9 | ... = ... | gen_binary_expr.rs:8:5:8:5 | PathExpr |
5+
| gen_binary_expr.rs:9:5:9:10 | ... += ... | gen_binary_expr.rs:9:5:9:5 | PathExpr |
Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
| gen_binary_expr.rs:5:5:5:9 | BinaryExpr | + |
2-
| gen_binary_expr.rs:6:5:6:10 | BinaryExpr | && |
3-
| gen_binary_expr.rs:7:5:7:10 | BinaryExpr | <= |
4-
| gen_binary_expr.rs:8:5:8:9 | BinaryExpr | = |
5-
| gen_binary_expr.rs:9:5:9:10 | BinaryExpr | += |
1+
| gen_binary_expr.rs:5:5:5:9 | ... + ... | + |
2+
| gen_binary_expr.rs:6:5:6:10 | ... && ... | && |
3+
| gen_binary_expr.rs:7:5:7:10 | ... <= ... | <= |
4+
| gen_binary_expr.rs:8:5:8:9 | ... = ... | = |
5+
| gen_binary_expr.rs:9:5:9:10 | ... += ... | += |
Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
| gen_binary_expr.rs:5:5:5:9 | BinaryExpr | gen_binary_expr.rs:5:9:5:9 | PathExpr |
2-
| gen_binary_expr.rs:6:5:6:10 | BinaryExpr | gen_binary_expr.rs:6:10:6:10 | PathExpr |
3-
| gen_binary_expr.rs:7:5:7:10 | BinaryExpr | gen_binary_expr.rs:7:10:7:10 | PathExpr |
4-
| gen_binary_expr.rs:8:5:8:9 | BinaryExpr | gen_binary_expr.rs:8:9:8:9 | PathExpr |
5-
| gen_binary_expr.rs:9:5:9:10 | BinaryExpr | gen_binary_expr.rs:9:10:9:10 | PathExpr |
1+
| gen_binary_expr.rs:5:5:5:9 | ... + ... | gen_binary_expr.rs:5:9:5:9 | PathExpr |
2+
| gen_binary_expr.rs:6:5:6:10 | ... && ... | gen_binary_expr.rs:6:10:6:10 | PathExpr |
3+
| gen_binary_expr.rs:7:5:7:10 | ... <= ... | gen_binary_expr.rs:7:10:7:10 | PathExpr |
4+
| gen_binary_expr.rs:8:5:8:9 | ... = ... | gen_binary_expr.rs:8:9:8:9 | PathExpr |
5+
| gen_binary_expr.rs:9:5:9:10 | ... += ... | gen_binary_expr.rs:9:10:9:10 | PathExpr |
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
| gen_closure_expr.rs:5:5:5:13 | ClosureExpr | gen_closure_expr.rs:5:9:5:13 | BinaryExpr |
1+
| gen_closure_expr.rs:5:5:5:13 | ClosureExpr | gen_closure_expr.rs:5:9:5:13 | ... + ... |
22
| gen_closure_expr.rs:6:5:6:34 | ClosureExpr | gen_closure_expr.rs:6:26:6:34 | BlockExpr |
3-
| gen_closure_expr.rs:7:5:7:27 | ClosureExpr | gen_closure_expr.rs:7:23:7:27 | BinaryExpr |
3+
| gen_closure_expr.rs:7:5:7:27 | ClosureExpr | gen_closure_expr.rs:7:23:7:27 | ... + ... |
44
| gen_closure_expr.rs:8:6:9:15 | ClosureExpr | gen_closure_expr.rs:9:9:9:15 | YieldExpr |
55
| gen_closure_expr.rs:10:6:11:23 | ClosureExpr | gen_closure_expr.rs:11:17:11:23 | YieldExpr |
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
1-
| gen_if_expr.rs:5:5:7:5 | IfExpr | gen_if_expr.rs:5:8:5:14 | BinaryExpr |
2-
| gen_if_expr.rs:8:13:12:5 | IfExpr | gen_if_expr.rs:8:16:8:20 | BinaryExpr |
1+
| gen_if_expr.rs:5:5:7:5 | IfExpr | gen_if_expr.rs:5:8:5:14 | ... == ... |
2+
| gen_if_expr.rs:8:13:12:5 | IfExpr | gen_if_expr.rs:8:16:8:20 | ... > ... |
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
deadEnd
2+
| gen_match_arm.rs:10:20:10:25 | ... != ... |
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
| gen_match_arm.rs:6:9:6:29 | MatchArm | gen_match_arm.rs:6:28:6:28 | PathExpr |
22
| gen_match_arm.rs:7:9:7:26 | MatchArm | gen_match_arm.rs:7:25:7:25 | LiteralExpr |
3-
| gen_match_arm.rs:10:9:10:35 | MatchArm | gen_match_arm.rs:10:30:10:34 | BinaryExpr |
3+
| gen_match_arm.rs:10:9:10:35 | MatchArm | gen_match_arm.rs:10:30:10:34 | ... / ... |
44
| gen_match_arm.rs:11:9:11:15 | MatchArm | gen_match_arm.rs:11:14:11:14 | LiteralExpr |
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
deadEnd
2+
| gen_match_expr.rs:10:20:10:25 | ... != ... |
Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
1-
| gen_prefix_expr.rs:5:13:5:15 | PrefixExpr | getNumberOfAttrs: | 0 | hasExpr: | yes | hasOperatorName: | yes |
2-
| gen_prefix_expr.rs:6:13:6:17 | PrefixExpr | getNumberOfAttrs: | 0 | hasExpr: | yes | hasOperatorName: | yes |
3-
| gen_prefix_expr.rs:7:13:7:16 | PrefixExpr | getNumberOfAttrs: | 0 | hasExpr: | yes | hasOperatorName: | yes |
1+
| gen_prefix_expr.rs:5:13:5:15 | - ... | getNumberOfAttrs: | 0 | hasExpr: | yes | hasOperatorName: | yes |
2+
| gen_prefix_expr.rs:6:13:6:17 | ! ... | getNumberOfAttrs: | 0 | hasExpr: | yes | hasOperatorName: | yes |
3+
| gen_prefix_expr.rs:7:13:7:16 | * ... | getNumberOfAttrs: | 0 | hasExpr: | yes | hasOperatorName: | yes |
Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
1-
| gen_prefix_expr.rs:5:13:5:15 | PrefixExpr | gen_prefix_expr.rs:5:14:5:15 | LiteralExpr |
2-
| gen_prefix_expr.rs:6:13:6:17 | PrefixExpr | gen_prefix_expr.rs:6:14:6:17 | LiteralExpr |
3-
| gen_prefix_expr.rs:7:13:7:16 | PrefixExpr | gen_prefix_expr.rs:7:14:7:16 | PathExpr |
1+
| gen_prefix_expr.rs:5:13:5:15 | - ... | gen_prefix_expr.rs:5:14:5:15 | LiteralExpr |
2+
| gen_prefix_expr.rs:6:13:6:17 | ! ... | gen_prefix_expr.rs:6:14:6:17 | LiteralExpr |
3+
| gen_prefix_expr.rs:7:13:7:16 | * ... | gen_prefix_expr.rs:7:14:7:16 | PathExpr |
Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
1-
| gen_prefix_expr.rs:5:13:5:15 | PrefixExpr | - |
2-
| gen_prefix_expr.rs:6:13:6:17 | PrefixExpr | ! |
3-
| gen_prefix_expr.rs:7:13:7:16 | PrefixExpr | * |
1+
| gen_prefix_expr.rs:5:13:5:15 | - ... | - |
2+
| gen_prefix_expr.rs:6:13:6:17 | ! ... | ! |
3+
| gen_prefix_expr.rs:7:13:7:16 | * ... | * |
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
deadEnd
2+
| test.rs:124:28:124:33 | ... < ... |
3+
| test.rs:139:30:141:9 | BlockExpr |

0 commit comments

Comments
 (0)