Skip to content

Rust: Add more Operation subclasses #19562

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 7 commits into from
May 27, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 38 additions & 0 deletions rust/ql/lib/codeql/rust/elements/ArithmeticOperation.qll
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
/**
* Provides classes for arithmetic operations.
*/

private import codeql.rust.elements.BinaryExpr
private import codeql.rust.elements.PrefixExpr
private import codeql.rust.elements.Operation
private import codeql.rust.elements.AssignmentOperation

/**
* An arithmetic operation, such as `+`, `*=`, or `-`.
*/
abstract private class ArithmeticOperationImpl extends Operation { }

final class ArithmeticOperation = ArithmeticOperationImpl;

/**
* A binary arithmetic operation, such as `+` or `*`.
*/
final class BinaryArithmeticOperation extends BinaryExpr, ArithmeticOperationImpl {
BinaryArithmeticOperation() { this.getOperatorName() = ["+", "-", "*", "/", "%"] }
}

/**
* An arithmetic assignment operation, such as `+=` or `*=`.
*/
final class AssignArithmeticOperation extends BinaryExpr, ArithmeticOperationImpl,
AssignmentOperation
{
AssignArithmeticOperation() { this.getOperatorName() = ["+=", "-=", "*=", "/=", "%="] }
}

/**
* A prefix arithmetic operation, such as `-`.
*/
final class PrefixArithmeticOperation extends PrefixExpr, ArithmeticOperationImpl {
PrefixArithmeticOperation() { this.getOperatorName() = "-" }
}
28 changes: 28 additions & 0 deletions rust/ql/lib/codeql/rust/elements/BitwiseOperation.qll
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
/**
* Provides classes for bitwise operations.
*/

private import codeql.rust.elements.BinaryExpr
private import codeql.rust.elements.Operation
private import codeql.rust.elements.AssignmentOperation

/**
* A bitwise operation, such as `&`, `<<`, or `|=`.
*/
abstract private class BitwiseOperationImpl extends Operation { }

final class BitwiseOperation = BitwiseOperationImpl;

/**
* A binary bitwise operation, such as `&` or `<<`.
*/
final class BinaryBitwiseOperation extends BinaryExpr, BitwiseOperationImpl {
BinaryBitwiseOperation() { this.getOperatorName() = ["&", "|", "^", "<<", ">>"] }
}

/**
* A bitwise assignment operation, such as `|=` or `<<=`.
*/
final class AssignBitwiseOperation extends BinaryExpr, BitwiseOperationImpl, AssignmentOperation {
AssignBitwiseOperation() { this.getOperatorName() = ["&=", "|=", "^=", "<<=", ">>="] }
}
13 changes: 13 additions & 0 deletions rust/ql/lib/codeql/rust/elements/DerefExpr.qll
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
/**
* Provides classes for deref expressions (`*`).
*/

private import codeql.rust.elements.PrefixExpr
private import codeql.rust.elements.Operation

/**
* A dereference expression, the prefix operator `*`.
*/
final class DerefExpr extends PrefixExpr, Operation {
DerefExpr() { this.getOperatorName() = "*" }
}
7 changes: 6 additions & 1 deletion rust/ql/lib/codeql/rust/elements/internal/RefExprImpl.qll
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
*/

private import codeql.rust.elements.internal.generated.RefExpr
private import codeql.rust.elements.internal.OperationImpl::Impl as OperationImpl

/**
* INTERNAL: This module contains the customizable definition of `RefExpr` and should not
Expand All @@ -21,11 +22,15 @@ module Impl {
* let raw_mut: &mut i32 = &raw mut foo;
* ```
*/
class RefExpr extends Generated::RefExpr {
class RefExpr extends Generated::RefExpr, OperationImpl::Operation {
override string toStringImpl() {
result = "&" + concat(int i | | this.getSpecPart(i), " " order by i)
}

override string getOperatorName() { result = "&" }

override Expr getAnOperand() { result = this.getExpr() }

private string getSpecPart(int index) {
index = 0 and this.isRaw() and result = "raw"
or
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -610,7 +610,7 @@ module Impl {
exists(Expr mid |
assignmentExprDescendant(mid) and
getImmediateParent(e) = mid and
not mid.(PrefixExpr).getOperatorName() = "*" and
not mid instanceof DerefExpr and
not mid instanceof FieldExpr and
not mid instanceof IndexExpr
)
Expand Down
6 changes: 2 additions & 4 deletions rust/ql/lib/codeql/rust/internal/TypeInference.qll
Original file line number Diff line number Diff line change
Expand Up @@ -259,8 +259,7 @@ private predicate typeEqualityLeft(AstNode n1, TypePath path1, AstNode n2, TypeP
typeEquality(n1, path1, n2, path2)
or
n2 =
any(PrefixExpr pe |
pe.getOperatorName() = "*" and
any(DerefExpr pe |
pe.getExpr() = n1 and
path1.isCons(TRefTypeParameter(), path2)
)
Expand All @@ -271,8 +270,7 @@ private predicate typeEqualityRight(AstNode n1, TypePath path1, AstNode n2, Type
typeEquality(n1, path1, n2, path2)
or
n2 =
any(PrefixExpr pe |
pe.getOperatorName() = "*" and
any(DerefExpr pe |
pe.getExpr() = n1 and
path1 = TypePath::cons(TRefTypeParameter(), path2)
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,9 +50,7 @@ module AccessInvalidPointer {
* A pointer access using the unary `*` operator.
*/
private class DereferenceSink extends Sink {
DereferenceSink() {
exists(PrefixExpr p | p.getOperatorName() = "*" and p.getExpr() = this.asExpr().getExpr())
}
DereferenceSink() { any(DerefExpr p).getExpr() = this.asExpr().getExpr() }
}

/**
Expand Down
3 changes: 3 additions & 0 deletions rust/ql/lib/rust.qll
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,11 @@ import codeql.rust.elements
import codeql.Locations
import codeql.files.FileSystem
import codeql.rust.elements.Operation
import codeql.rust.elements.ArithmeticOperation
import codeql.rust.elements.AssignmentOperation
import codeql.rust.elements.BitwiseOperation
import codeql.rust.elements.ComparisonOperation
import codeql.rust.elements.DerefExpr
import codeql.rust.elements.LiteralExprExt
import codeql.rust.elements.LogicalOperation
import codeql.rust.elements.AsyncBlockExpr
Expand Down
16 changes: 16 additions & 0 deletions rust/ql/test/library-tests/operations/Operations.ql
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,22 @@ string describe(Expr op) {
op instanceof LessOrEqualsOperation and result = "LessOrEqualsOperation"
or
op instanceof GreaterOrEqualsOperation and result = "GreaterOrEqualsOperation"
or
op instanceof ArithmeticOperation and result = "ArithmeticOperation"
or
op instanceof BinaryArithmeticOperation and result = "BinaryArithmeticOperation"
or
op instanceof AssignArithmeticOperation and result = "AssignArithmeticOperation"
or
op instanceof PrefixArithmeticOperation and result = "PrefixArithmeticOperation"
or
op instanceof BitwiseOperation and result = "BitwiseOperation"
or
op instanceof BinaryBitwiseOperation and result = "BinaryBitwiseOperation"
or
op instanceof AssignBitwiseOperation and result = "AssignBitwiseOperation"
or
op instanceof DerefExpr and result = "DerefExpr"
}

module OperationsTest implements TestSig {
Expand Down
46 changes: 23 additions & 23 deletions rust/ql/test/library-tests/operations/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,38 +19,38 @@ fn test_operations(
x >= y; // $ Operation Op=>= Operands=2 BinaryExpr ComparisonOperation RelationalOperation GreaterOrEqualsOperation Greater=x Lesser=y

// arithmetic operations
x + y; // $ Operation Op=+ Operands=2 BinaryExpr
x - y; // $ Operation Op=- Operands=2 BinaryExpr
x * y; // $ Operation Op=* Operands=2 BinaryExpr
x / y; // $ Operation Op=/ Operands=2 BinaryExpr
x % y; // $ Operation Op=% Operands=2 BinaryExpr
x += y; // $ Operation Op=+= Operands=2 AssignmentOperation BinaryExpr
x -= y; // $ Operation Op=-= Operands=2 AssignmentOperation BinaryExpr
x *= y; // $ Operation Op=*= Operands=2 AssignmentOperation BinaryExpr
x /= y; // $ Operation Op=/= Operands=2 AssignmentOperation BinaryExpr
x %= y; // $ Operation Op=%= Operands=2 AssignmentOperation BinaryExpr
-x; // $ Operation Op=- Operands=1 PrefixExpr
x + y; // $ Operation Op=+ Operands=2 BinaryExpr ArithmeticOperation BinaryArithmeticOperation
x - y; // $ Operation Op=- Operands=2 BinaryExpr ArithmeticOperation BinaryArithmeticOperation
x * y; // $ Operation Op=* Operands=2 BinaryExpr ArithmeticOperation BinaryArithmeticOperation
x / y; // $ Operation Op=/ Operands=2 BinaryExpr ArithmeticOperation BinaryArithmeticOperation
x % y; // $ Operation Op=% Operands=2 BinaryExpr ArithmeticOperation BinaryArithmeticOperation
x += y; // $ Operation Op=+= Operands=2 AssignmentOperation BinaryExpr ArithmeticOperation AssignArithmeticOperation
x -= y; // $ Operation Op=-= Operands=2 AssignmentOperation BinaryExpr ArithmeticOperation AssignArithmeticOperation
x *= y; // $ Operation Op=*= Operands=2 AssignmentOperation BinaryExpr ArithmeticOperation AssignArithmeticOperation
x /= y; // $ Operation Op=/= Operands=2 AssignmentOperation BinaryExpr ArithmeticOperation AssignArithmeticOperation
x %= y; // $ Operation Op=%= Operands=2 AssignmentOperation BinaryExpr ArithmeticOperation AssignArithmeticOperation
-x; // $ Operation Op=- Operands=1 PrefixExpr ArithmeticOperation PrefixArithmeticOperation

// logical operations
a && b; // $ Operation Op=&& Operands=2 BinaryExpr LogicalOperation
a || b; // $ Operation Op=|| Operands=2 BinaryExpr LogicalOperation
!a; // $ Operation Op=! Operands=1 PrefixExpr LogicalOperation

// bitwise operations
x & y; // $ Operation Op=& Operands=2 BinaryExpr
x | y; // $ Operation Op=| Operands=2 BinaryExpr
x ^ y; // $ Operation Op=^ Operands=2 BinaryExpr
x << y; // $ Operation Op=<< Operands=2 BinaryExpr
x >> y; // $ Operation Op=>> Operands=2 BinaryExpr
x &= y; // $ Operation Op=&= Operands=2 AssignmentOperation BinaryExpr
x |= y; // $ Operation Op=|= Operands=2 AssignmentOperation BinaryExpr
x ^= y; // $ Operation Op=^= Operands=2 AssignmentOperation BinaryExpr
x <<= y; // $ Operation Op=<<= Operands=2 AssignmentOperation BinaryExpr
x >>= y; // $ Operation Op=>>= Operands=2 AssignmentOperation BinaryExpr
x & y; // $ Operation Op=& Operands=2 BinaryExpr BitwiseOperation BinaryBitwiseOperation
x | y; // $ Operation Op=| Operands=2 BinaryExpr BitwiseOperation BinaryBitwiseOperation
x ^ y; // $ Operation Op=^ Operands=2 BinaryExpr BitwiseOperation BinaryBitwiseOperation
x << y; // $ Operation Op=<< Operands=2 BinaryExpr BitwiseOperation BinaryBitwiseOperation
x >> y; // $ Operation Op=>> Operands=2 BinaryExpr BitwiseOperation BinaryBitwiseOperation
x &= y; // $ Operation Op=&= Operands=2 AssignmentOperation BinaryExpr BitwiseOperation AssignBitwiseOperation
x |= y; // $ Operation Op=|= Operands=2 AssignmentOperation BinaryExpr BitwiseOperation AssignBitwiseOperation
x ^= y; // $ Operation Op=^= Operands=2 AssignmentOperation BinaryExpr BitwiseOperation AssignBitwiseOperation
x <<= y; // $ Operation Op=<<= Operands=2 AssignmentOperation BinaryExpr BitwiseOperation AssignBitwiseOperation
x >>= y; // $ Operation Op=>>= Operands=2 AssignmentOperation BinaryExpr BitwiseOperation AssignBitwiseOperation

// miscellaneous expressions that might be operations
*ptr; // $ Operation Op=* Operands=1 PrefixExpr
&x; // $ RefExpr
*ptr; // $ Operation Op=* Operands=1 PrefixExpr DerefExpr
&x; // $ Operation Op=& Operands=1 RefExpr MISSING: PrefixExpr
res?;

return Ok(());
Expand Down