Skip to content

Commit 04fae43

Browse files
author
Dave Bartolomeo
committed
Minimize language-specific code for sign analysis
1 parent 952e495 commit 04fae43

8 files changed

Lines changed: 452 additions & 284 deletions

File tree

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ class SemBasicBlock extends TSemBasicBlock {
2121
block.bbDominates(getJavaBasicBlock(otherBlock))
2222
}
2323

24-
final SemExpr getAnExpr() { result = getSemanticExpr(block.getANode()) }
24+
final SemExpr getAnExpr() { SemanticExprConfig::getExprBasicBlock(result) = this }
2525
}
2626

2727
SemBasicBlock getSemanticBasicBlock(BasicBlock block) { result = MkSemBasicBlock(block) }

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

Lines changed: 24 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ private import SemanticType
99
private import SemanticExprSpecific::SemanticExprConfig as Specific
1010

1111
private newtype TOpcode =
12+
TInitializeParameter() or
1213
TCopyValue() or
1314
TLoad() or
1415
TStore() or
@@ -40,8 +41,6 @@ private newtype TOpcode =
4041
TStringConstant() or
4142
TAddOne() or // TODO: Combine with `TAdd`
4243
TSubOne() or // TODO: Combine with `TSub`
43-
TPostInc() or // TODO: Reconnect result to merge with `TPreInc`
44-
TPostDec() or // TODO: Reconnect result to merge with `TPreDec`
4544
TConditional() or // TODO: Represent as flow
4645
TCall() or
4746
TUnknown()
@@ -51,6 +50,10 @@ class Opcode extends TOpcode {
5150
}
5251

5352
module Opcode {
53+
class InitializeParameter extends Opcode, TInitializeParameter {
54+
override string toString() { result = "InitializeParameter" }
55+
}
56+
5457
class CopyValue extends Opcode, TCopyValue {
5558
override string toString() { result = "CopyValue" }
5659
}
@@ -155,14 +158,6 @@ module Opcode {
155158
override string toString() { result = "SubOne" }
156159
}
157160

158-
class PostInc extends Opcode, TPostInc {
159-
override string toString() { result = "PostInc" }
160-
}
161-
162-
class PostDec extends Opcode, TPostDec {
163-
override string toString() { result = "PostDec" }
164-
}
165-
166161
class Conditional extends Opcode, TConditional {
167162
override string toString() { result = "Conditional" }
168163
}
@@ -378,10 +373,26 @@ class SemSubOneExpr extends SemUnaryExpr {
378373
SemSubOneExpr() { opcode instanceof Opcode::SubOne }
379374
}
380375

381-
class SemLoadExpr extends SemKnownExpr {
382-
SemLoadExpr() { opcode instanceof Opcode::Load and Specific::loadExpr(this, type) }
376+
private class SemNullaryExpr extends SemKnownExpr {
377+
SemNullaryExpr() { Specific::nullaryExpr(this, opcode, type) }
378+
}
379+
380+
class SemInitializeParameterExpr extends SemNullaryExpr {
381+
SemInitializeParameterExpr() { opcode instanceof Opcode::InitializeParameter }
382+
}
383+
384+
class SemLoadExpr extends SemNullaryExpr {
385+
SemLoadExpr() { opcode instanceof Opcode::Load }
386+
387+
final SemSsaVariable getDef() { result.getAUse() = this }
388+
}
389+
390+
class SemSsaLoadExpr extends SemLoadExpr {
391+
SemSsaLoadExpr() { exists(getDef()) }
392+
}
383393

384-
final SemSsaVariable getDef() { result = Specific::getLoadDef(this) }
394+
class SemNonSsaLoadExpr extends SemLoadExpr {
395+
SemNonSsaLoadExpr() { not exists(getDef()) }
385396
}
386397

387398
class SemConditionalExpr extends SemKnownExpr {

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

Lines changed: 60 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,10 @@ private module Impl {
1010
TPrimaryExpr(J::Expr e) or
1111
TPostUpdateExpr(J::UnaryAssignExpr e) {
1212
e instanceof J::PostIncExpr or e instanceof J::PostDecExpr
13+
} or
14+
TEnhancedForInit(J::EnhancedForStmt for) or
15+
TParameterInit(SSA::SsaImplicitInit init, J::Parameter param) {
16+
init.isParameterDefinition(param)
1317
}
1418

1519
TExpr getResultExpr(J::Expr e) { result = TPrimaryExpr(e) }
@@ -22,6 +26,8 @@ module SemanticExprConfig {
2226
string toString() { none() }
2327

2428
J::Location getLocation() { none() }
29+
30+
J::BasicBlock getBasicBlock() { none() }
2531
}
2632

2733
private class PrimaryExpr extends Expr, TPrimaryExpr {
@@ -33,6 +39,8 @@ module SemanticExprConfig {
3339

3440
override J::Location getLocation() { result = e.getLocation() }
3541

42+
override J::BasicBlock getBasicBlock() { result = e.getBasicBlock() }
43+
3644
J::Expr getExpr() { result = e }
3745
}
3846

@@ -45,14 +53,43 @@ module SemanticExprConfig {
4553

4654
override J::Location getLocation() { result = e.getLocation() }
4755

56+
override J::BasicBlock getBasicBlock() { result = e.getBasicBlock() }
57+
4858
J::UnaryAssignExpr getExpr() { result = e }
4959
}
5060

61+
private class EnhancedForInitExpr extends Expr, TEnhancedForInit {
62+
J::EnhancedForStmt for;
63+
64+
EnhancedForInitExpr() { this = TEnhancedForInit(for) }
65+
66+
override string toString() { result = "init of " + for.getVariable().toString() }
67+
68+
override J::Location getLocation() { result = for.getVariable().getLocation() }
69+
70+
override J::BasicBlock getBasicBlock() { result = for.getVariable().getBasicBlock() }
71+
}
72+
73+
private class ParameterInitExpr extends Expr, TParameterInit {
74+
SSA::SsaImplicitInit init;
75+
J::Parameter param;
76+
77+
ParameterInitExpr() { this = TParameterInit(init, param) }
78+
79+
override string toString() { result = "param init: " + init.toString() }
80+
81+
override J::Location getLocation() { result = init.getLocation() }
82+
83+
override J::BasicBlock getBasicBlock() { result = init.getBasicBlock() }
84+
85+
final J::Parameter getParameter() { result = param }
86+
}
87+
5188
string exprToString(Expr e) { result = e.toString() }
5289

5390
J::Location getExprLocation(Expr e) { result = e.getLocation() }
5491

55-
SemBasicBlock getExprBasicBlock(Expr e) { result.getAnExpr() = e }
92+
SemBasicBlock getExprBasicBlock(Expr e) { result = getSemanticBasicBlock(e.getBasicBlock()) }
5693

5794
predicate integerLiteral(Expr expr, SemIntegerType type, int value) {
5895
exists(J::Expr javaExpr | javaExpr = expr.(PrimaryExpr).getExpr() |
@@ -172,7 +209,8 @@ module SemanticExprConfig {
172209
assignOp instanceof J::AssignRShiftExpr and opcode instanceof Opcode::ShiftRight
173210
or
174211
// TODO: Add new opcode or add an implicit conversion
175-
assignOp instanceof J::AssignURShiftExpr and opcode instanceof Opcode::ShiftRightUnsigned
212+
assignOp instanceof J::AssignURShiftExpr and
213+
opcode instanceof Opcode::ShiftRightUnsigned
176214
)
177215
)
178216
)
@@ -237,13 +275,15 @@ module SemanticExprConfig {
237275
)
238276
}
239277

240-
predicate loadExpr(Expr expr, SemType type) {
241-
type = getSemanticType(expr.(PrimaryExpr).getExpr().(J::RValue).getType())
242-
}
243-
244-
SemSsaVariable getLoadDef(Expr expr) {
245-
exists(SSA::SsaVariable var | expr.(PrimaryExpr).getExpr() = var.getAUse() |
246-
result = getSemanticSsaVariable(var)
278+
predicate nullaryExpr(Expr expr, Opcode opcode, SemType type) {
279+
exists(ParameterInitExpr paramInit | paramInit = expr |
280+
opcode instanceof Opcode::InitializeParameter and
281+
type = getSemanticType(paramInit.getParameter().getType())
282+
)
283+
or
284+
exists(J::RValue rval | rval = expr.(PrimaryExpr).getExpr() |
285+
type = getSemanticType(rval.getType()) and
286+
opcode instanceof Opcode::Load
247287
)
248288
}
249289

@@ -260,6 +300,10 @@ module SemanticExprConfig {
260300

261301
SemType getUnknownExprType(Expr expr) {
262302
result = getSemanticType(expr.(PrimaryExpr).getExpr().getType())
303+
or
304+
exists(J::EnhancedForStmt for | expr = TEnhancedForInit(for) |
305+
result = getSemanticType(for.getVariable().getType())
306+
)
263307
}
264308
}
265309

@@ -282,5 +326,12 @@ SemExpr getUpdateExpr(SSA::SsaExplicitUpdate update) {
282326
or
283327
(expr instanceof J::PostIncExpr or expr instanceof J::PostDecExpr) and
284328
result = TPostUpdateExpr(expr)
329+
or
330+
exists(J::EnhancedForStmt for |
331+
for.getVariable() = expr and
332+
result = TEnhancedForInit(for)
333+
)
285334
)
286335
}
336+
337+
SemExpr getEnhancedForInitExpr(J::EnhancedForStmt for) { result = TEnhancedForInit(for) }

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

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,9 @@ class SemSsaVariable extends TSemSsaVariable {
1919

2020
final string toString() { result = var.toString() }
2121

22-
final SemExpr getAUse() { result = getSemanticExpr(var.getAUse()) }
22+
final Location getLocation() { result = var.getLocation() }
23+
24+
final SemLoadExpr getAUse() { result = getSemanticExpr(var.getAUse()) }
2325

2426
final SemSsaSourceVariable getSourceVariable() {
2527
result = getSemanticSsaSourceVariable(var.getSourceVariable())

java/ql/lib/semmle/code/java/semantic/analysis/Sign.qll

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -246,6 +246,8 @@ class Sign extends TSign {
246246

247247
/** Perform `op` on this sign. */
248248
Sign applyUnaryOp(Opcode op) {
249+
op instanceof Opcode::CopyValue and result = this
250+
or
249251
op instanceof Opcode::AddOne and result = inc()
250252
or
251253
op instanceof Opcode::SubOne and result = dec()

0 commit comments

Comments
 (0)