Skip to content

Commit afa3399

Browse files
author
Dave Bartolomeo
committed
Zero diffs between Java AST and Semantic range analysis
1 parent 8b4d6a2 commit afa3399

8 files changed

Lines changed: 220 additions & 170 deletions

File tree

java/ql/lib/semmle/code/java/semantic/SemanticExpr.qll

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,8 @@ private newtype TOpcode =
4343
TSubOne() or // TODO: Combine with `TSub`
4444
TConditional() or // TODO: Represent as flow
4545
TCall() or
46+
TBox() or
47+
TUnbox() or
4648
TUnknown()
4749

4850
class Opcode extends TOpcode {
@@ -170,6 +172,14 @@ module Opcode {
170172
override string toString() { result = "StringConstant" }
171173
}
172174

175+
class Box extends Opcode, TBox {
176+
override string toString() { result = "Box" }
177+
}
178+
179+
class Unbox extends Opcode, TUnbox {
180+
override string toString() { result = "Unbox" }
181+
}
182+
173183
class Unknown extends Opcode, TUnknown {
174184
override string toString() { result = "Unknown" }
175185
}
@@ -325,6 +335,10 @@ class SemShiftRightExpr extends SemBinaryExpr {
325335
SemShiftRightExpr() { opcode instanceof Opcode::ShiftRight }
326336
}
327337

338+
class SemShiftRightUnsignedExpr extends SemBinaryExpr {
339+
SemShiftRightUnsignedExpr() { opcode instanceof Opcode::ShiftRightUnsigned }
340+
}
341+
328342
class SemBitAndExpr extends SemBinaryExpr {
329343
SemBitAndExpr() { opcode instanceof Opcode::BitAnd }
330344
}
@@ -345,6 +359,14 @@ class SemUnaryExpr extends SemKnownExpr {
345359
final SemExpr getOperand() { result = operand }
346360
}
347361

362+
class SemBoxExpr extends SemUnaryExpr {
363+
SemBoxExpr() { opcode instanceof Opcode::Box }
364+
}
365+
366+
class SemUnboxExpr extends SemUnaryExpr {
367+
SemUnboxExpr() { opcode instanceof Opcode::Unbox }
368+
}
369+
348370
class SemConvertExpr extends SemUnaryExpr {
349371
SemConvertExpr() { opcode instanceof Opcode::Convert }
350372
}

java/ql/lib/semmle/code/java/semantic/SemanticExprSpecific.qll

Lines changed: 24 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -249,12 +249,24 @@ module SemanticExprConfig {
249249
)
250250
)
251251
or
252-
exists(J::CastExpr cast | cast = javaExpr |
253-
// TODO: Boolean? Null? Boxing?
254-
getSemanticType(cast.getType()) instanceof SemNumericType and
255-
getSemanticType(cast.getExpr().getType()) instanceof SemNumericType and
256-
opcode instanceof Opcode::Convert and
257-
operand = getResultExpr(cast.getExpr())
252+
exists(J::CastExpr cast, J::Type srcType, J::Type destType |
253+
cast = javaExpr and srcType = cast.getExpr().getType() and destType = cast.getType()
254+
|
255+
operand = getResultExpr(cast.getExpr()) and
256+
(
257+
// TODO: Conversions between `boolean` and numeric types should probably be comparisons
258+
srcType instanceof J::PrimitiveType and
259+
destType instanceof J::PrimitiveType and
260+
opcode instanceof Opcode::Convert
261+
or
262+
srcType instanceof J::PrimitiveType and
263+
destType instanceof J::RefType and
264+
opcode instanceof Opcode::Box
265+
or
266+
srcType instanceof J::RefType and
267+
destType instanceof J::PrimitiveType and
268+
opcode instanceof Opcode::Unbox
269+
)
258270
)
259271
or
260272
exists(J::AssignExpr assign | assign = javaExpr |
@@ -344,9 +356,8 @@ module SemanticExprConfig {
344356
final Location getLocation() { result = super.getLocation() }
345357
}
346358

347-
predicate explicitUpdate(SsaVariable v, SemType type, Expr sourceExpr) {
359+
predicate explicitUpdate(SsaVariable v, Expr sourceExpr) {
348360
exists(SSA::SsaExplicitUpdate update | v = update |
349-
type = getSemanticType(update.getSourceVariable().getType()) and
350361
exists(J::Expr expr | expr = update.getDefiningExpr() |
351362
(
352363
expr instanceof J::AssignOp or
@@ -370,14 +381,16 @@ module SemanticExprConfig {
370381
)
371382
}
372383

373-
predicate phi(SsaVariable v, SemType type) {
374-
type = getSemanticType(v.(SSA::SsaPhiNode).getSourceVariable().getType())
375-
}
384+
predicate phi(SsaVariable v) { v instanceof SSA::SsaPhiNode }
376385

377386
SsaVariable getAPhiInput(SsaVariable v) { result = v.(SSA::SsaPhiNode).getAPhiInput() }
378387

379388
Expr getAUse(SsaVariable v) { result = getResultExpr(v.(SSA::SsaVariable).getAUse()) }
380389

390+
SemType getSsaVariableType(SsaVariable v) {
391+
result = getSemanticType(v.(SSA::SsaVariable).getSourceVariable().getType())
392+
}
393+
381394
BasicBlock getSsaVariableBasicBlock(SsaVariable v) {
382395
result = v.(SSA::SsaVariable).getBasicBlock()
383396
}

java/ql/lib/semmle/code/java/semantic/SemanticGuard.qll

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@ class SemGuard instanceof Specific::Guard {
1414

1515
final string toString() { result = super.toString() }
1616

17+
final Specific::Location getLocation() { result = super.getLocation() }
18+
1719
final predicate isEquality(SemExpr e1, SemExpr e2, boolean polarity) {
1820
Specific::equalityGuard(this, e1, e2, polarity)
1921
}

java/ql/lib/semmle/code/java/semantic/SemanticSSA.qll

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,29 +8,27 @@ private import SemanticType
88
private import SemanticExprSpecific::SemanticExprConfig as Specific
99

1010
class SemSsaVariable instanceof Specific::SsaVariable {
11-
SemType type;
12-
1311
final string toString() { result = super.toString() }
1412

1513
final Specific::Location getLocation() { result = super.getLocation() }
1614

1715
final SemLoadExpr getAUse() { result = Specific::getAUse(this) }
1816

19-
final SemType getType() { result = type }
17+
final SemType getType() { result = Specific::getSsaVariableType(this) }
2018

2119
final SemBasicBlock getBasicBlock() { result = Specific::getSsaVariableBasicBlock(this) }
2220
}
2321

2422
class SemSsaExplicitUpdate extends SemSsaVariable {
2523
SemExpr sourceExpr;
2624

27-
SemSsaExplicitUpdate() { Specific::explicitUpdate(this, type, sourceExpr) }
25+
SemSsaExplicitUpdate() { Specific::explicitUpdate(this, sourceExpr) }
2826

2927
final SemExpr getSourceExpr() { result = sourceExpr }
3028
}
3129

3230
class SemSsaPhiNode extends SemSsaVariable {
33-
SemSsaPhiNode() { Specific::phi(this, type) }
31+
SemSsaPhiNode() { Specific::phi(this) }
3432

3533
final SemSsaVariable getAPhiInput() { result = Specific::getAPhiInput(this) }
3634
}

0 commit comments

Comments
 (0)