Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 14 additions & 19 deletions cpp/ql/lib/semmle/code/cpp/controlflow/IRGuards.qll
Original file line number Diff line number Diff line change
Expand Up @@ -160,23 +160,6 @@ module GuardsInput implements SharedGuards::InputSig<Cpp::Location, Instruction,
*/
SwitchEdge getEdge() { result = edge }

/**
* Holds if this case takes control-flow from `bb1` to `bb2` when
* the case matches the scrutinee.
*/
predicate matchEdge(BasicBlock bb1, BasicBlock bb2) {
switch.getBlock() = bb1 and
this.getBasicBlock() = bb2
}

/**
* Holds if case takes control-flow from `bb1` to `bb2` when the
* case does not match the scrutinee.
*
* This predicate never holds for C/C++.
*/
predicate nonMatchEdge(BasicBlock bb1, BasicBlock bb2) { none() }

/**
* Gets the scrutinee expression.
*/
Expand All @@ -188,16 +171,28 @@ module GuardsInput implements SharedGuards::InputSig<Cpp::Location, Instruction,
predicate isDefaultCase() { edge.isDefault() }

/**
* Gets the constant expression of this case.
* Gets the expression matched by this case.
*/
ConstantExpr asConstantCase() {
AstNode getAPattern() {
// Note: This only has a value if there is a unique value for the case.
// So the will not be a result when using the GCC case range extension.
// Instead, we model these using the `LogicInput_v1::rangeGuard` predicate.
result = this and exists(this.getEdge().getValue())
}
}

/**
* Holds if the case `c` takes control-flow from `bb1` to `bb2` when
* the case matches the scrutinee.
*/
predicate caseMatchEdge(Case c, BasicBlock bb1, BasicBlock bb2) {
exists(SwitchInstruction switch |
switch.getSuccessor(any(SwitchEdge e)) = c and
switch.getBlock() = bb1 and
c.getBasicBlock() = bb2
)
}

abstract private class BinExpr extends Expr instanceof BinaryInstruction {
Expr getAnOperand() { result = super.getAnInput() }
}
Expand Down
12 changes: 1 addition & 11 deletions csharp/ql/lib/semmle/code/csharp/controlflow/Guards.qll
Original file line number Diff line number Diff line change
Expand Up @@ -81,17 +81,7 @@ private module GuardsInput implements SharedGuards::InputSig<Location, ControlFl

predicate isDefaultCase() { this instanceof DefaultCase }

ConstantExpr asConstantCase() { super.getPattern() = result }

predicate matchEdge(BasicBlock bb1, BasicBlock bb2) {
bb1.getASuccessor(any(MatchingSuccessor s | s.getValue() = true)) = bb2 and
bb1.getLastNode() = super.getPattern().getControlFlowNode()
}

predicate nonMatchEdge(BasicBlock bb1, BasicBlock bb2) {
bb1.getASuccessor(any(MatchingSuccessor s | s.getValue() = false)) = bb2 and
bb1.getLastNode() = super.getPattern().getControlFlowNode()
}
AstNode getAPattern() { super.getPattern() = result }
}

abstract private class BinExpr extends BinaryOperation { }
Expand Down
71 changes: 3 additions & 68 deletions go/ql/lib/semmle/go/controlflow/Guards.qll
Original file line number Diff line number Diff line change
Expand Up @@ -93,64 +93,13 @@ private module GuardsInput implements
class Case extends AstNode {
G::ExpressionSwitchStmt switch;

Case() {
exists(switch.getExpr()) and
this = switch.getACase()
or
not exists(switch.getExpr()) and
(
this = switch.getANonDefaultCase().getAnExpr()
or
this = switch.getDefault()
)
}
Case() { this = switch.getACase() }

Expr getSwitchExpr() {
result = switch.getExpr()
or
not exists(switch.getExpr()) and result = this
}
Expr getSwitchExpr() { result = switch.getExpr() }

predicate isDefaultCase() { this = switch.getDefault() }

ConstantExpr asConstantCase() {
exists(G::CaseClause cc |
this = cc and
cc.getNumExpr() = 1 and
result = cc.getExpr(0)
)
}

predicate matchEdge(CfgImpl::Cfg::BasicBlock bb1, CfgImpl::Cfg::BasicBlock bb2) {
exists(Expr caseExpr |
caseExpr = this.(G::CaseClause).getAnExpr()
or
caseExpr = this
|
caseExpressionBranch(caseExpr, bb1,
any(MatchingSuccessor successor |
bb1.getASuccessor(successor) = bb2 and successor.getValue() = true
))
)
}

predicate nonMatchEdge(CfgImpl::Cfg::BasicBlock bb1, CfgImpl::Cfg::BasicBlock bb2) {
exists(G::CaseClause cc, int last, Expr caseExpr |
cc = this and
last = max(int i | exists(cc.getExpr(i))) and
caseExpr = cc.getExpr(last)
|
caseExpressionBranch(caseExpr, bb1,
any(MatchingSuccessor successor |
bb1.getASuccessor(successor) = bb2 and successor.getValue() = false
))
)
or
caseExpressionBranch(this, bb1,
any(MatchingSuccessor successor |
bb1.getASuccessor(successor) = bb2 and successor.getValue() = false
))
}
AstNode getAPattern() { result = this.(G::CaseClause).getAnExpr() }
}

additional predicate caseExpressionBranch(
Expand All @@ -164,20 +113,6 @@ private module GuardsInput implements
)
}

predicate equalityBranchEdge(
Expr left, Expr right, CfgImpl::Cfg::BasicBlock bb1, CfgImpl::Cfg::BasicBlock bb2, boolean equal
) {
exists(G::CaseClause cc, G::ExpressionSwitchStmt switch |
cc = switch.getACase() and
left = switch.getExpr() and
right = cc.getAnExpr() and
caseExpressionBranch(right, bb1,
any(MatchingSuccessor successor |
bb1.getASuccessor(successor) = bb2 and successor.getValue() = equal
))
)
}

class AndExpr = G::LandExpr;

class OrExpr = G::LorExpr;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,8 @@ controlsResult
| guards.go:12:7:12:15 | ...<... | zero | false |
| guards.go:14:7:14:16 | ...==... | positive | false |
| guards.go:14:7:14:16 | ...==... | zero | true |
| guards.go:23:2:24:21 | case clause | tagged one or two | false |
| guards.go:23:2:24:21 | case clause | tagged other | false |
| guards.go:23:2:24:21 | case clause | tagged zero | true |
| guards.go:25:2:26:27 | case clause | tagged other | false |
| guards.go:34:2:35:30 | case clause | tagged false boolean | true |
| guards.go:34:7:34:11 | value | tagged false boolean | false |
| guards.go:38:2:39:29 | case clause | tagged true boolean | true |
| guards.go:38:7:38:11 | value | tagged true boolean | true |
| guards.go:42:2:43:33 | case clause | tagged false comparison | true |
| guards.go:42:7:42:17 | ...<... | tagged false comparison | false |
| guards.go:48:5:48:5 | a | compound true | true |
| guards.go:56:5:56:20 | ...==... | upcast | true |
Expand All @@ -35,10 +28,6 @@ controlsResult
| guards.go:118:5:118:32 | ...==... | errors new | true |
| guards.go:122:5:122:32 | ...==... | fmt errorf | true |
| guards.go:126:5:126:28 | ...==... | source non-nil return | true |
| guards.go:137:2:138:26 | case clause | default case | false |
| guards.go:137:2:138:26 | case clause | non-default case | true |
| guards.go:148:2:149:28 | case clause | case after default | true |
| guards.go:148:2:149:28 | case clause | default before case | false |
| guards.go:162:7:162:11 | value | expressionless case | true |
| guards.go:162:7:162:11 | value | expressionless default | false |
| guards.go:183:5:183:21 | call to isNotNil | non-variadic wrapper | true |
Expand All @@ -59,29 +48,35 @@ valueControlsResult
| guards.go:12:7:12:11 | value | negative | Upper bound -1 |
| guards.go:12:7:12:11 | value | positive | Lower bound 0 |
| guards.go:12:7:12:11 | value | zero | Lower bound 0 |
| guards.go:12:7:12:15 | ...<... | negative | match |
| guards.go:12:7:12:15 | ...<... | negative | true |
| guards.go:12:7:12:15 | ...<... | positive | false |
| guards.go:12:7:12:15 | ...<... | positive | no match |
| guards.go:12:7:12:15 | ...<... | zero | false |
| guards.go:12:7:12:15 | ...<... | zero | no match |
| guards.go:14:7:14:11 | value | positive | not 0 |
| guards.go:14:7:14:11 | value | zero | 0 |
| guards.go:14:7:14:16 | ...==... | positive | false |
| guards.go:14:7:14:16 | ...==... | positive | no match |
| guards.go:14:7:14:16 | ...==... | zero | match |
| guards.go:14:7:14:16 | ...==... | zero | true |
| guards.go:22:9:22:13 | value | tagged one or two | not 0 |
| guards.go:22:9:22:13 | value | tagged other | not 0 |
| guards.go:22:9:22:13 | value | tagged other | not 1 |
| guards.go:22:9:22:13 | value | tagged other | not 2 |
| guards.go:22:9:22:13 | value | tagged zero | 0 |
| guards.go:23:2:24:21 | case clause | tagged one or two | false |
| guards.go:23:2:24:21 | case clause | tagged other | false |
| guards.go:23:2:24:21 | case clause | tagged zero | true |
| guards.go:25:2:26:27 | case clause | tagged other | false |
| guards.go:34:2:35:30 | case clause | tagged false boolean | true |
| guards.go:23:7:23:7 | 0 | tagged one or two | no match |
| guards.go:23:7:23:7 | 0 | tagged other | no match |
| guards.go:23:7:23:7 | 0 | tagged zero | match |
| guards.go:25:7:25:7 | 1 | tagged other | no match |
| guards.go:25:10:25:10 | 2 | tagged other | no match |
| guards.go:34:7:34:11 | value | tagged false boolean | false |
| guards.go:38:2:39:29 | case clause | tagged true boolean | true |
| guards.go:34:7:34:11 | value | tagged false boolean | match |
| guards.go:38:7:38:11 | value | tagged true boolean | match |
| guards.go:38:7:38:11 | value | tagged true boolean | true |
| guards.go:42:2:43:33 | case clause | tagged false comparison | true |
| guards.go:42:7:42:12 | number | tagged false comparison | Lower bound 10 |
| guards.go:42:7:42:17 | ...<... | tagged false comparison | false |
| guards.go:42:7:42:17 | ...<... | tagged false comparison | match |
| guards.go:48:5:48:5 | a | compound true | true |
| guards.go:56:5:56:15 | type conversion | upcast | 0 |
| guards.go:56:5:56:20 | ...==... | upcast | true |
Expand Down Expand Up @@ -134,14 +129,16 @@ valueControlsResult
| guards.go:126:14:126:28 | call to returnsNonNil | source non-nil return | no exception |
| guards.go:136:9:136:13 | value | default case | not 0 |
| guards.go:136:9:136:13 | value | non-default case | 0 |
| guards.go:137:2:138:26 | case clause | default case | false |
| guards.go:137:2:138:26 | case clause | non-default case | true |
| guards.go:137:7:137:7 | 0 | default case | no match |
| guards.go:137:7:137:7 | 0 | non-default case | match |
| guards.go:145:9:145:13 | value | case after default | 0 |
| guards.go:145:9:145:13 | value | default before case | not 0 |
| guards.go:148:2:149:28 | case clause | case after default | true |
| guards.go:148:2:149:28 | case clause | default before case | false |
| guards.go:148:7:148:7 | 0 | case after default | match |
| guards.go:148:7:148:7 | 0 | default before case | no match |
| guards.go:162:7:162:11 | value | expressionless case | match |
| guards.go:162:7:162:11 | value | expressionless case | true |
| guards.go:162:7:162:11 | value | expressionless default | false |
| guards.go:162:7:162:11 | value | expressionless default | no match |
| guards.go:183:5:183:21 | call to isNotNil | non-variadic wrapper | no exception |
| guards.go:183:5:183:21 | call to isNotNil | non-variadic wrapper | true |
| guards.go:183:14:183:20 | pointer | non-variadic wrapper | not null |
Expand Down Expand Up @@ -187,8 +184,6 @@ valueControlsResult
ensuresEqResult
| guards.go:14:7:14:16 | ...==... | 0 = value | true |
| guards.go:14:7:14:16 | ...==... | value = 0 | true |
| guards.go:23:2:24:21 | case clause | 0 = value | true |
| guards.go:23:2:24:21 | case clause | value = 0 | true |
| guards.go:56:5:56:20 | ...==... | 0 = type conversion | true |
| guards.go:56:5:56:20 | ...==... | type conversion = 0 | true |
| guards.go:59:5:59:19 | ...==... | 0 = type conversion | true |
Expand All @@ -211,12 +206,6 @@ ensuresEqResult
| guards.go:122:5:122:32 | ...==... | value = call to Errorf | true |
| guards.go:126:5:126:28 | ...==... | call to returnsNonNil = value | true |
| guards.go:126:5:126:28 | ...==... | value = call to returnsNonNil | true |
| guards.go:137:2:138:26 | case clause | 0 = value | true |
| guards.go:137:2:138:26 | case clause | value = 0 | true |
| guards.go:148:2:149:28 | case clause | 0 = value | true |
| guards.go:148:2:149:28 | case clause | value = 0 | true |
| guards.go:171:2:172:13 | case clause | 0 = value | true |
| guards.go:171:2:172:13 | case clause | value = 0 | true |
| guards.go:179:9:179:22 | ...!=... | nil = pointer | false |
| guards.go:179:9:179:22 | ...!=... | pointer = nil | false |
| guards.go:189:10:189:23 | ...!=... | nil = pointer | false |
Expand Down
17 changes: 1 addition & 16 deletions java/ql/lib/semmle/code/java/controlflow/Guards.qll
Original file line number Diff line number Diff line change
Expand Up @@ -161,28 +161,13 @@ private module GuardsInput implements SharedGuards::InputSig<Location, ControlFl

predicate isDefaultCase() { this instanceof DefaultCase }

ConstantExpr asConstantCase() {
exists(ConstCase cc | this = cc |
cc.getValue() = result and
strictcount(cc.getValue(_)) = 1
)
}
AstNode getAPattern() { result = this.(ConstCase).getValue(_) }

private ControlFlowNode getPatternNode() {
result = this.(J::PatternCase).getUniquePattern().getControlFlowNode()
or
result = unique(Expr e | this.(J::ConstCase).getValue(_) = e).getControlFlowNode()
}

predicate matchEdge(BasicBlock bb1, BasicBlock bb2) {
bb1.getASuccessor(any(MatchingSuccessor s | s.getValue() = true)) = bb2 and
bb1.getLastNode() = this.getPatternNode()
}

predicate nonMatchEdge(BasicBlock bb1, BasicBlock bb2) {
bb1.getASuccessor(any(MatchingSuccessor s | s.getValue() = false)) = bb2 and
bb1.getLastNode() = this.getPatternNode()
}
}

class AndExpr extends BinaryExpr {
Expand Down
16 changes: 8 additions & 8 deletions java/ql/test/library-tests/guards/Guards.java
Original file line number Diff line number Diff line change
Expand Up @@ -36,13 +36,13 @@ void t1(int[] a, String s) {
s = "bar";
switch (s) {
case "bar":
chk(); // $ guarded='s:match "bar"' guarded='s:bar'
chk(); // $ guarded='"bar":match' guarded='s:bar'
break;
case "foo":
chk(); // $ guarded='s:non-match "bar"' guarded='s:not bar' guarded='s:match "foo"' guarded='s:foo' guarded=g(3):false
chk(); // $ guarded='"bar":no match' guarded='s:not bar' guarded='"foo":match' guarded='s:foo' guarded=g(3):false
break;
default:
chk(); // $ guarded='s:non-match "bar"' guarded='s:non-match "foo"' guarded='s:not bar' guarded='s:not foo' guarded=g(3):false
chk(); // $ guarded='"bar":no match' guarded='"foo":no match' guarded='s:not bar' guarded='s:not foo' guarded=g(3):false
break;
}

Expand Down Expand Up @@ -89,13 +89,13 @@ void t4() {
chk(); // $ guarded='x == null:true' guarded='x:null' guarded=g(1):false guarded=g(2):false guarded=g(Alt2):false guarded=g(3):false
switch (x) {
case E1:
chk(); // $ guarded='x:match E1' guarded='x:E1' guarded=g(1):true guarded=g(2):false guarded=g(Alt2):false guarded=g(3):false
chk(); // $ guarded='E1:match' guarded='x:E1' guarded=g(1):true guarded=g(2):false guarded=g(Alt2):false guarded=g(3):false
break;
case E2:
chk(); // $ guarded='x:non-match E1' guarded='x:not E1' guarded='x:match E2' guarded='x:E2' guarded=g(3):false
chk(); // $ guarded='E1:no match' guarded='x:not E1' guarded='E2:match' guarded='x:E2' guarded=g(3):false
break;
case E3:
chk(); // $ guarded='x:non-match E1' guarded='x:non-match E2' guarded='x:not E1' guarded='x:not E2' guarded='x:match E3' guarded='x:E3' guarded=g(3):true
chk(); // $ guarded='E1:no match' guarded='E2:no match' guarded='x:not E1' guarded='x:not E2' guarded='E3:match' guarded='x:E3' guarded=g(3):true
break;
}
Object o = g(4) ? new Object() : null;
Expand Down Expand Up @@ -195,10 +195,10 @@ void testWrappers(String s, Integer i) {

switch (testEnumWrapper(g(1))) {
case SUCCESS:
chk(); // $ guarded='testEnumWrapper(...):SUCCESS' guarded='testEnumWrapper(...):match SUCCESS' guarded=g(1):true
chk(); // $ guarded='testEnumWrapper(...):SUCCESS' guarded='SUCCESS:match' guarded=g(1):true
break;
case FAILURE:
chk(); // $ guarded='testEnumWrapper(...):not SUCCESS' guarded='testEnumWrapper(...):non-match SUCCESS' guarded='testEnumWrapper(...):FAILURE' guarded='testEnumWrapper(...):match FAILURE' guarded=g(1):false
chk(); // $ guarded='testEnumWrapper(...):not SUCCESS' guarded='SUCCESS:no match' guarded='testEnumWrapper(...):FAILURE' guarded='FAILURE:match' guarded=g(1):false
break;
}
}
Expand Down
Loading
Loading