Skip to content

Commit b57847f

Browse files
committed
C#: Include "phi reads" in DataFlow::Node
1 parent b8e1aa6 commit b57847f

File tree

10 files changed

+691
-342
lines changed

10 files changed

+691
-342
lines changed

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

+23
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,15 @@ private module Cached {
5555
)
5656
}
5757

58+
cached
59+
ReadAccess getAFirstReadExt(DefinitionExt def) {
60+
exists(BasicBlock bb1, int i1, BasicBlock bb2, int i2 |
61+
def.definesAt(_, bb1, i1, _) and
62+
adjacentDefReadExt(def, bb1, i1, bb2, i2) and
63+
result = bb2.getNode(i2)
64+
)
65+
}
66+
5867
cached
5968
predicate hasAdjacentReads(Definition def, ReadAccess first, ReadAccess second) {
6069
exists(BasicBlock bb1, int i1, BasicBlock bb2, int i2 |
@@ -64,13 +73,27 @@ private module Cached {
6473
)
6574
}
6675

76+
cached
77+
predicate hasAdjacentReadsExt(DefinitionExt def, ReadAccess first, ReadAccess second) {
78+
exists(BasicBlock bb1, int i1, BasicBlock bb2, int i2 |
79+
first = bb1.getNode(i1) and
80+
adjacentDefReadExt(def, bb1, i1, bb2, i2) and
81+
second = bb2.getNode(i2)
82+
)
83+
}
84+
6785
cached
6886
Definition getAPhiInput(PhiNode phi) { phiHasInputFromBlock(phi, result, _) }
6987

7088
cached
7189
predicate lastRefBeforeRedef(Definition def, BasicBlock bb, int i, Definition next) {
7290
lastRefRedef(def, bb, i, next)
7391
}
92+
93+
cached
94+
predicate lastRefBeforeRedefExt(DefinitionExt def, BasicBlock bb, int i, DefinitionExt next) {
95+
lastRefRedefExt(def, bb, i, next)
96+
}
7497
}
7598

7699
import Cached

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

+12-26
Original file line numberDiff line numberDiff line change
@@ -138,25 +138,6 @@ module Ssa {
138138
}
139139
}
140140

141-
private string getSplitString(Definition def) {
142-
exists(ControlFlow::BasicBlock bb, int i, ControlFlow::Node cfn |
143-
def.definesAt(_, bb, i) and
144-
result = cfn.(ControlFlow::Nodes::ElementNode).getSplitsString()
145-
|
146-
cfn = bb.getNode(i)
147-
or
148-
not exists(bb.getNode(i)) and
149-
cfn = bb.getFirstNode()
150-
)
151-
}
152-
153-
private string getToStringPrefix(Definition def) {
154-
result = "[" + getSplitString(def) + "] "
155-
or
156-
not exists(getSplitString(def)) and
157-
result = ""
158-
}
159-
160141
/**
161142
* A static single assignment (SSA) definition. Either an explicit variable
162143
* definition (`ExplicitDefinition`), an implicit variable definition
@@ -521,8 +502,8 @@ module Ssa {
521502

522503
override string toString() {
523504
if this.getADefinition() instanceof AssignableDefinitions::ImplicitParameterDefinition
524-
then result = getToStringPrefix(this) + "SSA param(" + this.getSourceVariable() + ")"
525-
else result = getToStringPrefix(this) + "SSA def(" + this.getSourceVariable() + ")"
505+
then result = SsaImpl::getToStringPrefix(this) + "SSA param(" + this.getSourceVariable() + ")"
506+
else result = SsaImpl::getToStringPrefix(this) + "SSA def(" + this.getSourceVariable() + ")"
526507
}
527508

528509
override Location getLocation() { result = ad.getLocation() }
@@ -570,8 +551,12 @@ module Ssa {
570551

571552
override string toString() {
572553
if this.getSourceVariable().getAssignable() instanceof LocalScopeVariable
573-
then result = getToStringPrefix(this) + "SSA capture def(" + this.getSourceVariable() + ")"
574-
else result = getToStringPrefix(this) + "SSA entry def(" + this.getSourceVariable() + ")"
554+
then
555+
result =
556+
SsaImpl::getToStringPrefix(this) + "SSA capture def(" + this.getSourceVariable() + ")"
557+
else
558+
result =
559+
SsaImpl::getToStringPrefix(this) + "SSA entry def(" + this.getSourceVariable() + ")"
575560
}
576561

577562
override Location getLocation() { result = this.getCallable().getLocation() }
@@ -612,7 +597,7 @@ module Ssa {
612597
}
613598

614599
override string toString() {
615-
result = getToStringPrefix(this) + "SSA call def(" + this.getSourceVariable() + ")"
600+
result = SsaImpl::getToStringPrefix(this) + "SSA call def(" + this.getSourceVariable() + ")"
616601
}
617602

618603
override Location getLocation() { result = this.getCall().getLocation() }
@@ -640,7 +625,8 @@ module Ssa {
640625
final Definition getQualifierDefinition() { result = q }
641626

642627
override string toString() {
643-
result = getToStringPrefix(this) + "SSA qualifier def(" + this.getSourceVariable() + ")"
628+
result =
629+
SsaImpl::getToStringPrefix(this) + "SSA qualifier def(" + this.getSourceVariable() + ")"
644630
}
645631

646632
override Location getLocation() { result = this.getQualifierDefinition().getLocation() }
@@ -682,7 +668,7 @@ module Ssa {
682668
}
683669

684670
override string toString() {
685-
result = getToStringPrefix(this) + "SSA phi(" + this.getSourceVariable() + ")"
671+
result = SsaImpl::getToStringPrefix(this) + "SSA phi(" + this.getSourceVariable() + ")"
686672
}
687673

688674
/*

0 commit comments

Comments
 (0)