Skip to content

Commit 8a6c529

Browse files
authored
Merge pull request github#18233 from MathiasVP/fix-join-order-in-barrier-guards
C++: Fix two bad joins in barrier guards
2 parents ee8ce1c + c511532 commit 8a6c529

File tree

2 files changed

+21
-9
lines changed

2 files changed

+21
-9
lines changed

cpp/ql/lib/semmle/code/cpp/ir/dataflow/internal/DataFlowUtil.qll

Lines changed: 20 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2275,6 +2275,12 @@ private predicate guardControlsPhiInput(
22752275
*/
22762276
signature predicate guardChecksSig(IRGuardCondition g, Expr e, boolean branch);
22772277

2278+
bindingset[g, n]
2279+
pragma[inline_late]
2280+
private predicate controls(IRGuardCondition g, Node n, boolean edge) {
2281+
g.controls(n.getBasicBlock(), edge)
2282+
}
2283+
22782284
/**
22792285
* Provides a set of barrier nodes for a guard that validates an expression.
22802286
*
@@ -2318,15 +2324,17 @@ module BarrierGuard<guardChecksSig/3 guardChecks> {
23182324
exists(IRGuardCondition g, Expr e, ValueNumber value, boolean edge |
23192325
e = value.getAnInstruction().getConvertedResultExpression() and
23202326
result.asConvertedExpr() = e and
2321-
guardChecks(g, value.getAnInstruction().getConvertedResultExpression(), edge) and
2322-
g.controls(result.getBasicBlock(), edge)
2327+
guardChecks(g,
2328+
pragma[only_bind_into](value.getAnInstruction().getConvertedResultExpression()), edge) and
2329+
controls(g, result, edge)
23232330
)
23242331
or
23252332
exists(
23262333
IRGuardCondition g, boolean branch, Ssa::DefinitionExt def, IRBlock input, Ssa::PhiNode phi
23272334
|
23282335
guardChecks(g, def.getARead().asOperand().getDef().getConvertedResultExpression(), branch) and
2329-
guardControlsPhiInput(g, branch, def, input, phi) and
2336+
guardControlsPhiInput(g, branch, def, pragma[only_bind_into](input),
2337+
pragma[only_bind_into](phi)) and
23302338
result = TSsaPhiInputNode(phi, input)
23312339
)
23322340
}
@@ -2404,8 +2412,9 @@ module BarrierGuard<guardChecksSig/3 guardChecks> {
24042412
exists(IRGuardCondition g, Expr e, ValueNumber value, boolean edge |
24052413
e = value.getAnInstruction().getConvertedResultExpression() and
24062414
result.asIndirectConvertedExpr(indirectionIndex) = e and
2407-
guardChecks(g, value.getAnInstruction().getConvertedResultExpression(), edge) and
2408-
g.controls(result.getBasicBlock(), edge)
2415+
guardChecks(g,
2416+
pragma[only_bind_into](value.getAnInstruction().getConvertedResultExpression()), edge) and
2417+
controls(g, result, edge)
24092418
)
24102419
or
24112420
exists(
@@ -2414,7 +2423,8 @@ module BarrierGuard<guardChecksSig/3 guardChecks> {
24142423
guardChecks(g,
24152424
def.getARead().asIndirectOperand(indirectionIndex).getDef().getConvertedResultExpression(),
24162425
branch) and
2417-
guardControlsPhiInput(g, branch, def, input, phi) and
2426+
guardControlsPhiInput(g, branch, def, pragma[only_bind_into](input),
2427+
pragma[only_bind_into](phi)) and
24182428
result = TSsaPhiInputNode(phi, input)
24192429
)
24202430
}
@@ -2443,17 +2453,18 @@ module InstructionBarrierGuard<instructionGuardChecksSig/3 instructionGuardCheck
24432453
/** Gets a node that is safely guarded by the given guard check. */
24442454
Node getABarrierNode() {
24452455
exists(IRGuardCondition g, ValueNumber value, boolean edge, Operand use |
2446-
instructionGuardChecks(g, value.getAnInstruction(), edge) and
2456+
instructionGuardChecks(g, pragma[only_bind_into](value.getAnInstruction()), edge) and
24472457
use = value.getAnInstruction().getAUse() and
24482458
result.asOperand() = use and
2449-
g.controls(result.getBasicBlock(), edge)
2459+
controls(g, result, edge)
24502460
)
24512461
or
24522462
exists(
24532463
IRGuardCondition g, boolean branch, Ssa::DefinitionExt def, IRBlock input, Ssa::PhiNode phi
24542464
|
24552465
instructionGuardChecks(g, def.getARead().asOperand().getDef(), branch) and
2456-
guardControlsPhiInput(g, branch, def, input, phi) and
2466+
guardControlsPhiInput(g, branch, def, pragma[only_bind_into](input),
2467+
pragma[only_bind_into](phi)) and
24572468
result = TSsaPhiInputNode(phi, input)
24582469
)
24592470
}

cpp/ql/lib/semmle/code/cpp/ir/dataflow/internal/SsaInternals.qll

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1276,6 +1276,7 @@ class DefinitionExt extends SsaImpl::DefinitionExt {
12761276
}
12771277

12781278
/** Gets a node that represents a read of this SSA definition. */
1279+
pragma[nomagic]
12791280
Node getARead() {
12801281
exists(SourceVariable sv, IRBlock bb, int i | SsaCached::ssaDefReachesReadExt(sv, this, bb, i) |
12811282
useToNode(bb, i, sv, result)

0 commit comments

Comments
 (0)