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
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
---
category: deprecated
---
* The classes `ConditionNode` and `ConditionBlock` have been deprecated. Use `Guard` from `semmle.code.java.controlflow.Guards` instead, for example `Guard.controls`, `Guard.directlyControls`, or `Guard.hasBranchEdge`.
8 changes: 6 additions & 2 deletions java/ql/lib/semmle/code/java/ControlFlowGraph.qll
Original file line number Diff line number Diff line change
Expand Up @@ -676,8 +676,12 @@ private module Input implements InputSig1, InputSig2 {
}
}

/** A control-flow node that branches based on a boolean condition. */
class ConditionNode extends ControlFlowNode {
/**
* DEPRECATED: Use `Guard` from `semmle.code.java.controlflow.Guards` instead.
*
* A control-flow node that branches based on a boolean condition.
*/
deprecated class ConditionNode extends ControlFlowNode {
ConditionNode() { exists(this.getASuccessor(any(BooleanSuccessor t))) }

/** Gets a true- or false-successor of the `ConditionNode`. */
Expand Down
4 changes: 3 additions & 1 deletion java/ql/lib/semmle/code/java/controlflow/Guards.qll
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,11 @@ private import codeql.controlflow.Guards as SharedGuards
private import codeql.controlflow.SuccessorType

/**
* DEPRECATED: Use `Guard` instead.
*
* A basic block that terminates in a condition, splitting the subsequent control flow.
*/
class ConditionBlock extends BasicBlock {
deprecated class ConditionBlock extends BasicBlock {
ConditionBlock() { this.getLastNode() instanceof ConditionNode }

/** Gets the last node of this basic block. */
Expand Down
13 changes: 6 additions & 7 deletions java/ql/lib/semmle/code/java/controlflow/UnreachableBlocks.qll
Original file line number Diff line number Diff line change
Expand Up @@ -199,14 +199,13 @@ class ConstSwitchStmt extends SwitchStmt {
*/
class UnreachableBasicBlock extends BasicBlock {
UnreachableBasicBlock() {
// Condition blocks with a constant condition that causes a true/false successor to be
// unreachable. Note: conditions including a single boolean literal e.g. if (false) are not
// modeled as a ConditionBlock - this case is covered by the blocks-without-a-predecessor
// check below.
exists(ConditionBlock conditionBlock, boolean constant |
constant = conditionBlock.getCondition().(ConstantExpr).getBooleanValue()
// Guards with a constant condition that causes a true/false successor to be
// unreachable. Note: conditions including a single boolean literal e.g. if (false) do not
// branch - this case is covered by the blocks-without-a-predecessor check below.
exists(Guard guard, boolean constant |
constant = guard.(ConstantExpr).getBooleanValue()
|
conditionBlock.controls(this, constant.booleanNot())
guard.directlyControls(this, constant.booleanNot())
)
or
// This block is not reachable in the CFG, and is not the entrypoint in a callable, an
Expand Down
10 changes: 5 additions & 5 deletions java/ql/lib/semmle/code/java/dataflow/Nullness.qll
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ private predicate unboxed(Expr e) {
or
exists(ChooseExpr cond | cond.getType() instanceof PrimitiveType | cond.getAResultExpr() = e)
or
exists(ConditionNode cond | cond.getCondition() = e)
exists(Guard cond | cond = e and cond.hasBranchEdge(_, _, _))
or
exists(Parameter p | p.getType() instanceof PrimitiveType and p.getAnArgument() = e)
or
Expand Down Expand Up @@ -188,9 +188,9 @@ private Expr nonEmptyExpr() {
v.(SsaExplicitWrite).getValue() = nonEmptyExpr()
or
// ...or it is guarded by a condition proving its length to be non-zero.
exists(ConditionBlock cond, boolean branch, FieldAccess length |
exists(Guard cond, boolean branch, FieldAccess length |
cond.controls(result.getBasicBlock(), branch) and
cond.getCondition() = nonZeroGuard(length, branch) and
cond = nonZeroGuard(length, branch) and
length.getField().hasName("length") and
length.getQualifier() = v.getARead()
)
Expand All @@ -200,7 +200,7 @@ private Expr nonEmptyExpr() {
// A use of a Collection variable is non-empty if...
result = v.getARead() and
v.getSourceVariable().getType() instanceof CollectionType and
exists(ConditionBlock cond, boolean branch, Expr c |
exists(Guard cond, boolean branch, Expr c |
// ...it is guarded by a condition...
cond.controls(result.getBasicBlock(), branch) and
// ...and it isn't modified in the scope of the condition...
Expand All @@ -211,7 +211,7 @@ private Expr nonEmptyExpr() {
|
m instanceof CollectionQueryMethod
) and
cond.getCondition() = c
cond = c
|
// ...and the condition proves that it is non-empty, either by using the `isEmpty` method...
c.(MethodCall).getMethod().hasName("isEmpty") and
Expand Down
2 changes: 1 addition & 1 deletion java/ql/lib/semmle/code/java/dataflow/TypeFlow.qll
Original file line number Diff line number Diff line change
Expand Up @@ -362,7 +362,7 @@ private module Input implements TypeFlowInput<Location> {
/** Holds if `ioe` checks `v` and its true-successor is `bb`. */
private predicate instanceofTrueSuccessor(InstanceOfExpr ioe, BasicBlock bb, Base::SsaDefinition v) {
ioe.getExpr() = v.getARead() and
exists(ConditionBlock cb | cb.getCondition() = ioe and cb.getTestSuccessor(true) = bb)
ioe.(Guard).hasBranchEdge(_, bb, true)
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,14 @@ import semmle.code.java.controlflow.Guards
* Holds if `ma` is controlled by the condition expression `e`.
*/
predicate conditionControlsMethod(MethodCall ma, Expr e) {
exists(ConditionBlock cb, SensitiveExecutionMethod m, boolean cond |
exists(Guard g, SensitiveExecutionMethod m, boolean cond |
ma.getMethod() = m and
cb.controls(ma.getBasicBlock(), cond) and
not cb.controls(any(SensitiveExecutionMethod sem).getAReference().getBasicBlock(),
g.controls(ma.getBasicBlock(), cond) and
not g.controls(any(SensitiveExecutionMethod sem).getAReference().getBasicBlock(),
cond.booleanNot()) and
not cb.controls(any(ThrowStmt t).getBasicBlock(), cond.booleanNot()) and
not cb.controls(any(ReturnStmt r).getBasicBlock(), cond.booleanNot()) and
e = cb.getCondition()
not g.controls(any(ThrowStmt t).getBasicBlock(), cond.booleanNot()) and
not g.controls(any(ReturnStmt r).getBasicBlock(), cond.booleanNot()) and
e = g
)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,10 +43,10 @@ class RightShiftOp extends Expr {
}

private predicate boundedRead(VarRead read) {
exists(SsaDefinition v, ConditionBlock cb, ComparisonExpr comp, boolean testIsTrue |
exists(SsaDefinition v, Guard g, ComparisonExpr comp, boolean testIsTrue |
read = v.getARead() and
cb.controls(read.getBasicBlock(), testIsTrue) and
cb.getCondition() = comp
g.controls(read.getBasicBlock(), testIsTrue) and
g = comp
|
comp.getLesserOperand() = v.getARead() and testIsTrue = true
or
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,7 @@ private import semmle.code.java.controlflow.Guards
* Holds if the given `ComparisonExpr` is thought to be true when `VarAccess` is accessed.
*/
private predicate conditionHolds(ComparisonExpr ce, VarAccess va) {
exists(ConditionBlock cond |
cond.getCondition() = ce and
cond.controls(va.getBasicBlock(), true)
)
ce.(Guard).controls(va.getBasicBlock(), true)
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,19 +25,18 @@ class BoundKind extends string {
/**
* The information from `s1` implies that `test` always has the value `testIsTrue`.
*/
predicate uselessTest(ConditionNode s1, BinaryExpr test, boolean testIsTrue) {
predicate uselessTest(Guard s1, BinaryExpr test, boolean testIsTrue) {
exists(
ConditionBlock cb, SsaDefinition v, BinaryExpr cond, boolean condIsTrue, int k1, int k2,
SsaDefinition v, BinaryExpr cond, boolean condIsTrue, int k1, int k2,
CompileTimeConstantExpr c1, CompileTimeConstantExpr c2
|
s1.getCondition() = cond and
cb.getCondition() = cond and
s1 = cond and
cond.hasOperands(v.getARead(), c1) and
c1.getIntValue() = k1 and
test.hasOperands(v.getARead(), c2) and
c2.getIntValue() = k2 and
v.getSourceVariable().getVariable() instanceof LocalScopeVariable and
cb.controls(test.getBasicBlock(), condIsTrue) and
s1.controls(test.getBasicBlock(), condIsTrue) and
v.getSourceVariable().getType() instanceof IntegralType and
exists(BoundKind boundKind, int bound |
// Simple range analysis. We infer a bound based on `cond` being
Expand Down
18 changes: 5 additions & 13 deletions java/ql/src/Likely Bugs/Concurrency/UnreleasedLock.ql
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ predicate failedLock(LockType t, BasicBlock lockblock, BasicBlock exblock) {
) and
(
lock.getAnExceptionSuccessor() = exblock.getFirstNode() or
lock.(ConditionNode).getAFalseSuccessor() = exblock.getFirstNode()
lock.asExpr().(Guard).hasBranchEdge(lockblock, exblock, false)
)
)
}
Expand All @@ -70,12 +70,7 @@ predicate failedLock(LockType t, BasicBlock lockblock, BasicBlock exblock) {
* successor.
*/
predicate heldByCurrentThreadCheck(LockType t, BasicBlock checkblock, BasicBlock falsesucc) {
exists(ConditionBlock conditionBlock |
conditionBlock.getCondition() = t.getIsHeldByCurrentThreadAccess()
|
conditionBlock = checkblock and
conditionBlock.getTestSuccessor(false) = falsesucc
)
t.getIsHeldByCurrentThreadAccess().(Guard).hasBranchEdge(checkblock, falsesucc, false)
}

/**
Expand All @@ -85,16 +80,13 @@ predicate heldByCurrentThreadCheck(LockType t, BasicBlock checkblock, BasicBlock
* the true successor of `checkblock` must contain an unlock access.
*/
predicate variableLockStateCheck(LockType t, BasicBlock checkblock, BasicBlock falsesucc) {
exists(ConditionBlock conditionBlock, VarAccess v |
exists(VarAccess v |
v.getType() instanceof BooleanType and
// Ensure that a lock access is assigned to the variable
v.getVariable().getAnAssignedValue() = t.getLockAccess() and
// Ensure that the `true` successor of the condition block contains an unlock access
conditionBlock.getTestSuccessor(true) = t.getUnlockAccess().getBasicBlock() and
conditionBlock.getCondition() = v
|
conditionBlock = checkblock and
conditionBlock.getTestSuccessor(false) = falsesucc
v.(Guard).hasBranchEdge(checkblock, t.getUnlockAccess().getBasicBlock(), true) and
v.(Guard).hasBranchEdge(checkblock, falsesucc, false)
)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,10 +43,10 @@ predicate loopExit(LoopStmt loop, Stmt exit) {
* `break` statements that can exit the loop.
*/
predicate loopExitGuard(LoopStmt loop, Expr cond) {
exists(ConditionBlock cb, boolean branch |
cond = cb.getCondition() and
exists(Guard g, boolean branch |
cond = g and
cond.getEnclosingStmt().getEnclosingStmt*() = loop.getBody() and
forex(Stmt exit | loopExit(loop, exit) | cb.controls(exit.getBasicBlock(), branch))
forex(Stmt exit | loopExit(loop, exit) | g.controls(exit.getBasicBlock(), branch))
)
}

Expand Down
6 changes: 5 additions & 1 deletion java/ql/src/Security/CWE/CWE-367/TOCTOURace.ql
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,11 @@ where
not call1 = call2 and
// The use is controlled by one of the branches of the condition, i.e. whether it
// is reached actually depends on that condition.
call1.getBasicBlock().(ConditionBlock).controls(call2.getBasicBlock(), _) and
exists(Guard g |
// The branch must be reached through call1, even in a short-circuit expression.
g.hasBranchEdge(call1.getBasicBlock(), _, _) and
g.directlyControls(call2.getBasicBlock(), _)
) and
// Two calls to synchronized methods on the same variable.
synchCallOn(call1, r) and
synchCallOn(call2, r) and
Expand Down
6 changes: 3 additions & 3 deletions java/ql/src/Security/CWE/CWE-421/SocketAuthRace.ql
Original file line number Diff line number Diff line change
Expand Up @@ -50,9 +50,9 @@ class ServerSocketChannelAcceptMethod extends ConnectionMethod {
}

predicate controlledByAuth(Expr controlled, Expr condition) {
exists(ConditionBlock b |
condition = b.getCondition() and
b.controls(controlled.getBasicBlock(), _) and
exists(Guard g |
condition = g and
g.controls(controlled.getBasicBlock(), _) and
condition.(MethodCall).getMethod() instanceof AuthMethod
)
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
---
category: minorAnalysis
---
* The `java/unreachable-exit-in-loop` query now recognizes guards whose values flow through boolean variables. This may result in additional alerts.
Original file line number Diff line number Diff line change
Expand Up @@ -34,15 +34,12 @@ deprecated module AndroidFileLeakConfig implements DataFlow::ConfigSig {
*/
predicate isSource(DataFlow::Node src) {
exists(
OnActivityForResultMethod oafr, ConditionBlock cb, CompileTimeConstantExpr cc,
VarAccess intentVar
OnActivityForResultMethod oafr, Guard g, CompileTimeConstantExpr cc, VarAccess intentVar
|
cb.getCondition()
.(ValueOrReferenceEqualsExpr)
.hasOperands(oafr.getParameter(0).getAnAccess(), cc) and
g.(ValueOrReferenceEqualsExpr).hasOperands(oafr.getParameter(0).getAnAccess(), cc) and
cc.getIntValue() = any(AndroidFileIntentInput fi).getRequestCode() and
intentVar.getType() instanceof TypeIntent and
cb.controls(intentVar.getBasicBlock(), true) and
g.controls(intentVar.getBasicBlock(), true) and
src.asExpr() = intentVar
)
}
Expand Down
8 changes: 4 additions & 4 deletions java/ql/test/library-tests/guards/guards.ql
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ import java
import semmle.code.java.controlflow.Guards
import utils.test.BasicBlock

from ConditionBlock cb, boolean testIsTrue, BasicBlock controlled
from Guard g, boolean testIsTrue, BasicBlock controlled
where
cb.controls(controlled, testIsTrue) and
cb.getEnclosingCallable().getDeclaringType().hasName("Test")
select cb.getCondition(), testIsTrue, getFirstAstNode(controlled)
g.directlyControls(controlled, testIsTrue) and
g.getEnclosingCallable().getDeclaringType().hasName("Test")
select g, testIsTrue, getFirstAstNode(controlled)
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,4 @@
| Test.java:20:4:20:10 | act(...) | This uses the state of $@ which $@. But these are not jointly synchronized. | Test.java:17:32:17:42 | r | r | Test.java:19:7:19:18 | getState(...) | is checked at a previous call |
| Test.java:27:4:27:10 | act(...) | This uses the state of $@ which $@. But these are not jointly synchronized. | Test.java:24:19:24:28 | r | r | Test.java:26:7:26:18 | getState(...) | is checked at a previous call |
| Test.java:35:5:35:11 | act(...) | This uses the state of $@ which $@. But these are not jointly synchronized. | Test.java:31:19:31:28 | r | r | Test.java:34:8:34:19 | getState(...) | is checked at a previous call |
| Test.java:137:4:137:10 | act(...) | This uses the state of $@ which $@. But these are not jointly synchronized. | Test.java:135:43:135:52 | r | r | Test.java:136:17:136:28 | getState(...) | is checked at a previous call |
Original file line number Diff line number Diff line change
Expand Up @@ -125,4 +125,16 @@ public void act() {

public void sideEffect() { }
}

public synchronized void shortCircuitOr(Resource r) {
if (aField || r.getState()) {
r.act();
}
}

public synchronized void shortCircuitAnd(Resource r) {
if (aField && r.getState()) {
r.act(); // $ Alert
}
}
}
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
| InfiniteLoop.java:4:13:4:36 | for (...;...;...) | Loop might not terminate, as termination depends in part on $@ being false but it is always true. | InfiniteLoop.java:4:27:4:30 | ... < ... | this test |
| InfiniteLoop.java:17:13:17:25 | while (...) | Loop might not terminate, as termination depends in part on $@ being false but it is always true. | InfiniteLoop.java:17:20:17:24 | ... > ... | this test |
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,18 @@ public void bad() {
private boolean shouldBreak() {
return Math.random() < 0.5;
}

void indirectGuard(int x) {
boolean positive = x > 0;
if (positive) {
while (x > 0) { // $ Alert
if (shouldBreak()) break;
}
}
if (!positive) {
while (x > 0) {
if (shouldBreak()) break;
}
}
}
}