Skip to content

Commit 3c638b5

Browse files
author
Robert Marsh
committed
C++: add edge-based predicates to IRGuards
These predicates currently take a pair of `IRBlock`s - as it stands, at most one edge can exist from one `IRBlock` to a given other `IRBlock`. We may need to revisit that assumption and create an `IREdge` IPA type at some future date
1 parent b85b774 commit 3c638b5

File tree

3 files changed

+28
-7
lines changed

3 files changed

+28
-7
lines changed

cpp/ql/src/semmle/code/cpp/controlflow/IRGuards.qll

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -281,6 +281,16 @@ class IRGuardCondition extends Instruction {
281281
ne.controls(controlled, testIsTrue.booleanNot()))
282282
}
283283

284+
predicate controlsEdge(IRBlock pred, IRBlock succ, boolean testIsTrue) {
285+
pred.getASuccessor() = succ and
286+
controls(pred, testIsTrue)
287+
or
288+
hasBranchEdge(succ, testIsTrue) and
289+
branch.getCondition() = this and
290+
branch.getBlock() = pred
291+
}
292+
293+
284294
/**
285295
* Holds if `branch` jumps directly to `succ` when this condition is `testIsTrue`.
286296
*
@@ -296,7 +306,8 @@ class IRGuardCondition extends Instruction {
296306
* return x;
297307
* ```
298308
*/
299-
predicate hasBranchEdge(IRBlock succ, boolean testIsTrue) {
309+
private predicate hasBranchEdge(IRBlock succ, boolean testIsTrue) {
310+
branch.getCondition() = this and
300311
(
301312
testIsTrue = true and
302313
succ.getFirstInstruction() = branch.getTrueSuccessor()
@@ -319,6 +330,14 @@ class IRGuardCondition extends Instruction {
319330
)
320331
}
321332

333+
/** Holds if (determined by this guard) `left < right + k` must be `isLessThan` on the edge from
334+
* `pred` to `succ`. If `isLessThan = false` then this implies `left >= right + k`. */
335+
cached predicate ensuresLtEdge(Operand left, Operand right, int k, IRBlock pred, IRBlock succ, boolean isLessThan) {
336+
exists(boolean testIsTrue |
337+
compares_lt(this, left, right, k, isLessThan, testIsTrue) and this.controlsEdge(pred, succ, testIsTrue)
338+
)
339+
}
340+
322341
/** Holds if (determined by this guard) `left == right + k` evaluates to `areEqual` if this expression evaluates to `testIsTrue`. */
323342
cached predicate comparesEq(Operand left, Operand right, int k, boolean areEqual, boolean testIsTrue) {
324343
compares_eq(this, left, right, k, areEqual, testIsTrue)
@@ -331,6 +350,13 @@ class IRGuardCondition extends Instruction {
331350
compares_eq(this, left, right, k, areEqual, testIsTrue) and this.controls(block, testIsTrue)
332351
)
333352
}
353+
/** Holds if (determined by this guard) `left == right + k` must be `areEqual` on the edge from
354+
* `pred` to `succ`. If `areEqual = false` then this implies `left != right + k`. */
355+
cached predicate ensuresEqEdge(Operand left, Operand right, int k, IRBlock pred, IRBlock succ, boolean areEqual) {
356+
exists(boolean testIsTrue |
357+
compares_eq(this, left, right, k, areEqual, testIsTrue) and this.controlsEdge(pred, succ, testIsTrue)
358+
)
359+
}
334360

335361
/**
336362
* Holds if this condition controls `block`, meaning that `block` is only

cpp/ql/src/semmle/code/cpp/rangeanalysis/RangeAnalysis.qll

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -386,11 +386,7 @@ private predicate boundFlowStepPhi(
386386
or
387387
exists(IRGuardCondition guard, boolean testIsTrue |
388388
guard = boundFlowCond(valueNumberOfOperand(op2), op1, delta, upper, testIsTrue) and
389-
(
390-
guard.hasBranchEdge(op2.getPredecessorBlock().getLastInstruction(), op2.getUseInstruction().getBlock(), testIsTrue)
391-
or
392-
guard.controls(op2.getPredecessorBlock(), testIsTrue)
393-
) and
389+
guard.controlsEdge(op2.getPredecessorBlock(), op2.getUseInstruction().getBlock(), testIsTrue) and
394390
reason = TCondReason(guard)
395391
)
396392
}

cpp/ql/test/library-tests/controlflow/guards-ir/test.c

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -151,4 +151,3 @@ void test5(int x) {
151151
void test6(int x, int y) {
152152
return x && y;
153153
}
154-

0 commit comments

Comments
 (0)