Skip to content

Commit 69398b0

Browse files
committed
Fixup of range and modulus analysis.
1 parent a72f848 commit 69398b0

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
@@ -52,15 +52,15 @@ private module Impl {
5252
e2.(ExprNode::PreDecrExpr).getOperand() = e1 and delta = -1
5353
or
5454
exists(ConstantIntegerExpr x |
55-
e2.(ExprNode::AddExpr).getAnOperand() = e1 and
56-
e2.(ExprNode::AddExpr).getAnOperand() = x and
55+
e2.(ExprNode::AddOperation).getAnOperand() = e1 and
56+
e2.(ExprNode::AddOperation).getAnOperand() = x and
5757
e1 != x and
5858
x.getIntValue() = delta
5959
)
6060
or
6161
exists(ConstantIntegerExpr x |
62-
e2.(ExprNode::SubExpr).getLeftOperand() = e1 and
63-
e2.(ExprNode::SubExpr).getRightOperand() = x and
62+
e2.(ExprNode::SubOperation).getLeftOperand() = e1 and
63+
e2.(ExprNode::SubOperation).getRightOperand() = x and
6464
x.getIntValue() = -delta
6565
)
6666
or
@@ -318,168 +318,80 @@ module ExprNode {
318318
}
319319

320320
/** An addition operation. */
321-
abstract private class AddExprImpl extends BinaryOperation {
322-
override TAddOp getOp() { any() }
323-
}
324-
325-
final class AddExpr = AddExprImpl;
326-
327-
private class AddAddExpr extends AddExprImpl {
328-
override CS::AddExpr e;
329-
}
321+
class AddOperation extends BinaryOperation {
322+
override CS::AddOperation e;
330323

331-
private class AddAssignAddExpr extends AddExprImpl, AssignOperation {
332-
override CS::AssignAddExpr e;
324+
override TAddOp getOp() { any() }
333325
}
334326

335327
/** A subtraction operation. */
336-
abstract private class SubExprImpl extends BinaryOperation {
337-
override TSubOp getOp() { any() }
338-
}
339-
340-
final class SubExpr = SubExprImpl;
328+
class SubOperation extends BinaryOperation {
329+
override CS::SubOperation e;
341330

342-
private class AddSubExpr extends SubExprImpl {
343-
override CS::SubExpr e;
344-
}
345-
346-
private class AddAssignSubExpr extends SubExprImpl, AssignOperation {
347-
override CS::AssignSubExpr e;
331+
override TSubOp getOp() { any() }
348332
}
349333

350334
/** A multiplication operation. */
351-
abstract private class MulExprImpl extends BinaryOperation {
352-
override TMulOp getOp() { any() }
353-
}
354-
355-
final class MulExpr = MulExprImpl;
356-
357-
private class AddMulExpr extends MulExprImpl {
358-
override CS::MulExpr e;
359-
}
335+
class MulOperation extends BinaryOperation {
336+
override CS::MulOperation e;
360337

361-
private class AddAssignMulExpr extends MulExprImpl, AssignOperation {
362-
override CS::AssignMulExpr e;
338+
override TMulOp getOp() { any() }
363339
}
364340

365341
/** A division operation. */
366-
abstract private class DivExprImpl extends BinaryOperation {
367-
override TDivOp getOp() { any() }
368-
}
369-
370-
final class DivExpr = DivExprImpl;
342+
class DivOperation extends BinaryOperation {
343+
override CS::DivOperation e;
371344

372-
private class AddDivExpr extends DivExprImpl {
373-
override CS::DivExpr e;
374-
}
375-
376-
private class AddAssignDivExpr extends DivExprImpl, AssignOperation {
377-
override CS::AssignDivExpr e;
345+
override TDivOp getOp() { any() }
378346
}
379347

380348
/** A remainder operation. */
381-
abstract private class RemExprImpl extends BinaryOperation {
382-
override TRemOp getOp() { any() }
383-
}
384-
385-
final class RemExpr = RemExprImpl;
386-
387-
private class AddRemExpr extends RemExprImpl {
388-
override CS::RemExpr e;
389-
}
349+
class RemOperation extends BinaryOperation {
350+
override CS::RemOperation e;
390351

391-
private class AddAssignRemExpr extends RemExprImpl, AssignOperation {
392-
override CS::AssignRemExpr e;
352+
override TRemOp getOp() { any() }
393353
}
394354

395355
/** A bitwise-and operation. */
396-
abstract private class BitwiseAndExprImpl extends BinaryOperation {
397-
override TBitAndOp getOp() { any() }
398-
}
399-
400-
final class BitwiseAndExpr = BitwiseAndExprImpl;
356+
class BitwiseAndOperation extends BinaryOperation {
357+
override CS::BitwiseAndOperation e;
401358

402-
private class AddBitwiseAndExpr extends BitwiseAndExprImpl {
403-
override CS::BitwiseAndExpr e;
404-
}
405-
406-
private class AddAssignAndExpr extends BitwiseAndExprImpl, AssignOperation {
407-
override CS::AssignAndExpr e;
359+
override TBitAndOp getOp() { any() }
408360
}
409361

410362
/** A bitwise-or operation. */
411-
abstract private class BitwiseOrExprImpl extends BinaryOperation {
412-
override TBitOrOp getOp() { any() }
413-
}
363+
class BitwiseOrOperation extends BinaryOperation {
364+
override CS::BitwiseOrOperation e;
414365

415-
final class BitwiseOrExpr = BitwiseOrExprImpl;
416-
417-
private class AddBitwiseOrExpr extends BitwiseOrExprImpl {
418-
override CS::BitwiseOrExpr e;
419-
}
420-
421-
private class AddAssignOrExpr extends BitwiseOrExprImpl, AssignOperation {
422-
override CS::AssignOrExpr e;
366+
override TBitOrOp getOp() { any() }
423367
}
424368

425369
/** A bitwise-xor operation. */
426-
abstract private class BitwiseXorExprImpl extends BinaryOperation {
427-
override TBitXorOp getOp() { any() }
428-
}
429-
430-
final class BitwiseXorExpr = BitwiseXorExprImpl;
431-
432-
private class AddBitwiseXorExpr extends BitwiseXorExprImpl {
433-
override CS::BitwiseXorExpr e;
434-
}
370+
class BitwiseXorOperation extends BinaryOperation {
371+
override CS::BitwiseXorOperation e;
435372

436-
private class AddAssignXorExpr extends BitwiseXorExprImpl, AssignOperation {
437-
override CS::AssignXorExpr e;
373+
override TBitXorOp getOp() { any() }
438374
}
439375

440376
/** A left-shift operation. */
441-
abstract private class LeftShiftExprImpl extends BinaryOperation {
442-
override TLeftShiftOp getOp() { any() }
443-
}
377+
class LeftShiftOperation extends BinaryOperation {
378+
override CS::LeftShiftOperation e;
444379

445-
final class LeftShiftExpr = LeftShiftExprImpl;
446-
447-
private class AddLeftShiftExpr extends LeftShiftExprImpl {
448-
override CS::LeftShiftExpr e;
449-
}
450-
451-
private class AddAssignLeftShiftExpr extends LeftShiftExprImpl, AssignOperation {
452-
override CS::AssignLeftShiftExpr e;
380+
override TLeftShiftOp getOp() { any() }
453381
}
454382

455383
/** A right-shift operation. */
456-
abstract private class RightShiftExprImpl extends BinaryOperation {
457-
override TRightShiftOp getOp() { any() }
458-
}
459-
460-
final class RightShiftExpr = RightShiftExprImpl;
461-
462-
private class AddRightShiftExpr extends RightShiftExprImpl {
463-
override CS::RightShiftExpr e;
464-
}
384+
class RightShiftOperation extends BinaryOperation {
385+
override CS::RightShiftOperation e;
465386

466-
private class AddAssignRightShiftExpr extends RightShiftExprImpl, AssignOperation {
467-
override CS::AssignRightShiftExpr e;
387+
override TRightShiftOp getOp() { any() }
468388
}
469389

470390
/** An unsigned right-shift operation. */
471-
abstract private class UnsignedRightShiftExprImpl extends BinaryOperation {
472-
override TUnsignedRightShiftOp getOp() { any() }
473-
}
391+
class UnsignedRightShiftOperation extends BinaryOperation {
392+
override CS::UnsignedRightShiftOperation e;
474393

475-
final class UnsignedRightShiftExpr = UnsignedRightShiftExprImpl;
476-
477-
private class AddUnsignedRightShiftExpr extends UnsignedRightShiftExprImpl {
478-
override CS::UnsignedRightShiftExpr e;
479-
}
480-
481-
private class AddAssignUnsignedRightShiftExpr extends UnsignedRightShiftExprImpl, AssignOperation {
482-
override CS::AssignUnsighedRightShiftExpr e;
394+
override TUnsignedRightShiftOp getOp() { any() }
483395
}
484396

485397
/** 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)