Skip to content

Commit 80ec997

Browse files
committed
C#: Update the QL library implementation for Bitwise operations.
1 parent 45b3f7a commit 80ec997

5 files changed

Lines changed: 40 additions & 36 deletions

File tree

csharp/ql/lib/experimental/code/csharp/Cryptography/NonCryptographicHashes.qll

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -50,15 +50,15 @@ private predicate maybeUsedInElfHashFunction(Variable v, Operation xor, Operatio
5050
|
5151
add instanceof AddOperation and
5252
e1.getAChild*() = add.getAnOperand() and
53-
e1 instanceof BinaryBitwiseOperation and
54-
e2 = e1.(BinaryBitwiseOperation).getLeftOperand() and
53+
e1 instanceof BinaryBitwiseExpr and
54+
e2 = e1.(BinaryBitwiseExpr).getLeftOperand() and
5555
v = addAssign.getTargetVariable() and
5656
addAssign.getAChild*() = add and
5757
(xor instanceof BitwiseXorExpr or xor instanceof AssignXorExpr) and
5858
addAssign.getControlFlowNode().getASuccessor*() = xor.getControlFlowNode() and
5959
xorAssign.getAChild*() = xor and
6060
v = xorAssign.getTargetVariable() and
61-
(notOp instanceof UnaryBitwiseOperation or notOp instanceof AssignBitwiseOperation) and
61+
(notOp instanceof UnaryBitwiseOperation or notOp instanceof AssignBitwiseExpr) and
6262
xor.getControlFlowNode().getASuccessor*() = notOp.getControlFlowNode() and
6363
notAssign.getAChild*() = notOp and
6464
v = notAssign.getTargetVariable() and

csharp/ql/lib/semmle/code/csharp/exprs/BitwiseOperation.qll

Lines changed: 27 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -10,16 +10,16 @@ import Expr
1010
* A bitwise operation. Either a unary bitwise operation (`UnaryBitwiseOperation`)
1111
* or a binary bitwise operation (`BinaryBitwiseOperation`).
1212
*/
13-
class BitwiseOperation extends Operation, @bit_expr { }
13+
class BitwiseOperation extends Operation, @bit_operation { }
1414

1515
/**
1616
* A unary bitwise operation, that is, a bitwise complement operation
1717
* (`ComplementExpr`).
1818
*/
19-
class UnaryBitwiseOperation extends BitwiseOperation, UnaryOperation, @un_bit_op_expr { }
19+
class UnaryBitwiseOperation extends BitwiseOperation, UnaryOperation, @un_bit_operation { }
2020

2121
/**
22-
* A bitwise complement operation, for example `~x`.
22+
* A bitwise complement expression, for example `~x`.
2323
*/
2424
class ComplementExpr extends UnaryBitwiseOperation, @bit_not_expr {
2525
override string getOperator() { result = "~" }
@@ -28,67 +28,71 @@ class ComplementExpr extends UnaryBitwiseOperation, @bit_not_expr {
2828
}
2929

3030
/**
31-
* A binary bitwise operation. Either a bitwise-and operation
32-
* (`BitwiseAndExpr`), a bitwise-or operation (`BitwiseOrExpr`),
33-
* a bitwise exclusive-or operation (`BitwiseXorExpr`), a left-shift
34-
* operation (`LeftShiftExpr`), a right-shift operation (`RightShiftExpr`),
35-
* or an unsigned right-shift operation (`UnsignedRightShiftExpr`).
31+
* A binary bitwise operation. Either a binary bitwise expression (`BinaryBitwiseExpr`) or
32+
* a bitwise assignment expression (`AssignBitwiseExpr`).
3633
*/
37-
class BinaryBitwiseOperation extends BitwiseOperation, BinaryOperation, @bin_bit_op_expr {
34+
class BinaryBitwiseOperation extends BitwiseOperation, BinaryOperation, @bin_bit_operation {
3835
override string getOperator() { none() }
3936
}
4037

4138
/**
42-
* A left-shift operation, for example `x << y`.
39+
* A binary bitwise expression. Either a bitwise-and expression
40+
* (`BitwiseAndExpr`), a bitwise-or expression (`BitwiseOrExpr`),
41+
* a bitwise exclusive-or expression (`BitwiseXorExpr`), a left-shift
42+
* expression (`LeftShiftExpr`), a right-shift expression (`RightShiftExpr`),
43+
* or an unsigned right-shift expression (`UnsignedRightShiftExpr`).
4344
*/
44-
class LeftShiftExpr extends BinaryBitwiseOperation, LeftShiftOperation, @lshift_expr {
45+
class BinaryBitwiseExpr extends BinaryBitwiseOperation, @bin_bit_expr { }
46+
47+
/**
48+
* A left-shift expression, for example `x << y`.
49+
*/
50+
class LeftShiftExpr extends BinaryBitwiseExpr, LeftShiftOperation, @lshift_expr {
4551
override string getOperator() { result = "<<" }
4652

4753
override string getAPrimaryQlClass() { result = "LeftShiftExpr" }
4854
}
4955

5056
/**
51-
* A right-shift operation, for example `x >> y`.
57+
* A right-shift expression, for example `x >> y`.
5258
*/
53-
class RightShiftExpr extends BinaryBitwiseOperation, RightShiftOperation, @rshift_expr {
59+
class RightShiftExpr extends BinaryBitwiseExpr, RightShiftOperation, @rshift_expr {
5460
override string getOperator() { result = ">>" }
5561

5662
override string getAPrimaryQlClass() { result = "RightShiftExpr" }
5763
}
5864

5965
/**
60-
* An unsigned right-shift operation, for example `x >>> y`.
66+
* An unsigned right-shift expression, for example `x >>> y`.
6167
*/
62-
class UnsignedRightShiftExpr extends BinaryBitwiseOperation, UnsignedRightShiftOperation,
63-
@urshift_expr
64-
{
68+
class UnsignedRightShiftExpr extends BinaryBitwiseExpr, UnsignedRightShiftOperation, @urshift_expr {
6569
override string getOperator() { result = ">>>" }
6670

6771
override string getAPrimaryQlClass() { result = "UnsignedRightShiftExpr" }
6872
}
6973

7074
/**
71-
* A bitwise-and operation, for example `x & y`.
75+
* A bitwise-and expression, for example `x & y`.
7276
*/
73-
class BitwiseAndExpr extends BinaryBitwiseOperation, BitwiseAndOperation, @bit_and_expr {
77+
class BitwiseAndExpr extends BinaryBitwiseExpr, BitwiseAndOperation, @bit_and_expr {
7478
override string getOperator() { result = "&" }
7579

7680
override string getAPrimaryQlClass() { result = "BitwiseAndExpr" }
7781
}
7882

7983
/**
80-
* A bitwise-or operation, for example `x | y`.
84+
* A bitwise-or expression, for example `x | y`.
8185
*/
82-
class BitwiseOrExpr extends BinaryBitwiseOperation, BitwiseOrOperation, @bit_or_expr {
86+
class BitwiseOrExpr extends BinaryBitwiseExpr, BitwiseOrOperation, @bit_or_expr {
8387
override string getOperator() { result = "|" }
8488

8589
override string getAPrimaryQlClass() { result = "BitwiseOrExpr" }
8690
}
8791

8892
/**
89-
* A bitwise exclusive-or operation, for example `x ^ y`.
93+
* A bitwise exclusive-or expression, for example `x ^ y`.
9094
*/
91-
class BitwiseXorExpr extends BinaryBitwiseOperation, BitwiseXorOperation, @bit_xor_expr {
95+
class BitwiseXorExpr extends BinaryBitwiseExpr, BitwiseXorOperation, @bit_xor_expr {
9296
override string getOperator() { result = "^" }
9397

9498
override string getAPrimaryQlClass() { result = "BitwiseXorExpr" }

csharp/ql/lib/semmle/code/csharp/exprs/Expr.qll

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -212,7 +212,7 @@ class LocalConstantDeclExpr extends LocalVariableDeclExpr {
212212
* (`UnaryOperation`), a binary operation (`BinaryOperation`), or a
213213
* ternary operation (`TernaryOperation`).
214214
*/
215-
class Operation extends Expr, @op_expr {
215+
class Operation extends Expr, @operation_expr {
216216
/** Gets the name of the operator in this operation. */
217217
string getOperator() { none() }
218218

@@ -227,7 +227,7 @@ class Operation extends Expr, @op_expr {
227227
* indirection operation (`PointerIndirectionExpr`), an address-of operation
228228
* (`AddressOfExpr`), or a unary logical operation (`UnaryLogicalOperation`).
229229
*/
230-
class UnaryOperation extends Operation, @un_op {
230+
class UnaryOperation extends Operation, @un_operation {
231231
/** Gets the operand of this unary operation. */
232232
Expr getOperand() { result = this.getChild(0) }
233233

@@ -241,7 +241,7 @@ class UnaryOperation extends Operation, @un_op {
241241
* a binary logical operation (`BinaryLogicalOperation`), or an
242242
* assignment (`Assignment`).
243243
*/
244-
class BinaryOperation extends Operation, @bin_op {
244+
class BinaryOperation extends Operation, @bin_operation {
245245
/** Gets the left operand of this binary operation. */
246246
Expr getLeftOperand() { result = this.getChild(0) }
247247

csharp/ql/lib/semmle/code/csharp/exprs/Operation.qll

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,32 +7,32 @@ import Expr
77
/**
88
* A bitwise-and operation, either `x & y` or `x &= y`.
99
*/
10-
class BitwiseAndOperation extends BinaryOperation, @and_operation { }
10+
class BitwiseAndOperation extends BinaryBitwiseOperation, @and_operation { }
1111

1212
/**
1313
* A bitwise-or operation, either `x | y` or `x |= y`.
1414
*/
15-
class BitwiseOrOperation extends BinaryOperation, @or_operation { }
15+
class BitwiseOrOperation extends BinaryBitwiseOperation, @or_operation { }
1616

1717
/**
1818
* A bitwise exclusive-or operation, either `x ^ y` or `x ^= y`.
1919
*/
20-
class BitwiseXorOperation extends BinaryOperation, @xor_operation { }
20+
class BitwiseXorOperation extends BinaryBitwiseOperation, @xor_operation { }
2121

2222
/**
2323
* A left-shift operation, either `x << y` or `x <<= y`.
2424
*/
25-
class LeftShiftOperation extends BinaryOperation, @lshift_operation { }
25+
class LeftShiftOperation extends BinaryBitwiseOperation, @lshift_operation { }
2626

2727
/**
2828
* A right-shift operation, either `x >> y` or `x >>= y`.
2929
*/
30-
class RightShiftOperation extends BinaryOperation, @rshift_operation { }
30+
class RightShiftOperation extends BinaryBitwiseOperation, @rshift_operation { }
3131

3232
/**
3333
* An unsigned right-shift operation, either `x >>> y` or `x >>>= y`.
3434
*/
35-
class UnsignedRightShiftOperation extends BinaryOperation, @urshift_operation { }
35+
class UnsignedRightShiftOperation extends BinaryBitwiseOperation, @urshift_operation { }
3636

3737
/**
3838
* A null-coalescing operation, either `x ?? y` or `x ??= y`.

csharp/ql/test/library-tests/csharp11/operators.ql

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ query predicate binarybitwise(
1111
}
1212

1313
query predicate assignbitwise(
14-
AssignBitwiseOperation op, Expr left, Expr right, string name, string qlclass
14+
AssignBitwiseExpr op, Expr left, Expr right, string name, string qlclass
1515
) {
1616
op.getFile().getStem() = "Operators" and
1717
left = op.getLeftOperand() and

0 commit comments

Comments
 (0)