Skip to content

Commit 90869ec

Browse files
authored
Merge pull request github#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
}

0 commit comments

Comments
 (0)