Skip to content

Commit da0909c

Browse files
authored
Merge pull request #16896 from hvitved/ssa/dataflow-integration-prep
SSA: Add `BasicBlock.{getNode/1,length/0}` to the input signature
2 parents b779341 + 4ae8720 commit da0909c

File tree

19 files changed

+117
-33
lines changed

19 files changed

+117
-33
lines changed

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1809,7 +1809,7 @@ module IteratorFlow {
18091809
* Holds if `(bb, i)` contains a write to an iterator that may have been obtained
18101810
* by calling `begin` (or related functions) on the variable `v`.
18111811
*/
1812-
predicate variableWrite(IRBlock bb, int i, SourceVariable v, boolean certain) {
1812+
predicate variableWrite(BasicBlock bb, int i, SourceVariable v, boolean certain) {
18131813
certain = false and
18141814
exists(GetsIteratorCall beginCall, Instruction writeToDeref, IRBlock bbQual, int iQual |
18151815
isIteratorStoreInstruction(beginCall, writeToDeref) and
@@ -1820,7 +1820,7 @@ module IteratorFlow {
18201820
}
18211821

18221822
/** Holds if `(bb, i)` reads the container variable `v`. */
1823-
predicate variableRead(IRBlock bb, int i, SourceVariable v, boolean certain) {
1823+
predicate variableRead(BasicBlock bb, int i, SourceVariable v, boolean certain) {
18241824
Ssa::variableRead(bb, i, v, certain)
18251825
}
18261826
}

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -981,7 +981,7 @@ private module SsaInput implements SsaImplCommon::InputSig<Location> {
981981
* Holds if the `i`'th write in block `bb` writes to the variable `v`.
982982
* `certain` is `true` if the write is guaranteed to overwrite the entire variable.
983983
*/
984-
predicate variableWrite(IRBlock bb, int i, SourceVariable v, boolean certain) {
984+
predicate variableWrite(BasicBlock bb, int i, SourceVariable v, boolean certain) {
985985
DataFlowImplCommon::forceCachingInSameStage() and
986986
(
987987
exists(DefImpl def | def.hasIndexInBlock(bb, i, v) |
@@ -999,7 +999,7 @@ private module SsaInput implements SsaImplCommon::InputSig<Location> {
999999
* Holds if the `i`'th read in block `bb` reads to the variable `v`.
10001000
* `certain` is `true` if the read is guaranteed. For C++, this is always the case.
10011001
*/
1002-
predicate variableRead(IRBlock bb, int i, SourceVariable v, boolean certain) {
1002+
predicate variableRead(BasicBlock bb, int i, SourceVariable v, boolean certain) {
10031003
exists(UseImpl use | use.hasIndexInBlock(bb, i, v) |
10041004
if use.isCertain() then certain = true else certain = false
10051005
)

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

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -757,13 +757,19 @@ import Cached
757757
* between the SSA pruning stage, and the final SSA stage.
758758
*/
759759
module InputSigCommon {
760-
class BasicBlock = IRBlock;
760+
class BasicBlock extends IRBlock {
761+
ControlFlowNode getNode(int i) { result = this.getInstruction(i) }
762+
763+
int length() { result = this.getInstructionCount() }
764+
}
765+
766+
class ControlFlowNode = Instruction;
761767

762768
BasicBlock getImmediateBasicBlockDominator(BasicBlock bb) { result.immediatelyDominates(bb) }
763769

764770
BasicBlock getABasicBlockSuccessor(BasicBlock bb) { result = bb.getASuccessor() }
765771

766-
class ExitBasicBlock extends IRBlock {
772+
class ExitBasicBlock extends BasicBlock {
767773
ExitBasicBlock() { this.getLastInstruction() instanceof ExitFunctionInstruction }
768774
}
769775
}

csharp/ql/lib/semmle/code/cil/Ssa.qll

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ deprecated module Ssa {
3535
}
3636

3737
/** Gets the location of this SSA definition. */
38-
Location getLocation() { result = this.getVariableUpdate().getLocation() }
38+
override Location getLocation() { result = this.getVariableUpdate().getLocation() }
3939
}
4040

4141
/** A phi node. */

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

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,17 @@
11
private import cil
2+
private import CIL
23
private import codeql.ssa.Ssa as SsaImplCommon
34

45
deprecated private module SsaInput implements SsaImplCommon::InputSig<CIL::Location> {
56
class BasicBlock = CIL::BasicBlock;
67

8+
class ControlFlowNode = CIL::ControlFlowNode;
9+
710
BasicBlock getImmediateBasicBlockDominator(BasicBlock bb) { result = bb.getImmediateDominator() }
811

912
BasicBlock getABasicBlockSuccessor(BasicBlock bb) { result = bb.getASuccessor() }
1013

11-
class ExitBasicBlock = CIL::ExitBasicBlock;
14+
class ExitBasicBlock extends BasicBlock, CIL::ExitBasicBlock { }
1215

1316
class SourceVariable = CIL::StackVariable;
1417

csharp/ql/lib/semmle/code/csharp/controlflow/internal/PreSsa.qll

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,11 @@ module PreSsa {
8080
}
8181

8282
module SsaInput implements SsaImplCommon::InputSig<Location> {
83-
class BasicBlock = PreBasicBlocks::PreBasicBlock;
83+
class BasicBlock extends PreBasicBlocks::PreBasicBlock {
84+
ControlFlowNode getNode(int i) { result = this.getElement(i) }
85+
}
86+
87+
class ControlFlowNode = ControlFlowElement;
8488

8589
BasicBlock getImmediateBasicBlockDominator(BasicBlock bb) { result.immediatelyDominates(bb) }
8690

@@ -192,7 +196,7 @@ module PreSsa {
192196
SsaImpl::ssaDefReachesEndOfBlock(bb, this, _)
193197
}
194198

195-
Location getLocation() {
199+
override Location getLocation() {
196200
result = this.getDefinition().getLocation()
197201
or
198202
exists(Callable c, SsaInput::BasicBlock bb, SsaInput::SourceVariable v |

csharp/ql/lib/semmle/code/csharp/dataflow/SSA.qll

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -427,7 +427,7 @@ module Ssa {
427427
}
428428

429429
/** Gets the location of this SSA definition. */
430-
Location getLocation() { none() }
430+
override Location getLocation() { none() }
431431
}
432432

433433
/**

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

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,13 +36,15 @@ module BaseSsa {
3636

3737
class BasicBlock = ControlFlow::BasicBlock;
3838

39+
class ControlFlowNode = ControlFlow::Node;
40+
3941
BasicBlock getImmediateBasicBlockDominator(BasicBlock bb) {
4042
result = bb.getImmediateDominator()
4143
}
4244

4345
BasicBlock getABasicBlockSuccessor(BasicBlock bb) { result = bb.getASuccessor() }
4446

45-
class ExitBasicBlock = ControlFlow::BasicBlocks::ExitBlock;
47+
class ExitBasicBlock extends BasicBlock, ControlFlow::BasicBlocks::ExitBlock { }
4648

4749
class SourceVariable = PreSsa::SimpleLocalScopeVariable;
4850

@@ -93,7 +95,7 @@ module BaseSsa {
9395
not result instanceof PhiNode
9496
}
9597

96-
Location getLocation() {
98+
override Location getLocation() {
9799
result = this.getDefinition().getLocation()
98100
or
99101
exists(Callable c, SsaInput::BasicBlock bb, SsaInput::SourceVariable v |

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

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -263,14 +263,16 @@ module VariableCapture {
263263

264264
private module CaptureInput implements Shared::InputSig<Location> {
265265
private import csharp as Cs
266-
private import semmle.code.csharp.controlflow.ControlFlowGraph
266+
private import semmle.code.csharp.controlflow.ControlFlowGraph as Cfg
267267
private import semmle.code.csharp.controlflow.BasicBlocks as BasicBlocks
268268
private import TaintTrackingPrivate as TaintTrackingPrivate
269269

270270
class BasicBlock extends BasicBlocks::BasicBlock {
271271
Callable getEnclosingCallable() { result = super.getCallable() }
272272
}
273273

274+
class ControlFlowNode = Cfg::ControlFlow::Node;
275+
274276
BasicBlock getImmediateBasicBlockDominator(BasicBlock bb) {
275277
result = bb.getImmediateDominator()
276278
}

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

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,13 @@ private import semmle.code.csharp.controlflow.internal.PreSsa
1010
private module SsaInput implements SsaImplCommon::InputSig<Location> {
1111
class BasicBlock = ControlFlow::BasicBlock;
1212

13+
class ControlFlowNode = ControlFlow::Node;
14+
1315
BasicBlock getImmediateBasicBlockDominator(BasicBlock bb) { result = bb.getImmediateDominator() }
1416

1517
BasicBlock getABasicBlockSuccessor(BasicBlock bb) { result = bb.getASuccessor() }
1618

17-
class ExitBasicBlock = ControlFlow::BasicBlocks::ExitBlock;
19+
class ExitBasicBlock extends BasicBlock, ControlFlow::BasicBlocks::ExitBlock { }
1820

1921
class SourceVariable = Ssa::SourceVariable;
2022

@@ -24,7 +26,7 @@ private module SsaInput implements SsaImplCommon::InputSig<Location> {
2426
*
2527
* This includes implicit writes via calls.
2628
*/
27-
predicate variableWrite(ControlFlow::BasicBlock bb, int i, Ssa::SourceVariable v, boolean certain) {
29+
predicate variableWrite(BasicBlock bb, int i, Ssa::SourceVariable v, boolean certain) {
2830
variableWriteDirect(bb, i, v, certain)
2931
or
3032
variableWriteQualifier(bb, i, v, certain)
@@ -38,7 +40,7 @@ private module SsaInput implements SsaImplCommon::InputSig<Location> {
3840
*
3941
* This includes implicit reads via calls.
4042
*/
41-
predicate variableRead(ControlFlow::BasicBlock bb, int i, Ssa::SourceVariable v, boolean certain) {
43+
predicate variableRead(BasicBlock bb, int i, Ssa::SourceVariable v, boolean certain) {
4244
variableReadActual(bb, i, v) and
4345
certain = true
4446
or
@@ -1089,7 +1091,7 @@ class DefinitionExt extends Impl::DefinitionExt {
10891091
override string toString() { result = this.(Ssa::Definition).toString() }
10901092

10911093
/** Gets the location of this definition. */
1092-
Location getLocation() { result = this.(Ssa::Definition).getLocation() }
1094+
override Location getLocation() { result = this.(Ssa::Definition).getLocation() }
10931095

10941096
/** Gets the enclosing callable of this definition. */
10951097
Callable getEnclosingCallable() { result = this.(Ssa::Definition).getEnclosingCallable() }

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

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,11 +72,17 @@ private module CaptureInput implements VariableCapture::InputSig<Location> {
7272
class BasicBlock instanceof J::BasicBlock {
7373
string toString() { result = super.toString() }
7474

75+
ControlFlowNode getNode(int i) { result = super.getNode(i) }
76+
77+
int length() { result = super.length() }
78+
7579
Callable getEnclosingCallable() { result = super.getEnclosingCallable() }
7680

7781
Location getLocation() { result = super.getLocation() }
7882
}
7983

84+
class ControlFlowNode = J::ControlFlowNode;
85+
8086
BasicBlock getImmediateBasicBlockDominator(BasicBlock bb) { bbIDominates(result, bb) }
8187

8288
BasicBlock getABasicBlockSuccessor(BasicBlock bb) {

python/ql/lib/semmle/python/dataflow/new/internal/VariableCapture.qll

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@ private module CaptureInput implements Shared::InputSig<Location> {
2424
}
2525

2626
class BasicBlock extends PY::BasicBlock {
27+
int length() { result = count(int i | exists(this.getNode(i))) }
28+
2729
Callable getEnclosingCallable() { result = this.getScope() }
2830

2931
// Note `PY:BasicBlock` does not have a `getLocation`.
@@ -34,6 +36,8 @@ private module CaptureInput implements Shared::InputSig<Location> {
3436
Location getLocation() { result = super.getNode(0).getLocation() }
3537
}
3638

39+
class ControlFlowNode = PY::ControlFlowNode;
40+
3741
BasicBlock getImmediateBasicBlockDominator(BasicBlock bb) { result = bb.getImmediateDominator() }
3842

3943
BasicBlock getABasicBlockSuccessor(BasicBlock bb) { result = bb.getASuccessor() }

ruby/ql/lib/codeql/ruby/dataflow/SSA.qll

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -176,9 +176,6 @@ module Ssa {
176176

177177
override string toString() { result = this.getControlFlowNode().toString() }
178178

179-
/** Gets the location of this SSA definition. */
180-
Location getLocation() { result = this.getControlFlowNode().getLocation() }
181-
182179
/** Gets the scope of this SSA definition. */
183180
CfgScope getScope() { result = this.getBasicBlock().getScope() }
184181
}

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

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -352,15 +352,16 @@ module VariableCapture {
352352
}
353353

354354
private module CaptureInput implements Shared::InputSig<Location> {
355-
private import ruby as R
356-
private import codeql.ruby.controlflow.ControlFlowGraph
355+
private import codeql.ruby.controlflow.ControlFlowGraph as Cfg
357356
private import codeql.ruby.controlflow.BasicBlocks as BasicBlocks
358357
private import TaintTrackingPrivate as TaintTrackingPrivate
359358

360359
class BasicBlock extends BasicBlocks::BasicBlock {
361360
Callable getEnclosingCallable() { result = this.getScope() }
362361
}
363362

363+
class ControlFlowNode = Cfg::CfgNode;
364+
364365
BasicBlock getImmediateBasicBlockDominator(BasicBlock bb) {
365366
result = bb.getImmediateDominator()
366367
}

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

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,15 +7,18 @@ private import codeql.ruby.ast.Variable
77
private import Cfg::CfgNodes::ExprNodes
88

99
private module SsaInput implements SsaImplCommon::InputSig<Location> {
10+
private import codeql.ruby.controlflow.ControlFlowGraph as Cfg
1011
private import codeql.ruby.controlflow.BasicBlocks as BasicBlocks
1112

1213
class BasicBlock = BasicBlocks::BasicBlock;
1314

15+
class ControlFlowNode = Cfg::CfgNode;
16+
1417
BasicBlock getImmediateBasicBlockDominator(BasicBlock bb) { result = bb.getImmediateDominator() }
1518

1619
BasicBlock getABasicBlockSuccessor(BasicBlock bb) { result = bb.getASuccessor() }
1720

18-
class ExitBasicBlock = BasicBlocks::ExitBasicBlock;
21+
class ExitBasicBlock extends BasicBlock, BasicBlocks::ExitBasicBlock { }
1922

2023
class SourceVariable = LocalVariable;
2124

@@ -494,8 +497,7 @@ class DefinitionExt extends Impl::DefinitionExt {
494497

495498
override string toString() { result = this.(Ssa::Definition).toString() }
496499

497-
/** Gets the location of this definition. */
498-
Location getLocation() { result = this.(Ssa::Definition).getLocation() }
500+
override Location getLocation() { result = this.(Ssa::Definition).getLocation() }
499501
}
500502

501503
/**
@@ -506,5 +508,5 @@ class DefinitionExt extends Impl::DefinitionExt {
506508
class PhiReadNode extends DefinitionExt, Impl::PhiReadNode {
507509
override string toString() { result = "SSA phi read(" + this.getSourceVariable() + ")" }
508510

509-
override Location getLocation() { result = this.getBasicBlock().getLocation() }
511+
override Location getLocation() { result = Impl::PhiReadNode.super.getLocation() }
510512
}

shared/dataflow/codeql/dataflow/VariableCapture.qll

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,13 +17,28 @@ signature module InputSig<LocationSig Location> {
1717
/** Gets a textual representation of this basic block. */
1818
string toString();
1919

20+
/** Gets the `i`th node in this basic block. */
21+
ControlFlowNode getNode(int i);
22+
23+
/** Gets the length of this basic block. */
24+
int length();
25+
2026
/** Gets the enclosing callable. */
2127
Callable getEnclosingCallable();
2228

2329
/** Gets the location of this basic block. */
2430
Location getLocation();
2531
}
2632

33+
/** A control flow node. */
34+
class ControlFlowNode {
35+
/** Gets a textual representation of this control flow node. */
36+
string toString();
37+
38+
/** Gets the location of this control flow node. */
39+
Location getLocation();
40+
}
41+
2742
/**
2843
* Gets the basic block that immediately dominates basic block `bb`, if any.
2944
*
@@ -672,6 +687,8 @@ module Flow<LocationSig Location, InputSig<Location> Input> implements OutputSig
672687
private module CaptureSsaInput implements Ssa::InputSig<Location> {
673688
final class BasicBlock = Input::BasicBlock;
674689

690+
final class ControlFlowNode = Input::ControlFlowNode;
691+
675692
BasicBlock getImmediateBasicBlockDominator(BasicBlock bb) {
676693
result = Input::getImmediateBasicBlockDominator(bb)
677694
}
@@ -717,10 +734,10 @@ module Flow<LocationSig Location, InputSig<Location> Input> implements OutputSig
717734
TSynthPhi(CaptureSsa::DefinitionExt phi) {
718735
phi instanceof CaptureSsa::PhiNode or phi instanceof CaptureSsa::PhiReadNode
719736
} or
720-
TExprNode(Expr expr, boolean isPost) {
721-
expr instanceof VariableRead and isPost = [false, true]
737+
TExprNode(Expr expr, Boolean isPost) {
738+
expr instanceof VariableRead
722739
or
723-
synthRead(_, _, _, _, expr) and isPost = [false, true]
740+
synthRead(_, _, _, _, expr)
724741
} or
725742
TParamNode(CapturedParameter p) or
726743
TThisParamNode(Callable c) { captureAccess(_, c) } or

0 commit comments

Comments
 (0)