Rust: Add LogicalOperation.qll

This commit is contained in:
Tom Hvitved
2024-09-24 13:32:42 +02:00
parent 3bd5c6e445
commit 8c956e8276
3 changed files with 49 additions and 23 deletions

View File

@@ -73,7 +73,7 @@ class BecomeExprTree extends StandardPostOrderTree instanceof BecomeExpr {
}
class BinaryOpExprTree extends StandardPostOrderTree instanceof BinaryExpr {
BinaryOpExprTree() { super.getOperatorName() != "&&" and super.getOperatorName() != "||" }
BinaryOpExprTree() { not this instanceof LogicalOrExpr and not this instanceof LogicalAndExpr }
override ControlFlowTree getChildNode(int i) {
i = 0 and result = super.getLhs()
@@ -82,61 +82,53 @@ class BinaryOpExprTree extends StandardPostOrderTree instanceof BinaryExpr {
}
}
class LogicalOrBinaryOpExprTree extends PreOrderTree instanceof BinaryExpr {
LogicalOrBinaryOpExprTree() { super.getOperatorName() = "||" }
final override predicate propagatesAbnormal(AstNode child) {
child = [super.getRhs(), super.getLhs()]
}
class LogicalOrBinaryOpExprTree extends PreOrderTree, LogicalOrExpr {
final override predicate propagatesAbnormal(AstNode child) { child = this.getAnOperand() }
override predicate succ(AstNode pred, AstNode succ, Completion c) {
// Edge to the first node in the lhs
pred = this and
first(super.getLhs(), succ) and
first(this.getLhs(), succ) and
completionIsSimple(c)
or
// Edge from the last node in the lhs to the first node in the rhs
last(super.getLhs(), pred, c) and
first(super.getRhs(), succ) and
last(this.getLhs(), pred, c) and
first(this.getRhs(), succ) and
c.(BooleanCompletion).failed()
}
override predicate last(AstNode node, Completion c) {
// Lhs. as the last node
last(super.getLhs(), node, c) and
last(this.getLhs(), node, c) and
c.(BooleanCompletion).succeeded()
or
// Rhs. as the last node
last(super.getRhs(), node, c)
last(this.getRhs(), node, c)
}
}
class LogicalAndBinaryOpExprTree extends PreOrderTree instanceof BinaryExpr {
LogicalAndBinaryOpExprTree() { super.getOperatorName() = "&&" }
final override predicate propagatesAbnormal(AstNode child) {
child = [super.getRhs(), super.getLhs()]
}
class LogicalAndBinaryOpExprTree extends PreOrderTree, LogicalAndExpr {
final override predicate propagatesAbnormal(AstNode child) { child = this.getAnOperand() }
override predicate succ(AstNode pred, AstNode succ, Completion c) {
// Edge to the first node in the lhs
pred = this and
first(super.getLhs(), succ) and
first(this.getLhs(), succ) and
completionIsSimple(c)
or
// Edge from the last node in the lhs to the first node in the rhs
last(super.getLhs(), pred, c) and
first(super.getRhs(), succ) and
last(this.getLhs(), pred, c) and
first(this.getRhs(), succ) and
c.(BooleanCompletion).succeeded()
}
override predicate last(AstNode node, Completion c) {
// Lhs. as the last node
last(super.getLhs(), node, c) and
last(this.getLhs(), node, c) and
c.(BooleanCompletion).failed()
or
// Rhs. as the last node
last(super.getRhs(), node, c)
last(this.getRhs(), node, c)
}
}

View File

@@ -0,0 +1,33 @@
private import codeql.rust.elements.Expr
private import codeql.rust.elements.BinaryExpr
private import codeql.rust.elements.PrefixExpr
abstract private class LogicalOperationImpl extends Expr {
abstract Expr getAnOperand();
}
final class LogicalOperation = LogicalOperationImpl;
abstract private class BinaryLogicalOperationImpl extends BinaryExpr, LogicalOperationImpl {
override Expr getAnOperand() { result = [this.getLhs(), this.getRhs()] }
}
final class BinaryLogicalOperation = BinaryLogicalOperationImpl;
final class LogicalAndExpr extends BinaryLogicalOperationImpl, BinaryExpr {
LogicalAndExpr() { this.getOperatorName() = "&&" }
}
final class LogicalOrExpr extends BinaryLogicalOperationImpl {
LogicalOrExpr() { this.getOperatorName() = "||" }
}
abstract private class UnaryLogicalOperationImpl extends PrefixExpr, LogicalOperationImpl { }
final class UnaryLogicalOperation = UnaryLogicalOperationImpl;
final class LogicalNotExpr extends UnaryLogicalOperationImpl {
LogicalNotExpr() { this.getOperatorName() = "!" }
override Expr getAnOperand() { result = this.getExpr() }
}

View File

@@ -3,3 +3,4 @@
import codeql.rust.elements
import codeql.Locations
import codeql.files.FileSystem
import codeql.rust.elements.LogicalOperation