Skip to content

Commit 80231b6

Browse files
committed
Fixup of range and modulus analysis.
1 parent 9c4d693 commit 80231b6

File tree

6 files changed

+54
-142
lines changed

6 files changed

+54
-142
lines changed

csharp/ql/lib/semmle/code/csharp/dataflow/ModulusAnalysis.qll

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ private predicate valueFlowStepSsa(SsaVariable v, SsaReadPosition pos, Expr e, i
2828
* `ConstantIntegerExpr`s.
2929
*/
3030
private predicate nonConstAddition(Expr add, Expr larg, Expr rarg) {
31-
exists(AddExpr a | a = add |
31+
exists(AddOperation a | a = add |
3232
larg = a.getLhs() and
3333
rarg = a.getRhs()
3434
) and
@@ -41,7 +41,7 @@ private predicate nonConstAddition(Expr add, Expr larg, Expr rarg) {
4141
* a `ConstantIntegerExpr`.
4242
*/
4343
private predicate nonConstSubtraction(Expr sub, Expr larg, Expr rarg) {
44-
exists(SubExpr s | s = sub |
44+
exists(SubOperation s | s = sub |
4545
larg = s.getLhs() and
4646
rarg = s.getRhs()
4747
) and
@@ -50,7 +50,7 @@ private predicate nonConstSubtraction(Expr sub, Expr larg, Expr rarg) {
5050

5151
/** Gets an expression that is the remainder modulo `mod` of `arg`. */
5252
private Expr modExpr(Expr arg, int mod) {
53-
exists(RemExpr rem |
53+
exists(RemOperation rem |
5454
result = rem and
5555
arg = rem.getLeftOperand() and
5656
rem.getRightOperand().(ConstantIntegerExpr).getIntValue() = mod and
@@ -60,7 +60,7 @@ private Expr modExpr(Expr arg, int mod) {
6060
exists(ConstantIntegerExpr c |
6161
mod = 2.pow([1 .. 30]) and
6262
c.getIntValue() = mod - 1 and
63-
result.(BitwiseAndExpr).hasOperands(arg, c)
63+
result.(BitwiseAndOperation).hasOperands(arg, c)
6464
)
6565
}
6666

@@ -105,11 +105,11 @@ private predicate andmaskFactor(int mask, int factor) {
105105
/** Holds if `e` is evenly divisible by `factor`. */
106106
private predicate evenlyDivisibleExpr(Expr e, int factor) {
107107
exists(ConstantIntegerExpr c, int k | k = c.getIntValue() |
108-
e.(MulExpr).getAnOperand() = c and factor = k.abs() and factor >= 2
108+
e.(MulOperation).getAnOperand() = c and factor = k.abs() and factor >= 2
109109
or
110-
e.(LeftShiftExpr).getRhs() = c and factor = 2.pow(k) and k > 0
110+
e.(LeftShiftOperation).getRhs() = c and factor = 2.pow(k) and k > 0
111111
or
112-
e.(BitwiseAndExpr).getAnOperand() = c and factor = max(int f | andmaskFactor(k, f))
112+
e.(BitwiseAndOperation).getAnOperand() = c and factor = max(int f | andmaskFactor(k, f))
113113
)
114114
}
115115

csharp/ql/lib/semmle/code/csharp/dataflow/internal/rangeanalysis/ModulusAnalysisSpecific.qll

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -20,17 +20,17 @@ module Private {
2020

2121
class ConditionalExpr = RU::ExprNode::ConditionalExpr;
2222

23-
class AddExpr = RU::ExprNode::AddExpr;
23+
class AddOperation = RU::ExprNode::AddOperation;
2424

25-
class SubExpr = RU::ExprNode::SubExpr;
25+
class SubOperation = RU::ExprNode::SubOperation;
2626

27-
class RemExpr = RU::ExprNode::RemExpr;
27+
class RemOperation = RU::ExprNode::RemOperation;
2828

29-
class BitwiseAndExpr = RU::ExprNode::BitwiseAndExpr;
29+
class BitwiseAndOperation = RU::ExprNode::BitwiseAndOperation;
3030

31-
class MulExpr = RU::ExprNode::MulExpr;
31+
class MulOperation = RU::ExprNode::MulOperation;
3232

33-
class LeftShiftExpr = RU::ExprNode::LeftShiftExpr;
33+
class LeftShiftOperation = RU::ExprNode::LeftShiftOperation;
3434

3535
predicate guardControlsSsaRead = RU::guardControlsSsaRead/3;
3636

csharp/ql/lib/semmle/code/csharp/dataflow/internal/rangeanalysis/RangeUtils.qll

Lines changed: 37 additions & 125 deletions
Original file line numberDiff line numberDiff line change
@@ -64,15 +64,15 @@ private module Impl {
6464
e2.(ExprNode::PreDecrExpr).getOperand() = e1 and delta = -1
6565
or
6666
exists(ConstantIntegerExpr x |
67-
e2.(ExprNode::AddExpr).getAnOperand() = e1 and
68-
e2.(ExprNode::AddExpr).getAnOperand() = x and
67+
e2.(ExprNode::AddOperation).getAnOperand() = e1 and
68+
e2.(ExprNode::AddOperation).getAnOperand() = x and
6969
e1 != x and
7070
x.getIntValue() = delta
7171
)
7272
or
7373
exists(ConstantIntegerExpr x |
74-
e2.(ExprNode::SubExpr).getLeftOperand() = e1 and
75-
e2.(ExprNode::SubExpr).getRightOperand() = x and
74+
e2.(ExprNode::SubOperation).getLeftOperand() = e1 and
75+
e2.(ExprNode::SubOperation).getRightOperand() = x and
7676
x.getIntValue() = -delta
7777
)
7878
or
@@ -330,168 +330,80 @@ module ExprNode {
330330
}
331331

332332
/** An addition operation. */
333-
abstract private class AddExprImpl extends BinaryOperation {
334-
override TAddOp getOp() { any() }
335-
}
336-
337-
final class AddExpr = AddExprImpl;
338-
339-
private class AddAddExpr extends AddExprImpl {
340-
override CS::AddExpr e;
341-
}
333+
class AddOperation extends BinaryOperation {
334+
override CS::AddOperation e;
342335

343-
private class AddAssignAddExpr extends AddExprImpl, AssignOperation {
344-
override CS::AssignAddExpr e;
336+
override TAddOp getOp() { any() }
345337
}
346338

347339
/** A subtraction operation. */
348-
abstract private class SubExprImpl extends BinaryOperation {
349-
override TSubOp getOp() { any() }
350-
}
351-
352-
final class SubExpr = SubExprImpl;
340+
class SubOperation extends BinaryOperation {
341+
override CS::SubOperation e;
353342

354-
private class AddSubExpr extends SubExprImpl {
355-
override CS::SubExpr e;
356-
}
357-
358-
private class AddAssignSubExpr extends SubExprImpl, AssignOperation {
359-
override CS::AssignSubExpr e;
343+
override TSubOp getOp() { any() }
360344
}
361345

362346
/** A multiplication operation. */
363-
abstract private class MulExprImpl extends BinaryOperation {
364-
override TMulOp getOp() { any() }
365-
}
366-
367-
final class MulExpr = MulExprImpl;
368-
369-
private class AddMulExpr extends MulExprImpl {
370-
override CS::MulExpr e;
371-
}
347+
class MulOperation extends BinaryOperation {
348+
override CS::MulOperation e;
372349

373-
private class AddAssignMulExpr extends MulExprImpl, AssignOperation {
374-
override CS::AssignMulExpr e;
350+
override TMulOp getOp() { any() }
375351
}
376352

377353
/** A division operation. */
378-
abstract private class DivExprImpl extends BinaryOperation {
379-
override TDivOp getOp() { any() }
380-
}
381-
382-
final class DivExpr = DivExprImpl;
354+
class DivOperation extends BinaryOperation {
355+
override CS::DivOperation e;
383356

384-
private class AddDivExpr extends DivExprImpl {
385-
override CS::DivExpr e;
386-
}
387-
388-
private class AddAssignDivExpr extends DivExprImpl, AssignOperation {
389-
override CS::AssignDivExpr e;
357+
override TDivOp getOp() { any() }
390358
}
391359

392360
/** A remainder operation. */
393-
abstract private class RemExprImpl extends BinaryOperation {
394-
override TRemOp getOp() { any() }
395-
}
396-
397-
final class RemExpr = RemExprImpl;
398-
399-
private class AddRemExpr extends RemExprImpl {
400-
override CS::RemExpr e;
401-
}
361+
class RemOperation extends BinaryOperation {
362+
override CS::RemOperation e;
402363

403-
private class AddAssignRemExpr extends RemExprImpl, AssignOperation {
404-
override CS::AssignRemExpr e;
364+
override TRemOp getOp() { any() }
405365
}
406366

407367
/** A bitwise-and operation. */
408-
abstract private class BitwiseAndExprImpl extends BinaryOperation {
409-
override TBitAndOp getOp() { any() }
410-
}
411-
412-
final class BitwiseAndExpr = BitwiseAndExprImpl;
368+
class BitwiseAndOperation extends BinaryOperation {
369+
override CS::BitwiseAndOperation e;
413370

414-
private class AddBitwiseAndExpr extends BitwiseAndExprImpl {
415-
override CS::BitwiseAndExpr e;
416-
}
417-
418-
private class AddAssignAndExpr extends BitwiseAndExprImpl, AssignOperation {
419-
override CS::AssignAndExpr e;
371+
override TBitAndOp getOp() { any() }
420372
}
421373

422374
/** A bitwise-or operation. */
423-
abstract private class BitwiseOrExprImpl extends BinaryOperation {
424-
override TBitOrOp getOp() { any() }
425-
}
375+
class BitwiseOrOperation extends BinaryOperation {
376+
override CS::BitwiseOrOperation e;
426377

427-
final class BitwiseOrExpr = BitwiseOrExprImpl;
428-
429-
private class AddBitwiseOrExpr extends BitwiseOrExprImpl {
430-
override CS::BitwiseOrExpr e;
431-
}
432-
433-
private class AddAssignOrExpr extends BitwiseOrExprImpl, AssignOperation {
434-
override CS::AssignOrExpr e;
378+
override TBitOrOp getOp() { any() }
435379
}
436380

437381
/** A bitwise-xor operation. */
438-
abstract private class BitwiseXorExprImpl extends BinaryOperation {
439-
override TBitXorOp getOp() { any() }
440-
}
441-
442-
final class BitwiseXorExpr = BitwiseXorExprImpl;
443-
444-
private class AddBitwiseXorExpr extends BitwiseXorExprImpl {
445-
override CS::BitwiseXorExpr e;
446-
}
382+
class BitwiseXorOperation extends BinaryOperation {
383+
override CS::BitwiseXorOperation e;
447384

448-
private class AddAssignXorExpr extends BitwiseXorExprImpl, AssignOperation {
449-
override CS::AssignXorExpr e;
385+
override TBitXorOp getOp() { any() }
450386
}
451387

452388
/** A left-shift operation. */
453-
abstract private class LeftShiftExprImpl extends BinaryOperation {
454-
override TLeftShiftOp getOp() { any() }
455-
}
389+
class LeftShiftOperation extends BinaryOperation {
390+
override CS::LeftShiftOperation e;
456391

457-
final class LeftShiftExpr = LeftShiftExprImpl;
458-
459-
private class AddLeftShiftExpr extends LeftShiftExprImpl {
460-
override CS::LeftShiftExpr e;
461-
}
462-
463-
private class AddAssignLeftShiftExpr extends LeftShiftExprImpl, AssignOperation {
464-
override CS::AssignLeftShiftExpr e;
392+
override TLeftShiftOp getOp() { any() }
465393
}
466394

467395
/** A right-shift operation. */
468-
abstract private class RightShiftExprImpl extends BinaryOperation {
469-
override TRightShiftOp getOp() { any() }
470-
}
471-
472-
final class RightShiftExpr = RightShiftExprImpl;
473-
474-
private class AddRightShiftExpr extends RightShiftExprImpl {
475-
override CS::RightShiftExpr e;
476-
}
396+
class RightShiftOperation extends BinaryOperation {
397+
override CS::RightShiftOperation e;
477398

478-
private class AddAssignRightShiftExpr extends RightShiftExprImpl, AssignOperation {
479-
override CS::AssignRightShiftExpr e;
399+
override TRightShiftOp getOp() { any() }
480400
}
481401

482402
/** An unsigned right-shift operation. */
483-
abstract private class UnsignedRightShiftExprImpl extends BinaryOperation {
484-
override TUnsignedRightShiftOp getOp() { any() }
485-
}
403+
class UnsignedRightShiftOperation extends BinaryOperation {
404+
override CS::UnsignedRightShiftOperation e;
486405

487-
final class UnsignedRightShiftExpr = UnsignedRightShiftExprImpl;
488-
489-
private class AddUnsignedRightShiftExpr extends UnsignedRightShiftExprImpl {
490-
override CS::UnsignedRightShiftExpr e;
491-
}
492-
493-
private class AddAssignUnsignedRightShiftExpr extends UnsignedRightShiftExprImpl, AssignOperation {
494-
override CS::AssignUnsighedRightShiftExpr e;
406+
override TUnsignedRightShiftOp getOp() { any() }
495407
}
496408

497409
/** A conditional expression. */

csharp/ql/lib/semmle/code/csharp/dataflow/internal/rangeanalysis/SignAnalysisCommon.qll

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -316,7 +316,7 @@ Sign exprSign(Expr e) {
316316
private Sign specificSubExprSign(Expr e) {
317317
result = exprSign(getASubExprWithSameSign(e))
318318
or
319-
exists(DivExpr div | div = e |
319+
exists(DivOperation div | div = e |
320320
result = exprSign(div.getLeftOperand()) and
321321
result != TZero() and
322322
div.getRightOperand().(RealLiteral).getValue().toFloat() = 0

csharp/ql/lib/semmle/code/csharp/dataflow/internal/rangeanalysis/SignAnalysisSpecific.qll

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ module Private {
4141

4242
class RealLiteral = RU::ExprNode::RealLiteral;
4343

44-
class DivExpr = RU::ExprNode::DivExpr;
44+
class DivOperation = RU::ExprNode::DivOperation;
4545

4646
class UnaryOperation = RU::ExprNode::UnaryOperation;
4747

csharp/ql/lib/semmle/code/csharp/dataflow/internal/rangeanalysis/SsaUtils.qll

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ ExprNode getAnExplicitDefinitionRead(ExprNode src) {
2929
ExprNode ssaRead(Definition v, int delta) {
3030
exists(v.getAReadAtNode(result)) and delta = 0
3131
or
32-
exists(ExprNode::AddExpr add, int d1, ConstantIntegerExpr c |
32+
exists(ExprNode::AddOperation add, int d1, ConstantIntegerExpr c |
3333
result = add and
3434
delta = d1 - c.getIntValue()
3535
|
@@ -38,7 +38,7 @@ ExprNode ssaRead(Definition v, int delta) {
3838
add.getRightOperand() = ssaRead(v, d1) and add.getLeftOperand() = c
3939
)
4040
or
41-
exists(ExprNode::SubExpr sub, int d1, ConstantIntegerExpr c |
41+
exists(ExprNode::SubOperation sub, int d1, ConstantIntegerExpr c |
4242
result = sub and
4343
sub.getLeftOperand() = ssaRead(v, d1) and
4444
sub.getRightOperand() = c and

0 commit comments

Comments
 (0)