Skip to content

Commit 012ea4b

Browse files
authored
Merge pull request github#18113 from hvitved/dataflow/more-common
Data flow: Move more logic into `DataFlowImplCommon`
2 parents 2e9f857 + 7f9adbd commit 012ea4b

File tree

5 files changed

+152
-104
lines changed

5 files changed

+152
-104
lines changed

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -545,7 +545,7 @@ module ProductFlow {
545545
private predicate outImpl1(Flow1::PathNode pred1, Flow1::PathNode succ1, DataFlowCall call) {
546546
Flow1::PathGraph::edges(pred1, succ1, _, _) and
547547
exists(ReturnKindExt returnKind |
548-
succ1.getNode() = returnKind.getAnOutNode(call) and
548+
succ1.getNode() = getAnOutNodeExt(call, returnKind) and
549549
returnKind = getParamReturnPosition(_, pred1.asParameterReturnNode()).getKind()
550550
)
551551
}
@@ -573,7 +573,7 @@ module ProductFlow {
573573
private predicate outImpl2(Flow2::PathNode pred2, Flow2::PathNode succ2, DataFlowCall call) {
574574
Flow2::PathGraph::edges(pred2, succ2, _, _) and
575575
exists(ReturnKindExt returnKind |
576-
succ2.getNode() = returnKind.getAnOutNode(call) and
576+
succ2.getNode() = getAnOutNodeExt(call, returnKind) and
577577
returnKind = getParamReturnPosition(_, pred2.asParameterReturnNode()).getKind()
578578
)
579579
}

shared/dataflow/codeql/dataflow/internal/DataFlowImpl.qll

Lines changed: 30 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -343,7 +343,7 @@ module MakeImpl<LocationSig Location, InputSig<Location> Lang> {
343343
bindingset[n, cc]
344344
pragma[inline_late]
345345
private predicate isUnreachableInCall1(NodeEx n, LocalCallContextSpecificCall cc) {
346-
cc.unreachable(n.asNode())
346+
cc.unreachable(n)
347347
}
348348

349349
/**
@@ -423,7 +423,7 @@ module MakeImpl<LocationSig Location, InputSig<Location> Lang> {
423423

424424
pragma[nomagic]
425425
private predicate readSetEx(NodeEx node1, ContentSet c, NodeEx node2) {
426-
readSet(pragma[only_bind_into](node1.asNode()), c, pragma[only_bind_into](node2.asNode())) and
426+
readEx(node1, c, node2) and
427427
stepFilter(node1, node2)
428428
or
429429
exists(Node n |
@@ -450,44 +450,33 @@ module MakeImpl<LocationSig Location, InputSig<Location> Lang> {
450450
bindingset[c]
451451
private predicate expectsContentEx(NodeEx n, Content c) {
452452
exists(ContentSet cs |
453-
expectsContentCached(n.asNode(), cs) and
453+
expectsContentSet(n, cs) and
454454
pragma[only_bind_out](c) = pragma[only_bind_into](cs).getAReadContent()
455455
)
456456
}
457457

458458
pragma[nomagic]
459-
private predicate notExpectsContent(NodeEx n) { not expectsContentCached(n.asNode(), _) }
459+
private predicate notExpectsContent(NodeEx n) { not expectsContentSet(n, _) }
460460

461461
pragma[nomagic]
462-
private predicate storeExUnrestricted(
462+
private predicate storeUnrestricted(
463463
NodeEx node1, Content c, NodeEx node2, DataFlowType contentType, DataFlowType containerType
464464
) {
465-
store(pragma[only_bind_into](node1.asNode()), c, pragma[only_bind_into](node2.asNode()),
466-
contentType, containerType) and
465+
storeEx(node1, c, node2, contentType, containerType) and
467466
stepFilter(node1, node2)
468467
}
469468

470469
pragma[nomagic]
471470
private predicate hasReadStep(Content c) { read(_, c, _) }
472471

473472
pragma[nomagic]
474-
private predicate storeEx(
473+
private predicate store(
475474
NodeEx node1, Content c, NodeEx node2, DataFlowType contentType, DataFlowType containerType
476475
) {
477-
storeExUnrestricted(node1, c, node2, contentType, containerType) and
476+
storeUnrestricted(node1, c, node2, contentType, containerType) and
478477
hasReadStep(c)
479478
}
480479

481-
pragma[nomagic]
482-
private predicate viableReturnPosOutEx(DataFlowCall call, ReturnPosition pos, NodeEx out) {
483-
viableReturnPosOut(call, pos, out.asNode())
484-
}
485-
486-
pragma[nomagic]
487-
private predicate viableParamArgEx(DataFlowCall call, ParamNodeEx p, ArgNodeEx arg) {
488-
viableParamArg(call, p.asNode(), arg.asNode())
489-
}
490-
491480
/**
492481
* Holds if field flow should be used for the given configuration.
493482
*/
@@ -520,7 +509,7 @@ module MakeImpl<LocationSig Location, InputSig<Location> Lang> {
520509
exists(ParameterPosition pos | p.isParameterOf(_, pos) |
521510
not kind.(ParamUpdateReturnKind).getPosition() = pos
522511
or
523-
allowParameterReturnInSelfCached(p.asNode())
512+
allowParameterReturnInSelfEx(p)
524513
)
525514
}
526515

@@ -558,7 +547,7 @@ module MakeImpl<LocationSig Location, InputSig<Location> Lang> {
558547
exists(NodeEx mid |
559548
useFieldFlow() and
560549
fwdFlow(mid, cc) and
561-
storeEx(mid, _, node, _, _)
550+
store(mid, _, node, _, _)
562551
)
563552
or
564553
// read
@@ -653,7 +642,7 @@ module MakeImpl<LocationSig Location, InputSig<Location> Lang> {
653642
not fullBarrier(node) and
654643
useFieldFlow() and
655644
fwdFlow(mid, _) and
656-
storeEx(mid, c, node, _, _)
645+
store(mid, c, node, _, _)
657646
)
658647
}
659648

@@ -796,7 +785,7 @@ module MakeImpl<LocationSig Location, InputSig<Location> Lang> {
796785
exists(NodeEx mid |
797786
revFlow(mid, toReturn) and
798787
fwdFlowConsCand(c) and
799-
storeEx(node, c, mid, _, _)
788+
store(node, c, mid, _, _)
800789
)
801790
}
802791

@@ -893,7 +882,7 @@ module MakeImpl<LocationSig Location, InputSig<Location> Lang> {
893882
) {
894883
revFlowIsReadAndStored(c) and
895884
revFlow(node2) and
896-
storeEx(node1, c, node2, contentType, containerType) and
885+
store(node1, c, node2, contentType, containerType) and
897886
exists(ap1)
898887
}
899888

@@ -1152,7 +1141,7 @@ module MakeImpl<LocationSig Location, InputSig<Location> Lang> {
11521141
flowOutOfCallNodeCand1(call, ret, _, out) and
11531142
c = ret.getEnclosingCallable()
11541143
|
1155-
scope = getSecondLevelScopeCached(ret.asNode())
1144+
scope = getSecondLevelScopeEx(ret)
11561145
or
11571146
ret = TParamReturnNode(_, scope)
11581147
)
@@ -1496,7 +1485,7 @@ module MakeImpl<LocationSig Location, InputSig<Location> Lang> {
14961485
PrevStage::revFlow(node, state, apa) and
14971486
filter(node, state, t0, ap, t) and
14981487
(
1499-
if castingNodeEx(node)
1488+
if node instanceof CastingNodeEx
15001489
then
15011490
ap instanceof ApNil or
15021491
compatibleContainer(getHeadContent(ap), node.getDataFlowType()) or
@@ -2627,10 +2616,7 @@ module MakeImpl<LocationSig Location, InputSig<Location> Lang> {
26272616
FlowCheckNode() {
26282617
revFlow(this, _, _) and
26292618
(
2630-
castNode(this.asNode()) or
2631-
clearsContentCached(this.asNode(), _) or
2632-
expectsContentCached(this.asNode(), _) or
2633-
neverSkipInPathGraph(this.asNode()) or
2619+
flowCheckNode(this) or
26342620
Config::neverSkip(this.asNode())
26352621
)
26362622
}
@@ -2665,7 +2651,7 @@ module MakeImpl<LocationSig Location, InputSig<Location> Lang> {
26652651
or
26662652
node instanceof ParamNodeEx
26672653
or
2668-
node.asNode() instanceof OutNodeExt
2654+
node instanceof OutNodeEx
26692655
or
26702656
storeStepCand(_, _, _, node, _, _)
26712657
or
@@ -2899,15 +2885,9 @@ module MakeImpl<LocationSig Location, InputSig<Location> Lang> {
28992885

29002886
predicate isHidden() {
29012887
not Config::includeHiddenNodes() and
2902-
(
2903-
hiddenNode(this.getNodeEx().asNode()) and
2904-
not this.isSource() and
2905-
not this instanceof PathNodeSink
2906-
or
2907-
this.getNodeEx() instanceof TNodeImplicitRead
2908-
or
2909-
hiddenNode(this.getNodeEx().asParamReturnNode())
2910-
)
2888+
hiddenNode(this.getNodeEx()) and
2889+
not this.isSource() and
2890+
not this instanceof PathNodeSink
29112891
}
29122892

29132893
/** Gets a textual representation of this element. */
@@ -3770,11 +3750,6 @@ module MakeImpl<LocationSig Location, InputSig<Location> Lang> {
37703750

37713751
private module Stage2 = MkStage<Stage1>::Stage<Stage2Param>;
37723752

3773-
pragma[nomagic]
3774-
private predicate castingNodeEx(NodeEx node) {
3775-
node.asNode() instanceof CastingNode or exists(node.asParamReturnNode())
3776-
}
3777-
37783753
private module Stage3Param implements MkStage<Stage2>::StageParam {
37793754
private module PrevStage = Stage2;
37803755

@@ -3888,7 +3863,7 @@ module MakeImpl<LocationSig Location, InputSig<Location> Lang> {
38883863

38893864
bindingset[node, t0]
38903865
private predicate strengthenType(NodeEx node, DataFlowType t0, DataFlowType t) {
3891-
if castingNodeEx(node)
3866+
if node instanceof CastingNodeEx
38923867
then
38933868
exists(DataFlowType nt | nt = node.getDataFlowType() |
38943869
if typeStrongerThanFilter(nt, t0)
@@ -3945,7 +3920,7 @@ module MakeImpl<LocationSig Location, InputSig<Location> Lang> {
39453920
pragma[nomagic]
39463921
private predicate clearSet(NodeEx node, ContentSet c) {
39473922
PrevStage::revFlow(node) and
3948-
clearsContentCached(node.asNode(), c)
3923+
clearsContentSet(node, c)
39493924
}
39503925

39513926
pragma[nomagic]
@@ -5024,7 +4999,7 @@ module MakeImpl<LocationSig Location, InputSig<Location> Lang> {
50244999
bindingset[c]
50255000
private predicate clearsContentEx(NodeEx n, Content c) {
50265001
exists(ContentSet cs |
5027-
clearsContentCached(n.asNode(), cs) and
5002+
clearsContentSet(n, cs) and
50285003
pragma[only_bind_out](c) = pragma[only_bind_into](cs).getAReadContent()
50295004
)
50305005
}
@@ -5377,7 +5352,7 @@ module MakeImpl<LocationSig Location, InputSig<Location> Lang> {
53775352
midNode = mid.getNodeEx() and
53785353
t1 = mid.getType() and
53795354
ap1 = mid.getAp() and
5380-
storeExUnrestricted(midNode, c, node, contentType, t2) and
5355+
storeUnrestricted(midNode, c, node, contentType, t2) and
53815356
ap2.getHead() = c and
53825357
ap2.len() = unbindInt(ap1.len() + 1) and
53835358
compatibleTypesFilter(t1, contentType)
@@ -5442,9 +5417,8 @@ module MakeImpl<LocationSig Location, InputSig<Location> Lang> {
54425417
PartialAccessPath ap
54435418
) {
54445419
exists(ReturnKindExt kind, DataFlowCall call |
5445-
partialPathOutOfCallable1(mid, call, kind, state, cc, t, ap)
5446-
|
5447-
out.asNode() = kind.getAnOutNode(call)
5420+
partialPathOutOfCallable1(mid, call, kind, state, cc, t, ap) and
5421+
out = kind.getAnOutNodeEx(call)
54485422
)
54495423
}
54505424

@@ -5529,7 +5503,7 @@ module MakeImpl<LocationSig Location, InputSig<Location> Lang> {
55295503
) {
55305504
exists(DataFlowCall call, ReturnKindExt kind |
55315505
partialPathThroughCallable0(call, mid, kind, state, cc, t, ap) and
5532-
out.asNode() = kind.getAnOutNode(call)
5506+
out = kind.getAnOutNodeEx(call)
55335507
)
55345508
}
55355509

@@ -5549,7 +5523,7 @@ module MakeImpl<LocationSig Location, InputSig<Location> Lang> {
55495523
not outBarrier(node, state) and
55505524
// if a node is not the target of a store, we can check `clearsContent` immediately
55515525
(
5552-
storeExUnrestricted(_, _, node, _, _)
5526+
storeUnrestricted(_, _, node, _, _)
55535527
or
55545528
not clearsContentEx(node, ap.getHead())
55555529
)
@@ -5690,7 +5664,7 @@ module MakeImpl<LocationSig Location, InputSig<Location> Lang> {
56905664
exists(NodeEx midNode |
56915665
midNode = mid.getNodeEx() and
56925666
ap = mid.getAp() and
5693-
storeExUnrestricted(node, c, midNode, _, _) and
5667+
storeUnrestricted(node, c, midNode, _, _) and
56945668
ap.getHead() = c
56955669
)
56965670
}
@@ -5745,7 +5719,7 @@ module MakeImpl<LocationSig Location, InputSig<Location> Lang> {
57455719
) {
57465720
exists(DataFlowCall call, ArgumentPosition pos |
57475721
revPartialPathThroughCallable0(call, mid, pos, state, ap) and
5748-
node.asNode().(ArgNode).argumentOf(call, pos)
5722+
node.argumentOf(call, pos)
57495723
)
57505724
}
57515725

0 commit comments

Comments
 (0)