Skip to content

Commit bf6cfd3

Browse files
committed
Rangeanalysis: Simplify api.
1 parent 30aefab commit bf6cfd3

File tree

8 files changed

+41
-91
lines changed

8 files changed

+41
-91
lines changed

cpp/ql/lib/semmle/code/cpp/rangeanalysis/new/internal/semantic/analysis/RangeAnalysisImpl.qll

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
private import RangeAnalysisConstantSpecific
22
private import RangeAnalysisRelativeSpecific
33
private import semmle.code.cpp.rangeanalysis.new.internal.semantic.analysis.FloatDelta
4-
private import RangeUtils
54
private import semmle.code.cpp.rangeanalysis.new.internal.semantic.SemanticExpr
65
private import semmle.code.cpp.rangeanalysis.new.internal.semantic.SemanticCFG
76
private import semmle.code.cpp.rangeanalysis.new.internal.semantic.SemanticGuard
@@ -88,6 +87,10 @@ module Sem implements Semantic {
8887

8988
class AddressType = SemAddressType;
9089

90+
SemType getExprType(SemExpr e) { result = e.getSemType() }
91+
92+
SemType getSsaType(SemSsaVariable var) { result = var.getType() }
93+
9194
class SsaVariable = SemSsaVariable;
9295

9396
class SsaPhiNode = SemSsaPhiNode;
@@ -103,7 +106,7 @@ module Sem implements Semantic {
103106

104107
module SignAnalysis implements SignAnalysisSig<Sem> {
105108
private import SignAnalysisCommon as SA
106-
import SA::SignAnalysis<FloatDelta, Util>
109+
import SA::SignAnalysis<FloatDelta>
107110
}
108111

109112
module ConstantBounds implements BoundSig<SemLocation, Sem, FloatDelta> {
@@ -166,18 +169,16 @@ private module ModulusAnalysisInstantiated implements ModulusAnalysisSig<Sem> {
166169
class ModBound = AllBounds::SemBound;
167170

168171
private import codeql.rangeanalysis.ModulusAnalysis as MA
169-
import MA::ModulusAnalysis<SemLocation, Sem, FloatDelta, AllBounds, Util>
172+
import MA::ModulusAnalysis<SemLocation, Sem, FloatDelta, AllBounds>
170173
}
171174

172-
module Util = RangeUtil<FloatDelta, CppLangImplConstant>;
173-
174175
module ConstantStage =
175176
RangeStage<SemLocation, Sem, FloatDelta, ConstantBounds, FloatOverflow, CppLangImplConstant,
176-
SignAnalysis, ModulusAnalysisInstantiated, Util>;
177+
SignAnalysis, ModulusAnalysisInstantiated>;
177178

178179
module RelativeStage =
179180
RangeStage<SemLocation, Sem, FloatDelta, RelativeBounds, FloatOverflow, CppLangImplRelative,
180-
SignAnalysis, ModulusAnalysisInstantiated, Util>;
181+
SignAnalysis, ModulusAnalysisInstantiated>;
181182

182183
private newtype TSemReason =
183184
TSemNoReason() or

cpp/ql/lib/semmle/code/cpp/rangeanalysis/new/internal/semantic/analysis/RangeUtils.qll

Lines changed: 0 additions & 27 deletions
This file was deleted.

cpp/ql/lib/semmle/code/cpp/rangeanalysis/new/internal/semantic/analysis/SignAnalysisCommon.qll

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,9 @@ private import RangeAnalysisImpl
1111
private import SignAnalysisSpecific as Specific
1212
private import semmle.code.cpp.rangeanalysis.new.internal.semantic.Semantic
1313
private import ConstantAnalysis
14-
private import RangeUtils
1514
private import Sign
1615

17-
module SignAnalysis<DeltaSig D, UtilSig<Sem, D> Utils> {
16+
module SignAnalysis<DeltaSig D> {
1817
private import codeql.rangeanalysis.internal.RangeUtils::MakeUtils<Sem, D>
1918

2019
/**
@@ -148,7 +147,7 @@ module SignAnalysis<DeltaSig D, UtilSig<Sem, D> Utils> {
148147
not this instanceof ConstantSignExpr and
149148
(
150149
// Only track numeric types.
151-
Utils::getTrackedType(this) instanceof SemNumericType
150+
Sem::getExprType(this) instanceof SemNumericType
152151
or
153152
// Unless the language says to track this expression anyway.
154153
Specific::trackUnknownNonNumericExpr(this)
@@ -203,7 +202,7 @@ module SignAnalysis<DeltaSig D, UtilSig<Sem, D> Utils> {
203202

204203
/** An expression of an unsigned type. */
205204
private class UnsignedExpr extends FlowSignExpr {
206-
UnsignedExpr() { Utils::getTrackedType(this) instanceof SemUnsignedIntegerType }
205+
UnsignedExpr() { Sem::getExprType(this) instanceof SemUnsignedIntegerType }
207206

208207
override Sign getSignRestriction() {
209208
result = TPos() or
@@ -276,7 +275,7 @@ module SignAnalysis<DeltaSig D, UtilSig<Sem, D> Utils> {
276275
override SemUnboxExpr cast;
277276

278277
UnboxSignExpr() {
279-
exists(SemType fromType | fromType = Utils::getTrackedType(cast.getOperand()) |
278+
exists(SemType fromType | fromType = Sem::getExprType(cast.getOperand()) |
280279
// Only numeric source types are handled here.
281280
fromType instanceof SemNumericType
282281
)
@@ -471,7 +470,7 @@ module SignAnalysis<DeltaSig D, UtilSig<Sem, D> Utils> {
471470
Sign semExprSign(SemExpr e) {
472471
exists(Sign s | s = e.(SignExpr).getSign() |
473472
if
474-
Utils::getTrackedType(e) instanceof SemUnsignedIntegerType and
473+
Sem::getExprType(e) instanceof SemUnsignedIntegerType and
475474
s = TNeg() and
476475
not Specific::ignoreTypeRestrictions(e)
477476
then result = TPos()

cpp/ql/test/library-tests/ir/modulus-analysis/ModulusAnalysis.ql

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,14 @@ import cpp
22
import codeql.rangeanalysis.ModulusAnalysis
33
import semmle.code.cpp.rangeanalysis.new.internal.semantic.Semantic
44
import semmle.code.cpp.rangeanalysis.new.internal.semantic.SemanticLocation
5-
import semmle.code.cpp.rangeanalysis.new.internal.semantic.analysis.RangeUtils
65
import semmle.code.cpp.rangeanalysis.new.internal.semantic.analysis.FloatDelta
76
import semmle.code.cpp.rangeanalysis.new.internal.semantic.analysis.RangeAnalysisRelativeSpecific
87
import semmle.code.cpp.rangeanalysis.new.internal.semantic.analysis.RangeAnalysisImpl
98
import semmle.code.cpp.rangeanalysis.new.internal.semantic.SemanticExprSpecific
109
import semmle.code.cpp.ir.IR as IR
1110
import TestUtilities.InlineExpectationsTest
1211

13-
module ModulusAnalysisInstantiated =
14-
ModulusAnalysis<SemLocation, Sem, FloatDelta, ConstantBounds,
15-
RangeUtil<FloatDelta, CppLangImplRelative>>;
12+
module ModulusAnalysisInstantiated = ModulusAnalysis<SemLocation, Sem, FloatDelta, ConstantBounds>;
1613

1714
module ModulusAnalysisTest implements TestSig {
1815
string getARelevantTag() { result = "mod" }

cpp/ql/test/library-tests/ir/sign-analysis/SignAnalysis.ql

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,13 @@
11
import cpp
22
import semmle.code.cpp.rangeanalysis.new.internal.semantic.analysis.SignAnalysisCommon
33
import semmle.code.cpp.rangeanalysis.new.internal.semantic.Semantic
4-
import semmle.code.cpp.rangeanalysis.new.internal.semantic.analysis.RangeUtils
54
import semmle.code.cpp.rangeanalysis.new.internal.semantic.analysis.FloatDelta
65
import semmle.code.cpp.rangeanalysis.new.internal.semantic.analysis.RangeAnalysisRelativeSpecific
76
import semmle.code.cpp.rangeanalysis.new.internal.semantic.SemanticExprSpecific
87
import semmle.code.cpp.ir.IR as IR
98
import TestUtilities.InlineExpectationsTest
109

11-
module SignAnalysisInstantiated =
12-
SignAnalysis<FloatDelta, RangeUtil<FloatDelta, CppLangImplRelative>>;
10+
module SignAnalysisInstantiated = SignAnalysis<FloatDelta>;
1311

1412
module SignAnalysisTest implements TestSig {
1513
string getARelevantTag() { result = "sign" }

java/ql/lib/semmle/code/java/dataflow/RangeAnalysis.qll

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -241,6 +241,10 @@ module Sem implements Semantic {
241241
AddressType() { none() }
242242
}
243243

244+
Type getExprType(Expr e) { result = e.getType() }
245+
246+
Type getSsaType(SsaVariable var) { result = var.getSourceVariable().getType() }
247+
244248
final private class FinalSsaVariable = SSA::SsaVariable;
245249

246250
class SsaVariable extends FinalSsaVariable {
@@ -281,7 +285,7 @@ module Modulus implements ModulusAnalysisSig<Sem> {
281285
class ModBound = Bound;
282286

283287
private import codeql.rangeanalysis.ModulusAnalysis as Mod
284-
import Mod::ModulusAnalysis<Location, Sem, IntDelta, Bounds, Utils>
288+
import Mod::ModulusAnalysis<Location, Sem, IntDelta, Bounds>
285289
}
286290

287291
module IntDelta implements DeltaSig {
@@ -365,14 +369,6 @@ module JavaLangImpl implements LangSig<Sem, IntDelta> {
365369
predicate javaCompatibility() { any() }
366370
}
367371

368-
module Utils implements UtilSig<Sem, IntDelta> {
369-
Sem::Type getTrackedTypeForSsaVariable(Sem::SsaVariable var) {
370-
result = var.getSourceVariable().getType()
371-
}
372-
373-
Sem::Type getTrackedType(Sem::Expr e) { result = e.getType() }
374-
}
375-
376372
module Bounds implements BoundSig<Location, Sem, IntDelta> {
377373
class SemBound = Bound;
378374

@@ -390,7 +386,7 @@ module Overflow implements OverflowSig<Sem, IntDelta> {
390386
}
391387

392388
module Range =
393-
RangeStage<Location, Sem, IntDelta, Bounds, Overflow, JavaLangImpl, SignInp, Modulus, Utils>;
389+
RangeStage<Location, Sem, IntDelta, Bounds, Overflow, JavaLangImpl, SignInp, Modulus>;
394390

395391
predicate bounded = Range::semBounded/5;
396392

shared/rangeanalysis/codeql/rangeanalysis/ModulusAnalysis.qll

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,7 @@ private import codeql.util.Location
1414
private import RangeAnalysis
1515

1616
module ModulusAnalysis<
17-
LocationSig Location, Semantic Sem, DeltaSig D, BoundSig<Location, Sem, D> Bounds,
18-
UtilSig<Sem, D> U>
17+
LocationSig Location, Semantic Sem, DeltaSig D, BoundSig<Location, Sem, D> Bounds>
1918
{
2019
private import internal.RangeUtils::MakeUtils<Sem, D>
2120

shared/rangeanalysis/codeql/rangeanalysis/RangeAnalysis.qll

Lines changed: 19 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -188,6 +188,12 @@ signature module Semantic {
188188

189189
class AddressType extends Type;
190190

191+
/** Gets the type of an SSA variable. */
192+
Type getSsaType(SsaVariable var);
193+
194+
/** Gets the type of an expression. */
195+
Type getExprType(Expr e);
196+
191197
class SsaVariable {
192198
Expr getAUse();
193199

@@ -285,24 +291,6 @@ signature module LangSig<Semantic Sem, DeltaSig D> {
285291
default predicate javaCompatibility() { none() }
286292
}
287293

288-
signature module UtilSig<Semantic Sem, DeltaSig DeltaParam> {
289-
/**
290-
* Gets the type used to track the specified source variable's range information.
291-
*
292-
* Usually, this just `e.getType()`, but the language can override this to track immutable boxed
293-
* primitive types as the underlying primitive type.
294-
*/
295-
Sem::Type getTrackedTypeForSsaVariable(Sem::SsaVariable var);
296-
297-
/**
298-
* Gets the type used to track the specified expression's range information.
299-
*
300-
* Usually, this just `e.getSemType()`, but the language can override this to track immutable boxed
301-
* primitive types as the underlying primitive type.
302-
*/
303-
Sem::Type getTrackedType(Sem::Expr e);
304-
}
305-
306294
signature module BoundSig<LocationSig Location, Semantic Sem, DeltaSig D> {
307295
class SemBound {
308296
string toString();
@@ -326,11 +314,10 @@ signature module OverflowSig<Semantic Sem, DeltaSig D> {
326314
module RangeStage<
327315
LocationSig Location, Semantic Sem, DeltaSig D, BoundSig<Location, Sem, D> Bounds,
328316
OverflowSig<Sem, D> OverflowParam, LangSig<Sem, D> LangParam, SignAnalysisSig<Sem> SignAnalysis,
329-
ModulusAnalysisSig<Sem> ModulusAnalysisParam, UtilSig<Sem, D> UtilParam>
317+
ModulusAnalysisSig<Sem> ModulusAnalysisParam>
330318
{
331319
private import Bounds
332320
private import LangParam
333-
private import UtilParam
334321
private import D
335322
private import OverflowParam
336323
private import SignAnalysis
@@ -375,8 +362,8 @@ module RangeStage<
375362
*/
376363
private class SafeCastExpr extends ConvertOrBoxExpr {
377364
SafeCastExpr() {
378-
Sem::conversionCannotOverflow(getTrackedType(pragma[only_bind_into](this.getOperand())),
379-
pragma[only_bind_out](getTrackedType(this)))
365+
Sem::conversionCannotOverflow(Sem::getExprType(pragma[only_bind_into](this.getOperand())),
366+
pragma[only_bind_out](Sem::getExprType(this)))
380367
}
381368
}
382369

@@ -386,14 +373,14 @@ module RangeStage<
386373
private class NarrowingCastExpr extends ConvertOrBoxExpr {
387374
NarrowingCastExpr() {
388375
not this instanceof SafeCastExpr and
389-
typeBound(getTrackedType(this), _, _)
376+
typeBound(Sem::getExprType(this), _, _)
390377
}
391378

392379
/** Gets the lower bound of the resulting type. */
393-
float getLowerBound() { typeBound(getTrackedType(this), result, _) }
380+
float getLowerBound() { typeBound(Sem::getExprType(this), result, _) }
394381

395382
/** Gets the upper bound of the resulting type. */
396-
float getUpperBound() { typeBound(getTrackedType(this), _, result) }
383+
float getUpperBound() { typeBound(Sem::getExprType(this), _, result) }
397384
}
398385

399386
cached
@@ -556,8 +543,8 @@ module RangeStage<
556543
) and
557544
(
558545
if
559-
getTrackedTypeForSsaVariable(v) instanceof Sem::IntegerType or
560-
getTrackedTypeForSsaVariable(v) instanceof Sem::AddressType
546+
Sem::getSsaType(v) instanceof Sem::IntegerType or
547+
Sem::getSsaType(v) instanceof Sem::AddressType
561548
then
562549
upper = true and strengthen = -1
563550
or
@@ -666,7 +653,7 @@ module RangeStage<
666653
private predicate unequalFlowStepIntegralSsa(
667654
Sem::SsaVariable v, SsaReadPosition pos, Sem::Expr e, D::Delta delta, SemReason reason
668655
) {
669-
getTrackedTypeForSsaVariable(v) instanceof Sem::IntegerType and
656+
Sem::getSsaType(v) instanceof Sem::IntegerType and
670657
exists(Sem::Guard guard, boolean testIsTrue |
671658
pos.hasReadOfVar(v) and
672659
guard = eqFlowCond(v, e, delta, false, testIsTrue) and
@@ -677,12 +664,12 @@ module RangeStage<
677664

678665
/** Holds if `e >= 1` as determined by sign analysis. */
679666
private predicate strictlyPositiveIntegralExpr(Sem::Expr e) {
680-
semStrictlyPositive(e) and getTrackedType(e) instanceof Sem::IntegerType
667+
semStrictlyPositive(e) and Sem::getExprType(e) instanceof Sem::IntegerType
681668
}
682669

683670
/** Holds if `e <= -1` as determined by sign analysis. */
684671
private predicate strictlyNegativeIntegralExpr(Sem::Expr e) {
685-
semStrictlyNegative(e) and getTrackedType(e) instanceof Sem::IntegerType
672+
semStrictlyNegative(e) and Sem::getExprType(e) instanceof Sem::IntegerType
686673
}
687674

688675
/**
@@ -764,7 +751,7 @@ module RangeStage<
764751
* therefore only valid for non-negative numbers.
765752
*/
766753
private predicate boundFlowStepDiv(Sem::Expr e2, Sem::Expr e1, D::Delta factor) {
767-
getTrackedType(e2) instanceof Sem::IntegerType and
754+
Sem::getExprType(e2) instanceof Sem::IntegerType and
768755
exists(Sem::ConstantIntegerExpr c, D::Delta k |
769756
k = D::fromInt(c.getIntValue()) and D::toFloat(k) > 0
770757
|
@@ -1101,7 +1088,7 @@ module RangeStage<
11011088
(
11021089
expr instanceof Sem::NegateExpr or
11031090
expr instanceof Sem::PreDecExpr or
1104-
getTrackedType(expr.(Sem::DivExpr)) instanceof Sem::FloatingPointType
1091+
Sem::getExprType(expr.(Sem::DivExpr)) instanceof Sem::FloatingPointType
11051092
)
11061093
or
11071094
positively = true and

0 commit comments

Comments
 (0)