Skip to content

Commit c6761db

Browse files
committed
SSA: Replace the Guards interface in the SSA data flow integration.
1 parent a027353 commit c6761db

File tree

6 files changed

+62
-58
lines changed
  • csharp/ql/lib/semmle/code/csharp/dataflow/internal
  • javascript/ql/lib/semmle/javascript/dataflow/internal/sharedlib
  • java/ql/lib/semmle/code/java/dataflow/internal
  • ruby/ql/lib/codeql/ruby/dataflow/internal
  • rust/ql/lib/codeql/rust/dataflow/internal
  • shared/ssa/codeql/ssa

6 files changed

+62
-58
lines changed

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

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1047,8 +1047,17 @@ private module DataFlowIntegrationInput implements Impl::DataFlowIntegrationInpu
10471047
}
10481048

10491049
class Guard extends Guards::Guard {
1050-
predicate hasCfgNode(ControlFlow::BasicBlock bb, int i) {
1051-
this.getAControlFlowNode() = bb.getNode(i)
1050+
/**
1051+
* Holds if the control flow branching from `bb1` is dependent on this guard,
1052+
* and that the edge from `bb1` to `bb2` corresponds to the evaluation of this
1053+
* guard to `branch`.
1054+
*/
1055+
predicate controlsBranchEdge(BasicBlock bb1, BasicBlock bb2, boolean branch) {
1056+
exists(ControlFlow::SuccessorTypes::ConditionalSuccessor s |
1057+
this.getAControlFlowNode() = bb1.getLastNode() and
1058+
bb2 = bb1.getASuccessorByType(s) and
1059+
s.getValue() = branch
1060+
)
10521061
}
10531062
}
10541063

@@ -1060,16 +1069,6 @@ private module DataFlowIntegrationInput implements Impl::DataFlowIntegrationInpu
10601069
conditionBlock.edgeDominates(bb, s)
10611070
)
10621071
}
1063-
1064-
/** Gets an immediate conditional successor of basic block `bb`, if any. */
1065-
ControlFlow::BasicBlock getAConditionalBasicBlockSuccessor(
1066-
ControlFlow::BasicBlock bb, boolean branch
1067-
) {
1068-
exists(ControlFlow::SuccessorTypes::ConditionalSuccessor s |
1069-
result = bb.getASuccessorByType(s) and
1070-
s.getValue() = branch
1071-
)
1072-
}
10731072
}
10741073

10751074
private module DataFlowIntegrationImpl = Impl::DataFlowIntegration<DataFlowIntegrationInput>;

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

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -667,22 +667,20 @@ private module DataFlowIntegrationInput implements Impl::DataFlowIntegrationInpu
667667
}
668668

669669
class Guard extends Guards::Guard {
670-
predicate hasCfgNode(BasicBlock bb, int i) {
671-
this = bb.getNode(i).asExpr()
672-
or
673-
this = bb.getNode(i).asStmt()
670+
/**
671+
* Holds if the control flow branching from `bb1` is dependent on this guard,
672+
* and that the edge from `bb1` to `bb2` corresponds to the evaluation of this
673+
* guard to `branch`.
674+
*/
675+
predicate controlsBranchEdge(BasicBlock bb1, BasicBlock bb2, boolean branch) {
676+
super.hasBranchEdge(bb1, bb2, branch)
674677
}
675678
}
676679

677680
/** Holds if the guard `guard` controls block `bb` upon evaluating to `branch`. */
678681
predicate guardControlsBlock(Guard guard, BasicBlock bb, boolean branch) {
679682
guard.controls(bb, branch)
680683
}
681-
682-
/** Gets an immediate conditional successor of basic block `bb`, if any. */
683-
BasicBlock getAConditionalBasicBlockSuccessor(BasicBlock bb, boolean branch) {
684-
result = bb.(Guards::ConditionBlock).getTestSuccessor(branch)
685-
}
686684
}
687685

688686
private module DataFlowIntegrationImpl = Impl::DataFlowIntegration<DataFlowIntegrationInput>;

javascript/ql/lib/semmle/javascript/dataflow/internal/sharedlib/Ssa.qll

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,19 @@ module SsaDataflowInput implements DataFlowIntegrationInputSig {
8181
class Guard extends js::ControlFlowNode {
8282
Guard() { this = any(js::ConditionGuardNode g).getTest() }
8383

84-
predicate hasCfgNode(js::BasicBlock bb, int i) { this = bb.getNode(i) }
84+
/**
85+
* Holds if the control flow branching from `bb1` is dependent on this guard,
86+
* and that the edge from `bb1` to `bb2` corresponds to the evaluation of this
87+
* guard to `branch`.
88+
*/
89+
predicate controlsBranchEdge(js::BasicBlock bb1, js::BasicBlock bb2, boolean branch) {
90+
exists(js::ConditionGuardNode g |
91+
g.getTest() = this and
92+
bb1 = this.getBasicBlock() and
93+
bb2 = g.getBasicBlock() and
94+
branch = g.getOutcome()
95+
)
96+
}
8597
}
8698

8799
pragma[inline]
@@ -92,14 +104,6 @@ module SsaDataflowInput implements DataFlowIntegrationInputSig {
92104
branch = g.getOutcome()
93105
)
94106
}
95-
96-
js::BasicBlock getAConditionalBasicBlockSuccessor(js::BasicBlock bb, boolean branch) {
97-
exists(js::ConditionGuardNode g |
98-
bb = g.getTest().getBasicBlock() and
99-
result = g.getBasicBlock() and
100-
branch = g.getOutcome()
101-
)
102-
}
103107
}
104108

105109
import DataFlowIntegration<SsaDataflowInput>

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

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -515,21 +515,24 @@ private module DataFlowIntegrationInput implements Impl::DataFlowIntegrationInpu
515515
predicate ssaDefInitializesParam(WriteDefinition def, Parameter p) { p.isInitializedBy(def) }
516516

517517
class Guard extends Cfg::CfgNodes::AstCfgNode {
518-
predicate hasCfgNode(SsaInput::BasicBlock bb, int i) { this = bb.getNode(i) }
518+
/**
519+
* Holds if the control flow branching from `bb1` is dependent on this guard,
520+
* and that the edge from `bb1` to `bb2` corresponds to the evaluation of this
521+
* guard to `branch`.
522+
*/
523+
predicate controlsBranchEdge(SsaInput::BasicBlock bb1, SsaInput::BasicBlock bb2, boolean branch) {
524+
exists(Cfg::SuccessorTypes::ConditionalSuccessor s |
525+
this.getBasicBlock() = bb1 and
526+
bb2 = bb1.getASuccessor(s) and
527+
s.getValue() = branch
528+
)
529+
}
519530
}
520531

521532
/** Holds if the guard `guard` controls block `bb` upon evaluating to `branch`. */
522533
predicate guardControlsBlock(Guard guard, SsaInput::BasicBlock bb, boolean branch) {
523534
Guards::guardControlsBlock(guard, bb, branch)
524535
}
525-
526-
/** Gets an immediate conditional successor of basic block `bb`, if any. */
527-
SsaInput::BasicBlock getAConditionalBasicBlockSuccessor(SsaInput::BasicBlock bb, boolean branch) {
528-
exists(Cfg::SuccessorTypes::ConditionalSuccessor s |
529-
result = bb.getASuccessor(s) and
530-
s.getValue() = branch
531-
)
532-
}
533536
}
534537

535538
private module DataFlowIntegrationImpl = Impl::DataFlowIntegration<DataFlowIntegrationInput>;

rust/ql/lib/codeql/rust/dataflow/internal/SsaImpl.qll

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -361,7 +361,18 @@ private module DataFlowIntegrationInput implements Impl::DataFlowIntegrationInpu
361361
}
362362

363363
class Guard extends CfgNodes::AstCfgNode {
364-
predicate hasCfgNode(SsaInput::BasicBlock bb, int i) { this = bb.getNode(i) }
364+
/**
365+
* Holds if the control flow branching from `bb1` is dependent on this guard,
366+
* and that the edge from `bb1` to `bb2` corresponds to the evaluation of this
367+
* guard to `branch`.
368+
*/
369+
predicate controlsBranchEdge(SsaInput::BasicBlock bb1, SsaInput::BasicBlock bb2, boolean branch) {
370+
exists(Cfg::ConditionalSuccessor s |
371+
this = bb1.getANode() and
372+
bb2 = bb1.getASuccessor(s) and
373+
s.getValue() = branch
374+
)
375+
}
365376
}
366377

367378
/** Holds if the guard `guard` controls block `bb` upon evaluating to `branch`. */
@@ -372,14 +383,6 @@ private module DataFlowIntegrationInput implements Impl::DataFlowIntegrationInpu
372383
conditionBlock.edgeDominates(bb, s)
373384
)
374385
}
375-
376-
/** Gets an immediate conditional successor of basic block `bb`, if any. */
377-
SsaInput::BasicBlock getAConditionalBasicBlockSuccessor(SsaInput::BasicBlock bb, boolean branch) {
378-
exists(Cfg::ConditionalSuccessor s |
379-
result = bb.getASuccessor(s) and
380-
s.getValue() = branch
381-
)
382-
}
383386
}
384387

385388
private module DataFlowIntegrationImpl = Impl::DataFlowIntegration<DataFlowIntegrationInput>;

shared/ssa/codeql/ssa/Ssa.qll

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1434,15 +1434,16 @@ module Make<LocationSig Location, InputSig<Location> Input> {
14341434
/** Gets a textual representation of this guard. */
14351435
string toString();
14361436

1437-
/** Holds if the `i`th node of basic block `bb` evaluates this guard. */
1438-
predicate hasCfgNode(BasicBlock bb, int i);
1437+
/**
1438+
* Holds if the control flow branching from `bb1` is dependent on this guard,
1439+
* and that the edge from `bb1` to `bb2` corresponds to the evaluation of this
1440+
* guard to `branch`.
1441+
*/
1442+
predicate controlsBranchEdge(BasicBlock bb1, BasicBlock bb2, boolean branch);
14391443
}
14401444

14411445
/** Holds if `guard` controls block `bb` upon evaluating to `branch`. */
14421446
predicate guardControlsBlock(Guard guard, BasicBlock bb, boolean branch);
1443-
1444-
/** Gets an immediate conditional successor of basic block `bb`, if any. */
1445-
BasicBlock getAConditionalBasicBlockSuccessor(BasicBlock bb, boolean branch);
14461447
}
14471448

14481449
/**
@@ -1891,11 +1892,7 @@ module Make<LocationSig Location, InputSig<Location> Input> {
18911892
|
18921893
DfInput::guardControlsBlock(g, bb, branch)
18931894
or
1894-
exists(int last |
1895-
last = bb.length() - 1 and
1896-
g.hasCfgNode(bb, last) and
1897-
DfInput::getAConditionalBasicBlockSuccessor(bb, branch) = phi.getBasicBlock()
1898-
)
1895+
g.controlsBranchEdge(bb, phi.getBasicBlock(), branch)
18991896
)
19001897
)
19011898
}

0 commit comments

Comments
 (0)