mirror of
https://github.com/github/codeql.git
synced 2025-12-16 16:53:25 +01:00
Rangeanalysis: Implement shared ssaRead predicate
This commit is contained in:
@@ -106,6 +106,8 @@ signature module Semantic {
|
||||
|
||||
class ShiftRightUnsignedExpr extends BinaryExpr;
|
||||
|
||||
default predicate isAssignOp(BinaryExpr bin) { none() }
|
||||
|
||||
class RelationalExpr extends Expr {
|
||||
Expr getLesserOperand();
|
||||
|
||||
@@ -126,9 +128,15 @@ signature module Semantic {
|
||||
|
||||
class NegateExpr extends UnaryExpr;
|
||||
|
||||
class AddOneExpr extends UnaryExpr;
|
||||
class PreIncExpr extends UnaryExpr;
|
||||
|
||||
class SubOneExpr extends UnaryExpr;
|
||||
class PreDecExpr extends UnaryExpr;
|
||||
|
||||
class PostIncExpr extends UnaryExpr;
|
||||
|
||||
class PostDecExpr extends UnaryExpr;
|
||||
|
||||
class CopyValueExpr extends UnaryExpr;
|
||||
|
||||
class ConditionalExpr extends Expr {
|
||||
Expr getBranchExpr(boolean branch);
|
||||
@@ -168,7 +176,9 @@ signature module Semantic {
|
||||
|
||||
class SsaPhiNode extends SsaVariable;
|
||||
|
||||
class SsaExplicitUpdate extends SsaVariable;
|
||||
class SsaExplicitUpdate extends SsaVariable {
|
||||
Expr getDefiningExpr();
|
||||
}
|
||||
|
||||
class SsaReadPosition {
|
||||
predicate hasReadOfVar(SsaVariable v);
|
||||
@@ -1188,12 +1198,12 @@ module RangeStage<
|
||||
positively = false and
|
||||
(
|
||||
expr instanceof Sem::NegateExpr or
|
||||
expr instanceof Sem::SubOneExpr or
|
||||
expr instanceof Sem::PreDecExpr or
|
||||
getTrackedType(expr.(Sem::DivExpr)) instanceof Sem::FloatingPointType
|
||||
)
|
||||
or
|
||||
positively = true and
|
||||
expr instanceof Sem::AddOneExpr
|
||||
expr instanceof Sem::PreIncExpr
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -0,0 +1,37 @@
|
||||
private import codeql.rangeanalysis.RangeAnalysis
|
||||
|
||||
module MakeUtils<Semantic Lang, DeltaSig D> {
|
||||
/**
|
||||
* Gets an expression that equals `v - d`.
|
||||
*/
|
||||
Lang::Expr ssaRead(Lang::SsaVariable v, D::Delta delta) {
|
||||
result = v.getAUse() and delta = D::fromInt(0)
|
||||
or
|
||||
exists(D::Delta d1, Lang::ConstantIntegerExpr c |
|
||||
result.(Lang::AddExpr).hasOperands(ssaRead(v, d1), c) and
|
||||
delta = D::fromFloat(D::toFloat(d1) - c.getIntValue()) and
|
||||
// In the scope of `x += ..`, which is SSA translated as `x2 = x1 + ..`,
|
||||
// the variable `x1` is shadowed by `x2`, so there's no need to view this
|
||||
// as a read of `x1`.
|
||||
not Lang::isAssignOp(result)
|
||||
)
|
||||
or
|
||||
exists(Lang::SubExpr sub, D::Delta d1, Lang::ConstantIntegerExpr c |
|
||||
result = sub and
|
||||
sub.getLeftOperand() = ssaRead(v, d1) and
|
||||
sub.getRightOperand() = c and
|
||||
delta = D::fromFloat(D::toFloat(d1) + c.getIntValue()) and
|
||||
not Lang::isAssignOp(result)
|
||||
)
|
||||
or
|
||||
result = v.(Lang::SsaExplicitUpdate).getDefiningExpr() and
|
||||
if result instanceof Lang::PostIncExpr
|
||||
then delta = D::fromFloat(1) // x++ === ++x - 1
|
||||
else
|
||||
if result instanceof Lang::PostDecExpr
|
||||
then delta = D::fromFloat(-1) // x-- === --x + 1
|
||||
else delta = D::fromFloat(0)
|
||||
or
|
||||
result.(Lang::CopyValueExpr).getOperand() = ssaRead(v, delta)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user