Skip to content

Commit 820d2cb

Browse files
committed
Shared: Use edge dominance in basic block library
1 parent 7619f1d commit 820d2cb

File tree

16 files changed

+142
-75
lines changed

16 files changed

+142
-75
lines changed

csharp/ql/lib/semmle/code/csharp/controlflow/BasicBlocks.qll

Lines changed: 30 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -202,6 +202,29 @@ final class BasicBlock extends BasicBlocksImpl::BasicBlock {
202202
*/
203203
BasicBlock getImmediateDominator() { result = super.getImmediateDominator() }
204204

205+
/**
206+
* Holds if the edge with successor type `s` out of this basic block is a
207+
* dominating edge for `dominated`.
208+
*
209+
* That is, all paths reaching `dominated` from the entry point basic
210+
* block must go through the `s` edge out of this basic block.
211+
*
212+
* Edge dominance is similar to node dominance except it concerns edges
213+
* instead of nodes: A basic block is dominated by a _basic block_ `bb` if it
214+
* can only be reached through `bb` and dominated by an _edge_ `s` if it can
215+
* only be reached through `s`.
216+
*
217+
* Note that where all basic blocks (except the entry basic block) are
218+
* strictly dominated by at least one basic block, a basic block may not be
219+
* dominated by any edge. If an edge dominates a basic block `bb`, then
220+
* both endpoints of the edge dominates `bb`. The converse is not the case,
221+
* as there may be multiple paths between the endpoints with none of them
222+
* dominating.
223+
*/
224+
predicate edgeDominates(BasicBlock dominated, ControlFlow::SuccessorType s) {
225+
super.edgeDominates(dominated, s)
226+
}
227+
205228
/**
206229
* Holds if this basic block strictly post-dominates basic block `bb`.
207230
*
@@ -296,11 +319,14 @@ final class JoinBlockPredecessor extends BasicBlock, BasicBlocksImpl::JoinPredec
296319
* control flow.
297320
*/
298321
final class ConditionBlock extends BasicBlock, BasicBlocksImpl::ConditionBasicBlock {
299-
predicate immediatelyControls(BasicBlock succ, ConditionalSuccessor s) {
300-
super.immediatelyControls(succ, s)
322+
/** DEPRECATED: Use `edgeDominates` instead. */
323+
deprecated predicate immediatelyControls(BasicBlock succ, ConditionalSuccessor s) {
324+
this.getASuccessor(s) = succ and
325+
BasicBlocksImpl::dominatingEdge(this, succ)
301326
}
302327

303-
predicate controls(BasicBlock controlled, ConditionalSuccessor s) {
304-
super.controls(controlled, s)
328+
/** DEPRECATED: Use `edgeDominates` instead. */
329+
deprecated predicate controls(BasicBlock controlled, ConditionalSuccessor s) {
330+
super.edgeDominates(controlled, s)
305331
}
306332
}

csharp/ql/lib/semmle/code/csharp/controlflow/ControlFlowElement.qll

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -225,6 +225,6 @@ class ControlFlowElement extends ExprOrStmtParent, @control_flow_element {
225225
this.controlsBlockSplit(controlled, s, cb)
226226
or
227227
cb.getLastNode() = this.getAControlFlowNode() and
228-
cb.controls(controlled, s)
228+
cb.edgeDominates(controlled, s)
229229
}
230230
}

csharp/ql/lib/semmle/code/csharp/dataflow/internal/SsaImpl.qll

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1119,7 +1119,7 @@ private module DataFlowIntegrationInput implements Impl::DataFlowIntegrationInpu
11191119
exists(ConditionBlock conditionBlock, ControlFlow::SuccessorTypes::ConditionalSuccessor s |
11201120
guard.getAControlFlowNode() = conditionBlock.getLastNode() and
11211121
s.getValue() = branch and
1122-
conditionBlock.controls(bb, s)
1122+
conditionBlock.edgeDominates(bb, s)
11231123
)
11241124
}
11251125

csharp/ql/test/library-tests/controlflow/graph/Condition.ql

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,8 @@ import ControlFlow
44
query predicate conditionBlock(
55
BasicBlocks::ConditionBlock cb, BasicBlock controlled, boolean testIsTrue
66
) {
7-
cb.controls(controlled, any(SuccessorTypes::ConditionalSuccessor s | testIsTrue = s.getValue()))
7+
cb.edgeDominates(controlled,
8+
any(SuccessorTypes::ConditionalSuccessor s | testIsTrue = s.getValue()))
89
}
910

1011
ControlFlow::Node successor(ControlFlow::Node node, boolean kind) {

ruby/ql/lib/codeql/ruby/controlflow/BasicBlocks.qll

Lines changed: 32 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -168,6 +168,29 @@ final class BasicBlock extends BasicBlocksImpl::BasicBlock {
168168
*/
169169
BasicBlock getImmediateDominator() { result = super.getImmediateDominator() }
170170

171+
/**
172+
* Holds if the edge with successor type `s` out of this basic block is a
173+
* dominating edge for `dominated`.
174+
*
175+
* That is, all paths reaching `dominated` from the entry point basic
176+
* block must go through the `s` edge out of this basic block.
177+
*
178+
* Edge dominance is similar to node dominance except it concerns edges
179+
* instead of nodes: A basic block is dominated by a _basic block_ `bb` if it
180+
* can only be reached through `bb` and dominated by an _edge_ `s` if it can
181+
* only be reached through `s`.
182+
*
183+
* Note that where all basic blocks (except the entry basic block) are
184+
* strictly dominated by at least one basic block, a basic block may not be
185+
* dominated by any edge. If an edge dominates a basic block `bb`, then
186+
* both endpoints of the edge dominates `bb`. The converse is not the case,
187+
* as there may be multiple paths between the endpoints with none of them
188+
* dominating.
189+
*/
190+
predicate edgeDominates(BasicBlock dominated, SuccessorType s) {
191+
super.edgeDominates(dominated, s)
192+
}
193+
171194
/**
172195
* Holds if this basic block strictly post-dominates basic block `bb`.
173196
*
@@ -248,21 +271,26 @@ final class JoinBlockPredecessor extends BasicBlock, BasicBlocksImpl::JoinPredec
248271
*/
249272
final class ConditionBlock extends BasicBlock, BasicBlocksImpl::ConditionBasicBlock {
250273
/**
274+
* DEPRECATED: Use `edgeDominates` instead.
275+
*
251276
* Holds if basic block `succ` is immediately controlled by this basic
252277
* block with conditional value `s`. That is, `succ` is an immediate
253278
* successor of this block, and `succ` can only be reached from
254279
* the callable entry point by going via the `s` edge out of this basic block.
255280
*/
256-
predicate immediatelyControls(BasicBlock succ, ConditionalSuccessor s) {
257-
super.immediatelyControls(succ, s)
281+
deprecated predicate immediatelyControls(BasicBlock succ, ConditionalSuccessor s) {
282+
this.getASuccessor(s) = succ and
283+
BasicBlocksImpl::dominatingEdge(this, succ)
258284
}
259285

260286
/**
287+
* DEPRECATED: Use `edgeDominates` instead.
288+
*
261289
* Holds if basic block `controlled` is controlled by this basic block with
262290
* conditional value `s`. That is, `controlled` can only be reached from the
263291
* callable entry point by going via the `s` edge out of this basic block.
264292
*/
265-
predicate controls(BasicBlock controlled, ConditionalSuccessor s) {
266-
super.controls(controlled, s)
293+
deprecated predicate controls(BasicBlock controlled, ConditionalSuccessor s) {
294+
super.edgeDominates(controlled, s)
267295
}
268296
}

ruby/ql/lib/codeql/ruby/controlflow/internal/Guards.qll

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,6 @@ predicate guardControlsBlock(CfgNodes::AstCfgNode guard, BasicBlock bb, boolean
66
exists(ConditionBlock conditionBlock, SuccessorTypes::ConditionalSuccessor s |
77
guard = conditionBlock.getLastNode() and
88
s.getValue() = branch and
9-
conditionBlock.controls(bb, s)
9+
conditionBlock.edgeDominates(bb, s)
1010
)
1111
}

ruby/ql/lib/codeql/ruby/dataflow/internal/DataFlowPrivate.qll

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2387,7 +2387,7 @@ module TypeInference {
23872387
|
23882388
m = resolveConstantReadAccess(pattern.getExpr()) and
23892389
cb.getLastNode() = pattern and
2390-
cb.controls(read.getBasicBlock(),
2390+
cb.edgeDominates(read.getBasicBlock(),
23912391
any(SuccessorTypes::MatchingSuccessor match | match.getValue() = true)) and
23922392
caseRead = def.getARead() and
23932393
read = def.getARead() and

ruby/ql/lib/codeql/ruby/security/ConditionalBypassCustomizations.qll

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ module ConditionalBypass {
4747

4848
SensitiveActionGuardConditional() {
4949
exists(ConditionBlock cb, BasicBlock controlled |
50-
cb.controls(controlled, _) and
50+
cb.edgeDominates(controlled, _) and
5151
controlled.getANode() = action.asExpr() and
5252
cb.getLastNode() = this.asExpr()
5353
)

ruby/ql/test/library-tests/controlflow/graph/BasicBlocks.ql

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,8 @@ query predicate immediateDominator(BasicBlock bb1, BasicBlock bb2) {
1010
bb1.getImmediateDominator() = bb2
1111
}
1212

13-
query predicate controls(ConditionBlock bb1, BasicBlock bb2, SuccessorType t) {
14-
bb1.controls(bb2, t)
13+
query predicate controls(ConditionBlock bb1, BasicBlock bb2, SuccessorTypes::ConditionalSuccessor t) {
14+
bb1.edgeDominates(bb2, t)
1515
}
1616

1717
query predicate successor(ConditionBlock bb1, BasicBlock bb2, SuccessorType t) {

ruby/ql/test/library-tests/dataflow/barrier-guards/barrier-guards.ql

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ query predicate newStyleBarrierGuards(DataFlow::Node n) {
1414

1515
query predicate controls(CfgNode condition, BasicBlock bb, SuccessorTypes::ConditionalSuccessor s) {
1616
exists(ConditionBlock cb |
17-
cb.controls(bb, s) and
17+
cb.edgeDominates(bb, s) and
1818
condition = cb.getLastNode()
1919
)
2020
}

0 commit comments

Comments
 (0)