Skip to content

Commit 29b3cbe

Browse files
hvitvedCopilot
andcommitted
Java: Adopt pattern-aware CFG/Guards support
Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
1 parent 7b461c1 commit 29b3cbe

22 files changed

Lines changed: 496 additions & 247 deletions

File tree

‎java/ql/lib/semmle/code/java/ControlFlowGraph.qll‎

Lines changed: 90 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import java
88
private import codeql.controlflow.ControlFlowGraph
99
private import codeql.controlflow.SuccessorType
1010
private import controlflow.internal.Preconditions
11+
private import controlflow.internal.SwitchCases
1112

1213
private module Cfg0 = Make0<Location, Ast>;
1314

@@ -41,11 +42,17 @@ private module Ast implements AstSig<Location> {
4142
AstNode getChild(AstNode n, int index) {
4243
not skipControlFlow(result) and
4344
not skipControlFlow(n) and
45+
not isBindingTypeTest(n, _, _, result) and
4446
result.(Expr).isNthChildOf(n, index)
4547
or
4648
result.(Stmt).isNthChildOf(n, index)
4749
or
4850
result = n.(SwitchCase).getRuleExpression() and index = -1
51+
or
52+
isBindingTypeTest(_, n, _, result) and
53+
index = 0
54+
or
55+
isBindingTypeTest(n, result, index, _)
4956
}
5057

5158
Callable getEnclosingCallable(AstNode node) {
@@ -177,10 +184,7 @@ private module Ast implements AstSig<Location> {
177184

178185
class Case extends AstNode instanceof J::SwitchCase {
179186
/** Gets the pattern being matched by this case at the specified (zero-based) `index`. */
180-
AstNode getPattern(int index) {
181-
result = this.(PatternCase).getPattern(index) or
182-
result = this.(ConstCase).getValue(index)
183-
}
187+
AstNode getPattern(int index) { result = getSwitchCasePattern(this, index) }
184188

185189
/** Gets the guard expression of this case, if any. */
186190
Expr getGuard() { result = this.(PatternCase).getGuard() }
@@ -241,12 +245,45 @@ private module Ast implements AstSig<Location> {
241245
boolean getValue() { result = this.getBooleanValue() }
242246
}
243247

248+
final class Pattern = PatternImpl;
249+
250+
abstract private class PatternImpl extends AstNode { }
251+
252+
private class NonBindingPattern extends PatternImpl {
253+
NonBindingPattern() {
254+
isBindingTypeTest(_, this, _, _)
255+
or
256+
this instanceof RecordPatternExpr
257+
}
258+
}
259+
260+
class AndPattern extends Pattern {
261+
AndPattern() { none() }
262+
}
263+
264+
class OrPattern extends Pattern {
265+
OrPattern() { none() }
266+
}
267+
268+
class NotPattern extends Pattern {
269+
NotPattern() { none() }
270+
271+
Pattern getNegatedPattern() { none() }
272+
}
273+
274+
class BindingPattern extends PatternImpl {
275+
BindingPattern() {
276+
isBindingTypeTest(_, _, _, this)
277+
or
278+
// needed for cases like `A(var x)` where there is no type test
279+
this = any(RecordPatternExpr rpe).getSubPattern(_).(LocalVariableDeclExpr)
280+
}
281+
}
282+
244283
final private class FinalInstanceOfExpr = J::InstanceOfExpr;
245284

246285
class PatternMatchExpr extends FinalInstanceOfExpr {
247-
PatternMatchExpr() { this.isPattern() }
248-
249-
AstNode getPattern() { result = super.getPattern() }
286+
AstNode getPattern() { result = getInstanceOfExprPattern(this) }
250287
}
251288
}
252289

@@ -529,7 +566,24 @@ private module Input implements InputSig1, InputSig2 {
529566

530567
/** Holds if this case matches all possible values. */
531568
predicate matchAll(Ast::Case c) {
532-
c.(J::PatternCase).getAPattern().getType() instanceof TypeObject
569+
c.(J::PatternCase).getAPattern().getType() instanceof TypeObject and
570+
// A later null case must remain reachable without matching this pattern or
571+
// evaluating its guard. Combined null/default cases are ordered last.
572+
not exists(Ast::Switch switch, int i, int j |
573+
switch.getCase(i) = c and
574+
(
575+
switch.getCase(j) instanceof J::NullDefaultCase
576+
or
577+
j > i and switch.getCase(j).(J::ConstCase).getValue(_) instanceof NullLiteral
578+
)
579+
)
580+
}
581+
582+
predicate patternIsExhaustive(Ast::Pattern pattern) {
583+
exists(RecordPatternExpr rpe, LocalVariableDeclExpr sub, int i |
584+
rpe.subPatternIsUnrestricted(sub, i) and
585+
isBindingTypeTest(rpe, pattern, i, sub)
586+
)
533587
}
534588

535589
private newtype TLabel =
@@ -619,10 +673,10 @@ private module Input implements InputSig1, InputSig2 {
619673
n1.isBefore(assertstmt) and
620674
n2.isBefore(assertstmt.getExpr())
621675
or
622-
n1.isAfterValue(assertstmt.getExpr(), any(BooleanSuccessor s | s.getValue() = true)) and
676+
n1.isAfterTrue(assertstmt.getExpr()) and
623677
n2.isAfter(assertstmt)
624678
or
625-
n1.isAfterValue(assertstmt.getExpr(), any(BooleanSuccessor s | s.getValue() = false)) and
679+
n1.isAfterFalse(assertstmt.getExpr()) and
626680
(
627681
n2.isBefore(assertstmt.getMessage())
628682
or
@@ -676,13 +730,36 @@ private module Input implements InputSig1, InputSig2 {
676730
}
677731
}
678732

733+
private predicate conditionNode(
734+
ControlFlowNode n, BooleanSuccessor b, SuccessorType underlying, ExprParent condition
735+
) {
736+
exists(n.getASuccessor(underlying)) and
737+
(
738+
b = underlying and
739+
(condition = n.asExpr() or condition = n.asStmt())
740+
or
741+
condition =
742+
any(Ast::PatternMatchExpr pme |
743+
underlying.(MatchingSuccessor).getValue() = b.getValue() and
744+
n.injects(pme.getPattern())
745+
)
746+
or
747+
n.isAfterValue(condition, b) and
748+
underlying instanceof DirectSuccessor
749+
)
750+
}
751+
679752
/** A control-flow node that branches based on a boolean condition. */
680753
class ConditionNode extends ControlFlowNode {
681-
ConditionNode() { exists(this.getASuccessor(any(BooleanSuccessor t))) }
754+
ConditionNode() { conditionNode(this, _, _, _) }
682755

683756
/** Gets a true- or false-successor of the `ConditionNode`. */
684757
ControlFlowNode getABranchSuccessor(boolean branch) {
685-
result = this.getASuccessor(any(BooleanSuccessor t | t.getValue() = branch))
758+
exists(BooleanSuccessor b, SuccessorType underlying |
759+
conditionNode(this, b, underlying, _) and
760+
result = this.getASuccessor(underlying) and
761+
branch = b.getValue()
762+
)
686763
}
687764

688765
/** Gets a true-successor of the `ConditionNode`. */
@@ -692,5 +769,5 @@ class ConditionNode extends ControlFlowNode {
692769
ControlFlowNode getAFalseSuccessor() { result = this.getABranchSuccessor(false) }
693770

694771
/** Gets the condition of this `ConditionNode`. */
695-
ExprParent getCondition() { result = this.asExpr() or result = this.asStmt() }
772+
ExprParent getCondition() { conditionNode(this, _, _, result) }
696773
}

‎java/ql/lib/semmle/code/java/Expr.qll‎

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2761,6 +2761,23 @@ class RecordPatternExpr extends Expr, @recordpatternexpr {
27612761
*/
27622762
PatternExpr getSubPattern(int i) { result.isNthChildOf(this, i) }
27632763

2764+
/**
2765+
* Holds if the `i`th sub pattern `subPattern` of this record pattern matches
2766+
* any value of its type.
2767+
*
2768+
* This is used to determine if this record pattern matches any record of its type.
2769+
*/
2770+
predicate subPatternIsUnrestricted(PatternExpr subPattern, int idx) {
2771+
subPattern = this.getSubPattern(idx) and
2772+
subPattern.getType() =
2773+
this.getType().(Record).getCanonicalConstructor().getParameter(idx).getType() and
2774+
(
2775+
subPattern instanceof LocalVariableDeclExpr
2776+
or
2777+
subPattern.(RecordPatternExpr).isUnrestricted()
2778+
)
2779+
}
2780+
27642781
/**
27652782
* Holds if this record pattern matches any record of its type.
27662783
*
@@ -2769,13 +2786,7 @@ class RecordPatternExpr extends Expr, @recordpatternexpr {
27692786
*/
27702787
predicate isUnrestricted() {
27712788
forall(PatternExpr subPattern, int idx | subPattern = this.getSubPattern(idx) |
2772-
subPattern.getType() =
2773-
this.getType().(Record).getCanonicalConstructor().getParameter(idx).getType() and
2774-
(
2775-
subPattern instanceof LocalVariableDeclExpr
2776-
or
2777-
subPattern.(RecordPatternExpr).isUnrestricted()
2778-
)
2789+
this.subPatternIsUnrestricted(subPattern, idx)
27792790
)
27802791
}
27812792

‎java/ql/lib/semmle/code/java/controlflow/Guards.qll‎

Lines changed: 67 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -12,21 +12,24 @@ private import semmle.code.java.controlflow.internal.SwitchCases
1212
private import codeql.controlflow.Guards as SharedGuards
1313
private import codeql.controlflow.SuccessorType
1414

15-
/**
16-
* A basic block that terminates in a condition, splitting the subsequent control flow.
17-
*/
15+
/** A basic block that contains a condition. */
1816
class ConditionBlock extends BasicBlock {
19-
ConditionBlock() { this.getLastNode() instanceof ConditionNode }
17+
private ConditionNode cn;
18+
19+
ConditionBlock() {
20+
if cn.injects(cn.getCondition()) then cn = this.getLastNode() else cn = this.getANode()
21+
}
2022

21-
/** Gets the last node of this basic block. */
22-
ConditionNode getConditionNode() { result = this.getLastNode() }
23+
/** Gets a condition node in this basic block. */
24+
ConditionNode getConditionNode() { result = cn }
2325

24-
/** Gets the condition of the last node of this basic block. */
26+
/** Gets a condition in this basic block. */
2527
ExprParent getCondition() { result = this.getConditionNode().getCondition() }
2628

27-
/** Gets a `true`- or `false`-successor of the last node of this basic block. */
28-
BasicBlock getTestSuccessor(boolean testIsTrue) {
29-
result.getFirstNode() = this.getConditionNode().getABranchSuccessor(testIsTrue)
29+
/** Gets a `true`- or `false`-successor for `cond` inside this block. */
30+
BasicBlock getTestSuccessor(ExprParent cond, boolean testIsTrue) {
31+
cond = cn.getCondition() and
32+
result.getANode() = cn.getABranchSuccessor(testIsTrue)
3033
}
3134

3235
/*
@@ -64,13 +67,14 @@ class ConditionBlock extends BasicBlock {
6467
*/
6568

6669
/**
67-
* Holds if `controlled` is a basic block controlled by this condition, that
70+
* Holds if `controlled` is a basic block controlled by condition `cond`, that
6871
* is, a basic blocks for which the condition is `testIsTrue`.
6972
*/
70-
predicate controls(BasicBlock controlled, boolean testIsTrue) {
73+
predicate controls(ExprParent cond, BasicBlock controlled, boolean testIsTrue) {
74+
cond = cn.getCondition() and
7175
exists(BasicBlock succ |
72-
succ = this.getTestSuccessor(testIsTrue) and
73-
dominatingEdge(this, succ) and
76+
succ = this.getTestSuccessor(cond, testIsTrue) and
77+
(dominatingEdge(this, succ) or this = succ) and
7478
succ.dominates(controlled)
7579
)
7680
}
@@ -157,15 +161,50 @@ private module GuardsInput implements SharedGuards::InputSig<Location, ControlFl
157161
decl = v.getDefiningExpr() and
158162
this = v.getARead()
159163
)
164+
or
165+
(
166+
this instanceof TypeAccess or
167+
this instanceof ArrayTypeAccess or
168+
this instanceof RecordPatternExpr
169+
) and
170+
(
171+
this = any(Case c).getAPattern()
172+
or
173+
this = any(PatternMatchExpr pm).getPattern()
174+
)
160175
}
161176
}
162177

163178
class Case extends ExprParent instanceof J::SwitchCase {
164179
Expr getSwitchExpr() { result = super.getSelectorExpr() }
165180

166-
AstNode getAPattern() {
167-
result = this.(ConstCase).getValue(_) or result = this.(J::PatternCase).getAPattern()
168-
}
181+
AstNode getAPattern() { result = getSwitchCasePattern(this, _) }
182+
}
183+
184+
class Pattern extends AstNode {
185+
Pattern() { none() }
186+
187+
Pattern getASubPattern() { none() }
188+
}
189+
190+
class AndPattern extends Pattern {
191+
AndPattern() { none() }
192+
}
193+
194+
class OrPattern extends Pattern {
195+
OrPattern() { none() }
196+
}
197+
198+
class NotPattern extends Pattern {
199+
NotPattern() { none() }
200+
201+
Pattern getNegatedPattern() { none() }
202+
}
203+
204+
final private class FinalInstanceOfExpr = J::InstanceOfExpr;
205+
206+
class PatternMatchExpr extends FinalInstanceOfExpr {
207+
AstNode getPattern() { result = getInstanceOfExprPattern(this) }
169208
}
170209

171210
class AndExpr extends BinaryExpr {
@@ -280,8 +319,6 @@ private module LogicInputCommon {
280319
predicate additionalNullCheck(
281320
GuardsImpl::PreGuard guard, GuardValue val, GuardsInput::Expr e, boolean isNull
282321
) {
283-
guard.(InstanceOfExpr).getExpr() = e and val.asBooleanValue() = true and isNull = false
284-
or
285322
exists(MethodCall call |
286323
call = guard and
287324
call.getAnArgument() = e and
@@ -369,14 +406,6 @@ final class Guard extends Guards_v3::Guard {
369406
result = this.(SwitchCase).getEnclosingCallable()
370407
}
371408

372-
/**
373-
* DEPRECATED: Use `appliesTypeTest(Expr testedExpr, Type testedType, boolean restricted, GuardValue val)` instead.
374-
*/
375-
deprecated predicate appliesTypeTest(Expr testedExpr, Type testedType, boolean restricted) {
376-
this.appliesTypeTest(testedExpr, testedType, restricted,
377-
any(GuardValue gv | gv.asBooleanValue() = true))
378-
}
379-
380409
/**
381410
* Holds if this guard tests whether `testedExpr` has type `testedType` upon evaluating to `val`.
382411
*
@@ -388,29 +417,21 @@ final class Guard extends Guards_v3::Guard {
388417
*/
389418
predicate appliesTypeTest(Expr testedExpr, Type testedType, boolean restricted, GuardValue val) {
390419
(
391-
exists(InstanceOfExpr ioe | this = ioe |
392-
testedExpr = ioe.getExpr() and
393-
testedType = ioe.getSyntacticCheckedType() and
394-
val.asBooleanValue() = true
420+
exists(InstanceOfExpr ioe |
421+
this = getInstanceOfExprPattern(ioe) and
422+
testedExpr = ioe.getExpr()
395423
)
396424
or
397-
exists(PatternCase pc, PatternExpr pat | this = pat |
398-
pc.getSelectorExpr() = testedExpr and
399-
pc.getAPattern() = pat and
400-
testedType = pat.getType() and
401-
val.isMatch()
425+
exists(PatternCase pc |
426+
this = getSwitchCasePattern(pc, _) and
427+
pc.getSelectorExpr() = testedExpr
402428
)
403429
) and
404-
(
405-
if
406-
exists(RecordPatternExpr rpe |
407-
rpe = [this.(InstanceOfExpr).getPattern(), this.(PatternExpr)]
408-
|
409-
not rpe.isUnrestricted()
410-
)
411-
then restricted = true
412-
else restricted = false
413-
)
430+
testedType = this.(Expr).getType() and
431+
val.isMatch() and
432+
if this = any(RecordPatternExpr rpe | not rpe.isUnrestricted())
433+
then restricted = true
434+
else restricted = false
414435
}
415436
}
416437

0 commit comments

Comments
 (0)