Skip to content

Commit 638d039

Browse files
committed
Java, C#: Refactor explicitSsaDefSign in sign analysis
1 parent 7545fe7 commit 638d039

4 files changed

Lines changed: 77 additions & 30 deletions

File tree

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

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -245,6 +245,19 @@ private Sign ssaDefSign(SsaVariable v) {
245245
)
246246
}
247247

248+
/** Returns the sign of explicit SSA definition `v`. */
249+
Sign explicitSsaDefSign(SsaVariable v) {
250+
exists(VariableUpdate def | def = getExplicitSsaAssignment(v) |
251+
result = exprSign(getExprFromSsaAssignment(def))
252+
or
253+
anySign(result) and explicitSsaDefWithAnySign(def)
254+
or
255+
result = exprSign(getIncrementOperand(def)).inc()
256+
or
257+
result = exprSign(getDecrementOperand(def)).dec()
258+
)
259+
}
260+
248261
/** Returns the sign of implicit SSA definition `v`. */
249262
private Sign implicitSsaDefSign(SsaVariable v) {
250263
result = fieldSign(getImplicitSsaDeclaration(v))

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

Lines changed: 24 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,8 @@ module Private {
3232

3333
class Expr = CS::Expr;
3434

35+
class VariableUpdate = CS::AssignableDefinition;
36+
3537
predicate ssaRead = SU::ssaRead/2;
3638
}
3739

@@ -107,19 +109,28 @@ private module Impl {
107109
}
108110
}
109111

110-
/** Returns the sign of explicit SSA definition `v`. */
111-
Sign explicitSsaDefSign(Ssa::ExplicitDefinition v) {
112-
exists(AssignableDefinition def | def = v.getADefinition() |
113-
result = exprSign(def.getSource())
114-
or
115-
anySign(result) and
116-
not exists(def.getSource()) and
117-
not def.getElement() instanceof MutatorOperation
118-
or
119-
result = exprSign(def.getElement().(IncrementOperation).getOperand()).inc()
120-
or
121-
result = exprSign(def.getElement().(DecrementOperation).getOperand()).dec()
122-
)
112+
/** Returns the underlying variable update of the explicit SSA variable `v`. */
113+
AssignableDefinition getExplicitSsaAssignment(Ssa::ExplicitDefinition v) {
114+
result = v.getADefinition()
115+
}
116+
117+
/** Returns the assignment of the variable update `def`. */
118+
Expr getExprFromSsaAssignment(AssignableDefinition def) { result = def.getSource() }
119+
120+
/** Holds if `def` can have any sign. */
121+
predicate explicitSsaDefWithAnySign(AssignableDefinition def) {
122+
not exists(def.getSource()) and
123+
not def.getElement() instanceof MutatorOperation
124+
}
125+
126+
/** Returns the operand of the operation if `def` is a decrement. */
127+
Expr getDecrementOperand(AssignableDefinition def) {
128+
result = def.getElement().(DecrementOperation).getOperand()
129+
}
130+
131+
/** Returns the operand of the operation if `def` is an increment. */
132+
Expr getIncrementOperand(AssignableDefinition def) {
133+
result = def.getElement().(IncrementOperation).getOperand()
123134
}
124135

125136
/** Gets the variable underlying the implicit SSA variable `v`. */

java/ql/src/semmle/code/java/dataflow/internal/rangeanalysis/SignAnalysisCommon.qll

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -245,6 +245,19 @@ private Sign ssaDefSign(SsaVariable v) {
245245
)
246246
}
247247

248+
/** Returns the sign of explicit SSA definition `v`. */
249+
Sign explicitSsaDefSign(SsaVariable v) {
250+
exists(VariableUpdate def | def = getExplicitSsaAssignment(v) |
251+
result = exprSign(getExprFromSsaAssignment(def))
252+
or
253+
anySign(result) and explicitSsaDefWithAnySign(def)
254+
or
255+
result = exprSign(getIncrementOperand(def)).inc()
256+
or
257+
result = exprSign(getDecrementOperand(def)).dec()
258+
)
259+
}
260+
248261
/** Returns the sign of implicit SSA definition `v`. */
249262
private Sign implicitSsaDefSign(SsaVariable v) {
250263
result = fieldSign(getImplicitSsaDeclaration(v))

java/ql/src/semmle/code/java/dataflow/internal/rangeanalysis/SignAnalysisSpecific.qll

Lines changed: 27 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,8 @@ module Private {
3636

3737
class NumericOrCharType = J::NumericOrCharType;
3838

39+
class VariableUpdate = J::VariableUpdate;
40+
3941
predicate ssaRead = RU::ssaRead/2;
4042

4143
predicate guardControlsSsaRead = RU::guardControlsSsaRead/3;
@@ -96,23 +98,31 @@ private module Impl {
9698
e instanceof ClassInstanceExpr and e.getType() instanceof NumericOrCharType
9799
}
98100

99-
/** Returns the sign of explicit SSA definition `v`. */
100-
Sign explicitSsaDefSign(SsaVariable v) {
101-
exists(VariableUpdate def | def = v.(SsaExplicitUpdate).getDefiningExpr() |
102-
result = exprSign(def.(VariableAssign).getSource())
103-
or
104-
exists(EnhancedForStmt for | def = for.getVariable())
105-
or
106-
result = exprSign(def.(PostIncExpr).getExpr()).inc()
107-
or
108-
result = exprSign(def.(PreIncExpr).getExpr()).inc()
109-
or
110-
result = exprSign(def.(PostDecExpr).getExpr()).dec()
111-
or
112-
result = exprSign(def.(PreDecExpr).getExpr()).dec()
113-
or
114-
exists(AssignOp a | a = def and result = exprSign(a))
115-
)
101+
/** Returns the underlying variable update of the explicit SSA variable `v`. */
102+
VariableUpdate getExplicitSsaAssignment(SsaVariable v) {
103+
result = v.(SsaExplicitUpdate).getDefiningExpr()
104+
}
105+
106+
/** Returns the assignment of the variable update `def`. */
107+
Expr getExprFromSsaAssignment(VariableUpdate def) {
108+
result = def.(VariableAssign).getSource()
109+
or
110+
exists(AssignOp a | a = def and result = a)
111+
}
112+
113+
/** Holds if `def` can have any sign. */
114+
predicate explicitSsaDefWithAnySign(VariableUpdate def) {
115+
exists(EnhancedForStmt for | def = for.getVariable())
116+
}
117+
118+
/** Returns the operand of the operation if `def` is a decrement. */
119+
Expr getDecrementOperand(Element e) {
120+
result = e.(PostDecExpr).getExpr() or result = e.(PreDecExpr).getExpr()
121+
}
122+
123+
/** Returns the operand of the operation if `def` is an increment. */
124+
Expr getIncrementOperand(Element e) {
125+
result = e.(PostIncExpr).getExpr() or result = e.(PreIncExpr).getExpr()
116126
}
117127

118128
/** Gets the variable underlying the implicit SSA variable `v`. */

0 commit comments

Comments
 (0)