8000 Merge pull request #19562 from geoffw0/operations3 · github/codeql@c971737 · GitHub
[go: up one dir, main page]

Skip to content

Commit c971737

Browse files
authored
Merge pull request #19562 from geoffw0/operations3
Rust: Add more Operation subclasses
2 parents bf2cfab + 1e64f50 commit c971737

File tree

10 files changed

+131
-32
lines changed

10 files changed

+131
-32
lines changed
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
/**
2+
* Provides classes for arithmetic operations.
3+
*/
4+
5+
private import codeql.rust.elements.BinaryExpr
6+
private import codeql.rust.elements.PrefixExpr
7+
private import codeql.rust.elements.Operation
8+
private import codeql.rust.elements.AssignmentOperation
9+
10+
/**
11+
* An arithmetic operation, such as `+`, `*=`, or `-`.
12+
*/
13+
abstract private class ArithmeticOperationImpl extends Operation { }
14+
15+
final class ArithmeticOperation = ArithmeticOperationImpl;
16+
17+
/**
18+
* A binary arithmetic operation, such as `+` or `*`.
19+
*/
20+
final class BinaryArithmeticOperation extends BinaryExpr, ArithmeticOperationImpl {
21+
BinaryArithmeticOperation() { this.getOperatorName() = ["+", "-", "*", "/", "%"] }
22+
}
23+
24+
/**
25+
* An arithmetic assignment operation, such as `+=` or `*=`.
26+
*/
27+
final class AssignArithmeticOperation extends BinaryExpr, ArithmeticOperationImpl,
28+
AssignmentOperation
29+
{
30+
AssignArithmeticOperation() { this.getOperatorName() = ["+=", "-=", "*=", "/=", "%="] }
31+
}
32+
33+
/**
34+
* A prefix arithmetic operation, such as `-`.
35+
*/
36+
final class PrefixArithmeticOperation extends PrefixExpr, ArithmeticOperationImpl {
37+
PrefixArithmeticOperation() { this.getOperatorName() = "-" }
38+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
/**
2+
* Provides classes for bitwise operations.
3+
*/
4+
5+
private import codeql.rust.elements.BinaryExpr
6+
private import codeql.rust.elements.Operation
7+
private import codeql.rust.elements.AssignmentOperation
8+
9+
/**
10+
* A bitwise operation, such as `&`, `<<`, or `|=`.
11+
*/
12+
abstract private class BitwiseOperationImpl extends Operation { }
13+
14+
final class BitwiseOperation = BitwiseOperationImpl;
15+
16+
/**
17+
* A binary bitwise operation, such as `&` or `<<`.
18+
*/
19+
final class BinaryBitwiseOperation extends BinaryExpr, BitwiseOperationImpl {
20+
BinaryBitwiseOperation() { this.getOperatorName() = ["&", "|", "^", "<<", ">>"] }
21+
}
22+
23+
/**
24+
* A bitwise assignment operation, such as `|=` or `<<=`.
25+
*/
26+
final class AssignBitwiseOperation extends BinaryExpr, BitwiseOperationImpl, AssignmentOperation {
27+
AssignBitwiseOperation() { this.getOperatorName() = ["&=", "|=", "^=", "<<=", ">>="] }
28+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
/**
2+
* Provides classes for deref expressions (`*`).
3+
*/
4+
5+
private import codeql.rust.elements.PrefixExpr
6+
private import codeql.rust.elements.Operation
7+
8+
/**
9+
* A dereference expression, the prefix operator `*`.
10+
*/
11+
final class DerefExpr extends PrefixExpr, Operation {
12+
DerefExpr() { this.getOperatorName() = "*" }
13+
}

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

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
*/
66

77
private import codeql.rust.elements.internal.generated.RefExpr
8+
private import codeql.rust.elements.internal.OperationImpl::Impl as OperationImpl
89

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

30+
override string getOperatorName() { result = "&" }
31+
32+
override Expr getAnOperand() { result = this.getExpr() }
33+
2934
private string getSpecPart(int index) {
3035
index = 0 and this.isRaw() and result = "raw"
3136
or

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -610,7 +610,7 @@ module Impl {
610610
exists(Expr mid |
611611
assignmentExprDescendant(mid) and
612612
getImmediateParent(e) = mid and
613-
not mid.(PrefixExpr).getOperatorName() = "*" and
613+
not mid instanceof DerefExpr and
614614
not mid instanceof FieldExpr and
615615
not mid instanceof IndexExpr
616616
)

rust/ql/lib/codeql/rust/internal/TypeInference.qll

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -259,8 +259,7 @@ private predicate typeEqualityLeft(AstNode n1, TypePath path1, AstNode n2, TypeP
259259
typeEquality(n1, path1, n2, path2)
260260
or
261261
n2 =
262-
any(PrefixExpr pe |
263-
pe.getOperatorName() = "*" and
262+
any(DerefExpr pe |
264263
pe.getExpr() = n1 and
265264
path1.isCons(TRefTypeParameter(), path2)
266265
)
@@ -271,8 +270,7 @@ private predicate typeEqualityRight(AstNode n1, TypePath path1, AstNode n2, Type
271270
typeEquality(n1, path1, n2, path2)
272271
or
273272
n2 =
274-
any(PrefixExpr pe |
275-
pe.getOperatorName() = "*" and
273+
any(DerefExpr pe |
276274
pe.getExpr() = n1 and
277275
path1 = TypePath::cons(TRefTypeParameter(), path2)
278276
)

rust/ql/lib/codeql/rust/security/AccessInvalidPointerExtensions.qll

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -50,9 +50,7 @@ module AccessInvalidPointer {
5050
* A pointer access using the unary `*` operator.
5151
*/
5252
private class DereferenceSink extends Sink {
53-
DereferenceSink() {
54-
exists(PrefixExpr p | p.getOperatorName() = "*" and p.getExpr() = this.asExpr().getExpr())
55-
}
53+
DereferenceSink() { any(DerefExpr p).getExpr() = this.asExpr().getExpr() }
5654
}
5755

5856
/**

rust/ql/lib/rust.qll

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,11 @@ import codeql.rust.elements
44
import codeql.Locations
55
import codeql.files.FileSystem
66
import codeql.rust.elements.Operation
7+
import codeql.rust.elements.ArithmeticOperation
78
import codeql.rust.elements.AssignmentOperation
9+
import codeql.rust.elements.BitwiseOperation
810
import codeql.rust.elements.ComparisonOperation
11+
import codeql.rust.elements.DerefExpr
912
import codeql.rust.elements.LiteralExprExt
1013
import codeql.rust.elements.LogicalOperation
1114
import codeql.rust.elements.AsyncBlockExpr

rust/ql/test/library-tests/operations/Operations.ql

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,22 @@ string describe(Expr op) {
3232
op instanceof LessOrEqualsOperation and result = "LessOrEqualsOperation"
3333
or
3434
op instanceof GreaterOrEqualsOperation and result = "GreaterOrEqualsOperation"
35+
or
36+
op instanceof ArithmeticOperation and result = "ArithmeticOperation"
37+
or
38+
op instanceof BinaryArithmeticOperation and result = "BinaryArithmeticOperation"
39+
or
40+
op instanceof AssignArithmeticOperation and result = "AssignArithmeticOperation"
41+
or
42+
op instanceof PrefixArithmeticOperation and result = "PrefixArithmeticOperation"
43+
or
44+
op instanceof BitwiseOperation and result = "BitwiseOperation"
45+
or
46+
op instanceof BinaryBitwiseOperation and result = "BinaryBitwiseOperation"
47+
or
48+
op instanceof AssignBitwiseOperation and result = "AssignBitwiseOperation"
49+
or
50+
op instanceof DerefExpr and result = "DerefExpr"
3551
}
3652

3753
module OperationsTest implements TestSig {

rust/ql/test/library-tests/operations/test.rs

Lines changed: 23 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -19,38 +19,38 @@ fn test_operations(
1919
x >= y; // $ Operation Op=>= Operands=2 BinaryExpr ComparisonOperation RelationalOperation GreaterOrEqualsOperation Greater=x Lesser=y
2020

< F438 /td>
2121
// arithmetic operations
22-
x + y; // $ Operation Op=+ Operands=2 BinaryExpr
23-
x - y; // $ Operation Op=- Operands=2 BinaryExpr
24-
x * y; // $ Operation Op=* Operands=2 BinaryExpr
25-
x / y; // $ Operation Op=/ Operands=2 BinaryExpr
26-
x % y; // $ Operation Op=% Operands=2 BinaryExpr
27-
x += y; // $ Operation Op=+= Operands=2 AssignmentOperation BinaryExpr
28-
x -= y; // $ Operation Op=-= Operands=2 AssignmentOperation BinaryExpr
29-
x *= y; // $ Operation Op=*= Operands=2 AssignmentOperation BinaryExpr
30-
x /= y; // $ Operation Op=/= Operands=2 AssignmentOperation BinaryExpr
31-
x %= y; // $ Operation Op=%= Operands=2 AssignmentOperation BinaryExpr
32-
-x; // $ Operation Op=- Operands=1 PrefixExpr
22+
x + y; // $ Operation Op=+ Operands=2 BinaryExpr ArithmeticOperation BinaryArithmeticOperation
23+
x - y; // $ Operation Op=- Operands=2 BinaryExpr ArithmeticOperation BinaryArithmeticOperation
24+
x * y; // $ Operation Op=* Operands=2 BinaryExpr ArithmeticOperation BinaryArithmeticOperation
25+
x / y; // $ Operation Op=/ Operands=2 BinaryExpr ArithmeticOperation BinaryArithmeticOperation
26+
x % y; // $ Operation Op=% Operands=2 BinaryExpr ArithmeticOperation BinaryArithmeticOperation
27+
x += y; // $ Operation Op=+= Operands=2 AssignmentOperation BinaryExpr ArithmeticOperation AssignArithmeticOperation
28+
x -= y; // $ Operation Op=-= Operands=2 AssignmentOperation BinaryExpr ArithmeticOperation AssignArithmeticOperation
29+
x *= y; // $ Operation Op=*= Operands=2 AssignmentOperation BinaryExpr ArithmeticOperation AssignArithmeticOperation
30+
x /= y; // $ Operation Op=/= Operands=2 AssignmentOperation BinaryExpr ArithmeticOperation AssignArithmeticOperation
31+
x %= y; // $ Operation Op=%= Operands=2 AssignmentOperation BinaryExpr ArithmeticOperation AssignArithmeticOperation
32+
-x; // $ Operation Op=- Operands=1 PrefixExpr ArithmeticOperation PrefixArithmeticOperation
3333

3434
// logical operations
3535
a && b; // $ Operation Op=&& Operands=2 BinaryExpr LogicalOperation
3636
a || b; // $ Operation Op=|| Operands=2 BinaryExpr LogicalOperation
3737
!a; // $ Operation Op=! Operands=1 PrefixExpr LogicalOperation
3838

3939
// bitwise operations
40-
x & y; // $ Operation Op=& Operands=2 BinaryExpr
41-
x | y; // $ Operation Op=| Operands=2 BinaryExpr
42-
x ^ y; // $ Operation Op=^ Operands=2 BinaryExpr
43-
x << y; // $ Operation Op=<< Operands=2 BinaryExpr
44-
x >> y; // $ Operation Op=>> Operands=2 BinaryExpr
45-
x &= y; // $ Operation Op=&= Operands=2 AssignmentOperation BinaryExpr
46-
x |= y; // $ Operation Op=|= Operands=2 AssignmentOperation BinaryExpr
47-
x ^= y; // $ Operation Op=^= Operands=2 AssignmentOperation BinaryExpr
48-
x <<= y; // $ Operation Op=<<= Operands=2 AssignmentOperation BinaryExpr
49-
x >>= y; // $ Operation Op=>>= Operands=2 AssignmentOperation BinaryExpr
40+
x & y; // $ Operation Op=& Operands=2 BinaryExpr BitwiseOperation BinaryBitwiseOperation
41+
x | y; // $ Operation Op=| Operands=2 BinaryExpr BitwiseOperation BinaryBitwiseOperation
42+
x ^ y; // $ Operation Op=^ Operands=2 BinaryExpr BitwiseOperation BinaryBitwiseOperation
43+
x << y; // $ Operation Op=<< Operands=2 BinaryExpr BitwiseOperation BinaryBitwiseOperation
44+
x >> y; // $ Operation Op=>> Operands=2 BinaryExpr BitwiseOperation BinaryBitwiseOperation
45+
x &= y; // $ Operation Op=&= Operands=2 AssignmentOperation BinaryExpr BitwiseOperation AssignBitwiseOperation
46+
x |= y; // $ Operation Op=|= Operands=2 AssignmentOperation BinaryExpr BitwiseOperation AssignBitwiseOperation
47+
x ^= y; // $ Operation Op=^= Operands=2 AssignmentOperation BinaryExpr BitwiseOperation AssignBitwiseOperation
48+
x <<= y; // $ Operation Op=<<= Operands=2 AssignmentOperation BinaryExpr BitwiseOperation AssignBitwiseOperation
49+
x >>= y; // $ Operation Op=>>= Operands=2 AssignmentOperation BinaryExpr BitwiseOperation AssignBitwiseOperation
5050

5151
// miscellaneous expressions that might be operations
52-
*ptr; // $ Operation Op=* Operands=1 PrefixExpr
53-
&x; // $ RefExpr
52+
*ptr; // $ Operation Op=* Operands=1 PrefixExpr DerefExpr
53+
&x; // $ Operation Op=& Operands=1 RefExpr MISSING: PrefixExpr
5454
res?;
5555

5656
return Ok(());

0 commit comments

Comments
 (0)
0