Skip to content

Commit 1a56f0b

Browse files
committed
Data flow: Cache getNodeType
1 parent 044c920 commit 1a56f0b

File tree

2 files changed

+42
-34
lines changed

2 files changed

+42
-34
lines changed

csharp/ql/src/semmle/code/csharp/dataflow/internal/DataFlowImpl.qll

Lines changed: 15 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1327,11 +1327,11 @@ private module LocalFlowBigStep {
13271327
(
13281328
localFlowStepNodeCand1(node1, node2, config) and
13291329
preservesValue = true and
1330-
t = getNodeType(node1)
1330+
t = getNodeDataFlowType(node1)
13311331
or
13321332
additionalLocalFlowStepNodeCand2(node1, node2, config) and
13331333
preservesValue = false and
1334-
t = getNodeType(node2)
1334+
t = getNodeDataFlowType(node2)
13351335
) and
13361336
node1 != node2 and
13371337
cc.relevantFor(getNodeEnclosingCallable(node1)) and
@@ -1350,7 +1350,7 @@ private module LocalFlowBigStep {
13501350
additionalLocalFlowStepNodeCand2(mid, node2, config) and
13511351
not mid instanceof FlowCheckNode and
13521352
preservesValue = false and
1353-
t = getNodeType(node2) and
1353+
t = getNodeDataFlowType(node2) and
13541354
Stage2::revFlow(node2, pragma[only_bind_into](config))
13551355
)
13561356
)
@@ -1384,7 +1384,7 @@ private module Stage3 {
13841384
private ApApprox getApprox(Ap ap) { result = ap.toBoolNonEmpty() }
13851385

13861386
private ApNil getApNil(Node node) {
1387-
PrevStage::revFlow(node, _) and result = TFrontNil(getNodeType(node))
1387+
PrevStage::revFlow(node, _) and result = TFrontNil(getNodeDataFlowType(node))
13881388
}
13891389

13901390
bindingset[tc, tail]
@@ -1443,7 +1443,9 @@ private module Stage3 {
14431443
bindingset[node, ap]
14441444
private predicate filter(Node node, Ap ap) {
14451445
not ap.isClearedAt(node) and
1446-
if node instanceof CastingNode then compatibleTypes(getNodeType(node), ap.getType()) else any()
1446+
if node instanceof CastingNode
1447+
then compatibleTypes(getNodeDataFlowType(node), ap.getType())
1448+
else any()
14471449
}
14481450

14491451
bindingset[ap, contentType]
@@ -2088,7 +2090,7 @@ private module Stage4 {
20882090
private ApApprox getApprox(Ap ap) { result = ap.getFront() }
20892091

20902092
private ApNil getApNil(Node node) {
2091-
PrevStage::revFlow(node, _) and result = TNil(getNodeType(node))
2093+
PrevStage::revFlow(node, _) and result = TNil(getNodeDataFlowType(node))
20922094
}
20932095

20942096
bindingset[tc, tail]
@@ -2758,7 +2760,7 @@ private newtype TPathNode =
27582760
config.isSource(node) and
27592761
cc instanceof CallContextAny and
27602762
sc instanceof SummaryCtxNone and
2761-
ap = TAccessPathNil(getNodeType(node))
2763+
ap = TAccessPathNil(getNodeDataFlowType(node))
27622764
or
27632765
// ... or a step from an existing PathNode to another node.
27642766
exists(PathNodeMid mid |
@@ -3148,7 +3150,7 @@ private predicate pathStep(PathNodeMid mid, Node node, CallContext cc, SummaryCt
31483150
cc instanceof CallContextAny and
31493151
sc instanceof SummaryCtxNone and
31503152
mid.getAp() instanceof AccessPathNil and
3151-
ap = TAccessPathNil(getNodeType(node))
3153+
ap = TAccessPathNil(getNodeDataFlowType(node))
31523154
or
31533155
exists(TypedContent tc | pathStoreStep(mid, node, ap.pop(tc), tc, cc)) and
31543156
sc = mid.getSummaryCtx()
@@ -3591,7 +3593,7 @@ private module FlowExploration {
35913593
cc instanceof CallContextAny and
35923594
sc1 = TSummaryCtx1None() and
35933595
sc2 = TSummaryCtx2None() and
3594-
ap = TPartialNil(getNodeType(node)) and
3596+
ap = TPartialNil(getNodeDataFlowType(node)) and
35953597
not fullBarrier(node, config) and
35963598
exists(config.explorationLimit())
35973599
or
@@ -3627,7 +3629,7 @@ private module FlowExploration {
36273629
not fullBarrier(node, config) and
36283630
not clearsContent(node, ap.getHead().getContent()) and
36293631
if node instanceof CastingNode
3630-
then compatibleTypes(getNodeType(node), ap.getType())
3632+
then compatibleTypes(getNodeDataFlowType(node), ap.getType())
36313633
else any()
36323634
)
36333635
}
@@ -3797,7 +3799,7 @@ private module FlowExploration {
37973799
sc1 = mid.getSummaryCtx1() and
37983800
sc2 = mid.getSummaryCtx2() and
37993801
mid.getAp() instanceof PartialAccessPathNil and
3800-
ap = TPartialNil(getNodeType(node)) and
3802+
ap = TPartialNil(getNodeDataFlowType(node)) and
38013803
config = mid.getConfiguration()
38023804
)
38033805
or
@@ -3813,7 +3815,7 @@ private module FlowExploration {
38133815
sc1 = TSummaryCtx1None() and
38143816
sc2 = TSummaryCtx2None() and
38153817
mid.getAp() instanceof PartialAccessPathNil and
3816-
ap = TPartialNil(getNodeType(node)) and
3818+
ap = TPartialNil(getNodeDataFlowType(node)) and
38173819
config = mid.getConfiguration()
38183820
or
38193821
partialPathStoreStep(mid, _, _, node, ap) and
@@ -3827,7 +3829,7 @@ private module FlowExploration {
38273829
sc1 = mid.getSummaryCtx1() and
38283830
sc2 = mid.getSummaryCtx2() and
38293831
apConsFwd(ap, tc, ap0, config) and
3830-
compatibleTypes(ap.getType(), getNodeType(node))
3832+
compatibleTypes(ap.getType(), getNodeDataFlowType(node))
38313833
)
38323834
or
38333835
partialPathIntoCallable(mid, node, _, cc, sc1, sc2, _, ap, config)

csharp/ql/src/semmle/code/csharp/dataflow/internal/DataFlowImplCommon.qll

Lines changed: 27 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,7 @@ private module LambdaFlow {
119119
) {
120120
revLambdaFlow0(lambdaCall, kind, node, t, toReturn, toJump, lastCall) and
121121
if node instanceof CastNode or node instanceof ArgumentNode or node instanceof ReturnNode
122-
then compatibleTypes(t, getNodeType(node))
122+
then compatibleTypes(t, getNodeDataFlowType(node))
123123
else any()
124124
}
125125

@@ -129,7 +129,7 @@ private module LambdaFlow {
129129
boolean toJump, DataFlowCallOption lastCall
130130
) {
131131
lambdaCall(lambdaCall, kind, node) and
132-
t = getNodeType(node) and
132+
t = getNodeDataFlowType(node) and
133133
toReturn = false and
134134
toJump = false and
135135
lastCall = TDataFlowCallNone()
@@ -146,7 +146,7 @@ private module LambdaFlow {
146146
getNodeEnclosingCallable(node) = getNodeEnclosingCallable(mid)
147147
|
148148
preservesValue = false and
149-
t = getNodeType(node)
149+
t = getNodeDataFlowType(node)
150150
or
151151
preservesValue = true and
152152
t = t0
@@ -168,7 +168,7 @@ private module LambdaFlow {
168168
getNodeEnclosingCallable(node) != getNodeEnclosingCallable(mid)
169169
|
170170
preservesValue = false and
171-
t = getNodeType(node)
171+
t = getNodeDataFlowType(node)
172172
or
173173
preservesValue = true and
174174
t = t0
@@ -250,6 +250,9 @@ private module Cached {
250250
c = call.getEnclosingCallable()
251251
}
252252

253+
cached
254+
predicate nodeDataFlowType(Node n, DataFlowType t) { t = getNodeType(n) }
255+
253256
/**
254257
* Gets a viable target for the lambda call `call`.
255258
*
@@ -282,7 +285,7 @@ private module Cached {
282285
exists(int i |
283286
viableParam(call, i, p) and
284287
arg.argumentOf(call, i) and
285-
compatibleTypes(getNodeType(arg), getNodeType(p))
288+
compatibleTypes(getNodeDataFlowType(arg), getNodeDataFlowType(p))
286289
)
287290
}
288291

@@ -430,10 +433,10 @@ private module Cached {
430433
then
431434
// normal flow through
432435
read = TReadStepTypesNone() and
433-
compatibleTypes(getNodeType(p), getNodeType(node))
436+
compatibleTypes(getNodeDataFlowType(p), getNodeDataFlowType(node))
434437
or
435438
// getter
436-
compatibleTypes(read.getContentType(), getNodeType(node))
439+
compatibleTypes(read.getContentType(), getNodeDataFlowType(node))
437440
else any()
438441
}
439442

@@ -455,7 +458,7 @@ private module Cached {
455458
readStepWithTypes(mid, read.getContainerType(), read.getContent(), node,
456459
read.getContentType()) and
457460
Cand::parameterValueFlowReturnCand(p, _, true) and
458-
compatibleTypes(getNodeType(p), read.getContainerType())
461+
compatibleTypes(getNodeDataFlowType(p), read.getContainerType())
459462
)
460463
or
461464
parameterValueFlow0_0(TReadStepTypesNone(), p, node, read)
@@ -511,11 +514,11 @@ private module Cached {
511514
|
512515
// normal flow through
513516
read = TReadStepTypesNone() and
514-
compatibleTypes(getNodeType(arg), getNodeType(out))
517+
compatibleTypes(getNodeDataFlowType(arg), getNodeDataFlowType(out))
515518
or
516519
// getter
517-
compatibleTypes(getNodeType(arg), read.getContainerType()) and
518-
compatibleTypes(read.getContentType(), getNodeType(out))
520+
compatibleTypes(getNodeDataFlowType(arg), read.getContainerType()) and
521+
compatibleTypes(read.getContentType(), getNodeDataFlowType(out))
519522
)
520523
}
521524

@@ -653,8 +656,8 @@ private module Cached {
653656
) {
654657
storeStep(node1, c, node2) and
655658
readStep(_, c, _) and
656-
contentType = getNodeType(node1) and
657-
containerType = getNodeType(node2)
659+
contentType = getNodeDataFlowType(node1) and
660+
containerType = getNodeDataFlowType(node2)
658661
or
659662
exists(Node n1, Node n2 |
660663
n1 = node1.(PostUpdateNode).getPreUpdateNode() and
@@ -663,8 +666,8 @@ private module Cached {
663666
argumentValueFlowsThrough(n2, TReadStepTypesSome(containerType, c, contentType), n1)
664667
or
665668
readStep(n2, c, n1) and
666-
contentType = getNodeType(n1) and
667-
containerType = getNodeType(n2)
669+
contentType = getNodeDataFlowType(n1) and
670+
containerType = getNodeDataFlowType(n2)
668671
)
669672
}
670673

@@ -784,8 +787,8 @@ private predicate readStepWithTypes(
784787
Node n1, DataFlowType container, Content c, Node n2, DataFlowType content
785788
) {
786789
readStep(n1, c, n2) and
787-
container = getNodeType(n1) and
788-
content = getNodeType(n2)
790+
container = getNodeDataFlowType(n1) and
791+
content = getNodeDataFlowType(n2)
789792
}
790793

791794
private newtype TReadStepTypesOption =
@@ -1023,10 +1026,13 @@ class ReturnPosition extends TReturnPosition0 {
10231026
*/
10241027
pragma[inline]
10251028
DataFlowCallable getNodeEnclosingCallable(Node n) {
1026-
exists(Node n0 |
1027-
pragma[only_bind_into](n0) = n and
1028-
nodeEnclosingCallable(n0, pragma[only_bind_into](result))
1029-
)
1029+
nodeEnclosingCallable(pragma[only_bind_out](n), pragma[only_bind_into](result))
1030+
}
1031+
1032+
/** Gets the type of `n` used for type pruning. */
1033+
pragma[inline]
1034+
DataFlowType getNodeDataFlowType(Node n) {
1035+
nodeDataFlowType(pragma[only_bind_out](n), pragma[only_bind_into](result))
10301036
}
10311037

10321038
pragma[noinline]

0 commit comments

Comments
 (0)