Skip to content

Commit b290565

Browse files
committed
Further optimise guardingFunction: remove redundant condition, and order guard -> guardFunction case to work backwards from interesting return sites, allowing us to go backwards not forwards through BasicBlock::dominates
1 parent 4edd81e commit b290565

File tree

1 file changed

+18
-13
lines changed

1 file changed

+18
-13
lines changed

go/ql/lib/semmle/go/dataflow/internal/DataFlowUtil.qll

Lines changed: 18 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -341,6 +341,21 @@ module BarrierGuard<guardChecksSig/3 guardChecks> {
341341
localFlow(pragma[only_bind_out](outp.getNode(c)), resNode)
342342
}
343343

344+
private predicate onlyPossibleReturnSatisfyingProperty(
345+
FuncDecl fd, FunctionOutput outp, Node ret, DataFlow::Property p
346+
) {
347+
exists(boolean b |
348+
onlyPossibleReturnOfBool(fd, outp, ret, b) and
349+
p.isBoolean(b)
350+
)
351+
or
352+
onlyPossibleReturnOfNonNil(fd, outp, ret) and
353+
p.isNonNil()
354+
or
355+
onlyPossibleReturnOfNil(fd, outp, ret) and
356+
p.isNil()
357+
}
358+
344359
/**
345360
* Holds if whenever `p` holds of output `outp` of function `f`, this node
346361
* is known to validate the input `inp` of `f`.
@@ -356,23 +371,13 @@ module BarrierGuard<guardChecksSig/3 guardChecks> {
356371
exists(FuncDecl fd, Node arg, Node ret |
357372
fd.getFunction() = f and
358373
localFlow(inp.getExitNode(fd), pragma[only_bind_out](arg)) and
359-
ret = outp.getEntryNode(fd) and
360374
(
361375
// Case: a function like "if someBarrierGuard(arg) { return true } else { return false }"
362376
exists(ControlFlow::ConditionGuardNode guard |
363-
guards(g, guard, arg) and
364-
guard.dominates(ret.getBasicBlock())
377+
guards(g, pragma[only_bind_out](guard), arg) and
378+
guard.dominates(pragma[only_bind_out](ret).getBasicBlock())
365379
|
366-
exists(boolean b |
367-
onlyPossibleReturnOfBool(fd, outp, ret, b) and
368-
p.isBoolean(b)
369-
)
370-
or
371-
onlyPossibleReturnOfNonNil(fd, outp, ret) and
372-
p.isNonNil()
373-
or
374-
onlyPossibleReturnOfNil(fd, outp, ret) and
375-
p.isNil()
380+
onlyPossibleReturnSatisfyingProperty(fd, outp, ret, p)
376381
)
377382
or
378383
// Case: a function like "return someBarrierGuard(arg)"

0 commit comments

Comments
 (0)