Skip to content
Merged
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
---
* Deprecated `guardEnsuresEq`, `guardEnsuresNeq`, and `guardEnsuresLeq`. Use `guardValueEnsuresEq`, `guardValueEnsuresNeq`, and `guardValueEnsuresLeq`, respectively, with a `GuardValue` and `Guard.valueControls` to also support switch match outcomes.
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
---
category: feature
---
* Added `guardValueEnsuresEq`, `guardValueEnsuresNeq`, and `guardValueEnsuresLeq` to support switch match outcomes as well as Boolean guard values.
12 changes: 6 additions & 6 deletions go/ql/lib/semmle/go/controlflow/ControlFlowGraph.qll
Original file line number Diff line number Diff line change
Expand Up @@ -292,8 +292,8 @@ module ControlFlow {
}

/**
* DEPRECATED: Use `guardEnsuresLeq` from `semmle.go.controlflow.Guards`
* instead.
* DEPRECATED: Use `guardValueEnsuresLeq` from `semmle.go.controlflow.Guards`
* together with `Guard.valueControls` instead.
*
* Holds if this guard ensures that `lesser <= greater + bias` holds.
*/
Expand All @@ -311,8 +311,8 @@ module ControlFlow {
}

/**
* DEPRECATED: Use `guardEnsuresEq` from `semmle.go.controlflow.Guards`
* instead.
* DEPRECATED: Use `guardValueEnsuresEq` from `semmle.go.controlflow.Guards`
* together with `Guard.valueControls` instead.
*
* Holds if this guard ensures that `i = j` holds.
*/
Expand All @@ -324,8 +324,8 @@ module ControlFlow {
}

/**
* DEPRECATED: Use `guardEnsuresNeq` from `semmle.go.controlflow.Guards`
* instead.
* DEPRECATED: Use `guardValueEnsuresNeq` from `semmle.go.controlflow.Guards`
* together with `Guard.valueControls` instead.
*
* Holds if this guard ensures that `i != j` holds.
*/
Expand Down
49 changes: 40 additions & 9 deletions go/ql/lib/semmle/go/controlflow/Guards.qll
Original file line number Diff line number Diff line change
Expand Up @@ -480,30 +480,61 @@ overlay[caller?]
pragma[inline]
predicate guardEnsures(Expr e, boolean b, BasicBlock bb) { e.(Guard).controls(bb, b) }

/** Holds if `guard` evaluating to `branch` ensures that `i = j` holds. */
predicate guardEnsuresEq(Guard guard, boolean branch, DataFlow::Node i, DataFlow::Node j) {
guard.isEquality(i.asExpr(), j.asExpr(), any(GuardValue gv | gv.asBooleanValue() = branch))
/**
* DEPRECATED: Use `guardValueEnsuresEq` with a `GuardValue` and `Guard.valueControls` instead.
*
* Holds if `guard` evaluating to `branch` ensures that `i = j` holds.
*/
deprecated predicate guardEnsuresEq(Guard guard, boolean branch, DataFlow::Node i, DataFlow::Node j) {
guardValueEnsuresEq(guard, any(GuardValue value | value.asBooleanValue() = branch), i, j)
}

/** Holds if `guard` evaluating to `value` ensures that `i = j` holds. */
predicate guardValueEnsuresEq(Guard guard, GuardValue value, DataFlow::Node i, DataFlow::Node j) {
guard.isEquality(i.asExpr(), j.asExpr(), value)
}

/** Holds if `guard` evaluating to `branch` ensures that `i != j` holds. */
predicate guardEnsuresNeq(Guard guard, boolean branch, DataFlow::Node i, DataFlow::Node j) {
/**
* DEPRECATED: Use `guardValueEnsuresNeq` with a `GuardValue` and `Guard.valueControls` instead.
*
* Holds if `guard` evaluating to `branch` ensures that `i != j` holds.
*/
deprecated predicate guardEnsuresNeq(Guard guard, boolean branch, DataFlow::Node i, DataFlow::Node j) {
guardValueEnsuresNeq(guard, any(GuardValue value | value.asBooleanValue() = branch), i, j)
}

/** Holds if `guard` evaluating to `value` ensures that `i != j` holds. */
predicate guardValueEnsuresNeq(Guard guard, GuardValue value, DataFlow::Node i, DataFlow::Node j) {
exists(GuardValue eqval |
guard.isEquality(i.asExpr(), j.asExpr(), eqval) and
branch = eqval.getDualValue().asBooleanValue()
value = eqval.getDualValue()
)
}

/**
* DEPRECATED: Use `guardValueEnsuresLeq` with a `GuardValue` and `Guard.valueControls` instead.
*
* Holds if `guard` evaluating to `branch` ensures that `lesser <= greater + bias`
* holds.
*/
predicate guardEnsuresLeq(
deprecated predicate guardEnsuresLeq(
Guard guard, boolean branch, DataFlow::Node lesser, DataFlow::Node greater, int bias
) {
guardValueEnsuresLeq(guard, any(GuardValue value | value.asBooleanValue() = branch), lesser,
greater, bias)
}

/**
* Holds if `guard` evaluating to `value` ensures that `lesser <= greater + bias`
* holds.
*/
predicate guardValueEnsuresLeq(
Guard guard, GuardValue value, DataFlow::Node lesser, DataFlow::Node greater, int bias
) {
exists(DataFlow::RelationalComparisonNode rel |
guard = rel.asExpr() and
rel.leq(branch, lesser, greater, bias)
rel.leq(value.asBooleanValue(), lesser, greater, bias)
)
or
guardEnsuresEq(guard, branch, lesser, greater) and bias = 0
guardValueEnsuresEq(guard, value, lesser, greater) and bias = 0
}
8 changes: 4 additions & 4 deletions go/ql/src/InconsistentCode/ConstantLengthComparison.ql
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import go
private import semmle.go.controlflow.Guards

from
ForStmt fs, Variable i, DataFlow::ElementReadNode idx, GVN a, Guard cond, boolean branch,
ForStmt fs, Variable i, DataFlow::ElementReadNode idx, GVN a, Guard cond, GuardValue value,
DataFlow::CallNode lenA
where
// `i` is incremented in `fs`
Expand All @@ -28,10 +28,10 @@ where
lenA.getArgument(0) = a.getANode() and
// and is checked against a constant
exists(DataFlow::Node const | exists(const.getIntValue()) |
guardEnsuresNeq(cond, branch, lenA, const) or
guardEnsuresLeq(cond, branch, const, lenA, _)
guardValueEnsuresNeq(cond, value, lenA, const) or
guardValueEnsuresLeq(cond, value, const, lenA, _)
) and
cond.controls(idx.getBasicBlock(), branch) and
cond.valueControls(idx.getBasicBlock(), value) and
// and that check happens inside the loop body
cond.(Expr).getParent+() = fs
select cond, "This checks the length against a constant, but it $@.", idx,
Expand Down
28 changes: 14 additions & 14 deletions go/ql/src/InconsistentCode/LengthComparisonOffByOne.ql
Original file line number Diff line number Diff line change
Expand Up @@ -42,29 +42,29 @@ DataFlow::CallNode arrayLen(DataFlow::SsaNode array) {
}

/**
* Holds if `guard` evaluating to `branch` checks that `index` is less than or equal to
* Holds if `guard` evaluating to `value` checks that `index` is less than or equal to
* `array.length`.
*/
predicate lengthLeGuard(Guard guard, boolean branch, Index index, DataFlow::SsaNode array) {
guardEnsuresLeq(guard, branch, getAUse(index), arrayLen(array), 0)
predicate lengthLeGuard(Guard guard, GuardValue value, Index index, DataFlow::SsaNode array) {
guardValueEnsuresLeq(guard, value, getAUse(index), arrayLen(array), 0)
or
exists(int i, int bias | index = ConstantIndex(i) |
guardEnsuresLeq(guard, branch, getAUse(ConstantIndex(i + bias)), arrayLen(array), bias)
guardValueEnsuresLeq(guard, value, getAUse(ConstantIndex(i + bias)), arrayLen(array), bias)
)
}

predicate isControllingLengthLeGuard(
Guard guard, boolean branch, Index index, DataFlow::SsaNode array, BasicBlock bb
Guard guard, GuardValue value, Index index, DataFlow::SsaNode array, BasicBlock bb
) {
lengthLeGuard(guard, branch, index, array) and
guard.controls(bb, branch)
lengthLeGuard(guard, value, index, array) and
guard.valueControls(bb, value)
}

/**
* Holds if `guard` evaluating to `branch` checks that `index` is not equal to `array.length`.
* Holds if `guard` evaluating to `value` checks that `index` is not equal to `array.length`.
*/
predicate lengthNeGuard(Guard guard, boolean branch, Index index, DataFlow::SsaNode array) {
guardEnsuresNeq(guard, branch, getAUse(index), arrayLen(array))
predicate lengthNeGuard(Guard guard, GuardValue value, Index index, DataFlow::SsaNode array) {
guardValueEnsuresNeq(guard, value, getAUse(index), arrayLen(array))
}

/**
Expand All @@ -87,21 +87,21 @@ predicate isRegexpMethodCall(DataFlow::MethodCallNode c) {
}

from
Guard cond, boolean branch, DataFlow::SsaNode array, Index index, DataFlow::ElementReadNode ea,
Guard cond, GuardValue value, DataFlow::SsaNode array, Index index, DataFlow::ElementReadNode ea,
BasicBlock bb
where
// there is a read from `array[index]`
elementRead(ea, array, index, bb) and
// and it is guarded by a comparison `index <= len(array)`
isControllingLengthLeGuard(cond, branch, index, array, bb) and
isControllingLengthLeGuard(cond, value, index, array, bb) and
// and report the innermost guard that establishes the comparison
not exists(Guard innerCond |
isControllingLengthLeGuard(innerCond, _, index, array, bb) and
innerCond.(Expr).getParent+() = cond.(Expr)
) and
// but the read is not guarded by another check that `index != len(array)`
not exists(Guard ne, boolean neBranch |
lengthNeGuard(ne, neBranch, index, array) and ne.controls(bb, neBranch)
not exists(Guard ne, GuardValue neValue |
lengthNeGuard(ne, neValue, index, array) and ne.valueControls(bb, neValue)
) and
// and it is not additionally guarded by a stronger index check
not exists(Index index2, int i, int i2 |
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
---
category: minorAnalysis
---
* The queries `go/index-out-of-bounds` and `go/constant-length-comparison` now recognize switch-derived equality and inequality checks against lengths.
24 changes: 12 additions & 12 deletions go/ql/src/experimental/IntegerOverflow/RangeAnalysis.qll
Original file line number Diff line number Diff line change
Expand Up @@ -39,12 +39,12 @@ float getAnUpperBound(Expr expr) {
if
//if a condition expression exists before and one of the operand happens to be the identifier, we use this condition expression to narrow down the range.
exists(
Guard n, boolean branch, DataFlow::Node lesser, DataFlow::Node greater,
Guard n, GuardValue value, DataFlow::Node lesser, DataFlow::Node greater,
ReachableBasicBlock bb
|
guardEnsuresLeq(n, branch, lesser, greater, _) and
guardValueEnsuresLeq(n, value, lesser, greater, _) and
lesser.asExpr() = getAUse(v) and
n.controls(bb, branch) and
n.valueControls(bb, value) and
bb.getANode() = IR::evalExprInstruction(identifier) and
not exists(Expr e |
e = getAUse(v) and
Expand All @@ -53,12 +53,12 @@ float getAnUpperBound(Expr expr) {
)
then
exists(
Guard n, boolean branch, ReachableBasicBlock bb, DataFlow::Node lesser,
Guard n, GuardValue value, ReachableBasicBlock bb, DataFlow::Node lesser,
DataFlow::Node greater, int bias
|
n.controls(bb, branch) and
n.valueControls(bb, value) and
bb.getANode() = IR::evalExprInstruction(identifier) and
guardEnsuresLeq(n, branch, lesser, greater, bias) and
guardValueEnsuresLeq(n, value, lesser, greater, bias) and
getAUse(v) = lesser.asExpr() and
not exists(Expr e |
e = getAUse(v) and
Expand Down Expand Up @@ -190,12 +190,12 @@ float getALowerBound(Expr expr) {
//if exists a condition expression before this identifier
if
exists(
Guard n, boolean branch, DataFlow::Node greater, DataFlow::Node lesser,
Guard n, GuardValue value, DataFlow::Node greater, DataFlow::Node lesser,
ReachableBasicBlock bb
|
guardEnsuresLeq(n, branch, lesser, greater, _) and
guardValueEnsuresLeq(n, value, lesser, greater, _) and
greater.asExpr() = getAUse(v) and
n.controls(bb, branch) and
n.valueControls(bb, value) and
bb.getANode() = IR::evalExprInstruction(identifier) and
not exists(Expr e |
e = getAUse(v) and
Expand All @@ -204,12 +204,12 @@ float getALowerBound(Expr expr) {
)
then
exists(
Guard n, boolean branch, ReachableBasicBlock bb, DataFlow::Node lesser,
Guard n, GuardValue value, ReachableBasicBlock bb, DataFlow::Node lesser,
DataFlow::Node greater, int bias, float lbs
|
n.controls(bb, branch) and
n.valueControls(bb, value) and
bb.getANode() = IR::evalExprInstruction(identifier) and
guardEnsuresLeq(n, branch, lesser, greater, bias) and
guardValueEnsuresLeq(n, value, lesser, greater, bias) and
getAUse(v) = greater.asExpr() and
not exists(Expr e |
e = getAUse(v) and
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -190,6 +190,18 @@ valueControlsResult
ensuresEqResult
| guards.go:14:7:14:16 | ...==... | 0 = value | true |
| guards.go:14:7:14:16 | ...==... | value = 0 | true |
| guards.go:23:7:23:7 | 0 | 0 = value | match |
| guards.go:23:7:23:7 | 0 | value = 0 | match |
| guards.go:25:7:25:7 | 1 | 1 = value | match |
| guards.go:25:7:25:7 | 1 | value = 1 | match |
| guards.go:25:10:25:10 | 2 | 2 = value | match |
| guards.go:25:10:25:10 | 2 | value = 2 | match |
| guards.go:34:7:34:11 | value | false = value | match |
| guards.go:34:7:34:11 | value | value = false | match |
| guards.go:38:7:38:11 | value | true = value | match |
| guards.go:38:7:38:11 | value | value = true | match |
| guards.go:42:7:42:17 | ...<... | ...<... = false | match |
| guards.go:42:7:42:17 | ...<... | false = ...<... | match |
| 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 @@ -212,6 +224,12 @@ 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:7:137:7 | 0 | 0 = value | match |
| guards.go:137:7:137:7 | 0 | value = 0 | match |
| guards.go:148:7:148:7 | 0 | 0 = value | match |
| guards.go:148:7:148:7 | 0 | value = 0 | match |
| guards.go:171:7:171:7 | 0 | 0 = value | match |
| guards.go:171:7:171:7 | 0 | value = 0 | match |
| 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 Expand Up @@ -242,6 +260,20 @@ ensuresEqResult
| guards.go:383:5:383:34 | ...==... | nil = call to modelNullGuard | true |
| guards.go:386:5:386:37 | ...!=... | call to modelNotNullGuard = nil | false |
| guards.go:386:5:386:37 | ...!=... | nil = call to modelNotNullGuard | false |
ensuresValueResult
| guards.go:23:7:23:7 | 0 | tagged one or two | 0 != value | no match |
| guards.go:23:7:23:7 | 0 | tagged one or two | value != 0 | no match |
| guards.go:23:7:23:7 | 0 | tagged other | 0 != value | no match |
| guards.go:23:7:23:7 | 0 | tagged other | value != 0 | no match |
| guards.go:23:7:23:7 | 0 | tagged zero | 0 <= value | match |
| guards.go:23:7:23:7 | 0 | tagged zero | 0 = value | match |
| guards.go:23:7:23:7 | 0 | tagged zero | value <= 0 | match |
| guards.go:23:7:23:7 | 0 | tagged zero | value = 0 | match |
| guards.go:25:7:25:7 | 1 | tagged other | 1 != value | no match |
| guards.go:25:7:25:7 | 1 | tagged other | value != 1 | no match |
| guards.go:25:10:25:10 | 2 | tagged other | 2 != value | no match |
| guards.go:25:10:25:10 | 2 | tagged other | value != 2 | no match |
| guards.go:68:5:68:15 | ...<=... | at most ten | value <= 10 | true |
modelBarrierResult
| guards.go:375:27:375:33 | pointer | model bool | model-bool |
| guards.go:378:27:378:33 | pointer | model zero | model-zero |
Expand Down
28 changes: 25 additions & 3 deletions go/ql/test/library-tests/semmle/go/controlflow/Guards/Guards.ql
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,32 @@ query predicate valueControlsResult(Guard guard, string label, string outcome) {
}

query predicate ensuresEqResult(Guard guard, string label, string outcome) {
exists(boolean branch, DataFlow::Node left, DataFlow::Node right |
guardEnsuresEq(guard, branch, left, right) and
exists(GuardValue value, DataFlow::Node left, DataFlow::Node right |
guardValueEnsuresEq(guard, value, left, right) and
label = left.toString() + " = " + right.toString() and
outcome = branch.toString()
outcome = value.toString()
)
}

query predicate ensuresValueResult(Guard guard, string label, string relationship, string outcome) {
exists(DataFlow::CallNode call, GuardValue value, DataFlow::Node left, DataFlow::Node right |
sinkCall(call, label) and
label in [
"tagged zero", "tagged one or two", "tagged other", "fallthrough default", "at most ten",
"above ten"
] and
guard.valueControls(call.getBasicBlock(), value) and
(
guardValueEnsuresEq(guard, value, left, right) and
relationship = left.toString() + " = " + right.toString()
or
guardValueEnsuresNeq(guard, value, left, right) and
relationship = left.toString() + " != " + right.toString()
or
guardValueEnsuresLeq(guard, value, left, right, 0) and
relationship = left.toString() + " <= " + right.toString()
) and
outcome = value.toString()
)
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
| ConstantLengthComparison.go:5:6:5:17 | ...==... | This checks the length against a constant, but it $@. | ConstantLengthComparison.go:5:31:5:35 | index expression | is indexed using a variable |
| tst.go:19:8:19:8 | 1 | This checks the length against a constant, but it $@. | tst.go:20:20:20:24 | index expression | is indexed using a variable |
| tst.go:29:8:29:8 | 0 | This checks the length against a constant, but it $@. | tst.go:32:16:32:20 | index expression | is indexed using a variable |
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,27 @@ func isPrefixOfGood2(xs, ys []int) bool {
}
return true
}

func switchConstantLengthMatch(xs, ys []int) bool {
for i := 0; i < len(xs); i++ {
switch len(ys) {
case 1: // $ Alert
return xs[i] == ys[i]
}
}
return false
}

func switchConstantLengthNonMatch(xs, ys []int) bool {
for i := 0; i < len(xs); i++ {
switch len(ys) {
case 0: // $ Alert
return false
default:
if xs[i] != ys[i] {
return false
}
}
}
return true
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,5 @@
| main.go:6:5:6:15 | ...<=... | Off-by-one index comparison against length may lead to out-of-bounds $@. | main.go:7:10:7:13 | index expression | read |
| main.go:29:5:29:14 | ...>... | Off-by-one index comparison against length may lead to out-of-bounds $@. | main.go:30:10:30:13 | index expression | read |
| main.go:87:7:87:17 | ...>=... | Off-by-one index comparison against length may lead to out-of-bounds $@. | main.go:93:9:93:12 | index expression | read |
| main.go:100:7:100:17 | call to len | Off-by-one index comparison against length may lead to out-of-bounds $@. | main.go:101:10:101:18 | index expression | read |
| main.go:124:5:124:20 | ...<=... | Off-by-one index comparison against length may lead to out-of-bounds $@. | main.go:129:11:129:19 | index expression | read |
Loading
Loading